branch: elpa/undo-fu
commit 8eea40c6cb4f50b6e318d7987b4ad7eef0daf20c
Author: Campbell Barton <[email protected]>
Commit: Campbell Barton <[email protected]>
Fix: undo/redo with a count of zero reversing unconstrained undo/redo
Undoing or redoing nothing still updated the undo/redo state,
so the next unconstrained undo/redo could reverse direction.
Resolve by returning before the undo/redo state is changed.
Also don't show a "Redo" message when redoing nothing,
or report a missing undo step, since there is nothing to redo.
---
changelog.rst | 4 +-
tests/undo-fu_tests.el | 164 ++++++++++++++++++++++++++++++++++++++++++
undo-fu.el | 192 ++++++++++++++++++++++++++-----------------------
3 files changed, 270 insertions(+), 90 deletions(-)
diff --git a/changelog.rst b/changelog.rst
index fc61f71f8f..eb118fc2b2 100644
--- a/changelog.rst
+++ b/changelog.rst
@@ -7,8 +7,8 @@ Change Log
- Add ``undo-fu-clear-all`` to delete all undo history.
- Add ``undo-fu-trim`` option (enabled by default) to remove
``undo-equiv-table`` entries consumed by redo.
- - Fix undo with a count of zero (``C-u 0``) causing the next redo to undo
the last change,
- removing it from the undo history.
+ - 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.
- Version 0.5 (2022-07-10)
diff --git a/tests/undo-fu_tests.el b/tests/undo-fu_tests.el
index 9e7e9a310a..1514626b83 100644
--- a/tests/undo-fu_tests.el
+++ b/tests/undo-fu_tests.el
@@ -93,11 +93,22 @@ Messages are stored in `undo-fu-test--captured-messages'."
"Key binding used for `undo-fu-disable-checkpoint' in tests.")
(defconst undo-fu-test-key-redo-all [f8]
"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.")
+
+(defun undo-fu-test--keyboard-quit ()
+ "Stand in for `keyboard-quit' in keyboard macros.
+
+Running `keyboard-quit' from a keyboard macro signals `quit', aborting the
macro,
+so only set `this-command', seen as `last-command' by the next command in the
macro."
+ (interactive)
+ (setq this-command 'keyboard-quit))
(global-set-key undo-fu-test-key-undo 'undo-fu-only-undo)
(global-set-key undo-fu-test-key-redo 'undo-fu-only-redo)
(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)
(defmacro simulate-input (&rest keys)
"Helper macro to simulate input using KEYS."
@@ -806,12 +817,73 @@ and the undo step can still be redone afterwards."
(simulate-input
undo-fu-test-key-undo)
(should (equal "" (buffer-string)))
+ (setq undo-fu-test--captured-messages nil)
(execute-kbd-macro (vconcat [?\C-u ?0] undo-fu-test-key-redo))
(should (equal "" (buffer-string)))
+ (should-not undo-fu-test--captured-messages)
(simulate-input
undo-fu-test-key-redo)
(should (equal "aaa" (buffer-string)))))
+(ert-deftest undo-fu-test-redo-zero-steps-between-unconstrained-undo ()
+ "Redo zero steps via C-u 0 between unconstrained undo steps.
+
+Write 3 words, undo 1 step, then disable the checkpoint. Redoing nothing
+must not change the undo state, otherwise the next unconstrained undo
+would redo \" ccc\" instead of undoing \" bbb\"."
+ (with-undo-fu-test ""
+ (simulate-input
+ "aaa")
+ (simulate-input
+ " bbb")
+ (simulate-input
+ " ccc")
+ (simulate-input
+ undo-fu-test-key-undo)
+ (should (equal "aaa bbb" (buffer-string)))
+ (simulate-input
+ undo-fu-test-key-disable-checkpoint)
+ (setq undo-fu-test--captured-messages nil)
+ (execute-kbd-macro (vconcat [?\C-u ?0] undo-fu-test-key-redo))
+ (should (equal "aaa bbb" (buffer-string)))
+ (should-not undo-fu-test--captured-messages)
+ (simulate-input
+ undo-fu-test-key-undo)
+ (should (equal "aaa" (buffer-string)))))
+
+(ert-deftest undo-fu-test-redo-zero-steps-after-keyboard-quit ()
+ "Redo zero steps via C-u 0 after `keyboard-quit', stepping over the
checkpoint.
+
+Write 3 words, undo 2, then press `keyboard-quit' and redo nothing.
+The checkpoint is stepped over as with any other count, so the following
+redo steps are unconstrained, continuing past the initial undo step."
+ (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)
+ (should (equal "aaa" (buffer-string)))
+ (setq undo-fu-test--captured-messages nil)
+ ;; A single key-macro so `last-command' is `keyboard-quit' when the redo
runs.
+ (execute-kbd-macro (vconcat undo-fu-test-key-keyboard-quit [?\C-u ?0]
undo-fu-test-key-redo))
+ (should (equal "aaa" (buffer-string)))
+ (should (member "Redo checkpoint stepped over!"
undo-fu-test--captured-messages))
+ (simulate-input
+ 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)))
+ ;; Unconstrained, redo continues past the initial undo step.
+ (simulate-input
+ undo-fu-test-key-redo)
+ (should (equal "aaa bbb" (buffer-string)))))
+
(ert-deftest undo-fu-test-undo-zero-steps ()
"Undo zero steps via C-u 0, undoing nothing.
@@ -825,8 +897,10 @@ and removing it from the undo history)."
" bbb")
(simulate-input
" ccc")
+ (setq undo-fu-test--captured-messages nil)
(execute-kbd-macro (vconcat [?\C-u ?0] undo-fu-test-key-undo))
(should (equal "aaa bbb ccc" (buffer-string)))
+ (should-not undo-fu-test--captured-messages)
(should-error-with-message
(simulate-input
undo-fu-test-key-redo)
@@ -838,6 +912,96 @@ and removing it from the undo history)."
undo-fu-test-key-undo)
(should (equal "aaa bbb" (buffer-string)))))
+(ert-deftest undo-fu-test-undo-zero-steps-between-unconstrained-redo ()
+ "Undo zero steps via C-u 0 between unconstrained redo steps.
+
+Write 3 words, undo them, then redo 2 steps unconstrained. Undoing nothing
+must not change the redo state, otherwise the next unconstrained redo
+would undo the last redo instead of redoing \" ccc\"."
+ (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-disable-checkpoint
+ undo-fu-test-key-redo
+ undo-fu-test-key-redo)
+ (should (equal "aaa bbb" (buffer-string)))
+ (setq undo-fu-test--captured-messages nil)
+ (execute-kbd-macro (vconcat [?\C-u ?0] undo-fu-test-key-undo))
+ (should (equal "aaa bbb" (buffer-string)))
+ (should-not undo-fu-test--captured-messages)
+ (simulate-input
+ undo-fu-test-key-redo)
+ (should (equal "aaa bbb ccc" (buffer-string)))))
+
+(ert-deftest undo-fu-test-undo-zero-steps-after-keyboard-quit ()
+ "Undo zero steps via C-u 0 after `keyboard-quit', stepping over the
checkpoint.
+
+Write 2 words, undo the second, then write a third word and undo it.
+Press `keyboard-quit' and undo nothing: the checkpoint is stepped over
+as with any other count, so the following undo is unconstrained,
+restoring the second word instead of skipping it."
+ (with-undo-fu-test ""
+ (simulate-input
+ "aaa")
+ (simulate-input
+ " bbb")
+ (simulate-input
+ undo-fu-test-key-undo)
+ (should (equal "aaa" (buffer-string)))
+ (simulate-input
+ " ccc")
+ (simulate-input
+ undo-fu-test-key-undo)
+ (should (equal "aaa" (buffer-string)))
+ (setq undo-fu-test--captured-messages nil)
+ ;; A single key-macro so `last-command' is `keyboard-quit' when the undo
runs.
+ (execute-kbd-macro (vconcat undo-fu-test-key-keyboard-quit [?\C-u ?0]
undo-fu-test-key-undo))
+ (should (equal "aaa" (buffer-string)))
+ (should (member "Undo checkpoint ignored!"
undo-fu-test--captured-messages))
+ (simulate-input
+ undo-fu-test-key-undo)
+ (should (equal "aaa bbb" (buffer-string)))))
+
+(ert-deftest undo-fu-test-undo-redo-zero-steps-deactivate-region ()
+ "Undo & redo zero steps via C-u 0 with an active region, deactivating it.
+
+Undo & redo deactivate the region before running (unless
`undo-fu-allow-undo-in-region'
+is set), this must also apply when nothing is undone or redone."
+ ;; Off in batch mode, needed for `region-active-p'.
+ (let ((transient-mark-mode t))
+ (with-undo-fu-test ""
+ (simulate-input
+ "aaa")
+ (simulate-input
+ " bbb")
+ (simulate-input
+ undo-fu-test-key-undo)
+ (should (equal "aaa" (buffer-string)))
+ (set-mark (point-min))
+ (should (region-active-p))
+ (execute-kbd-macro (vconcat [?\C-u ?0] undo-fu-test-key-undo))
+ (should (equal "aaa" (buffer-string)))
+ (should-not (region-active-p))
+ (set-mark (point-min))
+ (should (region-active-p))
+ (execute-kbd-macro (vconcat [?\C-u ?0] undo-fu-test-key-redo))
+ (should (equal "aaa" (buffer-string)))
+ (should-not (region-active-p))
+ ;; The undo step can still be redone.
+ (simulate-input
+ undo-fu-test-key-redo)
+ (should (equal "aaa bbb" (buffer-string))))))
+
;; Local Variables:
;; fill-column: 99
;; indent-tabs-mode: nil
diff --git a/undo-fu.el b/undo-fu.el
index 2330a339e9..05db4480d4 100644
--- a/undo-fu.el
+++ b/undo-fu.el
@@ -355,48 +355,59 @@ Optional argument ARG The number of steps to redo."
(undo-fu--checkpoint-update was-undo-or-redo "Redo checkpoint stepped
over!")
- (when undo-fu--respect
- (when (null was-undo-or-redo)
- (user-error "Redo without undo step (%s to ignore)"
- (substitute-command-keys
- (format "\\[%s]" (symbol-name
(undo-fu--quit-command)))))))
-
- (let* ((last-command
- (cond
- (was-undo
- ;; Break undo chain, avoid having to press [keyboard-quit].
- 'ignore)
- (was-redo
- ;; Checked by the undo function.
- 'undo)
- ((eq last-command 'keyboard-quit)
- ;; This case needs to be explicitly detected.
- ;; If we undo until there is no undo information left,
- ;; then press `keyboard-quit' and redo, it fails without this
case.
- 'ignore)
- (t
- ;; No change.
- last-command)))
- (success
- (condition-case err
- (progn
- (cond
- (undo-fu--respect
- (undo-fu--backport-undo-redo-with-trim steps))
- (t
- (undo-fu--with-message-suffix " (unconstrained)"
- (let ((undo-no-redo nil))
- (undo steps)))))
- 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)))
+ (cond
+ ((< steps 1)
+ ;; Redo nothing (a count below 1), returning before the undo/redo state
is changed
+ ;; (otherwise unconstrained undo would reverse after redoing 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-fu--backport-undo-redo-with-trim' & `undo' aren't called, as
they would
+ ;; show a "Redo" message although nothing is redone, and may replace
+ ;; `pending-undo-list' & `buffer-undo-list'.
+ t)
+ (t
+ (when undo-fu--respect
+ (when (null was-undo-or-redo)
+ (user-error "Redo without undo step (%s to ignore)"
+ (substitute-command-keys
+ (format "\\[%s]" (symbol-name
(undo-fu--quit-command)))))))
+
+ (let* ((last-command
+ (cond
+ (was-undo
+ ;; Break undo chain, avoid having to press [keyboard-quit].
+ 'ignore)
+ (was-redo
+ ;; Checked by the undo function.
+ 'undo)
+ ((eq last-command 'keyboard-quit)
+ ;; This case needs to be explicitly detected.
+ ;; If we undo until there is no undo information left,
+ ;; then press `keyboard-quit' and redo, it fails without this
case.
+ 'ignore)
+ (t
+ ;; No change.
+ last-command)))
+ (success
+ (condition-case err
+ (progn
+ (cond
+ (undo-fu--respect
+ (undo-fu--backport-undo-redo-with-trim steps))
+ (t
+ (undo-fu--with-message-suffix " (unconstrained)"
+ (let ((undo-no-redo nil))
+ (undo steps)))))
+ 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)))))
;;;###autoload
(defun undo-fu-only-undo (&optional arg)
@@ -417,52 +428,57 @@ Optional argument ARG the number of steps to undo."
(undo-fu--checkpoint-update was-undo-or-redo "Undo checkpoint ignored!")
- ;; Swap in 'undo' for our own function name.
- ;; Without this undo won't stop once the first undo step is reached.
- (let* ((last-command
- (cond
- ;; Special case, to avoid being locked out of the undo-redo chain.
- ;; Without this, continuously redoing will end up in a state
where undo & redo fails.
- ;;
- ;; Detect this case and break the chain. Only do this when
previously redoing
- ;; otherwise undo will reverse immediately once it reaches the
beginning,
- ;; which we don't want even when unconstrained,
- ;; as we don't want to present the undo chain as infinite in
either direction.
- ((and was-redo (null undo-fu--respect) (eq t pending-undo-list))
- 'ignore)
- (was-undo-or-redo
- ;; Checked by the undo function.
- 'undo)
- (t
- ;; No change.
- last-command)))
- (success
- (condition-case err
- (progn
- (cond
- ((< steps 1)
- ;; Undo nothing.
- ;; NOTE: Don't call `undo-only', although it undoes
nothing in this case,
- ;; it would add an `undo-equiv-table' entry for the last
change
- ;; (as if it were an undo), causing the next redo to undo
the last change,
- ;; removing it from history.
- nil)
- ((and undo-fu--respect (null undo-fu--in-region))
- (undo-only steps))
- (t
- (undo-fu--with-message-suffix " (unconstrained)"
- (let ((undo-no-redo nil))
- (undo steps)))))
- t)
- (error
- (message "%s" (error-message-string err))
- nil))))
-
- (when success
- (setq undo-fu--was-redo nil))
-
- (setq this-command 'undo-fu-only-undo)
- success)))
+ (cond
+ ((< steps 1)
+ ;; Undo nothing (a count below 1), returning before the undo/redo state
is changed
+ ;; (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.
+ t)
+ (t
+ ;; Swap in 'undo' for our own function name.
+ ;; Without this undo won't stop once the first undo step is reached.
+ (let* ((last-command
+ (cond
+ ;; Special case, to avoid being locked out of the undo-redo
chain.
+ ;; Without this, continuously redoing will end up in a state
+ ;; where undo & redo fails.
+ ;;
+ ;; Detect this case and break the chain. Only do this when
previously redoing
+ ;; otherwise undo will reverse immediately once it reaches the
beginning,
+ ;; which we don't want even when unconstrained,
+ ;; as we don't want to present the undo chain as infinite in
either direction.
+ ((and was-redo (null undo-fu--respect) (eq t pending-undo-list))
+ 'ignore)
+ (was-undo-or-redo
+ ;; Checked by the undo function.
+ 'undo)
+ (t
+ ;; No change.
+ last-command)))
+ (success
+ (condition-case err
+ (progn
+ (cond
+ ((and undo-fu--respect (null undo-fu--in-region))
+ (undo-only steps))
+ (t
+ (undo-fu--with-message-suffix " (unconstrained)"
+ (let ((undo-no-redo nil))
+ (undo steps)))))
+ t)
+ (error
+ (message "%s" (error-message-string err))
+ nil))))
+
+ (when success
+ (setq undo-fu--was-redo nil))
+
+ (setq this-command 'undo-fu-only-undo)
+ success)))))
;; ---------------------------------------------------------------------------
;; Public Functions (Extra Utilities)