Function: LaTeX-env-label-as-keyval

LaTeX-env-label-as-keyval is a byte-compiled function defined in latex.el.

Signature

(LaTeX-env-label-as-keyval OPTIONAL &optional KEYWORD KEYVALS ENVIRONMENT)

Documentation

Query for a label and insert it in the optional argument of an environment.

OPTIONAL is ignored. Optional KEYWORD is a string to search for in the optional argument, label is only included if KEYWORD is found. KEYVALS is a string with key=val's read in. If nil, this function searchs for key=val's itself. ENVIRONMENT is a string with the name of environment, if non-nil, don't bother to find out.

Source Code

;; Defined in ~/.emacs.d/elpa/auctex-14.1.2/latex.el
(defun LaTeX-env-label-as-keyval (_optional &optional keyword keyvals environment)
  "Query for a label and insert it in the optional argument of an environment.
OPTIONAL is ignored.  Optional KEYWORD is a string to search for
in the optional argument, label is only included if KEYWORD is
found.  KEYVALS is a string with key=val's read in.  If nil, this
function searchs for key=val's itself.  ENVIRONMENT is a string
with the name of environment, if non-nil, don't bother to find
out."
  (let ((env-start (make-marker))
        (body-start (make-marker))
        (opt-start (make-marker))
        (opt-end   (make-marker))
        (currenv (or environment (LaTeX-current-environment))))
    ;; Save the starting point as we will come back here
    (set-marker body-start (point))
    ;; Go to the start of the current environment and save the position
    (LaTeX-find-matching-begin)
    (set-marker env-start (point))
    ;; Check if an opt. argument is there; assume that it starts in
    ;; the same line and save the points in markers
    (when (re-search-forward
           (concat "\\\\begin{" currenv "}[ \t]*\\[") body-start t)
      (set-marker opt-start (1- (point)))
      (goto-char opt-start)
      (forward-sexp)
      (set-marker opt-end (1- (point))))
    ;; If keyword argument is given and keyvals argument is not given,
    ;; parse the optional argument and put it into keyvals
    (when (and keyword
               (marker-position opt-start)
               (not keyvals))
      (setq keyvals (buffer-substring-no-properties
                     (1+ opt-start) opt-end)))
    ;; If keyword is given, only insert a label when keyword is found
    ;; inside the keyvals.  If keyword is nil, then insert a label
    ;; anyways
    (if (stringp keyword)
        (when (and (stringp keyvals)
                   (not (string= keyvals ""))
                   (string-match (concat keyword "[ \t]*=") keyvals))
          (goto-char opt-end)
          (let ((opt-label (LaTeX-label currenv 'environment t)))
            (when opt-label
              (insert (if (equal (preceding-char) ?,)
                          "label="
                        ",label=")
                      TeX-grop opt-label TeX-grcl))))
      (let ((opt-label (LaTeX-label currenv 'environment t)))
        (when opt-label
          ;; Check if an opt. argument is found and go to the end if
          (if (marker-position opt-end)
              (progn
                (goto-char opt-end)
                (insert (if (equal (preceding-char) ?,)
                            "label="
                          ",label=")
                        TeX-grop opt-label TeX-grcl))
            ;; Otherwise start at the beginning of environment in
            ;; order to not mess with any other mandatory arguments
            ;; which can be there
            (goto-char env-start)
            (re-search-forward (concat "\\\\begin{" currenv "}"))
            (insert LaTeX-optop "label=" TeX-grop opt-label TeX-grcl LaTeX-optcl)))))
    ;; Go to where we started and clean up the markers
    (goto-char body-start)
    (set-marker env-start nil)
    (set-marker body-start nil)
    (set-marker opt-start nil)
    (set-marker opt-end nil)))