Subject: Re: [bug#49946] [PATCH 00/31] Tree-sitter, node-gyp addon support and emacs-tree-sitter

From: Andrew Tropin <[email-masked]>
Date: Fri, 10 Feb 2023 13:21:18 +0400

On 2021-08-08 23:29, Pierre Langlois wrote:

> 4. Finally, emacs support!
>
>   emacs-tree-sitter can be used for syntax highlighting, replacing
>   font-lock.  The tree-sitter runtime library is built as a Rust crate
>   and exposed to emacs using a module thanks to the rust-emacs crate.
>
>   Then emacs-tree-sitter-core provides just the core APIs as exposed by
>   the rust module, then emacs-tree-sitter integrates it with emacs'
>   syntax highlighting (and probably other things).
>
>   Finally, we bundle together the various language grammars into a
>   single emacs-tree-sitter-langs package so that they can get loaded
>   along with their respective major modes.  Note that there are grammars
>   left to support which I've not yet managed to package, but what we
>   have already is a good start I think :-). So far I've tested C++,
>   Rust, Python and JavaScript and JSON.
>
>   * gnu: Add [email-masked].
>   * gnu: Add rust-tree-sitter.
>   * gnu: rust-emacs-module: Update to 0.16.
>   * gnu: rust-emacs-macros: Update to 0.17.
>   * gnu: rust-emacs: Update to 0.17.
>   * gnu: Add emacs-tree-sitter-core.
>   * gnu: Add emacs-tree-sitter.
>   * gnu: Add emacs-tree-sitter-langs.

Hi Pierre!

Implemented a loading of tree-sitter grammars in Emacs using guix
native-search-paths and built-in treesit package.  I did it for emacs
package, but it will work only with Emacs 29 and greater (right now it's
emacs-next and similiar packages), until emacs package updated to
version 29 it won't take any effect on it except providing one more
environment variable in guix profile.

I tested it with emacs-next-pgtk, the patch is attached below, please
let me know what do you think!

From b5ecd4e3734e9dd0bc76ebe95cab9c43aa85a3fe Mon Sep 17 00:00:00 2001
From: Andrew Tropin <[email-masked]>
Date: Fri, 10 Feb 2023 12:32:12 +0400
Subject: [PATCH] gnu: emacs: Add TREE_SITTER_GRAMMAR_PATH support.

gnu/packages/emacs.scm (emacs)[native-search-paths]: Add a search-path for
tree-sitter grammars.
gnu/packages/aux-files/emacs/guix-emacs.el: Add directories from
TREE_SITTER_GRAMMAR_PATH to treesit-extra-load-path.
---
 gnu/packages/aux-files/emacs/guix-emacs.el |  7 +++++++
 gnu/packages/emacs.scm                     | 10 +++++++++-
 2 files changed, 16 insertions(+), 1 deletion(-)

diff --git a/gnu/packages/aux-files/emacs/guix-emacs.el b/gnu/packages/aux-files/emacs/guix-emacs.el
index 56dbcb8d67..708093267d 100644
--- a/gnu/packages/aux-files/emacs/guix-emacs.el
+++ b/gnu/packages/aux-files/emacs/guix-emacs.el
@@ -76,6 +76,13 @@ The files in the list do not have extensions (.el, .elc)."
                    (when (file-directory-p pkg-dir)
                      (package-load-descriptor pkg-dir)))))))))))
 
+;; If emacs built with tree-sitter, read the value of the environment variable
+;; to make tree-sitter grammars available in emacs out-of-the-box.
+(with-eval-after-load 'treesit
+  (when-let ((grammar-path (getenv "TREE_SITTER_GRAMMAR_PATH")))
+    (mapcar (lambda (x) (add-to-list 'treesit-extra-load-path x))
+            (split-string grammar-path ":"))))
+
 (provide 'guix-emacs)
 
 ;;; guix-emacs.el ends here
diff --git a/gnu/packages/emacs.scm b/gnu/packages/emacs.scm
index 4ce41deb88..a3cc9d2e22 100644
--- a/gnu/packages/emacs.scm
+++ b/gnu/packages/emacs.scm
@@ -367,7 +367,15 @@ (define* (emacs-byte-compile-directory dir)
             (files '("lib/emacs/native-site-lisp")))
            (search-path-specification
             (variable "INFOPATH")
-            (files '("share/info")))))
+            (files '("share/info")))
+           ;; tree-sitter support is not yet available in emacs 28, but this
+           ;; search path won't harm and also will be benifitial for
+           ;; emacs-next and other emacs-* packages, which have tree-sitter
+           ;; support enabled.  Please, remove this comment, when emacs
+           ;; package is updated to 29.
+           (search-path-specification
+            (variable "TREE_SITTER_GRAMMAR_PATH")
+            (files '("lib/tree-sitter")))))
 
     (home-page "https://www.gnu.org/software/emacs/")
     (synopsis "The extensible, customizable, self-documenting text editor")
-- 
2.39.1

-- 
Best regards,
Andrew Tropin