1

I tend to use query-replace-regexp over an entire buffer rather than at the current position so I regularly use the sequence C-< (beginning-of-buffer), then C-r (query-replace-repexp).

I'd like to make another function bound to C-S-r (C-R) which does this for me. I thought that if I simply wrapped it all together such as:

(defun query-replace-regexp-whole-buffer ()
  "query-replace-regexp from the beginning of the buffer."
  (interactive)
  (beginning-of-buffer)
  (query-replace-regexp))

that this would be adequate, unfortunately though I'm getting some errors.

query-replace-regexp-whole-buffer: Wrong number of arguments: #[(regexp to-string &optional delimited start end) "Å Æ
Ç&  " [regexp to-string delimited start end perform-replace t nil] 10 1940879 (let ((common (query-replace-read-args (concat "Query replace" (if current-prefix-arg " word" "") " regexp" (if (and transient-mark-mode mark-active) " in region" "")) t))) (list (nth 0 common) (nth 1 common) (nth 2 common) (if (and transient-mark-mode mark-active) (region-beginning)) (if (and transient-mark-mode mark-active) (region-end))))], 0

I can't really see what I'm doing wrong, hopefully someone can help.

2 Answers 2

3

When called from Lisp, query-replace-regexp expects to be passed regular expression and the intended replacement as arguments. If you want to emulate the questions asked when invoked interactively, you need to use call-interactively:

(defun query-replace-regexp-whole-buffer ()
  "query-replace-regexp from the beginning of the buffer."
  (interactive)
  (goto-char (point-min))
  (call-interactively 'query-replace-regexp))

Also note that one should never call beginning-of-buffer from Lisp code; it will do unnecessary work, such as pushing the mark and printing a message.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your help and also thats a good tip about not using beginning-of-buffer :)
2

You need to read arguments yourself and pass them to query-replace-regexp... This could be done by extending your interactive, so function will look something like:

(defun query-replace-regexp-whole-buffer (regex to-string)
  "query-replace-regexp from the beginning of the buffer."
  (interactive "sRegex to search: \nsString to replace: ")
  (save-excursion
     (goto-char (point-min))
     (query-replace-regexp regex to-string)))

5 Comments

just noticed another answer - this could be better for you, as you don't need to explicitly read arguments
Thanks for your help, this is also interesting to know how the interactive statement works. I may have to look at elisp in a bit more detail.
save-excursion is a good idea, and so is showing the use of interactive. One downside compared to my answer is that you don't get the fancy prompting that query-replace-regexp performs interactively. (For the OP: use M-x find-function RET query-replace-regexp RET to navigate to the source of query-replace-regexp, and don't forget to install the elisp sources package if you haven't already.)
yes, I thought about pointing to original source, but decided that it could be too complicated for author of question :-)
This is actually very useful to me since a lot of my regexp replaces are the same. Emacs is so helpful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.