Subject: Re: bug#51762: 13.0.14; environment formating

From: Ikumi Keita <[email-masked]>
Date: Tue, 16 Nov 2021 15:29:30 +0900

>>>>> Arash Esbati <[email-masked]> writes:
>>> Would it help if we generlize the control symbol idea by saying:
>> 
>>> "\\([^ \r\n%\\]\\|\\\\[^a-zA-Z0-9\\]\\)\\([ \t]\\|\\\\\\\\\\)*"
>>> ^^^^^^^^^^^^^^
>> 
>> I'm afraid that it doesn't match a line
>> \\% This is a code comment.
>> , either. Try typing M-q on the following paragraph in latex mode
>> buffer:
>> ----------------------------------------------------------------------
>> Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Donec hendrerit
>> tempor tellus. Donec pretium posuere tellus. Proin quam nisl, tincidunt et,
>> \\% This is a code comment.
>> mattis eget, convallis nec, purus.
>> ----------------------------------------------------------------------

> I agree; it's hard to come up with a regexp to catch all possible
> comment lines.

Indeed.

>> I ended up with the attached tentative patch. I hope this doesn't slow
>> down the filling loop significantly. What do you think about it?

First of all, my previous propoal doesn't work correctly for lines
ending with "\par". The first regexp group in the current code spans the
highlighted interval in the attached screenshot actually, so the
criterion "(not (match-beginning 0))" in the previous proposal is wrong.
I attach the revised patch at last of this message.

> Do you have an idea about the performance hit?

Unfortunatetly, no. I just tried several examples by hand and checked
that "make check" passes. They worked smoothly so I expect there isn't
serious performance problem.

> And while we're at it, we'll have to take care of this comment in
> `LaTeX-verbatim-macro-boundaries':

>     ;; XXX: Here we assume we are dealing with \verb which
>     ;; expects the delimiter right behind the command.
>     ;; However, \lstinline can also cope with whitespace as
>     ;; well as an optional argument after the command.

> Other packages like fancyvrb and minted do the same: Inline verb macros
> can have an optional and a mandatory argument.  So the regexp fun will
> continue :-)

OMG! 😖

Anyway, I'd like to commit the attached revised fix along with some
additional regression tests if no one objects.

Regards,
Ikumi Keita

diff --git a/latex.el b/latex.el
index 05238344..1b4346f6 100644
--- a/latex.el
+++ b/latex.el
@@ -4004,7 +4004,8 @@ You can disable filling inside a specific environment by adding
 it to `LaTeX-indent-environment-list', only indentation is
 performed in that case."
   (interactive "*r\nP")
-  (let ((end-marker (save-excursion (goto-char to) (point-marker))))
+  (let ((end-marker (save-excursion (goto-char to) (point-marker)))
+        has-code-comment)
     (if (or (assoc (LaTeX-current-environment) LaTeX-indent-environment-list)
             (member (TeX-current-macro) LaTeX-fill-excluded-macros)
             ;; This could be generalized, if there are more cases where
@@ -4019,12 +4020,24 @@ performed in that case."
       (save-restriction
         (goto-char from)
         (while (< (point) end-marker)
-          (if (re-search-forward
+          (if (or
+               ;; Code comments.
+               (when (setq has-code-comment
+                           (TeX-search-forward-comment-start end-marker))
+                 (goto-char has-code-comment)
+                 (when
+                     ;; See if there is at least one non-whitespace
+                     ;; character before the comment starts.
+                     (save-excursion
+                       (skip-chars-backward " \t" (line-beginning-position))
+                       (bolp))
+                   ;; Not a code comment.  Go back to the former
+                   ;; point.
+                   (setq has-code-comment nil)
+                   (goto-char from))
+                 has-code-comment)
+               (re-search-forward
                (concat "\\("
-                       ;; Code comments.
-                       "\\([^ \r\n%\\]\\|\\\\%\\)\\([ \t]\\|\\\\\\\\\\)*"
-                       TeX-comment-start-regexp
-                       "\\|"
                        ;; Lines ending with `\par'.
                        "\\(\\=\\|[^" TeX-esc "\n]\\)\\("
                        (regexp-quote (concat TeX-esc TeX-esc))
@@ -4038,7 +4051,7 @@ performed in that case."
                        "\\(\\s-*\\*\\)?"
                        "\\(\\s-*\\[[^]]*\\]\\)?"
                        "\\s-*$\\)")
-               end-marker t)
+               end-marker t))
               (progn
                 (goto-char (line-end-position))
                 (delete-horizontal-space)
@@ -4052,7 +4065,8 @@ performed in that case."
                   ;; Code comments and lines ending with `\par' are
                   ;; included in filling.  Lines ending with `\\' are
                   ;; skipped.
-                  (if (match-string 1)
+                  (if (or has-code-comment
+                          (match-beginning 1))
                       (LaTeX-fill-region-as-para-do from (point) justify-flag)
                     (LaTeX-fill-region-as-para-do
                      from (line-beginning-position 0) justify-flag)