annotate lisp/replace.el @ 398:74fd4e045ea6 r21-2-29

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