annotate lisp/prim/replace.el @ 70:131b0175ea99 r20-0b30

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