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