diff options
| author | Yuan Fu | 2023-02-03 18:35:33 -0800 |
|---|---|---|
| committer | Yuan Fu | 2023-02-04 15:33:26 -0800 |
| commit | b80f36b88c76b8f8ce3f2e6f9bd56aa2ccbe7b39 (patch) | |
| tree | 1e4a8f660ce45e3e267778badf76362174f2835c | |
| parent | 671e5d9fad5852165f4e63992c91cd6f8c715004 (diff) | |
| download | emacs-b80f36b88c76b8f8ce3f2e6f9bd56aa2ccbe7b39.tar.gz emacs-b80f36b88c76b8f8ce3f2e6f9bd56aa2ccbe7b39.zip | |
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.
| -rw-r--r-- | lisp/progmodes/c-ts-mode.el | 29 | ||||
| -rw-r--r-- | test/lisp/progmodes/c-ts-mode-resources/indent-bsd.erts | 2 | ||||
| -rw-r--r-- | test/lisp/progmodes/c-ts-mode-resources/indent.erts | 4 |
3 files changed, 20 insertions, 15 deletions
diff --git a/lisp/progmodes/c-ts-mode.el b/lisp/progmodes/c-ts-mode.el index 2a164af26ea..1a8ef79dac8 100644 --- a/lisp/progmodes/c-ts-mode.el +++ b/lisp/progmodes/c-ts-mode.el | |||
| @@ -89,18 +89,19 @@ | |||
| 89 | 89 | ||
| 90 | (defun c-ts-mode--indent-style-setter (sym val) | 90 | (defun c-ts-mode--indent-style-setter (sym val) |
| 91 | "Custom setter for `c-ts-mode-set-style'. | 91 | "Custom setter for `c-ts-mode-set-style'. |
| 92 | |||
| 92 | Apart from setting the default value of SYM to VAL, also change | 93 | Apart from setting the default value of SYM to VAL, also change |
| 93 | the value of SYM in `c-ts-mode' and `c++-ts-mode' buffers to VAL." | 94 | the value of SYM in `c-ts-mode' and `c++-ts-mode' buffers to VAL. |
| 95 | |||
| 96 | SYM should be `c-ts-mode-indent-style', and VAL should be a style | ||
| 97 | symbol." | ||
| 94 | (set-default sym val) | 98 | (set-default sym val) |
| 95 | (named-let loop ((res nil) | 99 | (named-let loop ((res nil) |
| 96 | (buffers (buffer-list))) | 100 | (buffers (buffer-list))) |
| 97 | (if (null buffers) | 101 | (if (null buffers) |
| 98 | (mapc (lambda (b) | 102 | (mapc (lambda (b) |
| 99 | (with-current-buffer b | 103 | (with-current-buffer b |
| 100 | (setq-local treesit-simple-indent-rules | 104 | (c-ts-mode-set-style val))) |
| 101 | (treesit--indent-rules-optimize | ||
| 102 | (c-ts-mode--get-indent-style | ||
| 103 | (if (derived-mode-p 'c-ts-mode) 'c 'cpp)))))) | ||
| 104 | res) | 105 | res) |
| 105 | (let ((buffer (car buffers))) | 106 | (let ((buffer (car buffers))) |
| 106 | (with-current-buffer buffer | 107 | (with-current-buffer buffer |
| @@ -112,8 +113,8 @@ the value of SYM in `c-ts-mode' and `c++-ts-mode' buffers to VAL." | |||
| 112 | "Style used for indentation. | 113 | "Style used for indentation. |
| 113 | 114 | ||
| 114 | The selected style could be one of GNU, K&R, LINUX or BSD. If | 115 | The selected style could be one of GNU, K&R, LINUX or BSD. If |
| 115 | one of the supplied styles doesn't suffice a function could be | 116 | one of the supplied styles doesn't suffice, a function could be |
| 116 | set instead. This function is expected return a list that | 117 | set instead. This function is expected to return a list that |
| 117 | follows the form of `treesit-simple-indent-rules'." | 118 | follows the form of `treesit-simple-indent-rules'." |
| 118 | :version "29.1" | 119 | :version "29.1" |
| 119 | :type '(choice (symbol :tag "Gnu" gnu) | 120 | :type '(choice (symbol :tag "Gnu" gnu) |
| @@ -134,7 +135,7 @@ MODE is either `c' or `cpp'." | |||
| 134 | `((,mode ,@style)))) | 135 | `((,mode ,@style)))) |
| 135 | 136 | ||
| 136 | (defun c-ts-mode--prompt-for-style () | 137 | (defun c-ts-mode--prompt-for-style () |
| 137 | "Prompt for a indent style and return the symbol for it." | 138 | "Prompt for an indent style and return the symbol for it." |
| 138 | (let ((mode (if (derived-mode-p 'c-ts-mode) 'c 'c++))) | 139 | (let ((mode (if (derived-mode-p 'c-ts-mode) 'c 'c++))) |
| 139 | (intern | 140 | (intern |
| 140 | (completing-read | 141 | (completing-read |
| @@ -142,16 +143,20 @@ MODE is either `c' or `cpp'." | |||
| 142 | (mapcar #'car (c-ts-mode--indent-styles mode)) | 143 | (mapcar #'car (c-ts-mode--indent-styles mode)) |
| 143 | nil t nil nil "gnu")))) | 144 | nil t nil nil "gnu")))) |
| 144 | 145 | ||
| 145 | (defun c-ts-mode-set-style (style) | 146 | (defun c-ts-mode-set-global-style (style) |
| 146 | "Set the indent style of C/C++ modes globally to STYLE. | 147 | "Set the indent style of C/C++ modes globally to STYLE. |
| 147 | 148 | ||
| 148 | This changes the current indent style of every C/C++ buffer and | 149 | This changes the current indent style of every C/C++ buffer and |
| 149 | the default C/C++ indent style in this Emacs session." | 150 | the default C/C++ indent style for `c-ts-mode' and `c++-ts-mode' |
| 151 | in this Emacs session." | ||
| 150 | (interactive (list (c-ts-mode--prompt-for-style))) | 152 | (interactive (list (c-ts-mode--prompt-for-style))) |
| 151 | (c-ts-mode--indent-style-setter 'c-ts-mode-indent-style style)) | 153 | (c-ts-mode--indent-style-setter 'c-ts-mode-indent-style style)) |
| 152 | 154 | ||
| 153 | (defun c-ts-mode-set-local-style (style) | 155 | (defun c-ts-mode-set-style (style) |
| 154 | "Set the C/C++ indent style of the current buffer to STYLE." | 156 | "Set the C/C++ indent style of the current buffer to STYLE. |
| 157 | |||
| 158 | To set the default indent style globally, use | ||
| 159 | `c-ts-mode-set-global-style'." | ||
| 155 | (interactive (list (c-ts-mode--prompt-for-style))) | 160 | (interactive (list (c-ts-mode--prompt-for-style))) |
| 156 | (if (not (derived-mode-p 'c-ts-mode 'c++-ts-mode)) | 161 | (if (not (derived-mode-p 'c-ts-mode 'c++-ts-mode)) |
| 157 | (user-error "The current buffer is not in `c-ts-mode' nor `c++-ts-mode'") | 162 | (user-error "The current buffer is not in `c-ts-mode' nor `c++-ts-mode'") |
diff --git a/test/lisp/progmodes/c-ts-mode-resources/indent-bsd.erts b/test/lisp/progmodes/c-ts-mode-resources/indent-bsd.erts index ba4f854baf8..74e34fe821b 100644 --- a/test/lisp/progmodes/c-ts-mode-resources/indent-bsd.erts +++ b/test/lisp/progmodes/c-ts-mode-resources/indent-bsd.erts | |||
| @@ -3,7 +3,7 @@ Code: | |||
| 3 | (c-ts-mode) | 3 | (c-ts-mode) |
| 4 | (setq-local indent-tabs-mode nil) | 4 | (setq-local indent-tabs-mode nil) |
| 5 | (setq-local c-ts-mode-indent-offset 2) | 5 | (setq-local c-ts-mode-indent-offset 2) |
| 6 | (c-ts-mode-set-local-style 'bsd) | 6 | (c-ts-mode-set-style 'bsd) |
| 7 | (indent-region (point-min) (point-max))) | 7 | (indent-region (point-min) (point-max))) |
| 8 | 8 | ||
| 9 | Point-Char: | | 9 | Point-Char: | |
diff --git a/test/lisp/progmodes/c-ts-mode-resources/indent.erts b/test/lisp/progmodes/c-ts-mode-resources/indent.erts index 058c6e9099c..7dcc3b0fb3a 100644 --- a/test/lisp/progmodes/c-ts-mode-resources/indent.erts +++ b/test/lisp/progmodes/c-ts-mode-resources/indent.erts | |||
| @@ -3,7 +3,7 @@ Code: | |||
| 3 | (c-ts-mode) | 3 | (c-ts-mode) |
| 4 | (setq-local indent-tabs-mode nil) | 4 | (setq-local indent-tabs-mode nil) |
| 5 | (setq-local c-ts-mode-indent-offset 2) | 5 | (setq-local c-ts-mode-indent-offset 2) |
| 6 | (c-ts-mode-set-local-style 'gnu) | 6 | (c-ts-mode-set-style 'gnu) |
| 7 | (indent-region (point-min) (point-max))) | 7 | (indent-region (point-min) (point-max))) |
| 8 | 8 | ||
| 9 | Point-Char: | | 9 | Point-Char: | |
| @@ -196,7 +196,7 @@ Code: | |||
| 196 | (c-ts-mode) | 196 | (c-ts-mode) |
| 197 | (setq-local indent-tabs-mode nil) | 197 | (setq-local indent-tabs-mode nil) |
| 198 | (setq-local c-ts-mode-indent-offset 8) | 198 | (setq-local c-ts-mode-indent-offset 8) |
| 199 | (c-ts-mode-set-local-style 'linux) | 199 | (c-ts-mode-set-style 'linux) |
| 200 | (indent-region (point-min) (point-max))) | 200 | (indent-region (point-min) (point-max))) |
| 201 | 201 | ||
| 202 | Name: Labels (Linux Style) | 202 | Name: Labels (Linux Style) |