1 ;;; replace.el --- replace commands for Emacs
3 ;; Copyright (C) 1985, 86, 87, 92, 94, 96, 1997, 2000, 2001, 2002,
4 ;; 2003, 2004 Free Software Foundation, Inc.
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
27 ;; This package supplies the string and regular-expression replace functions
28 ;; documented in the Emacs user's manual.
32 (defcustom case-replace t
33 "*Non-nil means `query-replace' should preserve case in replacements."
37 (defvar query-replace-history nil
)
39 (defcustom query-replace-interactive nil
40 "Non-nil means `query-replace' uses the last search string.
41 That becomes the \"string to replace\".
42 If value is `initial', the last search string is inserted into
43 the minibuffer as an initial value for \"string to replace\"."
44 :type
'(choice (const :tag
"Off" nil
)
45 (const :tag
"Initial content" initial
)
46 (other :tag
"Use default value" t
))
49 (defcustom query-replace-from-history-variable
'query-replace-history
50 "History list to use for the FROM argument of `query-replace' commands.
51 The value of this variable should be a symbol; that symbol
52 is used as a variable to hold a history list for the strings
53 or patterns to be replaced."
58 (defcustom query-replace-to-history-variable
'query-replace-history
59 "History list to use for the TO argument of `query-replace' commands.
60 The value of this variable should be a symbol; that symbol
61 is used as a variable to hold a history list for replacement
67 (defcustom query-replace-skip-read-only nil
68 "*Non-nil means `query-replace' and friends ignore read-only matches."
73 (defun query-replace-read-args (string regexp-flag
&optional noerror
)
75 (barf-if-buffer-read-only))
77 (if (and query-replace-interactive
78 (not (eq query-replace-interactive
'initial
)))
79 (setq from
(car (if regexp-flag regexp-search-ring search-ring
)))
80 ;; The save-excursion here is in case the user marks and copies
81 ;; a region in order to specify the minibuffer input.
82 ;; That should not clobber the region for the query-replace itself.
84 (setq from
(read-from-minibuffer
85 (format "%s: " string
)
86 (if (eq query-replace-interactive
'initial
)
87 (car (if regexp-flag regexp-search-ring search-ring
)))
89 query-replace-from-history-variable
91 ;; Warn if user types \n or \t, but don't reject the input.
93 (string-match "\\(\\`\\|[^\\]\\)\\(\\\\\\\\\\)*\\(\\\\[nt]\\)" from
)
94 (let ((match (match-string 3 from
)))
96 ((string= match
"\\n")
97 (message "Note: `\\n' here doesn't match a newline; to do that, type C-q C-j instead"))
98 ((string= match
"\\t")
99 (message "Note: `\\t' here doesn't match a tab; to do that, just type TAB")))
103 (setq to
(read-from-minibuffer
104 (format "%s %s with: " string from
)
106 query-replace-to-history-variable from t
)))
107 (when (and regexp-flag
108 (string-match "\\(\\`\\|[^\\]\\)\\(\\\\\\\\\\)*\\\\[,#]" to
))
112 (setq pos
(match-end 0))
113 (push (substring to
0 (- pos
2)) list
)
114 (setq char
(aref to
(1- pos
))
115 to
(substring to pos
))
117 (push '(number-to-string replace-count
) list
))
119 (setq pos
(read-from-string to
))
120 (push `(replace-quote ,(car pos
)) list
)
122 ;; Swallow a space after a symbol
123 ;; if there is a space.
124 (if (and (or (symbolp (car pos
))
125 ;; Swallow a space after 'foo
126 ;; but not after (quote foo).
127 (and (eq (car-safe (car pos
)) 'quote
)
128 (not (= ?\
( (aref to
0)))))
129 (eq (string-match " " to
(cdr pos
))
133 (setq to
(substring to end
)))))
134 (string-match "\\(\\`\\|[^\\]\\)\\(\\\\\\\\\\)*\\\\[,#]" to
)))
135 (setq to
(nreverse (delete "" (cons to list
)))))
136 (replace-match-string-symbols to
)
137 (setq to
(cons 'replace-eval-replacement
138 (if (> (length to
) 1)
141 (list from to current-prefix-arg
)))
143 (defun query-replace (from-string to-string
&optional delimited start end
)
144 "Replace some occurrences of FROM-STRING with TO-STRING.
145 As each match is found, the user must type a character saying
146 what to do with it. For directions, type \\[help-command] at that time.
148 In Transient Mark mode, if the mark is active, operate on the contents
149 of the region. Otherwise, operate from point to the end of the buffer.
151 If `query-replace-interactive' is non-nil, the last incremental search
152 string is used as FROM-STRING--you don't have to specify it with the
155 Matching is independent of case if `case-fold-search' is non-nil and
156 FROM-STRING has no uppercase letters. Replacement transfers the case
157 pattern of the old text to the new text, if `case-replace' and
158 `case-fold-search' are non-nil and FROM-STRING has no uppercase
159 letters. \(Transferring the case pattern means that if the old text
160 matched is all caps, or capitalized, then its replacement is upcased
163 Third arg DELIMITED (prefix arg if interactive), if non-nil, means replace
164 only matches surrounded by word boundaries.
165 Fourth and fifth arg START and END specify the region to operate on.
167 To customize possible responses, change the \"bindings\" in `query-replace-map'."
168 (interactive (let ((common
169 (query-replace-read-args "Query replace" nil
)))
170 (list (nth 0 common
) (nth 1 common
) (nth 2 common
)
171 ;; These are done separately here
172 ;; so that command-history will record these expressions
173 ;; rather than the values they had this time.
174 (if (and transient-mark-mode mark-active
)
176 (if (and transient-mark-mode mark-active
)
178 (perform-replace from-string to-string t nil delimited nil nil start end
))
180 (define-key esc-map
"%" 'query-replace
)
182 (defun query-replace-regexp (regexp to-string
&optional delimited start end
)
183 "Replace some things after point matching REGEXP with TO-STRING.
184 As each match is found, the user must type a character saying
185 what to do with it. For directions, type \\[help-command] at that time.
187 In Transient Mark mode, if the mark is active, operate on the contents
188 of the region. Otherwise, operate from point to the end of the buffer.
190 If `query-replace-interactive' is non-nil, the last incremental search
191 regexp is used as REGEXP--you don't have to specify it with the
194 Matching is independent of case if `case-fold-search' is non-nil and
195 REGEXP has no uppercase letters. Replacement transfers the case
196 pattern of the old text to the new text, if `case-replace' and
197 `case-fold-search' are non-nil and REGEXP has no uppercase letters.
198 \(Transferring the case pattern means that if the old text matched is
199 all caps, or capitalized, then its replacement is upcased or
202 Third arg DELIMITED (prefix arg if interactive), if non-nil, means replace
203 only matches surrounded by word boundaries.
204 Fourth and fifth arg START and END specify the region to operate on.
206 In TO-STRING, `\\&' stands for whatever matched the whole of REGEXP,
207 and `\\=\\N' (where N is a digit) stands for
208 whatever what matched the Nth `\\(...\\)' in REGEXP.
209 `\\?' lets you edit the replacement text in the minibuffer
210 at the given position for each replacement.
212 In interactive calls, the replacement text can contain `\\,'
213 followed by a Lisp expression. Each
214 replacement evaluates that expression to compute the replacement
215 string. Inside of that expression, `\\&' is a string denoting the
216 whole match as a string, `\\N' for a partial match, `\\#&' and `\\#N'
217 for the whole or a partial match converted to a number with
218 `string-to-number', and `\\#' itself for the number of replacements
219 done so far (starting with zero).
221 If the replacement expression is a symbol, write a space after it
222 to terminate it. One space there, if any, will be discarded.
224 When using those Lisp features interactively in the replacement
225 text, TO-STRING is actually made a list instead of a string.
226 Use \\[repeat-complex-command] after this command for details."
229 (query-replace-read-args "Query replace regexp" t
)))
230 (list (nth 0 common
) (nth 1 common
) (nth 2 common
)
231 ;; These are done separately here
232 ;; so that command-history will record these expressions
233 ;; rather than the values they had this time.
234 (if (and transient-mark-mode mark-active
)
236 (if (and transient-mark-mode mark-active
)
238 (perform-replace regexp to-string t t delimited nil nil start end
))
240 (define-key esc-map
[?\C-%
] 'query-replace-regexp
)
242 (defun query-replace-regexp-eval (regexp to-expr
&optional delimited start end
)
243 "Replace some things after point matching REGEXP with the result of TO-EXPR.
244 As each match is found, the user must type a character saying
245 what to do with it. For directions, type \\[help-command] at that time.
247 TO-EXPR is a Lisp expression evaluated to compute each replacement. It may
248 reference `replace-count' to get the number of replacements already made.
249 If the result of TO-EXPR is not a string, it is converted to one using
250 `prin1-to-string' with the NOESCAPE argument (which see).
252 For convenience, when entering TO-EXPR interactively, you can use `\\&' or
253 `\\0' to stand for whatever matched the whole of REGEXP, and `\\N' (where
254 N is a digit) to stand for whatever matched the Nth `\\(...\\)' in REGEXP.
255 Use `\\#&' or `\\#N' if you want a number instead of a string.
256 In interactive use, `\\#' in itself stands for `replace-count'.
258 In Transient Mark mode, if the mark is active, operate on the contents
259 of the region. Otherwise, operate from point to the end of the buffer.
261 If `query-replace-interactive' is non-nil, the last incremental search
262 regexp is used as REGEXP--you don't have to specify it with the
265 Preserves case in each replacement if `case-replace' and `case-fold-search'
266 are non-nil and REGEXP has no uppercase letters.
268 Third arg DELIMITED (prefix arg if interactive), if non-nil, means replace
269 only matches that are surrounded by word boundaries.
270 Fourth and fifth arg START and END specify the region to operate on."
273 (if query-replace-interactive
274 (setq from
(car regexp-search-ring
))
275 (setq from
(read-from-minibuffer "Query replace regexp: "
277 query-replace-from-history-variable
279 (setq to
(list (read-from-minibuffer
280 (format "Query replace regexp %s with eval: " from
)
281 nil nil t query-replace-to-history-variable from t
)))
282 ;; We make TO a list because replace-match-string-symbols requires one,
283 ;; and the user might enter a single token.
284 (replace-match-string-symbols to
)
285 (list from
(car to
) current-prefix-arg
286 (if (and transient-mark-mode mark-active
)
288 (if (and transient-mark-mode mark-active
)
290 (perform-replace regexp
(cons 'replace-eval-replacement to-expr
)
291 t
'literal delimited nil nil start end
))
293 (defun map-query-replace-regexp (regexp to-strings
&optional n start end
)
294 "Replace some matches for REGEXP with various strings, in rotation.
295 The second argument TO-STRINGS contains the replacement strings,
296 separated by spaces. Third arg DELIMITED (prefix arg if interactive),
297 if non-nil, means replace only matches surrounded by word boundaries.
298 This command works like `query-replace-regexp' except that each
299 successive replacement uses the next successive replacement string,
300 wrapping around from the last such string to the first.
302 In Transient Mark mode, if the mark is active, operate on the contents
303 of the region. Otherwise, operate from point to the end of the buffer.
305 Non-interactively, TO-STRINGS may be a list of replacement strings.
307 If `query-replace-interactive' is non-nil, the last incremental search
308 regexp is used as REGEXP--you don't have to specify it with the minibuffer.
310 A prefix argument N says to use each replacement string N times
311 before rotating to the next.
312 Fourth and fifth arg START and END specify the region to operate on."
315 (setq from
(if query-replace-interactive
316 (car regexp-search-ring
)
317 (read-from-minibuffer "Map query replace (regexp): "
319 'query-replace-history nil t
)))
320 (setq to
(read-from-minibuffer
321 (format "Query replace %s with (space-separated strings): "
324 'query-replace-history from t
))
326 (and current-prefix-arg
327 (prefix-numeric-value current-prefix-arg
))
328 (if (and transient-mark-mode mark-active
)
330 (if (and transient-mark-mode mark-active
)
333 (if (listp to-strings
)
334 (setq replacements to-strings
)
335 (while (/= (length to-strings
) 0)
336 (if (string-match " " to-strings
)
339 (list (substring to-strings
0
340 (string-match " " to-strings
))))
341 to-strings
(substring to-strings
342 (1+ (string-match " " to-strings
))))
343 (setq replacements
(append replacements
(list to-strings
))
345 (perform-replace regexp replacements t t nil n nil start end
)))
347 (defun replace-string (from-string to-string
&optional delimited start end
)
348 "Replace occurrences of FROM-STRING with TO-STRING.
349 Preserve case in each match if `case-replace' and `case-fold-search'
350 are non-nil and FROM-STRING has no uppercase letters.
351 \(Preserving case means that if the string matched is all caps, or capitalized,
352 then its replacement is upcased or capitalized.)
354 In Transient Mark mode, if the mark is active, operate on the contents
355 of the region. Otherwise, operate from point to the end of the buffer.
357 Third arg DELIMITED (prefix arg if interactive), if non-nil, means replace
358 only matches surrounded by word boundaries.
359 Fourth and fifth arg START and END specify the region to operate on.
361 If `query-replace-interactive' is non-nil, the last incremental search
362 string is used as FROM-STRING--you don't have to specify it with the
365 This function is usually the wrong thing to use in a Lisp program.
366 What you probably want is a loop like this:
367 (while (search-forward FROM-STRING nil t)
368 (replace-match TO-STRING nil t))
369 which will run faster and will not set the mark or print anything.
370 \(You may need a more complex loop if FROM-STRING can match the null string
371 and TO-STRING is also null.)"
374 (query-replace-read-args "Replace string" nil
)))
375 (list (nth 0 common
) (nth 1 common
) (nth 2 common
)
376 (if (and transient-mark-mode mark-active
)
378 (if (and transient-mark-mode mark-active
)
380 (perform-replace from-string to-string nil nil delimited nil nil start end
))
382 (defun replace-regexp (regexp to-string
&optional delimited start end
)
383 "Replace things after point matching REGEXP with TO-STRING.
384 Preserve case in each match if `case-replace' and `case-fold-search'
385 are non-nil and REGEXP has no uppercase letters.
387 In Transient Mark mode, if the mark is active, operate on the contents
388 of the region. Otherwise, operate from point to the end of the buffer.
390 Third arg DELIMITED (prefix arg if interactive), if non-nil, means replace
391 only matches surrounded by word boundaries.
392 Fourth and fifth arg START and END specify the region to operate on.
394 In TO-STRING, `\\&' stands for whatever matched the whole of REGEXP,
395 and `\\=\\N' (where N is a digit) stands for
396 whatever what matched the Nth `\\(...\\)' in REGEXP.
397 `\\?' lets you edit the replacement text in the minibuffer
398 at the given position for each replacement.
400 In interactive calls, the replacement text may contain `\\,'
401 followed by a Lisp expression used as part of the replacement
402 text. Inside of that expression, `\\&' is a string denoting the
403 whole match, `\\N' a partial matches, `\\#&' and `\\#N' the
404 respective numeric values from `string-to-number', and `\\#'
405 itself for `replace-count', the number of replacements occured so
408 If your Lisp expression is an identifier and the next letter in
409 the replacement string would be interpreted as part of it, you
410 can wrap it with an expression like `\\,(or \\#)'. Incidentally,
411 for this particular case you may also enter `\\#' in the
412 replacement text directly.
414 When using those Lisp features interactively in the replacement
415 text, TO-STRING is actually made a list instead of a string.
416 Use \\[repeat-complex-command] after this command for details.
418 If `query-replace-interactive' is non-nil, the last incremental search
419 regexp is used as REGEXP--you don't have to specify it with the minibuffer.
421 This function is usually the wrong thing to use in a Lisp program.
422 What you probably want is a loop like this:
423 (while (re-search-forward REGEXP nil t)
424 (replace-match TO-STRING nil nil))
425 which will run faster and will not set the mark or print anything."
428 (query-replace-read-args "Replace regexp" t
)))
429 (list (nth 0 common
) (nth 1 common
) (nth 2 common
)
430 (if (and transient-mark-mode mark-active
)
432 (if (and transient-mark-mode mark-active
)
434 (perform-replace regexp to-string nil t delimited nil nil start end
))
437 (defvar regexp-history nil
438 "History list for some commands that read regular expressions.")
441 (defalias 'delete-non-matching-lines
'keep-lines
)
442 (defalias 'delete-matching-lines
'flush-lines
)
443 (defalias 'count-matches
'how-many
)
446 (defun keep-lines-read-args (prompt)
447 "Read arguments for `keep-lines' and friends.
448 Prompt for a regexp with PROMPT.
449 Value is a list, (REGEXP)."
450 (list (read-from-minibuffer prompt nil nil nil
451 'regexp-history nil t
)))
453 (defun keep-lines (regexp &optional rstart rend
)
454 "Delete all lines except those containing matches for REGEXP.
455 A match split across lines preserves all the lines it lies in.
456 Applies to all lines after point.
458 If REGEXP contains upper case characters (excluding those preceded by `\\'),
459 the matching is case-sensitive.
461 Second and third arg RSTART and REND specify the region to operate on.
463 Interactively, in Transient Mark mode when the mark is active, operate
464 on the contents of the region. Otherwise, operate from point to the
469 (barf-if-buffer-read-only)
470 (keep-lines-read-args "Keep lines (containing match for regexp): ")))
473 (goto-char (min rstart rend
))
474 (setq rend
(copy-marker (max rstart rend
))))
475 (if (and transient-mark-mode mark-active
)
476 (setq rstart
(region-beginning)
477 rend
(copy-marker (region-end)))
479 rend
(point-max-marker)))
482 (or (bolp) (forward-line 1))
483 (let ((start (point))
484 (case-fold-search (and case-fold-search
485 (isearch-no-upper-case-p regexp t
))))
486 (while (< (point) rend
)
487 ;; Start is first char not preserved by previous match.
488 (if (not (re-search-forward regexp rend
'move
))
489 (delete-region start rend
)
490 (let ((end (save-excursion (goto-char (match-beginning 0))
493 ;; Now end is first char preserved by the new match.
495 (delete-region start end
))))
497 (setq start
(save-excursion (forward-line 1) (point)))
498 ;; If the match was empty, avoid matching again at same place.
499 (and (< (point) rend
)
500 (= (match-beginning 0) (match-end 0))
501 (forward-char 1))))))
504 (defun flush-lines (regexp &optional rstart rend
)
505 "Delete lines containing matches for REGEXP.
506 If a match is split across lines, all the lines it lies in are deleted.
507 Applies to lines after point.
509 If REGEXP contains upper case characters (excluding those preceded by `\\'),
510 the matching is case-sensitive.
512 Second and third arg RSTART and REND specify the region to operate on.
514 Interactively, in Transient Mark mode when the mark is active, operate
515 on the contents of the region. Otherwise, operate from point to the
520 (barf-if-buffer-read-only)
521 (keep-lines-read-args "Flush lines (containing match for regexp): ")))
524 (goto-char (min rstart rend
))
525 (setq rend
(copy-marker (max rstart rend
))))
526 (if (and transient-mark-mode mark-active
)
527 (setq rstart
(region-beginning)
528 rend
(copy-marker (region-end)))
530 rend
(point-max-marker)))
532 (let ((case-fold-search (and case-fold-search
533 (isearch-no-upper-case-p regexp t
))))
535 (while (and (< (point) rend
)
536 (re-search-forward regexp rend t
))
537 (delete-region (save-excursion (goto-char (match-beginning 0))
540 (progn (forward-line 1) (point)))))))
543 (defun how-many (regexp &optional rstart rend
)
544 "Print number of matches for REGEXP following point.
546 If REGEXP contains upper case characters (excluding those preceded by `\\'),
547 the matching is case-sensitive.
549 Second and third arg RSTART and REND specify the region to operate on.
551 Interactively, in Transient Mark mode when the mark is active, operate
552 on the contents of the region. Otherwise, operate from point to the
556 (keep-lines-read-args "How many matches for (regexp): "))
559 (goto-char (min rstart rend
))
560 (if (and transient-mark-mode mark-active
)
561 (setq rstart
(region-beginning)
562 rend
(copy-marker (region-end)))
564 rend
(point-max-marker)))
568 (case-fold-search (and case-fold-search
569 (isearch-no-upper-case-p regexp t
))))
570 (while (and (< (point) rend
)
571 (progn (setq opoint
(point))
572 (re-search-forward regexp rend t
)))
573 (if (= opoint
(point))
575 (setq count
(1+ count
))))
576 (message "%d occurrences" count
))))
579 (defvar occur-mode-map
580 (let ((map (make-sparse-keymap)))
581 (define-key map
[mouse-2
] 'occur-mode-mouse-goto
)
582 (define-key map
"\C-c\C-c" 'occur-mode-goto-occurrence
)
583 (define-key map
"\C-m" 'occur-mode-goto-occurrence
)
584 (define-key map
"o" 'occur-mode-goto-occurrence-other-window
)
585 (define-key map
"\C-o" 'occur-mode-display-occurrence
)
586 (define-key map
"\M-n" 'occur-next
)
587 (define-key map
"\M-p" 'occur-prev
)
588 (define-key map
"r" 'occur-rename-buffer
)
589 (define-key map
"c" 'clone-buffer
)
590 (define-key map
"g" 'revert-buffer
)
591 (define-key map
"q" 'quit-window
)
592 (define-key map
"z" 'kill-this-buffer
)
594 "Keymap for `occur-mode'.")
596 (defvar occur-revert-arguments nil
597 "Arguments to pass to `occur-1' to revert an Occur mode buffer.
598 See `occur-revert-function'.")
600 (defcustom occur-mode-hook
'(turn-on-font-lock)
601 "Hook run when entering Occur mode."
605 (defcustom occur-hook nil
606 "Hook run when `occur' is called."
610 (put 'occur-mode
'mode-class
'special
)
612 "Major mode for output from \\[occur].
613 \\<occur-mode-map>Move point to one of the items in this buffer, then use
614 \\[occur-mode-goto-occurrence] to go to the occurrence that the item refers to.
615 Alternatively, click \\[occur-mode-mouse-goto] on an item to go to it.
619 (kill-all-local-variables)
620 (use-local-map occur-mode-map
)
621 (setq major-mode
'occur-mode
)
622 (setq mode-name
"Occur")
623 (set (make-local-variable 'revert-buffer-function
) 'occur-revert-function
)
624 (make-local-variable 'occur-revert-arguments
)
625 (add-hook 'change-major-mode-hook
'font-lock-defontify nil t
)
626 (setq next-error-function
'occur-next-error
)
627 (run-hooks 'occur-mode-hook
))
629 (defun occur-revert-function (ignore1 ignore2
)
630 "Handle `revert-buffer' for Occur mode buffers."
631 (apply 'occur-1
(append occur-revert-arguments
(list (buffer-name)))))
633 (defun occur-mode-mouse-goto (event)
634 "In Occur mode, go to the occurrence whose line you click on."
638 (set-buffer (window-buffer (posn-window (event-end event
))))
640 (goto-char (posn-point (event-end event
)))
641 (setq pos
(occur-mode-find-occurrence))))
642 (pop-to-buffer (marker-buffer pos
))
645 (defun occur-mode-find-occurrence ()
646 (let ((pos (get-text-property (point) 'occur-target
)))
648 (error "No occurrence on this line"))
649 (unless (buffer-live-p (marker-buffer pos
))
650 (error "Buffer for this occurrence was killed"))
653 (defun occur-mode-goto-occurrence ()
654 "Go to the occurrence the current line describes."
656 (let ((pos (occur-mode-find-occurrence)))
657 (pop-to-buffer (marker-buffer pos
))
660 (defun occur-mode-goto-occurrence-other-window ()
661 "Go to the occurrence the current line describes, in another window."
663 (let ((pos (occur-mode-find-occurrence)))
664 (switch-to-buffer-other-window (marker-buffer pos
))
667 (defun occur-mode-display-occurrence ()
668 "Display in another window the occurrence the current line describes."
670 (let ((pos (occur-mode-find-occurrence))
672 ;; Bind these to ensure `display-buffer' puts it in another window.
673 same-window-buffer-names
675 (setq window
(display-buffer (marker-buffer pos
)))
676 ;; This is the way to set point in the proper window.
677 (save-selected-window
678 (select-window window
)
681 (defun occur-find-match (n search message
)
682 (if (not n
) (setq n
1))
685 (setq r
(funcall search
(point) 'occur-match
))
687 (get-text-property r
'occur-match
)
688 (setq r
(funcall search r
'occur-match
)))
694 (defun occur-next (&optional n
)
695 "Move to the Nth (default 1) next match in an Occur mode buffer."
697 (occur-find-match n
#'next-single-property-change
"No more matches"))
699 (defun occur-prev (&optional n
)
700 "Move to the Nth (default 1) previous match in an Occur mode buffer."
702 (occur-find-match n
#'previous-single-property-change
"No earlier matches"))
704 (defun occur-next-error (&optional argp reset
)
705 "Move to the Nth (default 1) next match in an Occur mode buffer.
706 Compatibility function for \\[next-error] invocations."
709 (occur-find-match 0 #'next-single-property-change
"No first match"))
711 (prefix-numeric-value argp
)
712 (if (> 0 (prefix-numeric-value argp
))
713 #'previous-single-property-change
714 #'next-single-property-change
)
716 (occur-mode-goto-occurrence))
719 (defcustom list-matching-lines-default-context-lines
0
720 "*Default number of context lines included around `list-matching-lines' matches.
721 A negative number means to include that many lines before the match.
722 A positive number means to include that many lines both before and after."
726 (defalias 'list-matching-lines
'occur
)
728 (defcustom list-matching-lines-face
'bold
729 "*Face used by \\[list-matching-lines] to show the text that matches.
730 If the value is nil, don't highlight the matching portions specially."
734 (defcustom list-matching-lines-buffer-name-face
'underline
735 "*Face used by \\[list-matching-lines] to show the names of buffers.
736 If the value is nil, don't highlight the buffer names specially."
740 (defun occur-accumulate-lines (count &optional no-props
)
742 (let ((forwardp (> count
0))
744 (while (not (or (zerop count
)
748 (setq count
(+ count
(if forwardp -
1 1)))
750 (funcall (if no-props
751 #'buffer-substring-no-properties
753 (line-beginning-position)
756 (forward-line (if forwardp
1 -
1)))
759 (defun occur-read-primary-args ()
760 (list (let* ((default (car regexp-history
))
762 (read-from-minibuffer
764 (format "List lines matching regexp (default `%s'): "
766 "List lines matching regexp: ")
774 (when current-prefix-arg
775 (prefix-numeric-value current-prefix-arg
))))
777 (defun occur-rename-buffer (&optional unique-p
)
778 "Rename the current *Occur* buffer to *Occur: original-buffer-name*.
779 Here `original-buffer-name' is the buffer name were occur was originally run.
780 When given the prefix argument, the renaming will not clobber the existing
781 buffer(s) of that name, but use `generate-new-buffer-name' instead.
782 You can add this to `occur-hook' if you always want a separate *Occur*
783 buffer for each buffer where you invoke `occur'."
786 (if (eq major-mode
'occur-mode
) (current-buffer) (get-buffer "*Occur*"))
787 (rename-buffer (concat "*Occur: "
788 (mapconcat #'buffer-name
789 (car (cddr occur-revert-arguments
)) "/")
793 (defun occur (regexp &optional nlines
)
794 "Show all lines in the current buffer containing a match for REGEXP.
796 If a match spreads across multiple lines, all those lines are shown.
798 Each line is displayed with NLINES lines before and after, or -NLINES
799 before if NLINES is negative.
800 NLINES defaults to `list-matching-lines-default-context-lines'.
801 Interactively it is the prefix arg.
803 The lines are shown in a buffer named `*Occur*'.
804 It serves as a menu to find any of the occurrences in this buffer.
805 \\<occur-mode-map>\\[describe-mode] in that buffer will explain how.
807 If REGEXP contains upper case characters (excluding those preceded by `\\'),
808 the matching is case-sensitive."
809 (interactive (occur-read-primary-args))
810 (occur-1 regexp nlines
(list (current-buffer))))
812 (defun multi-occur (bufs regexp
&optional nlines
)
813 "Show all lines in buffers BUFS containing a match for REGEXP.
814 This function acts on multiple buffers; otherwise, it is exactly like
818 (let* ((bufs (list (read-buffer "First buffer to search: "
819 (current-buffer) t
)))
821 (ido-ignore-item-temp-list bufs
))
822 (while (not (string-equal
823 (setq buf
(read-buffer
824 (if (eq read-buffer-function
'ido-read-buffer
)
825 "Next buffer to search (C-j to end): "
826 "Next buffer to search (RET to end): ")
829 (add-to-list 'bufs buf
)
830 (setq ido-ignore-item-temp-list bufs
))
831 (nreverse (mapcar #'get-buffer bufs
)))
832 (occur-read-primary-args)))
833 (occur-1 regexp nlines bufs
))
835 (defun multi-occur-by-filename-regexp (bufregexp regexp
&optional nlines
)
836 "Show all lines matching REGEXP in buffers named by BUFREGEXP.
837 See also `multi-occur'."
840 (let* ((default (car regexp-history
))
842 (read-from-minibuffer
843 "List lines in buffers whose filename matches regexp: "
851 (occur-read-primary-args)))
853 (occur-1 regexp nlines
855 (mapcar (lambda (buf)
856 (when (and (buffer-file-name buf
)
857 (string-match bufregexp
858 (buffer-file-name buf
)))
862 (defun occur-1 (regexp nlines bufs
&optional buf-name
)
864 (setq buf-name
"*Occur*"))
865 (let ((occur-buf (get-buffer-create buf-name
))
867 (active-bufs (delq nil
(mapcar #'(lambda (buf)
868 (when (buffer-live-p buf
) buf
))
870 ;; Handle the case where one of the buffers we're searching is the
871 ;; *Occur* buffer itself.
872 (when (memq occur-buf bufs
)
873 (setq occur-buf
(with-current-buffer occur-buf
874 (clone-buffer "*Occur-temp*"))
876 (with-current-buffer occur-buf
877 (setq buffer-read-only nil
)
880 (let ((count (occur-engine
881 regexp active-bufs occur-buf
882 (or nlines list-matching-lines-default-context-lines
)
883 (and case-fold-search
884 (isearch-no-upper-case-p regexp t
))
885 list-matching-lines-buffer-name-face
886 nil list-matching-lines-face nil
)))
887 (let* ((bufcount (length active-bufs
))
888 (diff (- (length bufs
) bufcount
)))
889 (message "Searched %d buffer%s%s; %s match%s for `%s'"
890 bufcount
(if (= bufcount
1) "" "s")
891 (if (zerop diff
) "" (format " (%d killed)" diff
))
892 (if (zerop count
) "no" (format "%d" count
))
893 (if (= count
1) "" "es")
895 ;; If we had to make a temporary buffer, make it the *Occur*
898 (with-current-buffer (get-buffer buf-name
)
899 (kill-buffer (current-buffer)))
900 (rename-buffer buf-name
))
901 (setq occur-revert-arguments
(list regexp nlines bufs
)
905 (display-buffer occur-buf
)
906 (setq next-error-last-buffer occur-buf
))
907 (kill-buffer occur-buf
)))
908 (run-hooks 'occur-hook
))))
910 (defun occur-engine-add-prefix (lines)
913 (concat " :" line
"\n"))
916 (defun occur-engine (regexp buffers out-buf nlines case-fold-search
917 title-face prefix-face match-face keep-props
)
918 (with-current-buffer out-buf
919 (setq buffer-read-only nil
)
920 (let ((globalcount 0)
922 ;; Map over all the buffers
923 (dolist (buf buffers
)
924 (when (buffer-live-p buf
)
925 (let ((matches 0) ;; count of matched lines
926 (lines 1) ;; line count
934 (headerpt (with-current-buffer out-buf
(point))))
938 ;; Set CODING only if the current buffer locally
939 ;; binds buffer-file-coding-system.
940 (not (local-variable-p 'buffer-file-coding-system
))
941 (setq coding buffer-file-coding-system
))
943 (goto-char (point-min)) ;; begin searching in the buffer
945 (setq origpt
(point))
946 (when (setq endpt
(re-search-forward regexp nil t
))
947 (setq matches
(1+ matches
)) ;; increment match count
948 (setq matchbeg
(match-beginning 0)
949 matchend
(match-end 0))
950 (setq begpt
(save-excursion
952 (line-beginning-position)))
953 (setq lines
(+ lines
(1- (count-lines origpt endpt
))))
954 (setq marker
(make-marker))
955 (set-marker marker matchbeg
)
956 (setq curstring
(buffer-substring begpt
957 (line-end-position)))
958 ;; Depropertize the string, and maybe
959 ;; highlight the matches
960 (let ((len (length curstring
))
963 (set-text-properties 0 len nil curstring
))
964 (while (and (< start len
)
965 (string-match regexp curstring start
))
966 (add-text-properties (match-beginning 0)
971 `(font-lock-face ,match-face
)))
973 (setq start
(match-end 0))))
974 ;; Generate the string to insert for this match
977 ;; Using 7 digits aligns tabs properly.
978 (apply #'propertize
(format "%7d:" lines
)
981 `(font-lock-face prefix-face
))
987 ;; The simple display style
989 ;; The complex multi-line display
990 ;; style. Generate a list of lines,
991 ;; concatenate them all together.
994 (occur-engine-add-prefix (nreverse (cdr (occur-accumulate-lines (- (1+ nlines
)) keep-props
))))
996 (occur-engine-add-prefix (cdr (occur-accumulate-lines (1+ nlines
) keep-props
))))))))
997 ;; Actually insert the match display data
998 (with-current-buffer out-buf
1000 (end (progn (insert data
) (point))))
1001 (unless (= nlines
0)
1002 (insert "-------\n"))
1003 (add-text-properties
1005 `(occur-target ,marker help-echo
"mouse-2: go to this occurrence"))
1006 ;; We don't put `mouse-face' on the newline,
1007 ;; because that loses.
1008 (add-text-properties beg
(1- end
) '(mouse-face highlight
)))))
1012 (setq lines
(1+ lines
))
1013 ;; On to the next match...
1015 (goto-char (point-max))))))
1016 (when (not (zerop matches
)) ;; is the count zero?
1017 (setq globalcount
(+ globalcount matches
))
1018 (with-current-buffer out-buf
1019 (goto-char headerpt
)
1022 (insert (format "%d match%s for \"%s\" in buffer: %s\n"
1023 matches
(if (= matches
1) "" "es")
1024 regexp
(buffer-name buf
)))
1026 (add-text-properties beg end
1029 `(font-lock-face ,title-face
))
1030 `(occur-title ,buf
))))
1031 (goto-char (point-min)))))))
1033 ;; CODING is buffer-file-coding-system of the first buffer
1034 ;; that locally binds it. Let's use it also for the output
1036 (set-buffer-file-coding-system coding
))
1037 ;; Return the number of matches
1041 ;; It would be nice to use \\[...], but there is no reasonable way
1042 ;; to make that display both SPC and Y.
1043 (defconst query-replace-help
1044 "Type Space or `y' to replace one match, Delete or `n' to skip to next,
1045 RET or `q' to exit, Period to replace one match and exit,
1046 Comma to replace but not move point immediately,
1047 C-r to enter recursive edit (\\[exit-recursive-edit] to get out again),
1048 C-w to delete match and recursive edit,
1049 C-l to clear the screen, redisplay, and offer same replacement again,
1050 ! to replace all remaining matches with no more questions,
1051 ^ to move point back to previous match,
1052 E to edit the replacement string"
1053 "Help message while in `query-replace'.")
1055 (defvar query-replace-map
(make-sparse-keymap)
1056 "Keymap that defines the responses to questions in `query-replace'.
1057 The \"bindings\" in this map are not commands; they are answers.
1058 The valid answers include `act', `skip', `act-and-show',
1059 `exit', `act-and-exit', `edit', `delete-and-edit', `recenter',
1060 `automatic', `backup', `exit-prefix', and `help'.")
1062 (define-key query-replace-map
" " 'act
)
1063 (define-key query-replace-map
"\d" 'skip
)
1064 (define-key query-replace-map
[delete] 'skip)
1065 (define-key query-replace-map [backspace] 'skip)
1066 (define-key query-replace-map "y" 'act)
1067 (define-key query-replace-map "n" 'skip)
1068 (define-key query-replace-map "Y" 'act)
1069 (define-key query-replace-map "N" 'skip)
1070 (define-key query-replace-map "e" 'edit-replacement)
1071 (define-key query-replace-map "E" 'edit-replacement)
1072 (define-key query-replace-map "," 'act-and-show)
1073 (define-key query-replace-map "q" 'exit)
1074 (define-key query-replace-map "\r" 'exit)
1075 (define-key query-replace-map [return] 'exit)
1076 (define-key query-replace-map "." 'act-and-exit)
1077 (define-key query-replace-map "\C-r" 'edit)
1078 (define-key query-replace-map "\C-w" 'delete-and-edit)
1079 (define-key query-replace-map "\C-l" 'recenter)
1080 (define-key query-replace-map "!" 'automatic)
1081 (define-key query-replace-map "^" 'backup)
1082 (define-key query-replace-map "\C-h" 'help)
1083 (define-key query-replace-map [f1] 'help)
1084 (define-key query-replace-map [help] 'help)
1085 (define-key query-replace-map "?" 'help)
1086 (define-key query-replace-map "\C-g" 'quit)
1087 (define-key query-replace-map "\C-]" 'quit)
1088 (define-key query-replace-map "\e" 'exit-prefix)
1089 (define-key query-replace-map [escape] 'exit-prefix)
1091 (defun replace-match-string-symbols (n)
1092 "Process a list (and any sub-lists), expanding certain symbols.
1094 N (match-string N) (where N is a string of digits)
1095 #N (string-to-number (match-string N))
1097 #& (string-to-number (match-string 0))
1100 Note that these symbols must be preceeded by a backslash in order to
1105 (replace-match-string-symbols (car n))) ;Process sub-list
1107 (let ((name (symbol-name (car n))))
1109 ((string-match "^[0-9]+$" name)
1110 (setcar n (list 'match-string (string-to-number name))))
1111 ((string-match "^#[0-9]+$" name)
1112 (setcar n (list 'string-to-number
1114 (string-to-number (substring name 1))))))
1116 (setcar n '(match-string 0)))
1117 ((string= "#&" name)
1118 (setcar n '(string-to-number (match-string 0))))
1120 (setcar n 'replace-count))))))
1123 (defun replace-eval-replacement (expression replace-count)
1124 (let ((replacement (eval expression)))
1125 (if (stringp replacement)
1127 (prin1-to-string replacement t))))
1129 (defun replace-quote (replacement)
1130 "Quote a replacement string.
1131 This just doubles all backslashes in REPLACEMENT and
1132 returns the resulting string. If REPLACEMENT is not
1133 a string, it is first passed through `prin1-to-string'
1134 with the `noescape' argument set.
1136 `match-data' is preserved across the call."
1138 (replace-regexp-in-string "\\\\" "\\\\"
1139 (if (stringp replacement)
1141 (prin1-to-string replacement t))
1144 (defun replace-loop-through-replacements (data replace-count)
1145 ;; DATA is a vector contaning the following values:
1146 ;; 0 next-rotate-count
1148 ;; 2 next-replacement
1150 (if (= (aref data 0) replace-count)
1152 (aset data 0 (+ replace-count (aref data 1)))
1153 (let ((next (cdr (aref data 2))))
1154 (aset data 2 (if (consp next) next (aref data 3))))))
1155 (car (aref data 2)))
1157 (defun replace-match-data (integers reuse &optional new)
1158 "Like `match-data', but markers in REUSE get invalidated.
1159 If NEW is non-NIL, it is set and returned instead of fresh data,
1160 but coerced to the correct value of INTEGERS."
1163 (set-match-data new)
1165 (eq (null integers) (markerp (car reuse)))
1167 (match-data integers
1170 (if (markerp (car reuse))
1171 (set-marker (car reuse) nil))
1172 (setq reuse (cdr reuse)))))))
1174 (defun replace-match-maybe-edit (newtext fixedcase literal noedit match-data)
1175 "Make a replacement with `replace-match', editing `\\?'.
1176 NEXTEXT, FIXEDCASE, LITERAL are just passed on. If NOEDIT is true, no
1177 check for `\\?' is made to save time. MATCH-DATA is used for the
1178 replacement. In case editing is done, it is changed to use markers.
1180 The return value is non-NIL if there has been no `\\?' or NOEDIT was
1181 passed in. If LITERAL is set, no checking is done, anyway."
1182 (unless (or literal noedit)
1184 (while (string-match "\\(\\`\\|[^\\]\\)\\(\\\\\\\\\\)*\\(\\\\\\?\\)"
1187 (read-input "Edit replacement string: "
1190 (replace-match "" t t newtext 3)
1191 (1+ (match-beginning 3)))
1194 nil match-data match-data))))
1196 (set-match-data match-data)
1197 (replace-match newtext fixedcase literal)
1200 (defun perform-replace (from-string replacements
1201 query-flag regexp-flag delimited-flag
1202 &optional repeat-count map start end)
1203 "Subroutine of `query-replace'. Its complexity handles interactive queries.
1204 Don't use this in your own program unless you want to query and set the mark
1205 just as `query-replace' does. Instead, write a simple loop like this:
1207 (while (re-search-forward \"foo[ \\t]+bar\" nil t)
1208 (replace-match \"foobar\" nil nil))
1210 which will run faster and probably do exactly what you want. Please
1211 see the documentation of `replace-match' to find out how to simulate
1214 This function returns nil if and only if there were no matches to
1215 make, or the user didn't cancel the call."
1216 (or map (setq map query-replace-map))
1217 (and query-flag minibuffer-auto-raise
1218 (raise-frame (window-frame (minibuffer-window))))
1219 (let ((nocasify (not (and case-fold-search case-replace
1220 (string-equal from-string
1221 (downcase from-string)))))
1222 (case-fold-search (and case-fold-search
1223 (string-equal from-string
1224 (downcase from-string))))
1225 (literal (or (not regexp-flag) (eq regexp-flag 'literal)))
1226 (search-function (if regexp-flag 're-search-forward 'search-forward))
1227 (search-string from-string)
1228 (real-match-data nil) ; the match data for the current match
1229 (next-replacement nil)
1234 (nonempty-match nil)
1236 ;; If non-nil, it is marker saying where in the buffer to stop.
1239 ;; Data for the next match. If a cons, it has the same format as
1240 ;; (match-data); otherwise it is t if a match is possible at point.
1245 (substitute-command-keys
1246 "Query replacing %s with %s: (\\<query-replace-map>\\[help] for help) "))))
1248 ;; If region is active, in Transient Mark mode, operate on region.
1250 (setq limit (copy-marker (max start end)))
1251 (goto-char (min start end))
1254 ;; REPLACEMENTS is either a string, a list of strings, or a cons cell
1255 ;; containing a function and its first argument. The function is
1256 ;; called to generate each replacement like this:
1257 ;; (funcall (car replacements) (cdr replacements) replace-count)
1258 ;; It must return a string.
1260 ((stringp replacements)
1261 (setq next-replacement replacements
1263 ((stringp (car replacements)) ; If it isn't a string, it must be a cons
1264 (or repeat-count (setq repeat-count 1))
1265 (setq replacements (cons 'replace-loop-through-replacements
1266 (vector repeat-count repeat-count
1267 replacements replacements)))))
1270 (setq search-function 're-search-forward
1271 search-string (concat "\\b"
1272 (if regexp-flag from-string
1273 (regexp-quote from-string))
1278 ;; Loop finding occurrences that perhaps should be replaced.
1279 (while (and keep-going
1280 (not (or (eobp) (and limit (>= (point) limit))))
1281 ;; Use the next match if it is already known;
1282 ;; otherwise, search for a match after moving forward
1283 ;; one char if progress is required.
1284 (setq real-match-data
1285 (if (consp match-again)
1286 (progn (goto-char (nth 1 match-again))
1287 (replace-match-data t
1290 (and (or match-again
1291 ;; MATCH-AGAIN non-nil means we
1292 ;; accept an adjacent match. If
1293 ;; we don't, move one char to the
1294 ;; right. This takes us a
1295 ;; character too far at the end,
1296 ;; but this is undone after the
1301 (and limit (>= (point) limit))))))
1302 (funcall search-function search-string limit t)
1303 ;; For speed, use only integers and
1304 ;; reuse the list used last time.
1305 (replace-match-data t real-match-data)))))
1306 ;; Optionally ignore matches that have a read-only property.
1307 (unless (and query-replace-skip-read-only
1308 (text-property-not-all
1309 (match-beginning 0) (match-end 0)
1312 ;; Record whether the match is nonempty, to avoid an infinite loop
1313 ;; repeatedly matching the same empty string.
1314 (setq nonempty-match
1315 (/= (nth 0 real-match-data) (nth 1 real-match-data)))
1317 ;; If the match is empty, record that the next one can't be
1320 ;; Otherwise, if matching a regular expression, do the next
1321 ;; match now, since the replacement for this match may
1322 ;; affect whether the next match is adjacent to this one.
1323 ;; If that match is empty, don't use it.
1326 (or (not regexp-flag)
1327 (and (looking-at search-string)
1328 (let ((match (match-data)))
1329 (and (/= (nth 0 match) (nth 1 match))
1332 ;; Calculate the replacement string, if necessary.
1334 (set-match-data real-match-data)
1335 (setq next-replacement
1336 (funcall (car replacements) (cdr replacements)
1339 (if (not query-flag)
1340 (let ((inhibit-read-only
1341 query-replace-skip-read-only))
1343 (replace-highlight (nth 0 real-match-data)
1344 (nth 1 real-match-data)))
1346 (replace-match-maybe-edit
1347 next-replacement nocasify literal
1348 noedit real-match-data)
1349 replace-count (1+ replace-count)))
1351 (let (done replaced key def)
1352 ;; Loop reading commands until one of them sets done,
1353 ;; which means it has finished handling this
1354 ;; occurrence. Any command that sets `done' should
1355 ;; leave behind proper match data for the stack.
1356 ;; Commands not setting `done' need to adjust
1357 ;; `real-match-data'.
1359 (set-match-data real-match-data)
1360 (replace-highlight (match-beginning 0) (match-end 0))
1361 ;; Bind message-log-max so we don't fill up the message log
1362 ;; with a bunch of identical messages.
1363 (let ((message-log-max nil))
1364 (message message from-string next-replacement))
1365 (setq key (read-event))
1366 ;; Necessary in case something happens during read-event
1367 ;; that clobbers the match data.
1368 (set-match-data real-match-data)
1369 (setq key (vector key))
1370 (setq def (lookup-key map key))
1371 ;; Restore the match data while we process the command.
1372 (cond ((eq def 'help)
1373 (with-output-to-temp-buffer "*Help*"
1375 (concat "Query replacing "
1376 (if regexp-flag "regexp " "")
1377 from-string " with "
1378 next-replacement ".\n\n"
1379 (substitute-command-keys
1380 query-replace-help)))
1381 (with-current-buffer standard-output
1384 (setq keep-going nil)
1388 (let ((elt (pop stack)))
1389 (goto-char (nth 0 elt))
1390 (setq replaced (nth 1 elt)
1395 (message "No previous match")
1396 (ding 'no-terminate)
1401 (replace-match-maybe-edit
1402 next-replacement nocasify literal
1403 noedit real-match-data)
1404 replace-count (1+ replace-count)))
1405 (setq done t replaced t))
1406 ((eq def 'act-and-exit)
1409 (replace-match-maybe-edit
1410 next-replacement nocasify literal
1411 noedit real-match-data)
1412 replace-count (1+ replace-count)))
1413 (setq keep-going nil)
1414 (setq done t replaced t))
1415 ((eq def 'act-and-show)
1418 (replace-match-maybe-edit
1419 next-replacement nocasify literal
1420 noedit real-match-data)
1421 replace-count (1+ replace-count)
1422 real-match-data (replace-match-data
1425 ((eq def 'automatic)
1428 (replace-match-maybe-edit
1429 next-replacement nocasify literal
1430 noedit real-match-data)
1431 replace-count (1+ replace-count)))
1432 (setq done t query-flag nil replaced t))
1438 (let ((opos (point-marker)))
1439 (setq real-match-data (replace-match-data
1442 (goto-char (match-beginning 0))
1444 (save-window-excursion
1447 (set-marker opos nil))
1448 ;; Before we make the replacement,
1449 ;; decide whether the search string
1450 ;; can match again just after this match.
1451 (if (and regexp-flag nonempty-match)
1452 (setq match-again (and (looking-at search-string)
1454 ;; Edit replacement.
1455 ((eq def 'edit-replacement)
1456 (setq real-match-data (replace-match-data
1460 (read-input "Edit replacement string: "
1464 (set-match-data real-match-data)
1466 (replace-match-maybe-edit
1467 next-replacement nocasify literal noedit
1472 ((eq def 'delete-and-edit)
1473 (replace-match "" t t)
1474 (setq real-match-data (replace-match-data
1475 nil real-match-data))
1476 (replace-dehighlight)
1477 (save-excursion (recursive-edit))
1479 ;; Note: we do not need to treat `exit-prefix'
1480 ;; specially here, since we reread
1481 ;; any unrecognized character.
1483 (setq this-command 'mode-exited)
1484 (setq keep-going nil)
1485 (setq unread-command-events
1486 (append (listify-key-sequence key)
1487 unread-command-events))
1489 ;; Record previous position for ^ when we move on.
1490 ;; Change markers to numbers in the match data
1491 ;; since lots of markers slow down editing.
1492 (push (list (point) replaced
1493 ;;; If the replacement has already happened, all we need is the
1494 ;;; current match start and end. We could get this with a trivial
1496 ;;; (save-excursion (goto-char (match-beginning 0))
1497 ;;; (search-forward (match-string 0))
1499 ;;; if we really wanted to avoid manually constructing match data.
1500 ;;; Adding current-buffer is necessary so that match-data calls can
1501 ;;; return markers which are appropriate for editing.
1510 ;; The code preventing adjacent regexp matches in the condition
1511 ;; of the while-loop above will haven taken us one character
1512 ;; beyond the last replacement. Undo that.
1513 (when (and regexp-flag (not match-again) (> replace-count 0))
1516 (replace-dehighlight))
1517 (or unread-command-events
1518 (message "Replaced %d occurrence%s"
1520 (if (= replace-count 1) "" "s")))
1521 (and keep-going stack)))
1523 (defcustom query-replace-highlight t
1524 "*Non-nil means to highlight words during query replacement."
1528 (defvar replace-overlay nil)
1530 (defun replace-dehighlight ()
1531 (and replace-overlay
1533 (delete-overlay replace-overlay)
1534 (setq replace-overlay nil))))
1536 (defun replace-highlight (start end)
1537 (and query-replace-highlight
1539 (move-overlay replace-overlay start end (current-buffer))
1540 (setq replace-overlay (make-overlay start end))
1541 (overlay-put replace-overlay 'face
1542 (if (facep 'query-replace)
1543 'query-replace 'region)))))
1545 ;;; arch-tag: 16b4cd61-fd40-497b-b86f-b667c4cf88e4
1546 ;;; replace.el ends here