branch: elpa/undo-fu
commit 005ce713932d46f305cfe36e8ae7aaa96aa63e66
Author: Campbell Barton <[email protected]>
Commit: Campbell Barton <[email protected]>
Fix: redo undoing the last change after the built-in undo of zero steps
The built-in `undo` undoing nothing maps the last change to 'empty in
`undo-equiv-table`, which was treated as an undo step, so redo undid the
change, removing it from the undo history.
Resolve by ignoring 'empty entries when checking for a step to redo,
while the undo chain still continues over them. Undo after redoing onto
a change mapped to 'empty now continues from that change, instead of
failing with "No further undo information".
Both are handled around the back-port of `undo-redo`, keeping it
unchanged, see the NOTE in `undo-fu--undo-redo`.
---
changelog.rst | 2 ++
tests/undo-fu_tests.el | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++
undo-fu.el | 24 ++++++++++++---
3 files changed, 105 insertions(+), 5 deletions(-)
diff --git a/changelog.rst b/changelog.rst
index e833bc85c9..eda503bf58 100644
--- a/changelog.rst
+++ b/changelog.rst
@@ -15,6 +15,8 @@ Change Log
undoing the change just redone instead of redoing the next.
- Fix undo after redo running in region (and redo showing "Redo in region")
after undoing in region in another buffer, with
``undo-fu-allow-undo-in-region``.
+ - Fix redo undoing the last change after the built-in ``undo`` with a count
of zero (``C-u 0``),
+ removing it from the undo history (Emacs 28 and newer).
- Version 0.5 (2022-07-10)
diff --git a/tests/undo-fu_tests.el b/tests/undo-fu_tests.el
index 6e7babb255..1b372ca51c 100644
--- a/tests/undo-fu_tests.el
+++ b/tests/undo-fu_tests.el
@@ -95,6 +95,8 @@ Messages are stored in `undo-fu-test--captured-messages'."
"Key binding used for `undo-fu-only-redo-all' in tests.")
(defconst undo-fu-test-key-keyboard-quit [f10]
"Key binding used for `undo-fu-test--keyboard-quit' in tests.")
+(defconst undo-fu-test-key-builtin-undo [?\C-/]
+ "Key binding used for the built-in `undo' in tests.")
(defun undo-fu-test--keyboard-quit ()
"Stand in for `keyboard-quit' in keyboard macros.
@@ -109,6 +111,7 @@ so only set `this-command', seen as `last-command' by the
next command in the ma
(global-set-key undo-fu-test-key-disable-checkpoint
'undo-fu-disable-checkpoint)
(global-set-key undo-fu-test-key-redo-all 'undo-fu-only-redo-all)
(global-set-key undo-fu-test-key-keyboard-quit 'undo-fu-test--keyboard-quit)
+(global-set-key undo-fu-test-key-builtin-undo 'undo)
(defmacro simulate-input (&rest keys)
"Helper macro to simulate input using KEYS."
@@ -1536,6 +1539,87 @@ is set), this must also apply when nothing is undone or
redone."
undo-fu-test-key-redo)
(should (equal "aaa bbb" (buffer-string))))))
+(ert-deftest undo-fu-test-redo-after-builtin-undo-zero-steps ()
+ "Redo after the built-in `undo' undoes zero steps via C-u 0.
+
+Write 3 words, then undo 0 steps with `undo', which maps \" ccc\" to \\='empty
+in `undo-equiv-table' (so the next undo continues the undo chain).
+Redo must not treat \" ccc\" as an undo step (undoing it and removing it from
+the undo history)."
+ ;; Redo after undo, stopping at the initial undo step.
+ (with-undo-fu-test ""
+ (simulate-input
+ "aaa")
+ (simulate-input
+ " bbb")
+ (simulate-input
+ " ccc")
+ (execute-kbd-macro (vconcat [?\C-u ?0] undo-fu-test-key-builtin-undo))
+ (should (equal "aaa bbb ccc" (buffer-string)))
+ (simulate-input
+ undo-fu-test-key-undo)
+ (should (equal "aaa bbb" (buffer-string)))
+ (execute-kbd-macro (vconcat [?\C-u ?5] undo-fu-test-key-redo))
+ (should (equal "aaa bbb ccc" (buffer-string)))
+ ;; Undo continues from " ccc".
+ (simulate-input
+ undo-fu-test-key-undo)
+ (should (equal "aaa bbb" (buffer-string)))
+ (simulate-input
+ undo-fu-test-key-redo-all)
+ (should (equal "aaa bbb ccc" (buffer-string)))
+ (simulate-input
+ undo-fu-test-key-undo)
+ (should (equal "aaa bbb" (buffer-string))))
+
+ ;; Redo without undo, there is nothing to redo.
+ (with-undo-fu-test ""
+ (simulate-input
+ "aaa")
+ (simulate-input
+ " bbb")
+ (simulate-input
+ " ccc")
+ (execute-kbd-macro (vconcat [?\C-u ?0] undo-fu-test-key-builtin-undo))
+ (should-error-with-message
+ (simulate-input
+ undo-fu-test-key-redo)
+ 'user-error
+ "Redo without undo step (C-g to ignore)")
+ (should (equal "aaa bbb ccc" (buffer-string)))
+ (simulate-input
+ undo-fu-test-key-redo-all)
+ (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-undo-consecutive-boundaries ()
+ "Undo with consecutive boundaries at the head of the undo list.
+
+Undoing over the extra boundary undoes nothing, so `undo' maps the last change
+to \\='empty in `undo-equiv-table' (so the next undo continues the undo chain).
+Undo must continue the chain, otherwise each undo restarts it,
+only undoing the boundaries without reaching the earlier changes."
+ (with-undo-fu-test ""
+ (simulate-input
+ "one")
+ (simulate-input
+ " two")
+ ;; Add two boundaries, `undo-boundary' doesn't add a boundary when one is
at the head.
+ (push nil buffer-undo-list)
+ (push nil buffer-undo-list)
+ (should (equal "one two" (buffer-string)))
+ (simulate-input
+ undo-fu-test-key-undo)
+ (should (equal "one two" (buffer-string)))
+ (simulate-input
+ undo-fu-test-key-undo)
+ (should (equal "one" (buffer-string)))
+ (simulate-input
+ undo-fu-test-key-undo)
+ (should (equal "" (buffer-string)))))
+
;; Local Variables:
;; fill-column: 99
;; indent-tabs-mode: nil
diff --git a/undo-fu.el b/undo-fu.el
index d4bd85c34c..c5dd645779 100644
--- a/undo-fu.el
+++ b/undo-fu.el
@@ -135,6 +135,11 @@ This checks if UNDO-LIST represents a state that resulted
from an undo."
;; the next undo continuing the chain inherits it, running in region.
(setq undo-in-region nil)
(undo-fu--backport-undo-redo 1)
+ ;; NOTE: `undo' can't continue from 'empty (see
`undo-fu--last-change-can-redo-p'),
+ ;; it only maps a change to 'empty when `pending-undo-list' is the change,
so use that.
+ ;; `undo-redo' has this bug, handled here to keep the back-port unchanged.
+ (when (eq pending-undo-list 'empty)
+ (setq pending-undo-list (undo-fu--undo-list-skip-boundaries
buffer-undo-list)))
(when undo-fu-trim
;; 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.
@@ -157,7 +162,7 @@ Steps redone are recorded as a redo even when a later step
fails."
;; 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))
+ (while (and (< count steps) (undo-fu--last-change-can-redo-p))
;; 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
@@ -315,6 +320,16 @@ Optional argument BODY runs with the message suffix."
(t
nil)))
+(defun undo-fu--last-change-can-redo-p ()
+ "Return t when the last change is an undo step which can be redone."
+ (declare (important-return-value t))
+ ;; NOTE: `undo' maps a change to 'empty when it undoes nothing (a count of
zero or
+ ;; consecutive boundaries), continuing the undo chain over it, so only redo
checks this:
+ ;; the change isn't an undo, redoing it would undo it, removing it from the
history.
+ ;; Emacs 27 and older map it to t, so it can't be detected there.
+ (let ((equiv (undo-fu--backport-undo--last-change-was-undo-p
buffer-undo-list)))
+ (and equiv (not (eq equiv 'empty)))))
+
;; ---------------------------------------------------------------------------
;; Public Functions
@@ -382,7 +397,7 @@ Optional argument ARG The number of steps to redo."
t)
(t
(when undo-fu--respect
- (when (null was-undo-or-redo)
+ (when (null (undo-fu--last-change-can-redo-p))
(user-error "Redo without undo step (%s to ignore)"
(substitute-command-keys
(format "\\[%s]" (symbol-name
(undo-fu--quit-command)))))))
@@ -451,9 +466,8 @@ Optional argument ARG the number of steps to undo."
;; (otherwise unconstrained redo would reverse after undoing nothing).
;; NOTE: this follows `undo-fu--checkpoint-update' so [keyboard-quit]
steps over
;; the checkpoint & the region is deactivated, as with any other count.
- ;; `undo-only' & `undo' aren't called, as they would show an "Undo"
message
- ;; and add an `undo-equiv-table' entry for the last change (as if it
were an undo),
- ;; causing a redo to undo the last change, removing it from history.
+ ;; `undo' isn't called, as it would show an "Undo" message and map the
last change
+ ;; to 'empty in `undo-equiv-table' (see:
`undo-fu--last-change-can-redo-p').
t)
(t
;; Swap in 'undo' for our own function name.