From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Stefan Monnier Newsgroups: gmane.emacs.devel Subject: Re: Make all tree-sitter modes optional Date: Wed, 15 Feb 2023 13:34:02 -0500 Message-ID: References: <83o7rexe2n.fsf@gnu.org> <83h6x5xym7.fsf@gnu.org> <83h6wr6gmz.fsf@gnu.org> <868ri140sr.fsf@mail.linkov.net> <83fsc92gbz.fsf@gnu.org> <83cz6ccagy.fsf@gnu.org> <838rgzaqmj.fsf@gnu.org> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="24360"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Cc: Eli Zaretskii , juri@linkov.net, casouri@gmail.com, larsi@gnus.org, theo@thornhill.no, jostein@secure.kjonigsen.net, emacs-devel@gnu.org To: Alan Mackenzie Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Wed Feb 15 19:34:34 2023 Return-path: Envelope-to: ged-emacs-devel@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pSMbx-000639-Hg for ged-emacs-devel@m.gmane-mx.org; Wed, 15 Feb 2023 19:34:33 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pSMbc-0006W0-7f; Wed, 15 Feb 2023 13:34:12 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pSMbb-0006VM-6K for emacs-devel@gnu.org; Wed, 15 Feb 2023 13:34:11 -0500 Original-Received: from mailscanner.iro.umontreal.ca ([132.204.25.50]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pSMbZ-0002ei-3v; Wed, 15 Feb 2023 13:34:10 -0500 Original-Received: from pmg1.iro.umontreal.ca (localhost.localdomain [127.0.0.1]) by pmg1.iro.umontreal.ca (Proxmox) with ESMTP id 9B96F1000BF; Wed, 15 Feb 2023 13:34:06 -0500 (EST) Original-Received: from mail01.iro.umontreal.ca (unknown [172.31.2.1]) by pmg1.iro.umontreal.ca (Proxmox) with ESMTP id 51B7B10008C; Wed, 15 Feb 2023 13:34:04 -0500 (EST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=iro.umontreal.ca; s=mail; t=1676486044; bh=ViUaAOXKrnsMAtrDcwJFqhik1N6S8eaUXSUIdOLEh9I=; h=From:To:Cc:Subject:In-Reply-To:References:Date:From; b=AsCfwqDyMP/7aG8fxZLJ45WycBSNX1NoOZax33br27l9+nWn1tp0OnXWi18fWP9PI Z/uVj2o/loBLVrc2XAB3pjuykFt+eMMbVODVfwlEL91l9h0m80I0a0s7g3veAtxXP/ 3N+UASuf1mKjMvpEzu/1tf7Xfi4A0B+P6jMY0WfGifqsIg4rGArXySLJaiAeIz3xjQ wzyllQlpnTldV7H3qqxoNICQHBa3MrqAgkIXtaQthdvsKXR/IEge39pSoYzjZcpHy/ 21XXaoBwbuylNUQcT2EE5P8Lj/akQpKqaLVOngWmEd0kOibn3KVHtAFC0BKyvUYK7S /1rh3ClXV7Lhw== Original-Received: from pastel (104-222-113-2.cpe.teksavvy.com [104.222.113.2]) by mail01.iro.umontreal.ca (Postfix) with ESMTPSA id 0FFC61202E7; Wed, 15 Feb 2023 13:34:04 -0500 (EST) In-Reply-To: (Alan Mackenzie's message of "Wed, 15 Feb 2023 17:57:15 +0000") Received-SPF: pass client-ip=132.204.25.50; envelope-from=monnier@iro.umontreal.ca; helo=mailscanner.iro.umontreal.ca X-Spam_score_int: -42 X-Spam_score: -4.3 X-Spam_bar: ---- X-Spam_report: (-4.3 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.devel:303320 Archived-At: > 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)