Function: TeX-find-macro-end-helper

TeX-find-macro-end-helper is a byte-compiled function defined in tex.el.

Signature

(TeX-find-macro-end-helper START &optional SIGNATURE)

Documentation

Find the end of a macro given its START.

START is the position just before the starting token of the macro. If the macro is followed by square brackets or curly braces, those will be considered part of it. SIGNATURE, as in TeX-find-macro-boundaries, restricts how many arguments are allowed.

Source Code

;; Defined in ~/.emacs.d/elpa/auctex-14.1.2/tex.el
(defun TeX-find-macro-end-helper (start &optional signature)
  "Find the end of a macro given its START.
START is the position just before the starting token of the macro.
If the macro is followed by square brackets or curly braces,
those will be considered part of it.  SIGNATURE, as in
`TeX-find-macro-boundaries', restricts how many arguments are allowed."
  (save-excursion
    (save-match-data
      (catch 'found
        (goto-char (1+ start))
        (if (zerop (skip-chars-forward "A-Za-z@"))
            (forward-char)
          (skip-chars-forward "*"))
        (let* ((max-tot (and (integerp signature) signature))
               (max-opt (and (consp signature) (car signature)))
               (max-req (and (consp signature) (cdr signature)))
               (num-opt 0)
               (num-req 0)
               (sig-pred (when (functionp signature) signature))
               last-arg-start last-args)
          (while (not (eobp))
            (when (or (and max-tot (>= (+ num-opt num-req) max-tot))
                      (and sig-pred
                           (progn
                             (when last-arg-start
                               (let ((arg (buffer-substring-no-properties
                                           last-arg-start (point))))
                                 (setq last-args (nconc last-args (list arg)))))
                             (funcall sig-pred last-args))))
              (throw 'found (point)))
            (cond
             ;; Skip over pairs of square brackets
             ((or (looking-at "[ \t]*\n?[ \t]*\\(\\[\\)") ; Be conservative: Consider
                                        ; only consecutive lines.
                  (and (looking-at (concat "[ \t]*" TeX-comment-start-regexp))
                       (save-excursion
                         (forward-line 1)
                         (looking-at "[ \t]*\\(\\[\\)"))))
              (when (and max-opt (>= num-opt max-opt))
                (throw 'found (point)))
              (cl-incf num-opt)
              (goto-char (match-beginning 1))
              (setq last-arg-start (point))
              ;; Imitate `font-latex-find-matching-close', motivated by
              ;; examples like \begin{enumerate}[a{]}].
              (let ((syntax (TeX-search-syntax-table ?\[ ?\]))
                    (parse-sexp-ignore-comments
                     (not (derived-mode-p 'docTeX-mode))))
                (modify-syntax-entry ?\{ "|" syntax)
                (modify-syntax-entry ?\} "|" syntax)
                (modify-syntax-entry ?\\ "/" syntax)
                (condition-case nil
                    (with-syntax-table syntax
                      (forward-sexp))
                  (scan-error (throw 'found (point))))))
             ;; Skip over pairs of curly braces
             ((or (looking-at "[ \t]*\n?[ \t]*{") ; Be conservative: Consider
                                        ; only consecutive lines.
                  (and (looking-at (concat "[ \t]*" TeX-comment-start-regexp))
                       (save-excursion
                         (forward-line 1)
                         (looking-at "[ \t]*{"))))
              (when (and max-req (>= num-req max-req))
                (throw 'found (point)))
              (cl-incf num-req)
              (goto-char (match-end 0))
              (setq last-arg-start (1- (point)))
              (goto-char (or (TeX-find-closing-brace)
                             ;; If we cannot find a regular end, use the
                             ;; next whitespace.
                             (save-excursion (skip-chars-forward "^ \t\n")
                                             (point)))))
             (t
              (throw 'found (point))))))
        ;; Make sure that this function does not return nil, even
        ;; when the above `while' loop is totally skipped. (bug#35638)
        (throw 'found (point))))))