From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Augusto Stoffel Newsgroups: gmane.emacs.bugs Subject: bug#53126: 29.0.50; [PATCH] Lazy highlight/count when reading query-replace string, etc. Date: Thu, 24 Mar 2022 20:03:33 +0100 Message-ID: <878rsz6um2.fsf@gmail.com> References: <87sftyweb2.fsf@gmail.com> <861r1iyrvw.fsf@mail.linkov.net> <87zgo6owaf.fsf@gmail.com> <86k0f9xnrn.fsf@mail.linkov.net> <87tuedp6pl.fsf@gmail.com> <861r1g7n3b.fsf@mail.linkov.net> <87o84jcx5x.fsf@gmail.com> <8635lvif0r.fsf@mail.linkov.net> <87mtidip1w.fsf@gmail.com> <86zglrl4gq.fsf@mail.linkov.net> <87ee32yk7v.fsf@gmail.com> <861qz1zqfb.fsf@mail.linkov.net> <875yod1wyb.fsf@gmail.com> <86mtho5y56.fsf@mail.linkov.net> <87sfrgz979.fsf@gmail.com> <861qz0475w.fsf@mail.linkov.net> <87sfrd2cbb.fsf@gmail.com> <86fsnc4fvm.fsf@mail.linkov.net> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="16288"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.91 (gnu/linux) Cc: 53126@debbugs.gnu.org To: Juri Linkov Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Thu Mar 24 20:17:02 2022 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nXSxC-00040R-4s for geb-bug-gnu-emacs@m.gmane-mx.org; Thu, 24 Mar 2022 20:17:02 +0100 Original-Received: from localhost ([::1]:49148 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nXSxA-0004JB-L0 for geb-bug-gnu-emacs@m.gmane-mx.org; Thu, 24 Mar 2022 15:17:00 -0400 Original-Received: from eggs.gnu.org ([209.51.188.92]:43662) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nXSkc-0006hj-2t for bug-gnu-emacs@gnu.org; Thu, 24 Mar 2022 15:04:02 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]:55524) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nXSkb-0003yG-PP for bug-gnu-emacs@gnu.org; Thu, 24 Mar 2022 15:04:01 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1nXSkb-0004WY-Lj for bug-gnu-emacs@gnu.org; Thu, 24 Mar 2022 15:04:01 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Augusto Stoffel Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Thu, 24 Mar 2022 19:04:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 53126 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch Original-Received: via spool by 53126-submit@debbugs.gnu.org id=B53126.164814862417367 (code B ref 53126); Thu, 24 Mar 2022 19:04:01 +0000 Original-Received: (at 53126) by debbugs.gnu.org; 24 Mar 2022 19:03:44 +0000 Original-Received: from localhost ([127.0.0.1]:49421 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1nXSkJ-0004W3-SN for submit@debbugs.gnu.org; Thu, 24 Mar 2022 15:03:44 -0400 Original-Received: from mail-ej1-f50.google.com ([209.85.218.50]:39565) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1nXSkI-0004Vg-A6 for 53126@debbugs.gnu.org; Thu, 24 Mar 2022 15:03:42 -0400 Original-Received: by mail-ej1-f50.google.com with SMTP id dr20so11069551ejc.6 for <53126@debbugs.gnu.org>; Thu, 24 Mar 2022 12:03:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:references:date:in-reply-to:message-id :user-agent:mime-version; bh=hwr6fh5e7gexp2f16wA2PCgycn1++CVlkINwCkeRZcY=; b=FydUYV/J690rzY69RTWRefEtJ8nWq0WEclx89G/LFqsZjUWm5OoDc1qUzlohIVraDA yoPFGCMC8EB8iJmRMP9K3RaWyo6YSr+yKjNjcxg+n+WlPQs1X2QvH+y6I4hZ3YfeebiU IvdDW3G5/jj9U2MFJqUlDEKrgsiEcvSqsLrhdjPCBTkN3lLkKgcjuQaUI4gv2K6noKyU n0wN7hAZH4Ju6GdGI4+ceSiewpgE58rn3xayA7sleJaWp8mXCvZy9MrrkLE7xA1HuZDH MWWbccfU5oTjuOv8BQ/IqCP3PLEF0yxdui8fqWK0pAKurMsFp84FMNTsQ+qLu84eC/6N 2sgg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:references:date:in-reply-to :message-id:user-agent:mime-version; bh=hwr6fh5e7gexp2f16wA2PCgycn1++CVlkINwCkeRZcY=; b=ly0+pbz4Uq/Wb5egNZQGjYWme5Nv+0hLJZEvuJP7Sc2YfzjqrDWwtGNZVKMMCZtpL4 j5OYp6omhKeS/rB0+4f4pal7BxOax9oOgdkXvJyHWS9amoTXFKHgpmQFRkgwwT3XcSwk 7ZZm4Xy51vyLLf9oKwyPaMo7zNxtmed4xbm0PSc61si4yBqTTuSXOHyCYhjR8Jyzn18i bY21SdXyXtXS3mnl5Dmix5Gg4vbpE3EaDcjEVFrEE3dfRyevRNEe6jfMYFEkA+nGC3h5 Mjs68Ivp9WJ+eyBJVvm8sftg+8MgeIxmaa9E8kVwBxw25digOCv8bXAtmop1zcLpgepm iMtA== X-Gm-Message-State: AOAM533p0XM73N928SGIk+0xaHILpwDdIKILW71aE4R0bxLqve9P1LuV RxcSYecZD3PenAvpkJPCgtpoOsOLh3U= X-Google-Smtp-Source: ABdhPJzvb8sFezJLLKCr02K7+jFpE2WK1rDljjDxjbpIyAjMcXYB8yo0SGGdA6tpRWSf+WLoapofbA== X-Received: by 2002:a17:906:7944:b0:6da:b834:2f3e with SMTP id l4-20020a170906794400b006dab8342f3emr7570757ejo.353.1648148615781; Thu, 24 Mar 2022 12:03:35 -0700 (PDT) Original-Received: from ars3 ([2a02:8109:8ac0:56d0::7039]) by smtp.gmail.com with ESMTPSA id j18-20020a170906535200b006e007fa9d0fsm1515139ejo.149.2022.03.24.12.03.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 24 Mar 2022 12:03:34 -0700 (PDT) In-Reply-To: <86fsnc4fvm.fsf@mail.linkov.net> (Juri Linkov's message of "Sun, 20 Mar 2022 20:51:09 +0200") X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: "bug-gnu-emacs" Xref: news.gmane.io gmane.emacs.bugs:228889 Archived-At: --=-=-= Content-Type: text/plain On Sun, 20 Mar 2022 at 20:51, Juri Linkov wrote: > Sorry, I have no idea who and how might want to use lazy-highlighting > in the minibuffer. I'd just provide a hook that any user can add > to the minibuffer-setup-hook, or any package author can add > to minibuffer-with-setup-hook. But in any case we need more opinions. All right, I think this brings us back to the original idea: we add lazy highlight to query-replace now and decide later about the other commands in replace.el. I've attached an updated patch that applies over the already merged changes. Juri: you mentioned the idea of adding a new option `query-replace-read-lazy-highlight'. This is easier to add then remove in the future, so my suggestion would be to first wait and see if anyone actually needs that option. For now lazy highlight when reading the query-replace args is controlled by the good old `query-replace-lazy-highlight'. --=-=-= Content-Type: text/x-patch Content-Disposition: attachment; filename=0001-Display-lazy-highlight-and-match-count-in-query-repl.patch >From bc85df88bf3bee99997163b6233ff82445eac66b Mon Sep 17 00:00:00 2001 From: Augusto Stoffel Date: Thu, 17 Mar 2022 20:17:26 +0100 Subject: [PATCH] Display lazy highlight and match count in query-replace * lisp/isearch.el (isearch-query-replace): Don't clean up lazy highlight if applicable. * lisp/replace.el (query-replace-read-args, query-replace-read-to): Add lazy highlighting and count. (replace--region-filter): New function, extracted from 'perform-replace'. (perform-replace): Use 'replace--region-filter'. --- lisp/isearch.el | 21 +++++++---- lisp/replace.el | 99 ++++++++++++++++++++++++++++++++++++------------- 2 files changed, 87 insertions(+), 33 deletions(-) diff --git a/lisp/isearch.el b/lisp/isearch.el index 9b311cb49e..e93e4d8b92 100644 --- a/lisp/isearch.el +++ b/lisp/isearch.el @@ -2352,7 +2352,9 @@ isearch-query-replace (isearch-recursive-edit nil) (isearch-string-propertized (isearch-string-propertize isearch-string))) - (isearch-done nil t) + (let ((lazy-highlight-cleanup (and lazy-highlight-cleanup + (not query-replace-lazy-highlight)))) + (isearch-done nil t)) (isearch-clean-overlays) (if (and isearch-other-end (if backward @@ -2368,13 +2370,16 @@ isearch-query-replace (symbol-value query-replace-from-history-variable))) (perform-replace isearch-string-propertized - (query-replace-read-to - isearch-string-propertized - (concat "Query replace" - (isearch--describe-regexp-mode (or delimited isearch-regexp-function) t) - (if backward " backward" "") - (if (use-region-p) " in region" "")) - isearch-regexp) + (condition-case error + (query-replace-read-to + isearch-string-propertized + (concat "Query replace" + (isearch--describe-regexp-mode (or delimited isearch-regexp-function) t) + (if backward " backward" "") + (if (use-region-p) " in region" "")) + isearch-regexp) + (t (lazy-highlight-cleanup lazy-highlight-cleanup) + (signal (car error) (cdr error)))) t isearch-regexp (or delimited isearch-regexp-function) nil nil (if (use-region-p) (region-beginning)) (if (use-region-p) (region-end)) diff --git a/lisp/replace.el b/lisp/replace.el index 06be597855..a56e493d99 100644 --- a/lisp/replace.el +++ b/lisp/replace.el @@ -352,8 +352,15 @@ query-replace-read-to (query-replace-compile-replacement (save-excursion (let* ((history-add-new-input nil) + (count (if (and query-replace-lazy-highlight + minibuffer-lazy-count-format + isearch-lazy-count + isearch-lazy-count-total) + (format minibuffer-lazy-count-format + isearch-lazy-count-total) + "")) (to (read-from-minibuffer - (format "%s %s with: " prompt (query-replace-descr from)) + (format "%s%s %s with: " count prompt (query-replace-descr from)) nil nil nil query-replace-to-history-variable from t))) (add-to-history query-replace-to-history-variable to nil t) @@ -365,14 +372,49 @@ query-replace-read-args (unless noerror (barf-if-buffer-read-only)) (save-mark-and-excursion - (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 '-)))))) + (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)))))) (defun query-replace (from-string to-string &optional delimited start end backward region-noncontiguous-p) "Replace some occurrences of FROM-STRING with TO-STRING. @@ -2773,6 +2815,26 @@ replace--push-stack ,search-str ,next-replace) ,stack)) +(defun replace--region-filter (bounds) + "Return a function that decides if a region is inside BOUNDS. +BOUNDS is a list of cons cells of the form (START . END). The +returned function takes as argument two buffer positions, START +and END." + (let ((region-bounds + (mapcar (lambda (position) + (cons (copy-marker (car position)) + (copy-marker (cdr position)))) + bounds))) + (lambda (start end) + (delq nil (mapcar + (lambda (bounds) + (and + (>= start (car bounds)) + (<= start (cdr bounds)) + (>= end (car bounds)) + (<= end (cdr bounds)))) + region-bounds))))) + (defun perform-replace (from-string replacements query-flag regexp-flag delimited-flag &optional repeat-count map start end backward region-noncontiguous-p) @@ -2857,22 +2919,9 @@ perform-replace ;; Unless a single contiguous chunk is selected, operate on multiple chunks. (when region-noncontiguous-p - (let ((region-bounds - (mapcar (lambda (position) - (cons (copy-marker (car position)) - (copy-marker (cdr position)))) - (funcall region-extract-function 'bounds)))) - (setq region-filter - (lambda (start end) - (delq nil (mapcar - (lambda (bounds) - (and - (>= start (car bounds)) - (<= start (cdr bounds)) - (>= end (car bounds)) - (<= end (cdr bounds)))) - region-bounds)))) - (add-function :after-while isearch-filter-predicate region-filter))) + (setq region-filter (replace--region-filter + (funcall region-extract-function 'bounds))) + (add-function :after-while isearch-filter-predicate region-filter)) ;; If region is active, in Transient Mark mode, operate on region. (if backward -- 2.35.1 --=-=-=--