summaryrefslogtreecommitdiff
path: root/lisp/progmodes/c-ts-mode.el
diff options
context:
space:
mode:
authorTheodor Thornhill <theo@thornhill.no>2023-02-17 23:46:24 +0100
committerTheodor Thornhill <theo@thornhill.no>2023-02-19 20:47:05 +0100
commitd397f3d508464b1eab2d2b3b7e4c3b80aff9d536 (patch)
treeb953b86bcc0793904beadcddaf8cdc0e2ce9206d /lisp/progmodes/c-ts-mode.el
parent683961cd733d4a660ffdb9c1a6e6ad565ffae2cb (diff)
downloademacs-d397f3d508464b1eab2d2b3b7e4c3b80aff9d536.tar.gz
emacs-d397f3d508464b1eab2d2b3b7e4c3b80aff9d536.tar.bz2
emacs-d397f3d508464b1eab2d2b3b7e4c3b80aff9d536.zip
Add comment style toggle for c-ts-mode (bug#61550)
* lisp/progmodes/c-ts-mode.el (c-ts-mode-toggle-comment-style): New command. (c-ts-base-mode-map): Add binding. (c-ts-mode-set-modeline): New function. (c-ts-mode): Set modeline. (c++-ts-mode): Set modeline.
Diffstat (limited to 'lisp/progmodes/c-ts-mode.el')
-rw-r--r--lisp/progmodes/c-ts-mode.el24
1 files changed, 23 insertions, 1 deletions
diff --git a/lisp/progmodes/c-ts-mode.el b/lisp/progmodes/c-ts-mode.el
index a60c464093e..05875e9267a 100644
--- a/lisp/progmodes/c-ts-mode.el
+++ b/lisp/progmodes/c-ts-mode.el
@@ -87,6 +87,25 @@
:safe 'integerp
:group 'c)
+(defun c-ts-mode-toggle-comment-style ()
+ "Toggle the comment style between block and line comments.
+Optional numeric ARG, if supplied, switches to block comment
+style when positive, to line comment style when negative, and
+just toggles it when zero or left out."
+ (interactive)
+ (pcase-let ((`(,starter . ,ender)
+ (if (string= comment-start "// ")
+ (cons "/* " " */")
+ (cons "// " ""))))
+ (setq-local comment-start starter
+ comment-end ender))
+ (c-ts-mode-set-modeline))
+
+(defun c-ts-mode-set-modeline ()
+ (setq mode-name
+ (concat (if (eq major-mode 'c-ts-mode) "C" "C++") comment-start))
+ (force-mode-line-update))
+
(defun c-ts-mode--indent-style-setter (sym val)
"Custom setter for `c-ts-mode-set-style'.
@@ -740,7 +759,8 @@ the semicolon. This function skips the semicolon."
:parent prog-mode-map
"C-c C-q" #'c-ts-mode-indent-defun
"C-c ." #'c-ts-mode-set-style
- "C-c C-c" #'comment-region)
+ "C-c C-c" #'comment-region
+ "C-c C-k" #'c-ts-mode-toggle-comment-style)
;;;###autoload
(define-derived-mode c-ts-base-mode prog-mode "C"
@@ -824,6 +844,7 @@ To use tree-sitter C/C++ modes by default, evaluate
in your configuration."
:group 'c
+ :after-hook (c-ts-mode-set-modeline)
(when (treesit-ready-p 'c)
(treesit-parser-create 'c)
@@ -856,6 +877,7 @@ To use tree-sitter C/C++ modes by default, evaluate
in your configuration."
:group 'c++
+ :after-hook (c-ts-mode-set-modeline)
(when (treesit-ready-p 'cpp)
(treesit-parser-create 'cpp)