]> git.eshelyaron.com Git - emacs.git/commitdiff
* lisp/treesit.el (treesit-auto-install): New defcustom.
authorJuri Linkov <juri@linkov.net>
Fri, 18 Apr 2025 16:17:39 +0000 (19:17 +0300)
committerEshel Yaron <me@eshelyaron.com>
Mon, 21 Apr 2025 20:35:40 +0000 (22:35 +0200)
(treesit-ensure-installed): New function.

(cherry picked from commit c9b6be7d27299093d16d592f1ffe0a731136beb2)

lisp/treesit.el

index 841dd82f1acb0e9f4d711e7b5922920799f9ed39..a0fef8d083a41974ea277c719a04fe663d533b6c 100644 (file)
@@ -5268,6 +5268,29 @@ If anything goes wrong, this function signals an `treesit-error'."
       (dolist (file (directory-files query-dir t "\\.scm\\'" t))
         (copy-file file (expand-file-name (file-name-nondirectory file) dest-dir) t)))))
 
+(defcustom treesit-auto-install 'ask
+  "Automatic installation of a language grammar.
+If `ask', prompt for a confirmation before installing the required
+language grammar.  If `always', silently install the grammar.
+If nil or `never' or anything else, don't install the grammar
+even while visiting a file in the mode that requires such grammar."
+  :type '(choice (const :tag "Never try to install grammar" never)
+                 (const :tag "Always install grammar automatically" always)
+                 (const :tag "Ask whether to install grammar" ask))
+  :version "31.1")
+
+(defun treesit-ensure-installed (lang)
+  "Ensure that the grammar for the language LANG is installed.
+The option `treesit-auto-install' defines whether to install it
+automatically, or ask for a confirmation."
+  (or (treesit-ready-p lang t)
+      (when (or (eq treesit-auto-install 'always)
+                (and (eq treesit-auto-install 'ask)
+                     (y-or-n-p (format "Install grammar for `%s'?" lang))))
+        (treesit-install-language-grammar lang)
+        ;; Check that the grammar was installed successfully
+        (treesit-ready-p lang))))
+
 ;;; Shortdocs
 
 (defun treesit--generate-shortdoc-examples ()