diff options
| author | Daniel Colascione | 2012-10-07 14:31:58 -0800 |
|---|---|---|
| committer | Daniel Colascione | 2012-10-07 14:31:58 -0800 |
| commit | 36a305a723c63fd345be65c536c52fe9765c14be (patch) | |
| tree | fb89d9e103552863214c60297a65320917109357 /lisp/replace.el | |
| parent | 2ab329f3b5d52a39f0a45c3d9c129f1c19560142 (diff) | |
| parent | 795b1482a9e314cda32d62ac2988f573d359366e (diff) | |
| download | emacs-36a305a723c63fd345be65c536c52fe9765c14be.tar.gz emacs-36a305a723c63fd345be65c536c52fe9765c14be.zip | |
Merge from trunk
Diffstat (limited to 'lisp/replace.el')
| -rw-r--r-- | lisp/replace.el | 193 |
1 files changed, 102 insertions, 91 deletions
diff --git a/lisp/replace.el b/lisp/replace.el index 001f7d1a78d..e714015fccf 100644 --- a/lisp/replace.el +++ b/lisp/replace.el | |||
| @@ -62,6 +62,10 @@ no default value.") | |||
| 62 | (defvar query-replace-interactive nil | 62 | (defvar query-replace-interactive nil |
| 63 | "Non-nil means `query-replace' uses the last search string. | 63 | "Non-nil means `query-replace' uses the last search string. |
| 64 | That becomes the \"string to replace\".") | 64 | That becomes the \"string to replace\".") |
| 65 | (make-obsolete-variable 'query-replace-interactive | ||
| 66 | "use `M-n' to pull the last incremental search string | ||
| 67 | to the minibuffer that reads the string to replace, or invoke replacements | ||
| 68 | from Isearch by using a key sequence like `C-s C-s M-%'." "24.3") | ||
| 65 | 69 | ||
| 66 | (defcustom query-replace-from-history-variable 'query-replace-history | 70 | (defcustom query-replace-from-history-variable 'query-replace-history |
| 67 | "History list to use for the FROM argument of `query-replace' commands. | 71 | "History list to use for the FROM argument of `query-replace' commands. |
| @@ -128,20 +132,22 @@ wants to replace FROM with TO." | |||
| 128 | (if query-replace-interactive | 132 | (if query-replace-interactive |
| 129 | (car (if regexp-flag regexp-search-ring search-ring)) | 133 | (car (if regexp-flag regexp-search-ring search-ring)) |
| 130 | (let* ((history-add-new-input nil) | 134 | (let* ((history-add-new-input nil) |
| 135 | (prompt | ||
| 136 | (if query-replace-defaults | ||
| 137 | (format "%s (default %s -> %s): " prompt | ||
| 138 | (query-replace-descr (car query-replace-defaults)) | ||
| 139 | (query-replace-descr (cdr query-replace-defaults))) | ||
| 140 | (format "%s: " prompt))) | ||
| 131 | (from | 141 | (from |
| 132 | ;; The save-excursion here is in case the user marks and copies | 142 | ;; The save-excursion here is in case the user marks and copies |
| 133 | ;; a region in order to specify the minibuffer input. | 143 | ;; a region in order to specify the minibuffer input. |
| 134 | ;; That should not clobber the region for the query-replace itself. | 144 | ;; That should not clobber the region for the query-replace itself. |
| 135 | (save-excursion | 145 | (save-excursion |
| 136 | (read-from-minibuffer | 146 | (if regexp-flag |
| 137 | (if query-replace-defaults | 147 | (read-regexp prompt nil query-replace-from-history-variable) |
| 138 | (format "%s (default %s -> %s): " prompt | 148 | (read-from-minibuffer |
| 139 | (query-replace-descr (car query-replace-defaults)) | 149 | prompt nil nil nil query-replace-from-history-variable |
| 140 | (query-replace-descr (cdr query-replace-defaults))) | 150 | (car (if regexp-flag regexp-search-ring search-ring)) t))))) |
| 141 | (format "%s: " prompt)) | ||
| 142 | nil nil nil | ||
| 143 | query-replace-from-history-variable | ||
| 144 | nil t)))) | ||
| 145 | (if (and (zerop (length from)) query-replace-defaults) | 151 | (if (and (zerop (length from)) query-replace-defaults) |
| 146 | (cons (car query-replace-defaults) | 152 | (cons (car query-replace-defaults) |
| 147 | (query-replace-compile-replacement | 153 | (query-replace-compile-replacement |
| @@ -230,9 +236,11 @@ what to do with it. For directions, type \\[help-command] at that time. | |||
| 230 | In Transient Mark mode, if the mark is active, operate on the contents | 236 | In Transient Mark mode, if the mark is active, operate on the contents |
| 231 | of the region. Otherwise, operate from point to the end of the buffer. | 237 | of the region. Otherwise, operate from point to the end of the buffer. |
| 232 | 238 | ||
| 233 | If `query-replace-interactive' is non-nil, the last incremental search | 239 | Use \\<minibuffer-local-map>\\[next-history-element] \ |
| 234 | string is used as FROM-STRING--you don't have to specify it with the | 240 | to pull the last incremental search string to the minibuffer |
| 235 | minibuffer. | 241 | that reads FROM-STRING, or invoke replacements from |
| 242 | incremental search with a key sequence like `C-s C-s M-%' | ||
| 243 | to use its current search string as the string to replace. | ||
| 236 | 244 | ||
| 237 | Matching is independent of case if `case-fold-search' is non-nil and | 245 | Matching is independent of case if `case-fold-search' is non-nil and |
| 238 | FROM-STRING has no uppercase letters. Replacement transfers the case | 246 | FROM-STRING has no uppercase letters. Replacement transfers the case |
| @@ -278,9 +286,11 @@ what to do with it. For directions, type \\[help-command] at that time. | |||
| 278 | In Transient Mark mode, if the mark is active, operate on the contents | 286 | In Transient Mark mode, if the mark is active, operate on the contents |
| 279 | of the region. Otherwise, operate from point to the end of the buffer. | 287 | of the region. Otherwise, operate from point to the end of the buffer. |
| 280 | 288 | ||
| 281 | If `query-replace-interactive' is non-nil, the last incremental search | 289 | Use \\<minibuffer-local-map>\\[next-history-element] \ |
| 282 | regexp is used as REGEXP--you don't have to specify it with the | 290 | to pull the last incremental search regexp to the minibuffer |
| 283 | minibuffer. | 291 | that reads REGEXP, or invoke replacements from |
| 292 | incremental search with a key sequence like `C-M-s C-M-s C-M-%' | ||
| 293 | to use its current search regexp as the regexp to replace. | ||
| 284 | 294 | ||
| 285 | Matching is independent of case if `case-fold-search' is non-nil and | 295 | Matching is independent of case if `case-fold-search' is non-nil and |
| 286 | REGEXP has no uppercase letters. Replacement transfers the case | 296 | REGEXP has no uppercase letters. Replacement transfers the case |
| @@ -363,9 +373,9 @@ In interactive use, `\\#' in itself stands for `replace-count'. | |||
| 363 | In Transient Mark mode, if the mark is active, operate on the contents | 373 | In Transient Mark mode, if the mark is active, operate on the contents |
| 364 | of the region. Otherwise, operate from point to the end of the buffer. | 374 | of the region. Otherwise, operate from point to the end of the buffer. |
| 365 | 375 | ||
| 366 | If `query-replace-interactive' is non-nil, the last incremental search | 376 | Use \\<minibuffer-local-map>\\[next-history-element] \ |
| 367 | regexp is used as REGEXP--you don't have to specify it with the | 377 | to pull the last incremental search regexp to the minibuffer |
| 368 | minibuffer. | 378 | that reads REGEXP. |
| 369 | 379 | ||
| 370 | Preserves case in each replacement if `case-replace' and `case-fold-search' | 380 | Preserves case in each replacement if `case-replace' and `case-fold-search' |
| 371 | are non-nil and REGEXP has no uppercase letters. | 381 | are non-nil and REGEXP has no uppercase letters. |
| @@ -377,35 +387,33 @@ regexp in `search-whitespace-regexp'. | |||
| 377 | Third arg DELIMITED (prefix arg if interactive), if non-nil, means replace | 387 | Third arg DELIMITED (prefix arg if interactive), if non-nil, means replace |
| 378 | only matches that are surrounded by word boundaries. | 388 | only matches that are surrounded by word boundaries. |
| 379 | Fourth and fifth arg START and END specify the region to operate on." | 389 | Fourth and fifth arg START and END specify the region to operate on." |
| 390 | (declare (obsolete "use the `\\,' feature of `query-replace-regexp' | ||
| 391 | for interactive calls, and `search-forward-regexp'/`replace-match' | ||
| 392 | for Lisp calls." "22.1")) | ||
| 380 | (interactive | 393 | (interactive |
| 381 | (progn | 394 | (progn |
| 382 | (barf-if-buffer-read-only) | 395 | (barf-if-buffer-read-only) |
| 383 | (let* ((from | 396 | (let* ((from |
| 384 | ;; Let-bind the history var to disable the "foo -> bar" default. | 397 | ;; Let-bind the history var to disable the "foo -> bar" |
| 385 | ;; Maybe we shouldn't disable this default, but for now I'll | 398 | ;; default. Maybe we shouldn't disable this default, but |
| 386 | ;; leave it off. --Stef | 399 | ;; for now I'll leave it off. --Stef |
| 387 | (let ((query-replace-to-history-variable nil)) | 400 | (let ((query-replace-to-history-variable nil)) |
| 388 | (query-replace-read-from "Query replace regexp" t))) | 401 | (query-replace-read-from "Query replace regexp" t))) |
| 389 | (to (list (read-from-minibuffer | 402 | (to (list (read-from-minibuffer |
| 390 | (format "Query replace regexp %s with eval: " | 403 | (format "Query replace regexp %s with eval: " |
| 391 | (query-replace-descr from)) | 404 | (query-replace-descr from)) |
| 392 | nil nil t query-replace-to-history-variable from t)))) | 405 | nil nil t query-replace-to-history-variable from t)))) |
| 393 | ;; We make TO a list because replace-match-string-symbols requires one, | 406 | ;; We make TO a list because replace-match-string-symbols requires one, |
| 394 | ;; and the user might enter a single token. | 407 | ;; and the user might enter a single token. |
| 395 | (replace-match-string-symbols to) | 408 | (replace-match-string-symbols to) |
| 396 | (list from (car to) current-prefix-arg | 409 | (list from (car to) current-prefix-arg |
| 397 | (if (and transient-mark-mode mark-active) | 410 | (if (and transient-mark-mode mark-active) |
| 398 | (region-beginning)) | 411 | (region-beginning)) |
| 399 | (if (and transient-mark-mode mark-active) | 412 | (if (and transient-mark-mode mark-active) |
| 400 | (region-end)))))) | 413 | (region-end)))))) |
| 401 | (perform-replace regexp (cons 'replace-eval-replacement to-expr) | 414 | (perform-replace regexp (cons 'replace-eval-replacement to-expr) |
| 402 | t 'literal delimited nil nil start end)) | 415 | t 'literal delimited nil nil start end)) |
| 403 | 416 | ||
| 404 | (make-obsolete 'query-replace-regexp-eval | ||
| 405 | "for interactive use, use the special `\\,' feature of | ||
| 406 | `query-replace-regexp' instead. Non-interactively, a loop | ||
| 407 | using `search-forward-regexp' and `replace-match' is preferred." "22.1") | ||
| 408 | |||
| 409 | (defun map-query-replace-regexp (regexp to-strings &optional n start end) | 417 | (defun map-query-replace-regexp (regexp to-strings &optional n start end) |
| 410 | "Replace some matches for REGEXP with various strings, in rotation. | 418 | "Replace some matches for REGEXP with various strings, in rotation. |
| 411 | The second argument TO-STRINGS contains the replacement strings, separated | 419 | The second argument TO-STRINGS contains the replacement strings, separated |
| @@ -418,19 +426,16 @@ of the region. Otherwise, operate from point to the end of the buffer. | |||
| 418 | 426 | ||
| 419 | Non-interactively, TO-STRINGS may be a list of replacement strings. | 427 | Non-interactively, TO-STRINGS may be a list of replacement strings. |
| 420 | 428 | ||
| 421 | If `query-replace-interactive' is non-nil, the last incremental search | 429 | Use \\<minibuffer-local-map>\\[next-history-element] \ |
| 422 | regexp is used as REGEXP--you don't have to specify it with the minibuffer. | 430 | to pull the last incremental search regexp to the minibuffer |
| 431 | that reads REGEXP. | ||
| 423 | 432 | ||
| 424 | A prefix argument N says to use each replacement string N times | 433 | A prefix argument N says to use each replacement string N times |
| 425 | before rotating to the next. | 434 | before rotating to the next. |
| 426 | Fourth and fifth arg START and END specify the region to operate on." | 435 | Fourth and fifth arg START and END specify the region to operate on." |
| 427 | (interactive | 436 | (interactive |
| 428 | (let* ((from (if query-replace-interactive | 437 | (let* ((from (read-regexp "Map query replace (regexp): " nil |
| 429 | (car regexp-search-ring) | 438 | query-replace-from-history-variable)) |
| 430 | (read-from-minibuffer "Map query replace (regexp): " | ||
| 431 | nil nil nil | ||
| 432 | query-replace-from-history-variable | ||
| 433 | nil t))) | ||
| 434 | (to (read-from-minibuffer | 439 | (to (read-from-minibuffer |
| 435 | (format "Query replace %s with (space-separated strings): " | 440 | (format "Query replace %s with (space-separated strings): " |
| 436 | (query-replace-descr from)) | 441 | (query-replace-descr from)) |
| @@ -476,9 +481,9 @@ Third arg DELIMITED (prefix arg if interactive), if non-nil, means replace | |||
| 476 | only matches surrounded by word boundaries. | 481 | only matches surrounded by word boundaries. |
| 477 | Fourth and fifth arg START and END specify the region to operate on. | 482 | Fourth and fifth arg START and END specify the region to operate on. |
| 478 | 483 | ||
| 479 | If `query-replace-interactive' is non-nil, the last incremental search | 484 | Use \\<minibuffer-local-map>\\[next-history-element] \ |
| 480 | string is used as FROM-STRING--you don't have to specify it with the | 485 | to pull the last incremental search string to the minibuffer |
| 481 | minibuffer. | 486 | that reads FROM-STRING. |
| 482 | 487 | ||
| 483 | This function is usually the wrong thing to use in a Lisp program. | 488 | This function is usually the wrong thing to use in a Lisp program. |
| 484 | What you probably want is a loop like this: | 489 | What you probably want is a loop like this: |
| @@ -541,8 +546,9 @@ When using those Lisp features interactively in the replacement | |||
| 541 | text, TO-STRING is actually made a list instead of a string. | 546 | text, TO-STRING is actually made a list instead of a string. |
| 542 | Use \\[repeat-complex-command] after this command for details. | 547 | Use \\[repeat-complex-command] after this command for details. |
| 543 | 548 | ||
| 544 | If `query-replace-interactive' is non-nil, the last incremental search | 549 | Use \\<minibuffer-local-map>\\[next-history-element] \ |
| 545 | regexp is used as REGEXP--you don't have to specify it with the minibuffer. | 550 | to pull the last incremental search regexp to the minibuffer |
| 551 | that reads REGEXP. | ||
| 546 | 552 | ||
| 547 | This function is usually the wrong thing to use in a Lisp program. | 553 | This function is usually the wrong thing to use in a Lisp program. |
| 548 | What you probably want is a loop like this: | 554 | What you probably want is a loop like this: |
| @@ -574,38 +580,47 @@ of `history-length', which see.") | |||
| 574 | (defvar occur-collect-regexp-history '("\\1") | 580 | (defvar occur-collect-regexp-history '("\\1") |
| 575 | "History of regexp for occur's collect operation") | 581 | "History of regexp for occur's collect operation") |
| 576 | 582 | ||
| 577 | (defun read-regexp (prompt &optional default-value) | 583 | (defun read-regexp (prompt &optional defaults history) |
| 578 | "Read regexp as a string using the regexp history and some useful defaults. | 584 | "Read and return a regular expression as a string. |
| 579 | Prompt for a regular expression with PROMPT (without a colon and | 585 | When PROMPT doesn't end with a colon and space, it adds a final \": \". |
| 580 | space) in the minibuffer. The optional argument DEFAULT-VALUE | 586 | If DEFAULTS is non-nil, it displays the first default in the prompt. |
| 581 | provides the value to display in the minibuffer prompt that is | 587 | |
| 582 | returned if the user just types RET. | 588 | Non-nil optional arg DEFAULTS is a string or a list of strings that |
| 583 | Values available via M-n are the string at point, the last isearch | 589 | are prepended to a list of standard default values, which include the |
| 584 | regexp, the last isearch string, and the last replacement regexp." | 590 | string at point, the last isearch regexp, the last isearch string, and |
| 585 | (let* ((defaults | 591 | the last replacement regexp. |
| 586 | (list (regexp-quote | 592 | |
| 587 | (or (funcall (or find-tag-default-function | 593 | Non-nil HISTORY is a symbol to use for the history list. |
| 588 | (get major-mode 'find-tag-default-function) | 594 | If HISTORY is nil, `regexp-history' is used." |
| 589 | 'find-tag-default)) | 595 | (let* ((default (if (consp defaults) (car defaults) defaults)) |
| 590 | "")) | 596 | (defaults |
| 591 | (car regexp-search-ring) | 597 | (append |
| 592 | (regexp-quote (or (car search-ring) "")) | 598 | (if (listp defaults) defaults (list defaults)) |
| 593 | (car (symbol-value | 599 | (list (regexp-quote |
| 594 | query-replace-from-history-variable)))) | 600 | (or (funcall (or find-tag-default-function |
| 601 | (get major-mode 'find-tag-default-function) | ||
| 602 | 'find-tag-default)) | ||
| 603 | "")) | ||
| 604 | (car regexp-search-ring) | ||
| 605 | (regexp-quote (or (car search-ring) "")) | ||
| 606 | (car (symbol-value | ||
| 607 | query-replace-from-history-variable))))) | ||
| 595 | (defaults (delete-dups (delq nil (delete "" defaults)))) | 608 | (defaults (delete-dups (delq nil (delete "" defaults)))) |
| 596 | ;; Don't add automatically the car of defaults for empty input | 609 | ;; Do not automatically add default to the history for empty input. |
| 597 | (history-add-new-input nil) | 610 | (history-add-new-input nil) |
| 598 | (input | 611 | (input (read-from-minibuffer |
| 599 | (read-from-minibuffer | 612 | (cond ((string-match-p ":[ \t]*\\'" prompt) |
| 600 | (if default-value | 613 | prompt) |
| 601 | (format "%s (default %s): " prompt | 614 | (default |
| 602 | (query-replace-descr default-value)) | 615 | (format "%s (default %s): " prompt |
| 603 | (format "%s: " prompt)) | 616 | (query-replace-descr default))) |
| 604 | nil nil nil 'regexp-history defaults t))) | 617 | (t |
| 618 | (format "%s: " prompt))) | ||
| 619 | nil nil nil (or history 'regexp-history) defaults t))) | ||
| 605 | (if (equal input "") | 620 | (if (equal input "") |
| 606 | (or default-value input) | 621 | (or default input) |
| 607 | (prog1 input | 622 | (prog1 input |
| 608 | (add-to-history 'regexp-history input))))) | 623 | (add-to-history (or history 'regexp-history) input))))) |
| 609 | 624 | ||
| 610 | 625 | ||
| 611 | (defalias 'delete-non-matching-lines 'keep-lines) | 626 | (defalias 'delete-non-matching-lines 'keep-lines) |
| @@ -1130,9 +1145,9 @@ which means to discard all text properties." | |||
| 1130 | "\\&" | 1145 | "\\&" |
| 1131 | ;; Get the regexp for collection pattern. | 1146 | ;; Get the regexp for collection pattern. |
| 1132 | (let ((default (car occur-collect-regexp-history))) | 1147 | (let ((default (car occur-collect-regexp-history))) |
| 1133 | (read-string | 1148 | (read-regexp |
| 1134 | (format "Regexp to collect (default %s): " default) | 1149 | (format "Regexp to collect (default %s): " default) |
| 1135 | nil 'occur-collect-regexp-history default))) | 1150 | default 'occur-collect-regexp-history))) |
| 1136 | ;; Otherwise normal occur takes numerical prefix argument. | 1151 | ;; Otherwise normal occur takes numerical prefix argument. |
| 1137 | (when current-prefix-arg | 1152 | (when current-prefix-arg |
| 1138 | (prefix-numeric-value current-prefix-arg)))))) | 1153 | (prefix-numeric-value current-prefix-arg)))))) |
| @@ -1219,14 +1234,10 @@ See also `multi-occur'." | |||
| 1219 | (cons | 1234 | (cons |
| 1220 | (let* ((default (car regexp-history)) | 1235 | (let* ((default (car regexp-history)) |
| 1221 | (input | 1236 | (input |
| 1222 | (read-from-minibuffer | 1237 | (read-regexp |
| 1223 | (if current-prefix-arg | 1238 | (if current-prefix-arg |
| 1224 | "List lines in buffers whose names match regexp: " | 1239 | "List lines in buffers whose names match regexp: " |
| 1225 | "List lines in buffers whose filenames match regexp: ") | 1240 | "List lines in buffers whose filenames match regexp: ")))) |
| 1226 | nil | ||
| 1227 | nil | ||
| 1228 | nil | ||
| 1229 | 'regexp-history))) | ||
| 1230 | (if (equal input "") | 1241 | (if (equal input "") |
| 1231 | default | 1242 | default |
| 1232 | input)) | 1243 | input)) |