Function: LaTeX-completion-parse-args

LaTeX-completion-parse-args is a byte-compiled function defined in latex.el.

Signature

(LaTeX-completion-parse-args ENTRY)

Documentation

Return the match of buffer position ENTRY with AUCTeX macro definitions.

ENTRY is generated by the function LaTeX-what-macro. This function matches the current buffer position (that is, which macro argument) with the corresponding definition in TeX-symbol-list(var)/TeX-symbol-list(fun) or LaTeX-environment-list(var)/LaTeX-environment-list(fun) and returns it.

Source Code

;; Defined in ~/.emacs.d/elpa/auctex-14.1.2/latex.el
(defun LaTeX-completion-parse-args (entry)
  "Return the match of buffer position ENTRY with AUCTeX macro definitions.
ENTRY is generated by the function `LaTeX-what-macro'.  This
function matches the current buffer position (that is, which macro
argument) with the corresponding definition in `TeX-symbol-list'
or `LaTeX-environment-list' and returns it."
  (let* ((name (nth 0 entry))
         (mac-or-env (nth 1 entry))
         (total-num (nth 2 entry))
         (type (nth 3 entry))
         (opt-num (nth 4 entry))
         (opt-dis (nth 5 entry))
         (mand-num (- total-num opt-num))
         (cnt 0)
         (again t)
         arg-list
         arg
         result)
    (setq arg-list (cdr (assoc name (if (eq mac-or-env 'mac)
                                        (TeX-symbol-list)
                                      (LaTeX-environment-list)))))

    ;; Check if there is a `LaTeX-env-*-args' in the `arg-list' and
    ;; remove it:
    (when (and (eq mac-or-env 'env)
               (memq (car arg-list) '(LaTeX-env-args
                                      LaTeX-env-item-args
                                      LaTeX-env-label-args)))
      (pop arg-list))

    ;; Check for `TeX-arg-conditional' here and change `arg-list'
    ;; accordingly.
    ;; FIXME: Turn `y-or-n-p' into `always' otherwise there will be a
    ;; query during in-buffer completion.  This will work for most
    ;; cases, but will also fail for example in hyperref.el.  This
    ;; decision should revisited at a later stage:
    (when (assq 'TeX-arg-conditional arg-list)
      (cl-letf (((symbol-function 'y-or-n-p) #'TeX-always))
        (while (and arg-list
                    (setq arg (car arg-list)))
          (if (and (listp arg) (eq (car arg) 'TeX-arg-conditional))
              (setq result (append (reverse (if (eval (nth 1 arg) t)
                                                (nth 2 arg)
                                              (nth 3 arg)))
                                   result))
            (push arg result))
          (pop arg-list)))
      (setq arg-list (nreverse result)))

    ;; Now parse the `arg-list':
    (cond ((and (eq type 'optional)
                (= opt-dis 0))
           ;; Optional arg without mandatory one before: This case is
           ;; straight and we just pick the correct one out of the
           ;; list:
           (setq result (nth (1- total-num) arg-list)))

          ;; Mandatory arg: Loop over the arg-list and drop all
          ;; vectors at the list beginning:
          ((eq type 'mandatory)
           (while (vectorp (car arg-list))
             (pop arg-list))
           ;; The next entry must be a mandatory arg.  If we're
           ;; looking for the first mandatory argument, just pick the
           ;; first element.  Otherwise loop further over the list and
           ;; count for the correct arg:
           (if (= mand-num 1)
               (setq result (car arg-list))
             (while again
               (cond ((vectorp (car arg-list))
                      (pop arg-list)
                      (setq again t))
                     ((= (cl-incf cnt) mand-num)
                      (setq again nil)
                      (setq result (car arg-list)))
                     (t
                      ;; Be a little conservative against infloops.
                      (if arg-list
                          (progn (setq again t)
                                 (pop arg-list))
                        (setq again nil)))))))

          ;; Optional arg after mandatory one(s): This isn't fun :-(
          ((and (eq type 'optional)
                (/= opt-dis 0))
           (setq again t)
           (setq cnt 0)
           ;; The idea is: Look for non-vectors and count the number
           ;; of mandatory argument in `mand-num'.
           (while again
             (cond ((and (not (vectorp (car arg-list)))
                         (/= (cl-incf cnt) mand-num))
                    (pop arg-list)
                    (setq again t))
                   ((and (not (vectorp (car arg-list)))
                         ;; Don't incf mand-num again; is done in the
                         ;; clause above:
                         (= cnt mand-num))
                    (setq again nil))
                   ;; If the clauses above fail, we can safely drop
                   ;; vectors:
                   ((vectorp (car arg-list))
                    (pop arg-list)
                    (setq again t))
                   (t
                    (setq again nil))))
           (setq result (nth opt-dis arg-list)))
          (t nil))
    result))