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