all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Augusto Stoffel <arstoffel@gmail.com>
To: Juri Linkov <juri@linkov.net>
Cc: 53126@debbugs.gnu.org
Subject: bug#53126: 29.0.50; [PATCH] Lazy highlight/count when reading query-replace string, etc.
Date: Mon, 10 Jan 2022 18:34:18 +0100	[thread overview]
Message-ID: <87o84jcx5x.fsf@gmail.com> (raw)
In-Reply-To: <861r1g7n3b.fsf@mail.linkov.net> (Juri Linkov's message of "Sun,  09 Jan 2022 20:58:32 +0200")

[-- Attachment #1: Type: text/plain, Size: 2799 bytes --]


Hi Juri,

I attached a new patch (still a sketch) that requires no changes in
comint.el and simple.el.  Perhaps you will find this approach more
acceptable.

More comments below.

On Sun,  9 Jan 2022 at 20:58, Juri Linkov <juri@linkov.net> wrote:

> It would be great to use your new variable with a function
> to show replacement counts in perform-replace.  IIUC,
> let-binding isearch-lazy-count-display-function to
> isearch-read-with-highlight-count will suppress isearch-message?
>

I tried this and it's relatively simple to do, but there is a problem.
Suppose you want to replace all "a" with "z", and your buffer has 20
"a"s initially.  Then, as you keep hitting "y" to confirm a replacement
the count will be

   1/20, 1/19, ..., 1/1

since the number of "a"s decrease, and the point is always at the first
of the still-existing ones.  But probably one should count the number of
prompts, so

   1/20, 2/20, ..., 20/20

I think this means `perform-replace' has to implement its own way to
display a count.

> I meant using simply
>
>   (add-hook 'minibuffer-setup-hook 'isearch-read-with-highlight-setup)
>
> But it seems isearch-read-with-highlight-setup doesn't set
> isearch-lazy-count-display-function.
>

I guess this could be done.  But note that there are two possible types
of counts: a "current/total" counter or just a "total" counter.  Each
use case calls for a different count style.

>>> Then maybe a new feature could be named e.g. "lazy-minibuffer"?
>
> This feature has little to do with isearch.  This is why there are
> efforts to move away from the prefix isearch- for lazy-related
> functions and variables, so we have now:
>
>   lazy-count-prefix-format
>   lazy-count-suffix-format
>   lazy-highlight-buffer
>   lazy-highlight-buffer-max-at-a-time
>   lazy-highlight-cleanup
>   lazy-highlight-initial-delay
>   lazy-highlight-interval
>   lazy-highlight-max-at-a-time
>   ...
>
> There are still isearch-specific names like isearch-lazy-count
> that enables lazy-count in isearch-mode.  What would be a similar
> name for the minibuffer?  Maybe minibuffer-lazy-count?

I see, the lazy-* names are the new ones!

>
> Then the prefix isearch- needs to be removed from other names too,
> e.g. isearch-lazy-count-display-function -> lazy-count-display-function
> isearch-read-with-highlight-setup maybe to minibuffer-lazy-highlight-setup,
> etc.

Yes, I agree.  The names I came up with are horrible and
`lazy-minibuffer' is weird, but your current suggestion is nice.

By the way, I'm debating a bit whether
`isearch-lazy-count-display-function' should be:

1. Either nil or function, as it is right now,
2. #'ignore by default, so similar to 1) but a bit easier to use with
   `add-function'
3. a hook, the main inconvenience being that it can't be easily let-bound.


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Allow-reading-from-minibuffer-with-lazy-highlight-an.patch --]
[-- Type: text/x-patch, Size: 8434 bytes --]

From 471c37844f0baba925bd948e13005d2227d43f65 Mon Sep 17 00:00:00 2001
From: Augusto Stoffel <arstoffel@gmail.com>
Date: Sat, 8 Jan 2022 11:08:46 +0100
Subject: [PATCH 1/2] Allow reading from minibuffer with lazy highlight and
 match count

* lisp/isearch.el (isearch-lazy-count-display-function): New variable
allowing to display the lazy count in special ways.
(isearch-edit-string): Add lazy highlight and count of matching text.
(isearch-lazy-highlight-new-loop,
isearch-lazy-highlight-buffer-update): Use
`isearch-lazy-count-display-function' instead of hardcoded call to
`isearch-message'.
(isearch-read-with-highlight--overlay,
isearch-read-with-highlight--after-change,
isearch-read-with-highlight--exit, isearch-read-with-highlight-setup,
isearch-read-with-highlight-count): Variable and functions
implementing the lazy highlight functionality while reading from
minibuffer.
* lisp/simple.el (minibuffer-history-isearch-setup): Set
`isearch-lazy-count-display-function' appropriately.
* lisp/comint.el (comint-history-isearch-setup,
comint-history-isearch-end): Set `isearch-lazy-count-display-function'
appropriately.
---
 lisp/isearch.el | 100 +++++++++++++++++++++++++++++++++++++++---------
 1 file changed, 82 insertions(+), 18 deletions(-)

diff --git a/lisp/isearch.el b/lisp/isearch.el
index 7593a0ec98..c10f365ee5 100644
--- a/lisp/isearch.el
+++ b/lisp/isearch.el
@@ -465,6 +465,9 @@ lazy-count-suffix-format
   :group 'lazy-count
   :version "27.1")
 
