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

    Fix: redo failing part way leaving the redo state as after an undo
    
    Redoing multiple steps raised an error before the steps already redone
    were recorded as a redo.
    
    While nothing reads the difference (the only check also needs the undo
    history to be exhausted, which is always the first step redone),
    keep the state accurate.
    
    Also note that a step which fails part way is left partially redone,
    see the comment in `undo-fu--redo-steps'.
---
 tests/undo-fu_tests.el | 112 +++++++++++++++++++++++++++++++++++++++++++++++--
 undo-fu.el             |  19 +++++----
 2 files changed, 120 insertions(+), 11 deletions(-)

diff --git a/tests/undo-fu_tests.el b/tests/undo-fu_tests.el
index bdc1b8576c..6e7babb255 100644
--- a/tests/undo-fu_tests.el
+++ b/tests/undo-fu_tests.el
@@ -992,7 +992,8 @@ those 2 and stop at the original state."
 
 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, which is shown as a message instead of an error."
+and the second fails, which is shown as a message instead of an error.
+After widening, redo continues with the step which failed."
   (with-undo-fu-test "hello"
     (goto-char (point-max))
     (simulate-input
@@ -1011,10 +1012,23 @@ and the second fails, which is shown as a message 
instead of an error."
     (execute-kbd-macro (vconcat [?\C-u ?2] undo-fu-test-key-redo))
     (widen)
     (should (equal "helloX" (buffer-string)))
+    ;; The step redone is kept as a redo.
+    (should (undo-fu--was-undo-or-redo))
+    (should undo-fu--last-redo)
     (should
      (member
       "Changes to be undone are outside visible portion of buffer"
-      undo-fu-test--captured-messages))))
+      undo-fu-test--captured-messages))
+    ;; Redo the step which failed, then undo both steps.
+    (simulate-input
+      undo-fu-test-key-redo)
+    (should (equal "YhelloX" (buffer-string)))
+    (simulate-input
+      undo-fu-test-key-undo)
+    (should (equal "helloX" (buffer-string)))
+    (simulate-input
+      undo-fu-test-key-undo)
+    (should (equal "hello" (buffer-string)))))
 
 (ert-deftest undo-fu-test-multi-step-redo-partial-failure-within-step ()
   "Redo multiple steps where a later step fails part way, then undo.
@@ -1062,7 +1076,8 @@ Undo reverts the partially redone step instead of 
finishing it."
 
 Edit the end then the start of the buffer, undo both, then narrow the buffer
 to exclude the start.  Redo-all redoes the first step and raises the error
-of the second, without the message which would show all steps were redone."
+of the second, without the message which would show all steps were redone.
+After widening, redo continues with the step which failed."
   (with-undo-fu-test "hello"
     (goto-char (point-max))
     (simulate-input
@@ -1085,7 +1100,96 @@ of the second, without the message which would show all 
steps were redone."
       "Changes to be undone are outside visible portion of buffer")
     (widen)
     (should (equal "helloX" (buffer-string)))
-    (should-not (member "Redo All" undo-fu-test--captured-messages))))
+    ;; The step redone is kept as a redo.
+    (should (undo-fu--was-undo-or-redo))
+    (should undo-fu--last-redo)
+    (should-not (member "Redo All" undo-fu-test--captured-messages))
+    ;; Redo the step which failed, then undo both steps.
+    (simulate-input
+      undo-fu-test-key-redo)
+    (should (equal "YhelloX" (buffer-string)))
+    (simulate-input
+      undo-fu-test-key-undo)
+    (should (equal "helloX" (buffer-string)))
+    (simulate-input
+      undo-fu-test-key-undo)
+    (should (equal "hello" (buffer-string)))))
+
+(ert-deftest undo-fu-test-multi-step-redo-first-step-failure ()
+  "Redo multiple steps where the first step fails, keeping the undo state.
+
+Edit the end then the start of the buffer, undo both, then narrow the buffer
+to exclude the end.  Redo 2 steps at once via C-u 2, the first step fails,
+which is shown as a message instead of an error.
+As nothing was redone, the last action is still an undo,
+so unconstrained redo redoes it (instead of continuing the undo)."
+  (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 (point-min) (1- (point-max)))
+    (setq undo-fu-test--captured-messages nil)
+    (execute-kbd-macro (vconcat [?\C-u ?2] undo-fu-test-key-redo))
+    (widen)
+    (should (equal "hello" (buffer-string)))
+    (should
+     (member
+      "Changes to be undone are outside visible portion of buffer"
+      undo-fu-test--captured-messages))
+    ;; Nothing was redone, the last action is still an undo.
+    (should (undo-fu--was-undo-or-redo))
+    (should-not undo-fu--last-redo)
+    (simulate-input
+      undo-fu-test-key-disable-checkpoint)
+    (simulate-input
+      undo-fu-test-key-redo)
+    (should (equal "helloX" (buffer-string)))))
+
+(ert-deftest undo-fu-test-redo-all-first-step-failure ()
+  "Redo-all where the first step fails, keeping the undo state.
+
+Edit the end then the start of the buffer, undo both, then narrow the buffer
+to exclude the end.  Redo-all raises the error of the first step.
+As nothing was redone, the last action is still an undo,
+so unconstrained redo redoes it (instead of continuing the undo)."
+  (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 (point-min) (1- (point-max)))
+    (should-error-with-message
+        (simulate-input
+          undo-fu-test-key-redo-all)
+      'error
+      "Changes to be undone are outside visible portion of buffer")
+    (widen)
+    (should (equal "hello" (buffer-string)))
+    ;; Nothing was redone, the last action is still an undo.
+    (should (undo-fu--was-undo-or-redo))
+    (should-not undo-fu--last-redo)
+    (simulate-input
+      undo-fu-test-key-disable-checkpoint)
+    (simulate-input
+      undo-fu-test-key-redo)
+    (should (equal "helloX" (buffer-string)))))
 
 (ert-deftest undo-fu-test-redo-zero-steps ()
   "Redo zero steps via C-u 0, redoing nothing.
diff --git a/undo-fu.el b/undo-fu.el
index a96c006b8b..d4bd85c34c 100644
--- a/undo-fu.el
+++ b/undo-fu.el
@@ -143,10 +143,16 @@ This checks if UNDO-LIST represents a state that resulted 
from an undo."
       ;; the value is the position undo had reached in older history (used by 
`undo-only'
       ;; to skip changes which were already undone), not the next group in 
