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