;;; replace.el --- replace commands for Emacs
;; Copyright (C) 1985, 1986, 1987, 1992, 1994, 1996, 1997, 2000, 2001,
-;; 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
+;; 2002, 2003, 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
;; Maintainer: FSF
(defvar query-replace-history nil)
+(defvar query-replace-defaults nil
+ "Default values of FROM-STRING and TO-STRING for `query-replace'.
+This is a cons cell (FROM-STRING . TO-STRING), or nil if there is
+no default value.")
+
(defvar query-replace-interactive nil
"Non-nil means `query-replace' uses the last search string.
That becomes the \"string to replace\".")
wants to replace FROM with TO."
(if query-replace-interactive
(car (if regexp-flag regexp-search-ring search-ring))
- (let* ((lastfrom (car (symbol-value query-replace-from-history-variable)))
- (lastto (car (symbol-value query-replace-to-history-variable)))
+ (let* ((history-add-new-input nil)
(from
;; The save-excursion here is in case the user marks and copies
;; a region in order to specify the minibuffer input.
;; That should not clobber the region for the query-replace itself.
(save-excursion
- (when (equal lastfrom lastto)
- ;; Typically, this is because the two histlists are shared.
- (setq lastfrom (cadr (symbol-value
- query-replace-from-history-variable))))
(read-from-minibuffer
- (if (and lastto lastfrom)
+ (if query-replace-defaults
(format "%s (default %s -> %s): " prompt
- (query-replace-descr lastfrom)
- (query-replace-descr lastto))
+ (query-replace-descr (car query-replace-defaults))
+ (query-replace-descr (cdr query-replace-defaults)))
(format "%s: " prompt))
nil nil nil
query-replace-from-history-variable
- nil t t))))
- (if (and (zerop (length from)) lastto lastfrom)
- (progn
- (set query-replace-from-history-variable
- (cdr (symbol-value query-replace-from-history-variable)))
- (cons lastfrom
- (query-replace-compile-replacement lastto regexp-flag)))
+ nil t))))
+ (if (and (zerop (length from)) query-replace-defaults)
+ (cons (car query-replace-defaults)
+ (query-replace-compile-replacement
+ (cdr query-replace-defaults) regexp-flag))
+ (add-to-history query-replace-from-history-variable from nil t)
;; Warn if user types \n or \t, but don't reject the input.
(and regexp-flag
(string-match "\\(\\`\\|[^\\]\\)\\(\\\\\\\\\\)*\\(\\\\[nt]\\)" from)
"Query and return the `to' argument of a query-replace operation."
(query-replace-compile-replacement
(save-excursion
- (read-from-minibuffer
- (format "%s %s with: " prompt (query-replace-descr from))
- nil nil nil
- query-replace-to-history-variable from t t))
+ (let* ((history-add-new-input nil)
+ (to (read-from-minibuffer
+ (format "%s %s with: " prompt (query-replace-descr from))
+ nil nil nil
+ query-replace-to-history-variable from t)))
+ (add-to-history query-replace-to-history-variable to nil t)
+ (setq query-replace-defaults (cons from to))
+ to))
regexp-flag))
(defun query-replace-read-args (prompt regexp-flag &optional noerror)
In interactive calls, the replacement text may contain `\\,'
followed by a Lisp expression used as part of the replacement
text. Inside of that expression, `\\&' is a string denoting the
-whole match, `\\N' a partial matches, `\\#&' and `\\#N' the
-respective numeric values from `string-to-number', and `\\#'
-itself for `replace-count', the number of replacements occured so
-far.
+whole match, `\\N' a partial match, `\\#&' and `\\#N' the respective
+numeric values from `string-to-number', and `\\#' itself for
+`replace-count', the number of replacements occurred so far.
If your Lisp expression is an identifier and the next letter in
the replacement string would be interpreted as part of it, you
(endpt nil)
(marker nil)
(curstring "")
+ (inhibit-field-text-motion t)
(headerpt (with-current-buffer out-buf (point))))
(with-current-buffer buf
(or coding
# replace-count
Note that these symbols must be preceeded by a backslash in order to
-type them."
- (while n
+type them using Lisp syntax."
+ (while (consp n)
(cond
((consp (car n))
(replace-match-string-symbols (car n))) ;Process sub-list
(defun replace-match-data (integers reuse &optional new)
"Like `match-data', but markers in REUSE get invalidated.
-If NEW is non-NIL, it is set and returned instead of fresh data,
+If NEW is non-nil, it is set and returned instead of fresh data,
but coerced to the correct value of INTEGERS."
(or (and new
(progn
check for `\\?' is made to save time. MATCH-DATA is used for the
replacement. In case editing is done, it is changed to use markers.
-The return value is non-NIL if there has been no `\\?' or NOEDIT was
+The return value is non-nil if there has been no `\\?' or NOEDIT was
passed in. If LITERAL is set, no checking is done, anyway."
(unless (or literal noedit)
(setq noedit t)
(search-string from-string)
(real-match-data nil) ; the match data for the current match
(next-replacement nil)
+ ;; This is non-nil if we know there is nothing for the user
+ ;; to edit in the replacement.
(noedit nil)
(keep-going t)
(stack nil)
;; For speed, use only integers and
;; reuse the list used last time.
(replace-match-data t real-match-data)))))
-
+
;; Record whether the match is nonempty, to avoid an infinite loop
;; repeatedly matching the same empty string.
(setq nonempty-match
(set-match-data real-match-data)
(setq next-replacement
(funcall (car replacements) (cdr replacements)
- replace-count)
- noedit nil))
+ replace-count)))
(if (not query-flag)
(let ((inhibit-read-only
query-replace-skip-read-only))