diff mbox series

[meta-oe,2/2] emacs: add PACKAGECONFIG for tree-sitter

Message ID 20261119102542.104648-2-f_l_k@t-online.de
State Under Review
Headers show
Series [meta-oe,1/2] libtreesitter: add recipe for 0.27.2 | expand

Commit Message

Markus Volk Nov. 19, 2026, 10:25 a.m. UTC
Emacs can use libtree-sitter for its tree-sitter based major modes
such as c-ts-mode and python-ts-mode. Add a PACKAGECONFIG for it,
disabled by default.

Emacs 30.2 still calls ts_language_version, which tree-sitter 0.26
removed. Backport the upstream fix that uses ts_language_abi_version
instead (Upstream-Status: Backport).

Tested on intel-corei7-64 with tree-sitter 0.27.2: emacs links
libtree-sitter, treesit-available-p returns t and C code parses with
tree-sitter-c 0.24.1.

AI-Generated: Uses Claude Code (Claude Opus 5.5)
Signed-off-by: Markus Volk <f_l_k@t-online.de>
---
 meta-oe/recipes-support/emacs/emacs_30.2.bb   |   6 +-
 ...t-Tree-sitter-version-0.26-and-later.patch | 118 ++++++++++++++++++
 2 files changed, 123 insertions(+), 1 deletion(-)
 create mode 100644 meta-oe/recipes-support/emacs/files/0001-Support-Tree-sitter-version-0.26-and-later.patch
diff mbox series

Patch

diff --git a/meta-oe/recipes-support/emacs/emacs_30.2.bb b/meta-oe/recipes-support/emacs/emacs_30.2.bb
index 95769aa94c..839036d987 100644
--- a/meta-oe/recipes-support/emacs/emacs_30.2.bb
+++ b/meta-oe/recipes-support/emacs/emacs_30.2.bb
@@ -4,7 +4,10 @@  HOMEPAGE = "https://www.gnu.org/software/emacs/"
 LICENSE = "GPL-3.0-only"
 LIC_FILES_CHKSUM = "file://COPYING;md5=1ebbd3e34237af26da5dc08a4e440464"
 
-SRC_URI = "${GNU_MIRROR}/emacs/${BP}.tar.xz"
+SRC_URI = " \
+    ${GNU_MIRROR}/emacs/${BP}.tar.xz \
+    file://0001-Support-Tree-sitter-version-0.26-and-later.patch \
+"
 UPSTREAM_CHECK_URI = "${GNU_MIRROR}/emacs/"
 
 SRC_URI:append:class-target = " \
@@ -21,6 +24,7 @@  PACKAGECONFIG[gnutls] = "--with-gnutls=yes,--with-gnutls=no,gnutls"
 PACKAGECONFIG[kerberos] = "--with-kerberos=yes,--with-kerberos=no,krb5"
 PACKAGECONFIG[libgmp] = "--with-libgmp=yes,--with-libgmp=no,gmp"
 PACKAGECONFIG[selinux] = "--with-selinux=yes,--with-selinux=no,libselinux"
+PACKAGECONFIG[tree-sitter] = "--with-tree-sitter=yes,--with-tree-sitter=no,libtreesitter"
 
 PACKAGECONFIG ??= "gnutls kerberos libgmp"
 
