Subject: bug#22295: viper-mode undo bug introduced between Nov 10 and Nov 14

From: Stefan Monnier <[email-masked]>
Date: Fri, 10 Jun 2016 13:05:22 -0400

>> The old code seemed "simple" enough that the problem was probably
>> simple to fix (once identified).
> Well, most problems are simple to fix once you actually know what they
> are.

The problem seems to be the following: when you hit ESC, Viper will remove
boundaries from the undo-list, without making any changes to the buffer,
so the top-level loop won't add a boundary before the next command and
ends up hence "amalgamating" the next command with the previous one.

In the old code, we just always blindly added a boundary to
current-buffer before running a command, whereas now we only do so if
the buffer has been modified since the last time we pushed a boundary.

I think the patch below fixes the original problem.
Another way to fix it would be to change undo-auto--add-boundary so it
always considers (current-buffer) regardless of
undo-auto--undoably-changed-buffers.

        Stefan

diff --git a/lisp/emulation/viper-cmd.el b/lisp/emulation/viper-cmd.el
index 93cf3b0..ca52f98 100644
--- a/lisp/emulation/viper-cmd.el
+++ b/lisp/emulation/viper-cmd.el
@@ -1715,7 +1715,8 @@ viper-adjust-undo
       (let ((inhibit-quit t)
 	    tmp tmp2)
 	(setq viper-undo-needs-adjustment nil)
-	(if (listp buffer-undo-list)
+	(when (listp buffer-undo-list)
+	  (let ((had-boundary (null (car buffer-undo-list))))
 	    (if (setq tmp (memq viper-buffer-undo-list-mark buffer-undo-list))
 		(progn
 		  (setq tmp2 (cdr tmp)) ; the part after mark
@@ -1729,8 +1730,11 @@ viper-adjust-undo
 			(delq viper-buffer-undo-list-mark buffer-undo-list))
 		  ;; restore tail of buffer-undo-list
 		  (setq buffer-undo-list (nconc buffer-undo-list tmp2)))
-	      (setq buffer-undo-list (delq nil buffer-undo-list)))))
-    ))
+	      (setq buffer-undo-list (delq nil buffer-undo-list)))
+            ;; The top-level loop only adds boundaries if there has been
+            ;; modifications in the buffer, so make sure we don't accidentally
+            ;; drop the "final" boundary (bug#22295).
+	    (if had-boundary (undo-boundary)))))))

 (defun viper-set-complex-command-for-undo ()