Function: define-treesit-generic-mode
define-treesit-generic-mode is an autoloaded macro defined in
treesit-x.el.gz.
Signature
(define-treesit-generic-mode MODE [DOCSTRING] [KEYWORD-ARGS...] &rest BODY)
Documentation
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.
Probably introduced at or before Emacs version 31.1.
Source Code
;; Defined in /usr/src/emacs/lisp/treesit-x.el.gz
;;; 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 (list source :copy-queries t))))
(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) t)
;; Add it to auto-mode-alist
(dolist (re ,auto-mode)
(add-to-list 'auto-mode-alist (cons re ',mode)))
(define-derived-mode ,mode
,(or (if (eq (car-safe parent) 'quote) (cadr parent) 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)
,@body
(treesit-major-mode-setup)))))