branch: elpa/undo-fu
commit 388cf4c19b51f9317d7a2504beb6e1a6505e2952
Author: Campbell Barton <[email protected]>
Commit: Campbell Barton <[email protected]>

    Fix: unconstrained redo after a constrained redo undoing instead of redoing
    
    The back-port of `undo-redo' leaves `pending-undo-list' continuing the
    undo, so `undo' (used for unconstrained redo) undid the change just
    redone. This broke pressing [keyboard-quit] to keep redoing part way
    through the undo steps.
    
    Resolve by tracking whether the last redo was constrained, breaking the
    undo chain in this case so `undo' redoes the next undo step.
    
    Regression introduced in 46de023b5f8d.
---
 changelog.rst          |  2 ++
 tests/undo-fu_tests.el | 93 ++++++++++++++++++++++++++++++++++++++++++++++++++
 undo-fu.el             | 23 ++++++++-----
 3 files changed, 110 insertions(+), 8 deletions(-)

diff --git a/changelog.rst b/changelog.rst
index b72795a3a4..2010e9ceb8 100644
--- a/changelog.rst
+++ b/changelog.rst
@@ -11,6 +11,8 @@ Change Log
     (removing it from the undo history), or reversing unconstrained undo/redo.
   - Fix redo with a count larger than the undo steps (``C-u N``) undoing past 
the initial undo step,
     discarding the earlier undo history.
+  - Fix unconstrained redo (``C-g`` then redo) after a constrained redo
+    undoing the change just redone instead of redoing the next.
 
 - Version 0.5 (2022-07-10)
 
diff --git a/tests/undo-fu_tests.el b/tests/undo-fu_tests.el
index b35a46e71e..f3d30a84b0 100644
--- a/tests/undo-fu_tests.el
+++ b/tests/undo-fu_tests.el
@@ -1023,6 +1023,99 @@ redo steps are unconstrained, continuing past the 
initial undo step."
       undo-fu-test-key-redo)
     (should (equal "aaa bbb" (buffer-string)))))
 
