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

From: Juri Linkov <[email-masked]>
Date: Mon, 04 Apr 2022 19:37:10 +0300

> 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))

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.

Alternatively, the same lambda above could be added to

  (add-hook 'minibuffer-setup-hook (lambda () ...))