+(defvar isearch-lazy-count-display-function nil
+  "Function called when new lazy count results are available.")
+
 \f
 ;; Define isearch help map.
 
@@ -1808,19 +1811,21 @@ isearch-edit-string
 	  ;; for some incompatibility with gmhist.
 	  (minibuffer-history-symbol)
 	  ;; Search string might have meta information on text properties.
-	  (minibuffer-allow-text-properties t))
+	  (minibuffer-allow-text-properties t)
+          (isearch-lazy-count-display-function #'isearch-read-with-highlight-count))
      (setq isearch-new-string
-	   (read-from-minibuffer
-	    (isearch-message-prefix nil isearch-nonincremental)
-	    (cons isearch-string (1+ (or (isearch-fail-pos)
-					 (length isearch-string))))
-	    minibuffer-local-isearch-map nil
-	    (if isearch-regexp
-		(cons 'regexp-search-ring
-		      (1+ (or regexp-search-ring-yank-pointer -1)))
-	      (cons 'search-ring
-		    (1+ (or search-ring-yank-pointer -1))))
-	    nil t)
+	   (minibuffer-with-setup-hook #'isearch-read-with-highlight-setup
+             (read-from-minibuffer
+	      (isearch-message-prefix nil isearch-nonincremental)
+	      (cons isearch-string (1+ (or (isearch-fail-pos)
+					   (length isearch-string))))
+	      minibuffer-local-isearch-map nil
+	      (if isearch-regexp
+		  (cons 'regexp-search-ring
+		        (1+ (or regexp-search-ring-yank-pointer -1)))
+	        (cons 'search-ring
+		      (1+ (or search-ring-yank-pointer -1))))
+	      nil t))
 	   isearch-new-message
 	   (mapconcat 'isearch-text-char-description
 		      isearch-new-string "")))))
@@ -4023,7 +4028,7 @@ isearch-lazy-highlight-new-loop
 			         isearch-lazy-highlight-window-end))))))
     ;; something important did indeed change
     (lazy-highlight-cleanup t (not (equal isearch-string ""))) ;stop old timer
