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