1

I want to activate the TAB key from within an elisp function.

here is my function:

(defun aline ()
    "We insert a pair of dollar signs and position
    point in between them."
    (interactive)
        (progn
            (insert "\\begin{align*}\n\\ \n  \\end{align*}")
            (kbd "TAB")
            (backward-char 16)
        )
)

Its desired behaviour is to print out:

\begin{align*}
\ <cursor>
\end{align*} 

Unfortunately it indents the \end{align*}

\begin{align*}
\ 
  \end{align*}

This is fixed in AUCTEX by pressing TAB. How can I get my function to press the TAB key?

I realise I could fix it by removing spaces after "\n"

(insert "\\begin{align*}\n\\ \n\\end{align*}")

but I want to learn e-lisp and I'm curious of how to fix this with an automated TAB press

I have tried the following commands:

(kbd "TAB")

(insert "?\^I")

(tab-char)

  • 3
    You can find what function is bound to a key by typing <kbd>F1 k</kbd> and then the key you're interested in. Calling a function by the key it's bound to is possible (not in the way you tried) but it isn't a way to write reliable code. – DoMiNeLa10 Jul 16 '17 at 15:34
  • 3
    Beginners frequently look for ways to have Emacs press certain keys because they have not yet learned how to determine what function is attached to a particular key using features such as M-x describe-key or C-h k, and then how to include that particular function (with the appropriate arguments) in the new function to achieve the desired result. You can read about function arguments by typing M-x describe-function or C-h f. Certain functions also behave differently depending upon whether they are called interactively, or non-interactively. You may want to use call-interactively. – lawlist Jul 16 '17 at 15:39
2

I suggest

(execute-kbd-macro [?\t])

to activate the tab key.

  • That would do different things in different modes - it's also rather obscure even if you only want to execute it in a single mode. It's better IMO to find out the function that the key is bound to and call that. – NickD Jul 17 '17 at 2:30
  • I agree to be careful when to use this call. OTOH this call is an option. It could be useful for writing tests, I guess. – Marco Wahl Jul 17 '17 at 8:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.