2013-06-02 Making TAB jump out of the group (en)

I like Yasnippet. I like especially how you can say \frac{$1}{$2}$0 in a snippet file, and then have TAB jump from $1 to $2 to $0. I like it so much that I decided to make my TAB jump like that even when I’m not filling in a snippet.

;;; TAB jumps yasnippet-like to "next field" or after environment in LaTeX or ConTeXt

(defcustom TeX-jumping-tab-regex-list
  '("[]}][[{]?" ; closing brace/bracket with optional opening one
    "\n *\\\\end{.*?}\n? *" ; end of a LaTeX environment
    "\n *\\\\\\(stop\\|end\\)[a-z]+\n? *" ; end of ConTeXt startstop or block
    "\n *\\\\stop\\[[a-z]+\\]\n? *" ; end of alternative ConTeXt startstop syntax
    )
  "A list of regexen for TeX-jumping-tab command.  If looking-at
  one of these, TAB will jump to its end.")

(defun TeX-jumping-tab ()
  "If point is not at the beginning of line and is looking-at one
of the elements of jumping-tab-regex-list, move to its end.  If
not, call indent-for-tab-command."
  (interactive)
  (let ((found nil)
	(regex-list (if (not (bolp)) TeX-jumping-tab-regex-list))) ; if at beg-of-line, just indent!
    (while (and regex-list (not found))
      (if (not (looking-at (car regex-list)))
	  (setq regex-list (cdr regex-list))
	(setq found t)
	(goto-char (match-end 0))))
    (unless found (call-interactively 'indent-for-tab-command))))

(eval-after-load 'tex '(define-key TeX-mode-map "\t" 'TeX-jumping-tab))

I’m not really sure whether this code is Lispy, but it works - and I couldn’t devise a simple way to make it work without the temporary found variable. (While I’m at it, I can see now that I probably should have said (unless (bolp) ...) in a more suitable place instead of making regex-list potentially nil; let’s treat this as an exercise left to the reader;).) Also, it assumes that the usual meaning of TAB is indent-for-tab-command; since I use it only in AUCTeX, when it’s true, I’m fine with that; in general, I probably should have used defadvice instead.

CategoryEnglish, CategoryBlog, CategoryEmacs