Subject: bug#44581: 28.0.50; undo comment-region error

From: Alan Mackenzie <[email-masked]>
Date: Sun, 15 Nov 2020 12:20:18 +0000

Hello, Mattias.

On Wed, Nov 11, 2020 at 19:43:28 +0100, Mattias Engdegård wrote:
> This bug appears to be a consequence of eb0d10d567a. Alan, can you take a look?
> The error does not appear if comment-combine-change-calls is set to nil.

Yes.  The region supplied to comment-region-default was (BEG END).  But
changes were made outside of this region (namely, deletion of
whitespace), so the invocation of macro combine-change-calls was
invalid.

Changing the END argument to combine-change-calls to
beginning-of-next-line solves this problem.

Maybe I need the same change in uncomment-region-default too, but I'm
not sure.

Here is a provisional patch, based on the master branch (although the
fix should probably go into emacs-27).  Could you test it, please.

diff --git a/lisp/newcomment.el b/lisp/newcomment.el
index e111ae8e22..3eb158dc2c 100644
--- a/lisp/newcomment.el
+++ b/lisp/newcomment.el
@@ -1292,7 +1292,15 @@ comment-region-default-1
 
 (defun comment-region-default (beg end &optional arg)
   (if comment-combine-change-calls
-      (combine-change-calls beg end (comment-region-default-1 beg end arg))
+      (combine-change-calls beg
+          ;; A new line might get inserted and whitespace deleted
+          ;; after END for line comments.  Ensure the next argument is
+          ;; after any and all changes.
+          (save-excursion
+            (goto-char end)
+            (forward-line)
+            (point))
+        (comment-region-default-1 beg end arg))
     (comment-region-default-1 beg end arg)))
 
 ;;;###autoload

-- 
Alan Mackenzie (Nuremberg, Germany).