this list.
       ;; Removing entries found this way breaks `undo-only', so only remove 
this key.
-      (remhash ul undo-equiv-table))))
+      (remhash ul undo-equiv-table))
+    ;; Record the redo, so the steps redone are kept as a redo when a later 
step fails
+    ;; (in practice only when narrowing hides a change from `primitive-undo').
+    ;; `this-command' too, as the back-port sets it to `undo'.
+    (setq this-command 'undo-fu-only-redo)
+    (setq undo-fu--last-redo 'constrained)))
 
 (defun undo-fu--redo-steps (steps)
-  "Redo up to STEPS steps, stopping once the last change isn't an undo."
+  "Redo up to STEPS steps, stopping once the last change isn't an undo.
+Steps redone are recorded as a redo even when a later step fails."
   (declare (important-return-value nil))
   ;; Redo one step at a time, passing STEPS to `primitive-undo' would continue 
past
   ;; the undo steps, undoing (and discarding) the earlier history when STEPS 
is too large.
@@ -157,6 +163,8 @@ This checks if UNDO-LIST represents a state that resulted 
from an undo."
       ;; so when a step fails part way, the changes it made would be added to
       ;; that group, causing the next undo to finish the redo instead of 
reverting it.
       ;; When the step succeeds, the boundary is removed with the changes it 
made.
+      ;; A step failing part way stays partially redone (as with `undo'),
+      ;; since rolling it back could fail the same way.
       (undo-boundary)
       (undo-fu--undo-redo)
       (setq count (1+ count)))))
@@ -342,9 +350,7 @@ to perform unconstrained undo/redo actions."
     (undo-fu--with-messages-as-non-repeating-list message-list
       (undo-fu--redo-steps most-positive-fixnum))
     (dolist (message-text message-list)
-      (message "%s All" message-text)))
-  (setq this-command 'undo-fu-only-redo)
-  (setq undo-fu--last-redo 'constrained))
+      (message "%s All" message-text))))
 
 ;; Uses Emacs 28's `undo-redo' (back-ported).
 ;;;###autoload
@@ -408,8 +414,7 @@ Optional argument ARG The number of steps to redo."
                   (progn
                     (cond
                      (undo-fu--respect
-                      (undo-fu--redo-steps steps)
-                      (setq undo-fu--last-redo 'constrained))
+                      (undo-fu--redo-steps steps))
                      (t
                       (undo-fu--undo-unconstrained steps)
                       (setq undo-fu--last-redo 'unconstrained)))

Reply via email to