1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
\ProvidesPackage{addtoluatexpath}[2024-03-14]
\RequirePackage{luacode}
\providecommand{\input@path}{} % initialize input@path if not defined yet
\begin{luacode*}
atlp_paths = {"."} -- global table containing paths added by this package
function atlp_find_file(f) -- find a path in list of atlp_paths
for i, k in pairs(atlp_paths) do
local fp = kpse.find_file(k ..'/'.. f)
if (fp) then
return fp
end
end -- if nothing returned, issue a package error
texio.write_nl('addtoluatexpath searched for file: '..f)
texio.write_nl('addtoluatexpath searched paths were: '..token.get_macro('input@path'))
tex.sprint('\\PackageError{addtoluatexpath}{a file was not found}{}')
tex.sprint('\\stop')
end
function atlp_main(atlp_raw) -- add to path from raw string
local atlp_tbl = require'luakeys'().parse(atlp_raw, {naked_as_value=true}) -- paths as table
local atlp_no_lua = atlp_tbl['nolua'] or false -- check and set nolua=true
local atlp_no_tex = atlp_tbl['notex'] or false -- check and set notex=true
if atlp_raw:find('*') ~= nil then -- if *, must use penlight to expand subdirectories
penlight = require'penlight'
atlp_tbl = penlight.List(atlp_tbl) -- convert to pl.List for easy manipulation
end
for __, p in ipairs(atlp_tbl) do
if p:find('*') == nil then -- add paths without *, and continue the loop after
if not atlp_no_lua then package.path = package.path .. ';'..p..'/?.lua;' end
if not atlp_no_tex then token.set_macro('input@path', token.get_macro('input@path')..'{'..p..'/}', 'global') end
atlp_paths[#atlp_paths + 1] = p -- append the added path to global list
goto continue
end
local p, c = p:gsub('*','') -- if * added, include subdirectories
local atlp_subdirs = penlight.List(penlight.dir.getdirectories(p))
-- troubleshooting --texio.write_nl(penlight.pretty.write(atlp_subdirs))
if c == 2 then
atlp_subdirs = atlp_subdirs:map(function(s) return s ..'/**' end) -- add ** to subdirs for recursive inclusion
end
atlp_tbl:append(p) -- make sure p (current path without *) is still added!
atlp_tbl:extend(atlp_subdirs) -- extend path to include additional subdirs; the for loop is lengthened
::continue::
end
-- -- troubleshooting: show all paths
--texio.write_nl('Lua Paths >>> \n'..package.path:gsub(';','\n'))
--texio.write_nl('TeX Paths >>> \n'..token.get_macro('input@path'):gsub('}{','\n'))
end
atlp_main(token.get_macro('@raw@opt@addtoluatexpath.sty'))
\end{luacode*}
\NewDocumentCommand{\addtoluatexpath}{m}{\luadirect{atlp_main(\luastring{#1})}} % a command
\AtEndOfPackage{\let\@unprocessedoptions\relax}
|