Since version 22 of Emacs, we can use \,(function) for manipualting (parts of) the regex-search result before replacing it. But – this is mentioned often, but nonetheless still the truth – we can use this construct only in the standard interactive way. (Interactive like: By pressing C-M-% or calling query-replace-regexp with M-x.)
As an example:
If we have
- [Foo Bar 1900]
and want to get
- [Foo Bar \function{foo1900}{1900}]
we can use:
M-x query-replace-regexp <return>
\[\([A-Za-z-]+\)\([^0-9]*\) \([0-9]\{4\}\)\]
[\1\2 \\function{\,(downcase \1)\3}{\3}]
to get it done. So this can be done pretty easy.
In my own defun, I can use query only by replacing without freely modifying the match, or modify the prepared replaced string without any querying. The only way I see, is to serialize it in such a way:
(defun form-to-function ()
(interactive)
(goto-char (point-min))
(while (query-replace-regexp
"\\[\\([A-Za-z-]+\\)\\([^0-9]*\\) \\([0-9]\\{4\\}\\)\\]"
"[\\1\\2 \\\\function{\\1\\3}{\\3}]" ))
(goto-char (point-min))
(while (search-forward-regexp "\\([a-z0-9]\\)" nil t)
(replace-match (downcase (match-string 1)) t nil)
)
)
For me the query is important, because I can't be sure, what the buffer offers me (= I can't be sure, the author used this kind of string always in the same manner).
I want to use an elisp function, because it is not the only recurring replacement (and also not only one buffer (I know about dired-do-query-replace-regexp but I prefer working buffer-by-buffer with replace-defuns)).
At first I thought I only miss something like a query-replace-match to use instead of replace-match. But I fear, I am also missing the easy and flexible way of rearrange the string the the query-replace-regexp.
So I think, I need a \, for use in an defun. And I really wonder, if I am the only one, who is missing this feature.
query-replace-regexpis for interactive use only. In lisp code one usesread-keyory-or-n-pto query the user whether to replace the text or not. AFAIK, the only reasonable way to modify the behaviour ofquery-replace-regexptemporarily is to letreplace-re-search-functionto a function which replaces the action ofre-search-forwardinquery-replace-regexp. Note, you should replace(goto-char 1)with(goto-char (point-min))in your code. That leaves the user more freedom to restrict the area to be processed.query-replace-regexpin a defun, also it is not suggested. (Perhaps because a query is not thought as a wish?) But anyway, I fear I wasn't precise enough. I don't want to modify the behaviour ofquery-replace-regexp, I want to use the functionality I have with [C-M-%] combined with\,in a defun. In other words: I'm searching for an easy to use search and manipulate replace function for my defun. Thank you very much, also for pointing me to use(point-min)instead of just1. I refined my question.read-keyandy-or-no-pjust asks for approval but doesn't present the highlighted match and moreover doesn't show the resulting replacement in the minibuffer, which is very convenient.