1
Fork 0
mirror of git://git.sv.gnu.org/emacs.git synced 2025-12-06 06:20:55 -08:00

Use treesit-extra-load-path for more values of treesit-auto-install-grammar

* lisp/treesit.el (treesit-auto-install-grammar): Use the first
writable directory from 'treesit-extra-load-path' for values
'always', 'ask', 'ask-dir' (bug#79862).

* src/treesit.c (syms_of_treesit): Extend docstring.
This commit is contained in:
Juri Linkov 2025-11-30 20:14:19 +02:00
parent 16b19ceb28
commit 9174bc811a
3 changed files with 26 additions and 19 deletions

View file

@ -927,7 +927,7 @@ available when such modes are turned on.
*** 'treesit-extra-load-path' now is a customizable user option. *** 'treesit-extra-load-path' now is a customizable user option.
The first directory in the list is used as the default directory The first directory in the list is used as the default directory
to install the language grammar when 'treesit-auto-install-grammar' to install the language grammar when 'treesit-auto-install-grammar'
is 'ask' or 'ask-dir'. is 'ask', 'ask-dir' or 'always'.
*** 'treesit-language-source-alist' supports keywords. *** 'treesit-language-source-alist' supports keywords.
The language and URL are mandatory, but remaining data can use keywords: The language and URL are mandatory, but remaining data can use keywords:

View file

@ -5462,15 +5462,24 @@ The copied query files are queries/highlights.scm."
"Whether to install tree-sitter language grammar libraries when needed. "Whether to install tree-sitter language grammar libraries when needed.
This controls whether Emacs will install missing grammar libraries This controls whether Emacs will install missing grammar libraries
when they are needed by some tree-sitter based mode. when they are needed by some tree-sitter based mode.
If `always', install the grammar library without asking. If `always', install the grammar library without asking.
If `ask', ask for confirmation before installing the required grammar library. If `ask', ask for confirmation before installing the grammar library.
If `ask-dir', ask for confirmation and also for a directory name where If `ask-dir', ask for confirmation and also for a directory name where
to install the grammar library. The directory name is also asked when to install the grammar library. The selected directory name is then
the value is `ask' and `treesit-extra-load-path' is customized to a list added to the list in `treesit-extra-load-path', but not saved, so
of directories. it's used only within the current session. It's advisable to
If nil or `never' or anything else, don't install the grammar library customize and save `treesit-extra-load-path' manually.
even while visiting a file in the mode that requires such grammar; this
might display a warning and/or fail to turn on the mode." The default directory for the above three values is the first writeable
directory from the list in `treesit-extra-load-path'. If it's nil, then
the grammar is installed to the standard location, the \"tree-sitter\"
directory under `user-emacs-directory'.
If the value of this variable is nil or `never' or anything else, don't
install the grammar library even while visiting a file in the mode that
requires such grammar; this might display a warning and/or fail to turn
on the mode."
:type '(choice (const :tag "Never install grammar libraries" never) :type '(choice (const :tag "Never install grammar libraries" never)
(const :tag "Always automatically install grammar libraries" (const :tag "Always automatically install grammar libraries"
always) always)
@ -5486,23 +5495,20 @@ The option `treesit-auto-install-grammar' defines whether to install
the grammar library if it's unavailable." the grammar library if it's unavailable."
(when (treesit-available-p) (when (treesit-available-p)
(or (treesit-ready-p lang t) (or (treesit-ready-p lang t)
(let (out-dir) (let ((out-dir (or (seq-find #'file-writable-p
treesit-extra-load-path)
(locate-user-emacs-file "tree-sitter"))))
(when (or (eq treesit-auto-install-grammar 'always) (when (or (eq treesit-auto-install-grammar 'always)
(and (memq treesit-auto-install-grammar '(ask ask-dir)) (and (memq treesit-auto-install-grammar '(ask ask-dir))
(y-or-n-p (format "\ (y-or-n-p (format "\
Tree-sitter grammar for `%s' is missing; install it?" lang)) Tree-sitter grammar for `%s' is missing; install it?" lang))
(or (and (eq treesit-auto-install-grammar 'ask) (or (eq treesit-auto-install-grammar 'ask)
;; Still ask dir for customized path (progn
(null treesit-extra-load-path))
(let ((default-out-dir
(or (seq-find #'file-writable-p
treesit-extra-load-path)
(locate-user-emacs-file "tree-sitter"))))
(setq out-dir (read-directory-name (setq out-dir (read-directory-name
(format-prompt "\ (format-prompt "\
Install grammar for `%s' to" nil lang) Install grammar for `%s' to" nil lang)
default-out-dir out-dir
treesit-extra-load-path t)) treesit-extra-load-path))
(add-to-list 'treesit-extra-load-path out-dir) (add-to-list 'treesit-extra-load-path out-dir)
t)))) t))))
(treesit-install-language-grammar lang out-dir) (treesit-install-language-grammar lang out-dir)

View file

@ -5275,7 +5275,8 @@ Emacs first looks in the directories mentioned in this variable,
then in the `tree-sitter' subdirectory of `user-emacs-directory', and then in the `tree-sitter' subdirectory of `user-emacs-directory', and
then in the system default locations for dynamic libraries, in that order. then in the system default locations for dynamic libraries, in that order.
The first writeable directory in the list is special: it's used as the The first writeable directory in the list is special: it's used as the
default directory when automatically installing the language grammar. */); default directory when automatically installing the language grammar
using `treesit-ensure-installed'. */);
Vtreesit_extra_load_path = Qnil; Vtreesit_extra_load_path = Qnil;
DEFVAR_LISP ("treesit-thing-settings", DEFVAR_LISP ("treesit-thing-settings",