bug#81533: c-ts-mode: Improved block comment indentation

Björn Lindqvist <[email protected]> Sun, 2 Aug 2026 02:37:02 +0200
Newsgroups gmane.emacs.bugs
Message-ID <CALG+76eS0yniMJFbdxc7-BGpBe9tyUMHvj47My4ZzrDBjghd0Q@mail.gmail.com>
Hello Emacs developers!


I was unhappy with the way c-ts-mode indents block comments so I
fixed it. :) Two examples, with <T> marking the point's
position when pressing TAB. Here "second" should align to "first":

int foo; /* first
<T>second */

int foo; /* first

<T>second */

The attached patch modifies the c-ts-mode block comment indentation
rules so that

1) lines beginning with "*" are aligned to the first "*",
2) lines following the first or blank lines are indented three spaces,
3) and other lines are aligned to the previous line.

The rules work for like 99% of all block comments and is how both
Emacs and CPython overwhelmingly indents them. I tried to incorporate
my changes into the existing prev-adaptive-prefix system but couldn't
make it work so I wrote new code instead.


-- 
mvh/best regards Björn Lindqvist
0001-c-ts-mode-Improved-block-comment-indentation.patch (text/x-patch, 2.6 KB)
From 856989429f54fd64d7e9d3ae9ba1d9b7f9ceff09 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Bj=C3=B6rn=20Lindqvist?= <[email protected]>
Date: Sun, 1 Aug 2026 10:18:22 +0200
Subject: [PATCH] c-ts-mode: Improved block comment indentation

1) lines beginning with "*" are aligned to the first "*"
2) lines following the first or blank lines are indented three spaces
3) other lines are aligned to the previous line

* lisp/progmodes/c-ts-mode.el (c-ts-mode--simple-indent-rules): Improved
  block comment indentation.
---
 lisp/progmodes/c-ts-mode.el | 31 ++++++++++++++++++++++---------
 1 file changed, 22 insertions(+), 9 deletions(-)

diff --git a/lisp/progmodes/c-ts-mode.el b/lisp/progmodes/c-ts-mode.el
index 5f064716a89..ecf1e6ca635 100644
--- a/lisp/progmodes/c-ts-mode.el
+++ b/lisp/progmodes/c-ts-mode.el
@@ -473,6 +473,27 @@ c-ts-mode--emacs-macro-rules
     (cons (treesit-node-start parent)
           c-ts-indent-offset))))

+(defun c-ts-mode--block-comment-offset (_n parent bol &rest _)
+  "Indentation offset for lines in block comments.
+
+One space if line starts with \"*\", three if the previous line is blank
+or the first line of the comment, and otherwise same as previous line."
+  (save-excursion
+    (beginning-of-line)
+    (let* ((c-point (treesit-node-start parent))
+	   (c-row (line-number-at-pos c-point))
+	   ;; Make this a builtin!
+	   (c-col (save-excursion (goto-char c-point) (current-column)))
+	   (starred? (looking-at-p (rx (* blank) "*")))
+           (p-row (line-number-at-pos nil t)))
+      (forward-line -1)
+      (let ((prev-indent (current-indentation)))
+	(if starred?
+	    1
+          (if (or (<= (- p-row c-row) 1) (= prev-indent 0))
+              3
+            (- prev-indent c-col)))))))
+
 (defun c-ts-mode--simple-indent-rules (mode style)
   "Return the indent rules for MODE and STYLE.

@@ -513,15 +534,7 @@ c-ts-mode--simple-indent-rules
            ;; ((match nil "function_declarator" "parameters") parent 0)
            ;; ((parent-is "template_declaration") parent 0)

-           ;; `c-ts-common-looking-at-star' has to come before
-           ;; `c-ts-common-comment-2nd-line-matcher'.
-           ;; FIXME: consolidate into a single rule.
-           ((and (parent-is "comment") c-ts-common-looking-at-star)
-            c-ts-common-comment-start-after-first-star -1)
-           (c-ts-common-comment-2nd-line-matcher
-            c-ts-common-comment-2nd-line-anchor
-            1)
-           ((parent-is "comment") prev-adaptive-prefix 0)
+           ((parent-is "comment") parent c-ts-mode--block-comment-offset)

            ;; Preproc directives
            ((node-is "preproc_arg") no-indent)
--
2.55.0