Shadow insert-environment to fix some bugs in AucTeX

master
Jacopo De Simoi 7 years ago
parent 64770cda75
commit 3dc08c6b2f
  1. 97
      global.org

@ -1205,6 +1205,103 @@
(pop-mark)
(pop-mark))))
#+END_SRC
Shadow the AucTeX version of this function
#+BEGIN_SRC emacs-lisp
(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)
(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.
(cond (;; if the line contains only whitespace, delete them
(save-excursion (beginning-of-line)
(looking-at (concat prefix "[ \t]*$")))
(delete-region (match-beginning 0) (match-end 0)))
;; otherwise, something is written on the line. We can be
;; at the beginning of the text
((TeX-looking-at-backward (concat "^" prefix "[ \t]*")
(line-beginning-position))
;;this morally opens a new line
(beginning-of-line)
(newline)
(beginning-of-line 0))
((bolp)
(delete-horizontal-space)
(newline)
(beginning-of-line 0))
((looking-at "[ \t]*$")
(delete-horizontal-space)
(newline)
(when prefix (insert prefix))
(beginning-of-line))
(t
(delete-horizontal-space)
(newline 2)
(when prefix (insert prefix))
(beginning-of-line 0)))
;; What to do with the line containing mark.
(when active-mark
(save-excursion
(goto-char (mark))
(cond ((save-excursion (beginning-of-line)
(or (looking-at (concat prefix "[ \t]*$"))
(looking-at "[ \t]*$")))
(delete-region (match-beginning 0) (match-end 0)))
((TeX-looking-at-backward (concat "^" prefix "[ \t]*")
(line-beginning-position))
(beginning-of-line)
(newline)
(beginning-of-line 0))
((looking-at "[ \t]*$")
(delete-horizontal-space)
(insert-before-markers "\n")
;(newline)
(when prefix (insert prefix)))
(t
(delete-horizontal-space)
(insert-before-markers "\n")
(newline)
(when prefix (insert prefix))))))
;; 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
(or (assoc environment LaTeX-indent-environment-list)
(if auto-fill-function
;; Fill the region only when `auto-fill-mode' is active.
(LaTeX-fill-region content-start (line-beginning-position 2))))
(set-mark content-start))
(indent-according-to-mode))
(save-excursion (beginning-of-line 2) (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-hooks
environment env-start env-end)))
#+END_SRC
*** The hook
#+BEGIN_SRC emacs-lisp
(add-hook 'LaTeX-mode-hook

Loading…
Cancel
Save