(Man-fontify-manpage, Man-cleanup-manpage):
[emacs.git] / lisp / replace.el
blob219fab011629b74b31a459beaf01f127ba322626
1 ;;; replace.el --- replace commands for Emacs.
3 ;; Copyright (C) 1985, 1986, 1987, 1992, 1994 Free Software Foundation, Inc.
5 ;; This file is part of GNU Emacs.
7 ;; GNU Emacs is free software; you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published by
9 ;; the Free Software Foundation; either version 2, or (at your option)
10 ;; any later version.
12 ;; GNU Emacs is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;; GNU General Public License for more details.
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with GNU Emacs; see the file COPYING. If not, write to
19 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
21 ;;; Commentary:
23 ;; This package supplies the string and regular-expression replace functions
24 ;; documented in the Emacs user's manual.
26 ;;; Code:
28 (defconst case-replace t "\
29 *Non-nil means query-replace should preserve case in replacements.")
31 (defvar query-replace-history nil)
33 (defvar query-replace-interactive nil
34 "Non-nil means `query-replace' uses the last search string.
35 That becomes the \"string to replace\".")
37 (defun query-replace-read-args (string regexp-flag)
38 (let (from to)
39 (if query-replace-interactive
40 (setq from (car (if regexp-flag regexp-search-ring search-ring)))
41 (setq from (read-from-minibuffer (format "%s: " string)
42 nil nil nil
43 'query-replace-history)))
44 (setq to (read-from-minibuffer (format "%s %s with: " string from)
45 nil nil nil
46 'query-replace-history))
47 (list from to current-prefix-arg)))
49 (defun query-replace (from-string to-string &optional arg)
50 "Replace some occurrences of FROM-STRING with TO-STRING.
51 As each match is found, the user must type a character saying
52 what to do with it. For directions, type \\[help-command] at that time.
54 If `query-replace-interactive' is non-nil, the last incremental search
55 string is used as FROM-STRING--you don't have to specify it with the
56 minibuffer.
58 Preserves case in each replacement if `case-replace' and `case-fold-search'
59 are non-nil and FROM-STRING has no uppercase letters.
60 \(Preserving case means that if the string matched is all caps, or capitalized,
61 then its replacement is upcased or capitalized.)
63 Third arg DELIMITED (prefix arg if interactive), if non-nil, means replace
64 only matches surrounded by word boundaries.
66 To customize possible responses, change the \"bindings\" in `query-replace-map'."
67 (interactive (query-replace-read-args "Query replace" nil))
68 (perform-replace from-string to-string t nil arg))
69 (define-key esc-map "%" 'query-replace)
71 (defun query-replace-regexp (regexp to-string &optional arg)
72 "Replace some things after point matching REGEXP with TO-STRING.
73 As each match is found, the user must type a character saying
74 what to do with it. For directions, type \\[help-command] at that time.
76 If `query-replace-interactive' is non-nil, the last incremental search
77 regexp is used as REGEXP--you don't have to specify it with the
78 minibuffer.
80 Preserves case in each replacement if `case-replace' and `case-fold-search'
81 are non-nil and REGEXP has no uppercase letters.
82 Third arg DELIMITED (prefix arg if interactive), if non-nil, means replace
83 only matches surrounded by word boundaries.
84 In TO-STRING, `\\&' stands for whatever matched the whole of REGEXP,
85 and `\\=\\N' (where N is a digit) stands for
86 whatever what matched the Nth `\\(...\\)' in REGEXP."
87 (interactive (query-replace-read-args "Query replace regexp" t))
88 (perform-replace regexp to-string t t arg))
90 (defun map-query-replace-regexp (regexp to-strings &optional arg)
91 "Replace some matches for REGEXP with various strings, in rotation.
92 The second argument TO-STRINGS contains the replacement strings, separated
93 by spaces. This command works like `query-replace-regexp' except
94 that each successive replacement uses the next successive replacement string,
95 wrapping around from the last such string to the first.
97 Non-interactively, TO-STRINGS may be a list of replacement strings.
99 If `query-replace-interactive' is non-nil, the last incremental search
100 regexp is used as REGEXP--you don't have to specify it with the minibuffer.
102 A prefix argument N says to use each replacement string N times
103 before rotating to the next."
104 (interactive
105 (let (from to)
106 (setq from (if query-replace-interactive
107 (car regexp-search-ring)
108 (read-from-minibuffer "Map query replace (regexp): "
109 nil nil nil
110 'query-replace-history)))
111 (setq to (read-from-minibuffer
112 (format "Query replace %s with (space-separated strings): "
113 from)
114 nil nil nil
115 'query-replace-history))
116 (list from to current-prefix-arg)))
117 (let (replacements)
118 (if (listp to-strings)
119 (setq replacements to-strings)
120 (while (/= (length to-strings) 0)
121 (if (string-match " " to-strings)
122 (setq replacements
123 (append replacements
124 (list (substring to-strings 0
125 (string-match " " to-strings))))
126 to-strings (substring to-strings
127 (1+ (string-match " " to-strings))))
128 (setq replacements (append replacements (list to-strings))
129 to-strings ""))))
130 (perform-replace regexp replacements t t nil arg)))
132 (defun replace-string (from-string to-string &optional delimited)
133 "Replace occurrences of FROM-STRING with TO-STRING.
134 Preserve case in each match if `case-replace' and `case-fold-search'
135 are non-nil and FROM-STRING has no uppercase letters.
136 \(Preserving case means that if the string matched is all caps, or capitalized,
137 then its replacement is upcased or capitalized.)
139 Third arg DELIMITED (prefix arg if interactive), if non-nil, means replace
140 only matches surrounded by word boundaries.
142 If `query-replace-interactive' is non-nil, the last incremental search
143 string is used as FROM-STRING--you don't have to specify it with the
144 minibuffer.
146 This function is usually the wrong thing to use in a Lisp program.
147 What you probably want is a loop like this:
148 (while (search-forward FROM-STRING nil t)
149 (replace-match TO-STRING nil t))
150 which will run faster and will not set the mark or print anything."
151 (interactive (query-replace-read-args "Replace string" nil))
152 (perform-replace from-string to-string nil nil delimited))
154 (defun replace-regexp (regexp to-string &optional delimited)
155 "Replace things after point matching REGEXP with TO-STRING.
156 Preserve case in each match if `case-replace' and `case-fold-search'
157 are non-nil and REGEXP has no uppercase letters.
158 Third arg DELIMITED (prefix arg if interactive), if non-nil, means replace
159 only matches surrounded by word boundaries.
160 In TO-STRING, `\\&' stands for whatever matched the whole of REGEXP,
161 and `\\=\\N' (where N is a digit) stands for
162 whatever what matched the Nth `\\(...\\)' in REGEXP.
164 If `query-replace-interactive' is non-nil, the last incremental search
165 regexp is used as REGEXP--you don't have to specify it with the minibuffer.
167 This function is usually the wrong thing to use in a Lisp program.
168 What you probably want is a loop like this:
169 (while (re-search-forward REGEXP nil t)
170 (replace-match TO-STRING nil nil))
171 which will run faster and will not set the mark or print anything."
172 (interactive (query-replace-read-args "Replace regexp" t))
173 (perform-replace regexp to-string nil t delimited))
175 (defvar regexp-history nil
176 "History list for some commands that read regular expressions.")
178 (defalias 'delete-non-matching-lines 'keep-lines)
179 (defun keep-lines (regexp)
180 "Delete all lines except those containing matches for REGEXP.
181 A match split across lines preserves all the lines it lies in.
182 Applies to all lines after point."
183 (interactive (list (read-from-minibuffer
184 "Keep lines (containing match for regexp): "
185 nil nil nil 'regexp-history)))
186 (save-excursion
187 (or (bolp) (forward-line 1))
188 (let ((start (point)))
189 (while (not (eobp))
190 ;; Start is first char not preserved by previous match.
191 (if (not (re-search-forward regexp nil 'move))
192 (delete-region start (point-max))
193 (let ((end (save-excursion (goto-char (match-beginning 0))
194 (beginning-of-line)
195 (point))))
196 ;; Now end is first char preserved by the new match.
197 (if (< start end)
198 (delete-region start end))))
199 (setq start (save-excursion (forward-line 1)
200 (point)))
201 ;; If the match was empty, avoid matching again at same place.
202 (and (not (eobp)) (= (match-beginning 0) (match-end 0))
203 (forward-char 1))))))
205 (defalias 'delete-matching-lines 'flush-lines)
206 (defun flush-lines (regexp)
207 "Delete lines containing matches for REGEXP.
208 If a match is split across lines, all the lines it lies in are deleted.
209 Applies to lines after point."
210 (interactive (list (read-from-minibuffer
211 "Flush lines (containing match for regexp): "
212 nil nil nil 'regexp-history)))
213 (save-excursion
214 (while (and (not (eobp))
215 (re-search-forward regexp nil t))
216 (delete-region (save-excursion (goto-char (match-beginning 0))
217 (beginning-of-line)
218 (point))
219 (progn (forward-line 1) (point))))))
221 (defalias 'count-matches 'how-many)
222 (defun how-many (regexp)
223 "Print number of matches for REGEXP following point."
224 (interactive (list (read-from-minibuffer
225 "How many matches for (regexp): "
226 nil nil nil 'regexp-history)))
227 (let ((count 0) opoint)
228 (save-excursion
229 (while (and (not (eobp))
230 (progn (setq opoint (point))
231 (re-search-forward regexp nil t)))
232 (if (= opoint (point))
233 (forward-char 1)
234 (setq count (1+ count))))
235 (message "%d occurrences" count))))
237 (defvar occur-mode-map ())
238 (if occur-mode-map
240 (setq occur-mode-map (make-sparse-keymap))
241 (define-key occur-mode-map [mouse-2] 'occur-mode-mouse-goto)
242 (define-key occur-mode-map "\C-c\C-c" 'occur-mode-goto-occurrence)
243 (define-key occur-mode-map "\C-m" 'occur-mode-goto-occurrence))
245 (defvar occur-buffer nil)
246 (defvar occur-nlines nil)
247 (defvar occur-pos-list nil)
249 (defun occur-mode ()
250 "Major mode for output from \\[occur].
251 \\<occur-mode-map>Move point to one of the items in this buffer, then use
252 \\[occur-mode-goto-occurrence] to go to the occurrence that the item refers to.
253 Alternatively, click \\[occur-mode-mouse-goto] on an item to go to it.
255 \\{occur-mode-map}"
256 (kill-all-local-variables)
257 (use-local-map occur-mode-map)
258 (setq major-mode 'occur-mode)
259 (setq mode-name "Occur")
260 (make-local-variable 'occur-buffer)
261 (make-local-variable 'occur-nlines)
262 (make-local-variable 'occur-pos-list)
263 (run-hooks 'occur-mode-hook))
265 (defun occur-mode-mouse-goto (event)
266 "In Occur mode, go to the occurrence whose line you click on."
267 (interactive "e")
268 (let (buffer pos)
269 (save-excursion
270 (set-buffer (window-buffer (posn-window (event-end event))))
271 (save-excursion
272 (goto-char (posn-point (event-end event)))
273 (setq pos (occur-mode-find-occurrence))
274 (setq buffer occur-buffer)))
275 (pop-to-buffer buffer)
276 (goto-char (marker-position pos))))
278 (defun occur-mode-find-occurrence ()
279 (if (or (null occur-buffer)
280 (null (buffer-name occur-buffer)))
281 (progn
282 (setq occur-buffer nil
283 occur-pos-list nil)
284 (error "Buffer in which occurrences were found is deleted")))
285 (let* ((line-count
286 (count-lines (point-min)
287 (save-excursion
288 (beginning-of-line)
289 (point))))
290 (occur-number (save-excursion
291 (beginning-of-line)
292 (/ (1- line-count)
293 (cond ((< occur-nlines 0)
294 (- 2 occur-nlines))
295 ((> occur-nlines 0)
296 (+ 2 (* 2 occur-nlines)))
297 (t 1)))))
298 (pos (nth occur-number occur-pos-list)))
299 (if (< line-count 1)
300 (error "No occurrence on this line"))
301 (or pos
302 (error "No occurrence on this line"))
303 pos))
305 (defun occur-mode-goto-occurrence ()
306 "Go to the occurrence the current line describes."
307 (interactive)
308 (let ((pos (occur-mode-find-occurrence)))
309 (pop-to-buffer occur-buffer)
310 (goto-char (marker-position pos))))
312 (defvar list-matching-lines-default-context-lines 0
313 "*Default number of context lines to include around a `list-matching-lines'
314 match. A negative number means to include that many lines before the match.
315 A positive number means to include that many lines both before and after.")
317 (defalias 'list-matching-lines 'occur)
319 (defun occur (regexp &optional nlines)
320 "Show all lines in the current buffer containing a match for REGEXP.
322 If a match spreads across multiple lines, all those lines are shown.
324 Each line is displayed with NLINES lines before and after, or -NLINES
325 before if NLINES is negative.
326 NLINES defaults to `list-matching-lines-default-context-lines'.
327 Interactively it is the prefix arg.
329 The lines are shown in a buffer named `*Occur*'.
330 It serves as a menu to find any of the occurrences in this buffer.
331 \\[describe-mode] in that buffer will explain how."
332 (interactive (list (let* ((default (car regexp-history))
333 (input
334 (read-from-minibuffer
335 (if default
336 (format "List lines matching regexp (default `%s'): " default)
337 "List lines matching regexp: ")
338 nil nil nil
339 'regexp-history)))
340 (if (> (length input) 0) input
341 (setcar regexp-history default)))
342 current-prefix-arg))
343 (setq nlines (if nlines (prefix-numeric-value nlines)
344 list-matching-lines-default-context-lines))
345 (let ((first t)
346 (buffer (current-buffer))
347 (dir default-directory)
348 (linenum 1)
349 (prevpos (point-min))
350 (final-context-start (make-marker)))
351 ;;; (save-excursion
352 ;;; (beginning-of-line)
353 ;;; (setq linenum (1+ (count-lines (point-min) (point))))
354 ;;; (setq prevpos (point)))
355 (with-output-to-temp-buffer "*Occur*"
356 (save-excursion
357 (set-buffer standard-output)
358 (setq default-directory dir)
359 ;; We will insert the number of lines, and "lines", later.
360 (insert " matching ")
361 (let ((print-escape-newlines t))
362 (prin1 regexp))
363 (insert " in buffer " (buffer-name buffer) ?. ?\n)
364 (occur-mode)
365 (setq occur-buffer buffer)
366 (setq occur-nlines nlines)
367 (setq occur-pos-list ()))
368 (if (eq buffer standard-output)
369 (goto-char (point-max)))
370 (save-excursion
371 (beginning-of-buffer)
372 ;; Find next match, but give up if prev match was at end of buffer.
373 (while (and (not (= prevpos (point-max)))
374 (re-search-forward regexp nil t))
375 (goto-char (match-beginning 0))
376 (beginning-of-line)
377 (save-match-data
378 (setq linenum (+ linenum (count-lines prevpos (point)))))
379 (setq prevpos (point))
380 (goto-char (match-end 0))
381 (let* ((start (save-excursion
382 (goto-char (match-beginning 0))
383 (forward-line (if (< nlines 0) nlines (- nlines)))
384 (point)))
385 (end (save-excursion
386 (goto-char (match-end 0))
387 (if (> nlines 0)
388 (forward-line (1+ nlines))
389 (forward-line 1))
390 (point)))
391 (tag (format "%5d" linenum))
392 (empty (make-string (length tag) ?\ ))
393 tem)
394 (save-excursion
395 (setq tem (make-marker))
396 (set-marker tem (point))
397 (set-buffer standard-output)
398 (setq occur-pos-list (cons tem occur-pos-list))
399 (or first (zerop nlines)
400 (insert "--------\n"))
401 (setq first nil)
402 (insert-buffer-substring buffer start end)
403 (set-marker final-context-start
404 (- (point) (- end (match-end 0))))
405 (backward-char (- end start))
406 (setq tem nlines)
407 (while (> tem 0)
408 (insert empty ?:)
409 (forward-line 1)
410 (setq tem (1- tem)))
411 (let ((this-linenum linenum))
412 (while (< (point) final-context-start)
413 (if (null tag)
414 (setq tag (format "%5d" this-linenum)))
415 (insert tag ?:)
416 (put-text-property (save-excursion
417 (beginning-of-line)
418 (point))
419 (save-excursion
420 (end-of-line)
421 (point))
422 'mouse-face 'highlight)
423 (forward-line 1)
424 (setq tag nil)
425 (setq this-linenum (1+ this-linenum)))
426 (while (<= (point) final-context-start)
427 (insert empty ?:)
428 (forward-line 1)
429 (setq this-linenum (1+ this-linenum))))
430 (while (< tem nlines)
431 (insert empty ?:)
432 (forward-line 1)
433 (setq tem (1+ tem)))
434 (goto-char (point-max)))
435 (forward-line 1)))
436 (set-buffer standard-output)
437 ;; Put positions in increasing order to go with buffer.
438 (setq occur-pos-list (nreverse occur-pos-list))
439 (goto-char (point-min))
440 (if (= (length occur-pos-list) 1)
441 (insert "1 line")
442 (insert (format "%d lines" (length occur-pos-list))))
443 (if (interactive-p)
444 (message "%d matching lines." (length occur-pos-list)))))))
446 ;; It would be nice to use \\[...], but there is no reasonable way
447 ;; to make that display both SPC and Y.
448 (defconst query-replace-help
449 "Type Space or `y' to replace one match, Delete or `n' to skip to next,
450 RET or `q' to exit, Period to replace one match and exit,
451 Comma to replace but not move point immediately,
452 C-r to enter recursive edit (\\[exit-recursive-edit] to get out again),
453 C-w to delete match and recursive edit,
454 C-l to clear the screen, redisplay, and offer same replacement again,
455 ! to replace all remaining matches with no more questions,
456 ^ to move point back to previous match."
457 "Help message while in query-replace")
459 (defvar query-replace-map (make-sparse-keymap)
460 "Keymap that defines the responses to questions in `query-replace'.
461 The \"bindings\" in this map are not commands; they are answers.
462 The valid answers include `act', `skip', `act-and-show',
463 `exit', `act-and-exit', `edit', `delete-and-edit', `recenter',
464 `automatic', `backup', `exit-prefix', and `help'.")
466 (define-key query-replace-map " " 'act)
467 (define-key query-replace-map "\d" 'skip)
468 (define-key query-replace-map [delete] 'skip)
469 (define-key query-replace-map [backspace] 'skip)
470 (define-key query-replace-map "y" 'act)
471 (define-key query-replace-map "n" 'skip)
472 (define-key query-replace-map "Y" 'act)
473 (define-key query-replace-map "N" 'skip)
474 (define-key query-replace-map "," 'act-and-show)
475 (define-key query-replace-map "q" 'exit)
476 (define-key query-replace-map "\r" 'exit)
477 (define-key query-replace-map [return] 'exit)
478 (define-key query-replace-map "." 'act-and-exit)
479 (define-key query-replace-map "\C-r" 'edit)
480 (define-key query-replace-map "\C-w" 'delete-and-edit)
481 (define-key query-replace-map "\C-l" 'recenter)
482 (define-key query-replace-map "!" 'automatic)
483 (define-key query-replace-map "^" 'backup)
484 (define-key query-replace-map "\C-h" 'help)
485 (define-key query-replace-map [f1] 'help)
486 (define-key query-replace-map [help] 'help)
487 (define-key query-replace-map "?" 'help)
488 (define-key query-replace-map "\C-g" 'quit)
489 (define-key query-replace-map "\C-]" 'quit)
490 (define-key query-replace-map "\e" 'exit-prefix)
491 (define-key query-replace-map [escape] 'exit-prefix)
493 (defun perform-replace (from-string replacements
494 query-flag regexp-flag delimited-flag
495 &optional repeat-count map)
496 "Subroutine of `query-replace'. Its complexity handles interactive queries.
497 Don't use this in your own program unless you want to query and set the mark
498 just as `query-replace' does. Instead, write a simple loop like this:
499 (while (re-search-forward \"foo[ \t]+bar\" nil t)
500 (replace-match \"foobar\" nil nil))
501 which will run faster and probably do exactly what you want."
502 (or map (setq map query-replace-map))
503 (let ((nocasify (not (and case-fold-search case-replace
504 (string-equal from-string
505 (downcase from-string)))))
506 (literal (not regexp-flag))
507 (search-function (if regexp-flag 're-search-forward 'search-forward))
508 (search-string from-string)
509 (real-match-data nil) ; the match data for the current match
510 (next-replacement nil)
511 (replacement-index 0)
512 (keep-going t)
513 (stack nil)
514 (next-rotate-count 0)
515 (replace-count 0)
516 (lastrepl nil) ;Position after last match considered.
517 (match-again t)
518 (message
519 (if query-flag
520 (substitute-command-keys
521 "Query replacing %s with %s: (\\<query-replace-map>\\[help] for help) "))))
522 (if (stringp replacements)
523 (setq next-replacement replacements)
524 (or repeat-count (setq repeat-count 1)))
525 (if delimited-flag
526 (setq search-function 're-search-forward
527 search-string (concat "\\b"
528 (if regexp-flag from-string
529 (regexp-quote from-string))
530 "\\b")))
531 (push-mark)
532 (undo-boundary)
533 (unwind-protect
534 ;; Loop finding occurrences that perhaps should be replaced.
535 (while (and keep-going
536 (not (eobp))
537 (funcall search-function search-string nil t)
538 ;; If the search string matches immediately after
539 ;; the previous match, but it did not match there
540 ;; before the replacement was done, ignore the match.
541 (if (or (eq lastrepl (point))
542 (and regexp-flag
543 (eq lastrepl (match-beginning 0))
544 (not match-again)))
545 (if (eobp)
547 ;; Don't replace the null string
548 ;; right after end of previous replacement.
549 (forward-char 1)
550 (funcall search-function search-string nil t))
553 ;; Save the data associated with the real match.
554 (setq real-match-data (match-data))
556 ;; Before we make the replacement, decide whether the search string
557 ;; can match again just after this match.
558 (if regexp-flag
559 (setq match-again (looking-at search-string)))
560 ;; If time for a change, advance to next replacement string.
561 (if (and (listp replacements)
562 (= next-rotate-count replace-count))
563 (progn
564 (setq next-rotate-count
565 (+ next-rotate-count repeat-count))
566 (setq next-replacement (nth replacement-index replacements))
567 (setq replacement-index (% (1+ replacement-index) (length replacements)))))
568 (if (not query-flag)
569 (progn
570 (store-match-data real-match-data)
571 (replace-match next-replacement nocasify literal)
572 (setq replace-count (1+ replace-count)))
573 (undo-boundary)
574 (let (done replaced key def)
575 ;; Loop reading commands until one of them sets done,
576 ;; which means it has finished handling this occurrence.
577 (while (not done)
578 (store-match-data real-match-data)
579 (replace-highlight (match-beginning 0) (match-end 0))
580 ;; Bind message-log-max so we don't fill up the message log
581 ;; with a bunch of identical messages.
582 (let ((message-log-max nil))
583 (message message from-string next-replacement))
584 (setq key (read-event))
585 (setq key (vector key))
586 (setq def (lookup-key map key))
587 ;; Restore the match data while we process the command.
588 (cond ((eq def 'help)
589 (with-output-to-temp-buffer "*Help*"
590 (princ
591 (concat "Query replacing "
592 (if regexp-flag "regexp " "")
593 from-string " with "
594 next-replacement ".\n\n"
595 (substitute-command-keys
596 query-replace-help)))
597 (save-excursion
598 (set-buffer standard-output)
599 (help-mode))))
600 ((eq def 'exit)
601 (setq keep-going nil)
602 (setq done t))
603 ((eq def 'backup)
604 (if stack
605 (let ((elt (car stack)))
606 (goto-char (car elt))
607 (setq replaced (eq t (cdr elt)))
608 (or replaced
609 (store-match-data (cdr elt)))
610 (setq stack (cdr stack)))
611 (message "No previous match")
612 (ding 'no-terminate)
613 (sit-for 1)))
614 ((eq def 'act)
615 (or replaced
616 (replace-match next-replacement nocasify literal))
617 (setq done t replaced t))
618 ((eq def 'act-and-exit)
619 (or replaced
620 (replace-match next-replacement nocasify literal))
621 (setq keep-going nil)
622 (setq done t replaced t))
623 ((eq def 'act-and-show)
624 (if (not replaced)
625 (progn
626 (replace-match next-replacement nocasify literal)
627 (setq replaced t))))
628 ((eq def 'automatic)
629 (or replaced
630 (replace-match next-replacement nocasify literal))
631 (setq done t query-flag nil replaced t))
632 ((eq def 'skip)
633 (setq done t))
634 ((eq def 'recenter)
635 (recenter nil))
636 ((eq def 'edit)
637 (store-match-data
638 (prog1 (match-data)
639 (save-excursion (recursive-edit))))
640 ;; Before we make the replacement,
641 ;; decide whether the search string
642 ;; can match again just after this match.
643 (if regexp-flag
644 (setq match-again (looking-at search-string))))
645 ((eq def 'delete-and-edit)
646 (delete-region (match-beginning 0) (match-end 0))
647 (store-match-data
648 (prog1 (match-data)
649 (save-excursion (recursive-edit))))
650 (setq replaced t))
651 ;; Note: we do not need to treat `exit-prefix'
652 ;; specially here, since we reread
653 ;; any unrecognized character.
655 (setq this-command 'mode-exited)
656 (setq keep-going nil)
657 (setq unread-command-events
658 (append (listify-key-sequence key)
659 unread-command-events))
660 (setq done t))))
661 ;; Record previous position for ^ when we move on.
662 ;; Change markers to numbers in the match data
663 ;; since lots of markers slow down editing.
664 (setq stack
665 (cons (cons (point)
666 (or replaced
667 (mapcar (lambda (elt)
668 (and elt
669 (prog1 (marker-position elt)
670 (set-marker elt nil))))
671 (match-data))))
672 stack))
673 (if replaced (setq replace-count (1+ replace-count)))))
674 (setq lastrepl (point)))
675 (replace-dehighlight))
676 (or unread-command-events
677 (message "Replaced %d occurrence%s"
678 replace-count
679 (if (= replace-count 1) "" "s")))
680 (and keep-going stack)))
682 (defvar query-replace-highlight nil
683 "*Non-nil means to highlight words during query replacement.")
685 (defvar replace-overlay nil)
687 (defun replace-dehighlight ()
688 (and replace-overlay
689 (progn
690 (delete-overlay replace-overlay)
691 (setq replace-overlay nil))))
693 (defun replace-highlight (start end)
694 (and query-replace-highlight
695 (progn
696 (or replace-overlay
697 (progn
698 (setq replace-overlay (make-overlay start end))
699 (overlay-put replace-overlay 'face
700 (if (internal-find-face 'query-replace)
701 'query-replace 'region))))
702 (move-overlay replace-overlay start end (current-buffer)))))
704 ;;; replace.el ends here