Subject: Re: bug#39597: 27.0.60: M-x occur adds fontification to fundamental-mode

From: Stefan Monnier <[email-masked]>
Date: Tue, 18 Feb 2020 08:51:37 -0500

Juri Linkov [2020-02-18 02:12:32] wrote:

>> I think commit fb16313025 (2018-12-18T23:10:[email-masked]) had an
>> unintended side-effect on fundamental-mode.  From emacs -Q:
>
> This commit contains this change:
>
> diff --git a/lisp/replace.el b/lisp/replace.el
> index dcae12e9b7..b8f231eb55 100644
> --- a/lisp/replace.el
> +++ b/lisp/replace.el
> @@ -1907,10 +1907,8 @@ occur-engine
>        global-matches)))
>  
>  (defun occur-engine-line (beg end &optional keep-props)
> -  (if (and keep-props (if (boundp 'jit-lock-mode) jit-lock-mode)
> -	   (text-property-not-all beg end 'fontified t))
> -      (if (fboundp 'jit-lock-fontify-now)
> -	  (jit-lock-fontify-now beg end)))
> +  (if (and keep-props font-lock-mode)
> +      (font-lock-ensure beg end))
>    (if (and keep-props (not (eq occur-excluded-properties t)))
>        (let ((str (buffer-substring beg end)))
>  	(remove-list-of-text-properties
>
>> - C-x b repro RET
>> - foo RET "bar" RET baz
>> - M-s o . RET
>>
>> In emacs 26.3, the Occur buffer pops up and not much else happens;
>> starting with emacs 27, font-lock-string-face is applied to "bar".
>>
>> I haven't dug much more than that; FWIW however, emacs 26.3 already
>> applied font-lock-string-face to double-quoted strings when running
>> (font-lock-ensure) in a fundamental buffer, so maybe the right fix
>> belongs e.g. somewhere in font-lock-set-defaults?
>
> Stefan, could you suggest what to do with font-lock-ensure
> to not highlight double-quoted strings in fundamental-mode?

Maybe something like the patch below?
I'm not sufficiently familiar with the design of `font-lock-specified-p`
to be completely sure where the check should be placed; this is my best guess.

        Stefan

diff --git a/lisp/font-lock.el b/lisp/font-lock.el
index 438511b647..bf045d9d7e 100644
--- a/lisp/font-lock.el
+++ b/lisp/font-lock.el
@@ -1139,9 +1139,10 @@ font-lock-ensure
   "Make sure the region BEG...END has been fontified.
 If the region is not specified, it defaults to the entire accessible
 portion of the buffer."
-  (font-lock-set-defaults)
-  (funcall font-lock-ensure-function
-           (or beg (point-min)) (or end (point-max))))
+  (when (font-lock-specified-p t)
+    (font-lock-set-defaults)
+    (funcall font-lock-ensure-function
+             (or beg (point-min)) (or end (point-max)))))
 
 (defun font-lock-default-fontify-buffer ()
   "Fontify the whole buffer using `font-lock-fontify-region-function'."