diff options
| author | Gerd Moellmann | 2000-04-25 19:19:31 +0000 |
|---|---|---|
| committer | Gerd Moellmann | 2000-04-25 19:19:31 +0000 |
| commit | 47d722546b11876552fc9da0d89a5265b9b45b9a (patch) | |
| tree | 4668831f0595b1778f1f682b8d5b4547e36fd22e | |
| parent | 16bf9fa9d931390934a71792bc567c3520bd1aec (diff) | |
| download | emacs-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.el | 74 |
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. |
| 74 | As each match is found, the user must type a character saying | 76 | As each match is found, the user must type a character saying |
| 75 | what to do with it. For directions, type \\[help-command] at that time. | 77 | what 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 | ||
| 90 | Third arg DELIMITED (prefix arg if interactive), if non-nil, means replace | 92 | Third arg DELIMITED (prefix arg if interactive), if non-nil, means replace |
| 91 | only matches surrounded by word boundaries. | 93 | only matches surrounded by word boundaries. |
| 94 | Fourth and fifth arg START and END specify the region to operate on. | ||
| 92 | 95 | ||
| 93 | To customize possible responses, change the \"bindings\" in `query-replace-map'." | 96 | To 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. |
| 101 | As each match is found, the user must type a character saying | 104 | As each match is found, the user must type a character saying |
| 102 | what to do with it. For directions, type \\[help-command] at that time. | 105 | what to do with it. For directions, type \\[help-command] at that time. |
| @@ -110,16 +113,19 @@ minibuffer. | |||
| 110 | 113 | ||
| 111 | Preserves case in each replacement if `case-replace' and `case-fold-search' | 114 | Preserves case in each replacement if `case-replace' and `case-fold-search' |
| 112 | are non-nil and REGEXP has no uppercase letters. | 115 | are non-nil and REGEXP has no uppercase letters. |
| 116 | |||
| 113 | Third arg DELIMITED (prefix arg if interactive), if non-nil, means replace | 117 | Third arg DELIMITED (prefix arg if interactive), if non-nil, means replace |
| 114 | only matches surrounded by word boundaries. | 118 | only matches surrounded by word boundaries. |
| 119 | Fourth and fifth arg START and END specify the region to operate on. | ||
| 120 | |||
| 115 | In TO-STRING, `\\&' stands for whatever matched the whole of REGEXP, | 121 | In TO-STRING, `\\&' stands for whatever matched the whole of REGEXP, |
| 116 | and `\\=\\N' (where N is a digit) stands for | 122 | and `\\=\\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. |
| 124 | As each match is found, the user must type a character saying | 130 | As each match is found, the user must type a character saying |
| 125 | what to do with it. For directions, type \\[help-command] at that time. | 131 | what to do with it. For directions, type \\[help-command] at that time. |
| @@ -143,10 +149,15 @@ minibuffer. | |||
| 143 | 149 | ||
| 144 | Preserves case in each replacement if `case-replace' and `case-fold-search' | 150 | Preserves case in each replacement if `case-replace' and `case-fold-search' |
| 145 | are non-nil and REGEXP has no uppercase letters. | 151 | are non-nil and REGEXP has no uppercase letters. |
| 152 | |||
| 146 | Third arg DELIMITED (prefix arg if interactive), if non-nil, means replace | 153 | Third arg DELIMITED (prefix arg if interactive), if non-nil, means replace |
| 147 | only matches surrounded by word boundaries." | 154 | only matches surrounded by word boundaries. |
| 155 | Fourth 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. |
| 168 | The second argument TO-STRINGS contains the replacement strings, separated | 179 | The second argument TO-STRINGS contains the replacement strings, separated |
| 169 | by spaces. This command works like `query-replace-regexp' except | 180 | by spaces. This command works like `query-replace-regexp' except |
| @@ -179,9 +190,13 @@ If `query-replace-interactive' is non-nil, the last incremental search | |||
| 179 | regexp is used as REGEXP--you don't have to specify it with the minibuffer. | 190 | regexp is used as REGEXP--you don't have to specify it with the minibuffer. |
| 180 | 191 | ||
| 181 | A prefix argument N says to use each replacement string N times | 192 | A prefix argument N says to use each replacement string N times |
| 182 | before rotating to the next." | 193 | before rotating to the next. |
| 194 | Fourth 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. |
| 213 | Preserve case in each match if `case-replace' and `case-fold-search' | 228 | Preserve case in each match if `case-replace' and `case-fold-search' |
| 214 | are non-nil and FROM-STRING has no uppercase letters. | 229 | are 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 | ||
| 221 | Third arg DELIMITED (prefix arg if interactive), if non-nil, means replace | 236 | Third arg DELIMITED (prefix arg if interactive), if non-nil, means replace |
| 222 | only matches surrounded by word boundaries. | 237 | only matches surrounded by word boundaries. |
| 238 | Fourth and fifth arg START and END specify the region to operate on. | ||
| 223 | 239 | ||
| 224 | If `query-replace-interactive' is non-nil, the last incremental search | 240 | If `query-replace-interactive' is non-nil, the last incremental search |
| 225 | string is used as FROM-STRING--you don't have to specify it with the | 241 | string 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 |
| 234 | and TO-STRING is also null.)" | 250 | and 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. |
| 240 | Preserve case in each match if `case-replace' and `case-fold-search' | 256 | Preserve case in each match if `case-replace' and `case-fold-search' |
| 241 | are non-nil and REGEXP has no uppercase letters. | 257 | are non-nil and REGEXP has no uppercase letters. |
| 258 | |||
| 259 | In Transient Mark mode, if the mark is active, operate on the contents | ||
| 260 | of the region. Otherwise, operate from point to the end of the buffer. | ||
| 261 | |||
| 242 | Third arg DELIMITED (prefix arg if interactive), if non-nil, means replace | 262 | Third arg DELIMITED (prefix arg if interactive), if non-nil, means replace |
| 243 | only matches surrounded by word boundaries. | 263 | only matches surrounded by word boundaries. |
| 264 | Fourth and fifth arg START and END specify the region to operate on. | ||
| 265 | |||
| 244 | In TO-STRING, `\\&' stands for whatever matched the whole of REGEXP, | 266 | In TO-STRING, `\\&' stands for whatever matched the whole of REGEXP, |
| 245 | and `\\=\\N' (where N is a digit) stands for | 267 | and `\\=\\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 | ||
| 248 | In Transient Mark mode, if the mark is active, operate on the contents | ||
| 249 | of the region. Otherwise, operate from point to the end of the buffer. | ||
| 250 | |||
| 251 | If `query-replace-interactive' is non-nil, the last incremental search | 270 | If `query-replace-interactive' is non-nil, the last incremental search |
| 252 | regexp is used as REGEXP--you don't have to specify it with the minibuffer. | 271 | regexp 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)) |
| 258 | which will run faster and will not set the mark or print anything." | 277 | which 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 |