[Top][All Lists]

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

Make all tree-sitter modes optional

From: Eli Zaretskii
Subject: Make all tree-sitter modes optional
Date: Sun, 15 Jan 2023 16:01:40 +0200

This came out of the discussion in bug#60559 and other related
discussions, where I said:

> So here's a suggestion for such a solution: we make all the
> *-ts-mode's optional.  That is, we don't add any of them to
> auto-mode-alist unless the file *-ts-mode.el is loaded, and we
> document them all in NEWS and the user manual as optional.  users who
> want them will have to manually activate them.  This way, the original
> use case that started this bug report is automatically solved, and the
> other use case, where the user intends to activate one of these modes,
> is also served by showing the warning, which in that case is perfectly
> justified: the user asked for something that we cannot do, so we warn
> him/her.
> This is a retreat of sorts, but I think it strikes a better balance
> wrt user expectations, assuming not everyone will build with
> tree-sitter.

The proposed change to the current emacs-29 branch is below.  You will
see that where possible, just loading a TS mode modifies
auto-mode-alist if the tree-sitter support for that mode is available,
whereas for other modes auto-mode-alist is modified only when the mode
is actually turned on successfully for the first time.  This is
because some of the TS modes have their own *.el files, whereas others
share a .el file with other modes, and so loading that file doesn't
necessarily means the user wants to use the tree-sitter based mode.

With these changes, users are supposed to turn the mode manually when
they want to use it, or customize auto-mode-alist in their init files
to turn those modes automatically for certain files.  Modes that have
their own *.el files can be just 'require'd in the init file to modify

This arrangement is perhaps not ideal, but I think it is safe enough
and simple enough to go into Emacs 29.

Comments? objections? ideas for improvements?  I will install in a few
days, barring any serious problems.

Here's the patch:

diff --git a/etc/NEWS b/etc/NEWS
index d1ddd01..996134c 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -34,13 +34,14 @@ This feature existed in Emacs 28.1, but was less easy to 
 ** Emacs can be built with the tree-sitter parsing library.
-This library, together with grammar libraries, provides incremental
-parsing capabilities for several popular programming languages and
-other formatted files.  Emacs built with this library offers major
-modes, described elsewhere in this file, that are based on the
-tree-sitter's parsers.  If you have the tree-sitter library
-installed, the configure script will automatically include it in the
-build; use '--without-tree-sitter' at configure time to disable that.
+This library, together with separate grammar libraries for each
+language, provides incremental parsing capabilities for several
+popular programming languages and other formatted files.  Emacs built
+with this library offers major modes, described elsewhere in this
+file, that are based on the tree-sitter's parsers.  If you have the
+tree-sitter library installed, the configure script will automatically
+include it in the build; use '--without-tree-sitter' at configure time
+to disable that.
 Emacs modes based on the tree-sitter library require an additional
 grammar library for each mode.  These grammar libraries provide the
@@ -3181,19 +3182,10 @@ indentation, and navigation by defuns based on parsing 
the buffer text
 by a tree-sitter parser.  Some major modes also offer support for
 Imenu and 'which-func'.
