comparison lisp/replace.el @ 71025:82d1f472baee

* replace.el (query-replace-defaults): New variable. (query-replace-read-from): Use `query-replace-defaults' for default value, instead of history list. (query-replace-read-to): Update `query-replace-defaults'.
author Chong Yidong <cyd@stupidchicken.com>
date Sun, 28 May 2006 17:02:37 +0000
parents 83ce2ddffd16
children b5294b4387c4
comparison
equal deleted inserted replaced
71024:947a5b1dd3b6 71025:82d1f472baee
33 "*Non-nil means `query-replace' should preserve case in replacements." 33 "*Non-nil means `query-replace' should preserve case in replacements."
34 :type 'boolean 34 :type 'boolean
35 :group 'matching) 35 :group 'matching)
36 36
37 (defvar query-replace-history nil) 37 (defvar query-replace-history nil)
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.")
38 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\".")
42 47
92 "Query and return the `from' argument of a query-replace operation. 97 "Query and return the `from' argument of a query-replace operation.
93 The return value can also be a pair (FROM . TO) indicating that the user 98 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 ((from
98 (lastto (car (symbol-value query-replace-to-history-variable)))
99 (from
100 ;; The save-excursion here is in case the user marks and copies 103 ;; The save-excursion here is in case the user marks and copies
101 ;; a region in order to specify the minibuffer input. 104 ;; a region in order to specify the minibuffer input.
102 ;; That should not clobber the region for the query-replace itself. 105 ;; That should not clobber the region for the query-replace itself.
103 (save-excursion 106 (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 107 (read-from-minibuffer
109 (if (and lastto lastfrom) 108 (if query-replace-defaults
110 (format "%s (default %s -> %s): " prompt 109 (format "%s (default %s -> %s): " prompt
111 (query-replace-descr lastfrom) 110 (query-replace-descr (car query-replace-defaults))
112 (query-replace-descr lastto)) 111 (query-replace-descr (cdr query-replace-defaults)))
113 (format "%s: " prompt)) 112 (format "%s: " prompt))
114 nil nil nil 113 nil nil nil
115 query-replace-from-history-variable 114 query-replace-from-history-variable
116 nil t)))) 115 nil t))))
117 (if (and (zerop (length from)) lastto lastfrom) 116 (if (and (zerop (length from)) query-replace-defaults)
118 (progn 117 (progn
119 (set query-replace-from-history-variable 118 (set query-replace-from-history-variable
120 (cdr (symbol-value query-replace-from-history-variable))) 119 (cdr (symbol-value query-replace-from-history-variable)))
121 (cons lastfrom 120 (cons (car query-replace-defaults)
122 (query-replace-compile-replacement lastto regexp-flag))) 121 (query-replace-compile-replacement
122 (cdr query-replace-defaults) regexp-flag)))
123 ;; Warn if user types \n or \t, but don't reject the input. 123 ;; Warn if user types \n or \t, but don't reject the input.
124 (and regexp-flag 124 (and regexp-flag
125 (string-match "\\(\\`\\|[^\\]\\)\\(\\\\\\\\\\)*\\(\\\\[nt]\\)" from) 125 (string-match "\\(\\`\\|[^\\]\\)\\(\\\\\\\\\\)*\\(\\\\[nt]\\)" from)
126 (let ((match (match-string 3 from))) 126 (let ((match (match-string 3 from)))
127 (cond 127 (cond
175 175
176 (defun query-replace-read-to (from prompt regexp-flag) 176 (defun query-replace-read-to (from prompt regexp-flag)
177 "Query and return the `to' argument of a query-replace operation." 177 "Query and return the `to' argument of a query-replace operation."
178 (query-replace-compile-replacement 178 (query-replace-compile-replacement
179 (save-excursion 179 (save-excursion
180 (read-from-minibuffer 180 (let ((to (read-from-minibuffer
181 (format "%s %s with: " prompt (query-replace-descr from)) 181 (format "%s %s with: " prompt (query-replace-descr from))
182 nil nil nil 182 nil nil nil
183 query-replace-to-history-variable from t)) 183 query-replace-to-history-variable from t)))
184 (setq query-replace-defaults (cons from to))
185 to))
184 regexp-flag)) 186 regexp-flag))
185 187
186 (defun query-replace-read-args (prompt regexp-flag &optional noerror) 188 (defun query-replace-read-args (prompt regexp-flag &optional noerror)
187 (unless noerror 189 (unless noerror
188 (barf-if-buffer-read-only)) 190 (barf-if-buffer-read-only))