I am writing a piece of lisp code employing the function query-replace
. After this function runs, I'd like to know how many replacements were effectively done. At first this seems easy because the return value of query-replace
is a list whose elements precisely describe each attempted replacement, including whether or not the user answered y
or n
when queried.
However, in case a user replies with q
or RET
, signaling their intention to exit, the return value becomes nil
, even if a few replacements were performed before the exit request, so the return value says nothing about the number of replacements.
One solution I found is based on the fact that
the number of replacements is shown in the mini-buffer, as well as recorded in the *Messages*
buffer, regardless of whether or not the command exits prematurely. So one could parse the last line of the *Messages*
buffer in search for the desired number of replacements, but I feel there might be a more elegant alternative. Any ideas?
*Messages*
buffer. Thanks! As you say, I believe there is no way to obtain the value of a local variable, such asreplace-count
withinperform-replace
by means of some mechanism similar to advising a function, right? – Ruy Sep 29 '22 at 19:38perform-replace
, the fact that the change would have a cascading effect on several other functions, resulted in the decision that no change will be made. See https://debbugs.gnu.org/cgi/bugreport.cgi?bug=58177 – Ruy Oct 02 '22 at 18:27