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