0

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.
N.N.
  • 8,336
  • 12
  • 54
  • 94
  • Auctex knows when you're in a description environment, and modifies the behaviour of `M-RET` automatically. Is this not working of you, or are you using \item[description] in some other context? – Tyler Oct 19 '11 at 20:23

2 Answers2

2

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...)

Ben Lerner
  • 184
  • 4
1

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.

event_jr
  • 17,467
  • 4
  • 47
  • 62
  • `(LaTeX-insert-item)` is aware of LaTeX environments so that it indents properly. That's why I want similar functionality but for `\item[]`. I don't know how to make yasnippet aware of LaTeX environemnts. Also, `C-M-RET` and `C-RET` are not bound in AUCTeX. – N.N. Oct 19 '11 at 12:41
  • I don't work with LaTeX, but yasnippet is able to indent snippets in major-modes where indentation is well defined, i.e. TAB indents correctly. If you want the nice smooth field filling experience, I would look to yasnippet as a starting point. – event_jr Oct 19 '11 at 14:54