Subject: 27.0.50; diff-mode sometimes doesn't refine hunks

From: Juri Linkov <[email-masked]>
Date: Thu, 16 Aug 2018 23:48:05 +0300

It's difficult to debug a sporadic problem when automatic diff
refinement doesn't display refined overlays on some diff hunks
occasionally.  Looking at the code, it seems there is a possible
race condition when diff--font-lock-refined calls diff-hunk-next
that sets a timer that later might remove and try to re-add again
overlays already added by the direct call of diff--refine-hunk
from diff--font-lock-refined.  Also diff-hunk-next prevents
multiple consequent calls from refining a set of hunks
by checking diff--auto-refine-data.

To avoid such double refinement, this patch let-binds
diff-auto-refine-mode to nil before calling diff-hunk-next
for non-interactive navigation (it calls diff--refine-hunk
explicitly below):

diff --git a/lisp/vc/diff-mode.el b/lisp/vc/diff-mode.el
index b91a2ba45a..175687f184 100644
--- a/lisp/vc/diff-mode.el
+++ b/lisp/vc/diff-mode.el
@@ -2077,7 +2088,9 @@ diff--font-lock-refined
                   (point) 'diff--font-lock-refined nil max)))
     (let* ((min (point))
            (beg (or (ignore-errors (diff-beginning-of-hunk))
-                    (ignore-errors (diff-hunk-next) (point))
+                    (ignore-errors (let ((diff-auto-refine-mode nil))
+                                     (diff-hunk-next))
+                                   (point))
                     max)))
       (while (< beg max)
         (let ((end
@@ -2092,7 +2105,10 @@ diff--font-lock-refined
               (overlay-put ol 'modification-hooks
                            '(diff--font-lock-refine--refresh))))
           (goto-char (max beg end))
-          (setq beg (or (ignore-errors (diff-hunk-next) (point)) max)))))))
+          (setq beg (or (ignore-errors (let ((diff-auto-refine-mode nil))
+                                         (diff-hunk-next))
+                                       (point))
+                        max)))))))
 
 (defun diff--font-lock-refine--refresh (ol _after _beg _end &optional _len)
   (delete-overlay ol))