Subject: bug#51762: 13.0.14; environment formating

From: Ikumi Keita <[email-masked]>
Date: Sun, 14 Nov 2021 15:19:48 +0900

Hi Arash, thanks for your comment.

>>>>> Arash Esbati <[email-masked]> writes:
> Thank you for looking into this.  The way I understand this regexp:

>   "\\([^ \r\n%\\]\\|\\\\%\\)\\([ \t]\\|\\\\\\\\\\)*"
>                     ^^^^^^^

> is there to exclude the control symbol \%, i.e., being parsed as comment
> start.

I think so, too. Tassilo added it to fix bug#48937 this June.

> 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.
----------------------------------------------------------------------

>> Maybe we should give up regexp-based approach to find out code comments
>> accurately.

> Are you thinking about `syntax-ppss'?

No, other parts of latex.el ideintify code comments by a different logic
like:
       ;; A line with some code, followed by a comment?
       ((and (setq code-comment-start (save-excursion
                                        (beginning-of-line)
                                        (TeX-search-forward-comment-start
                                         (line-end-position))))
             (> (point) code-comment-start)
             (not (TeX-in-commented-line))
             (save-excursion
               (goto-char code-comment-start)
               ;; See if there is at least one non-whitespace character
               ;; before the comment starts.
               (re-search-backward "[^ \t\n]" (line-beginning-position) t)))

So it would be better to follow this logic than to rely on regexp. In
addition, regexp-based approach is easily fooled by percent sign in
\verb, while `TeX-search-forward-comment-start' (which in turn calls
`LaTeX-search-forward-comment-start') takes care of such cases.

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?

Regards,
Ikumi Keita

diff --git a/latex.el b/latex.el
index 05238344..97bada66 100644
--- a/latex.el
+++ b/latex.el
@@ -4019,12 +4019,24 @@ performed in that case."
       (save-restriction
         (goto-char from)
         (while (< (point) end-marker)
-          (if (re-search-forward
+          (if (or
+               ;; Code comments.
+               (let ((p (point)))
+                 (when (TeX-search-forward-comment-start end-marker)
+                   (goto-char (match-beginning 0))
+                   ;; Pretend as if there were no match to tell later
+                   ;; code that there is a code comment.
+                   (set-match-data nil)
+                   ;; See if there is at least one non-whitespace
+                   ;; character before the comment starts.
+                   (or (save-excursion
+                         (skip-chars-backward " \t" (line-beginning-position))
+                         (not (bolp)))
+                       (progn
+                         (goto-char p)
+                         nil))))
+               (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 +4050,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 +4064,7 @@ 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 (not (match-beginning 0)) ; code comment
                       (LaTeX-fill-region-as-para-do from (point) justify-flag)
                     (LaTeX-fill-region-as-para-do
                      from (line-beginning-position 0) justify-flag)