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

From: Juri Linkov <[email-masked]>
Date: Fri, 25 Mar 2022 10:39:12 +0200

> @@ -365,14 +372,49 @@ query-replace-read-args
> -    (let* ((from (query-replace-read-from prompt regexp-flag))
> -           (to (if (consp from) (prog1 (cdr from) (setq from (car from)))
> -                 (query-replace-read-to from prompt regexp-flag))))
> -      (list from to
> -            (or (and current-prefix-arg (not (eq current-prefix-arg '-)))
> -                (and (plist-member (text-properties-at 0 from) 'isearch-regexp-function)
> -                     (get-text-property 0 'isearch-regexp-function from)))
> -            (and current-prefix-arg (eq current-prefix-arg '-))))))

The name of the function is `query-replace-read-args'.
And now most of the function is dealing with highlighting:

> +    (condition-case error
> +        (let (;; Variables controlling lazy highlighting while reading
> +              ;; FROM and TO.
> +              (isearch-case-fold-search case-fold-search)
> +              (isearch-lazy-highlight query-replace-lazy-highlight)
> +              (isearch-regexp regexp-flag)
> +              (isearch-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)))
> +              (lazy-highlight-cleanup nil)
> +              (minibuffer-lazy-highlight-transform
> +               (lambda (string)
> +                 (let* ((split (query-replace--split-string string))
> +                        (from-string (if (consp split) (car split) split)))
> +                   (when (and case-fold-search search-upper-case)
> +	             (setq isearch-case-fold-search
> +                           (isearch-no-upper-case-p from-string regexp-flag)))
> +                   from-string)))
> +              from to)
> +          (when query-replace-lazy-highlight
> +            (add-hook 'minibuffer-setup-hook #'minibuffer-lazy-highlight-setup)
> +            (when (use-region-p)
> +              (letrec ((region-filter (replace--region-filter
> +                                       (funcall region-extract-function 'bounds)))
> +                       (cleanup (lambda ()
> +                                  (remove-function isearch-filter-predicate region-filter)
> +                                  (remove-hook 'minibuffer-exit-hook cleanup))))
> +                (add-function :after-while isearch-filter-predicate region-filter)
> +                (add-hook 'minibuffer-exit-hook cleanup))))
> +          (setq from (query-replace-read-from prompt regexp-flag))
> +          (setq to (if (consp from)
> +                       (prog1 (cdr from) (setq from (car from)))
> +                     (query-replace-read-to from prompt regexp-flag)))
> +          (list from to
> +                (or (and current-prefix-arg (not (eq current-prefix-arg '-)))
> +                    (and (plist-member (text-properties-at 0 from) 'isearch-regexp-function)
> +                         (get-text-property 0 'isearch-regexp-function from)))
> +                (and current-prefix-arg (eq current-prefix-arg '-))))
> +      (t (lazy-highlight-cleanup)
> +         (signal (car error) (cdr error))))))

This highlighting needs to be refactored from `query-replace-read-args'
into a separate highlighting function.