Subject: Re: bug#48365: 13.0.11; Dollars in distinct comments trigger math mode highlighting in text in-between

From: Ikumi Keita <[email-masked]>
Date: Wed, 12 May 2021 15:39:20 +0900

Hi Jean and Tassilo,

>>>>> jfbu <[email-masked]> writes:
>> Now that you've mentioned it, I remember I wanted to do that change also
>> in the past and Keita-san stopped me from doing so for exactly this
>> reason.

Yes, I did. ;-)

> Would it make sense to apply these rules:

> 1. a $ in comments can never start math mode
> 2. but it can and will always stop math mode

That is a clean idea and, fortunately, easy to implment. Could you test
the attached patch?

One remark: This patch does not follow rigorously your proposition. A $
in comments does not stop math mode if it is preceded by odd numbers of
$'s like this:
foo bar blah % \$

Regards,
Ikumi Keita

diff --git a/font-latex.el b/font-latex.el
index e852ea7f..78256ce5 100644
--- a/font-latex.el
+++ b/font-latex.el
@@ -1914,10 +1914,12 @@ signs to follow the point and must be 1 or 2."
                    ;; ignore this $.  However, we need to keep our %$
                    ;; workaround intact if there has been a valid
                    ;; occurrence of $ in a non-math context.
-                   (not (and
-                         (looking-back
-                          (concat TeX-comment-start-regexp "[ \t]*"))
-                         (looking-at "[$][ \t]*\n"))))))
+
+                   ;; If `num' is nil, we are searching "$" (or "$$")
+                   ;; which begins math mode.  In that case, we ignore
+                   ;; it.  Otherwise, we take it as a candidate which
+                   ;; ends math mode.
+                   (not num))))
         (skip-chars-forward "$" limit))
        ;; check 2: Else, is "$" escaped?
        ((TeX-escaped-p)