[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: History for query replace pairs

From: Juri Linkov
Subject: Re: History for query replace pairs
Date: Wed, 05 Nov 2014 01:09:47 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3.50 (x86_64-pc-linux-gnu)

> I'd rather use M-p as the default because the new behavior is IMO much
> better.  But it's really no big deal, we can switch it later.
> JL> Maybe this choice should be customizable?
> OK with me, and probably most Emacs-like :)

Then a new customizable variable `query-replace-from-to-defaults'
could define whether to show previous replacement pairs as defaults
available via M-n, or via M-p if nil:

=== modified file 'lisp/replace.el'
--- lisp/replace.el     2014-08-25 02:36:45 +0000
+++ lisp/replace.el     2014-11-04 23:04:55 +0000
@@ -54,10 +54,10 @@ (defvar query-replace-history nil
 See `query-replace-from-history-variable' and
-(defvar query-replace-defaults nil
+(defvar query-replace-from-to-history 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.")
+This is a list of cons cells (FROM-STRING . TO-STRING),
+or nil if there is no default values.")
 (defvar query-replace-interactive nil
   "Non-nil means `query-replace' uses the last search string.
@@ -67,6 +67,19 @@ (make-obsolete-variable 'query-replace-i
 to the minibuffer that reads the string to replace, or invoke replacements
 from Isearch by using a key sequence like `C-s C-s M-%'." "24.3")
+(defcustom query-replace-from-to-defaults nil
+  "Non-nil means a list of replacement pairs is available by M-n instead of 
+  :group 'matching
+  :type 'boolean
+  :version "24.5")
+(defvar query-replace-from-to-separator
+  (propertize
+   "\0" 'display (propertize
+                 (if (char-displayable-p ?\u2192) " \u2192 " " -> ")
+                 'face 'minibuffer-prompt))
+  "String that separates FROM and TO in the default replacements.")
 (defcustom query-replace-from-history-variable 'query-replace-history
   "History list to use for the FROM argument of `query-replace' commands.
 The value of this variable should be a symbol; that symbol
@@ -132,11 +145,14 @@ (defun query-replace-read-from (prompt r
   (if query-replace-interactive
       (car (if regexp-flag regexp-search-ring search-ring))
     (let* ((history-add-new-input nil)
+          (from-to-history (mapcar (lambda (from-to)
+                              (concat (query-replace-descr (car from-to))
+                                      query-replace-from-to-separator
+                                      (query-replace-descr (cdr from-to))))
+                            query-replace-from-to-history))
-           (if query-replace-defaults
-               (format "%s (default %s -> %s): " prompt
-                       (query-replace-descr (car query-replace-defaults))
-                       (query-replace-descr (cdr query-replace-defaults)))
+           (if from-to-history
+               (format "%s (default %s): " prompt (car from-to-history))
              (format "%s: " prompt)))
            ;; The save-excursion here is in case the user marks and copies
@@ -144,14 +160,29 @@ (defun query-replace-read-from (prompt r
            ;; That should not clobber the region for the query-replace itself.
              (if regexp-flag
-                 (read-regexp prompt nil query-replace-from-history-variable)
+                 (read-regexp prompt
+                              (if query-replace-from-to-defaults
+                                  from-to-history)
+                              (if query-replace-from-to-defaults
+                                  query-replace-from-history-variable
+                                'from-to-history))
-                prompt nil nil nil query-replace-from-history-variable
-                (car (if regexp-flag regexp-search-ring search-ring)) t)))))
-      (if (and (zerop (length from)) query-replace-defaults)
-         (cons (car query-replace-defaults)
+                prompt nil nil nil
+                (if query-replace-from-to-defaults
+                    query-replace-from-history-variable
+                  'from-to-history)
+                (if query-replace-from-to-defaults
+                    from-to-history
+                  (car (if regexp-flag regexp-search-ring search-ring)))
+                t)))))
+      (if (and (zerop (length from)) query-replace-from-to-history)
+         (cons (caar query-replace-from-to-history)
+               (query-replace-compile-replacement
+                (cdar query-replace-from-to-history) regexp-flag))
+       (let* ((to (if (string-match query-replace-from-to-separator from)
-                (cdr query-replace-defaults) regexp-flag))
+                       (substring from (match-end 0)) regexp-flag)))
+              (from (if to (substring from 0 (match-beginning 0)) from)))
        (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
@@ -163,7 +194,11 @@ (defun query-replace-read-from (prompt r
                ((string= match "\\t")
                 (message "Note: `\\t' here doesn't match a tab; to do that, 
just type TAB")))
               (sit-for 2)))
-       from))))
+         (if (not to)
+             from
+           (add-to-history query-replace-to-history-variable to nil t)
+           (push (cons from to) query-replace-from-to-history)
+           (cons from to)))))))
 (defun query-replace-compile-replacement (to regexp-flag)
   "Maybe convert a regexp replacement TO to Lisp.
@@ -216,7 +251,7 @@ (defun query-replace-read-to (from promp
                 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))
+       (push (cons from to) query-replace-from-to-history)

reply via email to

[Prev in Thread] Current Thread [Next in Thread]