]> git.eshelyaron.com Git - emacs.git/commitdiff
* lisp/replace.el (query-replace-read-from-default): New variable.
authorJuri Linkov <juri@linkov.net>
Tue, 1 Feb 2022 20:07:20 +0000 (22:07 +0200)
committerJuri Linkov <juri@linkov.net>
Tue, 1 Feb 2022 20:08:22 +0000 (22:08 +0200)
(query-replace-read-from-regexp-default): New variable.
(query-replace-read-from): Use new variables.

* lisp/progmodes/project.el (project-query-replace-regexp):
Let-bind query-replace-read-from-regexp-default to find-tag-default-as-regexp.

* lisp/progmodes/xref.el (xref-find-references-and-replace):
Let-bind query-replace-read-from-default to find-tag-default.

https://lists.gnu.org/archive/html/emacs-devel/2022-01/msg01909.html

etc/NEWS
lisp/progmodes/project.el
lisp/progmodes/xref.el
lisp/replace.el

index 5d4a59975c29380f824252ebacd9df9584598399..c2975b783d1fb31bb5fb0376c30843fa8491e02a 100644 (file)
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -1237,6 +1237,17 @@ inhibits 'isearch' matching the STRING parameter.
 ** New function 'replace-regexp-function'.
 It can be used to implement own regexp syntax for search/replace.
 
+---
+** New variable 'query-replace-read-from-default'.
+It can be set to a function that returns the default value (such as
+'find-tag-default') when 'query-replace' reads a string to replace.
+Another new variable 'query-replace-read-from-regexp-default'
+can be set to a function that returns the default value (such as
+'find-tag-default-as-regexp') when 'query-replace-regexp'
+reads a regexp to replace.  When these variables are nil
+(which is the default) then 'query-replace' uses the previous
+replacement from-to pair as the default value.
+
 ---
 ** New user option 'pp-use-max-width'.
 If non-nil, 'pp' will attempt to limit the line length when formatting
index c812f28c1bb9322684adda08ae15d08ffc97e76d..f606a25575ef4a46a4d807dda59cdd94c2c3faba 100644 (file)
@@ -1072,9 +1072,10 @@ Stops when a match is found and prompts for whether to replace it.
 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 ((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))))
   (fileloop-initialize-replace
    from to (project-files (project-current t)) 'default)
   (fileloop-continue))
index 37e2159782f9bb5ae531f188155912ab84a54029..4efa6520847be69d5075d1ccfc5434861ee84aba 100644 (file)
@@ -1481,8 +1481,9 @@ is nil, prompt only if there's no usable symbol at point."
 (defun xref-find-references-and-replace (from to)
   "Replace all references to identifier FROM with TO."
   (interactive
-   (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)
   (with-current-buffer
index 689a3f77ed336c1501a122d9e2c0531b64622cfa..91cd2b342054030aee354b5a1d09320b2d9bb184 100644 (file)
@@ -186,6 +186,12 @@ See `replace-regexp' and `query-replace-regexp-eval'.")
                         length)
              length)))))
 
+(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 @@ wants to replace FROM with TO."
             (symbol-value query-replace-from-history-variable)))
           (minibuffer-allow-text-properties t) ; separator uses text-properties
           (prompt
-           (cond ((and query-replace-defaults separator)
+            (cond ((and query-replace-read-from-regexp-default regexp-flag) prompt)
+                  ((and query-replace-read-from-default (not regexp-flag))
+                   (format-prompt prompt (funcall query-replace-read-from-default)))
+                  ((and query-replace-defaults separator)
                    (format-prompt prompt (car minibuffer-history)))
                   (query-replace-defaults
                    (format-prompt
@@ -255,10 +264,19 @@ wants to replace FROM with TO."
                                 (append '((separator . t) (face . t))
                                         text-property-default-nonsticky)))
                 (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)
                   (read-from-minibuffer
                    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)))))
            (to))
       (if (and (zerop (length from)) query-replace-defaults)
          (cons (caar query-replace-defaults)