Function: LaTeX-find-matching-end

LaTeX-find-matching-end is an interactive and byte-compiled function defined in latex.el.

Signature

(LaTeX-find-matching-end)

Documentation

Move point to the \end of the current environment.

If function is called inside a comment and LaTeX-syntactic-comments is enabled, try to find the environment in commented regions with the same comment prefix.

Key Bindings

Source Code

;; Defined in ~/.emacs.d/elpa/auctex-14.1.2/latex.el
(defun LaTeX-find-matching-end ()
  "Move point to the \\end of the current environment.

If function is called inside a comment and
`LaTeX-syntactic-comments' is enabled, try to find the
environment in commented regions with the same comment prefix."
  (declare (modes LaTeX-mode))
  (interactive)
  (let* ((regexp (concat (regexp-quote TeX-esc) "\\(begin\\|end\\)\\b"))
         (level 1)
         (in-comment (TeX-in-commented-line))
         (comment-prefix (and in-comment (TeX-comment-prefix)))
         (case-fold-search nil))
    ;; The following code until `while' handles exceptional cases that
    ;; the point is on "\begin{foo}" or "\end{foo}".
    ;; Note that it doesn't work for "\end{\foo{bar}}".  See bug#19281.
    (let ((pt (point)))
      (skip-chars-backward (concat "a-zA-Z* \t" TeX-grop))
      (unless (bolp)
        (backward-char 1)
        (if (and (looking-at regexp)
                 (char-equal (char-after (match-beginning 1)) ?e))
            (setq level 0)
          (goto-char pt))))
    (while (and (> level 0) (re-search-forward regexp nil t))
      (when (or (and LaTeX-syntactic-comments
                     (eq in-comment (TeX-in-commented-line))
                     ;; If we are in a commented line, check if the
                     ;; prefix matches the one we started out with.
                     (or (not in-comment)
                         (string= comment-prefix (TeX-comment-prefix))))
                (and (not LaTeX-syntactic-comments)
                     (not (TeX-in-commented-line)))
                ;; macrocode*? in docTeX-mode is special since we have
                ;; also regular code lines not starting with a
                ;; comment-prefix.  Hence, the next check just looks
                ;; if we're inside such a group and returns non-nil to
                ;; recognize such a situation.
                (and (eq major-mode 'docTeX-mode)
                     (looking-at-p " *{macrocode\\*?}")))
        (setq level
              (if (= (char-after (match-beginning 1)) ?b) ;;begin
                  (1+ level)
                (1- level)))))
    (if (= level 0)
        (re-search-forward
         (concat TeX-grop (LaTeX-environment-name-regexp) TeX-grcl))
      (error "Can't locate end of current environment"))))