Hi all,
I'd like to improve the behavior of `LaTeX-insert-environment' inside
other environments a bit further.
Suppose that there are following lines in a LaTeX document, where "-!-"
denotes the cursor position.
\begin{center}
abc-!- xyz
\end{center}
Currently, Typing C-c C-e equation RET results in:
\begin{center}
abc
\begin{equation}
\label{eq:1}
-!-
\end{equation}
xyz
\end{center}
Note that "xyz" is not indented and just at the beginning of the line. I
think this "xyz" should be indented as well. When one continues edit at
the position after "xyz", it usually ends up with typing TAB to get the
suitable indent for the line.
I expect that the attached patch does this job. It introduces a new flag
to distinguish whether there is additional text after the position where
the new environment is inserted, and does another indent if such text
exists.
I used this patch for 3 months or so and it worked as expected.
Does anyone find difficulties with this idea? E.g., should I introduce
some new user option to suppress this behavior?
Regards,
Ikumi Keita
diff --git a/latex.el b/latex.el
index d3d0f1a7..126c4236 100644
--- a/latex.el
+++ b/latex.el
@@ -688,7 +688,7 @@ environment just inserted, the buffer position just before
(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)
+ 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))
@@ -722,7 +722,9 @@ environment just inserted, the buffer position just before
;; created.
(beginning-of-line)
(newline)
- (beginning-of-line 0))
+ (beginning-of-line 0)
+ (unless active-mark
+ (setq additional-indent t)))
(;; In all other cases...
t
;; ...insert a new empty line after deleting all
@@ -736,7 +738,9 @@ environment just inserted, the buffer position just before
;; prefix.
(newline 2)
(when prefix (insert prefix))
- (beginning-of-line 0))))
+ (beginning-of-line 0)
+ (unless active-mark
+ (setq additional-indent t)))))
;; What to do with the line containing mark.
;; If there is active region...
(when active-mark
@@ -769,7 +773,8 @@ environment just inserted, the buffer position just before
;; line just created.
(beginning-of-line)
(set-mark (point))
- (newline))
+ (newline)
+ (setq additional-indent t))
(;; In all other cases...
t
;; ...make a new empty line after deleting all
@@ -782,7 +787,8 @@ environment just inserted, the buffer position just before
;; possible comment prefix.
(unless (eolp)
(newline)
- (when prefix (insert prefix)))))))
+ (when prefix (insert prefix))
+ (setq additional-indent t))))))
;; Now insert the environment.
(when prefix (insert prefix))
(setq env-start (point))
@@ -806,7 +812,9 @@ environment just inserted, the buffer position just before
(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))
+ (save-excursion (beginning-of-line 2) (indent-according-to-mode)
+ (when additional-indent
+ (forward-line 1) (indent-according-to-mode)))
(TeX-math-input-method-off)
(setq env-end (save-excursion
(search-forward