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