Function: texmathp

texmathp is an autoloaded, interactive and byte-compiled function defined in texmathp.el.

Signature

(texmathp)

Documentation

Determine if point is inside (La)TeX math mode.

Returns t or nil. Additional info is placed into texmathp-why. The functions assumes that you have (almost) syntactically correct (La)TeX in the buffer. See the variable texmathp-tex-commands about which commands are checked.

Key Bindings

Source Code

;; Defined in ~/.emacs.d/elpa/auctex-14.1.2/texmathp.el
;;;###autoload
(defun texmathp ()
  "Determine if point is inside (La)TeX math mode.
Returns t or nil.  Additional info is placed into `texmathp-why'.
The functions assumes that you have (almost) syntactically correct (La)TeX in
the buffer.
See the variable `texmathp-tex-commands' about which commands are checked."
  (interactive)
  (let* ((pos (point)) math-on sw-match
         (bound (save-excursion
                  (if (re-search-backward
                       (if (memq major-mode '(doctex-mode docTeX-mode))
                           "[\n\r]%*[ \t]*[\n\r]"
                         "[\n\r][ \t]*[\n\r]")
                       nil 1 texmathp-search-n-paragraphs)
                      (match-beginning 0)
                    (point-min))))
         (mac-match (texmathp-match-macro bound))
         (env-match (texmathp-match-environment
                     (if (and mac-match (> (cdr mac-match) bound))
                         (cdr mac-match)
                       bound)))
         (match (cons nil bound)))

    ;; Select the nearer match
    (and env-match (setq match env-match))
    ;; Use `>=' instead of `>' in case called inside \ensuremath{..}
    ;; beginning just at (point-min).
    (and mac-match (>= (cdr mac-match) (cdr match)) (setq match mac-match))
    (setq math-on (memq (nth 1 (assoc (car match) texmathp-tex-commands1))
                        '(env-on arg-on)))

    ;; Check for switches
    (and (not math-on)
         (setq sw-match (texmathp-match-switch bound))
         ;; Use `>=' instead of `>' by similar reason as above. (bug#41559)
         (>= (cdr sw-match) (cdr match))
         (eq (nth 1 (assoc (car sw-match) texmathp-tex-commands1)) 'sw-on)
         (setq match sw-match math-on t))

    ;; Check for togglers
    (if (not math-on)
        (save-excursion
          (goto-char (cdr match))
          (while (re-search-forward texmathp-toggle-regexp pos t)
            (if (setq math-on (not math-on))
                (setq sw-match (cons (match-string-no-properties 2) (match-beginning 2)))
              (setq sw-match nil)))
          (and math-on sw-match (setq match sw-match))))

    ;; Store info, show as message when interactive, and return
    (setq texmathp-why match)
    ;; Check also if the match is inside a verbatim construct and
    ;; return immediately nil.  This relies on the function
    ;; `LaTeX-verbatim-p'.  We add a check here in case this library
    ;; is used stand-alone without latex.el provided by AUCTeX
    ;; (bug#61410) and the `major-mode' doesn't derive from `TeX-mode'
    ;; (bug#69681):
    (if (and (derived-mode-p 'TeX-mode)
             (fboundp 'LaTeX-verbatim-p)
             (LaTeX-verbatim-p (cdr match)))
        (progn
          (setq texmathp-why `(nil . ,(cdr match)))
          (when (called-interactively-p 'any)
            (message "math-mode is off: Math command in verbatim construct at buffer position %d"
                     (cdr match)))
          nil)
      (and (called-interactively-p 'any)
           (message "math-mode is %s: %s begins at buffer position %d"
                    (if math-on "on" "off")
                    (or (car match) "new paragraph")
                    (cdr match)))
      (and math-on t))))