Hi,

one issue I regularly encounter with late_lua whatits is that they don't
allow the executed code to determine which node is currently executed.

Especially this means that attributes and properties can't be reliably
accessed and the code can't access related nodes next to the late_lua
node. This can be worked around by storing a closure in the data field
which captures a reference to the node, but this breaks in hard to debug
ways when the node is copied and the copied node has the same code but a
different identify.

Therefore it would be great if there would be a way to access the
currently evaluated late_lua whatsit node. I would have suggested
passing this as an argument to the executed function, but that could
break compatbility and it would appear odd due to LuaMetaTeX using the
second argument of lua functions is different ways. So the attached
suggested implemention instead adds a separate function which allows to
access this node: node.current_latelua (or node.direct.current_latelua)

Best,
Marcel
From d4c53f5bdf8e0bb716ef21847306ab24c8e67d3a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Marcel=20Fabian=20Kr=C3=BCger?= <[email protected]>
Date: Mon, 28 Feb 2022 11:59:15 +0100
Subject: [PATCH] Add node.current_latelua

---
 manual/luatex-nodes.tex                    | 12 ++++++++++
 source/texk/web2c/luatexdir/lua/lnodelib.c | 26 ++++++++++++++++++++++
 source/texk/web2c/luatexdir/lua/luastuff.c |  6 +++++
 3 files changed, 44 insertions(+)

diff --git a/manual/luatex-nodes.tex b/manual/luatex-nodes.tex
index 5a48b4c18..aaff0a38c 100644
--- a/manual/luatex-nodes.tex
+++ b/manual/luatex-nodes.tex
@@ -1421,6 +1421,18 @@ attribute list, not a copy thereof. Therefore, changing 
any of the attributes in
 the list will change these values for all nodes that have the current attribute
 list assigned to them.
 
+\subsection{\type {current_latelua}}
+
+\libindex{current_latelua}
+
+When called directly or indirectly from a \type{late_lua} whatsit, return the
+whatsit node which triggered this execution.
+
+\startfunctioncall
+<node> m =
+    node.current_latelua()
+\stopfunctioncall
+
 \subsection{\type {hpack}}
 
 \libindex {hpack}
diff --git a/source/texk/web2c/luatexdir/lua/lnodelib.c 
b/source/texk/web2c/luatexdir/lua/lnodelib.c
index 1ff36bc95..84bccee60 100644
--- a/source/texk/web2c/luatexdir/lua/lnodelib.c
+++ b/source/texk/web2c/luatexdir/lua/lnodelib.c
@@ -6541,6 +6541,30 @@ static int lua_nodelib_direct_currentattr(lua_State * L)
 }
 
 
+/* Defined in luastuff.c */
+extern halfword current_latelua;
+
+/* node.current_latelua(node m) */
+
+static int lua_nodelib_currentlatelua(lua_State * L)
+{
+    lua_nodelib_push_fast(L, current_latelua);
+    return 1;
+}
+
+
+/* node.direct.current_latelua() */
+
+static int lua_nodelib_direct_currentlatelua(lua_State * L)
+{
+    if (current_latelua)
+        lua_pushinteger(L, current_latelua);
+    else
+        lua_pushnil(L);
+    return 1;
+}
+
+
 /* node.direct.todirect */
 
 static int lua_nodelib_direct_todirect(lua_State * L)
@@ -8890,6 +8914,7 @@ static const struct luaL_Reg direct_nodelib_f[] = {
     {"copy_list", lua_nodelib_direct_copy_list},
     {"count", lua_nodelib_direct_count},
     {"current_attr", lua_nodelib_direct_currentattr},
+    {"current_latelua", lua_nodelib_direct_currentlatelua},
     {"dimensions", lua_nodelib_direct_dimensions},
     {"rangedimensions", lua_nodelib_direct_rangedimensions},
  /* {"do_ligature_n", lua_nodelib_direct_do_ligature_n}, */
@@ -9036,6 +9061,7 @@ static const struct luaL_Reg nodelib_f[] = {
     {"copy_list", lua_nodelib_copy_list},
     {"count", lua_nodelib_count},
     {"current_attr", lua_nodelib_currentattr},
+    {"current_latelua", lua_nodelib_currentlatelua},
     {"dimensions", lua_nodelib_dimensions},
     {"rangedimensions", lua_nodelib_rangedimensions},
  /* {"do_ligature_n", lua_nodelib_do_ligature_n}, */
diff --git a/source/texk/web2c/luatexdir/lua/luastuff.c 
b/source/texk/web2c/luatexdir/lua/luastuff.c
index fb05c60dd..469074d8c 100644
--- a/source/texk/web2c/luatexdir/lua/luastuff.c
+++ b/source/texk/web2c/luatexdir/lua/luastuff.c
@@ -32,6 +32,8 @@ lua_State *Luas = NULL;
 int luastate_bytes = 0;
 int lua_active = 0;
 
+halfword current_latelua = null;
+
 #ifdef LuajitTeX
 #define Luas_load(Luas,getS,ls,lua_id) \
     lua_load(Luas,getS,ls,lua_id);
@@ -594,8 +596,11 @@ void luacall_vf(int p, int f, int c)
 
 void late_lua(PDF pdf, halfword p)
 {
+    halfword saved;
     halfword t;
     (void) pdf;
+    saved = current_latelua;
+    current_latelua = p;
     t = late_lua_type(p);
     if (t == normal) {
         /*tex sets |def_ref| */
@@ -609,6 +614,7 @@ void late_lua(PDF pdf, halfword p)
     } else {
         /*tex Let's just ignore it, could be some user specific thing. */
     }
+    current_latelua = saved;
 }
 
 void luatokencall(int p, int nameptr)
-- 
2.35.1

_______________________________________________
dev-luatex mailing list
[email protected]
https://mailman.ntg.nl/mailman/listinfo/dev-luatex

Reply via email to