aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGerd Moellmann2000-04-25 19:19:31 +0000
committerGerd Moellmann2000-04-25 19:19:31 +0000
commit47d722546b11876552fc9da0d89a5265b9b45b9a (patch)
tree4668831f0595b1778f1f682b8d5b4547e36fd22e
parent16bf9fa9d931390934a71792bc567c3520bd1aec (diff)
downloademacs-47d722546b11876552fc9da0d89a5265b9b45b9a.tar.gz
emacs-47d722546b11876552fc9da0d89a5265b9b45b9a.zip
(perform-replace): Add parameters START and END. Use
them instead of the check for a region in Transient Mark mode. (query-replace-read-args): Return two more list elements for the start and end of the region in Transient Mark mode. (query-replace, query-replace-regexp, query-replace-regexp-eval) (map-query-replace-regexp, replace-string, replace-regexp): Add optional last arguments START and END and pass them to perform-replace.
-rw-r--r--lisp/replace.el74
1 files changed, 46 insertions, 28 deletions
diff --git a/lisp/replace.el b/lisp/replace.el
index 51005ea2fbe..fcb7ad423b6 100644
--- a/lisp/replace.el
+++ b/lisp/replace.el
@@ -67,9 +67,11 @@ strings or patterns."
67 (setq to (read-from-minibuffer (format "%s %s with: " string from) 67 (setq to (read-from-minibuffer (format "%s %s with: " string from)
68 nil nil nil 68 nil nil nil
69 query-replace-to-history-variable from t)) 69 query-replace-to-history-variable from t))
70 (list from to current-prefix-arg))) 70 (if (and transient-mark-mode mark-active)
71 (list from to current-prefix-arg (region-beginning) (region-end))
72 (list from to current-prefix-arg nil nil))))
71 73
72(defun query-replace (from-string to-string &optional delimited) 74(defun query-replace (from-string to-string &optional delimited start end)
73 "Replace some occurrences of FROM-STRING with TO-STRING. 75 "Replace some occurrences of FROM-STRING with TO-STRING.
74As each match is found, the user must type a character saying 76As each match is found, the user must type a character saying
75what to do with it. For directions, type \\[help-command] at that time. 77what to do with it. For directions, type \\[help-command] at that time.
@@ -89,14 +91,15 @@ then its replacement is upcased or capitalized.)
89 91
90Third arg DELIMITED (prefix arg if interactive), if non-nil, means replace 92Third arg DELIMITED (prefix arg if interactive), if non-nil, means replace
91only matches surrounded by word boundaries. 93only matches surrounded by word boundaries.
94Fourth and fifth arg START and END specify the region to operate on.
92 95
93To customize possible responses, change the \"bindings\" in `query-replace-map'." 96To customize possible responses, change the \"bindings\" in `query-replace-map'."
94 (interactive (query-replace-read-args "Query replace" nil)) 97 (interactive (query-replace-read-args "Query replace" nil))
95 (perform-replace from-string to-string t nil delimited)) 98 (perform-replace from-string to-string start end t nil delimited))
96 99
97(define-key esc-map "%" 'query-replace) 100(define-key esc-map "%" 'query-replace)
98 101
99(defun query-replace-regexp (regexp to-string &optional delimited) 102(defun query-replace-regexp (regexp to-string &optional delimited start end)
100 "Replace some things after point matching REGEXP with TO-STRING. 103 "Replace some things after point matching REGEXP with TO-STRING.
101As each match is found, the user must type a character saying 104As each match is found, the user must type a character saying
102what to do with it. For directions, type \\[help-command] at that time. 105what to do with it. For directions, type \\[help-command] at that time.
@@ -110,16 +113,19 @@ minibuffer.
110 113
111Preserves case in each replacement if `case-replace' and `case-fold-search' 114Preserves case in each replacement if `case-replace' and `case-fold-search'
112are non-nil and REGEXP has no uppercase letters. 115are non-nil and REGEXP has no uppercase letters.
116
113Third arg DELIMITED (prefix arg if interactive), if non-nil, means replace 117Third arg DELIMITED (prefix arg if interactive), if non-nil, means replace
114only matches surrounded by word boundaries. 118only matches surrounded by word boundaries.
119Fourth and fifth arg START and END specify the region to operate on.
120
115In TO-STRING, `\\&' stands for whatever matched the whole of REGEXP, 121In TO-STRING, `\\&' stands for whatever matched the whole of REGEXP,
116and `\\=\\N' (where N is a digit) stands for 122and `\\=\\N' (where N is a digit) stands for
117 whatever what matched the Nth `\\(...\\)' in REGEXP." 123 whatever what matched the Nth `\\(...\\)' in REGEXP."
118 (interactive (query-replace-read-args "Query replace regexp" t)) 124 (interactive (query-replace-read-args "Query replace regexp" t))
119 (perform-replace regexp to-string t t delimited)) 125 (perform-replace regexp to-string start end t t delimited))
120(define-key esc-map [?\C-%] 'query-replace-regexp) 126(define-key esc-map [?\C-%] 'query-replace-regexp)
121 127
122(defun query-replace-regexp-eval (regexp to-expr &optional delimited) 128(defun query-replace-regexp-eval (regexp to-expr &optional delimited start end)
123 "Replace some things after point matching REGEXP with the result of TO-EXPR. 129 "Replace some things after point matching REGEXP with the result of TO-EXPR.
124As each match is found, the user must type a character saying 130As each match is found, the user must type a character saying
125what to do with it. For directions, type \\[help-command] at that time. 131what to do with it. For directions, type \\[help-command] at that time.
@@ -143,10 +149,15 @@ minibuffer.
143 149
144Preserves case in each replacement if `case-replace' and `case-fold-search' 150Preserves case in each replacement if `case-replace' and `case-fold-search'
145are non-nil and REGEXP has no uppercase letters. 151are non-nil and REGEXP has no uppercase letters.
152
146Third arg DELIMITED (prefix arg if interactive), if non-nil, means replace 153Third arg DELIMITED (prefix arg if interactive), if non-nil, means replace
147only matches surrounded by word boundaries." 154only matches surrounded by word boundaries.
155Fourth and fifth arg START and END specify the region to operate on."
148 (interactive 156 (interactive
149 (let (from to) 157 (let (from to start end)
158 (when (and transient-mark-mode mark-active)
159 (setq start (region-beginning)
160 end (region-end)))
150 (if query-replace-interactive 161 (if query-replace-interactive
151 (setq from (car regexp-search-ring)) 162 (setq from (car regexp-search-ring))
152 (setq from (read-from-minibuffer "Query replace regexp: " 163 (setq from (read-from-minibuffer "Query replace regexp: "
@@ -159,11 +170,11 @@ only matches surrounded by word boundaries."
159 ;; We make TO a list because replace-match-string-symbols requires one, 170 ;; We make TO a list because replace-match-string-symbols requires one,
160 ;; and the user might enter a single token. 171 ;; and the user might enter a single token.
161 (replace-match-string-symbols to) 172 (replace-match-string-symbols to)
162 (list from (car to) current-prefix-arg))) 173 (list from (car to) start end current-prefix-arg)))
163 (perform-replace regexp (cons 'replace-eval-replacement to-expr) 174 (perform-replace regexp (cons 'replace-eval-replacement to-expr)
164 t t delimited)) 175 start end t t delimited))
165 176
166(defun map-query-replace-regexp (regexp to-strings &optional delimited) 177(defun map-query-replace-regexp (regexp to-strings &optional n start end)
167 "Replace some matches for REGEXP with various strings, in rotation. 178 "Replace some matches for REGEXP with various strings, in rotation.
168The second argument TO-STRINGS contains the replacement strings, separated 179The second argument TO-STRINGS contains the replacement strings, separated
169by spaces. This command works like `query-replace-regexp' except 180by spaces. This command works like `query-replace-regexp' except
@@ -179,9 +190,13 @@ If `query-replace-interactive' is non-nil, the last incremental search
179regexp is used as REGEXP--you don't have to specify it with the minibuffer. 190regexp is used as REGEXP--you don't have to specify it with the minibuffer.
180 191
181A prefix argument N says to use each replacement string N times 192A prefix argument N says to use each replacement string N times
182before rotating to the next." 193before rotating to the next.
194Fourth and fifth arg START and END specify the region to operate on."
183 (interactive 195 (interactive
184 (let (from to) 196 (let (from to start end)
197 (when (and transient-mark-mode mark-active)
198 (setq start (region-beginning)
199 end (region-end)))
185 (setq from (if query-replace-interactive 200 (setq from (if query-replace-interactive
186 (car regexp-search-ring) 201 (car regexp-search-ring)
187 (read-from-minibuffer "Map query replace (regexp): " 202 (read-from-minibuffer "Map query replace (regexp): "
@@ -192,7 +207,7 @@ before rotating to the next."
192 from) 207 from)
193 nil nil nil 208 nil nil nil
194 'query-replace-history from t)) 209 'query-replace-history from t))
195 (list from to current-prefix-arg))) 210 (list from to start end current-prefix-arg)))
196 (let (replacements) 211 (let (replacements)
197 (if (listp to-strings) 212 (if (listp to-strings)
198 (setq replacements to-strings) 213 (setq replacements to-strings)
@@ -206,9 +221,9 @@ before rotating to the next."
206 (1+ (string-match " " to-strings)))) 221 (1+ (string-match " " to-strings))))
207 (setq replacements (append replacements (list to-strings)) 222 (setq replacements (append replacements (list to-strings))
208 to-strings "")))) 223 to-strings ""))))
209 (perform-replace regexp replacements t t nil delimited))) 224 (perform-replace regexp replacements start end t t nil n)))
210 225
211(defun replace-string (from-string to-string &optional delimited) 226(defun replace-string (from-string to-string &optional delimited start end)
212 "Replace occurrences of FROM-STRING with TO-STRING. 227 "Replace occurrences of FROM-STRING with TO-STRING.
213Preserve case in each match if `case-replace' and `case-fold-search' 228Preserve case in each match if `case-replace' and `case-fold-search'
214are non-nil and FROM-STRING has no uppercase letters. 229are non-nil and FROM-STRING has no uppercase letters.
@@ -220,6 +235,7 @@ of the region. Otherwise, operate from point to the end of the buffer.
220 235
221Third arg DELIMITED (prefix arg if interactive), if non-nil, means replace 236Third arg DELIMITED (prefix arg if interactive), if non-nil, means replace
222only matches surrounded by word boundaries. 237only matches surrounded by word boundaries.
238Fourth and fifth arg START and END specify the region to operate on.
223 239
224If `query-replace-interactive' is non-nil, the last incremental search 240If `query-replace-interactive' is non-nil, the last incremental search
225string is used as FROM-STRING--you don't have to specify it with the 241string is used as FROM-STRING--you don't have to specify it with the
@@ -233,21 +249,24 @@ which will run faster and will not set the mark or print anything.
233\(You may need a more complex loop if FROM-STRING can match the null string 249\(You may need a more complex loop if FROM-STRING can match the null string
234and TO-STRING is also null.)" 250and TO-STRING is also null.)"
235 (interactive (query-replace-read-args "Replace string" nil)) 251 (interactive (query-replace-read-args "Replace string" nil))
236 (perform-replace from-string to-string nil nil delimited)) 252 (perform-replace from-string to-string start end nil nil delimited))
237 253
238(defun replace-regexp (regexp to-string &optional delimited) 254(defun replace-regexp (regexp to-string &optional delimited start end)
239 "Replace things after point matching REGEXP with TO-STRING. 255 "Replace things after point matching REGEXP with TO-STRING.
240Preserve case in each match if `case-replace' and `case-fold-search' 256Preserve case in each match if `case-replace' and `case-fold-search'
241are non-nil and REGEXP has no uppercase letters. 257are non-nil and REGEXP has no uppercase letters.
258
259In Transient Mark mode, if the mark is active, operate on the contents
260of the region. Otherwise, operate from point to the end of the buffer.
261
242Third arg DELIMITED (prefix arg if interactive), if non-nil, means replace 262Third arg DELIMITED (prefix arg if interactive), if non-nil, means replace
243only matches surrounded by word boundaries. 263only matches surrounded by word boundaries.
264Fourth and fifth arg START and END specify the region to operate on.
265
244In TO-STRING, `\\&' stands for whatever matched the whole of REGEXP, 266In TO-STRING, `\\&' stands for whatever matched the whole of REGEXP,
245and `\\=\\N' (where N is a digit) stands for 267and `\\=\\N' (where N is a digit) stands for
246 whatever what matched the Nth `\\(...\\)' in REGEXP. 268 whatever what matched the Nth `\\(...\\)' in REGEXP.
247 269
248In Transient Mark mode, if the mark is active, operate on the contents
249of the region. Otherwise, operate from point to the end of the buffer.
250
251If `query-replace-interactive' is non-nil, the last incremental search 270If `query-replace-interactive' is non-nil, the last incremental search
252regexp is used as REGEXP--you don't have to specify it with the minibuffer. 271regexp is used as REGEXP--you don't have to specify it with the minibuffer.
253 272
@@ -257,7 +276,7 @@ What you probably want is a loop like this:
257 (replace-match TO-STRING nil nil)) 276 (replace-match TO-STRING nil nil))
258which will run faster and will not set the mark or print anything." 277which will run faster and will not set the mark or print anything."
259 (interactive (query-replace-read-args "Replace regexp" t)) 278 (interactive (query-replace-read-args "Replace regexp" t))
260 (perform-replace regexp to-string nil t delimited)) 279 (perform-replace regexp to-string start end nil t delimited))
261 280
262(defvar regexp-history nil 281(defvar regexp-history nil
263 "History list for some commands that read regular expressions.") 282 "History list for some commands that read regular expressions.")
@@ -781,7 +800,7 @@ The valid answers include `act', `skip', `act-and-show',
781 (aset data 2 (if (consp next) next (aref data 3)))))) 800 (aset data 2 (if (consp next) next (aref data 3))))))
782 (car (aref data 2))) 801 (car (aref data 2)))
783 802
784(defun perform-replace (from-string replacements 803(defun perform-replace (from-string replacements start end
785 query-flag regexp-flag delimited-flag 804 query-flag regexp-flag delimited-flag
786 &optional repeat-count map) 805 &optional repeat-count map)
787 "Subroutine of `query-replace'. Its complexity handles interactive queries. 806 "Subroutine of `query-replace'. Its complexity handles interactive queries.
@@ -822,11 +841,10 @@ which will run faster and probably do exactly what you want."
822 "Query replacing %s with %s: (\\<query-replace-map>\\[help] for help) ")))) 841 "Query replacing %s with %s: (\\<query-replace-map>\\[help] for help) "))))
823 842
824 ;; If region is active, in Transient Mark mode, operate on region. 843 ;; If region is active, in Transient Mark mode, operate on region.
825 (if (and transient-mark-mode mark-active) 844 (when start
826 (progn 845 (setq limit (copy-marker (max start end)))
827 (setq limit (copy-marker (region-end))) 846 (goto-char (min start end))
828 (goto-char (region-beginning)) 847 (deactivate-mark))
829 (deactivate-mark)))
830 848
831 ;; REPLACEMENTS is either a string, a list of strings, or a cons cell 849 ;; REPLACEMENTS is either a string, a list of strings, or a cons cell
832 ;; containing a function and its first argument. The function is 850 ;; containing a function and its first argument. The function is