diff --git a/luametalatex-back-pdf.lua b/luametalatex-back-pdf.lua index aa72599..cd3f6e0 100644 --- a/luametalatex-back-pdf.lua +++ b/luametalatex-back-pdf.lua @@ -51,10 +51,15 @@ local colorstacks = {{ page_stack = {"0 g 0 G"}, }} local spacer_cmd = token.command_id'spacer' +local output_directory = arg['output-directory'] +local dir_sep = '/' -- FIXME local function get_pfile() if not pfile then pdfname = tex.jobname .. '.pdf' - pfile = newpdf.open(tex.jobname .. '.pdf') + if output_directory then + pdfname = output_directory .. dir_sep .. pdfname + end + pfile = newpdf.open(pdfname) end return pfile end diff --git a/luametalatex-basecallbacks.lua b/luametalatex-basecallbacks.lua index d298131..dea0ce8 100644 --- a/luametalatex-basecallbacks.lua +++ b/luametalatex-basecallbacks.lua @@ -4,6 +4,27 @@ local read_tfm = font.read_tfm local font_define = font.define local callbacks = require'luametalatex-callbacks' +local find_file = kpse.find_file +local output_directory = arg['output-directory'] + +local dir_sep = '/' -- FIXME + +if output_directory then + local old_find_file = find_file + local kpse_absolute = kpse.is_absolute + local attributes = lfs.attributes + function find_file(path, kind, must_exists) + --kind is always "tex" + if not kpse_absolute(path) then + local new_path = output_directory .. dir_sep .. path + if attributes(new_path, 'mode') == 'file' then + return new_path + end + end + return old_find_file(path, kind, must_exists) + end +end + if status.ini_version then function callbacks.define_font(name, size) local f = read_tfm(name, size) @@ -26,7 +47,11 @@ else end callbacks.__freeze'define_font' -function callbacks.find_log_file(name) return name end +if output_directory then + function callbacks.find_log_file(name) return output_directory .. dir_sep .. name end +else + function callbacks.find_log_file(name) return name end +end callbacks.__freeze'find_log_file' -- find_data_file is not an engine callback in luametatex, so we don't __freeze it @@ -38,11 +63,11 @@ if status.ini_version then if name == 'expl3.ltx' then name = 'luametalatex-ltexpl-hook' end - return kpse.find_file(name, 'tex', true) + return find_file(name, 'tex', true) end end local function normal_find_data_file(name) - return kpse.find_file(name, 'tex', true) + return find_file(name, 'tex', true) end function callbacks.open_data_file(name) local find_callback = callbacks.find_data_file @@ -50,7 +75,7 @@ function callbacks.open_data_file(name) if find_callback then path = find_callback(name) else - path = kpse.find_file(name, 'tex', true) + path = find_file(name, 'tex', true) end if not path then return end diff --git a/luametalatex-firstcode.lua b/luametalatex-firstcode.lua index 39a07ba..77186de 100644 --- a/luametalatex-firstcode.lua +++ b/luametalatex-firstcode.lua @@ -55,7 +55,6 @@ do end end - local new_whatsit = require'luametalatex-whatsits'.new local whatsit_id = node.id'whatsit' local spacer_cmd, relax_cmd = token.command_id'spacer', token.command_id'relax' @@ -98,12 +97,18 @@ local immediate_flag = lmlt.flag.immediate 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 = {}, {} +local output_directory = arg['output-directory'] +local dir_sep = '/' -- FIXME 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') + local name = add_file_extension:match(p.name) + if output_directory then + name = output_directory .. dir_sep .. name + end + ofiles[p.file], msg = io.open(name, 'w') if not ofiles[p.file] then error(msg) end diff --git a/luametalatex-lateinit.lua b/luametalatex-lateinit.lua index 2d494a1..cab017e 100644 --- a/luametalatex-lateinit.lua +++ b/luametalatex-lateinit.lua @@ -104,6 +104,7 @@ end if initex then local build_bytecode = nil -- To be filled + local output_directory = arg['output-directory'] function callbacks.pre_dump() local user_callback = callbacks.pre_dump if user_callback then user_callback() end @@ -144,6 +145,9 @@ if initex then end end lua.bytecode[tex.count[262]+1] = build_bytecode(table.concat(prepared, '\n')) + if output_directory then + lfs.chdir(output_directory) -- We can't change the location TeX writes it's format to, so we change the current directory instead + end end callbacks.__freeze('pre_dump', true) return function(f) diff --git a/luametalatex.so b/luametalatex.so index 79cf863..ec1a711 100755 Binary files a/luametalatex.so and b/luametalatex.so differ