Subject: Re: bug#53126: 29.0.50; [PATCH] Lazy highlight/count when reading query-replace string, etc.

From: Augusto Stoffel <[email-masked]>
Date: Tue, 05 Apr 2022 18:38:52 +0200

On Mon,  4 Apr 2022 at 19:37, Juri Linkov <[email-masked]> wrote:

>> I've attached a sketch of a macro to help activating the minibuffer lazy
>> highlight.  It doesn't make query-replace-read-args exactly short, but
>> if you like this, I can prepare the patch.
>
> I realized we already have the macro minibuffer-with-setup-hook,
> so we don't need more macros.
>
> The purpose of minibuffer-with-setup-hook is to set up the minibuffer
> to the initial state with buffer-local variables.  In case of
> lazy-highlighting, we can't set isearch variables directly,
> but all isearch variables can be replaced by one variable
> that contains a set of lazy-highlight parameters.  For example:
>
>   (minibuffer-with-setup-hook
>       (lambda ()
>         (setq-local lazy-highlight-params
>                     `((case-fold ,case-fold-search)
>                       (regexp ,regexp-flag)
>                       (regexp-function ,(or replace-regexp-function
>                                             (and current-prefix-arg
>                                                  (not (eq current-prefix-arg '-)))
>                                             (and replace-char-fold
>                                                  (not regexp-flag)
>                                                  #'char-fold-to-regexp)))))
>         (minibuffer-lazy-highlight-init))
>     (query-replace-read-from prompt regexp-flag))

With this code, case-fold-search and all following lazy highlight params
are evaluated with the minibuffer as current buffer, which is not the
intended behavior.

But that's a good point, we don't need a macro.  Among several
variations, we could make the setup code look like this:

     (minibuffer-with-setup-hook
           (minibuffer-lazy-highlight-init :case-fold case-fold-search
                                           :regexp regexp-flag
                                           ...)
       (query-replace-read-from prompt regexp-flag))

where now `minibuffer-lazy-highlight-init' is not the function that
initializes stuff, but rather a function that returns a closure that
initializes stuff.

> where minibuffer-lazy-highlight-init does a little more than
> minibuffer-lazy-highlight-setup.  Or maybe it's not needed,
> and minibuffer-lazy-highlight-setup should be enough.
> Or maybe instead of setq-local is can let-bind variables.
>
> Please also note that condition-case can be replaced by
> a hook in minibuffer-exit-hook that can remove highlighting
> after exiting the minibuffer.

If it was a `unwind-protect', I would agree.  But I don't know how to
simulate a `condition-case'.  Specifically, how can we determine if some
hook (the minibuffer-exit-hook in this case) is being run "normally" or
as part of the recovery from a signaled error?

> Alternatively, the same lambda above could be added to
>
>   (add-hook 'minibuffer-setup-hook (lambda () ...))

Why was it again that we want to avoid saying something like this?

    (let ((case-fold-search whatever)
          (isearch-regexp regexp-flag))
       (minibuffer-with-setup-hook #'minibuffer-lazy-highlight-init
         (query-replace-read-from prompt regexp-flag)))