bug#81533: c-ts-mode: Improved block comment indentation
Björn A. Lindqvist <[email protected]> Sun, 2 Aug 2026 19:22:13 +0200
| Newsgroups | gmane.emacs.bugs |
|---|---|
| Message-ID | <[email protected]> |
Hello Stefan!
On 8/2/26 16:26, Stefan Monnier wrote:
>>> 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.
>
> "when pressing TAB" is one case, "when calling
> `indent-according-to-mode`" is another. The first is free to mess up
> because the users are right there to see the problem and fix it, but the
> second needs to be more conservative because it's also used in cases
> where the users may not see the result.
I think the "batch indentation" use case is beyond the scope of my
PR. It doesn't work in c-ts-mode and I doubt it can work. The
tree-sitter C grammar is incomplete and real world C code is just too
bastardized with macros everywhere.
> The way `indent-line-function` is designed to handle this is that the
> function should return `noindent` when the language's syntax is of no
> help, such as when "indenting" the inside of a string or of a comment.
But then c-ts-mode already violates the "don't indent without language
syntax"-policy because it indents block comments. My PR only changes
(improves) the rules.
> The rule (1) sounds right, but rules (2) and (3) should simply refrain
> from modifying the line and return `noindent`.
Why indent lines prefixed with "*" be indented but not other lines?
While most people use "*" to draw a border, some use it for bullet
points.
Fwiw, c-ts-mode indents unprefixed lines, but not always
correctly. E.g., flying block comments are not handled well:
struct bc_frame *saved_fp; /* previous frame pointer,
NULL if bottommost frame */
becomes:
struct bc_frame *saved_fp; /* previous frame pointer,
NULL if bottommost frame */
My PR fixes that. :)
>>> The rules work for like 99% of all block comments and is how both
>>> Emacs and CPython overwhelmingly indents them.
>
> [ It took me a while to understand what you meant here.
> I was confused by the idea of CPython indenting anything. ]
My point is that you can't satisfy everyone: some people write essays,
use esoteric formatting styles, or draw ascii art in comments. But for
almost all comments the three rules are good enough.
>> Thanks. Yuan and Stefan, any comments?
>
> These issues are basically language-independent so I'm not
> happy with the current code: these issues should be handled
> once for all tree-sitter modes.
You could make the function public so that other modes could add it to
their default rules:
((parent-is "comment") parent c-ts-mode-common-block-comment-offset)
--
mvh/best regards Björn Lindqvist