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

From: Theodor Thornhill <[email-masked]>
Date: Wed, 07 Dec 2022 10:31:13 +0100

Eli Zaretskii <[email-masked]> writes:

>> From: Theodor Thornhill <[email-masked]>
>> Cc: "Theodor Thornhill via Bug reports for GNU Emacs, the Swiss army knife
>>  of text editors" <[email-masked]>, Eli Zaretskii <[email-masked]>,
>>  [email-masked], [email-masked]
>> Date: Sun, 04 Dec 2022 13:33:35 +0100
>> 
>> Theodor Thornhill <[email-masked]> writes:
>> 
>> > On 2 December 2022 22:09:55 CET, "Daniel Martín" <[email-masked]> wrote:
>> >>Theodor Thornhill <[email-masked]> writes:
>> >>
>> >>> Right - thanks.  However, this makes me wonder - should we really be
>> >>> setting mark here? I see that c-indent-defun does not, and it feels
>> >>> weird that indenting adds to the mark ring.  What do you think?  My
>> >>> first patch used
>> >>>
>> >>> (indent-region
>> >>>   (treesit-node-start node)
>> >>>   (treesit-node-end node))
>> >>>
>> >>> Which behaves similarly to c-indent-defun.
>> >>>
>> >>> See attached patch.
>> >>
>> >>I don't have a strong opinion, but if we can indent without setting the
>> >>mark, I think it'd be a cleaner command.  Specially if c-indent-defun
>> >>doesn't set the mark either.
>> >
>> > Yeah, we don't need to set the mark. Thanks for the feedback :)
>> > Theo
>> 
>> Eli, do you have any opinion on this matter? Should we implement this
>> function without using marks?  That will make the function a smidge
>> bigger, but would not mess up the mark-ring.  I think that's desirable,
>> at least.
>
> IMO, it is better not to set the mark, indeed.

Agreed, see attached patch.  What do you think, Yuan and Eli?

Should we expose the internal helper I used, or maybe this function
could just as well live in treesit.c?

Theo

>From 7638a3b0c4c15800f70d0d7e1f444d7512e8994f 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.

(bug#59662)
---
 lisp/progmodes/c-ts-mode.el | 23 ++++++++++++++++++++++-
 1 file changed, 22 insertions(+), 1 deletion(-)

diff --git a/lisp/progmodes/c-ts-mode.el b/lisp/progmodes/c-ts-mode.el
index 0c66b4959e..998e988901 100644
--- a/lisp/progmodes/c-ts-mode.el
+++ b/lisp/progmodes/c-ts-mode.el
@@ -517,9 +517,30 @@ 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 "*")
+  (when-let ((node (treesit-search-forward
+                    (treesit-node-at (point))
+                    treesit-defun-type-regexp t t)))
+    ;; Use internal function to make sure we get the top-level node.
+    (setq node (treesit--defun-maybe-top-level node))
+    (indent-region
+     (treesit-node-start node)
+     (treesit-node-end node))))
+
+(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