Function: LaTeX-insert-environment
LaTeX-insert-environment is a byte-compiled function defined in
latex.el.
Signature
(LaTeX-insert-environment ENVIRONMENT &optional EXTRA)
Documentation
Insert LaTeX ENVIRONMENT with optional argument EXTRA.
Source Code
;; Defined in ~/.emacs.d/elpa/auctex-14.1.2/latex.el
(defun LaTeX-insert-environment (environment &optional extra)
"Insert LaTeX ENVIRONMENT with optional argument EXTRA."
(let ((active-mark (and (TeX-active-mark) (not (eq (mark) (point)))))
prefix content-start env-start env-end additional-indent)
(when (and active-mark (< (mark) (point))) (exchange-point-and-mark))
;; Compute the prefix.
(when (and LaTeX-insert-into-comments (TeX-in-commented-line))
(save-excursion
(beginning-of-line)
(looking-at
(concat "^\\([ \t]*" TeX-comment-start-regexp "+\\)+[ \t]*"))
(setq prefix (match-string 0))))
;; What to do with the line containing point.
;; - Open a new empty line for later insertion of "\begin{foo}" and
;; put the point there.
;; - If there were at first any non-whitespace texts between the
;; point and EOL, send them into their new own line with possible
;; comment prefix.
(cond (;; When the entire line consists of whitespaces except
;; possible prefix...
(save-excursion (beginning-of-line)
(looking-at (concat prefix "[ \t]*$")))
;; ...make the line empty and put the point there.
(delete-region (match-beginning 0) (match-end 0)))
(;; When there are only whitespaces except possible prefix
;; between the point and BOL (including the case the point
;; is at BOL)...
(TeX-looking-at-backward (if prefix
(concat "^\\(" prefix "\\)?[ \t]*")
"^[ \t]*")
(line-beginning-position))
;; ...in this case, we have non-whitespace texts between
;; the point and EOL, so send the entire line into a new
;; next line and put the point on the empty line just
;; created.
(beginning-of-line)
(newline)
(beginning-of-line 0)
;; Take note that there are texts to be indented later
;; unless the region is activated.
(unless active-mark
(setq additional-indent t)))
(;; In all other cases...
t
;; ...insert a new empty line after deleting all
;; whitespaces around the point, put the point there...
(delete-horizontal-space)
(if (eolp)
(newline)
;; ...and if there were at first any non-whitespace texts
;; between (the original position of) the point and EOL,
;; send them into a new next line with possible comment
;; prefix.
(newline 2)
(when prefix (insert prefix))
(beginning-of-line 0)
;; Take note that there are texts to be indented later
;; unless the region is activated.
(unless active-mark
(setq additional-indent t)))))
;; What to do with the line containing mark.
;; If there is active region...
(when active-mark
;; - Open a new empty line for later insertion of "\end{foo}"
;; and put the mark there.
;; - If there were at first any non-whitespace texts between the
;; mark and EOL, pass them over the empty line and put them on
;; their own line with possible comment prefix.
(save-excursion
(goto-char (mark))
(cond (;; When the entire line consists of whitespaces except
;; possible prefix...
(save-excursion (beginning-of-line)
(looking-at
(if prefix
(concat "\\(" prefix "\\)?[ \t]*$")
"[ \t]*$")))
;; ...make the line empty and put the mark there.
(delete-region (match-beginning 0) (match-end 0)))
(;; When there are only whitespaces except possible prefix
;; between the mark and BOL (including the case the mark
;; is at BOL)...
(TeX-looking-at-backward (if prefix
(concat "^\\(" prefix "\\)?[ \t]*")
"^[ \t]*")
(line-beginning-position))
;; ...in this case, we have non-whitespace texts
;; between the mark and EOL, so send the entire line
;; into a new next line and put the mark on the empty
;; line just created.
(beginning-of-line)
(set-mark (point))
(newline)
;; Take note that there are texts to be indented later.
(setq additional-indent t))
(;; In all other cases...
t
;; ...make a new empty line after deleting all
;; whitespaces around the mark, put the mark there...
(delete-horizontal-space)
(insert-before-markers "\n")
;; ...and if there were at first any non-whitespace
;; texts between (the original position of) the mark
;; and EOL, send them into a new next line with
;; possible comment prefix.
(unless (eolp)
(newline)
(when prefix (insert prefix))
;; Take note that there are texts to be indented
;; later.
(setq additional-indent t))))))
;; Now insert the environment.
(when prefix (insert prefix))
(setq env-start (point))
(insert TeX-esc "begin" TeX-grop environment TeX-grcl)
(indent-according-to-mode)
(when extra (insert extra))
(setq content-start (line-beginning-position 2))
(unless active-mark
(newline)
(when prefix (insert prefix))
(newline))
(when active-mark (goto-char (mark)))
(when prefix (insert prefix))
(insert TeX-esc "end" TeX-grop environment TeX-grcl)
(end-of-line 0)
(if active-mark
(progn
(if (and auto-fill-function
(not (assoc environment LaTeX-indent-environment-list)))
;; Fill the region only when `auto-fill-mode' is active
;; and no special indent rule exists.
(LaTeX-fill-region content-start (line-beginning-position 2))
;; Else just indent the region. (bug#48518, bug#28382)
(indent-region content-start (line-beginning-position 2)))
(set-mark content-start))
(indent-according-to-mode))
;; Indent \end{foo}.
(save-excursion (beginning-of-line 2) (indent-according-to-mode)
(when additional-indent
;; Indent texts sent after the inserted
;; environment.
(forward-line 1) (indent-according-to-mode)))
(TeX-math-input-method-off)
(setq env-end (save-excursion
(search-forward
(concat TeX-esc "end" TeX-grop
environment TeX-grcl))
(match-beginning 0)))
(run-hook-with-args 'LaTeX-after-insert-env-hook
environment env-start env-end)))