0
|
1 ;;; replace.el --- search and replace commands for XEmacs.
|
|
2
|
|
3 ;; Copyright (C) 1985, 1986, 1987, 1992, 1994 Free Software Foundation, Inc.
|
|
4
|
|
5 ;; This file is part of XEmacs.
|
|
6
|
|
7 ;; XEmacs is free software; you can redistribute it and/or modify it
|
|
8 ;; under the terms of the GNU General Public License as published by
|
|
9 ;; the Free Software Foundation; either version 2, or (at your option)
|
|
10 ;; any later version.
|
|
11
|
|
12 ;; XEmacs is distributed in the hope that it will be useful, but
|
|
13 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
15 ;; General Public License for more details.
|
|
16
|
|
17 ;; You should have received a copy of the GNU General Public License
|
70
|
18 ;; along with XEmacs; see the file COPYING. If not, write to the
|
|
19 ;; Free Software Foundation, 59 Temple Place - Suite 330,
|
|
20 ;; Boston, MA 02111-1307, USA.
|
0
|
21
|
70
|
22 ;;; Synched up with: FSF 19.30.
|
0
|
23
|
|
24 ;;; Commentary:
|
|
25
|
|
26 ;; This package supplies the string and regular-expression replace functions
|
|
27 ;; documented in the XEmacs Reference Manual.
|
|
28
|
|
29 ;;; Code:
|
|
30
|
70
|
31 (defvar case-replace t "\
|
0
|
32 *Non-nil means `query-replace' should preserve case in replacements.
|
|
33 What this means is that `query-replace' will change the case of the
|
|
34 replacement text so that it matches the text that was replaced.
|
|
35 If this variable is nil, the replacement text will be inserted
|
|
36 exactly as it was specified by the user, irrespective of the case
|
|
37 of the text that was replaced.
|
|
38
|
|
39 Note that this flag has no effect if `case-fold-search' is nil,
|
|
40 or if the replacement text has any uppercase letters in it.")
|
|
41
|
|
42 (defvar query-replace-history nil)
|
|
43
|
|
44 (defvar query-replace-interactive nil
|
|
45 "Non-nil means `query-replace' uses the last search string.
|
|
46 That becomes the \"string to replace\".")
|
|
47
|
|
48 (defun query-replace-read-args (string regexp-flag)
|
|
49 (let (from to)
|
|
50 (if query-replace-interactive
|
|
51 (setq from (car (if regexp-flag regexp-search-ring search-ring)))
|
|
52 (setq from (read-from-minibuffer (format "%s: " (gettext string))
|
|
53 nil nil nil
|
|
54 'query-replace-history)))
|
|
55 (setq to (read-from-minibuffer (format "%s %s with: " (gettext string)
|
|
56 from)
|
|
57 nil nil nil
|
|
58 'query-replace-history))
|
|
59 (list from to current-prefix-arg)))
|
|
60
|
|
61 (defun query-replace (from-string to-string &optional arg)
|
|
62 "Replace some occurrences of FROM-STRING with TO-STRING.
|
|
63 As each match is found, the user must type a character saying
|
|
64 what to do with it. For directions, type \\[help-command] at that time.
|
|
65
|
|
66 If `query-replace-interactive' is non-nil, the last incremental search
|
|
67 string is used as FROM-STRING--you don't have to specify it with the
|
|
68 minibuffer.
|
|
69
|
|
70 Preserves case in each replacement if `case-replace' and `case-fold-search'
|
|
71 are non-nil and FROM-STRING has no uppercase letters.
|
|
72 \(Preserving case means that if the string matched is all caps, or capitalized,
|
|
73 then its replacement is upcased or capitalized.)
|
|
74
|
|
75 Third arg DELIMITED (prefix arg if interactive), if non-nil, means replace
|
|
76 only matches surrounded by word boundaries.
|
|
77
|
|
78 To customize possible responses, change the \"bindings\" in `query-replace-map'."
|
|
79 (interactive (query-replace-read-args "Query replace" nil))
|
|
80 (perform-replace from-string to-string t nil arg))
|
|
81
|
|
82 (defun query-replace-regexp (regexp to-string &optional arg)
|
|
83 "Replace some things after point matching REGEXP with TO-STRING.
|
|
84 As each match is found, the user must type a character saying
|
|
85 what to do with it. For directions, type \\[help-command] at that time.
|
|
86
|
|
87 If `query-replace-interactive' is non-nil, the last incremental search
|
|
88 regexp is used as REGEXP--you don't have to specify it with the
|
|
89 minibuffer.
|
|
90
|
|
91 Preserves case in each replacement if `case-replace' and `case-fold-search'
|
|
92 are non-nil and REGEXP has no uppercase letters.
|
|
93 Third arg DELIMITED (prefix arg if interactive), if non-nil, means replace
|
|
94 only matches surrounded by word boundaries.
|
|
95 In TO-STRING, `\\&' stands for whatever matched the whole of REGEXP,
|
|
96 and `\\=\\N' (where N is a digit) stands for
|
|
97 whatever what matched the Nth `\\(...\\)' in REGEXP."
|
|
98 (interactive (query-replace-read-args "Query replace regexp" t))
|
|
99 (perform-replace regexp to-string t t arg))
|
|
100
|
|
101 ;;#### Not patently useful
|
|
102 (defun map-query-replace-regexp (regexp to-strings &optional arg)
|
|
103 "Replace some matches for REGEXP with various strings, in rotation.
|
|
104 The second argument TO-STRINGS contains the replacement strings, separated
|
|
105 by spaces. This command works like `query-replace-regexp' except
|
|
106 that each successive replacement uses the next successive replacement string,
|
|
107 wrapping around from the last such string to the first.
|
|
108
|
|
109 Non-interactively, TO-STRINGS may be a list of replacement strings.
|
|
110
|
|
111 If `query-replace-interactive' is non-nil, the last incremental search
|
|
112 regexp is used as REGEXP--you don't have to specify it with the minibuffer.
|
|
113
|
|
114 A prefix argument N says to use each replacement string N times
|
|
115 before rotating to the next."
|
|
116 (interactive
|
|
117 (let (from to)
|
|
118 (setq from (if query-replace-interactive
|
|
119 (car regexp-search-ring)
|
|
120 (read-from-minibuffer "Map query replace (regexp): "
|
|
121 nil nil nil
|
|
122 'query-replace-history)))
|
|
123 (setq to (read-from-minibuffer
|
|
124 (format "Query replace %s with (space-separated strings): "
|
|
125 from)
|
|
126 nil nil nil
|
|
127 'query-replace-history))
|
|
128 (list from to current-prefix-arg)))
|
|
129 (let (replacements)
|
|
130 (if (listp to-strings)
|
|
131 (setq replacements to-strings)
|
|
132 (while (/= (length to-strings) 0)
|
|
133 (if (string-match " " to-strings)
|
|
134 (setq replacements
|
|
135 (append replacements
|
|
136 (list (substring to-strings 0
|
|
137 (string-match " " to-strings))))
|
|
138 to-strings (substring to-strings
|
|
139 (1+ (string-match " " to-strings))))
|
|
140 (setq replacements (append replacements (list to-strings))
|
|
141 to-strings ""))))
|
|
142 (perform-replace regexp replacements t t nil arg)))
|
|
143
|
|
144 (defun replace-string (from-string to-string &optional delimited)
|
|
145 "Replace occurrences of FROM-STRING with TO-STRING.
|
|
146 Preserve case in each match if `case-replace' and `case-fold-search'
|
|
147 are non-nil and FROM-STRING has no uppercase letters.
|
|
148 Third arg DELIMITED (prefix arg if interactive), if non-nil, means replace
|
|
149 only matches surrounded by word boundaries.
|
|
150
|
|
151 If `query-replace-interactive' is non-nil, the last incremental search
|
|
152 string is used as FROM-STRING--you don't have to specify it with the
|
|
153 minibuffer.
|
|
154
|
|
155 This function is usually the wrong thing to use in a Lisp program.
|
|
156 What you probably want is a loop like this:
|
|
157 (while (search-forward FROM-STRING nil t)
|
|
158 (replace-match TO-STRING nil t))
|
|
159 which will run faster and will not set the mark or print anything."
|
|
160 (interactive (query-replace-read-args "Replace string" nil))
|
|
161 (perform-replace from-string to-string nil nil delimited))
|
|
162
|
|
163 (defun replace-regexp (regexp to-string &optional delimited)
|
|
164 "Replace things after point matching REGEXP with TO-STRING.
|
|
165 Preserve case in each match if `case-replace' and `case-fold-search'
|
|
166 are non-nil and REGEXP has no uppercase letters.
|
|
167 \(Preserving case means that if the string matched is all caps, or capitalized,
|
|
168 then its replacement is upcased or capitalized.)
|
|
169
|
|
170 Third arg DELIMITED (prefix arg if interactive), if non-nil, means replace
|
|
171 only matches surrounded by word boundaries.
|
|
172 In TO-STRING, `\\&' stands for whatever matched the whole of REGEXP,
|
|
173 and `\\=\\N' (where N is a digit) stands for
|
|
174 whatever what matched the Nth `\\(...\\)' in REGEXP.
|
|
175
|
|
176 If `query-replace-interactive' is non-nil, the last incremental search
|
|
177 regexp is used as REGEXP--you don't have to specify it with the minibuffer.
|
|
178
|
|
179 This function is usually the wrong thing to use in a Lisp program.
|
|
180 What you probably want is a loop like this:
|
|
181 (while (re-search-forward REGEXP nil t)
|
|
182 (replace-match TO-STRING nil nil))
|
|
183 which will run faster and will not set the mark or print anything."
|
|
184 (interactive (query-replace-read-args "Replace regexp" t))
|
|
185 (perform-replace regexp to-string nil t delimited))
|
|
186
|
|
187
|
|
188 (defvar regexp-history nil
|
|
189 "History list for some commands that read regular expressions.")
|
|
190
|
|
191 (define-function 'keep-lines 'delete-non-matching-lines)
|
|
192 (defun delete-non-matching-lines (regexp)
|
|
193 "Delete all lines except those containing matches for REGEXP.
|
|
194 A match split across lines preserves all the lines it lies in.
|
|
195 Applies to all lines after point."
|
|
196 (interactive (list (read-from-minibuffer
|
|
197 "Keep lines (containing match for regexp): "
|
|
198 nil nil nil 'regexp-history)))
|
|
199 (save-excursion
|
|
200 (or (bolp) (forward-line 1))
|
|
201 (let ((start (point)))
|
|
202 (while (not (eobp))
|
|
203 ;; Start is first char not preserved by previous match.
|
|
204 (if (not (re-search-forward regexp nil 'move))
|
|
205 (delete-region start (point-max))
|
|
206 (let ((end (save-excursion (goto-char (match-beginning 0))
|
|
207 (beginning-of-line)
|
|
208 (point))))
|
|
209 ;; Now end is first char preserved by the new match.
|
|
210 (if (< start end)
|
|
211 (delete-region start end))))
|
|
212 (setq start (save-excursion (forward-line 1)
|
|
213 (point)))
|
|
214 ;; If the match was empty, avoid matching again at same place.
|
|
215 (and (not (eobp)) (= (match-beginning 0) (match-end 0))
|
|
216 (forward-char 1))))))
|
|
217
|
|
218 (define-function 'flush-lines 'delete-matching-lines)
|
|
219 (defun delete-matching-lines (regexp)
|
|
220 "Delete lines containing matches for REGEXP.
|
|
221 If a match is split across lines, all the lines it lies in are deleted.
|
|
222 Applies to lines after point."
|
|
223 (interactive (list (read-from-minibuffer
|
|
224 "Flush lines (containing match for regexp): "
|
|
225 nil nil nil 'regexp-history)))
|
|
226 (save-excursion
|
|
227 (while (and (not (eobp))
|
|
228 (re-search-forward regexp nil t))
|
|
229 (delete-region (save-excursion (goto-char (match-beginning 0))
|
|
230 (beginning-of-line)
|
|
231 (point))
|
|
232 (progn (forward-line 1) (point))))))
|
|
233
|
|
234 (define-function 'how-many 'count-matches)
|
|
235 (defun count-matches (regexp)
|
|
236 "Print number of matches for REGEXP following point."
|
|
237 (interactive (list (read-from-minibuffer
|
|
238 "How many matches for (regexp): "
|
|
239 nil nil nil 'regexp-history)))
|
|
240 (let ((count 0) opoint)
|
|
241 (save-excursion
|
|
242 (while (and (not (eobp))
|
|
243 (progn (setq opoint (point))
|
|
244 (re-search-forward regexp nil t)))
|
|
245 (if (= opoint (point))
|
|
246 (forward-char 1)
|
|
247 (setq count (1+ count))))
|
|
248 (message "%d occurrences" count))))
|
|
249
|
|
250
|
|
251 (defvar occur-mode-map ())
|
|
252 (if occur-mode-map
|
|
253 ()
|
|
254 (setq occur-mode-map (make-sparse-keymap))
|
70
|
255 (set-keymap-name occur-mode-map 'occur-mode-map)
|
|
256 (define-key occur-mode-map 'button2 'occur-mode-mouse-goto)
|
0
|
257 (define-key occur-mode-map "\C-c\C-c" 'occur-mode-goto-occurrence)
|
|
258 (define-key occur-mode-map "\C-m" 'occur-mode-goto-occurrence))
|
|
259
|
|
260 (defvar occur-buffer nil)
|
|
261 (defvar occur-nlines nil)
|
|
262 (defvar occur-pos-list nil)
|
|
263
|
|
264 (defun occur-mode ()
|
|
265 "Major mode for output from \\[occur].
|
|
266 \\<occur-mode-map>Move point to one of the items in this buffer, then use
|
|
267 \\[occur-mode-goto-occurrence] to go to the occurrence that the item refers to.
|
|
268 Alternatively, click \\[occur-mode-mouse-goto] on an item to go to it.
|
|
269
|
|
270 \\{occur-mode-map}"
|
|
271 (kill-all-local-variables)
|
|
272 (use-local-map occur-mode-map)
|
|
273 (setq major-mode 'occur-mode)
|
70
|
274 (setq mode-name (gettext "Occur"))
|
0
|
275 (make-local-variable 'occur-buffer)
|
|
276 (make-local-variable 'occur-nlines)
|
|
277 (make-local-variable 'occur-pos-list)
|
70
|
278 (require 'mode-motion)
|
|
279 (setq mode-motion-hook 'mode-motion-highlight-line)
|
0
|
280 (run-hooks 'occur-mode-hook))
|
|
281
|
70
|
282 (defun occur-mode-mouse-goto (e)
|
2
|
283 "Go to the occurrence highlighted by mouse.
|
|
284 This function is only reasonable when bound to a mouse key in the occur buffer"
|
0
|
285 (interactive "e")
|
|
286 (let ((window-save (selected-window))
|
|
287 (frame-save (selected-frame)))
|
|
288 ;; preserve the window/frame setup
|
|
289 (unwind-protect
|
|
290 (progn
|
70
|
291 (mouse-set-point e)
|
0
|
292 (occur-mode-goto-occurrence))
|
|
293 (select-frame frame-save)
|
|
294 (select-window window-save))))
|
|
295
|
|
296 (defun occur-mode-goto-occurrence ()
|
|
297 "Go to the occurrence the current line describes."
|
|
298 (interactive)
|
|
299 (if (or (null occur-buffer)
|
|
300 (null (buffer-name occur-buffer)))
|
|
301 (progn
|
|
302 (setq occur-buffer nil
|
|
303 occur-pos-list nil)
|
|
304 (error "Buffer in which occurrences were found is deleted")))
|
|
305 (let* ((line-count
|
|
306 (count-lines (point-min)
|
|
307 (save-excursion
|
|
308 (beginning-of-line)
|
|
309 (point))))
|
|
310 (occur-number (save-excursion
|
|
311 (beginning-of-line)
|
|
312 (/ (1- line-count)
|
|
313 (cond ((< occur-nlines 0)
|
|
314 (- 2 occur-nlines))
|
|
315 ((> occur-nlines 0)
|
|
316 (+ 2 (* 2 occur-nlines)))
|
|
317 (t 1)))))
|
|
318 (pos (nth occur-number occur-pos-list))
|
|
319 ;; removed t arg from Bob Weiner, 10/6/95
|
|
320 (window (get-buffer-window occur-buffer))
|
|
321 (occur-source-buffer occur-buffer))
|
|
322 (if (< line-count 1)
|
|
323 (error "No occurrence on this line"))
|
|
324 (or pos
|
|
325 (error "No occurrence on this line"))
|
70
|
326 ;; don't raise window unless it isn't visible
|
0
|
327 ;; allow for the possibility that the occur buffer is on another frame
|
|
328 (or (and window
|
|
329 (window-live-p window)
|
|
330 (frame-visible-p (window-frame window))
|
|
331 (set-buffer occur-source-buffer))
|
|
332 (and (pop-to-buffer occur-source-buffer)
|
|
333 (setq window (get-buffer-window occur-source-buffer))))
|
|
334 (goto-char pos)
|
|
335 (set-window-point window pos)))
|
|
336
|
|
337
|
|
338 (defvar list-matching-lines-default-context-lines 0
|
|
339 "*Default number of context lines to include around a `list-matching-lines'
|
|
340 match. A negative number means to include that many lines before the match.
|
|
341 A positive number means to include that many lines both before and after.")
|
|
342
|
|
343 ;; XEmacs addition
|
|
344 ;;; Damn you Jamie, this is utter trash.
|
|
345 (defvar list-matching-lines-whole-buffer t
|
|
346 "If t, occur operates on whole buffer, otherwise occur starts from point.
|
70
|
347 default is nil.")
|
0
|
348
|
|
349 (define-function 'occur 'list-matching-lines)
|
|
350 (defun list-matching-lines (regexp &optional nlines)
|
|
351 "Show all lines in the current buffer containing a match for REGEXP.
|
|
352
|
|
353 If a match spreads across multiple lines, all those lines are shown.
|
|
354
|
|
355 If variable `list-matching-lines-whole-buffer' is non-nil, the entire buffer is
|
|
356 searched, otherwise search begins at point.
|
|
357
|
70
|
358 Each line is displayed with NLINES lines before and after,
|
|
359 or -NLINES before if NLINES is negative.
|
0
|
360 NLINES defaults to `list-matching-lines-default-context-lines'.
|
|
361 Interactively it is the prefix arg.
|
|
362
|
|
363 The lines are shown in a buffer named `*Occur*'.
|
|
364 It serves as a menu to find any of the occurrences in this buffer.
|
|
365 \\[describe-mode] in that buffer will explain how."
|
|
366 (interactive
|
|
367 (list (let* ((default (or (symbol-near-point)
|
|
368 (and regexp-history
|
|
369 (car regexp-history))))
|
|
370 (minibuffer-history-minimum-string-length 0)
|
|
371 (input
|
|
372 (if default
|
|
373 ;; rewritten for I18N3 snarfing
|
|
374 (read-from-minibuffer
|
|
375 (format "List lines matching regexp (default `%s'): "
|
|
376 default) nil nil nil 'regexp-history)
|
|
377 (read-from-minibuffer
|
|
378 "List lines matching regexp: "
|
|
379 nil nil nil
|
|
380 'regexp-history))))
|
|
381 (if (and (equal input "") default)
|
|
382 (progn
|
|
383 (setq input default)
|
|
384 (setcar regexp-history default)))
|
|
385 ;; clear extra entries
|
|
386 (setcdr regexp-history (delete (car regexp-history)
|
|
387 (cdr regexp-history)))
|
|
388 input)
|
|
389 current-prefix-arg))
|
|
390 (if (equal regexp "")
|
|
391 (error "Must pass non-empty regexp to `list-matching-lines'"))
|
|
392 (setq nlines (if nlines (prefix-numeric-value nlines)
|
|
393 list-matching-lines-default-context-lines))
|
|
394 (let ((first t)
|
|
395 (dir default-directory)
|
|
396 (buffer (current-buffer))
|
|
397 (linenum 1)
|
|
398 (prevpos (point-min))
|
|
399 (final-context-start (make-marker)))
|
|
400 (if (not list-matching-lines-whole-buffer)
|
|
401 (save-excursion
|
|
402 (beginning-of-line)
|
|
403 (setq linenum (1+ (count-lines (point-min) (point))))
|
|
404 (setq prevpos (point))))
|
|
405 (with-output-to-temp-buffer "*Occur*"
|
|
406 (save-excursion
|
|
407 (set-buffer standard-output)
|
|
408 (setq default-directory dir)
|
|
409 ;; We will insert the number of lines, and "lines", later.
|
|
410 ;; #### Needs fixing for I18N3
|
|
411 (let ((print-escape-newlines t))
|
|
412 (insert (format " matching %s in buffer %s.\n"
|
|
413 regexp (buffer-name buffer))))
|
|
414 (occur-mode)
|
|
415 (setq occur-buffer buffer)
|
|
416 (setq occur-nlines nlines)
|
|
417 (setq occur-pos-list ()))
|
|
418 (if (eq buffer standard-output)
|
|
419 (goto-char (point-max)))
|
|
420 (save-excursion
|
|
421 (if list-matching-lines-whole-buffer
|
|
422 (beginning-of-buffer))
|
70
|
423 (message "Searching for %s ..." regexp)
|
0
|
424 ;; Find next match, but give up if prev match was at end of buffer.
|
|
425 (while (and (not (= prevpos (point-max)))
|
|
426 (re-search-forward regexp nil t))
|
|
427 (goto-char (match-beginning 0))
|
|
428 (beginning-of-line)
|
|
429 (save-match-data
|
|
430 (setq linenum (+ linenum (count-lines prevpos (point)))))
|
|
431 (setq prevpos (point))
|
|
432 (goto-char (match-end 0))
|
|
433 (let* ((start (save-excursion
|
|
434 (goto-char (match-beginning 0))
|
|
435 (forward-line (if (< nlines 0) nlines (- nlines)))
|
|
436 (point)))
|
|
437 (end (save-excursion
|
|
438 (goto-char (match-end 0))
|
|
439 (if (> nlines 0)
|
|
440 (forward-line (1+ nlines))
|
|
441 (forward-line 1))
|
|
442 (point)))
|
|
443 (tag (format "%5d" linenum))
|
|
444 (empty (make-string (length tag) ?\ ))
|
|
445 tem)
|
|
446 (save-excursion
|
|
447 (setq tem (make-marker))
|
|
448 (set-marker tem (point))
|
|
449 (set-buffer standard-output)
|
|
450 (setq occur-pos-list (cons tem occur-pos-list))
|
|
451 (or first (zerop nlines)
|
|
452 (insert "--------\n"))
|
|
453 (setq first nil)
|
|
454 (insert-buffer-substring buffer start end)
|
|
455 (set-marker final-context-start
|
|
456 (- (point) (- end (match-end 0))))
|
|
457 (backward-char (- end start))
|
|
458 (setq tem (if (< nlines 0) (- nlines) nlines))
|
|
459 (while (> tem 0)
|
|
460 (insert empty ?:)
|
|
461 (forward-line 1)
|
|
462 (setq tem (1- tem)))
|
|
463 (let ((this-linenum linenum))
|
|
464 (while (< (point) final-context-start)
|
|
465 (if (null tag)
|
|
466 (setq tag (format "%5d" this-linenum)))
|
|
467 (insert tag ?:)
|
|
468 ;; FSFmacs -- we handle this using mode-motion-highlight-line, above.
|
|
469 ; (put-text-property (save-excursion
|
|
470 ; (beginning-of-line)
|
|
471 ; (point))
|
|
472 ; (save-excursion
|
|
473 ; (end-of-line)
|
|
474 ; (point))
|
|
475 ; 'mouse-face 'highlight)
|
|
476 (forward-line 1)
|
|
477 (setq tag nil)
|
|
478 (setq this-linenum (1+ this-linenum)))
|
|
479 (while (<= (point) final-context-start)
|
|
480 (insert empty ?:)
|
|
481 (forward-line 1)
|
|
482 (setq this-linenum (1+ this-linenum))))
|
|
483 (while (< tem nlines)
|
|
484 (insert empty ?:)
|
|
485 (forward-line 1)
|
|
486 (setq tem (1+ tem)))
|
|
487 (goto-char (point-max)))
|
|
488 (forward-line 1)))
|
|
489 (set-buffer standard-output)
|
|
490 ;; Put positions in increasing order to go with buffer.
|
|
491 (setq occur-pos-list (nreverse occur-pos-list))
|
|
492 (goto-char (point-min))
|
|
493 (if (= (length occur-pos-list) 1)
|
|
494 (insert "1 line")
|
|
495 (insert (format "%d lines" (length occur-pos-list))))
|
|
496 (if (interactive-p)
|
|
497 (message "%d matching lines." (length occur-pos-list)))))))
|
|
498
|
|
499 ;; It would be nice to use \\[...], but there is no reasonable way
|
|
500 ;; to make that display both SPC and Y.
|
70
|
501 (defvar query-replace-help (purecopy
|
|
502 "Type Space or `y' to replace one match, Delete or `n' to skip to next,
|
0
|
503 RET or `q' to exit, Period to replace one match and exit,
|
|
504 Comma to replace but not move point immediately,
|
|
505 C-r to enter recursive edit (\\[exit-recursive-edit] to get out again),
|
|
506 C-w to delete match and recursive edit,
|
|
507 C-l to clear the frame, redisplay, and offer same replacement again,
|
|
508 ! to replace all remaining matches with no more questions,
|
70
|
509 ^ to move point back to previous match.")
|
0
|
510 "Help message while in query-replace")
|
|
511
|
70
|
512 (defvar query-replace-map nil
|
0
|
513 "Keymap that defines the responses to questions in `query-replace'.
|
|
514 The \"bindings\" in this map are not commands; they are answers.
|
|
515 The valid answers include `act', `skip', `act-and-show',
|
|
516 `exit', `act-and-exit', `edit', `delete-and-edit', `recenter',
|
|
517 `automatic', `backup', `exit-prefix', and `help'.")
|
|
518
|
|
519 (if query-replace-map
|
|
520 nil
|
|
521 (let ((map (make-sparse-keymap)))
|
|
522 (set-keymap-name map 'query-replace-map)
|
|
523 (define-key map " " 'act)
|
|
524 (define-key map "\d" 'skip)
|
|
525 (define-key map [delete] 'skip)
|
|
526 (define-key map [backspace] 'skip)
|
|
527 (define-key map "y" 'act)
|
|
528 (define-key map "n" 'skip)
|
|
529 (define-key map "Y" 'act)
|
|
530 (define-key map "N" 'skip)
|
|
531 (define-key map "," 'act-and-show)
|
|
532 (define-key map [escape] 'exit)
|
|
533 (define-key map "q" 'exit)
|
|
534 (define-key map [return] 'exit)
|
|
535 (define-key map "." 'act-and-exit)
|
|
536 (define-key map "\C-r" 'edit)
|
|
537 (define-key map "\C-w" 'delete-and-edit)
|
|
538 (define-key map "\C-l" 'recenter)
|
|
539 (define-key map "!" 'automatic)
|
|
540 (define-key map "^" 'backup)
|
|
541 (define-key map [(control h)] 'help) ;; XEmacs change
|
|
542 (define-key map [f1] 'help)
|
|
543 (define-key map [help] 'help)
|
|
544 (define-key map "?" 'help)
|
|
545 (define-key map "\C-g" 'quit)
|
|
546 (define-key map "\C-]" 'quit)
|
|
547 ;FSFmacs (define-key map "\e" 'exit-prefix)
|
|
548 (define-key map [escape] 'exit-prefix)
|
|
549
|
|
550 (setq query-replace-map map)))
|
|
551
|
|
552
|
|
553 (autoload 'isearch-highlight "isearch")
|
|
554
|
|
555 (defun perform-replace-next-event (event)
|
|
556 (if isearch-highlight
|
|
557 (let ((aborted t))
|
|
558 (unwind-protect
|
|
559 (progn
|
70
|
560 (isearch-highlight (match-beginning 0) (match-end 0))
|
0
|
561 (next-command-event event)
|
|
562 (setq aborted nil))
|
|
563 (isearch-dehighlight aborted)))
|
|
564 (next-command-event event)))
|
|
565
|
|
566 (defun perform-replace (from-string replacements
|
|
567 query-flag regexp-flag delimited-flag
|
|
568 &optional repeat-count map)
|
|
569 "Subroutine of `query-replace'. Its complexity handles interactive queries.
|
|
570 Don't use this in your own program unless you want to query and set the mark
|
|
571 just as `query-replace' does. Instead, write a simple loop like this:
|
|
572 (while (re-search-forward \"foo[ \t]+bar\" nil t)
|
|
573 (replace-match \"foobar\" nil nil))
|
|
574 which will run faster and probably do exactly what you want."
|
|
575 (or map (setq map query-replace-map))
|
|
576 (let* ((event (make-event))
|
|
577 (nocasify (not (and case-fold-search case-replace
|
70
|
578 (string-equal from-string
|
|
579 (downcase from-string)))))
|
0
|
580 (literal (not regexp-flag))
|
|
581 (search-function (if regexp-flag 're-search-forward 'search-forward))
|
|
582 (search-string from-string)
|
|
583 (real-match-data nil) ; the match data for the current match
|
|
584 (next-replacement nil)
|
|
585 (replacement-index 0)
|
|
586 (keep-going t)
|
|
587 (stack nil)
|
|
588 (next-rotate-count 0)
|
|
589 (replace-count 0)
|
|
590 (lastrepl nil) ;Position after last match considered.
|
|
591 (match-again t)
|
|
592 ;; XEmacs addition
|
|
593 (qr-case-fold-search
|
|
594 (if (and case-fold-search search-caps-disable-folding)
|
|
595 (isearch-no-upper-case-p search-string)
|
|
596 case-fold-search))
|
|
597 (message
|
|
598 (if query-flag
|
|
599 (substitute-command-keys
|
|
600 "Query replacing %s with %s: (\\<query-replace-map>\\[help] for help) "))))
|
|
601 (if (stringp replacements)
|
|
602 (setq next-replacement replacements)
|
|
603 (or repeat-count (setq repeat-count 1)))
|
|
604 (if delimited-flag
|
|
605 (setq search-function 're-search-forward
|
|
606 search-string (concat "\\b"
|
|
607 (if regexp-flag from-string
|
|
608 (regexp-quote from-string))
|
|
609 "\\b")))
|
|
610 (push-mark)
|
|
611 (undo-boundary)
|
70
|
612 ;; Loop finding occurrences that perhaps should be replaced.
|
|
613 (while (and keep-going
|
|
614 (not (eobp))
|
|
615 (let ((case-fold-search qr-case-fold-search))
|
|
616 (funcall search-function search-string nil t))
|
|
617 ;; If the search string matches immediately after
|
|
618 ;; the previous match, but it did not match there
|
|
619 ;; before the replacement was done, ignore the match.
|
|
620 (if (or (eq lastrepl (point))
|
|
621 (and regexp-flag
|
|
622 (eq lastrepl (match-beginning 0))
|
|
623 (not match-again)))
|
0
|
624
|
70
|
625 (if (eobp)
|
|
626 nil
|
|
627 ;; Don't replace the null string
|
|
628 ;; right after end of previous replacement.
|
|
629 (forward-char 1)
|
|
630 (let ((case-fold-search qr-case-fold-search))
|
|
631 (funcall search-function search-string nil t)))
|
|
632 t))
|
|
633 ;; Save the data associated with the real match.
|
|
634 (setq real-match-data (match-data))
|
|
635
|
|
636 ;; Before we make the replacement, decide whether the search string
|
|
637 ;; can match again just after this match.
|
|
638 (if regexp-flag
|
|
639 (progn
|
|
640 (setq match-again (looking-at search-string))
|
|
641 (store-match-data real-match-data)))
|
0
|
642
|
70
|
643 ;; If time for a change, advance to next replacement string.
|
|
644 (if (and (listp replacements)
|
|
645 (= next-rotate-count replace-count))
|
|
646 (progn
|
|
647 (setq next-rotate-count
|
|
648 (+ next-rotate-count repeat-count))
|
|
649 (setq next-replacement (nth replacement-index replacements))
|
|
650 (setq replacement-index (% (1+ replacement-index) (length replacements)))))
|
|
651 (if (not query-flag)
|
|
652 (progn
|
|
653 (store-match-data real-match-data)
|
|
654 (replace-match next-replacement nocasify literal)
|
|
655 (setq replace-count (1+ replace-count)))
|
|
656 (undo-boundary)
|
|
657 (let ((help-form
|
|
658 '(concat (format "Query replacing %s%s with %s.\n\n"
|
|
659 (if regexp-flag (gettext "regexp ") "")
|
|
660 from-string next-replacement)
|
|
661 (substitute-command-keys query-replace-help)))
|
|
662 (done nil)
|
|
663 (replaced nil)
|
|
664 def)
|
|
665 ;; Loop reading commands until one of them sets done,
|
|
666 ;; which means it has finished handling this occurrence.
|
|
667 (while (not done)
|
|
668 ;; Don't fill up the message log
|
|
669 ;; with a bunch of identical messages.
|
|
670 (display-message 'prompt
|
|
671 (format message from-string next-replacement))
|
|
672 (perform-replace-next-event event)
|
|
673 (setq def (lookup-key map (vector event)))
|
|
674 ;; Restore the match data while we process the command.
|
|
675 (store-match-data real-match-data)
|
|
676 (cond ((eq def 'help)
|
|
677 (with-output-to-temp-buffer (gettext "*Help*")
|
|
678 (princ (concat
|
|
679 (format "Query replacing %s%s with %s.\n\n"
|
|
680 (if regexp-flag "regexp " "")
|
|
681 from-string next-replacement)
|
|
682 (substitute-command-keys
|
|
683 query-replace-help)))
|
0
|
684 (save-excursion
|
|
685 (set-buffer standard-output)
|
|
686 (help-mode))))
|
70
|
687 ((eq def 'exit)
|
|
688 (setq keep-going nil)
|
|
689 (setq done t))
|
|
690 ((eq def 'backup)
|
|
691 (if stack
|
|
692 (let ((elt (car stack)))
|
|
693 (goto-char (car elt))
|
|
694 (setq replaced (eq t (cdr elt)))
|
|
695 (or replaced
|
|
696 (store-match-data (cdr elt)))
|
|
697 (setq stack (cdr stack)))
|
|
698 (progn
|
0
|
699 (message "No previous match")
|
|
700 (ding 'no-terminate)
|
70
|
701 (sit-for 1))))
|
|
702 ((eq def 'act)
|
|
703 (or replaced
|
|
704 (replace-match next-replacement nocasify literal))
|
|
705 (setq done t replaced t))
|
|
706 ((eq def 'act-and-exit)
|
|
707 (or replaced
|
|
708 (replace-match next-replacement nocasify literal))
|
|
709 (setq keep-going nil)
|
|
710 (setq done t replaced t))
|
|
711 ((eq def 'act-and-show)
|
|
712 (if (not replaced)
|
|
713 (progn
|
|
714 (replace-match next-replacement nocasify literal)
|
|
715 (setq replaced t))))
|
|
716 ((eq def 'automatic)
|
|
717 (or replaced
|
|
718 (replace-match next-replacement nocasify literal))
|
|
719 (setq done t query-flag nil replaced t))
|
|
720 ((eq def 'skip)
|
|
721 (setq done t))
|
|
722 ((eq def 'recenter)
|
|
723 (recenter nil))
|
|
724 ((eq def 'edit)
|
|
725 (store-match-data
|
|
726 (prog1 (match-data)
|
|
727 (save-excursion (recursive-edit))))
|
|
728 ;; Before we make the replacement,
|
|
729 ;; decide whether the search string
|
|
730 ;; can match again just after this match.
|
|
731 (if regexp-flag
|
|
732 (setq match-again (looking-at search-string))))
|
|
733 ((eq def 'delete-and-edit)
|
|
734 (delete-region (match-beginning 0) (match-end 0))
|
|
735 (store-match-data (prog1 (match-data)
|
|
736 (save-excursion (recursive-edit))))
|
|
737 (setq replaced t))
|
|
738 ;; Note: we do not need to treat `exit-prefix'
|
|
739 ;; specially here, since we reread
|
|
740 ;; any unrecognized character.
|
|
741 (t
|
|
742 (setq this-command 'mode-exited)
|
|
743 (setq keep-going nil)
|
|
744 (setq unread-command-events
|
|
745 (cons event unread-command-events))
|
|
746 (setq done t))))
|
|
747 ;; Record previous position for ^ when we move on.
|
|
748 ;; Change markers to numbers in the match data
|
|
749 ;; since lots of markers slow down editing.
|
|
750 (setq stack
|
|
751 (cons (cons (point)
|
|
752 (or replaced
|
|
753 (mapcar
|
|
754 #'(lambda (elt)
|
|
755 (if (markerp elt)
|
|
756 (prog1 (marker-position elt)
|
|
757 (set-marker elt nil))
|
|
758 elt))
|
|
759 (match-data))))
|
|
760 stack))
|
|
761 (if replaced (setq replace-count (1+ replace-count)))))
|
|
762 (setq lastrepl (point)))
|
0
|
763 (or unread-command-events
|
|
764 (message "Replaced %d occurrence%s"
|
|
765 replace-count
|
|
766 (if (= replace-count 1) "" "s")))
|
|
767 (and keep-going stack)))
|
|
768
|
70
|
769 ; FSF 19.30 original:
|
|
770 ; (defun match-string (num &optional string)
|
|
771 ; "Return string of text matched by last search.
|
|
772 ; NUM specifies which parenthesized expression in the last regexp.
|
|
773 ; Value is nil if NUMth pair didn't match, or there were less than NUM pairs.
|
|
774 ; Zero means the entire text matched by the whole regexp or whole string.
|
|
775 ; STRING should be given if the last search was by `string-match' on STRING."
|
|
776 ; (if (match-beginning num)
|
|
777 ; (if string
|
|
778 ; (substring string (match-beginning num) (match-end num))
|
|
779 ; (buffer-substring (match-beginning num) (match-end num)))))
|
0
|
780
|
70
|
781 ;; #### - this could stand to be in C...
|
|
782 (defmacro match-string (n &optional string)
|
|
783 "Returns the Nth subexpression matched by the last regular expression
|
|
784 search. The second argument, STRING, must be specified if the last
|
|
785 regular expression search was done with `string-match'."
|
|
786 ;; #### - note that match-beginning is byte coded, so it's more efficient
|
|
787 ;; to just call it twice than it is to let-bind its return value... --Stig
|
|
788 `(and (match-beginning ,n)
|
|
789 ,(if string
|
|
790 `(substring ,string (match-beginning ,n) (match-end ,n))
|
|
791 `(buffer-substring (match-beginning ,n) (match-end ,n)))))
|
0
|
792
|
|
793 (defmacro save-match-data (&rest body)
|
|
794 "Execute BODY forms, restoring the global value of the match data."
|
|
795 (let ((original (make-symbol "match-data")))
|
|
796 (list 'let (list (list original '(match-data)))
|
|
797 (list 'unwind-protect
|
|
798 (cons 'progn body)
|
|
799 (list 'store-match-data original)))))
|
|
800
|
|
801 ;;; replace.el ends here
|