Make C-M-RET insert \item[]
-
26-10-2019 - |
Question
In AUCTeX M-RET is bound to (LaTeX-insert-item)
which makes a new properly indented line and inserts \item
and a space after which the cursor is placed. I would like to extend this feature so that C-M-RET has similar functionality but for \item[description]
.
What I would like is for C-M-RET to
- make a new properly indented line and insert
\item[]
, - place the cursor between the square brackets, and
- place the cursor one space after
\item[]
as Tab is pressed.
Solution
The following function is cribbed from latex.el, and modified slightly to call the argument-prompting version LaTeX-item-argument
instead of just inserting the item directly.
(defun LaTeX-insert-item-arg ()
"Insert a new item in an environment, prompting for an item label.
You may use `LaTeX-item-list' to change the routines used to insert the item."
(interactive "*")
(let ((environment (LaTeX-current-environment)))
(when (and (TeX-active-mark)
(> (point) (mark)))
(exchange-point-and-mark))
(unless (bolp) (LaTeX-newline))
(if (assoc environment LaTeX-item-list)
(funcall (cdr (assoc environment LaTeX-item-list)))
(LaTeX-item-argument)) ;; ONLY THIS LINE IS DIFFERENT
(indent-according-to-mode)))
You can bind that function to any key you like:
(add-hook 'LaTeX-mode-hook (lambda ()
(local-set-key [(control return)] 'LaTeX-insert-item-arg)))
If you want M-C-RET
, use (meta control return)
instead, though it only seems to work with the Alt
key, and not the Esc
key (which usually behaves the same...)
OTHER TIPS
It sounds like you want Textmate snippets behavior.
You need yasnippet to do the snippet expansion/field movement. IMO, it's not a good solution to bind this to a key because the number of keys is limited, but yasnippet does allow you to do that as well.