unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: Juri Linkov <juri@jurta.org>
To: emacs-devel@gnu.org
Subject: Re: History for query replace pairs
Date: Tue, 04 Nov 2014 01:46:47 +0200	[thread overview]
Message-ID: <87ioivx2xk.fsf@mail.jurta.org> (raw)
In-Reply-To: <87sii0becx.fsf@lifelogs.com> (Ted Zlatanov's message of "Mon, 03 Nov 2014 08:30:54 -0500")

> This feature seems stalled, is this issue the only thing blocking it?

The only blocking thing is to decide whether to move through the history
of replacements using M-p or M-n.  This patch implements a sequence of M-n
to insert replacement pairs, but it's easy to change just one line from

(read-from-minibuffer
 prompt nil nil nil query-replace-from-history-variable defaults t)))))

to

(read-from-minibuffer
 prompt nil nil nil 'defaults nil t)

then M-p will move through replacement pairs instead of previous
single history strings `from' or `to'.

Maybe this choice should be customizable?

=== modified file 'lisp/replace.el'
--- lisp/replace.el	2014-08-25 02:36:45 +0000
+++ lisp/replace.el	2014-11-03 23:27:28 +0000
@@ -56,8 +56,8 @@ (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.")
+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,13 @@ (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")
 
+(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 +139,15 @@ (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)
+	   (defaults (mapcar (lambda (from-to)
+	   		       (concat (query-replace-descr (car from-to))
+				       query-replace-from-to-separator
+				       (query-replace-descr (cdr from-to))))
+	   		     query-replace-defaults))
 	   (prompt
-	    (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 defaults
+		(format "%s (default %s): " prompt
+		        (car defaults))
 	      (format "%s: " prompt)))
 	   (from
 	    ;; The save-excursion here is in case the user marks and copies
@@ -144,14 +155,18 @@ (defun query-replace-read-from (prompt r
 	    ;; That should not clobber the region for the query-replace itself.
 	    (save-excursion
 	      (if regexp-flag
-		  (read-regexp prompt nil query-replace-from-history-variable)
+		  (read-regexp prompt defaults query-replace-from-history-variable)
 		(read-from-minibuffer
-		 prompt nil nil nil query-replace-from-history-variable
-		 (car (if regexp-flag regexp-search-ring search-ring)) t)))))
+		 prompt nil nil nil query-replace-from-history-variable
+		 defaults t)))))
       (if (and (zerop (length from)) query-replace-defaults)
-	  (cons (car query-replace-defaults)
+	  (cons (caar query-replace-defaults)
+		(query-replace-compile-replacement
+		 (cdar query-replace-defaults) regexp-flag))
+	(let* ((to (if (string-match query-replace-from-to-separator from)
 		(query-replace-compile-replacement
-		 (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 +178,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-defaults)
+	    (cons from to)))))))
 
 (defun query-replace-compile-replacement (to regexp-flag)
   "Maybe convert a regexp replacement TO to Lisp.
@@ -216,7 +235,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-defaults)
        to))
    regexp-flag))
 


> You could use a Unicode character (RIGHT ARROW → for instance), it's
> unlikely to occur in a normal search and replace, and could be specially
> encoded to "→→" in those cases. So you'd store "X→→Y→A→→→→B" to record
> FROM="X→Y" and TO="A→→B".  It's an unambiguous encoding.  If that's too
> hacky, let's find another way, and let me know how I can help.

The patch uses "\0" as a separator that is highly unlikely to occur in
the replacement strings.

Also for better displaying of special characters in the minibuffer,
the following patch modifies isearch-text-char-description used by
query-replace-descr to put a formatted display property on special characters
instead of changing them.  This patch also works better for isearch,
because it keep the faces on the special characters in the failed string.

