aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTheodor Thornhill2023-01-25 21:04:00 +0100
committerTheodor Thornhill2023-01-28 19:39:23 +0100
commit6e50ee8bbb50ef86707cefed8ebb20f027156843 (patch)
treede2eb765018d58b300626dc0e5c281d1cd86820d
parent450db0587a91ff13403488fbabc744567be7f6fa (diff)
downloademacs-6e50ee8bbb50ef86707cefed8ebb20f027156843.tar.gz
emacs-6e50ee8bbb50ef86707cefed8ebb20f027156843.zip
Add c-ts-mode-set-style and :set for c-ts-mode-indent-style
* lisp/progmodes/c-ts-mode.el (c-ts-mode--indent-style-setter): New setter for the indent style defcustom. (c-ts-mode-indent-style): Don't quote the values and refer to the setter. (c-ts-mode-set-style): New command to interactively set the indent style. (c-ts-mode--get-indent-style): New function renamed from 'c-ts-mode--set-indent-style'.
-rw-r--r--lisp/progmodes/c-ts-mode.el67
1 files changed, 48 insertions, 19 deletions
diff --git a/lisp/progmodes/c-ts-mode.el b/lisp/progmodes/c-ts-mode.el
index eb2be9b792b..c6eb1afecec 100644
--- a/lisp/progmodes/c-ts-mode.el
+++ b/lisp/progmodes/c-ts-mode.el
@@ -92,6 +92,34 @@
92 :safe 'integerp 92 :safe 'integerp
93 :group 'c) 93 :group 'c)
94 94
95(defun c-ts-mode--indent-style-setter (sym val)
96 "Custom setter for `c-ts-mode-set-style'."
97 (set-default sym val)
98 (named-let loop ((res nil)
99 (buffers (buffer-list)))
100 (if (null buffers)
101 (mapc (lambda (b)
102 (with-current-buffer b
103 (setq-local treesit-simple-indent-rules
104 (treesit--indent-rules-optimize
105 (c-ts-mode--get-indent-style
106 (if (eq major-mode 'c-ts-mode) 'c 'cpp))))))
107 res)
108 (let ((buffer (car buffers)))
109 (with-current-buffer buffer
110 (if (or (eq major-mode 'c-ts-mode) (eq major-mode 'c++-ts-mode))
111 (loop (append res (list buffer)) (cdr buffers))
112 (loop res (cdr buffers))))))))
113
114(defun c-ts-mode--get-indent-style (mode)
115 "Helper function to set indentation style.
116MODE is either `c' or `cpp'."
117 (let ((style
118 (if (functionp c-ts-mode-indent-style)
119 (funcall c-ts-mode-indent-style)
120 (alist-get c-ts-mode-indent-style (c-ts-mode--indent-styles mode)))))
121 `((,mode ,@style))))
122
95(defcustom c-ts-mode-indent-style 'gnu 123(defcustom c-ts-mode-indent-style 'gnu
96 "Style used for indentation. 124 "Style used for indentation.
97 125
@@ -100,13 +128,27 @@ one of the supplied styles doesn't suffice a function could be
100set instead. This function is expected return a list that 128set instead. This function is expected return a list that
101follows the form of `treesit-simple-indent-rules'." 129follows the form of `treesit-simple-indent-rules'."
102 :version "29.1" 130 :version "29.1"
103 :type '(choice (symbol :tag "Gnu" 'gnu) 131 :type '(choice (symbol :tag "Gnu" gnu)
104 (symbol :tag "K&R" 'k&r) 132 (symbol :tag "K&R" k&r)
105 (symbol :tag "Linux" 'linux) 133 (symbol :tag "Linux" linux)
106 (symbol :tag "BSD" 'bsd) 134 (symbol :tag "BSD" bsd)
107 (function :tag "A function for user customized style" ignore)) 135 (function :tag "A function for user customized style" ignore))
136 :set #'c-ts-mode--indent-style-setter
108 :group 'c) 137 :group 'c)
109 138
139(defun c-ts-mode-set-style ()
140 (interactive)
141 (or (eq major-mode 'c-ts-mode) (eq major-mode 'c++-ts-mode)
142 (error "Buffer %s is not a c-ts-mode (c-ts-mode-set-style)"
143 (buffer-name)))
144 (c-ts-mode--indent-style-setter
145 'c-ts-mode-indent-style
146 (intern
147 (completing-read
148 "Select style: "
149 (mapcar #'car (c-ts-mode--indent-styles (if (eq major-mode 'c-ts-mode) 'c 'cpp)))
150 nil t nil nil "gnu"))))
151
110;;; Syntax table 152;;; Syntax table
111 153
112(defvar c-ts-mode--syntax-table 154(defvar c-ts-mode--syntax-table
@@ -249,19 +291,6 @@ MODE is either `c' or `cpp'."
249 ((parent-is "do_statement") parent-bol 0) 291 ((parent-is "do_statement") parent-bol 0)
250 ,@common)))) 292 ,@common))))
251 293
252(defun c-ts-mode--set-indent-style (mode)
253 "Helper function to set indentation style.
254MODE is either `c' or `cpp'."
255 (let ((style
256 (if (functionp c-ts-mode-indent-style)
257 (funcall c-ts-mode-indent-style)
258 (pcase c-ts-mode-indent-style
259 ('gnu (alist-get 'gnu (c-ts-mode--indent-styles mode)))
260 ('k&r (alist-get 'k&r (c-ts-mode--indent-styles mode)))
261 ('bsd (alist-get 'bsd (c-ts-mode--indent-styles mode)))
262 ('linux (alist-get 'linux (c-ts-mode--indent-styles mode)))))))
263 `((,mode ,@style))))
264
265(defun c-ts-mode--top-level-label-matcher (node &rest _) 294(defun c-ts-mode--top-level-label-matcher (node &rest _)
266 "A matcher that matches a top-level label. 295 "A matcher that matches a top-level label.
267NODE should be a labeled_statement." 296NODE should be a labeled_statement."
@@ -840,7 +869,7 @@ in your configuration."
840 (setq-local comment-end " */") 869 (setq-local comment-end " */")
841 ;; Indent. 870 ;; Indent.
842 (setq-local treesit-simple-indent-rules 871 (setq-local treesit-simple-indent-rules
843 (c-ts-mode--set-indent-style 'c)) 872 (c-ts-mode--get-indent-style 'c))
844 ;; Font-lock. 873 ;; Font-lock.
845 (setq-local treesit-font-lock-settings (c-ts-mode--font-lock-settings 'c)) 874 (setq-local treesit-font-lock-settings (c-ts-mode--font-lock-settings 'c))
846 (treesit-major-mode-setup))) 875 (treesit-major-mode-setup)))
@@ -870,7 +899,7 @@ in your configuration."
870 #'c-ts-mode--syntax-propertize) 899 #'c-ts-mode--syntax-propertize)
871 ;; Indent. 900 ;; Indent.
872 (setq-local treesit-simple-indent-rules 901 (setq-local treesit-simple-indent-rules
873 (c-ts-mode--set-indent-style 'cpp)) 902 (c-ts-mode--get-indent-style 'cpp))
874 ;; Font-lock. 903 ;; Font-lock.
875 (setq-local treesit-font-lock-settings (c-ts-mode--font-lock-settings 'cpp)) 904 (setq-local treesit-font-lock-settings (c-ts-mode--font-lock-settings 'cpp))
876 (treesit-major-mode-setup))) 905 (treesit-major-mode-setup)))