Commit | Line | Data |
---|---|---|
60370d40 | 1 | ;;; replace.el --- replace commands for Emacs |
c88ab9ce | 2 | |
6f1df6d9 SM |
3 | ;; Copyright (C) 1985, 1986, 1987, 1992, 1994, 1996, 1997, 2000, 2001, 2002, |
4 | ;; 2003, 2004 Free Software Foundation, Inc. | |
3a801d0c | 5 | |
30764597 PJ |
6 | ;; Maintainer: FSF |
7 | ||
698e1804 RS |
8 | ;; This file is part of GNU Emacs. |
9 | ||
10 | ;; GNU Emacs is free software; you can redistribute it and/or modify | |
11 | ;; it under the terms of the GNU General Public License as published by | |
e5d77022 | 12 | ;; the Free Software Foundation; either version 2, or (at your option) |
698e1804 RS |
13 | ;; any later version. |
14 | ||
15 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
16 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 | ;; GNU General Public License for more details. | |
19 | ||
20 | ;; You should have received a copy of the GNU General Public License | |
b578f267 EN |
21 | ;; along with GNU Emacs; see the file COPYING. If not, write to the |
22 | ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
23 | ;; Boston, MA 02111-1307, USA. | |
698e1804 | 24 | |
d9ecc911 ER |
25 | ;;; Commentary: |
26 | ||
27 | ;; This package supplies the string and regular-expression replace functions | |
28 | ;; documented in the Emacs user's manual. | |
29 | ||
4f4b8eff | 30 | ;;; Code: |
698e1804 | 31 | |
9d325ebf | 32 | (defcustom case-replace t |
f54701d1 | 33 | "*Non-nil means `query-replace' should preserve case in replacements." |
9d325ebf RS |
34 | :type 'boolean |
35 | :group 'matching) | |
77176e73 | 36 | |
770970cb RS |
37 | (defvar query-replace-history nil) |
38 | ||
5291cbca | 39 | (defvar query-replace-interactive nil |
151270f3 | 40 | "Non-nil means `query-replace' uses the last search string. |
5291cbca | 41 | That becomes the \"string to replace\".") |
151270f3 | 42 | |
bdb1c08f | 43 | (defcustom query-replace-from-history-variable 'query-replace-history |
f54701d1 | 44 | "History list to use for the FROM argument of `query-replace' commands. |
bdb1c08f RS |
45 | The value of this variable should be a symbol; that symbol |
46 | is used as a variable to hold a history list for the strings | |
47 | or patterns to be replaced." | |
48 | :group 'matching | |
cd32a7ba DN |
49 | :type 'symbol |
50 | :version "20.3") | |
bdb1c08f RS |
51 | |
52 | (defcustom query-replace-to-history-variable 'query-replace-history | |
f54701d1 | 53 | "History list to use for the TO argument of `query-replace' commands. |
bdb1c08f RS |
54 | The value of this variable should be a symbol; that symbol |
55 | is used as a variable to hold a history list for replacement | |
56 | strings or patterns." | |
57 | :group 'matching | |
cd32a7ba DN |
58 | :type 'symbol |
59 | :version "20.3") | |
bdb1c08f | 60 | |
1c4fe319 RS |
61 | (defcustom query-replace-skip-read-only nil |
62 | "*Non-nil means `query-replace' and friends ignore read-only matches." | |
63 | :type 'boolean | |
64 | :group 'matching | |
89e7ad59 | 65 | :version "21.4") |
1c4fe319 | 66 | |
6f1df6d9 SM |
67 | (defun query-replace-descr (string) |
68 | (mapconcat 'isearch-text-char-description string "")) | |
69 | ||
1606466a | 70 | (defun query-replace-read-from (string regexp-flag) |
6f1df6d9 SM |
71 | "Query and return the `from' argument of a query-replace operation. |
72 | The return value can also be a pair (FROM . TO) indicating that the user | |
73 | wants to replace FROM with TO." | |
1606466a SM |
74 | (if query-replace-interactive |
75 | (car (if regexp-flag regexp-search-ring search-ring)) | |
6f1df6d9 SM |
76 | (let* ((lastfrom (car (symbol-value query-replace-from-history-variable))) |
77 | (lastto (car (symbol-value query-replace-to-history-variable))) | |
78 | (from | |
1606466a SM |
79 | ;; The save-excursion here is in case the user marks and copies |
80 | ;; a region in order to specify the minibuffer input. | |
81 | ;; That should not clobber the region for the query-replace itself. | |
82 | (save-excursion | |
6f1df6d9 SM |
83 | (when (equal lastfrom lastto) |
84 | ;; Typically, this is because the two histlists are shared. | |
85 | (setq lastfrom (cadr (symbol-value | |
86 | query-replace-from-history-variable)))) | |
1606466a | 87 | (read-from-minibuffer |
6f1df6d9 SM |
88 | (if (and lastto lastfrom) |
89 | (format "%s (default %s -> %s): " string | |
90 | (query-replace-descr lastfrom) | |
91 | (query-replace-descr lastto)) | |
92 | (format "%s: " string)) | |
1606466a SM |
93 | nil nil nil |
94 | query-replace-from-history-variable | |
bbe9f175 | 95 | nil t t)))) |
6f1df6d9 | 96 | (if (and (zerop (length from)) lastto lastfrom) |
78629844 DK |
97 | (cons lastfrom |
98 | (query-replace-compile-replacement lastto regexp-flag)) | |
6f1df6d9 SM |
99 | ;; Warn if user types \n or \t, but don't reject the input. |
100 | (and regexp-flag | |
101 | (string-match "\\(\\`\\|[^\\]\\)\\(\\\\\\\\\\)*\\(\\\\[nt]\\)" from) | |
102 | (let ((match (match-string 3 from))) | |
103 | (cond | |
104 | ((string= match "\\n") | |
105 | (message "Note: `\\n' here doesn't match a newline; to do that, type C-q C-j instead")) | |
106 | ((string= match "\\t") | |
107 | (message "Note: `\\t' here doesn't match a tab; to do that, just type TAB"))) | |
108 | (sit-for 2))) | |
109 | from)))) | |
1606466a | 110 | |
78629844 DK |
111 | (defun query-replace-compile-replacement (to regexp-flag) |
112 | "Maybe convert a regexp replacement TO to Lisp. | |
113 | Returns a list suitable for `perform-replace' if necessary, | |
114 | the original string if not." | |
115 | (if (and regexp-flag | |
116 | (string-match "\\(\\`\\|[^\\]\\)\\(\\\\\\\\\\)*\\\\[,#]" to)) | |
7c1c02ac DK |
117 | (let (pos list char) |
118 | (while | |
119 | (progn | |
120 | (setq pos (match-end 0)) | |
121 | (push (substring to 0 (- pos 2)) list) | |
122 | (setq char (aref to (1- pos)) | |
123 | to (substring to pos)) | |
124 | (cond ((eq char ?\#) | |
125 | (push '(number-to-string replace-count) list)) | |
126 | ((eq char ?\,) | |
127 | (setq pos (read-from-string to)) | |
128 | (push `(replace-quote ,(car pos)) list) | |
9e5d1b63 RS |
129 | (let ((end |
130 | ;; Swallow a space after a symbol | |
131 | ;; if there is a space. | |
132 | (if (and (or (symbolp (car pos)) | |
133 | ;; Swallow a space after 'foo | |
134 | ;; but not after (quote foo). | |
135 | (and (eq (car-safe (car pos)) 'quote) | |
f1f6079c JL |
136 | (not (= ?\( (aref to 0))))) |
137 | (eq (string-match " " to (cdr pos)) | |
138 | (cdr pos))) | |
9e5d1b63 RS |
139 | (1+ (cdr pos)) |
140 | (cdr pos)))) | |
141 | (setq to (substring to end))))) | |
7c1c02ac | 142 | (string-match "\\(\\`\\|[^\\]\\)\\(\\\\\\\\\\)*\\\\[,#]" to))) |
78629844 DK |
143 | (setq to (nreverse (delete "" (cons to list)))) |
144 | (replace-match-string-symbols to) | |
145 | (cons 'replace-eval-replacement | |
146 | (if (cdr to) | |
147 | (cons 'concat to) | |
148 | (car to)))) | |
1606466a SM |
149 | to)) |
150 | ||
78629844 DK |
151 | |
152 | (defun query-replace-read-to (from string regexp-flag) | |
153 | "Query and return the `to' argument of a query-replace operation." | |
154 | (query-replace-compile-replacement | |
155 | (save-excursion | |
156 | (read-from-minibuffer | |
157 | (format "%s %s with: " string (query-replace-descr from)) | |
158 | nil nil nil | |
bbe9f175 | 159 | query-replace-to-history-variable from t t)) |
78629844 DK |
160 | regexp-flag)) |
161 | ||
1606466a SM |
162 | (defun query-replace-read-args (string regexp-flag &optional noerror) |
163 | (unless noerror | |
164 | (barf-if-buffer-read-only)) | |
165 | (let* ((from (query-replace-read-from string regexp-flag)) | |
6f1df6d9 SM |
166 | (to (if (consp from) (prog1 (cdr from) (setq from (car from))) |
167 | (query-replace-read-to from string regexp-flag)))) | |
10784bac | 168 | (list from to current-prefix-arg))) |
770970cb | 169 | |
47d72254 | 170 | (defun query-replace (from-string to-string &optional delimited start end) |
da44e784 RM |
171 | "Replace some occurrences of FROM-STRING with TO-STRING. |
172 | As each match is found, the user must type a character saying | |
173 | what to do with it. For directions, type \\[help-command] at that time. | |
174 | ||
7ef5c431 KH |
175 | In Transient Mark mode, if the mark is active, operate on the contents |
176 | of the region. Otherwise, operate from point to the end of the buffer. | |
177 | ||
151270f3 RS |
178 | If `query-replace-interactive' is non-nil, the last incremental search |
179 | string is used as FROM-STRING--you don't have to specify it with the | |
180 | minibuffer. | |
181 | ||
446d9629 RS |
182 | Matching is independent of case if `case-fold-search' is non-nil and |
183 | FROM-STRING has no uppercase letters. Replacement transfers the case | |
184 | pattern of the old text to the new text, if `case-replace' and | |
185 | `case-fold-search' are non-nil and FROM-STRING has no uppercase | |
186 | letters. \(Transferring the case pattern means that if the old text | |
187 | matched is all caps, or capitalized, then its replacement is upcased | |
188 | or capitalized.) | |
9b0bf2b6 | 189 | |
118a01c9 | 190 | Third arg DELIMITED (prefix arg if interactive), if non-nil, means replace |
81bdc14d | 191 | only matches surrounded by word boundaries. |
47d72254 | 192 | Fourth and fifth arg START and END specify the region to operate on. |
81bdc14d RS |
193 | |
194 | To customize possible responses, change the \"bindings\" in `query-replace-map'." | |
10784bac RS |
195 | (interactive (let ((common |
196 | (query-replace-read-args "Query replace" nil))) | |
197 | (list (nth 0 common) (nth 1 common) (nth 2 common) | |
198 | ;; These are done separately here | |
199 | ;; so that command-history will record these expressions | |
200 | ;; rather than the values they had this time. | |
201 | (if (and transient-mark-mode mark-active) | |
202 | (region-beginning)) | |
203 | (if (and transient-mark-mode mark-active) | |
204 | (region-end))))) | |
99a7559f | 205 | (perform-replace from-string to-string t nil delimited nil nil start end)) |
7ef5c431 | 206 | |
73fa8346 | 207 | (define-key esc-map "%" 'query-replace) |
da44e784 | 208 | |
47d72254 | 209 | (defun query-replace-regexp (regexp to-string &optional delimited start end) |
da44e784 RM |
210 | "Replace some things after point matching REGEXP with TO-STRING. |
211 | As each match is found, the user must type a character saying | |
212 | what to do with it. For directions, type \\[help-command] at that time. | |
213 | ||
7ef5c431 KH |
214 | In Transient Mark mode, if the mark is active, operate on the contents |
215 | of the region. Otherwise, operate from point to the end of the buffer. | |
216 | ||
151270f3 RS |
217 | If `query-replace-interactive' is non-nil, the last incremental search |
218 | regexp is used as REGEXP--you don't have to specify it with the | |
219 | minibuffer. | |
220 | ||
446d9629 RS |
221 | Matching is independent of case if `case-fold-search' is non-nil and |
222 | REGEXP has no uppercase letters. Replacement transfers the case | |
223 | pattern of the old text to the new text, if `case-replace' and | |
224 | `case-fold-search' are non-nil and REGEXP has no uppercase letters. | |
225 | \(Transferring the case pattern means that if the old text matched is | |
226 | all caps, or capitalized, then its replacement is upcased or | |
227 | capitalized.) | |
47d72254 | 228 | |
118a01c9 | 229 | Third arg DELIMITED (prefix arg if interactive), if non-nil, means replace |
da44e784 | 230 | only matches surrounded by word boundaries. |
47d72254 GM |
231 | Fourth and fifth arg START and END specify the region to operate on. |
232 | ||
118a01c9 RS |
233 | In TO-STRING, `\\&' stands for whatever matched the whole of REGEXP, |
234 | and `\\=\\N' (where N is a digit) stands for | |
2f57bf85 | 235 | whatever what matched the Nth `\\(...\\)' in REGEXP. |
7c1c02ac DK |
236 | `\\?' lets you edit the replacement text in the minibuffer |
237 | at the given position for each replacement. | |
238 | ||
ba8d15f9 RS |
239 | In interactive calls, the replacement text can contain `\\,' |
240 | followed by a Lisp expression. Each | |
241 | replacement evaluates that expression to compute the replacement | |
242 | string. Inside of that expression, `\\&' is a string denoting the | |
f1f6079c | 243 | whole match as a string, `\\N' for a partial match, `\\#&' and `\\#N' |
ba8d15f9 RS |
244 | for the whole or a partial match converted to a number with |
245 | `string-to-number', and `\\#' itself for the number of replacements | |
246 | done so far (starting with zero). | |
7c1c02ac | 247 | |
ba8d15f9 RS |
248 | If the replacement expression is a symbol, write a space after it |
249 | to terminate it. One space there, if any, will be discarded. | |
7c1c02ac DK |
250 | |
251 | When using those Lisp features interactively in the replacement | |
252 | text, TO-STRING is actually made a list instead of a string. | |
253 | Use \\[repeat-complex-command] after this command for details." | |
10784bac RS |
254 | (interactive |
255 | (let ((common | |
256 | (query-replace-read-args "Query replace regexp" t))) | |
7c1c02ac DK |
257 | (list (nth 0 common) (nth 1 common) (nth 2 common) |
258 | ;; These are done separately here | |
259 | ;; so that command-history will record these expressions | |
260 | ;; rather than the values they had this time. | |
261 | (if (and transient-mark-mode mark-active) | |
262 | (region-beginning)) | |
263 | (if (and transient-mark-mode mark-active) | |
264 | (region-end))))) | |
99a7559f | 265 | (perform-replace regexp to-string t t delimited nil nil start end)) |
2f57bf85 | 266 | |
cbc127de | 267 | (define-key esc-map [?\C-%] 'query-replace-regexp) |
da44e784 | 268 | |
47d72254 | 269 | (defun query-replace-regexp-eval (regexp to-expr &optional delimited start end) |
84482eb3 RS |
270 | "Replace some things after point matching REGEXP with the result of TO-EXPR. |
271 | As each match is found, the user must type a character saying | |
272 | what to do with it. For directions, type \\[help-command] at that time. | |
273 | ||
274 | TO-EXPR is a Lisp expression evaluated to compute each replacement. It may | |
275 | reference `replace-count' to get the number of replacements already made. | |
276 | If the result of TO-EXPR is not a string, it is converted to one using | |
277 | `prin1-to-string' with the NOESCAPE argument (which see). | |
278 | ||
279 | For convenience, when entering TO-EXPR interactively, you can use `\\&' or | |
653479ad AS |
280 | `\\0' to stand for whatever matched the whole of REGEXP, and `\\N' (where |
281 | N is a digit) to stand for whatever matched the Nth `\\(...\\)' in REGEXP. | |
84482eb3 | 282 | Use `\\#&' or `\\#N' if you want a number instead of a string. |
2f57bf85 | 283 | In interactive use, `\\#' in itself stands for `replace-count'. |
84482eb3 RS |
284 | |
285 | In Transient Mark mode, if the mark is active, operate on the contents | |
286 | of the region. Otherwise, operate from point to the end of the buffer. | |
287 | ||
288 | If `query-replace-interactive' is non-nil, the last incremental search | |
289 | regexp is used as REGEXP--you don't have to specify it with the | |
290 | minibuffer. | |
291 | ||
292 | Preserves case in each replacement if `case-replace' and `case-fold-search' | |
293 | are non-nil and REGEXP has no uppercase letters. | |
47d72254 | 294 | |
84482eb3 | 295 | Third arg DELIMITED (prefix arg if interactive), if non-nil, means replace |
470bbe9b | 296 | only matches that are surrounded by word boundaries. |
47d72254 | 297 | Fourth and fifth arg START and END specify the region to operate on." |
84482eb3 | 298 | (interactive |
cc0aea1a | 299 | (progn |
6f1df6d9 SM |
300 | (barf-if-buffer-read-only) |
301 | (let* ((from | |
302 | ;; Let-bind the history var to disable the "foo -> bar" default. | |
303 | ;; Maybe we shouldn't disable this default, but for now I'll | |
304 | ;; leave it off. --Stef | |
305 | (let ((query-replace-to-history-variable nil)) | |
306 | (query-replace-read-from "Query replace regexp" t))) | |
5291cbca | 307 | (to (list (read-from-minibuffer |
6f1df6d9 SM |
308 | (format "Query replace regexp %s with eval: " |
309 | (query-replace-descr from)) | |
5291cbca | 310 | nil nil t query-replace-to-history-variable from t)))) |
84482eb3 RS |
311 | ;; We make TO a list because replace-match-string-symbols requires one, |
312 | ;; and the user might enter a single token. | |
313 | (replace-match-string-symbols to) | |
10784bac RS |
314 | (list from (car to) current-prefix-arg |
315 | (if (and transient-mark-mode mark-active) | |
316 | (region-beginning)) | |
317 | (if (and transient-mark-mode mark-active) | |
cc0aea1a | 318 | (region-end)))))) |
d2ce3151 | 319 | (perform-replace regexp (cons 'replace-eval-replacement to-expr) |
d83a97ab | 320 | t 'literal delimited nil nil start end)) |
84482eb3 | 321 | |
47d72254 | 322 | (defun map-query-replace-regexp (regexp to-strings &optional n start end) |
da44e784 | 323 | "Replace some matches for REGEXP with various strings, in rotation. |
e730be7f DL |
324 | The second argument TO-STRINGS contains the replacement strings, |
325 | separated by spaces. Third arg DELIMITED (prefix arg if interactive), | |
326 | if non-nil, means replace only matches surrounded by word boundaries. | |
327 | This command works like `query-replace-regexp' except that each | |
328 | successive replacement uses the next successive replacement string, | |
da44e784 RM |
329 | wrapping around from the last such string to the first. |
330 | ||
7ef5c431 KH |
331 | In Transient Mark mode, if the mark is active, operate on the contents |
332 | of the region. Otherwise, operate from point to the end of the buffer. | |
333 | ||
da44e784 RM |
334 | Non-interactively, TO-STRINGS may be a list of replacement strings. |
335 | ||
151270f3 RS |
336 | If `query-replace-interactive' is non-nil, the last incremental search |
337 | regexp is used as REGEXP--you don't have to specify it with the minibuffer. | |
338 | ||
da44e784 | 339 | A prefix argument N says to use each replacement string N times |
47d72254 GM |
340 | before rotating to the next. |
341 | Fourth and fifth arg START and END specify the region to operate on." | |
770970cb | 342 | (interactive |
5291cbca | 343 | (let* ((from (if query-replace-interactive |
151270f3 RS |
344 | (car regexp-search-ring) |
345 | (read-from-minibuffer "Map query replace (regexp): " | |
346 | nil nil nil | |
fce31d51 | 347 | 'query-replace-history nil t))) |
5291cbca | 348 | (to (read-from-minibuffer |
770970cb | 349 | (format "Query replace %s with (space-separated strings): " |
6f1df6d9 | 350 | (query-replace-descr from)) |
770970cb | 351 | nil nil nil |
5291cbca | 352 | 'query-replace-history from t))) |
2f2f7e58 RS |
353 | (list from to |
354 | (and current-prefix-arg | |
355 | (prefix-numeric-value current-prefix-arg)) | |
10784bac RS |
356 | (if (and transient-mark-mode mark-active) |
357 | (region-beginning)) | |
358 | (if (and transient-mark-mode mark-active) | |
359 | (region-end))))) | |
da44e784 RM |
360 | (let (replacements) |
361 | (if (listp to-strings) | |
362 | (setq replacements to-strings) | |
363 | (while (/= (length to-strings) 0) | |
364 | (if (string-match " " to-strings) | |
365 | (setq replacements | |
366 | (append replacements | |
367 | (list (substring to-strings 0 | |
368 | (string-match " " to-strings)))) | |
369 | to-strings (substring to-strings | |
370 | (1+ (string-match " " to-strings)))) | |
371 | (setq replacements (append replacements (list to-strings)) | |
372 | to-strings "")))) | |
99a7559f | 373 | (perform-replace regexp replacements t t nil n nil start end))) |
da44e784 | 374 | |
47d72254 | 375 | (defun replace-string (from-string to-string &optional delimited start end) |
da44e784 RM |
376 | "Replace occurrences of FROM-STRING with TO-STRING. |
377 | Preserve case in each match if `case-replace' and `case-fold-search' | |
378 | are non-nil and FROM-STRING has no uppercase letters. | |
9b0bf2b6 RS |
379 | \(Preserving case means that if the string matched is all caps, or capitalized, |
380 | then its replacement is upcased or capitalized.) | |
381 | ||
7ef5c431 KH |
382 | In Transient Mark mode, if the mark is active, operate on the contents |
383 | of the region. Otherwise, operate from point to the end of the buffer. | |
384 | ||
118a01c9 | 385 | Third arg DELIMITED (prefix arg if interactive), if non-nil, means replace |
da44e784 | 386 | only matches surrounded by word boundaries. |
47d72254 | 387 | Fourth and fifth arg START and END specify the region to operate on. |
da44e784 | 388 | |
151270f3 RS |
389 | If `query-replace-interactive' is non-nil, the last incremental search |
390 | string is used as FROM-STRING--you don't have to specify it with the | |
391 | minibuffer. | |
392 | ||
da44e784 RM |
393 | This function is usually the wrong thing to use in a Lisp program. |
394 | What you probably want is a loop like this: | |
118a01c9 RS |
395 | (while (search-forward FROM-STRING nil t) |
396 | (replace-match TO-STRING nil t)) | |
87532fbe RS |
397 | which will run faster and will not set the mark or print anything. |
398 | \(You may need a more complex loop if FROM-STRING can match the null string | |
399 | and TO-STRING is also null.)" | |
10784bac RS |
400 | (interactive |
401 | (let ((common | |
402 | (query-replace-read-args "Replace string" nil))) | |
403 | (list (nth 0 common) (nth 1 common) (nth 2 common) | |
404 | (if (and transient-mark-mode mark-active) | |
405 | (region-beginning)) | |
406 | (if (and transient-mark-mode mark-active) | |
407 | (region-end))))) | |
99a7559f | 408 | (perform-replace from-string to-string nil nil delimited nil nil start end)) |
da44e784 | 409 | |
47d72254 | 410 | (defun replace-regexp (regexp to-string &optional delimited start end) |
da44e784 | 411 | "Replace things after point matching REGEXP with TO-STRING. |
118a01c9 | 412 | Preserve case in each match if `case-replace' and `case-fold-search' |
da44e784 | 413 | are non-nil and REGEXP has no uppercase letters. |
47d72254 GM |
414 | |
415 | In Transient Mark mode, if the mark is active, operate on the contents | |
416 | of the region. Otherwise, operate from point to the end of the buffer. | |
417 | ||
118a01c9 | 418 | Third arg DELIMITED (prefix arg if interactive), if non-nil, means replace |
da44e784 | 419 | only matches surrounded by word boundaries. |
47d72254 GM |
420 | Fourth and fifth arg START and END specify the region to operate on. |
421 | ||
118a01c9 RS |
422 | In TO-STRING, `\\&' stands for whatever matched the whole of REGEXP, |
423 | and `\\=\\N' (where N is a digit) stands for | |
7c1c02ac DK |
424 | whatever what matched the Nth `\\(...\\)' in REGEXP. |
425 | `\\?' lets you edit the replacement text in the minibuffer | |
426 | at the given position for each replacement. | |
427 | ||
428 | In interactive calls, the replacement text may contain `\\,' | |
429 | followed by a Lisp expression used as part of the replacement | |
430 | text. Inside of that expression, `\\&' is a string denoting the | |
431 | whole match, `\\N' a partial matches, `\\#&' and `\\#N' the | |
432 | respective numeric values from `string-to-number', and `\\#' | |
433 | itself for `replace-count', the number of replacements occured so | |
434 | far. | |
435 | ||
436 | If your Lisp expression is an identifier and the next letter in | |
437 | the replacement string would be interpreted as part of it, you | |
438 | can wrap it with an expression like `\\,(or \\#)'. Incidentally, | |
439 | for this particular case you may also enter `\\#' in the | |
440 | replacement text directly. | |
441 | ||
442 | When using those Lisp features interactively in the replacement | |
443 | text, TO-STRING is actually made a list instead of a string. | |
444 | Use \\[repeat-complex-command] after this command for details. | |
da44e784 | 445 | |
151270f3 RS |
446 | If `query-replace-interactive' is non-nil, the last incremental search |
447 | regexp is used as REGEXP--you don't have to specify it with the minibuffer. | |
448 | ||
da44e784 RM |
449 | This function is usually the wrong thing to use in a Lisp program. |
450 | What you probably want is a loop like this: | |
451 | (while (re-search-forward REGEXP nil t) | |
118a01c9 | 452 | (replace-match TO-STRING nil nil)) |
da44e784 | 453 | which will run faster and will not set the mark or print anything." |
10784bac RS |
454 | (interactive |
455 | (let ((common | |
456 | (query-replace-read-args "Replace regexp" t))) | |
457 | (list (nth 0 common) (nth 1 common) (nth 2 common) | |
458 | (if (and transient-mark-mode mark-active) | |
459 | (region-beginning)) | |
460 | (if (and transient-mark-mode mark-active) | |
461 | (region-end))))) | |
99a7559f | 462 | (perform-replace regexp to-string nil t delimited nil nil start end)) |
e32eb3e6 | 463 | |
4c53bd2b RS |
464 | \f |
465 | (defvar regexp-history nil | |
466 | "History list for some commands that read regular expressions.") | |
da44e784 | 467 | |
e32eb3e6 | 468 | |
31e1d920 | 469 | (defalias 'delete-non-matching-lines 'keep-lines) |
e32eb3e6 GM |
470 | (defalias 'delete-matching-lines 'flush-lines) |
471 | (defalias 'count-matches 'how-many) | |
472 | ||
473 | ||
474 | (defun keep-lines-read-args (prompt) | |
475 | "Read arguments for `keep-lines' and friends. | |
476 | Prompt for a regexp with PROMPT. | |
2ced751f RS |
477 | Value is a list, (REGEXP)." |
478 | (list (read-from-minibuffer prompt nil nil nil | |
479 | 'regexp-history nil t))) | |
e32eb3e6 GM |
480 | |
481 | (defun keep-lines (regexp &optional rstart rend) | |
698e1804 RS |
482 | "Delete all lines except those containing matches for REGEXP. |
483 | A match split across lines preserves all the lines it lies in. | |
d2a0ee8b RS |
484 | Applies to all lines after point. |
485 | ||
486 | If REGEXP contains upper case characters (excluding those preceded by `\\'), | |
e32eb3e6 GM |
487 | the matching is case-sensitive. |
488 | ||
489 | Second and third arg RSTART and REND specify the region to operate on. | |
490 | ||
2ced751f RS |
491 | Interactively, in Transient Mark mode when the mark is active, operate |
492 | on the contents of the region. Otherwise, operate from point to the | |
493 | end of the buffer." | |
494 | ||
e32eb3e6 | 495 | (interactive |
98faf1bb RS |
496 | (progn |
497 | (barf-if-buffer-read-only) | |
498 | (keep-lines-read-args "Keep lines (containing match for regexp): "))) | |
e32eb3e6 | 499 | (if rstart |
119831da RS |
500 | (progn |
501 | (goto-char (min rstart rend)) | |
502 | (setq rend (copy-marker (max rstart rend)))) | |
2ced751f RS |
503 | (if (and transient-mark-mode mark-active) |
504 | (setq rstart (region-beginning) | |
505 | rend (copy-marker (region-end))) | |
506 | (setq rstart (point) | |
507 | rend (point-max-marker))) | |
508 | (goto-char rstart)) | |
698e1804 RS |
509 | (save-excursion |
510 | (or (bolp) (forward-line 1)) | |
d2a0ee8b RS |
511 | (let ((start (point)) |
512 | (case-fold-search (and case-fold-search | |
513 | (isearch-no-upper-case-p regexp t)))) | |
e32eb3e6 | 514 | (while (< (point) rend) |
698e1804 | 515 | ;; Start is first char not preserved by previous match. |
e32eb3e6 GM |
516 | (if (not (re-search-forward regexp rend 'move)) |
517 | (delete-region start rend) | |
698e1804 RS |
518 | (let ((end (save-excursion (goto-char (match-beginning 0)) |
519 | (beginning-of-line) | |
520 | (point)))) | |
521 | ;; Now end is first char preserved by the new match. | |
522 | (if (< start end) | |
523 | (delete-region start end)))) | |
d99118b0 | 524 | |
e32eb3e6 | 525 | (setq start (save-excursion (forward-line 1) (point))) |
698e1804 | 526 | ;; If the match was empty, avoid matching again at same place. |
e32eb3e6 GM |
527 | (and (< (point) rend) |
528 | (= (match-beginning 0) (match-end 0)) | |
698e1804 RS |
529 | (forward-char 1)))))) |
530 | ||
e32eb3e6 GM |
531 | |
532 | (defun flush-lines (regexp &optional rstart rend) | |
698e1804 RS |
533 | "Delete lines containing matches for REGEXP. |
534 | If a match is split across lines, all the lines it lies in are deleted. | |
d2a0ee8b RS |
535 | Applies to lines after point. |
536 | ||
537 | If REGEXP contains upper case characters (excluding those preceded by `\\'), | |
e32eb3e6 GM |
538 | the matching is case-sensitive. |
539 | ||
540 | Second and third arg RSTART and REND specify the region to operate on. | |
541 | ||
2ced751f RS |
542 | Interactively, in Transient Mark mode when the mark is active, operate |
543 | on the contents of the region. Otherwise, operate from point to the | |
544 | end of the buffer." | |
545 | ||
e32eb3e6 | 546 | (interactive |
98faf1bb RS |
547 | (progn |
548 | (barf-if-buffer-read-only) | |
549 | (keep-lines-read-args "Flush lines (containing match for regexp): "))) | |
e32eb3e6 | 550 | (if rstart |
119831da RS |
551 | (progn |
552 | (goto-char (min rstart rend)) | |
553 | (setq rend (copy-marker (max rstart rend)))) | |
2ced751f RS |
554 | (if (and transient-mark-mode mark-active) |
555 | (setq rstart (region-beginning) | |
556 | rend (copy-marker (region-end))) | |
557 | (setq rstart (point) | |
558 | rend (point-max-marker))) | |
559 | (goto-char rstart)) | |
d2a0ee8b RS |
560 | (let ((case-fold-search (and case-fold-search |
561 | (isearch-no-upper-case-p regexp t)))) | |
562 | (save-excursion | |
e32eb3e6 GM |
563 | (while (and (< (point) rend) |
564 | (re-search-forward regexp rend t)) | |
d2a0ee8b RS |
565 | (delete-region (save-excursion (goto-char (match-beginning 0)) |
566 | (beginning-of-line) | |
567 | (point)) | |
568 | (progn (forward-line 1) (point))))))) | |
698e1804 | 569 | |
e32eb3e6 GM |
570 | |
571 | (defun how-many (regexp &optional rstart rend) | |
d2a0ee8b RS |
572 | "Print number of matches for REGEXP following point. |
573 | ||
574 | If REGEXP contains upper case characters (excluding those preceded by `\\'), | |
e32eb3e6 GM |
575 | the matching is case-sensitive. |
576 | ||
577 | Second and third arg RSTART and REND specify the region to operate on. | |
578 | ||
2ced751f RS |
579 | Interactively, in Transient Mark mode when the mark is active, operate |
580 | on the contents of the region. Otherwise, operate from point to the | |
581 | end of the buffer." | |
582 | ||
e32eb3e6 GM |
583 | (interactive |
584 | (keep-lines-read-args "How many matches for (regexp): ")) | |
f601efb0 SM |
585 | (save-excursion |
586 | (if rstart | |
587 | (goto-char (min rstart rend)) | |
2ced751f RS |
588 | (if (and transient-mark-mode mark-active) |
589 | (setq rstart (region-beginning) | |
590 | rend (copy-marker (region-end))) | |
591 | (setq rstart (point) | |
592 | rend (point-max-marker))) | |
593 | (goto-char rstart)) | |
f601efb0 SM |
594 | (let ((count 0) |
595 | opoint | |
596 | (case-fold-search (and case-fold-search | |
597 | (isearch-no-upper-case-p regexp t)))) | |
598 | (while (and (< (point) rend) | |
599 | (progn (setq opoint (point)) | |
600 | (re-search-forward regexp rend t))) | |
601 | (if (= opoint (point)) | |
602 | (forward-char 1) | |
603 | (setq count (1+ count)))) | |
604 | (message "%d occurrences" count)))) | |
e32eb3e6 | 605 | |
4c53bd2b | 606 | \f |
f601efb0 SM |
607 | (defvar occur-mode-map |
608 | (let ((map (make-sparse-keymap))) | |
609 | (define-key map [mouse-2] 'occur-mode-mouse-goto) | |
610 | (define-key map "\C-c\C-c" 'occur-mode-goto-occurrence) | |
611 | (define-key map "\C-m" 'occur-mode-goto-occurrence) | |
daae70bf | 612 | (define-key map "o" 'occur-mode-goto-occurrence-other-window) |
365486d6 | 613 | (define-key map "\C-o" 'occur-mode-display-occurrence) |
f601efb0 SM |
614 | (define-key map "\M-n" 'occur-next) |
615 | (define-key map "\M-p" 'occur-prev) | |
d99118b0 SS |
616 | (define-key map "r" 'occur-rename-buffer) |
617 | (define-key map "c" 'clone-buffer) | |
f601efb0 | 618 | (define-key map "g" 'revert-buffer) |
d99118b0 SS |
619 | (define-key map "q" 'quit-window) |
620 | (define-key map "z" 'kill-this-buffer) | |
b938735a | 621 | (define-key map "\C-c\C-f" 'next-error-follow-minor-mode) |
f601efb0 SM |
622 | map) |
623 | "Keymap for `occur-mode'.") | |
698e1804 | 624 | |
46b3d18e RS |
625 | (defvar occur-revert-arguments nil |
626 | "Arguments to pass to `occur-1' to revert an Occur mode buffer. | |
627 | See `occur-revert-function'.") | |
698e1804 | 628 | |
c9ae8cbb JB |
629 | (defcustom occur-mode-hook '(turn-on-font-lock) |
630 | "Hook run when entering Occur mode." | |
631 | :type 'hook | |
632 | :group 'matching) | |
633 | ||
634 | (defcustom occur-hook nil | |
635 | "Hook run when `occur' is called." | |
daae70bf CW |
636 | :type 'hook |
637 | :group 'matching) | |
638 | ||
de3c9b09 | 639 | (put 'occur-mode 'mode-class 'special) |
505847d4 | 640 | (defun occur-mode () |
698e1804 | 641 | "Major mode for output from \\[occur]. |
0081c8a1 RS |
642 | \\<occur-mode-map>Move point to one of the items in this buffer, then use |
643 | \\[occur-mode-goto-occurrence] to go to the occurrence that the item refers to. | |
644 | Alternatively, click \\[occur-mode-mouse-goto] on an item to go to it. | |
645 | ||
698e1804 | 646 | \\{occur-mode-map}" |
ce8b7b9f | 647 | (interactive) |
505847d4 RS |
648 | (kill-all-local-variables) |
649 | (use-local-map occur-mode-map) | |
650 | (setq major-mode 'occur-mode) | |
651 | (setq mode-name "Occur") | |
f601efb0 | 652 | (set (make-local-variable 'revert-buffer-function) 'occur-revert-function) |
c9ae8cbb | 653 | (make-local-variable 'occur-revert-arguments) |
fbefbe33 | 654 | (add-hook 'change-major-mode-hook 'font-lock-defontify nil t) |
423e4de7 | 655 | (setq next-error-function 'occur-next-error) |
c9ae8cbb | 656 | (run-hooks 'occur-mode-hook)) |
698e1804 | 657 | |
a41284da | 658 | (defun occur-revert-function (ignore1 ignore2) |
46b3d18e | 659 | "Handle `revert-buffer' for Occur mode buffers." |
e1690783 | 660 | (apply 'occur-1 (append occur-revert-arguments (list (buffer-name))))) |
a41284da | 661 | |
78bead73 RS |
662 | (defun occur-mode-mouse-goto (event) |
663 | "In Occur mode, go to the occurrence whose line you click on." | |
664 | (interactive "e") | |
46b3d18e | 665 | (let (pos) |
78bead73 RS |
666 | (save-excursion |
667 | (set-buffer (window-buffer (posn-window (event-end event)))) | |
668 | (save-excursion | |
669 | (goto-char (posn-point (event-end event))) | |
46b3d18e RS |
670 | (setq pos (occur-mode-find-occurrence)))) |
671 | (pop-to-buffer (marker-buffer pos)) | |
672 | (goto-char pos))) | |
78bead73 RS |
673 | |
674 | (defun occur-mode-find-occurrence () | |
46b3d18e RS |
675 | (let ((pos (get-text-property (point) 'occur-target))) |
676 | (unless pos | |
68608d9c | 677 | (error "No occurrence on this line")) |
46b3d18e RS |
678 | (unless (buffer-live-p (marker-buffer pos)) |
679 | (error "Buffer for this occurrence was killed")) | |
680 | pos)) | |
78bead73 RS |
681 | |
682 | (defun occur-mode-goto-occurrence () | |
683 | "Go to the occurrence the current line describes." | |
684 | (interactive) | |
46b3d18e RS |
685 | (let ((pos (occur-mode-find-occurrence))) |
686 | (pop-to-buffer (marker-buffer pos)) | |
687 | (goto-char pos))) | |
8d15583f | 688 | |
029024e2 RS |
689 | (defun occur-mode-goto-occurrence-other-window () |
690 | "Go to the occurrence the current line describes, in another window." | |
691 | (interactive) | |
46b3d18e RS |
692 | (let ((pos (occur-mode-find-occurrence))) |
693 | (switch-to-buffer-other-window (marker-buffer pos)) | |
694 | (goto-char pos))) | |
029024e2 | 695 | |
365486d6 RS |
696 | (defun occur-mode-display-occurrence () |
697 | "Display in another window the occurrence the current line describes." | |
698 | (interactive) | |
46b3d18e RS |
699 | (let ((pos (occur-mode-find-occurrence)) |
700 | window | |
701 | ;; Bind these to ensure `display-buffer' puts it in another window. | |
365486d6 | 702 | same-window-buffer-names |
46b3d18e RS |
703 | same-window-regexps) |
704 | (setq window (display-buffer (marker-buffer pos))) | |
365486d6 RS |
705 | ;; This is the way to set point in the proper window. |
706 | (save-selected-window | |
707 | (select-window window) | |
46b3d18e | 708 | (goto-char pos)))) |
365486d6 | 709 | |
123d5548 | 710 | (defun occur-find-match (n search message) |
8d15583f RS |
711 | (if (not n) (setq n 1)) |
712 | (let ((r)) | |
713 | (while (> n 0) | |
123d5548 JB |
714 | (setq r (funcall search (point) 'occur-match)) |
715 | (and r | |
716 | (get-text-property r 'occur-match) | |
717 | (setq r (funcall search r 'occur-match))) | |
8d15583f | 718 | (if r |
123d5548 JB |
719 | (goto-char r) |
720 | (error message)) | |
8d15583f RS |
721 | (setq n (1- n))))) |
722 | ||
123d5548 JB |
723 | (defun occur-next (&optional n) |
724 | "Move to the Nth (default 1) next match in an Occur mode buffer." | |
725 | (interactive "p") | |
726 | (occur-find-match n #'next-single-property-change "No more matches")) | |
727 | ||
8d15583f | 728 | (defun occur-prev (&optional n) |
46b3d18e | 729 | "Move to the Nth (default 1) previous match in an Occur mode buffer." |
8d15583f | 730 | (interactive "p") |
123d5548 | 731 | (occur-find-match n #'previous-single-property-change "No earlier matches")) |
423e4de7 KS |
732 | |
733 | (defun occur-next-error (&optional argp reset) | |
734 | "Move to the Nth (default 1) next match in an Occur mode buffer. | |
735 | Compatibility function for \\[next-error] invocations." | |
736 | (interactive "p") | |
5f9e0ca5 | 737 | ;; we need to run occur-find-match from within the Occur buffer |
f1f007dc | 738 | (with-current-buffer |
5f9e0ca5 TZ |
739 | (if (next-error-buffer-p (current-buffer)) |
740 | (current-buffer) | |
741 | (next-error-find-buffer nil nil (lambda() (eq major-mode 'occur-mode)))) | |
f1f007dc JL |
742 | |
743 | (goto-char (cond (reset (point-min)) | |
744 | ((< argp 0) (line-beginning-position)) | |
745 | ((line-end-position)))) | |
5f9e0ca5 | 746 | (occur-find-match |
f1f007dc JL |
747 | (abs argp) |
748 | (if (> 0 argp) | |
5f9e0ca5 TZ |
749 | #'previous-single-property-change |
750 | #'next-single-property-change) | |
751 | "No more matches") | |
752 | ;; In case the *Occur* buffer is visible in a nonselected window. | |
753 | (set-window-point (get-buffer-window (current-buffer)) (point)) | |
754 | (occur-mode-goto-occurrence))) | |
4c53bd2b | 755 | \f |
aaaecfcd JL |
756 | (defface match |
757 | '((((class color) (min-colors 88) (background light)) | |
758 | :background "Tan") | |
759 | (((class color) (min-colors 88) (background dark)) | |
760 | :background "RoyalBlue4") | |
761 | (((class color) (min-colors 8)) | |
762 | :background "blue" :foreground "white") | |
763 | (((type tty) (class mono)) | |
764 | :inverse-video t) | |
765 | (t :background "gray")) | |
766 | "Face used to highlight matches permanently." | |
767 | :group 'matching | |
768 | :version "21.4") | |
769 | ||
9d325ebf | 770 | (defcustom list-matching-lines-default-context-lines 0 |
e730be7f DL |
771 | "*Default number of context lines included around `list-matching-lines' matches. |
772 | A negative number means to include that many lines before the match. | |
9d325ebf RS |
773 | A positive number means to include that many lines both before and after." |
774 | :type 'integer | |
775 | :group 'matching) | |
698e1804 | 776 | |
31e1d920 | 777 | (defalias 'list-matching-lines 'occur) |
698e1804 | 778 | |
aaaecfcd | 779 | (defcustom list-matching-lines-face 'match |
e730be7f | 780 | "*Face used by \\[list-matching-lines] to show the text that matches. |
68608d9c CW |
781 | If the value is nil, don't highlight the matching portions specially." |
782 | :type 'face | |
783 | :group 'matching) | |
784 | ||
785 | (defcustom list-matching-lines-buffer-name-face 'underline | |
786 | "*Face used by \\[list-matching-lines] to show the names of buffers. | |
787 | If the value is nil, don't highlight the buffer names specially." | |
788 | :type 'face | |
789 | :group 'matching) | |
790 | ||
2f4e3f7a | 791 | (defun occur-accumulate-lines (count &optional keep-props) |
68608d9c CW |
792 | (save-excursion |
793 | (let ((forwardp (> count 0)) | |
f1f007dc | 794 | result beg end) |
68608d9c CW |
795 | (while (not (or (zerop count) |
796 | (if forwardp | |
797 | (eobp) | |
798 | (bobp)))) | |
b0523087 | 799 | (setq count (+ count (if forwardp -1 1))) |
f1f007dc JL |
800 | (setq beg (line-beginning-position) |
801 | end (line-end-position)) | |
2c13e47b | 802 | (if (and keep-props (boundp 'jit-lock-mode) jit-lock-mode |
f1f007dc | 803 | (text-property-not-all beg end 'fontified t)) |
2c13e47b | 804 | (jit-lock-fontify-now beg end)) |
68608d9c | 805 | (push |
2f4e3f7a RS |
806 | (funcall (if keep-props |
807 | #'buffer-substring | |
808 | #'buffer-substring-no-properties) | |
f1f007dc | 809 | beg end) |
68608d9c CW |
810 | result) |
811 | (forward-line (if forwardp 1 -1))) | |
812 | (nreverse result)))) | |
813 | ||
814 | (defun occur-read-primary-args () | |
815 | (list (let* ((default (car regexp-history)) | |
816 | (input | |
817 | (read-from-minibuffer | |
818 | (if default | |
819 | (format "List lines matching regexp (default `%s'): " | |
6f1df6d9 | 820 | (query-replace-descr default)) |
68608d9c CW |
821 | "List lines matching regexp: ") |
822 | nil | |
823 | nil | |
824 | nil | |
442a68c7 RS |
825 | 'regexp-history |
826 | default))) | |
68608d9c CW |
827 | (if (equal input "") |
828 | default | |
829 | input)) | |
bda1925e CW |
830 | (when current-prefix-arg |
831 | (prefix-numeric-value current-prefix-arg)))) | |
c9daced0 | 832 | |
d99118b0 SS |
833 | (defun occur-rename-buffer (&optional unique-p) |
834 | "Rename the current *Occur* buffer to *Occur: original-buffer-name*. | |
835 | Here `original-buffer-name' is the buffer name were occur was originally run. | |
836 | When given the prefix argument, the renaming will not clobber the existing | |
837 | buffer(s) of that name, but use `generate-new-buffer-name' instead. | |
2fb79329 | 838 | You can add this to `occur-hook' if you always want a separate *Occur* |
d99118b0 SS |
839 | buffer for each buffer where you invoke `occur'." |
840 | (interactive "P") | |
841 | (with-current-buffer | |
842 | (if (eq major-mode 'occur-mode) (current-buffer) (get-buffer "*Occur*")) | |
843 | (rename-buffer (concat "*Occur: " | |
844 | (mapconcat #'buffer-name | |
845 | (car (cddr occur-revert-arguments)) "/") | |
846 | "*") | |
847 | unique-p))) | |
848 | ||
698e1804 | 849 | (defun occur (regexp &optional nlines) |
99976f85 | 850 | "Show all lines in the current buffer containing a match for REGEXP. |
da44e784 RM |
851 | |
852 | If a match spreads across multiple lines, all those lines are shown. | |
698e1804 | 853 | |
da44e784 RM |
854 | Each line is displayed with NLINES lines before and after, or -NLINES |
855 | before if NLINES is negative. | |
856 | NLINES defaults to `list-matching-lines-default-context-lines'. | |
698e1804 RS |
857 | Interactively it is the prefix arg. |
858 | ||
4c53bd2b | 859 | The lines are shown in a buffer named `*Occur*'. |
698e1804 | 860 | It serves as a menu to find any of the occurrences in this buffer. |
de3c9b09 | 861 | \\<occur-mode-map>\\[describe-mode] in that buffer will explain how. |
9483d601 | 862 | |
de3c9b09 RS |
863 | If REGEXP contains upper case characters (excluding those preceded by `\\'), |
864 | the matching is case-sensitive." | |
68608d9c CW |
865 | (interactive (occur-read-primary-args)) |
866 | (occur-1 regexp nlines (list (current-buffer)))) | |
867 | ||
868 | (defun multi-occur (bufs regexp &optional nlines) | |
869 | "Show all lines in buffers BUFS containing a match for REGEXP. | |
870 | This function acts on multiple buffers; otherwise, it is exactly like | |
871 | `occur'." | |
a5dfed3e | 872 | (interactive |
68608d9c | 873 | (cons |
52698d45 KS |
874 | (let* ((bufs (list (read-buffer "First buffer to search: " |
875 | (current-buffer) t))) | |
876 | (buf nil) | |
877 | (ido-ignore-item-temp-list bufs)) | |
68608d9c | 878 | (while (not (string-equal |
f1180544 | 879 | (setq buf (read-buffer |
52698d45 KS |
880 | (if (eq read-buffer-function 'ido-read-buffer) |
881 | "Next buffer to search (C-j to end): " | |
882 | "Next buffer to search (RET to end): ") | |
883 | nil t)) | |
68608d9c | 884 | "")) |
52698d45 KS |
885 | (add-to-list 'bufs buf) |
886 | (setq ido-ignore-item-temp-list bufs)) | |
68608d9c CW |
887 | (nreverse (mapcar #'get-buffer bufs))) |
888 | (occur-read-primary-args))) | |
889 | (occur-1 regexp nlines bufs)) | |
890 | ||
891 | (defun multi-occur-by-filename-regexp (bufregexp regexp &optional nlines) | |
04fe158a | 892 | "Show all lines matching REGEXP in buffers named by BUFREGEXP. |
68608d9c CW |
893 | See also `multi-occur'." |
894 | (interactive | |
895 | (cons | |
896 | (let* ((default (car regexp-history)) | |
897 | (input | |
898 | (read-from-minibuffer | |
899 | "List lines in buffers whose filename matches regexp: " | |
900 | nil | |
901 | nil | |
902 | nil | |
903 | 'regexp-history))) | |
904 | (if (equal input "") | |
905 | default | |
906 | input)) | |
907 | (occur-read-primary-args))) | |
908 | (when bufregexp | |
909 | (occur-1 regexp nlines | |
910 | (delq nil | |
911 | (mapcar (lambda (buf) | |
912 | (when (and (buffer-file-name buf) | |
913 | (string-match bufregexp | |
914 | (buffer-file-name buf))) | |
915 | buf)) | |
916 | (buffer-list)))))) | |
917 | ||
e1690783 CW |
918 | (defun occur-1 (regexp nlines bufs &optional buf-name) |
919 | (unless buf-name | |
920 | (setq buf-name "*Occur*")) | |
921 | (let ((occur-buf (get-buffer-create buf-name)) | |
70ed2a76 CW |
922 | (made-temp-buf nil) |
923 | (active-bufs (delq nil (mapcar #'(lambda (buf) | |
924 | (when (buffer-live-p buf) buf)) | |
925 | bufs)))) | |
926 | ;; Handle the case where one of the buffers we're searching is the | |
927 | ;; *Occur* buffer itself. | |
928 | (when (memq occur-buf bufs) | |
929 | (setq occur-buf (with-current-buffer occur-buf | |
930 | (clone-buffer "*Occur-temp*")) | |
931 | made-temp-buf t)) | |
68608d9c CW |
932 | (with-current-buffer occur-buf |
933 | (setq buffer-read-only nil) | |
934 | (occur-mode) | |
935 | (erase-buffer) | |
936 | (let ((count (occur-engine | |
70ed2a76 | 937 | regexp active-bufs occur-buf |
68608d9c CW |
938 | (or nlines list-matching-lines-default-context-lines) |
939 | (and case-fold-search | |
940 | (isearch-no-upper-case-p regexp t)) | |
506a6d7e | 941 | list-matching-lines-buffer-name-face |
2f4e3f7a | 942 | nil list-matching-lines-face t))) |
b2043e7b AS |
943 | (let* ((bufcount (length active-bufs)) |
944 | (diff (- (length bufs) bufcount))) | |
945 | (message "Searched %d buffer%s%s; %s match%s for `%s'" | |
946 | bufcount (if (= bufcount 1) "" "s") | |
947 | (if (zerop diff) "" (format " (%d killed)" diff)) | |
948 | (if (zerop count) "no" (format "%d" count)) | |
949 | (if (= count 1) "" "es") | |
950 | regexp)) | |
70ed2a76 CW |
951 | ;; If we had to make a temporary buffer, make it the *Occur* |
952 | ;; buffer now. | |
953 | (when made-temp-buf | |
e1690783 CW |
954 | (with-current-buffer (get-buffer buf-name) |
955 | (kill-buffer (current-buffer))) | |
956 | (rename-buffer buf-name)) | |
70ed2a76 CW |
957 | (setq occur-revert-arguments (list regexp nlines bufs) |
958 | buffer-read-only t) | |
68608d9c | 959 | (if (> count 0) |
423e4de7 KS |
960 | (progn |
961 | (display-buffer occur-buf) | |
962 | (setq next-error-last-buffer occur-buf)) | |
2fb79329 JB |
963 | (kill-buffer occur-buf))) |
964 | (run-hooks 'occur-hook)))) | |
68608d9c | 965 | |
46b3d18e RS |
966 | (defun occur-engine-add-prefix (lines) |
967 | (mapcar | |
968 | #'(lambda (line) | |
261cca88 | 969 | (concat " :" line "\n")) |
46b3d18e RS |
970 | lines)) |
971 | ||
972 | (defun occur-engine (regexp buffers out-buf nlines case-fold-search | |
973 | title-face prefix-face match-face keep-props) | |
974 | (with-current-buffer out-buf | |
975 | (setq buffer-read-only nil) | |
5cb4031d KH |
976 | (let ((globalcount 0) |
977 | (coding nil)) | |
46b3d18e RS |
978 | ;; Map over all the buffers |
979 | (dolist (buf buffers) | |
980 | (when (buffer-live-p buf) | |
981 | (let ((matches 0) ;; count of matched lines | |
982 | (lines 1) ;; line count | |
983 | (matchbeg 0) | |
46b3d18e RS |
984 | (origpt nil) |
985 | (begpt nil) | |
986 | (endpt nil) | |
987 | (marker nil) | |
988 | (curstring "") | |
989 | (headerpt (with-current-buffer out-buf (point)))) | |
990 | (save-excursion | |
991 | (set-buffer buf) | |
5cb4031d KH |
992 | (or coding |
993 | ;; Set CODING only if the current buffer locally | |
994 | ;; binds buffer-file-coding-system. | |
995 | (not (local-variable-p 'buffer-file-coding-system)) | |
996 | (setq coding buffer-file-coding-system)) | |
68608d9c | 997 | (save-excursion |
46b3d18e RS |
998 | (goto-char (point-min)) ;; begin searching in the buffer |
999 | (while (not (eobp)) | |
1000 | (setq origpt (point)) | |
1001 | (when (setq endpt (re-search-forward regexp nil t)) | |
1002 | (setq matches (1+ matches)) ;; increment match count | |
5291cbca | 1003 | (setq matchbeg (match-beginning 0)) |
46b3d18e | 1004 | (setq lines (+ lines (1- (count-lines origpt endpt)))) |
f1f007dc JL |
1005 | (save-excursion |
1006 | (goto-char matchbeg) | |
1007 | (setq begpt (line-beginning-position) | |
1008 | endpt (line-end-position))) | |
46b3d18e RS |
1009 | (setq marker (make-marker)) |
1010 | (set-marker marker matchbeg) | |
2c13e47b | 1011 | (if (and keep-props (boundp 'jit-lock-mode) jit-lock-mode |
f1f007dc | 1012 | (text-property-not-all begpt endpt 'fontified t)) |
2c13e47b | 1013 | (jit-lock-fontify-now begpt endpt)) |
f1f007dc | 1014 | (setq curstring (buffer-substring begpt endpt)) |
46b3d18e RS |
1015 | ;; Depropertize the string, and maybe |
1016 | ;; highlight the matches | |
1017 | (let ((len (length curstring)) | |
1018 | (start 0)) | |
1019 | (unless keep-props | |
1020 | (set-text-properties 0 len nil curstring)) | |
1021 | (while (and (< start len) | |
1022 | (string-match regexp curstring start)) | |
f1f007dc JL |
1023 | (add-text-properties |
1024 | (match-beginning 0) (match-end 0) | |
1025 | (append | |
1026 | `(occur-match t) | |
1027 | (when match-face | |
1028 | ;; Use `face' rather than `font-lock-face' here | |
1029 | ;; so as to override faces copied from the buffer. | |
1030 | `(face ,match-face))) | |
1031 | curstring) | |
46b3d18e RS |
1032 | (setq start (match-end 0)))) |
1033 | ;; Generate the string to insert for this match | |
1034 | (let* ((out-line | |
1035 | (concat | |
261cca88 RS |
1036 | ;; Using 7 digits aligns tabs properly. |
1037 | (apply #'propertize (format "%7d:" lines) | |
46b3d18e RS |
1038 | (append |
1039 | (when prefix-face | |
506a6d7e | 1040 | `(font-lock-face prefix-face)) |
46b3d18e | 1041 | '(occur-prefix t))) |
f1f007dc JL |
1042 | ;; We don't put `mouse-face' on the newline, |
1043 | ;; because that loses. And don't put it | |
1044 | ;; on context lines to reduce flicker. | |
1045 | (propertize curstring 'mouse-face 'highlight) | |
46b3d18e RS |
1046 | "\n")) |
1047 | (data | |
1048 | (if (= nlines 0) | |
1049 | ;; The simple display style | |
1050 | out-line | |
1051 | ;; The complex multi-line display | |
1052 | ;; style. Generate a list of lines, | |
1053 | ;; concatenate them all together. | |
1054 | (apply #'concat | |
1055 | (nconc | |
f22a7406 | 1056 | (occur-engine-add-prefix (nreverse (cdr (occur-accumulate-lines (- (1+ (abs nlines))) keep-props)))) |
46b3d18e | 1057 | (list out-line) |
f22a7406 RS |
1058 | (if (> nlines 0) |
1059 | (occur-engine-add-prefix | |
1060 | (cdr (occur-accumulate-lines (1+ nlines) keep-props))))))))) | |
46b3d18e RS |
1061 | ;; Actually insert the match display data |
1062 | (with-current-buffer out-buf | |
1063 | (let ((beg (point)) | |
1064 | (end (progn (insert data) (point)))) | |
1065 | (unless (= nlines 0) | |
1066 | (insert "-------\n")) | |
1067 | (add-text-properties | |
e8e2a88c | 1068 | beg end |
f1f007dc | 1069 | `(occur-target ,marker help-echo "mouse-2: go to this occurrence"))))) |
46b3d18e | 1070 | (goto-char endpt)) |
e1690783 CW |
1071 | (if endpt |
1072 | (progn | |
1073 | (setq lines (1+ lines)) | |
1074 | ;; On to the next match... | |
1075 | (forward-line 1)) | |
1076 | (goto-char (point-max)))))) | |
46b3d18e | 1077 | (when (not (zerop matches)) ;; is the count zero? |
daae70bf | 1078 | (setq globalcount (+ globalcount matches)) |
46b3d18e RS |
1079 | (with-current-buffer out-buf |
1080 | (goto-char headerpt) | |
1081 | (let ((beg (point)) | |
1082 | end) | |
dbed3cd3 JPW |
1083 | (insert (format "%d match%s for \"%s\" in buffer: %s\n" |
1084 | matches (if (= matches 1) "" "es") | |
1085 | regexp (buffer-name buf))) | |
46b3d18e RS |
1086 | (setq end (point)) |
1087 | (add-text-properties beg end | |
1088 | (append | |
1089 | (when title-face | |
506a6d7e CW |
1090 | `(font-lock-face ,title-face)) |
1091 | `(occur-title ,buf)))) | |
46b3d18e | 1092 | (goto-char (point-min))))))) |
5cb4031d KH |
1093 | (if coding |
1094 | ;; CODING is buffer-file-coding-system of the first buffer | |
1095 | ;; that locally binds it. Let's use it also for the output | |
1096 | ;; buffer. | |
1097 | (set-buffer-file-coding-system coding)) | |
46b3d18e RS |
1098 | ;; Return the number of matches |
1099 | globalcount))) | |
68608d9c | 1100 | |
698e1804 | 1101 | \f |
81bdc14d RS |
1102 | ;; It would be nice to use \\[...], but there is no reasonable way |
1103 | ;; to make that display both SPC and Y. | |
698e1804 RS |
1104 | (defconst query-replace-help |
1105 | "Type Space or `y' to replace one match, Delete or `n' to skip to next, | |
be44f62c | 1106 | RET or `q' to exit, Period to replace one match and exit, |
698e1804 RS |
1107 | Comma to replace but not move point immediately, |
1108 | C-r to enter recursive edit (\\[exit-recursive-edit] to get out again), | |
1109 | C-w to delete match and recursive edit, | |
1110 | C-l to clear the screen, redisplay, and offer same replacement again, | |
1111 | ! to replace all remaining matches with no more questions, | |
7ce278f3 GM |
1112 | ^ to move point back to previous match, |
1113 | E to edit the replacement string" | |
f54701d1 | 1114 | "Help message while in `query-replace'.") |
698e1804 | 1115 | |
81bdc14d RS |
1116 | (defvar query-replace-map (make-sparse-keymap) |
1117 | "Keymap that defines the responses to questions in `query-replace'. | |
1118 | The \"bindings\" in this map are not commands; they are answers. | |
1119 | The valid answers include `act', `skip', `act-and-show', | |
1120 | `exit', `act-and-exit', `edit', `delete-and-edit', `recenter', | |
d9121bc0 | 1121 | `automatic', `backup', `exit-prefix', and `help'.") |
81bdc14d RS |
1122 | |
1123 | (define-key query-replace-map " " 'act) | |
1124 | (define-key query-replace-map "\d" 'skip) | |
1125 | (define-key query-replace-map [delete] 'skip) | |
9275e5e3 | 1126 | (define-key query-replace-map [backspace] 'skip) |
81bdc14d RS |
1127 | (define-key query-replace-map "y" 'act) |
1128 | (define-key query-replace-map "n" 'skip) | |
633a305a RS |
1129 | (define-key query-replace-map "Y" 'act) |
1130 | (define-key query-replace-map "N" 'skip) | |
34724fcb | 1131 | (define-key query-replace-map "e" 'edit-replacement) |
7ce278f3 | 1132 | (define-key query-replace-map "E" 'edit-replacement) |
81bdc14d | 1133 | (define-key query-replace-map "," 'act-and-show) |
81bdc14d | 1134 | (define-key query-replace-map "q" 'exit) |
919592c0 | 1135 | (define-key query-replace-map "\r" 'exit) |
384c7da4 | 1136 | (define-key query-replace-map [return] 'exit) |
81bdc14d RS |
1137 | (define-key query-replace-map "." 'act-and-exit) |
1138 | (define-key query-replace-map "\C-r" 'edit) | |
1139 | (define-key query-replace-map "\C-w" 'delete-and-edit) | |
1140 | (define-key query-replace-map "\C-l" 'recenter) | |
1141 | (define-key query-replace-map "!" 'automatic) | |
1142 | (define-key query-replace-map "^" 'backup) | |
1143 | (define-key query-replace-map "\C-h" 'help) | |
e731045a KH |
1144 | (define-key query-replace-map [f1] 'help) |
1145 | (define-key query-replace-map [help] 'help) | |
81bdc14d | 1146 | (define-key query-replace-map "?" 'help) |
bc6312e1 RS |
1147 | (define-key query-replace-map "\C-g" 'quit) |
1148 | (define-key query-replace-map "\C-]" 'quit) | |
d9121bc0 RS |
1149 | (define-key query-replace-map "\e" 'exit-prefix) |
1150 | (define-key query-replace-map [escape] 'exit-prefix) | |
81bdc14d | 1151 | |
84482eb3 | 1152 | (defun replace-match-string-symbols (n) |
e730be7f DL |
1153 | "Process a list (and any sub-lists), expanding certain symbols. |
1154 | Symbol Expands To | |
1155 | N (match-string N) (where N is a string of digits) | |
1156 | #N (string-to-number (match-string N)) | |
1157 | & (match-string 0) | |
1158 | #& (string-to-number (match-string 0)) | |
2f57bf85 | 1159 | # replace-count |
e730be7f DL |
1160 | |
1161 | Note that these symbols must be preceeded by a backslash in order to | |
1162 | type them." | |
84482eb3 RS |
1163 | (while n |
1164 | (cond | |
1165 | ((consp (car n)) | |
1166 | (replace-match-string-symbols (car n))) ;Process sub-list | |
1167 | ((symbolp (car n)) | |
1168 | (let ((name (symbol-name (car n)))) | |
1169 | (cond | |
1170 | ((string-match "^[0-9]+$" name) | |
1171 | (setcar n (list 'match-string (string-to-number name)))) | |
1172 | ((string-match "^#[0-9]+$" name) | |
1173 | (setcar n (list 'string-to-number | |
1174 | (list 'match-string | |
1175 | (string-to-number (substring name 1)))))) | |
1176 | ((string= "&" name) | |
1177 | (setcar n '(match-string 0))) | |
1178 | ((string= "#&" name) | |
2f57bf85 DK |
1179 | (setcar n '(string-to-number (match-string 0)))) |
1180 | ((string= "#" name) | |
1181 | (setcar n 'replace-count)))))) | |
84482eb3 RS |
1182 | (setq n (cdr n)))) |
1183 | ||
1184 | (defun replace-eval-replacement (expression replace-count) | |
1185 | (let ((replacement (eval expression))) | |
1186 | (if (stringp replacement) | |
1187 | replacement | |
1188 | (prin1-to-string replacement t)))) | |
1189 | ||
2f57bf85 DK |
1190 | (defun replace-quote (replacement) |
1191 | "Quote a replacement string. | |
1192 | This just doubles all backslashes in REPLACEMENT and | |
1193 | returns the resulting string. If REPLACEMENT is not | |
1194 | a string, it is first passed through `prin1-to-string' | |
1195 | with the `noescape' argument set. | |
1196 | ||
1197 | `match-data' is preserved across the call." | |
1198 | (save-match-data | |
1199 | (replace-regexp-in-string "\\\\" "\\\\" | |
1200 | (if (stringp replacement) | |
1201 | replacement | |
1202 | (prin1-to-string replacement t)) | |
1203 | t t))) | |
1204 | ||
84482eb3 RS |
1205 | (defun replace-loop-through-replacements (data replace-count) |
1206 | ;; DATA is a vector contaning the following values: | |
1207 | ;; 0 next-rotate-count | |
1208 | ;; 1 repeat-count | |
1209 | ;; 2 next-replacement | |
1210 | ;; 3 replacements | |
1211 | (if (= (aref data 0) replace-count) | |
1212 | (progn | |
1213 | (aset data 0 (+ replace-count (aref data 1))) | |
1214 | (let ((next (cdr (aref data 2)))) | |
1215 | (aset data 2 (if (consp next) next (aref data 3)))))) | |
1216 | (car (aref data 2))) | |
1217 | ||
7c1c02ac DK |
1218 | (defun replace-match-data (integers reuse &optional new) |
1219 | "Like `match-data', but markers in REUSE get invalidated. | |
1220 | If NEW is non-NIL, it is set and returned instead of fresh data, | |
1221 | but coerced to the correct value of INTEGERS." | |
1222 | (or (and new | |
1223 | (progn | |
1224 | (set-match-data new) | |
1225 | (and (eq new reuse) | |
1226 | (eq (null integers) (markerp (car reuse))) | |
1227 | new))) | |
1228 | (match-data integers | |
1229 | (prog1 reuse | |
1230 | (while reuse | |
1231 | (if (markerp (car reuse)) | |
1232 | (set-marker (car reuse) nil)) | |
1233 | (setq reuse (cdr reuse))))))) | |
1234 | ||
1235 | (defun replace-match-maybe-edit (newtext fixedcase literal noedit match-data) | |
1236 | "Make a replacement with `replace-match', editing `\\?'. | |
15fd7d5d | 1237 | NEWTEXT, FIXEDCASE, LITERAL are just passed on. If NOEDIT is true, no |
7c1c02ac DK |
1238 | check for `\\?' is made to save time. MATCH-DATA is used for the |
1239 | replacement. In case editing is done, it is changed to use markers. | |
1240 | ||
1241 | The return value is non-NIL if there has been no `\\?' or NOEDIT was | |
1242 | passed in. If LITERAL is set, no checking is done, anyway." | |
1243 | (unless (or literal noedit) | |
1244 | (setq noedit t) | |
1245 | (while (string-match "\\(\\`\\|[^\\]\\)\\(\\\\\\\\\\)*\\(\\\\\\?\\)" | |
1246 | newtext) | |
1247 | (setq newtext | |
1248 | (read-input "Edit replacement string: " | |
1249 | (prog1 | |
1250 | (cons | |
1251 | (replace-match "" t t newtext 3) | |
1252 | (1+ (match-beginning 3))) | |
1253 | (setq match-data | |
1254 | (replace-match-data | |
1255 | nil match-data match-data)))) | |
1256 | noedit nil))) | |
1257 | (set-match-data match-data) | |
1258 | (replace-match newtext fixedcase literal) | |
1259 | noedit) | |
1260 | ||
d99118b0 | 1261 | (defun perform-replace (from-string replacements |
698e1804 | 1262 | query-flag regexp-flag delimited-flag |
99a7559f | 1263 | &optional repeat-count map start end) |
698e1804 RS |
1264 | "Subroutine of `query-replace'. Its complexity handles interactive queries. |
1265 | Don't use this in your own program unless you want to query and set the mark | |
1266 | just as `query-replace' does. Instead, write a simple loop like this: | |
698665d1 GM |
1267 | |
1268 | (while (re-search-forward \"foo[ \\t]+bar\" nil t) | |
698e1804 | 1269 | (replace-match \"foobar\" nil nil)) |
698665d1 GM |
1270 | |
1271 | which will run faster and probably do exactly what you want. Please | |
1272 | see the documentation of `replace-match' to find out how to simulate | |
588c915a CW |
1273 | `case-replace'. |
1274 | ||
1275 | This function returns nil if and only if there were no matches to | |
1276 | make, or the user didn't cancel the call." | |
81bdc14d | 1277 | (or map (setq map query-replace-map)) |
1c1dadab RS |
1278 | (and query-flag minibuffer-auto-raise |
1279 | (raise-frame (window-frame (minibuffer-window)))) | |
698e1804 RS |
1280 | (let ((nocasify (not (and case-fold-search case-replace |
1281 | (string-equal from-string | |
1282 | (downcase from-string))))) | |
5a78b471 KH |
1283 | (case-fold-search (and case-fold-search |
1284 | (string-equal from-string | |
1285 | (downcase from-string)))) | |
d83a97ab | 1286 | (literal (or (not regexp-flag) (eq regexp-flag 'literal))) |
698e1804 RS |
1287 | (search-function (if regexp-flag 're-search-forward 'search-forward)) |
1288 | (search-string from-string) | |
e5d77022 | 1289 | (real-match-data nil) ; the match data for the current match |
698e1804 | 1290 | (next-replacement nil) |
7c1c02ac | 1291 | (noedit nil) |
698e1804 RS |
1292 | (keep-going t) |
1293 | (stack nil) | |
698e1804 | 1294 | (replace-count 0) |
5632eb27 PE |
1295 | (nonempty-match nil) |
1296 | ||
7ef5c431 KH |
1297 | ;; If non-nil, it is marker saying where in the buffer to stop. |
1298 | (limit nil) | |
1299 | ||
5632eb27 PE |
1300 | ;; Data for the next match. If a cons, it has the same format as |
1301 | ;; (match-data); otherwise it is t if a match is possible at point. | |
ae4eb03c | 1302 | (match-again t) |
5632eb27 | 1303 | |
35d59c0f JL |
1304 | (isearch-string isearch-string) |
1305 | (isearch-regexp isearch-regexp) | |
ccec9764 | 1306 | (isearch-case-fold-search isearch-case-fold-search) |
02d95a27 RS |
1307 | (message |
1308 | (if query-flag | |
1309 | (substitute-command-keys | |
1310 | "Query replacing %s with %s: (\\<query-replace-map>\\[help] for help) ")))) | |
7ef5c431 KH |
1311 | |
1312 | ;; If region is active, in Transient Mark mode, operate on region. | |
47d72254 GM |
1313 | (when start |
1314 | (setq limit (copy-marker (max start end))) | |
1315 | (goto-char (min start end)) | |
1316 | (deactivate-mark)) | |
84482eb3 RS |
1317 | |
1318 | ;; REPLACEMENTS is either a string, a list of strings, or a cons cell | |
1319 | ;; containing a function and its first argument. The function is | |
1320 | ;; called to generate each replacement like this: | |
1321 | ;; (funcall (car replacements) (cdr replacements) replace-count) | |
1322 | ;; It must return a string. | |
1323 | (cond | |
1324 | ((stringp replacements) | |
1325 | (setq next-replacement replacements | |
1326 | replacements nil)) | |
1327 | ((stringp (car replacements)) ; If it isn't a string, it must be a cons | |
1328 | (or repeat-count (setq repeat-count 1)) | |
1329 | (setq replacements (cons 'replace-loop-through-replacements | |
1330 | (vector repeat-count repeat-count | |
1331 | replacements replacements))))) | |
1332 | ||
698e1804 RS |
1333 | (if delimited-flag |
1334 | (setq search-function 're-search-forward | |
1335 | search-string (concat "\\b" | |
1336 | (if regexp-flag from-string | |
1337 | (regexp-quote from-string)) | |
1338 | "\\b"))) | |
ccec9764 JL |
1339 | (when query-replace-lazy-highlight |
1340 | (setq isearch-string search-string | |
1341 | isearch-regexp (or delimited-flag regexp-flag) | |
1342 | isearch-case-fold-search case-fold-search | |
1343 | isearch-lazy-highlight-last-string nil)) | |
35d59c0f | 1344 | |
698e1804 RS |
1345 | (push-mark) |
1346 | (undo-boundary) | |
e782e9f2 RS |
1347 | (unwind-protect |
1348 | ;; Loop finding occurrences that perhaps should be replaced. | |
1349 | (while (and keep-going | |
22a76778 | 1350 | (not (or (eobp) (and limit (>= (point) limit)))) |
5632eb27 PE |
1351 | ;; Use the next match if it is already known; |
1352 | ;; otherwise, search for a match after moving forward | |
1353 | ;; one char if progress is required. | |
1354 | (setq real-match-data | |
1355 | (if (consp match-again) | |
1356 | (progn (goto-char (nth 1 match-again)) | |
7c1c02ac DK |
1357 | (replace-match-data t |
1358 | real-match-data | |
1359 | match-again)) | |
5632eb27 | 1360 | (and (or match-again |
889617de GM |
1361 | ;; MATCH-AGAIN non-nil means we |
1362 | ;; accept an adjacent match. If | |
1363 | ;; we don't, move one char to the | |
1364 | ;; right. This takes us a | |
1365 | ;; character too far at the end, | |
1366 | ;; but this is undone after the | |
1367 | ;; while-loop. | |
22a76778 JL |
1368 | (progn |
1369 | (forward-char 1) | |
1370 | (not (or (eobp) | |
1371 | (and limit (>= (point) limit)))))) | |
7ef5c431 | 1372 | (funcall search-function search-string limit t) |
5632eb27 PE |
1373 | ;; For speed, use only integers and |
1374 | ;; reuse the list used last time. | |
7c1c02ac | 1375 | (replace-match-data t real-match-data))))) |
1c4fe319 RS |
1376 | ;; Optionally ignore matches that have a read-only property. |
1377 | (unless (and query-replace-skip-read-only | |
1378 | (text-property-not-all | |
1379 | (match-beginning 0) (match-end 0) | |
1380 | 'read-only nil)) | |
1381 | ||
1382 | ;; Record whether the match is nonempty, to avoid an infinite loop | |
1383 | ;; repeatedly matching the same empty string. | |
1384 | (setq nonempty-match | |
1385 | (/= (nth 0 real-match-data) (nth 1 real-match-data))) | |
1386 | ||
1387 | ;; If the match is empty, record that the next one can't be | |
1388 | ;; adjacent. | |
1389 | ||
1390 | ;; Otherwise, if matching a regular expression, do the next | |
1391 | ;; match now, since the replacement for this match may | |
1392 | ;; affect whether the next match is adjacent to this one. | |
1393 | ;; If that match is empty, don't use it. | |
1394 | (setq match-again | |
1395 | (and nonempty-match | |
1396 | (or (not regexp-flag) | |
1397 | (and (looking-at search-string) | |
1398 | (let ((match (match-data))) | |
1399 | (and (/= (nth 0 match) (nth 1 match)) | |
1400 | match)))))) | |
1401 | ||
1402 | ;; Calculate the replacement string, if necessary. | |
1403 | (when replacements | |
1404 | (set-match-data real-match-data) | |
1405 | (setq next-replacement | |
1406 | (funcall (car replacements) (cdr replacements) | |
7c1c02ac DK |
1407 | replace-count) |
1408 | noedit nil)) | |
1c4fe319 | 1409 | (if (not query-flag) |
cb05ca25 DK |
1410 | (let ((inhibit-read-only |
1411 | query-replace-skip-read-only)) | |
15fd7d5d | 1412 | (unless (or literal noedit) |
cb05ca25 DK |
1413 | (replace-highlight (nth 0 real-match-data) |
1414 | (nth 1 real-match-data))) | |
7c1c02ac DK |
1415 | (setq noedit |
1416 | (replace-match-maybe-edit | |
1417 | next-replacement nocasify literal | |
1418 | noedit real-match-data) | |
1419 | replace-count (1+ replace-count))) | |
1c4fe319 RS |
1420 | (undo-boundary) |
1421 | (let (done replaced key def) | |
1422 | ;; Loop reading commands until one of them sets done, | |
7c1c02ac DK |
1423 | ;; which means it has finished handling this |
1424 | ;; occurrence. Any command that sets `done' should | |
1425 | ;; leave behind proper match data for the stack. | |
1426 | ;; Commands not setting `done' need to adjust | |
1427 | ;; `real-match-data'. | |
1c4fe319 RS |
1428 | (while (not done) |
1429 | (set-match-data real-match-data) | |
1430 | (replace-highlight (match-beginning 0) (match-end 0)) | |
1431 | ;; Bind message-log-max so we don't fill up the message log | |
1432 | ;; with a bunch of identical messages. | |
1433 | (let ((message-log-max nil)) | |
b938735a JL |
1434 | (message message |
1435 | (query-replace-descr from-string) | |
1436 | (query-replace-descr next-replacement))) | |
1c4fe319 RS |
1437 | (setq key (read-event)) |
1438 | ;; Necessary in case something happens during read-event | |
1439 | ;; that clobbers the match data. | |
1440 | (set-match-data real-match-data) | |
1441 | (setq key (vector key)) | |
1442 | (setq def (lookup-key map key)) | |
1443 | ;; Restore the match data while we process the command. | |
1444 | (cond ((eq def 'help) | |
1445 | (with-output-to-temp-buffer "*Help*" | |
1446 | (princ | |
1447 | (concat "Query replacing " | |
1448 | (if regexp-flag "regexp " "") | |
1449 | from-string " with " | |
1450 | next-replacement ".\n\n" | |
1451 | (substitute-command-keys | |
1452 | query-replace-help))) | |
1453 | (with-current-buffer standard-output | |
1454 | (help-mode)))) | |
1455 | ((eq def 'exit) | |
1456 | (setq keep-going nil) | |
1457 | (setq done t)) | |
1458 | ((eq def 'backup) | |
1459 | (if stack | |
588c915a | 1460 | (let ((elt (pop stack))) |
7c1c02ac DK |
1461 | (goto-char (nth 0 elt)) |
1462 | (setq replaced (nth 1 elt) | |
1463 | real-match-data | |
1464 | (replace-match-data | |
1465 | t real-match-data | |
1466 | (nth 2 elt)))) | |
1c4fe319 RS |
1467 | (message "No previous match") |
1468 | (ding 'no-terminate) | |
1469 | (sit-for 1))) | |
1470 | ((eq def 'act) | |
1471 | (or replaced | |
7c1c02ac DK |
1472 | (setq noedit |
1473 | (replace-match-maybe-edit | |
1474 | next-replacement nocasify literal | |
1475 | noedit real-match-data) | |
1476 | replace-count (1+ replace-count))) | |
1c4fe319 RS |
1477 | (setq done t replaced t)) |
1478 | ((eq def 'act-and-exit) | |
1479 | (or replaced | |
7c1c02ac | 1480 | (setq noedit |
da6eb51c | 1481 | (replace-match-maybe-edit |
7c1c02ac DK |
1482 | next-replacement nocasify literal |
1483 | noedit real-match-data) | |
1484 | replace-count (1+ replace-count))) | |
1c4fe319 RS |
1485 | (setq keep-going nil) |
1486 | (setq done t replaced t)) | |
1487 | ((eq def 'act-and-show) | |
1488 | (if (not replaced) | |
7c1c02ac DK |
1489 | (setq noedit |
1490 | (replace-match-maybe-edit | |
1491 | next-replacement nocasify literal | |
1492 | noedit real-match-data) | |
1493 | replace-count (1+ replace-count) | |
1494 | real-match-data (replace-match-data | |
1495 | t real-match-data) | |
1496 | replaced t))) | |
1c4fe319 RS |
1497 | ((eq def 'automatic) |
1498 | (or replaced | |
7c1c02ac DK |
1499 | (setq noedit |
1500 | (replace-match-maybe-edit | |
1501 | next-replacement nocasify literal | |
1502 | noedit real-match-data) | |
1503 | replace-count (1+ replace-count))) | |
1c4fe319 RS |
1504 | (setq done t query-flag nil replaced t)) |
1505 | ((eq def 'skip) | |
1506 | (setq done t)) | |
1507 | ((eq def 'recenter) | |
1508 | (recenter nil)) | |
1509 | ((eq def 'edit) | |
1510 | (let ((opos (point-marker))) | |
7c1c02ac DK |
1511 | (setq real-match-data (replace-match-data |
1512 | nil real-match-data | |
1513 | real-match-data)) | |
1c4fe319 | 1514 | (goto-char (match-beginning 0)) |
86914dcc RS |
1515 | (save-excursion |
1516 | (save-window-excursion | |
1517 | (recursive-edit))) | |
7c1c02ac DK |
1518 | (goto-char opos) |
1519 | (set-marker opos nil)) | |
1c4fe319 RS |
1520 | ;; Before we make the replacement, |
1521 | ;; decide whether the search string | |
1522 | ;; can match again just after this match. | |
1523 | (if (and regexp-flag nonempty-match) | |
1524 | (setq match-again (and (looking-at search-string) | |
1525 | (match-data))))) | |
1c4fe319 RS |
1526 | ;; Edit replacement. |
1527 | ((eq def 'edit-replacement) | |
7c1c02ac DK |
1528 | (setq real-match-data (replace-match-data |
1529 | nil real-match-data | |
1530 | real-match-data) | |
1531 | next-replacement | |
1c4fe319 | 1532 | (read-input "Edit replacement string: " |
7c1c02ac DK |
1533 | next-replacement) |
1534 | noedit nil) | |
1535 | (if replaced | |
1536 | (set-match-data real-match-data) | |
1537 | (setq noedit | |
1538 | (replace-match-maybe-edit | |
1539 | next-replacement nocasify literal noedit | |
1540 | real-match-data) | |
1541 | replaced t)) | |
1c4fe319 | 1542 | (setq done t)) |
d99118b0 | 1543 | |
1c4fe319 | 1544 | ((eq def 'delete-and-edit) |
7c1c02ac DK |
1545 | (replace-match "" t t) |
1546 | (setq real-match-data (replace-match-data | |
1547 | nil real-match-data)) | |
1548 | (replace-dehighlight) | |
1549 | (save-excursion (recursive-edit)) | |
1c4fe319 RS |
1550 | (setq replaced t)) |
1551 | ;; Note: we do not need to treat `exit-prefix' | |
1552 | ;; specially here, since we reread | |
1553 | ;; any unrecognized character. | |
1554 | (t | |
1555 | (setq this-command 'mode-exited) | |
1556 | (setq keep-going nil) | |
1557 | (setq unread-command-events | |
1558 | (append (listify-key-sequence key) | |
1559 | unread-command-events)) | |
35d59c0f | 1560 | (setq done t))) |
ccec9764 JL |
1561 | (when query-replace-lazy-highlight |
1562 | ;; Restore isearch data for lazy highlighting | |
1563 | ;; in case of isearching during recursive edit | |
35d59c0f | 1564 | (setq isearch-string search-string |
ccec9764 JL |
1565 | isearch-regexp (or delimited-flag regexp-flag) |
1566 | isearch-case-fold-search case-fold-search) | |
1567 | ;; Force lazy rehighlighting only after replacements | |
1568 | (if (not (memq def '(skip backup))) | |
1569 | (setq isearch-lazy-highlight-last-string nil)))) | |
1c4fe319 RS |
1570 | ;; Record previous position for ^ when we move on. |
1571 | ;; Change markers to numbers in the match data | |
1572 | ;; since lots of markers slow down editing. | |
7c1c02ac DK |
1573 | (push (list (point) replaced |
1574 | ;;; If the replacement has already happened, all we need is the | |
1575 | ;;; current match start and end. We could get this with a trivial | |
1576 | ;;; match like | |
1577 | ;;; (save-excursion (goto-char (match-beginning 0)) | |
1578 | ;;; (search-forward (match-string 0)) | |
1579 | ;;; (match-data t)) | |
1580 | ;;; if we really wanted to avoid manually constructing match data. | |
1581 | ;;; Adding current-buffer is necessary so that match-data calls can | |
1582 | ;;; return markers which are appropriate for editing. | |
1583 | (if replaced | |
1584 | (list | |
1585 | (match-beginning 0) | |
1586 | (match-end 0) | |
1587 | (current-buffer)) | |
1588 | (match-data t))) | |
1589 | stack))))) | |
889617de GM |
1590 | |
1591 | ;; The code preventing adjacent regexp matches in the condition | |
1592 | ;; of the while-loop above will haven taken us one character | |
1593 | ;; beyond the last replacement. Undo that. | |
1594 | (when (and regexp-flag (not match-again) (> replace-count 0)) | |
1595 | (backward-char 1)) | |
d99118b0 | 1596 | |
e782e9f2 | 1597 | (replace-dehighlight)) |
4d33492a RS |
1598 | (or unread-command-events |
1599 | (message "Replaced %d occurrence%s" | |
1600 | replace-count | |
1601 | (if (= replace-count 1) "" "s"))) | |
1602 | (and keep-going stack))) | |
698e1804 | 1603 | |
ccec9764 JL |
1604 | (defcustom query-replace-highlight t |
1605 | "*Non-nil means to highlight matches during query replacement." | |
1606 | :type 'boolean | |
9d325ebf | 1607 | :group 'matching) |
e782e9f2 | 1608 | |
ccec9764 JL |
1609 | (defcustom query-replace-lazy-highlight t |
1610 | "*Controls the lazy-highlighting during query replacements. | |
1611 | When non-nil, all text in the buffer matching the current match | |
1612 | is highlighted lazily using isearch lazy highlighting (see | |
1613 | `isearch-lazy-highlight-initial-delay' and | |
1614 | `isearch-lazy-highlight-interval')." | |
1615 | :type 'boolean | |
1616 | :group 'matching | |
1617 | :version "21.4") | |
1618 | ||
1619 | (defface query-replace | |
1620 | '((t (:inherit isearch))) | |
1621 | "Face for highlighting query replacement matches." | |
1622 | :group 'matching | |
1623 | :version "21.4") | |
1624 | ||
e782e9f2 RS |
1625 | (defvar replace-overlay nil) |
1626 | ||
ccec9764 JL |
1627 | (defun replace-highlight (beg end) |
1628 | (if query-replace-highlight | |
1629 | (if replace-overlay | |
1630 | (move-overlay replace-overlay beg end (current-buffer)) | |
1631 | (setq replace-overlay (make-overlay beg end)) | |
1632 | (overlay-put replace-overlay 'priority 1) ;higher than lazy overlays | |
1633 | (overlay-put replace-overlay 'face 'query-replace))) | |
1634 | (if query-replace-lazy-highlight | |
1635 | (isearch-lazy-highlight-new-loop))) | |
1636 | ||
e782e9f2 | 1637 | (defun replace-dehighlight () |
ccec9764 JL |
1638 | (when replace-overlay |
1639 | (delete-overlay replace-overlay)) | |
1640 | (when query-replace-lazy-highlight | |
1641 | (isearch-lazy-highlight-cleanup isearch-lazy-highlight-cleanup) | |
1642 | (setq isearch-lazy-highlight-last-string nil))) | |
e782e9f2 | 1643 | |
6f1df6d9 | 1644 | ;; arch-tag: 16b4cd61-fd40-497b-b86f-b667c4cf88e4 |
c88ab9ce | 1645 | ;;; replace.el ends here |