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