Search code examples
emacskeyboard-shortcutsauctex

Make C-M-RET insert \item[]


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

  1. make a new properly indented line and insert \item[],
  2. place the cursor between the square brackets, and
  3. 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...)