\input does not look at open_read_file callback?
Hi, in the following code, the callback is never reached when called with luatex -ini bug -- David Kastrup
David Kastrup wrote:
Hi,
in the following code, the callback is never reached when called with luatex -ini bug
Thanks, I will fix this in the next snapshot (tomorrow or thursday). If you need a quick workaround, add this: \directlua){ callback.register("find_read_file", function(id,filename) return kpse.find_file(filename) end ) }
David Kastrup wrote:
Hi,
in the following code, the callback is never reached when called with luatex -ini bug
------------------------------------------------------------------------
\catcode`{=1 \catcode`}=2 \directlua0{callback.register("open_read_file",function(filename) print "opening file name";return end)} \input plain.tex
hm, works ok here opening file name ! I can't find file `plain.tex'. l.13 \input plain.tex you return nothing, while you need to return a table with at least a reader function that luatex can call for each line it wants callback.register('open_read_file', function(filename) print("opening file name " .. filename) local f = io.open(filename) if f then return { reader = function() return f:read() end, close = function() f:close() end } else return nil end end) readers can read from file and do with the input what they want before they pass it onto tex (btw, the return format is mentioned in the manual) Hans ----------------------------------------------------------------- Hans Hagen | PRAGMA ADE Ridderstraat 27 | 8061 GH Hasselt | The Netherlands tel: 038 477 53 69 | fax: 038 477 53 74 | www.pragma-ade.com | www.pragma-pod.nl -----------------------------------------------------------------
Hans Hagen
David Kastrup wrote:
in the following code, the callback is never reached when called with luatex -ini bug
------------------------------------------------------------------------
\catcode`{=1 \catcode`}=2 \directlua0{callback.register("open_read_file",function(filename) print "opening file name";return end)} \input plain.tex
hm, works ok here
I don't see that.
opening file name ! I can't find file `plain.tex'. l.13 \input plain.tex
So you don't even get to the stage where the callback open_read_file would get called, regardless of whether the callback works or not. Try it with an existing file (according to LuaTeX's notion of "existing").
you return nothing, while you need to return a table with at least a reader function that luatex can call for each line it wants
I am aware of that. The above code is not intended to work, but rather to produce a noticeable message (of course, this is a minimal example: the actual use case did something more useful), possibly bombing out afterwards. It does not do that: the callback is never reached in the first place.
(btw, the return format is mentioned in the manual)
I am aware of that. My actual code is quite more complex, but I did not want to burden the list with a complicated example they need to figure out. This was a minimal example demonstrating that the callback is never reached. That it does not return anything useful is irrelevant since it never gets to returning anything in the first place. Once the callback actually gets called, the above program will likely break in some manner. That is expected. See Taco's answer. -- David Kastrup
David Kastrup
Hans Hagen
writes: David Kastrup wrote:
in the following code, the callback is never reached when called with luatex -ini bug
you return nothing, while you need to return a table with at least a reader function that luatex can call for each line it wants
I am aware of that. The above code is not intended to work, but rather to produce a noticeable message (of course, this is a minimal example: the actual use case did something more useful), possibly bombing out afterwards.
It does not do that: the callback is never reached in the first place.
For what it is worth: here is the actual code I use. Adding the callback for find_read_file (as Taco suggested as a workaround) makes this work as expected. Without adding the callback, it does not work, contrary to the documentation. \begingroup \catcode `\{1 \catcode `\}2 \catcode `\@11 \catcode `\#=6 \catcode `\%=14 \gdef\lu@@inpencsetup{% \directlua0{ inputenc={auto = function(line) return line end; bytes = function(line) return unicode.utf8.char(unicode.latin1.byte(line,0,-1)) end }; callback.register("find_read_file", function(id,filename) return kpse.find_file(filename, "tex", true) end); callback.register("open_read_file", function(filename) local file = io.open(filename, "rb"); return {file = file; inputenc = defaultinputenc; reader = function(table) if localinputenc then table.inputenc = localinputenc; localinputenc = nil end; local line = table.file:read(); if line then return table.inputenc(line) else return nil end end; close = function(table) table.file:close() end} end)}} \lu@@inpencsetup \protected\gdef\XeTeXinputencoding"#1"{\directlua0{ localinputenc=inputenc["#1"]}} \protected\gdef\XeTeXdefaultencoding"#1"{\directlua0{ defaultinputenc=inputenc["#1"]}} \global\let\lu@@dump\dump \protected\gdef\dump{\everyjob \expandafter{\the\everyjob \lu@@inpencsetup\XeTeXdefaultencoding "bytes"} \lu@@dump} \endgroup \XeTeXdefaultencoding "bytes" \input latex.ltx -- David Kastrup
participants (3)
-
David Kastrup
-
Hans Hagen
-
Taco Hoekwater