Commit | Line | Data |
---|---|---|
60370d40 | 1 | ;;; replace.el --- replace commands for Emacs |
c88ab9ce | 2 | |
653479ad | 3 | ;; Copyright (C) 1985, 86, 87, 92, 94, 96, 1997, 2000, 2001, 2002 |
8f3ff96b | 4 | ;; 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 | ||
151270f3 RS |
39 | (defvar query-replace-interactive nil |
40 | "Non-nil means `query-replace' uses the last search string. | |
41 | That becomes the \"string to replace\".") | |
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 | |
86914dcc RS |
67 | (defun query-replace-read-args (string regexp-flag &optional noerror) |
68 | (unless noerror | |
69 | (barf-if-buffer-read-only)) | |
770970cb | 70 | (let (from to) |
151270f3 RS |
71 | (if query-replace-interactive |
72 | (setq from (car (if regexp-flag regexp-search-ring search-ring))) | |
2f2f7e58 RS |
73 | ;; The save-excursion here is in case the user marks and copies |
74 | ;; a region in order to specify the minibuffer input. | |
75 | ;; That should not clobber the region for the query-replace itself. | |
76 | (save-excursion | |
77 | (setq from (read-from-minibuffer (format "%s: " string) | |
78 | nil nil nil | |
79 | query-replace-from-history-variable | |
80 | nil t))) | |
d9291fa3 RS |
81 | ;; Warn if user types \n or \t, but don't reject the input. |
82 | (if (string-match "\\\\[nt]" from) | |
83 | (let ((match (match-string 0 from))) | |
84 | (cond | |
85 | ((string= match "\\n") | |
86 | (message "Note: `\\n' here doesn't match a newline; to do that, type C-q C-j instead")) | |
87 | ((string= match "\\t") | |
88 | (message "Note: `\\t' here doesn't match a tab; to do that, just type TAB"))) | |
89 | (sit-for 2)))) | |
90 | ||
2f2f7e58 RS |
91 | (save-excursion |
92 | (setq to (read-from-minibuffer (format "%s %s with: " string from) | |
93 | nil nil nil | |
94 | query-replace-to-history-variable from t))) | |
10784bac | 95 | (list from to current-prefix-arg))) |
770970cb | 96 | |
47d72254 | 97 | (defun query-replace (from-string to-string &optional delimited start end) |
da44e784 RM |
98 | "Replace some occurrences of FROM-STRING with TO-STRING. |
99 | As each match is found, the user must type a character saying | |
100 | what to do with it. For directions, type \\[help-command] at that time. | |
101 | ||
7ef5c431 KH |
102 | In Transient Mark mode, if the mark is active, operate on the contents |
103 | of the region. Otherwise, operate from point to the end of the buffer. | |
104 | ||
151270f3 RS |
105 | If `query-replace-interactive' is non-nil, the last incremental search |
106 | string is used as FROM-STRING--you don't have to specify it with the | |
107 | minibuffer. | |
108 | ||
446d9629 RS |
109 | Matching is independent of case if `case-fold-search' is non-nil and |
110 | FROM-STRING has no uppercase letters. Replacement transfers the case | |
111 | pattern of the old text to the new text, if `case-replace' and | |
112 | `case-fold-search' are non-nil and FROM-STRING has no uppercase | |
113 | letters. \(Transferring the case pattern means that if the old text | |
114 | matched is all caps, or capitalized, then its replacement is upcased | |
115 | or capitalized.) | |
9b0bf2b6 | 116 | |
118a01c9 | 117 | Third arg DELIMITED (prefix arg if interactive), if non-nil, means replace |
81bdc14d | 118 | only matches surrounded by word boundaries. |
47d72254 | 119 | Fourth and fifth arg START and END specify the region to operate on. |
81bdc14d RS |
120 | |
121 | To customize possible responses, change the \"bindings\" in `query-replace-map'." | |
10784bac RS |
122 | (interactive (let ((common |
123 | (query-replace-read-args "Query replace" nil))) | |
124 | (list (nth 0 common) (nth 1 common) (nth 2 common) | |
125 | ;; These are done separately here | |
126 | ;; so that command-history will record these expressions | |
127 | ;; rather than the values they had this time. | |
128 | (if (and transient-mark-mode mark-active) | |
129 | (region-beginning)) | |
130 | (if (and transient-mark-mode mark-active) | |
131 | (region-end))))) | |
99a7559f | 132 | (perform-replace from-string to-string t nil delimited nil nil start end)) |
7ef5c431 | 133 | |
73fa8346 | 134 | (define-key esc-map "%" 'query-replace) |
da44e784 | 135 | |
47d72254 | 136 | (defun query-replace-regexp (regexp to-string &optional delimited start end) |
da44e784 RM |
137 | "Replace some things after point matching REGEXP with TO-STRING. |
138 | As each match is found, the user must type a character saying | |
139 | what to do with it. For directions, type \\[help-command] at that time. | |
140 | ||
7ef5c431 KH |
141 | In Transient Mark mode, if the mark is active, operate on the contents |
142 | of the region. Otherwise, operate from point to the end of the buffer. | |
143 | ||
151270f3 RS |
144 | If `query-replace-interactive' is non-nil, the last incremental search |
145 | regexp is used as REGEXP--you don't have to specify it with the | |
146 | minibuffer. | |
147 | ||
446d9629 RS |
148 | Matching is independent of case if `case-fold-search' is non-nil and |
149 | REGEXP has no uppercase letters. Replacement transfers the case | |
150 | pattern of the old text to the new text, if `case-replace' and | |
151 | `case-fold-search' are non-nil and REGEXP has no uppercase letters. | |
152 | \(Transferring the case pattern means that if the old text matched is | |
153 | all caps, or capitalized, then its replacement is upcased or | |
154 | capitalized.) | |
47d72254 | 155 | |
118a01c9 | 156 | Third arg DELIMITED (prefix arg if interactive), if non-nil, means replace |
da44e784 | 157 | only matches surrounded by word boundaries. |
47d72254 GM |
158 | Fourth and fifth arg START and END specify the region to operate on. |
159 | ||
118a01c9 RS |
160 | In TO-STRING, `\\&' stands for whatever matched the whole of REGEXP, |
161 | and `\\=\\N' (where N is a digit) stands for | |
162 | whatever what matched the Nth `\\(...\\)' in REGEXP." | |
10784bac RS |
163 | (interactive |
164 | (let ((common | |
165 | (query-replace-read-args "Query replace regexp" t))) | |
166 | (list (nth 0 common) (nth 1 common) (nth 2 common) | |
167 | ;; These are done separately here | |
168 | ;; so that command-history will record these expressions | |
169 | ;; rather than the values they had this time. | |
170 | (if (and transient-mark-mode mark-active) | |
171 | (region-beginning)) | |
172 | (if (and transient-mark-mode mark-active) | |
173 | (region-end))))) | |
174 | ||
99a7559f | 175 | (perform-replace regexp to-string t t delimited nil nil start end)) |
cbc127de | 176 | (define-key esc-map [?\C-%] 'query-replace-regexp) |
da44e784 | 177 | |
47d72254 | 178 | (defun query-replace-regexp-eval (regexp to-expr &optional delimited start end) |
84482eb3 RS |
179 | "Replace some things after point matching REGEXP with the result of TO-EXPR. |
180 | As each match is found, the user must type a character saying | |
181 | what to do with it. For directions, type \\[help-command] at that time. | |
182 | ||
183 | TO-EXPR is a Lisp expression evaluated to compute each replacement. It may | |
184 | reference `replace-count' to get the number of replacements already made. | |
185 | If the result of TO-EXPR is not a string, it is converted to one using | |
186 | `prin1-to-string' with the NOESCAPE argument (which see). | |
187 | ||
188 | For convenience, when entering TO-EXPR interactively, you can use `\\&' or | |
653479ad AS |
189 | `\\0' to stand for whatever matched the whole of REGEXP, and `\\N' (where |
190 | N is a digit) to stand for whatever matched the Nth `\\(...\\)' in REGEXP. | |
84482eb3 RS |
191 | Use `\\#&' or `\\#N' if you want a number instead of a string. |
192 | ||
193 | In Transient Mark mode, if the mark is active, operate on the contents | |
194 | of the region. Otherwise, operate from point to the end of the buffer. | |
195 | ||
196 | If `query-replace-interactive' is non-nil, the last incremental search | |
197 | regexp is used as REGEXP--you don't have to specify it with the | |
198 | minibuffer. | |
199 | ||
200 | Preserves case in each replacement if `case-replace' and `case-fold-search' | |
201 | are non-nil and REGEXP has no uppercase letters. | |
47d72254 | 202 | |
84482eb3 | 203 | Third arg DELIMITED (prefix arg if interactive), if non-nil, means replace |
470bbe9b | 204 | only matches that are surrounded by word boundaries. |
47d72254 | 205 | Fourth and fifth arg START and END specify the region to operate on." |
84482eb3 | 206 | (interactive |
10784bac | 207 | (let (from to) |
84482eb3 RS |
208 | (if query-replace-interactive |
209 | (setq from (car regexp-search-ring)) | |
210 | (setq from (read-from-minibuffer "Query replace regexp: " | |
211 | nil nil nil | |
212 | query-replace-from-history-variable | |
213 | nil t))) | |
214 | (setq to (list (read-from-minibuffer | |
215 | (format "Query replace regexp %s with eval: " from) | |
216 | nil nil t query-replace-to-history-variable from t))) | |
217 | ;; We make TO a list because replace-match-string-symbols requires one, | |
218 | ;; and the user might enter a single token. | |
219 | (replace-match-string-symbols to) | |
10784bac RS |
220 | (list from (car to) current-prefix-arg |
221 | (if (and transient-mark-mode mark-active) | |
222 | (region-beginning)) | |
223 | (if (and transient-mark-mode mark-active) | |
224 | (region-end))))) | |
d2ce3151 | 225 | (perform-replace regexp (cons 'replace-eval-replacement to-expr) |
d83a97ab | 226 | t 'literal delimited nil nil start end)) |
84482eb3 | 227 | |
47d72254 | 228 | (defun map-query-replace-regexp (regexp to-strings &optional n start end) |
da44e784 | 229 | "Replace some matches for REGEXP with various strings, in rotation. |
e730be7f DL |
230 | The second argument TO-STRINGS contains the replacement strings, |
231 | separated by spaces. Third arg DELIMITED (prefix arg if interactive), | |
232 | if non-nil, means replace only matches surrounded by word boundaries. | |
233 | This command works like `query-replace-regexp' except that each | |
234 | successive replacement uses the next successive replacement string, | |
da44e784 RM |
235 | wrapping around from the last such string to the first. |
236 | ||
7ef5c431 KH |
237 | In Transient Mark mode, if the mark is active, operate on the contents |
238 | of the region. Otherwise, operate from point to the end of the buffer. | |
239 | ||
da44e784 RM |
240 | Non-interactively, TO-STRINGS may be a list of replacement strings. |
241 | ||
151270f3 RS |
242 | If `query-replace-interactive' is non-nil, the last incremental search |
243 | regexp is used as REGEXP--you don't have to specify it with the minibuffer. | |
244 | ||
da44e784 | 245 | A prefix argument N says to use each replacement string N times |
47d72254 GM |
246 | before rotating to the next. |
247 | Fourth and fifth arg START and END specify the region to operate on." | |
770970cb | 248 | (interactive |
10784bac | 249 | (let (from to) |
151270f3 RS |
250 | (setq from (if query-replace-interactive |
251 | (car regexp-search-ring) | |
252 | (read-from-minibuffer "Map query replace (regexp): " | |
253 | nil nil nil | |
fce31d51 | 254 | 'query-replace-history nil t))) |
770970cb RS |
255 | (setq to (read-from-minibuffer |
256 | (format "Query replace %s with (space-separated strings): " | |
257 | from) | |
258 | nil nil nil | |
4a8f3b3d | 259 | 'query-replace-history from t)) |
2f2f7e58 RS |
260 | (list from to |
261 | (and current-prefix-arg | |
262 | (prefix-numeric-value current-prefix-arg)) | |
10784bac RS |
263 | (if (and transient-mark-mode mark-active) |
264 | (region-beginning)) | |
265 | (if (and transient-mark-mode mark-active) | |
266 | (region-end))))) | |
da44e784 RM |
267 | (let (replacements) |
268 | (if (listp to-strings) | |
269 | (setq replacements to-strings) | |
270 | (while (/= (length to-strings) 0) | |
271 | (if (string-match " " to-strings) | |
272 | (setq replacements | |
273 | (append replacements | |
274 | (list (substring to-strings 0 | |
275 | (string-match " " to-strings)))) | |
276 | to-strings (substring to-strings | |
277 | (1+ (string-match " " to-strings)))) | |
278 | (setq replacements (append replacements (list to-strings)) | |
279 | to-strings "")))) | |
99a7559f | 280 | (perform-replace regexp replacements t t nil n nil start end))) |
da44e784 | 281 | |
47d72254 | 282 | (defun replace-string (from-string to-string &optional delimited start end) |
da44e784 RM |
283 | "Replace occurrences of FROM-STRING with TO-STRING. |
284 | Preserve case in each match if `case-replace' and `case-fold-search' | |
285 | are non-nil and FROM-STRING has no uppercase letters. | |
9b0bf2b6 RS |
286 | \(Preserving case means that if the string matched is all caps, or capitalized, |
287 | then its replacement is upcased or capitalized.) | |
288 | ||
7ef5c431 KH |
289 | In Transient Mark mode, if the mark is active, operate on the contents |
290 | of the region. Otherwise, operate from point to the end of the buffer. | |
291 | ||
118a01c9 | 292 | Third arg DELIMITED (prefix arg if interactive), if non-nil, means replace |
da44e784 | 293 | only matches surrounded by word boundaries. |
47d72254 | 294 | Fourth and fifth arg START and END specify the region to operate on. |
da44e784 | 295 | |
151270f3 RS |
296 | If `query-replace-interactive' is non-nil, the last incremental search |
297 | string is used as FROM-STRING--you don't have to specify it with the | |
298 | minibuffer. | |
299 | ||
da44e784 RM |
300 | This function is usually the wrong thing to use in a Lisp program. |
301 | What you probably want is a loop like this: | |
118a01c9 RS |
302 | (while (search-forward FROM-STRING nil t) |
303 | (replace-match TO-STRING nil t)) | |
87532fbe RS |
304 | which will run faster and will not set the mark or print anything. |
305 | \(You may need a more complex loop if FROM-STRING can match the null string | |
306 | and TO-STRING is also null.)" | |
10784bac RS |
307 | (interactive |
308 | (let ((common | |
309 | (query-replace-read-args "Replace string" nil))) | |
310 | (list (nth 0 common) (nth 1 common) (nth 2 common) | |
311 | (if (and transient-mark-mode mark-active) | |
312 | (region-beginning)) | |
313 | (if (and transient-mark-mode mark-active) | |
314 | (region-end))))) | |
99a7559f | 315 | (perform-replace from-string to-string nil nil delimited nil nil start end)) |
da44e784 | 316 | |
47d72254 | 317 | (defun replace-regexp (regexp to-string &optional delimited start end) |
da44e784 | 318 | "Replace things after point matching REGEXP with TO-STRING. |
118a01c9 | 319 | Preserve case in each match if `case-replace' and `case-fold-search' |
da44e784 | 320 | are non-nil and REGEXP has no uppercase letters. |
47d72254 GM |
321 | |
322 | In Transient Mark mode, if the mark is active, operate on the contents | |
323 | of the region. Otherwise, operate from point to the end of the buffer. | |
324 | ||
118a01c9 | 325 | Third arg DELIMITED (prefix arg if interactive), if non-nil, means replace |
da44e784 | 326 | only matches surrounded by word boundaries. |
47d72254 GM |
327 | Fourth and fifth arg START and END specify the region to operate on. |
328 | ||
118a01c9 RS |
329 | In TO-STRING, `\\&' stands for whatever matched the whole of REGEXP, |
330 | and `\\=\\N' (where N is a digit) stands for | |
880f22a1 | 331 | whatever what matched the Nth `\\(...\\)' in REGEXP. |
da44e784 | 332 | |
151270f3 RS |
333 | If `query-replace-interactive' is non-nil, the last incremental search |
334 | regexp is used as REGEXP--you don't have to specify it with the minibuffer. | |
335 | ||
da44e784 RM |
336 | This function is usually the wrong thing to use in a Lisp program. |
337 | What you probably want is a loop like this: | |
338 | (while (re-search-forward REGEXP nil t) | |
118a01c9 | 339 | (replace-match TO-STRING nil nil)) |
da44e784 | 340 | which will run faster and will not set the mark or print anything." |
10784bac RS |
341 | (interactive |
342 | (let ((common | |
343 | (query-replace-read-args "Replace regexp" t))) | |
344 | (list (nth 0 common) (nth 1 common) (nth 2 common) | |
345 | (if (and transient-mark-mode mark-active) | |
346 | (region-beginning)) | |
347 | (if (and transient-mark-mode mark-active) | |
348 | (region-end))))) | |
99a7559f | 349 | (perform-replace regexp to-string nil t delimited nil nil start end)) |
e32eb3e6 | 350 | |
4c53bd2b RS |
351 | \f |
352 | (defvar regexp-history nil | |
353 | "History list for some commands that read regular expressions.") | |
da44e784 | 354 | |
e32eb3e6 | 355 | |
31e1d920 | 356 | (defalias 'delete-non-matching-lines 'keep-lines) |
e32eb3e6 GM |
357 | (defalias 'delete-matching-lines 'flush-lines) |
358 | (defalias 'count-matches 'how-many) | |
359 | ||
360 | ||
361 | (defun keep-lines-read-args (prompt) | |
362 | "Read arguments for `keep-lines' and friends. | |
363 | Prompt for a regexp with PROMPT. | |
2ced751f RS |
364 | Value is a list, (REGEXP)." |
365 | (list (read-from-minibuffer prompt nil nil nil | |
366 | 'regexp-history nil t))) | |
e32eb3e6 GM |
367 | |
368 | (defun keep-lines (regexp &optional rstart rend) | |
698e1804 RS |
369 | "Delete all lines except those containing matches for REGEXP. |
370 | A match split across lines preserves all the lines it lies in. | |
d2a0ee8b RS |
371 | Applies to all lines after point. |
372 | ||
373 | If REGEXP contains upper case characters (excluding those preceded by `\\'), | |
e32eb3e6 GM |
374 | the matching is case-sensitive. |
375 | ||
376 | Second and third arg RSTART and REND specify the region to operate on. | |
377 | ||
2ced751f RS |
378 | Interactively, in Transient Mark mode when the mark is active, operate |
379 | on the contents of the region. Otherwise, operate from point to the | |
380 | end of the buffer." | |
381 | ||
e32eb3e6 | 382 | (interactive |
98faf1bb RS |
383 | (progn |
384 | (barf-if-buffer-read-only) | |
385 | (keep-lines-read-args "Keep lines (containing match for regexp): "))) | |
e32eb3e6 | 386 | (if rstart |
119831da RS |
387 | (progn |
388 | (goto-char (min rstart rend)) | |
389 | (setq rend (copy-marker (max rstart rend)))) | |
2ced751f RS |
390 | (if (and transient-mark-mode mark-active) |
391 | (setq rstart (region-beginning) | |
392 | rend (copy-marker (region-end))) | |
393 | (setq rstart (point) | |
394 | rend (point-max-marker))) | |
395 | (goto-char rstart)) | |
698e1804 RS |
396 | (save-excursion |
397 | (or (bolp) (forward-line 1)) | |
d2a0ee8b RS |
398 | (let ((start (point)) |
399 | (case-fold-search (and case-fold-search | |
400 | (isearch-no-upper-case-p regexp t)))) | |
e32eb3e6 | 401 | (while (< (point) rend) |
698e1804 | 402 | ;; Start is first char not preserved by previous match. |
e32eb3e6 GM |
403 | (if (not (re-search-forward regexp rend 'move)) |
404 | (delete-region start rend) | |
698e1804 RS |
405 | (let ((end (save-excursion (goto-char (match-beginning 0)) |
406 | (beginning-of-line) | |
407 | (point)))) | |
408 | ;; Now end is first char preserved by the new match. | |
409 | (if (< start end) | |
410 | (delete-region start end)))) | |
d99118b0 | 411 | |
e32eb3e6 | 412 | (setq start (save-excursion (forward-line 1) (point))) |
698e1804 | 413 | ;; If the match was empty, avoid matching again at same place. |
e32eb3e6 GM |
414 | (and (< (point) rend) |
415 | (= (match-beginning 0) (match-end 0)) | |
698e1804 RS |
416 | (forward-char 1)))))) |
417 | ||
e32eb3e6 GM |
418 | |
419 | (defun flush-lines (regexp &optional rstart rend) | |
698e1804 RS |
420 | "Delete lines containing matches for REGEXP. |
421 | If a match is split across lines, all the lines it lies in are deleted. | |
d2a0ee8b RS |
422 | Applies to lines after point. |
423 | ||
424 | If REGEXP contains upper case characters (excluding those preceded by `\\'), | |
e32eb3e6 GM |
425 | the matching is case-sensitive. |
426 | ||
427 | Second and third arg RSTART and REND specify the region to operate on. | |
428 | ||
2ced751f RS |
429 | Interactively, in Transient Mark mode when the mark is active, operate |
430 | on the contents of the region. Otherwise, operate from point to the | |
431 | end of the buffer." | |
432 | ||
e32eb3e6 | 433 | (interactive |
98faf1bb RS |
434 | (progn |
435 | (barf-if-buffer-read-only) | |
436 | (keep-lines-read-args "Flush lines (containing match for regexp): "))) | |
e32eb3e6 | 437 | (if rstart |
119831da RS |
438 | (progn |
439 | (goto-char (min rstart rend)) | |
440 | (setq rend (copy-marker (max rstart rend)))) | |
2ced751f RS |
441 | (if (and transient-mark-mode mark-active) |
442 | (setq rstart (region-beginning) | |
443 | rend (copy-marker (region-end))) | |
444 | (setq rstart (point) | |
445 | rend (point-max-marker))) | |
446 | (goto-char rstart)) | |
d2a0ee8b RS |
447 | (let ((case-fold-search (and case-fold-search |
448 | (isearch-no-upper-case-p regexp t)))) | |
449 | (save-excursion | |
e32eb3e6 GM |
450 | (while (and (< (point) rend) |
451 | (re-search-forward regexp rend t)) | |
d2a0ee8b RS |
452 | (delete-region (save-excursion (goto-char (match-beginning 0)) |
453 | (beginning-of-line) | |
454 | (point)) | |
455 | (progn (forward-line 1) (point))))))) | |
698e1804 | 456 | |
e32eb3e6 GM |
457 | |
458 | (defun how-many (regexp &optional rstart rend) | |
d2a0ee8b RS |
459 | "Print number of matches for REGEXP following point. |
460 | ||
461 | If REGEXP contains upper case characters (excluding those preceded by `\\'), | |
e32eb3e6 GM |
462 | the matching is case-sensitive. |
463 | ||
464 | Second and third arg RSTART and REND specify the region to operate on. | |
465 | ||
2ced751f RS |
466 | Interactively, in Transient Mark mode when the mark is active, operate |
467 | on the contents of the region. Otherwise, operate from point to the | |
468 | end of the buffer." | |
469 | ||
e32eb3e6 GM |
470 | (interactive |
471 | (keep-lines-read-args "How many matches for (regexp): ")) | |
f601efb0 SM |
472 | (save-excursion |
473 | (if rstart | |
474 | (goto-char (min rstart rend)) | |
2ced751f RS |
475 | (if (and transient-mark-mode mark-active) |
476 | (setq rstart (region-beginning) | |
477 | rend (copy-marker (region-end))) | |
478 | (setq rstart (point) | |
479 | rend (point-max-marker))) | |
480 | (goto-char rstart)) | |
f601efb0 SM |
481 | (let ((count 0) |
482 | opoint | |
483 | (case-fold-search (and case-fold-search | |
484 | (isearch-no-upper-case-p regexp t)))) | |
485 | (while (and (< (point) rend) | |
486 | (progn (setq opoint (point)) | |
487 | (re-search-forward regexp rend t))) | |
488 | (if (= opoint (point)) | |
489 | (forward-char 1) | |
490 | (setq count (1+ count)))) | |
491 | (message "%d occurrences" count)))) | |
e32eb3e6 | 492 | |
4c53bd2b | 493 | \f |
f601efb0 SM |
494 | (defvar occur-mode-map |
495 | (let ((map (make-sparse-keymap))) | |
496 | (define-key map [mouse-2] 'occur-mode-mouse-goto) | |
497 | (define-key map "\C-c\C-c" 'occur-mode-goto-occurrence) | |
498 | (define-key map "\C-m" 'occur-mode-goto-occurrence) | |
daae70bf | 499 | (define-key map "o" 'occur-mode-goto-occurrence-other-window) |
365486d6 | 500 | (define-key map "\C-o" 'occur-mode-display-occurrence) |
f601efb0 SM |
501 | (define-key map "\M-n" 'occur-next) |
502 | (define-key map "\M-p" 'occur-prev) | |
d99118b0 SS |
503 | (define-key map "r" 'occur-rename-buffer) |
504 | (define-key map "c" 'clone-buffer) | |
f601efb0 | 505 | (define-key map "g" 'revert-buffer) |
d99118b0 SS |
506 | (define-key map "q" 'quit-window) |
507 | (define-key map "z" 'kill-this-buffer) | |
f601efb0 SM |
508 | map) |
509 | "Keymap for `occur-mode'.") | |
698e1804 | 510 | |
46b3d18e RS |
511 | (defvar occur-revert-arguments nil |
512 | "Arguments to pass to `occur-1' to revert an Occur mode buffer. | |
513 | See `occur-revert-function'.") | |
698e1804 | 514 | |
c9ae8cbb JB |
515 | (defcustom occur-mode-hook '(turn-on-font-lock) |
516 | "Hook run when entering Occur mode." | |
517 | :type 'hook | |
518 | :group 'matching) | |
519 | ||
520 | (defcustom occur-hook nil | |
521 | "Hook run when `occur' is called." | |
daae70bf CW |
522 | :type 'hook |
523 | :group 'matching) | |
524 | ||
de3c9b09 | 525 | (put 'occur-mode 'mode-class 'special) |
505847d4 | 526 | (defun occur-mode () |
698e1804 | 527 | "Major mode for output from \\[occur]. |
0081c8a1 RS |
528 | \\<occur-mode-map>Move point to one of the items in this buffer, then use |
529 | \\[occur-mode-goto-occurrence] to go to the occurrence that the item refers to. | |
530 | Alternatively, click \\[occur-mode-mouse-goto] on an item to go to it. | |
531 | ||
698e1804 | 532 | \\{occur-mode-map}" |
ce8b7b9f | 533 | (interactive) |
505847d4 RS |
534 | (kill-all-local-variables) |
535 | (use-local-map occur-mode-map) | |
536 | (setq major-mode 'occur-mode) | |
537 | (setq mode-name "Occur") | |
f601efb0 | 538 | (set (make-local-variable 'revert-buffer-function) 'occur-revert-function) |
c9ae8cbb | 539 | (make-local-variable 'occur-revert-arguments) |
fbefbe33 | 540 | (add-hook 'change-major-mode-hook 'font-lock-defontify nil t) |
c9ae8cbb | 541 | (run-hooks 'occur-mode-hook)) |
698e1804 | 542 | |
a41284da | 543 | (defun occur-revert-function (ignore1 ignore2) |
46b3d18e | 544 | "Handle `revert-buffer' for Occur mode buffers." |
e1690783 | 545 | (apply 'occur-1 (append occur-revert-arguments (list (buffer-name))))) |
a41284da | 546 | |
78bead73 RS |
547 | (defun occur-mode-mouse-goto (event) |
548 | "In Occur mode, go to the occurrence whose line you click on." | |
549 | (interactive "e") | |
46b3d18e | 550 | (let (pos) |
78bead73 RS |
551 | (save-excursion |
552 | (set-buffer (window-buffer (posn-window (event-end event)))) | |
553 | (save-excursion | |
554 | (goto-char (posn-point (event-end event))) | |
46b3d18e RS |
555 | (setq pos (occur-mode-find-occurrence)))) |
556 | (pop-to-buffer (marker-buffer pos)) | |
557 | (goto-char pos))) | |
78bead73 RS |
558 | |
559 | (defun occur-mode-find-occurrence () | |
46b3d18e RS |
560 | (let ((pos (get-text-property (point) 'occur-target))) |
561 | (unless pos | |
68608d9c | 562 | (error "No occurrence on this line")) |
46b3d18e RS |
563 | (unless (buffer-live-p (marker-buffer pos)) |
564 | (error "Buffer for this occurrence was killed")) | |
565 | pos)) | |
78bead73 RS |
566 | |
567 | (defun occur-mode-goto-occurrence () | |
568 | "Go to the occurrence the current line describes." | |
569 | (interactive) | |
46b3d18e RS |
570 | (let ((pos (occur-mode-find-occurrence))) |
571 | (pop-to-buffer (marker-buffer pos)) | |
572 | (goto-char pos))) | |
8d15583f | 573 | |
029024e2 RS |
574 | (defun occur-mode-goto-occurrence-other-window () |
575 | "Go to the occurrence the current line describes, in another window." | |
576 | (interactive) | |
46b3d18e RS |
577 | (let ((pos (occur-mode-find-occurrence))) |
578 | (switch-to-buffer-other-window (marker-buffer pos)) | |
579 | (goto-char pos))) | |
029024e2 | 580 | |
365486d6 RS |
581 | (defun occur-mode-display-occurrence () |
582 | "Display in another window the occurrence the current line describes." | |
583 | (interactive) | |
46b3d18e RS |
584 | (let ((pos (occur-mode-find-occurrence)) |
585 | window | |
586 | ;; Bind these to ensure `display-buffer' puts it in another window. | |
365486d6 | 587 | same-window-buffer-names |
46b3d18e RS |
588 | same-window-regexps) |
589 | (setq window (display-buffer (marker-buffer pos))) | |
365486d6 RS |
590 | ;; This is the way to set point in the proper window. |
591 | (save-selected-window | |
592 | (select-window window) | |
46b3d18e | 593 | (goto-char pos)))) |
365486d6 | 594 | |
123d5548 | 595 | (defun occur-find-match (n search message) |
8d15583f RS |
596 | (if (not n) (setq n 1)) |
597 | (let ((r)) | |
598 | (while (> n 0) | |
123d5548 JB |
599 | (setq r (funcall search (point) 'occur-match)) |
600 | (and r | |
601 | (get-text-property r 'occur-match) | |
602 | (setq r (funcall search r 'occur-match))) | |
8d15583f | 603 | (if r |
123d5548 JB |
604 | (goto-char r) |
605 | (error message)) | |
8d15583f RS |
606 | (setq n (1- n))))) |
607 | ||
123d5548 JB |
608 | (defun occur-next (&optional n) |
609 | "Move to the Nth (default 1) next match in an Occur mode buffer." | |
610 | (interactive "p") | |
611 | (occur-find-match n #'next-single-property-change "No more matches")) | |
612 | ||
8d15583f | 613 | (defun occur-prev (&optional n) |
46b3d18e | 614 | "Move to the Nth (default 1) previous match in an Occur mode buffer." |
8d15583f | 615 | (interactive "p") |
123d5548 | 616 | (occur-find-match n #'previous-single-property-change "No earlier matches")) |
4c53bd2b | 617 | \f |
9d325ebf | 618 | (defcustom list-matching-lines-default-context-lines 0 |
e730be7f DL |
619 | "*Default number of context lines included around `list-matching-lines' matches. |
620 | A negative number means to include that many lines before the match. | |
9d325ebf RS |
621 | A positive number means to include that many lines both before and after." |
622 | :type 'integer | |
623 | :group 'matching) | |
698e1804 | 624 | |
31e1d920 | 625 | (defalias 'list-matching-lines 'occur) |
698e1804 | 626 | |
68608d9c | 627 | (defcustom list-matching-lines-face 'bold |
e730be7f | 628 | "*Face used by \\[list-matching-lines] to show the text that matches. |
68608d9c CW |
629 | If the value is nil, don't highlight the matching portions specially." |
630 | :type 'face | |
631 | :group 'matching) | |
632 | ||
633 | (defcustom list-matching-lines-buffer-name-face 'underline | |
634 | "*Face used by \\[list-matching-lines] to show the names of buffers. | |
635 | If the value is nil, don't highlight the buffer names specially." | |
636 | :type 'face | |
637 | :group 'matching) | |
638 | ||
9e2b2e30 | 639 | (defun occur-accumulate-lines (count &optional no-props) |
68608d9c CW |
640 | (save-excursion |
641 | (let ((forwardp (> count 0)) | |
642 | (result nil)) | |
643 | (while (not (or (zerop count) | |
644 | (if forwardp | |
645 | (eobp) | |
646 | (bobp)))) | |
b0523087 | 647 | (setq count (+ count (if forwardp -1 1))) |
68608d9c | 648 | (push |
9e2b2e30 CW |
649 | (funcall (if no-props |
650 | #'buffer-substring-no-properties | |
651 | #'buffer-substring) | |
68608d9c CW |
652 | (line-beginning-position) |
653 | (line-end-position)) | |
654 | result) | |
655 | (forward-line (if forwardp 1 -1))) | |
656 | (nreverse result)))) | |
657 | ||
658 | (defun occur-read-primary-args () | |
659 | (list (let* ((default (car regexp-history)) | |
660 | (input | |
661 | (read-from-minibuffer | |
662 | (if default | |
663 | (format "List lines matching regexp (default `%s'): " | |
664 | default) | |
665 | "List lines matching regexp: ") | |
666 | nil | |
667 | nil | |
668 | nil | |
669 | 'regexp-history))) | |
670 | (if (equal input "") | |
671 | default | |
672 | input)) | |
bda1925e CW |
673 | (when current-prefix-arg |
674 | (prefix-numeric-value current-prefix-arg)))) | |
c9daced0 | 675 | |
d99118b0 SS |
676 | (defun occur-rename-buffer (&optional unique-p) |
677 | "Rename the current *Occur* buffer to *Occur: original-buffer-name*. | |
678 | Here `original-buffer-name' is the buffer name were occur was originally run. | |
679 | When given the prefix argument, the renaming will not clobber the existing | |
680 | buffer(s) of that name, but use `generate-new-buffer-name' instead. | |
2fb79329 | 681 | You can add this to `occur-hook' if you always want a separate *Occur* |
d99118b0 SS |
682 | buffer for each buffer where you invoke `occur'." |
683 | (interactive "P") | |
684 | (with-current-buffer | |
685 | (if (eq major-mode 'occur-mode) (current-buffer) (get-buffer "*Occur*")) | |
686 | (rename-buffer (concat "*Occur: " | |
687 | (mapconcat #'buffer-name | |
688 | (car (cddr occur-revert-arguments)) "/") | |
689 | "*") | |
690 | unique-p))) | |
691 | ||
698e1804 | 692 | (defun occur (regexp &optional nlines) |
99976f85 | 693 | "Show all lines in the current buffer containing a match for REGEXP. |
da44e784 RM |
694 | |
695 | If a match spreads across multiple lines, all those lines are shown. | |
698e1804 | 696 | |
da44e784 RM |
697 | Each line is displayed with NLINES lines before and after, or -NLINES |
698 | before if NLINES is negative. | |
699 | NLINES defaults to `list-matching-lines-default-context-lines'. | |
698e1804 RS |
700 | Interactively it is the prefix arg. |
701 | ||
4c53bd2b | 702 | The lines are shown in a buffer named `*Occur*'. |
698e1804 | 703 | It serves as a menu to find any of the occurrences in this buffer. |
de3c9b09 | 704 | \\<occur-mode-map>\\[describe-mode] in that buffer will explain how. |
9483d601 | 705 | |
de3c9b09 RS |
706 | If REGEXP contains upper case characters (excluding those preceded by `\\'), |
707 | the matching is case-sensitive." | |
68608d9c CW |
708 | (interactive (occur-read-primary-args)) |
709 | (occur-1 regexp nlines (list (current-buffer)))) | |
710 | ||
711 | (defun multi-occur (bufs regexp &optional nlines) | |
712 | "Show all lines in buffers BUFS containing a match for REGEXP. | |
713 | This function acts on multiple buffers; otherwise, it is exactly like | |
714 | `occur'." | |
a5dfed3e | 715 | (interactive |
68608d9c | 716 | (cons |
52698d45 KS |
717 | (let* ((bufs (list (read-buffer "First buffer to search: " |
718 | (current-buffer) t))) | |
719 | (buf nil) | |
720 | (ido-ignore-item-temp-list bufs)) | |
68608d9c | 721 | (while (not (string-equal |
f1180544 | 722 | (setq buf (read-buffer |
52698d45 KS |
723 | (if (eq read-buffer-function 'ido-read-buffer) |
724 | "Next buffer to search (C-j to end): " | |
725 | "Next buffer to search (RET to end): ") | |
726 | nil t)) | |
68608d9c | 727 | "")) |
52698d45 KS |
728 | (add-to-list 'bufs buf) |
729 | (setq ido-ignore-item-temp-list bufs)) | |
68608d9c CW |
730 | (nreverse (mapcar #'get-buffer bufs))) |
731 | (occur-read-primary-args))) | |
732 | (occur-1 regexp nlines bufs)) | |
733 | ||
734 | (defun multi-occur-by-filename-regexp (bufregexp regexp &optional nlines) | |
04fe158a | 735 | "Show all lines matching REGEXP in buffers named by BUFREGEXP. |
68608d9c CW |
736 | See also `multi-occur'." |
737 | (interactive | |
738 | (cons | |
739 | (let* ((default (car regexp-history)) | |
740 | (input | |
741 | (read-from-minibuffer | |
742 | "List lines in buffers whose filename matches regexp: " | |
743 | nil | |
744 | nil | |
745 | nil | |
746 | 'regexp-history))) | |
747 | (if (equal input "") | |
748 | default | |
749 | input)) | |
750 | (occur-read-primary-args))) | |
751 | (when bufregexp | |
752 | (occur-1 regexp nlines | |
753 | (delq nil | |
754 | (mapcar (lambda (buf) | |
755 | (when (and (buffer-file-name buf) | |
756 | (string-match bufregexp | |
757 | (buffer-file-name buf))) | |
758 | buf)) | |
759 | (buffer-list)))))) | |
760 | ||
e1690783 CW |
761 | (defun occur-1 (regexp nlines bufs &optional buf-name) |
762 | (unless buf-name | |
763 | (setq buf-name "*Occur*")) | |
764 | (let ((occur-buf (get-buffer-create buf-name)) | |
70ed2a76 CW |
765 | (made-temp-buf nil) |
766 | (active-bufs (delq nil (mapcar #'(lambda (buf) | |
767 | (when (buffer-live-p buf) buf)) | |
768 | bufs)))) | |
769 | ;; Handle the case where one of the buffers we're searching is the | |
770 | ;; *Occur* buffer itself. | |
771 | (when (memq occur-buf bufs) | |
772 | (setq occur-buf (with-current-buffer occur-buf | |
773 | (clone-buffer "*Occur-temp*")) | |
774 | made-temp-buf t)) | |
68608d9c CW |
775 | (with-current-buffer occur-buf |
776 | (setq buffer-read-only nil) | |
777 | (occur-mode) | |
778 | (erase-buffer) | |
779 | (let ((count (occur-engine | |
70ed2a76 | 780 | regexp active-bufs occur-buf |
68608d9c CW |
781 | (or nlines list-matching-lines-default-context-lines) |
782 | (and case-fold-search | |
783 | (isearch-no-upper-case-p regexp t)) | |
506a6d7e CW |
784 | list-matching-lines-buffer-name-face |
785 | nil list-matching-lines-face nil))) | |
b2043e7b AS |
786 | (let* ((bufcount (length active-bufs)) |
787 | (diff (- (length bufs) bufcount))) | |
788 | (message "Searched %d buffer%s%s; %s match%s for `%s'" | |
789 | bufcount (if (= bufcount 1) "" "s") | |
790 | (if (zerop diff) "" (format " (%d killed)" diff)) | |
791 | (if (zerop count) "no" (format "%d" count)) | |
792 | (if (= count 1) "" "es") | |
793 | regexp)) | |
70ed2a76 CW |
794 | ;; If we had to make a temporary buffer, make it the *Occur* |
795 | ;; buffer now. | |
796 | (when made-temp-buf | |
e1690783 CW |
797 | (with-current-buffer (get-buffer buf-name) |
798 | (kill-buffer (current-buffer))) | |
799 | (rename-buffer buf-name)) | |
70ed2a76 CW |
800 | (setq occur-revert-arguments (list regexp nlines bufs) |
801 | buffer-read-only t) | |
68608d9c CW |
802 | (if (> count 0) |
803 | (display-buffer occur-buf) | |
2fb79329 JB |
804 | (kill-buffer occur-buf))) |
805 | (run-hooks 'occur-hook)))) | |
68608d9c | 806 | |
46b3d18e RS |
807 | (defun occur-engine-add-prefix (lines) |
808 | (mapcar | |
809 | #'(lambda (line) | |
261cca88 | 810 | (concat " :" line "\n")) |
46b3d18e RS |
811 | lines)) |
812 | ||
813 | (defun occur-engine (regexp buffers out-buf nlines case-fold-search | |
814 | title-face prefix-face match-face keep-props) | |
815 | (with-current-buffer out-buf | |
816 | (setq buffer-read-only nil) | |
5cb4031d KH |
817 | (let ((globalcount 0) |
818 | (coding nil)) | |
46b3d18e RS |
819 | ;; Map over all the buffers |
820 | (dolist (buf buffers) | |
821 | (when (buffer-live-p buf) | |
822 | (let ((matches 0) ;; count of matched lines | |
823 | (lines 1) ;; line count | |
824 | (matchbeg 0) | |
825 | (matchend 0) | |
826 | (origpt nil) | |
827 | (begpt nil) | |
828 | (endpt nil) | |
829 | (marker nil) | |
830 | (curstring "") | |
831 | (headerpt (with-current-buffer out-buf (point)))) | |
832 | (save-excursion | |
833 | (set-buffer buf) | |
5cb4031d KH |
834 | (or coding |
835 | ;; Set CODING only if the current buffer locally | |
836 | ;; binds buffer-file-coding-system. | |
837 | (not (local-variable-p 'buffer-file-coding-system)) | |
838 | (setq coding buffer-file-coding-system)) | |
68608d9c | 839 | (save-excursion |
46b3d18e RS |
840 | (goto-char (point-min)) ;; begin searching in the buffer |
841 | (while (not (eobp)) | |
842 | (setq origpt (point)) | |
843 | (when (setq endpt (re-search-forward regexp nil t)) | |
844 | (setq matches (1+ matches)) ;; increment match count | |
46b3d18e RS |
845 | (setq matchbeg (match-beginning 0) |
846 | matchend (match-end 0)) | |
847 | (setq begpt (save-excursion | |
848 | (goto-char matchbeg) | |
849 | (line-beginning-position))) | |
850 | (setq lines (+ lines (1- (count-lines origpt endpt)))) | |
851 | (setq marker (make-marker)) | |
852 | (set-marker marker matchbeg) | |
853 | (setq curstring (buffer-substring begpt | |
854 | (line-end-position))) | |
855 | ;; Depropertize the string, and maybe | |
856 | ;; highlight the matches | |
857 | (let ((len (length curstring)) | |
858 | (start 0)) | |
859 | (unless keep-props | |
860 | (set-text-properties 0 len nil curstring)) | |
861 | (while (and (< start len) | |
862 | (string-match regexp curstring start)) | |
863 | (add-text-properties (match-beginning 0) | |
864 | (match-end 0) | |
865 | (append | |
506a6d7e | 866 | `(occur-match t) |
46b3d18e | 867 | (when match-face |
506a6d7e | 868 | `(font-lock-face ,match-face))) |
46b3d18e RS |
869 | curstring) |
870 | (setq start (match-end 0)))) | |
871 | ;; Generate the string to insert for this match | |
872 | (let* ((out-line | |
873 | (concat | |
261cca88 RS |
874 | ;; Using 7 digits aligns tabs properly. |
875 | (apply #'propertize (format "%7d:" lines) | |
46b3d18e RS |
876 | (append |
877 | (when prefix-face | |
506a6d7e | 878 | `(font-lock-face prefix-face)) |
46b3d18e RS |
879 | '(occur-prefix t))) |
880 | curstring | |
881 | "\n")) | |
882 | (data | |
883 | (if (= nlines 0) | |
884 | ;; The simple display style | |
885 | out-line | |
886 | ;; The complex multi-line display | |
887 | ;; style. Generate a list of lines, | |
888 | ;; concatenate them all together. | |
889 | (apply #'concat | |
890 | (nconc | |
b0523087 | 891 | (occur-engine-add-prefix (nreverse (cdr (occur-accumulate-lines (- (1+ nlines)) keep-props)))) |
46b3d18e | 892 | (list out-line) |
b0523087 | 893 | (occur-engine-add-prefix (cdr (occur-accumulate-lines (1+ nlines) keep-props)))))))) |
46b3d18e RS |
894 | ;; Actually insert the match display data |
895 | (with-current-buffer out-buf | |
896 | (let ((beg (point)) | |
897 | (end (progn (insert data) (point)))) | |
898 | (unless (= nlines 0) | |
899 | (insert "-------\n")) | |
900 | (add-text-properties | |
e8e2a88c CW |
901 | beg end |
902 | `(occur-target ,marker help-echo "mouse-2: go to this occurrence")) | |
903 | ;; We don't put `mouse-face' on the newline, | |
904 | ;; because that loses. | |
905 | (add-text-properties beg (1- end) '(mouse-face highlight))))) | |
46b3d18e | 906 | (goto-char endpt)) |
e1690783 CW |
907 | (if endpt |
908 | (progn | |
909 | (setq lines (1+ lines)) | |
910 | ;; On to the next match... | |
911 | (forward-line 1)) | |
912 | (goto-char (point-max)))))) | |
46b3d18e | 913 | (when (not (zerop matches)) ;; is the count zero? |
daae70bf | 914 | (setq globalcount (+ globalcount matches)) |
46b3d18e RS |
915 | (with-current-buffer out-buf |
916 | (goto-char headerpt) | |
917 | (let ((beg (point)) | |
918 | end) | |
dbed3cd3 JPW |
919 | (insert (format "%d match%s for \"%s\" in buffer: %s\n" |
920 | matches (if (= matches 1) "" "es") | |
921 | regexp (buffer-name buf))) | |
46b3d18e RS |
922 | (setq end (point)) |
923 | (add-text-properties beg end | |
924 | (append | |
925 | (when title-face | |
506a6d7e CW |
926 | `(font-lock-face ,title-face)) |
927 | `(occur-title ,buf)))) | |
46b3d18e | 928 | (goto-char (point-min))))))) |
5cb4031d KH |
929 | (if coding |
930 | ;; CODING is buffer-file-coding-system of the first buffer | |
931 | ;; that locally binds it. Let's use it also for the output | |
932 | ;; buffer. | |
933 | (set-buffer-file-coding-system coding)) | |
46b3d18e RS |
934 | ;; Return the number of matches |
935 | globalcount))) | |
68608d9c | 936 | |
698e1804 | 937 | \f |
81bdc14d RS |
938 | ;; It would be nice to use \\[...], but there is no reasonable way |
939 | ;; to make that display both SPC and Y. | |
698e1804 RS |
940 | (defconst query-replace-help |
941 | "Type Space or `y' to replace one match, Delete or `n' to skip to next, | |
be44f62c | 942 | RET or `q' to exit, Period to replace one match and exit, |
698e1804 RS |
943 | Comma to replace but not move point immediately, |
944 | C-r to enter recursive edit (\\[exit-recursive-edit] to get out again), | |
945 | C-w to delete match and recursive edit, | |
946 | C-l to clear the screen, redisplay, and offer same replacement again, | |
947 | ! to replace all remaining matches with no more questions, | |
7ce278f3 GM |
948 | ^ to move point back to previous match, |
949 | E to edit the replacement string" | |
f54701d1 | 950 | "Help message while in `query-replace'.") |
698e1804 | 951 | |
81bdc14d RS |
952 | (defvar query-replace-map (make-sparse-keymap) |
953 | "Keymap that defines the responses to questions in `query-replace'. | |
954 | The \"bindings\" in this map are not commands; they are answers. | |
955 | The valid answers include `act', `skip', `act-and-show', | |
956 | `exit', `act-and-exit', `edit', `delete-and-edit', `recenter', | |
d9121bc0 | 957 | `automatic', `backup', `exit-prefix', and `help'.") |
81bdc14d RS |
958 | |
959 | (define-key query-replace-map " " 'act) | |
960 | (define-key query-replace-map "\d" 'skip) | |
961 | (define-key query-replace-map [delete] 'skip) | |
9275e5e3 | 962 | (define-key query-replace-map [backspace] 'skip) |
81bdc14d RS |
963 | (define-key query-replace-map "y" 'act) |
964 | (define-key query-replace-map "n" 'skip) | |
633a305a RS |
965 | (define-key query-replace-map "Y" 'act) |
966 | (define-key query-replace-map "N" 'skip) | |
34724fcb | 967 | (define-key query-replace-map "e" 'edit-replacement) |
7ce278f3 | 968 | (define-key query-replace-map "E" 'edit-replacement) |
81bdc14d | 969 | (define-key query-replace-map "," 'act-and-show) |
81bdc14d | 970 | (define-key query-replace-map "q" 'exit) |
919592c0 | 971 | (define-key query-replace-map "\r" 'exit) |
384c7da4 | 972 | (define-key query-replace-map [return] 'exit) |
81bdc14d RS |
973 | (define-key query-replace-map "." 'act-and-exit) |
974 | (define-key query-replace-map "\C-r" 'edit) | |
975 | (define-key query-replace-map "\C-w" 'delete-and-edit) | |
976 | (define-key query-replace-map "\C-l" 'recenter) | |
977 | (define-key query-replace-map "!" 'automatic) | |
978 | (define-key query-replace-map "^" 'backup) | |
979 | (define-key query-replace-map "\C-h" 'help) | |
e731045a KH |
980 | (define-key query-replace-map [f1] 'help) |
981 | (define-key query-replace-map [help] 'help) | |
81bdc14d | 982 | (define-key query-replace-map "?" 'help) |
bc6312e1 RS |
983 | (define-key query-replace-map "\C-g" 'quit) |
984 | (define-key query-replace-map "\C-]" 'quit) | |
d9121bc0 RS |
985 | (define-key query-replace-map "\e" 'exit-prefix) |
986 | (define-key query-replace-map [escape] 'exit-prefix) | |
81bdc14d | 987 | |
84482eb3 | 988 | (defun replace-match-string-symbols (n) |
e730be7f DL |
989 | "Process a list (and any sub-lists), expanding certain symbols. |
990 | Symbol Expands To | |
991 | N (match-string N) (where N is a string of digits) | |
992 | #N (string-to-number (match-string N)) | |
993 | & (match-string 0) | |
994 | #& (string-to-number (match-string 0)) | |
995 | ||
996 | Note that these symbols must be preceeded by a backslash in order to | |
997 | type them." | |
84482eb3 RS |
998 | (while n |
999 | (cond | |
1000 | ((consp (car n)) | |
1001 | (replace-match-string-symbols (car n))) ;Process sub-list | |
1002 | ((symbolp (car n)) | |
1003 | (let ((name (symbol-name (car n)))) | |
1004 | (cond | |
1005 | ((string-match "^[0-9]+$" name) | |
1006 | (setcar n (list 'match-string (string-to-number name)))) | |
1007 | ((string-match "^#[0-9]+$" name) | |
1008 | (setcar n (list 'string-to-number | |
1009 | (list 'match-string | |
1010 | (string-to-number (substring name 1)))))) | |
1011 | ((string= "&" name) | |
1012 | (setcar n '(match-string 0))) | |
1013 | ((string= "#&" name) | |
1014 | (setcar n '(string-to-number (match-string 0)))))))) | |
1015 | (setq n (cdr n)))) | |
1016 | ||
1017 | (defun replace-eval-replacement (expression replace-count) | |
1018 | (let ((replacement (eval expression))) | |
1019 | (if (stringp replacement) | |
1020 | replacement | |
1021 | (prin1-to-string replacement t)))) | |
1022 | ||
1023 | (defun replace-loop-through-replacements (data replace-count) | |
1024 | ;; DATA is a vector contaning the following values: | |
1025 | ;; 0 next-rotate-count | |
1026 | ;; 1 repeat-count | |
1027 | ;; 2 next-replacement | |
1028 | ;; 3 replacements | |
1029 | (if (= (aref data 0) replace-count) | |
1030 | (progn | |
1031 | (aset data 0 (+ replace-count (aref data 1))) | |
1032 | (let ((next (cdr (aref data 2)))) | |
1033 | (aset data 2 (if (consp next) next (aref data 3)))))) | |
1034 | (car (aref data 2))) | |
1035 | ||
d99118b0 | 1036 | (defun perform-replace (from-string replacements |
698e1804 | 1037 | query-flag regexp-flag delimited-flag |
99a7559f | 1038 | &optional repeat-count map start end) |
698e1804 RS |
1039 | "Subroutine of `query-replace'. Its complexity handles interactive queries. |
1040 | Don't use this in your own program unless you want to query and set the mark | |
1041 | just as `query-replace' does. Instead, write a simple loop like this: | |
698665d1 GM |
1042 | |
1043 | (while (re-search-forward \"foo[ \\t]+bar\" nil t) | |
698e1804 | 1044 | (replace-match \"foobar\" nil nil)) |
698665d1 GM |
1045 | |
1046 | which will run faster and probably do exactly what you want. Please | |
1047 | see the documentation of `replace-match' to find out how to simulate | |
588c915a CW |
1048 | `case-replace'. |
1049 | ||
1050 | This function returns nil if and only if there were no matches to | |
1051 | make, or the user didn't cancel the call." | |
81bdc14d | 1052 | (or map (setq map query-replace-map)) |
1c1dadab RS |
1053 | (and query-flag minibuffer-auto-raise |
1054 | (raise-frame (window-frame (minibuffer-window)))) | |
698e1804 RS |
1055 | (let ((nocasify (not (and case-fold-search case-replace |
1056 | (string-equal from-string | |
1057 | (downcase from-string))))) | |
5a78b471 KH |
1058 | (case-fold-search (and case-fold-search |
1059 | (string-equal from-string | |
1060 | (downcase from-string)))) | |
d83a97ab | 1061 | (literal (or (not regexp-flag) (eq regexp-flag 'literal))) |
698e1804 RS |
1062 | (search-function (if regexp-flag 're-search-forward 'search-forward)) |
1063 | (search-string from-string) | |
e5d77022 | 1064 | (real-match-data nil) ; the match data for the current match |
698e1804 | 1065 | (next-replacement nil) |
698e1804 RS |
1066 | (keep-going t) |
1067 | (stack nil) | |
698e1804 | 1068 | (replace-count 0) |
5632eb27 PE |
1069 | (nonempty-match nil) |
1070 | ||
7ef5c431 KH |
1071 | ;; If non-nil, it is marker saying where in the buffer to stop. |
1072 | (limit nil) | |
1073 | ||
5632eb27 PE |
1074 | ;; Data for the next match. If a cons, it has the same format as |
1075 | ;; (match-data); otherwise it is t if a match is possible at point. | |
ae4eb03c | 1076 | (match-again t) |
5632eb27 | 1077 | |
02d95a27 RS |
1078 | (message |
1079 | (if query-flag | |
1080 | (substitute-command-keys | |
1081 | "Query replacing %s with %s: (\\<query-replace-map>\\[help] for help) ")))) | |
7ef5c431 KH |
1082 | |
1083 | ;; If region is active, in Transient Mark mode, operate on region. | |
47d72254 GM |
1084 | (when start |
1085 | (setq limit (copy-marker (max start end))) | |
1086 | (goto-char (min start end)) | |
1087 | (deactivate-mark)) | |
84482eb3 RS |
1088 | |
1089 | ;; REPLACEMENTS is either a string, a list of strings, or a cons cell | |
1090 | ;; containing a function and its first argument. The function is | |
1091 | ;; called to generate each replacement like this: | |
1092 | ;; (funcall (car replacements) (cdr replacements) replace-count) | |
1093 | ;; It must return a string. | |
1094 | (cond | |
1095 | ((stringp replacements) | |
1096 | (setq next-replacement replacements | |
1097 | replacements nil)) | |
1098 | ((stringp (car replacements)) ; If it isn't a string, it must be a cons | |
1099 | (or repeat-count (setq repeat-count 1)) | |
1100 | (setq replacements (cons 'replace-loop-through-replacements | |
1101 | (vector repeat-count repeat-count | |
1102 | replacements replacements))))) | |
1103 | ||
698e1804 RS |
1104 | (if delimited-flag |
1105 | (setq search-function 're-search-forward | |
1106 | search-string (concat "\\b" | |
1107 | (if regexp-flag from-string | |
1108 | (regexp-quote from-string)) | |
1109 | "\\b"))) | |
1110 | (push-mark) | |
1111 | (undo-boundary) | |
e782e9f2 RS |
1112 | (unwind-protect |
1113 | ;; Loop finding occurrences that perhaps should be replaced. | |
1114 | (while (and keep-going | |
1115 | (not (eobp)) | |
5632eb27 PE |
1116 | ;; Use the next match if it is already known; |
1117 | ;; otherwise, search for a match after moving forward | |
1118 | ;; one char if progress is required. | |
1119 | (setq real-match-data | |
1120 | (if (consp match-again) | |
1121 | (progn (goto-char (nth 1 match-again)) | |
1122 | match-again) | |
1123 | (and (or match-again | |
889617de GM |
1124 | ;; MATCH-AGAIN non-nil means we |
1125 | ;; accept an adjacent match. If | |
1126 | ;; we don't, move one char to the | |
1127 | ;; right. This takes us a | |
1128 | ;; character too far at the end, | |
1129 | ;; but this is undone after the | |
1130 | ;; while-loop. | |
8f3ff96b | 1131 | (progn (forward-char 1) (not (eobp)))) |
7ef5c431 | 1132 | (funcall search-function search-string limit t) |
5632eb27 PE |
1133 | ;; For speed, use only integers and |
1134 | ;; reuse the list used last time. | |
1135 | (match-data t real-match-data))))) | |
1c4fe319 RS |
1136 | ;; Optionally ignore matches that have a read-only property. |
1137 | (unless (and query-replace-skip-read-only | |
1138 | (text-property-not-all | |
1139 | (match-beginning 0) (match-end 0) | |
1140 | 'read-only nil)) | |
1141 | ||
1142 | ;; Record whether the match is nonempty, to avoid an infinite loop | |
1143 | ;; repeatedly matching the same empty string. | |
1144 | (setq nonempty-match | |
1145 | (/= (nth 0 real-match-data) (nth 1 real-match-data))) | |
1146 | ||
1147 | ;; If the match is empty, record that the next one can't be | |
1148 | ;; adjacent. | |
1149 | ||
1150 | ;; Otherwise, if matching a regular expression, do the next | |
1151 | ;; match now, since the replacement for this match may | |
1152 | ;; affect whether the next match is adjacent to this one. | |
1153 | ;; If that match is empty, don't use it. | |
1154 | (setq match-again | |
1155 | (and nonempty-match | |
1156 | (or (not regexp-flag) | |
1157 | (and (looking-at search-string) | |
1158 | (let ((match (match-data))) | |
1159 | (and (/= (nth 0 match) (nth 1 match)) | |
1160 | match)))))) | |
1161 | ||
1162 | ;; Calculate the replacement string, if necessary. | |
1163 | (when replacements | |
1164 | (set-match-data real-match-data) | |
1165 | (setq next-replacement | |
1166 | (funcall (car replacements) (cdr replacements) | |
1167 | replace-count))) | |
1168 | (if (not query-flag) | |
1169 | (let ((inhibit-read-only query-replace-skip-read-only)) | |
1170 | (set-match-data real-match-data) | |
1171 | (replace-match next-replacement nocasify literal) | |
1172 | (setq replace-count (1+ replace-count))) | |
1173 | (undo-boundary) | |
1174 | (let (done replaced key def) | |
1175 | ;; Loop reading commands until one of them sets done, | |
1176 | ;; which means it has finished handling this occurrence. | |
1177 | (while (not done) | |
1178 | (set-match-data real-match-data) | |
1179 | (replace-highlight (match-beginning 0) (match-end 0)) | |
1180 | ;; Bind message-log-max so we don't fill up the message log | |
1181 | ;; with a bunch of identical messages. | |
1182 | (let ((message-log-max nil)) | |
1183 | (message message from-string next-replacement)) | |
1184 | (setq key (read-event)) | |
1185 | ;; Necessary in case something happens during read-event | |
1186 | ;; that clobbers the match data. | |
1187 | (set-match-data real-match-data) | |
1188 | (setq key (vector key)) | |
1189 | (setq def (lookup-key map key)) | |
1190 | ;; Restore the match data while we process the command. | |
1191 | (cond ((eq def 'help) | |
1192 | (with-output-to-temp-buffer "*Help*" | |
1193 | (princ | |
1194 | (concat "Query replacing " | |
1195 | (if regexp-flag "regexp " "") | |
1196 | from-string " with " | |
1197 | next-replacement ".\n\n" | |
1198 | (substitute-command-keys | |
1199 | query-replace-help))) | |
1200 | (with-current-buffer standard-output | |
1201 | (help-mode)))) | |
1202 | ((eq def 'exit) | |
1203 | (setq keep-going nil) | |
1204 | (setq done t)) | |
1205 | ((eq def 'backup) | |
1206 | (if stack | |
588c915a | 1207 | (let ((elt (pop stack))) |
1c4fe319 RS |
1208 | (goto-char (car elt)) |
1209 | (setq replaced (eq t (cdr elt))) | |
1210 | (or replaced | |
588c915a | 1211 | (set-match-data (cdr elt)))) |
1c4fe319 RS |
1212 | (message "No previous match") |
1213 | (ding 'no-terminate) | |
1214 | (sit-for 1))) | |
1215 | ((eq def 'act) | |
1216 | (or replaced | |
1217 | (progn | |
1218 | (replace-match next-replacement nocasify literal) | |
1219 | (setq replace-count (1+ replace-count)))) | |
1220 | (setq done t replaced t)) | |
1221 | ((eq def 'act-and-exit) | |
1222 | (or replaced | |
1223 | (progn | |
1224 | (replace-match next-replacement nocasify literal) | |
1225 | (setq replace-count (1+ replace-count)))) | |
1226 | (setq keep-going nil) | |
1227 | (setq done t replaced t)) | |
1228 | ((eq def 'act-and-show) | |
1229 | (if (not replaced) | |
1230 | (progn | |
1231 | (replace-match next-replacement nocasify literal) | |
1232 | (setq replace-count (1+ replace-count)) | |
1233 | (setq replaced t)))) | |
1234 | ((eq def 'automatic) | |
1235 | (or replaced | |
1236 | (progn | |
1237 | (replace-match next-replacement nocasify literal) | |
1238 | (setq replace-count (1+ replace-count)))) | |
1239 | (setq done t query-flag nil replaced t)) | |
1240 | ((eq def 'skip) | |
1241 | (setq done t)) | |
1242 | ((eq def 'recenter) | |
1243 | (recenter nil)) | |
1244 | ((eq def 'edit) | |
1245 | (let ((opos (point-marker))) | |
1246 | (goto-char (match-beginning 0)) | |
1247 | (save-excursion | |
1248 | (funcall search-function search-string limit t) | |
1249 | (setq real-match-data (match-data))) | |
86914dcc RS |
1250 | (save-excursion |
1251 | (save-window-excursion | |
1252 | (recursive-edit))) | |
1c4fe319 RS |
1253 | (goto-char opos)) |
1254 | (set-match-data real-match-data) | |
1255 | ;; Before we make the replacement, | |
1256 | ;; decide whether the search string | |
1257 | ;; can match again just after this match. | |
1258 | (if (and regexp-flag nonempty-match) | |
1259 | (setq match-again (and (looking-at search-string) | |
1260 | (match-data))))) | |
d99118b0 | 1261 | |
1c4fe319 RS |
1262 | ;; Edit replacement. |
1263 | ((eq def 'edit-replacement) | |
1264 | (setq next-replacement | |
1265 | (read-input "Edit replacement string: " | |
1266 | next-replacement)) | |
1267 | (or replaced | |
1268 | (replace-match next-replacement nocasify literal)) | |
1269 | (setq done t)) | |
d99118b0 | 1270 | |
1c4fe319 RS |
1271 | ((eq def 'delete-and-edit) |
1272 | (delete-region (match-beginning 0) (match-end 0)) | |
1273 | (set-match-data | |
1274 | (prog1 (match-data) | |
1275 | (save-excursion (recursive-edit)))) | |
1276 | (setq replaced t)) | |
1277 | ;; Note: we do not need to treat `exit-prefix' | |
1278 | ;; specially here, since we reread | |
1279 | ;; any unrecognized character. | |
1280 | (t | |
1281 | (setq this-command 'mode-exited) | |
1282 | (setq keep-going nil) | |
1283 | (setq unread-command-events | |
1284 | (append (listify-key-sequence key) | |
1285 | unread-command-events)) | |
1286 | (setq done t)))) | |
1287 | ;; Record previous position for ^ when we move on. | |
1288 | ;; Change markers to numbers in the match data | |
1289 | ;; since lots of markers slow down editing. | |
1290 | (setq stack | |
1291 | (cons (cons (point) | |
1292 | (or replaced (match-data t))) | |
1293 | stack)))))) | |
889617de GM |
1294 | |
1295 | ;; The code preventing adjacent regexp matches in the condition | |
1296 | ;; of the while-loop above will haven taken us one character | |
1297 | ;; beyond the last replacement. Undo that. | |
1298 | (when (and regexp-flag (not match-again) (> replace-count 0)) | |
1299 | (backward-char 1)) | |
d99118b0 | 1300 | |
e782e9f2 | 1301 | (replace-dehighlight)) |
4d33492a RS |
1302 | (or unread-command-events |
1303 | (message "Replaced %d occurrence%s" | |
1304 | replace-count | |
1305 | (if (= replace-count 1) "" "s"))) | |
1306 | (and keep-going stack))) | |
698e1804 | 1307 | |
95807e68 | 1308 | (defcustom query-replace-highlight t |
9d325ebf RS |
1309 | "*Non-nil means to highlight words during query replacement." |
1310 | :type 'boolean | |
1311 | :group 'matching) | |
e782e9f2 RS |
1312 | |
1313 | (defvar replace-overlay nil) | |
1314 | ||
1315 | (defun replace-dehighlight () | |
1316 | (and replace-overlay | |
1317 | (progn | |
1318 | (delete-overlay replace-overlay) | |
1319 | (setq replace-overlay nil)))) | |
1320 | ||
1321 | (defun replace-highlight (start end) | |
1322 | (and query-replace-highlight | |
1323 | (progn | |
1324 | (or replace-overlay | |
1325 | (progn | |
1326 | (setq replace-overlay (make-overlay start end)) | |
1327 | (overlay-put replace-overlay 'face | |
e730be7f | 1328 | (if (facep 'query-replace) |
e782e9f2 RS |
1329 | 'query-replace 'region)))) |
1330 | (move-overlay replace-overlay start end (current-buffer))))) | |
1331 | ||
ab5796a9 | 1332 | ;;; arch-tag: 16b4cd61-fd40-497b-b86f-b667c4cf88e4 |
c88ab9ce | 1333 | ;;; replace.el ends here |