Subject: Re: bug#78814: 31.0.50; problems setting custom c++-ts-mode indent style

From: Eli Zaretskii <[email-masked]>
Date: Sat, 21 Jun 2025 17:44:10 +0300

[Please use Reply All to reply, to keep the bug tracker CC'ed.]

> From: Jacek Świerk <[email-masked]>
> Date: Sat, 21 Jun 2025 16:29:19 +0200
> 
> Here's more detailed reproduction recipe:
> 
> 1. Define a custom indentation style for C++:
> (defun my--c++-ts-mode-indent-style ()
>   "Use BSD style but don't indent inside namespaces."
>   `((cpp
>      ((n-p-gp nil nil "namespace_definition") grand-parent 0)
>      ,@(cdr (car (c-ts-mode--simple-indent-rules 'cpp 'bsd))))))
> 
> 2. Set the custom style as default:
> (setopt c-ts-mode-indent-style 'my--c++-ts-mode-indent-style)
> 
> 3. Enable C++ treesit mode:
> (c++-ts-mode)
> 
> 4. Verify `treesit-simple-indent-rules` - it shows only `((cpp))` instead of the expected rules.
> 
> This happens because c-ts-mode--simple-indent-rules, which is called while
> changing major mode (line 1563) to c++-ts-mode, has pcase only for 4 styles
> (gnu, k&r, linux and bsd), meaning it doesn't handle the situation when
> c-ts-mode-indent-style is set to some custom function. I can work around it by
> calling (setopt c-ts-mode-indent-style 'my--c++-ts-mode-indent-style) after
> switching to c++-ts-mode with a hook but is this the expected flow?

Thanks.  Does the patch below fix the problem?

diff --git a/lisp/progmodes/c-ts-mode.el b/lisp/progmodes/c-ts-mode.el
index 0df9fe3..5155193 100644
--- a/lisp/progmodes/c-ts-mode.el
+++ b/lisp/progmodes/c-ts-mode.el
@@ -1487,8 +1487,10 @@ c-ts-mode
       (setq-local comment-end " */")
       ;; Indent.
       (setq-local treesit-simple-indent-rules
-                  (c-ts-mode--simple-indent-rules
-                   'c c-ts-mode-indent-style))
+                  (if (functionp c-ts-mode-indent-style)
+                      (funcall c-ts-mode-indent-style)
+                    (c-ts-mode--simple-indent-rules
+                     'c c-ts-mode-indent-style)))
       ;; (setq-local treesit-simple-indent-rules
       ;;             `((c . ,(alist-get 'gnu (c-ts-mode--indent-styles 'c)))))
       ;; Font-lock.
@@ -1560,8 +1562,10 @@ c++-ts-mode
 
       ;; Indent.
       (setq-local treesit-simple-indent-rules
-                  (c-ts-mode--simple-indent-rules
-                   'cpp c-ts-mode-indent-style))
+                  (if (functionp c-ts-mode-indent-style)
+                      (funcall c-ts-mode-indent-style)
+                    (c-ts-mode--simple-indent-rules
+                     'cpp c-ts-mode-indent-style)))
 
       ;; Font-lock.
       (setq-local treesit-font-lock-settings