aboutsummaryrefslogtreecommitdiffstats
path: root/lisp/replace.el
diff options
context:
space:
mode:
authorJuri Linkov2014-11-08 01:33:41 +0200
committerJuri Linkov2014-11-08 01:33:41 +0200
commit2b513c3b1c67e16813118f7682cc088ecd070e07 (patch)
treee9a10bf4eb27a74cf5c6ce2bd8d87bbeb34e2859 /lisp/replace.el
parent3946aeb962440b8e76fea95c31aa518928082cc6 (diff)
downloademacs-2b513c3b1c67e16813118f7682cc088ecd070e07.tar.gz
emacs-2b513c3b1c67e16813118f7682cc088ecd070e07.zip
* lisp/replace.el: History for query replace pairs.
(query-replace-defaults): Promote to a list of cons cell. Doc fix. (query-replace-from-to-separator): New variable. (query-replace-read-from): Let-bind query-replace-from-to-history to a list of FROM-TO strings created from query-replace-defaults and separated by query-replace-from-to-separator. Use it as the history while reading from the minibuffer. Split the returned string by the separator to get FROM and TO parts, and add them to the history variables. (query-replace-read-to): Add FROM-TO pairs to query-replace-defaults. (query-replace-regexp-eval): Let-bind query-replace-defaults to nil. http://lists.gnu.org/archive/html/emacs-devel/2014-11/msg00253.html * lisp/isearch.el (isearch-text-char-description): Keep characters intact and put formatted strings with the `display' property.
Diffstat (limited to 'lisp/replace.el')
-rw-r--r--lisp/replace.el72
1 files changed, 48 insertions, 24 deletions
diff --git a/lisp/replace.el b/lisp/replace.el
index 32cf89b3255..c7fbcb5d99c 100644
--- a/lisp/replace.el
+++ b/lisp/replace.el
@@ -56,8 +56,8 @@ See `query-replace-from-history-variable' and
56 56
57(defvar query-replace-defaults nil 57(defvar query-replace-defaults nil
58 "Default values of FROM-STRING and TO-STRING for `query-replace'. 58 "Default values of FROM-STRING and TO-STRING for `query-replace'.
59This is a cons cell (FROM-STRING . TO-STRING), or nil if there is 59This is a list of cons cells (FROM-STRING . TO-STRING), or nil
60no default value.") 60if there are no default values.")
61 61
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.
@@ -67,6 +67,12 @@ That becomes the \"string to replace\".")
67to the minibuffer that reads the string to replace, or invoke replacements 67to the minibuffer that reads the string to replace, or invoke replacements
68from Isearch by using a key sequence like `C-s C-s M-%'." "24.3") 68from Isearch by using a key sequence like `C-s C-s M-%'." "24.3")
69 69
70(defvar query-replace-from-to-separator
71 (propertize "\0"
72 'display (propertize " \u2192 " 'face 'minibuffer-prompt)
73 'separator t)
74 "String that separates FROM and TO in the history of replacement pairs.")
75
70(defcustom query-replace-from-history-variable 'query-replace-history 76(defcustom query-replace-from-history-variable 'query-replace-history
71 "History list to use for the FROM argument of `query-replace' commands. 77 "History list to use for the FROM argument of `query-replace' commands.
72The value of this variable should be a symbol; that symbol 78The value of this variable should be a symbol; that symbol
@@ -132,11 +138,19 @@ wants to replace FROM with TO."
132 (if query-replace-interactive 138 (if query-replace-interactive
133 (car (if regexp-flag regexp-search-ring search-ring)) 139 (car (if regexp-flag regexp-search-ring search-ring))
134 (let* ((history-add-new-input nil) 140 (let* ((history-add-new-input nil)
141 (query-replace-from-to-history
142 (append
143 (when query-replace-from-to-separator
144 (mapcar (lambda (from-to)
145 (concat (query-replace-descr (car from-to))
146 query-replace-from-to-separator
147 (query-replace-descr (cdr from-to))))
148 query-replace-defaults))
149 (symbol-value query-replace-from-history-variable)))
150 (minibuffer-allow-text-properties t) ; separator uses text-properties
135 (prompt 151 (prompt
136 (if query-replace-defaults 152 (if (and query-replace-defaults query-replace-from-to-separator)
137 (format "%s (default %s -> %s): " prompt 153 (format "%s (default %s): " prompt (car query-replace-from-to-history))
138 (query-replace-descr (car query-replace-defaults))
139 (query-replace-descr (cdr query-replace-defaults)))
140 (format "%s: " prompt))) 154 (format "%s: " prompt)))
141 (from 155 (from
142 ;; The save-excursion here is in case the user marks and copies 156 ;; The save-excursion here is in case the user marks and copies
@@ -144,26 +158,36 @@ wants to replace FROM with TO."
144 ;; That should not clobber the region for the query-replace itself. 158 ;; That should not clobber the region for the query-replace itself.
145 (save-excursion 159 (save-excursion
146 (if regexp-flag 160 (if regexp-flag
147 (read-regexp prompt nil query-replace-from-history-variable) 161 (read-regexp prompt nil 'query-replace-from-to-history)
148 (read-from-minibuffer 162 (read-from-minibuffer
149 prompt nil nil nil query-replace-from-history-variable 163 prompt nil nil nil 'query-replace-from-to-history
150 (car (if regexp-flag regexp-search-ring search-ring)) t))))) 164 (car (if regexp-flag regexp-search-ring search-ring)) t)))))
151 (if (and (zerop (length from)) query-replace-defaults) 165 (if (and (zerop (length from)) query-replace-defaults)
152 (cons (car query-replace-defaults) 166 (cons (caar query-replace-defaults)
153 (query-replace-compile-replacement 167 (query-replace-compile-replacement
154 (cdr query-replace-defaults) regexp-flag)) 168 (cdar query-replace-defaults) regexp-flag))
155 (add-to-history query-replace-from-history-variable from nil t) 169 (let* ((to (if (and (string-match query-replace-from-to-separator from)
156 ;; Warn if user types \n or \t, but don't reject the input. 170 (get-text-property (match-beginning 0) 'separator from))
157 (and regexp-flag 171 (query-replace-compile-replacement
158 (string-match "\\(\\`\\|[^\\]\\)\\(\\\\\\\\\\)*\\(\\\\[nt]\\)" from) 172 (substring-no-properties from (match-end 0)) regexp-flag)))
159 (let ((match (match-string 3 from))) 173 (from (if to (substring-no-properties from 0 (match-beginning 0))
160 (cond 174 (substring-no-properties from))))
161 ((string= match "\\n") 175 (add-to-history query-replace-from-history-variable from nil t)
162 (message "Note: `\\n' here doesn't match a newline; to do that, type C-q C-j instead")) 176 ;; Warn if user types \n or \t, but don't reject the input.
163 ((string= match "\\t") 177 (and regexp-flag
164 (message "Note: `\\t' here doesn't match a tab; to do that, just type TAB"))) 178 (string-match "\\(\\`\\|[^\\]\\)\\(\\\\\\\\\\)*\\(\\\\[nt]\\)" from)
165 (sit-for 2))) 179 (let ((match (match-string 3 from)))
166 from)))) 180 (cond
181 ((string= match "\\n")
182 (message "Note: `\\n' here doesn't match a newline; to do that, type C-q C-j instead"))
183 ((string= match "\\t")
184 (message "Note: `\\t' here doesn't match a tab; to do that, just type TAB")))
185 (sit-for 2)))
186 (if (not to)
187 from
188 (add-to-history query-replace-to-history-variable to nil t)
189 (add-to-history 'query-replace-defaults (cons from to) nil t)
190 (cons from to)))))))
167 191
168(defun query-replace-compile-replacement (to regexp-flag) 192(defun query-replace-compile-replacement (to regexp-flag)
169 "Maybe convert a regexp replacement TO to Lisp. 193 "Maybe convert a regexp replacement TO to Lisp.
@@ -216,7 +240,7 @@ the original string if not."
216 nil nil nil 240 nil nil nil
217 query-replace-to-history-variable from t))) 241 query-replace-to-history-variable from t)))
218 (add-to-history query-replace-to-history-variable to nil t) 242 (add-to-history query-replace-to-history-variable to nil t)
219 (setq query-replace-defaults (cons from to)) 243 (add-to-history 'query-replace-defaults (cons from to) nil t)
220 to)) 244 to))
221 regexp-flag)) 245 regexp-flag))
222 246
@@ -421,7 +445,7 @@ for Lisp calls." "22.1"))
421 ;; Let-bind the history var to disable the "foo -> bar" 445 ;; Let-bind the history var to disable the "foo -> bar"
422 ;; default. Maybe we shouldn't disable this default, but 446 ;; default. Maybe we shouldn't disable this default, but
423 ;; for now I'll leave it off. --Stef 447 ;; for now I'll leave it off. --Stef
424 (let ((query-replace-to-history-variable nil)) 448 (let ((query-replace-defaults nil))
425 (query-replace-read-from "Query replace regexp" t))) 449 (query-replace-read-from "Query replace regexp" t)))
426 (to (list (read-from-minibuffer 450 (to (list (read-from-minibuffer
427 (format "Query replace regexp %s with eval: " 451 (format "Query replace regexp %s with eval: "