Function: isearch-query-replace

isearch-query-replace is an interactive and byte-compiled function defined in isearch.el.gz.

Signature

(isearch-query-replace &optional ARG REGEXP-FLAG)

Documentation

Start query-replace with string to replace from last search string.

The ARG (prefix arg if interactive), if non-nil, means replace only matches surrounded by word boundaries. A negative prefix arg means replace backward. Note that using the prefix arg is possible only when isearch-allow-scroll is non-nil or isearch-allow-prefix is non-nil, and it doesn't always provide the correct matches for query-replace, so the preferred way to run word replacements from Isearch is M-s w ... M-%.

As each match is found, the user must type a character saying what to do with it. Type SPC or y to replace the match, DEL or n to skip and go to the next match. For more directions, type <f1> (help-command) at that time.

View in manual

Key Bindings

Source Code

;; Defined in /usr/src/emacs/lisp/isearch.el.gz
(defun isearch-query-replace (&optional arg regexp-flag)
  "Start `query-replace' with string to replace from last search string.
The ARG (prefix arg if interactive), if non-nil, means replace
only matches surrounded by word boundaries.  A negative prefix
arg means replace backward.  Note that using the prefix arg
is possible only when `isearch-allow-scroll' is non-nil or
`isearch-allow-prefix' is non-nil, and it doesn't always provide the
correct matches for `query-replace', so the preferred way to run word
replacements from Isearch is `M-s w ... M-%'.

As each match is found, the user must type a character saying
what to do with it.  Type SPC or `y' to replace the match,
DEL or `n' to skip and go to the next match.  For more directions,
type \\[help-command] at that time."
  (interactive
   (list current-prefix-arg))
  (barf-if-buffer-read-only)
  (if regexp-flag (setq isearch-regexp t))
  (let ((case-fold-search isearch-case-fold-search)
	;; set `search-upper-case' to nil to not call
	;; `isearch-no-upper-case-p' in `perform-replace'
	(search-upper-case nil)
	(search-invisible isearch-invisible)
	(replace-lax-whitespace
	 isearch-lax-whitespace)
	(replace-regexp-lax-whitespace
	 isearch-regexp-lax-whitespace)
	(delimited (and arg (not (eq arg '-))))
	(backward (and arg (eq arg '-)))
	;; Set `isearch-recursive-edit' to nil to prevent calling
	;; `exit-recursive-edit' in `isearch-done' that terminates
	;; the execution of this command when it is non-nil.
	;; We call `exit-recursive-edit' explicitly at the end below.
	(isearch-recursive-edit nil)
	(isearch-string-propertized
         (isearch-string-propertize isearch-string)))
    (isearch-done nil t)
    (isearch-clean-overlays)
    (if (and isearch-other-end
	     (if backward
		 (> isearch-other-end (point))
	       (< isearch-other-end (point)))
             (not (and transient-mark-mode mark-active
                       (if backward
			   (> (mark) (point))
			 (< (mark) (point))))))
        (goto-char isearch-other-end))
    (set query-replace-from-history-variable
         (cons isearch-string-propertized
               (symbol-value query-replace-from-history-variable)))
    (perform-replace
     isearch-string-propertized
     (query-replace-read-to
      isearch-string-propertized
      (concat "Query replace"
              (isearch--describe-regexp-mode (or delimited isearch-regexp-function) t)
	      (if backward " backward" "")
	      (if (use-region-p) " in region" ""))
      isearch-regexp)
     t isearch-regexp (or delimited isearch-regexp-function) nil nil
     (use-region-beginning) (use-region-end)
     backward))
  (and isearch-recursive-edit (exit-recursive-edit)))