Don't hardcode \immediate flag value and move custom functions into table

This commit is contained in:
Marcel Fabian Krüger 2021-11-06 13:23:29 +01:00
parent d61bb927a3
commit 32c5de7db7
7 changed files with 79 additions and 66 deletions

View File

@ -1,3 +1,5 @@
local lmlt = luametalatex
local scan_int = token.scan_int local scan_int = token.scan_int
local scan_token = token.scan_token local scan_token = token.scan_token
local scan_keyword = token.scan_keyword local scan_keyword = token.scan_keyword
@ -22,6 +24,8 @@ local utils = require'luametalatex-pdf-utils'
local strip_floats = utils.strip_floats local strip_floats = utils.strip_floats
local to_bp = utils.to_bp local to_bp = utils.to_bp
local immediate_flag = lmlt.flag.immediate
local pdfname, pfile local pdfname, pfile
local fontdirs = setmetatable({}, {__index=function(t, k)t[k] = pfile:getobj() return t[k] end}) local fontdirs = setmetatable({}, {__index=function(t, k)t[k] = pfile:getobj() return t[k] end})
local nodefont_meta = {} local nodefont_meta = {}
@ -65,15 +69,15 @@ end
local properties = node.direct.properties local properties = node.direct.properties
local reset_deadcycles do local reset_deadcycles do
local tokens = { local tokens = {
token.primitive_tokens.global, lmlt.primitive_tokens.global,
token.primitive_tokens.deadcycles,token.create(0x30), lmlt.primitive_tokens.deadcycles,token.create(0x30),
token.primitive_tokens.relax, lmlt.primitive_tokens.relax,
} }
function reset_deadcycles() function reset_deadcycles()
token.put_next(tokens) token.put_next(tokens)
end end
end end
token.luacmd("shipout", function() lmlt.luacmd("shipout", function()
local pfile = get_pfile() local pfile = get_pfile()
local total_voffset, total_hoffset = tex.voffset + pdfvariable.vorigin, tex.hoffset + pdfvariable.horigin local total_voffset, total_hoffset = tex.voffset + pdfvariable.vorigin, tex.hoffset + pdfvariable.horigin
local voff = node.new'kern' local voff = node.new'kern'
@ -247,7 +251,7 @@ function callbacks.stop_run()
end end
callbacks.__freeze('stop_run', true) callbacks.__freeze('stop_run', true)
token.luacmd("pdfvariable", function() lmlt.luacmd("pdfvariable", function()
for _, n in ipairs(pdf.variable_names) do for _, n in ipairs(pdf.variable_names) do
if scan_keyword(n) then if scan_keyword(n) then
return token.put_next(token.create('pdfvariable ' .. n)) return token.put_next(token.create('pdfvariable ' .. n))
@ -637,7 +641,7 @@ local function maybe_gobble_cmd(cmd)
token.put_next(t) token.put_next(t)
end end
end end
token.luacmd("pdffeedback", function() lmlt.luacmd("pdffeedback", function()
if scan_keyword"colorstackinit" then if scan_keyword"colorstackinit" then
local page = scan_keyword'page' local page = scan_keyword'page'
or (scan_keyword'nopage' and false) -- If you want to pass "page" as mode or (scan_keyword'nopage' and false) -- If you want to pass "page" as mode
@ -656,10 +660,10 @@ token.luacmd("pdffeedback", function()
error(string.format("Unknown PDF feedback %s", scan_word())) error(string.format("Unknown PDF feedback %s", scan_word()))
end end
end) end)
token.luacmd("pdfextension", function(_, immediate) lmlt.luacmd("pdfextension", function(_, immediate)
if immediate == "value" then return end if immediate == "value" then return end
if immediate and immediate & 0x7 ~= 0 then if immediate and immediate & ~immediate_flag ~= 0 then
immediate = immediate & 0x8 immediate = immediate & immediate_flag
tex.error("Unexpected prefix", "You used \\pdfextension with a prefix that doesn't belong there. I will ignore it for now.") tex.error("Unexpected prefix", "You used \\pdfextension with a prefix that doesn't belong there. I will ignore it for now.")
end end
if scan_keyword"colorstack" then if scan_keyword"colorstack" then
@ -727,7 +731,7 @@ token.luacmd("pdfextension", function(_, immediate)
local attr = scan_keyword'stream' and (scan_keyword'attr' and scan_string() or '') local attr = scan_keyword'stream' and (scan_keyword'attr' and scan_string() or '')
local isfile = scan_keyword'file' local isfile = scan_keyword'file'
local content = scan_string() local content = scan_string()
if immediate == 8 then if immediate == immediate_flag then
if attr then if attr then
pfile:stream(num, attr, content, isfile) pfile:stream(num, attr, content, isfile)
else else
@ -850,11 +854,10 @@ local lastimage = -1
local lastimagepages = -1 local lastimagepages = -1
-- These are very minimal right now but LaTeX isn't using the scaling etc. stuff anyway. -- These are very minimal right now but LaTeX isn't using the scaling etc. stuff anyway.
token.luacmd("saveimageresource", function(_, immediate) lmlt.luacmd("saveimageresource", function(_, immediate)
if immediate == "value" then return end if immediate == "value" then return end
if immediate and immediate & 0x7 ~= 0 then if immediate and immediate & ~immediate_flag ~= 0 then
print(immediate) immediate = immediate & immediate_flag
immediate = immediate & 0x8
tex.error("Unexpected prefix", "You used \\saveimageresource with a prefix that doesn't belong there. I will ignore it for now.") tex.error("Unexpected prefix", "You used \\saveimageresource with a prefix that doesn't belong there. I will ignore it for now.")
end end
local attr = scan_keyword'attr' and scan_string() or nil local attr = scan_keyword'attr' and scan_string() or nil
@ -880,24 +883,24 @@ token.luacmd("saveimageresource", function(_, immediate)
local pfile = get_pfile() local pfile = get_pfile()
lastimage = imglib.get_num(pfile, img) lastimage = imglib.get_num(pfile, img)
lastimagepages = img.pages or 1 lastimagepages = img.pages or 1
if immediate == 8 then if immediate == immediate_flag then
imglib_immediatewrite(pfile, img) imglib_immediatewrite(pfile, img)
end end
end, "value") end, "value")
token.luacmd("useimageresource", function() lmlt.luacmd("useimageresource", function()
local pfile = get_pfile() local pfile = get_pfile()
local img = assert(imglib.from_num(scan_int())) local img = assert(imglib.from_num(scan_int()))
imglib_write(pfile, img) imglib_write(pfile, img)
end, "protected") end, "protected")
local integer_code = token.value.integer local integer_code = lmlt.value.integer
token.luacmd("lastsavedimageresourceindex", function() lmlt.luacmd("lastsavedimageresourceindex", function()
return integer_code, lastimage return integer_code, lastimage
end, "value") end, "value")
token.luacmd("lastsavedimageresourcepages", function() lmlt.luacmd("lastsavedimageresourcepages", function()
return integer_code, lastimagepages return integer_code, lastimagepages
end, "value") end, "value")
@ -919,10 +922,10 @@ tex.useboxresource = savedbox.use
local lastbox = -1 local lastbox = -1
token.luacmd("saveboxresource", function(_, immediate) lmlt.luacmd("saveboxresource", function(_, immediate)
if immediate == "value" then return end if immediate == "value" then return end
if immediate and immediate & 0x7 ~= 0 then if immediate and immediate & ~immediate_flag ~= 0 then
immediate = immediate & 0x8 immediate = immediate & immediate_flag
tex.error("Unexpected prefix", "You used \\saveboxresource with a prefix that doesn't belong there. I will ignore it for now.") tex.error("Unexpected prefix", "You used \\saveboxresource with a prefix that doesn't belong there. I will ignore it for now.")
end end
local type local type
@ -935,11 +938,11 @@ token.luacmd("saveboxresource", function(_, immediate)
local margin = scan_keyword'margin' and scan_dimen() or nil local margin = scan_keyword'margin' and scan_dimen() or nil
local box = scan_int() local box = scan_int()
local index = tex.saveboxresource(box, attr, resources, immediate == 8, type, margin) local index = tex.saveboxresource(box, attr, resources, immediate == immediate_flag, type, margin)
lastbox = index lastbox = index
end, "value") end, "value")
token.luacmd("useboxresource", function() lmlt.luacmd("useboxresource", function()
local width, height, depth local width, height, depth
while true do while true do
if scan_keyword'width' then if scan_keyword'width' then
@ -956,7 +959,7 @@ token.luacmd("useboxresource", function()
node.write((tex.useboxresource(index, width, height, depth))) node.write((tex.useboxresource(index, width, height, depth)))
end, "protected") end, "protected")
token.luacmd("lastsavedboxresourceindex", function() lmlt.luacmd("lastsavedboxresourceindex", function()
return integer_code, lastbox return integer_code, lastbox
end, "value") end, "value")
@ -964,15 +967,15 @@ local saved_pos_x, saved_pos_y = -1, -1
local save_pos_whatsit = declare_whatsit('save_pos', function(_, _, _, x, y) local save_pos_whatsit = declare_whatsit('save_pos', function(_, _, _, x, y)
saved_pos_x, saved_pos_y = assert(math.tointeger(x)), assert(math.tointeger(y)) saved_pos_x, saved_pos_y = assert(math.tointeger(x)), assert(math.tointeger(y))
end) end)
token.luacmd("savepos", function() -- \savepos lmlt.luacmd("savepos", function() -- \savepos
return node.direct.write(node.direct.new(whatsit_id, save_pos_whatsit)) return node.direct.write(node.direct.new(whatsit_id, save_pos_whatsit))
end, "protected") end, "protected")
token.luacmd("lastxpos", function() lmlt.luacmd("lastxpos", function()
return integer_code, (saved_pos_x+.5)//1 return integer_code, (saved_pos_x+.5)//1
end, "value") end, "value")
token.luacmd("lastypos", function() lmlt.luacmd("lastypos", function()
return integer_code, (saved_pos_y+.5)//1 return integer_code, (saved_pos_y+.5)//1
end, "value") end, "value")

View File

@ -1,9 +1,11 @@
local lmlt = luametalatex
local scan_dimen = token.scan_dimen local scan_dimen = token.scan_dimen
local scan_int = token.scan_int local scan_int = token.scan_int
local scan_keyword = token.scan_keyword local scan_keyword = token.scan_keyword
local count_code = token.value.integer local count_code = lmlt.value.integer
local dimen_code = token.value.dimension local dimen_code = lmlt.value.dimension
local set_local = require'luametalatex-local' local set_local = require'luametalatex-local'
@ -26,7 +28,7 @@ function texmeta.__newindex(t, k, v)
end end
local function tex_variable(value, scanner, name, default) local function tex_variable(value, scanner, name, default)
token.luacmd(name, function(_, scanning) lmlt.luacmd(name, function(_, scanning)
if scanning == 'value' then if scanning == 'value' then
return value, tex_variables[name] return value, tex_variables[name]
else else
@ -95,7 +97,7 @@ end
local function pdf_variable(value, scanner, name, default, force_default) local function pdf_variable(value, scanner, name, default, force_default)
pdf_variable_names[#pdf_variable_names+1] = name pdf_variable_names[#pdf_variable_names+1] = name
token.luacmd('pdfvariable ' .. name, function(_, scanning) lmlt.luacmd('pdfvariable ' .. name, function(_, scanning)
if scanning == 'value' then if scanning == 'value' then
return value, real_pdf_variables[name] return value, real_pdf_variables[name]
elseif force_default then elseif force_default then

View File

@ -10,7 +10,7 @@ function tex.gettextdir() return tex.textdirection end
function tex.getlinedir() return tex.linedirection end function tex.getlinedir() return tex.linedirection end
function tex.getmathdir() return tex.mathdirection end function tex.getmathdir() return tex.mathdirection end
function tex.getpardir() return tex.pardirection end function tex.getpardir() return tex.pardirection end
local integer_code = token.value.integer local integer_code = luametalatex.value.integer
local function set_xdir(id, scanning) local function set_xdir(id, scanning)
if scanning == 'value' then if scanning == 'value' then
return integer_code, getters[id]() return integer_code, getters[id]()
@ -30,7 +30,7 @@ return function(name)
local getter = tex["get" .. name] local getter = tex["get" .. name]
local setter = tex["set" .. name] local setter = tex["set" .. name]
assert(getter and setter, "direction parameter undefined") assert(getter and setter, "direction parameter undefined")
local idx = token.luacmd(name, set_xdir, "protected", "global", "value") local idx = luametalatex.luacmd(name, set_xdir, "protected", "global", "value")
getters[idx] = getter getters[idx] = getter
setters[idx] = setter setters[idx] = setter
return idx return idx

View File

@ -1,3 +1,5 @@
local lmlt = luametalatex
local scan_int = token.scan_int local scan_int = token.scan_int
token.scan_int = scan_int -- For compatibility with LuaTeX packages token.scan_int = scan_int -- For compatibility with LuaTeX packages
local scan_token = token.scan_token local scan_token = token.scan_token
@ -61,6 +63,8 @@ if status.ini_version then
setcatcode('global', 2, 32, 10) setcatcode('global', 2, 32, 10)
end end
local immediate_flag = lmlt.flag.immediate
local l = lpeg or require'lpeg' local l = lpeg or require'lpeg'
local add_file_extension = l.Cs((1-('.' * (1-l.S'./\\')^0) * -1)^0 * (l.P(1)^1+l.Cc'.tex')) local add_file_extension = l.Cs((1-('.' * (1-l.S'./\\')^0) * -1)^0 * (l.P(1)^1+l.Cc'.tex'))
local ofiles, ifiles = {}, {} local ofiles, ifiles = {}, {}
@ -75,17 +79,17 @@ local function do_openout(p)
end end
end end
local open_whatsit = new_whatsit('open', do_openout) local open_whatsit = new_whatsit('open', do_openout)
token.luacmd("openout", function(_, immediate) -- \openout lmlt.luacmd("openout", function(_, immediate) -- \openout
if immediate == "value" then return end if immediate == "value" then return end
if immediate and immediate & ~0x2000 ~= 0 then if immediate and immediate & ~immediate_flag ~= 0 then
immediate = immediate & 0x2000 immediate = immediate & immediate_flag
tex.error("Unexpected prefix", "You used \\openout with a prefix that doesn't belong there. I will ignore it for now.") tex.error("Unexpected prefix", "You used \\openout with a prefix that doesn't belong there. I will ignore it for now.")
end end
local file = scan_int() local file = scan_int()
scan_keyword'=' scan_keyword'='
local name = scan_filename() local name = scan_filename()
local props = {file = file, name = name} local props = {file = file, name = name}
if immediate and immediate == 0x2000 then if immediate and immediate == immediate_flag then
do_openout(props) do_openout(props)
else else
local whatsit = node.direct.new(whatsit_id, open_whatsit) local whatsit = node.direct.new(whatsit_id, open_whatsit)
@ -93,7 +97,7 @@ token.luacmd("openout", function(_, immediate) -- \openout
node.direct.write(whatsit) node.direct.write(whatsit)
end end
end, "value") end, "value")
token.luacmd("openin", function(_, prefix) lmlt.luacmd("openin", function(_, prefix)
if prefix == "value" then return end if prefix == "value" then return end
local file = scan_int() local file = scan_int()
scan_keyword'=' scan_keyword'='
@ -112,15 +116,15 @@ local function do_closeout(p)
end end
end end
local close_whatsit = new_whatsit('close', do_closeout) local close_whatsit = new_whatsit('close', do_closeout)
token.luacmd("closeout", function(_, immediate) -- \closeout lmlt.luacmd("closeout", function(_, immediate) -- \closeout
if immediate == "value" then return end if immediate == "value" then return end
if immediate and immediate & ~0x2000 ~= 0 then if immediate and immediate & ~immediate_flag ~= 0 then
immediate = immediate & 0x2000 immediate = immediate & immediate_flag
tex.error("Unexpected prefix", "You used \\closeout with a prefix that doesn't belong there. I will ignore it for now.") tex.error("Unexpected prefix", "You used \\closeout with a prefix that doesn't belong there. I will ignore it for now.")
end end
local file = scan_int() local file = scan_int()
local props = {file = file} local props = {file = file}
if immediate == 0x2000 then if immediate == immediate_flag then
do_closeout(props) do_closeout(props)
else else
local whatsit = node.direct.new(whatsit_id, close_whatsit) local whatsit = node.direct.new(whatsit_id, close_whatsit)
@ -128,7 +132,7 @@ token.luacmd("closeout", function(_, immediate) -- \closeout
node.direct.write(whatsit) node.direct.write(whatsit)
end end
end, "value") end, "value")
token.luacmd("closein", function(_, prefix) lmlt.luacmd("closein", function(_, prefix)
if prefix == "value" then return end if prefix == "value" then return end
local file = scan_int() local file = scan_int()
if ifiles[file] then if ifiles[file] then
@ -148,16 +152,16 @@ local function do_write(p)
end end
end end
local write_whatsit = new_whatsit('write', do_write) local write_whatsit = new_whatsit('write', do_write)
token.luacmd("write", function(_, immediate, ...) -- \write lmlt.luacmd("write", function(_, immediate, ...) -- \write
if immediate == "value" then return end if immediate == "value" then return end
if immediate and immediate & ~0x2000 ~= 0 then if immediate and immediate & ~immediate_flag ~= 0 then
immediate = immediate & 0x2000 immediate = immediate & immediate_flag
tex.error("Unexpected prefix", "You used \\write with a prefix that doesn't belong there. I will ignore it for now.") tex.error("Unexpected prefix", "You used \\write with a prefix that doesn't belong there. I will ignore it for now.")
end end
local file = scan_int() local file = scan_int()
local content = scan_tokenlist() local content = scan_tokenlist()
local props = {file = file, data = content} local props = {file = file, data = content}
if immediate == 0x2000 then if immediate == immediate_flag then
do_write(props) do_write(props)
else else
local whatsit = node.direct.new(whatsit_id, write_whatsit) local whatsit = node.direct.new(whatsit_id, write_whatsit)
@ -176,13 +180,13 @@ local function prefix_to_tokens(prefix)
end end
end end
end end
local expand_after = token.primitive_tokens.expandafter local expand_after = lmlt.primitive_tokens.expandafter
local input_tok = token.primitive_tokens.input local input_tok = lmlt.primitive_tokens.input
local endlocalcontrol = token.primitive_tokens.endlocalcontrol local endlocalcontrol = lmlt.primitive_tokens.endlocalcontrol
local afterassignment = token.primitive_tokens.afterassignment local afterassignment = lmlt.primitive_tokens.afterassignment
local lbrace = token.new(0, 1) local lbrace = token.new(0, 1)
local rbrace = token.new(0, 2) local rbrace = token.new(0, 2)
token.luacmd("read", function(_, prefix) lmlt.luacmd("read", function(_, prefix)
if immediate == "value" then return end if immediate == "value" then return end
local id = scan_int() local id = scan_int()
if not scan_keyword'to' then if not scan_keyword'to' then
@ -224,12 +228,12 @@ token.luacmd("read", function(_, prefix)
tex.runlocal(function() tex.runlocal(function()
tokens[#tokens+1] = rbrace tokens[#tokens+1] = rbrace
token.put_next(tokens) token.put_next(tokens)
token.put_next(token.primitive_tokens.def, token.create(macro), lbrace) token.put_next(lmlt.primitive_tokens.def, token.create(macro), lbrace)
prefix_to_tokens(prefix) prefix_to_tokens(prefix)
end) end)
end, "value") end, "value")
token.luacmd("readline", function(_, prefix) lmlt.luacmd("readline", function(_, prefix)
if immediate == "value" then return end if immediate == "value" then return end
local id = scan_int() local id = scan_int()
if not scan_keyword'to' then if not scan_keyword'to' then
@ -266,7 +270,7 @@ local integer_code, boolean_code do
end end
end end
token.luacmd("ifeof", function(_) lmlt.luacmd("ifeof", function(_)
local id = scan_int() local id = scan_int()
return boolean_code, not ifiles[id] return boolean_code, not ifiles[id]
end, "condition") end, "condition")
@ -283,7 +287,7 @@ local late_lua_whatsit = new_whatsit('late_lua', function(p, pfile, n, x, y)
end end
return pdf._latelua(pfile, x, y, code) return pdf._latelua(pfile, x, y, code)
end) end)
token.luacmd("latelua", function() -- \latelua lmlt.luacmd("latelua", function() -- \latelua
local content = scan_tokenlist() local content = scan_tokenlist()
local props = {token = content} local props = {token = content}
local whatsit = node.direct.new(whatsit_id, late_lua_whatsit) local whatsit = node.direct.new(whatsit_id, late_lua_whatsit)
@ -298,7 +302,7 @@ require'luametalatex-baseregisters'
require'luametalatex-back-pdf' require'luametalatex-back-pdf'
require'luametalatex-node-luaotfload' require'luametalatex-node-luaotfload'
token.luacmd("Umathcodenum", function(_, scanning) lmlt.luacmd("Umathcodenum", function(_, scanning)
if scanning then if scanning then
local class, family, char = tex.getmathcodes (scan_int()) local class, family, char = tex.getmathcodes (scan_int())
return integer_code, char | (class | family << 3) << 21 return integer_code, char | (class | family << 3) << 21

View File

@ -1,3 +1,5 @@
luametalatex = luametalatex or {}
local lmlt = luametalatex
local initex = status.ini_version local initex = status.ini_version
if initex then if initex then
@ -34,13 +36,14 @@ do
primitives[prim[3]] = token_new(prim[2], prim[1]) primitives[prim[3]] = token_new(prim[2], prim[1])
end end
end end
token.primitive_tokens = primitives lmlt.primitive_tokens = primitives
do do
local command_id = swap_table(token.getcommandvalues()) local command_id = swap_table(token.getcommandvalues())
function token.command_id(name) return command_id[name] end function token.command_id(name) return command_id[name] end
end end
token.value = swap_table(token.getfunctionvalues()) lmlt.value = swap_table(token.getfunctionvalues())
lmlt.flag = swap_table(tex.getflagvalues())
local functions = lua.getfunctionstable() local functions = lua.getfunctionstable()
-- I am not sure why this is necessary, but otherwise LuaMetaTeX resets -- I am not sure why this is necessary, but otherwise LuaMetaTeX resets
@ -71,7 +74,7 @@ local lua_call_cmd = token.command_id'lua_call'
local lua_value_cmd = token.command_id'lua_value' local lua_value_cmd = token.command_id'lua_value'
local lua_protected_call_cmd = token.command_id'lua_protected_call' local lua_protected_call_cmd = token.command_id'lua_protected_call'
local if_test_cmd = token.command_id'if_test' local if_test_cmd = token.command_id'if_test'
function token.luacmd(name, func, ...) function lmlt.luacmd(name, func, ...)
local idx local idx
local tok = token.create(name) local tok = token.create(name)
local cmd = tok.command local cmd = tok.command

View File

@ -12,7 +12,7 @@ lua.get_functions_table()[restore_func] = function()
end end
end end
end end
local restore_toks = {token.primitive_tokens.atendofgroup , token.new(restore_func, token.command_id'lua_call')} local restore_toks = {luametalatex.primitive_tokens.atendofgroup , token.new(restore_func, token.command_id'lua_call')}
local put_next = token.put_next local put_next = token.put_next
local runlocal = tex.runlocal local runlocal = tex.runlocal
local function put_restore_toks() local function put_restore_toks()

View File

@ -4,12 +4,13 @@
-- Necessary especially for etoolbox's patching commands -- Necessary especially for etoolbox's patching commands
local lmlt = luametalatex
local primitive_meaning = { local primitive_meaning = {
token.primitive_tokens.expandafter, lmlt.primitive_tokens.expandafter,
token.primitive_tokens.relax, lmlt.primitive_tokens.relax,
token.primitive_tokens.meaning, lmlt.primitive_tokens.meaning,
} }
token.luacmd("meaning", function() lmlt.luacmd("meaning", function()
local peeked = token.peeknext() local peeked = token.peeknext()
token.put_next(primitive_meaning) token.put_next(primitive_meaning)
token.skipnextexpanded() token.skipnextexpanded()