-    (when (and isearch-lazy-count isearch-mode (null isearch-message-function))
+    (when isearch-lazy-count
       (when (or (equal isearch-string "")
                 ;; Check if this place was reached by a condition above
                 ;; other than changed window boundaries (that shouldn't
@@ -4042,7 +4047,11 @@ isearch-lazy-highlight-new-loop
         (setq isearch-lazy-count-current nil
               isearch-lazy-count-total nil)
         ;; Delay updating the message if possible, to avoid flicker
-        (when (string-equal isearch-string "") (isearch-message))))
+        (when (string-equal isearch-string "")
+          (when (and isearch-mode (null isearch-message-function))
+            (isearch-message))
+          (when isearch-lazy-count-display-function
+            (funcall isearch-lazy-count-display-function)))))
     (setq isearch-lazy-highlight-window-start-changed nil)
     (setq isearch-lazy-highlight-window-end-changed nil)
     (setq isearch-lazy-highlight-error isearch-error)
@@ -4095,13 +4104,16 @@ isearch-lazy-highlight-new-loop
                                  'isearch-lazy-highlight-start))))
   ;; Update the current match number only in isearch-mode and
   ;; unless isearch-mode is used specially with isearch-message-function
-  (when (and isearch-lazy-count isearch-mode (null isearch-message-function))
+  (when isearch-lazy-count
     ;; Update isearch-lazy-count-current only when it was already set
     ;; at the end of isearch-lazy-highlight-buffer-update
     (when isearch-lazy-count-current
       (setq isearch-lazy-count-current
             (gethash (point) isearch-lazy-count-hash 0))
-      (isearch-message))))
+      (when (and isearch-mode (null isearch-message-function))
+        (isearch-message))
+      (when isearch-lazy-count-display-function
+        (funcall isearch-lazy-count-display-function)))))
 
 (defun isearch-lazy-highlight-search (string bound)
   "Search ahead for the next or previous match, for lazy highlighting.
@@ -4302,16 +4314,68 @@ isearch-lazy-highlight-buffer-update
 		    (setq looping nil
 			  nomore  t))))
 	    (if nomore
-		(when (and isearch-lazy-count isearch-mode (null isearch-message-function))
+		(when isearch-lazy-count
 		  (unless isearch-lazy-count-total
 		    (setq isearch-lazy-count-total 0))
 		  (setq isearch-lazy-count-current
 			(gethash opoint isearch-lazy-count-hash 0))
-		  (isearch-message))
+                  (when (and isearch-mode (null isearch-message-function))
+                    (isearch-message))
+		  (when isearch-lazy-count-display-function
+                    (funcall isearch-lazy-count-display-function)))
 	      (setq isearch-lazy-highlight-timer
 		    (run-at-time lazy-highlight-interval nil
 				 'isearch-lazy-highlight-buffer-update)))))))))
 
+;; Reading from minibuffer with lazy highlight and match count
+
+(defvar isearch-read-with-highlight--overlay nil
+  "Overlay for minibuffer prompt updates.")
+
+(defvar isearch-read-with-highlight-transform #'identity
+  "Function to transform minibuffer text into a `isearch-string' for highlighting.")
+
+(defun isearch-read-with-highlight--after-change (_beg _end _len)
+  "Update lazy highlight state in minibuffer selected window."
+  (when isearch-lazy-highlight
+    (let ((inhibit-redisplay t) ;; Avoid cursor flickering
+          (string (minibuffer-contents)))
+      (with-minibuffer-selected-window
+        (setq isearch-string (funcall isearch-read-with-highlight-transform string))
+        (isearch-lazy-highlight-new-loop)))))
+
+(defun isearch-read-with-highlight--exit ()
+  "Unwind changes from `isearch-read-with-highlight-setup'."
+  (remove-hook 'after-change-functions
+               #'isearch-read-with-highlight--after-change)
+  (remove-hook 'minibuffer-exit-hook #'isearch-read-with-highlight--exit)
+  (setq isearch-read-with-highlight--overlay nil)
+  (lazy-highlight-cleanup))
+
+(defun isearch-read-with-highlight-setup ()
+  "Set up minibuffer for lazy highlight of matches in the original window.
+
+This is intended to be called via `minibuffer-with-setup-hook'.
+Note that several other isearch variables influence the lazy
+highlighting, including `isearch-regexp' and
+`isearch-lazy-count-display-function'."
+  (add-hook 'after-change-functions
+            #'isearch-read-with-highlight--after-change)
+  (add-hook 'minibuffer-exit-hook #'isearch-read-with-highlight--exit)
+  (setq isearch-read-with-highlight--overlay
+        (make-overlay (point-min) (point-min) (current-buffer) t t))
+  (isearch-read-with-highlight--after-change nil nil nil))
+
+(defun isearch-read-with-highlight-count ()
+  "Display total match count in the minibuffer prompt."
+  (when isearch-read-with-highlight--overlay
+    (overlay-put isearch-read-with-highlight--overlay
+                 'before-string
+                 (and isearch-lazy-count-total
+                      (not isearch-error)
+                      (format "%s " isearch-lazy-count-total)))))
+
+\f
 (defun isearch-resume (string regexp word forward message case-fold)
   "Resume an incremental search.
 STRING is the string or regexp searched for.
-- 
2.34.1


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #3: 0002-Lazy-highlight-when-reading-query-replace-text.patch --]
[-- Type: text/x-patch, Size: 2014 bytes --]

From 8a24c1cc976e003494c3526304a02de8f37b5896 Mon Sep 17 00:00:00 2001
From: Augusto Stoffel <arstoffel@gmail.com>
Date: Sat, 8 Jan 2022 13:15:40 +0100
Subject: [PATCH 2/2] Lazy highlight when reading query-replace text

* lisp/replace.el (query-replace-read-from): Add lazy highlighting.
---
 lisp/replace.el | 14 ++++++++++++++
 1 file changed, 14 insertions(+)

diff --git a/lisp/replace.el b/lisp/replace.el
index 60e507c642..31478ee4a9 100644
--- a/lisp/replace.el
+++ b/lisp/replace.el
@@ -244,6 +244,19 @@ query-replace-read-from
                                    (query-replace-descr
                                     (cdar query-replace-defaults)))))
                   (t (format-prompt prompt nil))))
+           ;; Set up lazy highlighting while reading FROM regexp.
+           ;; TODO: Treat the `region-noncontiguous-p' case by setting
+           ;; `isearch-filter-predicate'.
+           (isearch-lazy-highlight query-replace-highlight)
+           (isearch-regexp regexp-flag)
+           (isearch-regexp-function nil)
+           (isearch-case-fold-search case-fold-search) ;; TODO: the case-folding rule here is complicated...
+           (isearch-read-with-highlight-transform
+            (lambda (string)
+              (let ((from (query-replace--split-string string)))
+                (if (consp from) (car from) from))))
+           (isearch-lazy-count-display-function
+            #'isearch-read-with-highlight-count)
 	   (from
 	    ;; The save-excursion here is in case the user marks and copies
 	    ;; a region in order to specify the minibuffer input.
@@ -251,6 +264,7 @@ query-replace-read-from
 	    (save-excursion
               (minibuffer-with-setup-hook
                   (lambda ()
+                    (isearch-read-with-highlight-setup)
                     (setq-local text-property-default-nonsticky
                                 (append '((separator . t) (face . t))
                                         text-property-default-nonsticky)))
-- 
2.34.1


  reply	other threads:[~2022-01-10 17:34 UTC|newest]

Thread overview: 57+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-01-08 13:24 bug#53126: 29.0.50; [PATCH] Lazy highlight/count when reading query-replace string, etc Augusto Stoffel
2022-01-08 18:59 ` Juri Linkov
2022-01-08 19:35   ` Augusto Stoffel
2022-01-09  9:10     ` Juri Linkov
2022-01-09 10:02       ` Augusto Stoffel
2022-01-09 10:30         ` Augusto Stoffel
2022-01-09 18:58         ` Juri Linkov
2022-01-10 17:34           ` Augusto Stoffel [this message]
2022-01-10 19:09             ` Juri Linkov
2022-02-26 16:13               ` Augusto Stoffel
2022-03-15 17:09                 ` Juri Linkov
2022-03-15 21:33                   ` Augusto Stoffel
2022-03-16 18:56                     ` Juri Linkov
2022-03-16 20:09                       ` Augusto Stoffel
2022-03-17 17:09                         ` Juri Linkov
2022-03-17 19:10                           ` Augusto Stoffel
2022-03-17 20:40                             ` Juri Linkov
2022-03-17 21:42                               ` Augusto Stoffel
2022-03-20  9:38                               ` Augusto Stoffel
2022-03-20 18:51                                 ` Juri Linkov
2022-03-24 19:03                                   ` Augusto Stoffel
2022-03-25  8:39                                     ` Juri Linkov
2022-03-25  9:43                                       ` Augusto Stoffel
2022-03-27  7:46                                         ` Juri Linkov
2022-04-01  9:06                                           ` Augusto Stoffel
2022-04-01 16:35                                             ` Juri Linkov
2022-04-01 18:12                                               ` Augusto Stoffel
2022-04-02 18:23                                                 ` Juri Linkov
2022-04-03  8:32                                                   ` Augusto Stoffel
2022-04-03 17:06                                                     ` Juri Linkov
2022-04-04 16:37                                                     ` Juri Linkov
2022-04-05 16:38                                                       ` Augusto Stoffel
2022-04-05 17:12                                                         ` Juri Linkov
2022-04-07 19:32                                                           ` Augusto Stoffel
2022-04-08  7:32                                                             ` Juri Linkov
2022-04-08  7:53                                                               ` Augusto Stoffel
2022-04-09 11:06                                                               ` Augusto Stoffel
2022-04-10 19:38                                                                 ` Juri Linkov
2022-03-15 17:24                 ` Juri Linkov
2022-03-15 21:21                   ` Augusto Stoffel
2022-03-16 19:02                     ` Juri Linkov
2022-03-16 20:25                       ` Augusto Stoffel
2022-03-17 17:05                         ` Juri Linkov
2022-03-17 19:06                           ` Augusto Stoffel
2022-03-20 19:24                             ` Juri Linkov
2022-03-20 19:59                               ` Augusto Stoffel
2022-03-20 20:29                                 ` Juri Linkov
2022-03-20 20:56                                   ` Augusto Stoffel
2022-03-23 18:20                                     ` Juri Linkov
2022-03-23 18:54                                       ` Augusto Stoffel
2022-03-23 19:17                                         ` Eli Zaretskii
2022-03-23 19:53                                         ` Juri Linkov
2022-03-23 20:06                                           ` Juri Linkov
2022-03-23 20:30                                             ` Augusto Stoffel
2022-03-23 20:43                                               ` Juri Linkov
2022-03-17 19:45                           ` Augusto Stoffel
2022-03-17 20:43                             ` Juri Linkov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87o84jcx5x.fsf@gmail.com \
    --to=arstoffel@gmail.com \
    --cc=53126@debbugs.gnu.org \
    --cc=juri@linkov.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.