[IDEA] Use interactive prefix arg for number of pairs - #2
Conversation
|
Indeed, we've thought a little bit about the idea after it was suggested during the Q&A, but then remembered that sadly the prefix argument is already in use by We've also thought of having a However, here's a piece of advice that you can drop into your config that adds the functionality you want. The approach is the same except that (1) I make sure to ignore the returned (define-advice query-replace-parallel--prompt
(:override (regexp-flag n) hok/count-arg)
(concat "Query replace parallel"
(and regexp-flag " regexp")
(and (use-region-p) " in region")
(and n (format " (%d)" n))))
(define-advice query-replace-parallel--read-args
(:override (regexp-flag) hok/count-arg)
(let ((n (prefix-numeric-value current-prefix-arg)))
(cl-loop for i from n downto 1
for (from to _delim _backward)
= (query-replace-read-args
(query-replace-parallel--prompt regexp-flag (and (> n 1) i))
regexp-flag)
for pair = (cons from to)
collect pair into pairs
finally (cl-return (list pairs nil nil))))) |
|
We've also thought of having a transient menu like you
suggested, but concluded that we didn't want to pull in a whole
another dependency for a use case of unknown frequency. For
now we prefer to keep the interface consistent with query-replace
to reduce surprises for anyone who's got years of muscle
memory. :-)
I agree!
However, here's a piece of advice that you can drop into your
config that adds the functionality you want. The approach is the
same except that (1) I make sure to ignore the returned delim and
backward flags, and (2) I also added a counter that shows you how
many pairs are still to be input (shown only when n > 1).
(define-advice query-replace-parallel--prompt
(:override (regexp-flag n) hok/count-arg)
(concat "Query replace parallel"
(and regexp-flag " regexp")
(and (use-region-p) " in region")
(and n (format " (%d)" n))))
(define-advice query-replace-parallel--read-args
(:override (regexp-flag) hok/count-arg)
(let ((n (prefix-numeric-value current-prefix-arg)))
(cl-loop for i from n downto 1
for (from to _delim _backward)
= (query-replace-read-args
(query-replace-parallel--prompt regexp-flag (and (> n 1) i))
regexp-flag)
for pair = (cons from to)
collect pair into pairs
finally (cl-return (list pairs nil nil)))))
This works great! Thank you!!
`delimited-flag' also affects lazy highlighting, so with a prefix arg,
query-replace-parallel only highlits whole-word matches while reading
args. Then, when performing replacements, `delimited-flag' is ignored.
This advice does the trick:
(require 'map)
(define-advice minibuffer-lazy-highlight-setup
(:filter-args (args) my/count-arg)
(map-delete args :regexp-function))
Note that this solution affects the lazy highlighting of normal
`query-replace' itself also, so C-u M-x query-replace highlights
individual characters, but then only replaces whole word matches.
|
|
Oh, good catch! I forgot that (define-advice query-replace-parallel--read-args
(:override (regexp-flag) hok/count-arg)
(let ((n (prefix-numeric-value current-prefix-arg)))
(cl-loop for i from n downto 1
for (from to delim backward)
= (let ((current-prefix-arg nil))
(query-replace-read-args
(query-replace-parallel--prompt regexp-flag (and (> n 1) i))
regexp-flag))
for pair = (cons from to)
collect pair into pairs
finally (cl-return (list pairs delim backward)))))This also keeps the effect local, so query-replace's usual lazy highlighting should continue to work. :-) |
|
That's a much better solution. Thank you! Might I suggest adding the two advices to the README for folks who come from the talk and are interested in using this package as a stand-in for |
|
You could also link to this comment, but then folks would have to use GitHub to discover your useful advices. |
|
I've added a small FAQ section to the README with instructions. Thanks! :-) |
|
Perfect! Thanks :) |
Inspired by the suggestion from your talk Q&A to use the prefix arg to determine how many times to prompt for replacements pairs.
This patch doesn't work because
current-prefix-argis already used inquery-replace:Seems like a niche use-case...
I still like the idea of having
query-replace-parallelbe a drop-in replacement forquery-replace. One potential option is to add atransientmenu that would allow users to specify the number of queries as well as a value forDELIMITEDand other parameters.That's definitely outside the scope of this project though :)
Thanks!!!