=== modified file 'lisp/isearch.el'
--- lisp/isearch.el	2014-09-04 16:14:26 +0000
+++ lisp/isearch.el	2014-11-03 23:22:06 +0000
@@ -2873,8 +2873,12 @@ (defun isearch-no-upper-case-p (string r
 
 (defun isearch-text-char-description (c)
   (cond
-   ((< c ?\s) (propertize (format "^%c" (+ c 64)) 'face 'escape-glyph))
-   ((= c ?\^?) (propertize "^?" 'face 'escape-glyph))
+   ((< c ?\s) (propertize
+	       (char-to-string c)
+	       'display (propertize (format "^%c" (+ c 64)) 'face 'escape-glyph)))
+   ((= c ?\^?) (propertize
+		(char-to-string c)
+		'display (propertize "^?" 'face 'escape-glyph)))
    (t (char-to-string c))))
 
 ;; General function to unread characters or events.




  reply	other threads:[~2014-11-03 23:46 UTC|newest]

Thread overview: 67+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-08-08 20:20 History for query replace pairs Tom
2014-08-08 20:28 ` Drew Adams
2014-08-08 23:38 ` Juri Linkov
2014-08-09  5:35   ` Herring, Davis
2014-08-10  1:18   ` Josh
2014-08-10  5:59     ` Tom
2014-08-11 18:34       ` Tom
2014-10-04 21:45         ` Ted Zlatanov
2014-10-04 23:36           ` Juri Linkov
2014-10-05  1:52             ` Yuri Khan
2014-10-05 21:54               ` Juri Linkov
2014-10-05  7:09             ` Andreas Schwab
2014-10-05  1:52           ` Stefan Monnier
2014-10-05  5:59           ` Tom
2014-10-06  0:46             ` Ted Zlatanov
2014-10-06 20:17               ` Tom
2014-10-06 22:35               ` Juri Linkov
2014-10-07 22:11                 ` Juri Linkov
2014-10-14 17:14                   ` Stefan Monnier
2014-10-14 19:02                     ` Juri Linkov
2014-10-14 19:13                       ` Alan Mackenzie
2014-10-14 19:44                         ` Juri Linkov
2014-10-14 20:15                           ` Alan Mackenzie
2014-10-14 20:16                           ` Drew Adams
2014-10-14 20:28                             ` Juri Linkov
2014-10-14 21:19                               ` Drew Adams
2014-10-14 20:05                         ` Andreas Schwab
2014-10-14 20:09                           ` Alan Mackenzie
2014-10-14 20:23                             ` Andreas Schwab
2014-10-21 18:23                       ` Stefan Monnier
2014-10-21 22:53                         ` Juri Linkov
2014-10-22 12:58                           ` Stefan Monnier
2014-10-23  9:06                             ` Artur Malabarba
2014-10-25 20:57                               ` Juri Linkov
2014-10-26  1:12                                 ` Artur Malabarba
2014-10-26  2:31                                   ` Stefan Monnier
2014-10-26  6:58                                   ` Andreas Schwab
2014-10-25 20:52                             ` Juri Linkov
2014-10-26  2:29                               ` Stefan Monnier
2014-10-26 23:27                                 ` Juri Linkov
2014-11-03 13:30                               ` Ted Zlatanov
2014-11-03 23:46                                 ` Juri Linkov [this message]
2014-11-04  0:59                                   ` Ted Zlatanov
2014-11-04 23:09                                     ` Juri Linkov
2014-11-05  1:55                                       ` Stefan Monnier
2014-11-05 23:20                                         ` Juri Linkov
2014-11-06  2:35                                           ` Stefan Monnier
2014-11-07 23:34                                             ` Juri Linkov
2014-11-08  0:59                                               ` Ted Zlatanov
2014-11-08  8:46                                                 ` Eli Zaretskii
2014-11-08 10:29                                                   ` Juri Linkov
2014-11-08 11:24                                                     ` Eli Zaretskii
2014-11-08 15:28                                                       ` Stefan Monnier
2014-11-08 17:29                                                         ` Eli Zaretskii
2014-11-08 22:52                                                         ` Juri Linkov
2014-11-09  2:01                                                           ` Stefan Monnier
2014-11-09 16:15                                                             ` Eli Zaretskii
2014-11-09 17:11                                                               ` Juri Linkov
2014-11-09 22:14                                                                 ` Stefan Monnier
2014-11-09 23:12                                                                   ` Juri Linkov
2014-11-09 22:10                                                               ` Stefan Monnier
2014-11-09  2:29                                                           ` Paul Eggert
2014-11-09 17:15                                                             ` Juri Linkov
2014-11-08 22:51                                                       ` Juri Linkov
2014-11-09 17:29                                                         ` Eli Zaretskii
2014-11-08 10:15                                                 ` Juri Linkov
2014-11-08  8:25                                               ` Eli Zaretskii

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87ioivx2xk.fsf@mail.jurta.org \
    --to=juri@jurta.org \
    --cc=emacs-devel@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).