2021-11-06 13:23:29 +01:00
|
|
|
local lmlt = luametalatex
|
|
|
|
|
2021-11-06 11:38:39 +01:00
|
|
|
local scan_int = token.scan_int
|
2021-03-20 19:00:02 +01:00
|
|
|
token.scan_int = scan_int -- For compatibility with LuaTeX packages
|
2020-08-13 22:24:13 +02:00
|
|
|
local scan_token = token.scan_token
|
2021-11-06 11:38:39 +01:00
|
|
|
local scan_tokenlist = token.scantokenlist
|
2020-08-13 22:24:13 +02:00
|
|
|
local scan_keyword = token.scan_keyword
|
|
|
|
local scan_csname = token.scan_csname
|
|
|
|
local set_macro = token.set_macro
|
|
|
|
|
|
|
|
local callback_find = callback.find
|
|
|
|
|
2021-03-20 19:00:02 +01:00
|
|
|
local constants = status.getconstants()
|
|
|
|
|
2020-05-28 14:37:19 +02:00
|
|
|
local lua_call_cmd = token.command_id'lua_call'
|
2020-01-02 04:14:39 +01:00
|
|
|
local properties = node.direct.get_properties_table()
|
2020-05-31 09:30:49 +02:00
|
|
|
node.direct.properties = properties
|
2020-06-02 01:22:59 +02:00
|
|
|
function node.direct.get_properties_table()
|
|
|
|
return properties
|
|
|
|
end
|
2019-07-17 21:14:34 +02:00
|
|
|
-- setmetatable(node.direct.get_properties_table(), {
|
|
|
|
-- __index = function(t, id)
|
|
|
|
-- local new = {}
|
|
|
|
-- t[id] = new
|
|
|
|
-- return new
|
|
|
|
-- end
|
|
|
|
-- })
|
|
|
|
|
2021-11-07 00:06:27 +01:00
|
|
|
do
|
|
|
|
luametalatex.texio = texio
|
|
|
|
local compat_texio = {}
|
|
|
|
for k,v in next, texio do compat_texio[k] = v end
|
|
|
|
local writenl = texio.writenl
|
|
|
|
local write = texio.write
|
|
|
|
texio = compat_texio
|
|
|
|
function texio.write(selector, ...)
|
|
|
|
if selector == 'term' then
|
|
|
|
selector = 'terminal'
|
|
|
|
elseif selector == 'log' then
|
|
|
|
selector = 'logfile'
|
|
|
|
elseif selector == 'term and log' then
|
|
|
|
selector = 'terminal_and_logfile'
|
|
|
|
end
|
|
|
|
return write(selector, ...)
|
|
|
|
end
|
|
|
|
function texio.write_nl(selector, ...)
|
|
|
|
if selector == 'term' then
|
|
|
|
selector = 'terminal'
|
|
|
|
elseif selector == 'log' then
|
|
|
|
selector = 'logfile'
|
|
|
|
elseif selector == 'term and log' then
|
|
|
|
selector = 'terminal_and_logfile'
|
|
|
|
end
|
|
|
|
return writenl(selector, ...)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2020-07-01 19:47:25 +02:00
|
|
|
local new_whatsit = require'luametalatex-whatsits'.new
|
2019-07-17 21:14:34 +02:00
|
|
|
local whatsit_id = node.id'whatsit'
|
|
|
|
local spacer_cmd, relax_cmd = token.command_id'spacer', token.command_id'relax'
|
|
|
|
local function scan_filename()
|
|
|
|
local name = {}
|
|
|
|
local quoted = false
|
|
|
|
local tok, cmd
|
|
|
|
repeat
|
2020-08-13 22:24:13 +02:00
|
|
|
tok = scan_token()
|
2019-07-17 21:14:34 +02:00
|
|
|
cmd = tok.command
|
|
|
|
until cmd ~= spacer_cmd and cmd ~= relax_cmd
|
2021-03-20 19:00:02 +01:00
|
|
|
while (tok.command <= 12 and tok.command > 0 and tok.command ~= 9 and tok.index <= constants.max_character_code
|
2019-07-17 21:14:34 +02:00
|
|
|
or (token.put_next(tok) and false))
|
2020-07-07 16:52:06 +02:00
|
|
|
and (quoted or tok.index ~= string.byte' ') do
|
|
|
|
if tok.index == string.byte'"' then
|
2019-07-17 21:14:34 +02:00
|
|
|
quoted = not quoted
|
|
|
|
else
|
2020-07-07 16:52:06 +02:00
|
|
|
name[#name+1] = tok.index
|
2019-07-17 21:14:34 +02:00
|
|
|
end
|
2020-08-13 22:24:13 +02:00
|
|
|
tok = scan_token()
|
2019-07-17 21:14:34 +02:00
|
|
|
end
|
|
|
|
return utf8.char(table.unpack(name))
|
|
|
|
end
|
|
|
|
|
2020-08-13 22:24:13 +02:00
|
|
|
-- These are chosen to coincide with ltluatex's default catcodetables.
|
|
|
|
-- In expl-hook, we check that the values are as expected.
|
|
|
|
local initex_catcodetable = 1
|
|
|
|
local string_catcodetable = 2
|
|
|
|
if status.ini_version then
|
2021-03-20 19:00:02 +01:00
|
|
|
tex.runlocal(function()tex.sprint[[\initcatcodetable 1\initcatcodetable 2]]end)
|
2020-08-13 22:24:13 +02:00
|
|
|
local setcatcode = tex.setcatcode
|
|
|
|
for i=0,127 do
|
|
|
|
setcatcode('global', 2, i, 12)
|
|
|
|
end
|
|
|
|
setcatcode('global', 2, 32, 10)
|
|
|
|
end
|
|
|
|
|
2021-11-06 13:23:29 +01:00
|
|
|
local immediate_flag = lmlt.flag.immediate
|
|
|
|
|
2020-07-08 16:50:45 +02:00
|
|
|
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'))
|
2020-08-13 22:24:13 +02:00
|
|
|
local ofiles, ifiles = {}, {}
|
2019-07-17 21:14:34 +02:00
|
|
|
local function do_openout(p)
|
|
|
|
if ofiles[p.file] then
|
2020-08-13 22:24:13 +02:00
|
|
|
ofiles[p.file]:close()
|
|
|
|
end
|
|
|
|
local msg
|
|
|
|
ofiles[p.file], msg = io.open(add_file_extension:match(p.name), 'w')
|
|
|
|
if not ofiles[p.file] then
|
|
|
|
error(msg)
|
2019-07-17 21:14:34 +02:00
|
|
|
end
|
|
|
|
end
|
2020-07-01 19:47:25 +02:00
|
|
|
local open_whatsit = new_whatsit('open', do_openout)
|
2021-11-06 13:23:29 +01:00
|
|
|
lmlt.luacmd("openout", function(_, immediate) -- \openout
|
2020-08-13 22:24:13 +02:00
|
|
|
if immediate == "value" then return end
|
2021-11-06 13:23:29 +01:00
|
|
|
if immediate and immediate & ~immediate_flag ~= 0 then
|
|
|
|
immediate = immediate & immediate_flag
|
2020-08-13 22:24:13 +02:00
|
|
|
tex.error("Unexpected prefix", "You used \\openout with a prefix that doesn't belong there. I will ignore it for now.")
|
|
|
|
end
|
|
|
|
local file = scan_int()
|
|
|
|
scan_keyword'='
|
2019-07-17 21:14:34 +02:00
|
|
|
local name = scan_filename()
|
2020-07-01 19:47:25 +02:00
|
|
|
local props = {file = file, name = name}
|
2021-11-06 13:23:29 +01:00
|
|
|
if immediate and immediate == immediate_flag then
|
2019-07-17 21:14:34 +02:00
|
|
|
do_openout(props)
|
|
|
|
else
|
2020-07-01 19:47:25 +02:00
|
|
|
local whatsit = node.direct.new(whatsit_id, open_whatsit)
|
2019-07-17 21:14:34 +02:00
|
|
|
properties[whatsit] = props
|
2020-01-02 04:14:39 +01:00
|
|
|
node.direct.write(whatsit)
|
2019-07-17 21:14:34 +02:00
|
|
|
end
|
2020-08-13 22:24:13 +02:00
|
|
|
end, "value")
|
2021-11-06 13:23:29 +01:00
|
|
|
lmlt.luacmd("openin", function(_, prefix)
|
2020-08-13 22:24:13 +02:00
|
|
|
if prefix == "value" then return end
|
|
|
|
local file = scan_int()
|
|
|
|
scan_keyword'='
|
|
|
|
local name = scan_filename()
|
|
|
|
if ifiles[file] then
|
|
|
|
ifiles[file]:close()
|
|
|
|
end
|
|
|
|
local msg
|
|
|
|
ifiles[file] = callback_find('open_data_file', true)(name) -- raw to pick up our wrapper which handles defaults and finding the file
|
|
|
|
end, "value")
|
|
|
|
|
2019-07-17 21:14:34 +02:00
|
|
|
local function do_closeout(p)
|
|
|
|
if ofiles[p.file] then
|
|
|
|
ofiles[p.file]:close()
|
|
|
|
ofiles[p.file] = nil
|
|
|
|
end
|
|
|
|
end
|
2020-07-01 19:47:25 +02:00
|
|
|
local close_whatsit = new_whatsit('close', do_closeout)
|
2021-11-06 13:23:29 +01:00
|
|
|
lmlt.luacmd("closeout", function(_, immediate) -- \closeout
|
2020-08-13 22:24:13 +02:00
|
|
|
if immediate == "value" then return end
|
2021-11-06 13:23:29 +01:00
|
|
|
if immediate and immediate & ~immediate_flag ~= 0 then
|
|
|
|
immediate = immediate & immediate_flag
|
2020-08-13 22:24:13 +02:00
|
|
|
tex.error("Unexpected prefix", "You used \\closeout with a prefix that doesn't belong there. I will ignore it for now.")
|
|
|
|
end
|
|
|
|
local file = scan_int()
|
2020-07-01 19:47:25 +02:00
|
|
|
local props = {file = file}
|
2021-11-06 13:23:29 +01:00
|
|
|
if immediate == immediate_flag then
|
2019-07-17 21:14:34 +02:00
|
|
|
do_closeout(props)
|
|
|
|
else
|
2020-07-01 19:47:25 +02:00
|
|
|
local whatsit = node.direct.new(whatsit_id, close_whatsit)
|
2019-07-17 21:14:34 +02:00
|
|
|
properties[whatsit] = props
|
2020-01-02 04:14:39 +01:00
|
|
|
node.direct.write(whatsit)
|
2019-07-17 21:14:34 +02:00
|
|
|
end
|
2020-08-13 22:24:13 +02:00
|
|
|
end, "value")
|
2021-11-06 13:23:29 +01:00
|
|
|
lmlt.luacmd("closein", function(_, prefix)
|
2020-08-13 22:24:13 +02:00
|
|
|
if prefix == "value" then return end
|
|
|
|
local file = scan_int()
|
|
|
|
if ifiles[file] then
|
|
|
|
ifiles[file]:close()
|
|
|
|
ifiles[file] = nil
|
|
|
|
end
|
|
|
|
end, "value")
|
|
|
|
|
2019-07-17 21:14:34 +02:00
|
|
|
local function do_write(p)
|
2021-03-20 19:00:02 +01:00
|
|
|
local data = token.serialize(p.data)
|
2020-08-25 00:20:23 +02:00
|
|
|
local content = data and data .. '\n' or '\n'
|
2019-07-17 21:14:34 +02:00
|
|
|
local file = ofiles[p.file]
|
|
|
|
if file then
|
|
|
|
file:write(content)
|
|
|
|
else
|
|
|
|
texio.write_nl(p.file < 0 and "log" or "term and log", content)
|
|
|
|
end
|
|
|
|
end
|
2020-07-01 19:47:25 +02:00
|
|
|
local write_whatsit = new_whatsit('write', do_write)
|
2021-11-06 13:23:29 +01:00
|
|
|
lmlt.luacmd("write", function(_, immediate, ...) -- \write
|
2020-08-13 22:24:13 +02:00
|
|
|
if immediate == "value" then return end
|
2021-11-06 13:23:29 +01:00
|
|
|
if immediate and immediate & ~immediate_flag ~= 0 then
|
|
|
|
immediate = immediate & immediate_flag
|
2020-08-13 22:24:13 +02:00
|
|
|
tex.error("Unexpected prefix", "You used \\write with a prefix that doesn't belong there. I will ignore it for now.")
|
|
|
|
end
|
|
|
|
local file = scan_int()
|
|
|
|
local content = scan_tokenlist()
|
2020-07-01 19:47:25 +02:00
|
|
|
local props = {file = file, data = content}
|
2021-11-06 13:23:29 +01:00
|
|
|
if immediate == immediate_flag then
|
2019-07-17 21:14:34 +02:00
|
|
|
do_write(props)
|
|
|
|
else
|
2020-07-01 19:47:25 +02:00
|
|
|
local whatsit = node.direct.new(whatsit_id, write_whatsit)
|
2019-07-17 21:14:34 +02:00
|
|
|
properties[whatsit] = props
|
2020-01-02 04:14:39 +01:00
|
|
|
node.direct.write(whatsit)
|
2019-07-17 21:14:34 +02:00
|
|
|
end
|
2020-08-13 22:24:13 +02:00
|
|
|
end, "value")
|
|
|
|
|
|
|
|
local undefined_tok = token.new(0, token.command_id'undefined_cs')
|
|
|
|
local prefix_cmd = token.command_id'prefix'
|
|
|
|
local function prefix_to_tokens(prefix)
|
|
|
|
if not prefix then return end
|
|
|
|
for i=2, 0, -1 do
|
|
|
|
if prefix & (1<<i) ~= 0 then
|
|
|
|
token.put_next(token.new(i, prefix_cmd))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2021-11-06 13:23:29 +01:00
|
|
|
local expand_after = lmlt.primitive_tokens.expandafter
|
|
|
|
local input_tok = lmlt.primitive_tokens.input
|
|
|
|
local endlocalcontrol = lmlt.primitive_tokens.endlocalcontrol
|
|
|
|
local afterassignment = lmlt.primitive_tokens.afterassignment
|
2020-08-13 22:24:13 +02:00
|
|
|
local lbrace = token.new(0, 1)
|
|
|
|
local rbrace = token.new(0, 2)
|
2021-11-06 13:23:29 +01:00
|
|
|
lmlt.luacmd("read", function(_, prefix)
|
2020-08-13 22:24:13 +02:00
|
|
|
if immediate == "value" then return end
|
|
|
|
local id = scan_int()
|
|
|
|
if not scan_keyword'to' then
|
|
|
|
tex.error("Missing `to' inserted", "You should have said `\\read<number> to \\cs'.\nI'm going to look for the \\cs now.")
|
|
|
|
end
|
|
|
|
local macro = scan_csname(true)
|
|
|
|
local file = ifiles[id]
|
|
|
|
local tokens = {}
|
|
|
|
local balance = 0
|
2021-04-16 10:15:58 +02:00
|
|
|
repeat
|
|
|
|
local line
|
|
|
|
if file then
|
|
|
|
line = file:reader()
|
|
|
|
if not line then
|
|
|
|
file:close()
|
|
|
|
ifiles[id] = nil
|
|
|
|
end
|
|
|
|
else
|
|
|
|
line = io.stdin:read()
|
2020-08-13 22:24:13 +02:00
|
|
|
end
|
2021-04-16 10:15:58 +02:00
|
|
|
local endlocal
|
|
|
|
tex.runlocal(function()
|
2021-11-06 11:38:39 +01:00
|
|
|
endlocal = token.get_next()
|
2021-04-16 10:15:58 +02:00
|
|
|
tex.sprint(endlocal)
|
|
|
|
tex.print(line and line ~= "" and line or " ")
|
|
|
|
tex.print(endlocal)
|
|
|
|
end)
|
|
|
|
while true do
|
2021-11-06 11:38:39 +01:00
|
|
|
local tok = token.get_next()
|
2021-04-16 10:15:58 +02:00
|
|
|
if tok == endlocal then break end
|
|
|
|
if tok.command == 1 then
|
|
|
|
balance = balance + 1
|
|
|
|
elseif tok.command == 2 then
|
|
|
|
balance = balance - 1
|
|
|
|
end
|
|
|
|
tokens[#tokens+1] = tok
|
|
|
|
end
|
|
|
|
until balance == 0
|
2021-03-20 19:00:02 +01:00
|
|
|
tex.runlocal(function()
|
2020-08-13 22:24:13 +02:00
|
|
|
tokens[#tokens+1] = rbrace
|
|
|
|
token.put_next(tokens)
|
2021-11-06 13:23:29 +01:00
|
|
|
token.put_next(lmlt.primitive_tokens.def, token.create(macro), lbrace)
|
2020-08-13 22:24:13 +02:00
|
|
|
prefix_to_tokens(prefix)
|
|
|
|
end)
|
|
|
|
end, "value")
|
|
|
|
|
2021-11-06 13:23:29 +01:00
|
|
|
lmlt.luacmd("readline", function(_, prefix)
|
2020-08-13 22:24:13 +02:00
|
|
|
if immediate == "value" then return end
|
|
|
|
local id = scan_int()
|
|
|
|
if not scan_keyword'to' then
|
|
|
|
tex.error("Missing `to' inserted", "You should have said `\\read<number> to \\cs'.\nI'm going to look for the \\cs now.")
|
|
|
|
end
|
|
|
|
local macro = scan_csname(true)
|
|
|
|
local file = ifiles[id]
|
|
|
|
local line
|
|
|
|
if file then
|
|
|
|
line = file:reader()
|
|
|
|
if not line then
|
|
|
|
file:close()
|
|
|
|
ifiles[id] = nil
|
|
|
|
end
|
|
|
|
else
|
|
|
|
error[[FIXME: Ask the user for input]]
|
|
|
|
end
|
|
|
|
line = line and line:match"^(.*[^ ])[ ]*$"
|
|
|
|
local endlinechar = tex.endlinechar
|
|
|
|
if endlinechar >= 0 and endlinechar < 0x80 then
|
|
|
|
line = (line or '') .. string.char(endlinechar)
|
|
|
|
end
|
|
|
|
set_macro(string_catcodetable, macro, line or '', prefix)
|
|
|
|
end, "value")
|
|
|
|
|
|
|
|
local integer_code, boolean_code do
|
2021-03-20 19:00:02 +01:00
|
|
|
local value_values = token.getfunctionvalues()
|
2020-08-13 22:24:13 +02:00
|
|
|
for i=0,#value_values do
|
|
|
|
if value_values[i] == "integer" then
|
|
|
|
integer_code = i
|
|
|
|
elseif value_values[i] == "boolean" then
|
|
|
|
boolean_code = i
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-11-06 13:23:29 +01:00
|
|
|
lmlt.luacmd("ifeof", function(_)
|
2020-08-13 22:24:13 +02:00
|
|
|
local id = scan_int()
|
|
|
|
return boolean_code, not ifiles[id]
|
|
|
|
end, "condition")
|
|
|
|
|
2020-07-12 19:56:50 +02:00
|
|
|
local late_lua_whatsit = new_whatsit('late_lua', function(p, pfile, n, x, y)
|
|
|
|
local code = p.data
|
|
|
|
if not code then
|
2021-03-20 19:00:02 +01:00
|
|
|
code = token.serialize(p.token)
|
2020-07-12 19:56:50 +02:00
|
|
|
end
|
|
|
|
if type(code) == 'string' then
|
|
|
|
code = assert(load(code, nil, 't'))
|
|
|
|
elseif not code then
|
|
|
|
error[[Missing code in latelua]]
|
|
|
|
end
|
|
|
|
return pdf._latelua(pfile, x, y, code)
|
|
|
|
end)
|
2021-11-06 13:23:29 +01:00
|
|
|
lmlt.luacmd("latelua", function() -- \latelua
|
2020-08-13 22:24:13 +02:00
|
|
|
local content = scan_tokenlist()
|
2020-07-12 19:56:50 +02:00
|
|
|
local props = {token = content}
|
|
|
|
local whatsit = node.direct.new(whatsit_id, late_lua_whatsit)
|
|
|
|
properties[whatsit] = props
|
|
|
|
node.direct.write(whatsit)
|
|
|
|
end, "protected")
|
2020-05-26 19:29:58 +02:00
|
|
|
|
2020-07-04 06:27:30 +02:00
|
|
|
local functions = lua.get_functions_table()
|
2020-07-09 17:32:42 +02:00
|
|
|
|
2021-03-20 19:00:02 +01:00
|
|
|
require'luametalatex-meaning'
|
2020-06-28 04:52:06 +02:00
|
|
|
require'luametalatex-baseregisters'
|
2019-07-17 21:14:34 +02:00
|
|
|
require'luametalatex-back-pdf'
|
2020-05-31 09:30:49 +02:00
|
|
|
require'luametalatex-node-luaotfload'
|
|
|
|
|
2021-11-06 13:23:29 +01:00
|
|
|
lmlt.luacmd("Umathcodenum", function(_, scanning)
|
2020-05-31 09:30:49 +02:00
|
|
|
if scanning then
|
2020-08-13 22:24:13 +02:00
|
|
|
local class, family, char = tex.getmathcodes (scan_int())
|
2020-05-31 09:30:49 +02:00
|
|
|
return integer_code, char | (class | family << 3) << 21
|
|
|
|
else
|
2020-08-13 22:24:13 +02:00
|
|
|
local char = scan_int()
|
|
|
|
local mathcode = scan_int()
|
2020-05-31 09:30:49 +02:00
|
|
|
tex.setmathcodes(char, (mathcode >> 21) & 7, mathcode >> 24, mathcode & 0x1FFFFF)
|
|
|
|
end
|
|
|
|
end, "force", "global", "value")
|
2020-08-13 22:24:13 +02:00
|
|
|
|
|
|
|
-- This is effectivly the last line before we hand over to normal TeX.
|
|
|
|
require'luametalatex-callbacks'.__freeze = nil
|