Subject: Re: bug#46558: Interaction between font-lock-extend-region-wholelines and font-lock-extend-region-multiline

From: Sébastien Miquel <[email-masked]>
Date: Sun, 19 Jun 2022 12:56:24 +0000

Hi,

Lars Ingebrigtsen writes:
> Do you have a patch to propose here, by any chance?

Here's what I had in mind.

-- 
Sébastien Miquel
From 57a5a959c5e5f9041c76faa0842ff999e59d78bc Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Miquel?= <[email-masked]>
Date: Sun, 19 Jun 2022 14:38:54 +0200
Subject: [PATCH] Small fix in font-lock-extend-region-multiline (Bug#46558)

* lisp/font-lock.el (font-lock-extend-region-multiline): Do not extend
the region if `font-lock-multiline' starts at `font-lock-end'.
---
 lisp/font-lock.el | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)

diff --git a/lisp/font-lock.el b/lisp/font-lock.el
index 488874a175..df0a26f4d0 100644
--- a/lisp/font-lock.el
+++ b/lisp/font-lock.el
@@ -1245,12 +1245,17 @@ Put first the functions more likely to cause a change and cheaper to compute.")
       (setq font-lock-beg (or (previous-single-property-change
                                font-lock-beg 'font-lock-multiline)
                               (point-min))))
-    ;;
-    (when (get-text-property font-lock-end 'font-lock-multiline)
-      (setq changed t)
-      (setq font-lock-end (or (text-property-any font-lock-end (point-max)
-                                                 'font-lock-multiline nil)
-                              (point-max))))
+    ;; If `font-lock-multiline' starts at `font-lock-end', do not
+    ;; extend the region.
+    (let ((before-end (max (point-min) (1- font-lock-end)))
+          (new-end nil))
+      (when (get-text-property before-end 'font-lock-multiline)
+        (setq new-end (or (text-property-any before-end (point-max)
+                                             'font-lock-multiline nil)
+                          (point-max)))
+        (when (/= new-end font-lock-end)
+          (setq changed t)
+          (setq font-lock-end new-end))))
     changed))

 (defun font-lock-extend-region-wholelines ()
-- 
2.36.1