Mercurial > emacs
comparison lisp/replace.el @ 10157:7953cde9d951
(perform-replace): Report number of replacements when done.
(query-replace, query-replace-regexp, replace-string)
(map-query-replace-regexp. replace-regexp): No message here.
author | Richard M. Stallman <rms@gnu.org> |
---|---|
date | Wed, 14 Dec 1994 03:44:51 +0000 |
parents | 689adf47dee3 |
children | a44beb55d3e1 |
comparison
equal
deleted
inserted
replaced
10156:baf3b68f7e56 | 10157:7953cde9d951 |
---|---|
63 Third arg DELIMITED (prefix arg if interactive), if non-nil, means replace | 63 Third arg DELIMITED (prefix arg if interactive), if non-nil, means replace |
64 only matches surrounded by word boundaries. | 64 only matches surrounded by word boundaries. |
65 | 65 |
66 To customize possible responses, change the \"bindings\" in `query-replace-map'." | 66 To customize possible responses, change the \"bindings\" in `query-replace-map'." |
67 (interactive (query-replace-read-args "Query replace" nil)) | 67 (interactive (query-replace-read-args "Query replace" nil)) |
68 (perform-replace from-string to-string t nil arg) | 68 (perform-replace from-string to-string t nil arg)) |
69 (or unread-command-events (message "Done"))) | |
70 (define-key esc-map "%" 'query-replace) | 69 (define-key esc-map "%" 'query-replace) |
71 | 70 |
72 (defun query-replace-regexp (regexp to-string &optional arg) | 71 (defun query-replace-regexp (regexp to-string &optional arg) |
73 "Replace some things after point matching REGEXP with TO-STRING. | 72 "Replace some things after point matching REGEXP with TO-STRING. |
74 As each match is found, the user must type a character saying | 73 As each match is found, the user must type a character saying |
84 only matches surrounded by word boundaries. | 83 only matches surrounded by word boundaries. |
85 In TO-STRING, `\\&' stands for whatever matched the whole of REGEXP, | 84 In TO-STRING, `\\&' stands for whatever matched the whole of REGEXP, |
86 and `\\=\\N' (where N is a digit) stands for | 85 and `\\=\\N' (where N is a digit) stands for |
87 whatever what matched the Nth `\\(...\\)' in REGEXP." | 86 whatever what matched the Nth `\\(...\\)' in REGEXP." |
88 (interactive (query-replace-read-args "Query replace regexp" t)) | 87 (interactive (query-replace-read-args "Query replace regexp" t)) |
89 (perform-replace regexp to-string t t arg) | 88 (perform-replace regexp to-string t t arg)) |
90 (or unread-command-events (message "Done"))) | |
91 | 89 |
92 (defun map-query-replace-regexp (regexp to-strings &optional arg) | 90 (defun map-query-replace-regexp (regexp to-strings &optional arg) |
93 "Replace some matches for REGEXP with various strings, in rotation. | 91 "Replace some matches for REGEXP with various strings, in rotation. |
94 The second argument TO-STRINGS contains the replacement strings, separated | 92 The second argument TO-STRINGS contains the replacement strings, separated |
95 by spaces. This command works like `query-replace-regexp' except | 93 by spaces. This command works like `query-replace-regexp' except |
127 (string-match " " to-strings)))) | 125 (string-match " " to-strings)))) |
128 to-strings (substring to-strings | 126 to-strings (substring to-strings |
129 (1+ (string-match " " to-strings)))) | 127 (1+ (string-match " " to-strings)))) |
130 (setq replacements (append replacements (list to-strings)) | 128 (setq replacements (append replacements (list to-strings)) |
131 to-strings "")))) | 129 to-strings "")))) |
132 (perform-replace regexp replacements t t nil arg)) | 130 (perform-replace regexp replacements t t nil arg))) |
133 (or unread-command-events (message "Done"))) | |
134 | 131 |
135 (defun replace-string (from-string to-string &optional delimited) | 132 (defun replace-string (from-string to-string &optional delimited) |
136 "Replace occurrences of FROM-STRING with TO-STRING. | 133 "Replace occurrences of FROM-STRING with TO-STRING. |
137 Preserve case in each match if `case-replace' and `case-fold-search' | 134 Preserve case in each match if `case-replace' and `case-fold-search' |
138 are non-nil and FROM-STRING has no uppercase letters. | 135 are non-nil and FROM-STRING has no uppercase letters. |
150 What you probably want is a loop like this: | 147 What you probably want is a loop like this: |
151 (while (search-forward FROM-STRING nil t) | 148 (while (search-forward FROM-STRING nil t) |
152 (replace-match TO-STRING nil t)) | 149 (replace-match TO-STRING nil t)) |
153 which will run faster and will not set the mark or print anything." | 150 which will run faster and will not set the mark or print anything." |
154 (interactive (query-replace-read-args "Replace string" nil)) | 151 (interactive (query-replace-read-args "Replace string" nil)) |
155 (perform-replace from-string to-string nil nil delimited) | 152 (perform-replace from-string to-string nil nil delimited)) |
156 (or unread-command-events (message "Done"))) | |
157 | 153 |
158 (defun replace-regexp (regexp to-string &optional delimited) | 154 (defun replace-regexp (regexp to-string &optional delimited) |
159 "Replace things after point matching REGEXP with TO-STRING. | 155 "Replace things after point matching REGEXP with TO-STRING. |
160 Preserve case in each match if `case-replace' and `case-fold-search' | 156 Preserve case in each match if `case-replace' and `case-fold-search' |
161 are non-nil and REGEXP has no uppercase letters. | 157 are non-nil and REGEXP has no uppercase letters. |
172 What you probably want is a loop like this: | 168 What you probably want is a loop like this: |
173 (while (re-search-forward REGEXP nil t) | 169 (while (re-search-forward REGEXP nil t) |
174 (replace-match TO-STRING nil nil)) | 170 (replace-match TO-STRING nil nil)) |
175 which will run faster and will not set the mark or print anything." | 171 which will run faster and will not set the mark or print anything." |
176 (interactive (query-replace-read-args "Replace regexp" t)) | 172 (interactive (query-replace-read-args "Replace regexp" t)) |
177 (perform-replace regexp to-string nil t delimited) | 173 (perform-replace regexp to-string nil t delimited)) |
178 (or unread-command-events (message "Done"))) | |
179 | 174 |
180 (defvar regexp-history nil | 175 (defvar regexp-history nil |
181 "History list for some commands that read regular expressions.") | 176 "History list for some commands that read regular expressions.") |
182 | 177 |
183 (defalias 'delete-non-matching-lines 'keep-lines) | 178 (defalias 'delete-non-matching-lines 'keep-lines) |
663 (match-data)))) | 658 (match-data)))) |
664 stack)) | 659 stack)) |
665 (if replaced (setq replace-count (1+ replace-count))))) | 660 (if replaced (setq replace-count (1+ replace-count))))) |
666 (setq lastrepl (point))) | 661 (setq lastrepl (point))) |
667 (replace-dehighlight)) | 662 (replace-dehighlight)) |
668 (and keep-going stack))) | 663 (or unread-command-events |
664 (message "Replaced %d occurrence%s" | |
665 replace-count | |
666 (if (= replace-count 1) "" "s"))) | |
667 (and keep-going stack))) | |
669 | 668 |
670 (defvar query-replace-highlight nil | 669 (defvar query-replace-highlight nil |
671 "*Non-nil means to highlight words during query replacement.") | 670 "*Non-nil means to highlight words during query replacement.") |
672 | 671 |
673 (defvar replace-overlay nil) | 672 (defvar replace-overlay nil) |