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?