GNU bug report logs - #41692
26.3; Enhancement request, `query-replace': let user use text at point as default

Previous Next

Package: emacs;

Reported by: Drew Adams <drew.adams <at> oracle.com>

Date: Wed, 3 Jun 2020 18:36:01 UTC

Severity: wishlist

Tags: fixed

Found in version 26.3

Fixed in version 28.0.50

Done: Juri Linkov <juri <at> linkov.net>

Bug is archived. No further changes may be made.

To add a comment to this bug, you must first unarchive it, by sending
a message to control AT debbugs.gnu.org, with unarchive 41692 in the body.
You can then email your comments to 41692 AT debbugs.gnu.org in the normal way.

Toggle the display of automated, internal messages from the tracker.

View this report as an mbox folder, status mbox, maintainer mbox


Report forwarded to bug-gnu-emacs <at> gnu.org:
bug#41692; Package emacs. (Wed, 03 Jun 2020 18:36:02 GMT) Full text and rfc822 format available.

Acknowledgement sent to Drew Adams <drew.adams <at> oracle.com>:
New bug report received and forwarded. Copy sent to bug-gnu-emacs <at> gnu.org. (Wed, 03 Jun 2020 18:36:02 GMT) Full text and rfc822 format available.

Message #5 received at submit <at> debbugs.gnu.org (full text, mbox):

From: Drew Adams <drew.adams <at> oracle.com>
To: bug-gnu-emacs <at> gnu.org
Subject: 26.3; Enhancement request, `query-replace': let user use text at
 point as default
Date: Wed, 3 Jun 2020 11:35:41 -0700 (PDT)
If you want to default the FROM or TO text for `query-replace' to some
text at point, you currently need to first isearch for it.  This request
is essentially to be able to skip doing that.

As an example (only), in my own code, my version of
`query-replace-read-from' can get the default any of these ways:

* Text of nonempty active region, if option
  `search/replace-region-as-default-flag' is non-nil.

* Text of secondary selection, if option
  `search/replace-2nd-sel-as-default-flag' is non-nil.

* What the (function) value of option `search/replace-default-fn'
  returns, when called.  This defaults to a function that grabs text at
  point.  (If the function returns a list of strings, then those are the
  multiple default values.)

(As with vanilla query-replace, you can also use the history lists,
and you can enter nothing to repeat the previous query replacement
operation.)

I don't say that vanilla Emacs should do all of that.  But maybe at
least something like the third one - a function that returns a string or
list of strings to be used as default values, and with, as its default
value, a function that grabs some text at point.

In GNU Emacs 26.3 (build 1, x86_64-w64-mingw32)
 of 2019-08-29
Repository revision: 96dd0196c28bc36779584e47fffcca433c9309cd
Windowing system distributor `Microsoft Corp.', version 10.0.18362
Configured using:
 `configure --without-dbus --host=x86_64-w64-mingw32
 --without-compress-install 'CFLAGS=-O2 -static -g3''




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#41692; Package emacs. (Wed, 03 Jun 2020 23:06:03 GMT) Full text and rfc822 format available.

Message #8 received at 41692 <at> debbugs.gnu.org (full text, mbox):

From: Juri Linkov <juri <at> linkov.net>
To: Drew Adams <drew.adams <at> oracle.com>
Cc: 41692 <at> debbugs.gnu.org
Subject: Re: bug#41692: 26.3; Enhancement request, `query-replace': let user
 use text at point as default
Date: Thu, 04 Jun 2020 01:54:47 +0300
> If you want to default the FROM or TO text for `query-replace' to some
> text at point, you currently need to first isearch for it.  This request
> is essentially to be able to skip doing that.
>
> As an example (only), in my own code, my version of
> `query-replace-read-from' can get the default any of these ways:
>
> * Text of nonempty active region, if option
>   `search/replace-region-as-default-flag' is non-nil.
>
> * Text of secondary selection, if option
>   `search/replace-2nd-sel-as-default-flag' is non-nil.
>
> * What the (function) value of option `search/replace-default-fn'
>   returns, when called.  This defaults to a function that grabs text at
>   point.  (If the function returns a list of strings, then those are the
>   multiple default values.)
>
> (As with vanilla query-replace, you can also use the history lists,
> and you can enter nothing to repeat the previous query replacement
> operation.)
>
> I don't say that vanilla Emacs should do all of that.  But maybe at
> least something like the third one - a function that returns a string or
> list of strings to be used as default values, and with, as its default
> value, a function that grabs some text at point.

query-replace-regexp already does all the above.  It provides text
at point for first M-n, the last search string for the second M-n, etc.




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#41692; Package emacs. (Wed, 03 Jun 2020 23:24:01 GMT) Full text and rfc822 format available.

Message #11 received at 41692 <at> debbugs.gnu.org (full text, mbox):

From: Drew Adams <drew.adams <at> oracle.com>
To: Juri Linkov <juri <at> linkov.net>
Cc: 41692 <at> debbugs.gnu.org
Subject: RE: bug#41692: 26.3; Enhancement request, `query-replace': let user
 use text at point as default
