unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Tino Calancha <f92capac@gmail.com>
To: 21684@debbugs.gnu.org
Subject: bug#21684: 25.0.50; undo in query-replace w/o exit
Date: Sun, 18 Oct 2015 18:45:46 +0900 (JST)	[thread overview]
Message-ID: <alpine.LRH.2.20.1510181843390.3984@calancha-ilc.kek.jp> (raw)
In-Reply-To: <87r3kt5fnp.fsf@mail.linkov.net>

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


>Maybe better would be for undo "U" to only undo and skip non-replacement
>entries in the stack?  (Implementation-wise this means looping until
>finding a previous non-replacement entry in the stack).
I agree, its better undo 'just' undo. Fixed in the new patch
(replace-3.patch).

>A minor comment is that to save cons cells you could just put both new
>elements shallow as elements 3 and 4 in the stack instead of adding
>a list of them.
Sure, thank you for remind me this. Applied.

>Another minor suggestions is for better names you could rename
>the prefix -solved-regexp to -replaced
Done. Thank you.

>Also we need this feature to be documented in etc/NEWS.
>Have you already signed FSF copyright assignment papers?
I didn't. I would like to sign. Maybe someone could guide me how to
complete such process.

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

diff --git a/lisp/replace.el b/lisp/replace.el
index 3a908ac..43a5a78 100644
--- a/lisp/replace.el
+++ b/lisp/replace.el
@@ -1802,6 +1802,7 @@ 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,
 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,
@@ -1831,6 +1832,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)
     (define-key map "\C-h" 'help)
     (define-key map [f1] 'help)
     (define-key map [help] 'help)
@@ -1856,7 +1859,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', `quit', and `help'.
 
 This keymap is used by `y-or-n-p' as well as `query-replace'.")
 
@@ -2105,6 +2108,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)
@@ -2279,6 +2285,22 @@ 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 (subtituted matched groups) 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)
@@ -2332,6 +2354,50 @@ perform-replace
 			   (message "No previous match")
 			   (ding 'no-terminate)
 			   (sit-for 1)))
+			((eq def 'undo)
+			 (if (null stack)
+                             (progn
+                               (message "Nothing to undo")
+                               (ding 'no-terminate)
+                               (sit-for 1))
+			   (let ((stack-idx 0)
+                                 (stack-len (length stack)))
+                             (while (and (< stack-idx stack-len) stack (null replaced))
+                               (let* (search-string next-replacement (elt (nth stack-idx stack)))
+                                 (setq stack-idx                      (1+ stack-idx)
+                                       replaced                       (nth 1 elt)
+                                       ;; Bind locally swapped values (search-string <---> replacement).
+                                       search-string                  (if replaced (nth 4 elt) (nth 3 elt))
+                                       next-replacement               (if replaced (nth 3 elt) (nth 4 elt))
+                                       search-string-replaced    search-string
+                                       next-replacement-replaced next-replacement)
+
+                                 (when (and (null replaced) (= stack-idx stack-len))
+                                   (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))))))))
+			   (setq replaced nil
+                                 last-was-undo t)))
 			((eq def 'act)
 			 (or replaced
 			     (setq noedit
@@ -2454,9 +2520,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"

  reply	other threads:[~2015-10-18  9:45 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 [this message]
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
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

  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=alpine.LRH.2.20.1510181843390.3984@calancha-ilc.kek.jp \
    --to=f92capac@gmail.com \
    --cc=21684@debbugs.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).