Subject: bug#20240: 25.0.50; CC Mode broken fontification and commenting

From: Alan Mackenzie <[email-masked]>
Date: 3 Apr 2015 20:52:19 -0000

Hello, Stefan and Mark.

In article <[email-masked]> you wrote:
> Alan Mackenzie <[email-masked]> writes:

>> In article <[email-masked]> you wrote:
>>>>>From emacs -Q:
>>
>>> 1. C-x C-f foo.c RET
>>> 2. Insert: { RET }
>>> 3. Select the first line and M-;
>>
>>> Only `/* ` is inserted at the beginning of the buffer, which is wrong.
>>
>> It actually signals an error at that point, which is indeed wrong.

> Ah, I missed it then, thanks.

>> I've just committed a fix to the Emacs repository.  Could you please update
>> your sources, rebuild, and confirm that the fix works, please.
>>
>> Thanks for taking the trouble to report this bug.

> You're welcome. You fixed the example I gave, but as it turns out my
> example was too minimal. Hopefully this is better: uncommenting the
> commented line in the following will signal a jit-lock error

> int main(void) {
>  for(;;) {
>    /* return 0; */
>  }
> }

> Repeating M-; on that line, eventually I get `return 0;` uncommented but
> fontified as a comment.

That is actually a different (though similar) bug.

Stefan, do you have any objections to the patch below, which widens the
buffer in jit-lock-after-change before setting the 'fontified text
properties?  Mark's (latest) bug is happening because `comment-dwim' is
narrowing to an existing comment which isn't at BOL, and
jit-lock-after-change is erroring because it's trying to set the text
properties at col 0, but can't (because of the narrowing).

Like you said a while back, narrowing can cause problems.

diff --git a/lisp/jit-lock.el b/lisp/jit-lock.el
index 0faabeb..d5651c6 100644
--- a/lisp/jit-lock.el
+++ b/lisp/jit-lock.el
@@ -650,12 +650,14 @@ will take place when text is fontified stealthily."
     (let ((jit-lock-start start)
           (jit-lock-end end))
       (with-buffer-prepared-for-jit-lock
-          (run-hook-with-args 'jit-lock-after-change-extend-region-functions
-                              start end old-len)
-          ;; Make sure we change at least one char (in case of deletions).
-          (setq jit-lock-end (min (max jit-lock-end (1+ start)) (point-max)))
-          ;; Request refontification.
-          (put-text-property jit-lock-start jit-lock-end 'fontified nil))
+          (save-restriction
+           (widen)
+           (run-hook-with-args 'jit-lock-after-change-extend-region-functions
+                               start end old-len)
+           ;; Make sure we change at least one char (in case of deletions).
+           (setq jit-lock-end (min (max jit-lock-end (1+ start)) (point-max)))
+           ;; Request refontification.
+           (put-text-property jit-lock-start jit-lock-end 'fontified nil)))
       ;; Mark the change for deferred contextual refontification.
       (when jit-lock-context-unfontify-pos
         (setq jit-lock-context-unfontify-pos

-- 
Alan Mackenzie (Nuremberg, Germany).