Subject: Re: bug#73332: 31.0.50; Whitespace-mode breaking font-locking

From: Stefan Monnier <[email-masked]>
Date: Thu, 19 Sep 2024 11:50:26 -0400

> Stefan, is there a way of avoiding this effect, perhaps by using a
> function in whitespace-font-lock-keywords?

We could of course replace the regexp with a function and thus do
"whatever we want", as in:

       ,@(when (memq 'missing-newline-at-eof whitespace-active-style)
           ;; Show missing newline.
           `(,(lambda (limit)
                (when (and (re-search-forward ".\\'" nil t)
                           ;; Don't mark the end of the buffer if point is
                           ;; there -- it probably means that the user is
                           ;; typing something at the end of the buffer.
                           (/= whitespace-point (point-max)))
                  (put-text-property (match-beginning 0)
                                     (match-end 0)
                                     'face 'whitespace-missing-newline-at-eof)
                  nil))))

But personally I think the root cause is the use of OVERRIDE and I'd
replace it with `prepend` as in the patch below, but whitespace uses
OVERRIDE "everywhere", whereas my argument would suggest it should use
`prepend` at all those places, which is likely beyond the scope of `emacs-30`.

        Stefan
diff --git a/lisp/whitespace.el b/lisp/whitespace.el
index 28d131b054c..3693125ec7b 100644
--- a/lisp/whitespace.el
+++ b/lisp/whitespace.el
@@ -739,7 +739,7 @@ whitespace-space-before-tab-regexp
 
 (defcustom whitespace-indentation-regexp
   '("^\t*\\(\\( \\{%d\\}\\)+\\)[^\n\t]"
-    . "^ *\\(\t+\\)[^\n]")
+    . "^ *\\(\t+\\).")
   "Regexps to match indentation whitespace that should be visualized.
 
 The value should be a cons whose car specifies the regexp to match
@@ -1752,7 +1752,7 @@ whitespace-report-region
                        ((eq (car option) 'space-after-tab::space)
                         (whitespace-space-after-tab-regexp 'space))
                        ((eq (car option) 'missing-newline-at-eof)
-                        "[^\n]\\'")
+                        ".\\'")
                        (t
                         (cdr option)))))
                  (when (re-search-forward regexp rend t)
@@ -2188,14 +2188,14 @@ whitespace-color-on
               1 whitespace-space-after-tab t)))
        ,@(when (memq 'missing-newline-at-eof whitespace-active-style)
            ;; Show missing newline.
-           `(("[^\n]\\'" 0
-              ;; Don't mark the end of the buffer is point is there --
+           `((".\\'" 0
+              ;; Don't mark the end of the buffer if point is there --
               ;; it probably means that the user is typing something
               ;; at the end of the buffer.
               (and (/= whitespace-point (point-max))
                    'whitespace-missing-newline-at-eof)
-              t)))))
-    (font-lock-add-keywords nil whitespace-font-lock-keywords t)
+              prepend)))))
+    (font-lock-add-keywords nil whitespace-font-lock-keywords 'append)
     (font-lock-flush)))