Subject: bug#59662: 29.0.50; [PATCH] Add treesit--indent-defun

From: Theodor Thornhill <[email-masked]>
Date: Fri, 02 Dec 2022 16:06:46 +0100

Eli Zaretskii <[email-masked]> writes:

>> Date: Thu, 01 Dec 2022 08:22:45 +0100
>> From: Theodor Thornhill <[email-masked]>
>> CC: [email-masked], [email-masked]
>> 
>> >SGTM, but I'd begin by doing this only in c-ts-mode.el (and maybe also in
>> >csharp-ts-mode).  After all, until now this functionality and the "C-c C-q"
>> >binding was only in CC Mode.  (My original question about this was due to
>> >lack of enthusiasm to add the "C Indent" node of the Emacs manual text that
>> >explains this feature is only available in CC Mode, not in the tree-sitter
>> >based C/C++ modes.)
>> 
>> So you'd like the change in lisp.el, but bound only in the modes you mention, or have something similar to the original patch in treesit, but still bound only to the same modes?
>
> Why in lisp.el? we are talking about a feature of C-like languages.  The
> current binding of "C-c C-q" is in cc-cmds.el, so what I had in mind is to
> have its counterpart in c-ts-mode.el.

Something like this?

Theo

>From 26a8780950017a911bac7290366da05e0e35f13f Mon Sep 17 00:00:00 2001
From: Theodor Thornhill <[email-masked]>
Date: Fri, 2 Dec 2022 16:05:35 +0100
Subject: [PATCH] Add c-ts-mode--indent-defun

Add in this function to mimic 'c-indent-defun'

* lisp/progmodes/c-ts-mode.el (c-ts-mode--indent-defun): New function.
(c-ts-mode-map): New mode map that uses said function.
---
 lisp/progmodes/c-ts-mode.el | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)

diff --git a/lisp/progmodes/c-ts-mode.el b/lisp/progmodes/c-ts-mode.el
index fcabb5beac..3171600901 100644
--- a/lisp/progmodes/c-ts-mode.el
+++ b/lisp/progmodes/c-ts-mode.el
@@ -518,9 +518,25 @@ c-ts-mode--end-of-defun
       (if (looking-at "\\s<\\|\n")
 	  (forward-line 1)))))
 
+(defun c-ts-mode--indent-defun ()
+  "Indent the current top-level declaration syntactically.
+
+`treesit-defun-type-regexp' defines what constructs to indent."
+  (interactive)
+  (save-excursion
+    (mark-defun)
+    (indent-region (region-beginning) (region-end))))
+
+(defvar-keymap c-ts-mode-map
+  :doc "Keymap for the C language with tree-sitter"
+  :parent prog-mode-map
+  "C-c C-q" #'c-ts-mode--indent-defun)
+
 ;;;###autoload
 (define-derived-mode c-ts-base-mode prog-mode "C"
-  "Major mode for editing C, powered by tree-sitter."
+  "Major mode for editing C, powered by tree-sitter.
+
+\\{c-ts-mode-map}"
   :syntax-table c-ts-mode--syntax-table
 
   ;; Navigation.
-- 
2.34.1