[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Patch] Add project.el command to replace symbol at point throughout

From: Juri Linkov
Subject: Re: [Patch] Add project.el command to replace symbol at point throughout project
Date: Mon, 31 Jan 2022 20:25:29 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/29.0.50 (x86_64-pc-linux-gnu)

>>> +                  (read-string
>>> +                   prompt
>>> +                   nil nil
>>> +                   (if read-regexp-defaults-function
>>> +                       (funcall read-regexp-defaults-function)
>>> +                     (query-replace-read-from-suggestions))
>> Isn't it too strange that read-string uses the read-regexp function?
>> Maybe better to add a new variable read-string-defaults-function.
> That sounds fine to me.
>> Then it could provide an option to use 'car' of
>> query-replace-read-from-suggestions, and allow a custom function.
> ...less confident about this one, because read-regexp-defaults-function
> doesn't do anything like that, right? It might be nice if the options
> mirrored each other.
> But if it's too much of a bother right now, I could just add two
> hand-written prompts with default to xref-find-references-and-replace,
> without the fancy "arrow". It's not like the replacements history is going
> to see too much use for this command.
> In the meantime, I think you should install your latest patch now (without
> the xref.el part, since it doesn't work yet).

After thinking more about this, I can't find a possible use for
read-string-defaults-function, because every call of read-string
provides own default value.  Also using read-regexp-defaults-function
in query-replace-read-from is not the right thing either - when
the users already customized it for e.g. occur, it would be too
unexpected when it will use a tag at point instead of from->to
pairs in query-replace.

Since query-replace is a very special command, the most uncontroversial
thing to do for a conservative approach would be to add two specific
variables (that later could be turned into options when needed):

diff --git a/lisp/replace.el b/lisp/replace.el
index 689a3f77ed..91cd2b3420 100644
--- a/lisp/replace.el
+++ b/lisp/replace.el
@@ -186,6 +186,12 @@ query-replace--split-string
+(defvar query-replace-read-from-default nil
+  "Function to get default non-regexp value for `query-replace-read-from'.")
+(defvar query-replace-read-from-regexp-default nil
+  "Function to get default regexp value for `query-replace-read-from'.")
 (defun query-replace-read-from-suggestions ()
   "Return a list of standard suggestions for `query-replace-read-from'.
 By default, the list includes the active region, the identifier
@@ -234,7 +240,10 @@ query-replace-read-from
             (symbol-value query-replace-from-history-variable)))
           (minibuffer-allow-text-properties t) ; separator uses text-properties
-           (cond ((and query-replace-defaults separator)
+            (cond ((and query-replace-read-from-regexp-default regexp-flag) 
+                  ((and query-replace-read-from-default (not regexp-flag))
+                   (format-prompt prompt (funcall 
+                  ((and query-replace-defaults separator)
                    (format-prompt prompt (car minibuffer-history)))
@@ -255,10 +264,19 @@ query-replace-read-from
                                 (append '((separator . t) (face . t))
                 (if regexp-flag
-                    (read-regexp prompt nil 'minibuffer-history)
+                    (read-regexp
+                     (if query-replace-read-from-regexp-default
+                         (string-remove-suffix ": " prompt)
+                       prompt)
+                     query-replace-read-from-regexp-default
+                     'minibuffer-history)
                    prompt nil nil nil nil
-                   (query-replace-read-from-suggestions) t)))))
+                   (if query-replace-read-from-default
+                       (cons (funcall query-replace-read-from-default)
+                             (query-replace-read-from-suggestions))
+                     (query-replace-read-from-suggestions))
+                   t)))))
       (if (and (zerop (length from)) query-replace-defaults)
          (cons (caar query-replace-defaults)
Then you could use these variables like this:

diff --git a/lisp/progmodes/project.el b/lisp/progmodes/project.el
index c812f28c1b..f606a25575 100644
--- a/lisp/progmodes/project.el
+++ b/lisp/progmodes/project.el
@@ -1072,9 +1072,10 @@ project-query-replace-regexp
 If you exit the `query-replace', you can later continue the
 `query-replace' loop using the command \\[fileloop-continue]."
-   (pcase-let ((`(,from ,to)
-                (query-replace-read-args "Query replace (regexp)" t t)))
-     (list from to)))
+   (let ((query-replace-read-from-regexp-default 'find-tag-default-as-regexp))
+     (pcase-let ((`(,from ,to)
+                  (query-replace-read-args "Query replace (regexp)" t t)))
+       (list from to))))
    from to (project-files (project-current t)) 'default)
diff --git a/lisp/progmodes/xref.el b/lisp/progmodes/xref.el
index 37e2159782..4efa652084 100644
--- a/lisp/progmodes/xref.el
+++ b/lisp/progmodes/xref.el
@@ -1481,8 +1481,9 @@ xref-find-references
 (defun xref-find-references-and-replace (from to)
   "Replace all references to identifier FROM with TO."
-   (let ((common
-          (query-replace-read-args "Query replace identifier" nil)))
+   (let* ((query-replace-read-from-default 'find-tag-default)
+          (common
+           (query-replace-read-args "Query replace identifier" nil)))
      (list (nth 0 common) (nth 1 common))))
   (require 'xref)

reply via email to

[Prev in Thread] Current Thread [Next in Thread]