[latex3-commits] [git/LaTeX3-latex3-latex2e] shared_callback: Better interface for shared engine callbacks (8b3a6e28)

Marcel Fabian Krüger tex at 2krueger.de
Mon May 16 20:27:08 CEST 2022


Repository : https://github.com/latex3/latex2e
On branch  : shared_callback
Link       : https://github.com/latex3/latex2e/commit/8b3a6e283a13bdea961948d70ca5b30bdd9be20a

>---------------------------------------------------------------

commit 8b3a6e283a13bdea961948d70ca5b30bdd9be20a
Author: Marcel Fabian Krüger <tex at 2krueger.de>
Date:   Mon May 16 20:16:03 2022 +0200

    Better interface for shared engine callbacks


>---------------------------------------------------------------

8b3a6e283a13bdea961948d70ca5b30bdd9be20a
 base/ltluatex.dtx | 59 ++++++++++++++++++++++++++++++++++++++++++++-----------
 1 file changed, 47 insertions(+), 12 deletions(-)

diff --git a/base/ltluatex.dtx b/base/ltluatex.dtx
index 18623fd6..bd6b6cfc 100644
--- a/base/ltluatex.dtx
+++ b/base/ltluatex.dtx
@@ -1398,9 +1398,7 @@ local callbacktypes = callbacktypes or {
   ligaturing             = simple,
   kerning                = simple,
   insert_local_par       = simple,
-  pre_mlist_to_hlist_filter = list,
-  mlist_to_hlist         = exclusive,
-  post_mlist_to_hlist_filter = reverselist,
+% mlist_to_hlist         = exclusive,
   new_graf               = exclusive,
 %    \end{macrocode}
 % Section 8.5: information reporting callbacks.
@@ -1461,6 +1459,29 @@ local callbacktypes = callbacktypes or {
 luatexbase.callbacktypes=callbacktypes
 %    \end{macrocode}
 %
+% Sometimes multiple callbacks correspond to a single underlying engine level callback.
+% Then the engine level callback should be registered as long as at least one of these
+% callbacks is in use. This is implemented though a shared table which counts how many
+% of the involved callbacks are currently in use. The enging level callback is registered
+% iff this count is not 0.
+%
+% We add |mlist_to_hlist| directly to the list to demonstrate this, but the handler gets
+% added later when it is actually defined.
+%
+% All callbacks in this list are treated as user defined callbacks.
+%
+%    \begin{macrocode}
+local shared_callbacks = {
+  mlist_to_hlist = {
+    callback = "mlist_to_hlist",
+    count = 0,
+    handler = nil,
+  },
+}
+shared_callbacks.pre_mlist_to_hlist_filter = shared_callbacks.mlist_to_hlist
+shared_callbacks.post_mlist_to_hlist_filter = shared_callbacks.mlist_to_hlist
+%    \end{macrocode}
+%
 % \begin{macro}{callback.register}
 % \changes{v1.0a}{2015/09/24}{Function modified}
 %   Save the original function for registering callbacks and prevent the
@@ -1639,11 +1660,7 @@ local defaults = {
 % If a default function is not required, it may be declared as |false|.
 % First we need a list of user callbacks.
 %    \begin{macrocode}
-local user_callbacks_defaults = {
-  pre_mlist_to_hlist_filter = list_handler_default,
-  mlist_to_hlist = node.mlist_to_hlist,
-  post_mlist_to_hlist_filter = list_handler_default,
-}
+local user_callbacks_defaults = {}
 %    \end{macrocode}
 %
 % \begin{macro}{create_callback}
@@ -1738,9 +1755,18 @@ local function add_to_callback(name, func, description)
     l = { }
     callbacklist[name] = l
 %    \end{macrocode}
+% Handle count for shared engine callbacks.
+%    \begin{macrocode}
+    local shared = shared_callbacks[name]
+    if shared then
+      shared.count = shared.count + 1
+      if shared.count == 1 then
+        callback_register(shared.callback, shared.handler)
+      end
+%    \end{macrocode}
 % If it is not a user defined callback use the primitive callback register.
 %    \begin{macrocode}
-    if user_callbacks_defaults[name] == nil then
+    elseif user_callbacks_defaults[name] == nil then
       callback_register(name, handlers[callbacktypes[name]](name))
     end
   end
@@ -1823,7 +1849,13 @@ local function remove_from_callback(name, description)
   )
   if #l == 0 then
     callbacklist[name] = nil
-    if user_callbacks_defaults[name] == nil then
+    local shared = shared_callbacks[name]
+    if shared then
+      shared.count = shared.count - 1
+      if shared.count == 0 then
+        callback_register(shared.callback, nil)
+      end
+    elseif user_callbacks_defaults[name] == nil then
       callback_register(name, nil)
     end
   end
@@ -1920,7 +1952,10 @@ luatexbase.uninstall = uninstall
 %   To emulate these callbacks, the ``real'' |mlist_to_hlist| is replaced by a
 %   wrapper calling the wrappers before and after.
 %    \begin{macrocode}
-callback_register("mlist_to_hlist", function(head, display_type, need_penalties)
+create_callback('pre_mlist_to_hlist_filter', 'list')
+create_callback('mlist_to_hlist', 'exclusive', node.mlist_to_hlist)
+create_callback('post_mlist_to_hlist_filter', 'list')
+function shared_callbacks.mlist_to_hlist.handler(head, display_type, need_penalties)
   local current = call_callback("pre_mlist_to_hlist_filter", head, display_type, need_penalties)
   if current == false then
     flush_list(head)
@@ -1933,7 +1968,7 @@ callback_register("mlist_to_hlist", function(head, display_type, need_penalties)
     return nil
   end
   return post
-end)
+end
 %    \end{macrocode}
 % \end{macro}
 % \endgroup





More information about the latex3-commits mailing list.