Subject: Re: bug#21684: Acknowledgement (25.0.50; undo in query-replace w/o exit)

From: Tino Calancha <[email-masked]>
Date: Tue, 20 Oct 2015 22:01:20 +0900 (JST)

In previous patch 'undo-all just show the total number of undid 
replacements if the first element was replaced.

New patch (replace-5.patch) fix that: undo-all always show the accumulated
number of undone performed.
diff --git a/lisp/replace.el b/lisp/replace.el
index 3a908ac..16a9942 100644
--- a/lisp/replace.el
+++ b/lisp/replace.el
@@ -1802,6 +1802,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,
@@ -1831,6 +1833,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)
@@ -1856,7 +1860,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'.")
 
@@ -2105,6 +2109,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 +2286,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)
@@ -2332,6 +2356,67 @@ 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 (= stack-idx stack-len)
+                                   (cond ((and (null replaced) (zerop num-replacements))
+                                          (message "Nothing to undo")
+                                          (ding 'no-terminate)
+                                          (sit-for 1))
+                                         ((or replaced (null (zerop num-replacements)))
+                                          (let ((num-replacements (if replaced (1+ num-replacements)
+                                                                    num-replacements)))
+                                            (message "Undid %d %s" num-replacements
+                                                     (if (= num-replacements 1)
+                                                         "replacement"
+                                                       "replacements"))
+                                            (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)))))))
+			   (setq replaced nil last-was-undo t)))
 			((eq def 'act)
 			 (or replaced
 			     (setq noedit
@@ -2454,9 +2539,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"