annotate lisp/replace.el @ 5750:66d2f63df75f

Correct some spelling and formatting in behavior.el. Mentioned in tracker issue 826, the third thing mentioned there (the file name at the bottom of the file) had already been fixed. lisp/ChangeLog addition: 2013-08-05 Aidan Kehoe <kehoea@parhasard.net> * behavior.el: (override-behavior): Correct some spelling and formatting here, thank you Steven Mitchell in tracker issue 826.
author Aidan Kehoe <kehoea@parhasard.net>
date Mon, 05 Aug 2013 10:05:32 +0100
parents c6b1500299a7
children 0bddb59072b6
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1 ;;; replace.el --- search and replace commands for XEmacs.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
2
5686
c6b1500299a7 recenter-top-bottom synced from GNU and new default for C-l
Mats Lidell <mats.lidell@cag.se>
parents: 5473
diff changeset
3 ;; Copyright (C) 1985-7, 1992, 1994, 1997, 2003, 2012 Free Software Foundation, Inc.
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
4
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
5 ;; Maintainer: XEmacs Development Team
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
6 ;; Keywords: dumped, matching
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
7
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
8 ;; This file is part of XEmacs.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
9
5402
308d34e9f07d Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents: 3000
diff changeset
10 ;; XEmacs is free software: you can redistribute it and/or modify it
308d34e9f07d Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents: 3000
diff changeset
11 ;; under the terms of the GNU General Public License as published by the
308d34e9f07d Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents: 3000
diff changeset
12 ;; Free Software Foundation, either version 3 of the License, or (at your
308d34e9f07d Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents: 3000
diff changeset
13 ;; option) any later version.
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
14
5402
308d34e9f07d Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents: 3000
diff changeset
15 ;; XEmacs is distributed in the hope that it will be useful, but WITHOUT
308d34e9f07d Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents: 3000
diff changeset
16 ;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
308d34e9f07d Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents: 3000
diff changeset
17 ;; FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
308d34e9f07d Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents: 3000
diff changeset
18 ;; for more details.
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
19
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
20 ;; You should have received a copy of the GNU General Public License
5402
308d34e9f07d Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents: 3000
diff changeset
21 ;; along with XEmacs. If not, see <http://www.gnu.org/licenses/>.
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
22
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
23 ;;; Synched up with: FSF 19.34 [Partially].
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
24
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
25 ;;; Commentary:
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
26
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
27 ;; This file is dumped with XEmacs.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
28
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
29 ;; This package supplies the string and regular-expression replace functions
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
30 ;; documented in the XEmacs Reference Manual.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
31
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
32 ;; All the gettext calls are for XEmacs I18N3 message catalog support.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
33 ;; (This is hopelessly broken and we should remove it. -sb)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
34
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
35 ;;; Code:
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
36
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
37 (defvar case-replace t "\
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
38 *Non-nil means `query-replace' should preserve case in replacements.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
39 What this means is that `query-replace' will change the case of the
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
40 replacement text so that it matches the text that was replaced.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
41 If this variable is nil, the replacement text will be inserted
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
42 exactly as it was specified by the user, irrespective of the case
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
43 of the text that was replaced.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
44
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
45 Note that this flag has no effect if `case-fold-search' is nil,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
46 or if the replacement text has any uppercase letters in it.")
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
47
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
48 (defvar query-replace-history nil)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
49
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
50 (defvar query-replace-interactive nil
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
51 "Non-nil means `query-replace' uses the last search string.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
52 That becomes the \"string to replace\".")
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
53
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
54 (defvar replace-search-function
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
55 (lambda (str limit)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
56 (search-forward str limit t))
444
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 442
diff changeset
57 "Function used by perform-replace to search forward for a string. It will be
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
58 called with two arguments: the string to search for and a limit bounding the
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
59 search.")
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
60
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
61 (defvar replace-re-search-function
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
62 (lambda (regexp limit)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
63 (re-search-forward regexp limit t))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
64 "Function used by perform-replace to search forward for a regular
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
65 expression. It will be called with two arguments: the regexp to search for and
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
66 a limit bounding the search.")
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
67
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
68 (defun query-replace-read-args (string regexp-flag)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
69 (let (from to)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
70 (if query-replace-interactive
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
71 (setq from (car (if regexp-flag regexp-search-ring search-ring)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
72 (setq from (read-from-minibuffer (format "%s: " (gettext string))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
73 nil nil nil
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
74 'query-replace-history)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
75 (setq to (read-from-minibuffer (format "%s %s with: " (gettext string)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
76 from)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
77 nil nil nil
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
78 'query-replace-history))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
79 (list from to current-prefix-arg)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
80
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
81 ;; As per suggestion from Per Abrahamsen, limit replacement to the region
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
82 ;; if the region is active.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
83 (defun query-replace (from-string to-string &optional delimited)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
84 "Replace some occurrences of FROM-STRING with TO-STRING.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
85 As each match is found, the user must type a character saying
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
86 what to do with it. For directions, type \\[help-command] at that time.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
87
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
88 If `query-replace-interactive' is non-nil, the last incremental search
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
89 string is used as FROM-STRING--you don't have to specify it with the
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
90 minibuffer.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
91
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
92 Preserves case in each replacement if `case-replace' and `case-fold-search'
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
93 are non-nil and FROM-STRING has no uppercase letters.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
94 \(Preserving case means that if the string matched is all caps, or capitalized,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
95 then its replacement is upcased or capitalized.)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
96
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
97 Third arg DELIMITED (prefix arg if interactive), if non-nil, means replace
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
98 only matches surrounded by word boundaries.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
99
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
100 To customize possible responses, change the \"bindings\" in `query-replace-map'."
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
101 (interactive (query-replace-read-args "Query replace" nil))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
102 (perform-replace from-string to-string t nil delimited))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
103
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
104 (defun query-replace-regexp (regexp to-string &optional delimited)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
105 "Replace some things after point matching REGEXP with TO-STRING.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
106 As each match is found, the user must type a character saying
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
107 what to do with it. For directions, type \\[help-command] at that time.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
108
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
109 If `query-replace-interactive' is non-nil, the last incremental search
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
110 regexp is used as REGEXP--you don't have to specify it with the
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
111 minibuffer.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
112
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
113 Preserves case in each replacement if `case-replace' and `case-fold-search'
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
114 are non-nil and REGEXP has no uppercase letters.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
115 Third arg DELIMITED (prefix arg if interactive), if non-nil, means replace
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
116 only matches surrounded by word boundaries.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
117 In TO-STRING, `\\&' stands for whatever matched the whole of REGEXP,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
118 and `\\=\\N' (where N is a digit) stands for
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
119 whatever what matched the Nth `\\(...\\)' in REGEXP."
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
120 (interactive (query-replace-read-args "Query replace regexp" t))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
121 (perform-replace regexp to-string t t delimited))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
122
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
123 ;;#### Not patently useful
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
124 (defun map-query-replace-regexp (regexp to-strings &optional arg)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
125 "Replace some matches for REGEXP with various strings, in rotation.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
126 The second argument TO-STRINGS contains the replacement strings, separated
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
127 by spaces. This command works like `query-replace-regexp' except
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
128 that each successive replacement uses the next successive replacement string,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
129 wrapping around from the last such string to the first.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
130
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
131 Non-interactively, TO-STRINGS may be a list of replacement strings.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
132
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
133 If `query-replace-interactive' is non-nil, the last incremental search
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
134 regexp is used as REGEXP--you don't have to specify it with the minibuffer.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
135
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
136 A prefix argument N says to use each replacement string N times
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
137 before rotating to the next."
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
138 (interactive
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
139 (let (from to)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
140 (setq from (if query-replace-interactive
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
141 (car regexp-search-ring)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
142 (read-from-minibuffer "Map query replace (regexp): "
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
143 nil nil nil
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
144 'query-replace-history)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
145 (setq to (read-from-minibuffer
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
146 (format "Query replace %s with (space-separated strings): "
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
147 from)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
148 nil nil nil
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
149 'query-replace-history))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
150 (list from to current-prefix-arg)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
151 (let (replacements)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
152 (if (listp to-strings)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
153 (setq replacements to-strings)
5366
f00192e1cd49 Examining the result of #'length: `eql', not `=', it's better style & cheaper
Aidan Kehoe <kehoea@parhasard.net>
parents: 3000
diff changeset
154 (while (not (eql (length to-strings) 0))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
155 (if (string-match " " to-strings)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
156 (setq replacements
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
157 (append replacements
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
158 (list (substring to-strings 0
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
159 (string-match " " to-strings))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
160 to-strings (substring to-strings
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
161 (1+ (string-match " " to-strings))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
162 (setq replacements (append replacements (list to-strings))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
163 to-strings ""))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
164 (perform-replace regexp replacements t t nil arg)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
165
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
166 (defun replace-string (from-string to-string &optional delimited)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
167 "Replace occurrences of FROM-STRING with TO-STRING.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
168 Preserve case in each match if `case-replace' and `case-fold-search'
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
169 are non-nil and FROM-STRING has no uppercase letters.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
170 \(Preserving case means that if the string matched is all caps, or capitalized,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
171 then its replacement is upcased or capitalized.)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
172
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
173 Third arg DELIMITED (prefix arg if interactive), if non-nil, means replace
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
174 only matches surrounded by word boundaries.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
175
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
176 If `query-replace-interactive' is non-nil, the last incremental search
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
177 string is used as FROM-STRING--you don't have to specify it with the
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
178 minibuffer.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
179
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
180 This function is usually the wrong thing to use in a Lisp program.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
181 What you probably want is a loop like this:
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
182 (while (search-forward FROM-STRING nil t)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
183 (replace-match TO-STRING nil t))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
184 which will run faster and will not set the mark or print anything."
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
185 (interactive (query-replace-read-args "Replace string" nil))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
186 (perform-replace from-string to-string nil nil delimited))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
187
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
188 (defun replace-regexp (regexp to-string &optional delimited)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
189 "Replace things after point matching REGEXP with TO-STRING.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
190 Preserve case in each match if `case-replace' and `case-fold-search'
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
191 are non-nil and REGEXP has no uppercase letters.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
192 \(Preserving case means that if the string matched is all caps, or capitalized,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
193 then its replacement is upcased or capitalized.)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
194
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
195 Third arg DELIMITED (prefix arg if interactive), if non-nil, means replace
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
196 only matches surrounded by word boundaries.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
197 In TO-STRING, `\\&' stands for whatever matched the whole of REGEXP,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
198 and `\\=\\N' (where N is a digit) stands for
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
199 whatever what matched the Nth `\\(...\\)' in REGEXP.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
200
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
201 If `query-replace-interactive' is non-nil, the last incremental search
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
202 regexp is used as REGEXP--you don't have to specify it with the minibuffer.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
203
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
204 This function is usually the wrong thing to use in a Lisp program.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
205 What you probably want is a loop like this:
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
206 (while (re-search-forward REGEXP nil t)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
207 (replace-match TO-STRING nil nil))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
208 which will run faster and will not set the mark or print anything."
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
209 (interactive (query-replace-read-args "Replace regexp" t))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
210 (perform-replace regexp to-string nil t delimited))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
211
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
212
1069
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
213
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
214 ;; gse wonders: Is there a better place for this to go? Might other packages
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
215 ;; want to use it?
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
216 (defvar regexp-history nil
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
217 "History list for some commands that read regular expressions.")
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
218
1069
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
219 (defun operate-on-non-matching-lines (regexp delete kill &optional beg end)
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
220 "Internal function used by delete-non-matching-lines,
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
221 kill-non-matching-lines, and copy-matching-lines.
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
222
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
223 REGEXP is a regular expression to *not* match when performing
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
224 operations.
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
225
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
226 If DELETE is non-nil, the lines of text are deleted. It doesn't make
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
227 sense to set this to nil if KILL is nil -- nothing will happen.
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
228
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
229 If KILL is non-nil, the lines of text are stored in the kill ring (as
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
230 one block of text).
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
231
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
232 BEG and END, if non-nil, specify the start and end locations to work
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
233 within. If these are nil, point and point-max are used.
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
234
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
235 A match split across lines preserves all the lines it lies in.
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
236 Applies to all lines after point.
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
237
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
238 Returns the number of lines matched."
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
239 (with-search-caps-disable-folding regexp t
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
240 (save-excursion
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
241 ;; Move to a beginning point if specified.
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
242 (when beg (goto-char beg))
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
243 ;; Always start on the beginning of a line.
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
244 (or (bolp) (forward-line 1))
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
245
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
246 (let ((matched-text nil)
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
247 (curmatch-start (point))
2610
16738b49b833 [xemacs-hg @ 2005-02-23 22:09:13 by adrian]
adrian
parents: 1476
diff changeset
248 (limit (copy-marker (point-max)))
16738b49b833 [xemacs-hg @ 2005-02-23 22:09:13 by adrian]
adrian
parents: 1476
diff changeset
249 (matched-text-buffer (generate-new-buffer " *matched-text*"))
16738b49b833 [xemacs-hg @ 2005-02-23 22:09:13 by adrian]
adrian
parents: 1476
diff changeset
250 lines-matched)
1069
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
251 ;; Limit search if limits were specified.
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
252 (when end (setq limit (copy-marker end)))
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
253
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
254 ;; Search. Stop if we are at end of buffer or outside the
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
255 ;; limit.
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
256 (while (not (or
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
257 (eobp)
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
258 (and limit (>= (point) limit))))
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
259 ;; curmatch-start is first char not preserved by previous match.
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
260 (if (not (re-search-forward regexp limit 'move))
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
261 (let ((curmatch-end limit))
2610
16738b49b833 [xemacs-hg @ 2005-02-23 22:09:13 by adrian]
adrian
parents: 1476
diff changeset
262 (append-to-buffer matched-text-buffer curmatch-start curmatch-end)
1069
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
263 (if delete (delete-region curmatch-start curmatch-end)))
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
264 (let ((curmatch-end (save-excursion (goto-char (match-beginning 0))
2610
16738b49b833 [xemacs-hg @ 2005-02-23 22:09:13 by adrian]
adrian
parents: 1476
diff changeset
265 (beginning-of-line)
16738b49b833 [xemacs-hg @ 2005-02-23 22:09:13 by adrian]
adrian
parents: 1476
diff changeset
266 (point))))
1069
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
267 ;; Now curmatch-end is first char preserved by the new match.
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
268 (if (< curmatch-start curmatch-end)
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
269 (progn
2610
16738b49b833 [xemacs-hg @ 2005-02-23 22:09:13 by adrian]
adrian
parents: 1476
diff changeset
270 (append-to-buffer matched-text-buffer curmatch-start curmatch-end)
1069
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
271 (if delete (delete-region curmatch-start curmatch-end))))))
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
272 (setq curmatch-start (save-excursion (forward-line 1)
2610
16738b49b833 [xemacs-hg @ 2005-02-23 22:09:13 by adrian]
adrian
parents: 1476
diff changeset
273 (point)))
1069
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
274 ;; If the match was empty, avoid matching again at same place.
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
275 (and (not (eobp)) (= (match-beginning 0) (match-end 0))
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
276 (forward-char 1)))
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
277
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
278 ;; If any lines were matched and KILL is non-nil, insert the
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
279 ;; matched lines into the kill ring.
2610
16738b49b833 [xemacs-hg @ 2005-02-23 22:09:13 by adrian]
adrian
parents: 1476
diff changeset
280 (setq matched-text (buffer-string matched-text-buffer))
1069
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
281 (if (and matched-text kill) (kill-new matched-text))
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
282
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
283 ;; Return the number of matched lines.
2610
16738b49b833 [xemacs-hg @ 2005-02-23 22:09:13 by adrian]
adrian
parents: 1476
diff changeset
284 (setq lines-matched
16738b49b833 [xemacs-hg @ 2005-02-23 22:09:13 by adrian]
adrian
parents: 1476
diff changeset
285 (with-current-buffer matched-text-buffer
16738b49b833 [xemacs-hg @ 2005-02-23 22:09:13 by adrian]
adrian
parents: 1476
diff changeset
286 (count-lines (point-min) (point-max))))
16738b49b833 [xemacs-hg @ 2005-02-23 22:09:13 by adrian]
adrian
parents: 1476
diff changeset
287 (kill-buffer matched-text-buffer)
16738b49b833 [xemacs-hg @ 2005-02-23 22:09:13 by adrian]
adrian
parents: 1476
diff changeset
288 lines-matched))))
1069
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
289
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
290 (define-function 'keep-lines 'delete-non-matching-lines)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
291 (defun delete-non-matching-lines (regexp)
1069
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
292 "Delete lines that do not match REGEXP, from point to the end of the
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
293 buffer (or within the region, if it is active)."
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
294 (interactive (list (read-from-minibuffer
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
295 "Keep lines (containing match for regexp): "
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
296 nil nil nil 'regexp-history)))
1069
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
297 (let ((beg nil)
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
298 (end nil)
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
299 (count nil))
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
300 (when (region-active-p)
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
301 (setq beg (region-beginning))
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
302 (setq end (region-end)))
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
303 (setq count (operate-on-non-matching-lines regexp t nil beg end))
1476
d824841064f3 [xemacs-hg @ 2003-05-12 05:12:10 by youngs]
youngs
parents: 1333
diff changeset
304 (when (interactive-p)
d824841064f3 [xemacs-hg @ 2003-05-12 05:12:10 by youngs]
youngs
parents: 1333
diff changeset
305 (message "%i lines deleted" count))))
1069
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
306
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
307 (defun kill-non-matching-lines (regexp)
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
308 "Delete the lines that do not match REGEXP, from point to the end of
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
309 the buffer (or within the region, if it is active). The deleted lines
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
310 are placed in the kill ring as one block of text."
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
311 (interactive (list (read-from-minibuffer
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
312 "Kill non-matching lines (regexp): "
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
313 nil nil nil 'regexp-history)))
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
314 (let ((beg nil)
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
315 (end nil)
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
316 (count nil))
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
317 (when (region-active-p)
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
318 (setq beg (region-beginning))
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
319 (setq end (region-end)))
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
320 (setq count (operate-on-non-matching-lines regexp t t beg end))
1476
d824841064f3 [xemacs-hg @ 2003-05-12 05:12:10 by youngs]
youngs
parents: 1333
diff changeset
321 (when (interactive-p)
d824841064f3 [xemacs-hg @ 2003-05-12 05:12:10 by youngs]
youngs
parents: 1333
diff changeset
322 (message "%i lines killed" count))))
1069
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
323
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
324 (defun copy-non-matching-lines (regexp)
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
325 "Find all lines that do not match REGEXP from point to the end of the
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
326 buffer (or within the region, if it is active), and place them in the
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
327 kill ring as one block of text."
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
328 (interactive (list (read-from-minibuffer
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
329 "Copy non-matching lines (regexp): "
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
330 nil nil nil 'regexp-history)))
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
331 (let ((beg nil)
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
332 (end nil)
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
333 (count nil))
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
334 (when (region-active-p)
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
335 (setq beg (region-beginning))
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
336 (setq end (region-end)))
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
337 (setq count (operate-on-non-matching-lines regexp nil t beg end))
1476
d824841064f3 [xemacs-hg @ 2003-05-12 05:12:10 by youngs]
youngs
parents: 1333
diff changeset
338 (when (interactive-p)
d824841064f3 [xemacs-hg @ 2003-05-12 05:12:10 by youngs]
youngs
parents: 1333
diff changeset
339 (message "%i lines copied" count))))
1069
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
340
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
341 (defun operate-on-matching-lines (regexp delete kill &optional beg end)
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
342 "Internal function used by delete-matching-lines, kill-matching-lines,
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
343 and copy-matching-lines.
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
344
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
345 If DELETE is non-nil, the lines of text are deleted. It doesn't make
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
346 sense to set this to nil if KILL is nil -- nothing will happen.
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
347
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
348 If KILL is non-nil, the lines of text are stored in the kill ring (as
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
349 one block of text).
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
350
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
351 BEG and END, if non-nil, specify the start and end locations to work
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
352 within. If these are nil, point and point-max are used.
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
353
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
354 If a match is split across lines, all the lines it lies in are deleted.
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
355 Applies to lines after point.
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
356 Returns the number of lines matched."
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
357 (with-search-caps-disable-folding regexp t
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
358 (save-excursion
1069
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
359 (let ((matched-text nil)
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
360 (curmatch-start nil)
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
361 (curmatch-end nil)
2610
16738b49b833 [xemacs-hg @ 2005-02-23 22:09:13 by adrian]
adrian
parents: 1476
diff changeset
362 (limit nil)
16738b49b833 [xemacs-hg @ 2005-02-23 22:09:13 by adrian]
adrian
parents: 1476
diff changeset
363 (matched-text-buffer (generate-new-buffer " *matched-text*"))
16738b49b833 [xemacs-hg @ 2005-02-23 22:09:13 by adrian]
adrian
parents: 1476
diff changeset
364 lines-matched)
1069
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
365 ;; Limit search if limits were specified.
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
366 (when beg (goto-char beg))
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
367 (when end (setq limit (copy-marker end)))
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
368
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
369 (while (and (not (eobp))
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
370 (re-search-forward regexp limit t))
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
371 (setq curmatch-start (save-excursion (goto-char (match-beginning 0))
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
372 (beginning-of-line)
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
373 (point)))
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
374 (setq curmatch-end (progn (forward-line 1) (point)))
2610
16738b49b833 [xemacs-hg @ 2005-02-23 22:09:13 by adrian]
adrian
parents: 1476
diff changeset
375 (append-to-buffer matched-text-buffer curmatch-start curmatch-end)
1069
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
376 (if delete (delete-region curmatch-start curmatch-end)))
2610
16738b49b833 [xemacs-hg @ 2005-02-23 22:09:13 by adrian]
adrian
parents: 1476
diff changeset
377 (setq matched-text (buffer-string matched-text-buffer))
1069
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
378 (if (and matched-text kill) (kill-new matched-text))
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
379
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
380 ;; Return the number of matched lines.
2610
16738b49b833 [xemacs-hg @ 2005-02-23 22:09:13 by adrian]
adrian
parents: 1476
diff changeset
381 (setq lines-matched
16738b49b833 [xemacs-hg @ 2005-02-23 22:09:13 by adrian]
adrian
parents: 1476
diff changeset
382 (with-current-buffer matched-text-buffer
16738b49b833 [xemacs-hg @ 2005-02-23 22:09:13 by adrian]
adrian
parents: 1476
diff changeset
383 (count-lines (point-min) (point-max))))
16738b49b833 [xemacs-hg @ 2005-02-23 22:09:13 by adrian]
adrian
parents: 1476
diff changeset
384 (kill-buffer matched-text-buffer)
16738b49b833 [xemacs-hg @ 2005-02-23 22:09:13 by adrian]
adrian
parents: 1476
diff changeset
385 lines-matched))))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
386
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
387 (define-function 'flush-lines 'delete-matching-lines)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
388 (defun delete-matching-lines (regexp)
1069
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
389 "Delete the lines that match REGEXP, from point to the end of the
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
390 buffer (or within the region, if it is active)."
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
391 (interactive (list (read-from-minibuffer
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
392 "Flush lines (containing match for regexp): "
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
393 nil nil nil 'regexp-history)))
1069
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
394 (let ((beg nil)
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
395 (end nil)
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
396 (count nil))
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
397 (when (region-active-p)
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
398 (setq beg (region-beginning))
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
399 (setq end (region-end)))
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
400 (setq count (operate-on-matching-lines regexp t nil beg end))
1476
d824841064f3 [xemacs-hg @ 2003-05-12 05:12:10 by youngs]
youngs
parents: 1333
diff changeset
401 (when (interactive-p)
d824841064f3 [xemacs-hg @ 2003-05-12 05:12:10 by youngs]
youngs
parents: 1333
diff changeset
402 (message "%i lines deleted" count))))
1069
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
403
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
404 (defun kill-matching-lines (regexp)
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
405 "Delete the lines that match REGEXP, from point to the end of the
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
406 buffer (or within the region, if it is active). The deleted lines are
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
407 placed in the kill ring as one block of text."
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
408 (interactive (list (read-from-minibuffer
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
409 "Kill lines (containing match for regexp): "
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
410 nil nil nil 'regexp-history)))
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
411 (let ((beg nil)
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
412 (end nil)
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
413 (count nil))
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
414 (when (region-active-p)
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
415 (setq beg (region-beginning))
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
416 (setq end (region-end)))
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
417 (setq count (operate-on-matching-lines regexp t t beg end))
1476
d824841064f3 [xemacs-hg @ 2003-05-12 05:12:10 by youngs]
youngs
parents: 1333
diff changeset
418 (when (interactive-p)
d824841064f3 [xemacs-hg @ 2003-05-12 05:12:10 by youngs]
youngs
parents: 1333
diff changeset
419 (message "%i lines killed" count))))
1069
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
420
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
421 (defun copy-matching-lines (regexp)
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
422 "Find all lines that match REGEXP from point to the end of the
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
423 buffer (or within the region, if it is active), and place them in the
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
424 kill ring as one block of text."
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
425 (interactive (list (read-from-minibuffer
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
426 "Copy lines (containing match for regexp): "
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
427 nil nil nil 'regexp-history)))
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
428 (let ((beg nil)
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
429 (end nil)
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
430 (count nil))
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
431 (when (region-active-p)
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
432 (setq beg (region-beginning))
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
433 (setq end (region-end)))
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
434 (setq count (operate-on-matching-lines regexp nil t beg end))
1476
d824841064f3 [xemacs-hg @ 2003-05-12 05:12:10 by youngs]
youngs
parents: 1333
diff changeset
435 (when (interactive-p)
d824841064f3 [xemacs-hg @ 2003-05-12 05:12:10 by youngs]
youngs
parents: 1333
diff changeset
436 (message "%i lines copied" count))))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
437
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
438 (define-function 'how-many 'count-matches)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
439 (defun count-matches (regexp)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
440 "Print number of matches for REGEXP following point."
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
441 (interactive (list (read-from-minibuffer
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
442 "How many matches for (regexp): "
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
443 nil nil nil 'regexp-history)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
444 (with-interactive-search-caps-disable-folding regexp t
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
445 (let ((count 0) opoint)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
446 (save-excursion
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
447 (while (and (not (eobp))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
448 (progn (setq opoint (point))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
449 (re-search-forward regexp nil t)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
450 (if (= opoint (point))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
451 (forward-char 1)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
452 (setq count (1+ count))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
453 (message "%d occurrences" count)))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
454
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
455
3000
5df5ea55d3fc [xemacs-hg @ 2005-10-18 20:49:41 by malcolmp]
malcolmp
parents: 2610
diff changeset
456 ;;; occur code moved to occur.el
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
457
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
458 ;; It would be nice to use \\[...], but there is no reasonable way
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
459 ;; to make that display both SPC and Y.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
460 (defconst query-replace-help
444
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 442
diff changeset
461 "Type Space or `y' to replace one match, Delete or `n' to skip to next,
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
462 RET or `q' to exit, Period to replace one match and exit,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
463 Comma to replace but not move point immediately,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
464 C-r to enter recursive edit (\\[exit-recursive-edit] to get out again),
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
465 C-w to delete match and recursive edit,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
466 C-l to clear the frame, redisplay, and offer same replacement again,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
467 ! to replace all remaining matches with no more questions,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
468 ^ to move point back to previous match."
444
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 442
diff changeset
469
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
470 "Help message while in query-replace")
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
471
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
472 (defvar query-replace-map nil
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
473 "Keymap that defines the responses to questions in `query-replace'.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
474 The \"bindings\" in this map are not commands; they are answers.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
475 The valid answers include `act', `skip', `act-and-show',
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
476 `exit', `act-and-exit', `edit', `delete-and-edit', `recenter',
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
477 `automatic', `backup', `exit-prefix', and `help'.")
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
478
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
479 ;; Why does it seem that ever file has a different method of doing this?
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
480 (if query-replace-map
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
481 nil
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
482 (let ((map (make-sparse-keymap)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
483 (set-keymap-name map 'query-replace-map)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
484 (define-key map " " 'act)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
485 (define-key map "\d" 'skip)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
486 (define-key map [delete] 'skip)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
487 (define-key map [backspace] 'skip)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
488 (define-key map "y" 'act)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
489 (define-key map "n" 'skip)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
490 (define-key map "Y" 'act)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
491 (define-key map "N" 'skip)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
492 (define-key map "," 'act-and-show)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
493 (define-key map [escape] 'exit)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
494 (define-key map "q" 'exit)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
495 (define-key map [return] 'exit)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
496 (define-key map "." 'act-and-exit)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
497 (define-key map "\C-r" 'edit)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
498 (define-key map "\C-w" 'delete-and-edit)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
499 (define-key map "\C-l" 'recenter)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
500 (define-key map "!" 'automatic)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
501 (define-key map "^" 'backup)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
502 (define-key map [(control h)] 'help) ;; XEmacs change
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
503 (define-key map [f1] 'help)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
504 (define-key map [help] 'help)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
505 (define-key map "?" 'help)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
506 (define-key map "\C-g" 'quit)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
507 (define-key map "\C-]" 'quit)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
508 ;FSFmacs (define-key map "\e" 'exit-prefix)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
509 (define-key map [escape] 'exit-prefix)
444
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 442
diff changeset
510
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
511 (setq query-replace-map map)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
512
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
513 ;; isearch-mode is dumped, so don't autoload.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
514 ;(autoload 'isearch-highlight "isearch")
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
515
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
516 ;; XEmacs
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
517 (defun perform-replace-next-event (event)
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 428
diff changeset
518 (if search-highlight
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
519 (let ((aborted t))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
520 (unwind-protect
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
521 (progn
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
522 (if (match-beginning 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
523 (isearch-highlight (match-beginning 0) (match-end 0)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
524 (next-command-event event)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
525 (setq aborted nil))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
526 (isearch-dehighlight aborted)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
527 (next-command-event event)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
528
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
529 (defun perform-replace (from-string replacements
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
530 query-flag regexp-flag delimited-flag
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
531 &optional repeat-count map)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
532 "Subroutine of `query-replace'. Its complexity handles interactive queries.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
533 Don't use this in your own program unless you want to query and set the mark
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
534 just as `query-replace' does. Instead, write a simple loop like this:
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
535 (while (re-search-forward \"foo[ \t]+bar\" nil t)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
536 (replace-match \"foobar\" nil nil))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
537 which will run faster and probably do exactly what you want.
444
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 442
diff changeset
538 When searching for a match, this function uses
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 442
diff changeset
539 `replace-search-function' and `replace-re-search-function'."
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
540 (or map (setq map query-replace-map))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
541 (let* ((event (make-event))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
542 (nocasify (not (and case-fold-search case-replace
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
543 (string-equal from-string
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
544 (downcase from-string)))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
545 (literal (not regexp-flag))
444
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 442
diff changeset
546 (search-function (if regexp-flag
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 442
diff changeset
547 replace-re-search-function
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
548 replace-search-function))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
549 (search-string from-string)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
550 (real-match-data nil) ; the match data for the current match
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
551 (next-replacement nil)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
552 (replacement-index 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
553 (keep-going t)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
554 (stack nil)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
555 (next-rotate-count 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
556 (replace-count 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
557 (lastrepl nil) ;Position after last match considered.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
558 ;; If non-nil, it is marker saying where in the buffer to
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
559 ;; stop.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
560 (limit nil)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
561 (match-again t)
5686
c6b1500299a7 recenter-top-bottom synced from GNU and new default for C-l
Mats Lidell <mats.lidell@cag.se>
parents: 5473
diff changeset
562 (recenter-last-op nil) ; Start cycling order with initial position.
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
563 ;; XEmacs addition
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
564 (qr-case-fold-search
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
565 (if (and case-fold-search search-caps-disable-folding)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
566 (no-upper-case-p search-string regexp-flag)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
567 case-fold-search))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
568 (message
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
569 (if query-flag
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
570 (substitute-command-keys
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
571 "Query replacing %s with %s: (\\<query-replace-map>\\[help] for help) "))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
572 ;; If the region is active, operate on region.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
573 (when (region-active-p)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
574 ;; Original Per Abrahamsen's code simply narrowed the region,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
575 ;; thus providing a visual indication of the search boundary.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
576 ;; Stallman, on the other hand, handles it like this.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
577 (setq limit (copy-marker (region-end)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
578 (goto-char (region-beginning))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
579 (zmacs-deactivate-region))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
580 (if (stringp replacements)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
581 (setq next-replacement replacements)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
582 (or repeat-count (setq repeat-count 1)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
583 (if delimited-flag
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
584 (setq search-function replace-re-search-function
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
585 search-string (concat "\\b"
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
586 (if regexp-flag from-string
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
587 (regexp-quote from-string))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
588 "\\b")))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
589 (push-mark)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
590 (undo-boundary)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
591 (unwind-protect
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
592 ;; Loop finding occurrences that perhaps should be replaced.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
593 (while (and keep-going
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
594 (not (eobp))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
595 (or (null limit) (< (point) limit))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
596 (let ((case-fold-search qr-case-fold-search))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
597 (funcall search-function search-string limit))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
598 ;; If the search string matches immediately after
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
599 ;; the previous match, but it did not match there
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
600 ;; before the replacement was done, ignore the match.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
601 (if (or (eq lastrepl (point))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
602 (and regexp-flag
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
603 (eq lastrepl (match-beginning 0))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
604 (not match-again)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
605 (if (or (eobp)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
606 (and limit (>= (point) limit)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
607 nil
444
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 442
diff changeset
608 ;; Don't replace the null string
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
609 ;; right after end of previous replacement.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
610 (forward-char 1)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
611 (let ((case-fold-search qr-case-fold-search))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
612 (funcall search-function search-string limit)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
613 t))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
614
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
615 ;; Save the data associated with the real match.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
616 (setq real-match-data (match-data))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
617
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
618 ;; Before we make the replacement, decide whether the search string
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
619 ;; can match again just after this match.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
620 (if regexp-flag
444
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 442
diff changeset
621 (progn
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
622 (setq match-again (looking-at search-string))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
623 ;; XEmacs addition
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
624 (store-match-data real-match-data)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
625 ;; If time for a change, advance to next replacement string.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
626 (if (and (listp replacements)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
627 (= next-rotate-count replace-count))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
628 (progn
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
629 (setq next-rotate-count
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
630 (+ next-rotate-count repeat-count))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
631 (setq next-replacement (nth replacement-index replacements))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
632 (setq replacement-index (% (1+ replacement-index) (length replacements)))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
633 (if (not query-flag)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
634 (progn
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
635 (store-match-data real-match-data)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
636 (replace-match next-replacement nocasify literal)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
637 (setq replace-count (1+ replace-count)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
638 (undo-boundary)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
639 (let ((help-form
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
640 '(concat (format "Query replacing %s%s with %s.\n\n"
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
641 (if regexp-flag (gettext "regexp ") "")
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
642 from-string next-replacement)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
643 (substitute-command-keys query-replace-help)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
644 done replaced def)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
645 ;; Loop reading commands until one of them sets done,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
646 ;; which means it has finished handling this occurrence.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
647 (while (not done)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
648 ;; Don't fill up the message log
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
649 ;; with a bunch of identical messages.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
650 ;; XEmacs change
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
651 (display-message 'prompt
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
652 (format message from-string next-replacement))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
653 (perform-replace-next-event event)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
654 (setq def (lookup-key map (vector event)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
655 ;; Restore the match data while we process the command.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
656 (store-match-data real-match-data)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
657 (cond ((eq def 'help)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
658 (with-output-to-temp-buffer (gettext "*Help*")
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
659 (princ (concat
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
660 (format "Query replacing %s%s with %s.\n\n"
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
661 (if regexp-flag "regexp " "")
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
662 from-string next-replacement)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
663 (substitute-command-keys
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
664 query-replace-help)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
665 (save-excursion
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
666 (set-buffer standard-output)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
667 (help-mode))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
668 ((eq def 'exit)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
669 (setq keep-going nil)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
670 (setq done t))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
671 ((eq def 'backup)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
672 (if stack
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
673 (let ((elt (car stack)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
674 (goto-char (car elt))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
675 (setq replaced (eq t (cdr elt)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
676 (or replaced
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
677 (store-match-data (cdr elt)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
678 (setq stack (cdr stack)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
679 (message "No previous match")
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
680 (ding 'no-terminate)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
681 (sit-for 1)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
682 ((eq def 'act)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
683 (or replaced
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
684 (replace-match next-replacement nocasify literal))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
685 (setq done t replaced t))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
686 ((eq def 'act-and-exit)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
687 (or replaced
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
688 (replace-match next-replacement nocasify literal))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
689 (setq keep-going nil)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
690 (setq done t replaced t))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
691 ((eq def 'act-and-show)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
692 (if (not replaced)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
693 (progn
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
694 (replace-match next-replacement nocasify literal)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
695 (store-match-data nil)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
696 (setq replaced t))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
697 ((eq def 'automatic)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
698 (or replaced
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
699 (replace-match next-replacement nocasify literal))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
700 (setq done t query-flag nil replaced t))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
701 ((eq def 'skip)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
702 (setq done t))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
703 ((eq def 'recenter)
5686
c6b1500299a7 recenter-top-bottom synced from GNU and new default for C-l
Mats Lidell <mats.lidell@cag.se>
parents: 5473
diff changeset
704 ;; `this-command' has the value `query-replace',
c6b1500299a7 recenter-top-bottom synced from GNU and new default for C-l
Mats Lidell <mats.lidell@cag.se>
parents: 5473
diff changeset
705 ;; so we need to bind it to `recenter-top-bottom'
c6b1500299a7 recenter-top-bottom synced from GNU and new default for C-l
Mats Lidell <mats.lidell@cag.se>
parents: 5473
diff changeset
706 ;; to allow it to detect a sequence of `C-l'.
c6b1500299a7 recenter-top-bottom synced from GNU and new default for C-l
Mats Lidell <mats.lidell@cag.se>
parents: 5473
diff changeset
707 (let ((this-command 'recenter-top-bottom)
c6b1500299a7 recenter-top-bottom synced from GNU and new default for C-l
Mats Lidell <mats.lidell@cag.se>
parents: 5473
diff changeset
708 (last-command 'recenter-top-bottom))
c6b1500299a7 recenter-top-bottom synced from GNU and new default for C-l
Mats Lidell <mats.lidell@cag.se>
parents: 5473
diff changeset
709 (recenter-top-bottom)))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
710 ((eq def 'edit)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
711 (store-match-data
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
712 (prog1 (match-data)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
713 (save-excursion (recursive-edit))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
714 ;; Before we make the replacement,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
715 ;; decide whether the search string
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
716 ;; can match again just after this match.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
717 (if regexp-flag
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
718 (setq match-again (looking-at search-string))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
719 ((eq def 'delete-and-edit)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
720 (delete-region (match-beginning 0) (match-end 0))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
721 (store-match-data (prog1 (match-data)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
722 (save-excursion (recursive-edit))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
723 (setq replaced t))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
724 ;; Note: we do not need to treat `exit-prefix'
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
725 ;; specially here, since we reread
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
726 ;; any unrecognized character.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
727 (t
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
728 (setq this-command 'mode-exited)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
729 (setq keep-going nil)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
730 (setq unread-command-events
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
731 (cons event unread-command-events))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
732 (setq done t))))
5686
c6b1500299a7 recenter-top-bottom synced from GNU and new default for C-l
Mats Lidell <mats.lidell@cag.se>
parents: 5473
diff changeset
733 (unless (eq def 'recenter)
c6b1500299a7 recenter-top-bottom synced from GNU and new default for C-l
Mats Lidell <mats.lidell@cag.se>
parents: 5473
diff changeset
734 ;; Reset recenter cycling order to initial position.
c6b1500299a7 recenter-top-bottom synced from GNU and new default for C-l
Mats Lidell <mats.lidell@cag.se>
parents: 5473
diff changeset
735 (setq recenter-last-op nil))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
736 ;; Record previous position for ^ when we move on.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
737 ;; Change markers to numbers in the match data
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
738 ;; since lots of markers slow down editing.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
739 (setq stack
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
740 (cons (cons (point)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
741 (or replaced
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
742 (match-data t)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
743 stack))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
744 (if replaced (setq replace-count (1+ replace-count)))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
745 (setq lastrepl (point)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
746 ;; Useless in XEmacs. We handle (de)highlighting through
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
747 ;; perform-replace-next-event.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
748 ;(replace-dehighlight)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
749 )
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
750 (or unread-command-events
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
751 (message "Replaced %d occurrence%s"
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
752 replace-count
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
753 (if (= replace-count 1) "" "s")))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
754 (and keep-going stack)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
755
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
756 ;; FSFmacs code: someone should port it.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
757
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
758 ;(defvar query-replace-highlight nil
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
759 ; "*Non-nil means to highlight words during query replacement.")
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
760
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
761 ;(defvar replace-overlay nil)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
762
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
763 ;(defun replace-dehighlight ()
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
764 ; (and replace-overlay
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
765 ; (progn
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
766 ; (delete-overlay replace-overlay)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
767 ; (setq replace-overlay nil))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
768
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
769 ;(defun replace-highlight (start end)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
770 ; (and query-replace-highlight
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
771 ; (progn
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
772 ; (or replace-overlay
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
773 ; (progn
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
774 ; (setq replace-overlay (make-overlay start end))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
775 ; (overlay-put replace-overlay 'face
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
776 ; (if (internal-find-face 'query-replace)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
777 ; 'query-replace 'region))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
778 ; (move-overlay replace-overlay start end (current-buffer)))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
779
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
780 ;;; replace.el ends here