Function: reftex-what-macro
reftex-what-macro is an autoloaded and byte-compiled function defined
in reftex-parse.el.gz.
Signature
(reftex-what-macro WHICH &optional BOUND)
Documentation
Find out if point is within the arguments of any TeX-macro.
The return value is either ("\\macro" . (point)) or a list of them.
If WHICH is nil, immediately return nil.
If WHICH is 1, return innermost enclosing macro.
If WHICH is t, return list of all macros enclosing point.
If WHICH is a list of macros, look only for those macros and return the
name of the first macro in this list found to enclose point.
If the optional BOUND is an integer, bound backwards directed
searches to this point. If it is nil, limit to nearest \section -
like statement.
This function is pretty stable, but can be fooled if the text contains things like \macro{aa}{bb} where \macro is defined to take only one argument. As RefTeX cannot know this, the string "bb" would still be considered an argument of macro \macro.
Source Code
;; Defined in /usr/src/emacs/lisp/textmodes/reftex-parse.el.gz
;;;###autoload
(defun reftex-what-macro (which &optional bound)
"Find out if point is within the arguments of any TeX-macro.
The return value is either (\"\\macro\" . (point)) or a list of them.
If WHICH is nil, immediately return nil.
If WHICH is 1, return innermost enclosing macro.
If WHICH is t, return list of all macros enclosing point.
If WHICH is a list of macros, look only for those macros and return the
name of the first macro in this list found to enclose point.
If the optional BOUND is an integer, bound backwards directed
searches to this point. If it is nil, limit to nearest \\section -
like statement.
This function is pretty stable, but can be fooled if the text contains
things like \\macro{aa}{bb} where \\macro is defined to take only one
argument. As RefTeX cannot know this, the string \"bb\" would still be
considered an argument of macro \\macro."
(unless reftex-section-regexp (reftex-compile-variables))
(catch 'exit
(if (null which) (throw 'exit nil))
(let ((bound (or bound (save-excursion (re-search-backward
reftex-section-regexp nil 1)
(point))))
pos cmd-list cmd cnt cnt-opt entry)
(save-restriction
(save-excursion
(narrow-to-region (max (point-min) bound) (point-max))
;; move back out of the current parenthesis
(while (condition-case nil
(let ((forward-sexp-function nil))
(up-list -1) t)
(error nil))
(setq cnt 1 cnt-opt 0)
;; move back over any touching sexps
(while (and (reftex-move-to-previous-arg bound)
(condition-case nil
(let ((forward-sexp-function nil))
(if (eq (preceding-char) ?\))
;; '?\(' and '?\)' receive the
;; punctuation syntax "." in
;; `reftex-syntax-table', so we have
;; to change it in order move back
;; over the optional arg in
;; parentheses correctly:
(let ((temp-table (make-syntax-table)))
(modify-syntax-entry ?\( "()" temp-table)
(modify-syntax-entry ?\) ")(" temp-table)
(with-syntax-table temp-table
(backward-sexp)))
(backward-sexp))
t)
(error nil)))
(if (memq (following-char) '(?\( ?\[)) (incf cnt-opt))
(incf cnt))
(setq pos (point))
(when (and (memq (following-char) '(?\[ ?\( ?\{))
(re-search-backward "\\\\[*a-zA-Z]+\\=" nil t))
(setq cmd (reftex-match-string 0))
(when (looking-at "\\\\begin{[^}]*}")
(setq cmd (reftex-match-string 0)
cnt (1- cnt)))
;; This does ignore optional arguments. Very hard to fix.
(when (setq entry (assoc cmd reftex-env-or-mac-alist))
(if (> cnt (or (nth 4 entry) 100))
(setq cmd nil)))
(cond
((null cmd))
((eq t which)
(push (cons cmd (point)) cmd-list))
((or (eq 1 which) (member cmd which))
(throw 'exit (cons cmd (point))))))
(goto-char pos)))
(nreverse cmd-list)))))