unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#41692: 26.3; Enhancement request, `query-replace': let user use text at point as default
@ 2020-06-03 18:35 Drew Adams
  2020-06-03 22:54 ` Juri Linkov
  0 siblings, 1 reply; 10+ messages in thread
From: Drew Adams @ 2020-06-03 18:35 UTC (permalink / raw)
  To: 41692

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''





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

* bug#41692: 26.3; Enhancement request, `query-replace': let user use text at point as default
  2020-06-03 18:35 bug#41692: 26.3; Enhancement request, `query-replace': let user use text at point as default Drew Adams
@ 2020-06-03 22:54 ` Juri Linkov
  2020-06-03 23:22   ` Drew Adams
  0 siblings, 1 reply; 10+ messages in thread
From: Juri Linkov @ 2020-06-03 22:54 UTC (permalink / raw)
  To: Drew Adams; +Cc: 41692

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





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

* bug#41692: 26.3; Enhancement request, `query-replace': let user use text at point as default
  2020-06-03 22:54 ` Juri Linkov
@ 2020-06-03 23:22   ` Drew Adams
  2020-06-04 22:05     ` Juri Linkov
  0 siblings, 1 reply; 10+ messages in thread
From: Drew Adams @ 2020-06-03 23:22 UTC (permalink / raw)
  To: Juri Linkov; +Cc: 41692

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





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

* bug#41692: 26.3; Enhancement request, `query-replace': let user use text at point as default
  2020-06-03 23:22   ` Drew Adams
@ 2020-06-04 22:05     ` Juri Linkov
  2020-06-04 23:01       ` Drew Adams
  0 siblings, 1 reply; 10+ messages in thread
From: Juri Linkov @ 2020-06-04 22:05 UTC (permalink / raw)
  To: Drew Adams; +Cc: 41692

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





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

* bug#41692: 26.3; Enhancement request, `query-replace': let user use text at point as default
  2020-06-04 22:05     ` Juri Linkov
@ 2020-06-04 23:01       ` Drew Adams
  2020-06-11 22:58         ` Juri Linkov
  0 siblings, 1 reply; 10+ messages in thread
From: Drew Adams @ 2020-06-04 23:01 UTC (permalink / raw)
  To: Juri Linkov; +Cc: 41692

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





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

* bug#41692: 26.3; Enhancement request, `query-replace': let user use text at point as default
  2020-06-04 23:01       ` Drew Adams
@ 2020-06-11 22:58         ` Juri Linkov
  2020-06-12  0:34           ` Drew Adams
  0 siblings, 1 reply; 10+ messages in thread
From: Juri Linkov @ 2020-06-11 22:58 UTC (permalink / raw)
  To: Drew Adams; +Cc: 41692

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

>> 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':


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

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)

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

* bug#41692: 26.3; Enhancement request, `query-replace': let user use text at point as default
  2020-06-11 22:58         ` Juri Linkov
@ 2020-06-12  0:34           ` Drew Adams
  2020-06-12  0:46             ` Drew Adams
  0 siblings, 1 reply; 10+ messages in thread
From: Drew Adams @ 2020-06-12  0:34 UTC (permalink / raw)
  To: Juri Linkov; +Cc: 41692

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





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

* bug#41692: 26.3; Enhancement request, `query-replace': let user use text at point as default
  2020-06-12  0:34           ` Drew Adams
@ 2020-06-12  0:46             ` Drew Adams
  2021-01-27 18:33               ` Juri Linkov
  0 siblings, 1 reply; 10+ messages in thread
From: Drew Adams @ 2020-06-12  0:46 UTC (permalink / raw)
  To: Juri Linkov; +Cc: 41692

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





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

* bug#41692: 26.3; Enhancement request, `query-replace': let user use text at point as default
  2020-06-12  0:46             ` Drew Adams
@ 2021-01-27 18:33               ` Juri Linkov
  2021-01-27 18:55                 ` bug#41692: [External] : " Drew Adams
  0 siblings, 1 reply; 10+ messages in thread
From: Juri Linkov @ 2021-01-27 18:33 UTC (permalink / raw)
  To: Drew Adams; +Cc: 41692

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.





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

* bug#41692: [External] : Re: bug#41692: 26.3; Enhancement request, `query-replace': let user use text at point as default
  2021-01-27 18:33               ` Juri Linkov
@ 2021-01-27 18:55                 ` Drew Adams
  0 siblings, 0 replies; 10+ messages in thread
From: Drew Adams @ 2021-01-27 18:55 UTC (permalink / raw)
  To: Juri Linkov; +Cc: 41692@debbugs.gnu.org

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





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

end of thread, other threads:[~2021-01-27 18:55 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-06-03 18:35 bug#41692: 26.3; Enhancement request, `query-replace': let user use text at point as default Drew Adams
2020-06-03 22:54 ` Juri Linkov
2020-06-03 23:22   ` Drew Adams
2020-06-04 22:05     ` Juri Linkov
2020-06-04 23:01       ` Drew Adams
2020-06-11 22:58         ` Juri Linkov
2020-06-12  0:34           ` Drew Adams
2020-06-12  0:46             ` Drew Adams
2021-01-27 18:33               ` Juri Linkov
2021-01-27 18:55                 ` bug#41692: [External] : " Drew Adams

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