1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
|
;;; treesit-x.el --- tree-sitter extensions -*- lexical-binding: t; -*-
;; Copyright (C) 2025 Free Software Foundation, Inc.
;; Maintainer: emacs-devel@gnu.org
;; Keywords: treesit, tree-sitter, languages, generic, font-lock
;; Package: emacs
;; This file is part of GNU Emacs.
;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;; This file contains a collection of treesit generic modes.
;;
;; INSTALLATION:
;;
;; Add this line to your init file:
;;
;; (require 'treesit-x)
;;
;; Then visiting a file that has a tree-sitter grammar will ask you
;; for confirmation before installing that grammar. Afterwards it
;; will highlight text according to syntax defined by the grammar.
;;; Code:
(require 'treesit)
(treesit-declare-unavailable-functions)
;;; Define treesit generic mode
;;;###autoload
(defmacro define-treesit-generic-mode (mode &optional docstring &rest body)
"Create a new treesit generic mode MODE.
A \"treesit\" mode is a simple major mode with basic support for
Font Lock mode, but otherwise does not have any special keystrokes
or functionality available.
MODE is the name of the command for the treesit generic mode; don't
quote it. The optional DOCSTRING is the documentation for the mode
command. If you do not supply it, `define-treesit-generic-mode'
uses a default documentation string instead.
KEYWORD-ARGS are optional arguments in the form of pairs of keyword
and value. The following keyword arguments are currently supported:
:lang is a language symbol of the corresponding tree-sitter grammar.
:source is either a string for the URL or a list in the same format
as for elements in `treesit-language-source-alist', i.e.
(URL REVISION SOURCE-DIR CC C++ COMMIT).
:auto-mode is a regular expression or a list of regular expressions
to add to `auto-mode-alist'. These regular expressions are added
when Emacs runs the macro expansion.
:parent is the name of the command for the parent mode.
:name is a string that will appear in the mode line.
BODY are forms to execute just before running the
hooks for the new mode. Do not use `interactive' here.
These forms do some additional setup. The mode command calls
these functions just before it runs `treesit-major-mode-setup'
and the mode hook `MODE-hook'.
See at the bottom of the file treesit-x.el for some examples
of `define-treesit-generic-mode'.
\(fn MODE [DOCSTRING] [KEYWORD-ARGS...] &rest BODY)"
(declare (debug (&define name [&optional stringp]
[&rest keywordp sexp] def-body))
(doc-string 2)
(indent defun))
(when (and docstring (not (stringp docstring)))
;; Some trickiness, since what appears to be the docstring may really be
;; the first element of the body.
(push docstring body)
(setq docstring nil))
(let* ((mode-name (symbol-name mode))
(pretty-name (capitalize (replace-regexp-in-string
"-mode\\'" "" mode-name)))
lang source auto-mode parent name)
;; Process the keyword args.
(while (keywordp (car body))
(pcase (pop body)
(:lang (setq lang (pop body)))
(:source (setq source (pop body)))
(:auto-mode (setq auto-mode (pop body)))
(:parent (setq parent (pop body)))
(:name (setq name (pop body)))
(_ (pop body))))
(when (stringp source)
(setq source (list 'quote (ensure-list source))))
(when (stringp auto-mode)
(setq auto-mode (list 'quote (ensure-list auto-mode))))
`(progn
;; Add lang and source to source-alist.
(add-to-list 'treesit-language-source-alist (cons ,lang ,source))
;; Add it to auto-mode-alist
(dolist (re ,auto-mode)
(add-to-list 'auto-mode-alist (cons re ',mode)))
(define-derived-mode ,mode
,(or parent 'fundamental-mode)
,(or name pretty-name)
,(or docstring
(concat (or name pretty-name) " mode.\n"
"This a tree-sitter mode defined with `define-treesit-generic-mode'."))
(treesit-generic-mode-setup ,lang ,source)
,@body
(treesit-major-mode-setup)))))
;;;###autoload
(defun treesit-generic-mode-setup (lang source)
"Go into the treesit generic mode MODE."
(unless (treesit-ready-p lang t)
(when (y-or-n-p (format "Install grammar for %s?" lang))
(apply
#'treesit--install-language-grammar-1
(locate-user-emacs-file "tree-sitter")
lang source)))
(when (treesit-ready-p lang)
(setq treesit-primary-parser (treesit-parser-create lang))
(when-let* ((query (treesit-generic-mode-font-lock-query lang)))
(setq-local treesit-font-lock-settings
(treesit-font-lock-rules
:language lang
:feature 'highlights
query))
(setq-local treesit-font-lock-feature-list '((highlights))))))
;;; Generic font-lock handling
(defvar treesit-generic-mode-font-lock-map
'(
("@boolean" . "@font-lock-constant-face")
("@comment" . "@font-lock-comment-face")
("@constant" . "@font-lock-constant-face")
("@error" . "@font-lock-warning-face")
("@escape" . "@font-lock-escape-face")
("@keyword" . "@font-lock-keyword-face")
("@operator" . "@font-lock-operator-face")
("@property" . "@font-lock-property-use-face")
("@punctuation.bracket" . "@font-lock-bracket-face")
("@punctuation.delimiter" . "@font-lock-delimiter-face")
("@punctuation.special" . "@font-lock-misc-punctuation-face")
("@string.regexp" . "@font-lock-regexp-face")
("@string.special" . "@font-lock-string-face")
("@string" . "@font-lock-string-face")
("@variable.builtin" . "@font-lock-builtin-face")
("@variable.parameter" . "@font-lock-variable-name-face")
)
"A mapping from default capture names to font-lock faces.")
(defun treesit-generic-mode-font-lock-query (lang)
"Find the file highlights.scm and return its queries as a string."
(let* ((file (expand-file-name
(format "queries/%s/highlights.scm" lang)
(locate-user-emacs-file "tree-sitter")))
(query (when (file-exists-p file)
(with-temp-buffer
(insert-file-contents file)
(buffer-substring-no-properties (point-min) (point-max))))))
(when query
(setq query (replace-regexp-in-string "(#set! [^)]+)" "" query nil nil))
(pcase-dolist (`(,from . ,to) treesit-generic-mode-font-lock-map)
(setq query (replace-regexp-in-string from to query nil t)))
query)))
;;; Default treesit generic modes
(define-treesit-generic-mode gitattributes-generic-ts-mode
"Tree-sitter generic mode for .gitattributes files."
:lang 'gitattributes
:source "https://github.com/tree-sitter-grammars/tree-sitter-gitattributes"
:auto-mode "gitattributes\\'"
:name "Git-Attributes"
(setq-local comment-start "# ")
(setq-local comment-end ""))
(provide 'treesit-x)
;;; treesit-x.el ends here
|