(compose-region): Mention the special handling of TAB.
[bpt/emacs.git] / lisp / replace.el
1 ;;; replace.el --- replace commands for Emacs
2
3 ;; Copyright (C) 1985, 1986, 1987, 1992, 1994, 1996, 1997, 2000, 2001,
4 ;; 2002, 2003, 2004, 2005, 2006, 2007, 2008 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 3 of the License, or
13 ;; (at your option) 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. If not, see <http://www.gnu.org/licenses/>.
22
23 ;;; Commentary:
24
25 ;; This package supplies the string and regular-expression replace functions
26 ;; documented in the Emacs user's manual.
27
28 ;;; Code:
29
30 (defcustom case-replace t
31 "*Non-nil means `query-replace' should preserve case in replacements."
32 :type 'boolean
33 :group 'matching)
34
35 (defvar query-replace-history nil)
36
37 (defvar query-replace-defaults nil
38 "Default values of FROM-STRING and TO-STRING for `query-replace'.
39 This is a cons cell (FROM-STRING . TO-STRING), or nil if there is
40 no default value.")
41
42 (defvar query-replace-interactive nil
43 "Non-nil means `query-replace' uses the last search string.
44 That becomes the \"string to replace\".")
45
46 (defcustom query-replace-from-history-variable 'query-replace-history
47 "History list to use for the FROM argument of `query-replace' commands.
48 The value of this variable should be a symbol; that symbol
49 is used as a variable to hold a history list for the strings
50 or patterns to be replaced."
51 :group 'matching
52 :type 'symbol
53 :version "20.3")
54
55 (defcustom query-replace-to-history-variable 'query-replace-history
56 "History list to use for the TO argument of `query-replace' commands.
57 The value of this variable should be a symbol; that symbol
58 is used as a variable to hold a history list for replacement
59 strings or patterns."
60 :group 'matching
61 :type 'symbol
62 :version "20.3")
63
64 (defcustom query-replace-skip-read-only nil
65 "*Non-nil means `query-replace' and friends ignore read-only matches."
66 :type 'boolean
67 :group 'matching
68 :version "22.1")
69
70 (defcustom query-replace-show-replacement t
71 "*Non-nil means to show what actual replacement text will be."
72 :type 'boolean
73 :group 'matching
74 :version "23.1")
75
76 (defcustom query-replace-highlight t
77 "*Non-nil means to highlight matches during query replacement."
78 :type 'boolean
79 :group 'matching)
80
81 (defcustom query-replace-lazy-highlight t
82 "*Controls the lazy-highlighting during query replacements.
83 When non-nil, all text in the buffer matching the current match
84 is highlighted lazily using isearch lazy highlighting (see
85 `lazy-highlight-initial-delay' and `lazy-highlight-interval')."
86 :type 'boolean
87 :group 'lazy-highlight
88 :group 'matching
89 :version "22.1")
90
91 (defface query-replace
92 '((t (:inherit isearch)))
93 "Face for highlighting query replacement matches."
94 :group 'matching
95 :version "22.1")
96
97 (defun query-replace-descr (string)
98 (mapconcat 'isearch-text-char-description string ""))
99
100 (defun query-replace-read-from (prompt regexp-flag)
101 "Query and return the `from' argument of a query-replace operation.
102 The return value can also be a pair (FROM . TO) indicating that the user
103 wants to replace FROM with TO."
104 (if query-replace-interactive
105 (car (if regexp-flag regexp-search-ring search-ring))
106 (let* ((history-add-new-input nil)
107 (from
108 ;; The save-excursion here is in case the user marks and copies
109 ;; a region in order to specify the minibuffer input.
110 ;; That should not clobber the region for the query-replace itself.
111 (save-excursion
112 (read-from-minibuffer
113 (if query-replace-defaults
114 (format "%s (default %s -> %s): " prompt
115 (query-replace-descr (car query-replace-defaults))
116 (query-replace-descr (cdr query-replace-defaults)))
117 (format "%s: " prompt))
118 nil nil nil
119 query-replace-from-history-variable
120 nil t))))
121 (if (and (zerop (length from)) query-replace-defaults)
122 (cons (car query-replace-defaults)
123 (query-replace-compile-replacement
124 (cdr query-replace-defaults) regexp-flag))
125 (add-to-history query-replace-from-history-variable from nil t)
126 ;; Warn if user types \n or \t, but don't reject the input.
127 (and regexp-flag
128 (string-match "\\(\\`\\|[^\\]\\)\\(\\\\\\\\\\)*\\(\\\\[nt]\\)" from)
129 (let ((match (match-string 3 from)))
130 (cond
131 ((string= match "\\n")
132 (message "Note: `\\n' here doesn't match a newline; to do that, type C-q C-j instead"))
133 ((string= match "\\t")
134 (message "Note: `\\t' here doesn't match a tab; to do that, just type TAB")))
135 (sit-for 2)))
136 from))))
137
138 (defun query-replace-compile-replacement (to regexp-flag)
139 "Maybe convert a regexp replacement TO to Lisp.
140 Returns a list suitable for `perform-replace' if necessary,
141 the original string if not."
142 (if (and regexp-flag
143 (string-match "\\(\\`\\|[^\\]\\)\\(\\\\\\\\\\)*\\\\[,#]" to))
144 (let (pos list char)
145 (while
146 (progn
147 (setq pos (match-end 0))
148 (push (substring to 0 (- pos 2)) list)
149 (setq char (aref to (1- pos))
150 to (substring to pos))
151 (cond ((eq char ?\#)
152 (push '(number-to-string replace-count) list))
153 ((eq char ?\,)
154 (setq pos (read-from-string to))
155 (push `(replace-quote ,(car pos)) list)
156 (let ((end
157 ;; Swallow a space after a symbol
158 ;; if there is a space.
159 (if (and (or (symbolp (car pos))
160 ;; Swallow a space after 'foo
161 ;; but not after (quote foo).
162 (and (eq (car-safe (car pos)) 'quote)
163 (not (= ?\( (aref to 0)))))
164 (eq (string-match " " to (cdr pos))
165 (cdr pos)))
166 (1+ (cdr pos))
167 (cdr pos))))
168 (setq to (substring to end)))))
169 (string-match "\\(\\`\\|[^\\]\\)\\(\\\\\\\\\\)*\\\\[,#]" to)))
170 (setq to (nreverse (delete "" (cons to list))))
171 (replace-match-string-symbols to)
172 (cons 'replace-eval-replacement
173 (if (cdr to)
174 (cons 'concat to)
175 (car to))))
176 to))
177
178
179 (defun query-replace-read-to (from prompt regexp-flag)
180 "Query and return the `to' argument of a query-replace operation."
181 (query-replace-compile-replacement
182 (save-excursion
183 (let* ((history-add-new-input nil)
184 (to (read-from-minibuffer
185 (format "%s %s with: " prompt (query-replace-descr from))
186 nil nil nil
187 query-replace-to-history-variable from t)))
188 (add-to-history query-replace-to-history-variable to nil t)
189 (setq query-replace-defaults (cons from to))
190 to))
191 regexp-flag))
192
193 (defun query-replace-read-args (prompt regexp-flag &optional noerror)
194 (unless noerror
195 (barf-if-buffer-read-only))
196 (let* ((from (query-replace-read-from prompt regexp-flag))
197 (to (if (consp from) (prog1 (cdr from) (setq from (car from)))
198 (query-replace-read-to from prompt regexp-flag))))
199 (list from to current-prefix-arg)))
200
201 (defun query-replace (from-string to-string &optional delimited start end)
202 "Replace some occurrences of FROM-STRING with TO-STRING.
203 As each match is found, the user must type a character saying
204 what to do with it. For directions, type \\[help-command] at that time.
205
206 In Transient Mark mode, if the mark is active, operate on the contents
207 of the region. Otherwise, operate from point to the end of the buffer.
208
209 If `query-replace-interactive' is non-nil, the last incremental search
210 string is used as FROM-STRING--you don't have to specify it with the
211 minibuffer.
212
213 Matching is independent of case if `case-fold-search' is non-nil and
214 FROM-STRING has no uppercase letters. Replacement transfers the case
215 pattern of the old text to the new text, if `case-replace' and
216 `case-fold-search' are non-nil and FROM-STRING has no uppercase
217 letters. \(Transferring the case pattern means that if the old text
218 matched is all caps, or capitalized, then its replacement is upcased
219 or capitalized.)
220
221 Third arg DELIMITED (prefix arg if interactive), if non-nil, means replace
222 only matches surrounded by word boundaries.
223 Fourth and fifth arg START and END specify the region to operate on.
224
225 To customize possible responses, change the \"bindings\" in `query-replace-map'."
226 (interactive (let ((common
227 (query-replace-read-args
228 (if (and transient-mark-mode mark-active)
229 "Query replace in region"
230 "Query replace")
231 nil)))
232 (list (nth 0 common) (nth 1 common) (nth 2 common)
233 ;; These are done separately here
234 ;; so that command-history will record these expressions
235 ;; rather than the values they had this time.
236 (if (and transient-mark-mode mark-active)
237 (region-beginning))
238 (if (and transient-mark-mode mark-active)
239 (region-end)))))
240 (perform-replace from-string to-string t nil delimited nil nil start end))
241
242 (define-key esc-map "%" 'query-replace)
243
244 (defun query-replace-regexp (regexp to-string &optional delimited start end)
245 "Replace some things after point matching REGEXP with TO-STRING.
246 As each match is found, the user must type a character saying
247 what to do with it. For directions, type \\[help-command] at that time.
248
249 In Transient Mark mode, if the mark is active, operate on the contents
250 of the region. Otherwise, operate from point to the end of the buffer.
251
252 If `query-replace-interactive' is non-nil, the last incremental search
253 regexp is used as REGEXP--you don't have to specify it with the
254 minibuffer.
255
256 Matching is independent of case if `case-fold-search' is non-nil and
257 REGEXP has no uppercase letters. Replacement transfers the case
258 pattern of the old text to the new text, if `case-replace' and
259 `case-fold-search' are non-nil and REGEXP has no uppercase letters.
260 \(Transferring the case pattern means that if the old text matched is
261 all caps, or capitalized, then its replacement is upcased or
262 capitalized.)
263
264 Third arg DELIMITED (prefix arg if interactive), if non-nil, means replace
265 only matches surrounded by word boundaries.
266 Fourth and fifth arg START and END specify the region to operate on.
267
268 In TO-STRING, `\\&' stands for whatever matched the whole of REGEXP,
269 and `\\=\\N' (where N is a digit) stands for
270 whatever what matched the Nth `\\(...\\)' in REGEXP.
271 `\\?' lets you edit the replacement text in the minibuffer
272 at the given position for each replacement.
273
274 In interactive calls, the replacement text can contain `\\,'
275 followed by a Lisp expression. Each
276 replacement evaluates that expression to compute the replacement
277 string. Inside of that expression, `\\&' is a string denoting the
278 whole match as a string, `\\N' for a partial match, `\\#&' and `\\#N'
279 for the whole or a partial match converted to a number with
280 `string-to-number', and `\\#' itself for the number of replacements
281 done so far (starting with zero).
282
283 If the replacement expression is a symbol, write a space after it
284 to terminate it. One space there, if any, will be discarded.
285
286 When using those Lisp features interactively in the replacement
287 text, TO-STRING is actually made a list instead of a string.
288 Use \\[repeat-complex-command] after this command for details."
289 (interactive
290 (let ((common
291 (query-replace-read-args
292 (if (and transient-mark-mode mark-active)
293 "Query replace regexp in region"
294 "Query replace regexp")
295 t)))
296 (list (nth 0 common) (nth 1 common) (nth 2 common)
297 ;; These are done separately here
298 ;; so that command-history will record these expressions
299 ;; rather than the values they had this time.
300 (if (and transient-mark-mode mark-active)
301 (region-beginning))
302 (if (and transient-mark-mode mark-active)
303 (region-end)))))
304 (perform-replace regexp to-string t t delimited nil nil start end))
305
306 (define-key esc-map [?\C-%] 'query-replace-regexp)
307
308 (defun query-replace-regexp-eval (regexp to-expr &optional delimited start end)
309 "Replace some things after point matching REGEXP with the result of TO-EXPR.
310
311 Interactive use of this function is deprecated in favor of the
312 `\\,' feature of `query-replace-regexp'. For non-interactive use, a loop
313 using `search-forward-regexp' and `replace-match' is preferred.
314
315 As each match is found, the user must type a character saying
316 what to do with it. For directions, type \\[help-command] at that time.
317
318 TO-EXPR is a Lisp expression evaluated to compute each replacement. It may
319 reference `replace-count' to get the number of replacements already made.
320 If the result of TO-EXPR is not a string, it is converted to one using
321 `prin1-to-string' with the NOESCAPE argument (which see).
322
323 For convenience, when entering TO-EXPR interactively, you can use `\\&' or
324 `\\0' to stand for whatever matched the whole of REGEXP, and `\\N' (where
325 N is a digit) to stand for whatever matched the Nth `\\(...\\)' in REGEXP.
326 Use `\\#&' or `\\#N' if you want a number instead of a string.
327 In interactive use, `\\#' in itself stands for `replace-count'.
328
329 In Transient Mark mode, if the mark is active, operate on the contents
330 of the region. Otherwise, operate from point to the end of the buffer.
331
332 If `query-replace-interactive' is non-nil, the last incremental search
333 regexp is used as REGEXP--you don't have to specify it with the
334 minibuffer.
335
336 Preserves case in each replacement if `case-replace' and `case-fold-search'
337 are non-nil and REGEXP has no uppercase letters.
338
339 Third arg DELIMITED (prefix arg if interactive), if non-nil, means replace
340 only matches that are surrounded by word boundaries.
341 Fourth and fifth arg START and END specify the region to operate on."
342 (interactive
343 (progn
344 (barf-if-buffer-read-only)
345 (let* ((from
346 ;; Let-bind the history var to disable the "foo -> bar" default.
347 ;; Maybe we shouldn't disable this default, but for now I'll
348 ;; leave it off. --Stef
349 (let ((query-replace-to-history-variable nil))
350 (query-replace-read-from "Query replace regexp" t)))
351 (to (list (read-from-minibuffer
352 (format "Query replace regexp %s with eval: "
353 (query-replace-descr from))
354 nil nil t query-replace-to-history-variable from t))))
355 ;; We make TO a list because replace-match-string-symbols requires one,
356 ;; and the user might enter a single token.
357 (replace-match-string-symbols to)
358 (list from (car to) current-prefix-arg
359 (if (and transient-mark-mode mark-active)
360 (region-beginning))
361 (if (and transient-mark-mode mark-active)
362 (region-end))))))
363 (perform-replace regexp (cons 'replace-eval-replacement to-expr)
364 t 'literal delimited nil nil start end))
365
366 (make-obsolete 'query-replace-regexp-eval
367 "for interactive use, use the special `\\,' feature of
368 `query-replace-regexp' instead. Non-interactively, a loop
369 using `search-forward-regexp' and `replace-match' is preferred." "22.1")
370
371 (defun map-query-replace-regexp (regexp to-strings &optional n start end)
372 "Replace some matches for REGEXP with various strings, in rotation.
373 The second argument TO-STRINGS contains the replacement strings, separated
374 by spaces. This command works like `query-replace-regexp' except that
375 each successive replacement uses the next successive replacement string,
376 wrapping around from the last such string to the first.
377
378 In Transient Mark mode, if the mark is active, operate on the contents
379 of the region. Otherwise, operate from point to the end of the buffer.
380
381 Non-interactively, TO-STRINGS may be a list of replacement strings.
382
383 If `query-replace-interactive' is non-nil, the last incremental search
384 regexp is used as REGEXP--you don't have to specify it with the minibuffer.
385
386 A prefix argument N says to use each replacement string N times
387 before rotating to the next.
388 Fourth and fifth arg START and END specify the region to operate on."
389 (interactive
390 (let* ((from (if query-replace-interactive
391 (car regexp-search-ring)
392 (read-from-minibuffer "Map query replace (regexp): "
393 nil nil nil
394 'query-replace-history nil t)))
395 (to (read-from-minibuffer
396 (format "Query replace %s with (space-separated strings): "
397 (query-replace-descr from))
398 nil nil nil
399 'query-replace-history from t)))
400 (list from to
401 (and current-prefix-arg
402 (prefix-numeric-value current-prefix-arg))
403 (if (and transient-mark-mode mark-active)
404 (region-beginning))
405 (if (and transient-mark-mode mark-active)
406 (region-end)))))
407 (let (replacements)
408 (if (listp to-strings)
409 (setq replacements to-strings)
410 (while (/= (length to-strings) 0)
411 (if (string-match " " to-strings)
412 (setq replacements
413 (append replacements
414 (list (substring to-strings 0
415 (string-match " " to-strings))))
416 to-strings (substring to-strings
417 (1+ (string-match " " to-strings))))
418 (setq replacements (append replacements (list to-strings))
419 to-strings ""))))
420 (perform-replace regexp replacements t t nil n nil start end)))
421
422 (defun replace-string (from-string to-string &optional delimited start end)
423 "Replace occurrences of FROM-STRING with TO-STRING.
424 Preserve case in each match if `case-replace' and `case-fold-search'
425 are non-nil and FROM-STRING has no uppercase letters.
426 \(Preserving case means that if the string matched is all caps, or capitalized,
427 then its replacement is upcased or capitalized.)
428
429 In Transient Mark mode, if the mark is active, operate on the contents
430 of the region. Otherwise, operate from point to the end of the buffer.
431
432 Third arg DELIMITED (prefix arg if interactive), if non-nil, means replace
433 only matches surrounded by word boundaries.
434 Fourth and fifth arg START and END specify the region to operate on.
435
436 If `query-replace-interactive' is non-nil, the last incremental search
437 string is used as FROM-STRING--you don't have to specify it with the
438 minibuffer.
439
440 This function is usually the wrong thing to use in a Lisp program.
441 What you probably want is a loop like this:
442 (while (search-forward FROM-STRING nil t)
443 (replace-match TO-STRING nil t))
444 which will run faster and will not set the mark or print anything.
445 \(You may need a more complex loop if FROM-STRING can match the null string
446 and TO-STRING is also null.)"
447 (interactive
448 (let ((common
449 (query-replace-read-args
450 (if (and transient-mark-mode mark-active)
451 "Replace string in region"
452 "Replace string")
453 nil)))
454 (list (nth 0 common) (nth 1 common) (nth 2 common)
455 (if (and transient-mark-mode mark-active)
456 (region-beginning))
457 (if (and transient-mark-mode mark-active)
458 (region-end)))))
459 (perform-replace from-string to-string nil nil delimited nil nil start end))
460
461 (defun replace-regexp (regexp to-string &optional delimited start end)
462 "Replace things after point matching REGEXP with TO-STRING.
463 Preserve case in each match if `case-replace' and `case-fold-search'
464 are non-nil and REGEXP has no uppercase letters.
465
466 In Transient Mark mode, if the mark is active, operate on the contents
467 of the region. Otherwise, operate from point to the end of the buffer.
468
469 Third arg DELIMITED (prefix arg if interactive), if non-nil, means replace
470 only matches surrounded by word boundaries.
471 Fourth and fifth arg START and END specify the region to operate on.
472
473 In TO-STRING, `\\&' stands for whatever matched the whole of REGEXP,
474 and `\\=\\N' (where N is a digit) stands for
475 whatever what matched the Nth `\\(...\\)' in REGEXP.
476 `\\?' lets you edit the replacement text in the minibuffer
477 at the given position for each replacement.
478
479 In interactive calls, the replacement text may contain `\\,'
480 followed by a Lisp expression used as part of the replacement
481 text. Inside of that expression, `\\&' is a string denoting the
482 whole match, `\\N' a partial match, `\\#&' and `\\#N' the respective
483 numeric values from `string-to-number', and `\\#' itself for
484 `replace-count', the number of replacements occurred so far.
485
486 If your Lisp expression is an identifier and the next letter in
487 the replacement string would be interpreted as part of it, you
488 can wrap it with an expression like `\\,(or \\#)'. Incidentally,
489 for this particular case you may also enter `\\#' in the
490 replacement text directly.
491
492 When using those Lisp features interactively in the replacement
493 text, TO-STRING is actually made a list instead of a string.
494 Use \\[repeat-complex-command] after this command for details.
495
496 If `query-replace-interactive' is non-nil, the last incremental search
497 regexp is used as REGEXP--you don't have to specify it with the minibuffer.
498
499 This function is usually the wrong thing to use in a Lisp program.
500 What you probably want is a loop like this:
501 (while (re-search-forward REGEXP nil t)
502 (replace-match TO-STRING nil nil))
503 which will run faster and will not set the mark or print anything."
504 (interactive
505 (let ((common
506 (query-replace-read-args
507 (if (and transient-mark-mode mark-active)
508 "Replace regexp in region"
509 "Replace regexp")
510 t)))
511 (list (nth 0 common) (nth 1 common) (nth 2 common)
512 (if (and transient-mark-mode mark-active)
513 (region-beginning))
514 (if (and transient-mark-mode mark-active)
515 (region-end)))))
516 (perform-replace regexp to-string nil t delimited nil nil start end))
517
518 \f
519 (defvar regexp-history nil
520 "History list for some commands that read regular expressions.
521
522 Maximum length of the history list is determined by the value
523 of `history-length', which see.")
524
525
526 (defalias 'delete-non-matching-lines 'keep-lines)
527 (defalias 'delete-matching-lines 'flush-lines)
528 (defalias 'count-matches 'how-many)
529
530
531 (defun keep-lines-read-args (prompt)
532 "Read arguments for `keep-lines' and friends.
533 Prompt for a regexp with PROMPT.
534 Value is a list, (REGEXP)."
535 (let* ((default (list
536 (regexp-quote
537 (or (funcall (or find-tag-default-function
538 (get major-mode 'find-tag-default-function)
539 'find-tag-default))
540 ""))
541 (car regexp-search-ring)
542 (regexp-quote (or (car search-ring) ""))
543 (car (symbol-value
544 query-replace-from-history-variable))))
545 (default (delete-dups (delq nil (delete "" default)))))
546 (list (read-from-minibuffer prompt nil nil nil
547 'regexp-history default t)
548 nil nil t)))
549
550 (defun keep-lines (regexp &optional rstart rend interactive)
551 "Delete all lines except those containing matches for REGEXP.
552 A match split across lines preserves all the lines it lies in.
553 When called from Lisp (and usually interactively as well, see below)
554 applies to all lines starting after point.
555
556 If REGEXP contains upper case characters (excluding those preceded by `\\')
557 and `search-upper-case' is non-nil, the matching is case-sensitive.
558
559 Second and third arg RSTART and REND specify the region to operate on.
560 This command operates on (the accessible part of) all lines whose
561 accessible part is entirely contained in the region determined by RSTART
562 and REND. (A newline ending a line counts as part of that line.)
563
564 Interactively, in Transient Mark mode when the mark is active, operate
565 on all lines whose accessible part is entirely contained in the region.
566 Otherwise, the command applies to all lines starting after point.
567 When calling this function from Lisp, you can pretend that it was
568 called interactively by passing a non-nil INTERACTIVE argument.
569
570 This function starts looking for the next match from the end of
571 the previous match. Hence, it ignores matches that overlap
572 a previously found match."
573
574 (interactive
575 (progn
576 (barf-if-buffer-read-only)
577 (keep-lines-read-args "Keep lines (containing match for regexp): ")))
578 (if rstart
579 (progn
580 (goto-char (min rstart rend))
581 (setq rend
582 (progn
583 (save-excursion
584 (goto-char (max rstart rend))
585 (unless (or (bolp) (eobp))
586 (forward-line 0))
587 (point-marker)))))
588 (if (and interactive transient-mark-mode mark-active)
589 (setq rstart (region-beginning)
590 rend (progn
591 (goto-char (region-end))
592 (unless (or (bolp) (eobp))
593 (forward-line 0))
594 (point-marker)))
595 (setq rstart (point)
596 rend (point-max-marker)))
597 (goto-char rstart))
598 (save-excursion
599 (or (bolp) (forward-line 1))
600 (let ((start (point))
601 (case-fold-search
602 (if (and case-fold-search search-upper-case)
603 (isearch-no-upper-case-p regexp t)
604 case-fold-search)))
605 (while (< (point) rend)
606 ;; Start is first char not preserved by previous match.
607 (if (not (re-search-forward regexp rend 'move))
608 (delete-region start rend)
609 (let ((end (save-excursion (goto-char (match-beginning 0))
610 (forward-line 0)
611 (point))))
612 ;; Now end is first char preserved by the new match.
613 (if (< start end)
614 (delete-region start end))))
615
616 (setq start (save-excursion (forward-line 1) (point)))
617 ;; If the match was empty, avoid matching again at same place.
618 (and (< (point) rend)
619 (= (match-beginning 0) (match-end 0))
620 (forward-char 1)))))
621 (set-marker rend nil)
622 nil)
623
624
625 (defun flush-lines (regexp &optional rstart rend interactive)
626 "Delete lines containing matches for REGEXP.
627 When called from Lisp (and usually when called interactively as
628 well, see below), applies to the part of the buffer after point.
629 The line point is in is deleted if and only if it contains a
630 match for regexp starting after point.
631
632 If REGEXP contains upper case characters (excluding those preceded by `\\')
633 and `search-upper-case' is non-nil, the matching is case-sensitive.
634
635 Second and third arg RSTART and REND specify the region to operate on.
636 Lines partially contained in this region are deleted if and only if
637 they contain a match entirely contained in it.
638
639 Interactively, in Transient Mark mode when the mark is active, operate
640 on the contents of the region. Otherwise, operate from point to the
641 end of (the accessible portion of) the buffer. When calling this function
642 from Lisp, you can pretend that it was called interactively by passing
643 a non-nil INTERACTIVE argument.
644
645 If a match is split across lines, all the lines it lies in are deleted.
646 They are deleted _before_ looking for the next match. Hence, a match
647 starting on the same line at which another match ended is ignored."
648
649 (interactive
650 (progn
651 (barf-if-buffer-read-only)
652 (keep-lines-read-args "Flush lines (containing match for regexp): ")))
653 (if rstart
654 (progn
655 (goto-char (min rstart rend))
656 (setq rend (copy-marker (max rstart rend))))
657 (if (and interactive transient-mark-mode mark-active)
658 (setq rstart (region-beginning)
659 rend (copy-marker (region-end)))
660 (setq rstart (point)
661 rend (point-max-marker)))
662 (goto-char rstart))
663 (let ((case-fold-search
664 (if (and case-fold-search search-upper-case)
665 (isearch-no-upper-case-p regexp t)
666 case-fold-search)))
667 (save-excursion
668 (while (and (< (point) rend)
669 (re-search-forward regexp rend t))
670 (delete-region (save-excursion (goto-char (match-beginning 0))
671 (forward-line 0)
672 (point))
673 (progn (forward-line 1) (point))))))
674 (set-marker rend nil)
675 nil)
676
677
678 (defun how-many (regexp &optional rstart rend interactive)
679 "Print and return number of matches for REGEXP following point.
680 When called from Lisp and INTERACTIVE is omitted or nil, just return
681 the number, do not print it; if INTERACTIVE is t, the function behaves
682 in all respects has if it had been called interactively.
683
684 If REGEXP contains upper case characters (excluding those preceded by `\\')
685 and `search-upper-case' is non-nil, the matching is case-sensitive.
686
687 Second and third arg RSTART and REND specify the region to operate on.
688
689 Interactively, in Transient Mark mode when the mark is active, operate
690 on the contents of the region. Otherwise, operate from point to the
691 end of (the accessible portion of) the buffer.
692
693 This function starts looking for the next match from the end of
694 the previous match. Hence, it ignores matches that overlap
695 a previously found match."
696
697 (interactive
698 (keep-lines-read-args "How many matches for (regexp): "))
699 (save-excursion
700 (if rstart
701 (progn
702 (goto-char (min rstart rend))
703 (setq rend (max rstart rend)))
704 (if (and interactive transient-mark-mode mark-active)
705 (setq rstart (region-beginning)
706 rend (region-end))
707 (setq rstart (point)
708 rend (point-max)))
709 (goto-char rstart))
710 (let ((count 0)
711 opoint
712 (case-fold-search
713 (if (and case-fold-search search-upper-case)
714 (isearch-no-upper-case-p regexp t)
715 case-fold-search)))
716 (while (and (< (point) rend)
717 (progn (setq opoint (point))
718 (re-search-forward regexp rend t)))
719 (if (= opoint (point))
720 (forward-char 1)
721 (setq count (1+ count))))
722 (when interactive (message "%d occurrence%s"
723 count
724 (if (= count 1) "" "s")))
725 count)))
726
727 \f
728 (defvar occur-mode-map
729 (let ((map (make-sparse-keymap)))
730 ;; We use this alternative name, so we can use \\[occur-mode-mouse-goto].
731 (define-key map [mouse-2] 'occur-mode-mouse-goto)
732 (define-key map "\C-c\C-c" 'occur-mode-goto-occurrence)
733 (define-key map "\C-m" 'occur-mode-goto-occurrence)
734 (define-key map "o" 'occur-mode-goto-occurrence-other-window)
735 (define-key map "\C-o" 'occur-mode-display-occurrence)
736 (define-key map "\M-n" 'occur-next)
737 (define-key map "\M-p" 'occur-prev)
738 (define-key map "r" 'occur-rename-buffer)
739 (define-key map "c" 'clone-buffer)
740 (define-key map "g" 'revert-buffer)
741 (define-key map "q" 'quit-window)
742 (define-key map "z" 'kill-this-buffer)
743 (define-key map "\C-c\C-f" 'next-error-follow-minor-mode)
744 (define-key map [menu-bar] (make-sparse-keymap))
745 (define-key map [menu-bar occur]
746 (cons "Occur" map))
747 (define-key map [next-error-follow-minor-mode]
748 (menu-bar-make-mm-toggle next-error-follow-minor-mode
749 "Auto Occurrence Display"
750 "Display another occurrence when moving the cursor"))
751 (define-key map [separator-1] '("--"))
752 (define-key map [kill-this-buffer]
753 '(menu-item "Kill occur buffer" kill-this-buffer
754 :help "Kill the current *Occur* buffer"))
755 (define-key map [quit-window]
756 '(menu-item "Quit occur window" quit-window
757 :help "Quit the current *Occur* buffer. Bury it, and maybe delete the selected frame"))
758 (define-key map [revert-buffer]
759 '(menu-item "Revert occur buffer" revert-buffer
760 :help "Replace the text in the *Occur* buffer with the results of rerunning occur"))
761 (define-key map [clone-buffer]
762 '(menu-item "Clone occur buffer" clone-buffer
763 :help "Create and return a twin copy of the current *Occur* buffer"))
764 (define-key map [occur-rename-buffer]
765 '(menu-item "Rename occur buffer" occur-rename-buffer
766 :help "Rename the current *Occur* buffer to *Occur: original-buffer-name*."))
767 (define-key map [separator-2] '("--"))
768 (define-key map [occur-mode-goto-occurrence-other-window]
769 '(menu-item "Go To Occurrence Other Window" occur-mode-goto-occurrence-other-window
770 :help "Go to the occurrence the current line describes, in another window"))
771 (define-key map [occur-mode-goto-occurrence]
772 '(menu-item "Go To Occurrence" occur-mode-goto-occurrence
773 :help "Go to the occurrence the current line describes"))
774 (define-key map [occur-mode-display-occurrence]
775 '(menu-item "Display Occurrence" occur-mode-display-occurrence
776 :help "Display in another window the occurrence the current line describes"))
777 (define-key map [occur-next]
778 '(menu-item "Move to next match" occur-next
779 :help "Move to the Nth (default 1) next match in an Occur mode buffer"))
780 (define-key map [occur-prev]
781 '(menu-item "Move to previous match" occur-prev
782 :help "Move to the Nth (default 1) previous match in an Occur mode buffer"))
783 map)
784 "Keymap for `occur-mode'.")
785
786 (defvar occur-revert-arguments nil
787 "Arguments to pass to `occur-1' to revert an Occur mode buffer.
788 See `occur-revert-function'.")
789
790 (defcustom occur-mode-hook '(turn-on-font-lock)
791 "Hook run when entering Occur mode."
792 :type 'hook
793 :group 'matching)
794
795 (defcustom occur-hook nil
796 "Hook run by Occur when there are any matches."
797 :type 'hook
798 :group 'matching)
799
800 (defcustom occur-mode-find-occurrence-hook nil
801 "Hook run by Occur after locating an occurrence.
802 This will be called with the cursor position at the occurrence. An application
803 for this is to reveal context in an outline-mode when the occurrence is hidden."
804 :type 'hook
805 :group 'matching)
806
807 (put 'occur-mode 'mode-class 'special)
808 (defun occur-mode ()
809 "Major mode for output from \\[occur].
810 \\<occur-mode-map>Move point to one of the items in this buffer, then use
811 \\[occur-mode-goto-occurrence] to go to the occurrence that the item refers to.
812 Alternatively, click \\[occur-mode-mouse-goto] on an item to go to it.
813
814 \\{occur-mode-map}"
815 (interactive)
816 (kill-all-local-variables)
817 (use-local-map occur-mode-map)
818 (setq major-mode 'occur-mode)
819 (setq mode-name "Occur")
820 (set (make-local-variable 'revert-buffer-function) 'occur-revert-function)
821 (make-local-variable 'occur-revert-arguments)
822 (add-hook 'change-major-mode-hook 'font-lock-defontify nil t)
823 (setq next-error-function 'occur-next-error)
824 (run-mode-hooks 'occur-mode-hook))
825
826 (defun occur-revert-function (ignore1 ignore2)
827 "Handle `revert-buffer' for Occur mode buffers."
828 (apply 'occur-1 (append occur-revert-arguments (list (buffer-name)))))
829
830 (defun occur-mode-find-occurrence ()
831 (let ((pos (get-text-property (point) 'occur-target)))
832 (unless pos
833 (error "No occurrence on this line"))
834 (unless (buffer-live-p (marker-buffer pos))
835 (error "Buffer for this occurrence was killed"))
836 pos))
837
838 (defalias 'occur-mode-mouse-goto 'occur-mode-goto-occurrence)
839 (defun occur-mode-goto-occurrence (&optional event)
840 "Go to the occurrence the current line describes."
841 (interactive (list last-nonmenu-event))
842 (let ((pos
843 (if (null event)
844 ;; Actually `event-end' works correctly with a nil argument as
845 ;; well, so we could dispense with this test, but let's not
846 ;; rely on this undocumented behavior.
847 (occur-mode-find-occurrence)
848 (with-current-buffer (window-buffer (posn-window (event-end event)))
849 (save-excursion
850 (goto-char (posn-point (event-end event)))
851 (occur-mode-find-occurrence)))))
852 same-window-buffer-names
853 same-window-regexps)
854 (pop-to-buffer (marker-buffer pos))
855 (goto-char pos)
856 (run-hooks 'occur-mode-find-occurrence-hook)))
857
858 (defun occur-mode-goto-occurrence-other-window ()
859 "Go to the occurrence the current line describes, in another window."
860 (interactive)
861 (let ((pos (occur-mode-find-occurrence)))
862 (switch-to-buffer-other-window (marker-buffer pos))
863 (goto-char pos)
864 (run-hooks 'occur-mode-find-occurrence-hook)))
865
866 (defun occur-mode-display-occurrence ()
867 "Display in another window the occurrence the current line describes."
868 (interactive)
869 (let ((pos (occur-mode-find-occurrence))
870 window
871 ;; Bind these to ensure `display-buffer' puts it in another window.
872 same-window-buffer-names
873 same-window-regexps)
874 (setq window (display-buffer (marker-buffer pos)))
875 ;; This is the way to set point in the proper window.
876 (save-selected-window
877 (select-window window)
878 (goto-char pos)
879 (run-hooks 'occur-mode-find-occurrence-hook))))
880
881 (defun occur-find-match (n search message)
882 (if (not n) (setq n 1))
883 (let ((r))
884 (while (> n 0)
885 (setq r (funcall search (point) 'occur-match))
886 (and r
887 (get-text-property r 'occur-match)
888 (setq r (funcall search r 'occur-match)))
889 (if r
890 (goto-char r)
891 (error message))
892 (setq n (1- n)))))
893
894 (defun occur-next (&optional n)
895 "Move to the Nth (default 1) next match in an Occur mode buffer."
896 (interactive "p")
897 (occur-find-match n #'next-single-property-change "No more matches"))
898
899 (defun occur-prev (&optional n)
900 "Move to the Nth (default 1) previous match in an Occur mode buffer."
901 (interactive "p")
902 (occur-find-match n #'previous-single-property-change "No earlier matches"))
903
904 (defun occur-next-error (&optional argp reset)
905 "Move to the Nth (default 1) next match in an Occur mode buffer.
906 Compatibility function for \\[next-error] invocations."
907 (interactive "p")
908 ;; we need to run occur-find-match from within the Occur buffer
909 (with-current-buffer
910 ;; Choose the buffer and make it current.
911 (if (next-error-buffer-p (current-buffer))
912 (current-buffer)
913 (next-error-find-buffer nil nil
914 (lambda ()
915 (eq major-mode 'occur-mode))))
916
917 (goto-char (cond (reset (point-min))
918 ((< argp 0) (line-beginning-position))
919 ((> argp 0) (line-end-position))
920 ((point))))
921 (occur-find-match
922 (abs argp)
923 (if (> 0 argp)
924 #'previous-single-property-change
925 #'next-single-property-change)
926 "No more matches")
927 ;; In case the *Occur* buffer is visible in a nonselected window.
928 (let ((win (get-buffer-window (current-buffer) t)))
929 (if win (set-window-point win (point))))
930 (occur-mode-goto-occurrence)))
931 \f
932 (defface match
933 '((((class color) (min-colors 88) (background light))
934 :background "yellow1")
935 (((class color) (min-colors 88) (background dark))
936 :background "RoyalBlue3")
937 (((class color) (min-colors 8) (background light))
938 :background "yellow" :foreground "black")
939 (((class color) (min-colors 8) (background dark))
940 :background "blue" :foreground "white")
941 (((type tty) (class mono))
942 :inverse-video t)
943 (t :background "gray"))
944 "Face used to highlight matches permanently."
945 :group 'matching
946 :version "22.1")
947
948 (defcustom list-matching-lines-default-context-lines 0
949 "*Default number of context lines included around `list-matching-lines' matches.
950 A negative number means to include that many lines before the match.
951 A positive number means to include that many lines both before and after."
952 :type 'integer
953 :group 'matching)
954
955 (defalias 'list-matching-lines 'occur)
956
957 (defcustom list-matching-lines-face 'match
958 "*Face used by \\[list-matching-lines] to show the text that matches.
959 If the value is nil, don't highlight the matching portions specially."
960 :type 'face
961 :group 'matching)
962
963 (defcustom list-matching-lines-buffer-name-face 'underline
964 "*Face used by \\[list-matching-lines] to show the names of buffers.
965 If the value is nil, don't highlight the buffer names specially."
966 :type 'face
967 :group 'matching)
968
969 (defcustom occur-excluded-properties
970 '(read-only invisible intangible field mouse-face help-echo local-map keymap
971 yank-handler follow-link)
972 "*Text properties to discard when copying lines to the *Occur* buffer.
973 The value should be a list of text properties to discard or t,
974 which means to discard all text properties."
975 :type '(choice (const :tag "All" t) (repeat symbol))
976 :group 'matching
977 :version "22.1")
978
979 (defun occur-accumulate-lines (count &optional keep-props)
980 (save-excursion
981 (let ((forwardp (> count 0))
982 result beg end)
983 (while (not (or (zerop count)
984 (if forwardp
985 (eobp)
986 (bobp))))
987 (setq count (+ count (if forwardp -1 1)))
988 (setq beg (line-beginning-position)
989 end (line-end-position))
990 (if (and keep-props (if (boundp 'jit-lock-mode) jit-lock-mode)
991 (text-property-not-all beg end 'fontified t))
992 (if (fboundp 'jit-lock-fontify-now)
993 (jit-lock-fontify-now beg end)))
994 (push
995 (if (and keep-props (not (eq occur-excluded-properties t)))
996 (let ((str (buffer-substring beg end)))
997 (remove-list-of-text-properties
998 0 (length str) occur-excluded-properties str)
999 str)
1000 (buffer-substring-no-properties beg end))
1001 result)
1002 (forward-line (if forwardp 1 -1)))
1003 (nreverse result))))
1004
1005 (defun occur-read-primary-args ()
1006 (let* ((default (car regexp-history))
1007 (defaults
1008 (list (and transient-mark-mode mark-active
1009 (regexp-quote
1010 (buffer-substring-no-properties
1011 (region-beginning) (region-end))))
1012 (regexp-quote
1013 (or (funcall
1014 (or find-tag-default-function
1015 (get major-mode 'find-tag-default-function)
1016 'find-tag-default))
1017 ""))
1018 (car regexp-search-ring)
1019 (regexp-quote (or (car search-ring) ""))
1020 (car (symbol-value
1021 query-replace-from-history-variable))))
1022 (defaults (delete-dups (delq nil (delete "" defaults))))
1023 ;; Don't add automatically the car of defaults for empty input
1024 (history-add-new-input nil)
1025 (input
1026 (read-from-minibuffer
1027 (if default
1028 (format "List lines matching regexp (default %s): "
1029 (query-replace-descr default))
1030 "List lines matching regexp: ")
1031 nil nil nil 'regexp-history defaults)))
1032 (list (if (equal input "")
1033 default
1034 (prog1 input
1035 (add-to-history 'regexp-history input)))
1036 (when current-prefix-arg
1037 (prefix-numeric-value current-prefix-arg)))))
1038
1039 (defun occur-rename-buffer (&optional unique-p interactive-p)
1040 "Rename the current *Occur* buffer to *Occur: original-buffer-name*.
1041 Here `original-buffer-name' is the buffer name were Occur was originally run.
1042 When given the prefix argument, or called non-interactively, the renaming
1043 will not clobber the existing buffer(s) of that name, but use
1044 `generate-new-buffer-name' instead. You can add this to `occur-hook'
1045 if you always want a separate *Occur* buffer for each buffer where you
1046 invoke `occur'."
1047 (interactive "P\np")
1048 (with-current-buffer
1049 (if (eq major-mode 'occur-mode) (current-buffer) (get-buffer "*Occur*"))
1050 (rename-buffer (concat "*Occur: "
1051 (mapconcat #'buffer-name
1052 (car (cddr occur-revert-arguments)) "/")
1053 "*")
1054 (or unique-p (not interactive-p)))))
1055
1056 (defun occur (regexp &optional nlines)
1057 "Show all lines in the current buffer containing a match for REGEXP.
1058 This function can not handle matches that span more than one line.
1059
1060 Each line is displayed with NLINES lines before and after, or -NLINES
1061 before if NLINES is negative.
1062 NLINES defaults to `list-matching-lines-default-context-lines'.
1063 Interactively it is the prefix arg.
1064
1065 The lines are shown in a buffer named `*Occur*'.
1066 It serves as a menu to find any of the occurrences in this buffer.
1067 \\<occur-mode-map>\\[describe-mode] in that buffer will explain how.
1068
1069 If REGEXP contains upper case characters (excluding those preceded by `\\')
1070 and `search-upper-case' is non-nil, the matching is case-sensitive."
1071 (interactive (occur-read-primary-args))
1072 (occur-1 regexp nlines (list (current-buffer))))
1073
1074 (defun multi-occur (bufs regexp &optional nlines)
1075 "Show all lines in buffers BUFS containing a match for REGEXP.
1076 This function acts on multiple buffers; otherwise, it is exactly like
1077 `occur'. When you invoke this command interactively, you must specify
1078 the buffer names that you want, one by one."
1079 (interactive
1080 (cons
1081 (let* ((bufs (list (read-buffer "First buffer to search: "
1082 (current-buffer) t)))
1083 (buf nil)
1084 (ido-ignore-item-temp-list bufs))
1085 (while (not (string-equal
1086 (setq buf (read-buffer
1087 (if (eq read-buffer-function 'ido-read-buffer)
1088 "Next buffer to search (C-j to end): "
1089 "Next buffer to search (RET to end): ")
1090 nil t))
1091 ""))
1092 (add-to-list 'bufs buf)
1093 (setq ido-ignore-item-temp-list bufs))
1094 (nreverse (mapcar #'get-buffer bufs)))
1095 (occur-read-primary-args)))
1096 (occur-1 regexp nlines bufs))
1097
1098 (defun multi-occur-in-matching-buffers (bufregexp regexp &optional allbufs)
1099 "Show all lines matching REGEXP in buffers specified by BUFREGEXP.
1100 Normally BUFREGEXP matches against each buffer's visited file name,
1101 but if you specify a prefix argument, it matches against the buffer name.
1102 See also `multi-occur'."
1103 (interactive
1104 (cons
1105 (let* ((default (car regexp-history))
1106 (input
1107 (read-from-minibuffer
1108 (if current-prefix-arg
1109 "List lines in buffers whose names match regexp: "
1110 "List lines in buffers whose filenames match regexp: ")
1111 nil
1112 nil
1113 nil
1114 'regexp-history)))
1115 (if (equal input "")
1116 default
1117 input))
1118 (occur-read-primary-args)))
1119 (when bufregexp
1120 (occur-1 regexp nil
1121 (delq nil
1122 (mapcar (lambda (buf)
1123 (when (if allbufs
1124 (string-match bufregexp
1125 (buffer-name buf))
1126 (and (buffer-file-name buf)
1127 (string-match bufregexp
1128 (buffer-file-name buf))))
1129 buf))
1130 (buffer-list))))))
1131
1132 (defun occur-1 (regexp nlines bufs &optional buf-name)
1133 (unless (and regexp (not (equal regexp "")))
1134 (error "Occur doesn't work with the empty regexp"))
1135 (unless buf-name
1136 (setq buf-name "*Occur*"))
1137 (let (occur-buf
1138 (active-bufs (delq nil (mapcar #'(lambda (buf)
1139 (when (buffer-live-p buf) buf))
1140 bufs))))
1141 ;; Handle the case where one of the buffers we're searching is the
1142 ;; output buffer. Just rename it.
1143 (when (member buf-name (mapcar 'buffer-name active-bufs))
1144 (with-current-buffer (get-buffer buf-name)
1145 (rename-uniquely)))
1146
1147 ;; Now find or create the output buffer.
1148 ;; If we just renamed that buffer, we will make a new one here.
1149 (setq occur-buf (get-buffer-create buf-name))
1150
1151 (with-current-buffer occur-buf
1152 (occur-mode)
1153 (let ((inhibit-read-only t)
1154 ;; Don't generate undo entries for creation of the initial contents.
1155 (buffer-undo-list t))
1156 (erase-buffer)
1157 (let ((count (occur-engine
1158 regexp active-bufs occur-buf
1159 (or nlines list-matching-lines-default-context-lines)
1160 (if (and case-fold-search search-upper-case)
1161 (isearch-no-upper-case-p regexp t)
1162 case-fold-search)
1163 list-matching-lines-buffer-name-face
1164 nil list-matching-lines-face
1165 (not (eq occur-excluded-properties t)))))
1166 (let* ((bufcount (length active-bufs))
1167 (diff (- (length bufs) bufcount)))
1168 (message "Searched %d buffer%s%s; %s match%s for `%s'"
1169 bufcount (if (= bufcount 1) "" "s")
1170 (if (zerop diff) "" (format " (%d killed)" diff))
1171 (if (zerop count) "no" (format "%d" count))
1172 (if (= count 1) "" "es")
1173 regexp))
1174 (setq occur-revert-arguments (list regexp nlines bufs))
1175 (if (= count 0)
1176 (kill-buffer occur-buf)
1177 (display-buffer occur-buf)
1178 (setq next-error-last-buffer occur-buf)
1179 (setq buffer-read-only t)
1180 (set-buffer-modified-p nil)
1181 (run-hooks 'occur-hook)))))))
1182
1183 (defun occur-engine-add-prefix (lines)
1184 (mapcar
1185 #'(lambda (line)
1186 (concat " :" line "\n"))
1187 lines))
1188
1189 (defun occur-engine (regexp buffers out-buf nlines case-fold-search
1190 title-face prefix-face match-face keep-props)
1191 (with-current-buffer out-buf
1192 (let ((globalcount 0)
1193 (coding nil))
1194 ;; Map over all the buffers
1195 (dolist (buf buffers)
1196 (when (buffer-live-p buf)
1197 (let ((matches 0) ;; count of matched lines
1198 (lines 1) ;; line count
1199 (matchbeg 0)
1200 (origpt nil)
1201 (begpt nil)
1202 (endpt nil)
1203 (marker nil)
1204 (curstring "")
1205 (inhibit-field-text-motion t)
1206 (headerpt (with-current-buffer out-buf (point))))
1207 (with-current-buffer buf
1208 (or coding
1209 ;; Set CODING only if the current buffer locally
1210 ;; binds buffer-file-coding-system.
1211 (not (local-variable-p 'buffer-file-coding-system))
1212 (setq coding buffer-file-coding-system))
1213 (save-excursion
1214 (goto-char (point-min)) ;; begin searching in the buffer
1215 (while (not (eobp))
1216 (setq origpt (point))
1217 (when (setq endpt (re-search-forward regexp nil t))
1218 (setq matches (1+ matches)) ;; increment match count
1219 (setq matchbeg (match-beginning 0))
1220 (setq lines (+ lines (1- (count-lines origpt endpt))))
1221 (save-excursion
1222 (goto-char matchbeg)
1223 (setq begpt (line-beginning-position)
1224 endpt (line-end-position)))
1225 (setq marker (make-marker))
1226 (set-marker marker matchbeg)
1227 (if (and keep-props
1228 (if (boundp 'jit-lock-mode) jit-lock-mode)
1229 (text-property-not-all begpt endpt 'fontified t))
1230 (if (fboundp 'jit-lock-fontify-now)
1231 (jit-lock-fontify-now begpt endpt)))
1232 (if (and keep-props (not (eq occur-excluded-properties t)))
1233 (progn
1234 (setq curstring (buffer-substring begpt endpt))
1235 (remove-list-of-text-properties
1236 0 (length curstring) occur-excluded-properties curstring))
1237 (setq curstring (buffer-substring-no-properties begpt endpt)))
1238 ;; Highlight the matches
1239 (let ((len (length curstring))
1240 (start 0))
1241 (while (and (< start len)
1242 (string-match regexp curstring start))
1243 (add-text-properties
1244 (match-beginning 0) (match-end 0)
1245 (append
1246 `(occur-match t)
1247 (when match-face
1248 ;; Use `face' rather than `font-lock-face' here
1249 ;; so as to override faces copied from the buffer.
1250 `(face ,match-face)))
1251 curstring)
1252 (setq start (match-end 0))))
1253 ;; Generate the string to insert for this match
1254 (let* ((out-line
1255 (concat
1256 ;; Using 7 digits aligns tabs properly.
1257 (apply #'propertize (format "%7d:" lines)
1258 (append
1259 (when prefix-face
1260 `(font-lock-face prefix-face))
1261 `(occur-prefix t mouse-face (highlight)
1262 occur-target ,marker follow-link t
1263 help-echo "mouse-2: go to this occurrence")))
1264 ;; We don't put `mouse-face' on the newline,
1265 ;; because that loses. And don't put it
1266 ;; on context lines to reduce flicker.
1267 (propertize curstring 'mouse-face (list 'highlight)
1268 'occur-target marker
1269 'follow-link t
1270 'help-echo
1271 "mouse-2: go to this occurrence")
1272 ;; Add marker at eol, but no mouse props.
1273 (propertize "\n" 'occur-target marker)))
1274 (data
1275 (if (= nlines 0)
1276 ;; The simple display style
1277 out-line
1278 ;; The complex multi-line display style.
1279 (occur-context-lines out-line nlines keep-props)
1280 )))
1281 ;; Actually insert the match display data
1282 (with-current-buffer out-buf
1283 (let ((beg (point))
1284 (end (progn (insert data) (point))))
1285 (unless (= nlines 0)
1286 (insert "-------\n")))))
1287 (goto-char endpt))
1288 (if endpt
1289 (progn
1290 (setq lines (1+ lines))
1291 ;; On to the next match...
1292 (forward-line 1))
1293 (goto-char (point-max))))))
1294 (when (not (zerop matches)) ;; is the count zero?
1295 (setq globalcount (+ globalcount matches))
1296 (with-current-buffer out-buf
1297 (goto-char headerpt)
1298 (let ((beg (point))
1299 end)
1300 (insert (format "%d match%s for \"%s\" in buffer: %s\n"
1301 matches (if (= matches 1) "" "es")
1302 regexp (buffer-name buf)))
1303 (setq end (point))
1304 (add-text-properties beg end
1305 (append
1306 (when title-face
1307 `(font-lock-face ,title-face))
1308 `(occur-title ,buf))))
1309 (goto-char (point-min)))))))
1310 (if coding
1311 ;; CODING is buffer-file-coding-system of the first buffer
1312 ;; that locally binds it. Let's use it also for the output
1313 ;; buffer.
1314 (set-buffer-file-coding-system coding))
1315 ;; Return the number of matches
1316 globalcount)))
1317
1318 ;; Generate context display for occur.
1319 ;; OUT-LINE is the line where the match is.
1320 ;; NLINES and KEEP-PROPS are args to occur-engine.
1321 ;; Generate a list of lines, add prefixes to all but OUT-LINE,
1322 ;; then concatenate them all together.
1323 (defun occur-context-lines (out-line nlines keep-props)
1324 (apply #'concat
1325 (nconc
1326 (occur-engine-add-prefix
1327 (nreverse (cdr (occur-accumulate-lines
1328 (- (1+ (abs nlines))) keep-props))))
1329 (list out-line)
1330 (if (> nlines 0)
1331 (occur-engine-add-prefix
1332 (cdr (occur-accumulate-lines (1+ nlines) keep-props)))))))
1333 \f
1334 ;; It would be nice to use \\[...], but there is no reasonable way
1335 ;; to make that display both SPC and Y.
1336 (defconst query-replace-help
1337 "Type Space or `y' to replace one match, Delete or `n' to skip to next,
1338 RET or `q' to exit, Period to replace one match and exit,
1339 Comma to replace but not move point immediately,
1340 C-r to enter recursive edit (\\[exit-recursive-edit] to get out again),
1341 C-w to delete match and recursive edit,
1342 C-l to clear the screen, redisplay, and offer same replacement again,
1343 ! to replace all remaining matches with no more questions,
1344 ^ to move point back to previous match,
1345 E to edit the replacement string"
1346 "Help message while in `query-replace'.")
1347
1348 (defvar query-replace-map
1349 (let ((map (make-sparse-keymap)))
1350 (define-key map " " 'act)
1351 (define-key map "\d" 'skip)
1352 (define-key map [delete] 'skip)
1353 (define-key map [backspace] 'skip)
1354 (define-key map "y" 'act)
1355 (define-key map "n" 'skip)
1356 (define-key map "Y" 'act)
1357 (define-key map "N" 'skip)
1358 (define-key map "e" 'edit-replacement)
1359 (define-key map "E" 'edit-replacement)
1360 (define-key map "," 'act-and-show)
1361 (define-key map "q" 'exit)
1362 (define-key map "\r" 'exit)
1363 (define-key map [return] 'exit)
1364 (define-key map "." 'act-and-exit)
1365 (define-key map "\C-r" 'edit)
1366 (define-key map "\C-w" 'delete-and-edit)
1367 (define-key map "\C-l" 'recenter)
1368 (define-key map "!" 'automatic)
1369 (define-key map "^" 'backup)
1370 (define-key map "\C-h" 'help)
1371 (define-key map [f1] 'help)
1372 (define-key map [help] 'help)
1373 (define-key map "?" 'help)
1374 (define-key map "\C-g" 'quit)
1375 (define-key map "\C-]" 'quit)
1376 (define-key map "\e" 'exit-prefix)
1377 (define-key map [escape] 'exit-prefix)
1378 map)
1379 "Keymap that defines the responses to questions in `query-replace'.
1380 The \"bindings\" in this map are not commands; they are answers.
1381 The valid answers include `act', `skip', `act-and-show',
1382 `exit', `act-and-exit', `edit', `delete-and-edit', `recenter',
1383 `automatic', `backup', `exit-prefix', and `help'.")
1384
1385 (defun replace-match-string-symbols (n)
1386 "Process a list (and any sub-lists), expanding certain symbols.
1387 Symbol Expands To
1388 N (match-string N) (where N is a string of digits)
1389 #N (string-to-number (match-string N))
1390 & (match-string 0)
1391 #& (string-to-number (match-string 0))
1392 # replace-count
1393
1394 Note that these symbols must be preceeded by a backslash in order to
1395 type them using Lisp syntax."
1396 (while (consp n)
1397 (cond
1398 ((consp (car n))
1399 (replace-match-string-symbols (car n))) ;Process sub-list
1400 ((symbolp (car n))
1401 (let ((name (symbol-name (car n))))
1402 (cond
1403 ((string-match "^[0-9]+$" name)
1404 (setcar n (list 'match-string (string-to-number name))))
1405 ((string-match "^#[0-9]+$" name)
1406 (setcar n (list 'string-to-number
1407 (list 'match-string
1408 (string-to-number (substring name 1))))))
1409 ((string= "&" name)
1410 (setcar n '(match-string 0)))
1411 ((string= "#&" name)
1412 (setcar n '(string-to-number (match-string 0))))
1413 ((string= "#" name)
1414 (setcar n 'replace-count))))))
1415 (setq n (cdr n))))
1416
1417 (defun replace-eval-replacement (expression replace-count)
1418 (let ((replacement (eval expression)))
1419 (if (stringp replacement)
1420 replacement
1421 (prin1-to-string replacement t))))
1422
1423 (defun replace-quote (replacement)
1424 "Quote a replacement string.
1425 This just doubles all backslashes in REPLACEMENT and
1426 returns the resulting string. If REPLACEMENT is not
1427 a string, it is first passed through `prin1-to-string'
1428 with the `noescape' argument set.
1429
1430 `match-data' is preserved across the call."
1431 (save-match-data
1432 (replace-regexp-in-string "\\\\" "\\\\"
1433 (if (stringp replacement)
1434 replacement
1435 (prin1-to-string replacement t))
1436 t t)))
1437
1438 (defun replace-loop-through-replacements (data replace-count)
1439 ;; DATA is a vector contaning the following values:
1440 ;; 0 next-rotate-count
1441 ;; 1 repeat-count
1442 ;; 2 next-replacement
1443 ;; 3 replacements
1444 (if (= (aref data 0) replace-count)
1445 (progn
1446 (aset data 0 (+ replace-count (aref data 1)))
1447 (let ((next (cdr (aref data 2))))
1448 (aset data 2 (if (consp next) next (aref data 3))))))
1449 (car (aref data 2)))
1450
1451 (defun replace-match-data (integers reuse &optional new)
1452 "Like `match-data', but markers in REUSE get invalidated.
1453 If NEW is non-nil, it is set and returned instead of fresh data,
1454 but coerced to the correct value of INTEGERS."
1455 (or (and new
1456 (progn
1457 (set-match-data new)
1458 (and (eq new reuse)
1459 (eq (null integers) (markerp (car reuse)))
1460 new)))
1461 (match-data integers reuse t)))
1462
1463 (defun replace-match-maybe-edit (newtext fixedcase literal noedit match-data)
1464 "Make a replacement with `replace-match', editing `\\?'.
1465 NEWTEXT, FIXEDCASE, LITERAL are just passed on. If NOEDIT is true, no
1466 check for `\\?' is made to save time. MATCH-DATA is used for the
1467 replacement. In case editing is done, it is changed to use markers.
1468
1469 The return value is non-nil if there has been no `\\?' or NOEDIT was
1470 passed in. If LITERAL is set, no checking is done, anyway."
1471 (unless (or literal noedit)
1472 (setq noedit t)
1473 (while (string-match "\\(\\`\\|[^\\]\\)\\(\\\\\\\\\\)*\\(\\\\\\?\\)"
1474 newtext)
1475 (setq newtext
1476 (read-string "Edit replacement string: "
1477 (prog1
1478 (cons
1479 (replace-match "" t t newtext 3)
1480 (1+ (match-beginning 3)))
1481 (setq match-data
1482 (replace-match-data
1483 nil match-data match-data))))
1484 noedit nil)))
1485 (set-match-data match-data)
1486 (replace-match newtext fixedcase literal)
1487 noedit)
1488
1489 (defvar replace-search-function 'search-forward
1490 "Function to use when searching for strings to replace.
1491 It is used by `query-replace' and `replace-string', and is called
1492 with three arguments, as if it were `search-forward'.")
1493
1494 (defvar replace-re-search-function 're-search-forward
1495 "Function to use when searching for regexps to replace.
1496 It is used by `query-replace-regexp', `replace-regexp',
1497 `query-replace-regexp-eval', and `map-query-replace-regexp'. It
1498 is called with three arguments, as if it were `search-forward'.")
1499
1500 (defun perform-replace (from-string replacements
1501 query-flag regexp-flag delimited-flag
1502 &optional repeat-count map start end)
1503 "Subroutine of `query-replace'. Its complexity handles interactive queries.
1504 Don't use this in your own program unless you want to query and set the mark
1505 just as `query-replace' does. Instead, write a simple loop like this:
1506
1507 (while (re-search-forward \"foo[ \\t]+bar\" nil t)
1508 (replace-match \"foobar\" nil nil))
1509
1510 which will run faster and probably do exactly what you want. Please
1511 see the documentation of `replace-match' to find out how to simulate
1512 `case-replace'.
1513
1514 This function returns nil if and only if there were no matches to
1515 make, or the user didn't cancel the call."
1516 (or map (setq map query-replace-map))
1517 (and query-flag minibuffer-auto-raise
1518 (raise-frame (window-frame (minibuffer-window))))
1519 (let* ((case-fold-search
1520 (if (and case-fold-search search-upper-case)
1521 (isearch-no-upper-case-p from-string regexp-flag)
1522 case-fold-search))
1523 (nocasify (not (and case-replace case-fold-search)))
1524 (literal (or (not regexp-flag) (eq regexp-flag 'literal)))
1525 (search-function
1526 (if regexp-flag
1527 replace-re-search-function
1528 replace-search-function))
1529 (search-string from-string)
1530 (real-match-data nil) ; The match data for the current match.
1531 (next-replacement nil)
1532 ;; This is non-nil if we know there is nothing for the user
1533 ;; to edit in the replacement.
1534 (noedit nil)
1535 (keep-going t)
1536 (stack nil)
1537 (replace-count 0)
1538 (nonempty-match nil)
1539
1540 ;; If non-nil, it is marker saying where in the buffer to stop.
1541 (limit nil)
1542
1543 ;; Data for the next match. If a cons, it has the same format as
1544 ;; (match-data); otherwise it is t if a match is possible at point.
1545 (match-again t)
1546
1547 (message
1548 (if query-flag
1549 (apply 'propertize
1550 (substitute-command-keys
1551 "Query replacing %s with %s: (\\<query-replace-map>\\[help] for help) ")
1552 minibuffer-prompt-properties))))
1553
1554 ;; If region is active, in Transient Mark mode, operate on region.
1555 (when start
1556 (setq limit (copy-marker (max start end)))
1557 (goto-char (min start end))
1558 (deactivate-mark))
1559
1560 ;; REPLACEMENTS is either a string, a list of strings, or a cons cell
1561 ;; containing a function and its first argument. The function is
1562 ;; called to generate each replacement like this:
1563 ;; (funcall (car replacements) (cdr replacements) replace-count)
1564 ;; It must return a string.
1565 (cond
1566 ((stringp replacements)
1567 (setq next-replacement replacements
1568 replacements nil))
1569 ((stringp (car replacements)) ; If it isn't a string, it must be a cons
1570 (or repeat-count (setq repeat-count 1))
1571 (setq replacements (cons 'replace-loop-through-replacements
1572 (vector repeat-count repeat-count
1573 replacements replacements)))))
1574
1575 (if delimited-flag
1576 (setq search-function 're-search-forward
1577 search-string (concat "\\b"
1578 (if regexp-flag from-string
1579 (regexp-quote from-string))
1580 "\\b")))
1581 (when query-replace-lazy-highlight
1582 (setq isearch-lazy-highlight-last-string nil))
1583
1584 (push-mark)
1585 (undo-boundary)
1586 (unwind-protect
1587 ;; Loop finding occurrences that perhaps should be replaced.
1588 (while (and keep-going
1589 (not (or (eobp) (and limit (>= (point) limit))))
1590 ;; Use the next match if it is already known;
1591 ;; otherwise, search for a match after moving forward
1592 ;; one char if progress is required.
1593 (setq real-match-data
1594 (cond ((consp match-again)
1595 (goto-char (nth 1 match-again))
1596 (replace-match-data
1597 t real-match-data match-again))
1598 ;; MATCH-AGAIN non-nil means accept an
1599 ;; adjacent match.
1600 (match-again
1601 (and
1602 (funcall search-function search-string
1603 limit t)
1604 ;; For speed, use only integers and
1605 ;; reuse the list used last time.
1606 (replace-match-data t real-match-data)))
1607 ((and (< (1+ (point)) (point-max))
1608 (or (null limit)
1609 (< (1+ (point)) limit)))
1610 ;; If not accepting adjacent matches,
1611 ;; move one char to the right before
1612 ;; searching again. Undo the motion
1613 ;; if the search fails.
1614 (let ((opoint (point)))
1615 (forward-char 1)
1616 (if (funcall
1617 search-function search-string
1618 limit t)
1619 (replace-match-data
1620 t real-match-data)
1621 (goto-char opoint)
1622 nil))))))
1623
1624 ;; Record whether the match is nonempty, to avoid an infinite loop
1625 ;; repeatedly matching the same empty string.
1626 (setq nonempty-match
1627 (/= (nth 0 real-match-data) (nth 1 real-match-data)))
1628
1629 ;; If the match is empty, record that the next one can't be
1630 ;; adjacent.
1631
1632 ;; Otherwise, if matching a regular expression, do the next
1633 ;; match now, since the replacement for this match may
1634 ;; affect whether the next match is adjacent to this one.
1635 ;; If that match is empty, don't use it.
1636 (setq match-again
1637 (and nonempty-match
1638 (or (not regexp-flag)
1639 (and (looking-at search-string)
1640 (let ((match (match-data)))
1641 (and (/= (nth 0 match) (nth 1 match))
1642 match))))))
1643
1644 ;; Optionally ignore matches that have a read-only property.
1645 (unless (and query-replace-skip-read-only
1646 (text-property-not-all
1647 (nth 0 real-match-data) (nth 1 real-match-data)
1648 'read-only nil))
1649
1650 ;; Calculate the replacement string, if necessary.
1651 (when replacements
1652 (set-match-data real-match-data)
1653 (setq next-replacement
1654 (funcall (car replacements) (cdr replacements)
1655 replace-count)))
1656 (if (not query-flag)
1657 (let ((inhibit-read-only
1658 query-replace-skip-read-only))
1659 (unless (or literal noedit)
1660 (replace-highlight
1661 (nth 0 real-match-data) (nth 1 real-match-data)
1662 start end search-string
1663 (or delimited-flag regexp-flag) case-fold-search))
1664 (setq noedit
1665 (replace-match-maybe-edit
1666 next-replacement nocasify literal
1667 noedit real-match-data)
1668 replace-count (1+ replace-count)))
1669 (undo-boundary)
1670 (let (done replaced key def)
1671 ;; Loop reading commands until one of them sets done,
1672 ;; which means it has finished handling this
1673 ;; occurrence. Any command that sets `done' should
1674 ;; leave behind proper match data for the stack.
1675 ;; Commands not setting `done' need to adjust
1676 ;; `real-match-data'.
1677 (while (not done)
1678 (set-match-data real-match-data)
1679 (replace-highlight
1680 (match-beginning 0) (match-end 0)
1681 start end search-string
1682 (or delimited-flag regexp-flag) case-fold-search)
1683 ;; Bind message-log-max so we don't fill up the message log
1684 ;; with a bunch of identical messages.
1685 (let ((message-log-max nil)
1686 (replacement-presentation
1687 (if query-replace-show-replacement
1688 (save-match-data
1689 (set-match-data real-match-data)
1690 (match-substitute-replacement next-replacement
1691 nocasify literal))
1692 next-replacement)))
1693 (message message
1694 (query-replace-descr from-string)
1695 (query-replace-descr replacement-presentation)))
1696 (setq key (read-event))
1697 ;; Necessary in case something happens during read-event
1698 ;; that clobbers the match data.
1699 (set-match-data real-match-data)
1700 (setq key (vector key))
1701 (setq def (lookup-key map key))
1702 ;; Restore the match data while we process the command.
1703 (cond ((eq def 'help)
1704 (with-output-to-temp-buffer "*Help*"
1705 (princ
1706 (concat "Query replacing "
1707 (if regexp-flag "regexp " "")
1708 from-string " with "
1709 next-replacement ".\n\n"
1710 (substitute-command-keys
1711 query-replace-help)))
1712 (with-current-buffer standard-output
1713 (help-mode))))
1714 ((eq def 'exit)
1715 (setq keep-going nil)
1716 (setq done t))
1717 ((eq def 'backup)
1718 (if stack
1719 (let ((elt (pop stack)))
1720 (goto-char (nth 0 elt))
1721 (setq replaced (nth 1 elt)
1722 real-match-data
1723 (replace-match-data
1724 t real-match-data
1725 (nth 2 elt))))
1726 (message "No previous match")
1727 (ding 'no-terminate)
1728 (sit-for 1)))
1729 ((eq def 'act)
1730 (or replaced
1731 (setq noedit
1732 (replace-match-maybe-edit
1733 next-replacement nocasify literal
1734 noedit real-match-data)
1735 replace-count (1+ replace-count)))
1736 (setq done t replaced t))
1737 ((eq def 'act-and-exit)
1738 (or replaced
1739 (setq noedit
1740 (replace-match-maybe-edit
1741 next-replacement nocasify literal
1742 noedit real-match-data)
1743 replace-count (1+ replace-count)))
1744 (setq keep-going nil)
1745 (setq done t replaced t))
1746 ((eq def 'act-and-show)
1747 (if (not replaced)
1748 (setq noedit
1749 (replace-match-maybe-edit
1750 next-replacement nocasify literal
1751 noedit real-match-data)
1752 replace-count (1+ replace-count)
1753 real-match-data (replace-match-data
1754 t real-match-data)
1755 replaced t)))
1756 ((eq def 'automatic)
1757 (or replaced
1758 (setq noedit
1759 (replace-match-maybe-edit
1760 next-replacement nocasify literal
1761 noedit real-match-data)
1762 replace-count (1+ replace-count)))
1763 (setq done t query-flag nil replaced t))
1764 ((eq def 'skip)
1765 (setq done t))
1766 ((eq def 'recenter)
1767 (recenter nil))
1768 ((eq def 'edit)
1769 (let ((opos (point-marker)))
1770 (setq real-match-data (replace-match-data
1771 nil real-match-data
1772 real-match-data))
1773 (goto-char (match-beginning 0))
1774 (save-excursion
1775 (save-window-excursion
1776 (recursive-edit)))
1777 (goto-char opos)
1778 (set-marker opos nil))
1779 ;; Before we make the replacement,
1780 ;; decide whether the search string
1781 ;; can match again just after this match.
1782 (if (and regexp-flag nonempty-match)
1783 (setq match-again (and (looking-at search-string)
1784 (match-data)))))
1785 ;; Edit replacement.
1786 ((eq def 'edit-replacement)
1787 (setq real-match-data (replace-match-data
1788 nil real-match-data
1789 real-match-data)
1790 next-replacement
1791 (read-string "Edit replacement string: "
1792 next-replacement)
1793 noedit nil)
1794 (if replaced
1795 (set-match-data real-match-data)
1796 (setq noedit
1797 (replace-match-maybe-edit
1798 next-replacement nocasify literal noedit
1799 real-match-data)
1800 replaced t))
1801 (setq done t))
1802
1803 ((eq def 'delete-and-edit)
1804 (replace-match "" t t)
1805 (setq real-match-data (replace-match-data
1806 nil real-match-data))
1807 (replace-dehighlight)
1808 (save-excursion (recursive-edit))
1809 (setq replaced t))
1810 ;; Note: we do not need to treat `exit-prefix'
1811 ;; specially here, since we reread
1812 ;; any unrecognized character.
1813 (t
1814 (setq this-command 'mode-exited)
1815 (setq keep-going nil)
1816 (setq unread-command-events
1817 (append (listify-key-sequence key)
1818 unread-command-events))
1819 (setq done t)))
1820 (when query-replace-lazy-highlight
1821 ;; Force lazy rehighlighting only after replacements
1822 (if (not (memq def '(skip backup)))
1823 (setq isearch-lazy-highlight-last-string nil))))
1824 ;; Record previous position for ^ when we move on.
1825 ;; Change markers to numbers in the match data
1826 ;; since lots of markers slow down editing.
1827 (push (list (point) replaced
1828 ;;; If the replacement has already happened, all we need is the
1829 ;;; current match start and end. We could get this with a trivial
1830 ;;; match like
1831 ;;; (save-excursion (goto-char (match-beginning 0))
1832 ;;; (search-forward (match-string 0))
1833 ;;; (match-data t))
1834 ;;; if we really wanted to avoid manually constructing match data.
1835 ;;; Adding current-buffer is necessary so that match-data calls can
1836 ;;; return markers which are appropriate for editing.
1837 (if replaced
1838 (list
1839 (match-beginning 0)
1840 (match-end 0)
1841 (current-buffer))
1842 (match-data t)))
1843 stack)))))
1844
1845 (replace-dehighlight))
1846 (or unread-command-events
1847 (message "Replaced %d occurrence%s"
1848 replace-count
1849 (if (= replace-count 1) "" "s")))
1850 (and keep-going stack)))
1851
1852 (defvar replace-overlay nil)
1853
1854 (defun replace-highlight (match-beg match-end range-beg range-end
1855 string regexp case-fold)
1856 (if query-replace-highlight
1857 (if replace-overlay
1858 (move-overlay replace-overlay match-beg match-end (current-buffer))
1859 (setq replace-overlay (make-overlay match-beg match-end))
1860 (overlay-put replace-overlay 'priority 1001) ;higher than lazy overlays
1861 (overlay-put replace-overlay 'face 'query-replace)))
1862 (if query-replace-lazy-highlight
1863 (let ((isearch-string string)
1864 (isearch-regexp regexp)
1865 (search-whitespace-regexp nil)
1866 (isearch-case-fold-search case-fold))
1867 (isearch-lazy-highlight-new-loop range-beg range-end))))
1868
1869 (defun replace-dehighlight ()
1870 (when replace-overlay
1871 (delete-overlay replace-overlay))
1872 (when query-replace-lazy-highlight
1873 (lazy-highlight-cleanup lazy-highlight-cleanup)
1874 (setq isearch-lazy-highlight-last-string nil)))
1875
1876 ;; arch-tag: 16b4cd61-fd40-497b-b86f-b667c4cf88e4
1877 ;;; replace.el ends here