comparison lisp/replace.el @ 90428:a8190f7e546e

Merge from emacs--devo--0 Patches applied: * emacs--devo--0 (patch 285-296) - Update from CVS - Merge from gnus--rel--5.10 - Update from CVS: admin/FOR-RELEASE: Update refcard section. * gnus--rel--5.10 (patch 102-104) - Update from CVS Revision: emacs@sv.gnu.org/emacs--unicode--0--patch-64
author Miles Bader <miles@gnu.org>
date Wed, 07 Jun 2006 18:05:10 +0000
parents 146cd8369025 21306dd42b3c
children 858cb33ae39d
comparison
equal deleted inserted replaced
90427:ddb25860d044 90428:a8190f7e546e
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* ((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)
126 (let ((match (match-string 3 from))) 125 (let ((match (match-string 3 from)))
127 (cond 126 (cond
175 174
176 (defun query-replace-read-to (from prompt regexp-flag) 175 (defun query-replace-read-to (from prompt regexp-flag)
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)
187 (unless noerror 190 (unless noerror
188 (barf-if-buffer-read-only)) 191 (barf-if-buffer-read-only))