Subject: Re: bug#61215: 29.0.60; font-lock broken in diff-mode with long lines

From: Juri Linkov <[email-masked]>
Date: Mon, 06 Feb 2023 19:15:49 +0200

>> If you think the problem is in diff-syntax-fontify-hunk,
>> then I could try to find a working workaround local to diff-mode.
>
> Yes, now this is something for diff-mode to solve: it shouldn't assume
> that it will always be able to go to the beginning and the end of a
> hunk, because narrowing could interfere with that.  Diff-mode should
> have some fallback plan for these cases.

After debugging I discovered that the problem is not caused by font-lock
in diff-mode.  The problem is in easy-mmode-define-navigation.
diff-mode uses it to navigate diff hunks.  It supports such a feature
that when the user narrowed the diff buffer, it puts narrowing back
after moving to the next hunk.  But this fails when the diff buffer
is narrowed automatically by long-line-optimizations.  If there is
no way to distinguish whether the narrowing is automatic by long-lines
or manual by the user, this patch fixes the problem.  It works without
errors, and even highlights first 250000 characters of the long line:

diff --git a/lisp/emacs-lisp/easy-mmode.el b/lisp/emacs-lisp/easy-mmode.el
index 5721470ad0d..d9176061434 100644
--- a/lisp/emacs-lisp/easy-mmode.el
+++ b/lisp/emacs-lisp/easy-mmode.el
@@ -782,7 +782,10 @@ easy-mmode-define-navigation
          (when-narrowed
           (lambda (body)
             (if (null narrowfun) body
-              `(let ((was-narrowed (prog1 (buffer-narrowed-p) (widen))))
+              `(let ((was-narrowed
+                      (prog1 (unless (long-line-optimizations-p)
+                               (buffer-narrowed-p))
+                        (widen))))
                  ,body
                  (when was-narrowed (funcall #',narrowfun)))))))
     (unless name (setq name base-name))