From 749ba35bf5e0974384550a0763fd27f08db0678c Mon Sep 17 00:00:00 2001 From: Juri Linkov Date: Tue, 1 Feb 2022 22:07:20 +0200 Subject: [PATCH] * lisp/replace.el (query-replace-read-from-default): New variable. (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 | 11 +++++++++++ lisp/progmodes/project.el | 7 ++++--- lisp/progmodes/xref.el | 5 +++-- lisp/replace.el | 24 +++++++++++++++++++++--- 4 files changed, 39 insertions(+), 8 deletions(-) diff --git a/etc/NEWS b/etc/NEWS index 5d4a59975c2..c2975b783d1 100644 --- 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 diff --git a/lisp/progmodes/project.el b/lisp/progmodes/project.el index c812f28c1bb..f606a25575e 100644 --- a/lisp/progmodes/project.el +++ b/lisp/progmodes/project.el @@ -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)) diff --git a/lisp/progmodes/xref.el b/lisp/progmodes/xref.el index 37e2159782f..4efa6520847 100644 --- a/lisp/progmodes/xref.el +++ b/lisp/progmodes/xref.el @@ -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 diff --git a/lisp/replace.el b/lisp/replace.el index 689a3f77ed3..91cd2b34205 100644 --- a/lisp/replace.el +++ b/lisp/replace.el @@ -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) -- 2.39.5