branch: elpa/undo-fu
commit 5ae861fc00f4f2d801f91334b2bef08e8ae165bf
Author: Campbell Barton <[email protected]>
Commit: Campbell Barton <[email protected]>

    Fix: undo after redo running in region after undoing in region elsewhere
    
    The back-port of `undo-redo' left `undo-in-region' as the last undo set
    it (in any buffer), so redo showed "Redo in region" and the next undo,
    continuing the chain, ran in region.
    
    Resolve by clearing `undo-in-region' before redoing a step, as `undo'
    does when restarting the chain, since redo never runs in region.
    This keeps the back-port unchanged from `undo-redo'.
---
 changelog.rst          |   2 +
 tests/undo-fu_tests.el | 106 +++++++++++++++++++++++++++++++++++++++++++++++++
 undo-fu.el             |   5 +++
 3 files changed, 113 insertions(+)

diff --git a/changelog.rst b/changelog.rst
index 2010e9ceb8..e833bc85c9 100644
--- a/changelog.rst
+++ b/changelog.rst
@@ -13,6 +13,8 @@ Change Log
     discarding the earlier undo history.
   - Fix unconstrained redo (``C-g`` then redo) after a constrained redo
     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``.
 
 - Version 0.5 (2022-07-10)
 
diff --git a/tests/undo-fu_tests.el b/tests/undo-fu_tests.el
index 7af5a60f19..bdc1b8576c 100644
--- a/tests/undo-fu_tests.el
+++ b/tests/undo-fu_tests.el
@@ -194,6 +194,56 @@ as garbage collection removes the entries it counts."
      (with-undo-fu-test ,initial-buffer-text
        ,@body)))
 
+(defmacro with-undo-fu-test-other-buffer (vars &rest body)
+  "Run BODY with VARS, a list of symbols (BUF BUF-OTHER), bound to buffers.
+BUF is the current buffer, BUF-OTHER is a new buffer,
+killed after BODY runs (switching back to BUF)."
+  (declare (indent 1))
+  (let ((buf (nth 0 vars))
+        (buf-other (nth 1 vars)))
+    `(let ((,buf (current-buffer))
+           (,buf-other (generate-new-buffer "untitled-other")))
+       (unwind-protect
+           (progn
+             ,@body)
+         (switch-to-buffer ,buf)
+         (kill-buffer ,buf-other)))))
+
+(defmacro with-undo-fu-test-undo-in-region (&rest body)
+  "Run BODY with `undo-fu-allow-undo-in-region' enabled.
+Also enable `transient-mark-mode' (off in batch mode), needed for 
`region-active-p'."
+  (declare (indent 0))
+  `(let ((undo-fu-allow-undo-in-region t)
+         (transient-mark-mode t))
+     ,@body))
+
+(defun undo-fu-test--select-all ()
+  "Select the whole buffer, activating the region.
+As `mark-whole-buffer' does, which is only intended for interactive use."
+  (push-mark (point-min) nil t)
+  (goto-char (point-max)))
+
+(defun undo-fu-test--undo-in-other-buffer-impl (buf-other key in-region words)
+  "Write WORDS in BUF-OTHER & undo the last using KEY, in region when 
IN-REGION is non-nil.
+This replaces `pending-undo-list'.  Switch back to the current buffer 
afterwards."
+  (let ((buf (current-buffer)))
+    (switch-to-buffer buf-other)
+    (buffer-reset-text "")
+    (dolist (word words)
+      (simulate-input
+        word))
+    (when in-region
+      (undo-fu-test--select-all))
+    (simulate-input
+      key)
+    (should (equal (apply #'concat (butlast words)) (buffer-string)))
+    (switch-to-buffer buf)))
+
+(defun undo-fu-test--undo-in-region-in-other-buffer (buf-other &rest words)
+  "Write WORDS in BUF-OTHER & undo the last in region, setting 
`undo-in-region'.
+Use with `with-undo-fu-test-undo-in-region'.  Switch back to the current 
buffer afterwards."
+  (undo-fu-test--undo-in-other-buffer-impl buf-other undo-fu-test-key-undo t 
words))
+
 ;; Suppress bell during tests.
 (setq ring-bell-function #'ignore)
 
@@ -1115,6 +1165,62 @@ redo steps are unconstrained, continuing past the 
initial undo step."
       undo-fu-test-key-redo)
     (should (equal "aaa bbb" (buffer-string)))))
 
+(ert-deftest undo-fu-test-redo-other-buffer-undo-in-region-message ()
+  "Redo after undoing in region in another buffer, without showing \"Redo in 
region\".
+
+Undo in one buffer, then undo in region in another buffer and switch back.
+Redo never runs in region, so the global `undo-in-region' left by the other 
buffer
+must not be shown."
+  (with-undo-fu-test-undo-in-region
+    (with-undo-fu-test ""
+      (with-undo-fu-test-other-buffer (buf buf-other)
+        (simulate-input
+          "aaa")
+        (simulate-input
+          " bbb")
+        (simulate-input
+          undo-fu-test-key-undo)
+        (should (equal "aaa" (buffer-string)))
+
+        (undo-fu-test--undo-in-region-in-other-buffer buf-other "1" " 2")
+        (setq undo-fu-test--captured-messages nil)
+        (simulate-input
+          undo-fu-test-key-redo)
+        (should (equal "aaa bbb" (buffer-string)))
+        (should (member "Redo" undo-fu-test--captured-messages))
+        (should-not (member "Redo in region" 
undo-fu-test--captured-messages))))))
+
+(ert-deftest undo-fu-test-undo-after-redo-other-buffer-undo-in-region ()
+  "Undo after redoing, following an undo in region in another buffer, not in 
region.
+
+Undo twice, then undo in region in another buffer and switch back.
+Redo one step, then undo: redo records `undo-in-region' for the next undo,
+so it must clear the value left by the other buffer, otherwise undo runs in 
region."
+  (with-undo-fu-test-undo-in-region
+    (with-undo-fu-test ""
+      (with-undo-fu-test-other-buffer (buf buf-other)
+        (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)))
+
+        (undo-fu-test--undo-in-region-in-other-buffer buf-other "1" " 2")
+        (simulate-input
+          undo-fu-test-key-redo)
+        (should (equal "aaa bbb" (buffer-string)))
+        (setq undo-fu-test--captured-messages nil)
+        (simulate-input
+          undo-fu-test-key-undo)
+        (should (equal "aaa" (buffer-string)))
+        (should (member "Undo" undo-fu-test--captured-messages))
+        (should-not (member "Undo in region" 
undo-fu-test--captured-messages))))))
+
 (ert-deftest undo-fu-test-unconstrained-redo-after-redo ()
   "Unconstrained redo after a constrained redo, continuing the redo.
 
diff --git a/undo-fu.el b/undo-fu.el
index 1d8369bf85..7316113d86 100644
--- a/undo-fu.el
+++ b/undo-fu.el
@@ -122,6 +122,11 @@ This checks if UNDO-LIST represents a state that resulted 
from an undo."
   (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)))
+    ;; Redo never runs in region, clear the value left by the last undo (in 
any buffer)
+    ;; as `undo' does when restarting the chain.
+    ;; NOTE: the back-port only reads this for its "Redo in region" message, 
however
+    ;; the next undo continuing the chain inherits it, running in region.
+    (setq undo-in-region nil)
     (undo-fu--backport-undo-redo 1)
     (when undo-fu-trim
       ;; Remove the entry of the group consumed by redo so undo+redo cancels 
out.

Reply via email to