[luatex] Using luarepl with lualatex - how do I get lua5.1's behavior for "require"?
Eduardo Ochs
eduardoochs at gmail.com
Mon Feb 16 21:57:08 CET 2015
Problem solved.
If there is a better way, I would like to know.
Cheers,
Eduardo Ochs
eduardoochs at gmail.com
http://angg.twu.net/
The code is below.
# Download luarepl-0.5 and untar it into /tmp/lua-repl-0.5/
cd /tmp/
wget https://github.com/hoelzro/lua-repl/archive/0.5.tar.gz
tar -C /tmp/ -xvzf /tmp/0.5.tar.gz
# Create /tmp/lualoader.lua
cat > /tmp/lualoader.lua <<'%%%'
-- From http://lua-users.org/wiki/LuaModulesLoader
local function lualoader(modulename)
local errmsg = ""
-- Find source
local modulepath = string.gsub(modulename, "%.", "/")
for path in string.gmatch(package.path, "([^;]+)") do
local filename = string.gsub(path, "%?", modulepath)
local file = io.open(filename, "rb")
if file then
-- Compile and return the module
return assert(loadstring(assert(file:read("*a")), filename))
end
errmsg = errmsg.."\n\tno file '"..filename.."' (checked with custom loader)"
end
return errmsg
end
--
-- See http://www.lua.org/manual/5.1/manual.html#pdf-package.loaders
table.insert(package.loaders, 2, lualoader)
--
-- See also: http://lua-users.org/wiki/BinaryModulesLoader
%%%
# Create /tmp/myrepl.lua
cat > /tmp/myrepl.lua <<'%%%'
repldir = "/tmp/lua-repl-0.5/"
package.path = ""
package.path = repldir.."?/init.lua;"..package.path
package.path = repldir.."?.lua;" ..package.path
repl = require "repl"
sync = require "repl.sync"
function sync:showprompt(p) io.write(p == ">" and ">>> " or ">>>> ") end
function sync:lines() return io.stdin:lines() end
function sync:displayerror(err) print(err) end
function sync:displayresults(results)
if results.n == 0 then return end
print(unpack(results, 1, results.n))
end
print()
sync:run()
%%%
# Create /tmp/myrepl.tex
cat > /tmp/myrepl.tex <<'%%%'
\documentclass{book}
\begin{document}
\directlua{dofile "lualoader.lua"}
\directlua{dofile "myrepl.lua"}
\end{document}
%%%
# Run LuaRepl from LuaLaTeX - the prompt will be ">>> "
cd /tmp/
lualatex myrepl.tex
More information about the luatex
mailing list