Function: LaTeX-repeat-recent-math

LaTeX-repeat-recent-math is an interactive and byte-compiled function defined in latex.el.

Signature

(LaTeX-repeat-recent-math &optional N)

Documentation

Insert a copy of the Nth most recent top-level math construct.

N should be a positive integer. The recognized constructs are
"\\=\\[ ... \\]", "$$ ... $$" and "\\begin{ENV} ... \\end{ENV}"
with ENV a math environment detected by texmathp. Any
"\\label{...}" macros inside the copied region are stripped.

Key Bindings

Source Code

;; Defined in ~/.emacs.d/elpa/auctex-14.1.2/latex.el
(defun LaTeX-repeat-recent-math (&optional n)
  "Insert a copy of the Nth most recent top-level math construct.
N should be a positive integer.  The recognized constructs are
\"\\=\\[ ... \\]\", \"$$ ... $$\" and \"\\begin{ENV} ... \\end{ENV}\"
with ENV a math environment detected by `texmathp'.  Any
\"\\label{...}\" macros inside the copied region are stripped."
  (declare (modes LaTeX-mode))
  (interactive "*p")
  (setq n (or n 1))
  (unless (> n 0) (user-error "N must be positive"))
  (let* ((env-re (concat (regexp-quote TeX-esc) "begin"
                         TeX-grop
                         "\\(" (regexp-opt (LaTeX--math-environment-list)) "\\)"
                         TeX-grcl))
         (search-re (concat env-re "\\|"
                            "\\(" (regexp-quote "$$") "\\|"
                            (regexp-quote (concat TeX-esc "[")) "\\)"))
         beg end)
    (save-excursion
      (catch 'found
        (while (re-search-backward search-re nil t)
          (unless (nth 4 (syntax-ppss))
            (let ((open (match-string-no-properties 0)))
              (when (save-excursion
                      (goto-char (match-end 0))
                      (texmathp))
                (cl-decf n)
                (when (zerop n)
                  (setq beg (point))
                  (goto-char (match-end 0))
                  (let ((end-re
                         (let ((type (if (string-match env-re open)
                                         (match-string-no-properties 1 open)
                                       open)))
                           (regexp-quote (LaTeX--closing type)))))
                    (re-search-forward end-re))
                  (setq end (point))
                  (throw 'found t))))))))
    (unless (and beg end)
      (user-error "Nth most recent top-level math construct not found"))
    (let ((contents (buffer-substring-no-properties beg end)))
      (beginning-of-line)
      (if (looking-at-p "[[:blank:]]*$")
          (delete-region (point) (line-end-position))
        (end-of-line)
        (delete-horizontal-space)
        (insert "\n"))
      (save-excursion (insert contents))
      (save-restriction
        (narrow-to-region (point) (+ (point) (length contents)))
        (indent-region (point-min) (point-max))
        (save-excursion (LaTeX--strip-labels))
        (forward-line)))))