Subject: Re: bug#25751: Query replace lazy highlighting

From: Juri Linkov <[email-masked]>
Date: Sun, 19 Feb 2017 01:17:04 +0200

>> > I think the problem is not that we remove the highlight and add it
>> > anew, the problem is that there's a redisplay cycle in between the
>> > removal and the following addition.  The fact that setting
>> > lazy-highlight-initial-delay alleviates the problem to some extent,
>> > but still leaves the flicker tells me that there's a call to sit-for
>> > or some such somewhere in the code that processes replacements, and
>> > the removal and addition of the highlight are on two different sides
>> > of that sit-for call.  One possible solution would be to remove the
>> > highlight and add it without triggering redisplay, then I'd expect the
>> > flicker to go away.
>> >
>> > Does this make sense?
>>
>> This feature is called _lazy_ highlighting where _lazy_ implies that it's
>> intended to highlight matches much later after a lot of redisplay cycles.
>
> You could still leave the "lazy" part, if you both remove and re-add
> the overlays after the idle delay.  IOW, the important thing is not to
> have redisplay between removal and addition of the highlight.

That's an option too, and here is the tested patch (it sets
lazy-highlight-max-at-a-time to nil to avoid redisplay between
lazy iterations):
diff --git a/lisp/isearch.el b/lisp/isearch.el
index 5262435..15143ce 100644
--- a/lisp/isearch.el
+++ b/lisp/isearch.el
@@ -332,7 +332,7 @@ (define-obsolete-variable-alias 'isearch-lazy-highlight-max-at-a-time
                                 'lazy-highlight-max-at-a-time
                                 "22.1")
 
-(defcustom lazy-highlight-max-at-a-time 20
+(defcustom lazy-highlight-max-at-a-time nil
   "Maximum matches to highlight at a time (for `lazy-highlight').
 Larger values may reduce Isearch's responsiveness to user input;
 smaller values make matches highlight slowly.
@@ -3122,13 +3122,13 @@ (define-obsolete-variable-alias 'isearch-lazy-highlight-word
 (defvar isearch-lazy-highlight-forward nil)
 (defvar isearch-lazy-highlight-error nil)
 
-(defun lazy-highlight-cleanup (&optional force)
+(defun lazy-highlight-cleanup (&optional force postpone)
   "Stop lazy highlighting and remove extra highlighting from current buffer.
 FORCE non-nil means do it whether or not `lazy-highlight-cleanup'
 is nil.  This function is called when exiting an incremental search if
 `lazy-highlight-cleanup' is non-nil."
   (interactive '(t))
-  (if (or force lazy-highlight-cleanup)
+  (when (and (or force lazy-highlight-cleanup) (not postpone))
       (while isearch-lazy-highlight-overlays
         (delete-overlay (car isearch-lazy-highlight-overlays))
         (setq isearch-lazy-highlight-overlays
@@ -3173,7 +3173,7 @@ (defun isearch-lazy-highlight-new-loop (&optional beg end)
 		 (not (equal isearch-error
 			     isearch-lazy-highlight-error))))
     ;; something important did indeed change
-    (lazy-highlight-cleanup t)        ;kill old loop & remove overlays
+    (lazy-highlight-cleanup t (not (equal isearch-string ""))) ;stop old timer
     (setq isearch-lazy-highlight-error isearch-error)
     ;; It used to check for `(not isearch-error)' here, but actually
     ;; lazy-highlighting might find matches to highlight even when
@@ -3204,7 +3204,7 @@ (defun isearch-lazy-highlight-new-loop (&optional beg end)
     (unless (equal isearch-string "")
       (setq isearch-lazy-highlight-timer
             (run-with-idle-timer lazy-highlight-initial-delay nil
-                                 'isearch-lazy-highlight-update)))))
+                                 'isearch-lazy-highlight-start)))))
 
 (defun isearch-lazy-highlight-search ()
   "Search ahead for the next or previous match, for lazy highlighting.
@@ -3249,6 +3249,10 @@ (defun isearch-lazy-highlight-search ()
 	success)
     (error nil)))
 
+(defun isearch-lazy-highlight-start ()
+  (lazy-highlight-cleanup t) ;remove old overlays
+  (isearch-lazy-highlight-update))
+
 (defun isearch-lazy-highlight-update ()
   "Update highlighting of other matches for current search."
   (let ((max lazy-highlight-max-at-a-time)