Subject: bug#66670: [PATCH] Use buffer-local comment-continue in comment-indent-new-line

From: Spencer Baugh <[email-masked]>
Date: Sat, 21 Oct 2023 16:08:42 -0400

Tags: patch

This fixes a remaining issue with whitespace values of comment-continue,
as described in https://github.com/ocaml/tuareg/issues/216

Namely, the fixes done so far don't cover comment-indent-new-line.

In GNU Emacs 29.1.50 (build 12, x86_64-pc-linux-gnu, X toolkit, cairo
 version 1.15.12, Xaw scroll bars) of 2023-10-19 built on
 igm-qws-u22796a
Repository revision: 9163e634e296435aa7a78bc6b77b4ee90666d2ac
Repository branch: emacs-29
Windowing system distributor 'The X.Org Foundation', version 11.0.12011000
System Description: Rocky Linux 8.8 (Green Obsidian)

Configured using:
 'configure --config-cache --with-x-toolkit=lucid
 --with-gif=ifavailable'

>From 9fab95f193eb7da9e6787122b885d1de29b5b657 Mon Sep 17 00:00:00 2001
From: Spencer Baugh <[email-masked]>
Date: Sat, 21 Oct 2023 16:05:05 -0400
Subject: [PATCH] Use buffer-local comment-continue in comment-indent-new-line

As the FIXME said, comment-indent-new-line was wiping out the value of
comment-continue even if it had been set explicitly.  Checking
local-variable-p should be sufficient to fix this.

This makes comment-indent-new-line work properly if comment-continue
can't be recalculated from comment-start, such as if comment-continue
is all whitespace.

* lisp/newcomment.el (comment-indent-new-line): Don't set
comment-continue to nil if it's local-variable-p.
* test/lisp/newcomment-tests.el
(local-comment-continue-in-comment-indent-new-line): Add a test.
---
 lisp/newcomment.el            |  5 +++--
 test/lisp/newcomment-tests.el | 17 +++++++++++++++++
 2 files changed, 20 insertions(+), 2 deletions(-)

diff --git a/lisp/newcomment.el b/lisp/newcomment.el
index 9ae7de70a0e..b217f700f22 100644
--- a/lisp/newcomment.el
+++ b/lisp/newcomment.el
@@ -1541,9 +1541,10 @@ comment-indent-new-line
                                     (cadr (assoc comment-style
                                                  comment-styles))))
                      ;; Recreate comment-continue from comment-start.
-                     ;; FIXME: wrong if comment-continue was set explicitly!
                      ;; FIXME: use prev line's continuation if available.
-                     (comment-continue nil))
+                     (comment-continue (if (local-variable-p 'comment-continue)
+                                           comment-continue
+                                         nil)))
                 (comment-indent continuep))
 	      (save-excursion
 		(let ((pt (point)))
diff --git a/test/lisp/newcomment-tests.el b/test/lisp/newcomment-tests.el
index cb0f304c463..9851b4c2c45 100644
--- a/test/lisp/newcomment-tests.el
+++ b/test/lisp/newcomment-tests.el
@@ -36,4 +36,21 @@ test-uncomment-space-comment-continue
               (uncomment-region (point-min) (point-max))
               (buffer-string))))))
 
+(ert-deftest local-comment-continue-in-comment-indent-new-line ()
+  (with-temp-buffer
+    (setq-local comment-start "/* ")
+    (setq-local comment-end "*/")
+    (insert "foo")
+    (newline)
+    (insert "bar")
+    (forward-line -1)
+    (end-of-line)
+    (comment-region (point-min) (point-max))
+    (should (equal (thing-at-point 'line) "/* foo\n"))
+    (comment-indent-new-line)
+    (should (equal (thing-at-point 'line) " * \n"))
+    (setq-local comment-continue "   ")
+    (comment-indent-new-line)
+    (should (equal (thing-at-point 'line) "   \n"))))
+
 ;;; newcomment-tests.el ends here
-- 
2.39.3