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

From: Stefan Monnier <[email-masked]>
Date: Thu, 24 Apr 2025 14:58:14 -0400

> We could fix this within comment-dwim.

I don't think we could because `comment-indent` is also a command.
Maybe we could make `comment-dwim` do something more useful than calling
`comment-indent` in that case, but that's "another problem".

> However, I propose instead to change comment-indent (or possibly
> comment-search-forward).

+1

> -    (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))

AFAICT we don't need to check (< (nth 8 st) (point)).
Am I missing something?

Also, I think the bug will still bite when
`comment-insert-comment-function` is set.

BTW, the patch looks simple in terms of the diff, but it makes the
control flow (which is already not great) even more complicated.

How 'bout the one below?

        Stefan
diff --git a/lisp/newcomment.el b/lisp/newcomment.el
index 23fcbc05372..d04090c0a75 100644
--- a/lisp/newcomment.el
+++ b/lisp/newcomment.el
@@ -713,18 +713,28 @@
 If CONTINUE is non-nil, use the `comment-continue' markers if any."
   (interactive "*")
   (comment-normalize-vars)
-  (let ((starter (or (and continue comment-continue)
-                     comment-start))
-	(ender (or (and continue comment-continue "")
-                   comment-end)))
-    (unless starter (error "No comment syntax defined"))
     (beginning-of-line)
-    (let* ((eolpos (line-end-position))
+  (let* ((starter (or (and continue comment-continue)
+                      comment-start
+                      (error "No comment syntax defined")))
+	 (ender (or (and continue comment-continue "")
+                    comment-end))
+         (eolpos (line-end-position))
 	   (begpos (comment-search-forward eolpos t))
 	   cpos indent)
-      (if (and comment-insert-comment-function (not begpos))
+    (cond
+     ;; 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 begpos) (not continue)
+           comment-use-syntax comment-use-global-state
+           (nth 4 (syntax-ppss (line-beginning-position))))
+      ;; We don't know anything about the nature of the multiline
+      ;; construct, so immediately delegate to the mode.
+      (indent-according-to-mode))
+     ((and comment-insert-comment-function (not begpos))
 	  ;; If no comment and c-i-c-f is set, let it do everything.
-	  (funcall comment-insert-comment-function)
+      (funcall comment-insert-comment-function))
+     (t
 	;; An existing comment?
 	(if begpos
 	    (progn