branch: elpa/undo-fu
commit 66cb98a4ae794d1a64714a8046b320763f3960ca
Author: Campbell Barton <[email protected]>
Commit: Campbell Barton <[email protected]>
Fix: redo with a count above the undo steps undoing past the initial step
Passing the count to `primitive-undo' continued past the undo steps,
undoing (and discarding) the earlier history.
Resolve by redoing one step at a time in `undo-fu--redo-steps',
also used by redo-all. As each step redoes a single group, trimming
only removes that group's entry.
---
changelog.rst | 2 ++
tests/undo-fu_tests.el | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++
undo-fu.el | 62 +++++++++++++++++++-----------------------
3 files changed, 102 insertions(+), 35 deletions(-)
diff --git a/changelog.rst b/changelog.rst
index eb118fc2b2..b72795a3a4 100644
--- a/changelog.rst
+++ b/changelog.rst
@@ -9,6 +9,8 @@ Change Log
- Add ``undo-fu-trim`` option (enabled by default) to remove
``undo-equiv-table`` entries consumed by redo.
- Fix undo/redo with a count of zero (``C-u 0``) causing the next redo to
undo the last change
(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.
- Version 0.5 (2022-07-10)
diff --git a/tests/undo-fu_tests.el b/tests/undo-fu_tests.el
index a8415e8327..b35a46e71e 100644
--- a/tests/undo-fu_tests.el
+++ b/tests/undo-fu_tests.el
@@ -667,6 +667,38 @@ for `undo-fu-only-redo'."
(execute-kbd-macro (vconcat [?\C-u ?3] undo-fu-test-key-redo))
(should (equal "aaa bbb ccc" (buffer-string)))))
+(ert-deftest undo-fu-test-multi-step-redo-stops-at-initial-undo ()
+ "Redo with a count larger than the undo steps, without undoing.
+
+Write 3 words, undo 2, then redo 3 at once via C-u 3. Only the 2 undo
+steps should be redone, the remaining step must not undo \" ccc\"
+\(which would also remove it from the undo history)."
+ (with-undo-fu-test ""
+ (simulate-input
+ "aaa")
+ (simulate-input
+ " bbb")
+ (simulate-input
+ " ccc")
+ (simulate-input
+ undo-fu-test-key-undo)
+ (simulate-input
+ undo-fu-test-key-undo)
+ (should (equal "aaa" (buffer-string)))
+ ;; Redo 3 steps at once via C-u 3, only 2 are available.
+ (execute-kbd-macro (vconcat [?\C-u ?3] undo-fu-test-key-redo))
+ (should (equal "aaa bbb ccc" (buffer-string)))
+ ;; The history is intact, undo all the way back.
+ (simulate-input
+ undo-fu-test-key-undo)
+ (should (equal "aaa bbb" (buffer-string)))
+ (simulate-input
+ undo-fu-test-key-undo)
+ (should (equal "aaa" (buffer-string)))
+ (simulate-input
+ undo-fu-test-key-undo)
+ (should (equal "" (buffer-string)))))
+
(ert-deftest undo-fu-test-multi-step-redo-keeps-undone-history ()
"Redo multiple steps, keeping undo from restoring previously undone changes.
@@ -842,6 +874,47 @@ and the second fails, which is shown as a message instead
of an error."
"Changes to be undone are outside visible portion of buffer"
undo-fu-test--captured-messages))))
+(ert-deftest undo-fu-test-multi-step-redo-partial-failure-within-step ()
+ "Redo multiple steps where a later step fails part way, then undo.
+
+Edit the buffer, then edit the end and the start in one command, undo both,
+then narrow the buffer to exclude the start. Redo 2 steps at once via C-u 2,
+the second step fails after redoing the edit at the end.
+Undo reverts the partially redone step instead of finishing it."
+ (with-undo-fu-test "hello"
+ (goto-char (point-max))
+ (simulate-input
+ "X")
+ (let ((map (make-sparse-keymap)))
+ (define-key
+ map [f9]
+ (lambda ()
+ (interactive)
+ (goto-char (point-max))
+ (insert "E")
+ (goto-char (point-min))
+ (insert "S")))
+ (use-local-map map))
+ (simulate-input
+ [f9])
+ (should (equal "ShelloXE" (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 "helloXE" (buffer-string)))
+ ;; Revert the partially redone step.
+ (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-redo-all-partial-failure ()
"Redo-all where a later step fails, keeping the steps redone.
diff --git a/undo-fu.el b/undo-fu.el
index fcf2080cab..9f97c19052 100644
--- a/undo-fu.el
+++ b/undo-fu.el
@@ -116,48 +116,41 @@ This checks if UNDO-LIST represents a state that resulted
from an undo."
(setq pending-undo-list new-pul)
(setq buffer-undo-list new-ul)))))
-(defun undo-fu--backport-undo-redo-with-trim (&optional arg)
- "Call `undo-fu--backport-undo-redo', trimming `undo-equiv-table' when
enabled."
+(defun undo-fu--backport-undo-redo-with-trim ()
+ "Redo a single step, trimming `undo-equiv-table' when enabled."
(declare (important-return-value nil))
;; The equiv-table key (as used by `undo-fu--backport-undo-redo').
(let ((ul (undo-fu--undo-list-skip-boundaries buffer-undo-list)))
- (undo-fu--backport-undo-redo arg)
+ (undo-fu--backport-undo-redo 1)
(when undo-fu-trim
- (undo-fu--trim-undo-equiv-table ul buffer-undo-list (or arg 1)))))
+ ;; Remove the entry of the group consumed by redo so undo+redo cancels
out.
+ ;; As only one group is redone, this is the only key it could contain.
+ ;;
+ ;; NOTE: while it may seem the entry value could be followed to remove
more entries,
+ ;; 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))))
;; ---------------------------------------------------------------------------
;; Internal Functions/Macros
-(defun undo-fu--trim-undo-equiv-table (ul new-ul steps)
- "Remove `undo-equiv-table' entries consumed by redo so undo+redo cancels
out."
+(defun undo-fu--redo-steps (steps)
+ "Redo up to STEPS steps, stopping once the last change isn't an undo."
(declare (important-return-value nil))
- ;; UL is the nils-skipped head of the old `buffer-undo-list' (the start point
- ;; passed to `primitive-undo'). NEW-UL is the remaining tail returned by
- ;; `primitive-undo'. STEPS is the requested number of redo steps.
- ;;
- ;; While the requested step count is known, `primitive-undo' may process
fewer
- ;; groups than requested, so we can't rely on it. Instead, remove the entry
of
- ;; each group actually processed: the nil-separated groups between UL and
NEW-UL,
- ;; capped at STEPS. Each entry is keyed by the first record of its group,
- ;; as `undo' stores the undo list (without leading boundaries) after each
undo.
- ;;
- ;; NOTE: while it may seem the entry value could be followed to find the
next group,
- ;; 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'.
- (let ((consumed 0)
- (scan ul)
- (in-group nil))
- (while (and (consp scan) (not (eq scan new-ul)) (< consumed steps))
- (cond
- ((null (car scan))
- (setq in-group nil))
- ((null in-group)
- (setq in-group t)
- (setq consumed (1+ consumed))
- (remhash scan undo-equiv-table)))
- (setq scan (cdr scan)))))
+ ;; 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.
+ (let ((count 0))
+ (while (and (< count steps) (undo-fu--was-undo-or-redo))
+ ;; Separate the changes made by this step from the group being redone.
+ ;; NOTE: after a step, `buffer-undo-list' starts with the next group's
changes,
+ ;; 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.
+ (undo-boundary)
+ (undo-fu--backport-undo-redo-with-trim)
+ (setq count (1+ count)))))
(defun undo-fu--checkpoint-disable ()
"Disable using the checkpoint.
@@ -329,8 +322,7 @@ to perform unconstrained undo/redo actions."
(undo-fu--undo-enabled-or-error)
(let ((message-list (list)))
(undo-fu--with-messages-as-non-repeating-list message-list
- (while (undo-fu--was-undo-or-redo)
- (undo-fu--backport-undo-redo-with-trim 1)))
+ (undo-fu--redo-steps most-positive-fixnum))
(dolist (message-text message-list)
(message "%s All" message-text)))
(setq this-command 'undo-fu-only-redo)
@@ -393,7 +385,7 @@ Optional argument ARG The number of steps to redo."
(progn
(cond
(undo-fu--respect
- (undo-fu--backport-undo-redo-with-trim steps))
+ (undo-fu--redo-steps steps))
(t
(undo-fu--with-message-suffix " (unconstrained)"
(let ((undo-no-redo nil))