diff options
Diffstat (limited to 'lisp/replace.el')
| -rw-r--r-- | lisp/replace.el | 87 |
1 files changed, 49 insertions, 38 deletions
diff --git a/lisp/replace.el b/lisp/replace.el index d6590c5516a..b6802aeaf57 100644 --- a/lisp/replace.el +++ b/lisp/replace.el | |||
| @@ -284,7 +284,7 @@ the original string if not." | |||
| 284 | (and current-prefix-arg (not (eq current-prefix-arg '-))) | 284 | (and current-prefix-arg (not (eq current-prefix-arg '-))) |
| 285 | (and current-prefix-arg (eq current-prefix-arg '-))))) | 285 | (and current-prefix-arg (eq current-prefix-arg '-))))) |
| 286 | 286 | ||
| 287 | (defun query-replace (from-string to-string &optional delimited start end backward) | 287 | (defun query-replace (from-string to-string &optional delimited start end backward region-noncontiguous-p) |
| 288 | "Replace some occurrences of FROM-STRING with TO-STRING. | 288 | "Replace some occurrences of FROM-STRING with TO-STRING. |
| 289 | As each match is found, the user must type a character saying | 289 | As each match is found, the user must type a character saying |
| 290 | what to do with it. For directions, type \\[help-command] at that time. | 290 | what to do with it. For directions, type \\[help-command] at that time. |
| @@ -328,22 +328,21 @@ To customize possible responses, change the bindings in `query-replace-map'." | |||
| 328 | (if current-prefix-arg | 328 | (if current-prefix-arg |
| 329 | (if (eq current-prefix-arg '-) " backward" " word") | 329 | (if (eq current-prefix-arg '-) " backward" " word") |
| 330 | "") | 330 | "") |
| 331 | (if (and transient-mark-mode mark-active) " in region" "")) | 331 | (if (use-region-p) " in region" "")) |
| 332 | nil))) | 332 | nil))) |
| 333 | (list (nth 0 common) (nth 1 common) (nth 2 common) | 333 | (list (nth 0 common) (nth 1 common) (nth 2 common) |
| 334 | ;; These are done separately here | 334 | ;; These are done separately here |
| 335 | ;; so that command-history will record these expressions | 335 | ;; so that command-history will record these expressions |
| 336 | ;; rather than the values they had this time. | 336 | ;; rather than the values they had this time. |
| 337 | (if (and transient-mark-mode mark-active) | 337 | (if (use-region-p) (region-beginning)) |
| 338 | (region-beginning)) | 338 | (if (use-region-p) (region-end)) |
| 339 | (if (and transient-mark-mode mark-active) | 339 | (nth 3 common) |
| 340 | (region-end)) | 340 | (if (use-region-p) (region-noncontiguous-p))))) |
| 341 | (nth 3 common)))) | 341 | (perform-replace from-string to-string t nil delimited nil nil start end backward region-noncontiguous-p)) |
| 342 | (perform-replace from-string to-string t nil delimited nil nil start end backward)) | ||
| 343 | 342 | ||
| 344 | (define-key esc-map "%" 'query-replace) | 343 | (define-key esc-map "%" 'query-replace) |
| 345 | 344 | ||
| 346 | (defun query-replace-regexp (regexp to-string &optional delimited start end backward) | 345 | (defun query-replace-regexp (regexp to-string &optional delimited start end backward region-noncontiguous-p) |
| 347 | "Replace some things after point matching REGEXP with TO-STRING. | 346 | "Replace some things after point matching REGEXP with TO-STRING. |
| 348 | As each match is found, the user must type a character saying | 347 | As each match is found, the user must type a character saying |
| 349 | what to do with it. For directions, type \\[help-command] at that time. | 348 | what to do with it. For directions, type \\[help-command] at that time. |
| @@ -408,18 +407,17 @@ Use \\[repeat-complex-command] after this command for details." | |||
| 408 | (if (eq current-prefix-arg '-) " backward" " word") | 407 | (if (eq current-prefix-arg '-) " backward" " word") |
| 409 | "") | 408 | "") |
| 410 | " regexp" | 409 | " regexp" |
| 411 | (if (and transient-mark-mode mark-active) " in region" "")) | 410 | (if (use-region-p) " in region" "")) |
| 412 | t))) | 411 | t))) |
| 413 | (list (nth 0 common) (nth 1 common) (nth 2 common) | 412 | (list (nth 0 common) (nth 1 common) (nth 2 common) |
| 414 | ;; These are done separately here | 413 | ;; These are done separately here |
| 415 | ;; so that command-history will record these expressions | 414 | ;; so that command-history will record these expressions |
| 416 | ;; rather than the values they had this time. | 415 | ;; rather than the values they had this time. |
| 417 | (if (and transient-mark-mode mark-active) | 416 | (if (use-region-p) (region-beginning)) |
| 418 | (region-beginning)) | 417 | (if (use-region-p) (region-end)) |
| 419 | (if (and transient-mark-mode mark-active) | 418 | (nth 3 common) |
| 420 | (region-end)) | 419 | (if (use-region-p) (region-noncontiguous-p))))) |
| 421 | (nth 3 common)))) | 420 | (perform-replace regexp to-string t t delimited nil nil start end backward region-noncontiguous-p)) |
| 422 | (perform-replace regexp to-string t t delimited nil nil start end backward)) | ||
| 423 | 421 | ||
| 424 | (define-key esc-map [?\C-%] 'query-replace-regexp) | 422 | (define-key esc-map [?\C-%] 'query-replace-regexp) |
| 425 | 423 | ||
| @@ -485,10 +483,8 @@ for Lisp calls." "22.1")) | |||
| 485 | ;; and the user might enter a single token. | 483 | ;; and the user might enter a single token. |
| 486 | (replace-match-string-symbols to) | 484 | (replace-match-string-symbols to) |
| 487 | (list from (car to) current-prefix-arg | 485 | (list from (car to) current-prefix-arg |
| 488 | (if (and transient-mark-mode mark-active) | 486 | (if (use-region-p) (region-beginning)) |
| 489 | (region-beginning)) | 487 | (if (use-region-p) (region-end)))))) |
| 490 | (if (and transient-mark-mode mark-active) | ||
| 491 | (region-end)))))) | ||
| 492 | (perform-replace regexp (cons 'replace-eval-replacement to-expr) | 488 | (perform-replace regexp (cons 'replace-eval-replacement to-expr) |
| 493 | t 'literal delimited nil nil start end)) | 489 | t 'literal delimited nil nil start end)) |
| 494 | 490 | ||
| @@ -523,10 +519,8 @@ Fourth and fifth arg START and END specify the region to operate on." | |||
| 523 | (list from to | 519 | (list from to |
| 524 | (and current-prefix-arg | 520 | (and current-prefix-arg |
| 525 | (prefix-numeric-value current-prefix-arg)) | 521 | (prefix-numeric-value current-prefix-arg)) |
| 526 | (if (and transient-mark-mode mark-active) | 522 | (if (use-region-p) (region-beginning)) |
| 527 | (region-beginning)) | 523 | (if (use-region-p) (region-end))))) |
| 528 | (if (and transient-mark-mode mark-active) | ||
| 529 | (region-end))))) | ||
| 530 | (let (replacements) | 524 | (let (replacements) |
| 531 | (if (listp to-strings) | 525 | (if (listp to-strings) |
| 532 | (setq replacements to-strings) | 526 | (setq replacements to-strings) |
| @@ -587,13 +581,11 @@ and TO-STRING is also null.)" | |||
| 587 | (if (eq current-prefix-arg '-) " backward" " word") | 581 | (if (eq current-prefix-arg '-) " backward" " word") |
| 588 | "") | 582 | "") |
| 589 | " string" | 583 | " string" |
| 590 | (if (and transient-mark-mode mark-active) " in region" "")) | 584 | (if (use-region-p) " in region" "")) |
| 591 | nil))) | 585 | nil))) |
| 592 | (list (nth 0 common) (nth 1 common) (nth 2 common) | 586 | (list (nth 0 common) (nth 1 common) (nth 2 common) |
| 593 | (if (and transient-mark-mode mark-active) | 587 | (if (use-region-p) (region-beginning)) |
| 594 | (region-beginning)) | 588 | (if (use-region-p) (region-end)) |
| 595 | (if (and transient-mark-mode mark-active) | ||
| 596 | (region-end)) | ||
| 597 | (nth 3 common)))) | 589 | (nth 3 common)))) |
| 598 | (perform-replace from-string to-string nil nil delimited nil nil start end backward)) | 590 | (perform-replace from-string to-string nil nil delimited nil nil start end backward)) |
| 599 | 591 | ||
| @@ -661,13 +653,11 @@ which will run faster and will not set the mark or print anything." | |||
| 661 | (if (eq current-prefix-arg '-) " backward" " word") | 653 | (if (eq current-prefix-arg '-) " backward" " word") |
| 662 | "") | 654 | "") |
| 663 | " regexp" | 655 | " regexp" |
| 664 | (if (and transient-mark-mode mark-active) " in region" "")) | 656 | (if (use-region-p) " in region" "")) |
| 665 | t))) | 657 | t))) |
| 666 | (list (nth 0 common) (nth 1 common) (nth 2 common) | 658 | (list (nth 0 common) (nth 1 common) (nth 2 common) |
| 667 | (if (and transient-mark-mode mark-active) | 659 | (if (use-region-p) (region-beginning)) |
| 668 | (region-beginning)) | 660 | (if (use-region-p) (region-end)) |
| 669 | (if (and transient-mark-mode mark-active) | ||
| 670 | (region-end)) | ||
| 671 | (nth 3 common)))) | 661 | (nth 3 common)))) |
| 672 | (perform-replace regexp to-string nil t delimited nil nil start end backward)) | 662 | (perform-replace regexp to-string nil t delimited nil nil start end backward)) |
| 673 | 663 | ||
| @@ -832,7 +822,7 @@ a previously found match." | |||
| 832 | (unless (or (bolp) (eobp)) | 822 | (unless (or (bolp) (eobp)) |
| 833 | (forward-line 0)) | 823 | (forward-line 0)) |
| 834 | (point-marker))))) | 824 | (point-marker))))) |
| 835 | (if (and interactive transient-mark-mode mark-active) | 825 | (if (and interactive (use-region-p)) |
| 836 | (setq rstart (region-beginning) | 826 | (setq rstart (region-beginning) |
| 837 | rend (progn | 827 | rend (progn |
| 838 | (goto-char (region-end)) | 828 | (goto-char (region-end)) |
| @@ -901,7 +891,7 @@ starting on the same line at which another match ended is ignored." | |||
| 901 | (progn | 891 | (progn |
| 902 | (goto-char (min rstart rend)) | 892 | (goto-char (min rstart rend)) |
| 903 | (setq rend (copy-marker (max rstart rend)))) | 893 | (setq rend (copy-marker (max rstart rend)))) |
| 904 | (if (and interactive transient-mark-mode mark-active) | 894 | (if (and interactive (use-region-p)) |
| 905 | (setq rstart (region-beginning) | 895 | (setq rstart (region-beginning) |
| 906 | rend (copy-marker (region-end))) | 896 | rend (copy-marker (region-end))) |
| 907 | (setq rstart (point) | 897 | (setq rstart (point) |
| @@ -951,7 +941,7 @@ a previously found match." | |||
| 951 | (setq rend (max rstart rend))) | 941 | (setq rend (max rstart rend))) |
| 952 | (goto-char rstart) | 942 | (goto-char rstart) |
| 953 | (setq rend (point-max))) | 943 | (setq rend (point-max))) |
| 954 | (if (and interactive transient-mark-mode mark-active) | 944 | (if (and interactive (use-region-p)) |
| 955 | (setq rstart (region-beginning) | 945 | (setq rstart (region-beginning) |
| 956 | rend (region-end)) | 946 | rend (region-end)) |
| 957 | (setq rstart (point) | 947 | (setq rstart (point) |
| @@ -2068,7 +2058,7 @@ It is called with three arguments, as if it were | |||
| 2068 | 2058 | ||
| 2069 | (defun perform-replace (from-string replacements | 2059 | (defun perform-replace (from-string replacements |
| 2070 | query-flag regexp-flag delimited-flag | 2060 | query-flag regexp-flag delimited-flag |
| 2071 | &optional repeat-count map start end backward) | 2061 | &optional repeat-count map start end backward region-noncontiguous-p) |
| 2072 | "Subroutine of `query-replace'. Its complexity handles interactive queries. | 2062 | "Subroutine of `query-replace'. Its complexity handles interactive queries. |
| 2073 | Don't use this in your own program unless you want to query and set the mark | 2063 | Don't use this in your own program unless you want to query and set the mark |
| 2074 | just as `query-replace' does. Instead, write a simple loop like this: | 2064 | just as `query-replace' does. Instead, write a simple loop like this: |
| @@ -2115,6 +2105,9 @@ It must return a string." | |||
| 2115 | 2105 | ||
| 2116 | ;; If non-nil, it is marker saying where in the buffer to stop. | 2106 | ;; If non-nil, it is marker saying where in the buffer to stop. |
| 2117 | (limit nil) | 2107 | (limit nil) |
| 2108 | ;; Use local binding in add-function below. | ||
| 2109 | (isearch-filter-predicate isearch-filter-predicate) | ||
| 2110 | (region-bounds nil) | ||
| 2118 | 2111 | ||
| 2119 | ;; Data for the next match. If a cons, it has the same format as | 2112 | ;; Data for the next match. If a cons, it has the same format as |
| 2120 | ;; (match-data); otherwise it is t if a match is possible at point. | 2113 | ;; (match-data); otherwise it is t if a match is possible at point. |
| @@ -2127,6 +2120,24 @@ It must return a string." | |||
| 2127 | "Query replacing %s with %s: (\\<query-replace-map>\\[help] for help) ") | 2120 | "Query replacing %s with %s: (\\<query-replace-map>\\[help] for help) ") |
| 2128 | minibuffer-prompt-properties)))) | 2121 | minibuffer-prompt-properties)))) |
| 2129 | 2122 | ||
| 2123 | ;; Unless a single contiguous chunk is selected, operate on multiple chunks. | ||
| 2124 | (when region-noncontiguous-p | ||
| 2125 | (setq region-bounds | ||
| 2126 | (mapcar (lambda (position) | ||
| 2127 | (cons (copy-marker (car position)) | ||
| 2128 | (copy-marker (cdr position)))) | ||
| 2129 | (funcall region-extract-function 'bounds))) | ||
| 2130 | (add-function :after-while isearch-filter-predicate | ||
| 2131 | (lambda (start end) | ||
| 2132 | (delq nil (mapcar | ||
| 2133 | (lambda (bounds) | ||
| 2134 | (and | ||
| 2135 | (>= start (car bounds)) | ||
| 2136 | (<= start (cdr bounds)) | ||
| 2137 | (>= end (car bounds)) | ||
| 2138 | (<= end (cdr bounds)))) | ||
| 2139 | region-bounds))))) | ||
| 2140 | |||
| 2130 | ;; If region is active, in Transient Mark mode, operate on region. | 2141 | ;; If region is active, in Transient Mark mode, operate on region. |
| 2131 | (if backward | 2142 | (if backward |
| 2132 | (when end | 2143 | (when end |