Commit | Line | Data |
---|---|---|
c88ab9ce ER |
1 | ;;; replace.el --- replace commands for Emacs. |
2 | ||
eab69997 | 3 | ;; Copyright (C) 1985, 1986, 1987, 1992, 1994 Free Software Foundation, Inc. |
3a801d0c | 4 | |
698e1804 RS |
5 | ;; This file is part of GNU Emacs. |
6 | ||
7 | ;; GNU Emacs is free software; you can redistribute it and/or modify | |
8 | ;; it under the terms of the GNU General Public License as published by | |
e5d77022 | 9 | ;; the Free Software Foundation; either version 2, or (at your option) |
698e1804 RS |
10 | ;; any later version. |
11 | ||
12 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
13 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | ;; GNU General Public License for more details. | |
16 | ||
17 | ;; You should have received a copy of the GNU General Public License | |
18 | ;; along with GNU Emacs; see the file COPYING. If not, write to | |
19 | ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. | |
20 | ||
d9ecc911 ER |
21 | ;;; Commentary: |
22 | ||
23 | ;; This package supplies the string and regular-expression replace functions | |
24 | ;; documented in the Emacs user's manual. | |
25 | ||
4f4b8eff | 26 | ;;; Code: |
698e1804 | 27 | |
73fa8346 BP |
28 | (defconst case-replace t "\ |
29 | *Non-nil means query-replace should preserve case in replacements.") | |
77176e73 | 30 | |
770970cb RS |
31 | (defvar query-replace-history nil) |
32 | ||
151270f3 RS |
33 | (defvar query-replace-interactive nil |
34 | "Non-nil means `query-replace' uses the last search string. | |
35 | That becomes the \"string to replace\".") | |
36 | ||
37 | (defun query-replace-read-args (string regexp-flag) | |
770970cb | 38 | (let (from to) |
151270f3 RS |
39 | (if query-replace-interactive |
40 | (setq from (car (if regexp-flag regexp-search-ring search-ring))) | |
41 | (setq from (read-from-minibuffer (format "%s: " string) | |
42 | nil nil nil | |
43 | 'query-replace-history))) | |
770970cb RS |
44 | (setq to (read-from-minibuffer (format "%s %s with: " string from) |
45 | nil nil nil | |
46 | 'query-replace-history)) | |
47 | (list from to current-prefix-arg))) | |
48 | ||
da44e784 RM |
49 | (defun query-replace (from-string to-string &optional arg) |
50 | "Replace some occurrences of FROM-STRING with TO-STRING. | |
51 | As each match is found, the user must type a character saying | |
52 | what to do with it. For directions, type \\[help-command] at that time. | |
53 | ||
151270f3 RS |
54 | If `query-replace-interactive' is non-nil, the last incremental search |
55 | string is used as FROM-STRING--you don't have to specify it with the | |
56 | minibuffer. | |
57 | ||
118a01c9 | 58 | Preserves case in each replacement if `case-replace' and `case-fold-search' |
da44e784 | 59 | are non-nil and FROM-STRING has no uppercase letters. |
118a01c9 | 60 | Third arg DELIMITED (prefix arg if interactive), if non-nil, means replace |
81bdc14d RS |
61 | only matches surrounded by word boundaries. |
62 | ||
63 | To customize possible responses, change the \"bindings\" in `query-replace-map'." | |
151270f3 | 64 | (interactive (query-replace-read-args "Query replace" nil)) |
da44e784 | 65 | (perform-replace from-string to-string t nil arg) |
81bdc14d | 66 | (or unread-command-events (message "Done"))) |
73fa8346 | 67 | (define-key esc-map "%" 'query-replace) |
da44e784 | 68 | |
da44e784 RM |
69 | (defun query-replace-regexp (regexp to-string &optional arg) |
70 | "Replace some things after point matching REGEXP with TO-STRING. | |
71 | As each match is found, the user must type a character saying | |
72 | what to do with it. For directions, type \\[help-command] at that time. | |
73 | ||
151270f3 RS |
74 | If `query-replace-interactive' is non-nil, the last incremental search |
75 | regexp is used as REGEXP--you don't have to specify it with the | |
76 | minibuffer. | |
77 | ||
118a01c9 | 78 | Preserves case in each replacement if `case-replace' and `case-fold-search' |
da44e784 | 79 | are non-nil and REGEXP has no uppercase letters. |
118a01c9 | 80 | Third arg DELIMITED (prefix arg if interactive), if non-nil, means replace |
da44e784 | 81 | only matches surrounded by word boundaries. |
118a01c9 RS |
82 | In TO-STRING, `\\&' stands for whatever matched the whole of REGEXP, |
83 | and `\\=\\N' (where N is a digit) stands for | |
84 | whatever what matched the Nth `\\(...\\)' in REGEXP." | |
151270f3 | 85 | (interactive (query-replace-read-args "Query replace regexp" t)) |
da44e784 | 86 | (perform-replace regexp to-string t t arg) |
81bdc14d | 87 | (or unread-command-events (message "Done"))) |
da44e784 | 88 | |
da44e784 RM |
89 | (defun map-query-replace-regexp (regexp to-strings &optional arg) |
90 | "Replace some matches for REGEXP with various strings, in rotation. | |
91 | The second argument TO-STRINGS contains the replacement strings, separated | |
92 | by spaces. This command works like `query-replace-regexp' except | |
93 | that each successive replacement uses the next successive replacement string, | |
94 | wrapping around from the last such string to the first. | |
95 | ||
96 | Non-interactively, TO-STRINGS may be a list of replacement strings. | |
97 | ||
151270f3 RS |
98 | If `query-replace-interactive' is non-nil, the last incremental search |
99 | regexp is used as REGEXP--you don't have to specify it with the minibuffer. | |
100 | ||
da44e784 RM |
101 | A prefix argument N says to use each replacement string N times |
102 | before rotating to the next." | |
770970cb RS |
103 | (interactive |
104 | (let (from to) | |
151270f3 RS |
105 | (setq from (if query-replace-interactive |
106 | (car regexp-search-ring) | |
107 | (read-from-minibuffer "Map query replace (regexp): " | |
108 | nil nil nil | |
109 | 'query-replace-history))) | |
770970cb RS |
110 | (setq to (read-from-minibuffer |
111 | (format "Query replace %s with (space-separated strings): " | |
112 | from) | |
113 | nil nil nil | |
114 | 'query-replace-history)) | |
115 | (list from to current-prefix-arg))) | |
da44e784 RM |
116 | (let (replacements) |
117 | (if (listp to-strings) | |
118 | (setq replacements to-strings) | |
119 | (while (/= (length to-strings) 0) | |
120 | (if (string-match " " to-strings) | |
121 | (setq replacements | |
122 | (append replacements | |
123 | (list (substring to-strings 0 | |
124 | (string-match " " to-strings)))) | |
125 | to-strings (substring to-strings | |
126 | (1+ (string-match " " to-strings)))) | |
127 | (setq replacements (append replacements (list to-strings)) | |
128 | to-strings "")))) | |
129 | (perform-replace regexp replacements t t nil arg)) | |
81bdc14d | 130 | (or unread-command-events (message "Done"))) |
da44e784 | 131 | |
da44e784 RM |
132 | (defun replace-string (from-string to-string &optional delimited) |
133 | "Replace occurrences of FROM-STRING with TO-STRING. | |
134 | Preserve case in each match if `case-replace' and `case-fold-search' | |
135 | are non-nil and FROM-STRING has no uppercase letters. | |
118a01c9 | 136 | Third arg DELIMITED (prefix arg if interactive), if non-nil, means replace |
da44e784 RM |
137 | only matches surrounded by word boundaries. |
138 | ||
151270f3 RS |
139 | If `query-replace-interactive' is non-nil, the last incremental search |
140 | string is used as FROM-STRING--you don't have to specify it with the | |
141 | minibuffer. | |
142 | ||
da44e784 RM |
143 | This function is usually the wrong thing to use in a Lisp program. |
144 | What you probably want is a loop like this: | |
118a01c9 RS |
145 | (while (search-forward FROM-STRING nil t) |
146 | (replace-match TO-STRING nil t)) | |
da44e784 | 147 | which will run faster and will not set the mark or print anything." |
151270f3 | 148 | (interactive (query-replace-read-args "Replace string" nil)) |
da44e784 | 149 | (perform-replace from-string to-string nil nil delimited) |
81bdc14d | 150 | (or unread-command-events (message "Done"))) |
da44e784 | 151 | |
da44e784 RM |
152 | (defun replace-regexp (regexp to-string &optional delimited) |
153 | "Replace things after point matching REGEXP with TO-STRING. | |
118a01c9 | 154 | Preserve case in each match if `case-replace' and `case-fold-search' |
da44e784 | 155 | are non-nil and REGEXP has no uppercase letters. |
118a01c9 | 156 | Third arg DELIMITED (prefix arg if interactive), if non-nil, means replace |
da44e784 | 157 | only matches surrounded by word boundaries. |
118a01c9 RS |
158 | In TO-STRING, `\\&' stands for whatever matched the whole of REGEXP, |
159 | and `\\=\\N' (where N is a digit) stands for | |
880f22a1 | 160 | whatever what matched the Nth `\\(...\\)' in REGEXP. |
da44e784 | 161 | |
151270f3 RS |
162 | If `query-replace-interactive' is non-nil, the last incremental search |
163 | regexp is used as REGEXP--you don't have to specify it with the minibuffer. | |
164 | ||
da44e784 RM |
165 | This function is usually the wrong thing to use in a Lisp program. |
166 | What you probably want is a loop like this: | |
167 | (while (re-search-forward REGEXP nil t) | |
118a01c9 | 168 | (replace-match TO-STRING nil nil)) |
da44e784 | 169 | which will run faster and will not set the mark or print anything." |
151270f3 | 170 | (interactive (query-replace-read-args "Replace regexp" t)) |
da44e784 | 171 | (perform-replace regexp to-string nil t delimited) |
81bdc14d | 172 | (or unread-command-events (message "Done"))) |
4c53bd2b RS |
173 | \f |
174 | (defvar regexp-history nil | |
175 | "History list for some commands that read regular expressions.") | |
da44e784 | 176 | |
31e1d920 | 177 | (defalias 'delete-non-matching-lines 'keep-lines) |
698e1804 RS |
178 | (defun keep-lines (regexp) |
179 | "Delete all lines except those containing matches for REGEXP. | |
180 | A match split across lines preserves all the lines it lies in. | |
181 | Applies to all lines after point." | |
4c53bd2b | 182 | (interactive (list (read-from-minibuffer |
72f21cdf | 183 | "Keep lines (containing match for regexp): " |
4c53bd2b | 184 | nil nil nil 'regexp-history))) |
698e1804 RS |
185 | (save-excursion |
186 | (or (bolp) (forward-line 1)) | |
187 | (let ((start (point))) | |
188 | (while (not (eobp)) | |
189 | ;; Start is first char not preserved by previous match. | |
190 | (if (not (re-search-forward regexp nil 'move)) | |
191 | (delete-region start (point-max)) | |
192 | (let ((end (save-excursion (goto-char (match-beginning 0)) | |
193 | (beginning-of-line) | |
194 | (point)))) | |
195 | ;; Now end is first char preserved by the new match. | |
196 | (if (< start end) | |
197 | (delete-region start end)))) | |
198 | (setq start (save-excursion (forward-line 1) | |
199 | (point))) | |
200 | ;; If the match was empty, avoid matching again at same place. | |
201 | (and (not (eobp)) (= (match-beginning 0) (match-end 0)) | |
202 | (forward-char 1)))))) | |
203 | ||
31e1d920 | 204 | (defalias 'delete-matching-lines 'flush-lines) |
698e1804 RS |
205 | (defun flush-lines (regexp) |
206 | "Delete lines containing matches for REGEXP. | |
207 | If a match is split across lines, all the lines it lies in are deleted. | |
208 | Applies to lines after point." | |
4c53bd2b | 209 | (interactive (list (read-from-minibuffer |
72f21cdf | 210 | "Flush lines (containing match for regexp): " |
4c53bd2b | 211 | nil nil nil 'regexp-history))) |
698e1804 RS |
212 | (save-excursion |
213 | (while (and (not (eobp)) | |
214 | (re-search-forward regexp nil t)) | |
215 | (delete-region (save-excursion (goto-char (match-beginning 0)) | |
216 | (beginning-of-line) | |
217 | (point)) | |
218 | (progn (forward-line 1) (point)))))) | |
219 | ||
31e1d920 | 220 | (defalias 'count-matches 'how-many) |
698e1804 RS |
221 | (defun how-many (regexp) |
222 | "Print number of matches for REGEXP following point." | |
4c53bd2b | 223 | (interactive (list (read-from-minibuffer |
72f21cdf | 224 | "How many matches for (regexp): " |
4c53bd2b | 225 | nil nil nil 'regexp-history))) |
698e1804 RS |
226 | (let ((count 0) opoint) |
227 | (save-excursion | |
228 | (while (and (not (eobp)) | |
229 | (progn (setq opoint (point)) | |
230 | (re-search-forward regexp nil t))) | |
231 | (if (= opoint (point)) | |
232 | (forward-char 1) | |
233 | (setq count (1+ count)))) | |
234 | (message "%d occurrences" count)))) | |
4c53bd2b | 235 | \f |
698e1804 RS |
236 | (defvar occur-mode-map ()) |
237 | (if occur-mode-map | |
238 | () | |
239 | (setq occur-mode-map (make-sparse-keymap)) | |
78bead73 | 240 | (define-key occur-mode-map [mouse-2] 'occur-mode-mouse-goto) |
698e1804 RS |
241 | (define-key occur-mode-map "\C-c\C-c" 'occur-mode-goto-occurrence)) |
242 | ||
243 | (defvar occur-buffer nil) | |
244 | (defvar occur-nlines nil) | |
245 | (defvar occur-pos-list nil) | |
246 | ||
247 | (defun occur-mode () | |
248 | "Major mode for output from \\[occur]. | |
249 | Move point to one of the occurrences in this buffer, | |
250 | then use \\[occur-mode-goto-occurrence] to go to the same occurrence | |
251 | in the buffer that the occurrences were found in. | |
26649bdf | 252 | Or click \\<occur-mode-map>\\[occur-mode-mouse-goto] on an occurrence line. |
698e1804 RS |
253 | \\{occur-mode-map}" |
254 | (kill-all-local-variables) | |
255 | (use-local-map occur-mode-map) | |
256 | (setq major-mode 'occur-mode) | |
257 | (setq mode-name "Occur") | |
258 | (make-local-variable 'occur-buffer) | |
259 | (make-local-variable 'occur-nlines) | |
4baf5620 RS |
260 | (make-local-variable 'occur-pos-list) |
261 | (run-hooks 'occur-mode-hook)) | |
698e1804 | 262 | |
78bead73 RS |
263 | (defun occur-mode-mouse-goto (event) |
264 | "In Occur mode, go to the occurrence whose line you click on." | |
265 | (interactive "e") | |
266 | (let (buffer pos) | |
267 | (save-excursion | |
268 | (set-buffer (window-buffer (posn-window (event-end event)))) | |
269 | (save-excursion | |
270 | (goto-char (posn-point (event-end event))) | |
271 | (setq pos (occur-mode-find-occurrence)) | |
272 | (setq buffer occur-buffer))) | |
273 | (pop-to-buffer buffer) | |
274 | (goto-char (marker-position pos)))) | |
275 | ||
276 | (defun occur-mode-find-occurrence () | |
698e1804 RS |
277 | (if (or (null occur-buffer) |
278 | (null (buffer-name occur-buffer))) | |
279 | (progn | |
280 | (setq occur-buffer nil | |
281 | occur-pos-list nil) | |
282 | (error "Buffer in which occurrences were found is deleted"))) | |
78bead73 RS |
283 | (let* ((line-count |
284 | (count-lines (point-min) | |
285 | (save-excursion | |
286 | (beginning-of-line) | |
287 | (point)))) | |
288 | (occur-number (save-excursion | |
698e1804 | 289 | (beginning-of-line) |
78bead73 | 290 | (/ (1- line-count) |
698e1804 RS |
291 | (cond ((< occur-nlines 0) |
292 | (- 2 occur-nlines)) | |
293 | ((> occur-nlines 0) | |
294 | (+ 2 (* 2 occur-nlines))) | |
295 | (t 1))))) | |
296 | (pos (nth occur-number occur-pos-list))) | |
78bead73 RS |
297 | (if (< line-count 1) |
298 | (error "No occurrence on this line")) | |
43549f18 RS |
299 | (or pos |
300 | (error "No occurrence on this line")) | |
78bead73 RS |
301 | pos)) |
302 | ||
303 | (defun occur-mode-goto-occurrence () | |
304 | "Go to the occurrence the current line describes." | |
305 | (interactive) | |
306 | (let ((pos (occur-mode-find-occurrence))) | |
698e1804 | 307 | (pop-to-buffer occur-buffer) |
121e2227 | 308 | (goto-char (marker-position pos)))) |
4c53bd2b | 309 | \f |
698e1804 | 310 | (defvar list-matching-lines-default-context-lines 0 |
da44e784 | 311 | "*Default number of context lines to include around a `list-matching-lines' |
698e1804 RS |
312 | match. A negative number means to include that many lines before the match. |
313 | A positive number means to include that many lines both before and after.") | |
314 | ||
31e1d920 | 315 | (defalias 'list-matching-lines 'occur) |
698e1804 RS |
316 | |
317 | (defun occur (regexp &optional nlines) | |
99976f85 | 318 | "Show all lines in the current buffer containing a match for REGEXP. |
da44e784 RM |
319 | |
320 | If a match spreads across multiple lines, all those lines are shown. | |
698e1804 | 321 | |
da44e784 RM |
322 | Each line is displayed with NLINES lines before and after, or -NLINES |
323 | before if NLINES is negative. | |
324 | NLINES defaults to `list-matching-lines-default-context-lines'. | |
698e1804 RS |
325 | Interactively it is the prefix arg. |
326 | ||
4c53bd2b | 327 | The lines are shown in a buffer named `*Occur*'. |
698e1804 RS |
328 | It serves as a menu to find any of the occurrences in this buffer. |
329 | \\[describe-mode] in that buffer will explain how." | |
4c53bd2b RS |
330 | (interactive (list (let* ((default (car regexp-history)) |
331 | (input | |
332 | (read-from-minibuffer | |
166aaf6f RS |
333 | (if default |
334 | (format "List lines matching regexp (default `%s'): " default) | |
335 | "List lines matching regexp: ") | |
4c53bd2b RS |
336 | nil nil nil |
337 | 'regexp-history))) | |
338 | (if (> (length input) 0) input | |
339 | (setcar regexp-history default))) | |
f1c9e147 | 340 | current-prefix-arg)) |
698e1804 RS |
341 | (setq nlines (if nlines (prefix-numeric-value nlines) |
342 | list-matching-lines-default-context-lines)) | |
343 | (let ((first t) | |
344 | (buffer (current-buffer)) | |
345 | (linenum 1) | |
79c2e52b JB |
346 | (prevpos (point-min)) |
347 | (final-context-start (make-marker))) | |
99976f85 RS |
348 | ;;; (save-excursion |
349 | ;;; (beginning-of-line) | |
350 | ;;; (setq linenum (1+ (count-lines (point-min) (point)))) | |
351 | ;;; (setq prevpos (point))) | |
698e1804 RS |
352 | (with-output-to-temp-buffer "*Occur*" |
353 | (save-excursion | |
354 | (set-buffer standard-output) | |
355 | (insert "Lines matching ") | |
356 | (prin1 regexp) | |
357 | (insert " in buffer " (buffer-name buffer) ?. ?\n) | |
358 | (occur-mode) | |
359 | (setq occur-buffer buffer) | |
360 | (setq occur-nlines nlines) | |
361 | (setq occur-pos-list ())) | |
362 | (if (eq buffer standard-output) | |
363 | (goto-char (point-max))) | |
364 | (save-excursion | |
99976f85 | 365 | (beginning-of-buffer) |
698e1804 RS |
366 | ;; Find next match, but give up if prev match was at end of buffer. |
367 | (while (and (not (= prevpos (point-max))) | |
368 | (re-search-forward regexp nil t)) | |
da44e784 | 369 | (goto-char (match-beginning 0)) |
698e1804 | 370 | (beginning-of-line) |
4c53bd2b RS |
371 | (save-match-data |
372 | (setq linenum (+ linenum (count-lines prevpos (point))))) | |
698e1804 | 373 | (setq prevpos (point)) |
da44e784 | 374 | (goto-char (match-end 0)) |
698e1804 | 375 | (let* ((start (save-excursion |
da44e784 | 376 | (goto-char (match-beginning 0)) |
698e1804 RS |
377 | (forward-line (if (< nlines 0) nlines (- nlines))) |
378 | (point))) | |
379 | (end (save-excursion | |
da44e784 | 380 | (goto-char (match-end 0)) |
698e1804 RS |
381 | (if (> nlines 0) |
382 | (forward-line (1+ nlines)) | |
383 | (forward-line 1)) | |
384 | (point))) | |
385 | (tag (format "%3d" linenum)) | |
386 | (empty (make-string (length tag) ?\ )) | |
387 | tem) | |
388 | (save-excursion | |
79c2e52b JB |
389 | (setq tem (make-marker)) |
390 | (set-marker tem (point)) | |
698e1804 RS |
391 | (set-buffer standard-output) |
392 | (setq occur-pos-list (cons tem occur-pos-list)) | |
393 | (or first (zerop nlines) | |
394 | (insert "--------\n")) | |
395 | (setq first nil) | |
396 | (insert-buffer-substring buffer start end) | |
397 | (backward-char (- end start)) | |
da44e784 | 398 | (setq tem nlines) |
698e1804 RS |
399 | (while (> tem 0) |
400 | (insert empty ?:) | |
401 | (forward-line 1) | |
402 | (setq tem (1- tem))) | |
79c2e52b | 403 | (let ((this-linenum linenum)) |
da44e784 RM |
404 | (set-marker final-context-start |
405 | (+ (point) (- (match-end 0) (match-beginning 0)))) | |
406 | (while (< (point) final-context-start) | |
407 | (if (null tag) | |
408 | (setq tag (format "%3d" this-linenum))) | |
409 | (insert tag ?:) | |
f81ed6cf RS |
410 | (put-text-property (save-excursion |
411 | (beginning-of-line) | |
412 | (point)) | |
413 | (save-excursion | |
414 | (end-of-line) | |
415 | (point)) | |
416 | 'mouse-face 'highlight) | |
da44e784 RM |
417 | (setq tag nil) |
418 | (forward-line 1) | |
419 | (setq this-linenum (1+ this-linenum)))) | |
698e1804 RS |
420 | (while (< tem nlines) |
421 | (insert empty ?:) | |
422 | (forward-line 1) | |
423 | (setq tem (1+ tem)))) | |
424 | (forward-line 1))) | |
425 | (set-buffer standard-output) | |
426 | ;; Put positions in increasing order to go with buffer. | |
427 | (setq occur-pos-list (nreverse occur-pos-list)) | |
428 | (if (interactive-p) | |
429 | (message "%d matching lines." (length occur-pos-list))))))) | |
430 | \f | |
81bdc14d RS |
431 | ;; It would be nice to use \\[...], but there is no reasonable way |
432 | ;; to make that display both SPC and Y. | |
698e1804 RS |
433 | (defconst query-replace-help |
434 | "Type Space or `y' to replace one match, Delete or `n' to skip to next, | |
be44f62c | 435 | RET or `q' to exit, Period to replace one match and exit, |
698e1804 RS |
436 | Comma to replace but not move point immediately, |
437 | C-r to enter recursive edit (\\[exit-recursive-edit] to get out again), | |
438 | C-w to delete match and recursive edit, | |
439 | C-l to clear the screen, redisplay, and offer same replacement again, | |
440 | ! to replace all remaining matches with no more questions, | |
441 | ^ to move point back to previous match." | |
442 | "Help message while in query-replace") | |
443 | ||
81bdc14d RS |
444 | (defvar query-replace-map (make-sparse-keymap) |
445 | "Keymap that defines the responses to questions in `query-replace'. | |
446 | The \"bindings\" in this map are not commands; they are answers. | |
447 | The valid answers include `act', `skip', `act-and-show', | |
448 | `exit', `act-and-exit', `edit', `delete-and-edit', `recenter', | |
d9121bc0 | 449 | `automatic', `backup', `exit-prefix', and `help'.") |
81bdc14d RS |
450 | |
451 | (define-key query-replace-map " " 'act) | |
452 | (define-key query-replace-map "\d" 'skip) | |
453 | (define-key query-replace-map [delete] 'skip) | |
9275e5e3 | 454 | (define-key query-replace-map [backspace] 'skip) |
81bdc14d RS |
455 | (define-key query-replace-map "y" 'act) |
456 | (define-key query-replace-map "n" 'skip) | |
633a305a RS |
457 | (define-key query-replace-map "Y" 'act) |
458 | (define-key query-replace-map "N" 'skip) | |
81bdc14d | 459 | (define-key query-replace-map "," 'act-and-show) |
81bdc14d | 460 | (define-key query-replace-map "q" 'exit) |
919592c0 | 461 | (define-key query-replace-map "\r" 'exit) |
384c7da4 | 462 | (define-key query-replace-map [return] 'exit) |
81bdc14d RS |
463 | (define-key query-replace-map "." 'act-and-exit) |
464 | (define-key query-replace-map "\C-r" 'edit) | |
465 | (define-key query-replace-map "\C-w" 'delete-and-edit) | |
466 | (define-key query-replace-map "\C-l" 'recenter) | |
467 | (define-key query-replace-map "!" 'automatic) | |
468 | (define-key query-replace-map "^" 'backup) | |
469 | (define-key query-replace-map "\C-h" 'help) | |
470 | (define-key query-replace-map "?" 'help) | |
bc6312e1 RS |
471 | (define-key query-replace-map "\C-g" 'quit) |
472 | (define-key query-replace-map "\C-]" 'quit) | |
d9121bc0 RS |
473 | (define-key query-replace-map "\e" 'exit-prefix) |
474 | (define-key query-replace-map [escape] 'exit-prefix) | |
81bdc14d | 475 | |
698e1804 RS |
476 | (defun perform-replace (from-string replacements |
477 | query-flag regexp-flag delimited-flag | |
81bdc14d | 478 | &optional repeat-count map) |
698e1804 RS |
479 | "Subroutine of `query-replace'. Its complexity handles interactive queries. |
480 | Don't use this in your own program unless you want to query and set the mark | |
481 | just as `query-replace' does. Instead, write a simple loop like this: | |
482 | (while (re-search-forward \"foo[ \t]+bar\" nil t) | |
483 | (replace-match \"foobar\" nil nil)) | |
e782e9f2 | 484 | which will run faster and probably do exactly what you want." |
81bdc14d | 485 | (or map (setq map query-replace-map)) |
698e1804 RS |
486 | (let ((nocasify (not (and case-fold-search case-replace |
487 | (string-equal from-string | |
488 | (downcase from-string))))) | |
489 | (literal (not regexp-flag)) | |
490 | (search-function (if regexp-flag 're-search-forward 'search-forward)) | |
491 | (search-string from-string) | |
e5d77022 | 492 | (real-match-data nil) ; the match data for the current match |
698e1804 RS |
493 | (next-replacement nil) |
494 | (replacement-index 0) | |
495 | (keep-going t) | |
496 | (stack nil) | |
497 | (next-rotate-count 0) | |
498 | (replace-count 0) | |
da44e784 | 499 | (lastrepl nil) ;Position after last match considered. |
02d95a27 RS |
500 | (match-again t) |
501 | (message | |
502 | (if query-flag | |
503 | (substitute-command-keys | |
504 | "Query replacing %s with %s: (\\<query-replace-map>\\[help] for help) ")))) | |
698e1804 RS |
505 | (if (stringp replacements) |
506 | (setq next-replacement replacements) | |
507 | (or repeat-count (setq repeat-count 1))) | |
508 | (if delimited-flag | |
509 | (setq search-function 're-search-forward | |
510 | search-string (concat "\\b" | |
511 | (if regexp-flag from-string | |
512 | (regexp-quote from-string)) | |
513 | "\\b"))) | |
514 | (push-mark) | |
515 | (undo-boundary) | |
e782e9f2 RS |
516 | (unwind-protect |
517 | ;; Loop finding occurrences that perhaps should be replaced. | |
518 | (while (and keep-going | |
519 | (not (eobp)) | |
520 | (funcall search-function search-string nil t) | |
521 | ;; If the search string matches immediately after | |
522 | ;; the previous match, but it did not match there | |
523 | ;; before the replacement was done, ignore the match. | |
524 | (if (or (eq lastrepl (point)) | |
525 | (and regexp-flag | |
526 | (eq lastrepl (match-beginning 0)) | |
527 | (not match-again))) | |
528 | (if (eobp) | |
529 | nil | |
530 | ;; Don't replace the null string | |
531 | ;; right after end of previous replacement. | |
532 | (forward-char 1) | |
533 | (funcall search-function search-string nil t)) | |
534 | t)) | |
535 | ||
536 | ;; Save the data associated with the real match. | |
537 | (setq real-match-data (match-data)) | |
538 | ||
539 | ;; Before we make the replacement, decide whether the search string | |
540 | ;; can match again just after this match. | |
541 | (if regexp-flag | |
542 | (setq match-again (looking-at search-string))) | |
543 | ;; If time for a change, advance to next replacement string. | |
544 | (if (and (listp replacements) | |
545 | (= next-rotate-count replace-count)) | |
546 | (progn | |
547 | (setq next-rotate-count | |
548 | (+ next-rotate-count repeat-count)) | |
549 | (setq next-replacement (nth replacement-index replacements)) | |
550 | (setq replacement-index (% (1+ replacement-index) (length replacements))))) | |
551 | (if (not query-flag) | |
552 | (progn | |
553 | (store-match-data real-match-data) | |
554 | (replace-match next-replacement nocasify literal) | |
555 | (setq replace-count (1+ replace-count))) | |
556 | (undo-boundary) | |
557 | (let (done replaced key def) | |
558 | ;; Loop reading commands until one of them sets done, | |
559 | ;; which means it has finished handling this occurrence. | |
560 | (while (not done) | |
301a9d17 | 561 | (store-match-data real-match-data) |
e782e9f2 | 562 | (replace-highlight (match-beginning 0) (match-end 0)) |
02d95a27 | 563 | (message message from-string next-replacement) |
e782e9f2 RS |
564 | (setq key (read-event)) |
565 | (setq key (vector key)) | |
566 | (setq def (lookup-key map key)) | |
567 | ;; Restore the match data while we process the command. | |
e782e9f2 RS |
568 | (cond ((eq def 'help) |
569 | (with-output-to-temp-buffer "*Help*" | |
570 | (princ | |
571 | (concat "Query replacing " | |
572 | (if regexp-flag "regexp " "") | |
573 | from-string " with " | |
574 | next-replacement ".\n\n" | |
575 | (substitute-command-keys | |
b905ac33 KH |
576 | query-replace-help))) |
577 | (save-excursion | |
578 | (set-buffer standard-output) | |
579 | (help-mode)))) | |
e782e9f2 RS |
580 | ((eq def 'exit) |
581 | (setq keep-going nil) | |
582 | (setq done t)) | |
583 | ((eq def 'backup) | |
237e6ab0 KH |
584 | (if stack |
585 | (let ((elt (car stack))) | |
586 | (goto-char (car elt)) | |
587 | (setq replaced (eq t (cdr elt))) | |
588 | (or replaced | |
589 | (store-match-data (cdr elt))) | |
590 | (setq stack (cdr stack))) | |
591 | (message "No previous match") | |
592 | (ding 'no-terminate) | |
593 | (sit-for 1))) | |
e782e9f2 RS |
594 | ((eq def 'act) |
595 | (or replaced | |
596 | (replace-match next-replacement nocasify literal)) | |
597 | (setq done t replaced t)) | |
598 | ((eq def 'act-and-exit) | |
599 | (or replaced | |
600 | (replace-match next-replacement nocasify literal)) | |
601 | (setq keep-going nil) | |
602 | (setq done t replaced t)) | |
603 | ((eq def 'act-and-show) | |
604 | (if (not replaced) | |
605 | (progn | |
606 | (replace-match next-replacement nocasify literal) | |
607 | (setq replaced t)))) | |
608 | ((eq def 'automatic) | |
609 | (or replaced | |
610 | (replace-match next-replacement nocasify literal)) | |
611 | (setq done t query-flag nil replaced t)) | |
612 | ((eq def 'skip) | |
613 | (setq done t)) | |
614 | ((eq def 'recenter) | |
615 | (recenter nil)) | |
616 | ((eq def 'edit) | |
617 | (store-match-data | |
618 | (prog1 (match-data) | |
619 | (save-excursion (recursive-edit)))) | |
620 | ;; Before we make the replacement, | |
621 | ;; decide whether the search string | |
622 | ;; can match again just after this match. | |
623 | (if regexp-flag | |
624 | (setq match-again (looking-at search-string)))) | |
625 | ((eq def 'delete-and-edit) | |
626 | (delete-region (match-beginning 0) (match-end 0)) | |
627 | (store-match-data | |
628 | (prog1 (match-data) | |
629 | (save-excursion (recursive-edit)))) | |
630 | (setq replaced t)) | |
d9121bc0 RS |
631 | ;; Note: we do not need to treat `exit-prefix' |
632 | ;; specially here, since we reread | |
633 | ;; any unrecognized character. | |
e782e9f2 | 634 | (t |
d9121bc0 | 635 | (setq this-command 'mode-exited) |
e782e9f2 RS |
636 | (setq keep-going nil) |
637 | (setq unread-command-events | |
638 | (append (listify-key-sequence key) | |
639 | unread-command-events)) | |
640 | (setq done t)))) | |
641 | ;; Record previous position for ^ when we move on. | |
642 | ;; Change markers to numbers in the match data | |
643 | ;; since lots of markers slow down editing. | |
644 | (setq stack | |
645 | (cons (cons (point) | |
646 | (or replaced | |
eab69997 RM |
647 | (mapcar (lambda (elt) |
648 | (and elt | |
649 | (prog1 (marker-position elt) | |
650 | (set-marker elt nil)))) | |
e782e9f2 RS |
651 | (match-data)))) |
652 | stack)) | |
653 | (if replaced (setq replace-count (1+ replace-count))))) | |
654 | (setq lastrepl (point))) | |
655 | (replace-dehighlight)) | |
81bdc14d | 656 | (and keep-going stack))) |
698e1804 | 657 | |
e782e9f2 RS |
658 | (defvar query-replace-highlight nil |
659 | "*Non-nil means to highlight words during query replacement.") | |
660 | ||
661 | (defvar replace-overlay nil) | |
662 | ||
663 | (defun replace-dehighlight () | |
664 | (and replace-overlay | |
665 | (progn | |
666 | (delete-overlay replace-overlay) | |
667 | (setq replace-overlay nil)))) | |
668 | ||
669 | (defun replace-highlight (start end) | |
670 | (and query-replace-highlight | |
671 | (progn | |
672 | (or replace-overlay | |
673 | (progn | |
674 | (setq replace-overlay (make-overlay start end)) | |
675 | (overlay-put replace-overlay 'face | |
676 | (if (internal-find-face 'query-replace) | |
677 | 'query-replace 'region)))) | |
678 | (move-overlay replace-overlay start end (current-buffer))))) | |
679 | ||
c88ab9ce | 680 | ;;; replace.el ends here |