branch: elpa/evil
commit eae05b5a1b2de6fe2d262a9fc0fd24e46bebc2c8
Author: Tom Dalziel <33435574+tomd...@users.noreply.github.com>
Commit: GitHub <nore...@github.com>

    Always undo in normal state (#1888)
---
 evil-commands.el |  5 ++++-
 evil-tests.el    | 14 ++++++++++++++
 2 files changed, 18 insertions(+), 1 deletion(-)

diff --git a/evil-commands.el b/evil-commands.el
index a44cc25dc1..82fadf25cc 100644
--- a/evil-commands.el
+++ b/evil-commands.el
@@ -1778,7 +1778,10 @@ Add (add-hook 'evil-local-mode-hook 
'turn-on-undo-tree-mode) to your init file f
   :jump t
   (interactive "*p")
   (evil--check-undo-system)
-  (funcall evil-undo-function count))
+  (let ((state-before evil-state))
+    (unless (eq 'normal state-before) (evil-normal-state))
+    (funcall evil-undo-function count)
+    (unless (eq 'normal state-before) (evil-change-state state-before))))
 
 (evil-define-command evil-redo (count)
   "Undo COUNT changes in buffer using `evil-redo-function'."
diff --git a/evil-tests.el b/evil-tests.el
index 88a8aa5a07..42ad06c5ac 100644
--- a/evil-tests.el
+++ b/evil-tests.el
@@ -9816,6 +9816,20 @@ when an error stops the execution of the macro"
         ("``")
         "alpha bravo charlie [d]elta"))))
 
+(ert-deftest evil-test-insert-state-undo ()
+  "Test that undo isn't lost when done from insert state."
+  :tags '(evil)
+  (skip-unless (version<= "28" emacs-version))
+  (let (evil-want-fine-undo)
+    (customize-set-variable 'evil-undo-system 'undo-redo)
+    (evil-test-buffer
+      "alpha [ ]delta"
+      (evil-define-key* 'insert 'local [f8] 'evil-undo)
+      ("icharlie" [f8] [escape])
+      "alpha[ ] delta"
+      ("\C-r")
+      "alpha [c]harlie delta")))
+
 (ert-deftest evil-test-visual-update-x-selection ()
   "Test `evil-visual-update-x-selection'."
   :tags '(evil)

Reply via email to