unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: Juri Linkov <juri@linkov.net>
To: Dmitry Gutov <dgutov@yandex.ru>
Cc: Jon Eskin <eskinjp@gmail.com>, Eli Zaretskii <eliz@gnu.org>,
	emacs-devel@gnu.org
Subject: Re: [Patch] Add project.el command to replace symbol at point throughout project
Date: Mon, 31 Jan 2022 20:25:29 +0200	[thread overview]
Message-ID: <86sft3kbja.fsf@mail.linkov.net> (raw)
In-Reply-To: <829a1d78-c07e-4bb8-e3bd-07263271fc30@yandex.ru> (Dmitry Gutov's message of "Fri, 21 Jan 2022 05:06:06 +0200")

[-- Attachment #1: Type: text/plain, Size: 1761 bytes --]

>>> +                  (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):


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: query-replace-read-from-default.patch --]
[-- Type: text/x-diff, Size: 2515 bytes --]

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
                         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 @@ query-replace-read-from
 	     (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 @@ query-replace-read-from
                                 (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)

[-- Attachment #3: Type: text/plain, Size: 48 bytes --]


Then you could use these variables like this:


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #4: query-replace-read-from-regexp-default.patch --]
[-- Type: text/x-diff, Size: 1465 bytes --]

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]."
   (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 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."
   (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

  reply	other threads:[~2022-01-31 18:25 UTC|newest]

Thread overview: 42+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=86sft3kbja.fsf@mail.linkov.net \
    --to=juri@linkov.net \
    --cc=dgutov@yandex.ru \
    --cc=eliz@gnu.org \
    --cc=emacs-devel@gnu.org \
    --cc=eskinjp@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).