-Where major modes already exist in Emacs for editing certain kinds of
-files, the new modes based on tree-sitter are for now entirely
-optional, and you must turn them on manually, or customize
-'auto-mode-alist' to turn them on automatically.
-Where no major modes previously existed in Emacs for editing the kinds
-of files for which Emacs now provides a tree-sitter based mode, Emacs
-will now try to enable these new modes automatically when you visit
-such files, and will display a warning if the tree-sitter library or
-the parser grammar library is not available.  To prevent the warnings,
-either build Emacs with tree-sitter and install the grammar libraries,
-or customize 'auto-mode-alist' to specify some other major mode (or
-even 'fundamental-mode') for those kinds of files.
+The new modes based on tree-sitter are for now entirely optional, and
+you must turn them on manually, or load them in your init file, or
+customize 'auto-mode-alist' to turn them on automatically for certain
 Each major mode based on tree-sitter needs a language grammar library,
 usually named "libtree-sitter-LANG.so" ("libtree-sitter-LANG.dll" on
@@ -3210,20 +3202,18 @@ We recommend to install these libraries in one of the 
standard system
 locations (the last place in the above list).
 If a language grammar library required by a mode is not found in any
-of the above places, the mode will signal an error when you try to
+of the above places, the mode will display a warning when you try to
 turn it on.
 *** New major mode 'typescript-ts-mode'.
 A major mode based on the tree-sitter library for editing programs
-in the TypeScript language.  This mode is auto-enabled for files with
-the ".ts" extension.
+in the TypeScript language.
 *** New major mode 'tsx-ts-mode'.
 A major mode based on the tree-sitter library for editing programs
-in the TypeScript language, with support for TSX.  This mode is
-auto-enabled for files with the ".tsx" extension.
+in the TypeScript language, with support for TSX.
 *** New major mode 'c-ts-mode'.
@@ -3268,15 +3258,12 @@ Bash shell scripts.
 *** New major mode 'dockerfile-ts-mode'.
 A major mode based on the tree-sitter library for editing
-Dockerfiles.  This mode is auto-enabled for files which are named
-"Dockerfile", have the "Dockerfile." prefix, or have the ".dockerfile"
 *** New major mode 'cmake-ts-mode'.
 A major mode based on the tree-sitter library for editing CMake files.
-It is auto-enabled for files whose name is "CMakeLists.txt" or whose
-extension is ".cmake".
 *** New major mode 'toml-ts-mode'.
@@ -3286,23 +3273,22 @@ files written in TOML, a format for writing 
configuration files.
 *** New major mode 'go-ts-mode'.
 A major mode based on the tree-sitter library for editing programs in
-the Go language.  It is auto-enabled for files with the ".go" extension.
+the Go language.
 *** New major mode 'go-mod-ts-mode'.
 A major mode based on the tree-sitter library for editing "go.mod"
-files.  It is auto-enabled for files which are named "go.mod".
 *** New major mode 'yaml-ts-mode'.
 A major mode based on the tree-sitter library for editing files
-written in YAML.  It is auto-enabled for files with the ".yaml" or
-".yml" extensions.
+written in YAML.
 *** New major mode 'rust-ts-mode'.
 A major mode based on the tree-sitter library for editing programs in
-the Rust language.  It is auto-enabled for files with the ".rs" extension.
+the Rust language.
 *** New major mode 'ruby-ts-mode'.
diff --git a/lisp/progmodes/c-ts-mode.el b/lisp/progmodes/c-ts-mode.el
index 89a08a6..d004920 100644
--- a/lisp/progmodes/c-ts-mode.el
+++ b/lisp/progmodes/c-ts-mode.el
@@ -972,6 +972,19 @@ c++-ts-mode
     (setq-local treesit-font-lock-settings (c-ts-mode--font-lock-settings 
+;; 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
+                   . c++-ts-mode)))
+(if (treesit-ready-p 'c)
+    (add-to-list 'auto-mode-alist
+                 '("\\(\\.[chi]\\|\\.lex\\|\\.y\\(acc\\)?\\|\\.x[bp]m\\)\\'"
+                   . c-ts-mode)))
 (provide 'c-ts-mode)
 ;;; c-ts-mode.el ends here
diff --git a/lisp/progmodes/cmake-ts-mode.el b/lisp/progmodes/cmake-ts-mode.el
index a31250f..c241a28 100644
--- a/lisp/progmodes/cmake-ts-mode.el
+++ b/lisp/progmodes/cmake-ts-mode.el
@@ -195,10 +195,6 @@ cmake-ts-mode--imenu-1
       `((,name . ,marker))))))
-(add-to-list 'auto-mode-alist
-             '("\\(?:CMakeLists\\.txt\\|\\.cmake\\)\\'" . cmake-ts-mode))
 (define-derived-mode cmake-ts-mode prog-mode "CMake"
   "Major mode for editing CMake files, powered by tree-sitter."
   :group 'cmake
@@ -229,6 +225,10 @@ cmake-ts-mode
+(if (treesit-ready-p 'cmake)
+    (add-to-list 'auto-mode-alist
+                 '("\\(?:CMakeLists\\.txt\\|\\.cmake\\)\\'" . cmake-ts-mode)))
 (provide 'cmake-ts-mode)
 ;;; cmake-ts-mode.el ends here
diff --git a/lisp/progmodes/csharp-mode.el b/lisp/progmodes/csharp-mode.el
index 81ce416..04f7f22 100644
--- a/lisp/progmodes/csharp-mode.el
+++ b/lisp/progmodes/csharp-mode.el
@@ -884,9 +884,6 @@ csharp-ts-mode--defun-name
-(add-to-list 'auto-mode-alist '("\\.cs\\'" . csharp-mode))
 (define-derived-mode csharp-mode prog-mode "C#"
   "Major mode for editing Csharp code.
@@ -941,7 +938,9 @@ csharp-ts-mode
                 ("Struct" "\\`struct_declaration\\'" nil nil)
                 ("Method" "\\`method_declaration\\'" nil nil)))
