1 ;;; replace.el --- replace commands for Emacs
3 ;; Copyright (C) 1985-1987, 1992, 1994, 1996-1997, 2000-2011
4 ;; Free Software Foundation, Inc.
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;; This package supplies the string and regular-expression replace functions
27 ;; documented in the Emacs user's manual.
31 (defcustom case-replace t
32 "Non-nil means `query-replace' should preserve case in replacements."
36 (defvar query-replace-history nil
37 "Default history list for query-replace commands.
38 See `query-replace-from-history-variable' and
39 `query-replace-to-history-variable'.")
41 (defvar query-replace-defaults nil
42 "Default values of FROM-STRING and TO-STRING for `query-replace'.
43 This is a cons cell (FROM-STRING . TO-STRING), or nil if there is
46 (defvar query-replace-interactive nil
47 "Non-nil means `query-replace' uses the last search string.
48 That becomes the \"string to replace\".")
50 (defcustom query-replace-from-history-variable
'query-replace-history
51 "History list to use for the FROM argument of `query-replace' commands.
52 The value of this variable should be a symbol; that symbol
53 is used as a variable to hold a history list for the strings
54 or patterns to be replaced."
59 (defcustom query-replace-to-history-variable
'query-replace-history
60 "History list to use for the TO argument of `query-replace' commands.
61 The value of this variable should be a symbol; that symbol
62 is used as a variable to hold a history list for replacement
68 (defcustom query-replace-skip-read-only nil
69 "Non-nil means `query-replace' and friends ignore read-only matches."
74 (defcustom query-replace-show-replacement t
75 "Non-nil means to show what actual replacement text will be."
80 (defcustom query-replace-highlight t
81 "Non-nil means to highlight matches during query replacement."
85 (defcustom query-replace-lazy-highlight t
86 "Controls the lazy-highlighting during query replacements.
87 When non-nil, all text in the buffer matching the current match
88 is highlighted lazily using isearch lazy highlighting (see
89 `lazy-highlight-initial-delay' and `lazy-highlight-interval')."
91 :group
'lazy-highlight
95 (defface query-replace
96 '((t (:inherit isearch
)))
97 "Face for highlighting query replacement matches."
101 (defvar replace-count
0
102 "Number of replacements done so far.
103 See `replace-regexp' and `query-replace-regexp-eval'.")
105 (defun query-replace-descr (string)
106 (mapconcat 'isearch-text-char-description string
""))
108 (defun query-replace-read-from (prompt regexp-flag
)
109 "Query and return the `from' argument of a query-replace operation.
110 The return value can also be a pair (FROM . TO) indicating that the user
111 wants to replace FROM with TO."
112 (if query-replace-interactive
113 (car (if regexp-flag regexp-search-ring search-ring
))
114 (let* ((history-add-new-input nil
)
116 ;; The save-excursion here is in case the user marks and copies
117 ;; a region in order to specify the minibuffer input.
118 ;; That should not clobber the region for the query-replace itself.
120 (read-from-minibuffer
121 (if query-replace-defaults
122 (format "%s (default %s -> %s): " prompt
123 (query-replace-descr (car query-replace-defaults
))
124 (query-replace-descr (cdr query-replace-defaults
)))
125 (format "%s: " prompt
))
127 query-replace-from-history-variable
129 (if (and (zerop (length from
)) query-replace-defaults
)
130 (cons (car query-replace-defaults
)
131 (query-replace-compile-replacement
132 (cdr query-replace-defaults
) regexp-flag
))
133 (add-to-history query-replace-from-history-variable from nil t
)
134 ;; Warn if user types \n or \t, but don't reject the input.
136 (string-match "\\(\\`\\|[^\\]\\)\\(\\\\\\\\\\)*\\(\\\\[nt]\\)" from
)
137 (let ((match (match-string 3 from
)))
139 ((string= match
"\\n")
140 (message "Note: `\\n' here doesn't match a newline; to do that, type C-q C-j instead"))
141 ((string= match
"\\t")
142 (message "Note: `\\t' here doesn't match a tab; to do that, just type TAB")))
146 (defun query-replace-compile-replacement (to regexp-flag
)
147 "Maybe convert a regexp replacement TO to Lisp.
148 Returns a list suitable for `perform-replace' if necessary,
149 the original string if not."
151 (string-match "\\(\\`\\|[^\\]\\)\\(\\\\\\\\\\)*\\\\[,#]" to
))
155 (setq pos
(match-end 0))
156 (push (substring to
0 (- pos
2)) list
)
157 (setq char
(aref to
(1- pos
))
158 to
(substring to pos
))
160 (push '(number-to-string replace-count
) list
))
162 (setq pos
(read-from-string to
))
163 (push `(replace-quote ,(car pos
)) list
)
165 ;; Swallow a space after a symbol
166 ;; if there is a space.
167 (if (and (or (symbolp (car pos
))
168 ;; Swallow a space after 'foo
169 ;; but not after (quote foo).
170 (and (eq (car-safe (car pos
)) 'quote
)
171 (not (= ?\
( (aref to
0)))))
172 (eq (string-match " " to
(cdr pos
))
176 (setq to
(substring to end
)))))
177 (string-match "\\(\\`\\|[^\\]\\)\\(\\\\\\\\\\)*\\\\[,#]" to
)))
178 (setq to
(nreverse (delete "" (cons to list
))))
179 (replace-match-string-symbols to
)
180 (cons 'replace-eval-replacement
187 (defun query-replace-read-to (from prompt regexp-flag
)
188 "Query and return the `to' argument of a query-replace operation."
189 (query-replace-compile-replacement
191 (let* ((history-add-new-input nil
)
192 (to (read-from-minibuffer
193 (format "%s %s with: " prompt
(query-replace-descr from
))
195 query-replace-to-history-variable from t
)))
196 (add-to-history query-replace-to-history-variable to nil t
)
197 (setq query-replace-defaults
(cons from to
))
201 (defun query-replace-read-args (prompt regexp-flag
&optional noerror
)
203 (barf-if-buffer-read-only))
204 (let* ((from (query-replace-read-from prompt regexp-flag
))
205 (to (if (consp from
) (prog1 (cdr from
) (setq from
(car from
)))
206 (query-replace-read-to from prompt regexp-flag
))))
207 (list from to current-prefix-arg
)))
209 (defun query-replace (from-string to-string
&optional delimited start end
)
210 "Replace some occurrences of FROM-STRING with TO-STRING.
211 As each match is found, the user must type a character saying
212 what to do with it. For directions, type \\[help-command] at that time.
214 In Transient Mark mode, if the mark is active, operate on the contents
215 of the region. Otherwise, operate from point to the end of the buffer.
217 If `query-replace-interactive' is non-nil, the last incremental search
218 string is used as FROM-STRING--you don't have to specify it with the
221 Matching is independent of case if `case-fold-search' is non-nil and
222 FROM-STRING has no uppercase letters. Replacement transfers the case
223 pattern of the old text to the new text, if `case-replace' and
224 `case-fold-search' are non-nil and FROM-STRING has no uppercase
225 letters. \(Transferring the case pattern means that if the old text
226 matched is all caps, or capitalized, then its replacement is upcased
229 Third arg DELIMITED (prefix arg if interactive), if non-nil, means replace
230 only matches surrounded by word boundaries.
231 Fourth and fifth arg START and END specify the region to operate on.
233 To customize possible responses, change the \"bindings\" in `query-replace-map'."
236 (query-replace-read-args
237 (concat "Query replace"
238 (if current-prefix-arg
" word" "")
239 (if (and transient-mark-mode mark-active
) " in region" ""))
241 (list (nth 0 common
) (nth 1 common
) (nth 2 common
)
242 ;; These are done separately here
243 ;; so that command-history will record these expressions
244 ;; rather than the values they had this time.
245 (if (and transient-mark-mode mark-active
)
247 (if (and transient-mark-mode mark-active
)
249 (perform-replace from-string to-string t nil delimited nil nil start end
))
251 (define-key esc-map
"%" 'query-replace
)
253 (defun query-replace-regexp (regexp to-string
&optional delimited start end
)
254 "Replace some things after point matching REGEXP with TO-STRING.
255 As each match is found, the user must type a character saying
256 what to do with it. For directions, type \\[help-command] at that time.
258 In Transient Mark mode, if the mark is active, operate on the contents
259 of the region. Otherwise, operate from point to the end of the buffer.
261 If `query-replace-interactive' is non-nil, the last incremental search
262 regexp is used as REGEXP--you don't have to specify it with the
265 Matching is independent of case if `case-fold-search' is non-nil and
266 REGEXP has no uppercase letters. Replacement transfers the case
267 pattern of the old text to the new text, if `case-replace' and
268 `case-fold-search' are non-nil and REGEXP has no uppercase letters.
269 \(Transferring the case pattern means that if the old text matched is
270 all caps, or capitalized, then its replacement is upcased or
273 Third arg DELIMITED (prefix arg if interactive), if non-nil, means replace
274 only matches surrounded by word boundaries.
275 Fourth and fifth arg START and END specify the region to operate on.
277 In TO-STRING, `\\&' stands for whatever matched the whole of REGEXP,
278 and `\\=\\N' (where N is a digit) stands for
279 whatever what matched the Nth `\\(...\\)' in REGEXP.
280 `\\?' lets you edit the replacement text in the minibuffer
281 at the given position for each replacement.
283 In interactive calls, the replacement text can contain `\\,'
284 followed by a Lisp expression. Each
285 replacement evaluates that expression to compute the replacement
286 string. Inside of that expression, `\\&' is a string denoting the
287 whole match as a string, `\\N' for a partial match, `\\#&' and `\\#N'
288 for the whole or a partial match converted to a number with
289 `string-to-number', and `\\#' itself for the number of replacements
290 done so far (starting with zero).
292 If the replacement expression is a symbol, write a space after it
293 to terminate it. One space there, if any, will be discarded.
295 When using those Lisp features interactively in the replacement
296 text, TO-STRING is actually made a list instead of a string.
297 Use \\[repeat-complex-command] after this command for details."
300 (query-replace-read-args
301 (concat "Query replace"
302 (if current-prefix-arg
" word" "")
304 (if (and transient-mark-mode mark-active
) " in region" ""))
306 (list (nth 0 common
) (nth 1 common
) (nth 2 common
)
307 ;; These are done separately here
308 ;; so that command-history will record these expressions
309 ;; rather than the values they had this time.
310 (if (and transient-mark-mode mark-active
)
312 (if (and transient-mark-mode mark-active
)
314 (perform-replace regexp to-string t t delimited nil nil start end
))
316 (define-key esc-map
[?\C-%
] 'query-replace-regexp
)
318 (defun query-replace-regexp-eval (regexp to-expr
&optional delimited start end
)
319 "Replace some things after point matching REGEXP with the result of TO-EXPR.
321 Interactive use of this function is deprecated in favor of the
322 `\\,' feature of `query-replace-regexp'. For non-interactive use, a loop
323 using `search-forward-regexp' and `replace-match' is preferred.
325 As each match is found, the user must type a character saying
326 what to do with it. For directions, type \\[help-command] at that time.
328 TO-EXPR is a Lisp expression evaluated to compute each replacement. It may
329 reference `replace-count' to get the number of replacements already made.
330 If the result of TO-EXPR is not a string, it is converted to one using
331 `prin1-to-string' with the NOESCAPE argument (which see).
333 For convenience, when entering TO-EXPR interactively, you can use `\\&' or
334 `\\0' to stand for whatever matched the whole of REGEXP, and `\\N' (where
335 N is a digit) to stand for whatever matched the Nth `\\(...\\)' in REGEXP.
336 Use `\\#&' or `\\#N' if you want a number instead of a string.
337 In interactive use, `\\#' in itself stands for `replace-count'.
339 In Transient Mark mode, if the mark is active, operate on the contents
340 of the region. Otherwise, operate from point to the end of the buffer.
342 If `query-replace-interactive' is non-nil, the last incremental search
343 regexp is used as REGEXP--you don't have to specify it with the
346 Preserves case in each replacement if `case-replace' and `case-fold-search'
347 are non-nil and REGEXP has no uppercase letters.
349 Third arg DELIMITED (prefix arg if interactive), if non-nil, means replace
350 only matches that are surrounded by word boundaries.
351 Fourth and fifth arg START and END specify the region to operate on."
354 (barf-if-buffer-read-only)
356 ;; Let-bind the history var to disable the "foo -> bar" default.
357 ;; Maybe we shouldn't disable this default, but for now I'll
358 ;; leave it off. --Stef
359 (let ((query-replace-to-history-variable nil
))
360 (query-replace-read-from "Query replace regexp" t
)))
361 (to (list (read-from-minibuffer
362 (format "Query replace regexp %s with eval: "
363 (query-replace-descr from
))
364 nil nil t query-replace-to-history-variable from t
))))
365 ;; We make TO a list because replace-match-string-symbols requires one,
366 ;; and the user might enter a single token.
367 (replace-match-string-symbols to
)
368 (list from
(car to
) current-prefix-arg
369 (if (and transient-mark-mode mark-active
)
371 (if (and transient-mark-mode mark-active
)
373 (perform-replace regexp
(cons 'replace-eval-replacement to-expr
)
374 t
'literal delimited nil nil start end
))
376 (make-obsolete 'query-replace-regexp-eval
377 "for interactive use, use the special `\\,' feature of
378 `query-replace-regexp' instead. Non-interactively, a loop
379 using `search-forward-regexp' and `replace-match' is preferred." "22.1")
381 (defun map-query-replace-regexp (regexp to-strings
&optional n start end
)
382 "Replace some matches for REGEXP with various strings, in rotation.
383 The second argument TO-STRINGS contains the replacement strings, separated
384 by spaces. This command works like `query-replace-regexp' except that
385 each successive replacement uses the next successive replacement string,
386 wrapping around from the last such string to the first.
388 In Transient Mark mode, if the mark is active, operate on the contents
389 of the region. Otherwise, operate from point to the end of the buffer.
391 Non-interactively, TO-STRINGS may be a list of replacement strings.
393 If `query-replace-interactive' is non-nil, the last incremental search
394 regexp is used as REGEXP--you don't have to specify it with the minibuffer.
396 A prefix argument N says to use each replacement string N times
397 before rotating to the next.
398 Fourth and fifth arg START and END specify the region to operate on."
400 (let* ((from (if query-replace-interactive
401 (car regexp-search-ring
)
402 (read-from-minibuffer "Map query replace (regexp): "
404 query-replace-from-history-variable
406 (to (read-from-minibuffer
407 (format "Query replace %s with (space-separated strings): "
408 (query-replace-descr from
))
410 query-replace-to-history-variable from t
)))
412 (and current-prefix-arg
413 (prefix-numeric-value current-prefix-arg
))
414 (if (and transient-mark-mode mark-active
)
416 (if (and transient-mark-mode mark-active
)
419 (if (listp to-strings
)
420 (setq replacements to-strings
)
421 (while (/= (length to-strings
) 0)
422 (if (string-match " " to-strings
)
425 (list (substring to-strings
0
426 (string-match " " to-strings
))))
427 to-strings
(substring to-strings
428 (1+ (string-match " " to-strings
))))
429 (setq replacements
(append replacements
(list to-strings
))
431 (perform-replace regexp replacements t t nil n nil start end
)))
433 (defun replace-string (from-string to-string
&optional delimited start end
)
434 "Replace occurrences of FROM-STRING with TO-STRING.
435 Preserve case in each match if `case-replace' and `case-fold-search'
436 are non-nil and FROM-STRING has no uppercase letters.
437 \(Preserving case means that if the string matched is all caps, or capitalized,
438 then its replacement is upcased or capitalized.)
440 In Transient Mark mode, if the mark is active, operate on the contents
441 of the region. Otherwise, operate from point to the end of the buffer.
443 Third arg DELIMITED (prefix arg if interactive), if non-nil, means replace
444 only matches surrounded by word boundaries.
445 Fourth and fifth arg START and END specify the region to operate on.
447 If `query-replace-interactive' is non-nil, the last incremental search
448 string is used as FROM-STRING--you don't have to specify it with the
451 This function is usually the wrong thing to use in a Lisp program.
452 What you probably want is a loop like this:
453 (while (search-forward FROM-STRING nil t)
454 (replace-match TO-STRING nil t))
455 which will run faster and will not set the mark or print anything.
456 \(You may need a more complex loop if FROM-STRING can match the null string
457 and TO-STRING is also null.)"
460 (query-replace-read-args
462 (if current-prefix-arg
" word" "")
464 (if (and transient-mark-mode mark-active
) " in region" ""))
466 (list (nth 0 common
) (nth 1 common
) (nth 2 common
)
467 (if (and transient-mark-mode mark-active
)
469 (if (and transient-mark-mode mark-active
)
471 (perform-replace from-string to-string nil nil delimited nil nil start end
))
473 (defun replace-regexp (regexp to-string
&optional delimited start end
)
474 "Replace things after point matching REGEXP with TO-STRING.
475 Preserve case in each match if `case-replace' and `case-fold-search'
476 are non-nil and REGEXP has no uppercase letters.
478 In Transient Mark mode, if the mark is active, operate on the contents
479 of the region. Otherwise, operate from point to the end of the buffer.
481 Third arg DELIMITED (prefix arg if interactive), if non-nil, means replace
482 only matches surrounded by word boundaries.
483 Fourth and fifth arg START and END specify the region to operate on.
485 In TO-STRING, `\\&' stands for whatever matched the whole of REGEXP,
486 and `\\=\\N' (where N is a digit) stands for
487 whatever what matched the Nth `\\(...\\)' in REGEXP.
488 `\\?' lets you edit the replacement text in the minibuffer
489 at the given position for each replacement.
491 In interactive calls, the replacement text may contain `\\,'
492 followed by a Lisp expression used as part of the replacement
493 text. Inside of that expression, `\\&' is a string denoting the
494 whole match, `\\N' a partial match, `\\#&' and `\\#N' the respective
495 numeric values from `string-to-number', and `\\#' itself for
496 `replace-count', the number of replacements occurred so far.
498 If your Lisp expression is an identifier and the next letter in
499 the replacement string would be interpreted as part of it, you
500 can wrap it with an expression like `\\,(or \\#)'. Incidentally,
501 for this particular case you may also enter `\\#' in the
502 replacement text directly.
504 When using those Lisp features interactively in the replacement
505 text, TO-STRING is actually made a list instead of a string.
506 Use \\[repeat-complex-command] after this command for details.
508 If `query-replace-interactive' is non-nil, the last incremental search
509 regexp is used as REGEXP--you don't have to specify it with the minibuffer.
511 This function is usually the wrong thing to use in a Lisp program.
512 What you probably want is a loop like this:
513 (while (re-search-forward REGEXP nil t)
514 (replace-match TO-STRING nil nil))
515 which will run faster and will not set the mark or print anything."
518 (query-replace-read-args
520 (if current-prefix-arg
" word" "")
522 (if (and transient-mark-mode mark-active
) " in region" ""))
524 (list (nth 0 common
) (nth 1 common
) (nth 2 common
)
525 (if (and transient-mark-mode mark-active
)
527 (if (and transient-mark-mode mark-active
)
529 (perform-replace regexp to-string nil t delimited nil nil start end
))
532 (defvar regexp-history nil
533 "History list for some commands that read regular expressions.
535 Maximum length of the history list is determined by the value
536 of `history-length', which see.")
538 (defvar occur-collect-regexp-history
'("\\1")
539 "History of regexp for occur's collect operation")
541 (defun read-regexp (prompt &optional default-value
)
542 "Read regexp as a string using the regexp history and some useful defaults.
543 Prompt for a regular expression with PROMPT (without a colon and
544 space) in the minibuffer. The optional argument DEFAULT-VALUE
545 provides the value to display in the minibuffer prompt that is
546 returned if the user just types RET.
547 Values available via M-n are the string at point, the last isearch
548 regexp, the last isearch string, and the last replacement regexp."
551 (or (funcall (or find-tag-default-function
552 (get major-mode
'find-tag-default-function
)
555 (car regexp-search-ring
)
556 (regexp-quote (or (car search-ring
) ""))
558 query-replace-from-history-variable
))))
559 (defaults (delete-dups (delq nil
(delete "" defaults
))))
560 ;; Don't add automatically the car of defaults for empty input
561 (history-add-new-input nil
)
563 (read-from-minibuffer
565 (format "%s (default %s): " prompt
566 (query-replace-descr default-value
))
567 (format "%s: " prompt
))
568 nil nil nil
'regexp-history defaults t
)))
570 (or default-value input
)
572 (add-to-history 'regexp-history input
)))))
575 (defalias 'delete-non-matching-lines
'keep-lines
)
576 (defalias 'delete-matching-lines
'flush-lines
)
577 (defalias 'count-matches
'how-many
)
580 (defun keep-lines-read-args (prompt)
581 "Read arguments for `keep-lines' and friends.
582 Prompt for a regexp with PROMPT.
583 Value is a list, (REGEXP)."
584 (list (read-regexp prompt
) nil nil t
))
586 (defun keep-lines (regexp &optional rstart rend interactive
)
587 "Delete all lines except those containing matches for REGEXP.
588 A match split across lines preserves all the lines it lies in.
589 When called from Lisp (and usually interactively as well, see below)
590 applies to all lines starting after point.
592 If REGEXP contains upper case characters (excluding those preceded by `\\')
593 and `search-upper-case' is non-nil, the matching is case-sensitive.
595 Second and third arg RSTART and REND specify the region to operate on.
596 This command operates on (the accessible part of) all lines whose
597 accessible part is entirely contained in the region determined by RSTART
598 and REND. (A newline ending a line counts as part of that line.)
600 Interactively, in Transient Mark mode when the mark is active, operate
601 on all lines whose accessible part is entirely contained in the region.
602 Otherwise, the command applies to all lines starting after point.
603 When calling this function from Lisp, you can pretend that it was
604 called interactively by passing a non-nil INTERACTIVE argument.
606 This function starts looking for the next match from the end of
607 the previous match. Hence, it ignores matches that overlap
608 a previously found match."
612 (barf-if-buffer-read-only)
613 (keep-lines-read-args "Keep lines containing match for regexp")))
616 (goto-char (min rstart rend
))
620 (goto-char (max rstart rend
))
621 (unless (or (bolp) (eobp))
624 (if (and interactive transient-mark-mode mark-active
)
625 (setq rstart
(region-beginning)
627 (goto-char (region-end))
628 (unless (or (bolp) (eobp))
632 rend
(point-max-marker)))
635 (or (bolp) (forward-line 1))
636 (let ((start (point))
638 (if (and case-fold-search search-upper-case
)
639 (isearch-no-upper-case-p regexp t
)
641 (while (< (point) rend
)
642 ;; Start is first char not preserved by previous match.
643 (if (not (re-search-forward regexp rend
'move
))
644 (delete-region start rend
)
645 (let ((end (save-excursion (goto-char (match-beginning 0))
648 ;; Now end is first char preserved by the new match.
650 (delete-region start end
))))
652 (setq start
(save-excursion (forward-line 1) (point)))
653 ;; If the match was empty, avoid matching again at same place.
654 (and (< (point) rend
)
655 (= (match-beginning 0) (match-end 0))
657 (set-marker rend nil
)
661 (defun flush-lines (regexp &optional rstart rend interactive
)
662 "Delete lines containing matches for REGEXP.
663 When called from Lisp (and usually when called interactively as
664 well, see below), applies to the part of the buffer after point.
665 The line point is in is deleted if and only if it contains a
666 match for regexp starting after point.
668 If REGEXP contains upper case characters (excluding those preceded by `\\')
669 and `search-upper-case' is non-nil, the matching is case-sensitive.
671 Second and third arg RSTART and REND specify the region to operate on.
672 Lines partially contained in this region are deleted if and only if
673 they contain a match entirely contained in it.
675 Interactively, in Transient Mark mode when the mark is active, operate
676 on the contents of the region. Otherwise, operate from point to the
677 end of (the accessible portion of) the buffer. When calling this function
678 from Lisp, you can pretend that it was called interactively by passing
679 a non-nil INTERACTIVE argument.
681 If a match is split across lines, all the lines it lies in are deleted.
682 They are deleted _before_ looking for the next match. Hence, a match
683 starting on the same line at which another match ended is ignored."
687 (barf-if-buffer-read-only)
688 (keep-lines-read-args "Flush lines containing match for regexp")))
691 (goto-char (min rstart rend
))
692 (setq rend
(copy-marker (max rstart rend
))))
693 (if (and interactive transient-mark-mode mark-active
)
694 (setq rstart
(region-beginning)
695 rend
(copy-marker (region-end)))
697 rend
(point-max-marker)))
699 (let ((case-fold-search
700 (if (and case-fold-search search-upper-case
)
701 (isearch-no-upper-case-p regexp t
)
704 (while (and (< (point) rend
)
705 (re-search-forward regexp rend t
))
706 (delete-region (save-excursion (goto-char (match-beginning 0))
709 (progn (forward-line 1) (point))))))
710 (set-marker rend nil
)
714 (defun how-many (regexp &optional rstart rend interactive
)
715 "Print and return number of matches for REGEXP following point.
716 When called from Lisp and INTERACTIVE is omitted or nil, just return
717 the number, do not print it; if INTERACTIVE is t, the function behaves
718 in all respects as if it had been called interactively.
720 If REGEXP contains upper case characters (excluding those preceded by `\\')
721 and `search-upper-case' is non-nil, the matching is case-sensitive.
723 Second and third arg RSTART and REND specify the region to operate on.
725 Interactively, in Transient Mark mode when the mark is active, operate
726 on the contents of the region. Otherwise, operate from point to the
727 end of (the accessible portion of) the buffer.
729 This function starts looking for the next match from the end of
730 the previous match. Hence, it ignores matches that overlap
731 a previously found match."
734 (keep-lines-read-args "How many matches for regexp"))
738 (goto-char (min rstart rend
))
739 (setq rend
(max rstart rend
)))
740 (if (and interactive transient-mark-mode mark-active
)
741 (setq rstart
(region-beginning)
749 (if (and case-fold-search search-upper-case
)
750 (isearch-no-upper-case-p regexp t
)
752 (while (and (< (point) rend
)
753 (progn (setq opoint
(point))
754 (re-search-forward regexp rend t
)))
755 (if (= opoint
(point))
757 (setq count
(1+ count
))))
758 (when interactive
(message "%d occurrence%s"
760 (if (= count
1) "" "s")))
764 (defvar occur-menu-map
765 (let ((map (make-sparse-keymap)))
766 (define-key map
[next-error-follow-minor-mode
]
767 `(menu-item ,(purecopy "Auto Occurrence Display")
768 next-error-follow-minor-mode
770 "Display another occurrence when moving the cursor")
771 :button
(:toggle .
(and (boundp 'next-error-follow-minor-mode
)
772 next-error-follow-minor-mode
))))
773 (define-key map
[separator-1
] menu-bar-separator
)
774 (define-key map
[kill-this-buffer
]
775 `(menu-item ,(purecopy "Kill Occur Buffer") kill-this-buffer
776 :help
,(purecopy "Kill the current *Occur* buffer")))
777 (define-key map
[quit-window
]
778 `(menu-item ,(purecopy "Quit Occur Window") quit-window
779 :help
,(purecopy "Quit the current *Occur* buffer. Bury it, and maybe delete the selected frame")))
780 (define-key map
[revert-buffer
]
781 `(menu-item ,(purecopy "Revert Occur Buffer") revert-buffer
782 :help
,(purecopy "Replace the text in the *Occur* buffer with the results of rerunning occur")))
783 (define-key map
[clone-buffer
]
784 `(menu-item ,(purecopy "Clone Occur Buffer") clone-buffer
785 :help
,(purecopy "Create and return a twin copy of the current *Occur* buffer")))
786 (define-key map
[occur-rename-buffer
]
787 `(menu-item ,(purecopy "Rename Occur Buffer") occur-rename-buffer
788 :help
,(purecopy "Rename the current *Occur* buffer to *Occur: original-buffer-name*.")))
789 (define-key map
[separator-2
] menu-bar-separator
)
790 (define-key map
[occur-mode-goto-occurrence-other-window
]
791 `(menu-item ,(purecopy "Go To Occurrence Other Window") occur-mode-goto-occurrence-other-window
792 :help
,(purecopy "Go to the occurrence the current line describes, in another window")))
793 (define-key map
[occur-mode-goto-occurrence
]
794 `(menu-item ,(purecopy "Go To Occurrence") occur-mode-goto-occurrence
795 :help
,(purecopy "Go to the occurrence the current line describes")))
796 (define-key map
[occur-mode-display-occurrence
]
797 `(menu-item ,(purecopy "Display Occurrence") occur-mode-display-occurrence
798 :help
,(purecopy "Display in another window the occurrence the current line describes")))
799 (define-key map
[occur-next
]
800 `(menu-item ,(purecopy "Move to Next Match") occur-next
801 :help
,(purecopy "Move to the Nth (default 1) next match in an Occur mode buffer")))
802 (define-key map
[occur-prev
]
803 `(menu-item ,(purecopy "Move to Previous Match") occur-prev
804 :help
,(purecopy "Move to the Nth (default 1) previous match in an Occur mode buffer")))
806 "Menu keymap for `occur-mode'.")
808 (defvar occur-mode-map
809 (let ((map (make-sparse-keymap)))
810 ;; We use this alternative name, so we can use \\[occur-mode-mouse-goto].
811 (define-key map
[mouse-2
] 'occur-mode-mouse-goto
)
812 (define-key map
"\C-c\C-c" 'occur-mode-goto-occurrence
)
813 (define-key map
"\C-x\C-q" 'occur-edit-mode
)
814 (define-key map
"\C-m" 'occur-mode-goto-occurrence
)
815 (define-key map
"o" 'occur-mode-goto-occurrence-other-window
)
816 (define-key map
"\C-o" 'occur-mode-display-occurrence
)
817 (define-key map
"\M-n" 'occur-next
)
818 (define-key map
"\M-p" 'occur-prev
)
819 (define-key map
"r" 'occur-rename-buffer
)
820 (define-key map
"c" 'clone-buffer
)
821 (define-key map
"\C-c\C-f" 'next-error-follow-minor-mode
)
822 (define-key map
[menu-bar occur
] (cons (purecopy "Occur") occur-menu-map
))
824 "Keymap for `occur-mode'.")
826 (defvar occur-revert-arguments nil
827 "Arguments to pass to `occur-1' to revert an Occur mode buffer.
828 See `occur-revert-function'.")
830 (defcustom occur-mode-hook
'(turn-on-font-lock)
831 "Hook run when entering Occur mode."
835 (defcustom occur-hook nil
836 "Hook run by Occur when there are any matches."
840 (defcustom occur-mode-find-occurrence-hook nil
841 "Hook run by Occur after locating an occurrence.
842 This will be called with the cursor position at the occurrence. An application
843 for this is to reveal context in an outline-mode when the occurrence is hidden."
847 (put 'occur-mode
'mode-class
'special
)
848 (define-derived-mode occur-mode special-mode
"Occur"
849 "Major mode for output from \\[occur].
850 \\<occur-mode-map>Move point to one of the items in this buffer, then use
851 \\[occur-mode-goto-occurrence] to go to the occurrence that the item refers to.
852 Alternatively, click \\[occur-mode-mouse-goto] on an item to go to it.
855 (set (make-local-variable 'revert-buffer-function
) 'occur-revert-function
)
856 (make-local-variable 'occur-revert-arguments
)
857 (add-hook 'change-major-mode-hook
'font-lock-defontify nil t
)
858 (setq next-error-function
'occur-next-error
))
863 (defvar occur-edit-mode-map
864 (let ((map (make-sparse-keymap)))
865 (set-keymap-parent map text-mode-map
)
866 (define-key map
[mouse-2
] 'occur-mode-mouse-goto
)
867 (define-key map
"\C-c\C-c" 'occur-mode-goto-occurrence
)
868 (define-key map
"\C-x\C-q" 'occur-mode
)
869 (define-key map
"\C-c\C-f" 'next-error-follow-minor-mode
)
870 (define-key map
[menu-bar occur
] (cons (purecopy "Occur") occur-menu-map
))
872 "Keymap for `occur-edit-mode'.")
874 (define-derived-mode occur-edit-mode occur-mode
"Occur-Edit"
875 "Major mode for editing *Occur* buffers.
876 In this mode, changes to the *Occur* buffer are also applied to
877 the originating buffer.
879 To return to ordinary Occur mode, use \\[occur-mode]."
880 (setq buffer-read-only nil
)
881 (add-hook 'after-change-functions
'occur-after-change-function nil t
))
883 (defun occur-after-change-function (beg end length
)
886 (let* ((m (get-text-property (line-beginning-position) 'occur-target
))
887 (buf (marker-buffer m
))
888 (col (current-column)))
890 ;; Apply occur-target property to inserted (e.g. yanked) text.
891 (put-text-property beg end
'occur-target m
)
892 ;; Did we insert a newline? Occur Edit mode can't create new
893 ;; Occur entries; just discard everything after the newline.
895 (and (search-forward "\n" end t
)
896 (delete-region (1- (point)) end
))))
897 (let ((line (- (line-number-at-pos)
898 (line-number-at-pos (window-start))))
899 (readonly (with-current-buffer buf buffer-read-only
))
900 (win (or (get-buffer-window buf
)
901 (display-buffer buf t
)))
902 (text (save-excursion
904 (search-forward ":" nil t
)
905 (setq col
(- col
(current-column)))
906 (buffer-substring-no-properties (point) (line-end-position)))))
907 (with-selected-window win
911 (message "Buffer `%s' is read only." buf
)
912 (delete-region (line-beginning-position) (line-end-position))
914 (move-to-column col
))))))
917 (defun occur-revert-function (_ignore1 _ignore2
)
918 "Handle `revert-buffer' for Occur mode buffers."
919 (apply 'occur-1
(append occur-revert-arguments
(list (buffer-name)))))
921 (defun occur-mode-find-occurrence ()
922 (let ((pos (get-text-property (point) 'occur-target
)))
924 (error "No occurrence on this line"))
925 (unless (buffer-live-p (marker-buffer pos
))
926 (error "Buffer for this occurrence was killed"))
929 (defalias 'occur-mode-mouse-goto
'occur-mode-goto-occurrence
)
930 (defun occur-mode-goto-occurrence (&optional event
)
931 "Go to the occurrence the current line describes."
932 (interactive (list last-nonmenu-event
))
935 ;; Actually `event-end' works correctly with a nil argument as
936 ;; well, so we could dispense with this test, but let's not
937 ;; rely on this undocumented behavior.
938 (occur-mode-find-occurrence)
939 (with-current-buffer (window-buffer (posn-window (event-end event
)))
941 (goto-char (posn-point (event-end event
)))
942 (occur-mode-find-occurrence)))))
943 same-window-buffer-names
945 (pop-to-buffer (marker-buffer pos
))
947 (run-hooks 'occur-mode-find-occurrence-hook
)))
949 (defun occur-mode-goto-occurrence-other-window ()
950 "Go to the occurrence the current line describes, in another window."
952 (let ((pos (occur-mode-find-occurrence)))
953 (switch-to-buffer-other-window (marker-buffer pos
))
955 (run-hooks 'occur-mode-find-occurrence-hook
)))
957 (defun occur-mode-display-occurrence ()
958 "Display in another window the occurrence the current line describes."
960 (let ((pos (occur-mode-find-occurrence))
962 ;; Bind these to ensure `display-buffer' puts it in another window.
963 same-window-buffer-names
965 (setq window
(display-buffer (marker-buffer pos
)))
966 ;; This is the way to set point in the proper window.
967 (save-selected-window
968 (select-window window
)
970 (run-hooks 'occur-mode-find-occurrence-hook
))))
972 (defun occur-find-match (n search message
)
973 (if (not n
) (setq n
1))
976 (setq r
(funcall search
(point) 'occur-match
))
978 (get-text-property r
'occur-match
)
979 (setq r
(funcall search r
'occur-match
)))
985 (defun occur-next (&optional n
)
986 "Move to the Nth (default 1) next match in an Occur mode buffer."
988 (occur-find-match n
#'next-single-property-change
"No more matches"))
990 (defun occur-prev (&optional n
)
991 "Move to the Nth (default 1) previous match in an Occur mode buffer."
993 (occur-find-match n
#'previous-single-property-change
"No earlier matches"))
995 (defun occur-next-error (&optional argp reset
)
996 "Move to the Nth (default 1) next match in an Occur mode buffer.
997 Compatibility function for \\[next-error] invocations."
999 ;; we need to run occur-find-match from within the Occur buffer
1000 (with-current-buffer
1001 ;; Choose the buffer and make it current.
1002 (if (next-error-buffer-p (current-buffer))
1004 (next-error-find-buffer nil nil
1006 (eq major-mode
'occur-mode
))))
1008 (goto-char (cond (reset (point-min))
1009 ((< argp
0) (line-beginning-position))
1010 ((> argp
0) (line-end-position))
1015 #'previous-single-property-change
1016 #'next-single-property-change
)
1018 ;; In case the *Occur* buffer is visible in a nonselected window.
1019 (let ((win (get-buffer-window (current-buffer) t
)))
1020 (if win
(set-window-point win
(point))))
1021 (occur-mode-goto-occurrence)))
1024 '((((class color
) (min-colors 88) (background light
))
1025 :background
"yellow1")
1026 (((class color
) (min-colors 88) (background dark
))
1027 :background
"RoyalBlue3")
1028 (((class color
) (min-colors 8) (background light
))
1029 :background
"yellow" :foreground
"black")
1030 (((class color
) (min-colors 8) (background dark
))
1031 :background
"blue" :foreground
"white")
1032 (((type tty
) (class mono
))
1034 (t :background
"gray"))
1035 "Face used to highlight matches permanently."
1039 (defcustom list-matching-lines-default-context-lines
0
1040 "Default number of context lines included around `list-matching-lines' matches.
1041 A negative number means to include that many lines before the match.
1042 A positive number means to include that many lines both before and after."
1046 (defalias 'list-matching-lines
'occur
)
1048 (defcustom list-matching-lines-face
'match
1049 "Face used by \\[list-matching-lines] to show the text that matches.
1050 If the value is nil, don't highlight the matching portions specially."
1054 (defcustom list-matching-lines-buffer-name-face
'underline
1055 "Face used by \\[list-matching-lines] to show the names of buffers.
1056 If the value is nil, don't highlight the buffer names specially."
1060 (defcustom occur-excluded-properties
1061 '(read-only invisible intangible field mouse-face help-echo local-map keymap
1062 yank-handler follow-link
)
1063 "Text properties to discard when copying lines to the *Occur* buffer.
1064 The value should be a list of text properties to discard or t,
1065 which means to discard all text properties."
1066 :type
'(choice (const :tag
"All" t
) (repeat symbol
))
1070 (defun occur-read-primary-args ()
1071 (let* ((perform-collect (consp current-prefix-arg
))
1072 (regexp (read-regexp (if perform-collect
1073 "Collect strings matching regexp"
1074 "List lines matching regexp")
1075 (car regexp-history
))))
1078 ;; Perform collect operation
1079 (if (zerop (regexp-opt-depth regexp
))
1080 ;; No subexpression so collect the entire match.
1082 ;; Get the regexp for collection pattern.
1083 (let ((default (car occur-collect-regexp-history
)))
1085 (format "Regexp to collect (default %s): " default
)
1086 nil
'occur-collect-regexp-history default
)))
1087 ;; Otherwise normal occur takes numerical prefix argument.
1088 (when current-prefix-arg
1089 (prefix-numeric-value current-prefix-arg
))))))
1091 (defun occur-rename-buffer (&optional unique-p interactive-p
)
1092 "Rename the current *Occur* buffer to *Occur: original-buffer-name*.
1093 Here `original-buffer-name' is the buffer name where Occur was originally run.
1094 When given the prefix argument, or called non-interactively, the renaming
1095 will not clobber the existing buffer(s) of that name, but use
1096 `generate-new-buffer-name' instead. You can add this to `occur-hook'
1097 if you always want a separate *Occur* buffer for each buffer where you
1099 (interactive "P\np")
1100 (with-current-buffer
1101 (if (eq major-mode
'occur-mode
) (current-buffer) (get-buffer "*Occur*"))
1102 (rename-buffer (concat "*Occur: "
1103 (mapconcat #'buffer-name
1104 (car (cddr occur-revert-arguments
)) "/")
1106 (or unique-p
(not interactive-p
)))))
1108 (defun occur (regexp &optional nlines
)
1109 "Show all lines in the current buffer containing a match for REGEXP.
1110 If a match spreads across multiple lines, all those lines are shown.
1112 Each line is displayed with NLINES lines before and after, or -NLINES
1113 before if NLINES is negative.
1114 NLINES defaults to `list-matching-lines-default-context-lines'.
1115 Interactively it is the prefix arg.
1117 The lines are shown in a buffer named `*Occur*'.
1118 It serves as a menu to find any of the occurrences in this buffer.
1119 \\<occur-mode-map>\\[describe-mode] in that buffer will explain how.
1121 If REGEXP contains upper case characters (excluding those preceded by `\\')
1122 and `search-upper-case' is non-nil, the matching is case-sensitive.
1124 When NLINES is a string or when the function is called
1125 interactively with prefix argument without a number (`C-u' alone
1126 as prefix) the matching strings are collected into the `*Occur*'
1127 buffer by using NLINES as a replacement regexp. NLINES may
1128 contain \\& and \\N which convention follows `replace-match'.
1129 For example, providing \"defun\\s +\\(\\S +\\)\" for REGEXP and
1130 \"\\1\" for NLINES collects all the function names in a lisp
1131 program. When there is no parenthesized subexpressions in REGEXP
1132 the entire match is collected. In any case the searched buffers
1134 (interactive (occur-read-primary-args))
1135 (occur-1 regexp nlines
(list (current-buffer))))
1137 (defvar ido-ignore-item-temp-list
)
1139 (defun multi-occur (bufs regexp
&optional nlines
)
1140 "Show all lines in buffers BUFS containing a match for REGEXP.
1141 This function acts on multiple buffers; otherwise, it is exactly like
1142 `occur'. When you invoke this command interactively, you must specify
1143 the buffer names that you want, one by one."
1146 (let* ((bufs (list (read-buffer "First buffer to search: "
1147 (current-buffer) t
)))
1149 (ido-ignore-item-temp-list bufs
))
1150 (while (not (string-equal
1151 (setq buf
(read-buffer
1152 (if (eq read-buffer-function
'ido-read-buffer
)
1153 "Next buffer to search (C-j to end): "
1154 "Next buffer to search (RET to end): ")
1157 (add-to-list 'bufs buf
)
1158 (setq ido-ignore-item-temp-list bufs
))
1159 (nreverse (mapcar #'get-buffer bufs
)))
1160 (occur-read-primary-args)))
1161 (occur-1 regexp nlines bufs
))
1163 (defun multi-occur-in-matching-buffers (bufregexp regexp
&optional allbufs
)
1164 "Show all lines matching REGEXP in buffers specified by BUFREGEXP.
1165 Normally BUFREGEXP matches against each buffer's visited file name,
1166 but if you specify a prefix argument, it matches against the buffer name.
1167 See also `multi-occur'."
1170 (let* ((default (car regexp-history
))
1172 (read-from-minibuffer
1173 (if current-prefix-arg
1174 "List lines in buffers whose names match regexp: "
1175 "List lines in buffers whose filenames match regexp: ")
1180 (if (equal input
"")
1183 (occur-read-primary-args)))
1187 (mapcar (lambda (buf)
1189 (string-match bufregexp
1191 (and (buffer-file-name buf
)
1192 (string-match bufregexp
1193 (buffer-file-name buf
))))
1197 (defun occur-1 (regexp nlines bufs
&optional buf-name
)
1198 (unless (and regexp
(not (equal regexp
"")))
1199 (error "Occur doesn't work with the empty regexp"))
1201 (setq buf-name
"*Occur*"))
1203 (active-bufs (delq nil
(mapcar #'(lambda (buf)
1204 (when (buffer-live-p buf
) buf
))
1206 ;; Handle the case where one of the buffers we're searching is the
1207 ;; output buffer. Just rename it.
1208 (when (member buf-name
(mapcar 'buffer-name active-bufs
))
1209 (with-current-buffer (get-buffer buf-name
)
1212 ;; Now find or create the output buffer.
1213 ;; If we just renamed that buffer, we will make a new one here.
1214 (setq occur-buf
(get-buffer-create buf-name
))
1216 (with-current-buffer occur-buf
1217 (if (stringp nlines
)
1218 (fundamental-mode) ;; This is for collect opeartion.
1220 (let ((inhibit-read-only t
)
1221 ;; Don't generate undo entries for creation of the initial contents.
1222 (buffer-undo-list t
))
1225 (if (stringp nlines
)
1226 ;; Treat nlines as a regexp to collect.
1227 (let ((bufs active-bufs
)
1230 (with-current-buffer (car bufs
)
1232 (goto-char (point-min))
1233 (while (re-search-forward regexp nil t
)
1234 ;; Insert the replacement regexp.
1235 (let ((str (match-substitute-replacement nlines
)))
1237 (with-current-buffer occur-buf
1239 (setq count
(1+ count
))
1240 (or (zerop (current-column))
1241 (insert "\n"))))))))
1242 (setq bufs
(cdr bufs
)))
1244 ;; Perform normal occur.
1246 regexp active-bufs occur-buf
1247 (or nlines list-matching-lines-default-context-lines
)
1248 (if (and case-fold-search search-upper-case
)
1249 (isearch-no-upper-case-p regexp t
)
1251 list-matching-lines-buffer-name-face
1252 nil list-matching-lines-face
1253 (not (eq occur-excluded-properties t
))))))
1254 (let* ((bufcount (length active-bufs
))
1255 (diff (- (length bufs
) bufcount
)))
1256 (message "Searched %d buffer%s%s; %s match%s%s"
1257 bufcount
(if (= bufcount
1) "" "s")
1258 (if (zerop diff
) "" (format " (%d killed)" diff
))
1259 (if (zerop count
) "no" (format "%d" count
))
1260 (if (= count
1) "" "es")
1261 ;; Don't display regexp if with remaining text
1262 ;; it is longer than window-width.
1263 (if (> (+ (length regexp
) 42) (window-width))
1264 "" (format " for `%s'" (query-replace-descr regexp
)))))
1265 (setq occur-revert-arguments
(list regexp nlines bufs
))
1267 (kill-buffer occur-buf
)
1268 (display-buffer occur-buf
)
1269 (setq next-error-last-buffer occur-buf
)
1270 (setq buffer-read-only t
)
1271 (set-buffer-modified-p nil
)
1272 (run-hooks 'occur-hook
)))))))
1274 (defun occur-engine (regexp buffers out-buf nlines case-fold
1275 title-face prefix-face match-face keep-props
)
1276 (with-current-buffer out-buf
1277 (let ((globalcount 0)
1279 (case-fold-search case-fold
))
1280 ;; Map over all the buffers
1281 (dolist (buf buffers
)
1282 (when (buffer-live-p buf
)
1283 (let ((matches 0) ;; count of matched lines
1284 (lines 1) ;; line count
1285 (prev-after-lines nil
) ;; context lines of prev match
1286 (prev-lines nil
) ;; line number of prev match endpt
1294 (inhibit-field-text-motion t
)
1295 (headerpt (with-current-buffer out-buf
(point))))
1296 (with-current-buffer buf
1298 ;; Set CODING only if the current buffer locally
1299 ;; binds buffer-file-coding-system.
1300 (not (local-variable-p 'buffer-file-coding-system
))
1301 (setq coding buffer-file-coding-system
))
1303 (goto-char (point-min)) ;; begin searching in the buffer
1305 (setq origpt
(point))
1306 (when (setq endpt
(re-search-forward regexp nil t
))
1307 (setq matches
(1+ matches
)) ;; increment match count
1308 (setq matchbeg
(match-beginning 0))
1309 ;; Get beginning of first match line and end of the last.
1311 (goto-char matchbeg
)
1312 (setq begpt
(line-beginning-position))
1314 (setq endpt
(line-end-position)))
1315 ;; Sum line numbers up to the first match line.
1316 (setq lines
(+ lines
(count-lines origpt begpt
)))
1317 (setq marker
(make-marker))
1318 (set-marker marker matchbeg
)
1319 (setq curstring
(occur-engine-line begpt endpt keep-props
))
1320 ;; Highlight the matches
1321 (let ((len (length curstring
))
1323 (while (and (< start len
)
1324 (string-match regexp curstring start
))
1325 (add-text-properties
1326 (match-beginning 0) (match-end 0)
1330 ;; Use `face' rather than `font-lock-face' here
1331 ;; so as to override faces copied from the buffer.
1332 `(face ,match-face
)))
1334 (setq start
(match-end 0))))
1335 ;; Generate the string to insert for this match
1336 (let* ((match-prefix
1337 ;; Using 7 digits aligns tabs properly.
1338 (apply #'propertize
(format "%7d:" lines
)
1341 `(font-lock-face prefix-face
))
1342 `(occur-prefix t mouse-face
(highlight)
1343 occur-target
,marker follow-link t
1345 help-echo
"mouse-2: go to this occurrence"))))
1347 ;; We don't put `mouse-face' on the newline,
1348 ;; because that loses. And don't put it
1349 ;; on context lines to reduce flicker.
1350 (propertize curstring
'mouse-face
(list 'highlight
)
1351 'occur-target marker
1354 "mouse-2: go to this occurrence"))
1358 ;; Add non-numeric prefix to all non-first lines
1359 ;; of multi-line matches.
1360 (replace-regexp-in-string
1364 ;; Add marker at eol, but no mouse props.
1365 (propertize "\n" 'occur-target marker
)))
1368 ;; The simple display style
1370 ;; The complex multi-line display style.
1371 (setq ret
(occur-context-lines
1372 out-line nlines keep-props begpt endpt
1373 lines prev-lines prev-after-lines
))
1374 ;; Set first elem of the returned list to `data',
1375 ;; and the second elem to `prev-after-lines'.
1376 (setq prev-after-lines
(nth 1 ret
))
1378 ;; Actually insert the match display data
1379 (with-current-buffer out-buf
1384 ;; Sum line numbers between first and last match lines.
1385 (setq lines
(+ lines
(count-lines begpt endpt
)
1386 ;; Add 1 for empty last match line since
1387 ;; count-lines returns 1 line less.
1388 (if (and (bolp) (eolp)) 1 0)))
1389 ;; On to the next match...
1391 (goto-char (point-max)))
1392 (setq prev-lines
(1- lines
)))
1393 ;; Flush remaining context after-lines.
1394 (when prev-after-lines
1395 (with-current-buffer out-buf
1396 (insert (apply #'concat
(occur-engine-add-prefix
1397 prev-after-lines
)))))))
1398 (when (not (zerop matches
)) ;; is the count zero?
1399 (setq globalcount
(+ globalcount matches
))
1400 (with-current-buffer out-buf
1401 (goto-char headerpt
)
1405 (format "%d match%s%s in buffer: %s\n"
1406 matches
(if (= matches
1) "" "es")
1407 ;; Don't display regexp for multi-buffer.
1408 (if (> (length buffers
) 1)
1409 "" (format " for \"%s\""
1410 (query-replace-descr regexp
)))
1414 (add-text-properties beg end
1417 `(font-lock-face ,title-face
))
1418 `(occur-title ,buf
))))
1419 (goto-char (point-min)))))))
1420 ;; Display total match count and regexp for multi-buffer.
1421 (when (and (not (zerop globalcount
)) (> (length buffers
) 1))
1422 (goto-char (point-min))
1425 (insert (format "%d match%s total for \"%s\":\n"
1426 globalcount
(if (= globalcount
1) "" "es")
1427 (query-replace-descr regexp
)))
1429 (add-text-properties beg end
(when title-face
1430 `(font-lock-face ,title-face
))))
1431 (goto-char (point-min)))
1433 ;; CODING is buffer-file-coding-system of the first buffer
1434 ;; that locally binds it. Let's use it also for the output
1436 (set-buffer-file-coding-system coding
))
1437 ;; Return the number of matches
1440 (defun occur-engine-line (beg end
&optional keep-props
)
1441 (if (and keep-props
(if (boundp 'jit-lock-mode
) jit-lock-mode
)
1442 (text-property-not-all beg end
'fontified t
))
1443 (if (fboundp 'jit-lock-fontify-now
)
1444 (jit-lock-fontify-now beg end
)))
1445 (if (and keep-props
(not (eq occur-excluded-properties t
)))
1446 (let ((str (buffer-substring beg end
)))
1447 (remove-list-of-text-properties
1448 0 (length str
) occur-excluded-properties str
)
1450 (buffer-substring-no-properties beg end
)))
1452 (defun occur-engine-add-prefix (lines)
1455 (concat " :" line
"\n"))
1458 (defun occur-accumulate-lines (count &optional keep-props pt
)
1462 (let ((forwardp (> count
0))
1463 result beg end moved
)
1464 (while (not (or (zerop count
)
1467 (and (bobp) (not moved
)))))
1468 (setq count
(+ count
(if forwardp -
1 1)))
1469 (setq beg
(line-beginning-position)
1470 end
(line-end-position))
1471 (push (occur-engine-line beg end keep-props
) result
)
1472 (setq moved
(= 0 (forward-line (if forwardp
1 -
1)))))
1473 (nreverse result
))))
1475 ;; Generate context display for occur.
1476 ;; OUT-LINE is the line where the match is.
1477 ;; NLINES and KEEP-PROPS are args to occur-engine.
1478 ;; LINES is line count of the current match,
1479 ;; PREV-LINES is line count of the previous match,
1480 ;; PREV-AFTER-LINES is a list of after-context lines of the previous match.
1481 ;; Generate a list of lines, add prefixes to all but OUT-LINE,
1482 ;; then concatenate them all together.
1483 (defun occur-context-lines (out-line nlines keep-props begpt endpt
1484 lines prev-lines prev-after-lines
)
1485 ;; Find after- and before-context lines of the current match.
1487 (nreverse (cdr (occur-accumulate-lines
1488 (- (1+ (abs nlines
))) keep-props begpt
))))
1490 (cdr (occur-accumulate-lines
1491 (1+ nlines
) keep-props endpt
)))
1494 ;; Combine after-lines of the previous match
1495 ;; with before-lines of the current match.
1497 (when prev-after-lines
1498 ;; Don't overlap prev after-lines with current before-lines.
1499 (if (>= (+ prev-lines
(length prev-after-lines
))
1500 (- lines
(length before-lines
)))
1501 (setq prev-after-lines
1502 (butlast prev-after-lines
1503 (- (length prev-after-lines
)
1504 (- lines prev-lines
(length before-lines
) 1))))
1505 ;; Separate non-overlapping context lines with a dashed line.
1506 (setq separator
"-------\n")))
1509 ;; Don't overlap current before-lines with previous match line.
1510 (if (<= (- lines
(length before-lines
))
1513 (nthcdr (- (length before-lines
)
1514 (- lines prev-lines
1))
1516 ;; Separate non-overlapping before-context lines.
1517 (unless (> nlines
0)
1518 (setq separator
"-------\n"))))
1521 ;; Return a list where the first element is the output line.
1524 (and prev-after-lines
1525 (occur-engine-add-prefix prev-after-lines
))
1526 (and separator
(list separator
))
1527 (occur-engine-add-prefix before-lines
)
1529 ;; And the second element is the list of context after-lines.
1530 (if (> nlines
0) after-lines
))))
1533 ;; It would be nice to use \\[...], but there is no reasonable way
1534 ;; to make that display both SPC and Y.
1535 (defconst query-replace-help
1536 "Type Space or `y' to replace one match, Delete or `n' to skip to next,
1537 RET or `q' to exit, Period to replace one match and exit,
1538 Comma to replace but not move point immediately,
1539 C-r to enter recursive edit (\\[exit-recursive-edit] to get out again),
1540 C-w to delete match and recursive edit,
1541 C-l to clear the screen, redisplay, and offer same replacement again,
1542 ! to replace all remaining matches with no more questions,
1543 ^ to move point back to previous match,
1544 E to edit the replacement string"
1545 "Help message while in `query-replace'.")
1547 (defvar query-replace-map
1548 (let ((map (make-sparse-keymap)))
1549 (define-key map
" " 'act
)
1550 (define-key map
"\d" 'skip
)
1551 (define-key map
[delete] 'skip)
1552 (define-key map [backspace] 'skip)
1553 (define-key map "y" 'act)
1554 (define-key map "n" 'skip)
1555 (define-key map "Y" 'act)
1556 (define-key map "N" 'skip)
1557 (define-key map "e" 'edit-replacement)
1558 (define-key map "E" 'edit-replacement)
1559 (define-key map "," 'act-and-show)
1560 (define-key map "q" 'exit)
1561 (define-key map "\r" 'exit)
1562 (define-key map [return] 'exit)
1563 (define-key map "." 'act-and-exit)
1564 (define-key map "\C-r" 'edit)
1565 (define-key map "\C-w" 'delete-and-edit)
1566 (define-key map "\C-l" 'recenter)
1567 (define-key map "!" 'automatic)
1568 (define-key map "^" 'backup)
1569 (define-key map "\C-h" 'help)
1570 (define-key map [f1] 'help)
1571 (define-key map [help] 'help)
1572 (define-key map "?" 'help)
1573 (define-key map "\C-g" 'quit)
1574 (define-key map "\C-]" 'quit)
1575 (define-key map "\e" 'exit-prefix)
1576 (define-key map [escape] 'exit-prefix)
1578 "Keymap that defines the responses to questions in `query-replace'.
1579 The \"bindings\" in this map are not commands; they are answers.
1580 The valid answers include `act', `skip', `act-and-show',
1581 `exit', `act-and-exit', `edit', `delete-and-edit', `recenter',
1582 `automatic', `backup', `exit-prefix', and `help'.")
1584 (defvar multi-query-replace-map
1585 (let ((map (make-sparse-keymap)))
1586 (set-keymap-parent map query-replace-map)
1587 (define-key map "Y" 'automatic-all)
1588 (define-key map "N" 'exit-current)
1590 "Keymap that defines additional bindings for multi-buffer replacements.
1591 It extends its parent map `query-replace-map' with new bindings to
1592 operate on a set of buffers/files. The difference with its parent map
1593 is the additional answers `automatic-all' to replace all remaining
1594 matches in all remaining buffers with no more questions, and
1595 `exit-current' to skip remaining matches in the current buffer
1596 and to continue with the next buffer in the sequence.")
1598 (defun replace-match-string-symbols (n)
1599 "Process a list (and any sub-lists), expanding certain symbols.
1601 N (match-string N) (where N is a string of digits)
1602 #N (string-to-number (match-string N))
1604 #& (string-to-number (match-string 0))
1607 Note that these symbols must be preceded by a backslash in order to
1608 type them using Lisp syntax."
1612 (replace-match-string-symbols (car n))) ;Process sub-list
1614 (let ((name (symbol-name (car n))))
1616 ((string-match "^[0-9]+$" name)
1617 (setcar n (list 'match-string (string-to-number name))))
1618 ((string-match "^#[0-9]+$" name)
1619 (setcar n (list 'string-to-number
1621 (string-to-number (substring name 1))))))
1623 (setcar n '(match-string 0)))
1624 ((string= "#&" name)
1625 (setcar n '(string-to-number (match-string 0))))
1627 (setcar n 'replace-count))))))
1630 (defun replace-eval-replacement (expression count)
1631 (let* ((replace-count count)
1632 (replacement (eval expression)))
1633 (if (stringp replacement)
1635 (prin1-to-string replacement t))))
1637 (defun replace-quote (replacement)
1638 "Quote a replacement string.
1639 This just doubles all backslashes in REPLACEMENT and
1640 returns the resulting string. If REPLACEMENT is not
1641 a string, it is first passed through `prin1-to-string'
1642 with the `noescape' argument set.
1644 `match-data' is preserved across the call."
1646 (replace-regexp-in-string "\\\\" "\\\\"
1647 (if (stringp replacement)
1649 (prin1-to-string replacement t))
1652 (defun replace-loop-through-replacements (data count)
1653 ;; DATA is a vector contaning the following values:
1654 ;; 0 next-rotate-count
1656 ;; 2 next-replacement
1658 (if (= (aref data 0) count)
1660 (aset data 0 (+ count (aref data 1)))
1661 (let ((next (cdr (aref data 2))))
1662 (aset data 2 (if (consp next) next (aref data 3))))))
1663 (car (aref data 2)))
1665 (defun replace-match-data (integers reuse &optional new)
1666 "Like `match-data', but markers in REUSE get invalidated.
1667 If NEW is non-nil, it is set and returned instead of fresh data,
1668 but coerced to the correct value of INTEGERS."
1671 (set-match-data new)
1673 (eq (null integers) (markerp (car reuse)))
1675 (match-data integers reuse t)))
1677 (defun replace-match-maybe-edit (newtext fixedcase literal noedit match-data)
1678 "Make a replacement with `replace-match', editing `\\?'.
1679 NEWTEXT, FIXEDCASE, LITERAL are just passed on. If NOEDIT is true, no
1680 check for `\\?' is made to save time. MATCH-DATA is used for the
1681 replacement. In case editing is done, it is changed to use markers.
1683 The return value is non-nil if there has been no `\\?' or NOEDIT was
1684 passed in. If LITERAL is set, no checking is done, anyway."
1685 (unless (or literal noedit)
1687 (while (string-match "\\(\\`\\|[^\\]\\)\\(\\\\\\\\\\)*\\(\\\\\\?\\)"
1690 (read-string "Edit replacement string: "
1693 (replace-match "" t t newtext 3)
1694 (1+ (match-beginning 3)))
1697 nil match-data match-data))))
1699 (set-match-data match-data)
1700 (replace-match newtext fixedcase literal)
1703 (defvar replace-search-function 'search-forward
1704 "Function to use when searching for strings to replace.
1705 It is used by `query-replace' and `replace-string', and is called
1706 with three arguments, as if it were `search-forward'.")
1708 (defvar replace-re-search-function 're-search-forward
1709 "Function to use when searching for regexps to replace.
1710 It is used by `query-replace-regexp', `replace-regexp',
1711 `query-replace-regexp-eval', and `map-query-replace-regexp'.
1712 It is called with three arguments, as if it were
1713 `re-search-forward'.")
1715 (defun perform-replace (from-string replacements
1716 query-flag regexp-flag delimited-flag
1717 &optional repeat-count map start end)
1718 "Subroutine of `query-replace'. Its complexity handles interactive queries.
1719 Don't use this in your own program unless you want to query and set the mark
1720 just as `query-replace' does. Instead, write a simple loop like this:
1722 (while (re-search-forward \"foo[ \\t]+bar\" nil t)
1723 (replace-match \"foobar\" nil nil))
1725 which will run faster and probably do exactly what you want. Please
1726 see the documentation of `replace-match' to find out how to simulate
1729 This function returns nil if and only if there were no matches to
1730 make, or the user didn't cancel the call."
1731 (or map (setq map query-replace-map))
1732 (and query-flag minibuffer-auto-raise
1733 (raise-frame (window-frame (minibuffer-window))))
1734 (let* ((case-fold-search
1735 (if (and case-fold-search search-upper-case)
1736 (isearch-no-upper-case-p from-string regexp-flag)
1738 (nocasify (not (and case-replace case-fold-search)))
1739 (literal (or (not regexp-flag) (eq regexp-flag 'literal)))
1742 replace-re-search-function
1743 replace-search-function))
1744 (search-string from-string)
1745 (real-match-data nil) ; The match data for the current match.
1746 (next-replacement nil)
1747 ;; This is non-nil if we know there is nothing for the user
1748 ;; to edit in the replacement.
1753 (nonempty-match nil)
1755 (recenter-last-op nil) ; Start cycling order with initial position.
1757 ;; If non-nil, it is marker saying where in the buffer to stop.
1760 ;; Data for the next match. If a cons, it has the same format as
1761 ;; (match-data); otherwise it is t if a match is possible at point.
1767 (substitute-command-keys
1768 "Query replacing %s with %s: (\\<query-replace-map>\\[help] for help) ")
1769 minibuffer-prompt-properties))))
1771 ;; If region is active, in Transient Mark mode, operate on region.
1773 (setq limit (copy-marker (max start end)))
1774 (goto-char (min start end))
1777 ;; If last typed key in previous call of multi-buffer perform-replace
1778 ;; was `automatic-all', don't ask more questions in next files
1779 (when (eq (lookup-key map (vector last-input-event)) 'automatic-all)
1780 (setq query-flag nil multi-buffer t))
1782 ;; REPLACEMENTS is either a string, a list of strings, or a cons cell
1783 ;; containing a function and its first argument. The function is
1784 ;; called to generate each replacement like this:
1785 ;; (funcall (car replacements) (cdr replacements) replace-count)
1786 ;; It must return a string.
1788 ((stringp replacements)
1789 (setq next-replacement replacements
1791 ((stringp (car replacements)) ; If it isn't a string, it must be a cons
1792 (or repeat-count (setq repeat-count 1))
1793 (setq replacements (cons 'replace-loop-through-replacements
1794 (vector repeat-count repeat-count
1795 replacements replacements)))))
1798 (setq search-function 're-search-forward
1799 search-string (concat "\\b"
1800 (if regexp-flag from-string
1801 (regexp-quote from-string))
1803 (when query-replace-lazy-highlight
1804 (setq isearch-lazy-highlight-last-string nil))
1809 ;; Loop finding occurrences that perhaps should be replaced.
1810 (while (and keep-going
1811 (not (or (eobp) (and limit (>= (point) limit))))
1812 ;; Use the next match if it is already known;
1813 ;; otherwise, search for a match after moving forward
1814 ;; one char if progress is required.
1815 (setq real-match-data
1816 (cond ((consp match-again)
1817 (goto-char (nth 1 match-again))
1819 t real-match-data match-again))
1820 ;; MATCH-AGAIN non-nil means accept an
1824 (funcall search-function search-string
1826 ;; For speed, use only integers and
1827 ;; reuse the list used last time.
1828 (replace-match-data t real-match-data)))
1829 ((and (< (1+ (point)) (point-max))
1831 (< (1+ (point)) limit)))
1832 ;; If not accepting adjacent matches,
1833 ;; move one char to the right before
1834 ;; searching again. Undo the motion
1835 ;; if the search fails.
1836 (let ((opoint (point)))
1839 search-function search-string
1846 ;; Record whether the match is nonempty, to avoid an infinite loop
1847 ;; repeatedly matching the same empty string.
1848 (setq nonempty-match
1849 (/= (nth 0 real-match-data) (nth 1 real-match-data)))
1851 ;; If the match is empty, record that the next one can't be
1854 ;; Otherwise, if matching a regular expression, do the next
1855 ;; match now, since the replacement for this match may
1856 ;; affect whether the next match is adjacent to this one.
1857 ;; If that match is empty, don't use it.
1860 (or (not regexp-flag)
1861 (and (looking-at search-string)
1862 (let ((match (match-data)))
1863 (and (/= (nth 0 match) (nth 1 match))
1866 ;; Optionally ignore matches that have a read-only property.
1867 (unless (and query-replace-skip-read-only
1868 (text-property-not-all
1869 (nth 0 real-match-data) (nth 1 real-match-data)
1872 ;; Calculate the replacement string, if necessary.
1874 (set-match-data real-match-data)
1875 (setq next-replacement
1876 (funcall (car replacements) (cdr replacements)
1878 (if (not query-flag)
1880 (unless (or literal noedit)
1882 (nth 0 real-match-data) (nth 1 real-match-data)
1883 start end search-string
1884 (or delimited-flag regexp-flag) case-fold-search))
1886 (replace-match-maybe-edit
1887 next-replacement nocasify literal
1888 noedit real-match-data)
1889 replace-count (1+ replace-count)))
1891 (let (done replaced key def)
1892 ;; Loop reading commands until one of them sets done,
1893 ;; which means it has finished handling this
1894 ;; occurrence. Any command that sets `done' should
1895 ;; leave behind proper match data for the stack.
1896 ;; Commands not setting `done' need to adjust
1897 ;; `real-match-data'.
1899 (set-match-data real-match-data)
1901 (match-beginning 0) (match-end 0)
1902 start end search-string
1903 (or delimited-flag regexp-flag) case-fold-search)
1904 ;; Bind message-log-max so we don't fill up the message log
1905 ;; with a bunch of identical messages.
1906 (let ((message-log-max nil)
1907 (replacement-presentation
1908 (if query-replace-show-replacement
1910 (set-match-data real-match-data)
1911 (match-substitute-replacement next-replacement
1915 (query-replace-descr from-string)
1916 (query-replace-descr replacement-presentation)))
1917 (setq key (read-event))
1918 ;; Necessary in case something happens during read-event
1919 ;; that clobbers the match data.
1920 (set-match-data real-match-data)
1921 (setq key (vector key))
1922 (setq def (lookup-key map key))
1923 ;; Restore the match data while we process the command.
1924 (cond ((eq def 'help)
1925 (with-output-to-temp-buffer "*Help*"
1927 (concat "Query replacing "
1928 (if delimited-flag "word " "")
1929 (if regexp-flag "regexp " "")
1930 from-string " with "
1931 next-replacement ".\n\n"
1932 (substitute-command-keys
1933 query-replace-help)))
1934 (with-current-buffer standard-output
1937 (setq keep-going nil)
1939 ((eq def 'exit-current)
1940 (setq multi-buffer t keep-going nil done t))
1943 (let ((elt (pop stack)))
1944 (goto-char (nth 0 elt))
1945 (setq replaced (nth 1 elt)
1950 (message "No previous match")
1951 (ding 'no-terminate)
1956 (replace-match-maybe-edit
1957 next-replacement nocasify literal
1958 noedit real-match-data)
1959 replace-count (1+ replace-count)))
1960 (setq done t replaced t))
1961 ((eq def 'act-and-exit)
1964 (replace-match-maybe-edit
1965 next-replacement nocasify literal
1966 noedit real-match-data)
1967 replace-count (1+ replace-count)))
1968 (setq keep-going nil)
1969 (setq done t replaced t))
1970 ((eq def 'act-and-show)
1973 (replace-match-maybe-edit
1974 next-replacement nocasify literal
1975 noedit real-match-data)
1976 replace-count (1+ replace-count)
1977 real-match-data (replace-match-data
1980 ((or (eq def 'automatic) (eq def 'automatic-all))
1983 (replace-match-maybe-edit
1984 next-replacement nocasify literal
1985 noedit real-match-data)
1986 replace-count (1+ replace-count)))
1987 (setq done t query-flag nil replaced t)
1988 (if (eq def 'automatic-all) (setq multi-buffer t)))
1992 ;; `this-command' has the value `query-replace',
1993 ;; so we need to bind it to `recenter-top-bottom'
1994 ;; to allow it to detect a sequence of `C-l'.
1995 (let ((this-command 'recenter-top-bottom)
1996 (last-command 'recenter-top-bottom))
1997 (recenter-top-bottom)))
1999 (let ((opos (point-marker)))
2000 (setq real-match-data (replace-match-data
2003 (goto-char (match-beginning 0))
2005 (save-window-excursion
2008 (set-marker opos nil))
2009 ;; Before we make the replacement,
2010 ;; decide whether the search string
2011 ;; can match again just after this match.
2012 (if (and regexp-flag nonempty-match)
2013 (setq match-again (and (looking-at search-string)
2015 ;; Edit replacement.
2016 ((eq def 'edit-replacement)
2017 (setq real-match-data (replace-match-data
2021 (read-string "Edit replacement string: "
2025 (set-match-data real-match-data)
2027 (replace-match-maybe-edit
2028 next-replacement nocasify literal noedit
2033 ((eq def 'delete-and-edit)
2034 (replace-match "" t t)
2035 (setq real-match-data (replace-match-data
2036 nil real-match-data))
2037 (replace-dehighlight)
2038 (save-excursion (recursive-edit))
2040 ;; Note: we do not need to treat `exit-prefix'
2041 ;; specially here, since we reread
2042 ;; any unrecognized character.
2044 (setq this-command 'mode-exited)
2045 (setq keep-going nil)
2046 (setq unread-command-events
2047 (append (listify-key-sequence key)
2048 unread-command-events))
2050 (when query-replace-lazy-highlight
2051 ;; Force lazy rehighlighting only after replacements.
2052 (if (not (memq def '(skip backup)))
2053 (setq isearch-lazy-highlight-last-string nil)))
2054 (unless (eq def 'recenter)
2055 ;; Reset recenter cycling order to initial position.
2056 (setq recenter-last-op nil)))
2057 ;; Record previous position for ^ when we move on.
2058 ;; Change markers to numbers in the match data
2059 ;; since lots of markers slow down editing.
2060 (push (list (point) replaced
2061 ;;; If the replacement has already happened, all we need is the
2062 ;;; current match start and end. We could get this with a trivial
2064 ;;; (save-excursion (goto-char (match-beginning 0))
2065 ;;; (search-forward (match-string 0))
2067 ;;; if we really wanted to avoid manually constructing match data.
2068 ;;; Adding current-buffer is necessary so that match-data calls can
2069 ;;; return markers which are appropriate for editing.
2078 (replace-dehighlight))
2079 (or unread-command-events
2080 (message "Replaced %d occurrence%s"
2082 (if (= replace-count 1) "" "s")))
2083 (or (and keep-going stack) multi-buffer)))
2085 (defvar isearch-error)
2086 (defvar isearch-forward)
2087 (defvar isearch-case-fold-search)
2088 (defvar isearch-string)
2090 (defvar replace-overlay nil)
2092 (defun replace-highlight (match-beg match-end range-beg range-end
2093 string regexp case-fold)
2094 (if query-replace-highlight
2096 (move-overlay replace-overlay match-beg match-end (current-buffer))
2097 (setq replace-overlay (make-overlay match-beg match-end))
2098 (overlay-put replace-overlay 'priority 1001) ;higher than lazy overlays
2099 (overlay-put replace-overlay 'face 'query-replace)))
2100 (if query-replace-lazy-highlight
2101 (let ((isearch-string string)
2102 (isearch-regexp regexp)
2103 (search-whitespace-regexp nil)
2104 (isearch-case-fold-search case-fold)
2106 (isearch-error nil))
2107 ;; Set isearch-word to nil because word-replace is regexp-based,
2108 ;; so `isearch-search-fun' should not use `word-search-forward'.
2109 (if (and isearch-word isearch-regexp) (setq isearch-word nil))
2110 (isearch-lazy-highlight-new-loop range-beg range-end))))
2112 (defun replace-dehighlight ()
2113 (when replace-overlay
2114 (delete-overlay replace-overlay))
2115 (when query-replace-lazy-highlight
2116 (lazy-highlight-cleanup lazy-highlight-cleanup)
2117 (setq isearch-lazy-highlight-last-string nil)))
2119 ;;; replace.el ends here