emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Make all tree-sitter modes optional


From: Stefan Monnier
Subject: Re: Make all tree-sitter modes optional
Date: Wed, 15 Feb 2023 13:34:02 -0500
User-agent: Gnus/5.13 (Gnus v5.13)

> out again is unreasonably difficult.  Even restarting Emacs (which to me
> is a drastic operation) won't opt out if there are still buffers in
> c-ts-mode in the desktop.

Sounds like a bug, indeed.
The `add-to-list` should not happen just because `c-ts-mode` is loaded.
I think it should only happen if `c-ts-mode` is called explicitly
(i.e. interactively).

Maybe something like the patch below.

We could even add a `y-or-n-p` test in `c-ts--activate` to avoid
changing config vars without the user's consent.


        Stefan


diff --git a/lisp/progmodes/c-ts-mode.el b/lisp/progmodes/c-ts-mode.el
index 6db28459c32..e4148649822 100644
--- a/lisp/progmodes/c-ts-mode.el
+++ b/lisp/progmodes/c-ts-mode.el
@@ -740,7 +740,6 @@ c-ts-base-mode-map
   "C-c C-q" #'c-ts-mode-indent-defun
   "C-c ." #'c-ts-mode-set-style)
 
-;;;###autoload
 (define-derived-mode c-ts-base-mode prog-mode "C"
   "Major mode for editing C, powered by tree-sitter.
 
@@ -856,6 +855,7 @@ c-ts-mode
   :group 'c
 
   (when (treesit-ready-p 'c)
+    (when (called-interactively-p 'any) (c-ts--activate))
     (treesit-parser-create 'c)
     ;; Comments.
     (setq-local comment-start "/* ")
@@ -888,6 +888,7 @@ c++-ts-mode
   :group 'c++
 
   (when (treesit-ready-p 'cpp)
+    (when (called-interactively-p 'any) (c-ts--activate))
     (setq-local treesit-text-type-regexp
                 (regexp-opt '("comment"
                               "raw_string_literal")))
@@ -942,6 +943,7 @@ c-or-c++-ts-mode
 the code is C or C++ and based on that chooses whether to enable
 `c-ts-mode' or `c++-ts-mode'."
   (interactive)
+  (when (called-interactively-p 'any) (c-ts--activate))
   (if (save-excursion
         (save-restriction
           (save-match-data ; Why `save-match-data'?
@@ -950,22 +952,30 @@ c-or-c++-ts-mode
             (re-search-forward c-ts-mode--c-or-c++-regexp nil t))))
       (c++-ts-mode)
     (c-ts-mode)))
-;; The entries for C++ must come first to prevent *.c files be taken
-;; as C++ on case-insensitive filesystems, since *.C files are C++,
-;; not C.
-(if (treesit-ready-p 'cpp)
-    (add-to-list 'auto-mode-alist
-                 
'("\\(\\.ii\\|\\.\\(CC?\\|HH?\\)\\|\\.[ch]\\(pp\\|xx\\|\\+\\+\\)\\|\\.\\(cc\\|hh\\)\\)\\'"
-                   . c++-ts-mode)))
-
-(if (treesit-ready-p 'c)
-    (add-to-list 'auto-mode-alist
-                 '("\\(\\.[chi]\\|\\.lex\\|\\.y\\(acc\\)?\\|\\.x[bp]m\\)\\'"
-                   . c-ts-mode)))
-
-(if (and (treesit-ready-p 'cpp)
-         (treesit-ready-p 'c))
-    (add-to-list 'auto-mode-alist '("\\.h\\'" . c-or-c++-ts-mode)))
+
+(defun c-ts--activate ()
+  (unless (or (rassq 'c++-ts-mode auto-mode-alist)
+              (rassq 'c-ts-mode auto-mode-alist)
+              (rassq 'c-or-c++-ts-mode auto-mode-alist)
+              (rassq 'c++-ts-mode major-mode-remap-alist)
+              (rassq 'c-ts-mode major-mode-remap-alist)
+              (rassq 'c-or-c++-ts-mode major-mode-remap-alist))
+    ;; The entries for C++ must come first to prevent *.c files be taken
+    ;; as C++ on case-insensitive filesystems, since *.C files are C++,
+    ;; not C.
+    (if (treesit-ready-p 'cpp)
+        (add-to-list 'auto-mode-alist
+                     
'("\\(\\.ii\\|\\.\\(CC?\\|HH?\\)\\|\\.[ch]\\(pp\\|xx\\|\\+\\+\\)\\|\\.\\(cc\\|hh\\)\\)\\'"
+                       . c++-ts-mode)))
+
+    (if (treesit-ready-p 'c)
+        (add-to-list 'auto-mode-alist
+                     
'("\\(\\.[chi]\\|\\.lex\\|\\.y\\(acc\\)?\\|\\.x[bp]m\\)\\'"
+                       . c-ts-mode)))
+
+    (if (and (treesit-ready-p 'cpp)
+             (treesit-ready-p 'c))
+        (add-to-list 'auto-mode-alist '("\\.h\\'" . c-or-c++-ts-mode)))))
 
 (provide 'c-ts-mode)
 




reply via email to

[Prev in Thread] Current Thread [Next in Thread]