]> code.delx.au - gnu-emacs/blob - lisp/replace.el
Trailing whitespace deleted.
[gnu-emacs] / lisp / replace.el
1 ;;; replace.el --- replace commands for Emacs
2
3 ;; Copyright (C) 1985, 86, 87, 92, 94, 96, 1997, 2000, 2001, 2002
4 ;; Free Software Foundation, Inc.
5
6 ;; Maintainer: FSF
7
8 ;; This file is part of GNU Emacs.
9
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)
13 ;; any later version.
14
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.
19
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.
24
25 ;;; Commentary:
26
27 ;; This package supplies the string and regular-expression replace functions
28 ;; documented in the Emacs user's manual.
29
30 ;;; Code:
31
32 (defcustom case-replace t
33 "*Non-nil means `query-replace' should preserve case in replacements."
34 :type 'boolean
35 :group 'matching)
36
37 (defvar query-replace-history nil)
38
39 (defvar query-replace-interactive nil
40 "Non-nil means `query-replace' uses the last search string.
41 That becomes the \"string to replace\".")
42
43 (defcustom query-replace-from-history-variable 'query-replace-history
44 "History list to use for the FROM argument of `query-replace' commands.
45 The value of this variable should be a symbol; that symbol
46 is used as a variable to hold a history list for the strings
47 or patterns to be replaced."
48 :group 'matching
49 :type 'symbol
50 :version "20.3")
51
52 (defcustom query-replace-to-history-variable 'query-replace-history
53 "History list to use for the TO argument of `query-replace' commands.
54 The value of this variable should be a symbol; that symbol
55 is used as a variable to hold a history list for replacement
56 strings or patterns."
57 :group 'matching
58 :type 'symbol
59 :version "20.3")
60
61 (defcustom query-replace-skip-read-only nil
62 "*Non-nil means `query-replace' and friends ignore read-only matches."
63 :type 'boolean
64 :group 'matching
65 :version "21.4")
66
67 (defun query-replace-read-args (string regexp-flag &optional noerror)
68 (unless noerror
69 (barf-if-buffer-read-only))
70 (let (from to)
71 (if query-replace-interactive
72 (setq from (car (if regexp-flag regexp-search-ring search-ring)))
73 (setq from (read-from-minibuffer (format "%s: " string)
74 nil nil nil
75 query-replace-from-history-variable
76 nil t))
77 ;; Warn if user types \n or \t, but don't reject the input.
78 (if (string-match "\\\\[nt]" from)
79 (let ((match (match-string 0 from)))
80 (cond
81 ((string= match "\\n")
82 (message "Note: `\\n' here doesn't match a newline; to do that, type C-q C-j instead"))
83 ((string= match "\\t")
84 (message "Note: `\\t' here doesn't match a tab; to do that, just type TAB")))
85 (sit-for 2))))
86
87 (setq to (read-from-minibuffer (format "%s %s with: " string from)
88 nil nil nil
89 query-replace-to-history-variable from t))
90 (if (and transient-mark-mode mark-active)
91 (list from to current-prefix-arg (region-beginning) (region-end))
92 (list from to current-prefix-arg nil nil))))
93
94 (defun query-replace (from-string to-string &optional delimited start end)
95 "Replace some occurrences of FROM-STRING with TO-STRING.
96 As each match is found, the user must type a character saying
97 what to do with it. For directions, type \\[help-command] at that time.
98
99 In Transient Mark mode, if the mark is active, operate on the contents
100 of the region. Otherwise, operate from point to the end of the buffer.
101
102 If `query-replace-interactive' is non-nil, the last incremental search
103 string is used as FROM-STRING--you don't have to specify it with the
104 minibuffer.
105
106 Matching is independent of case if `case-fold-search' is non-nil and
107 FROM-STRING has no uppercase letters. Replacement transfers the case
108 pattern of the old text to the new text, if `case-replace' and
109 `case-fold-search' are non-nil and FROM-STRING has no uppercase
110 letters. \(Transferring the case pattern means that if the old text
111 matched is all caps, or capitalized, then its replacement is upcased
112 or capitalized.)
113
114 Third arg DELIMITED (prefix arg if interactive), if non-nil, means replace
115 only matches surrounded by word boundaries.
116 Fourth and fifth arg START and END specify the region to operate on.
117
118 To customize possible responses, change the \"bindings\" in `query-replace-map'."
119 (interactive (query-replace-read-args "Query replace" nil))
120 (perform-replace from-string to-string t nil delimited nil nil start end))
121
122 (define-key esc-map "%" 'query-replace)
123
124 (defun query-replace-regexp (regexp to-string &optional delimited start end)
125 "Replace some things after point matching REGEXP with TO-STRING.
126 As each match is found, the user must type a character saying
127 what to do with it. For directions, type \\[help-command] at that time.
128
129 In Transient Mark mode, if the mark is active, operate on the contents
130 of the region. Otherwise, operate from point to the end of the buffer.
131
132 If `query-replace-interactive' is non-nil, the last incremental search
133 regexp is used as REGEXP--you don't have to specify it with the
134 minibuffer.
135
136 Matching is independent of case if `case-fold-search' is non-nil and
137 REGEXP has no uppercase letters. Replacement transfers the case
138 pattern of the old text to the new text, if `case-replace' and
139 `case-fold-search' are non-nil and REGEXP has no uppercase letters.
140 \(Transferring the case pattern means that if the old text matched is
141 all caps, or capitalized, then its replacement is upcased or
142 capitalized.)
143
144 Third arg DELIMITED (prefix arg if interactive), if non-nil, means replace
145 only matches surrounded by word boundaries.
146 Fourth and fifth arg START and END specify the region to operate on.
147
148 In TO-STRING, `\\&' stands for whatever matched the whole of REGEXP,
149 and `\\=\\N' (where N is a digit) stands for
150 whatever what matched the Nth `\\(...\\)' in REGEXP."
151 (interactive (query-replace-read-args "Query replace regexp" t))
152 (perform-replace regexp to-string t t delimited nil nil start end))
153 (define-key esc-map [?\C-%] 'query-replace-regexp)
154
155 (defun query-replace-regexp-eval (regexp to-expr &optional delimited start end)
156 "Replace some things after point matching REGEXP with the result of TO-EXPR.
157 As each match is found, the user must type a character saying
158 what to do with it. For directions, type \\[help-command] at that time.
159
160 TO-EXPR is a Lisp expression evaluated to compute each replacement. It may
161 reference `replace-count' to get the number of replacements already made.
162 If the result of TO-EXPR is not a string, it is converted to one using
163 `prin1-to-string' with the NOESCAPE argument (which see).
164
165 For convenience, when entering TO-EXPR interactively, you can use `\\&' or
166 `\\0' to stand for whatever matched the whole of REGEXP, and `\\N' (where
167 N is a digit) to stand for whatever matched the Nth `\\(...\\)' in REGEXP.
168 Use `\\#&' or `\\#N' if you want a number instead of a string.
169
170 In Transient Mark mode, if the mark is active, operate on the contents
171 of the region. Otherwise, operate from point to the end of the buffer.
172
173 If `query-replace-interactive' is non-nil, the last incremental search
174 regexp is used as REGEXP--you don't have to specify it with the
175 minibuffer.
176
177 Preserves case in each replacement if `case-replace' and `case-fold-search'
178 are non-nil and REGEXP has no uppercase letters.
179
180 Third arg DELIMITED (prefix arg if interactive), if non-nil, means replace
181 only matches that are surrounded by word boundaries.
182 Fourth and fifth arg START and END specify the region to operate on."
183 (interactive
184 (let (from to start end)
185 (when (and transient-mark-mode mark-active)
186 (setq start (region-beginning)
187 end (region-end)))
188 (if query-replace-interactive
189 (setq from (car regexp-search-ring))
190 (setq from (read-from-minibuffer "Query replace regexp: "
191 nil nil nil
192 query-replace-from-history-variable
193 nil t)))
194 (setq to (list (read-from-minibuffer
195 (format "Query replace regexp %s with eval: " from)
196 nil nil t query-replace-to-history-variable from t)))
197 ;; We make TO a list because replace-match-string-symbols requires one,
198 ;; and the user might enter a single token.
199 (replace-match-string-symbols to)
200 (list from (car to) current-prefix-arg start end)))
201 (perform-replace regexp (cons 'replace-eval-replacement to-expr)
202 t t delimited nil nil start end))
203
204 (defun map-query-replace-regexp (regexp to-strings &optional n start end)
205 "Replace some matches for REGEXP with various strings, in rotation.
206 The second argument TO-STRINGS contains the replacement strings,
207 separated by spaces. Third arg DELIMITED (prefix arg if interactive),
208 if non-nil, means replace only matches surrounded by word boundaries.
209 This command works like `query-replace-regexp' except that each
210 successive replacement uses the next successive replacement string,
211 wrapping around from the last such string to the first.
212
213 In Transient Mark mode, if the mark is active, operate on the contents
214 of the region. Otherwise, operate from point to the end of the buffer.
215
216 Non-interactively, TO-STRINGS may be a list of replacement strings.
217
218 If `query-replace-interactive' is non-nil, the last incremental search
219 regexp is used as REGEXP--you don't have to specify it with the minibuffer.
220
221 A prefix argument N says to use each replacement string N times
222 before rotating to the next.
223 Fourth and fifth arg START and END specify the region to operate on."
224 (interactive
225 (let (from to start end)
226 (when (and transient-mark-mode mark-active)
227 (setq start (region-beginning)
228 end (region-end)))
229 (setq from (if query-replace-interactive
230 (car regexp-search-ring)
231 (read-from-minibuffer "Map query replace (regexp): "
232 nil nil nil
233 'query-replace-history nil t)))
234 (setq to (read-from-minibuffer
235 (format "Query replace %s with (space-separated strings): "
236 from)
237 nil nil nil
238 'query-replace-history from t))
239 (list from to start end current-prefix-arg)))
240 (let (replacements)
241 (if (listp to-strings)
242 (setq replacements to-strings)
243 (while (/= (length to-strings) 0)
244 (if (string-match " " to-strings)
245 (setq replacements
246 (append replacements
247 (list (substring to-strings 0
248 (string-match " " to-strings))))
249 to-strings (substring to-strings
250 (1+ (string-match " " to-strings))))
251 (setq replacements (append replacements (list to-strings))
252 to-strings ""))))
253 (perform-replace regexp replacements t t nil n nil start end)))
254
255 (defun replace-string (from-string to-string &optional delimited start end)
256 "Replace occurrences of FROM-STRING with TO-STRING.
257 Preserve case in each match if `case-replace' and `case-fold-search'
258 are non-nil and FROM-STRING has no uppercase letters.
259 \(Preserving case means that if the string matched is all caps, or capitalized,
260 then its replacement is upcased or capitalized.)
261
262 In Transient Mark mode, if the mark is active, operate on the contents
263 of the region. Otherwise, operate from point to the end of the buffer.
264
265 Third arg DELIMITED (prefix arg if interactive), if non-nil, means replace
266 only matches surrounded by word boundaries.
267 Fourth and fifth arg START and END specify the region to operate on.
268
269 If `query-replace-interactive' is non-nil, the last incremental search
270 string is used as FROM-STRING--you don't have to specify it with the
271 minibuffer.
272
273 This function is usually the wrong thing to use in a Lisp program.
274 What you probably want is a loop like this:
275 (while (search-forward FROM-STRING nil t)
276 (replace-match TO-STRING nil t))
277 which will run faster and will not set the mark or print anything.
278 \(You may need a more complex loop if FROM-STRING can match the null string
279 and TO-STRING is also null.)"
280 (interactive (query-replace-read-args "Replace string" nil))
281 (perform-replace from-string to-string nil nil delimited nil nil start end))
282
283 (defun replace-regexp (regexp to-string &optional delimited start end)
284 "Replace things after point matching REGEXP with TO-STRING.
285 Preserve case in each match if `case-replace' and `case-fold-search'
286 are non-nil and REGEXP has no uppercase letters.
287
288 In Transient Mark mode, if the mark is active, operate on the contents
289 of the region. Otherwise, operate from point to the end of the buffer.
290
291 Third arg DELIMITED (prefix arg if interactive), if non-nil, means replace
292 only matches surrounded by word boundaries.
293 Fourth and fifth arg START and END specify the region to operate on.
294
295 In TO-STRING, `\\&' stands for whatever matched the whole of REGEXP,
296 and `\\=\\N' (where N is a digit) stands for
297 whatever what matched the Nth `\\(...\\)' in REGEXP.
298
299 If `query-replace-interactive' is non-nil, the last incremental search
300 regexp is used as REGEXP--you don't have to specify it with the minibuffer.
301
302 This function is usually the wrong thing to use in a Lisp program.
303 What you probably want is a loop like this:
304 (while (re-search-forward REGEXP nil t)
305 (replace-match TO-STRING nil nil))
306 which will run faster and will not set the mark or print anything."
307 (interactive (query-replace-read-args "Replace regexp" t))
308 (perform-replace regexp to-string nil t delimited nil nil start end))
309
310 \f
311 (defvar regexp-history nil
312 "History list for some commands that read regular expressions.")
313
314
315 (defalias 'delete-non-matching-lines 'keep-lines)
316 (defalias 'delete-matching-lines 'flush-lines)
317 (defalias 'count-matches 'how-many)
318
319
320 (defun keep-lines-read-args (prompt)
321 "Read arguments for `keep-lines' and friends.
322 Prompt for a regexp with PROMPT.
323 Value is a list, (REGEXP)."
324 (list (read-from-minibuffer prompt nil nil nil
325 'regexp-history nil t)))
326
327 (defun keep-lines (regexp &optional rstart rend)
328 "Delete all lines except those containing matches for REGEXP.
329 A match split across lines preserves all the lines it lies in.
330 Applies to all lines after point.
331
332 If REGEXP contains upper case characters (excluding those preceded by `\\'),
333 the matching is case-sensitive.
334
335 Second and third arg RSTART and REND specify the region to operate on.
336
337 Interactively, in Transient Mark mode when the mark is active, operate
338 on the contents of the region. Otherwise, operate from point to the
339 end of the buffer."
340
341 (interactive
342 (progn
343 (barf-if-buffer-read-only)
344 (keep-lines-read-args "Keep lines (containing match for regexp): ")))
345 (if rstart
346 (progn
347 (goto-char (min rstart rend))
348 (setq rend (copy-marker (max rstart rend))))
349 (if (and transient-mark-mode mark-active)
350 (setq rstart (region-beginning)
351 rend (copy-marker (region-end)))
352 (setq rstart (point)
353 rend (point-max-marker)))
354 (goto-char rstart))
355 (save-excursion
356 (or (bolp) (forward-line 1))
357 (let ((start (point))
358 (case-fold-search (and case-fold-search
359 (isearch-no-upper-case-p regexp t))))
360 (while (< (point) rend)
361 ;; Start is first char not preserved by previous match.
362 (if (not (re-search-forward regexp rend 'move))
363 (delete-region start rend)
364 (let ((end (save-excursion (goto-char (match-beginning 0))
365 (beginning-of-line)
366 (point))))
367 ;; Now end is first char preserved by the new match.
368 (if (< start end)
369 (delete-region start end))))
370
371 (setq start (save-excursion (forward-line 1) (point)))
372 ;; If the match was empty, avoid matching again at same place.
373 (and (< (point) rend)
374 (= (match-beginning 0) (match-end 0))
375 (forward-char 1))))))
376
377
378 (defun flush-lines (regexp &optional rstart rend)
379 "Delete lines containing matches for REGEXP.
380 If a match is split across lines, all the lines it lies in are deleted.
381 Applies to lines after point.
382
383 If REGEXP contains upper case characters (excluding those preceded by `\\'),
384 the matching is case-sensitive.
385
386 Second and third arg RSTART and REND specify the region to operate on.
387
388 Interactively, in Transient Mark mode when the mark is active, operate
389 on the contents of the region. Otherwise, operate from point to the
390 end of the buffer."
391
392 (interactive
393 (progn
394 (barf-if-buffer-read-only)
395 (keep-lines-read-args "Flush lines (containing match for regexp): ")))
396 (if rstart
397 (progn
398 (goto-char (min rstart rend))
399 (setq rend (copy-marker (max rstart rend))))
400 (if (and transient-mark-mode mark-active)
401 (setq rstart (region-beginning)
402 rend (copy-marker (region-end)))
403 (setq rstart (point)
404 rend (point-max-marker)))
405 (goto-char rstart))
406 (let ((case-fold-search (and case-fold-search
407 (isearch-no-upper-case-p regexp t))))
408 (save-excursion
409 (while (and (< (point) rend)
410 (re-search-forward regexp rend t))
411 (delete-region (save-excursion (goto-char (match-beginning 0))
412 (beginning-of-line)
413 (point))
414 (progn (forward-line 1) (point)))))))
415
416
417 (defun how-many (regexp &optional rstart rend)
418 "Print number of matches for REGEXP following point.
419
420 If REGEXP contains upper case characters (excluding those preceded by `\\'),
421 the matching is case-sensitive.
422
423 Second and third arg RSTART and REND specify the region to operate on.
424
425 Interactively, in Transient Mark mode when the mark is active, operate
426 on the contents of the region. Otherwise, operate from point to the
427 end of the buffer."
428
429 (interactive
430 (keep-lines-read-args "How many matches for (regexp): "))
431 (save-excursion
432 (if rstart
433 (goto-char (min rstart rend))
434 (if (and transient-mark-mode mark-active)
435 (setq rstart (region-beginning)
436 rend (copy-marker (region-end)))
437 (setq rstart (point)
438 rend (point-max-marker)))
439 (goto-char rstart))
440 (let ((count 0)
441 opoint
442 (case-fold-search (and case-fold-search
443 (isearch-no-upper-case-p regexp t))))
444 (while (and (< (point) rend)
445 (progn (setq opoint (point))
446 (re-search-forward regexp rend t)))
447 (if (= opoint (point))
448 (forward-char 1)
449 (setq count (1+ count))))
450 (message "%d occurrences" count))))
451
452 \f
453 (defvar occur-mode-map
454 (let ((map (make-sparse-keymap)))
455 (define-key map [mouse-2] 'occur-mode-mouse-goto)
456 (define-key map "\C-c\C-c" 'occur-mode-goto-occurrence)
457 (define-key map "\C-m" 'occur-mode-goto-occurrence)
458 (define-key map "o" 'occur-mode-goto-occurrence-other-window)
459 (define-key map "\C-o" 'occur-mode-display-occurrence)
460 (define-key map "\M-n" 'occur-next)
461 (define-key map "\M-p" 'occur-prev)
462 (define-key map "r" 'occur-rename-buffer)
463 (define-key map "c" 'clone-buffer)
464 (define-key map "g" 'revert-buffer)
465 (define-key map "q" 'quit-window)
466 (define-key map "z" 'kill-this-buffer)
467 map)
468 "Keymap for `occur-mode'.")
469
470 (defvar occur-revert-arguments nil
471 "Arguments to pass to `occur-1' to revert an Occur mode buffer.
472 See `occur-revert-function'.")
473
474 (defcustom occur-mode-hook '(turn-on-font-lock)
475 "Hook run when entering Occur mode."
476 :type 'hook
477 :group 'matching)
478
479 (defcustom occur-hook nil
480 "Hook run when `occur' is called."
481 :type 'hook
482 :group 'matching)
483
484 (put 'occur-mode 'mode-class 'special)
485 (defun occur-mode ()
486 "Major mode for output from \\[occur].
487 \\<occur-mode-map>Move point to one of the items in this buffer, then use
488 \\[occur-mode-goto-occurrence] to go to the occurrence that the item refers to.
489 Alternatively, click \\[occur-mode-mouse-goto] on an item to go to it.
490
491 \\{occur-mode-map}"
492 (interactive)
493 (kill-all-local-variables)
494 (use-local-map occur-mode-map)
495 (setq major-mode 'occur-mode)
496 (setq mode-name "Occur")
497 (set (make-local-variable 'revert-buffer-function) 'occur-revert-function)
498 (make-local-variable 'occur-revert-arguments)
499 (add-hook 'change-major-mode-hook 'font-lock-defontify nil t)
500 (run-hooks 'occur-mode-hook))
501
502 (defun occur-revert-function (ignore1 ignore2)
503 "Handle `revert-buffer' for Occur mode buffers."
504 (apply 'occur-1 (append occur-revert-arguments (list (buffer-name)))))
505
506 (defun occur-mode-mouse-goto (event)
507 "In Occur mode, go to the occurrence whose line you click on."
508 (interactive "e")
509 (let (pos)
510 (save-excursion
511 (set-buffer (window-buffer (posn-window (event-end event))))
512 (save-excursion
513 (goto-char (posn-point (event-end event)))
514 (setq pos (occur-mode-find-occurrence))))
515 (pop-to-buffer (marker-buffer pos))
516 (goto-char pos)))
517
518 (defun occur-mode-find-occurrence ()
519 (let ((pos (get-text-property (point) 'occur-target)))
520 (unless pos
521 (error "No occurrence on this line"))
522 (unless (buffer-live-p (marker-buffer pos))
523 (error "Buffer for this occurrence was killed"))
524 pos))
525
526 (defun occur-mode-goto-occurrence ()
527 "Go to the occurrence the current line describes."
528 (interactive)
529 (let ((pos (occur-mode-find-occurrence)))
530 (pop-to-buffer (marker-buffer pos))
531 (goto-char pos)))
532
533 (defun occur-mode-goto-occurrence-other-window ()
534 "Go to the occurrence the current line describes, in another window."
535 (interactive)
536 (let ((pos (occur-mode-find-occurrence)))
537 (switch-to-buffer-other-window (marker-buffer pos))
538 (goto-char pos)))
539
540 (defun occur-mode-display-occurrence ()
541 "Display in another window the occurrence the current line describes."
542 (interactive)
543 (let ((pos (occur-mode-find-occurrence))
544 window
545 ;; Bind these to ensure `display-buffer' puts it in another window.
546 same-window-buffer-names
547 same-window-regexps)
548 (setq window (display-buffer (marker-buffer pos)))
549 ;; This is the way to set point in the proper window.
550 (save-selected-window
551 (select-window window)
552 (goto-char pos))))
553
554 (defun occur-find-match (n search message)
555 (if (not n) (setq n 1))
556 (let ((r))
557 (while (> n 0)
558 (setq r (funcall search (point) 'occur-match))
559 (and r
560 (get-text-property r 'occur-match)
561 (setq r (funcall search r 'occur-match)))
562 (if r
563 (goto-char r)
564 (error message))
565 (setq n (1- n)))))
566
567 (defun occur-next (&optional n)
568 "Move to the Nth (default 1) next match in an Occur mode buffer."
569 (interactive "p")
570 (occur-find-match n #'next-single-property-change "No more matches"))
571
572 (defun occur-prev (&optional n)
573 "Move to the Nth (default 1) previous match in an Occur mode buffer."
574 (interactive "p")
575 (occur-find-match n #'previous-single-property-change "No earlier matches"))
576 \f
577 (defcustom list-matching-lines-default-context-lines 0
578 "*Default number of context lines included around `list-matching-lines' matches.
579 A negative number means to include that many lines before the match.
580 A positive number means to include that many lines both before and after."
581 :type 'integer
582 :group 'matching)
583
584 (defalias 'list-matching-lines 'occur)
585
586 (defcustom list-matching-lines-face 'bold
587 "*Face used by \\[list-matching-lines] to show the text that matches.
588 If the value is nil, don't highlight the matching portions specially."
589 :type 'face
590 :group 'matching)
591
592 (defcustom list-matching-lines-buffer-name-face 'underline
593 "*Face used by \\[list-matching-lines] to show the names of buffers.
594 If the value is nil, don't highlight the buffer names specially."
595 :type 'face
596 :group 'matching)
597
598 (defun occur-accumulate-lines (count &optional no-props)
599 (save-excursion
600 (let ((forwardp (> count 0))
601 (result nil))
602 (while (not (or (zerop count)
603 (if forwardp
604 (eobp)
605 (bobp))))
606 (setq count (+ count (if forwardp -1 1)))
607 (push
608 (funcall (if no-props
609 #'buffer-substring-no-properties
610 #'buffer-substring)
611 (line-beginning-position)
612 (line-end-position))
613 result)
614 (forward-line (if forwardp 1 -1)))
615 (nreverse result))))
616
617 (defun occur-read-primary-args ()
618 (list (let* ((default (car regexp-history))
619 (input
620 (read-from-minibuffer
621 (if default
622 (format "List lines matching regexp (default `%s'): "
623 default)
624 "List lines matching regexp: ")
625 nil
626 nil
627 nil
628 'regexp-history)))
629 (if (equal input "")
630 default
631 input))
632 (when current-prefix-arg
633 (prefix-numeric-value current-prefix-arg))))
634
635 (defun occur-rename-buffer (&optional unique-p)
636 "Rename the current *Occur* buffer to *Occur: original-buffer-name*.
637 Here `original-buffer-name' is the buffer name were occur was originally run.
638 When given the prefix argument, the renaming will not clobber the existing
639 buffer(s) of that name, but use `generate-new-buffer-name' instead.
640 You can add this to `occur-hook' if you always want a separate *Occur*
641 buffer for each buffer where you invoke `occur'."
642 (interactive "P")
643 (with-current-buffer
644 (if (eq major-mode 'occur-mode) (current-buffer) (get-buffer "*Occur*"))
645 (rename-buffer (concat "*Occur: "
646 (mapconcat #'buffer-name
647 (car (cddr occur-revert-arguments)) "/")
648 "*")
649 unique-p)))
650
651 (defun occur (regexp &optional nlines)
652 "Show all lines in the current buffer containing a match for REGEXP.
653
654 If a match spreads across multiple lines, all those lines are shown.
655
656 Each line is displayed with NLINES lines before and after, or -NLINES
657 before if NLINES is negative.
658 NLINES defaults to `list-matching-lines-default-context-lines'.
659 Interactively it is the prefix arg.
660
661 The lines are shown in a buffer named `*Occur*'.
662 It serves as a menu to find any of the occurrences in this buffer.
663 \\<occur-mode-map>\\[describe-mode] in that buffer will explain how.
664
665 If REGEXP contains upper case characters (excluding those preceded by `\\'),
666 the matching is case-sensitive."
667 (interactive (occur-read-primary-args))
668 (occur-1 regexp nlines (list (current-buffer))))
669
670 (defun multi-occur (bufs regexp &optional nlines)
671 "Show all lines in buffers BUFS containing a match for REGEXP.
672 This function acts on multiple buffers; otherwise, it is exactly like
673 `occur'."
674 (interactive
675 (cons
676 (let* ((bufs (list (read-buffer "First buffer to search: "
677 (current-buffer) t)))
678 (buf nil)
679 (ido-ignore-item-temp-list bufs))
680 (while (not (string-equal
681 (setq buf (read-buffer
682 (if (eq read-buffer-function 'ido-read-buffer)
683 "Next buffer to search (C-j to end): "
684 "Next buffer to search (RET to end): ")
685 nil t))
686 ""))
687 (add-to-list 'bufs buf)
688 (setq ido-ignore-item-temp-list bufs))
689 (nreverse (mapcar #'get-buffer bufs)))
690 (occur-read-primary-args)))
691 (occur-1 regexp nlines bufs))
692
693 (defun multi-occur-by-filename-regexp (bufregexp regexp &optional nlines)
694 "Show all lines matching REGEXP in buffers named by BUFREGEXP.
695 See also `multi-occur'."
696 (interactive
697 (cons
698 (let* ((default (car regexp-history))
699 (input
700 (read-from-minibuffer
701 "List lines in buffers whose filename matches regexp: "
702 nil
703 nil
704 nil
705 'regexp-history)))
706 (if (equal input "")
707 default
708 input))
709 (occur-read-primary-args)))
710 (when bufregexp
711 (occur-1 regexp nlines
712 (delq nil
713 (mapcar (lambda (buf)
714 (when (and (buffer-file-name buf)
715 (string-match bufregexp
716 (buffer-file-name buf)))
717 buf))
718 (buffer-list))))))
719
720 (defun occur-1 (regexp nlines bufs &optional buf-name)
721 (unless buf-name
722 (setq buf-name "*Occur*"))
723 (let ((occur-buf (get-buffer-create buf-name))
724 (made-temp-buf nil)
725 (active-bufs (delq nil (mapcar #'(lambda (buf)
726 (when (buffer-live-p buf) buf))
727 bufs))))
728 ;; Handle the case where one of the buffers we're searching is the
729 ;; *Occur* buffer itself.
730 (when (memq occur-buf bufs)
731 (setq occur-buf (with-current-buffer occur-buf
732 (clone-buffer "*Occur-temp*"))
733 made-temp-buf t))
734 (with-current-buffer occur-buf
735 (setq buffer-read-only nil)
736 (occur-mode)
737 (erase-buffer)
738 (let ((count (occur-engine
739 regexp active-bufs occur-buf
740 (or nlines list-matching-lines-default-context-lines)
741 (and case-fold-search
742 (isearch-no-upper-case-p regexp t))
743 list-matching-lines-buffer-name-face
744 nil list-matching-lines-face nil)))
745 (let* ((bufcount (length active-bufs))
746 (diff (- (length bufs) bufcount)))
747 (message "Searched %d buffer%s%s; %s match%s for `%s'"
748 bufcount (if (= bufcount 1) "" "s")
749 (if (zerop diff) "" (format " (%d killed)" diff))
750 (if (zerop count) "no" (format "%d" count))
751 (if (= count 1) "" "es")
752 regexp))
753 ;; If we had to make a temporary buffer, make it the *Occur*
754 ;; buffer now.
755 (when made-temp-buf
756 (with-current-buffer (get-buffer buf-name)
757 (kill-buffer (current-buffer)))
758 (rename-buffer buf-name))
759 (setq occur-revert-arguments (list regexp nlines bufs)
760 buffer-read-only t)
761 (if (> count 0)
762 (display-buffer occur-buf)
763 (kill-buffer occur-buf)))
764 (run-hooks 'occur-hook))))
765
766 (defun occur-engine-add-prefix (lines)
767 (mapcar
768 #'(lambda (line)
769 (concat " :" line "\n"))
770 lines))
771
772 (defun occur-engine (regexp buffers out-buf nlines case-fold-search
773 title-face prefix-face match-face keep-props)
774 (with-current-buffer out-buf
775 (setq buffer-read-only nil)
776 (let ((globalcount 0)
777 (coding nil))
778 ;; Map over all the buffers
779 (dolist (buf buffers)
780 (when (buffer-live-p buf)
781 (let ((matches 0) ;; count of matched lines
782 (lines 1) ;; line count
783 (matchbeg 0)
784 (matchend 0)
785 (origpt nil)
786 (begpt nil)
787 (endpt nil)
788 (marker nil)
789 (curstring "")
790 (headerpt (with-current-buffer out-buf (point))))
791 (save-excursion
792 (set-buffer buf)
793 (or coding
794 ;; Set CODING only if the current buffer locally
795 ;; binds buffer-file-coding-system.
796 (not (local-variable-p 'buffer-file-coding-system))
797 (setq coding buffer-file-coding-system))
798 (save-excursion
799 (goto-char (point-min)) ;; begin searching in the buffer
800 (while (not (eobp))
801 (setq origpt (point))
802 (when (setq endpt (re-search-forward regexp nil t))
803 (setq matches (1+ matches)) ;; increment match count
804 (setq matchbeg (match-beginning 0)
805 matchend (match-end 0))
806 (setq begpt (save-excursion
807 (goto-char matchbeg)
808 (line-beginning-position)))
809 (setq lines (+ lines (1- (count-lines origpt endpt))))
810 (setq marker (make-marker))
811 (set-marker marker matchbeg)
812 (setq curstring (buffer-substring begpt
813 (line-end-position)))
814 ;; Depropertize the string, and maybe
815 ;; highlight the matches
816 (let ((len (length curstring))
817 (start 0))
818 (unless keep-props
819 (set-text-properties 0 len nil curstring))
820 (while (and (< start len)
821 (string-match regexp curstring start))
822 (add-text-properties (match-beginning 0)
823 (match-end 0)
824 (append
825 `(occur-match t)
826 (when match-face
827 `(font-lock-face ,match-face)))
828 curstring)
829 (setq start (match-end 0))))
830 ;; Generate the string to insert for this match
831 (let* ((out-line
832 (concat
833 ;; Using 7 digits aligns tabs properly.
834 (apply #'propertize (format "%7d:" lines)
835 (append
836 (when prefix-face
837 `(font-lock-face prefix-face))
838 '(occur-prefix t)))
839 curstring
840 "\n"))
841 (data
842 (if (= nlines 0)
843 ;; The simple display style
844 out-line
845 ;; The complex multi-line display
846 ;; style. Generate a list of lines,
847 ;; concatenate them all together.
848 (apply #'concat
849 (nconc
850 (occur-engine-add-prefix (nreverse (cdr (occur-accumulate-lines (- (1+ nlines)) keep-props))))
851 (list out-line)
852 (occur-engine-add-prefix (cdr (occur-accumulate-lines (1+ nlines) keep-props))))))))
853 ;; Actually insert the match display data
854 (with-current-buffer out-buf
855 (let ((beg (point))
856 (end (progn (insert data) (point))))
857 (unless (= nlines 0)
858 (insert "-------\n"))
859 (add-text-properties
860 beg end
861 `(occur-target ,marker help-echo "mouse-2: go to this occurrence"))
862 ;; We don't put `mouse-face' on the newline,
863 ;; because that loses.
864 (add-text-properties beg (1- end) '(mouse-face highlight)))))
865 (goto-char endpt))
866 (if endpt
867 (progn
868 (setq lines (1+ lines))
869 ;; On to the next match...
870 (forward-line 1))
871 (goto-char (point-max))))))
872 (when (not (zerop matches)) ;; is the count zero?
873 (setq globalcount (+ globalcount matches))
874 (with-current-buffer out-buf
875 (goto-char headerpt)
876 (let ((beg (point))
877 end)
878 (insert (format "%d lines matching \"%s\" in buffer: %s\n"
879 matches regexp (buffer-name buf)))
880 (setq end (point))
881 (add-text-properties beg end
882 (append
883 (when title-face
884 `(font-lock-face ,title-face))
885 `(occur-title ,buf))))
886 (goto-char (point-min)))))))
887 (if coding
888 ;; CODING is buffer-file-coding-system of the first buffer
889 ;; that locally binds it. Let's use it also for the output
890 ;; buffer.
891 (set-buffer-file-coding-system coding))
892 ;; Return the number of matches
893 globalcount)))
894
895 \f
896 ;; It would be nice to use \\[...], but there is no reasonable way
897 ;; to make that display both SPC and Y.
898 (defconst query-replace-help
899 "Type Space or `y' to replace one match, Delete or `n' to skip to next,
900 RET or `q' to exit, Period to replace one match and exit,
901 Comma to replace but not move point immediately,
902 C-r to enter recursive edit (\\[exit-recursive-edit] to get out again),
903 C-w to delete match and recursive edit,
904 C-l to clear the screen, redisplay, and offer same replacement again,
905 ! to replace all remaining matches with no more questions,
906 ^ to move point back to previous match,
907 E to edit the replacement string"
908 "Help message while in `query-replace'.")
909
910 (defvar query-replace-map (make-sparse-keymap)
911 "Keymap that defines the responses to questions in `query-replace'.
912 The \"bindings\" in this map are not commands; they are answers.
913 The valid answers include `act', `skip', `act-and-show',
914 `exit', `act-and-exit', `edit', `delete-and-edit', `recenter',
915 `automatic', `backup', `exit-prefix', and `help'.")
916
917 (define-key query-replace-map " " 'act)
918 (define-key query-replace-map "\d" 'skip)
919 (define-key query-replace-map [delete] 'skip)
920 (define-key query-replace-map [backspace] 'skip)
921 (define-key query-replace-map "y" 'act)
922 (define-key query-replace-map "n" 'skip)
923 (define-key query-replace-map "Y" 'act)
924 (define-key query-replace-map "N" 'skip)
925 (define-key query-replace-map "e" 'edit-replacement)
926 (define-key query-replace-map "E" 'edit-replacement)
927 (define-key query-replace-map "," 'act-and-show)
928 (define-key query-replace-map "q" 'exit)
929 (define-key query-replace-map "\r" 'exit)
930 (define-key query-replace-map [return] 'exit)
931 (define-key query-replace-map "." 'act-and-exit)
932 (define-key query-replace-map "\C-r" 'edit)
933 (define-key query-replace-map "\C-w" 'delete-and-edit)
934 (define-key query-replace-map "\C-l" 'recenter)
935 (define-key query-replace-map "!" 'automatic)
936 (define-key query-replace-map "^" 'backup)
937 (define-key query-replace-map "\C-h" 'help)
938 (define-key query-replace-map [f1] 'help)
939 (define-key query-replace-map [help] 'help)
940 (define-key query-replace-map "?" 'help)
941 (define-key query-replace-map "\C-g" 'quit)
942 (define-key query-replace-map "\C-]" 'quit)
943 (define-key query-replace-map "\e" 'exit-prefix)
944 (define-key query-replace-map [escape] 'exit-prefix)
945
946 (defun replace-match-string-symbols (n)
947 "Process a list (and any sub-lists), expanding certain symbols.
948 Symbol Expands To
949 N (match-string N) (where N is a string of digits)
950 #N (string-to-number (match-string N))
951 & (match-string 0)
952 #& (string-to-number (match-string 0))
953
954 Note that these symbols must be preceeded by a backslash in order to
955 type them."
956 (while n
957 (cond
958 ((consp (car n))
959 (replace-match-string-symbols (car n))) ;Process sub-list
960 ((symbolp (car n))
961 (let ((name (symbol-name (car n))))
962 (cond
963 ((string-match "^[0-9]+$" name)
964 (setcar n (list 'match-string (string-to-number name))))
965 ((string-match "^#[0-9]+$" name)
966 (setcar n (list 'string-to-number
967 (list 'match-string
968 (string-to-number (substring name 1))))))
969 ((string= "&" name)
970 (setcar n '(match-string 0)))
971 ((string= "#&" name)
972 (setcar n '(string-to-number (match-string 0))))))))
973 (setq n (cdr n))))
974
975 (defun replace-eval-replacement (expression replace-count)
976 (let ((replacement (eval expression)))
977 (if (stringp replacement)
978 replacement
979 (prin1-to-string replacement t))))
980
981 (defun replace-loop-through-replacements (data replace-count)
982 ;; DATA is a vector contaning the following values:
983 ;; 0 next-rotate-count
984 ;; 1 repeat-count
985 ;; 2 next-replacement
986 ;; 3 replacements
987 (if (= (aref data 0) replace-count)
988 (progn
989 (aset data 0 (+ replace-count (aref data 1)))
990 (let ((next (cdr (aref data 2))))
991 (aset data 2 (if (consp next) next (aref data 3))))))
992 (car (aref data 2)))
993
994 (defun perform-replace (from-string replacements
995 query-flag regexp-flag delimited-flag
996 &optional repeat-count map start end)
997 "Subroutine of `query-replace'. Its complexity handles interactive queries.
998 Don't use this in your own program unless you want to query and set the mark
999 just as `query-replace' does. Instead, write a simple loop like this:
1000
1001 (while (re-search-forward \"foo[ \\t]+bar\" nil t)
1002 (replace-match \"foobar\" nil nil))
1003
1004 which will run faster and probably do exactly what you want. Please
1005 see the documentation of `replace-match' to find out how to simulate
1006 `case-replace'.
1007
1008 This function returns nil if and only if there were no matches to
1009 make, or the user didn't cancel the call."
1010 (or map (setq map query-replace-map))
1011 (and query-flag minibuffer-auto-raise
1012 (raise-frame (window-frame (minibuffer-window))))
1013 (let ((nocasify (not (and case-fold-search case-replace
1014 (string-equal from-string
1015 (downcase from-string)))))
1016 (case-fold-search (and case-fold-search
1017 (string-equal from-string
1018 (downcase from-string))))
1019 (literal (not regexp-flag))
1020 (search-function (if regexp-flag 're-search-forward 'search-forward))
1021 (search-string from-string)
1022 (real-match-data nil) ; the match data for the current match
1023 (next-replacement nil)
1024 (keep-going t)
1025 (stack nil)
1026 (replace-count 0)
1027 (nonempty-match nil)
1028
1029 ;; If non-nil, it is marker saying where in the buffer to stop.
1030 (limit nil)
1031
1032 ;; Data for the next match. If a cons, it has the same format as
1033 ;; (match-data); otherwise it is t if a match is possible at point.
1034 (match-again t)
1035
1036 (message
1037 (if query-flag
1038 (substitute-command-keys
1039 "Query replacing %s with %s: (\\<query-replace-map>\\[help] for help) "))))
1040
1041 ;; If region is active, in Transient Mark mode, operate on region.
1042 (when start
1043 (setq limit (copy-marker (max start end)))
1044 (goto-char (min start end))
1045 (deactivate-mark))
1046
1047 ;; REPLACEMENTS is either a string, a list of strings, or a cons cell
1048 ;; containing a function and its first argument. The function is
1049 ;; called to generate each replacement like this:
1050 ;; (funcall (car replacements) (cdr replacements) replace-count)
1051 ;; It must return a string.
1052 (cond
1053 ((stringp replacements)
1054 (setq next-replacement replacements
1055 replacements nil))
1056 ((stringp (car replacements)) ; If it isn't a string, it must be a cons
1057 (or repeat-count (setq repeat-count 1))
1058 (setq replacements (cons 'replace-loop-through-replacements
1059 (vector repeat-count repeat-count
1060 replacements replacements)))))
1061
1062 (if delimited-flag
1063 (setq search-function 're-search-forward
1064 search-string (concat "\\b"
1065 (if regexp-flag from-string
1066 (regexp-quote from-string))
1067 "\\b")))
1068 (push-mark)
1069 (undo-boundary)
1070 (unwind-protect
1071 ;; Loop finding occurrences that perhaps should be replaced.
1072 (while (and keep-going
1073 (not (eobp))
1074 ;; Use the next match if it is already known;
1075 ;; otherwise, search for a match after moving forward
1076 ;; one char if progress is required.
1077 (setq real-match-data
1078 (if (consp match-again)
1079 (progn (goto-char (nth 1 match-again))
1080 match-again)
1081 (and (or match-again
1082 ;; MATCH-AGAIN non-nil means we
1083 ;; accept an adjacent match. If
1084 ;; we don't, move one char to the
1085 ;; right. This takes us a
1086 ;; character too far at the end,
1087 ;; but this is undone after the
1088 ;; while-loop.
1089 (progn (forward-char 1) (not (eobp))))
1090 (funcall search-function search-string limit t)
1091 ;; For speed, use only integers and
1092 ;; reuse the list used last time.
1093 (match-data t real-match-data)))))
1094 ;; Optionally ignore matches that have a read-only property.
1095 (unless (and query-replace-skip-read-only
1096 (text-property-not-all
1097 (match-beginning 0) (match-end 0)
1098 'read-only nil))
1099
1100 ;; Record whether the match is nonempty, to avoid an infinite loop
1101 ;; repeatedly matching the same empty string.
1102 (setq nonempty-match
1103 (/= (nth 0 real-match-data) (nth 1 real-match-data)))
1104
1105 ;; If the match is empty, record that the next one can't be
1106 ;; adjacent.
1107
1108 ;; Otherwise, if matching a regular expression, do the next
1109 ;; match now, since the replacement for this match may
1110 ;; affect whether the next match is adjacent to this one.
1111 ;; If that match is empty, don't use it.
1112 (setq match-again
1113 (and nonempty-match
1114 (or (not regexp-flag)
1115 (and (looking-at search-string)
1116 (let ((match (match-data)))
1117 (and (/= (nth 0 match) (nth 1 match))
1118 match))))))
1119
1120 ;; Calculate the replacement string, if necessary.
1121 (when replacements
1122 (set-match-data real-match-data)
1123 (setq next-replacement
1124 (funcall (car replacements) (cdr replacements)
1125 replace-count)))
1126 (if (not query-flag)
1127 (let ((inhibit-read-only query-replace-skip-read-only))
1128 (set-match-data real-match-data)
1129 (replace-match next-replacement nocasify literal)
1130 (setq replace-count (1+ replace-count)))
1131 (undo-boundary)
1132 (let (done replaced key def)
1133 ;; Loop reading commands until one of them sets done,
1134 ;; which means it has finished handling this occurrence.
1135 (while (not done)
1136 (set-match-data real-match-data)
1137 (replace-highlight (match-beginning 0) (match-end 0))
1138 ;; Bind message-log-max so we don't fill up the message log
1139 ;; with a bunch of identical messages.
1140 (let ((message-log-max nil))
1141 (message message from-string next-replacement))
1142 (setq key (read-event))
1143 ;; Necessary in case something happens during read-event
1144 ;; that clobbers the match data.
1145 (set-match-data real-match-data)
1146 (setq key (vector key))
1147 (setq def (lookup-key map key))
1148 ;; Restore the match data while we process the command.
1149 (cond ((eq def 'help)
1150 (with-output-to-temp-buffer "*Help*"
1151 (princ
1152 (concat "Query replacing "
1153 (if regexp-flag "regexp " "")
1154 from-string " with "
1155 next-replacement ".\n\n"
1156 (substitute-command-keys
1157 query-replace-help)))
1158 (with-current-buffer standard-output
1159 (help-mode))))
1160 ((eq def 'exit)
1161 (setq keep-going nil)
1162 (setq done t))
1163 ((eq def 'backup)
1164 (if stack
1165 (let ((elt (pop stack)))
1166 (goto-char (car elt))
1167 (setq replaced (eq t (cdr elt)))
1168 (or replaced
1169 (set-match-data (cdr elt))))
1170 (message "No previous match")
1171 (ding 'no-terminate)
1172 (sit-for 1)))
1173 ((eq def 'act)
1174 (or replaced
1175 (progn
1176 (replace-match next-replacement nocasify literal)
1177 (setq replace-count (1+ replace-count))))
1178 (setq done t replaced t))
1179 ((eq def 'act-and-exit)
1180 (or replaced
1181 (progn
1182 (replace-match next-replacement nocasify literal)
1183 (setq replace-count (1+ replace-count))))
1184 (setq keep-going nil)
1185 (setq done t replaced t))
1186 ((eq def 'act-and-show)
1187 (if (not replaced)
1188 (progn
1189 (replace-match next-replacement nocasify literal)
1190 (setq replace-count (1+ replace-count))
1191 (setq replaced t))))
1192 ((eq def 'automatic)
1193 (or replaced
1194 (progn
1195 (replace-match next-replacement nocasify literal)
1196 (setq replace-count (1+ replace-count))))
1197 (setq done t query-flag nil replaced t))
1198 ((eq def 'skip)
1199 (setq done t))
1200 ((eq def 'recenter)
1201 (recenter nil))
1202 ((eq def 'edit)
1203 (let ((opos (point-marker)))
1204 (goto-char (match-beginning 0))
1205 (save-excursion
1206 (funcall search-function search-string limit t)
1207 (setq real-match-data (match-data)))
1208 (save-excursion
1209 (save-window-excursion
1210 (recursive-edit)))
1211 (goto-char opos))
1212 (set-match-data real-match-data)
1213 ;; Before we make the replacement,
1214 ;; decide whether the search string
1215 ;; can match again just after this match.
1216 (if (and regexp-flag nonempty-match)
1217 (setq match-again (and (looking-at search-string)
1218 (match-data)))))
1219
1220 ;; Edit replacement.
1221 ((eq def 'edit-replacement)
1222 (setq next-replacement
1223 (read-input "Edit replacement string: "
1224 next-replacement))
1225 (or replaced
1226 (replace-match next-replacement nocasify literal))
1227 (setq done t))
1228
1229 ((eq def 'delete-and-edit)
1230 (delete-region (match-beginning 0) (match-end 0))
1231 (set-match-data
1232 (prog1 (match-data)
1233 (save-excursion (recursive-edit))))
1234 (setq replaced t))
1235 ;; Note: we do not need to treat `exit-prefix'
1236 ;; specially here, since we reread
1237 ;; any unrecognized character.
1238 (t
1239 (setq this-command 'mode-exited)
1240 (setq keep-going nil)
1241 (setq unread-command-events
1242 (append (listify-key-sequence key)
1243 unread-command-events))
1244 (setq done t))))
1245 ;; Record previous position for ^ when we move on.
1246 ;; Change markers to numbers in the match data
1247 ;; since lots of markers slow down editing.
1248 (setq stack
1249 (cons (cons (point)
1250 (or replaced (match-data t)))
1251 stack))))))
1252
1253 ;; The code preventing adjacent regexp matches in the condition
1254 ;; of the while-loop above will haven taken us one character
1255 ;; beyond the last replacement. Undo that.
1256 (when (and regexp-flag (not match-again) (> replace-count 0))
1257 (backward-char 1))
1258
1259 (replace-dehighlight))
1260 (or unread-command-events
1261 (message "Replaced %d occurrence%s"
1262 replace-count
1263 (if (= replace-count 1) "" "s")))
1264 (and keep-going stack)))
1265
1266 (defcustom query-replace-highlight t
1267 "*Non-nil means to highlight words during query replacement."
1268 :type 'boolean
1269 :group 'matching)
1270
1271 (defvar replace-overlay nil)
1272
1273 (defun replace-dehighlight ()
1274 (and replace-overlay
1275 (progn
1276 (delete-overlay replace-overlay)
1277 (setq replace-overlay nil))))
1278
1279 (defun replace-highlight (start end)
1280 (and query-replace-highlight
1281 (progn
1282 (or replace-overlay
1283 (progn
1284 (setq replace-overlay (make-overlay start end))
1285 (overlay-put replace-overlay 'face
1286 (if (facep 'query-replace)
1287 'query-replace 'region))))
1288 (move-overlay replace-overlay start end (current-buffer)))))
1289
1290 ;;; replace.el ends here