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