diff options
Diffstat (limited to 'Master/texmf-dist/tex/context/base/supp-fil.lua')
-rw-r--r-- | Master/texmf-dist/tex/context/base/supp-fil.lua | 243 |
1 files changed, 149 insertions, 94 deletions
diff --git a/Master/texmf-dist/tex/context/base/supp-fil.lua b/Master/texmf-dist/tex/context/base/supp-fil.lua index 8d69f64a72c..a2c0b5d7daa 100644 --- a/Master/texmf-dist/tex/context/base/supp-fil.lua +++ b/Master/texmf-dist/tex/context/base/supp-fil.lua @@ -6,6 +6,11 @@ if not modules then modules = { } end modules ['supp-fil'] = { license = "see context related readme files" } +-- This module will be redone ! + +-- context is not defined yet! todo! (we need to load tupp-fil after cld) +-- todo: move startreadingfile to lua and push regime there + --[[ldx-- <p>It's more convenient to manipulate filenames (paths) in <l n='lua'/> than in <l n='tex'/>. These methods have counterparts @@ -13,25 +18,34 @@ at the <l n='tex'/> side.</p> --ldx]]-- local find, gsub, match, format, concat = string.find, string.gsub, string.match, string.format, table.concat -local texsprint, texwrite, ctxcatcodes = tex.sprint, tex.write, tex.ctxcatcodes +local texcount = tex.count +local isfile = lfs.isfile -local trace_modules = false trackers.register("modules.loading", function(v) trace_modules = v end) +local trace_modules = false trackers.register("modules.loading", function(v) trace_modules = v end) +local trace_files = false trackers.register("resolvers.readfile", function(v) trace_files = v end) -support = support or { } -environment = environment or { } +local report_modules = logs.reporter("resolvers","modules") +local report_files = logs.reporter("files","readfile") -environment.outputfilename = environment.outputfilename or environment.jobname +commands = commands or { } +local commands = commands +environment = environment or { } +local environment = environment -function support.checkfilename(str) -- "/whatever..." "c:..." "http://..." - commands.chardef("kindoffile",boolean.tonumber(find(str,"^/") or find(str,"[%a]:"))) +local findbyscheme = resolvers.finders.byscheme + +function commands.checkfilename(str) -- "/whatever..." "c:..." "http://..." + texcount.kindoffile = (find(str,"^/") or find(str,"[%a]:") and 1) or 0 end -function support.thesanitizedfilename(str) - texwrite((gsub(str,"\\","/"))) +function commands.thesanitizedfilename(str) + context((gsub(str,"\\","/"))) end -function support.splitfilename(fullname) - local path, name, base, suffix, kind = '', fullname, fullname, '', 0 +local testcase = commands.testcase + +function commands.splitfilename(fullname) + local path, name, base, suffix = '', fullname, fullname, '' local p, n = match(fullname,"^(.+)/(.-)$") if p and n then path, name, base = p, n, n @@ -40,52 +54,47 @@ function support.splitfilename(fullname) if b and s then name, suffix = b, s end - if path == "" then - kind = 0 - elseif path == '.' then - kind = 1 - else - kind = 2 - end - commands.def("splitofffull", fullname) - commands.def("splitoffpath", path) - commands.def("splitoffbase", base) - commands.def("splitoffname", name) - commands.def("splitofftype", suffix) - commands.chardef("splitoffkind", kind) + texcount.splitoffkind = (path == "" and 0) or (path == '.' and 1) or 2 + local setvalue = context.setvalue + setvalue("splitofffull", fullname) + setvalue("splitoffpath", path) + setvalue("splitoffbase", base) + setvalue("splitoffname", name) + setvalue("splitofftype", suffix) end -function support.splitfiletype(fullname) +function commands.splitfiletype(fullname) local name, suffix = fullname, '' local n, s = match(fullname,"^(.+)%.(.-)$") if n and s then name, suffix = n, s end - commands.def("splitofffull", fullname) - commands.def("splitoffpath", "") - commands.def("splitoffname", name) - commands.def("splitofftype", suffix) + local setvalue = context.setvalue + setvalue("splitofffull", fullname) + setvalue("splitoffpath", "") + setvalue("splitoffname", name) + setvalue("splitofftype", suffix) end -function support.doifparentfileelse(n) - commands.testcase(n==environment.jobname or n==environment.jobname..'.tex' or n==environment.outputfilename) +function commands.doifparentfileelse(n) + testcase(n == environment.jobname or n == environment.jobname .. '.tex' or n == environment.outputfilename) end -- saves some .15 sec on 12 sec format generation local lastexistingfile = "" -function support.doiffileexistelse(name) +function commands.doiffileexistelse(name) if not name or name == "" then lastexistingfile = "" else lastexistingfile = resolvers.findtexfile(name) or "" end - return commands.testcase(lastexistingfile ~= "") + return testcase(lastexistingfile ~= "") end -function support.lastexistingfile() - texsprint(ctxcatcodes,lastexistingfile) +function commands.lastexistingfile() + context(lastexistingfile) end -- more, we can cache matches @@ -97,86 +106,111 @@ local found = { } -- can best be done in the resolver itself -- todo: tracing local function readfilename(specification,backtrack,treetoo) - local fnd = found[specification] + local name = specification.filename + local fnd = found[name] if not fnd then - local splitspec = resolvers.splitmethod(specification) - local filename = splitspec.path or "" - if lfs.isfile(filename) then - fnd = filename + if isfile(name) then + if trace_files then + report_files("found local: %s",name) + end + fnd = name end if not fnd and backtrack then - local fname = filename + local fname = name for i=1,backtrack,1 do fname = "../" .. fname - if lfs.isfile(fname) then + if isfile(fname) then + if trace_files then + report_files("found by backtracking: %s",fname) + end fnd = fname break + elseif trace_files then + report_files("not found by backtracking: %s",fname) end end end if not fnd and treetoo then ---~ fnd = resolvers.find_file(filename) - fnd = resolvers.findtexfile(filename) + fnd = resolvers.findtexfile(name) or "" + if trace_files then + if fnd ~= "" then + report_files("found by tree lookup: %s",fnd) + else + report_files("not found by tree lookup: %s",name) + end + end + end + found[name] = fnd + elseif trace_files then + if fnd ~= "" then + report_files("already found: %s",fnd) + else + report_files("already not found: %s",name) end - found[specification] = fnd end return fnd or "" end -support.readfilename = readfilename +function commands.readfilename(filename) + return findbyscheme("any",filename) +end -function finders.job(filename) return readfilename(filename,nil,false) end -- current path, no backtracking -function finders.loc(filename) return readfilename(filename,2, false) end -- current path, backtracking -function finders.sys(filename) return readfilename(filename,nil,true ) end -- current path, obeys tex search -function finders.fix(filename) return readfilename(filename,2, false) end -- specified path, backtracking -function finders.set(filename) return readfilename(filename,nil,false) end -- specified path, no backtracking -function finders.any(filename) return readfilename(filename,2, true ) end -- loc job sys +function finders.job(specification) return readfilename(specification,false,false) end -- current path, no backtracking +function finders.loc(specification) return readfilename(specification,2, false) end -- current path, backtracking +function finders.sys(specification) return readfilename(specification,false,true ) end -- current path, obeys tex search +function finders.fix(specification) return readfilename(specification,2, false) end -- specified path, backtracking +function finders.set(specification) return readfilename(specification,false,false) end -- specified path, no backtracking +function finders.any(specification) return readfilename(specification,2, true ) end -- loc job sys -openers.job = openers.generic loaders.job = loaders.generic -openers.loc = openers.generic loaders.loc = loaders.generic -openers.sys = openers.generic loaders.sys = loaders.generic -openers.fix = openers.generic loaders.fix = loaders.generic -openers.set = openers.generic loaders.set = loaders.generic -openers.any = openers.generic loaders.any = loaders.generic +openers.job = openers.file loaders.job = loaders.file -- default anyway +openers.loc = openers.file loaders.loc = loaders.file +openers.sys = openers.file loaders.sys = loaders.file +openers.fix = openers.file loaders.fix = loaders.file +openers.set = openers.file loaders.set = loaders.file +openers.any = openers.file loaders.any = loaders.file -function support.doreadfile(protocol,path,name) -- better do a split and then pass table - local specification +function finders.doreadfile(scheme,path,name) -- better do a split and then pass table + local fullname if url.hasscheme(name) then - specification = name + fullname = name else - specification = ((path == "") and format("%s:///%s",protocol,name)) or format("%s:///%s/%s",protocol,path,name) + fullname = ((path == "") and format("%s:///%s",scheme,name)) or format("%s:///%s/%s",scheme,path,name) end - texsprint(ctxcatcodes,resolvers.findtexfile(specification)) + return resolvers.findtexfile(fullname) or "" -- can be more direct end --- modules can only have a tex or mkiv suffix or can have a specified one +function commands.doreadfile(scheme,path,name) + context(finders.doreadfile(scheme,path,name)) +end + +-- modules can have a specific suffix or can specify one -local prefixes = { "m", "p", "s", "x", "t" } -local suffixes = { "tex", "mkiv" } +local prefixes = { "m", "p", "s", "x", "v", "t" } +local suffixes = { "mkiv", "tex", "mkvi" } -- order might change and how about cld local modstatus = { } -local function usemodule(name,hassheme) +local function usemodule(name,hasscheme) local foundname if hasscheme then -- no auto suffix as http will return a home page or error page -- so we only add one if missing local fullname = file.addsuffix(name,"tex") if trace_modules then - logs.report("modules","checking scheme driven file '%s'",fullname) + report_modules("checking url: '%s'",fullname) end foundname = resolvers.findtexfile(fullname) or "" elseif file.extname(name) ~= "" then if trace_modules then - logs.report("modules","checking suffix driven file '%s'",name) + report_modules("checking file: '%s'",name) end - foundname = support.readfilename(name,false,true) or "" + foundname = findbyscheme("any",name) or "" else for i=1,#suffixes do local fullname = file.addsuffix(name,suffixes[i]) if trace_modules then - logs.report("modules","checking suffix driven file '%s'",fullname) + report_modules("checking file: '%s'",fullname) end - foundname = support.readfilename(fullname,false,true) or "" + foundname = findbyscheme("any",fullname) or "" if foundname ~= "" then break end @@ -184,7 +218,7 @@ local function usemodule(name,hassheme) end if foundname ~= "" then if trace_modules then - logs.report("modules","loading '%s'",foundname) + report_modules("loading: '%s'",foundname) end context.startreadingfile() context.input(foundname) @@ -195,7 +229,7 @@ local function usemodule(name,hassheme) end end -function support.usemodules(prefix,askedname,truename) +function commands.usemodules(prefix,askedname,truename) local hasprefix = prefix and prefix ~= "" local hashname = ((hasprefix and prefix) or "*") .. "-" .. truename local status = modstatus[hashname] @@ -205,7 +239,7 @@ function support.usemodules(prefix,askedname,truename) status = status + 1 else if trace_modules then - logs.report("modules","locating '%s'",truename) + report_modules("locating: '%s'",truename) end local hasscheme = url.hasscheme(truename) if hasscheme then @@ -240,39 +274,60 @@ function support.usemodules(prefix,askedname,truename) end end if status == 0 then - if trace_modules then - logs.report("modules","skipping '%s' (not found)",truename) - else - interfaces.showmessage("systems",6,askedname) - end + report_modules("not found: '%s'",askedname) elseif status == 1 then - if not trace_modules then - interfaces.showmessage("systems",5,askedname) - end + report_modules("loaded: '%s'",trace_modules and truename or askedname) else - if trace_modules then - logs.report("modules","skipping '%s' (already loaded)",truename) - else - interfaces.showmessage("systems",7,askedname) - end + report_modules("already loaded: '%s'",trace_modules and truename or askedname) end modstatus[hashname] = status end +local loaded = { } + +function commands.uselibrary(name,patterns,action,failure) + local files = utilities.parsers.settings_to_array(name) + local done = false + for i=1,#files do + local filename = files[i] + if not loaded[filename] then + loaded[filename] = true + for i=1,#patterns do + local filename = format(patterns[i],filename) + -- local foundname = resolvers.findfile(filename) or "" + local foundname = finders.doreadfile("any",".",filename) + if foundname ~= "" then + action(name,foundname) + done = true + break + end + end + if done then + break + end + end + end + if failure and not done then + failure(name) + end +end + statistics.register("loaded tex modules", function() if next(modstatus) then - local t, f = { }, { } + local t, f, nt, nf = { }, { }, 0, 0 for k, v in table.sortedhash(modstatus) do k = file.basename(k) if v == 0 then - f[#f+1] = k + nf = nf + 1 + f[nf] = k else - t[#t+1] = k + nt = nt + 1 + t[nt] = k end end - local ts = (#t>0 and format(" (%s)",concat(t," "))) or "" - local fs = (#f>0 and format(" (%s)",concat(f," "))) or "" - return format("%s requested, %s found%s, %s missing%s",#t+#f,#t,ts,#f,fs) + local ts = (nt>0 and format(" (%s)",concat(t," "))) or "" + local fs = (nf>0 and format(" (%s)",concat(f," "))) or "" + return format("%s requested, %s found%s, %s missing%s",nt+nf,nt,ts,nf,fs) else return nil end |