unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: Jon Eskin <eskinjp@gmail.com>
To: Dmitry Gutov <dgutov@yandex.ru>, Juri Linkov <juri@linkov.net>
Cc: Eli Zaretskii <eliz@gnu.org>, emacs-devel@gnu.org
Subject: Re: [Patch] Add project.el command to replace symbol at point throughout project
Date: Sat, 15 Jan 2022 04:55:52 -0500	[thread overview]
Message-ID: <0617E8BF-CC43-4D82-8648-064252F644FD@gmail.com> (raw)
In-Reply-To: <44f72f97-d1d8-0b4c-73a0-7f077fb1ef26@yandex.ru>

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



> On Jan 14, 2022, at 3:28 PM, Dmitry Gutov <dgutov@yandex.ru> wrote:
> 
> On 14.01.2022 12:26, Jon Eskin wrote:
>>> So Jon, if you fancy writing a patch in this area, you can try implementing what Juri suggested:
>>> 
>>>  let-bind read-regexp-defaults-function around the call
>>>  of query-replace-read-args, and use a symbol at point as the default.
>>> 
>>> That can apply to both project-query-replace-regexp and xref-find-references-and-replace.
>> On it! I might take a little while but I'll check in if I get stuck.
> 
> Yeah, don't hesitate to ask questions if/when you have any.


Hey all,

I took a stab at the implementation Juri described for `project-query-replace-regexp` and I was able to get the correct behavior, but I wanted to check in and see if it needs improvements before looking at `xref-find-references-and-replace.`

When I first let-bound `read-regexp-defaults-function` around the call to `query-replace-read-args`, it didn't work- it looks like `read-regexp` needs to be passed a symbol for its `DEFAULTS` parameter or it ignores `read-regexp-default-function`. I passed in the symbol at point to `DEFAULTS` which works- if I understand correctly the value of any I pass in doesn't end up making a difference as long as it's a symbol. I wasn't sure what the reason was for that behavior, but I didn't want to mess with stuff I didn't understand.

Another issue is that hardcoded logic in `read-regexp`:

'If PROMPT ends in \":\" (followed by
optional whitespace), use it as-is.  Otherwise, add \": \" to the end,
possibly preceded by the default result (see below).'

The PROMPT passed into read-regexp does end in a ":" due to it being formatted by a call to `format-prompt` in `query-replace-read-from`. As a result, when the symbol is at point, `read-regexp` display the prompt with the last replacement from history. To address this, I added a cond case where the formatting takes place and omit the formatting step when there is a symbol at point so that `read-regex` will correctly format the prompt with the symbol at point. 

The result is that a call to `project-query-replace-regexp` will take the symbol at point as the default when available, otherwise it will use its previous behavior of defaulting to the last replacement available in history.

Let me know what you think.


[-- Attachment #2: 0001-Use-symbol-at-point-as-default-for-query-replace-rea.patch --]
[-- Type: application/octet-stream, Size: 3330 bytes --]

From 86c180b8b9021eb96ac2ea2f75682753a3822e50 Mon Sep 17 00:00:00 2001
From: Jon Eskin <eskinjp@gmail.com>
Date: Fri, 14 Jan 2022 10:03:30 -0500
Subject: [PATCH] Use symbol at point as default for query-replace-read-args

* lisp/progmodes/project.el
(project-query-replace-regexp): Let-bind
`read-regexp-defaults-function` to default to symbol at point.

* lisp/replace.el
(query-replace-read-from): Don't format prompt if there's a symbol at
point, otherwise read-regexp is forced to use the wrong prompt.
---
 lisp/progmodes/project.el |  8 +++++---
 lisp/replace.el           | 10 ++++++----
 2 files changed, 11 insertions(+), 7 deletions(-)

diff --git a/lisp/progmodes/project.el b/lisp/progmodes/project.el
index c812f28c1b..b44b23554f 100644
--- a/lisp/progmodes/project.el
+++ b/lisp/progmodes/project.el
@@ -1072,9 +1072,11 @@ project-query-replace-regexp
 If you exit the `query-replace', you can later continue the
 `query-replace' loop using the command \\[fileloop-continue]."
   (interactive
-   (pcase-let ((`(,from ,to)
-                (query-replace-read-args "Query replace (regexp)" t t)))
-     (list from to)))
+   (let ((read-regexp-defaults-function (lambda ()
+                                          (find-tag-default-as-regexp))))
+    (pcase-let ((`(,from ,to)
+                 (query-replace-read-args "Query replace (regexp)" t t)))
+      (list from to))))
   (fileloop-initialize-replace
    from to (project-files (project-current t)) 'default)
   (fileloop-continue))
