Subject: bug#65648: 13.2; indentation of nested environments

From: Ikumi Keita <[email-masked]>
Date: Mon, 04 Sep 2023 16:47:12 +0900

Hi Uwe and Paul, thank you for your discussion.

>>>>> Paul Nelson <[email-masked]> writes:
> There's no important difference here between indent-region and the
> LaTeX fill commands -- unless I'm mistakaen, they all ultimately call
> LaTeX-indent-line, one line at a time.

That's right. The doc strings of `LaTeX-fill-region',
`LaTeX-fill-environment', `LaTeX-fill-section' and `LaTeX-fill-buffer'
say "Fill and indent ...".

> Let's put three of those examples in a row and format them according
> to current AUCTeX and my proposal.

[...]

> The current AUCTeX indentation here is obviously broken;

Indeed :-(

>>>>> Uwe Brauer <[email-masked]> writes:
> To summarise, you convinced me, sigh. 
> Not sure what the others think.

OK, I don't object.

>>> What's about a new variable then.
>>> 
>>> (defvar LaTeX-fill-sloppy t
>>> "When t, Paul's proposal, when nil, Uwe's proposal, that is the old behavior")
>>> 
>>> Or does this make the code more difficult to maintain?

>> It seems possible to fine-tune the indentation code to give the
>> "optimum", at the cost of making that code slightly less efficient due
>> to additional checks for matching \begin's.  I think that if any "new
>> variable" should be given, then its purpose should be to determine
>> whether to give such a fine-tuning, and not whether to fix the noted
>> "obviously broken" behavior.

> I think the maintainers should decide that. I am in favor of such a
> variable, however I don't know enough of the code to really judge it.

I tried to implement the idea of such customize option as
`LaTeX-indent-always-align-end-with-begin'. See the attached patch.

When this option is non-nil, the "optimum" indentation
\begin{equation}
  n u m=\left[\begin{array}{ll}
                2 & 25
              \end{array}\right]
\end{equation}
is obtained while
Let $E = \begin{bmatrix}
           1 & 0\\
           0 & 1
         \end{bmatrix}$ be the
         identity matrix.
is formatted badly.

When the option is nil, Paul's proposal takes effect so
\begin{equation}
  n u m=\left[\begin{array}{ll}
                2 & 25
  \end{array}\right]
\end{equation}
and
Let $E = \begin{bmatrix}
           1 & 0\\
           0 & 1
\end{bmatrix}$ be the
identity matrix.
are obtained.

This is only a tentative patch. Comments are welcome.

Regards,
Ikumi Keita
#StandWithUkraine #StopWarInUkraine

diff --git a/latex.el b/latex.el
index d855b5c5..c7331173 100644
--- a/latex.el
+++ b/latex.el
@@ -4058,6 +4058,11 @@ value."
   :group 'LaTeX-indentation
   :type 'regexp)
 
+(defcustom LaTeX-indent-always-align-end-with-begin t
+  "If non-nil, always indent \\end{foo} aligned with \\begin{foo}."
+  :group 'LaTeX-indentation
+  :type 'boolean)
+
 (defvar docTeX-indent-inner-fixed
   `((,(concat (regexp-quote TeX-esc)
               "\\(begin\\|end\\)[ \t]*{macrocode\\*?}")
@@ -4310,8 +4315,12 @@ outer indentation in case of a commented line.  The symbols
                                  "\\("
                                  LaTeX-end-regexp
                                  "\\)"))
-             ;; Backindent at \end.
-             (- (LaTeX-indent-calculate-last force-type) LaTeX-indent-level))
+             (if LaTeX-indent-always-align-end-with-begin
+                 (save-excursion
+                   (LaTeX-find-matching-begin)
+                   (LaTeX-current-indentation force-type))
+               ;; Backindent at \end.
+               (- (LaTeX-indent-calculate-last force-type) LaTeX-indent-level)))
             ((looking-at (concat (regexp-quote TeX-esc) "right\\b"))
              ;; Backindent at \right.
              (- (LaTeX-indent-calculate-last force-type)
@@ -8935,8 +8944,11 @@ COL for efficiency."
                             LaTeX-indent-environment-list)
                       out)))))
       (cond ((looking-at tabular-like-end-regex)
-             beg-col)
-
+             (if LaTeX-indent-always-align-end-with-begin
+                 beg-col
+               (save-excursion
+                 (goto-char beg-pos)
+                 (LaTeX-current-indentation))))
             ((looking-at "\\\\\\\\")
              (+ 2 beg-col))