emacs-devel
[Top][All Lists]
Advanced

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

Re: Turning on/off tree-sitter modes


From: Stefan Monnier
Subject: Re: Turning on/off tree-sitter modes
Date: Tue, 03 Dec 2024 09:55:55 -0500
User-agent: Gnus/5.13 (Gnus v5.13)

>> >> Do you have a concrete example?
>> >
>> > cmake-ts-mode, dockerfile-ts-mode, elixir-ts-mode, lua-ts-mode, to
>> > name just a few.  Basically any FOO-ts-mode for which we don't have a
>> > corresponding FOO-mode.
>> 
>> Let's take the `cmake-ts-mode` as an example, then.
>> I consider it currently broken:
>> 
>>     emacs -Q foo.cmake
>> 
>> should put you in `cmake-ts-mode` when you have the CMake
>> tree-sitter grammar installed, but instead it remains in
>> fundamental-mode.
>> 
>> Until this is fixed I don't see any sense in discussing how the users
>> can specify whether they prefer `cmake-mode` or `cmake-ts-mode`.
>
> I think we are mis-communicating.  This thread's Subject is "Turning
> on/off tree-sitter modes", so I'm asking: what we will tell users to
> to do if they want to turn ON cmake-ts-mode?

And the answer depends on how we fix the current breakage, but most
likely it will be "same as for other modes, except doing nothing works
as well"?

> I didn't mean to say anything about the imaginary cmake-mode, I meant
> to point out that it doesn't exist in Emacs, so our solution to this
> cannot be based solely on major-mode-remap-defaults/alist.

Why not?

One way to fix the current breakage is with a patch like the one below,
after which the way to choose which CMake mode to use is the same as for
any other mode.


        Stefan


diff --git a/lisp/progmodes/cmake-ts-mode.el b/lisp/progmodes/cmake-ts-mode.el
index 597ef69d9b8..bf713fff2ae 100644
--- a/lisp/progmodes/cmake-ts-mode.el
+++ b/lisp/progmodes/cmake-ts-mode.el
@@ -245,8 +245,22 @@ cmake-ts-mode
 (derived-mode-add-parents 'cmake-ts-mode '(cmake-mode))
 
 (if (treesit-ready-p 'cmake)
-    (add-to-list 'auto-mode-alist
-                 '("\\(?:CMakeLists\\.txt\\|\\.cmake\\)\\'" . cmake-ts-mode)))
+    (add-to-list 'major-mode-remap-defaults '(cmake-mode . cmake-ts-mode)))
+
+;;;###autoload (add-to-list 'auto-mode-alist
+;;;###autoload              '("\\(?:CMakeLists\\.txt\\|\\.cmake\\)\\'"
+;;;###autoload                . cmake-mode))
+
+;;;###autoload (add-to-list 'major-mode-remap-defaults
+;;;###autoload              '(cmake-mode . cmake-ts-mode-maybe))
+
+;;;###autoload
+(defun cmake-ts-mode-maybe ()
+  "Enable `cmake-ts-mode' if available."
+  (cond
+   ((treesit-ready-p 'cmake) (cmake-ts-mode))
+   ((fboundp 'cmake-mode) (cmake-mode))
+   (t (fundamental-mode))))
 
 (provide 'cmake-ts-mode)
 




reply via email to

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