all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Tino Calancha <f92capac@gmail.com>
To: Lars Ingebrigtsen <larsi@gnus.org>
Cc: Tino Calancha <f92capac@gmail.com>, 21684@debbugs.gnu.org
Subject: bug#21684: 25.0.50; undo in query-replace w/o exit
Date: Wed, 24 Feb 2016 01:46:14 +0900 (JST)	[thread overview]
Message-ID: <alpine.LRH.2.20.1602240143580.15282@calancha-ilc.kek.jp> (raw)
In-Reply-To: <87wppvyg3p.fsf@gnus.org>

[-- Attachment #1: Type: text/plain, Size: 142 bytes --]


> Looks good.  Could you also add documentation for this (to the manual)
> and a NEWS entry?
Sure. Added NEWS entry and updated emacs manual.

[-- Attachment #2: Type: text/plain, Size: 8265 bytes --]

From 8c9f54de418166036df0da0c81d3c2b42da39430 Mon Sep 17 00:00:00 2001
From: Tino Calancha <f92capac@gmail.com>
Date: Wed, 24 Feb 2016 01:30:52 +0900
Subject: [PATCH 1/2] Undo in query-replace without exit

* lisp/replace.el (perform-replace): 'undo', undo last replacement
and move back to that place; 'undo-all', undo all replacements
and move back to the place where the first replacement was performed.
(Bug#21684)
---
 lisp/replace.el | 95 ++++++++++++++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 91 insertions(+), 4 deletions(-)

diff --git a/lisp/replace.el b/lisp/replace.el
index 488eff7..ab839ce 100644
--- a/lisp/replace.el
+++ b/lisp/replace.el
@@ -1824,6 +1824,8 @@ query-replace-help
 C-l to clear the screen, redisplay, and offer same replacement again,
 ! to replace all remaining matches in this buffer with no more questions,
 ^ to move point back to previous match,
+u to undo previous replacement,
+U to undo all replacements,
 E to edit the replacement string.
 In multi-buffer replacements type `Y' to replace all remaining
 matches in all remaining buffers with no more questions,
@@ -1853,6 +1855,8 @@ query-replace-map
     (define-key map "\C-l" 'recenter)
     (define-key map "!" 'automatic)
     (define-key map "^" 'backup)
+    (define-key map "u" 'undo)
+    (define-key map "U" 'undo-all)
     (define-key map "\C-h" 'help)
     (define-key map [f1] 'help)
     (define-key map [help] 'help)
@@ -1878,7 +1882,7 @@ query-replace-map
 `act-and-exit', `exit', `exit-prefix', `recenter', `scroll-up',
 `scroll-down', `scroll-other-window', `scroll-other-window-down',
 `edit', `edit-replacement', `delete-and-edit', `automatic',
-`backup', `quit', and `help'.
+`backup', `undo', `undo-all', `quit', and `help'.
 
 This keymap is used by `y-or-n-p' as well as `query-replace'.")
 
@@ -2132,6 +2136,9 @@ perform-replace
          (noedit nil)
          (keep-going t)
          (stack nil)
+         (search-string-replaced nil)    ; last string matching `from-string'
+         (next-replacement-replaced nil) ; replacement string (substituted regexp)
+         (last-was-undo)
          (replace-count 0)
          (skip-read-only-count 0)
          (skip-filtered-count 0)
@@ -2328,6 +2335,23 @@ perform-replace
 		   (match-beginning 0) (match-end 0)
 		   start end search-string
 		   regexp-flag delimited-flag case-fold-search backward)
+                  ;; Obtain the matched groups: needed only when regexp-flag non nil.
+                  (when (and last-was-undo regexp-flag)
+                    (setq last-was-undo nil
+                          real-match-data
+                          (save-excursion
+                            (goto-char (match-beginning 0))
+                            (looking-at search-string)
+                            (match-data t real-match-data))))
+                  ;; Matched string and next-replacement-replaced stored in stack.
+                  (setq search-string-replaced (buffer-substring-no-properties
+                                                (match-beginning 0)
+                                                (match-end 0))
+                        next-replacement-replaced
+                        (query-replace-descr
+                         (save-match-data
+                           (set-match-data real-match-data)
+                           (match-substitute-replacement next-replacement nocasify literal))))
 		  ;; Bind message-log-max so we don't fill up the message log
 		  ;; with a bunch of identical messages.
 		  (let ((message-log-max nil)
@@ -2381,6 +2405,66 @@ perform-replace
 			   (message "No previous match")
 			   (ding 'no-terminate)
 			   (sit-for 1)))
+			((or (eq def 'undo) (eq def 'undo-all))
+			 (if (null stack)
+                             (progn
+                               (message "Nothing to undo")
+                               (ding 'no-terminate)
+                               (sit-for 1))
+			   (let ((stack-idx         0)
+                                 (stack-len         (length stack))
+                                 (num-replacements  0)
+                                 search-string
+                                 next-replacement)
+                             (while (and (< stack-idx stack-len) stack (null replaced))
+                               (let* ((elt (nth stack-idx stack)))
+                                 (setq stack-idx                  (1+ stack-idx)
+                                       replaced                   (nth 1 elt)
+                                       ;; Bind swapped values (search-string <--> replacement)
+                                       search-string              (nth (if replaced 4 3) elt)
+                                       next-replacement           (nth (if replaced 3 4) elt)
+                                       search-string-replaced     search-string
+                                       next-replacement-replaced  next-replacement)
+
+                                 (when (and (= stack-idx stack-len)
+                                            (null replaced)
+                                            (zerop num-replacements))
+                                          (message "Nothing to undo")
+                                          (ding 'no-terminate)
+                                          (sit-for 1))
+
+                                 (when replaced
+                                   (setq stack (nthcdr stack-idx stack))
+                                   (goto-char (nth 0 elt))
+                                   (set-match-data (nth 2 elt))
+                                   (setq real-match-data
+                                         (save-excursion
+                                           (goto-char (match-beginning 0))
+                                           (looking-at search-string)
+                                           (match-data t (nth 2 elt)))
+                                         noedit
+                                         (replace-match-maybe-edit
+                                          next-replacement nocasify literal
+                                          noedit real-match-data backward)
+                                         replace-count (1- replace-count)
+                                         real-match-data
+                                         (save-excursion
+                                           (goto-char (match-beginning 0))
+                                           (looking-at next-replacement)
+                                           (match-data t (nth 2 elt))))
+                                   (when (eq def 'undo-all) ; Set replaced nil to keep in loop
+                                     (setq replaced          nil
+                                           stack-len         (- stack-len stack-idx)
+                                           stack-idx         0
+                                           num-replacements  (1+ num-replacements))))))
+                             (when (and (eq def 'undo-all) (null (zerop num-replacements)))
+                               (message "Undid %d %s" num-replacements
+                                        (if (= num-replacements 1)
+                                            "replacement"
+                                          "replacements"))
+                               (ding 'no-terminate)
+                               (sit-for 1)))
+			   (setq replaced nil last-was-undo t)))
 			((eq def 'act)
 			 (or replaced
 			     (setq noedit
@@ -2503,9 +2587,12 @@ perform-replace
 				 (match-beginning 0)
 				 (match-end 0)
 				 (current-buffer))
-			      (match-data t)))
-		      stack))))))
-
+			      (match-data t))
+				search-string-replaced
+				next-replacement-replaced)
+		      stack)
+                (setq next-replacement-replaced nil
+                      search-string-replaced    nil))))))
       (replace-dehighlight))
     (or unread-command-events
 	(message "Replaced %d occurrence%s%s"
-- 
2.7.0


[-- Attachment #3: Type: text/plain, Size: 2044 bytes --]

From a0bdcedd9aa6eb9e05dde2eae01389ae67f911c3 Mon Sep 17 00:00:00 2001
From: Tino Calancha <f92capac@gmail.com>
Date: Wed, 24 Feb 2016 01:38:50 +0900
Subject: [PATCH 2/2] ; Add NEWS and documentation for last commit

---
 ChangeLog.2                | 7 +++++++
 doc/lispref/searching.texi | 8 ++++++++
 etc/NEWS                   | 5 +++++
 3 files changed, 20 insertions(+)

diff --git a/ChangeLog.2 b/ChangeLog.2
index 6edc654..0d76762 100644
--- a/ChangeLog.2
+++ b/ChangeLog.2
@@ -1,3 +1,10 @@
+2016-02-23  Tino Calancha  <f92capac@gmail.com>
+
+	* lisp/replace.el (perform-replace): `undo', undo last replacement
+	and move back to that place; `undo-all', undo all replacements
+	and move back to the place where the first replacement was performed.
+	(Bug#21684)
+
 2016-02-15  Glenn Morris  <rgm@gnu.org>
 
 	* lisp/dired-aux.el: Require cl-lib.  (Bug#22613)
diff --git a/doc/lispref/searching.texi b/doc/lispref/searching.texi
index 1243d72..644716a 100644
--- a/doc/lispref/searching.texi
+++ b/doc/lispref/searching.texi
@@ -1805,6 +1805,14 @@ Search and Replace
 @item backup
 Move back to the previous place that a question was asked about.
 
+@item undo
+Undo last replacement and move back to the place where that
+replacement was performed.
+
+@item undo-all
+Undo all replacements and move back to the place where the first
+replacement was performed.
+
 @item edit
 Enter a recursive edit to deal with this question---instead of any
 other action that would normally be taken.
diff --git a/etc/NEWS b/etc/NEWS
index 255afde..8c14e6d 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -435,6 +435,11 @@ is intended for adding to 'kill-emacs-query-functions'.
 in favor of the global `M-s h' bindings introduced in Emacs-23.1.
 They'll disappear soon.
 
++++
+** New bindings for 'query-replace-map'.
+`undo', undo the last replacement; bind to `u'.
+`undo-all', undo all replacements; Bind to `U'.
+
 \f
 * Changes in Specialized Modes and Packages in Emacs 25.1
 
-- 
2.7.0


  reply	other threads:[~2016-02-23 16:46 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-10-14 13:26 bug#21684: 25.0.50; undo in query-replace w/o exit Tino Calancha
2015-10-14 16:23 ` Juri Linkov
2015-10-17 13:04   ` Tino Calancha
2015-10-17 21:49     ` Juri Linkov
2015-10-18  9:45       ` Tino Calancha
2015-10-18 15:58         ` Richard Stallman
2015-10-19  1:20           ` Tino Calancha
2015-10-19 22:04             ` Juri Linkov
2015-10-20 12:02               ` Tino Calancha
2016-02-23  8:40                 ` Lars Ingebrigtsen
2016-02-23 16:46                   ` Tino Calancha [this message]
2016-02-23 17:54                     ` Eli Zaretskii
2016-02-24  1:36                     ` Lars Ingebrigtsen
2016-02-24 13:13                       ` Tino Calancha
2016-05-31 22:32                       ` Glenn Morris
2016-06-01  2:44                         ` Tino Calancha
2016-06-01 17:45                           ` Glenn Morris
2016-06-01 17:53                             ` Lars Ingebrigtsen
2016-06-01 17:56                               ` Glenn Morris
2016-06-02 16:44                                 ` Glenn Morris
2016-06-04 22:06                         ` bug#21663: 25.0.50; isearch-edit-string dont resume multi isearches Juri Linkov
2015-10-26  4:09             ` bug#21684: 25.0.50; undo in query-replace w/o exit Richard Stallman
     [not found] ` <handler.21684.B.144482901430164.ack@debbugs.gnu.org>
2015-10-20 13:01   ` bug#21684: Acknowledgement (25.0.50; undo in query-replace w/o exit) Tino Calancha

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

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

  git send-email \
    --in-reply-to=alpine.LRH.2.20.1602240143580.15282@calancha-ilc.kek.jp \
    --to=f92capac@gmail.com \
    --cc=21684@debbugs.gnu.org \
    --cc=larsi@gnus.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 external index

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

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.