Function: LaTeX-what-macro
LaTeX-what-macro is a byte-compiled function defined in latex.el.
Signature
(LaTeX-what-macro &optional BOUND)
Documentation
Find out if point is within the arguments of any TeX-macro.
The return value is
("name" mac-or-env total-num type opt-num opt-distance)
"name" is the name of the macro (without backslash) or
environment as a string.
mac-or-env is one of the symbols mac or env.
total-num is the total number of the argument before the point started.
type is one of the symbols mandatory or optional.
opt-num is the number of optional arguments before the point started.
opt-distance the number of optional arguments after the last mandatory.
If the optional BOUND is an integer, limit backward searches to this point. If nil, limit to the previous 15 lines.
Source Code
;; Defined in ~/.emacs.d/elpa/auctex-14.1.2/latex.el
(defun LaTeX-what-macro (&optional bound)
"Find out if point is within the arguments of any TeX-macro.
The return value is
(\"name\" mac-or-env total-num type opt-num opt-distance)
\"name\" is the name of the macro (without backslash) or
environment as a string.
mac-or-env is one of the symbols `mac' or `env'.
total-num is the total number of the argument before the point started.
type is one of the symbols `mandatory' or `optional'.
opt-num is the number of optional arguments before the point started.
opt-distance the number of optional arguments after the last mandatory.
If the optional BOUND is an integer, limit backward searches to
this point. If nil, limit to the previous 15 lines."
(let ((bound (or bound (line-beginning-position -15)))
(env-or-mac 'mac)
cmd cnt cnt-opt type result ;; env-or-mac-start
(cnt-distance 0))
(save-excursion
(save-restriction
(narrow-to-region (max (point-min) bound) (point-max))
;; Move back out of the current parenthesis
(with-syntax-table (apply #'TeX-search-syntax-table
(LaTeX-completion-macro-delimiters))
(condition-case nil
(let ((forward-sexp-function nil))
(up-list -1))
(error nil))
;; Set the initial value of argument counter
(setq cnt 1)
;; Note that we count also the right opt. or man. arg and
;; record if we're inside a mand. or opt. argument
(if (= (following-char) ?\{)
(setq cnt-opt 0
type 'mandatory)
(setq cnt-opt 1
type 'optional))
;; Move back over any touching sexps
(while (and (LaTeX-move-to-previous-arg bound)
(condition-case nil
(let ((forward-sexp-function nil))
(backward-sexp) t)
(error nil)))
(unless (= (following-char) ?\{)
(cl-incf cnt-opt))
(cl-incf cnt)))
;; (setq env-or-mac-start (point))
(when (and (memql (following-char) ;; '(?\[ ?\{ ?\( ?<)
(LaTeX-completion-macro-delimiters 'open))
(re-search-backward "\\\\[*+a-zA-Z]+\\=" nil t))
(setq cmd (TeX-match-buffer 0))
(when (looking-at "\\\\begin{\\([^}]+\\)}")
(setq cmd (TeX-match-buffer 1))
(setq env-or-mac 'env)
(cl-decf cnt))
(when (and cmd (not (string= cmd "")))
(setq result (list (if (eq env-or-mac 'mac)
;; Strip leading backslash from
;; the macro
(substring cmd 1)
cmd)
env-or-mac cnt type cnt-opt))))))
;; If we were inside an optional argument after a mandatory one,
;; we have to find out the number of optional arguments before
;; the mandatory one.
(when (and (eq (nth 3 result) 'optional)
(/= 0 (- (nth 2 result) (nth 4 result))))
(save-excursion
(save-restriction
(narrow-to-region (max (point-min) bound) (point-max))
(with-syntax-table (apply #'TeX-search-syntax-table
(LaTeX-completion-macro-delimiters))
(let ((forward-sexp-function nil))
(up-list -1))
(unless (= (following-char) ?\{)
(cl-incf cnt-distance))
(while (and (LaTeX-move-to-previous-arg bound)
(condition-case nil
(let ((forward-sexp-function nil))
(backward-sexp)
(/= (following-char) ?\{))
(error nil)))
(cl-incf cnt-distance))))))
;; Check if we really have a result before adding something new:
(when result
(append result (list cnt-distance)))))