Subject: [PATCH v8 5/7] gnu: Add emacs-tree-sitter-core.

From: Pierre Langlois <[email-masked]>
Date: Fri, 17 Feb 2023 12:37:20 +0000

* gnu/packages/tree-sitter.scm (tree-sitter-emacs-module): New local variable.
(emacs-tree-sitter-core): New variable.
---
 gnu/packages/tree-sitter.scm | 85 ++++++++++++++++++++++++++++++++++++
 1 file changed, 85 insertions(+)

diff --git a/gnu/packages/tree-sitter.scm b/gnu/packages/tree-sitter.scm
index bb9972cb00..12f2d108d9 100644
--- a/gnu/packages/tree-sitter.scm
+++ b/gnu/packages/tree-sitter.scm
@@ -29,6 +29,7 @@ (define-module (gnu packages tree-sitter)
   #:use-module (gnu packages icu4c)
   #:use-module (gnu packages node)
   #:use-module (guix build-system cargo)
+  #:use-module (guix build-system emacs)
   #:use-module (guix build-system gnu)
   #:use-module (guix build-system tree-sitter)
   #:use-module (guix download)
@@ -538,3 +539,87 @@ (define-public tree-sitter-racket
      (git-version "0.1.0" revision commit)
      #:repository-url "https://github.com/6cdh/tree-sitter-racket"
      #:commit commit)))
+
+;; Local package definition solely for building the native emacs module
+;; written in Rust.
+(define tree-sitter-emacs-module
+  (package
+    (name "tree-sitter-emacs-module")
+    (version "0.18.0")
+    (source (origin
+              (method git-fetch)
+              (uri (git-reference
+                    (url "https://github.com/emacs-tree-sitter/elisp-tree-sitter")
+                    (commit version)))
+              (file-name (git-file-name name version))
+              (sha256
+               (base32
+                "1sdvz827v436qijs6xafakkfw2d16bvp8frymd818rppjc7a9dif"))))
+    (build-system cargo-build-system)
+    (inputs
+     (list tree-sitter))
+    (arguments
+     (list
+      #:cargo-inputs
+      `(("rust-anyhow" ,rust-anyhow-1)
+        ("rust-emacs" ,rust-emacs-0.18)
+        ("rust-libloading" ,rust-libloading-0.7)
+        ("rust-once-cell" ,rust-once-cell-1)
+        ("rust-tree-sitter" ,rust-tree-sitter-for-emacs))
+      #:phases
+      #~(modify-phases %standard-phases
+          (add-after 'unpack 'chdir
+            (lambda _ (chdir "core")))
+          (add-after 'chdir 'delete-cargo.lock
+            (lambda _ (delete-file "Cargo.lock")))
+          (add-after 'delete-cargo.lock 'do-not-fetch-from-github
+            (lambda _
+              (substitute* "Cargo.toml"
+                (("\\[patch.*") "")
+                (("git = .*") ""))))
+          (replace 'install
+            (lambda _
+              (let ((lib (string-append #$output "/lib")))
+                (mkdir-p lib)
+                (copy-file "target/release/libtsc_dyn.so"
+                           (string-append lib "/tsc-dyn.so"))))))))
+    (home-page #f)
+    (synopsis #f)
+    (description #f)
+    (license license:expat)))
+
+(define-public emacs-tree-sitter-core
+  (package
+    (name "emacs-tree-sitter-core")
+    (version (package-version tree-sitter-emacs-module))
+    (source (package-source tree-sitter-emacs-module))
+    (build-system emacs-build-system)
+    (native-inputs
+     (list tree-sitter-emacs-module))
+    (arguments
+     (list
+      #:phases
+      #~(modify-phases %standard-phases
+          (add-after 'unpack 'chdir
+            (lambda _ (chdir "core")))
+          (add-before 'install 'find-tsc-dyn
+            (lambda* (#:key inputs #:allow-other-keys)
+              (let ((module (search-input-file inputs "/lib/tsc-dyn.so")))
+                ;; Install the tsc-dyn module in site-lisp and the current
+                ;; directory for test to pass.
+                (install-file module (elpa-directory #$output))
+                (copy-file module "tsc-dyn.so")
+                ;; We replace the tsc-dyn-get.el file with an empty stub to
+                ;; prevent the code from downloading the module.
+                (call-with-output-file "tsc-dyn-get.el"
+                  (lambda (port)
+                    (for-each
+                     (lambda (sexp) (write sexp port))
+                     '((defun tsc-dyn-get-ensure (requested)
+                         nil)
+                       (provide 'tsc-dyn-get)))))))))))
+    (home-page "https://emacs-tree-sitter.github.io")
+    (synopsis "Tree-sitter bindings for Emacs Lisp, core library")
+    (description "This package provides core APIs of the Emacs binding for
+Tree-sitter, an incremental parsing system.")
+    (license license:expat)))
-- 
2.39.1