unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: Stefan Monnier <monnier@iro.umontreal.ca>
To: Alan Mackenzie <acm@muc.de>
Cc: Eli Zaretskii <eliz@gnu.org>,
	 juri@linkov.net,  casouri@gmail.com, larsi@gnus.org,
	 theo@thornhill.no,  jostein@secure.kjonigsen.net,
	emacs-devel@gnu.org
Subject: Re: Make all tree-sitter modes optional
Date: Wed, 15 Feb 2023 13:34:02 -0500	[thread overview]
Message-ID: <jwvcz6a4wwq.fsf-monnier+emacs@gnu.org> (raw)
In-Reply-To: <Y+0c+0zaXVbguSvC@ACM> (Alan Mackenzie's message of "Wed, 15 Feb 2023 17:57:15 +0000")

> 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)
 




  parent reply	other threads:[~2023-02-15 18:34 UTC|newest]

Thread overview: 119+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <84973.1672843723@hassadar.pretzelnet.org>
     [not found] ` <83wn62xi3k.fsf@gnu.org>
     [not found]   ` <m1cz7u5brr.fsf@yahoo.es>
     [not found]     ` <83o7rexe2n.fsf@gnu.org>
     [not found]       ` <83h6x5xym7.fsf@gnu.org>
2023-01-15 14:01         ` Make all tree-sitter modes optional Eli Zaretskii
2023-01-15 23:39           ` Dmitry Gutov
2023-01-16  7:43             ` Theodor Thornhill
2023-01-16 16:30               ` Dmitry Gutov
2023-01-16 12:34             ` Eli Zaretskii
2023-01-16 13:06               ` Dmitry Gutov
2023-01-16 14:20                 ` Eli Zaretskii
2023-01-16 14:50                   ` Dmitry Gutov
2023-01-16 14:59                     ` Eli Zaretskii
2023-01-17 12:59                       ` Dmitry Gutov
2023-01-17 13:47                         ` Eli Zaretskii
2023-01-17 14:32                           ` Dmitry Gutov
2023-01-17 14:52                             ` Eli Zaretskii
2023-01-17 15:22                               ` Dmitry Gutov
2023-01-17 17:02                                 ` Eli Zaretskii
2023-01-17 17:10                                   ` Dmitry Gutov
2023-01-17 17:38                                     ` Eli Zaretskii
2023-01-17 17:59                                       ` Dmitry Gutov
2023-01-17 18:04                                         ` Eli Zaretskii
2023-01-17 18:21                                           ` Dmitry Gutov
2023-01-17 18:40                                             ` Eli Zaretskii
2023-01-17 18:49                                               ` Dmitry Gutov
2023-01-17 19:03                                                 ` Eli Zaretskii
2023-01-17 19:21                                                   ` Dmitry Gutov
2023-01-18  1:11                                                     ` Yuan Fu
2023-01-18  1:23                                                       ` Dmitry Gutov
2023-01-18  3:34                                                         ` Eli Zaretskii
2023-01-18  3:52                                                           ` Dmitry Gutov
2023-01-18 12:06                                                             ` Eli Zaretskii
2023-01-18 14:00                                                               ` Dmitry Gutov
2023-01-18 14:51                                                                 ` Eli Zaretskii
2023-01-18 12:36                                                           ` Stefan Monnier
2023-01-17 17:34                             ` treesit-forward-sexp (was: Make all tree-sitter modes optional) Juri Linkov
2023-01-17 17:40                               ` Theodor Thornhill
2023-01-17 18:17                                 ` treesit-forward-sexp Juri Linkov
2023-01-17 17:50                               ` treesit-forward-sexp (was: Make all tree-sitter modes optional) Dmitry Gutov
2023-01-17 17:59                               ` Eli Zaretskii
2023-01-16  1:12           ` Make all tree-sitter modes optional Po Lu
2023-01-17 17:30           ` Juri Linkov
2023-01-17 17:58             ` Eli Zaretskii
2023-01-17 18:19               ` Juri Linkov
2023-01-17 18:41                 ` Eli Zaretskii
2023-02-14 19:08               ` Alan Mackenzie
2023-02-14 19:29                 ` Eli Zaretskii
2023-02-14 21:02                   ` Alan Mackenzie
2023-02-15 15:35                     ` Eli Zaretskii
2023-02-15 17:57                       ` Alan Mackenzie
2023-02-15 18:33                         ` Eli Zaretskii
2023-02-15 20:31                           ` Alan Mackenzie
2023-02-16  5:41                             ` tomas
2023-02-16  7:27                             ` Eli Zaretskii
2023-02-16 22:05                               ` Yuan Fu
     [not found]                           ` <87v8k2g04m.fsf@dick>
2023-02-15 20:34                             ` Eli Zaretskii
     [not found]                               ` <87mt5eegkw.fsf@dick>
2023-02-16  7:53                                 ` Eli Zaretskii
2023-02-16  8:52                                   ` Po Lu
2023-02-15 21:40                           ` Alan Mackenzie
2023-02-17 13:30                           ` Alan Mackenzie
2023-02-17 13:37                             ` Po Lu
2023-02-17 13:46                               ` Stefan Monnier
2023-02-17 14:16                                 ` Po Lu
2023-02-17 14:40                                   ` Eli Zaretskii
2023-02-17 14:56                                     ` Dmitry Gutov
2023-02-17 15:04                                       ` Eli Zaretskii
     [not found]                                         ` <8735741fic.fsf@dick>
