From 2a4948bb516435d11245256931eeb9cc0f562aba Mon Sep 17 00:00:00 2001 From: Theodor Thornhill Date: Wed, 25 Jan 2023 21:04:00 +0100 Subject: [PATCH] Initial c-ts-mode-set-style attempt --- lisp/progmodes/c-ts-mode.el | 45 +++++++++++++++++++++++++------------ 1 file changed, 31 insertions(+), 14 deletions(-) diff --git a/lisp/progmodes/c-ts-mode.el b/lisp/progmodes/c-ts-mode.el index 95f9001e0d..35f076b28d 100644 --- a/lisp/progmodes/c-ts-mode.el +++ b/lisp/progmodes/c-ts-mode.el @@ -92,7 +92,7 @@ c-ts-mode-indent-offset :safe 'integerp :group 'c) -(defcustom c-ts-mode-indent-style 'gnu +(defcustom c-ts-mode-indent-style "gnu" "Style used for indentation. The selected style could be one of GNU, K&R, LINUX or BSD. If @@ -100,13 +100,33 @@ c-ts-mode-indent-style set instead. This function is expected return a list that follows the form of `treesit-simple-indent-rules'." :version "29.1" - :type '(choice (symbol :tag "Gnu" 'gnu) - (symbol :tag "K&R" 'k&r) - (symbol :tag "Linux" 'linux) - (symbol :tag "BSD" 'bsd) + :type '(choice (string :tag "Gnu" "gnu") + (string :tag "K&R" "k&r") + (string :tag "Linux" "linux") + (string :tag "BSD" "bsd") (function :tag "A function for user customized style" ignore)) + :set #'c-ts-mode--indent-style :group 'c) +(defun c-ts-mode--indent-style (sym val) + "Custom setter for `c-ts-mode-indent-style'." + (set-default sym val)) + +(defun c-ts-mode-set-style () + (interactive) + (or (eq major-mode 'c-ts-mode) (eq major-mode 'c++-ts-mode) + (error "Buffer %s is not a c-ts-mode (c-ts-mode-set-style)" + (buffer-name))) + (if-let ((mode (cond ((eq major-mode 'c-ts-mode) 'c) + ((eq major-mode 'c++-ts-mode) 'cpp) + (t nil))) + (choice (completing-read "Select style: " '("gnu" "k&r" "linux" "bsd")))) + (c-ts-mode--indent-style 'c-ts-mode-indent-style choice) + (kill-local-variable 'treesit-simple-indent-rules) + (setq-local treesit-simple-indent-rules + (treesit--indent-rules-optimize + (c-ts-mode--set-indent-style mode))))) + ;;; Syntax table (defvar c-ts-mode--syntax-table @@ -224,19 +244,19 @@ c-ts-mode--indent-styles ((parent-is "do_statement") parent-bol c-ts-mode-indent-offset) ,@(when (eq mode 'cpp) `(((node-is "field_initializer_list") parent-bol ,(* c-ts-mode-indent-offset 2))))))) - `((gnu + `(("gnu" ;; Prepend rules to set highest priority ((match "while" "do_statement") parent 0) (c-ts-mode--top-level-label-matcher point-min 1) ,@common) - (k&r ,@common) - (linux + ("k&r" ,@common) + ("linux" ;; Reference: ;; https://www.kernel.org/doc/html/latest/process/coding-style.html, ;; and script/Lindent in Linux kernel repository. ((node-is "labeled_statement") point-min 0) ,@common) - (bsd + ("bsd" ((node-is "}") parent-bol 0) ((node-is "labeled_statement") parent-bol c-ts-mode-indent-offset) ((parent-is "labeled_statement") parent-bol c-ts-mode-indent-offset) @@ -255,11 +275,8 @@ c-ts-mode--set-indent-style (let ((style (if (functionp c-ts-mode-indent-style) (funcall c-ts-mode-indent-style) - (pcase c-ts-mode-indent-style - ('gnu (alist-get 'gnu (c-ts-mode--indent-styles mode))) - ('k&r (alist-get 'k&r (c-ts-mode--indent-styles mode))) - ('bsd (alist-get 'bsd (c-ts-mode--indent-styles mode))) - ('linux (alist-get 'linux (c-ts-mode--indent-styles mode))))))) + (alist-get c-ts-mode-indent-style + (c-ts-mode--indent-styles mode) nil nil #'equal)))) `((,mode ,@style)))) (defun c-ts-mode--top-level-label-matcher (node &rest _) -- 2.34.1