diff options
Diffstat (limited to 'lisp/replace.el')
| -rw-r--r-- | lisp/replace.el | 76 |
1 files changed, 48 insertions, 28 deletions
diff --git a/lisp/replace.el b/lisp/replace.el index 380dd83c87e..a7c8b859402 100644 --- a/lisp/replace.el +++ b/lisp/replace.el | |||
| @@ -1,7 +1,7 @@ | |||
| 1 | ;;; replace.el --- replace commands for Emacs | 1 | ;;; replace.el --- replace commands for Emacs |
| 2 | 2 | ||
| 3 | ;; Copyright (C) 1985, 86, 87, 92, 94, 96, 1997, 2000, 2001, 2002, | 3 | ;; Copyright (C) 1985, 1986, 1987, 1992, 1994, 1996, 1997, 2000, 2001, 2002, |
| 4 | ;; 2003, 2004 Free Software Foundation, Inc. | 4 | ;; 2003, 2004 Free Software Foundation, Inc. |
| 5 | 5 | ||
| 6 | ;; Maintainer: FSF | 6 | ;; Maintainer: FSF |
| 7 | 7 | ||
| @@ -64,37 +64,54 @@ strings or patterns." | |||
| 64 | :group 'matching | 64 | :group 'matching |
| 65 | :version "21.4") | 65 | :version "21.4") |
| 66 | 66 | ||
| 67 | (defun query-replace-descr (string) | ||
| 68 | (mapconcat 'isearch-text-char-description string "")) | ||
| 69 | |||
| 67 | (defun query-replace-read-from (string regexp-flag) | 70 | (defun query-replace-read-from (string regexp-flag) |
| 68 | "Query and return the `from' argument of a query-replace operation." | 71 | "Query and return the `from' argument of a query-replace operation. |
| 72 | The return value can also be a pair (FROM . TO) indicating that the user | ||
| 73 | wants to replace FROM with TO." | ||
| 69 | (if query-replace-interactive | 74 | (if query-replace-interactive |
| 70 | (car (if regexp-flag regexp-search-ring search-ring)) | 75 | (car (if regexp-flag regexp-search-ring search-ring)) |
| 71 | (let* ((from | 76 | (let* ((lastfrom (car (symbol-value query-replace-from-history-variable))) |
| 77 | (lastto (car (symbol-value query-replace-to-history-variable))) | ||
| 78 | (from | ||
| 72 | ;; The save-excursion here is in case the user marks and copies | 79 | ;; The save-excursion here is in case the user marks and copies |
| 73 | ;; a region in order to specify the minibuffer input. | 80 | ;; a region in order to specify the minibuffer input. |
| 74 | ;; That should not clobber the region for the query-replace itself. | 81 | ;; That should not clobber the region for the query-replace itself. |
| 75 | (save-excursion | 82 | (save-excursion |
| 83 | (when (equal lastfrom lastto) | ||
| 84 | ;; Typically, this is because the two histlists are shared. | ||
| 85 | (setq lastfrom (cadr (symbol-value | ||
| 86 | query-replace-from-history-variable)))) | ||
| 76 | (read-from-minibuffer | 87 | (read-from-minibuffer |
| 77 | (format "%s: " string) | 88 | (if (and lastto lastfrom) |
| 89 | (format "%s (default %s -> %s): " string | ||
| 90 | (query-replace-descr lastfrom) | ||
| 91 | (query-replace-descr lastto)) | ||
| 92 | (format "%s: " string)) | ||
| 78 | nil nil nil | 93 | nil nil nil |
| 79 | query-replace-from-history-variable | 94 | query-replace-from-history-variable |
| 80 | nil t)))) | 95 | nil t)))) |
| 81 | ;; Warn if user types \n or \t, but don't reject the input. | 96 | (if (and (zerop (length from)) lastto lastfrom) |
| 82 | (and regexp-flag | 97 | (cons lastfrom lastto) |
| 83 | (string-match "\\(\\`\\|[^\\]\\)\\(\\\\\\\\\\)*\\(\\\\[nt]\\)" from) | 98 | ;; Warn if user types \n or \t, but don't reject the input. |
| 84 | (let ((match (match-string 3 from))) | 99 | (and regexp-flag |
| 85 | (cond | 100 | (string-match "\\(\\`\\|[^\\]\\)\\(\\\\\\\\\\)*\\(\\\\[nt]\\)" from) |
| 86 | ((string= match "\\n") | 101 | (let ((match (match-string 3 from))) |
| 87 | (message "Note: `\\n' here doesn't match a newline; to do that, type C-q C-j instead")) | 102 | (cond |
| 88 | ((string= match "\\t") | 103 | ((string= match "\\n") |
| 89 | (message "Note: `\\t' here doesn't match a tab; to do that, just type TAB"))) | 104 | (message "Note: `\\n' here doesn't match a newline; to do that, type C-q C-j instead")) |
| 90 | (sit-for 2))) | 105 | ((string= match "\\t") |
| 91 | from))) | 106 | (message "Note: `\\t' here doesn't match a tab; to do that, just type TAB"))) |
| 107 | (sit-for 2))) | ||
| 108 | from)))) | ||
| 92 | 109 | ||
| 93 | (defun query-replace-read-to (from string regexp-flag) | 110 | (defun query-replace-read-to (from string regexp-flag) |
| 94 | "Query and return the `from' argument of a query-replace operation." | 111 | "Query and return the `from' argument of a query-replace operation." |
| 95 | (let ((to (save-excursion | 112 | (let ((to (save-excursion |
| 96 | (read-from-minibuffer | 113 | (read-from-minibuffer |
| 97 | (format "%s %s with: " string from) | 114 | (format "%s %s with: " string (query-replace-descr from)) |
| 98 | nil nil nil | 115 | nil nil nil |
| 99 | query-replace-to-history-variable from t)))) | 116 | query-replace-to-history-variable from t)))) |
| 100 | (when (and regexp-flag | 117 | (when (and regexp-flag |
| @@ -137,7 +154,8 @@ strings or patterns." | |||
| 137 | (unless noerror | 154 | (unless noerror |
| 138 | (barf-if-buffer-read-only)) | 155 | (barf-if-buffer-read-only)) |
| 139 | (let* ((from (query-replace-read-from string regexp-flag)) | 156 | (let* ((from (query-replace-read-from string regexp-flag)) |
| 140 | (to (query-replace-read-to from string regexp-flag))) | 157 | (to (if (consp from) (prog1 (cdr from) (setq from (car from))) |
| 158 | (query-replace-read-to from string regexp-flag)))) | ||
| 141 | (list from to current-prefix-arg))) | 159 | (list from to current-prefix-arg))) |
| 142 | 160 | ||
| 143 | (defun query-replace (from-string to-string &optional delimited start end) | 161 | (defun query-replace (from-string to-string &optional delimited start end) |
| @@ -269,14 +287,16 @@ Third arg DELIMITED (prefix arg if interactive), if non-nil, means replace | |||
| 269 | only matches that are surrounded by word boundaries. | 287 | only matches that are surrounded by word boundaries. |
| 270 | Fourth and fifth arg START and END specify the region to operate on." | 288 | Fourth and fifth arg START and END specify the region to operate on." |
| 271 | (interactive | 289 | (interactive |
| 272 | (let* ((from (if query-replace-interactive | 290 | (barf-if-buffer-read-only) |
| 273 | (car regexp-search-ring) | 291 | (let* ((from |
| 274 | (read-from-minibuffer "Query replace regexp: " | 292 | ;; Let-bind the history var to disable the "foo -> bar" default. |
| 275 | nil nil nil | 293 | ;; Maybe we shouldn't disable this default, but for now I'll |
| 276 | query-replace-from-history-variable | 294 | ;; leave it off. --Stef |
| 277 | nil t))) | 295 | (let ((query-replace-to-history-variable nil)) |
| 296 | (query-replace-read-from "Query replace regexp" t))) | ||
| 278 | (to (list (read-from-minibuffer | 297 | (to (list (read-from-minibuffer |
| 279 | (format "Query replace regexp %s with eval: " from) | 298 | (format "Query replace regexp %s with eval: " |
| 299 | (query-replace-descr from)) | ||
| 280 | nil nil t query-replace-to-history-variable from t)))) | 300 | nil nil t query-replace-to-history-variable from t)))) |
| 281 | ;; We make TO a list because replace-match-string-symbols requires one, | 301 | ;; We make TO a list because replace-match-string-symbols requires one, |
| 282 | ;; and the user might enter a single token. | 302 | ;; and the user might enter a single token. |
| @@ -317,7 +337,7 @@ Fourth and fifth arg START and END specify the region to operate on." | |||
| 317 | 'query-replace-history nil t))) | 337 | 'query-replace-history nil t))) |
| 318 | (to (read-from-minibuffer | 338 | (to (read-from-minibuffer |
| 319 | (format "Query replace %s with (space-separated strings): " | 339 | (format "Query replace %s with (space-separated strings): " |
| 320 | from) | 340 | (query-replace-descr from)) |
| 321 | nil nil nil | 341 | nil nil nil |
| 322 | 'query-replace-history from t))) | 342 | 'query-replace-history from t))) |
| 323 | (list from to | 343 | (list from to |
| @@ -760,7 +780,7 @@ If the value is nil, don't highlight the buffer names specially." | |||
| 760 | (read-from-minibuffer | 780 | (read-from-minibuffer |
| 761 | (if default | 781 | (if default |
| 762 | (format "List lines matching regexp (default `%s'): " | 782 | (format "List lines matching regexp (default `%s'): " |
| 763 | default) | 783 | (query-replace-descr default)) |
| 764 | "List lines matching regexp: ") | 784 | "List lines matching regexp: ") |
| 765 | nil | 785 | nil |
| 766 | nil | 786 | nil |
| @@ -1538,5 +1558,5 @@ make, or the user didn't cancel the call." | |||
| 1538 | (if (facep 'query-replace) | 1558 | (if (facep 'query-replace) |
| 1539 | 'query-replace 'region))))) | 1559 | 'query-replace 'region))))) |
| 1540 | 1560 | ||
| 1541 | ;;; arch-tag: 16b4cd61-fd40-497b-b86f-b667c4cf88e4 | 1561 | ;; arch-tag: 16b4cd61-fd40-497b-b86f-b667c4cf88e4 |
| 1542 | ;;; replace.el ends here | 1562 | ;;; replace.el ends here |