From 9f962641c4e813e41d188f4000ab3f83ce3aba8e Mon Sep 17 00:00:00 2001 From: Jon Eskin Date: Mon, 10 Jan 2022 15:04:35 -0500 Subject: [PATCH] Add function for project-wide replacement of symbol at point. * lisp/progmodes/project.el (project-query-replace-at-point): New command. (project--query-replace-at-point-read-args): New function. * doc/emacs/maintaining.texi (Project File Commands): Document 'project-query-replace-at-point' --- doc/emacs/maintaining.texi | 2 ++ lisp/progmodes/project.el | 24 ++++++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/doc/emacs/maintaining.texi b/doc/emacs/maintaining.texi index 9a23f23e0e..65ab5cdca5 100644 --- a/doc/emacs/maintaining.texi +++ b/doc/emacs/maintaining.texi @@ -1690,6 +1690,8 @@ Project File Commands @item M-x project-search Interactively search for regexp matches in all files that belong to the current project. +@item M-x project-query-replace-at-point +Perform query-replace for the symbol at point. @item C-x p r Perform query-replace for a regexp in all files that belong to the current project (@code{project-query-replace-regexp}). diff --git a/lisp/progmodes/project.el b/lisp/progmodes/project.el index eda19c46a3..acc6013066 100644 --- a/lisp/progmodes/project.el +++ b/lisp/progmodes/project.el @@ -849,6 +849,16 @@ project--read-regexp (read-regexp "Find regexp" (and sym (regexp-quote sym)) project-regexp-history-variable))) +(defun project--query-replace-at-point-read-args () + "Prompt for a string name to replace the symbol at point in the project." + (let* ((sym (thing-at-point 'symbol t)) + (regex-quoted-sym (and sym (regexp-quote sym))) + (replace (query-replace-read-to + regex-quoted-sym + "Throughout project replace symbol:" + nil))) + (list regex-quoted-sym replace))) + ;;;###autoload (defun project-find-file (&optional include-all) "Visit a file (with completion) in the current project. @@ -1065,6 +1075,20 @@ project-search regexp (project-files (project-current t)) 'default) (fileloop-continue)) +;;;###autoload +(defun project-query-replace-at-point (thing-at-point replacement) + "Query-replace THING-AT-POINT in all files of the project. +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 ((`(,thing-at-point ,replacement) + (project--query-replace-at-point-read-args))) + (list thing-at-point replacement))) + (fileloop-initialize-replace + thing-at-point replacement (project-files (project-current t)) 'default) + (fileloop-continue)) + ;;;###autoload (defun project-query-replace-regexp (from to) "Query-replace REGEXP in all the files of the project. -- 2.34.1