diff --git a/lisp/replace.el b/lisp/replace.el
index 60e507c642..3d7648611b 100644
--- a/lisp/replace.el
+++ b/lisp/replace.el
@@ -208,7 +208,8 @@ query-replace-read-from
 wants to replace FROM with TO."
   (if query-replace-interactive
       (car (if regexp-flag regexp-search-ring search-ring))
-    (let* ((history-add-new-input nil)
+    (let* ((sym-at-point (symbol-at-point))
+           (history-add-new-input nil)
 	   (separator-string
 	    (when query-replace-from-to-separator
 	      ;; Check if the first non-whitespace char is displayable
@@ -234,8 +235,9 @@ query-replace-read-from
 	     (symbol-value query-replace-from-history-variable)))
 	   (minibuffer-allow-text-properties t) ; separator uses text-properties
 	   (prompt
-	    (cond ((and query-replace-defaults separator)
-                   (format-prompt prompt (car minibuffer-history)))
+	    (cond (sym-at-point prompt) ;; if there's a symbol at point, let read-regexp format the prompt
+                  ((and query-replace-defaults separator)
+                     (format-prompt prompt (car minibuffer-history)))
                   (query-replace-defaults
                    (format-prompt
                     prompt (format "%s -> %s"
@@ -255,7 +257,7 @@ query-replace-read-from
                                 (append '((separator . t) (face . t))
                                         text-property-default-nonsticky)))
                 (if regexp-flag
-                    (read-regexp prompt nil 'minibuffer-history)
+                    (read-regexp prompt sym-at-point 'minibuffer-history)
                   (read-from-minibuffer
                    prompt nil nil nil nil
                    (query-replace-read-from-suggestions) t)))))
-- 
2.34.1


  reply	other threads:[~2022-01-15  9:55 UTC|newest]

Thread overview: 42+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-01-11  7:45 [Patch] Add project.el command to replace symbol at point throughout project Jon Eskin
2022-01-11  7:51 ` Manuel Uberti
2022-01-11  9:50   ` Jon Eskin
2022-01-12  3:43     ` Dmitry Gutov
2022-01-12  9:42       ` Jon Eskin
2022-01-13  1:03         ` Dmitry Gutov
2022-01-13  9:57           ` Jon Eskin
2022-01-14  2:39             ` Dmitry Gutov
2022-01-11 13:10 ` Eli Zaretskii
2022-01-11 15:15   ` Daniel Martín
2022-01-11 17:17     ` Eli Zaretskii
2022-01-12  3:46   ` Dmitry Gutov
2022-01-12 12:45     ` Eli Zaretskii
2022-01-13  1:19       ` Dmitry Gutov
2022-01-13  8:25         ` Eli Zaretskii
2022-01-14  2:43           ` Dmitry Gutov
2022-01-14  7:46             ` Eli Zaretskii
2022-01-14 10:26             ` Jon Eskin
2022-01-14 20:28               ` Dmitry Gutov
2022-01-15  9:55                 ` Jon Eskin [this message]
2022-01-15 18:30                   ` Juri Linkov
2022-01-16  3:02                     ` Dmitry Gutov
2022-01-16 17:58                       ` Juri Linkov
2022-01-17  0:19                         ` Dmitry Gutov
2022-01-17  1:15                           ` Dmitry Gutov
2022-01-17  8:17                             ` Juri Linkov
2022-01-21  3:06                               ` Dmitry Gutov
2022-01-31 18:25                                 ` Juri Linkov
2022-02-01  2:10                                   ` Dmitry Gutov
2022-02-01 20:09                                     ` Juri Linkov
2022-02-01 22:07                                       ` Dmitry Gutov
2022-02-02 19:51                                         ` Juri Linkov
2022-02-02 21:09                                           ` Dmitry Gutov
2022-01-15 18:41                   ` Jon Eskin
2022-01-16 17:55                     ` Juri Linkov
2022-01-17  1:41                   ` Dmitry Gutov
2022-01-17  6:36                     ` Jon Eskin
2022-01-12  3:42 ` Dmitry Gutov
2022-01-12  8:03   ` Jon Eskin
2022-01-12 19:43     ` Dmitry Gutov
2022-01-12 19:56       ` Juri Linkov
2022-01-12 22:12         ` Dmitry Gutov

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

  List information: https://www.gnu.org/software/emacs/

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

  git send-email \
    --in-reply-to=0617E8BF-CC43-4D82-8648-064252F644FD@gmail.com \
    --to=eskinjp@gmail.com \
    --cc=dgutov@yandex.ru \
    --cc=eliz@gnu.org \
    --cc=emacs-devel@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 public inbox

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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).