Subject: bug#78003: Acknowledgement (31.0.50; comment-indent misbehaves in multiline comments)

From: Sean Whitton <[email-masked]>
Date: Thu, 24 Apr 2025 15:40:00 +0800

Hello,

Proposed fix for the comment-indent part of this attached.

-- 
Sean Whitton
>From 7b4eec6dfe757260a6976a59a2728ba26a46d551 Mon Sep 17 00:00:00 2001
From: Sean Whitton <[email-masked]>
Date: Thu, 24 Apr 2025 15:38:34 +0800
Subject: [PATCH] comment-indent: Handle BOL already within a multiline comment

* lisp/newcomment.el (comment-indent): Newly handle the case
that BOL is already within a multiline comment (bug#78003).
---
 lisp/newcomment.el | 51 ++++++++++++++++++++++++++++++----------------
 1 file changed, 33 insertions(+), 18 deletions(-)

diff --git a/lisp/newcomment.el b/lisp/newcomment.el
index 23fcbc05372..fd498fe934c 100644
--- a/lisp/newcomment.el
+++ b/lisp/newcomment.el
@@ -719,8 +719,17 @@ comment-indent
                    comment-end)))
     (unless starter (error "No comment syntax defined"))
     (beginning-of-line)
-    (let* ((eolpos (line-end-position))
-	   (begpos (comment-search-forward eolpos t))
+    (let* ((startpos (comment-search-forward (line-end-position) t))
+           (begpos (or startpos
+                       ;; If we couldn't find a comment *starting* on
+                       ;; this line, see if we are already within a
+                       ;; multiline comment at BOL (bug#78003).
+                       (and (not continue)
+                            comment-use-syntax comment-use-global-state
+                            (let ((st (syntax-ppss (line-beginning-position))))
+                              (and (nth 4 st) (< (nth 8 st) (point))
+                                   (point))))))
+           (multilinep (and (not startpos) begpos))
 	   cpos indent)
       (if (and comment-insert-comment-function (not begpos))
 	  ;; If no comment and c-i-c-f is set, let it do everything.
@@ -750,22 +759,28 @@ comment-indent
 	    (setq cpos (point-marker))
 	    (insert ender)))
 	(goto-char begpos)
-	;; Compute desired indent.
-	(setq indent (save-excursion (funcall comment-indent-function)))
-	;; If `indent' is nil and there's code before the comment, we can't
-	;; use `indent-according-to-mode', so we default to comment-column.
-	(unless (or indent (save-excursion (skip-chars-backward " \t") (bolp)))
-	  (setq indent comment-column))
-	(if (not indent)
-	    ;; comment-indent-function refuses: delegate to line-indent.
-	    (indent-according-to-mode)
-	  ;; If the comment is at the right of code, adjust the indentation.
-	  (unless (save-excursion (skip-chars-backward " \t") (bolp))
-	    (setq indent (comment-choose-indent indent)))
-	  ;; If that's different from comment's current position, change it.
-	  (unless (= (current-column) indent)
-	    (delete-region (point) (progn (skip-chars-backward " \t") (point)))
-	    (indent-to indent)))
+        ;; Compute desired indent.
+        (if multilinep
+            ;; We don't know anything about the nature of the multiline
+            ;; construct, so immediately delegate to the mode.
+            (indent-according-to-mode)
+	  (setq indent (save-excursion (funcall comment-indent-function)))
+	  ;; If `indent' is nil and there's code before the comment, we can't
+	  ;; use `indent-according-to-mode', so we default to comment-column.
+	  (unless (or indent
+                      (save-excursion (skip-chars-backward " \t") (bolp)))
+	    (setq indent comment-column))
+	  (if (not indent)
+	      ;; comment-indent-function refuses: delegate to line-indent.
+	      (indent-according-to-mode)
+	    ;; If the comment is at the right of code, adjust the indentation.
+	    (unless (save-excursion (skip-chars-backward " \t") (bolp))
+	      (setq indent (comment-choose-indent indent)))
+	    ;; If that's different from comment's current position, change it.
+	    (unless (= (current-column) indent)
+	      (delete-region (point)
+                             (progn (skip-chars-backward " \t") (point)))
+	      (indent-to indent))))
 	(goto-char cpos)
 	(set-marker cpos nil)))))
 
-- 
2.49.0