Subject: Re: bug#69398: `LaTeX-fill-paragraph` behavior within indented environments

From: Ikumi Keita <[email-masked]>
Date: Thu, 07 Mar 2024 00:48:30 +0900

Hi Ruiyang,

>>>>> Ruiyang Wu <[email-masked]> writes:
> Hi Ikumi,
> Thanks for the reply and please take your time.

I figured out the reason but haven't come up with a satisfactory
solution.

[Basis]
The elisp function `save-excursion' uses marker internally to keep track
of the original position.

[Analysis]
,----
| \documentclass{article}
| 
| \begin{document}
| \begin{itemize}
| \item In indented environments, when the point is at the first letter
|     of a line, running ``LaTeX-fill-paragraph'' will move the point to
|   the beginning of the line. Ideally, it should stay with the letter.
| \end{itemize}
| \end{document}
`----

During the course of the filling, in `LaTeX-indent-outer-do', the line
in question once loses its indent:
,----
| \item In indented environments, when the point is at the first letter
| of a line, running ``LaTeX-fill-paragraph'' will move the point to the beginning of the line. Ideally, it should stay with the letter.
| \end{itemize}
`----
At this time, the marker locates at the "correct" position, namely at
the first "o" in the line immediately after "item". In other words, it
is at the beginning of the line immediately after "item".

Then `LaTeX-indent-outer-do' calls `indent-to', which inserts
whitespaces, to give the correct indentation. However, this insertion is
done at the position of the marker that the `save-excursion' uses. This
means that emacs doesn't send the marker after the inserted whitespaces
because that's the default behavior of emacs. Therefore, the marker
stays at the beginning of the line and the point is moved to that
marker position after M-q finishes, rather than the position after the
indentation.

[Solution?]
We can work around this particular example by replacing
  (save-excursion
    ...)
in `LaTeX-fill-paragraph' with
  (let ((m (point-marker)))
    (set-marker-insertion-type m t) ; Send marker after the inserted text.
    ...
    (goto-char m)
    (set-marker m nil))
, but I'm not sure this is the right thing to do:
1. This wouldn't match with behavior with other filling functions.
2. Even if we tweak all such occurences of `save-excursion', it isn't
   clear whether it is right that the position always "sticks" to the
   letter where before M-q is typed.

Does anyone find a good solution?

Regards,
Ikumi Keita
#StandWithUkraine #StopWarInUkraine
#Gaza #StopMassiveKilling #CeasefireNOW