Make c-ts-mode-set-style's effect local (bug#61245)

Now c-ts-mode-set-style's effect is local, and there is a new function
c-ts-mode-set-global-style that changes the global setting.

* lisp/progmodes/c-ts-mode.el:
(c-ts-mode--indent-style-setter): Use c-ts-mode-set-style.
(c-ts-mode-indent-style)
(c-ts-mode--prompt-for-style): Minor change in docstring.
(c-ts-mode-set-global-style): New function (from c-ts-mode-set-style).
(c-ts-mode-set-local-style): Remove function (became c-ts-mode-set-style).
(c-ts-mode-set-style): Renamed from c-ts-mode-set-local-style.
* test/lisp/progmodes/c-ts-mode-resources/indent-bsd.erts:
* test/lisp/progmodes/c-ts-mode-resources/indent.erts: Use
c-ts-mode-set-style.
This commit is contained in:
Yuan Fu 2023-02-03 18:35:33 -08:00
parent 671e5d9fad
commit b80f36b88c
No known key found for this signature in database
GPG key ID: 56E19BC57664A442
3 changed files with 20 additions and 15 deletions

View file

@ -89,18 +89,19 @@
(defun c-ts-mode--indent-style-setter (sym val) (defun c-ts-mode--indent-style-setter (sym val)
"Custom setter for `c-ts-mode-set-style'. "Custom setter for `c-ts-mode-set-style'.
Apart from setting the default value of SYM to VAL, also change Apart from setting the default value of SYM to VAL, also change
the value of SYM in `c-ts-mode' and `c++-ts-mode' buffers to VAL." the value of SYM in `c-ts-mode' and `c++-ts-mode' buffers to VAL.
SYM should be `c-ts-mode-indent-style', and VAL should be a style
symbol."
(set-default sym val) (set-default sym val)
(named-let loop ((res nil) (named-let loop ((res nil)
(buffers (buffer-list))) (buffers (buffer-list)))
(if (null buffers) (if (null buffers)
(mapc (lambda (b) (mapc (lambda (b)
(with-current-buffer b (with-current-buffer b
(setq-local treesit-simple-indent-rules (c-ts-mode-set-style val)))
(treesit--indent-rules-optimize
(c-ts-mode--get-indent-style
(if (derived-mode-p 'c-ts-mode) 'c 'cpp))))))
res) res)
(let ((buffer (car buffers))) (let ((buffer (car buffers)))
(with-current-buffer buffer (with-current-buffer buffer
@ -112,8 +113,8 @@ the value of SYM in `c-ts-mode' and `c++-ts-mode' buffers to VAL."
"Style used for indentation. "Style used for indentation.
The selected style could be one of GNU, K&R, LINUX or BSD. If The selected style could be one of GNU, K&R, LINUX or BSD. If
one of the supplied styles doesn't suffice a function could be one of the supplied styles doesn't suffice, a function could be
set instead. This function is expected return a list that set instead. This function is expected to return a list that
follows the form of `treesit-simple-indent-rules'." follows the form of `treesit-simple-indent-rules'."
:version "29.1" :version "29.1"
:type '(choice (symbol :tag "Gnu" gnu) :type '(choice (symbol :tag "Gnu" gnu)
@ -134,7 +135,7 @@ MODE is either `c' or `cpp'."
`((,mode ,@style)))) `((,mode ,@style))))
(defun c-ts-mode--prompt-for-style () (defun c-ts-mode--prompt-for-style ()
"Prompt for a indent style and return the symbol for it." "Prompt for an indent style and return the symbol for it."
(let ((mode (if (derived-mode-p 'c-ts-mode) 'c 'c++))) (let ((mode (if (derived-mode-p 'c-ts-mode) 'c 'c++)))
(intern (intern
(completing-read (completing-read
@ -142,16 +143,20 @@ MODE is either `c' or `cpp'."
(mapcar #'car (c-ts-mode--indent-styles mode)) (mapcar #'car (c-ts-mode--indent-styles mode))
nil t nil nil "gnu")))) nil t nil nil "gnu"))))
(defun c-ts-mode-set-style (style) (defun c-ts-mode-set-global-style (style)
"Set the indent style of C/C++ modes globally to STYLE. "Set the indent style of C/C++ modes globally to STYLE.
This changes the current indent style of every C/C++ buffer and This changes the current indent style of every C/C++ buffer and
the default C/C++ indent style in this Emacs session." the default C/C++ indent style for `c-ts-mode' and `c++-ts-mode'
in this Emacs session."
(interactive (list (c-ts-mode--prompt-for-style))) (interactive (list (c-ts-mode--prompt-for-style)))
(c-ts-mode--indent-style-setter 'c-ts-mode-indent-style style)) (c-ts-mode--indent-style-setter 'c-ts-mode-indent-style style))
(defun c-ts-mode-set-local-style (style) (defun c-ts-mode-set-style (style)
"Set the C/C++ indent style of the current buffer to STYLE." "Set the C/C++ indent style of the current buffer to STYLE.
To set the default indent style globally, use
`c-ts-mode-set-global-style'."
(interactive (list (c-ts-mode--prompt-for-style))) (interactive (list (c-ts-mode--prompt-for-style)))
(if (not (derived-mode-p 'c-ts-mode 'c++-ts-mode)) (if (not (derived-mode-p 'c-ts-mode 'c++-ts-mode))
(user-error "The current buffer is not in `c-ts-mode' nor `c++-ts-mode'") (user-error "The current buffer is not in `c-ts-mode' nor `c++-ts-mode'")

View file

@ -3,7 +3,7 @@ Code:
(c-ts-mode) (c-ts-mode)
(setq-local indent-tabs-mode nil) (setq-local indent-tabs-mode nil)
(setq-local c-ts-mode-indent-offset 2) (setq-local c-ts-mode-indent-offset 2)
(c-ts-mode-set-local-style 'bsd) (c-ts-mode-set-style 'bsd)
(indent-region (point-min) (point-max))) (indent-region (point-min) (point-max)))
Point-Char: | Point-Char: |

View file

@ -3,7 +3,7 @@ Code:
(c-ts-mode) (c-ts-mode)
(setq-local indent-tabs-mode nil) (setq-local indent-tabs-mode nil)
(setq-local c-ts-mode-indent-offset 2) (setq-local c-ts-mode-indent-offset 2)
(c-ts-mode-set-local-style 'gnu) (c-ts-mode-set-style 'gnu)
(indent-region (point-min) (point-max))) (indent-region (point-min) (point-max)))
Point-Char: | Point-Char: |
@ -196,7 +196,7 @@ Code:
(c-ts-mode) (c-ts-mode)
(setq-local indent-tabs-mode nil) (setq-local indent-tabs-mode nil)
(setq-local c-ts-mode-indent-offset 8) (setq-local c-ts-mode-indent-offset 8)
(c-ts-mode-set-local-style 'linux) (c-ts-mode-set-style 'linux)
(indent-region (point-min) (point-max))) (indent-region (point-min) (point-max)))
Name: Labels (Linux Style) Name: Labels (Linux Style)