diff --git a/meta-oe/recipes-support/emacs/files/0001-Support-Tree-sitter-version-0.26-and-later.patch b/meta-oe/recipes-support/emacs/files/0001-Support-Tree-sitter-version-0.26-and-later.patch
new file mode 100644
index 0000000000..f771e56206
--- /dev/null
+++ b/meta-oe/recipes-support/emacs/files/0001-Support-Tree-sitter-version-0.26-and-later.patch
@@ -0,0 +1,118 @@ 
+From d587ce8c65a0e22ab0a63ef2873a3dfcfbeba166 Mon Sep 17 00:00:00 2001
+From: Eli Zaretskii <eliz@gnu.org>
+Date: Fri, 17 Oct 2025 14:15:41 +0300
+Subject: [PATCH] Support Tree-sitter version 0.26 and later
+
+* src/treesit.c (init_treesit_functions)
+[TREE_SITTER_LANGUAGE_VERSION >= 15]: Define prototype for, and
+load 'ts_language_abi_version' instead of the deprecated (and
+removed in tree-sitter 0.26) 'ts_language_version'.
+(ts_language_abi_version) [TREE_SITTER_LANGUAGE_VERSION >= 15]:
+Define on WINDOWSNT, instead of 'ts_language_version'.
+(treesit_language_abi_version): New compatibility function.
+(treesit_load_language, Ftreesit_language_abi_version): Use
+'treesit_language_abi_version' instead of 'ts_language_version'.
+(Bug#79627)
+
+Upstream-Status: Backport [https://git.savannah.gnu.org/cgit/emacs.git/commit/?id=d587ce8c65a0e22ab0a63ef2873a3dfcfbeba166]
+
+Signed-off-by: Markus Volk <f_l_k@t-online.de>
+
+The hunk for treesit_load_language was applied by hand, the code around
+it differs in 30.2.
+---
+ src/treesit.c | 36 ++++++++++++++++++++++++++++++++++--
+ 1 file changed, 34 insertions(+), 2 deletions(-)
+
+diff --git a/src/treesit.c b/src/treesit.c
+--- a/src/treesit.c
++++ b/src/treesit.c
+@@ -34,7 +34,11 @@
+ # include "w32common.h"
+ 
+ /* In alphabetical order.  */
++#if TREE_SITTER_LANGUAGE_VERSION >= 15
++#undef ts_language_abi_version
++#else
+ #undef ts_language_version
++#endif
+ #undef ts_node_child
+ #undef ts_node_child_by_field_name
+ #undef ts_node_child_count
+@@ -89,7 +93,11 @@
+ #undef ts_tree_get_changed_ranges
+ #undef ts_tree_root_node
+ 
++#if TREE_SITTER_LANGUAGE_VERSION >= 15
++DEF_DLL_FN (uint32_t, ts_language_abi_version, (const TSLanguage *));
++#else
+ DEF_DLL_FN (uint32_t, ts_language_version, (const TSLanguage *));
++#endif
+ DEF_DLL_FN (TSNode, ts_node_child, (TSNode, uint32_t));
+ DEF_DLL_FN (TSNode, ts_node_child_by_field_name,
+ 	    (TSNode, const char *, uint32_t));
+@@ -166,7 +174,11 @@
+   if (!library)
+     return false;
+ 
++#if TREE_SITTER_LANGUAGE_VERSION >= 15
++  LOAD_DLL_FN (library, ts_language_abi_version);
++#else
+   LOAD_DLL_FN (library, ts_language_version);
++#endif
+   LOAD_DLL_FN (library, ts_node_child);
+   LOAD_DLL_FN (library, ts_node_child_by_field_name);
+   LOAD_DLL_FN (library, ts_node_child_count);
+@@ -224,7 +236,11 @@
+   return true;
+ }
+ 
++#if TREE_SITTER_LANGUAGE_VERSION >= 15
++#define ts_language_abi_version fn_ts_language_abi_version
++#else
+ #define ts_language_version fn_ts_language_version
++#endif
+ #define ts_node_child fn_ts_node_child
+ #define ts_node_child_by_field_name fn_ts_node_child_by_field_name
+ #define ts_node_child_count fn_ts_node_child_count
+@@ -632,6 +648,22 @@
+     }
+ }
+ 
++/* This function is a compatibility shim.  Tree-sitter 0.25 introduced
++   ts_language_abi_version as a replacement for ts_language_version, and
++   tree-sitter 0.26 removed ts_language_version.  Here we use the fact
++   that 0.25 bumped TREE_SITTER_LANGUAGE_VERSION to 15, to use the new
++   function instead of the old one, when Emacs is compiled against
++   tree-sitter version 0.25 or newer.  */
++static uint32_t
++treesit_language_abi_version (const TSLanguage *ts_lang)
++{
++#if TREE_SITTER_LANGUAGE_VERSION >= 15
++  return ts_language_abi_version (ts_lang);
++#else
++  return ts_language_version (ts_lang);
++#endif
++}
++
+ /* Load the dynamic library of LANGUAGE_SYMBOL and return the pointer
+    to the language definition.
+ 
+@@ -746,7 +778,7 @@
+     {
+       *signal_symbol = Qtreesit_load_language_error;
+       *signal_data = list2 (Qversion_mismatch,
+-			    make_fixnum (ts_language_version (lang)));
++			    make_fixnum (treesit_language_abi_version (lang)));
+       return NULL;
+     }
+   return lang;
+@@ -817,7 +849,7 @@
+ 						       &signal_data);
+       if (ts_language == NULL)
+ 	return Qnil;
+-      uint32_t version =  ts_language_version (ts_language);
++      uint32_t version =  treesit_language_abi_version (ts_language);
+       return make_fixnum((ptrdiff_t) version);
+     }
+ }