annotate lisp/prim/replace.el @ 207:e45d5e7c476e r20-4b2

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