Commit | Line | Data |
---|---|---|
60370d40 | 1 | ;;; replace.el --- replace commands for Emacs |
c88ab9ce | 2 | |
0d30b337 | 3 | ;; Copyright (C) 1985, 1986, 1987, 1992, 1994, 1996, 1997, 2000, 2001, |
1e4bd40d GM |
4 | ;; 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 |
5 | ;; Free Software Foundation, Inc. | |
3a801d0c | 6 | |
30764597 PJ |
7 | ;; Maintainer: FSF |
8 | ||
698e1804 RS |
9 | ;; This file is part of GNU Emacs. |
10 | ||
eb3fa2cf | 11 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
698e1804 | 12 | ;; it under the terms of the GNU General Public License as published by |
eb3fa2cf GM |
13 | ;; the Free Software Foundation, either version 3 of the License, or |
14 | ;; (at your option) any later version. | |
698e1804 RS |
15 | |
16 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
17 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 | ;; GNU General Public License for more details. | |
20 | ||
21 | ;; You should have received a copy of the GNU General Public License | |
eb3fa2cf | 22 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
698e1804 | 23 | |
d9ecc911 ER |
24 | ;;; Commentary: |
25 | ||
26 | ;; This package supplies the string and regular-expression replace functions | |
27 | ;; documented in the Emacs user's manual. | |
28 | ||
4f4b8eff | 29 | ;;; Code: |
698e1804 | 30 | |
9d325ebf | 31 | (defcustom case-replace t |
9201cc28 | 32 | "Non-nil means `query-replace' should preserve case in replacements." |
9d325ebf RS |
33 | :type 'boolean |
34 | :group 'matching) | |
77176e73 | 35 | |
770970cb RS |
36 | (defvar query-replace-history nil) |
37 | ||
6b59b130 CY |
38 | (defvar query-replace-defaults nil |
39 | "Default values of FROM-STRING and TO-STRING for `query-replace'. | |
40 | This is a cons cell (FROM-STRING . TO-STRING), or nil if there is | |
41 | no default value.") | |
42 | ||
5291cbca | 43 | (defvar query-replace-interactive nil |
151270f3 | 44 | "Non-nil means `query-replace' uses the last search string. |
5291cbca | 45 | That becomes the \"string to replace\".") |
151270f3 | 46 | |
bdb1c08f | 47 | (defcustom query-replace-from-history-variable 'query-replace-history |
f54701d1 | 48 | "History list to use for the FROM argument of `query-replace' commands. |
bdb1c08f RS |
49 | The value of this variable should be a symbol; that symbol |
50 | is used as a variable to hold a history list for the strings | |
51 | or patterns to be replaced." | |
52 | :group 'matching | |
cd32a7ba DN |
53 | :type 'symbol |
54 | :version "20.3") | |
bdb1c08f RS |
55 | |
56 | (defcustom query-replace-to-history-variable 'query-replace-history | |
f54701d1 | 57 | "History list to use for the TO argument of `query-replace' commands. |
bdb1c08f RS |
58 | The value of this variable should be a symbol; that symbol |
59 | is used as a variable to hold a history list for replacement | |
60 | strings or patterns." | |
61 | :group 'matching | |
cd32a7ba DN |
62 | :type 'symbol |
63 | :version "20.3") | |
bdb1c08f | 64 | |
1c4fe319 | 65 | (defcustom query-replace-skip-read-only nil |
9201cc28 | 66 | "Non-nil means `query-replace' and friends ignore read-only matches." |
1c4fe319 RS |
67 | :type 'boolean |
68 | :group 'matching | |
bf247b6e | 69 | :version "22.1") |
1c4fe319 | 70 | |
7abe68aa | 71 | (defcustom query-replace-show-replacement t |
9201cc28 | 72 | "Non-nil means to show what actual replacement text will be." |
7abe68aa JL |
73 | :type 'boolean |
74 | :group 'matching | |
75 | :version "23.1") | |
76 | ||
afd33362 | 77 | (defcustom query-replace-highlight t |
9201cc28 | 78 | "Non-nil means to highlight matches during query replacement." |
afd33362 JL |
79 | :type 'boolean |
80 | :group 'matching) | |
81 | ||
82 | (defcustom query-replace-lazy-highlight t | |
9201cc28 | 83 | "Controls the lazy-highlighting during query replacements. |
afd33362 JL |
84 | When non-nil, all text in the buffer matching the current match |
85 | is highlighted lazily using isearch lazy highlighting (see | |
86 | `lazy-highlight-initial-delay' and `lazy-highlight-interval')." | |
87 | :type 'boolean | |
88 | :group 'lazy-highlight | |
89 | :group 'matching | |
bf247b6e | 90 | :version "22.1") |
afd33362 JL |
91 | |
92 | (defface query-replace | |
93 | '((t (:inherit isearch))) | |
94 | "Face for highlighting query replacement matches." | |
95 | :group 'matching | |
bf247b6e | 96 | :version "22.1") |
afd33362 | 97 | |
6f1df6d9 SM |
98 | (defun query-replace-descr (string) |
99 | (mapconcat 'isearch-text-char-description string "")) | |
100 | ||
90c9fc2a | 101 | (defun query-replace-read-from (prompt regexp-flag) |
6f1df6d9 SM |
102 | "Query and return the `from' argument of a query-replace operation. |
103 | The return value can also be a pair (FROM . TO) indicating that the user | |
104 | wants to replace FROM with TO." | |
1606466a SM |
105 | (if query-replace-interactive |
106 | (car (if regexp-flag regexp-search-ring search-ring)) | |
2bf2b947 JL |
107 | (let* ((history-add-new-input nil) |
108 | (from | |
1606466a SM |
109 | ;; The save-excursion here is in case the user marks and copies |
110 | ;; a region in order to specify the minibuffer input. | |
111 | ;; That should not clobber the region for the query-replace itself. | |
112 | (save-excursion | |
113 | (read-from-minibuffer | |
6b59b130 | 114 | (if query-replace-defaults |
90c9fc2a | 115 | (format "%s (default %s -> %s): " prompt |
6b59b130 CY |
116 | (query-replace-descr (car query-replace-defaults)) |
117 | (query-replace-descr (cdr query-replace-defaults))) | |
90c9fc2a | 118 | (format "%s: " prompt)) |
1606466a SM |
119 | nil nil nil |
120 | query-replace-from-history-variable | |
311f8bac | 121 | nil t)))) |
6b59b130 | 122 | (if (and (zerop (length from)) query-replace-defaults) |
5dae4b11 CY |
123 | (cons (car query-replace-defaults) |
124 | (query-replace-compile-replacement | |
125 | (cdr query-replace-defaults) regexp-flag)) | |
2bf2b947 | 126 | (add-to-history query-replace-from-history-variable from nil t) |
6f1df6d9 SM |
127 | ;; Warn if user types \n or \t, but don't reject the input. |
128 | (and regexp-flag | |
129 | (string-match "\\(\\`\\|[^\\]\\)\\(\\\\\\\\\\)*\\(\\\\[nt]\\)" from) | |
130 | (let ((match (match-string 3 from))) | |
131 | (cond | |
132 | ((string= match "\\n") | |
133 | (message "Note: `\\n' here doesn't match a newline; to do that, type C-q C-j instead")) | |
134 | ((string= match "\\t") | |
135 | (message "Note: `\\t' here doesn't match a tab; to do that, just type TAB"))) | |
136 | (sit-for 2))) | |
137 | from)))) | |
1606466a | 138 | |
78629844 DK |
139 | (defun query-replace-compile-replacement (to regexp-flag) |
140 | "Maybe convert a regexp replacement TO to Lisp. | |
141 | Returns a list suitable for `perform-replace' if necessary, | |
142 | the original string if not." | |
143 | (if (and regexp-flag | |
144 | (string-match "\\(\\`\\|[^\\]\\)\\(\\\\\\\\\\)*\\\\[,#]" to)) | |
7c1c02ac DK |
145 | (let (pos list char) |
146 | (while | |
147 | (progn | |
148 | (setq pos (match-end 0)) | |
149 | (push (substring to 0 (- pos 2)) list) | |
150 | (setq char (aref to (1- pos)) | |
151 | to (substring to pos)) | |
152 | (cond ((eq char ?\#) | |
153 | (push '(number-to-string replace-count) list)) | |
154 | ((eq char ?\,) | |
155 | (setq pos (read-from-string to)) | |
156 | (push `(replace-quote ,(car pos)) list) | |
9e5d1b63 RS |
157 | (let ((end |
158 | ;; Swallow a space after a symbol | |
159 | ;; if there is a space. | |
160 | (if (and (or (symbolp (car pos)) | |
161 | ;; Swallow a space after 'foo | |
162 | ;; but not after (quote foo). | |
163 | (and (eq (car-safe (car pos)) 'quote) | |
f1f6079c JL |
164 | (not (= ?\( (aref to 0))))) |
165 | (eq (string-match " " to (cdr pos)) | |
166 | (cdr pos))) | |
9e5d1b63 RS |
167 | (1+ (cdr pos)) |
168 | (cdr pos)))) | |
169 | (setq to (substring to end))))) | |
7c1c02ac | 170 | (string-match "\\(\\`\\|[^\\]\\)\\(\\\\\\\\\\)*\\\\[,#]" to))) |
78629844 DK |
171 | (setq to (nreverse (delete "" (cons to list)))) |
172 | (replace-match-string-symbols to) | |
173 | (cons 'replace-eval-replacement | |
174 | (if (cdr to) | |
175 | (cons 'concat to) | |
176 | (car to)))) | |
1606466a SM |
177 | to)) |
178 | ||
78629844 | 179 | |
90c9fc2a | 180 | (defun query-replace-read-to (from prompt regexp-flag) |
78629844 DK |
181 | "Query and return the `to' argument of a query-replace operation." |
182 | (query-replace-compile-replacement | |
183 | (save-excursion | |
2bf2b947 JL |
184 | (let* ((history-add-new-input nil) |
185 | (to (read-from-minibuffer | |
186 | (format "%s %s with: " prompt (query-replace-descr from)) | |
187 | nil nil nil | |
188 | query-replace-to-history-variable from t))) | |
189 | (add-to-history query-replace-to-history-variable to nil t) | |
6b59b130 CY |
190 | (setq query-replace-defaults (cons from to)) |
191 | to)) | |
78629844 DK |
192 | regexp-flag)) |
193 | ||
90c9fc2a | 194 | (defun query-replace-read-args (prompt regexp-flag &optional noerror) |
1606466a SM |
195 | (unless noerror |
196 | (barf-if-buffer-read-only)) | |
90c9fc2a | 197 | (let* ((from (query-replace-read-from prompt regexp-flag)) |
6f1df6d9 | 198 | (to (if (consp from) (prog1 (cdr from) (setq from (car from))) |
90c9fc2a | 199 | (query-replace-read-to from prompt regexp-flag)))) |
10784bac | 200 | (list from to current-prefix-arg))) |
770970cb | 201 | |
47d72254 | 202 | (defun query-replace (from-string to-string &optional delimited start end) |
da44e784 RM |
203 | "Replace some occurrences of FROM-STRING with TO-STRING. |
204 | As each match is found, the user must type a character saying | |
205 | what to do with it. For directions, type \\[help-command] at that time. | |
206 | ||
7ef5c431 KH |
207 | In Transient Mark mode, if the mark is active, operate on the contents |
208 | of the region. Otherwise, operate from point to the end of the buffer. | |
209 | ||
151270f3 RS |
210 | If `query-replace-interactive' is non-nil, the last incremental search |
211 | string is used as FROM-STRING--you don't have to specify it with the | |
212 | minibuffer. | |
213 | ||
446d9629 RS |
214 | Matching is independent of case if `case-fold-search' is non-nil and |
215 | FROM-STRING has no uppercase letters. Replacement transfers the case | |
216 | pattern of the old text to the new text, if `case-replace' and | |
217 | `case-fold-search' are non-nil and FROM-STRING has no uppercase | |
218 | letters. \(Transferring the case pattern means that if the old text | |
219 | matched is all caps, or capitalized, then its replacement is upcased | |
220 | or capitalized.) | |
9b0bf2b6 | 221 | |
118a01c9 | 222 | Third arg DELIMITED (prefix arg if interactive), if non-nil, means replace |
81bdc14d | 223 | only matches surrounded by word boundaries. |
47d72254 | 224 | Fourth and fifth arg START and END specify the region to operate on. |
81bdc14d RS |
225 | |
226 | To customize possible responses, change the \"bindings\" in `query-replace-map'." | |
04ff2dee JL |
227 | (interactive |
228 | (let ((common | |
229 | (query-replace-read-args | |
230 | (concat "Query replace" | |
231 | (if current-prefix-arg " word" "") | |
232 | (if (and transient-mark-mode mark-active) " in region" "")) | |
233 | nil))) | |
234 | (list (nth 0 common) (nth 1 common) (nth 2 common) | |
235 | ;; These are done separately here | |
236 | ;; so that command-history will record these expressions | |
237 | ;; rather than the values they had this time. | |
238 | (if (and transient-mark-mode mark-active) | |
239 | (region-beginning)) | |
240 | (if (and transient-mark-mode mark-active) | |
241 | (region-end))))) | |
99a7559f | 242 | (perform-replace from-string to-string t nil delimited nil nil start end)) |
7ef5c431 | 243 | |
73fa8346 | 244 | (define-key esc-map "%" 'query-replace) |
da44e784 | 245 | |
47d72254 | 246 | (defun query-replace-regexp (regexp to-string &optional delimited start end) |
da44e784 RM |
247 | "Replace some things after point matching REGEXP with TO-STRING. |
248 | As each match is found, the user must type a character saying | |
249 | what to do with it. For directions, type \\[help-command] at that time. | |
250 | ||
7ef5c431 KH |
251 | In Transient Mark mode, if the mark is active, operate on the contents |
252 | of the region. Otherwise, operate from point to the end of the buffer. | |
253 | ||
151270f3 RS |
254 | If `query-replace-interactive' is non-nil, the last incremental search |
255 | regexp is used as REGEXP--you don't have to specify it with the | |
256 | minibuffer. | |
257 | ||
446d9629 RS |
258 | Matching is independent of case if `case-fold-search' is non-nil and |
259 | REGEXP has no uppercase letters. Replacement transfers the case | |
260 | pattern of the old text to the new text, if `case-replace' and | |
261 | `case-fold-search' are non-nil and REGEXP has no uppercase letters. | |
262 | \(Transferring the case pattern means that if the old text matched is | |
263 | all caps, or capitalized, then its replacement is upcased or | |
264 | capitalized.) | |
47d72254 | 265 | |
118a01c9 | 266 | Third arg DELIMITED (prefix arg if interactive), if non-nil, means replace |
da44e784 | 267 | only matches surrounded by word boundaries. |
47d72254 GM |
268 | Fourth and fifth arg START and END specify the region to operate on. |
269 | ||
118a01c9 RS |
270 | In TO-STRING, `\\&' stands for whatever matched the whole of REGEXP, |
271 | and `\\=\\N' (where N is a digit) stands for | |
2f57bf85 | 272 | whatever what matched the Nth `\\(...\\)' in REGEXP. |
7c1c02ac DK |
273 | `\\?' lets you edit the replacement text in the minibuffer |
274 | at the given position for each replacement. | |
275 | ||
ba8d15f9 RS |
276 | In interactive calls, the replacement text can contain `\\,' |
277 | followed by a Lisp expression. Each | |
278 | replacement evaluates that expression to compute the replacement | |
279 | string. Inside of that expression, `\\&' is a string denoting the | |
f1f6079c | 280 | whole match as a string, `\\N' for a partial match, `\\#&' and `\\#N' |
ba8d15f9 RS |
281 | for the whole or a partial match converted to a number with |
282 | `string-to-number', and `\\#' itself for the number of replacements | |
283 | done so far (starting with zero). | |
7c1c02ac | 284 | |
ba8d15f9 RS |
285 | If the replacement expression is a symbol, write a space after it |
286 | to terminate it. One space there, if any, will be discarded. | |
7c1c02ac DK |
287 | |
288 | When using those Lisp features interactively in the replacement | |
289 | text, TO-STRING is actually made a list instead of a string. | |
290 | Use \\[repeat-complex-command] after this command for details." | |
10784bac RS |
291 | (interactive |
292 | (let ((common | |
10ddc30e | 293 | (query-replace-read-args |
04ff2dee JL |
294 | (concat "Query replace" |
295 | (if current-prefix-arg " word" "") | |
296 | " regexp" | |
297 | (if (and transient-mark-mode mark-active) " in region" "")) | |
f79bdb3a | 298 | t))) |
7c1c02ac DK |
299 | (list (nth 0 common) (nth 1 common) (nth 2 common) |
300 | ;; These are done separately here | |
301 | ;; so that command-history will record these expressions | |
302 | ;; rather than the values they had this time. | |
303 | (if (and transient-mark-mode mark-active) | |
304 | (region-beginning)) | |
305 | (if (and transient-mark-mode mark-active) | |
306 | (region-end))))) | |
99a7559f | 307 | (perform-replace regexp to-string t t delimited nil nil start end)) |
2f57bf85 | 308 | |
cbc127de | 309 | (define-key esc-map [?\C-%] 'query-replace-regexp) |
da44e784 | 310 | |
47d72254 | 311 | (defun query-replace-regexp-eval (regexp to-expr &optional delimited start end) |
84482eb3 | 312 | "Replace some things after point matching REGEXP with the result of TO-EXPR. |
fc6a2250 DK |
313 | |
314 | Interactive use of this function is deprecated in favor of the | |
315 | `\\,' feature of `query-replace-regexp'. For non-interactive use, a loop | |
316 | using `search-forward-regexp' and `replace-match' is preferred. | |
317 | ||
84482eb3 RS |
318 | As each match is found, the user must type a character saying |
319 | what to do with it. For directions, type \\[help-command] at that time. | |
320 | ||
321 | TO-EXPR is a Lisp expression evaluated to compute each replacement. It may | |
322 | reference `replace-count' to get the number of replacements already made. | |
323 | If the result of TO-EXPR is not a string, it is converted to one using | |
324 | `prin1-to-string' with the NOESCAPE argument (which see). | |
325 | ||
326 | For convenience, when entering TO-EXPR interactively, you can use `\\&' or | |
653479ad AS |
327 | `\\0' to stand for whatever matched the whole of REGEXP, and `\\N' (where |
328 | N is a digit) to stand for whatever matched the Nth `\\(...\\)' in REGEXP. | |
84482eb3 | 329 | Use `\\#&' or `\\#N' if you want a number instead of a string. |
2f57bf85 | 330 | In interactive use, `\\#' in itself stands for `replace-count'. |
84482eb3 RS |
331 | |
332 | In Transient Mark mode, if the mark is active, operate on the contents | |
333 | of the region. Otherwise, operate from point to the end of the buffer. | |
334 | ||
335 | If `query-replace-interactive' is non-nil, the last incremental search | |
336 | regexp is used as REGEXP--you don't have to specify it with the | |
337 | minibuffer. | |
338 | ||
339 | Preserves case in each replacement if `case-replace' and `case-fold-search' | |
340 | are non-nil and REGEXP has no uppercase letters. | |
47d72254 | 341 | |
84482eb3 | 342 | Third arg DELIMITED (prefix arg if interactive), if non-nil, means replace |
470bbe9b | 343 | only matches that are surrounded by word boundaries. |
47d72254 | 344 | Fourth and fifth arg START and END specify the region to operate on." |
84482eb3 | 345 | (interactive |
cc0aea1a | 346 | (progn |
6f1df6d9 SM |
347 | (barf-if-buffer-read-only) |
348 | (let* ((from | |
349 | ;; Let-bind the history var to disable the "foo -> bar" default. | |
350 | ;; Maybe we shouldn't disable this default, but for now I'll | |
351 | ;; leave it off. --Stef | |
352 | (let ((query-replace-to-history-variable nil)) | |
353 | (query-replace-read-from "Query replace regexp" t))) | |
5291cbca | 354 | (to (list (read-from-minibuffer |
6f1df6d9 SM |
355 | (format "Query replace regexp %s with eval: " |
356 | (query-replace-descr from)) | |
5291cbca | 357 | nil nil t query-replace-to-history-variable from t)))) |
84482eb3 RS |
358 | ;; We make TO a list because replace-match-string-symbols requires one, |
359 | ;; and the user might enter a single token. | |
360 | (replace-match-string-symbols to) | |
10784bac RS |
361 | (list from (car to) current-prefix-arg |
362 | (if (and transient-mark-mode mark-active) | |
363 | (region-beginning)) | |
364 | (if (and transient-mark-mode mark-active) | |
cc0aea1a | 365 | (region-end)))))) |
d2ce3151 | 366 | (perform-replace regexp (cons 'replace-eval-replacement to-expr) |
d83a97ab | 367 | t 'literal delimited nil nil start end)) |
84482eb3 | 368 | |
fc6a2250 DK |
369 | (make-obsolete 'query-replace-regexp-eval |
370 | "for interactive use, use the special `\\,' feature of | |
371 | `query-replace-regexp' instead. Non-interactively, a loop | |
372 | using `search-forward-regexp' and `replace-match' is preferred." "22.1") | |
373 | ||
47d72254 | 374 | (defun map-query-replace-regexp (regexp to-strings &optional n start end) |
da44e784 | 375 | "Replace some matches for REGEXP with various strings, in rotation. |
d8f1d2f3 JB |
376 | The second argument TO-STRINGS contains the replacement strings, separated |
377 | by spaces. This command works like `query-replace-regexp' except that | |
378 | each successive replacement uses the next successive replacement string, | |
da44e784 RM |
379 | wrapping around from the last such string to the first. |
380 | ||
7ef5c431 KH |
381 | In Transient Mark mode, if the mark is active, operate on the contents |
382 | of the region. Otherwise, operate from point to the end of the buffer. | |
383 | ||
da44e784 RM |
384 | Non-interactively, TO-STRINGS may be a list of replacement strings. |
385 | ||
151270f3 RS |
386 | If `query-replace-interactive' is non-nil, the last incremental search |
387 | regexp is used as REGEXP--you don't have to specify it with the minibuffer. | |
388 | ||
da44e784 | 389 | A prefix argument N says to use each replacement string N times |
47d72254 GM |
390 | before rotating to the next. |
391 | Fourth and fifth arg START and END specify the region to operate on." | |
770970cb | 392 | (interactive |
5291cbca | 393 | (let* ((from (if query-replace-interactive |
151270f3 RS |
394 | (car regexp-search-ring) |
395 | (read-from-minibuffer "Map query replace (regexp): " | |
396 | nil nil nil | |
fce31d51 | 397 | 'query-replace-history nil t))) |
5291cbca | 398 | (to (read-from-minibuffer |
770970cb | 399 | (format "Query replace %s with (space-separated strings): " |
6f1df6d9 | 400 | (query-replace-descr from)) |
770970cb | 401 | nil nil nil |
5291cbca | 402 | 'query-replace-history from t))) |
2f2f7e58 RS |
403 | (list from to |
404 | (and current-prefix-arg | |
405 | (prefix-numeric-value current-prefix-arg)) | |
10784bac RS |
406 | (if (and transient-mark-mode mark-active) |
407 | (region-beginning)) | |
408 | (if (and transient-mark-mode mark-active) | |
409 | (region-end))))) | |
da44e784 RM |
410 | (let (replacements) |
411 | (if (listp to-strings) | |
412 | (setq replacements to-strings) | |
413 | (while (/= (length to-strings) 0) | |
414 | (if (string-match " " to-strings) | |
415 | (setq replacements | |
416 | (append replacements | |
417 | (list (substring to-strings 0 | |
418 | (string-match " " to-strings)))) | |
419 | to-strings (substring to-strings | |
420 | (1+ (string-match " " to-strings)))) | |
421 | (setq replacements (append replacements (list to-strings)) | |
422 | to-strings "")))) | |
99a7559f | 423 | (perform-replace regexp replacements t t nil n nil start end))) |
da44e784 | 424 | |
47d72254 | 425 | (defun replace-string (from-string to-string &optional delimited start end) |
da44e784 RM |
426 | "Replace occurrences of FROM-STRING with TO-STRING. |
427 | Preserve case in each match if `case-replace' and `case-fold-search' | |
428 | are non-nil and FROM-STRING has no uppercase letters. | |
9b0bf2b6 RS |
429 | \(Preserving case means that if the string matched is all caps, or capitalized, |
430 | then its replacement is upcased or capitalized.) | |
431 | ||
7ef5c431 KH |
432 | In Transient Mark mode, if the mark is active, operate on the contents |
433 | of the region. Otherwise, operate from point to the end of the buffer. | |
434 | ||
118a01c9 | 435 | Third arg DELIMITED (prefix arg if interactive), if non-nil, means replace |
da44e784 | 436 | only matches surrounded by word boundaries. |
47d72254 | 437 | Fourth and fifth arg START and END specify the region to operate on. |
da44e784 | 438 | |
151270f3 RS |
439 | If `query-replace-interactive' is non-nil, the last incremental search |
440 | string is used as FROM-STRING--you don't have to specify it with the | |
441 | minibuffer. | |
442 | ||
da44e784 RM |
443 | This function is usually the wrong thing to use in a Lisp program. |
444 | What you probably want is a loop like this: | |
118a01c9 RS |
445 | (while (search-forward FROM-STRING nil t) |
446 | (replace-match TO-STRING nil t)) | |
87532fbe RS |
447 | which will run faster and will not set the mark or print anything. |
448 | \(You may need a more complex loop if FROM-STRING can match the null string | |
449 | and TO-STRING is also null.)" | |
10784bac RS |
450 | (interactive |
451 | (let ((common | |
10ddc30e | 452 | (query-replace-read-args |
04ff2dee JL |
453 | (concat "Replace" |
454 | (if current-prefix-arg " word" "") | |
455 | " string" | |
456 | (if (and transient-mark-mode mark-active) " in region" "")) | |
f79bdb3a | 457 | nil))) |
10784bac RS |
458 | (list (nth 0 common) (nth 1 common) (nth 2 common) |
459 | (if (and transient-mark-mode mark-active) | |
460 | (region-beginning)) | |
461 | (if (and transient-mark-mode mark-active) | |
462 | (region-end))))) | |
99a7559f | 463 | (perform-replace from-string to-string nil nil delimited nil nil start end)) |
da44e784 | 464 | |
47d72254 | 465 | (defun replace-regexp (regexp to-string &optional delimited start end) |
da44e784 | 466 | "Replace things after point matching REGEXP with TO-STRING. |
118a01c9 | 467 | Preserve case in each match if `case-replace' and `case-fold-search' |
da44e784 | 468 | are non-nil and REGEXP has no uppercase letters. |
47d72254 GM |
469 | |
470 | In Transient Mark mode, if the mark is active, operate on the contents | |
471 | of the region. Otherwise, operate from point to the end of the buffer. | |
472 | ||
118a01c9 | 473 | Third arg DELIMITED (prefix arg if interactive), if non-nil, means replace |
da44e784 | 474 | only matches surrounded by word boundaries. |
47d72254 GM |
475 | Fourth and fifth arg START and END specify the region to operate on. |
476 | ||
118a01c9 RS |
477 | In TO-STRING, `\\&' stands for whatever matched the whole of REGEXP, |
478 | and `\\=\\N' (where N is a digit) stands for | |
7c1c02ac DK |
479 | whatever what matched the Nth `\\(...\\)' in REGEXP. |
480 | `\\?' lets you edit the replacement text in the minibuffer | |
481 | at the given position for each replacement. | |
482 | ||
483 | In interactive calls, the replacement text may contain `\\,' | |
484 | followed by a Lisp expression used as part of the replacement | |
485 | text. Inside of that expression, `\\&' is a string denoting the | |
107173cf JB |
486 | whole match, `\\N' a partial match, `\\#&' and `\\#N' the respective |
487 | numeric values from `string-to-number', and `\\#' itself for | |
488 | `replace-count', the number of replacements occurred so far. | |
7c1c02ac DK |
489 | |
490 | If your Lisp expression is an identifier and the next letter in | |
491 | the replacement string would be interpreted as part of it, you | |
492 | can wrap it with an expression like `\\,(or \\#)'. Incidentally, | |
493 | for this particular case you may also enter `\\#' in the | |
494 | replacement text directly. | |
495 | ||
496 | When using those Lisp features interactively in the replacement | |
497 | text, TO-STRING is actually made a list instead of a string. | |
498 | Use \\[repeat-complex-command] after this command for details. | |
da44e784 | 499 | |
151270f3 RS |
500 | If `query-replace-interactive' is non-nil, the last incremental search |
501 | regexp is used as REGEXP--you don't have to specify it with the minibuffer. | |
502 | ||
da44e784 RM |
503 | This function is usually the wrong thing to use in a Lisp program. |
504 | What you probably want is a loop like this: | |
505 | (while (re-search-forward REGEXP nil t) | |
118a01c9 | 506 | (replace-match TO-STRING nil nil)) |
da44e784 | 507 | which will run faster and will not set the mark or print anything." |
10784bac RS |
508 | (interactive |
509 | (let ((common | |
10ddc30e | 510 | (query-replace-read-args |
04ff2dee JL |
511 | (concat "Replace" |
512 | (if current-prefix-arg " word" "") | |
513 | " regexp" | |
514 | (if (and transient-mark-mode mark-active) " in region" "")) | |
f79bdb3a | 515 | t))) |
10784bac RS |
516 | (list (nth 0 common) (nth 1 common) (nth 2 common) |
517 | (if (and transient-mark-mode mark-active) | |
518 | (region-beginning)) | |
519 | (if (and transient-mark-mode mark-active) | |
520 | (region-end))))) | |
99a7559f | 521 | (perform-replace regexp to-string nil t delimited nil nil start end)) |
e32eb3e6 | 522 | |
4c53bd2b RS |
523 | \f |
524 | (defvar regexp-history nil | |
fae97ed8 EZ |
525 | "History list for some commands that read regular expressions. |
526 | ||
527 | Maximum length of the history list is determined by the value | |
528 | of `history-length', which see.") | |
da44e784 | 529 | |
10965505 | 530 | (defun read-regexp (prompt &optional default-value) |
96f606c5 | 531 | "Read regexp as a string using the regexp history and some useful defaults. |
714da757 | 532 | Prompt for a regular expression with PROMPT (without a colon and |
10965505 JL |
533 | space) in the minibuffer. The optional argument DEFAULT-VALUE |
534 | provides the value to display in the minibuffer prompt that is | |
535 | returned if the user just types RET. | |
536 | Values available via M-n are the string at point, the last isearch | |
537 | regexp, the last isearch string, and the last replacement regexp." | |
714da757 | 538 | (let* ((defaults |
96f606c5 JL |
539 | (list (regexp-quote |
540 | (or (funcall (or find-tag-default-function | |
541 | (get major-mode 'find-tag-default-function) | |
542 | 'find-tag-default)) | |
543 | "")) | |
544 | (car regexp-search-ring) | |
545 | (regexp-quote (or (car search-ring) "")) | |
546 | (car (symbol-value | |
547 | query-replace-from-history-variable)))) | |
548 | (defaults (delete-dups (delq nil (delete "" defaults)))) | |
549 | ;; Don't add automatically the car of defaults for empty input | |
550 | (history-add-new-input nil) | |
551 | (input | |
552 | (read-from-minibuffer | |
10965505 JL |
553 | (if default-value |
554 | (format "%s (default %s): " prompt | |
555 | (query-replace-descr default-value)) | |
96f606c5 JL |
556 | (format "%s: " prompt)) |
557 | nil nil nil 'regexp-history defaults t))) | |
558 | (if (equal input "") | |
10965505 | 559 | default-value |
96f606c5 JL |
560 | (prog1 input |
561 | (add-to-history 'regexp-history input))))) | |
562 | ||
e32eb3e6 | 563 | |
31e1d920 | 564 | (defalias 'delete-non-matching-lines 'keep-lines) |
e32eb3e6 GM |
565 | (defalias 'delete-matching-lines 'flush-lines) |
566 | (defalias 'count-matches 'how-many) | |
567 | ||
568 | ||
569 | (defun keep-lines-read-args (prompt) | |
570 | "Read arguments for `keep-lines' and friends. | |
571 | Prompt for a regexp with PROMPT. | |
2ced751f | 572 | Value is a list, (REGEXP)." |
c4bf8039 | 573 | (list (read-regexp prompt "") nil nil t)) |
e32eb3e6 | 574 | |
bace7209 | 575 | (defun keep-lines (regexp &optional rstart rend interactive) |
698e1804 RS |
576 | "Delete all lines except those containing matches for REGEXP. |
577 | A match split across lines preserves all the lines it lies in. | |
bace7209 LT |
578 | When called from Lisp (and usually interactively as well, see below) |
579 | applies to all lines starting after point. | |
d2a0ee8b | 580 | |
3be42fcd JL |
581 | If REGEXP contains upper case characters (excluding those preceded by `\\') |
582 | and `search-upper-case' is non-nil, the matching is case-sensitive. | |
e32eb3e6 GM |
583 | |
584 | Second and third arg RSTART and REND specify the region to operate on. | |
bace7209 LT |
585 | This command operates on (the accessible part of) all lines whose |
586 | accessible part is entirely contained in the region determined by RSTART | |
587 | and REND. (A newline ending a line counts as part of that line.) | |
e32eb3e6 | 588 | |
2ced751f | 589 | Interactively, in Transient Mark mode when the mark is active, operate |
bace7209 LT |
590 | on all lines whose accessible part is entirely contained in the region. |
591 | Otherwise, the command applies to all lines starting after point. | |
592 | When calling this function from Lisp, you can pretend that it was | |
593 | called interactively by passing a non-nil INTERACTIVE argument. | |
594 | ||
595 | This function starts looking for the next match from the end of | |
596 | the previous match. Hence, it ignores matches that overlap | |
597 | a previously found match." | |
2ced751f | 598 | |
e32eb3e6 | 599 | (interactive |
98faf1bb RS |
600 | (progn |
601 | (barf-if-buffer-read-only) | |
96f606c5 | 602 | (keep-lines-read-args "Keep lines containing match for regexp"))) |
e32eb3e6 | 603 | (if rstart |
119831da RS |
604 | (progn |
605 | (goto-char (min rstart rend)) | |
bace7209 LT |
606 | (setq rend |
607 | (progn | |
608 | (save-excursion | |
609 | (goto-char (max rstart rend)) | |
610 | (unless (or (bolp) (eobp)) | |
611 | (forward-line 0)) | |
612 | (point-marker))))) | |
613 | (if (and interactive transient-mark-mode mark-active) | |
2ced751f | 614 | (setq rstart (region-beginning) |
bace7209 LT |
615 | rend (progn |
616 | (goto-char (region-end)) | |
617 | (unless (or (bolp) (eobp)) | |
618 | (forward-line 0)) | |
619 | (point-marker))) | |
2ced751f RS |
620 | (setq rstart (point) |
621 | rend (point-max-marker))) | |
622 | (goto-char rstart)) | |
698e1804 RS |
623 | (save-excursion |
624 | (or (bolp) (forward-line 1)) | |
d2a0ee8b | 625 | (let ((start (point)) |
3be42fcd JL |
626 | (case-fold-search |
627 | (if (and case-fold-search search-upper-case) | |
628 | (isearch-no-upper-case-p regexp t) | |
629 | case-fold-search))) | |
e32eb3e6 | 630 | (while (< (point) rend) |
698e1804 | 631 | ;; Start is first char not preserved by previous match. |
e32eb3e6 GM |
632 | (if (not (re-search-forward regexp rend 'move)) |
633 | (delete-region start rend) | |
698e1804 | 634 | (let ((end (save-excursion (goto-char (match-beginning 0)) |
bace7209 | 635 | (forward-line 0) |
698e1804 RS |
636 | (point)))) |
637 | ;; Now end is first char preserved by the new match. | |
638 | (if (< start end) | |
639 | (delete-region start end)))) | |
d99118b0 | 640 | |
e32eb3e6 | 641 | (setq start (save-excursion (forward-line 1) (point))) |
698e1804 | 642 | ;; If the match was empty, avoid matching again at same place. |
e32eb3e6 GM |
643 | (and (< (point) rend) |
644 | (= (match-beginning 0) (match-end 0)) | |
bace7209 LT |
645 | (forward-char 1))))) |
646 | (set-marker rend nil) | |
647 | nil) | |
698e1804 | 648 | |
e32eb3e6 | 649 | |
bace7209 LT |
650 | (defun flush-lines (regexp &optional rstart rend interactive) |
651 | "Delete lines containing matches for REGEXP. | |
652 | When called from Lisp (and usually when called interactively as | |
653 | well, see below), applies to the part of the buffer after point. | |
654 | The line point is in is deleted if and only if it contains a | |
655 | match for regexp starting after point. | |
d2a0ee8b | 656 | |
3be42fcd JL |
657 | If REGEXP contains upper case characters (excluding those preceded by `\\') |
658 | and `search-upper-case' is non-nil, the matching is case-sensitive. | |
e32eb3e6 GM |
659 | |
660 | Second and third arg RSTART and REND specify the region to operate on. | |
bace7209 LT |
661 | Lines partially contained in this region are deleted if and only if |
662 | they contain a match entirely contained in it. | |
e32eb3e6 | 663 | |
2ced751f RS |
664 | Interactively, in Transient Mark mode when the mark is active, operate |
665 | on the contents of the region. Otherwise, operate from point to the | |
bace7209 LT |
666 | end of (the accessible portion of) the buffer. When calling this function |
667 | from Lisp, you can pretend that it was called interactively by passing | |
668 | a non-nil INTERACTIVE argument. | |
669 | ||
670 | If a match is split across lines, all the lines it lies in are deleted. | |
671 | They are deleted _before_ looking for the next match. Hence, a match | |
672 | starting on the same line at which another match ended is ignored." | |
2ced751f | 673 | |
e32eb3e6 | 674 | (interactive |
98faf1bb RS |
675 | (progn |
676 | (barf-if-buffer-read-only) | |
96f606c5 | 677 | (keep-lines-read-args "Flush lines containing match for regexp"))) |
e32eb3e6 | 678 | (if rstart |
119831da RS |
679 | (progn |
680 | (goto-char (min rstart rend)) | |
681 | (setq rend (copy-marker (max rstart rend)))) | |
bace7209 | 682 | (if (and interactive transient-mark-mode mark-active) |
2ced751f RS |
683 | (setq rstart (region-beginning) |
684 | rend (copy-marker (region-end))) | |
685 | (setq rstart (point) | |
686 | rend (point-max-marker))) | |
687 | (goto-char rstart)) | |
3be42fcd JL |
688 | (let ((case-fold-search |
689 | (if (and case-fold-search search-upper-case) | |
690 | (isearch-no-upper-case-p regexp t) | |
691 | case-fold-search))) | |
d2a0ee8b | 692 | (save-excursion |
e32eb3e6 GM |
693 | (while (and (< (point) rend) |
694 | (re-search-forward regexp rend t)) | |
d2a0ee8b | 695 | (delete-region (save-excursion (goto-char (match-beginning 0)) |
bace7209 | 696 | (forward-line 0) |
d2a0ee8b | 697 | (point)) |
bace7209 LT |
698 | (progn (forward-line 1) (point)))))) |
699 | (set-marker rend nil) | |
700 | nil) | |
698e1804 | 701 | |
e32eb3e6 | 702 | |
bace7209 LT |
703 | (defun how-many (regexp &optional rstart rend interactive) |
704 | "Print and return number of matches for REGEXP following point. | |
705 | When called from Lisp and INTERACTIVE is omitted or nil, just return | |
706 | the number, do not print it; if INTERACTIVE is t, the function behaves | |
3f2372cb | 707 | in all respects as if it had been called interactively. |
d2a0ee8b | 708 | |
3be42fcd JL |
709 | If REGEXP contains upper case characters (excluding those preceded by `\\') |
710 | and `search-upper-case' is non-nil, the matching is case-sensitive. | |
e32eb3e6 GM |
711 | |
712 | Second and third arg RSTART and REND specify the region to operate on. | |
713 | ||
2ced751f RS |
714 | Interactively, in Transient Mark mode when the mark is active, operate |
715 | on the contents of the region. Otherwise, operate from point to the | |
bace7209 LT |
716 | end of (the accessible portion of) the buffer. |
717 | ||
718 | This function starts looking for the next match from the end of | |
719 | the previous match. Hence, it ignores matches that overlap | |
720 | a previously found match." | |
2ced751f | 721 | |
e32eb3e6 | 722 | (interactive |
96f606c5 | 723 | (keep-lines-read-args "How many matches for regexp")) |
f601efb0 SM |
724 | (save-excursion |
725 | (if rstart | |
bace7209 LT |
726 | (progn |
727 | (goto-char (min rstart rend)) | |
728 | (setq rend (max rstart rend))) | |
729 | (if (and interactive transient-mark-mode mark-active) | |
2ced751f | 730 | (setq rstart (region-beginning) |
bace7209 | 731 | rend (region-end)) |
2ced751f | 732 | (setq rstart (point) |
bace7209 | 733 | rend (point-max))) |
2ced751f | 734 | (goto-char rstart)) |
f601efb0 SM |
735 | (let ((count 0) |
736 | opoint | |
3be42fcd JL |
737 | (case-fold-search |
738 | (if (and case-fold-search search-upper-case) | |
739 | (isearch-no-upper-case-p regexp t) | |
740 | case-fold-search))) | |
f601efb0 SM |
741 | (while (and (< (point) rend) |
742 | (progn (setq opoint (point)) | |
743 | (re-search-forward regexp rend t))) | |
744 | (if (= opoint (point)) | |
745 | (forward-char 1) | |
746 | (setq count (1+ count)))) | |
bace7209 LT |
747 | (when interactive (message "%d occurrence%s" |
748 | count | |
749 | (if (= count 1) "" "s"))) | |
750 | count))) | |
e32eb3e6 | 751 | |
4c53bd2b | 752 | \f |
f601efb0 SM |
753 | (defvar occur-mode-map |
754 | (let ((map (make-sparse-keymap))) | |
cedbd3f0 | 755 | ;; We use this alternative name, so we can use \\[occur-mode-mouse-goto]. |
f601efb0 SM |
756 | (define-key map [mouse-2] 'occur-mode-mouse-goto) |
757 | (define-key map "\C-c\C-c" 'occur-mode-goto-occurrence) | |
758 | (define-key map "\C-m" 'occur-mode-goto-occurrence) | |
daae70bf | 759 | (define-key map "o" 'occur-mode-goto-occurrence-other-window) |
365486d6 | 760 | (define-key map "\C-o" 'occur-mode-display-occurrence) |
f601efb0 SM |
761 | (define-key map "\M-n" 'occur-next) |
762 | (define-key map "\M-p" 'occur-prev) | |
d99118b0 SS |
763 | (define-key map "r" 'occur-rename-buffer) |
764 | (define-key map "c" 'clone-buffer) | |
f601efb0 | 765 | (define-key map "g" 'revert-buffer) |
d99118b0 SS |
766 | (define-key map "q" 'quit-window) |
767 | (define-key map "z" 'kill-this-buffer) | |
b938735a | 768 | (define-key map "\C-c\C-f" 'next-error-follow-minor-mode) |
85ea5b19 DN |
769 | (define-key map [menu-bar] (make-sparse-keymap)) |
770 | (define-key map [menu-bar occur] | |
771 | (cons "Occur" map)) | |
772 | (define-key map [next-error-follow-minor-mode] | |
773 | (menu-bar-make-mm-toggle next-error-follow-minor-mode | |
774 | "Auto Occurrence Display" | |
775 | "Display another occurrence when moving the cursor")) | |
776 | (define-key map [separator-1] '("--")) | |
9201cc28 | 777 | (define-key map [kill-this-buffer] |
a22f42ac DN |
778 | '(menu-item "Kill occur buffer" kill-this-buffer |
779 | :help "Kill the current *Occur* buffer")) | |
9201cc28 | 780 | (define-key map [quit-window] |
a22f42ac DN |
781 | '(menu-item "Quit occur window" quit-window |
782 | :help "Quit the current *Occur* buffer. Bury it, and maybe delete the selected frame")) | |
9201cc28 | 783 | (define-key map [revert-buffer] |
a22f42ac DN |
784 | '(menu-item "Revert occur buffer" revert-buffer |
785 | :help "Replace the text in the *Occur* buffer with the results of rerunning occur")) | |
9201cc28 | 786 | (define-key map [clone-buffer] |
a22f42ac DN |
787 | '(menu-item "Clone occur buffer" clone-buffer |
788 | :help "Create and return a twin copy of the current *Occur* buffer")) | |
9201cc28 | 789 | (define-key map [occur-rename-buffer] |
a22f42ac DN |
790 | '(menu-item "Rename occur buffer" occur-rename-buffer |
791 | :help "Rename the current *Occur* buffer to *Occur: original-buffer-name*.")) | |
85ea5b19 DN |
792 | (define-key map [separator-2] '("--")) |
793 | (define-key map [occur-mode-goto-occurrence-other-window] | |
a22f42ac DN |
794 | '(menu-item "Go To Occurrence Other Window" occur-mode-goto-occurrence-other-window |
795 | :help "Go to the occurrence the current line describes, in another window")) | |
85ea5b19 | 796 | (define-key map [occur-mode-goto-occurrence] |
a22f42ac DN |
797 | '(menu-item "Go To Occurrence" occur-mode-goto-occurrence |
798 | :help "Go to the occurrence the current line describes")) | |
85ea5b19 | 799 | (define-key map [occur-mode-display-occurrence] |
a22f42ac DN |
800 | '(menu-item "Display Occurrence" occur-mode-display-occurrence |
801 | :help "Display in another window the occurrence the current line describes")) | |
9201cc28 | 802 | (define-key map [occur-next] |
a22f42ac DN |
803 | '(menu-item "Move to next match" occur-next |
804 | :help "Move to the Nth (default 1) next match in an Occur mode buffer")) | |
9201cc28 | 805 | (define-key map [occur-prev] |
a22f42ac DN |
806 | '(menu-item "Move to previous match" occur-prev |
807 | :help "Move to the Nth (default 1) previous match in an Occur mode buffer")) | |
f601efb0 SM |
808 | map) |
809 | "Keymap for `occur-mode'.") | |
698e1804 | 810 | |
46b3d18e RS |
811 | (defvar occur-revert-arguments nil |
812 | "Arguments to pass to `occur-1' to revert an Occur mode buffer. | |
813 | See `occur-revert-function'.") | |
698e1804 | 814 | |
c9ae8cbb JB |
815 | (defcustom occur-mode-hook '(turn-on-font-lock) |
816 | "Hook run when entering Occur mode." | |
817 | :type 'hook | |
818 | :group 'matching) | |
819 | ||
820 | (defcustom occur-hook nil | |
c7d2f2cc | 821 | "Hook run by Occur when there are any matches." |
daae70bf CW |
822 | :type 'hook |
823 | :group 'matching) | |
824 | ||
8e62d5e8 CD |
825 | (defcustom occur-mode-find-occurrence-hook nil |
826 | "Hook run by Occur after locating an occurrence. | |
827 | This will be called with the cursor position at the occurrence. An application | |
828 | for this is to reveal context in an outline-mode when the occurrence is hidden." | |
829 | :type 'hook | |
830 | :group 'matching) | |
831 | ||
de3c9b09 | 832 | (put 'occur-mode 'mode-class 'special) |
505847d4 | 833 | (defun occur-mode () |
698e1804 | 834 | "Major mode for output from \\[occur]. |
0081c8a1 RS |
835 | \\<occur-mode-map>Move point to one of the items in this buffer, then use |
836 | \\[occur-mode-goto-occurrence] to go to the occurrence that the item refers to. | |
837 | Alternatively, click \\[occur-mode-mouse-goto] on an item to go to it. | |
838 | ||
698e1804 | 839 | \\{occur-mode-map}" |
ce8b7b9f | 840 | (interactive) |
505847d4 RS |
841 | (kill-all-local-variables) |
842 | (use-local-map occur-mode-map) | |
843 | (setq major-mode 'occur-mode) | |
844 | (setq mode-name "Occur") | |
f601efb0 | 845 | (set (make-local-variable 'revert-buffer-function) 'occur-revert-function) |
c9ae8cbb | 846 | (make-local-variable 'occur-revert-arguments) |
fbefbe33 | 847 | (add-hook 'change-major-mode-hook 'font-lock-defontify nil t) |
423e4de7 | 848 | (setq next-error-function 'occur-next-error) |
92e9557c | 849 | (run-mode-hooks 'occur-mode-hook)) |
698e1804 | 850 | |
a41284da | 851 | (defun occur-revert-function (ignore1 ignore2) |
46b3d18e | 852 | "Handle `revert-buffer' for Occur mode buffers." |
e1690783 | 853 | (apply 'occur-1 (append occur-revert-arguments (list (buffer-name))))) |
a41284da | 854 | |
78bead73 | 855 | (defun occur-mode-find-occurrence () |
46b3d18e RS |
856 | (let ((pos (get-text-property (point) 'occur-target))) |
857 | (unless pos | |
68608d9c | 858 | (error "No occurrence on this line")) |
46b3d18e RS |
859 | (unless (buffer-live-p (marker-buffer pos)) |
860 | (error "Buffer for this occurrence was killed")) | |
861 | pos)) | |
78bead73 | 862 | |
cedbd3f0 SM |
863 | (defalias 'occur-mode-mouse-goto 'occur-mode-goto-occurrence) |
864 | (defun occur-mode-goto-occurrence (&optional event) | |
78bead73 | 865 | "Go to the occurrence the current line describes." |
cedbd3f0 SM |
866 | (interactive (list last-nonmenu-event)) |
867 | (let ((pos | |
868 | (if (null event) | |
869 | ;; Actually `event-end' works correctly with a nil argument as | |
870 | ;; well, so we could dispense with this test, but let's not | |
871 | ;; rely on this undocumented behavior. | |
872 | (occur-mode-find-occurrence) | |
873 | (with-current-buffer (window-buffer (posn-window (event-end event))) | |
874 | (save-excursion | |
875 | (goto-char (posn-point (event-end event))) | |
876 | (occur-mode-find-occurrence))))) | |
877 | same-window-buffer-names | |
878 | same-window-regexps) | |
7e1d31d4 | 879 | (pop-to-buffer (marker-buffer pos)) |
8e62d5e8 CD |
880 | (goto-char pos) |
881 | (run-hooks 'occur-mode-find-occurrence-hook))) | |
8d15583f | 882 | |
029024e2 RS |
883 | (defun occur-mode-goto-occurrence-other-window () |
884 | "Go to the occurrence the current line describes, in another window." | |
885 | (interactive) | |
46b3d18e RS |
886 | (let ((pos (occur-mode-find-occurrence))) |
887 | (switch-to-buffer-other-window (marker-buffer pos)) | |
8e62d5e8 CD |
888 | (goto-char pos) |
889 | (run-hooks 'occur-mode-find-occurrence-hook))) | |
029024e2 | 890 | |
365486d6 RS |
891 | (defun occur-mode-display-occurrence () |
892 | "Display in another window the occurrence the current line describes." | |
893 | (interactive) | |
46b3d18e RS |
894 | (let ((pos (occur-mode-find-occurrence)) |
895 | window | |
896 | ;; Bind these to ensure `display-buffer' puts it in another window. | |
365486d6 | 897 | same-window-buffer-names |
46b3d18e RS |
898 | same-window-regexps) |
899 | (setq window (display-buffer (marker-buffer pos))) | |
365486d6 RS |
900 | ;; This is the way to set point in the proper window. |
901 | (save-selected-window | |
902 | (select-window window) | |
8e62d5e8 CD |
903 | (goto-char pos) |
904 | (run-hooks 'occur-mode-find-occurrence-hook)))) | |
365486d6 | 905 | |
123d5548 | 906 | (defun occur-find-match (n search message) |
8d15583f RS |
907 | (if (not n) (setq n 1)) |
908 | (let ((r)) | |
909 | (while (> n 0) | |
123d5548 JB |
910 | (setq r (funcall search (point) 'occur-match)) |
911 | (and r | |
912 | (get-text-property r 'occur-match) | |
913 | (setq r (funcall search r 'occur-match))) | |
8d15583f | 914 | (if r |
123d5548 JB |
915 | (goto-char r) |
916 | (error message)) | |
8d15583f RS |
917 | (setq n (1- n))))) |
918 | ||
123d5548 JB |
919 | (defun occur-next (&optional n) |
920 | "Move to the Nth (default 1) next match in an Occur mode buffer." | |
921 | (interactive "p") | |
922 | (occur-find-match n #'next-single-property-change "No more matches")) | |
923 | ||
8d15583f | 924 | (defun occur-prev (&optional n) |
46b3d18e | 925 | "Move to the Nth (default 1) previous match in an Occur mode buffer." |
8d15583f | 926 | (interactive "p") |
123d5548 | 927 | (occur-find-match n #'previous-single-property-change "No earlier matches")) |
423e4de7 KS |
928 | |
929 | (defun occur-next-error (&optional argp reset) | |
930 | "Move to the Nth (default 1) next match in an Occur mode buffer. | |
931 | Compatibility function for \\[next-error] invocations." | |
932 | (interactive "p") | |
5f9e0ca5 | 933 | ;; we need to run occur-find-match from within the Occur buffer |
f1f007dc | 934 | (with-current-buffer |
f42a241b | 935 | ;; Choose the buffer and make it current. |
5f9e0ca5 TZ |
936 | (if (next-error-buffer-p (current-buffer)) |
937 | (current-buffer) | |
f42a241b RS |
938 | (next-error-find-buffer nil nil |
939 | (lambda () | |
940 | (eq major-mode 'occur-mode)))) | |
f1f007dc JL |
941 | |
942 | (goto-char (cond (reset (point-min)) | |
943 | ((< argp 0) (line-beginning-position)) | |
6c6605b2 JL |
944 | ((> argp 0) (line-end-position)) |
945 | ((point)))) | |
5f9e0ca5 | 946 | (occur-find-match |
f1f007dc JL |
947 | (abs argp) |
948 | (if (> 0 argp) | |
5f9e0ca5 TZ |
949 | #'previous-single-property-change |
950 | #'next-single-property-change) | |
951 | "No more matches") | |
952 | ;; In case the *Occur* buffer is visible in a nonselected window. | |
084c41ca SM |
953 | (let ((win (get-buffer-window (current-buffer) t))) |
954 | (if win (set-window-point win (point)))) | |
5f9e0ca5 | 955 | (occur-mode-goto-occurrence))) |
4c53bd2b | 956 | \f |
aaaecfcd JL |
957 | (defface match |
958 | '((((class color) (min-colors 88) (background light)) | |
5183d4c9 | 959 | :background "yellow1") |
aaaecfcd | 960 | (((class color) (min-colors 88) (background dark)) |
4bc30b74 | 961 | :background "RoyalBlue3") |
330167fc RS |
962 | (((class color) (min-colors 8) (background light)) |
963 | :background "yellow" :foreground "black") | |
964 | (((class color) (min-colors 8) (background dark)) | |
aaaecfcd JL |
965 | :background "blue" :foreground "white") |
966 | (((type tty) (class mono)) | |
967 | :inverse-video t) | |
968 | (t :background "gray")) | |
969 | "Face used to highlight matches permanently." | |
970 | :group 'matching | |
bf247b6e | 971 | :version "22.1") |
aaaecfcd | 972 | |
9d325ebf | 973 | (defcustom list-matching-lines-default-context-lines 0 |
9201cc28 | 974 | "Default number of context lines included around `list-matching-lines' matches. |
e730be7f | 975 | A negative number means to include that many lines before the match. |
9d325ebf RS |
976 | A positive number means to include that many lines both before and after." |
977 | :type 'integer | |
978 | :group 'matching) | |
698e1804 | 979 | |
31e1d920 | 980 | (defalias 'list-matching-lines 'occur) |
698e1804 | 981 | |
aaaecfcd | 982 | (defcustom list-matching-lines-face 'match |
9201cc28 | 983 | "Face used by \\[list-matching-lines] to show the text that matches. |
68608d9c CW |
984 | If the value is nil, don't highlight the matching portions specially." |
985 | :type 'face | |
986 | :group 'matching) | |
987 | ||
988 | (defcustom list-matching-lines-buffer-name-face 'underline | |
9201cc28 | 989 | "Face used by \\[list-matching-lines] to show the names of buffers. |
68608d9c CW |
990 | If the value is nil, don't highlight the buffer names specially." |
991 | :type 'face | |
992 | :group 'matching) | |
993 | ||
8b363e6f JL |
994 | (defcustom occur-excluded-properties |
995 | '(read-only invisible intangible field mouse-face help-echo local-map keymap | |
996 | yank-handler follow-link) | |
9201cc28 | 997 | "Text properties to discard when copying lines to the *Occur* buffer. |
8b363e6f JL |
998 | The value should be a list of text properties to discard or t, |
999 | which means to discard all text properties." | |
1000 | :type '(choice (const :tag "All" t) (repeat symbol)) | |
1001 | :group 'matching | |
1002 | :version "22.1") | |
1003 | ||
2f4e3f7a | 1004 | (defun occur-accumulate-lines (count &optional keep-props) |
68608d9c CW |
1005 | (save-excursion |
1006 | (let ((forwardp (> count 0)) | |
f1f007dc | 1007 | result beg end) |
68608d9c CW |
1008 | (while (not (or (zerop count) |
1009 | (if forwardp | |
1010 | (eobp) | |
1011 | (bobp)))) | |
b0523087 | 1012 | (setq count (+ count (if forwardp -1 1))) |
f1f007dc JL |
1013 | (setq beg (line-beginning-position) |
1014 | end (line-end-position)) | |
99769096 | 1015 | (if (and keep-props (if (boundp 'jit-lock-mode) jit-lock-mode) |
f1f007dc | 1016 | (text-property-not-all beg end 'fontified t)) |
99769096 RS |
1017 | (if (fboundp 'jit-lock-fontify-now) |
1018 | (jit-lock-fontify-now beg end))) | |
68608d9c | 1019 | (push |
8b363e6f JL |
1020 | (if (and keep-props (not (eq occur-excluded-properties t))) |
1021 | (let ((str (buffer-substring beg end))) | |
1022 | (remove-list-of-text-properties | |
1023 | 0 (length str) occur-excluded-properties str) | |
1024 | str) | |
1025 | (buffer-substring-no-properties beg end)) | |
68608d9c CW |
1026 | result) |
1027 | (forward-line (if forwardp 1 -1))) | |
1028 | (nreverse result)))) | |
1029 | ||
1030 | (defun occur-read-primary-args () | |
714da757 JL |
1031 | (list (read-regexp "List lines matching regexp" |
1032 | (car regexp-history)) | |
96f606c5 JL |
1033 | (when current-prefix-arg |
1034 | (prefix-numeric-value current-prefix-arg)))) | |
c9daced0 | 1035 | |
a653724b | 1036 | (defun occur-rename-buffer (&optional unique-p interactive-p) |
d99118b0 | 1037 | "Rename the current *Occur* buffer to *Occur: original-buffer-name*. |
a653724b JB |
1038 | Here `original-buffer-name' is the buffer name were Occur was originally run. |
1039 | When given the prefix argument, or called non-interactively, the renaming | |
1040 | will not clobber the existing buffer(s) of that name, but use | |
1041 | `generate-new-buffer-name' instead. You can add this to `occur-hook' | |
1042 | if you always want a separate *Occur* buffer for each buffer where you | |
1043 | invoke `occur'." | |
1044 | (interactive "P\np") | |
d99118b0 SS |
1045 | (with-current-buffer |
1046 | (if (eq major-mode 'occur-mode) (current-buffer) (get-buffer "*Occur*")) | |
1047 | (rename-buffer (concat "*Occur: " | |
1048 | (mapconcat #'buffer-name | |
1049 | (car (cddr occur-revert-arguments)) "/") | |
1050 | "*") | |
a653724b | 1051 | (or unique-p (not interactive-p))))) |
d99118b0 | 1052 | |
698e1804 | 1053 | (defun occur (regexp &optional nlines) |
99976f85 | 1054 | "Show all lines in the current buffer containing a match for REGEXP. |
bace7209 | 1055 | This function can not handle matches that span more than one line. |
698e1804 | 1056 | |
da44e784 RM |
1057 | Each line is displayed with NLINES lines before and after, or -NLINES |
1058 | before if NLINES is negative. | |
1059 | NLINES defaults to `list-matching-lines-default-context-lines'. | |
698e1804 RS |
1060 | Interactively it is the prefix arg. |
1061 | ||
4c53bd2b | 1062 | The lines are shown in a buffer named `*Occur*'. |
698e1804 | 1063 | It serves as a menu to find any of the occurrences in this buffer. |
de3c9b09 | 1064 | \\<occur-mode-map>\\[describe-mode] in that buffer will explain how. |
9483d601 | 1065 | |
3be42fcd JL |
1066 | If REGEXP contains upper case characters (excluding those preceded by `\\') |
1067 | and `search-upper-case' is non-nil, the matching is case-sensitive." | |
68608d9c CW |
1068 | (interactive (occur-read-primary-args)) |
1069 | (occur-1 regexp nlines (list (current-buffer)))) | |
1070 | ||
1071 | (defun multi-occur (bufs regexp &optional nlines) | |
1072 | "Show all lines in buffers BUFS containing a match for REGEXP. | |
1073 | This function acts on multiple buffers; otherwise, it is exactly like | |
191b577e RS |
1074 | `occur'. When you invoke this command interactively, you must specify |
1075 | the buffer names that you want, one by one." | |
a5dfed3e | 1076 | (interactive |
68608d9c | 1077 | (cons |
52698d45 KS |
1078 | (let* ((bufs (list (read-buffer "First buffer to search: " |
1079 | (current-buffer) t))) | |
1080 | (buf nil) | |
1081 | (ido-ignore-item-temp-list bufs)) | |
68608d9c | 1082 | (while (not (string-equal |
f1180544 | 1083 | (setq buf (read-buffer |
52698d45 KS |
1084 | (if (eq read-buffer-function 'ido-read-buffer) |
1085 | "Next buffer to search (C-j to end): " | |
1086 | "Next buffer to search (RET to end): ") | |
1087 | nil t)) | |
68608d9c | 1088 | "")) |
52698d45 KS |
1089 | (add-to-list 'bufs buf) |
1090 | (setq ido-ignore-item-temp-list bufs)) | |
68608d9c CW |
1091 | (nreverse (mapcar #'get-buffer bufs))) |
1092 | (occur-read-primary-args))) | |
1093 | (occur-1 regexp nlines bufs)) | |
1094 | ||
191b577e RS |
1095 | (defun multi-occur-in-matching-buffers (bufregexp regexp &optional allbufs) |
1096 | "Show all lines matching REGEXP in buffers specified by BUFREGEXP. | |
1097 | Normally BUFREGEXP matches against each buffer's visited file name, | |
1098 | but if you specify a prefix argument, it matches against the buffer name. | |
68608d9c CW |
1099 | See also `multi-occur'." |
1100 | (interactive | |
1101 | (cons | |
1102 | (let* ((default (car regexp-history)) | |
1103 | (input | |
1104 | (read-from-minibuffer | |
689f4394 | 1105 | (if current-prefix-arg |
191b577e RS |
1106 | "List lines in buffers whose names match regexp: " |
1107 | "List lines in buffers whose filenames match regexp: ") | |
68608d9c CW |
1108 | nil |
1109 | nil | |
1110 | nil | |
1111 | 'regexp-history))) | |
1112 | (if (equal input "") | |
1113 | default | |
1114 | input)) | |
1115 | (occur-read-primary-args))) | |
1116 | (when bufregexp | |
689f4394 | 1117 | (occur-1 regexp nil |
68608d9c CW |
1118 | (delq nil |
1119 | (mapcar (lambda (buf) | |
191b577e RS |
1120 | (when (if allbufs |
1121 | (string-match bufregexp | |
1122 | (buffer-name buf)) | |
1123 | (and (buffer-file-name buf) | |
1124 | (string-match bufregexp | |
1125 | (buffer-file-name buf)))) | |
68608d9c CW |
1126 | buf)) |
1127 | (buffer-list)))))) | |
1128 | ||
e1690783 | 1129 | (defun occur-1 (regexp nlines bufs &optional buf-name) |
360289a6 JL |
1130 | (unless (and regexp (not (equal regexp ""))) |
1131 | (error "Occur doesn't work with the empty regexp")) | |
e1690783 CW |
1132 | (unless buf-name |
1133 | (setq buf-name "*Occur*")) | |
f42a241b | 1134 | (let (occur-buf |
70ed2a76 CW |
1135 | (active-bufs (delq nil (mapcar #'(lambda (buf) |
1136 | (when (buffer-live-p buf) buf)) | |
1137 | bufs)))) | |
1138 | ;; Handle the case where one of the buffers we're searching is the | |
f42a241b RS |
1139 | ;; output buffer. Just rename it. |
1140 | (when (member buf-name (mapcar 'buffer-name active-bufs)) | |
1141 | (with-current-buffer (get-buffer buf-name) | |
1142 | (rename-uniquely))) | |
1143 | ||
1144 | ;; Now find or create the output buffer. | |
1145 | ;; If we just renamed that buffer, we will make a new one here. | |
1146 | (setq occur-buf (get-buffer-create buf-name)) | |
1147 | ||
68608d9c | 1148 | (with-current-buffer occur-buf |
68608d9c | 1149 | (occur-mode) |
8121414a CY |
1150 | (let ((inhibit-read-only t) |
1151 | ;; Don't generate undo entries for creation of the initial contents. | |
1152 | (buffer-undo-list t)) | |
06df7f87 EZ |
1153 | (erase-buffer) |
1154 | (let ((count (occur-engine | |
1155 | regexp active-bufs occur-buf | |
1156 | (or nlines list-matching-lines-default-context-lines) | |
3be42fcd JL |
1157 | (if (and case-fold-search search-upper-case) |
1158 | (isearch-no-upper-case-p regexp t) | |
1159 | case-fold-search) | |
06df7f87 | 1160 | list-matching-lines-buffer-name-face |
8b363e6f JL |
1161 | nil list-matching-lines-face |
1162 | (not (eq occur-excluded-properties t))))) | |
06df7f87 EZ |
1163 | (let* ((bufcount (length active-bufs)) |
1164 | (diff (- (length bufs) bufcount))) | |
1165 | (message "Searched %d buffer%s%s; %s match%s for `%s'" | |
1166 | bufcount (if (= bufcount 1) "" "s") | |
1167 | (if (zerop diff) "" (format " (%d killed)" diff)) | |
1168 | (if (zerop count) "no" (format "%d" count)) | |
1169 | (if (= count 1) "" "es") | |
1170 | regexp)) | |
1171 | (setq occur-revert-arguments (list regexp nlines bufs)) | |
c7d2f2cc JB |
1172 | (if (= count 0) |
1173 | (kill-buffer occur-buf) | |
1174 | (display-buffer occur-buf) | |
1175 | (setq next-error-last-buffer occur-buf) | |
1176 | (setq buffer-read-only t) | |
1177 | (set-buffer-modified-p nil) | |
1178 | (run-hooks 'occur-hook))))))) | |
68608d9c | 1179 | |
46b3d18e RS |
1180 | (defun occur-engine-add-prefix (lines) |
1181 | (mapcar | |
1182 | #'(lambda (line) | |
261cca88 | 1183 | (concat " :" line "\n")) |
46b3d18e RS |
1184 | lines)) |
1185 | ||
1186 | (defun occur-engine (regexp buffers out-buf nlines case-fold-search | |
1187 | title-face prefix-face match-face keep-props) | |
1188 | (with-current-buffer out-buf | |
5cb4031d KH |
1189 | (let ((globalcount 0) |
1190 | (coding nil)) | |
46b3d18e RS |
1191 | ;; Map over all the buffers |
1192 | (dolist (buf buffers) | |
1193 | (when (buffer-live-p buf) | |
1194 | (let ((matches 0) ;; count of matched lines | |
1195 | (lines 1) ;; line count | |
1196 | (matchbeg 0) | |
46b3d18e RS |
1197 | (origpt nil) |
1198 | (begpt nil) | |
1199 | (endpt nil) | |
1200 | (marker nil) | |
1201 | (curstring "") | |
bc16bf5e | 1202 | (inhibit-field-text-motion t) |
46b3d18e | 1203 | (headerpt (with-current-buffer out-buf (point)))) |
cedbd3f0 | 1204 | (with-current-buffer buf |
5cb4031d KH |
1205 | (or coding |
1206 | ;; Set CODING only if the current buffer locally | |
1207 | ;; binds buffer-file-coding-system. | |
1208 | (not (local-variable-p 'buffer-file-coding-system)) | |
1209 | (setq coding buffer-file-coding-system)) | |
68608d9c | 1210 | (save-excursion |
46b3d18e RS |
1211 | (goto-char (point-min)) ;; begin searching in the buffer |
1212 | (while (not (eobp)) | |
1213 | (setq origpt (point)) | |
1214 | (when (setq endpt (re-search-forward regexp nil t)) | |
1215 | (setq matches (1+ matches)) ;; increment match count | |
5291cbca | 1216 | (setq matchbeg (match-beginning 0)) |
46b3d18e | 1217 | (setq lines (+ lines (1- (count-lines origpt endpt)))) |
f1f007dc JL |
1218 | (save-excursion |
1219 | (goto-char matchbeg) | |
1220 | (setq begpt (line-beginning-position) | |
1221 | endpt (line-end-position))) | |
46b3d18e RS |
1222 | (setq marker (make-marker)) |
1223 | (set-marker marker matchbeg) | |
99769096 RS |
1224 | (if (and keep-props |
1225 | (if (boundp 'jit-lock-mode) jit-lock-mode) | |
f1f007dc | 1226 | (text-property-not-all begpt endpt 'fontified t)) |
99769096 RS |
1227 | (if (fboundp 'jit-lock-fontify-now) |
1228 | (jit-lock-fontify-now begpt endpt))) | |
8b363e6f JL |
1229 | (if (and keep-props (not (eq occur-excluded-properties t))) |
1230 | (progn | |
1231 | (setq curstring (buffer-substring begpt endpt)) | |
1232 | (remove-list-of-text-properties | |
1233 | 0 (length curstring) occur-excluded-properties curstring)) | |
1234 | (setq curstring (buffer-substring-no-properties begpt endpt))) | |
1235 | ;; Highlight the matches | |
46b3d18e RS |
1236 | (let ((len (length curstring)) |
1237 | (start 0)) | |
46b3d18e RS |
1238 | (while (and (< start len) |
1239 | (string-match regexp curstring start)) | |
f1f007dc JL |
1240 | (add-text-properties |
1241 | (match-beginning 0) (match-end 0) | |
1242 | (append | |
1243 | `(occur-match t) | |
1244 | (when match-face | |
1245 | ;; Use `face' rather than `font-lock-face' here | |
1246 | ;; so as to override faces copied from the buffer. | |
1247 | `(face ,match-face))) | |
1248 | curstring) | |
46b3d18e RS |
1249 | (setq start (match-end 0)))) |
1250 | ;; Generate the string to insert for this match | |
1251 | (let* ((out-line | |
1252 | (concat | |
261cca88 | 1253 | ;; Using 7 digits aligns tabs properly. |
1e0ab2f0 | 1254 | (apply #'propertize (format "%7d:" lines) |
46b3d18e RS |
1255 | (append |
1256 | (when prefix-face | |
506a6d7e | 1257 | `(font-lock-face prefix-face)) |
1e0ab2f0 | 1258 | `(occur-prefix t mouse-face (highlight) |
c7cad90c RF |
1259 | occur-target ,marker follow-link t |
1260 | help-echo "mouse-2: go to this occurrence"))) | |
f1f007dc JL |
1261 | ;; We don't put `mouse-face' on the newline, |
1262 | ;; because that loses. And don't put it | |
1263 | ;; on context lines to reduce flicker. | |
1e0ab2f0 | 1264 | (propertize curstring 'mouse-face (list 'highlight) |
c7cad90c RF |
1265 | 'occur-target marker |
1266 | 'follow-link t | |
1267 | 'help-echo | |
1268 | "mouse-2: go to this occurrence") | |
61f570e2 RF |
1269 | ;; Add marker at eol, but no mouse props. |
1270 | (propertize "\n" 'occur-target marker))) | |
46b3d18e RS |
1271 | (data |
1272 | (if (= nlines 0) | |
1273 | ;; The simple display style | |
1274 | out-line | |
f8edc67e RS |
1275 | ;; The complex multi-line display style. |
1276 | (occur-context-lines out-line nlines keep-props) | |
1277 | ))) | |
46b3d18e RS |
1278 | ;; Actually insert the match display data |
1279 | (with-current-buffer out-buf | |
1280 | (let ((beg (point)) | |
1281 | (end (progn (insert data) (point)))) | |
1282 | (unless (= nlines 0) | |
c7cad90c | 1283 | (insert "-------\n"))))) |
46b3d18e | 1284 | (goto-char endpt)) |
e1690783 CW |
1285 | (if endpt |
1286 | (progn | |
1287 | (setq lines (1+ lines)) | |
1288 | ;; On to the next match... | |
1289 | (forward-line 1)) | |
1290 | (goto-char (point-max)))))) | |
46b3d18e | 1291 | (when (not (zerop matches)) ;; is the count zero? |
daae70bf | 1292 | (setq globalcount (+ globalcount matches)) |
46b3d18e RS |
1293 | (with-current-buffer out-buf |
1294 | (goto-char headerpt) | |
1295 | (let ((beg (point)) | |
1296 | end) | |
dbed3cd3 JPW |
1297 | (insert (format "%d match%s for \"%s\" in buffer: %s\n" |
1298 | matches (if (= matches 1) "" "es") | |
1299 | regexp (buffer-name buf))) | |
46b3d18e RS |
1300 | (setq end (point)) |
1301 | (add-text-properties beg end | |
1302 | (append | |
1303 | (when title-face | |
506a6d7e CW |
1304 | `(font-lock-face ,title-face)) |
1305 | `(occur-title ,buf)))) | |
46b3d18e | 1306 | (goto-char (point-min))))))) |
5cb4031d KH |
1307 | (if coding |
1308 | ;; CODING is buffer-file-coding-system of the first buffer | |
1309 | ;; that locally binds it. Let's use it also for the output | |
1310 | ;; buffer. | |
1311 | (set-buffer-file-coding-system coding)) | |
46b3d18e RS |
1312 | ;; Return the number of matches |
1313 | globalcount))) | |
68608d9c | 1314 | |
f8edc67e RS |
1315 | ;; Generate context display for occur. |
1316 | ;; OUT-LINE is the line where the match is. | |
1317 | ;; NLINES and KEEP-PROPS are args to occur-engine. | |
1318 | ;; Generate a list of lines, add prefixes to all but OUT-LINE, | |
1319 | ;; then concatenate them all together. | |
1320 | (defun occur-context-lines (out-line nlines keep-props) | |
1321 | (apply #'concat | |
1322 | (nconc | |
1323 | (occur-engine-add-prefix | |
1324 | (nreverse (cdr (occur-accumulate-lines | |
1325 | (- (1+ (abs nlines))) keep-props)))) | |
1326 | (list out-line) | |
1327 | (if (> nlines 0) | |
1328 | (occur-engine-add-prefix | |
1329 | (cdr (occur-accumulate-lines (1+ nlines) keep-props))))))) | |
698e1804 | 1330 | \f |
81bdc14d RS |
1331 | ;; It would be nice to use \\[...], but there is no reasonable way |
1332 | ;; to make that display both SPC and Y. | |
698e1804 RS |
1333 | (defconst query-replace-help |
1334 | "Type Space or `y' to replace one match, Delete or `n' to skip to next, | |
be44f62c | 1335 | RET or `q' to exit, Period to replace one match and exit, |
698e1804 RS |
1336 | Comma to replace but not move point immediately, |
1337 | C-r to enter recursive edit (\\[exit-recursive-edit] to get out again), | |
1338 | C-w to delete match and recursive edit, | |
1339 | C-l to clear the screen, redisplay, and offer same replacement again, | |
1340 | ! to replace all remaining matches with no more questions, | |
7ce278f3 GM |
1341 | ^ to move point back to previous match, |
1342 | E to edit the replacement string" | |
f54701d1 | 1343 | "Help message while in `query-replace'.") |
698e1804 | 1344 | |
cedbd3f0 SM |
1345 | (defvar query-replace-map |
1346 | (let ((map (make-sparse-keymap))) | |
1347 | (define-key map " " 'act) | |
1348 | (define-key map "\d" 'skip) | |
1349 | (define-key map [delete] 'skip) | |
1350 | (define-key map [backspace] 'skip) | |
1351 | (define-key map "y" 'act) | |
1352 | (define-key map "n" 'skip) | |
1353 | (define-key map "Y" 'act) | |
1354 | (define-key map "N" 'skip) | |
1355 | (define-key map "e" 'edit-replacement) | |
1356 | (define-key map "E" 'edit-replacement) | |
1357 | (define-key map "," 'act-and-show) | |
1358 | (define-key map "q" 'exit) | |
1359 | (define-key map "\r" 'exit) | |
1360 | (define-key map [return] 'exit) | |
1361 | (define-key map "." 'act-and-exit) | |
1362 | (define-key map "\C-r" 'edit) | |
1363 | (define-key map "\C-w" 'delete-and-edit) | |
1364 | (define-key map "\C-l" 'recenter) | |
1365 | (define-key map "!" 'automatic) | |
1366 | (define-key map "^" 'backup) | |
1367 | (define-key map "\C-h" 'help) | |
1368 | (define-key map [f1] 'help) | |
1369 | (define-key map [help] 'help) | |
1370 | (define-key map "?" 'help) | |
1371 | (define-key map "\C-g" 'quit) | |
1372 | (define-key map "\C-]" 'quit) | |
1373 | (define-key map "\e" 'exit-prefix) | |
1374 | (define-key map [escape] 'exit-prefix) | |
1375 | map) | |
81bdc14d RS |
1376 | "Keymap that defines the responses to questions in `query-replace'. |
1377 | The \"bindings\" in this map are not commands; they are answers. | |
1378 | The valid answers include `act', `skip', `act-and-show', | |
1379 | `exit', `act-and-exit', `edit', `delete-and-edit', `recenter', | |
d9121bc0 | 1380 | `automatic', `backup', `exit-prefix', and `help'.") |
81bdc14d | 1381 | |
b591f338 JL |
1382 | (defvar multi-query-replace-map |
1383 | (let ((map (make-sparse-keymap))) | |
1384 | (set-keymap-parent map query-replace-map) | |
1385 | (define-key map "Y" 'automatic-all) | |
1386 | (define-key map "N" 'exit-current) | |
1387 | map) | |
1388 | "Keymap that defines additional bindings for multi-buffer replacements. | |
1389 | It extends its parent map `query-replace-map' with new bindings to | |
1390 | operate on a set of buffers/files. The difference with its parent map | |
1391 | is the additional answers `automatic-all' to replace all remaining | |
1392 | matches in all remaining buffers with no more questions, and | |
1393 | `exit-current' to skip remaining matches in the current buffer | |
1394 | and to continue with the next buffer in the sequence.") | |
1395 | ||
84482eb3 | 1396 | (defun replace-match-string-symbols (n) |
e730be7f DL |
1397 | "Process a list (and any sub-lists), expanding certain symbols. |
1398 | Symbol Expands To | |
1399 | N (match-string N) (where N is a string of digits) | |
1400 | #N (string-to-number (match-string N)) | |
1401 | & (match-string 0) | |
1402 | #& (string-to-number (match-string 0)) | |
2f57bf85 | 1403 | # replace-count |
e730be7f DL |
1404 | |
1405 | Note that these symbols must be preceeded by a backslash in order to | |
f72f9f1a RS |
1406 | type them using Lisp syntax." |
1407 | (while (consp n) | |
84482eb3 RS |
1408 | (cond |
1409 | ((consp (car n)) | |
1410 | (replace-match-string-symbols (car n))) ;Process sub-list | |
1411 | ((symbolp (car n)) | |
1412 | (let ((name (symbol-name (car n)))) | |
1413 | (cond | |
1414 | ((string-match "^[0-9]+$" name) | |
1415 | (setcar n (list 'match-string (string-to-number name)))) | |
1416 | ((string-match "^#[0-9]+$" name) | |
1417 | (setcar n (list 'string-to-number | |
1418 | (list 'match-string | |
1419 | (string-to-number (substring name 1)))))) | |
1420 | ((string= "&" name) | |
1421 | (setcar n '(match-string 0))) | |
1422 | ((string= "#&" name) | |
2f57bf85 DK |
1423 | (setcar n '(string-to-number (match-string 0)))) |
1424 | ((string= "#" name) | |
1425 | (setcar n 'replace-count)))))) | |
84482eb3 RS |
1426 | (setq n (cdr n)))) |
1427 | ||
1428 | (defun replace-eval-replacement (expression replace-count) | |
1429 | (let ((replacement (eval expression))) | |
1430 | (if (stringp replacement) | |
1431 | replacement | |
1432 | (prin1-to-string replacement t)))) | |
1433 | ||
2f57bf85 DK |
1434 | (defun replace-quote (replacement) |
1435 | "Quote a replacement string. | |
1436 | This just doubles all backslashes in REPLACEMENT and | |
1437 | returns the resulting string. If REPLACEMENT is not | |
1438 | a string, it is first passed through `prin1-to-string' | |
1439 | with the `noescape' argument set. | |
1440 | ||
1441 | `match-data' is preserved across the call." | |
1442 | (save-match-data | |
1443 | (replace-regexp-in-string "\\\\" "\\\\" | |
1444 | (if (stringp replacement) | |
1445 | replacement | |
1446 | (prin1-to-string replacement t)) | |
1447 | t t))) | |
1448 | ||
84482eb3 RS |
1449 | (defun replace-loop-through-replacements (data replace-count) |
1450 | ;; DATA is a vector contaning the following values: | |
1451 | ;; 0 next-rotate-count | |
1452 | ;; 1 repeat-count | |
1453 | ;; 2 next-replacement | |
1454 | ;; 3 replacements | |
1455 | (if (= (aref data 0) replace-count) | |
1456 | (progn | |
1457 | (aset data 0 (+ replace-count (aref data 1))) | |
1458 | (let ((next (cdr (aref data 2)))) | |
1459 | (aset data 2 (if (consp next) next (aref data 3)))))) | |
1460 | (car (aref data 2))) | |
1461 | ||
7c1c02ac DK |
1462 | (defun replace-match-data (integers reuse &optional new) |
1463 | "Like `match-data', but markers in REUSE get invalidated. | |
6a964bb1 | 1464 | If NEW is non-nil, it is set and returned instead of fresh data, |
7c1c02ac DK |
1465 | but coerced to the correct value of INTEGERS." |
1466 | (or (and new | |
1467 | (progn | |
1468 | (set-match-data new) | |
1469 | (and (eq new reuse) | |
1470 | (eq (null integers) (markerp (car reuse))) | |
1471 | new))) | |
10ddc30e | 1472 | (match-data integers reuse t))) |
7c1c02ac DK |
1473 | |
1474 | (defun replace-match-maybe-edit (newtext fixedcase literal noedit match-data) | |
1475 | "Make a replacement with `replace-match', editing `\\?'. | |
15fd7d5d | 1476 | NEWTEXT, FIXEDCASE, LITERAL are just passed on. If NOEDIT is true, no |
7c1c02ac DK |
1477 | check for `\\?' is made to save time. MATCH-DATA is used for the |
1478 | replacement. In case editing is done, it is changed to use markers. | |
1479 | ||
6a964bb1 | 1480 | The return value is non-nil if there has been no `\\?' or NOEDIT was |
7c1c02ac DK |
1481 | passed in. If LITERAL is set, no checking is done, anyway." |
1482 | (unless (or literal noedit) | |
1483 | (setq noedit t) | |
1484 | (while (string-match "\\(\\`\\|[^\\]\\)\\(\\\\\\\\\\)*\\(\\\\\\?\\)" | |
1485 | newtext) | |
1486 | (setq newtext | |
3981e5b5 JB |
1487 | (read-string "Edit replacement string: " |
1488 | (prog1 | |
1489 | (cons | |
1490 | (replace-match "" t t newtext 3) | |
1491 | (1+ (match-beginning 3))) | |
1492 | (setq match-data | |
1493 | (replace-match-data | |
1494 | nil match-data match-data)))) | |
7c1c02ac DK |
1495 | noedit nil))) |
1496 | (set-match-data match-data) | |
1497 | (replace-match newtext fixedcase literal) | |
1498 | noedit) | |
1499 | ||
c35a09fc CY |
1500 | (defvar replace-search-function 'search-forward |
1501 | "Function to use when searching for strings to replace. | |
1502 | It is used by `query-replace' and `replace-string', and is called | |
1503 | with three arguments, as if it were `search-forward'.") | |
1504 | ||
1505 | (defvar replace-re-search-function 're-search-forward | |
1506 | "Function to use when searching for regexps to replace. | |
1507 | It is used by `query-replace-regexp', `replace-regexp', | |
96f606c5 JL |
1508 | `query-replace-regexp-eval', and `map-query-replace-regexp'. |
1509 | It is called with three arguments, as if it were | |
1510 | `re-search-forward'.") | |
c35a09fc | 1511 | |
d99118b0 | 1512 | (defun perform-replace (from-string replacements |
698e1804 | 1513 | query-flag regexp-flag delimited-flag |
99a7559f | 1514 | &optional repeat-count map start end) |
698e1804 RS |
1515 | "Subroutine of `query-replace'. Its complexity handles interactive queries. |
1516 | Don't use this in your own program unless you want to query and set the mark | |
1517 | just as `query-replace' does. Instead, write a simple loop like this: | |
698665d1 GM |
1518 | |
1519 | (while (re-search-forward \"foo[ \\t]+bar\" nil t) | |
698e1804 | 1520 | (replace-match \"foobar\" nil nil)) |
698665d1 GM |
1521 | |
1522 | which will run faster and probably do exactly what you want. Please | |
1523 | see the documentation of `replace-match' to find out how to simulate | |
588c915a CW |
1524 | `case-replace'. |
1525 | ||
1526 | This function returns nil if and only if there were no matches to | |
1527 | make, or the user didn't cancel the call." | |
81bdc14d | 1528 | (or map (setq map query-replace-map)) |
1c1dadab RS |
1529 | (and query-flag minibuffer-auto-raise |
1530 | (raise-frame (window-frame (minibuffer-window)))) | |
26cc71af | 1531 | (let* ((case-fold-search |
3be42fcd JL |
1532 | (if (and case-fold-search search-upper-case) |
1533 | (isearch-no-upper-case-p from-string regexp-flag) | |
1534 | case-fold-search)) | |
26cc71af SM |
1535 | (nocasify (not (and case-replace case-fold-search))) |
1536 | (literal (or (not regexp-flag) (eq regexp-flag 'literal))) | |
c35a09fc CY |
1537 | (search-function |
1538 | (if regexp-flag | |
1539 | replace-re-search-function | |
1540 | replace-search-function)) | |
26cc71af SM |
1541 | (search-string from-string) |
1542 | (real-match-data nil) ; The match data for the current match. | |
1543 | (next-replacement nil) | |
1544 | ;; This is non-nil if we know there is nothing for the user | |
1545 | ;; to edit in the replacement. | |
1546 | (noedit nil) | |
1547 | (keep-going t) | |
1548 | (stack nil) | |
1549 | (replace-count 0) | |
1550 | (nonempty-match nil) | |
b591f338 | 1551 | (multi-buffer nil) |
26cc71af SM |
1552 | |
1553 | ;; If non-nil, it is marker saying where in the buffer to stop. | |
1554 | (limit nil) | |
1555 | ||
1556 | ;; Data for the next match. If a cons, it has the same format as | |
1557 | ;; (match-data); otherwise it is t if a match is possible at point. | |
1558 | (match-again t) | |
1559 | ||
1560 | (message | |
1561 | (if query-flag | |
1562 | (apply 'propertize | |
1563 | (substitute-command-keys | |
1564 | "Query replacing %s with %s: (\\<query-replace-map>\\[help] for help) ") | |
1565 | minibuffer-prompt-properties)))) | |
7ef5c431 KH |
1566 | |
1567 | ;; If region is active, in Transient Mark mode, operate on region. | |
47d72254 GM |
1568 | (when start |
1569 | (setq limit (copy-marker (max start end))) | |
1570 | (goto-char (min start end)) | |
1571 | (deactivate-mark)) | |
84482eb3 | 1572 | |
b591f338 JL |
1573 | ;; If last typed key in previous call of multi-buffer perform-replace |
1574 | ;; was `automatic-all', don't ask more questions in next files | |
1e4bd40d | 1575 | (when (eq (lookup-key map (vector last-input-event)) 'automatic-all) |
b591f338 JL |
1576 | (setq query-flag nil multi-buffer t)) |
1577 | ||
84482eb3 RS |
1578 | ;; REPLACEMENTS is either a string, a list of strings, or a cons cell |
1579 | ;; containing a function and its first argument. The function is | |
1580 | ;; called to generate each replacement like this: | |
1581 | ;; (funcall (car replacements) (cdr replacements) replace-count) | |
1582 | ;; It must return a string. | |
1583 | (cond | |
1584 | ((stringp replacements) | |
1585 | (setq next-replacement replacements | |
1586 | replacements nil)) | |
1587 | ((stringp (car replacements)) ; If it isn't a string, it must be a cons | |
1588 | (or repeat-count (setq repeat-count 1)) | |
1589 | (setq replacements (cons 'replace-loop-through-replacements | |
1590 | (vector repeat-count repeat-count | |
1591 | replacements replacements))))) | |
1592 | ||
698e1804 RS |
1593 | (if delimited-flag |
1594 | (setq search-function 're-search-forward | |
1595 | search-string (concat "\\b" | |
1596 | (if regexp-flag from-string | |
1597 | (regexp-quote from-string)) | |
1598 | "\\b"))) | |
ccec9764 | 1599 | (when query-replace-lazy-highlight |
444697a1 | 1600 | (setq isearch-lazy-highlight-last-string nil)) |
35d59c0f | 1601 | |
698e1804 RS |
1602 | (push-mark) |
1603 | (undo-boundary) | |
e782e9f2 RS |
1604 | (unwind-protect |
1605 | ;; Loop finding occurrences that perhaps should be replaced. | |
1606 | (while (and keep-going | |
22a76778 | 1607 | (not (or (eobp) (and limit (>= (point) limit)))) |
5632eb27 PE |
1608 | ;; Use the next match if it is already known; |
1609 | ;; otherwise, search for a match after moving forward | |
1610 | ;; one char if progress is required. | |
1611 | (setq real-match-data | |
b3aad29e CY |
1612 | (cond ((consp match-again) |
1613 | (goto-char (nth 1 match-again)) | |
1614 | (replace-match-data | |
1615 | t real-match-data match-again)) | |
1616 | ;; MATCH-AGAIN non-nil means accept an | |
1617 | ;; adjacent match. | |
1618 | (match-again | |
1619 | (and | |
1620 | (funcall search-function search-string | |
1621 | limit t) | |
1622 | ;; For speed, use only integers and | |
1623 | ;; reuse the list used last time. | |
1624 | (replace-match-data t real-match-data))) | |
1625 | ((and (< (1+ (point)) (point-max)) | |
1626 | (or (null limit) | |
1627 | (< (1+ (point)) limit))) | |
1628 | ;; If not accepting adjacent matches, | |
1629 | ;; move one char to the right before | |
1630 | ;; searching again. Undo the motion | |
1631 | ;; if the search fails. | |
1632 | (let ((opoint (point))) | |
1633 | (forward-char 1) | |
1634 | (if (funcall | |
1635 | search-function search-string | |
1636 | limit t) | |
1637 | (replace-match-data | |
1638 | t real-match-data) | |
1639 | (goto-char opoint) | |
1640 | nil)))))) | |
6a964bb1 | 1641 | |
c0b45763 RS |
1642 | ;; Record whether the match is nonempty, to avoid an infinite loop |
1643 | ;; repeatedly matching the same empty string. | |
1644 | (setq nonempty-match | |
1645 | (/= (nth 0 real-match-data) (nth 1 real-match-data))) | |
1646 | ||
1647 | ;; If the match is empty, record that the next one can't be | |
1648 | ;; adjacent. | |
1649 | ||
1650 | ;; Otherwise, if matching a regular expression, do the next | |
1651 | ;; match now, since the replacement for this match may | |
1652 | ;; affect whether the next match is adjacent to this one. | |
1653 | ;; If that match is empty, don't use it. | |
1654 | (setq match-again | |
1655 | (and nonempty-match | |
1656 | (or (not regexp-flag) | |
1657 | (and (looking-at search-string) | |
1658 | (let ((match (match-data))) | |
1659 | (and (/= (nth 0 match) (nth 1 match)) | |
1660 | match)))))) | |
1661 | ||
1c4fe319 RS |
1662 | ;; Optionally ignore matches that have a read-only property. |
1663 | (unless (and query-replace-skip-read-only | |
1664 | (text-property-not-all | |
c0b45763 | 1665 | (nth 0 real-match-data) (nth 1 real-match-data) |
1c4fe319 RS |
1666 | 'read-only nil)) |
1667 | ||
1c4fe319 RS |
1668 | ;; Calculate the replacement string, if necessary. |
1669 | (when replacements | |
1670 | (set-match-data real-match-data) | |
1671 | (setq next-replacement | |
1672 | (funcall (car replacements) (cdr replacements) | |
2f857176 | 1673 | replace-count))) |
1c4fe319 | 1674 | (if (not query-flag) |
f2e7b9ef | 1675 | (progn |
15fd7d5d | 1676 | (unless (or literal noedit) |
444697a1 JL |
1677 | (replace-highlight |
1678 | (nth 0 real-match-data) (nth 1 real-match-data) | |
1679 | start end search-string | |
1680 | (or delimited-flag regexp-flag) case-fold-search)) | |
7c1c02ac DK |
1681 | (setq noedit |
1682 | (replace-match-maybe-edit | |
1683 | next-replacement nocasify literal | |
1684 | noedit real-match-data) | |
1685 | replace-count (1+ replace-count))) | |
1c4fe319 RS |
1686 | (undo-boundary) |
1687 | (let (done replaced key def) | |
1688 | ;; Loop reading commands until one of them sets done, | |
7c1c02ac DK |
1689 | ;; which means it has finished handling this |
1690 | ;; occurrence. Any command that sets `done' should | |
1691 | ;; leave behind proper match data for the stack. | |
1692 | ;; Commands not setting `done' need to adjust | |
1693 | ;; `real-match-data'. | |
1c4fe319 RS |
1694 | (while (not done) |
1695 | (set-match-data real-match-data) | |
444697a1 JL |
1696 | (replace-highlight |
1697 | (match-beginning 0) (match-end 0) | |
1698 | start end search-string | |
1699 | (or delimited-flag regexp-flag) case-fold-search) | |
1c4fe319 RS |
1700 | ;; Bind message-log-max so we don't fill up the message log |
1701 | ;; with a bunch of identical messages. | |
7abe68aa JL |
1702 | (let ((message-log-max nil) |
1703 | (replacement-presentation | |
1704 | (if query-replace-show-replacement | |
1705 | (save-match-data | |
1706 | (set-match-data real-match-data) | |
1707 | (match-substitute-replacement next-replacement | |
1708 | nocasify literal)) | |
1709 | next-replacement))) | |
b938735a JL |
1710 | (message message |
1711 | (query-replace-descr from-string) | |
7abe68aa | 1712 | (query-replace-descr replacement-presentation))) |
1c4fe319 RS |
1713 | (setq key (read-event)) |
1714 | ;; Necessary in case something happens during read-event | |
1715 | ;; that clobbers the match data. | |
1716 | (set-match-data real-match-data) | |
1717 | (setq key (vector key)) | |
1718 | (setq def (lookup-key map key)) | |
1719 | ;; Restore the match data while we process the command. | |
1720 | (cond ((eq def 'help) | |
1721 | (with-output-to-temp-buffer "*Help*" | |
1722 | (princ | |
1723 | (concat "Query replacing " | |
04ff2dee | 1724 | (if delimited-flag "word " "") |
1c4fe319 RS |
1725 | (if regexp-flag "regexp " "") |
1726 | from-string " with " | |
1727 | next-replacement ".\n\n" | |
1728 | (substitute-command-keys | |
1729 | query-replace-help))) | |
1730 | (with-current-buffer standard-output | |
1731 | (help-mode)))) | |
1732 | ((eq def 'exit) | |
1733 | (setq keep-going nil) | |
1734 | (setq done t)) | |
b591f338 JL |
1735 | ((eq def 'exit-current) |
1736 | (setq multi-buffer t keep-going nil done t)) | |
1c4fe319 RS |
1737 | ((eq def 'backup) |
1738 | (if stack | |
588c915a | 1739 | (let ((elt (pop stack))) |
7c1c02ac DK |
1740 | (goto-char (nth 0 elt)) |
1741 | (setq replaced (nth 1 elt) | |
1742 | real-match-data | |
1743 | (replace-match-data | |
1744 | t real-match-data | |
1745 | (nth 2 elt)))) | |
1c4fe319 RS |
1746 | (message "No previous match") |
1747 | (ding 'no-terminate) | |
1748 | (sit-for 1))) | |
1749 | ((eq def 'act) | |
1750 | (or replaced | |
7c1c02ac DK |
1751 | (setq noedit |
1752 | (replace-match-maybe-edit | |
1753 | next-replacement nocasify literal | |
1754 | noedit real-match-data) | |
1755 | replace-count (1+ replace-count))) | |
1c4fe319 RS |
1756 | (setq done t replaced t)) |
1757 | ((eq def 'act-and-exit) | |
1758 | (or replaced | |
7c1c02ac | 1759 | (setq noedit |
da6eb51c | 1760 | (replace-match-maybe-edit |
7c1c02ac DK |
1761 | next-replacement nocasify literal |
1762 | noedit real-match-data) | |
1763 | replace-count (1+ replace-count))) | |
1c4fe319 RS |
1764 | (setq keep-going nil) |
1765 | (setq done t replaced t)) | |
1766 | ((eq def 'act-and-show) | |
1767 | (if (not replaced) | |
7c1c02ac DK |
1768 | (setq noedit |
1769 | (replace-match-maybe-edit | |
1770 | next-replacement nocasify literal | |
1771 | noedit real-match-data) | |
1772 | replace-count (1+ replace-count) | |
1773 | real-match-data (replace-match-data | |
1774 | t real-match-data) | |
1775 | replaced t))) | |
b591f338 | 1776 | ((or (eq def 'automatic) (eq def 'automatic-all)) |
1c4fe319 | 1777 | (or replaced |
7c1c02ac DK |
1778 | (setq noedit |
1779 | (replace-match-maybe-edit | |
1780 | next-replacement nocasify literal | |
1781 | noedit real-match-data) | |
1782 | replace-count (1+ replace-count))) | |
b591f338 JL |
1783 | (setq done t query-flag nil replaced t) |
1784 | (if (eq def 'automatic-all) (setq multi-buffer t))) | |
1c4fe319 RS |
1785 | ((eq def 'skip) |
1786 | (setq done t)) | |
1787 | ((eq def 'recenter) | |
1788 | (recenter nil)) | |
1789 | ((eq def 'edit) | |
1790 | (let ((opos (point-marker))) | |
7c1c02ac DK |
1791 | (setq real-match-data (replace-match-data |
1792 | nil real-match-data | |
1793 | real-match-data)) | |
1c4fe319 | 1794 | (goto-char (match-beginning 0)) |
86914dcc RS |
1795 | (save-excursion |
1796 | (save-window-excursion | |
1797 | (recursive-edit))) | |
7c1c02ac DK |
1798 | (goto-char opos) |
1799 | (set-marker opos nil)) | |
1c4fe319 RS |
1800 | ;; Before we make the replacement, |
1801 | ;; decide whether the search string | |
1802 | ;; can match again just after this match. | |
1803 | (if (and regexp-flag nonempty-match) | |
1804 | (setq match-again (and (looking-at search-string) | |
1805 | (match-data))))) | |
1c4fe319 RS |
1806 | ;; Edit replacement. |
1807 | ((eq def 'edit-replacement) | |
7c1c02ac DK |
1808 | (setq real-match-data (replace-match-data |
1809 | nil real-match-data | |
1810 | real-match-data) | |
1811 | next-replacement | |
3981e5b5 JB |
1812 | (read-string "Edit replacement string: " |
1813 | next-replacement) | |
7c1c02ac DK |
1814 | noedit nil) |
1815 | (if replaced | |
1816 | (set-match-data real-match-data) | |
1817 | (setq noedit | |
1818 | (replace-match-maybe-edit | |
1819 | next-replacement nocasify literal noedit | |
1820 | real-match-data) | |
1821 | replaced t)) | |
1c4fe319 | 1822 | (setq done t)) |
d99118b0 | 1823 | |
1c4fe319 | 1824 | ((eq def 'delete-and-edit) |
7c1c02ac DK |
1825 | (replace-match "" t t) |
1826 | (setq real-match-data (replace-match-data | |
1827 | nil real-match-data)) | |
1828 | (replace-dehighlight) | |
1829 | (save-excursion (recursive-edit)) | |
1c4fe319 RS |
1830 | (setq replaced t)) |
1831 | ;; Note: we do not need to treat `exit-prefix' | |
1832 | ;; specially here, since we reread | |
1833 | ;; any unrecognized character. | |
1834 | (t | |
1835 | (setq this-command 'mode-exited) | |
1836 | (setq keep-going nil) | |
1837 | (setq unread-command-events | |
1838 | (append (listify-key-sequence key) | |
1839 | unread-command-events)) | |
35d59c0f | 1840 | (setq done t))) |
ccec9764 | 1841 | (when query-replace-lazy-highlight |
ccec9764 JL |
1842 | ;; Force lazy rehighlighting only after replacements |
1843 | (if (not (memq def '(skip backup))) | |
1844 | (setq isearch-lazy-highlight-last-string nil)))) | |
1c4fe319 RS |
1845 | ;; Record previous position for ^ when we move on. |
1846 | ;; Change markers to numbers in the match data | |
1847 | ;; since lots of markers slow down editing. | |
7c1c02ac | 1848 | (push (list (point) replaced |
bace7209 LT |
1849 | ;;; If the replacement has already happened, all we need is the |
1850 | ;;; current match start and end. We could get this with a trivial | |
1851 | ;;; match like | |
1852 | ;;; (save-excursion (goto-char (match-beginning 0)) | |
1853 | ;;; (search-forward (match-string 0)) | |
1854 | ;;; (match-data t)) | |
1855 | ;;; if we really wanted to avoid manually constructing match data. | |
1856 | ;;; Adding current-buffer is necessary so that match-data calls can | |
1857 | ;;; return markers which are appropriate for editing. | |
7c1c02ac DK |
1858 | (if replaced |
1859 | (list | |
1860 | (match-beginning 0) | |
1861 | (match-end 0) | |
1862 | (current-buffer)) | |
1863 | (match-data t))) | |
1864 | stack))))) | |
889617de | 1865 | |
e782e9f2 | 1866 | (replace-dehighlight)) |
4d33492a RS |
1867 | (or unread-command-events |
1868 | (message "Replaced %d occurrence%s" | |
1869 | replace-count | |
1870 | (if (= replace-count 1) "" "s"))) | |
b591f338 | 1871 | (or (and keep-going stack) multi-buffer))) |
698e1804 | 1872 | |
e782e9f2 RS |
1873 | (defvar replace-overlay nil) |
1874 | ||
444697a1 JL |
1875 | (defun replace-highlight (match-beg match-end range-beg range-end |
1876 | string regexp case-fold) | |
ccec9764 JL |
1877 | (if query-replace-highlight |
1878 | (if replace-overlay | |
d532160f RS |
1879 | (move-overlay replace-overlay match-beg match-end (current-buffer)) |
1880 | (setq replace-overlay (make-overlay match-beg match-end)) | |
a46961de | 1881 | (overlay-put replace-overlay 'priority 1001) ;higher than lazy overlays |
ccec9764 | 1882 | (overlay-put replace-overlay 'face 'query-replace))) |
444697a1 JL |
1883 | (if query-replace-lazy-highlight |
1884 | (let ((isearch-string string) | |
1885 | (isearch-regexp regexp) | |
0b60cc09 | 1886 | (search-whitespace-regexp nil) |
444697a1 JL |
1887 | (isearch-case-fold-search case-fold)) |
1888 | (isearch-lazy-highlight-new-loop range-beg range-end)))) | |
ccec9764 | 1889 | |
e782e9f2 | 1890 | (defun replace-dehighlight () |
ccec9764 JL |
1891 | (when replace-overlay |
1892 | (delete-overlay replace-overlay)) | |
1893 | (when query-replace-lazy-highlight | |
444697a1 | 1894 | (lazy-highlight-cleanup lazy-highlight-cleanup) |
ccec9764 | 1895 | (setq isearch-lazy-highlight-last-string nil))) |
e782e9f2 | 1896 | |
6f1df6d9 | 1897 | ;; arch-tag: 16b4cd61-fd40-497b-b86f-b667c4cf88e4 |
c88ab9ce | 1898 | ;;; replace.el ends here |