Subject: Re: bug#21092: 25.0.50; Option `lazy-highlight-max-at-a-time' does not work

From: Juri Linkov <[email-masked]>
Date: Wed, 02 Sep 2015 01:55:03 +0300

>> > You clearly do not want this done.
>>
>> Not at all.  I only disagree that it's a bug.
>
> Good to hear.  But it sure didn't sound like it:
>
>   J >>> There is no need to highlight all matches in the buffer
>   J >>> during Isearch.
>   E >> What if someone wants to?
>   J > Why would someone want such a strange thing?
>
> Anyway, I'm glad you agree now that such a strange thing can be useful.

I tried to find a useful application for this feature, and finally found.

> Whole buffer OR rest-of-buffer forward or backward.  Au choix.
> Both behaviors are useful.

Since you can scroll up or down, highlighting the whole buffer is unavoidable.

> Not clear why you want a separate option for this.  It cannot be
> used in combination with `lazy-highlight-max-at-a-time', can it?

Of course, it is useful in combination with ‘lazy-highlight-max-at-a-time’
that can define the incrementality (how many steps to do) regardless
whether highlighting the whole buffer or only matches on the screen.

> A second question concerns how to control whether this acts
> only in the current search direction or in both directions.

We can highlight only in the search direction because regexp
search might match different results depending on direction.
So you need to switch directions with ‘C-r’ to re-highlight
the buffer.

> For exmaple, I might want a toggle between max=10 and
> whole-buffer.

This will be possible with two separate variables:
the existing ‘lazy-highlight-max-at-a-time’
to toggle between max=10 and max=nil
and the new ‘lazy-highlight-buffer’
to toggle between whole-buffer and screen-only
implemented by this small patch:

diff --git a/lisp/isearch.el b/lisp/isearch.el
index 8d4bf24..a53b314 100644
--- a/lisp/isearch.el
+++ b/lisp/isearch.el
@@ -332,6 +332,13 @@ (defcustom lazy-highlight-max-at-a-time 20
 		 (integer :tag "Some"))
   :group 'lazy-highlight)
 
+(defcustom lazy-highlight-buffer nil
+  "Highlight the whole buffer (for `lazy-highlight').
+A value of nil means highlight all matches on the screen.
+A value of t means highlight all matches in the whole buffer."
+  :type 'boolean
+  :group 'lazy-highlight)
+
 (defface lazy-highlight
   '((((class color) (min-colors 88) (background light))
      (:background "paleturquoise"))
@@ -3029,10 +3040,12 @@ (defun isearch-lazy-highlight-new-loop (&optional beg end)
 			  isearch-lax-whitespace))
 		 (not (eq isearch-lazy-highlight-regexp-lax-whitespace
 			  isearch-regexp-lax-whitespace))
-                 (not (= (window-start)
-                         isearch-lazy-highlight-window-start))
-                 (not (= (window-end)   ; Window may have been split/joined.
-			 isearch-lazy-highlight-window-end))
+                 (if lazy-highlight-buffer nil
+                   (not (= (window-start)
+                           isearch-lazy-highlight-window-start)))
+                 (if lazy-highlight-buffer nil
+                   (not (= (window-end) ; Window may have been split/joined.
+                           isearch-lazy-highlight-window-end)))
 		 (not (eq isearch-forward
 			  isearch-lazy-highlight-forward))
 		 ;; In case we are recovering from an error.
@@ -3092,13 +3105,13 @@ (defun isearch-lazy-highlight-search ()
 				(+ isearch-lazy-highlight-start
 				   ;; Extend bound to match whole string at point
 				   (1- (length isearch-lazy-highlight-last-string)))
-			      (window-end)))
+			      (if lazy-highlight-buffer (point-max) (window-end))))
 		     (max (or isearch-lazy-highlight-start-limit (point-min))
 			  (if isearch-lazy-highlight-wrapped
 			      (- isearch-lazy-highlight-end
 				 ;; Extend bound to match whole string at point
 				 (1- (length isearch-lazy-highlight-last-string)))
-			    (window-start))))))
+			    (if lazy-highlight-buffer (point-min) (window-start)))))))
 	;; Use a loop like in `isearch-search'.
 	(while retry
 	  (setq success (isearch-search-string
@@ -3142,12 +3155,12 @@ (defun isearch-lazy-highlight-update ()
 			  (if isearch-lazy-highlight-forward
 			      (if (= mb (if isearch-lazy-highlight-wrapped
 					    isearch-lazy-highlight-start
-					  (window-end)))
+					  (if lazy-highlight-buffer (point-max) (window-end))))
 				  (setq found nil)
 				(forward-char 1))
 			    (if (= mb (if isearch-lazy-highlight-wrapped
 					  isearch-lazy-highlight-end
-					(window-start)))
+					(if lazy-highlight-buffer (point-min) (window-start))))
 				(setq found nil)
 			      (forward-char -1)))
 
@@ -3174,12 +3187,12 @@ (defun isearch-lazy-highlight-update ()
 		      (setq isearch-lazy-highlight-wrapped t)
 		      (if isearch-lazy-highlight-forward
 			  (progn
-			    (setq isearch-lazy-highlight-end (window-start))
+			    (setq isearch-lazy-highlight-end (if lazy-highlight-buffer (point-min) (window-start)))
 			    (goto-char (max (or isearch-lazy-highlight-start-limit (point-min))
-					    (window-start))))
-			(setq isearch-lazy-highlight-start (window-end))
+					    (if lazy-highlight-buffer (point-min) (window-start)))))
+			(setq isearch-lazy-highlight-start (if lazy-highlight-buffer (point-max) (window-end)))
 			(goto-char (min (or isearch-lazy-highlight-end-limit (point-max))
-					(window-end))))))))
+					(if lazy-highlight-buffer (point-max) (window-end)))))))))
 	    (unless nomore
 	      (setq isearch-lazy-highlight-timer
 		    (run-at-time lazy-highlight-interval nil