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