Subject: 26.1; patch for cancel-change-group to work after undo

From: Braun Gábor <[email-masked]>
Date: Tue, 12 Nov 2019 18:05:48 +0100

Hi,

Please consider the following fix to the problem, patching function 
cancel-change-group.

In the original implementation, the only form depending on whether the 
last command was an undo is the line

(unless (eq last-command 'undo) (undo-start))

which sets pending-undo-list to buffer-undo-list, when the last command 
was not an undo.  When it was, then pending-undo-list probably contains 
the pending undo entries from a buffer state before the start of the 
change group, which are obviously irrelevant for cancelling the change 
group.

The fix below is to use buffer-undo-list instead of pending-undo-list,
essentially inlining the call to undo-more for this change.  
As a side effect, the value of pending-undo-list will no longer be 
changed by cancel-change-group (unless something in the undo log changes 
it), but as far as I see, it doesn't matter, as its value is useful only 
directly after an undo command.

--- lisp/subr.el
+++ lisp/subr.el
@@ -2669,14 +2669,17 @@
               (progn
                 ;; Temporarily truncate the undo log at ELT.
                 (when (consp elt)
-                  (setcar elt nil) (setcdr elt nil))
-                (unless (eq last-command 'undo) (undo-start))
-                ;; Make sure there's no confusion.
-                (when (and (consp elt) (not (eq elt (last pending-undo-
list))))
-                  (error "Undoing to some unrelated state"))
+                  (setcar elt nil) (setcdr elt nil)
+                  ;; Make sure there's no confusion.
+                  (unless (eq elt (last buffer-undo-list))
+                    (error "Undoing to some unrelated state")))
                 ;; Undo it all.
                 (save-excursion
-                  (while (listp pending-undo-list) (undo-more 1)))
+                  (let ((undo-in-progress t))
+                    (while buffer-undo-list
+                      ;; Undo one step, removing it from undo log.
+                      (setq buffer-undo-list
+                            (primitive-undo 1 buffer-undo-list)))))
                 ;; Revert the undo info to what it was when we grabbed
                 ;; the state.
                 (setq buffer-undo-list elt))

Best wishes,

	Gábor