[latex3-commits] [git/LaTeX3-latex3-latex2e] callbacks: reverselist callback type (051934cd)

Marcel Fabian Krüger tex at 2krueger.de
Fri Jan 10 16:50:48 CET 2020


Repository : https://github.com/latex3/latex2e
On branch  : callbacks
Link       : https://github.com/latex3/latex2e/commit/051934cd37c589a0bf9d96cf6f3baa17d7716fc8

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

commit 051934cd37c589a0bf9d96cf6f3baa17d7716fc8
Author: Marcel Fabian Krüger <tex at 2krueger.de>
Date:   Fri Jan 10 14:48:29 2020 +0100

    reverselist callback type
    
    Add a variant of the `list` callback type which runs the registered
    callbacks in reverse order.
    This could be used especially for `post_linebreak_filter` but this is
    delayed to ensure compatibility.


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

051934cd37c589a0bf9d96cf6f3baa17d7716fc8
 base/ltluatex.dtx                          | 64 ++++++++++++++++++++++--------
 base/testfiles/tlb-ltluatex-001.luatex.tlg |  2 +-
 2 files changed, 49 insertions(+), 17 deletions(-)

diff --git a/base/ltluatex.dtx b/base/ltluatex.dtx
index 129aa2ef..0f7dcc4e 100644
--- a/base/ltluatex.dtx
+++ b/base/ltluatex.dtx
@@ -1239,12 +1239,13 @@ local callbacklist = callbacklist or { }
 % Numerical codes for callback types, and name-to-value association (the
 % table keys are strings, the values are numbers).
 %    \begin{macrocode}
-local list, data, exclusive, simple = 1, 2, 3, 4
-local types = {
-  list      = list,
-  data      = data,
-  exclusive = exclusive,
-  simple    = simple,
+local list, data, exclusive, simple, reverselist = 1, 2, 3, 4, 5
+local types   = {
+  list        = list,
+  data        = data,
+  exclusive   = exclusive,
+  simple      = simple,
+  reverselist = reverselist,
 }
 %    \end{macrocode}
 %
@@ -1441,6 +1442,8 @@ end
 %     |true|, then the same head is passed to the next function. If all
 %     functions return |true|, then |true| is returned, otherwise the return
 %     value of the last function not returning |true| is used.
+%   \item[reverselist] is a specialized variant of \emph{list} which executes
+%     functions in inverse order.
 %   \item[exclusive] is for functions with more complex signatures; functions in
 %     this type of callback are \emph{not} combined: An error is raised if
 %     a second callback is registered..
@@ -1498,12 +1501,39 @@ local function list_handler(name)
   end
 end
 %    \end{macrocode}
-% Default for user-defined |list| callbacks without explicit default.
+% Default for user-defined |list| and |reverselist| callbacks without explicit default.
 %    \begin{macrocode}
 local function list_handler_default()
   return true
 end
 %    \end{macrocode}
+% Handler for |reverselist| callbacks.
+% \changes{v1.1l}{2020/02/02}{Add reverselist callback type}
+%    \begin{macrocode}
+local function reverselist_handler(name)
+  return function(head, ...)
+    local ret
+    local alltrue = true
+    local callbacks = callbacklist[name]
+    for i = #callbacks, 1, -1 do
+      local cb = callbacks[i]
+      ret = cb.func(head, ...)
+      if ret == false then
+        luatexbase_warning(
+          "Function `" .. cb.description .. "' returned false\n"
+            .. "in callback `" .. name .."'"
+         )
+         break
+      end
+      if ret ~= true then
+        alltrue = false
+        head = ret
+      end
+    end
+    return alltrue and true or head
+  end
+end
+%    \end{macrocode}
 % Handler for |simple| callbacks.
 %    \begin{macrocode}
 local function simple_handler(name)
@@ -1522,17 +1552,19 @@ end
 %
 % Keep a handlers table for indexed access and a table with the corresponding default functions.
 %    \begin{macrocode}
-local handlers = {
-  [data]      = data_handler,
-  [exclusive] = exclusive_handler,
-  [list]      = list_handler,
-  [simple]    = simple_handler,
+local handlers  = {
+  [data]        = data_handler,
+  [exclusive]   = exclusive_handler,
+  [list]        = list_handler,
+  [reverselist] = reverselist_handler,
+  [simple]      = simple_handler,
 }
 local defaults = {
-  [data]      = data_handler_default,
-  [exclusive] = nil,
-  [list]      = list_handler_default,
-  [simple]    = simple_handler_default,
+  [data]        = data_handler_default,
+  [exclusive]   = nil,
+  [list]        = list_handler_default,
+  [reverselist] = list_handler_default,
+  [simple]      = simple_handler_default,
 }
 %    \end{macrocode}
 %
diff --git a/base/testfiles/tlb-ltluatex-001.luatex.tlg b/base/testfiles/tlb-ltluatex-001.luatex.tlg
index 41e42640..659f3a34 100644
--- a/base/testfiles/tlb-ltluatex-001.luatex.tlg
+++ b/base/testfiles/tlb-ltluatex-001.luatex.tlg
@@ -7,7 +7,7 @@ stack traceback:
 ^^I[C]: in function 'error'
 ^^I./ltluatex.lua:109: in upvalue 'module_error'
 ^^I./ltluatex.lua:116: in upvalue 'luatexbase_error'
-^^I./ltluatex.lua:397: in field 'create_callback'
+^^I./ltluatex.lua:423: in field 'create_callback'
 ^^I[\directlua]:1: in main chunk.
 l. ...}
 The lua interpreter ran into a problem, so the





More information about the latex3-commits mailing list