]> git.eshelyaron.com Git - emacs.git/commitdiff
Add c-ts-mode-indent-defun (bug#59662)
authorTheodor Thornhill <theo@thornhill.no>
Fri, 2 Dec 2022 15:05:35 +0000 (16:05 +0100)
committerYuan Fu <casouri@gmail.com>
Wed, 7 Dec 2022 20:01:51 +0000 (12:01 -0800)
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

index 862d8766e0b4205cb2a26d8cd78419d858f375bc..0b17541a0a277c47112a3b236633dce6fa95e7ae 100644 (file)
@@ -520,9 +520,30 @@ the subtrees."
       (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 "*")
+  (let ((orig-point (point-marker)))
+    ;; If `treesit-beginning-of-defun' returns nil, we are not in a
+    ;; defun, so don't indent anything.
+    (when (treesit-beginning-of-defun)
+      (let ((start (point)))
+        (treesit-end-of-defun)
+        (indent-region start (point))))
+    (goto-char orig-point)))
+
+(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.