| 1 |
-- |
| 2 |
-- This is file `ltluatex.lua', |
| 3 |
-- generated with the docstrip utility. |
| 4 |
-- |
| 5 |
-- The original source files were: |
| 6 |
-- |
| 7 |
-- ltluatex.dtx (with options: `lua') |
| 8 |
-- |
| 9 |
-- This is a generated file. |
| 10 |
-- |
| 11 |
-- The source is maintained by the LaTeX Project team and bug |
| 12 |
-- reports for it can be opened at https://latex-project.org/bugs.html |
| 13 |
-- (but please observe conditions on bug reports sent to that address!) |
| 14 |
-- |
| 15 |
-- |
| 16 |
-- Copyright (C) 2015-2025 |
| 17 |
-- The LaTeX Project and any individual authors listed elsewhere |
| 18 |
-- in this file. |
| 19 |
-- |
| 20 |
-- This file was generated from file(s) of the LaTeX base system. |
| 21 |
-- -------------------------------------------------------------- |
| 22 |
-- |
| 23 |
-- It may be distributed and/or modified under the |
| 24 |
-- conditions of the LaTeX Project Public License, either version 1.3c |
| 25 |
-- of this license or (at your option) any later version. |
| 26 |
-- The latest version of this license is in |
| 27 |
-- https://www.latex-project.org/lppl.txt |
| 28 |
-- and version 1.3c or later is part of all distributions of LaTeX |
| 29 |
-- version 2008 or later. |
| 30 |
-- |
| 31 |
-- This file has the LPPL maintenance status "maintained". |
| 32 |
-- |
| 33 |
-- This file may only be distributed together with a copy of the LaTeX |
| 34 |
-- base system. You may however distribute the LaTeX base system without |
| 35 |
-- such generated files. |
| 36 |
-- |
| 37 |
-- The list of all files belonging to the LaTeX base distribution is |
| 38 |
-- given in the file `manifest.txt'. See also `legal.txt' for additional |
| 39 |
-- information. |
| 40 |
-- |
| 41 |
-- The list of derived (unpacked) files belonging to the distribution |
| 42 |
-- and covered by LPPL is defined by the unpacking scripts (with |
| 43 |
-- extension .ins) which are part of the distribution. |
| 44 |
luatexbase = luatexbase or { } |
| 45 |
local luatexbase = luatexbase |
| 46 |
local string_gsub = string.gsub |
| 47 |
local tex_count = tex.count |
| 48 |
local tex_setcount = tex.setcount |
| 49 |
local texio_write_nl = texio.write_nl |
| 50 |
local flush_list = node.flush_list |
| 51 |
local luatexbase_warning |
| 52 |
local luatexbase_error |
| 53 |
local modules = modules or { } |
| 54 |
local function luatexbase_log(text) |
| 55 |
texio_write_nl("log", text) |
| 56 |
end |
| 57 |
local function provides_module(info) |
| 58 |
if not (info and info.name) then |
| 59 |
luatexbase_error("Missing module name for provides_module") |
| 60 |
end |
| 61 |
local function spaced(text) |
| 62 |
return text and (" " .. text) or "" |
| 63 |
end |
| 64 |
luatexbase_log( |
| 65 |
"Lua module: " .. info.name |
| 66 |
.. spaced(info.date) |
| 67 |
.. spaced(info.version and string_gsub(info.version or "","^(%d)","v%1")) |
| 68 |
.. spaced(info.description) |
| 69 |
) |
| 70 |
modules[info.name] = info |
| 71 |
end |
| 72 |
luatexbase.provides_module = provides_module |
| 73 |
local function msg_format(mod, msg_type, text) |
| 74 |
local leader = "" |
| 75 |
local cont |
| 76 |
local first_head |
| 77 |
if mod == "LaTeX" then |
| 78 |
cont = string_gsub(leader, ".", " ") |
| 79 |
first_head = leader .. "LaTeX: " |
| 80 |
else |
| 81 |
first_head = leader .. "Module " .. msg_type |
| 82 |
cont = "(" .. mod .. ")" |
| 83 |
.. string_gsub(first_head, ".", " ") |
| 84 |
first_head = leader .. "Module " .. mod .. " " .. msg_type .. ":" |
| 85 |
end |
| 86 |
if msg_type == "Error" then |
| 87 |
first_head = "\n" .. first_head |
| 88 |
end |
| 89 |
if string.sub(text,-1) ~= "\n" then |
| 90 |
text = text .. " " |
| 91 |
end |
| 92 |
return first_head .. " " |
| 93 |
.. string_gsub( |
| 94 |
text |
| 95 |
.. "on input line " |
| 96 |
.. tex.inputlineno, "\n", "\n" .. cont .. " " |
| 97 |
) |
| 98 |
.. "\n" |
| 99 |
end |
| 100 |
local function module_info(mod, text) |
| 101 |
texio_write_nl("log", msg_format(mod, "Info", text)) |
| 102 |
end |
| 103 |
luatexbase.module_info = module_info |
| 104 |
local function module_warning(mod, text) |
| 105 |
texio_write_nl("term and log",msg_format(mod, "Warning", text)) |
| 106 |
end |
| 107 |
luatexbase.module_warning = module_warning |
| 108 |
local function module_error(mod, text) |
| 109 |
error(msg_format(mod, "Error", text)) |
| 110 |
end |
| 111 |
luatexbase.module_error = module_error |
| 112 |
function luatexbase_warning(text) |
| 113 |
module_warning("luatexbase", text) |
| 114 |
end |
| 115 |
function luatexbase_error(text) |
| 116 |
module_error("luatexbase", text) |
| 117 |
end |
| 118 |
local luaregisterbasetable = { } |
| 119 |
local registermap = { |
| 120 |
attributezero = "assign_attr" , |
| 121 |
charzero = "char_given" , |
| 122 |
CountZero = "assign_int" , |
| 123 |
dimenzero = "assign_dimen" , |
| 124 |
mathcharzero = "math_given" , |
| 125 |
muskipzero = "assign_mu_skip" , |
| 126 |
skipzero = "assign_skip" , |
| 127 |
tokszero = "assign_toks" , |
| 128 |
} |
| 129 |
local createtoken |
| 130 |
if tex.luatexversion > 81 then |
| 131 |
createtoken = token.create |
| 132 |
elseif tex.luatexversion > 79 then |
| 133 |
createtoken = newtoken.create |
| 134 |
end |
| 135 |
local hashtokens = tex.hashtokens() |
| 136 |
local luatexversion = tex.luatexversion |
| 137 |
for i,j in pairs (registermap) do |
| 138 |
if luatexversion < 80 then |
| 139 |
luaregisterbasetable[hashtokens[i][1]] = |
| 140 |
hashtokens[i][2] |
| 141 |
else |
| 142 |
luaregisterbasetable[j] = createtoken(i).mode |
| 143 |
end |
| 144 |
end |
| 145 |
local registernumber |
| 146 |
if luatexversion < 80 then |
| 147 |
function registernumber(name) |
| 148 |
local nt = hashtokens[name] |
| 149 |
if(nt and luaregisterbasetable[nt[1]]) then |
| 150 |
return nt[2] - luaregisterbasetable[nt[1]] |
| 151 |
else |
| 152 |
return false |
| 153 |
end |
| 154 |
end |
| 155 |
else |
| 156 |
function registernumber(name) |
| 157 |
local nt = createtoken(name) |
| 158 |
if(luaregisterbasetable[nt.cmdname]) then |
| 159 |
return nt.mode - luaregisterbasetable[nt.cmdname] |
| 160 |
else |
| 161 |
return false |
| 162 |
end |
| 163 |
end |
| 164 |
end |
| 165 |
luatexbase.registernumber = registernumber |
| 166 |
local attributes=setmetatable( |
| 167 |
{}, |
| 168 |
{ |
| 169 |
__index = function(t,key) |
| 170 |
return registernumber(key) or nil |
| 171 |
end} |
| 172 |
) |
| 173 |
luatexbase.attributes = attributes |
| 174 |
local attribute_count_name = |
| 175 |
attribute_count_name or "e@alloc@attribute@count" |
| 176 |
local function new_attribute(name) |
| 177 |
tex_setcount("global", attribute_count_name, |
| 178 |
tex_count[attribute_count_name] + 1) |
| 179 |
if tex_count[attribute_count_name] > 65534 then |
| 180 |
luatexbase_error("No room for a new \\attribute") |
| 181 |
end |
| 182 |
attributes[name]= tex_count[attribute_count_name] |
| 183 |
luatexbase_log("Lua-only attribute " .. name .. " = " .. |
| 184 |
tex_count[attribute_count_name]) |
| 185 |
return tex_count[attribute_count_name] |
| 186 |
end |
| 187 |
luatexbase.new_attribute = new_attribute |
| 188 |
local whatsit_count_name = whatsit_count_name or "e@alloc@whatsit@count" |
| 189 |
local function new_whatsit(name) |
| 190 |
tex_setcount("global", whatsit_count_name, |
| 191 |
tex_count[whatsit_count_name] + 1) |
| 192 |
if tex_count[whatsit_count_name] > 65534 then |
| 193 |
luatexbase_error("No room for a new custom whatsit") |
| 194 |
end |
| 195 |
luatexbase_log("Custom whatsit " .. (name or "") .. " = " .. |
| 196 |
tex_count[whatsit_count_name]) |
| 197 |
return tex_count[whatsit_count_name] |
| 198 |
end |
| 199 |
luatexbase.new_whatsit = new_whatsit |
| 200 |
local bytecode_count_name = |
| 201 |
bytecode_count_name or "e@alloc@bytecode@count" |
| 202 |
local function new_bytecode(name) |
| 203 |
tex_setcount("global", bytecode_count_name, |
| 204 |
tex_count[bytecode_count_name] + 1) |
| 205 |
if tex_count[bytecode_count_name] > 65534 then |
| 206 |
luatexbase_error("No room for a new bytecode register") |
| 207 |
end |
| 208 |
luatexbase_log("Lua bytecode " .. (name or "") .. " = " .. |
| 209 |
tex_count[bytecode_count_name]) |
| 210 |
return tex_count[bytecode_count_name] |
| 211 |
end |
| 212 |
luatexbase.new_bytecode = new_bytecode |
| 213 |
local chunkname_count_name = |
| 214 |
chunkname_count_name or "e@alloc@luachunk@count" |
| 215 |
local function new_chunkname(name) |
| 216 |
tex_setcount("global", chunkname_count_name, |
| 217 |
tex_count[chunkname_count_name] + 1) |
| 218 |
local chunkname_count = tex_count[chunkname_count_name] |
| 219 |
chunkname_count = chunkname_count + 1 |
| 220 |
if chunkname_count > 65534 then |
| 221 |
luatexbase_error("No room for a new chunkname") |
| 222 |
end |
| 223 |
lua.name[chunkname_count]=name |
| 224 |
luatexbase_log("Lua chunkname " .. (name or "") .. " = " .. |
| 225 |
chunkname_count .. "\n") |
| 226 |
return chunkname_count |
| 227 |
end |
| 228 |
luatexbase.new_chunkname = new_chunkname |
| 229 |
local luafunction_count_name = |
| 230 |
luafunction_count_name or "e@alloc@luafunction@count" |
| 231 |
local function new_luafunction(name) |
| 232 |
tex_setcount("global", luafunction_count_name, |
| 233 |
math.max( |
| 234 |
#(lua.get_functions_table()), |
| 235 |
tex_count[luafunction_count_name]) |
| 236 |
+ 1) |
| 237 |
lua.get_functions_table()[tex_count[luafunction_count_name]] = false |
| 238 |
if tex_count[luafunction_count_name] > 65534 then |
| 239 |
luatexbase_error("No room for a new luafunction register") |
| 240 |
end |
| 241 |
luatexbase_log("Lua function " .. (name or "") .. " = " .. |
| 242 |
tex_count[luafunction_count_name]) |
| 243 |
return tex_count[luafunction_count_name] |
| 244 |
end |
| 245 |
luatexbase.new_luafunction = new_luafunction |
| 246 |
local realcallbacklist = {} |
| 247 |
local callbackrules = {} |
| 248 |
local callbacklist = setmetatable({}, { |
| 249 |
__index = function(t, name) |
| 250 |
local list = realcallbacklist[name] |
| 251 |
local rules = callbackrules[name] |
| 252 |
if list and rules then |
| 253 |
local meta = {} |
| 254 |
for i, entry in ipairs(list) do |
| 255 |
local t = {value = entry, count = 0, pos = i} |
| 256 |
meta[entry.description], list[i] = t, t |
| 257 |
end |
| 258 |
local count = #list |
| 259 |
local pos = count |
| 260 |
for i, rule in ipairs(rules) do |
| 261 |
local rule = rules[i] |
| 262 |
local pre, post = meta[rule[1]], meta[rule[2]] |
| 263 |
if pre and post then |
| 264 |
if rule.type then |
| 265 |
if not rule.hidden then |
| 266 |
assert(rule.type == 'incompatible-warning' and luatexbase_warning |
| 267 |
or rule.type == 'incompatible-error' and luatexbase_error)( |
| 268 |
"Incompatible functions \"" .. rule[1] .. "\" and \"" .. rule[2] |
| 269 |
.. "\" specified for callback \"" .. name .. "\".") |
| 270 |
rule.hidden = true |
| 271 |
end |
| 272 |
else |
| 273 |
local post_count = post.count |
| 274 |
post.count = post_count+1 |
| 275 |
if post_count == 0 then |
| 276 |
local post_pos = post.pos |
| 277 |
if post_pos ~= pos then |
| 278 |
local new_post_pos = list[pos] |
| 279 |
new_post_pos.pos = post_pos |
| 280 |
list[post_pos] = new_post_pos |
| 281 |
end |
| 282 |
list[pos] = nil |
| 283 |
pos = pos - 1 |
| 284 |
end |
| 285 |
pre[#pre+1] = post |
| 286 |
end |
| 287 |
end |
| 288 |
end |
| 289 |
for i=1, count do -- The actual sort begins |
| 290 |
local current = list[i] |
| 291 |
if current then |
| 292 |
meta[current.value.description] = nil |
| 293 |
for j, cur in ipairs(current) do |
| 294 |
local count = cur.count |
| 295 |
if count == 1 then |
| 296 |
pos = pos + 1 |
| 297 |
list[pos] = cur |
| 298 |
else |
| 299 |
cur.count = count - 1 |
| 300 |
end |
| 301 |
end |
| 302 |
list[i] = current.value |
| 303 |
else |
| 304 |
-- Cycle occurred. TODO: Show cycle for debugging |
| 305 |
-- list[i] = ... |
| 306 |
local remaining = {} |
| 307 |
for name, entry in next, meta do |
| 308 |
local value = entry.value |
| 309 |
list[#list + 1] = entry.value |
| 310 |
remaining[#remaining + 1] = name |
| 311 |
end |
| 312 |
table.sort(remaining) |
| 313 |
local first_name = remaining[1] |
| 314 |
for j, name in ipairs(remaining) do |
| 315 |
local entry = meta[name] |
| 316 |
list[i + j - 1] = entry.value |
| 317 |
for _, post_entry in ipairs(entry) do |
| 318 |
local post_name = post_entry.value.description |
| 319 |
if not remaining[post_name] then |
| 320 |
remaining[post_name] = name |
| 321 |
end |
| 322 |
end |
| 323 |
end |
| 324 |
local cycle = {first_name} |
| 325 |
local index = 1 |
| 326 |
local last_name = first_name |
| 327 |
repeat |
| 328 |
cycle[last_name] = index |
| 329 |
last_name = remaining[last_name] |
| 330 |
index = index + 1 |
| 331 |
cycle[index] = last_name |
| 332 |
until cycle[last_name] |
| 333 |
local length = index - cycle[last_name] + 1 |
| 334 |
table.move(cycle, cycle[last_name], index, 1) |
| 335 |
for i=2, length//2 do |
| 336 |
cycle[i], cycle[length + 1 - i] = cycle[length + 1 - i], cycle[i] |
| 337 |
end |
| 338 |
error('Cycle occurred at ' .. table.concat(cycle, ' -> ', 1, length)) |
| 339 |
end |
| 340 |
end |
| 341 |
end |
| 342 |
realcallbacklist[name] = list |
| 343 |
t[name] = list |
| 344 |
return list |
| 345 |
end |
| 346 |
}) |
| 347 |
local list, data, exclusive, simple, reverselist = 1, 2, 3, 4, 5 |
| 348 |
local types = { |
| 349 |
list = list, |
| 350 |
data = data, |
| 351 |
exclusive = exclusive, |
| 352 |
simple = simple, |
| 353 |
reverselist = reverselist, |
| 354 |
} |
| 355 |
local callbacktypes = callbacktypes or { |
| 356 |
find_read_file = exclusive, |
| 357 |
find_write_file = exclusive, |
| 358 |
find_font_file = data, |
| 359 |
find_output_file = data, |
| 360 |
find_format_file = data, |
| 361 |
find_vf_file = data, |
| 362 |
find_map_file = data, |
| 363 |
find_enc_file = data, |
| 364 |
find_pk_file = data, |
| 365 |
find_data_file = data, |
| 366 |
find_opentype_file = data, |
| 367 |
find_truetype_file = data, |
| 368 |
find_type1_file = data, |
| 369 |
find_image_file = data, |
| 370 |
open_read_file = exclusive, |
| 371 |
read_font_file = exclusive, |
| 372 |
read_vf_file = exclusive, |
| 373 |
read_map_file = exclusive, |
| 374 |
read_enc_file = exclusive, |
| 375 |
read_pk_file = exclusive, |
| 376 |
read_data_file = exclusive, |
| 377 |
read_truetype_file = exclusive, |
| 378 |
read_type1_file = exclusive, |
| 379 |
read_opentype_file = exclusive, |
| 380 |
find_cidmap_file = data, |
| 381 |
read_cidmap_file = exclusive, |
| 382 |
process_input_buffer = data, |
| 383 |
process_output_buffer = data, |
| 384 |
process_jobname = data, |
| 385 |
contribute_filter = simple, |
| 386 |
buildpage_filter = simple, |
| 387 |
build_page_insert = exclusive, |
| 388 |
pre_linebreak_filter = list, |
| 389 |
linebreak_filter = exclusive, |
| 390 |
append_to_vlist_filter = exclusive, |
| 391 |
post_linebreak_filter = reverselist, |
| 392 |
hpack_filter = list, |
| 393 |
vpack_filter = list, |
| 394 |
hpack_quality = exclusive, |
| 395 |
vpack_quality = exclusive, |
| 396 |
pre_output_filter = list, |
| 397 |
process_rule = exclusive, |
| 398 |
hyphenate = simple, |
| 399 |
ligaturing = simple, |
| 400 |
kerning = simple, |
| 401 |
insert_local_par = simple, |
| 402 |
new_graf = exclusive, |
| 403 |
pre_dump = simple, |
| 404 |
start_run = simple, |
| 405 |
stop_run = simple, |
| 406 |
start_page_number = simple, |
| 407 |
stop_page_number = simple, |
| 408 |
show_error_hook = simple, |
| 409 |
show_warning_message = simple, |
| 410 |
show_error_message = simple, |
| 411 |
show_lua_error_hook = simple, |
| 412 |
start_file = simple, |
| 413 |
stop_file = simple, |
| 414 |
call_edit = simple, |
| 415 |
finish_synctex = simple, |
| 416 |
wrapup_run = simple, |
| 417 |
finish_pdffile = data, |
| 418 |
finish_pdfpage = data, |
| 419 |
page_objnum_provider = data, |
| 420 |
page_order_index = data, |
| 421 |
process_pdf_image_content = data, |
| 422 |
define_font = exclusive, |
| 423 |
glyph_info = exclusive, |
| 424 |
glyph_not_found = exclusive, |
| 425 |
glyph_stream_provider = exclusive, |
| 426 |
make_extensible = exclusive, |
| 427 |
font_descriptor_objnum_provider = exclusive, |
| 428 |
input_level_string = exclusive, |
| 429 |
provide_charproc_data = exclusive, |
| 430 |
} |
| 431 |
luatexbase.callbacktypes=callbacktypes |
| 432 |
local shared_callbacks = { |
| 433 |
mlist_to_hlist = { |
| 434 |
callback = "mlist_to_hlist", |
| 435 |
count = 0, |
| 436 |
handler = nil, |
| 437 |
}, |
| 438 |
} |
| 439 |
shared_callbacks.pre_mlist_to_hlist_filter = shared_callbacks.mlist_to_hlist |
| 440 |
shared_callbacks.post_mlist_to_hlist_filter = shared_callbacks.mlist_to_hlist |
| 441 |
local callback_register = callback_register or callback.register |
| 442 |
function callback.register() |
| 443 |
luatexbase_error("Attempt to use callback.register() directly\n") |
| 444 |
end |
| 445 |
local function data_handler(name) |
| 446 |
return function(data, ...) |
| 447 |
for _,i in ipairs(callbacklist[name]) do |
| 448 |
data = i.func(data,...) |
| 449 |
end |
| 450 |
return data |
| 451 |
end |
| 452 |
end |
| 453 |
local function data_handler_default(value) |
| 454 |
return value |
| 455 |
end |
| 456 |
local function exclusive_handler(name) |
| 457 |
return function(...) |
| 458 |
return callbacklist[name][1].func(...) |
| 459 |
end |
| 460 |
end |
| 461 |
local function list_handler(name) |
| 462 |
return function(head, ...) |
| 463 |
local ret |
| 464 |
for _,i in ipairs(callbacklist[name]) do |
| 465 |
ret = i.func(head, ...) |
| 466 |
if ret == false then |
| 467 |
luatexbase_warning( |
| 468 |
"Function `" .. i.description .. "' returned false\n" |
| 469 |
.. "in callback `" .. name .."'" |
| 470 |
) |
| 471 |
return false |
| 472 |
end |
| 473 |
if ret ~= true then |
| 474 |
head = ret |
| 475 |
end |
| 476 |
end |
| 477 |
return head |
| 478 |
end |
| 479 |
end |
| 480 |
local function list_handler_default(head) |
| 481 |
return head |
| 482 |
end |
| 483 |
local function reverselist_handler(name) |
| 484 |
return function(head, ...) |
| 485 |
local ret |
| 486 |
local callbacks = callbacklist[name] |
| 487 |
for i = #callbacks, 1, -1 do |
| 488 |
local cb = callbacks[i] |
| 489 |
ret = cb.func(head, ...) |
| 490 |
if ret == false then |
| 491 |
luatexbase_warning( |
| 492 |
"Function `" .. cb.description .. "' returned false\n" |
| 493 |
.. "in callback `" .. name .."'" |
| 494 |
) |
| 495 |
return false |
| 496 |
end |
| 497 |
if ret ~= true then |
| 498 |
head = ret |
| 499 |
end |
| 500 |
end |
| 501 |
return head |
| 502 |
end |
| 503 |
end |
| 504 |
local function simple_handler(name) |
| 505 |
return function(...) |
| 506 |
for _,i in ipairs(callbacklist[name]) do |
| 507 |
i.func(...) |
| 508 |
end |
| 509 |
end |
| 510 |
end |
| 511 |
local function simple_handler_default() |
| 512 |
end |
| 513 |
local handlers = { |
| 514 |
[data] = data_handler, |
| 515 |
[exclusive] = exclusive_handler, |
| 516 |
[list] = list_handler, |
| 517 |
[reverselist] = reverselist_handler, |
| 518 |
[simple] = simple_handler, |
| 519 |
} |
| 520 |
local defaults = { |
| 521 |
[data] = data_handler_default, |
| 522 |
[exclusive] = nil, |
| 523 |
[list] = list_handler_default, |
| 524 |
[reverselist] = list_handler_default, |
| 525 |
[simple] = simple_handler_default, |
| 526 |
} |
| 527 |
local user_callbacks_defaults = {} |
| 528 |
local function create_callback(name, ctype, default) |
| 529 |
local ctype_id = types[ctype] |
| 530 |
if not name or name == "" |
| 531 |
or not ctype_id |
| 532 |
then |
| 533 |
luatexbase_error("Unable to create callback:\n" .. |
| 534 |
"valid callback name and type required") |
| 535 |
end |
| 536 |
if callbacktypes[name] then |
| 537 |
luatexbase_error("Unable to create callback `" .. name .. |
| 538 |
"':\ncallback is already defined") |
| 539 |
end |
| 540 |
default = default or defaults[ctype_id] |
| 541 |
if not default then |
| 542 |
luatexbase_error("Unable to create callback `" .. name .. |
| 543 |
"':\ndefault is required for `" .. ctype .. |
| 544 |
"' callbacks") |
| 545 |
elseif type (default) ~= "function" then |
| 546 |
luatexbase_error("Unable to create callback `" .. name .. |
| 547 |
"':\ndefault is not a function") |
| 548 |
end |
| 549 |
user_callbacks_defaults[name] = default |
| 550 |
callbacktypes[name] = ctype_id |
| 551 |
end |
| 552 |
luatexbase.create_callback = create_callback |
| 553 |
local function call_callback(name,...) |
| 554 |
if not name or name == "" then |
| 555 |
luatexbase_error("Unable to create callback:\n" .. |
| 556 |
"valid callback name required") |
| 557 |
end |
| 558 |
if user_callbacks_defaults[name] == nil then |
| 559 |
luatexbase_error("Unable to call callback `" .. name |
| 560 |
.. "':\nunknown or empty") |
| 561 |
end |
| 562 |
local l = callbacklist[name] |
| 563 |
local f |
| 564 |
if not l then |
| 565 |
f = user_callbacks_defaults[name] |
| 566 |
else |
| 567 |
f = handlers[callbacktypes[name]](name) |
| 568 |
end |
| 569 |
return f(...) |
| 570 |
end |
| 571 |
luatexbase.call_callback=call_callback |
| 572 |
local function add_to_callback(name, func, description) |
| 573 |
if not name or name == "" then |
| 574 |
luatexbase_error("Unable to register callback:\n" .. |
| 575 |
"valid callback name required") |
| 576 |
end |
| 577 |
if not callbacktypes[name] or |
| 578 |
type(func) ~= "function" or |
| 579 |
not description or |
| 580 |
description == "" then |
| 581 |
luatexbase_error( |
| 582 |
"Unable to register callback.\n\n" |
| 583 |
.. "Correct usage:\n" |
| 584 |
.. "add_to_callback(<callback>, <function>, <description>)" |
| 585 |
) |
| 586 |
end |
| 587 |
local l = realcallbacklist[name] |
| 588 |
if l == nil then |
| 589 |
l = { } |
| 590 |
realcallbacklist[name] = l |
| 591 |
local shared = shared_callbacks[name] |
| 592 |
if shared then |
| 593 |
shared.count = shared.count + 1 |
| 594 |
if shared.count == 1 then |
| 595 |
callback_register(shared.callback, shared.handler) |
| 596 |
end |
| 597 |
elseif user_callbacks_defaults[name] == nil then |
| 598 |
callback_register(name, handlers[callbacktypes[name]](name)) |
| 599 |
end |
| 600 |
end |
| 601 |
local f = { |
| 602 |
func = func, |
| 603 |
description = description, |
| 604 |
} |
| 605 |
if callbacktypes[name] == exclusive then |
| 606 |
if #l == 1 then |
| 607 |
luatexbase_error( |
| 608 |
"Cannot add second callback to exclusive function\n`" .. |
| 609 |
name .. "'") |
| 610 |
end |
| 611 |
end |
| 612 |
table.insert(l, f) |
| 613 |
callbacklist[name] = nil |
| 614 |
luatexbase_log( |
| 615 |
"Inserting `" .. description .. "' in `" .. name .. "'." |
| 616 |
) |
| 617 |
end |
| 618 |
luatexbase.add_to_callback = add_to_callback |
| 619 |
local function declare_callback_rule(name, desc1, relation, desc2) |
| 620 |
if not callbacktypes[name] or |
| 621 |
not desc1 or not desc2 or |
| 622 |
desc1 == "" or desc2 == "" then |
| 623 |
luatexbase_error( |
| 624 |
"Unable to create ordering constraint. " |
| 625 |
.. "Correct usage:\n" |
| 626 |
.. "declare_callback_rule(<callback>, <description_a>, <description_b>)" |
| 627 |
) |
| 628 |
end |
| 629 |
if relation == 'before' then |
| 630 |
relation = nil |
| 631 |
elseif relation == 'after' then |
| 632 |
desc2, desc1 = desc1, desc2 |
| 633 |
relation = nil |
| 634 |
elseif relation == 'incompatible-warning' or relation == 'incompatible-error' then |
| 635 |
elseif relation == 'unrelated' then |
| 636 |
else |
| 637 |
luatexbase_error( |
| 638 |
"Unknown relation type in declare_callback_rule" |
| 639 |
) |
| 640 |
end |
| 641 |
callbacklist[name] = nil |
| 642 |
local rules = callbackrules[name] |
| 643 |
if rules then |
| 644 |
for i, rule in ipairs(rules) do |
| 645 |
if rule[1] == desc1 and rule[2] == desc2 or rule[1] == desc2 and rule[2] == desc1 then |
| 646 |
if relation == 'unrelated' then |
| 647 |
table.remove(rules, i) |
| 648 |
else |
| 649 |
rule[1], rule[2], rule.type = desc1, desc2, relation |
| 650 |
end |
| 651 |
return |
| 652 |
end |
| 653 |
end |
| 654 |
if relation ~= 'unrelated' then |
| 655 |
rules[#rules + 1] = {desc1, desc2, type = relation} |
| 656 |
end |
| 657 |
elseif relation ~= 'unrelated' then |
| 658 |
callbackrules[name] = {{desc1, desc2, type = relation}} |
| 659 |
end |
| 660 |
end |
| 661 |
luatexbase.declare_callback_rule = declare_callback_rule |
| 662 |
local function remove_from_callback(name, description) |
| 663 |
if not name or name == "" then |
| 664 |
luatexbase_error("Unable to remove function from callback:\n" .. |
| 665 |
"valid callback name required") |
| 666 |
end |
| 667 |
if not callbacktypes[name] or |
| 668 |
not description or |
| 669 |
description == "" then |
| 670 |
luatexbase_error( |
| 671 |
"Unable to remove function from callback.\n\n" |
| 672 |
.. "Correct usage:\n" |
| 673 |
.. "remove_from_callback(<callback>, <description>)" |
| 674 |
) |
| 675 |
end |
| 676 |
local l = realcallbacklist[name] |
| 677 |
if not l then |
| 678 |
luatexbase_error( |
| 679 |
"No callback list for `" .. name .. "'\n") |
| 680 |
end |
| 681 |
local index = false |
| 682 |
for i,j in ipairs(l) do |
| 683 |
if j.description == description then |
| 684 |
index = i |
| 685 |
break |
| 686 |
end |
| 687 |
end |
| 688 |
if not index then |
| 689 |
luatexbase_error( |
| 690 |
"No callback `" .. description .. "' registered for `" .. |
| 691 |
name .. "'\n") |
| 692 |
end |
| 693 |
local cb = l[index] |
| 694 |
table.remove(l, index) |
| 695 |
luatexbase_log( |
| 696 |
"Removing `" .. description .. "' from `" .. name .. "'." |
| 697 |
) |
| 698 |
if #l == 0 then |
| 699 |
realcallbacklist[name] = nil |
| 700 |
callbacklist[name] = nil |
| 701 |
local shared = shared_callbacks[name] |
| 702 |
if shared then |
| 703 |
shared.count = shared.count - 1 |
| 704 |
if shared.count == 0 then |
| 705 |
callback_register(shared.callback, nil) |
| 706 |
end |
| 707 |
elseif user_callbacks_defaults[name] == nil then |
| 708 |
callback_register(name, nil) |
| 709 |
end |
| 710 |
end |
| 711 |
return cb.func,cb.description |
| 712 |
end |
| 713 |
luatexbase.remove_from_callback = remove_from_callback |
| 714 |
local function in_callback(name, description) |
| 715 |
if not name |
| 716 |
or name == "" |
| 717 |
or not realcallbacklist[name] |
| 718 |
or not callbacktypes[name] |
| 719 |
or not description then |
| 720 |
return false |
| 721 |
end |
| 722 |
for _, i in pairs(realcallbacklist[name]) do |
| 723 |
if i.description == description then |
| 724 |
return true |
| 725 |
end |
| 726 |
end |
| 727 |
return false |
| 728 |
end |
| 729 |
luatexbase.in_callback = in_callback |
| 730 |
local function disable_callback(name) |
| 731 |
if(realcallbacklist[name] == nil) then |
| 732 |
callback_register(name, false) |
| 733 |
else |
| 734 |
luatexbase_error("Callback list for " .. name .. " not empty") |
| 735 |
end |
| 736 |
end |
| 737 |
luatexbase.disable_callback = disable_callback |
| 738 |
local function callback_descriptions (name) |
| 739 |
local d = {} |
| 740 |
if not name |
| 741 |
or name == "" |
| 742 |
or not realcallbacklist[name] |
| 743 |
or not callbacktypes[name] |
| 744 |
then |
| 745 |
return d |
| 746 |
else |
| 747 |
for k, i in pairs(callbacklist[name]) do |
| 748 |
d[k]= i.description |
| 749 |
end |
| 750 |
end |
| 751 |
return d |
| 752 |
end |
| 753 |
luatexbase.callback_descriptions =callback_descriptions |
| 754 |
local function uninstall() |
| 755 |
module_info( |
| 756 |
"luatexbase", |
| 757 |
"Uninstalling kernel luatexbase code" |
| 758 |
) |
| 759 |
callback.register = callback_register |
| 760 |
luatexbase = nil |
| 761 |
end |
| 762 |
luatexbase.uninstall = uninstall |
| 763 |
create_callback('pre_mlist_to_hlist_filter', 'list') |
| 764 |
create_callback('mlist_to_hlist', 'exclusive', node.mlist_to_hlist) |
| 765 |
create_callback('post_mlist_to_hlist_filter', 'reverselist') |
| 766 |
function shared_callbacks.mlist_to_hlist.handler(head, display_type, need_penalties) |
| 767 |
local current = call_callback("pre_mlist_to_hlist_filter", head, display_type, need_penalties) |
| 768 |
if current == false then |
| 769 |
flush_list(head) |
| 770 |
return nil |
| 771 |
end |
| 772 |
current = call_callback("mlist_to_hlist", current, display_type, need_penalties) |
| 773 |
local post = call_callback("post_mlist_to_hlist_filter", current, display_type, need_penalties) |
| 774 |
if post == false then |
| 775 |
flush_list(current) |
| 776 |
return nil |
| 777 |
end |
| 778 |
return post |
| 779 |
end |