diff options
Diffstat (limited to 'tex/context/base/data-exp.lua')
-rw-r--r-- | tex/context/base/data-exp.lua | 106 |
1 files changed, 84 insertions, 22 deletions
diff --git a/tex/context/base/data-exp.lua b/tex/context/base/data-exp.lua index 6d15a1cd7..98fbe9f95 100644 --- a/tex/context/base/data-exp.lua +++ b/tex/context/base/data-exp.lua @@ -13,7 +13,7 @@ local lpegCt, lpegCs, lpegP, lpegC, lpegS = lpeg.Ct, lpeg.Cs, lpeg.P, lpeg.C, lp local type, next = type, next local ostype = os.type -local collapse_path = file.collapse_path +local collapsepath = file.collapsepath local trace_locating = false trackers.register("resolvers.locating", function(v) trace_locating = v end) local trace_expansions = false trackers.register("resolvers.expansions", function(v) trace_expansions = v end) @@ -49,22 +49,29 @@ local resolvers = resolvers local dummy_path_expr = "^!*unset/*$" local function do_first(a,b) - local t = { } - for s in gmatch(b,"[^,]+") do t[#t+1] = a .. s end + local t, n = { }, 0 + for s in gmatch(b,"[^,]+") do + n = n + 1 + t[n] = a .. s + end return "{" .. concat(t,",") .. "}" end local function do_second(a,b) - local t = { } - for s in gmatch(a,"[^,]+") do t[#t+1] = s .. b end + local t, n = { }, 0 + for s in gmatch(a,"[^,]+") do + n = n + 1 + t[n] = s .. b + end return "{" .. concat(t,",") .. "}" end local function do_both(a,b) - local t = { } + local t, n = { }, 0 for sa in gmatch(a,"[^,]+") do for sb in gmatch(b,"[^,]+") do - t[#t+1] = sa .. sb + n = n + 1 + t[n] = sa .. sb end end return "{" .. concat(t,",") .. "}" @@ -88,6 +95,7 @@ local function splitpathexpr(str, newlist, validate) report_resolvers("expanding variable '%s'",str) end local t, ok, done = newlist or { }, false, false + local n = #t str = lpegmatch(replacer_1,str) while true do done = false @@ -111,11 +119,15 @@ local function splitpathexpr(str, newlist, validate) if validate then for s in gmatch(str,"[^,]+") do s = validate(s) - if s then t[#t+1] = s end + if s then + n = n + 1 + t[n] = s + end end else for s in gmatch(str,"[^,]+") do - t[#t+1] = s + n = n + 1 + t[n] = s end end if trace_expansions then @@ -128,7 +140,7 @@ end local function validate(s) local isrecursive = find(s,"//$") - s = collapse_path(s) + s = collapsepath(s) if isrecursive then s = s .. "//" end @@ -151,11 +163,14 @@ function resolvers.expandedpathfromlist(pathlist) -- maybe not a list, just a pa splitpathexpr(pathlist[k],newlist,validate) end else + local n = 0 for k=1,#pathlist do for p in gmatch(pathlist[k],"([^,]+)") do ---~ p = collapse_path(p) p = validate(p) - if p ~= "" then newlist[#newlist+1] = p end + if p ~= "" then + n = n + 1 + newlist[n] = p + end end end end @@ -164,16 +179,63 @@ end -- We also put some cleanup code here. -local cleanup -- used recursively +--~ local cleanup -- used recursively +--~ local homedir + +--~ cleanup = lpeg.replacer { +--~ { +--~ "!", +--~ "" +--~ }, +--~ { +--~ "\\", +--~ "/" +--~ }, +--~ { +--~ "~" , +--~ function() +--~ return lpegmatch(cleanup,environment.homedir) +--~ end +--~ }, +--~ } + +--~ function resolvers.cleanpath(str) +--~ return str and lpegmatch(cleanup,str) +--~ end -cleanup = lpeg.replacer { - { "!", "" }, - { "\\", "/" }, - { "~" , function() return lpegmatch(cleanup,environment.homedir) end }, +local cleanup = lpeg.replacer { + { "!" , "" }, + { "\\" , "/" }, } +local homedir + function resolvers.cleanpath(str) - return str and lpegmatch(cleanup,str) + if not homedir then + homedir = lpegmatch(cleanup,environment.homedir or "") + if homedir == string.char(127) or homedir == "" or not lfs.isdir(homedir) then + if trace_expansions then + report_resolvers("no home dir set, ignoring dependent paths") + end + function resolvers.cleanpath(str) + if find(str,"~") then + return "" -- special case + else + return str and lpegmatch(cleanup,str) + end + end + else + cleanup = lpeg.replacer { + { "!" , "" }, + { "\\" , "/" }, + { "~" , homedir }, + } + function resolvers.cleanpath(str) + return str and lpegmatch(cleanup,str) + end + end + end + return resolvers.cleanpath(str) end -- This one strips quotes and funny tokens. @@ -203,8 +265,6 @@ end -- we join them and split them after the expansion has taken place. This -- is more convenient. ---~ local checkedsplit = string.checkedsplit - local cache = { } local splitter = lpegCt(lpeg.splitat(lpegS(ostype == "windows" and ";" or ":;"))) -- maybe add , @@ -219,15 +279,17 @@ local function splitconfigurationpath(str) -- beware, this can be either a path str = gsub(str,"\\","/") local split = lpegmatch(splitter,str) found = { } + local noffound = 0 for i=1,#split do local s = split[i] if not find(s,"^{*unset}*") then - found[#found+1] = s + noffound = noffound + 1 + found[noffound] = s end end if trace_expansions then report_resolvers("splitting path specification '%s'",str) - for k=1,#found do + for k=1,noffound do report_resolvers("% 4i: %s",k,found[k]) end end |