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