Function: reftex-what-environment

reftex-what-environment is an autoloaded and byte-compiled function defined in reftex-parse.el.gz.

Signature

(reftex-what-environment WHICH &optional BOUND)

Documentation

Find out if point is inside a LaTeX environment.

The return value is (e.g.) either ("equation" . (point)) or a list of them.

If WHICH is nil, immediately return nil. If WHICH is 1, return innermost enclosing environment. If WHICH is t, return list of all environments enclosing point. If WHICH is a list of environments, look only for those environments and
  return the name of the first environment in this list found to enclose
  point.

If the optional BOUND is an integer, bound backwards directed searches to this point. If it is nil, limit to nearest \section - like statement.

Source Code

;; Defined in /usr/src/emacs/lisp/textmodes/reftex-parse.el.gz
;;;###autoload
(defun reftex-what-environment (which &optional bound)
  "Find out if point is inside a LaTeX environment.
The return value is (e.g.) either (\"equation\" . (point)) or a list of
them.

If WHICH is nil, immediately return nil.
If WHICH is 1, return innermost enclosing environment.
If WHICH is t, return list of all environments enclosing point.
If WHICH is a list of environments, look only for those environments and
  return the name of the first environment in this list found to enclose
  point.

If the optional BOUND is an integer, bound backwards directed searches to
this point.  If it is nil, limit to nearest \\section - like statement."
  (unless reftex-section-regexp (reftex-compile-variables))
  (catch 'exit
    (save-excursion
      (if (null which) (throw 'exit nil))
      (let ((bound (or bound (save-excursion (re-search-backward
                                              reftex-section-regexp nil 1)
                                             (point))))
            env-list end-list env)
        (while (re-search-backward "\\\\\\(begin\\|end\\){\\([^}]+\\)}"
                                   bound t)
          (setq env (buffer-substring-no-properties
                     (match-beginning 2) (match-end 2)))
          (cond
           ((string= (match-string 1) "end")
            (push env end-list))
           ((equal env (car end-list))
            (setq end-list (cdr end-list)))
           ((eq t which)
            (push (cons env (point)) env-list))
           ((or (eq 1 which) (member env which))
            (throw 'exit (cons env (point))))))
        (nreverse env-list)))))