| 1 | ;;; replace.el --- replace commands for Emacs |
| 2 | |
| 3 | ;; Copyright (C) 1985, 86, 87, 92, 94, 96, 1997, 2000, 2001, 2002, |
| 4 | ;; 2003, 2004 Free Software Foundation, Inc. |
| 5 | |
| 6 | ;; Maintainer: FSF |
| 7 | |
| 8 | ;; This file is part of GNU Emacs. |
| 9 | |
| 10 | ;; GNU Emacs is free software; you can redistribute it and/or modify |
| 11 | ;; it under the terms of the GNU General Public License as published by |
| 12 | ;; the Free Software Foundation; either version 2, or (at your option) |
| 13 | ;; any later version. |
| 14 | |
| 15 | ;; GNU Emacs is distributed in the hope that it will be useful, |
| 16 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | ;; GNU General Public License for more details. |
| 19 | |
| 20 | ;; You should have received a copy of the GNU General Public License |
| 21 | ;; along with GNU Emacs; see the file COPYING. If not, write to the |
| 22 | ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 23 | ;; Boston, MA 02111-1307, USA. |
| 24 | |
| 25 | ;;; Commentary: |
| 26 | |
| 27 | ;; This package supplies the string and regular-expression replace functions |
| 28 | ;; documented in the Emacs user's manual. |
| 29 | |
| 30 | ;;; Code: |
| 31 | |
| 32 | (defcustom case-replace t |
| 33 | "*Non-nil means `query-replace' should preserve case in replacements." |
| 34 | :type 'boolean |
| 35 | :group 'matching) |
| 36 | |
| 37 | (defvar query-replace-history nil) |
| 38 | |
| 39 | (defcustom query-replace-interactive nil |
| 40 | "Non-nil means `query-replace' uses the last search string. |
| 41 | That becomes the \"string to replace\". |
| 42 | If value is `initial', the last search string is inserted into |
| 43 | the minibuffer as an initial value for \"string to replace\"." |
| 44 | :type '(choice (const :tag "Off" nil) |
| 45 | (const :tag "Initial content" initial) |
| 46 | (other :tag "Use default value" t)) |
| 47 | :group 'matching) |
| 48 | |
| 49 | (defcustom query-replace-from-history-variable 'query-replace-history |
| 50 | "History list to use for the FROM argument of `query-replace' commands. |
| 51 | The value of this variable should be a symbol; that symbol |
| 52 | is used as a variable to hold a history list for the strings |
| 53 | or patterns to be replaced." |
| 54 | :group 'matching |
| 55 | :type 'symbol |
| 56 | :version "20.3") |
| 57 | |
| 58 | (defcustom query-replace-to-history-variable 'query-replace-history |
| 59 | "History list to use for the TO argument of `query-replace' commands. |
| 60 | The value of this variable should be a symbol; that symbol |
| 61 | is used as a variable to hold a history list for replacement |
| 62 | strings or patterns." |
| 63 | :group 'matching |
| 64 | :type 'symbol |
| 65 | :version "20.3") |
| 66 | |
| 67 | (defcustom query-replace-skip-read-only nil |
| 68 | "*Non-nil means `query-replace' and friends ignore read-only matches." |
| 69 | :type 'boolean |
| 70 | :group 'matching |
| 71 | :version "21.4") |
| 72 | |
| 73 | (defun query-replace-read-args (string regexp-flag &optional noerror) |
| 74 | (unless noerror |
| 75 | (barf-if-buffer-read-only)) |
| 76 | (let (from to) |
| 77 | (if (and query-replace-interactive |
| 78 | (not (eq query-replace-interactive 'initial))) |
| 79 | (setq from (car (if regexp-flag regexp-search-ring search-ring))) |
| 80 | ;; The save-excursion here is in case the user marks and copies |
| 81 | ;; a region in order to specify the minibuffer input. |
| 82 | ;; That should not clobber the region for the query-replace itself. |
| 83 | (save-excursion |
| 84 | (setq from (read-from-minibuffer |
| 85 | (format "%s: " string) |
| 86 | (if (eq query-replace-interactive 'initial) |
| 87 | (car (if regexp-flag regexp-search-ring search-ring))) |
| 88 | nil nil |
| 89 | query-replace-from-history-variable |
| 90 | nil t))) |
| 91 | ;; Warn if user types \n or \t, but don't reject the input. |
| 92 | (and regexp-flag |
| 93 | (string-match "\\(\\`\\|[^\\]\\)\\(\\\\\\\\\\)*\\(\\\\[nt]\\)" from) |
| 94 | (let ((match (match-string 3 from))) |
| 95 | (cond |
| 96 | ((string= match "\\n") |
| 97 | (message "Note: `\\n' here doesn't match a newline; to do that, type C-q C-j instead")) |
| 98 | ((string= match "\\t") |
| 99 | (message "Note: `\\t' here doesn't match a tab; to do that, just type TAB"))) |
| 100 | (sit-for 2)))) |
| 101 | |
| 102 | (save-excursion |
| 103 | (setq to (read-from-minibuffer |
| 104 | (format "%s %s with: " string from) |
| 105 | nil nil nil |
| 106 | query-replace-to-history-variable from t))) |
| 107 | (when (and regexp-flag |
| 108 | (string-match "\\(\\`\\|[^\\]\\)\\(\\\\\\\\\\)*\\\\[,#]" to)) |
| 109 | (let (pos list char) |
| 110 | (while |
| 111 | (progn |
| 112 | (setq pos (match-end 0)) |
| 113 | (push (substring to 0 (- pos 2)) list) |
| 114 | (setq char (aref to (1- pos)) |
| 115 | to (substring to pos)) |
| 116 | (cond ((eq char ?\#) |
| 117 | (push '(number-to-string replace-count) list)) |
| 118 | ((eq char ?\,) |
| 119 | (setq pos (read-from-string to)) |
| 120 | (push `(replace-quote ,(car pos)) list) |
| 121 | (let ((end |
| 122 | ;; Swallow a space after a symbol |
| 123 | ;; if there is a space. |
| 124 | (if (and (or (symbolp (car pos)) |
| 125 | ;; Swallow a space after 'foo |
| 126 | ;; but not after (quote foo). |
| 127 | (and (eq (car-safe (car pos)) 'quote) |
| 128 | (= ?\( (aref to 0)))) |
| 129 | (string-match " " to (cdr pos))) |
| 130 | (1+ (cdr pos)) |
| 131 | (cdr pos)))) |
| 132 | (setq to (substring to end))))) |
| 133 | (string-match "\\(\\`\\|[^\\]\\)\\(\\\\\\\\\\)*\\\\[,#]" to))) |
| 134 | (setq to (nreverse (delete "" (cons to list))))) |
| 135 | (replace-match-string-symbols to) |
| 136 | (setq to (cons 'replace-eval-replacement |
| 137 | (if (> (length to) 1) |
| 138 | (cons 'concat to) |
| 139 | (car to))))) |
| 140 | (list from to current-prefix-arg))) |
| 141 | |
| 142 | (defun query-replace (from-string to-string &optional delimited start end) |
| 143 | "Replace some occurrences of FROM-STRING with TO-STRING. |
| 144 | As each match is found, the user must type a character saying |
| 145 | what to do with it. For directions, type \\[help-command] at that time. |
| 146 | |
| 147 | In Transient Mark mode, if the mark is active, operate on the contents |
| 148 | of the region. Otherwise, operate from point to the end of the buffer. |
| 149 | |
| 150 | If `query-replace-interactive' is non-nil, the last incremental search |
| 151 | string is used as FROM-STRING--you don't have to specify it with the |
| 152 | minibuffer. |
| 153 | |
| 154 | Matching is independent of case if `case-fold-search' is non-nil and |
| 155 | FROM-STRING has no uppercase letters. Replacement transfers the case |
| 156 | pattern of the old text to the new text, if `case-replace' and |
| 157 | `case-fold-search' are non-nil and FROM-STRING has no uppercase |
| 158 | letters. \(Transferring the case pattern means that if the old text |
| 159 | matched is all caps, or capitalized, then its replacement is upcased |
| 160 | or capitalized.) |
| 161 | |
| 162 | Third arg DELIMITED (prefix arg if interactive), if non-nil, means replace |
| 163 | only matches surrounded by word boundaries. |
| 164 | Fourth and fifth arg START and END specify the region to operate on. |
| 165 | |
| 166 | To customize possible responses, change the \"bindings\" in `query-replace-map'." |
| 167 | (interactive (let ((common |
| 168 | (query-replace-read-args "Query replace" nil))) |
| 169 | (list (nth 0 common) (nth 1 common) (nth 2 common) |
| 170 | ;; These are done separately here |
| 171 | ;; so that command-history will record these expressions |
| 172 | ;; rather than the values they had this time. |
| 173 | (if (and transient-mark-mode mark-active) |
| 174 | (region-beginning)) |
| 175 | (if (and transient-mark-mode mark-active) |
| 176 | (region-end))))) |
| 177 | (perform-replace from-string to-string t nil delimited nil nil start end)) |
| 178 | |
| 179 | (define-key esc-map "%" 'query-replace) |
| 180 | |
| 181 | (defun query-replace-regexp (regexp to-string &optional delimited start end) |
| 182 | "Replace some things after point matching REGEXP with TO-STRING. |
| 183 | As each match is found, the user must type a character saying |
| 184 | what to do with it. For directions, type \\[help-command] at that time. |
| 185 | |
| 186 | In Transient Mark mode, if the mark is active, operate on the contents |
| 187 | of the region. Otherwise, operate from point to the end of the buffer. |
| 188 | |
| 189 | If `query-replace-interactive' is non-nil, the last incremental search |
| 190 | regexp is used as REGEXP--you don't have to specify it with the |
| 191 | minibuffer. |
| 192 | |
| 193 | Matching is independent of case if `case-fold-search' is non-nil and |
| 194 | REGEXP has no uppercase letters. Replacement transfers the case |
| 195 | pattern of the old text to the new text, if `case-replace' and |
| 196 | `case-fold-search' are non-nil and REGEXP has no uppercase letters. |
| 197 | \(Transferring the case pattern means that if the old text matched is |
| 198 | all caps, or capitalized, then its replacement is upcased or |
| 199 | capitalized.) |
| 200 | |
| 201 | Third arg DELIMITED (prefix arg if interactive), if non-nil, means replace |
| 202 | only matches surrounded by word boundaries. |
| 203 | Fourth and fifth arg START and END specify the region to operate on. |
| 204 | |
| 205 | In TO-STRING, `\\&' stands for whatever matched the whole of REGEXP, |
| 206 | and `\\=\\N' (where N is a digit) stands for |
| 207 | whatever what matched the Nth `\\(...\\)' in REGEXP. |
| 208 | `\\?' lets you edit the replacement text in the minibuffer |
| 209 | at the given position for each replacement. |
| 210 | |
| 211 | In interactive calls, the replacement text can contain `\\,' |
| 212 | followed by a Lisp expression. Each |
| 213 | replacement evaluates that expression to compute the replacement |
| 214 | string. Inside of that expression, `\\&' is a string denoting the |
| 215 | whole match as a sting, `\\N' for a partial match, `\\#&' and `\\#N' |
| 216 | for the whole or a partial match converted to a number with |
| 217 | `string-to-number', and `\\#' itself for the number of replacements |
| 218 | done so far (starting with zero). |
| 219 | |
| 220 | If the replacement expression is a symbol, write a space after it |
| 221 | to terminate it. One space there, if any, will be discarded. |
| 222 | |
| 223 | When using those Lisp features interactively in the replacement |
| 224 | text, TO-STRING is actually made a list instead of a string. |
| 225 | Use \\[repeat-complex-command] after this command for details." |
| 226 | (interactive |
| 227 | (let ((common |
| 228 | (query-replace-read-args "Query replace regexp" t))) |
| 229 | (list (nth 0 common) (nth 1 common) (nth 2 common) |
| 230 | ;; These are done separately here |
| 231 | ;; so that command-history will record these expressions |
| 232 | ;; rather than the values they had this time. |
| 233 | (if (and transient-mark-mode mark-active) |
| 234 | (region-beginning)) |
| 235 | (if (and transient-mark-mode mark-active) |
| 236 | (region-end))))) |
| 237 | (perform-replace regexp to-string t t delimited nil nil start end)) |
| 238 | |
| 239 | (define-key esc-map [?\C-%] 'query-replace-regexp) |
| 240 | |
| 241 | (defun query-replace-regexp-eval (regexp to-expr &optional delimited start end) |
| 242 | "Replace some things after point matching REGEXP with the result of TO-EXPR. |
| 243 | As each match is found, the user must type a character saying |
| 244 | what to do with it. For directions, type \\[help-command] at that time. |
| 245 | |
| 246 | TO-EXPR is a Lisp expression evaluated to compute each replacement. It may |
| 247 | reference `replace-count' to get the number of replacements already made. |
| 248 | If the result of TO-EXPR is not a string, it is converted to one using |
| 249 | `prin1-to-string' with the NOESCAPE argument (which see). |
| 250 | |
| 251 | For convenience, when entering TO-EXPR interactively, you can use `\\&' or |
| 252 | `\\0' to stand for whatever matched the whole of REGEXP, and `\\N' (where |
| 253 | N is a digit) to stand for whatever matched the Nth `\\(...\\)' in REGEXP. |
| 254 | Use `\\#&' or `\\#N' if you want a number instead of a string. |
| 255 | In interactive use, `\\#' in itself stands for `replace-count'. |
| 256 | |
| 257 | In Transient Mark mode, if the mark is active, operate on the contents |
| 258 | of the region. Otherwise, operate from point to the end of the buffer. |
| 259 | |
| 260 | If `query-replace-interactive' is non-nil, the last incremental search |
| 261 | regexp is used as REGEXP--you don't have to specify it with the |
| 262 | minibuffer. |
| 263 | |
| 264 | Preserves case in each replacement if `case-replace' and `case-fold-search' |
| 265 | are non-nil and REGEXP has no uppercase letters. |
| 266 | |
| 267 | Third arg DELIMITED (prefix arg if interactive), if non-nil, means replace |
| 268 | only matches that are surrounded by word boundaries. |
| 269 | Fourth and fifth arg START and END specify the region to operate on." |
| 270 | (interactive |
| 271 | (let (from to) |
| 272 | (if query-replace-interactive |
| 273 | (setq from (car regexp-search-ring)) |
| 274 | (setq from (read-from-minibuffer "Query replace regexp: " |
| 275 | nil nil nil |
| 276 | query-replace-from-history-variable |
| 277 | nil t))) |
| 278 | (setq to (list (read-from-minibuffer |
| 279 | (format "Query replace regexp %s with eval: " from) |
| 280 | nil nil t query-replace-to-history-variable from t))) |
| 281 | ;; We make TO a list because replace-match-string-symbols requires one, |
| 282 | ;; and the user might enter a single token. |
| 283 | (replace-match-string-symbols to) |
| 284 | (list from (car to) current-prefix-arg |
| 285 | (if (and transient-mark-mode mark-active) |
| 286 | (region-beginning)) |
| 287 | (if (and transient-mark-mode mark-active) |
| 288 | (region-end))))) |
| 289 | (perform-replace regexp (cons 'replace-eval-replacement to-expr) |
| 290 | t 'literal delimited nil nil start end)) |
| 291 | |
| 292 | (defun map-query-replace-regexp (regexp to-strings &optional n start end) |
| 293 | "Replace some matches for REGEXP with various strings, in rotation. |
| 294 | The second argument TO-STRINGS contains the replacement strings, |
| 295 | separated by spaces. Third arg DELIMITED (prefix arg if interactive), |
| 296 | if non-nil, means replace only matches surrounded by word boundaries. |
| 297 | This command works like `query-replace-regexp' except that each |
| 298 | successive replacement uses the next successive replacement string, |
| 299 | wrapping around from the last such string to the first. |
| 300 | |
| 301 | In Transient Mark mode, if the mark is active, operate on the contents |
| 302 | of the region. Otherwise, operate from point to the end of the buffer. |
| 303 | |
| 304 | Non-interactively, TO-STRINGS may be a list of replacement strings. |
| 305 | |
| 306 | If `query-replace-interactive' is non-nil, the last incremental search |
| 307 | regexp is used as REGEXP--you don't have to specify it with the minibuffer. |
| 308 | |
| 309 | A prefix argument N says to use each replacement string N times |
| 310 | before rotating to the next. |
| 311 | Fourth and fifth arg START and END specify the region to operate on." |
| 312 | (interactive |
| 313 | (let (from to) |
| 314 | (setq from (if query-replace-interactive |
| 315 | (car regexp-search-ring) |
| 316 | (read-from-minibuffer "Map query replace (regexp): " |
| 317 | nil nil nil |
| 318 | 'query-replace-history nil t))) |
| 319 | (setq to (read-from-minibuffer |
| 320 | (format "Query replace %s with (space-separated strings): " |
| 321 | from) |
| 322 | nil nil nil |
| 323 | 'query-replace-history from t)) |
| 324 | (list from to |
| 325 | (and current-prefix-arg |
| 326 | (prefix-numeric-value current-prefix-arg)) |
| 327 | (if (and transient-mark-mode mark-active) |
| 328 | (region-beginning)) |
| 329 | (if (and transient-mark-mode mark-active) |
| 330 | (region-end))))) |
| 331 | (let (replacements) |
| 332 | (if (listp to-strings) |
| 333 | (setq replacements to-strings) |
| 334 | (while (/= (length to-strings) 0) |
| 335 | (if (string-match " " to-strings) |
| 336 | (setq replacements |
| 337 | (append replacements |
| 338 | (list (substring to-strings 0 |
| 339 | (string-match " " to-strings)))) |
| 340 | to-strings (substring to-strings |
| 341 | (1+ (string-match " " to-strings)))) |
| 342 | (setq replacements (append replacements (list to-strings)) |
| 343 | to-strings "")))) |
| 344 | (perform-replace regexp replacements t t nil n nil start end))) |
| 345 | |
| 346 | (defun replace-string (from-string to-string &optional delimited start end) |
| 347 | "Replace occurrences of FROM-STRING with TO-STRING. |
| 348 | Preserve case in each match if `case-replace' and `case-fold-search' |
| 349 | are non-nil and FROM-STRING has no uppercase letters. |
| 350 | \(Preserving case means that if the string matched is all caps, or capitalized, |
| 351 | then its replacement is upcased or capitalized.) |
| 352 | |
| 353 | In Transient Mark mode, if the mark is active, operate on the contents |
| 354 | of the region. Otherwise, operate from point to the end of the buffer. |
| 355 | |
| 356 | Third arg DELIMITED (prefix arg if interactive), if non-nil, means replace |
| 357 | only matches surrounded by word boundaries. |
| 358 | Fourth and fifth arg START and END specify the region to operate on. |
| 359 | |
| 360 | If `query-replace-interactive' is non-nil, the last incremental search |
| 361 | string is used as FROM-STRING--you don't have to specify it with the |
| 362 | minibuffer. |
| 363 | |
| 364 | This function is usually the wrong thing to use in a Lisp program. |
| 365 | What you probably want is a loop like this: |
| 366 | (while (search-forward FROM-STRING nil t) |
| 367 | (replace-match TO-STRING nil t)) |
| 368 | which will run faster and will not set the mark or print anything. |
| 369 | \(You may need a more complex loop if FROM-STRING can match the null string |
| 370 | and TO-STRING is also null.)" |
| 371 | (interactive |
| 372 | (let ((common |
| 373 | (query-replace-read-args "Replace string" nil))) |
| 374 | (list (nth 0 common) (nth 1 common) (nth 2 common) |
| 375 | (if (and transient-mark-mode mark-active) |
| 376 | (region-beginning)) |
| 377 | (if (and transient-mark-mode mark-active) |
| 378 | (region-end))))) |
| 379 | (perform-replace from-string to-string nil nil delimited nil nil start end)) |
| 380 | |
| 381 | (defun replace-regexp (regexp to-string &optional delimited start end) |
| 382 | "Replace things after point matching REGEXP with TO-STRING. |
| 383 | Preserve case in each match if `case-replace' and `case-fold-search' |
| 384 | are non-nil and REGEXP has no uppercase letters. |
| 385 | |
| 386 | In Transient Mark mode, if the mark is active, operate on the contents |
| 387 | of the region. Otherwise, operate from point to the end of the buffer. |
| 388 | |
| 389 | Third arg DELIMITED (prefix arg if interactive), if non-nil, means replace |
| 390 | only matches surrounded by word boundaries. |
| 391 | Fourth and fifth arg START and END specify the region to operate on. |
| 392 | |
| 393 | In TO-STRING, `\\&' stands for whatever matched the whole of REGEXP, |
| 394 | and `\\=\\N' (where N is a digit) stands for |
| 395 | whatever what matched the Nth `\\(...\\)' in REGEXP. |
| 396 | `\\?' lets you edit the replacement text in the minibuffer |
| 397 | at the given position for each replacement. |
| 398 | |
| 399 | In interactive calls, the replacement text may contain `\\,' |
| 400 | followed by a Lisp expression used as part of the replacement |
| 401 | text. Inside of that expression, `\\&' is a string denoting the |
| 402 | whole match, `\\N' a partial matches, `\\#&' and `\\#N' the |
| 403 | respective numeric values from `string-to-number', and `\\#' |
| 404 | itself for `replace-count', the number of replacements occured so |
| 405 | far. |
| 406 | |
| 407 | If your Lisp expression is an identifier and the next letter in |
| 408 | the replacement string would be interpreted as part of it, you |
| 409 | can wrap it with an expression like `\\,(or \\#)'. Incidentally, |
| 410 | for this particular case you may also enter `\\#' in the |
| 411 | replacement text directly. |
| 412 | |
| 413 | When using those Lisp features interactively in the replacement |
| 414 | text, TO-STRING is actually made a list instead of a string. |
| 415 | Use \\[repeat-complex-command] after this command for details. |
| 416 | |
| 417 | If `query-replace-interactive' is non-nil, the last incremental search |
| 418 | regexp is used as REGEXP--you don't have to specify it with the minibuffer. |
| 419 | |
| 420 | This function is usually the wrong thing to use in a Lisp program. |
| 421 | What you probably want is a loop like this: |
| 422 | (while (re-search-forward REGEXP nil t) |
| 423 | (replace-match TO-STRING nil nil)) |
| 424 | which will run faster and will not set the mark or print anything." |
| 425 | (interactive |
| 426 | (let ((common |
| 427 | (query-replace-read-args "Replace regexp" t))) |
| 428 | (list (nth 0 common) (nth 1 common) (nth 2 common) |
| 429 | (if (and transient-mark-mode mark-active) |
| 430 | (region-beginning)) |
| 431 | (if (and transient-mark-mode mark-active) |
| 432 | (region-end))))) |
| 433 | (perform-replace regexp to-string nil t delimited nil nil start end)) |
| 434 | |
| 435 | \f |
| 436 | (defvar regexp-history nil |
| 437 | "History list for some commands that read regular expressions.") |
| 438 | |
| 439 | |
| 440 | (defalias 'delete-non-matching-lines 'keep-lines) |
| 441 | (defalias 'delete-matching-lines 'flush-lines) |
| 442 | (defalias 'count-matches 'how-many) |
| 443 | |
| 444 | |
| 445 | (defun keep-lines-read-args (prompt) |
| 446 | "Read arguments for `keep-lines' and friends. |
| 447 | Prompt for a regexp with PROMPT. |
| 448 | Value is a list, (REGEXP)." |
| 449 | (list (read-from-minibuffer prompt nil nil nil |
| 450 | 'regexp-history nil t))) |
| 451 | |
| 452 | (defun keep-lines (regexp &optional rstart rend) |
| 453 | "Delete all lines except those containing matches for REGEXP. |
| 454 | A match split across lines preserves all the lines it lies in. |
| 455 | Applies to all lines after point. |
| 456 | |
| 457 | If REGEXP contains upper case characters (excluding those preceded by `\\'), |
| 458 | the matching is case-sensitive. |
| 459 | |
| 460 | Second and third arg RSTART and REND specify the region to operate on. |
| 461 | |
| 462 | Interactively, in Transient Mark mode when the mark is active, operate |
| 463 | on the contents of the region. Otherwise, operate from point to the |
| 464 | end of the buffer." |
| 465 | |
| 466 | (interactive |
| 467 | (progn |
| 468 | (barf-if-buffer-read-only) |
| 469 | (keep-lines-read-args "Keep lines (containing match for regexp): "))) |
| 470 | (if rstart |
| 471 | (progn |
| 472 | (goto-char (min rstart rend)) |
| 473 | (setq rend (copy-marker (max rstart rend)))) |
| 474 | (if (and transient-mark-mode mark-active) |
| 475 | (setq rstart (region-beginning) |
| 476 | rend (copy-marker (region-end))) |
| 477 | (setq rstart (point) |
| 478 | rend (point-max-marker))) |
| 479 | (goto-char rstart)) |
| 480 | (save-excursion |
| 481 | (or (bolp) (forward-line 1)) |
| 482 | (let ((start (point)) |
| 483 | (case-fold-search (and case-fold-search |
| 484 | (isearch-no-upper-case-p regexp t)))) |
| 485 | (while (< (point) rend) |
| 486 | ;; Start is first char not preserved by previous match. |
| 487 | (if (not (re-search-forward regexp rend 'move)) |
| 488 | (delete-region start rend) |
| 489 | (let ((end (save-excursion (goto-char (match-beginning 0)) |
| 490 | (beginning-of-line) |
| 491 | (point)))) |
| 492 | ;; Now end is first char preserved by the new match. |
| 493 | (if (< start end) |
| 494 | (delete-region start end)))) |
| 495 | |
| 496 | (setq start (save-excursion (forward-line 1) (point))) |
| 497 | ;; If the match was empty, avoid matching again at same place. |
| 498 | (and (< (point) rend) |
| 499 | (= (match-beginning 0) (match-end 0)) |
| 500 | (forward-char 1)))))) |
| 501 | |
| 502 | |
| 503 | (defun flush-lines (regexp &optional rstart rend) |
| 504 | "Delete lines containing matches for REGEXP. |
| 505 | If a match is split across lines, all the lines it lies in are deleted. |
| 506 | Applies to lines after point. |
| 507 | |
| 508 | If REGEXP contains upper case characters (excluding those preceded by `\\'), |
| 509 | the matching is case-sensitive. |
| 510 | |
| 511 | Second and third arg RSTART and REND specify the region to operate on. |
| 512 | |
| 513 | Interactively, in Transient Mark mode when the mark is active, operate |
| 514 | on the contents of the region. Otherwise, operate from point to the |
| 515 | end of the buffer." |
| 516 | |
| 517 | (interactive |
| 518 | (progn |
| 519 | (barf-if-buffer-read-only) |
| 520 | (keep-lines-read-args "Flush lines (containing match for regexp): "))) |
| 521 | (if rstart |
| 522 | (progn |
| 523 | (goto-char (min rstart rend)) |
| 524 | (setq rend (copy-marker (max rstart rend)))) |
| 525 | (if (and transient-mark-mode mark-active) |
| 526 | (setq rstart (region-beginning) |
| 527 | rend (copy-marker (region-end))) |
| 528 | (setq rstart (point) |
| 529 | rend (point-max-marker))) |
| 530 | (goto-char rstart)) |
| 531 | (let ((case-fold-search (and case-fold-search |
| 532 | (isearch-no-upper-case-p regexp t)))) |
| 533 | (save-excursion |
| 534 | (while (and (< (point) rend) |
| 535 | (re-search-forward regexp rend t)) |
| 536 | (delete-region (save-excursion (goto-char (match-beginning 0)) |
| 537 | (beginning-of-line) |
| 538 | (point)) |
| 539 | (progn (forward-line 1) (point))))))) |
| 540 | |
| 541 | |
| 542 | (defun how-many (regexp &optional rstart rend) |
| 543 | "Print number of matches for REGEXP following point. |
| 544 | |
| 545 | If REGEXP contains upper case characters (excluding those preceded by `\\'), |
| 546 | the matching is case-sensitive. |
| 547 | |
| 548 | Second and third arg RSTART and REND specify the region to operate on. |
| 549 | |
| 550 | Interactively, in Transient Mark mode when the mark is active, operate |
| 551 | on the contents of the region. Otherwise, operate from point to the |
| 552 | end of the buffer." |
| 553 | |
| 554 | (interactive |
| 555 | (keep-lines-read-args "How many matches for (regexp): ")) |
| 556 | (save-excursion |
| 557 | (if rstart |
| 558 | (goto-char (min rstart rend)) |
| 559 | (if (and transient-mark-mode mark-active) |
| 560 | (setq rstart (region-beginning) |
| 561 | rend (copy-marker (region-end))) |
| 562 | (setq rstart (point) |
| 563 | rend (point-max-marker))) |
| 564 | (goto-char rstart)) |
| 565 | (let ((count 0) |
| 566 | opoint |
| 567 | (case-fold-search (and case-fold-search |
| 568 | (isearch-no-upper-case-p regexp t)))) |
| 569 | (while (and (< (point) rend) |
| 570 | (progn (setq opoint (point)) |
| 571 | (re-search-forward regexp rend t))) |
| 572 | (if (= opoint (point)) |
| 573 | (forward-char 1) |
| 574 | (setq count (1+ count)))) |
| 575 | (message "%d occurrences" count)))) |
| 576 | |
| 577 | \f |
| 578 | (defvar occur-mode-map |
| 579 | (let ((map (make-sparse-keymap))) |
| 580 | (define-key map [mouse-2] 'occur-mode-mouse-goto) |
| 581 | (define-key map "\C-c\C-c" 'occur-mode-goto-occurrence) |
| 582 | (define-key map "\C-m" 'occur-mode-goto-occurrence) |
| 583 | (define-key map "o" 'occur-mode-goto-occurrence-other-window) |
| 584 | (define-key map "\C-o" 'occur-mode-display-occurrence) |
| 585 | (define-key map "\M-n" 'occur-next) |
| 586 | (define-key map "\M-p" 'occur-prev) |
| 587 | (define-key map "r" 'occur-rename-buffer) |
| 588 | (define-key map "c" 'clone-buffer) |
| 589 | (define-key map "g" 'revert-buffer) |
| 590 | (define-key map "q" 'quit-window) |
| 591 | (define-key map "z" 'kill-this-buffer) |
| 592 | map) |
| 593 | "Keymap for `occur-mode'.") |
| 594 | |
| 595 | (defvar occur-revert-arguments nil |
| 596 | "Arguments to pass to `occur-1' to revert an Occur mode buffer. |
| 597 | See `occur-revert-function'.") |
| 598 | |
| 599 | (defcustom occur-mode-hook '(turn-on-font-lock) |
| 600 | "Hook run when entering Occur mode." |
| 601 | :type 'hook |
| 602 | :group 'matching) |
| 603 | |
| 604 | (defcustom occur-hook nil |
| 605 | "Hook run when `occur' is called." |
| 606 | :type 'hook |
| 607 | :group 'matching) |
| 608 | |
| 609 | (put 'occur-mode 'mode-class 'special) |
| 610 | (defun occur-mode () |
| 611 | "Major mode for output from \\[occur]. |
| 612 | \\<occur-mode-map>Move point to one of the items in this buffer, then use |
| 613 | \\[occur-mode-goto-occurrence] to go to the occurrence that the item refers to. |
| 614 | Alternatively, click \\[occur-mode-mouse-goto] on an item to go to it. |
| 615 | |
| 616 | \\{occur-mode-map}" |
| 617 | (interactive) |
| 618 | (kill-all-local-variables) |
| 619 | (use-local-map occur-mode-map) |
| 620 | (setq major-mode 'occur-mode) |
| 621 | (setq mode-name "Occur") |
| 622 | (set (make-local-variable 'revert-buffer-function) 'occur-revert-function) |
| 623 | (make-local-variable 'occur-revert-arguments) |
| 624 | (add-hook 'change-major-mode-hook 'font-lock-defontify nil t) |
| 625 | (setq next-error-function 'occur-next-error) |
| 626 | (run-hooks 'occur-mode-hook)) |
| 627 | |
| 628 | (defun occur-revert-function (ignore1 ignore2) |
| 629 | "Handle `revert-buffer' for Occur mode buffers." |
| 630 | (apply 'occur-1 (append occur-revert-arguments (list (buffer-name))))) |
| 631 | |
| 632 | (defun occur-mode-mouse-goto (event) |
| 633 | "In Occur mode, go to the occurrence whose line you click on." |
| 634 | (interactive "e") |
| 635 | (let (pos) |
| 636 | (save-excursion |
| 637 | (set-buffer (window-buffer (posn-window (event-end event)))) |
| 638 | (save-excursion |
| 639 | (goto-char (posn-point (event-end event))) |
| 640 | (setq pos (occur-mode-find-occurrence)))) |
| 641 | (pop-to-buffer (marker-buffer pos)) |
| 642 | (goto-char pos))) |
| 643 | |
| 644 | (defun occur-mode-find-occurrence () |
| 645 | (let ((pos (get-text-property (point) 'occur-target))) |
| 646 | (unless pos |
| 647 | (error "No occurrence on this line")) |
| 648 | (unless (buffer-live-p (marker-buffer pos)) |
| 649 | (error "Buffer for this occurrence was killed")) |
| 650 | pos)) |
| 651 | |
| 652 | (defun occur-mode-goto-occurrence () |
| 653 | "Go to the occurrence the current line describes." |
| 654 | (interactive) |
| 655 | (let ((pos (occur-mode-find-occurrence))) |
| 656 | (pop-to-buffer (marker-buffer pos)) |
| 657 | (goto-char pos))) |
| 658 | |
| 659 | (defun occur-mode-goto-occurrence-other-window () |
| 660 | "Go to the occurrence the current line describes, in another window." |
| 661 | (interactive) |
| 662 | (let ((pos (occur-mode-find-occurrence))) |
| 663 | (switch-to-buffer-other-window (marker-buffer pos)) |
| 664 | (goto-char pos))) |
| 665 | |
| 666 | (defun occur-mode-display-occurrence () |
| 667 | "Display in another window the occurrence the current line describes." |
| 668 | (interactive) |
| 669 | (let ((pos (occur-mode-find-occurrence)) |
| 670 | window |
| 671 | ;; Bind these to ensure `display-buffer' puts it in another window. |
| 672 | same-window-buffer-names |
| 673 | same-window-regexps) |
| 674 | (setq window (display-buffer (marker-buffer pos))) |
| 675 | ;; This is the way to set point in the proper window. |
| 676 | (save-selected-window |
| 677 | (select-window window) |
| 678 | (goto-char pos)))) |
| 679 | |
| 680 | (defun occur-find-match (n search message) |
| 681 | (if (not n) (setq n 1)) |
| 682 | (let ((r)) |
| 683 | (while (> n 0) |
| 684 | (setq r (funcall search (point) 'occur-match)) |
| 685 | (and r |
| 686 | (get-text-property r 'occur-match) |
| 687 | (setq r (funcall search r 'occur-match))) |
| 688 | (if r |
| 689 | (goto-char r) |
| 690 | (error message)) |
| 691 | (setq n (1- n))))) |
| 692 | |
| 693 | (defun occur-next (&optional n) |
| 694 | "Move to the Nth (default 1) next match in an Occur mode buffer." |
| 695 | (interactive "p") |
| 696 | (occur-find-match n #'next-single-property-change "No more matches")) |
| 697 | |
| 698 | (defun occur-prev (&optional n) |
| 699 | "Move to the Nth (default 1) previous match in an Occur mode buffer." |
| 700 | (interactive "p") |
| 701 | (occur-find-match n #'previous-single-property-change "No earlier matches")) |
| 702 | |
| 703 | (defun occur-next-error (&optional argp reset) |
| 704 | "Move to the Nth (default 1) next match in an Occur mode buffer. |
| 705 | Compatibility function for \\[next-error] invocations." |
| 706 | (interactive "p") |
| 707 | (when reset |
| 708 | (occur-find-match 0 #'next-single-property-change "No first match")) |
| 709 | (occur-find-match |
| 710 | (prefix-numeric-value argp) |
| 711 | (if (> 0 (prefix-numeric-value argp)) |
| 712 | #'previous-single-property-change |
| 713 | #'next-single-property-change) |
| 714 | "No more matches") |
| 715 | (occur-mode-goto-occurrence)) |
| 716 | |
| 717 | \f |
| 718 | (defcustom list-matching-lines-default-context-lines 0 |
| 719 | "*Default number of context lines included around `list-matching-lines' matches. |
| 720 | A negative number means to include that many lines before the match. |
| 721 | A positive number means to include that many lines both before and after." |
| 722 | :type 'integer |
| 723 | :group 'matching) |
| 724 | |
| 725 | (defalias 'list-matching-lines 'occur) |
| 726 | |
| 727 | (defcustom list-matching-lines-face 'bold |
| 728 | "*Face used by \\[list-matching-lines] to show the text that matches. |
| 729 | If the value is nil, don't highlight the matching portions specially." |
| 730 | :type 'face |
| 731 | :group 'matching) |
| 732 | |
| 733 | (defcustom list-matching-lines-buffer-name-face 'underline |
| 734 | "*Face used by \\[list-matching-lines] to show the names of buffers. |
| 735 | If the value is nil, don't highlight the buffer names specially." |
| 736 | :type 'face |
| 737 | :group 'matching) |
| 738 | |
| 739 | (defun occur-accumulate-lines (count &optional no-props) |
| 740 | (save-excursion |
| 741 | (let ((forwardp (> count 0)) |
| 742 | (result nil)) |
| 743 | (while (not (or (zerop count) |
| 744 | (if forwardp |
| 745 | (eobp) |
| 746 | (bobp)))) |
| 747 | (setq count (+ count (if forwardp -1 1))) |
| 748 | (push |
| 749 | (funcall (if no-props |
| 750 | #'buffer-substring-no-properties |
| 751 | #'buffer-substring) |
| 752 | (line-beginning-position) |
| 753 | (line-end-position)) |
| 754 | result) |
| 755 | (forward-line (if forwardp 1 -1))) |
| 756 | (nreverse result)))) |
| 757 | |
| 758 | (defun occur-read-primary-args () |
| 759 | (list (let* ((default (car regexp-history)) |
| 760 | (input |
| 761 | (read-from-minibuffer |
| 762 | (if default |
| 763 | (format "List lines matching regexp (default `%s'): " |
| 764 | default) |
| 765 | "List lines matching regexp: ") |
| 766 | nil |
| 767 | nil |
| 768 | nil |
| 769 | 'regexp-history))) |
| 770 | (if (equal input "") |
| 771 | default |
| 772 | input)) |
| 773 | (when current-prefix-arg |
| 774 | (prefix-numeric-value current-prefix-arg)))) |
| 775 | |
| 776 | (defun occur-rename-buffer (&optional unique-p) |
| 777 | "Rename the current *Occur* buffer to *Occur: original-buffer-name*. |
| 778 | Here `original-buffer-name' is the buffer name were occur was originally run. |
| 779 | When given the prefix argument, the renaming will not clobber the existing |
| 780 | buffer(s) of that name, but use `generate-new-buffer-name' instead. |
| 781 | You can add this to `occur-hook' if you always want a separate *Occur* |
| 782 | buffer for each buffer where you invoke `occur'." |
| 783 | (interactive "P") |
| 784 | (with-current-buffer |
| 785 | (if (eq major-mode 'occur-mode) (current-buffer) (get-buffer "*Occur*")) |
| 786 | (rename-buffer (concat "*Occur: " |
| 787 | (mapconcat #'buffer-name |
| 788 | (car (cddr occur-revert-arguments)) "/") |
| 789 | "*") |
| 790 | unique-p))) |
| 791 | |
| 792 | (defun occur (regexp &optional nlines) |
| 793 | "Show all lines in the current buffer containing a match for REGEXP. |
| 794 | |
| 795 | If a match spreads across multiple lines, all those lines are shown. |
| 796 | |
| 797 | Each line is displayed with NLINES lines before and after, or -NLINES |
| 798 | before if NLINES is negative. |
| 799 | NLINES defaults to `list-matching-lines-default-context-lines'. |
| 800 | Interactively it is the prefix arg. |
| 801 | |
| 802 | The lines are shown in a buffer named `*Occur*'. |
| 803 | It serves as a menu to find any of the occurrences in this buffer. |
| 804 | \\<occur-mode-map>\\[describe-mode] in that buffer will explain how. |
| 805 | |
| 806 | If REGEXP contains upper case characters (excluding those preceded by `\\'), |
| 807 | the matching is case-sensitive." |
| 808 | (interactive (occur-read-primary-args)) |
| 809 | (occur-1 regexp nlines (list (current-buffer)))) |
| 810 | |
| 811 | (defun multi-occur (bufs regexp &optional nlines) |
| 812 | "Show all lines in buffers BUFS containing a match for REGEXP. |
| 813 | This function acts on multiple buffers; otherwise, it is exactly like |
| 814 | `occur'." |
| 815 | (interactive |
| 816 | (cons |
| 817 | (let* ((bufs (list (read-buffer "First buffer to search: " |
| 818 | (current-buffer) t))) |
| 819 | (buf nil) |
| 820 | (ido-ignore-item-temp-list bufs)) |
| 821 | (while (not (string-equal |
| 822 | (setq buf (read-buffer |
| 823 | (if (eq read-buffer-function 'ido-read-buffer) |
| 824 | "Next buffer to search (C-j to end): " |
| 825 | "Next buffer to search (RET to end): ") |
| 826 | nil t)) |
| 827 | "")) |
| 828 | (add-to-list 'bufs buf) |
| 829 | (setq ido-ignore-item-temp-list bufs)) |
| 830 | (nreverse (mapcar #'get-buffer bufs))) |
| 831 | (occur-read-primary-args))) |
| 832 | (occur-1 regexp nlines bufs)) |
| 833 | |
| 834 | (defun multi-occur-by-filename-regexp (bufregexp regexp &optional nlines) |
| 835 | "Show all lines matching REGEXP in buffers named by BUFREGEXP. |
| 836 | See also `multi-occur'." |
| 837 | (interactive |
| 838 | (cons |
| 839 | (let* ((default (car regexp-history)) |
| 840 | (input |
| 841 | (read-from-minibuffer |
| 842 | "List lines in buffers whose filename matches regexp: " |
| 843 | nil |
| 844 | nil |
| 845 | nil |
| 846 | 'regexp-history))) |
| 847 | (if (equal input "") |
| 848 | default |
| 849 | input)) |
| 850 | (occur-read-primary-args))) |
| 851 | (when bufregexp |
| 852 | (occur-1 regexp nlines |
| 853 | (delq nil |
| 854 | (mapcar (lambda (buf) |
| 855 | (when (and (buffer-file-name buf) |
| 856 | (string-match bufregexp |
| 857 | (buffer-file-name buf))) |
| 858 | buf)) |
| 859 | (buffer-list)))))) |
| 860 | |
| 861 | (defun occur-1 (regexp nlines bufs &optional buf-name) |
| 862 | (unless buf-name |
| 863 | (setq buf-name "*Occur*")) |
| 864 | (let ((occur-buf (get-buffer-create buf-name)) |
| 865 | (made-temp-buf nil) |
| 866 | (active-bufs (delq nil (mapcar #'(lambda (buf) |
| 867 | (when (buffer-live-p buf) buf)) |
| 868 | bufs)))) |
| 869 | ;; Handle the case where one of the buffers we're searching is the |
| 870 | ;; *Occur* buffer itself. |
| 871 | (when (memq occur-buf bufs) |
| 872 | (setq occur-buf (with-current-buffer occur-buf |
| 873 | (clone-buffer "*Occur-temp*")) |
| 874 | made-temp-buf t)) |
| 875 | (with-current-buffer occur-buf |
| 876 | (setq buffer-read-only nil) |
| 877 | (occur-mode) |
| 878 | (erase-buffer) |
| 879 | (let ((count (occur-engine |
| 880 | regexp active-bufs occur-buf |
| 881 | (or nlines list-matching-lines-default-context-lines) |
| 882 | (and case-fold-search |
| 883 | (isearch-no-upper-case-p regexp t)) |
| 884 | list-matching-lines-buffer-name-face |
| 885 | nil list-matching-lines-face nil))) |
| 886 | (let* ((bufcount (length active-bufs)) |
| 887 | (diff (- (length bufs) bufcount))) |
| 888 | (message "Searched %d buffer%s%s; %s match%s for `%s'" |
| 889 | bufcount (if (= bufcount 1) "" "s") |
| 890 | (if (zerop diff) "" (format " (%d killed)" diff)) |
| 891 | (if (zerop count) "no" (format "%d" count)) |
| 892 | (if (= count 1) "" "es") |
| 893 | regexp)) |
| 894 | ;; If we had to make a temporary buffer, make it the *Occur* |
| 895 | ;; buffer now. |
| 896 | (when made-temp-buf |
| 897 | (with-current-buffer (get-buffer buf-name) |
| 898 | (kill-buffer (current-buffer))) |
| 899 | (rename-buffer buf-name)) |
| 900 | (setq occur-revert-arguments (list regexp nlines bufs) |
| 901 | buffer-read-only t) |
| 902 | (if (> count 0) |
| 903 | (progn |
| 904 | (display-buffer occur-buf) |
| 905 | (setq next-error-last-buffer occur-buf)) |
| 906 | (kill-buffer occur-buf))) |
| 907 | (run-hooks 'occur-hook)))) |
| 908 | |
| 909 | (defun occur-engine-add-prefix (lines) |
| 910 | (mapcar |
| 911 | #'(lambda (line) |
| 912 | (concat " :" line "\n")) |
| 913 | lines)) |
| 914 | |
| 915 | (defun occur-engine (regexp buffers out-buf nlines case-fold-search |
| 916 | title-face prefix-face match-face keep-props) |
| 917 | (with-current-buffer out-buf |
| 918 | (setq buffer-read-only nil) |
| 919 | (let ((globalcount 0) |
| 920 | (coding nil)) |
| 921 | ;; Map over all the buffers |
| 922 | (dolist (buf buffers) |
| 923 | (when (buffer-live-p buf) |
| 924 | (let ((matches 0) ;; count of matched lines |
| 925 | (lines 1) ;; line count |
| 926 | (matchbeg 0) |
| 927 | (matchend 0) |
| 928 | (origpt nil) |
| 929 | (begpt nil) |
| 930 | (endpt nil) |
| 931 | (marker nil) |
| 932 | (curstring "") |
| 933 | (headerpt (with-current-buffer out-buf (point)))) |
| 934 | (save-excursion |
| 935 | (set-buffer buf) |
| 936 | (or coding |
| 937 | ;; Set CODING only if the current buffer locally |
| 938 | ;; binds buffer-file-coding-system. |
| 939 | (not (local-variable-p 'buffer-file-coding-system)) |
| 940 | (setq coding buffer-file-coding-system)) |
| 941 | (save-excursion |
| 942 | (goto-char (point-min)) ;; begin searching in the buffer |
| 943 | (while (not (eobp)) |
| 944 | (setq origpt (point)) |
| 945 | (when (setq endpt (re-search-forward regexp nil t)) |
| 946 | (setq matches (1+ matches)) ;; increment match count |
| 947 | (setq matchbeg (match-beginning 0) |
| 948 | matchend (match-end 0)) |
| 949 | (setq begpt (save-excursion |
| 950 | (goto-char matchbeg) |
| 951 | (line-beginning-position))) |
| 952 | (setq lines (+ lines (1- (count-lines origpt endpt)))) |
| 953 | (setq marker (make-marker)) |
| 954 | (set-marker marker matchbeg) |
| 955 | (setq curstring (buffer-substring begpt |
| 956 | (line-end-position))) |
| 957 | ;; Depropertize the string, and maybe |
| 958 | ;; highlight the matches |
| 959 | (let ((len (length curstring)) |
| 960 | (start 0)) |
| 961 | (unless keep-props |
| 962 | (set-text-properties 0 len nil curstring)) |
| 963 | (while (and (< start len) |
| 964 | (string-match regexp curstring start)) |
| 965 | (add-text-properties (match-beginning 0) |
| 966 | (match-end 0) |
| 967 | (append |
| 968 | `(occur-match t) |
| 969 | (when match-face |
| 970 | `(font-lock-face ,match-face))) |
| 971 | curstring) |
| 972 | (setq start (match-end 0)))) |
| 973 | ;; Generate the string to insert for this match |
| 974 | (let* ((out-line |
| 975 | (concat |
| 976 | ;; Using 7 digits aligns tabs properly. |
| 977 | (apply #'propertize (format "%7d:" lines) |
| 978 | (append |
| 979 | (when prefix-face |
| 980 | `(font-lock-face prefix-face)) |
| 981 | '(occur-prefix t))) |
| 982 | curstring |
| 983 | "\n")) |
| 984 | (data |
| 985 | (if (= nlines 0) |
| 986 | ;; The simple display style |
| 987 | out-line |
| 988 | ;; The complex multi-line display |
| 989 | ;; style. Generate a list of lines, |
| 990 | ;; concatenate them all together. |
| 991 | (apply #'concat |
| 992 | (nconc |
| 993 | (occur-engine-add-prefix (nreverse (cdr (occur-accumulate-lines (- (1+ nlines)) keep-props)))) |
| 994 | (list out-line) |
| 995 | (occur-engine-add-prefix (cdr (occur-accumulate-lines (1+ nlines) keep-props)))))))) |
| 996 | ;; Actually insert the match display data |
| 997 | (with-current-buffer out-buf |
| 998 | (let ((beg (point)) |
| 999 | (end (progn (insert data) (point)))) |
| 1000 | (unless (= nlines 0) |
| 1001 | (insert "-------\n")) |
| 1002 | (add-text-properties |
| 1003 | beg end |
| 1004 | `(occur-target ,marker help-echo "mouse-2: go to this occurrence")) |
| 1005 | ;; We don't put `mouse-face' on the newline, |
| 1006 | ;; because that loses. |
| 1007 | (add-text-properties beg (1- end) '(mouse-face highlight))))) |
| 1008 | (goto-char endpt)) |
| 1009 | (if endpt |
| 1010 | (progn |
| 1011 | (setq lines (1+ lines)) |
| 1012 | ;; On to the next match... |
| 1013 | (forward-line 1)) |
| 1014 | (goto-char (point-max)))))) |
| 1015 | (when (not (zerop matches)) ;; is the count zero? |
| 1016 | (setq globalcount (+ globalcount matches)) |
| 1017 | (with-current-buffer out-buf |
| 1018 | (goto-char headerpt) |
| 1019 | (let ((beg (point)) |
| 1020 | end) |
| 1021 | (insert (format "%d match%s for \"%s\" in buffer: %s\n" |
| 1022 | matches (if (= matches 1) "" "es") |
| 1023 | regexp (buffer-name buf))) |
| 1024 | (setq end (point)) |
| 1025 | (add-text-properties beg end |
| 1026 | (append |
| 1027 | (when title-face |
| 1028 | `(font-lock-face ,title-face)) |
| 1029 | `(occur-title ,buf)))) |
| 1030 | (goto-char (point-min))))))) |
| 1031 | (if coding |
| 1032 | ;; CODING is buffer-file-coding-system of the first buffer |
| 1033 | ;; that locally binds it. Let's use it also for the output |
| 1034 | ;; buffer. |
| 1035 | (set-buffer-file-coding-system coding)) |
| 1036 | ;; Return the number of matches |
| 1037 | globalcount))) |
| 1038 | |
| 1039 | \f |
| 1040 | ;; It would be nice to use \\[...], but there is no reasonable way |
| 1041 | ;; to make that display both SPC and Y. |
| 1042 | (defconst query-replace-help |
| 1043 | "Type Space or `y' to replace one match, Delete or `n' to skip to next, |
| 1044 | RET or `q' to exit, Period to replace one match and exit, |
| 1045 | Comma to replace but not move point immediately, |
| 1046 | C-r to enter recursive edit (\\[exit-recursive-edit] to get out again), |
| 1047 | C-w to delete match and recursive edit, |
| 1048 | C-l to clear the screen, redisplay, and offer same replacement again, |
| 1049 | ! to replace all remaining matches with no more questions, |
| 1050 | ^ to move point back to previous match, |
| 1051 | E to edit the replacement string" |
| 1052 | "Help message while in `query-replace'.") |
| 1053 | |
| 1054 | (defvar query-replace-map (make-sparse-keymap) |
| 1055 | "Keymap that defines the responses to questions in `query-replace'. |
| 1056 | The \"bindings\" in this map are not commands; they are answers. |
| 1057 | The valid answers include `act', `skip', `act-and-show', |
| 1058 | `exit', `act-and-exit', `edit', `delete-and-edit', `recenter', |
| 1059 | `automatic', `backup', `exit-prefix', and `help'.") |
| 1060 | |
| 1061 | (define-key query-replace-map " " 'act) |
| 1062 | (define-key query-replace-map "\d" 'skip) |
| 1063 | (define-key query-replace-map [delete] 'skip) |
| 1064 | (define-key query-replace-map [backspace] 'skip) |
| 1065 | (define-key query-replace-map "y" 'act) |
| 1066 | (define-key query-replace-map "n" 'skip) |
| 1067 | (define-key query-replace-map "Y" 'act) |
| 1068 | (define-key query-replace-map "N" 'skip) |
| 1069 | (define-key query-replace-map "e" 'edit-replacement) |
| 1070 | (define-key query-replace-map "E" 'edit-replacement) |
| 1071 | (define-key query-replace-map "," 'act-and-show) |
| 1072 | (define-key query-replace-map "q" 'exit) |
| 1073 | (define-key query-replace-map "\r" 'exit) |
| 1074 | (define-key query-replace-map [return] 'exit) |
| 1075 | (define-key query-replace-map "." 'act-and-exit) |
| 1076 | (define-key query-replace-map "\C-r" 'edit) |
| 1077 | (define-key query-replace-map "\C-w" 'delete-and-edit) |
| 1078 | (define-key query-replace-map "\C-l" 'recenter) |
| 1079 | (define-key query-replace-map "!" 'automatic) |
| 1080 | (define-key query-replace-map "^" 'backup) |
| 1081 | (define-key query-replace-map "\C-h" 'help) |
| 1082 | (define-key query-replace-map [f1] 'help) |
| 1083 | (define-key query-replace-map [help] 'help) |
| 1084 | (define-key query-replace-map "?" 'help) |
| 1085 | (define-key query-replace-map "\C-g" 'quit) |
| 1086 | (define-key query-replace-map "\C-]" 'quit) |
| 1087 | (define-key query-replace-map "\e" 'exit-prefix) |
| 1088 | (define-key query-replace-map [escape] 'exit-prefix) |
| 1089 | |
| 1090 | (defun replace-match-string-symbols (n) |
| 1091 | "Process a list (and any sub-lists), expanding certain symbols. |
| 1092 | Symbol Expands To |
| 1093 | N (match-string N) (where N is a string of digits) |
| 1094 | #N (string-to-number (match-string N)) |
| 1095 | & (match-string 0) |
| 1096 | #& (string-to-number (match-string 0)) |
| 1097 | # replace-count |
| 1098 | |
| 1099 | Note that these symbols must be preceeded by a backslash in order to |
| 1100 | type them." |
| 1101 | (while n |
| 1102 | (cond |
| 1103 | ((consp (car n)) |
| 1104 | (replace-match-string-symbols (car n))) ;Process sub-list |
| 1105 | ((symbolp (car n)) |
| 1106 | (let ((name (symbol-name (car n)))) |
| 1107 | (cond |
| 1108 | ((string-match "^[0-9]+$" name) |
| 1109 | (setcar n (list 'match-string (string-to-number name)))) |
| 1110 | ((string-match "^#[0-9]+$" name) |
| 1111 | (setcar n (list 'string-to-number |
| 1112 | (list 'match-string |
| 1113 | (string-to-number (substring name 1)))))) |
| 1114 | ((string= "&" name) |
| 1115 | (setcar n '(match-string 0))) |
| 1116 | ((string= "#&" name) |
| 1117 | (setcar n '(string-to-number (match-string 0)))) |
| 1118 | ((string= "#" name) |
| 1119 | (setcar n 'replace-count)))))) |
| 1120 | (setq n (cdr n)))) |
| 1121 | |
| 1122 | (defun replace-eval-replacement (expression replace-count) |
| 1123 | (let ((replacement (eval expression))) |
| 1124 | (if (stringp replacement) |
| 1125 | replacement |
| 1126 | (prin1-to-string replacement t)))) |
| 1127 | |
| 1128 | (defun replace-quote (replacement) |
| 1129 | "Quote a replacement string. |
| 1130 | This just doubles all backslashes in REPLACEMENT and |
| 1131 | returns the resulting string. If REPLACEMENT is not |
| 1132 | a string, it is first passed through `prin1-to-string' |
| 1133 | with the `noescape' argument set. |
| 1134 | |
| 1135 | `match-data' is preserved across the call." |
| 1136 | (save-match-data |
| 1137 | (replace-regexp-in-string "\\\\" "\\\\" |
| 1138 | (if (stringp replacement) |
| 1139 | replacement |
| 1140 | (prin1-to-string replacement t)) |
| 1141 | t t))) |
| 1142 | |
| 1143 | (defun replace-loop-through-replacements (data replace-count) |
| 1144 | ;; DATA is a vector contaning the following values: |
| 1145 | ;; 0 next-rotate-count |
| 1146 | ;; 1 repeat-count |
| 1147 | ;; 2 next-replacement |
| 1148 | ;; 3 replacements |
| 1149 | (if (= (aref data 0) replace-count) |
| 1150 | (progn |
| 1151 | (aset data 0 (+ replace-count (aref data 1))) |
| 1152 | (let ((next (cdr (aref data 2)))) |
| 1153 | (aset data 2 (if (consp next) next (aref data 3)))))) |
| 1154 | (car (aref data 2))) |
| 1155 | |
| 1156 | (defun replace-match-data (integers reuse &optional new) |
| 1157 | "Like `match-data', but markers in REUSE get invalidated. |
| 1158 | If NEW is non-NIL, it is set and returned instead of fresh data, |
| 1159 | but coerced to the correct value of INTEGERS." |
| 1160 | (or (and new |
| 1161 | (progn |
| 1162 | (set-match-data new) |
| 1163 | (and (eq new reuse) |
| 1164 | (eq (null integers) (markerp (car reuse))) |
| 1165 | new))) |
| 1166 | (match-data integers |
| 1167 | (prog1 reuse |
| 1168 | (while reuse |
| 1169 | (if (markerp (car reuse)) |
| 1170 | (set-marker (car reuse) nil)) |
| 1171 | (setq reuse (cdr reuse))))))) |
| 1172 | |
| 1173 | (defun replace-match-maybe-edit (newtext fixedcase literal noedit match-data) |
| 1174 | "Make a replacement with `replace-match', editing `\\?'. |
| 1175 | NEXTEXT, FIXEDCASE, LITERAL are just passed on. If NOEDIT is true, no |
| 1176 | check for `\\?' is made to save time. MATCH-DATA is used for the |
| 1177 | replacement. In case editing is done, it is changed to use markers. |
| 1178 | |
| 1179 | The return value is non-NIL if there has been no `\\?' or NOEDIT was |
| 1180 | passed in. If LITERAL is set, no checking is done, anyway." |
| 1181 | (unless (or literal noedit) |
| 1182 | (setq noedit t) |
| 1183 | (while (string-match "\\(\\`\\|[^\\]\\)\\(\\\\\\\\\\)*\\(\\\\\\?\\)" |
| 1184 | newtext) |
| 1185 | (setq newtext |
| 1186 | (read-input "Edit replacement string: " |
| 1187 | (prog1 |
| 1188 | (cons |
| 1189 | (replace-match "" t t newtext 3) |
| 1190 | (1+ (match-beginning 3))) |
| 1191 | (setq match-data |
| 1192 | (replace-match-data |
| 1193 | nil match-data match-data)))) |
| 1194 | noedit nil))) |
| 1195 | (set-match-data match-data) |
| 1196 | (replace-match newtext fixedcase literal) |
| 1197 | noedit) |
| 1198 | |
| 1199 | (defun perform-replace (from-string replacements |
| 1200 | query-flag regexp-flag delimited-flag |
| 1201 | &optional repeat-count map start end) |
| 1202 | "Subroutine of `query-replace'. Its complexity handles interactive queries. |
| 1203 | Don't use this in your own program unless you want to query and set the mark |
| 1204 | just as `query-replace' does. Instead, write a simple loop like this: |
| 1205 | |
| 1206 | (while (re-search-forward \"foo[ \\t]+bar\" nil t) |
| 1207 | (replace-match \"foobar\" nil nil)) |
| 1208 | |
| 1209 | which will run faster and probably do exactly what you want. Please |
| 1210 | see the documentation of `replace-match' to find out how to simulate |
| 1211 | `case-replace'. |
| 1212 | |
| 1213 | This function returns nil if and only if there were no matches to |
| 1214 | make, or the user didn't cancel the call." |
| 1215 | (or map (setq map query-replace-map)) |
| 1216 | (and query-flag minibuffer-auto-raise |
| 1217 | (raise-frame (window-frame (minibuffer-window)))) |
| 1218 | (let ((nocasify (not (and case-fold-search case-replace |
| 1219 | (string-equal from-string |
| 1220 | (downcase from-string))))) |
| 1221 | (case-fold-search (and case-fold-search |
| 1222 | (string-equal from-string |
| 1223 | (downcase from-string)))) |
| 1224 | (literal (or (not regexp-flag) (eq regexp-flag 'literal))) |
| 1225 | (search-function (if regexp-flag 're-search-forward 'search-forward)) |
| 1226 | (search-string from-string) |
| 1227 | (real-match-data nil) ; the match data for the current match |
| 1228 | (next-replacement nil) |
| 1229 | (noedit nil) |
| 1230 | (keep-going t) |
| 1231 | (stack nil) |
| 1232 | (replace-count 0) |
| 1233 | (nonempty-match nil) |
| 1234 | |
| 1235 | ;; If non-nil, it is marker saying where in the buffer to stop. |
| 1236 | (limit nil) |
| 1237 | |
| 1238 | ;; Data for the next match. If a cons, it has the same format as |
| 1239 | ;; (match-data); otherwise it is t if a match is possible at point. |
| 1240 | (match-again t) |
| 1241 | |
| 1242 | (message |
| 1243 | (if query-flag |
| 1244 | (substitute-command-keys |
| 1245 | "Query replacing %s with %s: (\\<query-replace-map>\\[help] for help) ")))) |
| 1246 | |
| 1247 | ;; If region is active, in Transient Mark mode, operate on region. |
| 1248 | (when start |
| 1249 | (setq limit (copy-marker (max start end))) |
| 1250 | (goto-char (min start end)) |
| 1251 | (deactivate-mark)) |
| 1252 | |
| 1253 | ;; REPLACEMENTS is either a string, a list of strings, or a cons cell |
| 1254 | ;; containing a function and its first argument. The function is |
| 1255 | ;; called to generate each replacement like this: |
| 1256 | ;; (funcall (car replacements) (cdr replacements) replace-count) |
| 1257 | ;; It must return a string. |
| 1258 | (cond |
| 1259 | ((stringp replacements) |
| 1260 | (setq next-replacement replacements |
| 1261 | replacements nil)) |
| 1262 | ((stringp (car replacements)) ; If it isn't a string, it must be a cons |
| 1263 | (or repeat-count (setq repeat-count 1)) |
| 1264 | (setq replacements (cons 'replace-loop-through-replacements |
| 1265 | (vector repeat-count repeat-count |
| 1266 | replacements replacements))))) |
| 1267 | |
| 1268 | (if delimited-flag |
| 1269 | (setq search-function 're-search-forward |
| 1270 | search-string (concat "\\b" |
| 1271 | (if regexp-flag from-string |
| 1272 | (regexp-quote from-string)) |
| 1273 | "\\b"))) |
| 1274 | (push-mark) |
| 1275 | (undo-boundary) |
| 1276 | (unwind-protect |
| 1277 | ;; Loop finding occurrences that perhaps should be replaced. |
| 1278 | (while (and keep-going |
| 1279 | (not (or (eobp) (and limit (>= (point) limit)))) |
| 1280 | ;; Use the next match if it is already known; |
| 1281 | ;; otherwise, search for a match after moving forward |
| 1282 | ;; one char if progress is required. |
| 1283 | (setq real-match-data |
| 1284 | (if (consp match-again) |
| 1285 | (progn (goto-char (nth 1 match-again)) |
| 1286 | (replace-match-data t |
| 1287 | real-match-data |
| 1288 | match-again)) |
| 1289 | (and (or match-again |
| 1290 | ;; MATCH-AGAIN non-nil means we |
| 1291 | ;; accept an adjacent match. If |
| 1292 | ;; we don't, move one char to the |
| 1293 | ;; right. This takes us a |
| 1294 | ;; character too far at the end, |
| 1295 | ;; but this is undone after the |
| 1296 | ;; while-loop. |
| 1297 | (progn |
| 1298 | (forward-char 1) |
| 1299 | (not (or (eobp) |
| 1300 | (and limit (>= (point) limit)))))) |
| 1301 | (funcall search-function search-string limit t) |
| 1302 | ;; For speed, use only integers and |
| 1303 | ;; reuse the list used last time. |
| 1304 | (replace-match-data t real-match-data))))) |
| 1305 | ;; Optionally ignore matches that have a read-only property. |
| 1306 | (unless (and query-replace-skip-read-only |
| 1307 | (text-property-not-all |
| 1308 | (match-beginning 0) (match-end 0) |
| 1309 | 'read-only nil)) |
| 1310 | |
| 1311 | ;; Record whether the match is nonempty, to avoid an infinite loop |
| 1312 | ;; repeatedly matching the same empty string. |
| 1313 | (setq nonempty-match |
| 1314 | (/= (nth 0 real-match-data) (nth 1 real-match-data))) |
| 1315 | |
| 1316 | ;; If the match is empty, record that the next one can't be |
| 1317 | ;; adjacent. |
| 1318 | |
| 1319 | ;; Otherwise, if matching a regular expression, do the next |
| 1320 | ;; match now, since the replacement for this match may |
| 1321 | ;; affect whether the next match is adjacent to this one. |
| 1322 | ;; If that match is empty, don't use it. |
| 1323 | (setq match-again |
| 1324 | (and nonempty-match |
| 1325 | (or (not regexp-flag) |
| 1326 | (and (looking-at search-string) |
| 1327 | (let ((match (match-data))) |
| 1328 | (and (/= (nth 0 match) (nth 1 match)) |
| 1329 | match)))))) |
| 1330 | |
| 1331 | ;; Calculate the replacement string, if necessary. |
| 1332 | (when replacements |
| 1333 | (set-match-data real-match-data) |
| 1334 | (setq next-replacement |
| 1335 | (funcall (car replacements) (cdr replacements) |
| 1336 | replace-count) |
| 1337 | noedit nil)) |
| 1338 | (if (not query-flag) |
| 1339 | (let ((inhibit-read-only |
| 1340 | query-replace-skip-read-only)) |
| 1341 | (unless noedit |
| 1342 | (replace-highlight (nth 0 real-match-data) |
| 1343 | (nth 1 real-match-data))) |
| 1344 | (setq noedit |
| 1345 | (replace-match-maybe-edit |
| 1346 | next-replacement nocasify literal |
| 1347 | noedit real-match-data) |
| 1348 | replace-count (1+ replace-count))) |
| 1349 | (undo-boundary) |
| 1350 | (let (done replaced key def) |
| 1351 | ;; Loop reading commands until one of them sets done, |
| 1352 | ;; which means it has finished handling this |
| 1353 | ;; occurrence. Any command that sets `done' should |
| 1354 | ;; leave behind proper match data for the stack. |
| 1355 | ;; Commands not setting `done' need to adjust |
| 1356 | ;; `real-match-data'. |
| 1357 | (while (not done) |
| 1358 | (set-match-data real-match-data) |
| 1359 | (replace-highlight (match-beginning 0) (match-end 0)) |
| 1360 | ;; Bind message-log-max so we don't fill up the message log |
| 1361 | ;; with a bunch of identical messages. |
| 1362 | (let ((message-log-max nil)) |
| 1363 | (message message from-string next-replacement)) |
| 1364 | (setq key (read-event)) |
| 1365 | ;; Necessary in case something happens during read-event |
| 1366 | ;; that clobbers the match data. |
| 1367 | (set-match-data real-match-data) |
| 1368 | (setq key (vector key)) |
| 1369 | (setq def (lookup-key map key)) |
| 1370 | ;; Restore the match data while we process the command. |
| 1371 | (cond ((eq def 'help) |
| 1372 | (with-output-to-temp-buffer "*Help*" |
| 1373 | (princ |
| 1374 | (concat "Query replacing " |
| 1375 | (if regexp-flag "regexp " "") |
| 1376 | from-string " with " |
| 1377 | next-replacement ".\n\n" |
| 1378 | (substitute-command-keys |
| 1379 | query-replace-help))) |
| 1380 | (with-current-buffer standard-output |
| 1381 | (help-mode)))) |
| 1382 | ((eq def 'exit) |
| 1383 | (setq keep-going nil) |
| 1384 | (setq done t)) |
| 1385 | ((eq def 'backup) |
| 1386 | (if stack |
| 1387 | (let ((elt (pop stack))) |
| 1388 | (goto-char (nth 0 elt)) |
| 1389 | (setq replaced (nth 1 elt) |
| 1390 | real-match-data |
| 1391 | (replace-match-data |
| 1392 | t real-match-data |
| 1393 | (nth 2 elt)))) |
| 1394 | (message "No previous match") |
| 1395 | (ding 'no-terminate) |
| 1396 | (sit-for 1))) |
| 1397 | ((eq def 'act) |
| 1398 | (or replaced |
| 1399 | (setq noedit |
| 1400 | (replace-match-maybe-edit |
| 1401 | next-replacement nocasify literal |
| 1402 | noedit real-match-data) |
| 1403 | replace-count (1+ replace-count))) |
| 1404 | (setq done t replaced t)) |
| 1405 | ((eq def 'act-and-exit) |
| 1406 | (or replaced |
| 1407 | (setq noedit |
| 1408 | (replace-match-maybe-edit |
| 1409 | next-replacement nocasify literal |
| 1410 | noedit real-match-data) |
| 1411 | replace-count (1+ replace-count))) |
| 1412 | (setq keep-going nil) |
| 1413 | (setq done t replaced t)) |
| 1414 | ((eq def 'act-and-show) |
| 1415 | (if (not replaced) |
| 1416 | (setq noedit |
| 1417 | (replace-match-maybe-edit |
| 1418 | next-replacement nocasify literal |
| 1419 | noedit real-match-data) |
| 1420 | replace-count (1+ replace-count) |
| 1421 | real-match-data (replace-match-data |
| 1422 | t real-match-data) |
| 1423 | replaced t))) |
| 1424 | ((eq def 'automatic) |
| 1425 | (or replaced |
| 1426 | (setq noedit |
| 1427 | (replace-match-maybe-edit |
| 1428 | next-replacement nocasify literal |
| 1429 | noedit real-match-data) |
| 1430 | replace-count (1+ replace-count))) |
| 1431 | (setq done t query-flag nil replaced t)) |
| 1432 | ((eq def 'skip) |
| 1433 | (setq done t)) |
| 1434 | ((eq def 'recenter) |
| 1435 | (recenter nil)) |
| 1436 | ((eq def 'edit) |
| 1437 | (let ((opos (point-marker))) |
| 1438 | (setq real-match-data (replace-match-data |
| 1439 | nil real-match-data |
| 1440 | real-match-data)) |
| 1441 | (goto-char (match-beginning 0)) |
| 1442 | (save-excursion |
| 1443 | (save-window-excursion |
| 1444 | (recursive-edit))) |
| 1445 | (goto-char opos) |
| 1446 | (set-marker opos nil)) |
| 1447 | ;; Before we make the replacement, |
| 1448 | ;; decide whether the search string |
| 1449 | ;; can match again just after this match. |
| 1450 | (if (and regexp-flag nonempty-match) |
| 1451 | (setq match-again (and (looking-at search-string) |
| 1452 | (match-data))))) |
| 1453 | ;; Edit replacement. |
| 1454 | ((eq def 'edit-replacement) |
| 1455 | (setq real-match-data (replace-match-data |
| 1456 | nil real-match-data |
| 1457 | real-match-data) |
| 1458 | next-replacement |
| 1459 | (read-input "Edit replacement string: " |
| 1460 | next-replacement) |
| 1461 | noedit nil) |
| 1462 | (if replaced |
| 1463 | (set-match-data real-match-data) |
| 1464 | (setq noedit |
| 1465 | (replace-match-maybe-edit |
| 1466 | next-replacement nocasify literal noedit |
| 1467 | real-match-data) |
| 1468 | replaced t)) |
| 1469 | (setq done t)) |
| 1470 | |
| 1471 | ((eq def 'delete-and-edit) |
| 1472 | (replace-match "" t t) |
| 1473 | (setq real-match-data (replace-match-data |
| 1474 | nil real-match-data)) |
| 1475 | (replace-dehighlight) |
| 1476 | (save-excursion (recursive-edit)) |
| 1477 | (setq replaced t)) |
| 1478 | ;; Note: we do not need to treat `exit-prefix' |
| 1479 | ;; specially here, since we reread |
| 1480 | ;; any unrecognized character. |
| 1481 | (t |
| 1482 | (setq this-command 'mode-exited) |
| 1483 | (setq keep-going nil) |
| 1484 | (setq unread-command-events |
| 1485 | (append (listify-key-sequence key) |
| 1486 | unread-command-events)) |
| 1487 | (setq done t)))) |
| 1488 | ;; Record previous position for ^ when we move on. |
| 1489 | ;; Change markers to numbers in the match data |
| 1490 | ;; since lots of markers slow down editing. |
| 1491 | (push (list (point) replaced |
| 1492 | ;;; If the replacement has already happened, all we need is the |
| 1493 | ;;; current match start and end. We could get this with a trivial |
| 1494 | ;;; match like |
| 1495 | ;;; (save-excursion (goto-char (match-beginning 0)) |
| 1496 | ;;; (search-forward (match-string 0)) |
| 1497 | ;;; (match-data t)) |
| 1498 | ;;; if we really wanted to avoid manually constructing match data. |
| 1499 | ;;; Adding current-buffer is necessary so that match-data calls can |
| 1500 | ;;; return markers which are appropriate for editing. |
| 1501 | (if replaced |
| 1502 | (list |
| 1503 | (match-beginning 0) |
| 1504 | (match-end 0) |
| 1505 | (current-buffer)) |
| 1506 | (match-data t))) |
| 1507 | stack))))) |
| 1508 | |
| 1509 | ;; The code preventing adjacent regexp matches in the condition |
| 1510 | ;; of the while-loop above will haven taken us one character |
| 1511 | ;; beyond the last replacement. Undo that. |
| 1512 | (when (and regexp-flag (not match-again) (> replace-count 0)) |
| 1513 | (backward-char 1)) |
| 1514 | |
| 1515 | (replace-dehighlight)) |
| 1516 | (or unread-command-events |
| 1517 | (message "Replaced %d occurrence%s" |
| 1518 | replace-count |
| 1519 | (if (= replace-count 1) "" "s"))) |
| 1520 | (and keep-going stack))) |
| 1521 | |
| 1522 | (defcustom query-replace-highlight t |
| 1523 | "*Non-nil means to highlight words during query replacement." |
| 1524 | :type 'boolean |
| 1525 | :group 'matching) |
| 1526 | |
| 1527 | (defvar replace-overlay nil) |
| 1528 | |
| 1529 | (defun replace-dehighlight () |
| 1530 | (and replace-overlay |
| 1531 | (progn |
| 1532 | (delete-overlay replace-overlay) |
| 1533 | (setq replace-overlay nil)))) |
| 1534 | |
| 1535 | (defun replace-highlight (start end) |
| 1536 | (and query-replace-highlight |
| 1537 | (if replace-overlay |
| 1538 | (move-overlay replace-overlay start end (current-buffer)) |
| 1539 | (setq replace-overlay (make-overlay start end)) |
| 1540 | (overlay-put replace-overlay 'face |
| 1541 | (if (facep 'query-replace) |
| 1542 | 'query-replace 'region))))) |
| 1543 | |
| 1544 | ;;; arch-tag: 16b4cd61-fd40-497b-b86f-b667c4cf88e4 |
| 1545 | ;;; replace.el ends here |