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

From: Theodor Thornhill <[email-masked]>
Date: Fri, 02 Dec 2022 19:32:33 +0100

Eli Zaretskii <[email-masked]> writes:

>> From: Theodor Thornhill <[email-masked]>
>> Cc: [email-masked], [email-masked]
>> Date: Fri, 02 Dec 2022 16:06:46 +0100
>> 
>> > 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?
>
> Yes, thanks.
>
> But a command should not have "--" in its name, since every command is by
> definition a public function, not an internal one.

That makes sense.  I was in doubt about that, but decided to go the
(wrong) direction.  Updated in latest patch.

However - mark-defun signals an error on some constructs because of
number-or-marker error:

Debugger entered--Lisp error: (wrong-type-argument integer-or-marker-p nil)
  treesit-end-of-defun()
  c-ts-mode--end-of-defun()
  end-of-defun()
  mark-defun(1 nil)
  mark-defun(1 23209)
  funcall-interactively(mark-defun 1 23209)
  call-interactively(mark-defun record nil)
  command-execute(mark-defun record)
  execute-extended-command(nil "mark-defun" nil)
  funcall-interactively(execute-extended-command nil "mark-defun" nil)
  call-interactively(execute-extended-command nil nil)
  command-execute(execute-extended-command)

I'll file a separate bug report for this one, if that's ok?  Yuan, are
you ok with this patch?

Theo

>From b7c82cf11404a68338aa495cbd4424f45e1f8405 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..36d67c5701 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