+(ert-deftest undo-fu-test-unconstrained-redo-after-redo ()
+  "Unconstrained redo after a constrained redo, continuing the redo.
+
+Write 3 words, undo them, then redo 1 step.  Disable the checkpoint and redo:
+the second word is redone instead of undoing the first.
+Redo then continues the chain, after which undo reverses it."
+  (with-undo-fu-test ""
+    (simulate-input
+      "aaa")
+    (simulate-input
+      " bbb")
+    (simulate-input
+      " ccc")
+    (simulate-input
+      undo-fu-test-key-undo
+      undo-fu-test-key-undo
+      undo-fu-test-key-undo)
+    (should (equal "" (buffer-string)))
+    (simulate-input
+      undo-fu-test-key-redo)
+    (should (equal "aaa" (buffer-string)))
+    (simulate-input
+      undo-fu-test-key-disable-checkpoint
+      undo-fu-test-key-redo)
+    (should (equal "aaa bbb" (buffer-string)))
+    (simulate-input
+      undo-fu-test-key-redo)
+    (should (equal "aaa bbb ccc" (buffer-string)))
+    (simulate-input
+      undo-fu-test-key-undo)
+    (should (equal "aaa bbb" (buffer-string)))))
+
+(ert-deftest undo-fu-test-unconstrained-redo-after-redo-and-cursor-motion ()
+  "Unconstrained redo after a constrained redo, with the cursor moved in 
between.
+
+Write 3 words, undo them, then redo 1 step.  Disable the checkpoint,
+move the cursor and redo: the second word is redone instead of undoing the 
first.
+
+Verifies the last redo being constrained is tracked, not taken from the 
checkpoint,
+which is already disabled by the time redo runs."
+  (with-undo-fu-test ""
+    (simulate-input
+      "aaa")
+    (simulate-input
+      " bbb")
+    (simulate-input
+      " ccc")
+    (simulate-input
+      undo-fu-test-key-undo
+      undo-fu-test-key-undo
+      undo-fu-test-key-undo)
+    (should (equal "" (buffer-string)))
+    (simulate-input
+      undo-fu-test-key-redo)
+    (should (equal "aaa" (buffer-string)))
+    (simulate-input
+      undo-fu-test-key-disable-checkpoint)
+    ;; Move the cursor, a command which doesn't change the buffer.
+    (simulate-input
+      [?\C-a])
+    (simulate-input
+      undo-fu-test-key-redo)
+    (should (equal "aaa bbb" (buffer-string)))))
+
+(ert-deftest undo-fu-test-unconstrained-redo-after-redo-partial-failure ()
+  "Unconstrained redo after a redo of multiple steps which fails part way.
+
+Edit the end then the start of the buffer, undo both, then narrow the buffer
+to exclude the start.  Redo 2 steps at once via C-u 2, the first step succeeds
+and the second fails.  Widen, disable the checkpoint and redo:
+the step which failed is redone, instead of undoing the step which succeeded."
+  (with-undo-fu-test "hello"
+    (goto-char (point-max))
+    (simulate-input
+      "X")
+    (goto-char (point-min))
+    (simulate-input
+      "Y")
+    (should (equal "YhelloX" (buffer-string)))
+    (simulate-input
+      undo-fu-test-key-undo)
+    (simulate-input
+      undo-fu-test-key-undo)
+    (should (equal "hello" (buffer-string)))
+    (narrow-to-region 2 (point-max))
+    (execute-kbd-macro (vconcat [?\C-u ?2] undo-fu-test-key-redo))
+    (widen)
+    (should (equal "helloX" (buffer-string)))
+    (simulate-input
+      undo-fu-test-key-disable-checkpoint
+      undo-fu-test-key-redo)
+    (should (equal "YhelloX" (buffer-string)))))
+
 (ert-deftest undo-fu-test-undo-zero-steps ()
   "Undo zero steps via C-u 0, undoing nothing.
 
diff --git a/undo-fu.el b/undo-fu.el
index 9f97c19052..c7eb375762 100644
--- a/undo-fu.el
+++ b/undo-fu.el
@@ -69,7 +69,9 @@ causing the number of steps to grow even though the buffer 
content is unchanged.
 ;; Initiated an undo-in region (don't use `undo-only').
 ;; Only use when `undo-fu-allow-undo-in-region' is true.
 (defvar-local undo-fu--in-region nil)
-;; Track the last undo/redo direction.
+;; Track the last undo/redo direction, nil after undo. After redo, how the 
steps were redone:
+;; - `constrained': by the back-port, leaving `pending-undo-list' continuing 
the undo.
+;; - `unconstrained': by `undo', leaving `pending-undo-list' continuing the 
redo.
 ;; Use in conjunction with `undo-fu--was-undo-or-redo' to ensure the value 
isn't stale.
 (defvar-local undo-fu--was-redo nil)
 ;; List of interactive commands.
@@ -326,7 +328,7 @@ to perform unconstrained undo/redo actions."
     (dolist (message-text message-list)
       (message "%s All" message-text)))
   (setq this-command 'undo-fu-only-redo)
-  (setq undo-fu--was-redo t))
+  (setq undo-fu--was-redo 'constrained))
 
 ;; Uses Emacs 28's `undo-redo' (back-ported).
 ;;;###autoload
@@ -369,7 +371,13 @@ Optional argument ARG The number of steps to redo."
                (was-undo
                 ;; Break undo chain, avoid having to press [keyboard-quit].
                 'ignore)
-               (was-redo
+               ((eq was-redo 'constrained)
+                ;; Break the chain so `undo' redoes the next undo step.
+                ;; NOTE: the back-port leaves `pending-undo-list' continuing 
the undo
+                ;; (as `undo-redo' does), which unconstrained undo after a 
constrained redo
+                ;; relies on.
+                'ignore)
+               ((eq was-redo 'unconstrained)
                 ;; Checked by the undo function.
                 'undo)
                ((eq last-command 'keyboard-quit)
@@ -385,19 +393,18 @@ Optional argument ARG The number of steps to redo."
                   (progn
                     (cond
                      (undo-fu--respect
-                      (undo-fu--redo-steps steps))
+                      (undo-fu--redo-steps steps)
+                      (setq undo-fu--was-redo 'constrained))
                      (t
                       (undo-fu--with-message-suffix " (unconstrained)"
                         (let ((undo-no-redo nil))
-                          (undo steps)))))
+                          (undo steps)))
+                      (setq undo-fu--was-redo 'unconstrained)))
                     t)
                 (error
                  (message "%s" (error-message-string err))
                  nil))))
 
-        (when success
-          (setq undo-fu--was-redo t))
-
         (setq this-command 'undo-fu-only-redo)
         success)))))
 

Reply via email to