diff options
Diffstat (limited to 'macros/luatex/generic/penlightplus/penlightplus.lua')
-rw-r--r-- | macros/luatex/generic/penlightplus/penlightplus.lua | 82 |
1 files changed, 53 insertions, 29 deletions
diff --git a/macros/luatex/generic/penlightplus/penlightplus.lua b/macros/luatex/generic/penlightplus/penlightplus.lua index 96eb565fb6..1f74d45777 100644 --- a/macros/luatex/generic/penlightplus/penlightplus.lua +++ b/macros/luatex/generic/penlightplus/penlightplus.lua @@ -1,5 +1,5 @@ --% Kale Ewasiuk (kalekje@gmail.com) ---% 2023-08-27 +--% 2023-09-04 --% Copyright (C) 2021-2023 Kale Ewasiuk --% --% Permission is hereby granted, free of charge, to any person obtaining a copy @@ -776,22 +776,36 @@ end penlight.tbls = {} -function penlight.get_tbl_item(s, p) -- get item with string, p means print value - p = p or false - local itm = 'tbl: INVALID INDEX WITH: "'..s..'"' - if s:find('%.') then - local tt = s:split('.') - itm = penlight.tbls[tt[1]][tt[2]] - elseif s:find('/') then - local tt = s:split('/') - itm = penlight.tbls[tt[1]][tonumber(tonumber(tt[2]))] - else - if s:isdigit() then - itm = penlight.tbls._recent[tonumber(s)] +penlight.rec_tbl = '' +penlight.rec_tbl_opts = {} + +function penlight.get_tbl_index(s) + local tbl = '' + local key = '' + if s:find('%.') then + local tt = s:split('.') + tbl = tt[1] + key = tt[2] + elseif s:find('/') then + local tt = s:split('/') + tbl = tt[1] + key = tonumber(tonumber(tt[2])) + if key < 0 then key = #penlight.tbls[tbl]+1+key end else - itm = penlight.tbls._recent[s] + tbl = penlight.rec_tbl + key = tonumber(s) or s + if type(key) == 'number' and key < 0 then key = #penlight.tbls[tbl]+1+key end end - end + if penlight.tbls[tbl] == nil or penlight.tbls[tbl][key] == nil then + penlight.tex.pkgerror('penlightplus', 'Invalid index of tbl using: "'..s..'"') + end + return tbl, key +end + +function penlight.get_tbl_item(s, p) -- get item with string, p means print value + p = p or false + local tbl, key = penlight.get_tbl_index(s) + local itm = penlight.tbls[tbl][key] if p then tex.sprint(tostring(itm)) end @@ -800,20 +814,30 @@ end function penlight.set_tbl_item(s, v) - if s:find('%.') then - local tt = s:split('.') - penlight.tbls[tt[1]][tt[2]] = v - elseif s:find('/') then - local tt = s:split('/') - penlight.tbls[tt[1]][tonumber(tonumber(tt[2]))] = v - else - if s:isdigit() then - penlight.tbls._recent[tonumber(s)] = v - else - penlight.tbls._recent[s] = v - end - end - end + tbl, key = penlight.get_tbl_index(s) + penlight.tbls[tbl][key] = v +end + +function penlight.check_recent_tbl_undefault() + local undefaults = {} + if penlight.rec_tbl_opts ~= nil then + local defaults = penlight.tablex.union( + penlight.rec_tbl_opts.defs or {}, + penlight.rec_tbl_opts.defaults or {} + ) + for k, v in pairs(penlight.tbls[penlight.rec_tbl]) do + if defaults[k] == nil then + undefaults[#undefaults+1] = k + end + end + if penlight.hasval(undefaults) then + penlight.tex.pkgerror('penlightplus', + 'Invalid keys passed to tbl keyval: ' .. (', '):join(undefaults) .. + ' ; choices are: ' .. (', '):join(penlight.tablex.keys(defaults)) + ) + end + end +end if penlight.hasval(__PL_GLOBALS__) then |