Date: Wed, 3 Jun 2020 16:22:52 -0700 (PDT)
> query-replace-regexp already does all the above.  It provides text
> at point for first M-n, the last search string for the second M-n, etc.

The bug report was about `query-replace' (but also other,
related commands, if relevant).  

Checking with the Emacs 27 pretest, as far as I can see:

1. There's NO such defaulting for `query-replace'.  Nada.

2. `query-replace-regexp' does NOT do "all of the above".
   It doesn't use the active region.

   It doesn't use the secondary selection.

   It doesn't use a function variable does it?  I mean a
   variable whose value is a function that returns some
   particular bunch of text at point.  Something similar
   to the option `search/replace-default-fn' I mentioned.

I see, for `C-M-%', only these two defaults:

1. A word at point (e.g. `foo'),
2. A regexp that matches a symbol at point (e.g. `\_<foo\_>'.

But yes, that's better than nothing - a good start.
`query-replace' could likewise use some love.




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#41692; Package emacs. (Thu, 04 Jun 2020 22:24:02 GMT) Full text and rfc822 format available.

Message #14 received at 41692 <at> debbugs.gnu.org (full text, mbox):

From: Juri Linkov <juri <at> linkov.net>
To: Drew Adams <drew.adams <at> oracle.com>
Cc: 41692 <at> debbugs.gnu.org
Subject: Re: bug#41692: 26.3; Enhancement request, `query-replace': let user
 use text at point as default
Date: Fri, 05 Jun 2020 01:05:27 +0300
> I see, for `C-M-%', only these two defaults:
>
> 1. A word at point (e.g. `foo'),
> 2. A regexp that matches a symbol at point (e.g. `\_<foo\_>'.
>
> But yes, that's better than nothing - a good start.
> `query-replace' could likewise use some love.

I agree that `query-replace' should at least do the same as
`query-replace-regexp', and `query-replace-regexp' could be extended.
Currently `query-replace-read-from' has:

                (if regexp-flag
                    (read-regexp prompt nil 'minibuffer-history)
                  (read-from-minibuffer
                   prompt nil nil nil nil (car search-ring) t))

It's easy to extend `read-regexp' with more options,
but what to do for non-regexp case?

Maybe to try to use `read-string' and extend it with more M-n options
like in `read-regexp'.




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#41692; Package emacs. (Thu, 04 Jun 2020 23:02:02 GMT) Full text and rfc822 format available.

Message #17 received at 41692 <at> debbugs.gnu.org (full text, mbox):

From: Drew Adams <drew.adams <at> oracle.com>
To: Juri Linkov <juri <at> linkov.net>
Cc: 41692 <at> debbugs.gnu.org
Subject: RE: bug#41692: 26.3; Enhancement request, `query-replace': let user
 use text at point as default
Date: Thu, 4 Jun 2020 16:01:21 -0700 (PDT)
> Currently `query-replace-read-from' has:
>   (if regexp-flag
>       (read-regexp prompt nil 'minibuffer-history)
>     (read-from-minibuffer
>       prompt nil nil nil nil (car search-ring) t))
> 
> It's easy to extend `read-regexp' with more options,
> but what to do for non-regexp case?
> 
> Maybe to try to use `read-string' and extend it with
> more M-n options like in `read-regexp'.

I don't understand the question.  `read-from-minibuffer'
accepts a list of default values.  Or maybe I don't
understand what you mean by "more options".

I think I referred you to my code in `replace+.el'.
Here it is, in case I didn't:

https://www.emacswiki.org/emacs/download/replace%2b.el

In my code for `query-replace-read-from' I determine
the default value(s) by evaluating this:
(search/replace-default
  (symbol-value query-replace-from-history-variable))

I use function `search/replace-default' in lots of
search and replace functions.  It's the place where
this logic is used to get default values:

 Return a default value or list of such for search &
 replace functions.
 A list of default input strings is computed and
 returned.  The possible strings are, in order:

 * The active region, if option
   `search/replace-region-as-default-flag' is non-nil.

 * The secondary selection, if option
   `search/replace-2nd-sel-as-default-flag' is non-nil.

 * The result of calling the value of option
   `search/replace-default-fn', if non-nil.

 * The first entry in the history list HISTORY.

Option `search/replace-default-fn' lets users decide
what function to use, to grab buffer text as the
default.  The option's default value is a function
that grabs the non-nil symbol name that's nearest point.
If that function isn't available then the default value
is `word-at-point'.

HTH.




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#41692; Package emacs. (Thu, 11 Jun 2020 23:39:03 GMT) Full text and rfc822 format available.

Message #20 received at 41692 <at> debbugs.gnu.org (full text, mbox):

From: Juri Linkov <juri <at> linkov.net>
To: Drew Adams <drew.adams <at> oracle.com>
Cc: 41692 <at> debbugs.gnu.org
Subject: Re: bug#41692: 26.3; Enhancement request, `query-replace': let user
 use text at point as default
Date: Fri, 12 Jun 2020 01:58:36 +0300
[Message part 1 (text/plain, inline)]
>> Currently `query-replace-read-from' has:
>>   (if regexp-flag
>>       (read-regexp prompt nil 'minibuffer-history)
>>     (read-from-minibuffer
>>       prompt nil nil nil nil (car search-ring) t))
>> 
>> It's easy to extend `read-regexp' with more options,
>> but what to do for non-regexp case?
>> 
>> Maybe to try to use `read-string' and extend it with
>> more M-n options like in `read-regexp'.
>
> I don't understand the question.  `read-from-minibuffer'
> accepts a list of default values.  Or maybe I don't
> understand what you mean by "more options".

Right, let's add more default values to `read-from-minibuffer':

[query-replace-read-from-defaults.patch (text/x-diff, inline)]
diff --git a/lisp/replace.el b/lisp/replace.el
index 69092c16f9..a5a5200df7 100644
--- a/lisp/replace.el
+++ b/lisp/replace.el
@@ -227,7 +227,16 @@ query-replace-read-from
                 (if regexp-flag
                     (read-regexp prompt nil 'minibuffer-history)
                   (read-from-minibuffer
-                   prompt nil nil nil nil (car search-ring) t)))))
+                   prompt nil nil nil nil
+                   (delq nil
+                         (list
+                          (when (use-region-p)
+                            (buffer-substring-no-properties
+                             (region-beginning) (region-end)))
+                          (find-tag-default)
+                          (car search-ring)
+                          (car (symbol-value query-replace-from-history-variable))))
+                   t)))))
            (to))
       (if (and (zerop (length from)) query-replace-defaults)
 	  (cons (caar query-replace-defaults)
@@ -798,6 +807,9 @@ read-regexp-suggestions
 appends the list returned by this function to the end of values available
 via \\<minibuffer-local-map>\\[next-history-element]."
   (list
+   (when (use-region-p)
+     (buffer-substring-no-properties
+      (region-beginning) (region-end)))
    (find-tag-default-as-regexp)
    (find-tag-default-as-symbol-regexp)
    (car regexp-search-ring)

Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#41692; Package emacs. (Fri, 12 Jun 2020 00:37:01 GMT) Full text and rfc822 format available.

Message #23 received at 41692 <at> debbugs.gnu.org (full text, mbox):

From: Drew Adams <drew.adams <at> oracle.com>
To: Juri Linkov <juri <at> linkov.net>
Cc: 41692 <at> debbugs.gnu.org
Subject: RE: bug#41692: 26.3; Enhancement request, `query-replace': let user
 use text at point as default
Date: Thu, 11 Jun 2020 17:34:21 -0700 (PDT)
> > `read-from-minibuffer' accepts a list of default
> > values.  Or maybe I don't understand what you
> > mean by "more options".
> 
> Right, let's add more default values to `read-from-minibuffer'

Yes, but not in a hard-coded way.  What I'd suggest
instead:

Provide a user option, to let users choose the list
of default sources (and thus also their order).

The option value could be a `repeat' of a `choice'
among: (1) nonempty active region, (2) secondary
selection, (3) result of calling the function that's
the value of a predefined option, (4) the most recent
entry in the query-replace history variable - and
whatever else you want to include.

#1-4 are what I use in my code.

Both my code and your code provide a hard-coded list.
But letting a user choose the list is better.

Your list includes `(find-tag-default)' and the first
entry of `search-ring'.  And your list doesn't include
the secondary selection or the result of funcalling
a function-valued variable (option).

I don't have a problem with your adding more choices
to the default value of the list option.

I do mind that you've removed those two (second sel
and function) from your list.  It's especially
important to include a funcallable option value.

So the two changes I'd like to see to your code are:
(1) add an option that lets users specify a list of
sexps to evaluate to provide the list of default
values, and (2) include secondary selection and
(especially) a function-valued user option among the
list-element `choice's.

But another consideration: `query-replace-read-from'
is used for several kinds of query-replacing,
including for `query-replace-regexp'.  For that
command, `regexp-search-ring', is more appropriate
than `search-ring'.

The option value `choice' could thus include, not a
hard-coded `search-ring', but a variable value, so
`query-replace-regexp' could bind that variable to
`regexp-search-ring', and `query-replace' could bind
it to `search-ring'.

IOW:

* One option for a user-chosen list of sources of
  default values.  Easy for a user to order the
  list, make it smaller, and change any of the
  choices that use a variable.

* One function-valued option, for a user-chosen
  function, to be funcalled with no args to produce
  a default value.

* One defvar whose value is used as a default value.

I'd actually prefer a user option for the last one
(the defvar), but I know that vanilla Emacs doesn't
like to let a command bind a user option.




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#41692; Package emacs. (Fri, 12 Jun 2020 00:47:02 GMT) Full text and rfc822 format available.

Message #26 received at 41692 <at> debbugs.gnu.org (full text, mbox):

From: Drew Adams <drew.adams <at> oracle.com>
To: Juri Linkov <juri <at> linkov.net>
Cc: 41692 <at> debbugs.gnu.org
Subject: RE: bug#41692: 26.3; Enhancement request, `query-replace': let user
 use text at point as default
Date: Thu, 11 Jun 2020 17:46:16 -0700 (PDT)
Sorry, the defvar I suggested is not good, at
least for picking up the car of `search-ring' or
`regexp-search-ring'.  Wasn't thinking straight.

Maybe either another function-valued variable
(to be called) or a sexp-valued variable (to be
evalled).

The main suggestion is more user control over
(1) the list of default sources and (2) some
of the choices (e.g. one or two function-valued
vars).




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#41692; Package emacs. (Wed, 27 Jan 2021 18:35:02 GMT) Full text and rfc822 format available.

Message #29 received at 41692 <at> debbugs.gnu.org (full text, mbox):

From: Juri Linkov <juri <at> linkov.net>
To: Drew Adams <drew.adams <at> oracle.com>
Cc: 41692 <at> debbugs.gnu.org
Subject: Re: bug#41692: 26.3; Enhancement request, `query-replace': let user
 use text at point as default
Date: Wed, 27 Jan 2021 20:33:46 +0200
tags 41692 fixed
close 41692 28.0.50
thanks

> Maybe either another function-valued variable
> (to be called) or a sexp-valued variable (to be
> evalled).

Thanks for the suggestion.  Now a new function
query-replace-read-from-suggestions is added that
is easy to redefine like read-regexp-suggestions.




Added tag(s) fixed. Request was from Juri Linkov <juri <at> linkov.net> to control <at> debbugs.gnu.org. (Wed, 27 Jan 2021 18:35:02 GMT) Full text and rfc822 format available.

bug marked as fixed in version 28.0.50, send any further explanations to 41692 <at> debbugs.gnu.org and Drew Adams <drew.adams <at> oracle.com> Request was from Juri Linkov <juri <at> linkov.net> to control <at> debbugs.gnu.org. (Wed, 27 Jan 2021 18:35:02 GMT) Full text and rfc822 format available.

Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#41692; Package emacs. (Wed, 27 Jan 2021 18:58:01 GMT) Full text and rfc822 format available.

Message #36 received at 41692 <at> debbugs.gnu.org (full text, mbox):

From: Drew Adams <drew.adams <at> oracle.com>
To: Juri Linkov <juri <at> linkov.net>
Cc: "41692 <at> debbugs.gnu.org" <41692 <at> debbugs.gnu.org>
Subject: RE: [External] : Re: bug#41692: 26.3; Enhancement request,
 `query-replace': let user use text at point as default
Date: Wed, 27 Jan 2021 18:55:42 +0000
> > Maybe either another function-valued variable
> > (to be called) or a sexp-valued variable (to be
> > evalled).
> 
> Thanks for the suggestion.  Now a new function
> query-replace-read-from-suggestions is added that
> is easy to redefine like read-regexp-suggestions.

I'll take your word for it, I guess.

The doc (and behavior) of `read-regexp-suggestions'
is a convoluted nightmare, IMHO.  Hope that's not
the case for your proposal for query-replace.




bug archived. Request was from Debbugs Internal Request <help-debbugs <at> gnu.org> to internal_control <at> debbugs.gnu.org. (Thu, 25 Feb 2021 12:24:13 GMT) Full text and rfc822 format available.

This bug report was last modified 3 years and 51 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.