unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* [Patch] Add project.el command to replace symbol at point throughout project
@ 2022-01-11  7:45 Jon Eskin
  2022-01-11  7:51 ` Manuel Uberti
                   ` (2 more replies)
  0 siblings, 3 replies; 42+ messages in thread
From: Jon Eskin @ 2022-01-11  7:45 UTC (permalink / raw)
  To: emacs-devel


[-- Attachment #1.1: Type: text/plain, Size: 1404 bytes --]

Attached is a small patch adding command 'project-query-replace-at-point'
to project.el. The command is designed to improve the ergonomics of making
a project wide text replacement of a symbol at point.

Currently, if you want to make a project wide replacement of a symbol using
project.el, the best options I've found are:

- Mark the symbol you wish to replace
- Save symbol to kill ring with 'kill-ring-save'
- Enter command 'project-query-replace-regexp'
- Paste in the symbol, taking care to quote regex if necessary, and hit
return
- Enter the replacement string and hit return

or

- Mark the symbol you wish to replace
- Save symbol to kill ring with 'kill-ring-save'
- Place cursor on symbol and enter command 'project-find-regexp'
- Hit return at the next prompt to accept the default prompt
- Enter command 'xref-query-replace-in-results'
- Enter the replacement string and hit return

'project-query-replace-at-point' regex-quotes the symbol at point and then
calls into the fileloop-initialize-replace function used by the existing
project-query-replace-regexp command.

Replacing a symbol with 'project-query-replace-at-point' occurs as follows:

- Place cursor on symbol and enter command 'project-query-replace-at-point'
- Enter the replacement string and hit return

Let me know what you guys think. I haven't contributed before so please let
me know if I'm doing anything incorrectly.

[-- Attachment #1.2: Type: text/html, Size: 1596 bytes --]

[-- Attachment #2: 0001-Add-function-for-project-wide-replacement-of-symbol-.patch --]
[-- Type: text/x-patch, Size: 2934 bytes --]

From 9f962641c4e813e41d188f4000ab3f83ce3aba8e Mon Sep 17 00:00:00 2001
From: Jon Eskin <eskinjp@gmail.com>
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


^ permalink raw reply related	[flat|nested] 42+ messages in thread

end of thread, other threads:[~2022-02-02 21:09 UTC | newest]

Thread overview: 42+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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
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

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).