2023-02-17 15:41                                           ` Alan Mackenzie
2023-02-17 16:04                                       ` Make all tree-sitter modes optional Stefan Kangas
2023-02-17 17:42                                         ` Morgan Willcock
2023-02-17 15:15                                     ` Gregory Heytings
2023-02-17 13:54                               ` Alan Mackenzie
     [not found]                               ` <d4c1a7f6-b5bf-f4f3-8d79-1c6b1d07cf70@yandex.ru>
2023-02-17 14:22                                 ` Po Lu
2023-02-17 14:58                             ` Eli Zaretskii
2023-02-17 15:18                               ` Alan Mackenzie
2023-02-15 18:34                         ` Stefan Monnier [this message]
2023-02-15 19:01                           ` Dmitry Gutov
2023-02-15 19:26                             ` Stefan Monnier
2023-02-15 19:47                               ` Eli Zaretskii
2023-02-15 19:53                                 ` Stefan Monnier
2023-02-15 20:06                                   ` Eli Zaretskii
2023-02-15 21:04                                     ` Stefan Monnier
2023-02-16  7:42                                       ` Eli Zaretskii
2023-02-16  9:49                                         ` Basil L. Contovounesios
2023-02-16 11:48                                           ` Eli Zaretskii
2023-02-16 14:41                                         ` Stefan Monnier
2023-02-16 15:42                                           ` Eli Zaretskii
2023-02-16 16:45                                             ` Stefan Monnier
2023-02-16 17:05                                               ` Eli Zaretskii
2023-02-16 19:14                                                 ` Dmitry Gutov
2023-02-16 20:07                                                 ` Stefan Monnier
2023-02-16  5:45                                     ` tomas
2023-02-16  8:26                                       ` Eli Zaretskii
2023-02-16 10:30                                         ` Alan Mackenzie
2023-02-16 12:38                                           ` Po Lu
2023-02-16 12:53                                             ` Dmitry Gutov
2023-02-15 20:24                               ` Dmitry Gutov
2023-02-16  7:05                                 ` Eli Zaretskii
2023-02-16  7:53                                   ` Theodor Thornhill
2023-02-16  8:34                                     ` Eli Zaretskii
2023-02-16  8:46                                       ` Theodor Thornhill
2023-02-16 11:58                                       ` Dmitry Gutov
2023-02-16 11:56                                     ` Dmitry Gutov
2023-02-16 14:48                                       ` Theodor Thornhill via Emacs development discussions.
2023-02-16 14:56                                         ` Dmitry Gutov
2023-02-16 15:15                                           ` Theodor Thornhill
2023-02-15 23:48                               ` Lynn Winebarger
2023-02-16  2:56                                 ` Stefan Monnier
2023-02-15 19:07                           ` Eli Zaretskii
2023-02-15 19:27                             ` Stefan Monnier
2023-02-15 21:06                             ` Basil L. Contovounesios
2023-02-16  7:44                               ` Eli Zaretskii
2023-02-15 18:25                 ` Juri Linkov
2023-01-19 16:53 Pedro Andres Aranda Gutierrez
2023-01-20  8:30 ` Eli Zaretskii
2023-01-20 16:31   ` Pedro Andres Aranda Gutierrez
2023-01-20 19:13     ` Eli Zaretskii
2023-01-21 11:30       ` Pedro Andres Aranda Gutierrez
2023-01-21 11:48         ` Eli Zaretskii
2023-01-22  6:23           ` Pedro Andres Aranda Gutierrez
2023-01-22  6:38             ` Eli Zaretskii
2023-01-22 10:46               ` Eli Zaretskii
  -- strict thread matches above, loose matches on Subject: below --
2023-02-18  7:55 Pedro Andres Aranda Gutierrez
2023-03-11 12:45 ` Ongaro

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=jwvcz6a4wwq.fsf-monnier+emacs@gnu.org \
    --to=monnier@iro.umontreal.ca \
    --cc=acm@muc.de \
    --cc=casouri@gmail.com \
    --cc=eliz@gnu.org \
    --cc=emacs-devel@gnu.org \
    --cc=jostein@secure.kjonigsen.net \
    --cc=juri@linkov.net \
    --cc=larsi@gnus.org \
    --cc=theo@thornhill.no \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).