Mercurial > emacs
annotate lisp/replace.el @ 2109:73485780a8f9
* simple.el (yank, yank-pop): Always return nil; don't rely on
exchange-point-and-mark to return nil.
* fill.el (justify-current-line): Return nil, to be sure to
conform with documentation.
author | Jim Blandy <jimb@redhat.com> |
---|---|
date | Thu, 11 Mar 1993 07:05:23 +0000 |
parents | ff782069e797 |
children | 706a869e685d |
rev | line source |
---|---|
658
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
324
diff
changeset
|
1 ;;; replace.el --- replace commands for Emacs. |
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
324
diff
changeset
|
2 |
862
46630543d659
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
846
diff
changeset
|
3 ;; Copyright (C) 1985, 1986, 1987, 1992 Free Software Foundation, Inc. |
61 | 4 |
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 | |
732 | 9 ;; the Free Software Foundation; either version 2, or (at your option) |
61 | 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 | |
788
c8d4eb38ebfc
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
732
diff
changeset
|
21 ;;; Code: |
61 | 22 |
268 | 23 (defconst case-replace t "\ |
24 *Non-nil means query-replace should preserve case in replacements.") | |
264 | 25 |
864
fe5f6b7c9727
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
862
diff
changeset
|
26 (defvar query-replace-history nil) |
fe5f6b7c9727
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
862
diff
changeset
|
27 |
fe5f6b7c9727
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
862
diff
changeset
|
28 (defun query-replace-read-args (string) |
fe5f6b7c9727
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
862
diff
changeset
|
29 (let (from to) |
fe5f6b7c9727
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
862
diff
changeset
|
30 (setq from (read-from-minibuffer (format "%s: " string) |
fe5f6b7c9727
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
862
diff
changeset
|
31 nil nil nil |
fe5f6b7c9727
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
862
diff
changeset
|
32 'query-replace-history)) |
fe5f6b7c9727
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
862
diff
changeset
|
33 (setq to (read-from-minibuffer (format "%s %s with: " string from) |
fe5f6b7c9727
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
862
diff
changeset
|
34 nil nil nil |
fe5f6b7c9727
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
862
diff
changeset
|
35 'query-replace-history)) |
fe5f6b7c9727
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
862
diff
changeset
|
36 (list from to current-prefix-arg))) |
fe5f6b7c9727
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
862
diff
changeset
|
37 |
260 | 38 (defun query-replace (from-string to-string &optional arg) |
39 "Replace some occurrences of FROM-STRING with TO-STRING. | |
40 As each match is found, the user must type a character saying | |
41 what to do with it. For directions, type \\[help-command] at that time. | |
42 | |
43 Preserves case in each replacement if case-replace and case-fold-search | |
44 are non-nil and FROM-STRING has no uppercase letters. | |
45 Third arg DELIMITED (prefix arg if interactive) non-nil means replace | |
2080
6ee99287dbc6
(query-replace-map): New keymap.
Richard M. Stallman <rms@gnu.org>
parents:
1820
diff
changeset
|
46 only matches surrounded by word boundaries. |
6ee99287dbc6
(query-replace-map): New keymap.
Richard M. Stallman <rms@gnu.org>
parents:
1820
diff
changeset
|
47 |
6ee99287dbc6
(query-replace-map): New keymap.
Richard M. Stallman <rms@gnu.org>
parents:
1820
diff
changeset
|
48 To customize possible responses, change the \"bindings\" in `query-replace-map'." |
864
fe5f6b7c9727
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
862
diff
changeset
|
49 (interactive (query-replace-read-args "Query replace")) |
260 | 50 (perform-replace from-string to-string t nil arg) |
2080
6ee99287dbc6
(query-replace-map): New keymap.
Richard M. Stallman <rms@gnu.org>
parents:
1820
diff
changeset
|
51 (or unread-command-events (message "Done"))) |
268 | 52 (define-key esc-map "%" 'query-replace) |
260 | 53 |
54 (defun query-replace-regexp (regexp to-string &optional arg) | |
55 "Replace some things after point matching REGEXP with TO-STRING. | |
56 As each match is found, the user must type a character saying | |
57 what to do with it. For directions, type \\[help-command] at that time. | |
58 | |
59 Preserves case in each replacement if case-replace and case-fold-search | |
60 are non-nil and REGEXP has no uppercase letters. | |
61 Third arg DELIMITED (prefix arg if interactive) non-nil means replace | |
62 only matches surrounded by word boundaries. | |
63 In TO-STRING, \\& means insert what matched REGEXP, | |
64 and \\=\\<n> means insert what matched <n>th \\(...\\) in REGEXP." | |
864
fe5f6b7c9727
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
862
diff
changeset
|
65 (interactive (query-replace-read-args "Query replace regexp")) |
260 | 66 (perform-replace regexp to-string t t arg) |
2080
6ee99287dbc6
(query-replace-map): New keymap.
Richard M. Stallman <rms@gnu.org>
parents:
1820
diff
changeset
|
67 (or unread-command-events (message "Done"))) |
260 | 68 |
69 (defun map-query-replace-regexp (regexp to-strings &optional arg) | |
70 "Replace some matches for REGEXP with various strings, in rotation. | |
71 The second argument TO-STRINGS contains the replacement strings, separated | |
72 by spaces. This command works like `query-replace-regexp' except | |
73 that each successive replacement uses the next successive replacement string, | |
74 wrapping around from the last such string to the first. | |
75 | |
76 Non-interactively, TO-STRINGS may be a list of replacement strings. | |
77 | |
78 A prefix argument N says to use each replacement string N times | |
79 before rotating to the next." | |
864
fe5f6b7c9727
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
862
diff
changeset
|
80 (interactive |
fe5f6b7c9727
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
862
diff
changeset
|
81 (let (from to) |
fe5f6b7c9727
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
862
diff
changeset
|
82 (setq from (read-from-minibuffer "Map query replace (regexp): " |
fe5f6b7c9727
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
862
diff
changeset
|
83 nil nil nil |
fe5f6b7c9727
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
862
diff
changeset
|
84 'query-replace-history)) |
fe5f6b7c9727
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
862
diff
changeset
|
85 (setq to (read-from-minibuffer |
fe5f6b7c9727
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
862
diff
changeset
|
86 (format "Query replace %s with (space-separated strings): " |
fe5f6b7c9727
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
862
diff
changeset
|
87 from) |
fe5f6b7c9727
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
862
diff
changeset
|
88 nil nil nil |
fe5f6b7c9727
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
862
diff
changeset
|
89 'query-replace-history)) |
fe5f6b7c9727
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
862
diff
changeset
|
90 (list from to current-prefix-arg))) |
260 | 91 (let (replacements) |
92 (if (listp to-strings) | |
93 (setq replacements to-strings) | |
94 (while (/= (length to-strings) 0) | |
95 (if (string-match " " to-strings) | |
96 (setq replacements | |
97 (append replacements | |
98 (list (substring to-strings 0 | |
99 (string-match " " to-strings)))) | |
100 to-strings (substring to-strings | |
101 (1+ (string-match " " to-strings)))) | |
102 (setq replacements (append replacements (list to-strings)) | |
103 to-strings "")))) | |
104 (perform-replace regexp replacements t t nil arg)) | |
2080
6ee99287dbc6
(query-replace-map): New keymap.
Richard M. Stallman <rms@gnu.org>
parents:
1820
diff
changeset
|
105 (or unread-command-events (message "Done"))) |
260 | 106 |
107 (defun replace-string (from-string to-string &optional delimited) | |
108 "Replace occurrences of FROM-STRING with TO-STRING. | |
109 Preserve case in each match if `case-replace' and `case-fold-search' | |
110 are non-nil and FROM-STRING has no uppercase letters. | |
111 Third arg DELIMITED (prefix arg if interactive) non-nil means replace | |
112 only matches surrounded by word boundaries. | |
113 | |
114 This function is usually the wrong thing to use in a Lisp program. | |
115 What you probably want is a loop like this: | |
116 (while (search-forward OLD-STRING nil t) | |
117 (replace-match REPLACEMENT nil t)) | |
118 which will run faster and will not set the mark or print anything." | |
864
fe5f6b7c9727
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
862
diff
changeset
|
119 (interactive (query-replace-read-args "Replace string")) |
260 | 120 (perform-replace from-string to-string nil nil delimited) |
2080
6ee99287dbc6
(query-replace-map): New keymap.
Richard M. Stallman <rms@gnu.org>
parents:
1820
diff
changeset
|
121 (or unread-command-events (message "Done"))) |
260 | 122 |
123 (defun replace-regexp (regexp to-string &optional delimited) | |
124 "Replace things after point matching REGEXP with TO-STRING. | |
125 Preserve case in each match if case-replace and case-fold-search | |
126 are non-nil and REGEXP has no uppercase letters. | |
127 Third arg DELIMITED (prefix arg if interactive) non-nil means replace | |
128 only matches surrounded by word boundaries. | |
129 In TO-STRING, \\& means insert what matched REGEXP, | |
130 and \\=\\<n> means insert what matched <n>th \\(...\\) in REGEXP. | |
131 | |
132 This function is usually the wrong thing to use in a Lisp program. | |
133 What you probably want is a loop like this: | |
134 (while (re-search-forward REGEXP nil t) | |
135 (replace-match REPLACEMENT nil nil)) | |
136 which will run faster and will not set the mark or print anything." | |
864
fe5f6b7c9727
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
862
diff
changeset
|
137 (interactive (query-replace-read-args "Replace regexp")) |
260 | 138 (perform-replace regexp to-string nil t delimited) |
2080
6ee99287dbc6
(query-replace-map): New keymap.
Richard M. Stallman <rms@gnu.org>
parents:
1820
diff
changeset
|
139 (or unread-command-events (message "Done"))) |
260 | 140 |
61 | 141 (fset 'delete-non-matching-lines 'keep-lines) |
142 (defun keep-lines (regexp) | |
143 "Delete all lines except those containing matches for REGEXP. | |
144 A match split across lines preserves all the lines it lies in. | |
145 Applies to all lines after point." | |
146 (interactive "sKeep lines (containing match for regexp): ") | |
147 (save-excursion | |
148 (or (bolp) (forward-line 1)) | |
149 (let ((start (point))) | |
150 (while (not (eobp)) | |
151 ;; Start is first char not preserved by previous match. | |
152 (if (not (re-search-forward regexp nil 'move)) | |
153 (delete-region start (point-max)) | |
154 (let ((end (save-excursion (goto-char (match-beginning 0)) | |
155 (beginning-of-line) | |
156 (point)))) | |
157 ;; Now end is first char preserved by the new match. | |
158 (if (< start end) | |
159 (delete-region start end)))) | |
160 (setq start (save-excursion (forward-line 1) | |
161 (point))) | |
162 ;; If the match was empty, avoid matching again at same place. | |
163 (and (not (eobp)) (= (match-beginning 0) (match-end 0)) | |
164 (forward-char 1)))))) | |
165 | |
166 (fset 'delete-matching-lines 'flush-lines) | |
167 (defun flush-lines (regexp) | |
168 "Delete lines containing matches for REGEXP. | |
169 If a match is split across lines, all the lines it lies in are deleted. | |
170 Applies to lines after point." | |
171 (interactive "sFlush lines (containing match for regexp): ") | |
172 (save-excursion | |
173 (while (and (not (eobp)) | |
174 (re-search-forward regexp nil t)) | |
175 (delete-region (save-excursion (goto-char (match-beginning 0)) | |
176 (beginning-of-line) | |
177 (point)) | |
178 (progn (forward-line 1) (point)))))) | |
179 | |
180 (fset 'count-matches 'how-many) | |
181 (defun how-many (regexp) | |
182 "Print number of matches for REGEXP following point." | |
183 (interactive "sHow many matches for (regexp): ") | |
184 (let ((count 0) opoint) | |
185 (save-excursion | |
186 (while (and (not (eobp)) | |
187 (progn (setq opoint (point)) | |
188 (re-search-forward regexp nil t))) | |
189 (if (= opoint (point)) | |
190 (forward-char 1) | |
191 (setq count (1+ count)))) | |
192 (message "%d occurrences" count)))) | |
193 | |
194 (defvar occur-mode-map ()) | |
195 (if occur-mode-map | |
196 () | |
197 (setq occur-mode-map (make-sparse-keymap)) | |
198 (define-key occur-mode-map "\C-c\C-c" 'occur-mode-goto-occurrence)) | |
199 | |
200 (defvar occur-buffer nil) | |
201 (defvar occur-nlines nil) | |
202 (defvar occur-pos-list nil) | |
73 | 203 (defvar occur-last-string "") |
61 | 204 |
205 (defun occur-mode () | |
206 "Major mode for output from \\[occur]. | |
207 Move point to one of the occurrences in this buffer, | |
208 then use \\[occur-mode-goto-occurrence] to go to the same occurrence | |
209 in the buffer that the occurrences were found in. | |
210 \\{occur-mode-map}" | |
211 (kill-all-local-variables) | |
212 (use-local-map occur-mode-map) | |
213 (setq major-mode 'occur-mode) | |
214 (setq mode-name "Occur") | |
215 (make-local-variable 'occur-buffer) | |
216 (make-local-variable 'occur-nlines) | |
217 (make-local-variable 'occur-pos-list)) | |
218 | |
219 (defun occur-mode-goto-occurrence () | |
220 "Go to the line this occurrence was found in, in the buffer it was found in." | |
221 (interactive) | |
222 (if (or (null occur-buffer) | |
223 (null (buffer-name occur-buffer))) | |
224 (progn | |
225 (setq occur-buffer nil | |
226 occur-pos-list nil) | |
227 (error "Buffer in which occurrences were found is deleted"))) | |
228 (let* ((occur-number (save-excursion | |
229 (beginning-of-line) | |
230 (/ (1- (count-lines (point-min) | |
231 (save-excursion | |
232 (beginning-of-line) | |
233 (point)))) | |
234 (cond ((< occur-nlines 0) | |
235 (- 2 occur-nlines)) | |
236 ((> occur-nlines 0) | |
237 (+ 2 (* 2 occur-nlines))) | |
238 (t 1))))) | |
239 (pos (nth occur-number occur-pos-list))) | |
240 (pop-to-buffer occur-buffer) | |
241 (goto-char (marker-position pos)))) | |
242 | |
243 (defvar list-matching-lines-default-context-lines 0 | |
260 | 244 "*Default number of context lines to include around a `list-matching-lines' |
61 | 245 match. A negative number means to include that many lines before the match. |
246 A positive number means to include that many lines both before and after.") | |
247 | |
248 (fset 'list-matching-lines 'occur) | |
249 | |
250 (defun occur (regexp &optional nlines) | |
1427
c49077849583
(occur): Always search entire buffer.
Richard M. Stallman <rms@gnu.org>
parents:
866
diff
changeset
|
251 "Show all lines in the current buffer containing a match for REGEXP. |
c49077849583
(occur): Always search entire buffer.
Richard M. Stallman <rms@gnu.org>
parents:
866
diff
changeset
|
252 Interactively, REGEXP defaults to the last REGEXP |
260 | 253 used interactively with \\[occur]. |
61 | 254 |
260 | 255 If a match spreads across multiple lines, all those lines are shown. |
256 | |
257 Each line is displayed with NLINES lines before and after, or -NLINES | |
258 before if NLINES is negative. | |
259 NLINES defaults to `list-matching-lines-default-context-lines'. | |
61 | 260 Interactively it is the prefix arg. |
261 | |
262 The lines are shown in a buffer named *Occur*. | |
263 It serves as a menu to find any of the occurrences in this buffer. | |
264 \\[describe-mode] in that buffer will explain how." | |
73 | 265 (interactive (list (setq occur-last-string |
266 (read-string "List lines matching regexp: " | |
267 occur-last-string)) | |
268 current-prefix-arg)) | |
61 | 269 (setq nlines (if nlines (prefix-numeric-value nlines) |
270 list-matching-lines-default-context-lines)) | |
271 (let ((first t) | |
272 (buffer (current-buffer)) | |
273 (linenum 1) | |
324 | 274 (prevpos (point-min)) |
275 (final-context-start (make-marker))) | |
1427
c49077849583
(occur): Always search entire buffer.
Richard M. Stallman <rms@gnu.org>
parents:
866
diff
changeset
|
276 ;;; (save-excursion |
c49077849583
(occur): Always search entire buffer.
Richard M. Stallman <rms@gnu.org>
parents:
866
diff
changeset
|
277 ;;; (beginning-of-line) |
c49077849583
(occur): Always search entire buffer.
Richard M. Stallman <rms@gnu.org>
parents:
866
diff
changeset
|
278 ;;; (setq linenum (1+ (count-lines (point-min) (point)))) |
c49077849583
(occur): Always search entire buffer.
Richard M. Stallman <rms@gnu.org>
parents:
866
diff
changeset
|
279 ;;; (setq prevpos (point))) |
61 | 280 (with-output-to-temp-buffer "*Occur*" |
281 (save-excursion | |
282 (set-buffer standard-output) | |
283 (insert "Lines matching ") | |
284 (prin1 regexp) | |
285 (insert " in buffer " (buffer-name buffer) ?. ?\n) | |
286 (occur-mode) | |
287 (setq occur-buffer buffer) | |
288 (setq occur-nlines nlines) | |
289 (setq occur-pos-list ())) | |
290 (if (eq buffer standard-output) | |
291 (goto-char (point-max))) | |
292 (save-excursion | |
1427
c49077849583
(occur): Always search entire buffer.
Richard M. Stallman <rms@gnu.org>
parents:
866
diff
changeset
|
293 (beginning-of-buffer) |
61 | 294 ;; Find next match, but give up if prev match was at end of buffer. |
295 (while (and (not (= prevpos (point-max))) | |
296 (re-search-forward regexp nil t)) | |
260 | 297 (goto-char (match-beginning 0)) |
61 | 298 (beginning-of-line) |
299 (setq linenum (+ linenum (count-lines prevpos (point)))) | |
300 (setq prevpos (point)) | |
260 | 301 (goto-char (match-end 0)) |
61 | 302 (let* ((start (save-excursion |
260 | 303 (goto-char (match-beginning 0)) |
61 | 304 (forward-line (if (< nlines 0) nlines (- nlines))) |
305 (point))) | |
306 (end (save-excursion | |
260 | 307 (goto-char (match-end 0)) |
61 | 308 (if (> nlines 0) |
309 (forward-line (1+ nlines)) | |
310 (forward-line 1)) | |
311 (point))) | |
312 (tag (format "%3d" linenum)) | |
313 (empty (make-string (length tag) ?\ )) | |
314 tem) | |
315 (save-excursion | |
324 | 316 (setq tem (make-marker)) |
317 (set-marker tem (point)) | |
61 | 318 (set-buffer standard-output) |
319 (setq occur-pos-list (cons tem occur-pos-list)) | |
320 (or first (zerop nlines) | |
321 (insert "--------\n")) | |
322 (setq first nil) | |
323 (insert-buffer-substring buffer start end) | |
324 (backward-char (- end start)) | |
260 | 325 (setq tem nlines) |
61 | 326 (while (> tem 0) |
327 (insert empty ?:) | |
328 (forward-line 1) | |
329 (setq tem (1- tem))) | |
324 | 330 (let ((this-linenum linenum)) |
260 | 331 (set-marker final-context-start |
332 (+ (point) (- (match-end 0) (match-beginning 0)))) | |
333 (while (< (point) final-context-start) | |
334 (if (null tag) | |
335 (setq tag (format "%3d" this-linenum))) | |
336 (insert tag ?:) | |
337 (setq tag nil) | |
338 (forward-line 1) | |
339 (setq this-linenum (1+ this-linenum)))) | |
61 | 340 (while (< tem nlines) |
341 (insert empty ?:) | |
342 (forward-line 1) | |
343 (setq tem (1+ tem)))) | |
344 (forward-line 1))) | |
345 (set-buffer standard-output) | |
346 ;; Put positions in increasing order to go with buffer. | |
347 (setq occur-pos-list (nreverse occur-pos-list)) | |
348 (if (interactive-p) | |
349 (message "%d matching lines." (length occur-pos-list))))))) | |
350 | |
2080
6ee99287dbc6
(query-replace-map): New keymap.
Richard M. Stallman <rms@gnu.org>
parents:
1820
diff
changeset
|
351 ;; It would be nice to use \\[...], but there is no reasonable way |
6ee99287dbc6
(query-replace-map): New keymap.
Richard M. Stallman <rms@gnu.org>
parents:
1820
diff
changeset
|
352 ;; to make that display both SPC and Y. |
61 | 353 (defconst query-replace-help |
354 "Type Space or `y' to replace one match, Delete or `n' to skip to next, | |
355 ESC or `q' to exit, Period to replace one match and exit, | |
356 Comma to replace but not move point immediately, | |
357 C-r to enter recursive edit (\\[exit-recursive-edit] to get out again), | |
358 C-w to delete match and recursive edit, | |
359 C-l to clear the screen, redisplay, and offer same replacement again, | |
360 ! to replace all remaining matches with no more questions, | |
361 ^ to move point back to previous match." | |
362 "Help message while in query-replace") | |
363 | |
2080
6ee99287dbc6
(query-replace-map): New keymap.
Richard M. Stallman <rms@gnu.org>
parents:
1820
diff
changeset
|
364 (defvar query-replace-map (make-sparse-keymap) |
6ee99287dbc6
(query-replace-map): New keymap.
Richard M. Stallman <rms@gnu.org>
parents:
1820
diff
changeset
|
365 "Keymap that defines the responses to questions in `query-replace'. |
6ee99287dbc6
(query-replace-map): New keymap.
Richard M. Stallman <rms@gnu.org>
parents:
1820
diff
changeset
|
366 The \"bindings\" in this map are not commands; they are answers. |
6ee99287dbc6
(query-replace-map): New keymap.
Richard M. Stallman <rms@gnu.org>
parents:
1820
diff
changeset
|
367 The valid answers include `act', `skip', `act-and-show', |
6ee99287dbc6
(query-replace-map): New keymap.
Richard M. Stallman <rms@gnu.org>
parents:
1820
diff
changeset
|
368 `exit', `act-and-exit', `edit', `delete-and-edit', `recenter', |
6ee99287dbc6
(query-replace-map): New keymap.
Richard M. Stallman <rms@gnu.org>
parents:
1820
diff
changeset
|
369 `automatic', `backup', and `help'.") |
6ee99287dbc6
(query-replace-map): New keymap.
Richard M. Stallman <rms@gnu.org>
parents:
1820
diff
changeset
|
370 |
6ee99287dbc6
(query-replace-map): New keymap.
Richard M. Stallman <rms@gnu.org>
parents:
1820
diff
changeset
|
371 (define-key query-replace-map " " 'act) |
6ee99287dbc6
(query-replace-map): New keymap.
Richard M. Stallman <rms@gnu.org>
parents:
1820
diff
changeset
|
372 (define-key query-replace-map "\d" 'skip) |
6ee99287dbc6
(query-replace-map): New keymap.
Richard M. Stallman <rms@gnu.org>
parents:
1820
diff
changeset
|
373 (define-key query-replace-map [delete] 'skip) |
6ee99287dbc6
(query-replace-map): New keymap.
Richard M. Stallman <rms@gnu.org>
parents:
1820
diff
changeset
|
374 (define-key query-replace-map "y" 'act) |
6ee99287dbc6
(query-replace-map): New keymap.
Richard M. Stallman <rms@gnu.org>
parents:
1820
diff
changeset
|
375 (define-key query-replace-map "n" 'skip) |
6ee99287dbc6
(query-replace-map): New keymap.
Richard M. Stallman <rms@gnu.org>
parents:
1820
diff
changeset
|
376 (define-key query-replace-map "," 'act-and-show) |
6ee99287dbc6
(query-replace-map): New keymap.
Richard M. Stallman <rms@gnu.org>
parents:
1820
diff
changeset
|
377 (define-key query-replace-map "\e" 'exit) |
6ee99287dbc6
(query-replace-map): New keymap.
Richard M. Stallman <rms@gnu.org>
parents:
1820
diff
changeset
|
378 (define-key query-replace-map "q" 'exit) |
6ee99287dbc6
(query-replace-map): New keymap.
Richard M. Stallman <rms@gnu.org>
parents:
1820
diff
changeset
|
379 (define-key query-replace-map "." 'act-and-exit) |
6ee99287dbc6
(query-replace-map): New keymap.
Richard M. Stallman <rms@gnu.org>
parents:
1820
diff
changeset
|
380 (define-key query-replace-map "\C-r" 'edit) |
6ee99287dbc6
(query-replace-map): New keymap.
Richard M. Stallman <rms@gnu.org>
parents:
1820
diff
changeset
|
381 (define-key query-replace-map "\C-w" 'delete-and-edit) |
6ee99287dbc6
(query-replace-map): New keymap.
Richard M. Stallman <rms@gnu.org>
parents:
1820
diff
changeset
|
382 (define-key query-replace-map "\C-l" 'recenter) |
6ee99287dbc6
(query-replace-map): New keymap.
Richard M. Stallman <rms@gnu.org>
parents:
1820
diff
changeset
|
383 (define-key query-replace-map "!" 'automatic) |
6ee99287dbc6
(query-replace-map): New keymap.
Richard M. Stallman <rms@gnu.org>
parents:
1820
diff
changeset
|
384 (define-key query-replace-map "^" 'backup) |
6ee99287dbc6
(query-replace-map): New keymap.
Richard M. Stallman <rms@gnu.org>
parents:
1820
diff
changeset
|
385 (define-key query-replace-map "\C-h" 'help) |
6ee99287dbc6
(query-replace-map): New keymap.
Richard M. Stallman <rms@gnu.org>
parents:
1820
diff
changeset
|
386 (define-key query-replace-map "?" 'help) |
2083
ff782069e797
(query-replace-map): Add `quit' bindings. Delete default binding.
Richard M. Stallman <rms@gnu.org>
parents:
2080
diff
changeset
|
387 (define-key query-replace-map "\C-g" 'quit) |
ff782069e797
(query-replace-map): Add `quit' bindings. Delete default binding.
Richard M. Stallman <rms@gnu.org>
parents:
2080
diff
changeset
|
388 (define-key query-replace-map "\C-]" 'quit) |
2080
6ee99287dbc6
(query-replace-map): New keymap.
Richard M. Stallman <rms@gnu.org>
parents:
1820
diff
changeset
|
389 |
61 | 390 (defun perform-replace (from-string replacements |
391 query-flag regexp-flag delimited-flag | |
2080
6ee99287dbc6
(query-replace-map): New keymap.
Richard M. Stallman <rms@gnu.org>
parents:
1820
diff
changeset
|
392 &optional repeat-count map) |
61 | 393 "Subroutine of `query-replace'. Its complexity handles interactive queries. |
394 Don't use this in your own program unless you want to query and set the mark | |
395 just as `query-replace' does. Instead, write a simple loop like this: | |
396 (while (re-search-forward \"foo[ \t]+bar\" nil t) | |
397 (replace-match \"foobar\" nil nil)) | |
398 which will run faster and do exactly what you probably want." | |
2080
6ee99287dbc6
(query-replace-map): New keymap.
Richard M. Stallman <rms@gnu.org>
parents:
1820
diff
changeset
|
399 (or map (setq map query-replace-map)) |
61 | 400 (let ((nocasify (not (and case-fold-search case-replace |
401 (string-equal from-string | |
402 (downcase from-string))))) | |
403 (literal (not regexp-flag)) | |
404 (search-function (if regexp-flag 're-search-forward 'search-forward)) | |
405 (search-string from-string) | |
732 | 406 (real-match-data nil) ; the match data for the current match |
61 | 407 (next-replacement nil) |
408 (replacement-index 0) | |
409 (keep-going t) | |
410 (stack nil) | |
411 (next-rotate-count 0) | |
412 (replace-count 0) | |
260 | 413 (lastrepl nil) ;Position after last match considered. |
862
46630543d659
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
846
diff
changeset
|
414 (match-again t)) |
61 | 415 (if (stringp replacements) |
416 (setq next-replacement replacements) | |
417 (or repeat-count (setq repeat-count 1))) | |
418 (if delimited-flag | |
419 (setq search-function 're-search-forward | |
420 search-string (concat "\\b" | |
421 (if regexp-flag from-string | |
422 (regexp-quote from-string)) | |
423 "\\b"))) | |
424 (push-mark) | |
425 (undo-boundary) | |
2080
6ee99287dbc6
(query-replace-map): New keymap.
Richard M. Stallman <rms@gnu.org>
parents:
1820
diff
changeset
|
426 ;; Loop finding occurrences that perhaps should be replaced. |
61 | 427 (while (and keep-going |
428 (not (eobp)) | |
429 (funcall search-function search-string nil t) | |
260 | 430 ;; If the search string matches immediately after |
431 ;; the previous match, but it did not match there | |
432 ;; before the replacement was done, ignore the match. | |
433 (if (or (eq lastrepl (point)) | |
434 (and regexp-flag | |
435 (eq lastrepl (match-beginning 0)) | |
436 (not match-again))) | |
437 (if (eobp) | |
438 nil | |
61 | 439 ;; Don't replace the null string |
440 ;; right after end of previous replacement. | |
441 (forward-char 1) | |
442 (funcall search-function search-string nil t)) | |
443 t)) | |
732 | 444 |
445 ;; Save the data associated with the real match. | |
446 (setq real-match-data (match-data)) | |
447 | |
260 | 448 ;; Before we make the replacement, decide whether the search string |
449 ;; can match again just after this match. | |
450 (if regexp-flag | |
451 (setq match-again (looking-at search-string))) | |
61 | 452 ;; If time for a change, advance to next replacement string. |
453 (if (and (listp replacements) | |
454 (= next-rotate-count replace-count)) | |
455 (progn | |
456 (setq next-rotate-count | |
457 (+ next-rotate-count repeat-count)) | |
458 (setq next-replacement (nth replacement-index replacements)) | |
459 (setq replacement-index (% (1+ replacement-index) (length replacements))))) | |
460 (if (not query-flag) | |
461 (progn | |
732 | 462 (store-match-data real-match-data) |
61 | 463 (replace-match next-replacement nocasify literal) |
464 (setq replace-count (1+ replace-count))) | |
465 (undo-boundary) | |
2080
6ee99287dbc6
(query-replace-map): New keymap.
Richard M. Stallman <rms@gnu.org>
parents:
1820
diff
changeset
|
466 (let (done replaced key) |
6ee99287dbc6
(query-replace-map): New keymap.
Richard M. Stallman <rms@gnu.org>
parents:
1820
diff
changeset
|
467 ;; Loop reading commands until one of them sets done, |
6ee99287dbc6
(query-replace-map): New keymap.
Richard M. Stallman <rms@gnu.org>
parents:
1820
diff
changeset
|
468 ;; which means it has finished handling this occurrence. |
61 | 469 (while (not done) |
2080
6ee99287dbc6
(query-replace-map): New keymap.
Richard M. Stallman <rms@gnu.org>
parents:
1820
diff
changeset
|
470 (message "Query replacing %s with %s: " |
6ee99287dbc6
(query-replace-map): New keymap.
Richard M. Stallman <rms@gnu.org>
parents:
1820
diff
changeset
|
471 from-string next-replacement) |
6ee99287dbc6
(query-replace-map): New keymap.
Richard M. Stallman <rms@gnu.org>
parents:
1820
diff
changeset
|
472 (setq key (vector (read-event))) |
6ee99287dbc6
(query-replace-map): New keymap.
Richard M. Stallman <rms@gnu.org>
parents:
1820
diff
changeset
|
473 (setq def (lookup-key map key)) |
732 | 474 ;; Restore the match data while we process the command. |
475 (store-match-data real-match-data) | |
2080
6ee99287dbc6
(query-replace-map): New keymap.
Richard M. Stallman <rms@gnu.org>
parents:
1820
diff
changeset
|
476 (cond ((eq def 'help) |
6ee99287dbc6
(query-replace-map): New keymap.
Richard M. Stallman <rms@gnu.org>
parents:
1820
diff
changeset
|
477 (with-output-to-temp-buffer "*Help*" |
6ee99287dbc6
(query-replace-map): New keymap.
Richard M. Stallman <rms@gnu.org>
parents:
1820
diff
changeset
|
478 (princ |
6ee99287dbc6
(query-replace-map): New keymap.
Richard M. Stallman <rms@gnu.org>
parents:
1820
diff
changeset
|
479 (concat "Query replacing " |
6ee99287dbc6
(query-replace-map): New keymap.
Richard M. Stallman <rms@gnu.org>
parents:
1820
diff
changeset
|
480 (if regexp-flag "regexp " "") |
6ee99287dbc6
(query-replace-map): New keymap.
Richard M. Stallman <rms@gnu.org>
parents:
1820
diff
changeset
|
481 from-string " with " |
6ee99287dbc6
(query-replace-map): New keymap.
Richard M. Stallman <rms@gnu.org>
parents:
1820
diff
changeset
|
482 next-replacement ".\n\n" |
6ee99287dbc6
(query-replace-map): New keymap.
Richard M. Stallman <rms@gnu.org>
parents:
1820
diff
changeset
|
483 (substitute-command-keys |
6ee99287dbc6
(query-replace-map): New keymap.
Richard M. Stallman <rms@gnu.org>
parents:
1820
diff
changeset
|
484 query-replace-help))))) |
6ee99287dbc6
(query-replace-map): New keymap.
Richard M. Stallman <rms@gnu.org>
parents:
1820
diff
changeset
|
485 ((eq def 'exit) |
61 | 486 (setq keep-going nil) |
487 (setq done t)) | |
2080
6ee99287dbc6
(query-replace-map): New keymap.
Richard M. Stallman <rms@gnu.org>
parents:
1820
diff
changeset
|
488 ((eq def 'backup) |
61 | 489 (let ((elt (car stack))) |
490 (goto-char (car elt)) | |
491 (setq replaced (eq t (cdr elt))) | |
492 (or replaced | |
493 (store-match-data (cdr elt))) | |
494 (setq stack (cdr stack)))) | |
2080
6ee99287dbc6
(query-replace-map): New keymap.
Richard M. Stallman <rms@gnu.org>
parents:
1820
diff
changeset
|
495 ((eq def 'act) |
61 | 496 (or replaced |
497 (replace-match next-replacement nocasify literal)) | |
498 (setq done t replaced t)) | |
2080
6ee99287dbc6
(query-replace-map): New keymap.
Richard M. Stallman <rms@gnu.org>
parents:
1820
diff
changeset
|
499 ((eq def 'act-and-exit) |
61 | 500 (or replaced |
501 (replace-match next-replacement nocasify literal)) | |
502 (setq keep-going nil) | |
503 (setq done t replaced t)) | |
2080
6ee99287dbc6
(query-replace-map): New keymap.
Richard M. Stallman <rms@gnu.org>
parents:
1820
diff
changeset
|
504 ((eq def 'act-and-show) |
61 | 505 (if (not replaced) |
506 (progn | |
507 (replace-match next-replacement nocasify literal) | |
508 (setq replaced t)))) | |
2080
6ee99287dbc6
(query-replace-map): New keymap.
Richard M. Stallman <rms@gnu.org>
parents:
1820
diff
changeset
|
509 ((eq def 'automatic) |
61 | 510 (or replaced |
511 (replace-match next-replacement nocasify literal)) | |
512 (setq done t query-flag nil replaced t)) | |
2080
6ee99287dbc6
(query-replace-map): New keymap.
Richard M. Stallman <rms@gnu.org>
parents:
1820
diff
changeset
|
513 ((eq def 'skip) |
61 | 514 (setq done t)) |
2080
6ee99287dbc6
(query-replace-map): New keymap.
Richard M. Stallman <rms@gnu.org>
parents:
1820
diff
changeset
|
515 ((eq def 'recenter) |
61 | 516 (recenter nil)) |
2080
6ee99287dbc6
(query-replace-map): New keymap.
Richard M. Stallman <rms@gnu.org>
parents:
1820
diff
changeset
|
517 ((eq def 'edit) |
61 | 518 (store-match-data |
519 (prog1 (match-data) | |
260 | 520 (save-excursion (recursive-edit)))) |
521 ;; Before we make the replacement, | |
522 ;; decide whether the search string | |
523 ;; can match again just after this match. | |
524 (if regexp-flag | |
525 (setq match-again (looking-at search-string)))) | |
2080
6ee99287dbc6
(query-replace-map): New keymap.
Richard M. Stallman <rms@gnu.org>
parents:
1820
diff
changeset
|
526 ((eq def 'delete-and-edit) |
61 | 527 (delete-region (match-beginning 0) (match-end 0)) |
528 (store-match-data | |
529 (prog1 (match-data) | |
530 (save-excursion (recursive-edit)))) | |
531 (setq replaced t)) | |
532 (t | |
533 (setq keep-going nil) | |
2080
6ee99287dbc6
(query-replace-map): New keymap.
Richard M. Stallman <rms@gnu.org>
parents:
1820
diff
changeset
|
534 (setq unread-command-events |
6ee99287dbc6
(query-replace-map): New keymap.
Richard M. Stallman <rms@gnu.org>
parents:
1820
diff
changeset
|
535 (append (listify-key-sequence key) |
6ee99287dbc6
(query-replace-map): New keymap.
Richard M. Stallman <rms@gnu.org>
parents:
1820
diff
changeset
|
536 unread-command-events)) |
61 | 537 (setq done t)))) |
538 ;; Record previous position for ^ when we move on. | |
539 ;; Change markers to numbers in the match data | |
540 ;; since lots of markers slow down editing. | |
541 (setq stack | |
542 (cons (cons (point) | |
543 (or replaced | |
544 (mapcar | |
545 (function (lambda (elt) | |
546 (and elt | |
547 (marker-position elt)))) | |
548 (match-data)))) | |
549 stack)) | |
550 (if replaced (setq replace-count (1+ replace-count))))) | |
551 (setq lastrepl (point))) | |
2080
6ee99287dbc6
(query-replace-map): New keymap.
Richard M. Stallman <rms@gnu.org>
parents:
1820
diff
changeset
|
552 (and keep-going stack))) |
61 | 553 |
658
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
324
diff
changeset
|
554 ;;; replace.el ends here |