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