branch: elpa/undo-fu
commit 001dac913204aacd5b0155f660813d48a26e93b5
Author: Campbell Barton <[email protected]>
Commit: Campbell Barton <[email protected]>
Fix: undo restoring undone changes after multi-step redo with undo-fu-trim
Trimming `undo-equiv-table' after redoing multiple steps (C-u N)
followed the entry values to find the groups redone, however 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.
Removing those entries made a later undo restore the undone changes.
Resolve by removing the entry of each group consumed by the redo,
see the NOTE in `undo-fu--trim-undo-equiv-table'.
---
tests/undo-fu_tests.el | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++
undo-fu.el | 24 +++++++++---------
2 files changed, 78 insertions(+), 12 deletions(-)
diff --git a/tests/undo-fu_tests.el b/tests/undo-fu_tests.el
index 1514626b83..a8415e8327 100644
--- a/tests/undo-fu_tests.el
+++ b/tests/undo-fu_tests.el
@@ -667,6 +667,72 @@ 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-keeps-undone-history ()
+ "Redo multiple steps, keeping undo from restoring previously undone changes.
+
+Write 2 words, undo the second, write 2 more words, undo 2, then redo
+both at once via C-u 2. Undoing all the way should never restore the
+word undone earlier. Verifies `undo-fu-trim' only removes the entries
+consumed by redo, keeping those `undo-only' uses to skip undone changes."
+ (with-undo-fu-test ""
+ (simulate-input
+ "aaa")
+ (simulate-input
+ " bbb")
+ ;; Undo " bbb", then continue editing so it's part of older history.
+ (simulate-input
+ undo-fu-test-key-undo)
+ (simulate-input
+ " xxx")
+ (simulate-input
+ " yyy")
+ (should (equal "aaa xxx yyy" (buffer-string)))
+ (simulate-input
+ undo-fu-test-key-undo)
+ (simulate-input
+ undo-fu-test-key-undo)
+ (should (equal "aaa" (buffer-string)))
+ ;; Redo 2 steps at once via C-u 2.
+ (execute-kbd-macro (vconcat [?\C-u ?2] undo-fu-test-key-redo))
+ (should (equal "aaa xxx yyy" (buffer-string)))
+ ;; Undo everything, " bbb" must not be restored.
+ (simulate-input
+ undo-fu-test-key-undo)
+ (should (equal "aaa xxx" (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-history-entry-count ()
+ "Verify redoing multiple steps at once removes an entry for each step.
+
+Write 3 words, undo 3 times, then redo all 3 at once via C-u 3.
+With `undo-fu-trim' enabled, the history entry count returns to 3
+\(as when redoing one step at a time, see `undo-fu-test-history-entry-count')."
+ (with-undo-fu-test-without-gc ""
+ (simulate-input
+ "aaa")
+ (simulate-input
+ " bbb")
+ (simulate-input
+ " ccc")
+ (should (equal 3 (undo-fu-test-count-history-entries)))
+ (simulate-input
+ undo-fu-test-key-undo)
+ (simulate-input
+ undo-fu-test-key-undo)
+ (simulate-input
+ undo-fu-test-key-undo)
+ (should (equal "" (buffer-string)))
+ (should (equal 6 (undo-fu-test-count-history-entries)))
+ ;; Redo 3 steps at once via C-u 3, each step removes an entry (trimmed).
+ (execute-kbd-macro (vconcat [?\C-u ?3] undo-fu-test-key-redo))
+ (should (equal "aaa bbb ccc" (buffer-string)))
+ (should (equal 3 (undo-fu-test-count-history-entries)))))
+
(ert-deftest undo-fu-test-trim-disabled-history-entry-count-grows ()
"Check redo increases the history entry count with `undo-fu-trim' disabled.
diff --git a/undo-fu.el b/undo-fu.el
index 05db4480d4..fcf2080cab 100644
--- a/undo-fu.el
+++ b/undo-fu.el
@@ -137,9 +137,15 @@ This checks if UNDO-LIST represents a state that resulted
from an undo."
;; `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, count the groups
- ;; actually processed: the nil-separated groups between UL and NEW-UL,
- ;; capped at STEPS.
+ ;; 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))
@@ -149,15 +155,9 @@ This checks if UNDO-LIST represents a state that resulted
from an undo."
(setq in-group nil))
((null in-group)
(setq in-group t)
- (setq consumed (1+ consumed))))
- (setq scan (cdr scan)))
- ;; Walk the equiv-table chain, removing exactly `consumed' entries.
- (let ((k ul))
- (dotimes (_ consumed)
- (let ((v (gethash k undo-equiv-table)))
- (remhash k undo-equiv-table)
- (when (consp v)
- (setq k (undo-fu--undo-list-skip-boundaries v))))))))
+ (setq consumed (1+ consumed))
+ (remhash scan undo-equiv-table)))
+ (setq scan (cdr scan)))))
(defun undo-fu--checkpoint-disable ()
"Disable using the checkpoint.