Skip to content

Commit

Permalink
reverselist callback type
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
zauguin committed Jan 13, 2020
1 parent 5ef70da commit 7f993ca
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 17 deletions.
64 changes: 48 additions & 16 deletions base/ltluatex.dtx
Expand Up @@ -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}
%
Expand Down Expand Up @@ -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..
Expand Down Expand Up @@ -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)
Expand All @@ -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}
%
Expand Down
2 changes: 1 addition & 1 deletion base/testfiles/tlb-ltluatex-001.luatex.tlg
Expand Up @@ -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
Expand Down

0 comments on commit 7f993ca

Please sign in to comment.