luametalatex/luametalatex-firstcode.lua

348 lines
10 KiB
Lua
Raw Normal View History

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
local scan_token = token.scan_token
2021-11-06 11:38:39 +01:00
local scan_tokenlist = token.scantokenlist
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
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
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
tok = scan_token()
2019-07-17 21:14:34 +02:00
end
return utf8.char(table.unpack(name))
end
-- 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)
local setcatcode = tex.setcatcode
for i=0,127 do
setcatcode('global', 2, i, 12)
end
setcatcode('global', 2, 32, 10)
end
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'))
local ofiles, ifiles = {}, {}
2019-07-17 21:14:34 +02:00
local function do_openout(p)
if ofiles[p.file] then
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
local open_whatsit = new_whatsit('open', do_openout)
lmlt.luacmd("openout", function(_, immediate) -- \openout
if immediate == "value" then return end
if immediate and immediate & ~immediate_flag ~= 0 then
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.")
end
local file = scan_int()
scan_keyword'='
2019-07-17 21:14:34 +02:00
local name = scan_filename()
local props = {file = file, name = name}
if immediate and immediate == immediate_flag then
2019-07-17 21:14:34 +02:00
do_openout(props)
else
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
end, "value")
lmlt.luacmd("openin", function(_, prefix)
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
local close_whatsit = new_whatsit('close', do_closeout)
lmlt.luacmd("closeout", function(_, immediate) -- \closeout
if immediate == "value" then return end
if immediate and immediate & ~immediate_flag ~= 0 then
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.")
end
local file = scan_int()
local props = {file = file}
if immediate == immediate_flag then
2019-07-17 21:14:34 +02:00
do_closeout(props)
else
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
end, "value")
lmlt.luacmd("closein", function(_, prefix)
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)
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
local write_whatsit = new_whatsit('write', do_write)
lmlt.luacmd("write", function(_, immediate, ...) -- \write
if immediate == "value" then return end
if immediate and immediate & ~immediate_flag ~= 0 then
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.")
end
local file = scan_int()
local content = scan_tokenlist()
local props = {file = file, data = content}
if immediate == immediate_flag then
2019-07-17 21:14:34 +02:00
do_write(props)
else
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
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
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
local lbrace = token.new(0, 1)
local rbrace = token.new(0, 2)
lmlt.luacmd("read", function(_, prefix)
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()
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()
tokens[#tokens+1] = rbrace
token.put_next(tokens)
token.put_next(lmlt.primitive_tokens.def, token.create(macro), lbrace)
prefix_to_tokens(prefix)
end)
end, "value")
lmlt.luacmd("readline", function(_, prefix)
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()
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
lmlt.luacmd("ifeof", function(_)
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)
lmlt.luacmd("latelua", function() -- \latelua
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'
lmlt.luacmd("Umathcodenum", function(_, scanning)
2020-05-31 09:30:49 +02:00
if scanning then
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
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")
-- This is effectivly the last line before we hand over to normal TeX.
require'luametalatex-callbacks'.__freeze = nil