diff options
Diffstat (limited to 'lisp/replace.el')
| -rw-r--r-- | lisp/replace.el | 43 |
1 files changed, 23 insertions, 20 deletions
diff --git a/lisp/replace.el b/lisp/replace.el index f1792b499fc..2f8fe86860c 100644 --- a/lisp/replace.el +++ b/lisp/replace.el | |||
| @@ -36,6 +36,11 @@ | |||
| 36 | 36 | ||
| 37 | (defvar query-replace-history nil) | 37 | (defvar query-replace-history nil) |
| 38 | 38 | ||
| 39 | (defvar query-replace-defaults nil | ||
| 40 | "Default values of FROM-STRING and TO-STRING for `query-replace'. | ||
| 41 | This is a cons cell (FROM-STRING . TO-STRING), or nil if there is | ||
| 42 | no default value.") | ||
| 43 | |||
| 39 | (defvar query-replace-interactive nil | 44 | (defvar query-replace-interactive nil |
| 40 | "Non-nil means `query-replace' uses the last search string. | 45 | "Non-nil means `query-replace' uses the last search string. |
| 41 | That becomes the \"string to replace\".") | 46 | That becomes the \"string to replace\".") |
| @@ -94,32 +99,26 @@ The return value can also be a pair (FROM . TO) indicating that the user | |||
| 94 | wants to replace FROM with TO." | 99 | wants to replace FROM with TO." |
| 95 | (if query-replace-interactive | 100 | (if query-replace-interactive |
| 96 | (car (if regexp-flag regexp-search-ring search-ring)) | 101 | (car (if regexp-flag regexp-search-ring search-ring)) |
| 97 | (let* ((lastfrom (car (symbol-value query-replace-from-history-variable))) | 102 | (let* ((history-add-new-input nil) |
| 98 | (lastto (car (symbol-value query-replace-to-history-variable))) | ||
| 99 | (from | 103 | (from |
| 100 | ;; The save-excursion here is in case the user marks and copies | 104 | ;; The save-excursion here is in case the user marks and copies |
| 101 | ;; a region in order to specify the minibuffer input. | 105 | ;; a region in order to specify the minibuffer input. |
| 102 | ;; That should not clobber the region for the query-replace itself. | 106 | ;; That should not clobber the region for the query-replace itself. |
| 103 | (save-excursion | 107 | (save-excursion |
| 104 | (when (equal lastfrom lastto) | ||
| 105 | ;; Typically, this is because the two histlists are shared. | ||
| 106 | (setq lastfrom (cadr (symbol-value | ||
| 107 | query-replace-from-history-variable)))) | ||
| 108 | (read-from-minibuffer | 108 | (read-from-minibuffer |
| 109 | (if (and lastto lastfrom) | 109 | (if query-replace-defaults |
| 110 | (format "%s (default %s -> %s): " prompt | 110 | (format "%s (default %s -> %s): " prompt |
| 111 | (query-replace-descr lastfrom) | 111 | (query-replace-descr (car query-replace-defaults)) |
| 112 | (query-replace-descr lastto)) | 112 | (query-replace-descr (cdr query-replace-defaults))) |
| 113 | (format "%s: " prompt)) | 113 | (format "%s: " prompt)) |
| 114 | nil nil nil | 114 | nil nil nil |
| 115 | query-replace-from-history-variable | 115 | query-replace-from-history-variable |
| 116 | nil t t)))) | 116 | nil t)))) |
| 117 | (if (and (zerop (length from)) lastto lastfrom) | 117 | (if (and (zerop (length from)) query-replace-defaults) |
| 118 | (progn | 118 | (cons (car query-replace-defaults) |
| 119 | (set query-replace-from-history-variable | 119 | (query-replace-compile-replacement |
| 120 | (cdr (symbol-value query-replace-from-history-variable))) | 120 | (cdr query-replace-defaults) regexp-flag)) |
| 121 | (cons lastfrom | 121 | (add-to-history query-replace-from-history-variable from nil t) |
| 122 | (query-replace-compile-replacement lastto regexp-flag))) | ||
| 123 | ;; Warn if user types \n or \t, but don't reject the input. | 122 | ;; Warn if user types \n or \t, but don't reject the input. |
| 124 | (and regexp-flag | 123 | (and regexp-flag |
| 125 | (string-match "\\(\\`\\|[^\\]\\)\\(\\\\\\\\\\)*\\(\\\\[nt]\\)" from) | 124 | (string-match "\\(\\`\\|[^\\]\\)\\(\\\\\\\\\\)*\\(\\\\[nt]\\)" from) |
| @@ -177,10 +176,14 @@ the original string if not." | |||
| 177 | "Query and return the `to' argument of a query-replace operation." | 176 | "Query and return the `to' argument of a query-replace operation." |
| 178 | (query-replace-compile-replacement | 177 | (query-replace-compile-replacement |
| 179 | (save-excursion | 178 | (save-excursion |
| 180 | (read-from-minibuffer | 179 | (let* ((history-add-new-input nil) |
| 181 | (format "%s %s with: " prompt (query-replace-descr from)) | 180 | (to (read-from-minibuffer |
| 182 | nil nil nil | 181 | (format "%s %s with: " prompt (query-replace-descr from)) |
| 183 | query-replace-to-history-variable from t t)) | 182 | nil nil nil |
| 183 | query-replace-to-history-variable from t))) | ||
| 184 | (add-to-history query-replace-to-history-variable to nil t) | ||
| 185 | (setq query-replace-defaults (cons from to)) | ||
| 186 | to)) | ||
| 184 | regexp-flag)) | 187 | regexp-flag)) |
| 185 | 188 | ||
| 186 | (defun query-replace-read-args (prompt regexp-flag &optional noerror) | 189 | (defun query-replace-read-args (prompt regexp-flag &optional noerror) |