-  (treesit-major-mode-setup))
+  (treesit-major-mode-setup)
+  (add-to-list 'auto-mode-alist '("\\.cs\\'" . csharp-ts-mode)))
 (provide 'csharp-mode)
diff --git a/lisp/progmodes/dockerfile-ts-mode.el 
index 3f8766e..2a295e8 100644
--- a/lisp/progmodes/dockerfile-ts-mode.el
+++ b/lisp/progmodes/dockerfile-ts-mode.el
@@ -133,12 +133,6 @@ dockerfile-ts-mode--imenu-1
       `((,name . ,marker))))))
-(add-to-list 'auto-mode-alist
-             ;; NOTE: We can't use `rx' here, as it breaks bootstrap.
-             '("\\(?:Dockerfile\\(?:\\..*\\)?\\|\\.[Dd]ockerfile\\)\\'"
-               . dockerfile-ts-mode))
 (define-derived-mode dockerfile-ts-mode prog-mode "Dockerfile"
   "Major mode for editing Dockerfiles, powered by tree-sitter."
   :group 'dockerfile
@@ -172,6 +166,12 @@ dockerfile-ts-mode
+(if (treesit-ready-p 'dockerfile)
+    (add-to-list 'auto-mode-alist
+                 ;; NOTE: We can't use `rx' here, as it breaks bootstrap.
+                 '("\\(?:Dockerfile\\(?:\\..*\\)?\\|\\.[Dd]ockerfile\\)\\'"
+                   . dockerfile-ts-mode)))
 (provide 'dockerfile-ts-mode)
 ;;; dockerfile-ts-mode.el ends here
diff --git a/lisp/progmodes/go-ts-mode.el b/lisp/progmodes/go-ts-mode.el
index 64e761d..d552e13 100644
--- a/lisp/progmodes/go-ts-mode.el
+++ b/lisp/progmodes/go-ts-mode.el
@@ -175,9 +175,6 @@ go-ts-mode--font-lock-settings
   "Tree-sitter font-lock settings for `go-ts-mode'.")
-(add-to-list 'auto-mode-alist '("\\.go\\'" . go-ts-mode))
 (define-derived-mode go-ts-mode prog-mode "Go"
   "Major mode for editing Go, powered by tree-sitter."
   :group 'go
@@ -226,6 +223,9 @@ go-ts-mode
+(if (treesit-ready-p 'go)
+    (add-to-list 'auto-mode-alist '("\\.go\\'" . go-ts-mode)))
 (defun go-ts-mode--defun-name (node)
   "Return the defun name of NODE.
 Return nil if there is no name or if NODE is not a defun node."
@@ -346,9 +346,6 @@ go-mod-ts-mode--font-lock-settings
   "Tree-sitter font-lock settings for `go-mod-ts-mode'.")
-(add-to-list 'auto-mode-alist '("/go\\.mod\\'" . go-mod-ts-mode))
 (define-derived-mode go-mod-ts-mode prog-mode "Go Mod"
   "Major mode for editing go.mod files, powered by tree-sitter."
   :group 'go
@@ -376,6 +373,9 @@ go-mod-ts-mode
+(if (treesit-ready-p 'gomod)
+    (add-to-list 'auto-mode-alist '("/go\\.mod\\'" . go-mod-ts-mode)))
 (provide 'go-ts-mode)
 ;;; go-ts-mode.el ends here
diff --git a/lisp/progmodes/java-ts-mode.el b/lisp/progmodes/java-ts-mode.el
index d29fcd8..d909a36 100644
--- a/lisp/progmodes/java-ts-mode.el
+++ b/lisp/progmodes/java-ts-mode.el
@@ -331,6 +331,9 @@ java-ts-mode
                 ("Method" "\\`method_declaration\\'" nil nil)))
+(if (treesit-ready-p 'java)
+    (add-to-list 'auto-mode-alist '("\\.java\\'" . java-ts-mode)))
 (provide 'java-ts-mode)
 ;;; java-ts-mode.el ends here
diff --git a/lisp/progmodes/js.el b/lisp/progmodes/js.el
index 058c890..4428672 100644
--- a/lisp/progmodes/js.el
+++ b/lisp/progmodes/js.el
@@ -3840,7 +3840,10 @@ js-ts-mode
                    nil nil)))
-    (treesit-major-mode-setup)))
+    (treesit-major-mode-setup)
+    (add-to-list 'auto-mode-alist
+                 '("\\(\\.js[mx]\\|\\.har\\)\\'" . js-ts-mode))))
 (define-derived-mode js-json-mode js-mode "JSON"
diff --git a/lisp/progmodes/json-ts-mode.el b/lisp/progmodes/json-ts-mode.el
index fbcda22..f54d018 100644
--- a/lisp/progmodes/json-ts-mode.el
+++ b/lisp/progmodes/json-ts-mode.el
@@ -160,6 +160,10 @@ json-ts-mode
+(if (treesit-ready-p 'json)
+    (add-to-list 'auto-mode-alist
+                 '("\\.json\\'" . json-ts-mode)))
 (provide 'json-ts-mode)
 ;;; json-ts-mode.el ends here
diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el
index 21d16db..a869cdc 100644
--- a/lisp/progmodes/python.el
+++ b/lisp/progmodes/python.el
@@ -6713,7 +6713,10 @@ python-ts-mode
     (when python-indent-guess-indent-offset
-      (python-indent-guess-indent-offset))))
+      (python-indent-guess-indent-offset))
+    (add-to-list 'auto-mode-alist
+                 '("\\.py[iw]?\\'\\|python[0-9.]*" . python-ts-mode))))
 ;;; Completion predicates for M-x
 ;; Commands that only make sense when editing Python code
diff --git a/lisp/progmodes/ruby-ts-mode.el b/lisp/progmodes/ruby-ts-mode.el
index d68b579..f798f09 100644
--- a/lisp/progmodes/ruby-ts-mode.el
+++ b/lisp/progmodes/ruby-ts-mode.el
@@ -986,6 +986,10 @@ ruby-ts-mode
+(if (treesit-ready-p 'ruby)
+    (add-to-list 'auto-mode-alist
 . ruby-ts-mode)))
 (provide 'ruby-ts-mode)
 ;;; ruby-ts-mode.el ends here
diff --git a/lisp/progmodes/rust-ts-mode.el b/lisp/progmodes/rust-ts-mode.el
index 7536726..08590ae 100644
--- a/lisp/progmodes/rust-ts-mode.el
+++ b/lisp/progmodes/rust-ts-mode.el
@@ -276,9 +276,6 @@ rust-ts-mode--defun-name
       (treesit-node-child-by-field-name node "name") t))))
-(add-to-list 'auto-mode-alist '("\\.rs\\'" . rust-ts-mode))
 (define-derived-mode rust-ts-mode prog-mode "Rust"
   "Major mode for editing Rust, powered by tree-sitter."
   :group 'rust
@@ -322,6 +319,9 @@ rust-ts-mode
+(if (treesit-ready-p 'rust)
+    (add-to-list 'auto-mode-alist '("\\.rs\\'" . rust-ts-mode)))
 (provide 'rust-ts-mode)
 ;;; rust-ts-mode.el ends here
diff --git a/lisp/progmodes/typescript-ts-mode.el 
index 037d5c8..0b0bbe1 100644
--- a/lisp/progmodes/typescript-ts-mode.el
+++ b/lisp/progmodes/typescript-ts-mode.el
@@ -313,12 +313,6 @@ typescript-ts-mode--font-lock-settings
    '((escape_sequence) @font-lock-escape-face)))
-(add-to-list 'auto-mode-alist '("\\.ts\\'" . typescript-ts-mode))
-(add-to-list 'auto-mode-alist '("\\.tsx\\'" . tsx-ts-mode))
 (define-derived-mode typescript-ts-base-mode prog-mode "TypeScript"
   "Major mode for editing TypeScript."
   :group 'typescript
@@ -373,6 +367,9 @@ typescript-ts-mode
+(if (treesit-ready-p 'typescript)
+    (add-to-list 'auto-mode-alist '("\\.ts\\'" . typescript-ts-mode)))
 (define-derived-mode tsx-ts-mode typescript-ts-base-mode "TypeScript[TSX]"
   "Major mode for editing TypeScript."
@@ -408,6 +405,9 @@ tsx-ts-mode
+(if (treesit-ready-p 'tsx)
+    (add-to-list 'auto-mode-alist '("\\.tsx\\'" . tsx-ts-mode)))
 (provide 'typescript-ts-mode)
 ;;; typescript-ts-mode.el ends here
diff --git a/lisp/textmodes/css-mode.el b/lisp/textmodes/css-mode.el
index 8991610..a1d7d4b 100644
--- a/lisp/textmodes/css-mode.el
+++ b/lisp/textmodes/css-mode.el
@@ -1827,7 +1827,9 @@ css-ts-mode
     (setq-local treesit-simple-imenu-settings
                 `(( nil ,(rx bos (or "rule_set" "media_statement") eos)
                     nil nil)))
-    (treesit-major-mode-setup)))
+    (treesit-major-mode-setup)
+    (add-to-list 'auto-mode-alist '("\\.css\\'" . css-ts-mode))))
 (define-derived-mode css-mode css-base-mode "CSS"
diff --git a/lisp/textmodes/toml-ts-mode.el b/lisp/textmodes/toml-ts-mode.el
index 2430c5f..4165420 100644
--- a/lisp/textmodes/toml-ts-mode.el
+++ b/lisp/textmodes/toml-ts-mode.el
@@ -117,8 +117,6 @@ toml-ts-mode--defun-name
      (or (treesit-node-text (treesit-node-child node 1) t)
          "Root table"))))
-(add-to-list 'auto-mode-alist '("\\.toml\\'" . toml-ts-mode))
 (define-derived-mode toml-ts-mode text-mode "TOML"
   "Major mode for editing TOML, powered by tree-sitter."
@@ -155,6 +153,9 @@ toml-ts-mode
+(if (treesit-ready-p 'toml)
+    (add-to-list 'auto-mode-alist '("\\.toml\\'" . toml-ts-mode)))
 (provide 'toml-ts-mode)
 ;;; toml-ts-mode.el ends here
diff --git a/lisp/textmodes/yaml-ts-mode.el b/lisp/textmodes/yaml-ts-mode.el
index 8c61ee0..a25230e 100644
--- a/lisp/textmodes/yaml-ts-mode.el
+++ b/lisp/textmodes/yaml-ts-mode.el
@@ -118,9 +118,6 @@ yaml-ts-mode--font-lock-settings
   "Tree-sitter font-lock settings for `yaml-ts-mode'.")
-(add-to-list 'auto-mode-alist '("\\.ya?ml\\'" . yaml-ts-mode))
 (define-derived-mode yaml-ts-mode text-mode "YAML"
   "Major mode for editing YAML, powered by tree-sitter."
   :group 'yaml
@@ -146,6 +143,9 @@ yaml-ts-mode
+(if (treesit-ready-p 'yaml)
+    (add-to-list 'auto-mode-alist '("\\.ya?ml\\'" . yaml-ts-mode)))
 (provide 'yaml-ts-mode)
 ;;; yaml-ts-mode.el ends here

reply via email to

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