diff options
Diffstat (limited to 'scripts')
28 files changed, 1197 insertions, 795 deletions
diff --git a/scripts/context/lua/luatools.lua b/scripts/context/lua/luatools.lua index e6fdd50d5..1d87322c1 100644 --- a/scripts/context/lua/luatools.lua +++ b/scripts/context/lua/luatools.lua @@ -269,7 +269,7 @@ function string:totable() return lpegmatch(pattern,self) end ---~ for _, str in ipairs { +--~ local t = { --~ "1234567123456712345671234567", --~ "a\tb\tc", --~ "aa\tbb\tcc", @@ -277,7 +277,10 @@ end --~ "aaaa\tbbbb\tcccc", --~ "aaaaa\tbbbbb\tccccc", --~ "aaaaaa\tbbbbbb\tcccccc", ---~ } do print(string.tabtospace(str)) end +--~ } +--~ for k,v do +--~ print(string.tabtospace(t[k])) +--~ end function string.tabtospace(str,tab) -- we don't handle embedded newlines @@ -446,6 +449,8 @@ function string:split(separator) return match(c,self) end +lpeg.splitters = cache + local cache = { } function lpeg.checkedsplit(separator,str) @@ -510,7 +515,7 @@ table.join = table.concat local concat, sort, insert, remove = table.concat, table.sort, table.insert, table.remove local format, find, gsub, lower, dump, match = string.format, string.find, string.gsub, string.lower, string.dump, string.match local getmetatable, setmetatable = getmetatable, setmetatable -local type, next, tostring, tonumber, ipairs, pairs = type, next, tostring, tonumber, ipairs, pairs +local type, next, tostring, tonumber, ipairs = type, next, tostring, tonumber, ipairs local unpack = unpack or table.unpack function table.strip(tab) @@ -577,7 +582,7 @@ end table.sortedkeys = sortedkeys table.sortedhashkeys = sortedhashkeys -function table.sortedpairs(t) +function table.sortedhash(t) local s = sortedhashkeys(t) -- maybe just sortedkeys local n = 0 local function kv(s) @@ -588,6 +593,8 @@ function table.sortedpairs(t) return kv, s end +table.sortedpairs = table.sortedhash + function table.append(t, list) for _,v in next, list do insert(t,v) @@ -710,18 +717,18 @@ end -- slower than #t on indexed tables (#t only returns the size of the numerically indexed slice) -function table.is_empty(t) +function table.is_empty(t) -- obolete, use inline code instead return not t or not next(t) end -function table.one_entry(t) +function table.one_entry(t) -- obolete, use inline code instead local n = next(t) return n and not next(t,n) end -function table.starts_at(t) - return ipairs(t,1)(t,0) -end +--~ function table.starts_at(t) -- obsolete, not nice +--~ return ipairs(t,1)(t,0) +--~ end function table.tohash(t,value) local h = { } @@ -825,7 +832,7 @@ local function do_serialize(root,name,depth,level,indexed) end -- we could check for k (index) being number (cardinal) if root and next(root) then - local first, last = nil, 0 -- #root cannot be trusted here + local first, last = nil, 0 -- #root cannot be trusted here (will be ok in 5.2 when ipairs is gone) if compact then -- NOT: for k=1,#root do (we need to quit at nil) for k,v in ipairs(root) do -- can we use next? @@ -1534,13 +1541,14 @@ function io.ask(question,default,options) elseif not options then return answer else - for _,v in pairs(options) do - if v == answer then + for k=1,#options do + if options[k] == answer then return answer end end local pattern = "^" .. answer - for _,v in pairs(options) do + for k=1,#options do + local v = options[k] if find(v,pattern) then return v end @@ -2314,7 +2322,7 @@ function file.splitname(str) -- returns drive, path, base, suffix return lpegmatch(pattern,str) end --- function test(t) for k, v in pairs(t) do print(v, "=>", file.splitname(v)) end end +-- function test(t) for k, v in next, t do print(v, "=>", file.splitname(v)) end end -- -- test { "c:", "c:/aa", "c:/aa/bb", "c:/aa/bb/cc", "c:/aa/bb/cc.dd", "c:/aa/bb/cc.dd.ee" } -- test { "c:", "c:aa", "c:aa/bb", "c:aa/bb/cc", "c:aa/bb/cc.dd", "c:aa/bb/cc.dd.ee" } @@ -2759,8 +2767,9 @@ local make_indeed = true -- false if string.find(os.getenv("PATH"),";") then -- os.type == "windows" function dir.mkdirs(...) - local str, pth = "", "" - for _, s in ipairs({...}) do + local str, pth, t = "", "", { ... } + for i=1,#t do + local s = t[i] if s ~= "" then if str ~= "" then str = str .. "/" .. s @@ -2858,8 +2867,9 @@ if string.find(os.getenv("PATH"),";") then -- os.type == "windows" else function dir.mkdirs(...) - local str, pth = "", "" - for _, s in ipairs({...}) do + local str, pth, t = "", "", { ... } + for i=1,#t do + local s = t[i] if s ~= "" then if str ~= "" then str = str .. "/" .. s @@ -3031,14 +3041,20 @@ unicode.utfname = { [4] = 'utf-32-be' } -function unicode.utftype(f) -- \000 fails ! +-- \000 fails in <= 5.0 but is valid in >=5.1 where %z is depricated + +function unicode.utftype(f) local str = f:read(4) if not str then f:seek('set') return 0 - elseif find(str,"^%z%z\254\255") then + -- elseif find(str,"^%z%z\254\255") then -- depricated + -- elseif find(str,"^\000\000\254\255") then -- not permitted and bugged + elseif find(str,"\000\000\254\255",1,true) then -- seems to work okay (TH) return 4 - elseif find(str,"^\255\254%z%z") then + -- elseif find(str,"^\255\254%z%z") then -- depricated + -- elseif find(str,"^\255\254\000\000") then -- not permitted and bugged + elseif find(str,"\255\254\000\000",1,true) then -- seems to work okay (TH) return 3 elseif find(str,"^\254\255") then f:seek('set',2) @@ -3239,6 +3255,7 @@ if not modules then modules = { } end modules ['l-utils'] = { local gsub = string.gsub local concat = table.concat +local type, next = type, next if not utils then utils = { } end if not utils.merger then utils.merger = { } end @@ -3314,9 +3331,10 @@ function utils.merger._self_libs_(libs,list) if type(libs) == 'string' then libs = { libs } end if type(list) == 'string' then list = { list } end local foundpath = nil - for _, lib in ipairs(libs) do - for _, pth in ipairs(list) do - pth = gsub(pth,"\\","/") -- file.clean_path + for i=1,#libs do + local lib = libs[i] + for j=1,#list do + local pth = gsub(list[j],"\\","/") -- file.clean_path utils.report("checking library path %s",pth) local name = pth .. "/" .. lib if lfs.isfile(name) then @@ -3328,7 +3346,8 @@ function utils.merger._self_libs_(libs,list) if foundpath then utils.report("using library path %s",foundpath) local right, wrong = { }, { } - for _, lib in ipairs(libs) do + for i=1,#libs do + local lib = libs[i] local fullname = foundpath .. "/" .. lib if lfs.isfile(fullname) then -- right[#right+1] = lib @@ -3681,6 +3700,8 @@ if not modules then modules = { } end modules ['trac-tra'] = { -- bound to a variable, like node.new, node.copy etc (contrary to for instance -- node.has_attribute which is bound to a has_attribute local variable in mkiv) +local debug = require "debug" + local getinfo = debug.getinfo local type, next = type, next local concat = table.concat @@ -3728,7 +3749,7 @@ function debugger.showstats(printer,threshold) local total, grandtotal, functions = 0, 0, 0 printer("\n") -- ugly but ok -- table.sort(counters) - for func, count in pairs(counters) do + for func, count in next, counters do if count > threshold then local name = getname(func) if not find(name,"for generator") then @@ -3763,7 +3784,7 @@ end --~ local total, grandtotal, functions = 0, 0, 0 --~ printer("\n") -- ugly but ok --~ -- table.sort(counters) ---~ for func, count in pairs(counters) do +--~ for func, count in next, counters do --~ if count > threshold then --~ printer(format("%8i %s", count, func)) --~ total = total + count @@ -3939,8 +3960,9 @@ end function setters.show(t) commands.writestatus("","") - for k,v in ipairs(setters.list(t)) do - commands.writestatus(t.name,v) + local list = setters.list(t) + for k=1,#list do + commands.writestatus(t.name,list[k]) end commands.writestatus("","") end @@ -4064,7 +4086,8 @@ if not environment.jobname then environ function environment.initialize_arguments(arg) local arguments, files = { }, { } environment.arguments, environment.files, environment.sortedflags = arguments, files, nil - for index, argument in pairs(arg) do + for index=1,#arg do + local argument = arg[index] if index > 0 then local flag, value = match(argument,"^%-+(.-)=(.-)$") if flag then @@ -4097,14 +4120,15 @@ function environment.argument(name,partial) return arguments[name] elseif partial then if not sortedflags then - sortedflags = { } - for _,v in pairs(table.sortedkeys(arguments)) do - sortedflags[#sortedflags+1] = "^" .. v + sortedflags = table.sortedkeys(arguments) + for k=1,#sortedflags do + sortedflags[k] = "^" .. sortedflags[k] end environment.sortedflags = sortedflags end -- example of potential clash: ^mode ^modefile - for _,v in ipairs(sortedflags) do + for k=1,#sortedflags do + local v = sortedflags[k] if find(name,v) then return arguments[sub(v,2,#v)] end @@ -4113,9 +4137,13 @@ function environment.argument(name,partial) return nil end +environment.argument("x",true) + function environment.split_arguments(separator) -- rather special, cut-off before separator local done, before, after = false, { }, { } - for _,v in ipairs(environment.original_arguments) do + local original_arguments = environment.original_arguments + for k=1,#original_arguments do + local v = original_arguments[k] if not done and v == separator then done = true elseif done then @@ -4134,9 +4162,10 @@ function environment.reconstruct_commandline(arg,noquote) a = resolvers.resolve(a) a = unquote(a) return a - elseif next(arg) then + elseif #arg > 0 then local result = { } - for _,a in ipairs(arg) do -- ipairs 1 .. #n + for i=1,#arg do + local a = arg[i] a = resolvers.resolve(a) a = unquote(a) a = gsub(a,'"','\\"') -- tricky @@ -4157,7 +4186,8 @@ if arg then -- new, reconstruct quoted snippets (maybe better just remove the " then and add them later) local newarg, instring = { }, false - for index, argument in ipairs(arg) do + for index=1,#arg do + local argument = arg[index] if find(argument,"^\"") then newarg[#newarg+1] = gsub(argument,"^\"","") if not find(argument,"\"$") then @@ -4840,7 +4870,7 @@ if not modules then modules = { } end modules ['data-inp'] = { -- * some public auxiliary functions were made private -- -- TODO: os.getenv -> os.env[] --- TODO: instances.[hashes,cnffiles,configurations,522] -> ipairs (alles check, sneller) +-- TODO: instances.[hashes,cnffiles,configurations,522] -- TODO: check escaping in find etc, too much, too slow -- This lib is multi-purpose and can be loaded again later on so that @@ -5251,8 +5281,8 @@ local function splitpathexpr(str, t, validate) end end if trace_expansions then - for k,v in ipairs(t) do - logs.report("fileio","% 4i: %s",k,v) + for k=1,#t do + logs.report("fileio","% 4i: %s",k,t[k]) end end return t @@ -5457,7 +5487,9 @@ local function load_cnf_file(fname) end local function collapse_cnf_data() -- potential optimization: pass start index (setup and configuration are shared) - for _,c in ipairs(instance.order) do + local order = instance.order + for i=1,#order do + local c = order[i] for k,v in next, c do if not instance.variables[k] then if instance.environment[k] then @@ -5473,8 +5505,9 @@ end function resolvers.load_cnf() local function loadoldconfigdata() - for _, fname in ipairs(instance.cnffiles) do - load_cnf_file(fname) + local cnffiles = instance.cnffiles + for i=1,#cnffiles do + load_cnf_file(cnffiles[i]) end end -- instance.cnffiles contain complete names now ! @@ -5486,9 +5519,10 @@ function resolvers.load_cnf() logs.report("fileio","no cnf files found (TEXMFCNF may not be set/known)") end else - instance.rootpath = instance.cnffiles[1] - for k,fname in ipairs(instance.cnffiles) do - instance.cnffiles[k] = file.collapse_path(fname) + local cnffiles = instance.cnffiles + instance.rootpath = cnffiles[1] + for k=1,#cnffiles do + instance.cnffiles[k] = file.collapse_path(cnffiles[k]) end for i=1,3 do instance.rootpath = file.dirname(instance.rootpath) @@ -5516,8 +5550,9 @@ function resolvers.load_lua() -- yet harmless else instance.rootpath = instance.luafiles[1] - for k,fname in ipairs(instance.luafiles) do - instance.luafiles[k] = file.collapse_path(fname) + local luafiles = instance.luafiles + for k=1,#luafiles do + instance.luafiles[k] = file.collapse_path(luafiles[k]) end for i=1,3 do instance.rootpath = file.dirname(instance.rootpath) @@ -5580,7 +5615,9 @@ end -- locators function resolvers.locatelists() - for _, path in ipairs(resolvers.clean_path_list('TEXMF')) do + local texmfpaths = resolvers.clean_path_list('TEXMF') + for i=1,#texmfpaths do + local path = texmfpaths[i] if trace_locating then logs.report("fileio","locating list of '%s'",path) end @@ -5613,7 +5650,9 @@ function resolvers.loadfiles() instance.loaderror = false instance.files = { } if not instance.renewcache then - for _, hash in ipairs(instance.hashes) do + local hashes = instance.hashes + for k=1,#hashes do + local hash = hashes[k] resolvers.hashdatabase(hash.tag,hash.name) if instance.loaderror then break end end @@ -5627,8 +5666,9 @@ end -- generators: function resolvers.loadlists() - for _, hash in ipairs(instance.hashes) do - resolvers.generatedatabase(hash.tag) + local hashes = instance.hashes + for i=1,#hashes do + resolvers.generatedatabase(hashes[i].tag) end end @@ -5725,8 +5765,7 @@ end -- we join them and split them after the expansion has taken place. This -- is more convenient. -local checkedsplit = string.checkedsplit -local normalsplit = string.split +--~ local checkedsplit = string.checkedsplit local cache = { } @@ -5750,8 +5789,8 @@ local split = lpegmatch(splitter,str) end if trace_expansions then logs.report("fileio","splitting path specification '%s'",str) - for k,v in ipairs(found) do - logs.report("fileio","% 4i: %s",k,v) + for k=1,#found do + logs.report("fileio","% 4i: %s",k,found[k]) end end cache[str] = found @@ -5763,8 +5802,9 @@ end resolvers.split_kpse_path = split_kpse_path function resolvers.splitconfig() - for i,c in ipairs(instance) do - for k,v in pairs(c) do + for i=1,#instance do + local c = instance[i] + for k,v in next, c do if type(v) == 'string' then local t = split_kpse_path(v) if #t > 1 then @@ -5776,8 +5816,10 @@ function resolvers.splitconfig() end function resolvers.joinconfig() - for i,c in ipairs(instance.order) do - for k,v in pairs(c) do -- ipairs? + local order = instance.order + for i=1,#order do + local c = order[i] + for k,v in next, c do -- indexed? if type(v) == 'table' then c[k] = file.join_path(v) end @@ -5804,8 +5846,9 @@ end function resolvers.splitexpansions() local ie = instance.expansions for k,v in next, ie do - local t, h = { }, { } - for _,vv in ipairs(split_kpse_path(v)) do + local t, h, p = { }, { }, split_kpse_path(v) + for kk=1,#p do + local vv = p[kk] if vv ~= "" and not h[vv] then t[#t+1] = vv h[vv] = true @@ -5852,11 +5895,15 @@ function resolvers.serialize(files) end t[#t+1] = "return {" if instance.sortdata then - for _, k in pairs(sortedkeys(files)) do -- ipairs + local sortedfiles = sortedkeys(files) + for i=1,#sortedfiles do + local k = sortedfiles[i] local fk = files[k] if type(fk) == 'table' then t[#t+1] = "\t['" .. k .. "']={" - for _, kk in pairs(sortedkeys(fk)) do -- ipairs + local sortedfk = sortedkeys(fk) + for j=1,#sortedfk do + local kk = sortedfk[j] t[#t+1] = dump(kk,fk[kk],"\t\t") end t[#t+1] = "\t}," @@ -5968,7 +6015,9 @@ function resolvers.resetconfig() end function resolvers.loadnewconfig() - for _, cnf in ipairs(instance.luafiles) do + local luafiles = instance.luafiles + for i=1,#luafiles do + local cnf = luafiles[i] local pathname = file.dirname(cnf) local filename = file.join(pathname,resolvers.luaname) local blob = loadfile(filename) @@ -6013,7 +6062,9 @@ end function resolvers.loadoldconfig() if not instance.renewcache then - for _, cnf in ipairs(instance.cnffiles) do + local cnffiles = instance.cnffiles + for i=1,#cnffiles do + local cnf = cnffiles[i] local dname = file.dirname(cnf) resolvers.load_data(dname,'configuration') instance.order[#instance.order+1] = instance.configuration[dname] @@ -6817,13 +6868,14 @@ function resolvers.for_files(command, files, filetype, mustexist) if trace_locating then report('') -- ? end - for _, file in ipairs(files) do + for f=1,#files do + local file = files[f] local result = command(file,filetype,mustexist) if type(result) == 'string' then report(result) else - for _,v in ipairs(result) do - report(v) + for i=1,#result do + report(result[i]) -- could be unpack end end end @@ -6870,7 +6922,7 @@ end function table.sequenced(t,sep) -- temp here local s = { } - for k, v in pairs(t) do -- pairs? + for k, v in next, t do -- indexed? s[#s+1] = k .. "=" .. tostring(v) end return concat(s, sep or " | ") @@ -6902,8 +6954,9 @@ function resolvers.clean_path(str) end function resolvers.do_with_path(name,func) - for _, v in pairs(resolvers.expanded_path_list(name)) do -- pairs? - func("^"..resolvers.clean_path(v)) + local pathlist = resolvers.expanded_path_list(name) + for i=1,#pathlist do + func("^"..resolvers.clean_path(pathlist[i])) end end @@ -6912,7 +6965,9 @@ function resolvers.do_with_var(name,func) end function resolvers.with_files(pattern,handle) - for _, hash in ipairs(instance.hashes) do + local hashes = instance.hashes + for i=1,#hashes do + local hash = hashes[i] local blobpath = hash.tag local blobtype = hash.type if blobpath then @@ -6927,7 +6982,7 @@ function resolvers.with_files(pattern,handle) if type(v) == "string" then handle(blobtype,blobpath,v,k) else - for _,vv in pairs(v) do -- ipairs? + for _,vv in next, v do -- indexed handle(blobtype,blobpath,vv,k) end end @@ -7098,7 +7153,8 @@ function caches.setpath(...) caches.path = '.' end caches.path = resolvers.clean_path(caches.path) - if not table.is_empty({...}) then + local dirs = { ... } + if #dirs > 0 then local pth = dir.mkdirs(caches.path,...) return pth end @@ -7281,7 +7337,7 @@ end function containers.is_valid(container, name) if name and name ~= "" then local storage = container.storage[name] - return storage and not table.is_empty(storage) and storage.cache_version == container.version + return storage and storage.cache_version == container.version else return false end @@ -7385,12 +7441,13 @@ resolvers.automounted = resolvers.automounted or { } function resolvers.automount(usecache) local mountpaths = resolvers.clean_path_list(resolvers.expansion('TEXMFMOUNT')) - if table.is_empty(mountpaths) and usecache then + if (not mountpaths or #mountpaths == 0) and usecache then mountpaths = { caches.setpath("mount") } end - if not table.is_empty(mountpaths) then + if mountpaths and #mountpaths > 0 then statistics.starttiming(resolvers.instance) - for k, root in pairs(mountpaths) do + for k=1,#mountpaths do + local root = mountpaths[k] local f = io.open(root.."/url.tmi") if f then for line in f:lines() do @@ -7661,7 +7718,9 @@ local function list(list,report) local instance = resolvers.instance local pat = upper(pattern or "","") local report = report or texio.write_nl - for _,key in pairs(table.sortedkeys(list)) do + local sorted = table.sortedkeys(list) + for i=1,#sorted do + local key = sorted[i] if instance.pattern == "" or find(upper(key),pat) then if instance.kpseonly then if instance.kpsevars[key] then @@ -7680,11 +7739,14 @@ function resolvers.listers.expansions() list(resolvers.instance.expansions) end function resolvers.listers.configurations(report) local report = report or texio.write_nl local instance = resolvers.instance - for _,key in ipairs(table.sortedkeys(instance.kpsevars)) do + local sorted = table.sortedkeys(instance.kpsevars) + for i=1,#sorted do + local key = sorted[i] if not instance.pattern or (instance.pattern=="") or find(key,instance.pattern) then report(format("%s\n",key)) - for i,c in ipairs(instance.order) do - local str = c[key] + local order = instance.order + for i=1,#order do + local str = order[i][key] if str then report(format("\t%s\t%s",i,str)) end @@ -7945,8 +8007,9 @@ function runners.make_format(texname) logs.simple("using uncompiled initialization file: %s",luaname) end else - for _, v in pairs({instance.luaname, instance.progname, barename}) do - v = string.gsub(v..".lua","%.lua%.lua$",".lua") + local what = { instance.luaname, instance.progname, barename } + for k=1,#what do + local v = string.gsub(what[k]..".lua","%.lua%.lua$",".lua") if v and (v ~= "") then luaname = resolvers.find_files(v)[1] or "" if luaname ~= "" then @@ -7970,7 +8033,8 @@ function runners.make_format(texname) logs.simple("using lua initialization file: %s",luaname) local mp = dir.glob(file.removesuffix(file.basename(luaname)).."-*.mem") if mp and #mp > 0 then - for _, name in ipairs(mp) do + for i=1,#mp do + local name = mp[i] logs.simple("removing related mplib format %s", file.basename(name)) os.remove(name) end diff --git a/scripts/context/lua/mtx-cache.lua b/scripts/context/lua/mtx-cache.lua index 9f54d9754..c2a0db00d 100644 --- a/scripts/context/lua/mtx-cache.lua +++ b/scripts/context/lua/mtx-cache.lua @@ -22,9 +22,11 @@ function scripts.cache.collect_two(...) return path, rest end +local suffixes = { "afm", "tfm", "def", "enc", "otf", "mp", "data" } + function scripts.cache.process_one(action) - for k, v in ipairs({ "afm", "tfm", "def", "enc", "otf", "mp", "data" }) do - action("fonts", v) + for i=1,#suffixes do + action("fonts", suffixes[i]) end end @@ -35,13 +37,10 @@ end -- todo: recursive delete of paths function scripts.cache.remove(list,keep) - local keepsuffixes = { } - for _, v in ipairs(keep or {}) do - keepsuffixes[v] = true - end - local n = 0 - for _,filename in ipairs(list) do - if filename:find("luatex%-cache") then -- safeguard + local n, keepsuffixes = 0, table.tohash(keep or { }) + for i=1,#list do + local filename = list[i] + if string.find(filename,"luatex%-cache") then -- safeguard if not keepsuffixes[file.extname(filename) or ""] then os.remove(filename) n = n + 1 diff --git a/scripts/context/lua/mtx-chars.lua b/scripts/context/lua/mtx-chars.lua index 0b93587a3..6acacfbd2 100644 --- a/scripts/context/lua/mtx-chars.lua +++ b/scripts/context/lua/mtx-chars.lua @@ -248,7 +248,7 @@ function scripts.chars.makeencoutf() end local f = open("xetx-cls.tex",banner_utf_classes) if f then - for k, v in pairs(xtxclasses) do + for k, v in next, xtxclasses do f:write(format("\\defineXTXcharinjectionclass[lb:%s]\n",k)) end f:write("\n") diff --git a/scripts/context/lua/mtx-check.lua b/scripts/context/lua/mtx-check.lua index 49c57ab97..4266ddf0d 100644 --- a/scripts/context/lua/mtx-check.lua +++ b/scripts/context/lua/mtx-check.lua @@ -105,8 +105,10 @@ function scripts.checker.check(filename) local str = io.loaddata(filename) if str then validator.check(str) - if #validator.errors > 0 then - for k, v in ipairs(validator.errors) do + local errors = validator.errors + if #errors > 0 then + for k=1,#errors do + local v = errors[k] local kind, position, line = v[1], v[2], v[3] local data = str:sub(position-30,position+30) data = data:gsub("(.)", { diff --git a/scripts/context/lua/mtx-context.lua b/scripts/context/lua/mtx-context.lua index 93a0577ea..df1ff3b85 100644 --- a/scripts/context/lua/mtx-context.lua +++ b/scripts/context/lua/mtx-context.lua @@ -66,7 +66,7 @@ do function ctxrunner.reflag(flags) local t = { } - for _, flag in pairs(flags) do + for _, flag in next, flags do local key, value = flag:match("^(.-)=(.+)$") if key and value then t[key] = value @@ -122,21 +122,24 @@ do return end end - if table.is_empty(ctxdata.prepfiles) then - logs.simple("nothing prepared, no ctl file saved") - os.remove(ctlname) - else + local prepfiles = ctxdata.prepfiles + if prepfiles and next(prepfiles) then logs.simple("saving logdata in: %s",ctlname) f = io.open(ctlname,'w') if f then f:write("<?xml version='1.0' standalone='yes'?>\n\n") f:write(string.format("<ctx:preplist local='%s'>\n",yn(ctxdata.runlocal))) - for _, name in ipairs(table.sortedkeys(ctxdata.prepfiles)) do - f:write(string.format("\t<ctx:prepfile done='%s'>%s</ctx:prepfile>\n",yn(ctxdata.prepfiles[name]),name)) + local sorted = table.sortedkeys(prepfiles) + for i=1,#sorted do + local name = sorted[i] + f:write(string.format("\t<ctx:prepfile done='%s'>%s</ctx:prepfile>\n",yn(prepfiles[name]),name)) end f:write("</ctx:preplist>\n") f:close() end + else + logs.simple("nothing prepared, no ctl file saved") + os.remove(ctlname) end end @@ -179,7 +182,7 @@ do local found = lfs.isfile(usedname) if not found then - for _, path in pairs(ctxdata.locations) do + for _, path in next, ctxdata.locations do local fullname = file.join(path,ctxdata.ctxname) if lfs.isfile(fullname) then usedname, found = fullname, true @@ -220,8 +223,9 @@ do ctxdata.flags = ctxrunner.reflag(ctxdata.flags) - for _, message in ipairs(ctxdata.messages) do - logs.simple("ctx comment: %s", xml.tostring(message)) + local messages = ctxdata.messages + for i=1,#messages do + logs.simple("ctx comment: %s", xml.tostring(messages[i])) end for r, d, k in xml.elements(ctxdata.xmldata,"ctx:value[@name='job']") do @@ -261,7 +265,9 @@ do local pluspath = false if #oldfiles == 0 then -- message: no files match pattern - for _, p in ipairs(ctxdata.paths) do + local paths = ctxdata.paths + for i=1,#paths do + local p = paths[i] local oldfiles = dir.glob(path.join(p,pattern)) if #oldfiles > 0 then pluspath = true @@ -272,15 +278,18 @@ do if #oldfiles == 0 then -- message: no old files else - for _, oldfile in ipairs(oldfiles) do - newfile = oldfile .. "." .. suffix -- addsuffix will add one only + for i=1,#oldfiles do + local oldfile = oldfiles[i] + local newfile = oldfile .. "." .. suffix -- addsuffix will add one only if ctxdata.runlocal then newfile = file.basename(newfile) end if oldfile ~= newfile and file.needsupdate(oldfile,newfile) then -- message: oldfile needs preprocessing -- os.remove(newfile) - for _, pp in ipairs(preprocessor:split(',')) do + local splitted = preprocessor:split(',') + for i=1,#splitted do + local pp = splitted[i] local command = commands[pp] if command then command = xml.copy(command) @@ -361,7 +370,9 @@ scripts.context.multipass = { function scripts.context.multipass.hashfiles(jobname) local hash = { } - for _, suffix in ipairs(scripts.context.multipass.suffixes) do + local suffixes = scripts.context.multipass.suffixes + for i=1,#suffixes do + local suffix = suffixes[i] local full = jobname .. suffix hash[full] = md5.hex(io.loaddata(full) or "unknown") end @@ -369,7 +380,7 @@ function scripts.context.multipass.hashfiles(jobname) end function scripts.context.multipass.changed(oldhash, newhash) - for k,v in pairs(oldhash) do + for k,v in next, oldhash do if v ~= newhash[k] then return true end @@ -407,7 +418,7 @@ function scripts.context.multipass.makeoptionfile(jobname,ctxdata,kindofrun,curr end local function setvalues(flag,format,plural) if type(flag) == "table" then - for k, v in pairs(flag) do + for k, v in next, flag do f:write(format:format(v),"\n") end else @@ -650,7 +661,7 @@ function scripts.context.run(ctxdata,filename) local files = (filename and { filename }) or environment.files if ctxdata then -- todo: interface - for k,v in pairs(ctxdata.flags) do + for k,v in next, ctxdata.flags do environment.setargument(k,v) end end @@ -671,7 +682,8 @@ function scripts.context.run(ctxdata,filename) end -- if formatfile and scriptfile then - for _, filename in ipairs(files) do + for i=1,#files do + local filename = files[i] local basename, pathname = file.basename(filename), file.dirname(filename) local jobname = file.removesuffix(basename) if pathname == "" then @@ -735,7 +747,7 @@ function scripts.context.run(ctxdata,filename) oldbase = file.removesuffix(jobname) newbase = file.removesuffix(resultname) if oldbase ~= newbase then - for _, suffix in pairs(scripts.context.beforesuffixes) do + for _, suffix in next, scripts.context.beforesuffixes do local oldname = file.addsuffix(oldbase,suffix) local newname = file.addsuffix(newbase,suffix) local tmpname = "keep-"..oldname @@ -839,7 +851,7 @@ function scripts.context.run(ctxdata,filename) os.remove(jobname..".top") -- if resultname then - for _, suffix in pairs(scripts.context.aftersuffixes) do + for _, suffix in next, scripts.context.aftersuffixes do local oldname = file.addsuffix(oldbase,suffix) local newname = file.addsuffix(newbase,suffix) local tmpname = "keep-"..oldname @@ -948,9 +960,11 @@ function scripts.context.make(name) (environment.argument("xetex") and "mtxrun texexec.rb --make --xetex " ) or false, } local list = (name and { name }) or (environment.files[1] and environment.files) or scripts.context.defaultformats - for _, name in ipairs(list) do + for i=1,#list do + local name = list[i] name = scripts.context.interfaces[name] or name - for _, runner in ipairs(runners) do + for i=1,#runners do + local runner = runners[i] if runner then local command = runner .. name logs.simple("running command: %s",command) @@ -1096,15 +1110,15 @@ function scripts.context.purge_job(jobname,all) jobname = file.basename(jobname) local filebase = file.removesuffix(jobname) local deleted = { } - for _, suffix in ipairs(obsolete_results) do - deleted[#deleted+1] = purge_file(filebase.."."..suffix,filebase..".pdf") + for i=1,#obsolete_results do + deleted[#deleted+1] = purge_file(filebase.."."..obsolete_results[i],filebase..".pdf") end - for _, suffix in ipairs(temporary_runfiles) do - deleted[#deleted+1] = purge_file(filebase.."."..suffix) + for i=1,#temporary_runfiles do + deleted[#deleted+1] = purge_file(filebase.."."..temporary_runfiles[i]) end if all then - for _, suffix in ipairs(persistent_runfiles) do - deleted[#deleted+1] = purge_file(filebase.."."..suffix) + for i=1,#persistent_runfiles do + deleted[#deleted+1] = purge_file(filebase.."."..persistent_runfiles[i]) end end if #deleted > 0 then @@ -1122,7 +1136,8 @@ function scripts.context.purge(all) local persistent = table.tohash(persistent_runfiles) local generic = table.tohash(generic_files) local deleted = { } - for _, name in ipairs(files) do + for i=1,#files do + local name = files[i] local suffix = file.extname(name) local basename = file.basename(name) if obsolete[suffix] or temporary[suffix] or persistent[suffix] or generic[basename] then @@ -1191,7 +1206,8 @@ function scripts.context.extras(pattern) else logs.extendbanner(extra) end - for k,v in ipairs(list) do + for i=1,#list do + local v = list[i] local data = io.loaddata(v) or "" data = string.match(data,"begin help(.-)end help") if data then diff --git a/scripts/context/lua/mtx-convert.lua b/scripts/context/lua/mtx-convert.lua index fe43c12f2..62198a621 100644 --- a/scripts/context/lua/mtx-convert.lua +++ b/scripts/context/lua/mtx-convert.lua @@ -112,8 +112,9 @@ function scripts.convert.convertall() end function scripts.convert.convertgiven() - for _, name in ipairs(environment.files) do - graphics.converters.convertfile(name) + local files = environment.files + for i=1,#files do + graphics.converters.convertfile(files[i]) end end diff --git a/scripts/context/lua/mtx-fonts.lua b/scripts/context/lua/mtx-fonts.lua index e5264fb41..74012ae38 100644 --- a/scripts/context/lua/mtx-fonts.lua +++ b/scripts/context/lua/mtx-fonts.lua @@ -31,8 +31,9 @@ function fonts.names.simple() version = simpleversion, } local specifications = data.specifications - for _, format in ipairs(simplelist) do - for tag, index in pairs(data.mappings[format]) do + for i=1,#simplelist do + local format = simplelist[i] + for tag, index in next, data.mappings[format] do local s = specifications[index] simplemappings[tag] = { s.rawname, s.filename, s.subfont } end @@ -88,7 +89,7 @@ local function showfeatures(tag,specification) -- maybe more local features = fonts.get_features(specification.filename,specification.format) if features then - for what, v in table.sortedpairs(features) do + for what, v in table.sortedhash(features) do local data = features[what] if data and next(data) then logs.simple() @@ -96,9 +97,9 @@ local function showfeatures(tag,specification) logs.simple() logs.simple("feature script languages") logs.simple() - for f,ff in table.sortedpairs(data) do + for f,ff in table.sortedhash(data) do local done = false - for s, ss in table.sortedpairs(ff) do + for s, ss in table.sortedhash(ff) do if s == "*" then s = "all" end if ss ["*"] then ss["*"] = nil ss.all = true end if done then @@ -131,11 +132,13 @@ local function list_specifications(t,info) if t then local s = table.sortedkeys(t) if info then - for k,v in ipairs(s) do + for k=1,#s do + local v = s[k] showfeatures(v,t[v]) end else - for k,v in ipairs(s) do + for k=1,#s do + local v = s[k] local entry = t[v] s[k] = { entry.familyname or "<nofamily>", @@ -151,7 +154,8 @@ local function list_specifications(t,info) e[k] = entry end table.formatcolumns(s) - for k,v in ipairs(s) do + for k=1,#s do + local v = s[k] texio.write_nl(v) end end @@ -162,11 +166,13 @@ local function list_matches(t,info) if t then local s, w = table.sortedkeys(t), { 0, 0, 0 } if info then - for k,v in ipairs(s) do + for k=1,#s do + local v = s[k] showfeatures(v,t[v]) end else - for k,v in ipairs(s) do + for k=1,#s do + local v = s[k] local entry = t[v] s[k] = { v, @@ -176,8 +182,8 @@ local function list_matches(t,info) } end table.formatcolumns(s) - for k,v in ipairs(s) do - texio.write_nl(v) + for k=1,#s do + texio.write_nl(s[k]) end end end @@ -263,7 +269,8 @@ function scripts.fonts.save() if fontinfo then logs.simple("font: %s located as %s",name,filename) if fontinfo[1] then - for _, v in ipairs(fontinfo) do + for k=1,#fontinfo do + local v = fontinfo[k] save(v.fontname,fontloader.open(filename,v.fullname)) end else diff --git a/scripts/context/lua/mtx-grep.lua b/scripts/context/lua/mtx-grep.lua index a6617d711..9604bc9f8 100644 --- a/scripts/context/lua/mtx-grep.lua +++ b/scripts/context/lua/mtx-grep.lua @@ -70,7 +70,9 @@ function scripts.grep.find(pattern, files, offset) end local capture = (content/check)^0 for i=offset or 1, #files do - for _, nam in ipairs(dir.glob(files[i])) do + local globbed = dir.glob(files[i]) + for i=1,#globbed do + local nam = globbed[i] name = nam local data = io.loaddata(name) if data then diff --git a/scripts/context/lua/mtx-interface.lua b/scripts/context/lua/mtx-interface.lua index 56cc68a55..730a030d9 100644 --- a/scripts/context/lua/mtx-interface.lua +++ b/scripts/context/lua/mtx-interface.lua @@ -18,7 +18,8 @@ local messageinterfaces = { 'en','cs','de','it','nl','ro','fr','pe','no' } function flushers.scite(interface,collection) local result, i = {}, 0 result[#result+1] = format("keywordclass.macros.context.%s=",interface) - for _, command in ipairs(collection) do + for i=1,#collection do + local command = collection[i] if i==0 then result[#result+1] = "\\\n" i = 5 @@ -38,7 +39,8 @@ function flushers.jedit(interface,collection) result[#result+1] = "<MODE>" result[#result+1] = "\t<RULES>" result[#result+1] = "\t\t<KEYWORDS>" - for _, command in ipairs(collection) do + for i=1,#collection do + local command = collection[i] result[#result+1] = format("\t\t\t<KEYWORD2>%s</KEYWORD2>",command) end result[#result+1] = "\t\t</KEYWORDS>" @@ -52,7 +54,8 @@ function flushers.bbedit(interface,collection) result[#result+1] = "<?xml version='1.0'?>" result[#result+1] = "<key>BBLMKeywordList</key>" result[#result+1] = "<array>" - for _, command in ipairs(collection) do + for i=1,#collection do + local command = collection[i] result[#result+1] = format("\t<string>\\%s</string>",command) end result[#result+1] = "</array>" @@ -60,7 +63,8 @@ function flushers.bbedit(interface,collection) end function flushers.raw(interface,collection) - for _, command in ipairs(collection) do + for i=1,#collection do + local command = collection[i] logs.simple(command) end end @@ -74,7 +78,8 @@ function scripts.interface.editor(editor) if xmlfile == "" then logs.simple("unable to locate cont-en.xml") end - for _, interface in ipairs(interfaces) do + for i=1,#interfaces do + local interface = interfaces[i] local keyfile = resolvers.find_file(format("keys-%s.xml",interface)) or "" if keyfile == "" then logs.simple("unable to locate keys-*.xml") @@ -150,7 +155,9 @@ function scripts.interface.context() texresult[#texresult+1] = format("%% definitions for interface %s for language %s\n%%",what,language) xmlresult[#xmlresult+1] = format("\t<!-- definitions for interface %s for language %s -->\n",what,language) xmlresult[#xmlresult+1] = format("\t<cd:%s>",what) - for _, key in ipairs(table.sortedkeys(t)) do + local sorted = table.sortedkeys(t) + for i=1,#sorted do + local key = sorted[i] local v = t[key] local value = v[language] or v["en"] if not value then @@ -178,7 +185,7 @@ function scripts.interface.context() return a .. b .. c .. b end) end - for language, _ in pairs(commands.setuplayout) do + for language, _ in next, commands.setuplayout do local texresult, xmlresult = { }, { } texresult[#texresult+1] = format("%% this file is auto-generated, don't edit this file\n%%") xmlresult[#xmlresult+1] = format("<?xml version='1.0'?>\n",tag) @@ -216,7 +223,8 @@ function scripts.interface.messages() local filename = resolvers.find_file(environment.files[1] or "mult-mes.lua") or "" if filename ~= "" then local messages = dofile(filename) - for _, interface in ipairs(messageinterfaces) do + for i=1,#messageinterfaces do + local interface = messageinterfaces[i] local texresult = { } for category, data in next, messages do for tag, message in next, data do diff --git a/scripts/context/lua/mtx-modules.lua b/scripts/context/lua/mtx-modules.lua index 9ade1fc23..3a348593f 100644 --- a/scripts/context/lua/mtx-modules.lua +++ b/scripts/context/lua/mtx-modules.lua @@ -123,7 +123,9 @@ local suffixes = table.tohash { 'tex','mkii','mkiv','mp' } function scripts.modules.process(runtex) local processed = { } local prep = environment.argument("prep") - for _, shortname in ipairs(environment.files) do + local files = environment.files + for i=1,#files do + local shortname = files[i] local suffix = file.suffix(shortname) if suffixes[suffix] then local longname @@ -139,7 +141,8 @@ function scripts.modules.process(runtex) end end end - for _, name in ipairs(processed) do + for i=1,#processed do + local name = processed[i] logs.simple("modules","processed: %s",name) end end diff --git a/scripts/context/lua/mtx-mptopdf.lua b/scripts/context/lua/mtx-mptopdf.lua index c3d5b50dc..342ff1c28 100644 --- a/scripts/context/lua/mtx-mptopdf.lua +++ b/scripts/context/lua/mtx-mptopdf.lua @@ -86,7 +86,8 @@ function scripts.mptopdf.convertall() exit(1) end local report = { } - for _,fn in ipairs(files) do + for i=1,#files do + local fn = files[i] local success, name = scripts.mptopdf.aux.do_convert(fn) if success > 0 then report[#report+1] = { fn, name } @@ -95,7 +96,8 @@ function scripts.mptopdf.convertall() if #report > 0 then logs.simple("number of converted files: %i", #report) logs.simple("") - for _, r in ipairs(report) do + for i=1,#report do + local r = report[i] logs.simple("%s => %s", r[1], r[2]) end else diff --git a/scripts/context/lua/mtx-patterns.lua b/scripts/context/lua/mtx-patterns.lua index 9d5c2eb74..293016991 100644 --- a/scripts/context/lua/mtx-patterns.lua +++ b/scripts/context/lua/mtx-patterns.lua @@ -14,25 +14,25 @@ scripts.patterns = scripts.patterns or { } scripts.patterns.list = { { "??", "hyph-ar.tex", "arabic" }, { "bg", "hyph-bg.tex", "bulgarian" }, --- { "ca", "hyph-ca.tex", "" }, + { "ca", "hyph-ca.tex", "catalan" }, { "??", "hyph-cop.tex", "coptic" }, { "cs", "hyph-cs.tex", "czech" }, - { "??", "hyph-cy.tex", "welsh" }, + { "cy", "hyph-cy.tex", "welsh" }, { "da", "hyph-da.tex", "danish" }, { "deo", "hyph-de-1901.tex", "german, old spelling" }, { "de", "hyph-de-1996.tex", "german, new spelling" }, --~ { "??", "hyph-el-monoton.tex", "" }, --~ { "??", "hyph-el-polyton.tex", "" }, ---~ { "agr", "hyph-grc", "ancient greek" }, + { "agr", "hyph-grc", "ancient greek" }, --~ { "???", "hyph-x-ibycus", "ancient greek in ibycus encoding" }, --~ { "gr", "", "" }, - { "??", "hyph-eo.tex", "esperanto" }, + { "eo", "hyph-eo.tex", "esperanto" }, { "gb", "hyph-en-gb.tex", "british english" }, { "us", "hyph-en-us.tex", "american english" }, { "es", "hyph-es.tex", "spanish" }, { "et", "hyph-et.tex", "estonian" }, { "eu", "hyph-eu.tex", "basque" }, -- ba is Bashkir! - { "??", "hyph-fa.tex", "farsi" }, + { "fa", "hyph-fa.tex", "farsi" }, { "fi", "hyph-fi.tex", "finnish" }, { "fr", "hyph-fr.tex", "french" }, -- { "??", "hyph-ga.tex", "" }, @@ -43,12 +43,11 @@ scripts.patterns.list = { { "hu", "hyph-hu.tex", "hungarian" }, { "??", "hyph-ia.tex", "interlingua" }, { "??", "hyph-id.tex", "indonesian" }, - { "??", "hyph-is.tex", "icelandic" }, + { "is", "hyph-is.tex", "icelandic" }, { "it", "hyph-it.tex", "italian" }, { "la", "hyph-la.tex", "latin" }, { "lt", "hyph-lt.tex", "lithuanian" }, - { "??", "hyph-mn-cyrl.tex", "mongolian, cyrillic script" }, - { "??", "hyph-mn-cyrl-x-new.tex", "mongolian, cyrillic script (new patterns)" }, + { "mn", "hyph-mn-cyrl.tex", "mongolian, cyrillic script" }, { "nb", "hyph-nb.tex", "norwegian bokmål" }, { "nl", "hyph-nl.tex", "dutch" }, { "nn", "hyph-nn.tex", "norwegian nynorsk" }, @@ -56,14 +55,14 @@ scripts.patterns.list = { { "pt", "hyph-pt.tex", "portuguese" }, { "ro", "hyph-ro.tex", "romanian" }, { "ru", "hyph-ru.tex", "russian" }, - { "sk", "hyph-sk.tex", "" }, + { "sk", "hyph-sk.tex", "slovak" }, { "sl", "hyph-sl.tex", "slovenian" }, - { "??", "hyph-sr-cyrl.tex", "serbian" }, + { "sr", "hyph-sr-cyrl.tex", "serbian" }, { "sv", "hyph-sv.tex", "swedish" }, { "tr", "hyph-tr.tex", "turkish" }, { "tk", "hyph-tk.tex", "turkman" }, { "uk", "hyph-uk.tex", "ukrainian" }, - { "??", "hyph-zh-latn.tex", "zh-latn, chinese Pinyin" }, + { "zh", "hyph-zh-latn.tex", "zh-latn, chinese Pinyin" }, } @@ -154,7 +153,7 @@ function scripts.patterns.load(path,name,mnemonic,fullcheck) end h.patterns = nil h.hyphenation = nil - for k, v in pairs(h) do + for k, v in next, h do if not permitted_commands[k] then okay = false end if mnemonic then logs.simple("command \\%s found in language %s, file %s, n=%s",k,mnemonic,name,v) @@ -163,7 +162,7 @@ function scripts.patterns.load(path,name,mnemonic,fullcheck) end end if not environment.argument("fast") then - for k, v in pairs(c) do + for k, v in next, c do if mnemonic then logs.simple("command \\%s found in comment of language %s, file %s, n=%s",k,mnemonic,name,v) else @@ -223,7 +222,7 @@ function scripts.patterns.load(path,name,mnemonic,fullcheck) end end local stripped = { } - for k, v in pairs(p) do + for k, v in next, p do if mnemonic then logs.simple("invalid character %s (0x%04X) in patterns of language %s, file %s, n=%s",char(k),k,mnemonic,name,v) else @@ -235,7 +234,7 @@ function scripts.patterns.load(path,name,mnemonic,fullcheck) stripped[k] = true end end - for k, v in pairs(h) do + for k, v in next, h do if mnemonic then logs.simple("invalid character %s (0x%04X) in exceptions of language %s, file %s, n=%s",char(k),k,mnemonic,name,v) else @@ -248,7 +247,7 @@ function scripts.patterns.load(path,name,mnemonic,fullcheck) end end local stripset = "" - for k, v in pairs(stripped) do + for k, v in next, stripped do logs.simple("entries that contain character %s will be omitted",char(k)) stripset = stripset .. "%" .. char(k) end @@ -294,8 +293,10 @@ end function scripts.patterns.check() local path = environment.argument("path") or "." local found = false - if #environment.files > 0 then - for _, name in ipairs(environment.files) do + local files = environment.files + if #files > 0 then + for i=1,#files do + local name = files[i] logs.simple("checking language file %s", name) local okay = scripts.patterns.load(path,name,nil,not environment.argument("fast")) if #environment.files > 1 then @@ -303,7 +304,7 @@ function scripts.patterns.check() end end else - for k, v in pairs(scripts.patterns.list) do + for k, v in next, scripts.patterns.list do local mnemonic, name = v[1], v[2] logs.simple("checking language %s, file %s", mnemonic, name) local okay = scripts.patterns.load(path,name,mnemonic,not environment.argument("fast")) @@ -324,7 +325,7 @@ function scripts.patterns.convert() if path == destination then logs.simple("source path and destination path should differ (use --path and/or --destination)") else - for k, v in pairs(scripts.patterns.list) do + for k, v in next, scripts.patterns.list do local mnemonic, name = v[1], v[2] logs.simple("converting language %s, file %s", mnemonic, name) local okay, patterns, hyphenations, comment, stripped, pused, hused = scripts.patterns.load(path,name,false) @@ -362,3 +363,4 @@ end -- mtxrun --script pattern --check --path=c:/data/develop/svn-hyphen/trunk/hyph-utf8/tex/generic/hyph-utf8/patterns -- mtxrun --script pattern --check --fast --path=c:/data/develop/svn-hyphen/trunk/hyph-utf8/tex/generic/hyph-utf8/patterns -- mtxrun --script pattern --convert --path=c:/data/develop/svn-hyphen/trunk/hyph-utf8/tex/generic/hyph-utf8/patterns --destination=e:/tmp/patterns +-- mtxrun --script pattern --convert --path=c:/data/develop/svn-hyphen/branches/luatex/hyph-utf8/tex/generic/hyph-utf8/patterns/tex --destination=e:/tmp/patterns diff --git a/scripts/context/lua/mtx-profile.lua b/scripts/context/lua/mtx-profile.lua index 9e2aed288..11d48d039 100644 --- a/scripts/context/lua/mtx-profile.lua +++ b/scripts/context/lua/mtx-profile.lua @@ -54,9 +54,13 @@ function scripts.profiler.analyse(filename) f:close() print("") local loaded = { } - for _, filename in ipairs(table.sortedkeys(times)) do + local sortedtable.sortedkeys(times) + for i=1,#sorted do + local filename = sorted[i] local functions = times[filename] - for _, functionname in ipairs(table.sortedkeys(functions)) do + local sorted = table.sortedkeys(functions) + for i=1,#sorted do + local functionname = sorted[i] local totaltime = functions[functionname] local count = counts[functionname] totalcount = totalcount + count @@ -81,7 +85,9 @@ function scripts.profiler.analyse(filename) end end print("") - for _, call in ipairs(table.sortedkeys(calls)) do + local sorted = table.sortedkeys(calls) + for i=1,#sorted do + local call = sorted[i] local n = calls[call] totalcalls = totalcalls + n if n > callthreshold then diff --git a/scripts/context/lua/mtx-scite.lua b/scripts/context/lua/mtx-scite.lua index 6071bed3a..d5f0a5344 100644 --- a/scripts/context/lua/mtx-scite.lua +++ b/scripts/context/lua/mtx-scite.lua @@ -28,7 +28,8 @@ function scripts.scite.start(indeed) fontpath = "" end local binpaths = file.split_path(os.getenv("PATH")) or file.split_path(os.getenv("path")) - for _, scitesignal in ipairs(scitesignals) do + for i=1,#scitesignals do + local scitesignal = scitesignals[i] local scitepath = resolvers.find_file(scitesignal,"other text files") or "" if scitepath ~= "" then scitepath = file.dirname(scitepath) -- data @@ -83,11 +84,11 @@ function scripts.scite.start(indeed) logdata[#logdata+1] = { "up to date : %s", basename } end end - for _, property in ipairs(properties) do - check_state(property,userpath) + for i=1,#properties do + check_state(properties[i],userpath) end - for _, luafile in ipairs(luafiles) do - check_state(luafile,userpath) + for i=1,#luafiles do + check_state(luafiles[i],userpath) end if fontpath ~= "" then check_state(extrafont,fontpath) @@ -127,14 +128,16 @@ function scripts.scite.start(indeed) end if #logdata > 0 then logs.simple("") - for k,v in ipairs(logdata) do + for k=1,#logdata do + local v = logdata[k] logs.simple(v[1],v[2]) end end if indeed then if #tobecopied > 0 then logs.simple("warning : copying updated files") - for _, what in ipairs(tobecopied) do + for i=1,#tobecopied do + local what = tobecopied[i] logs.simple("copying : '%s' => '%s'",what[1],what[2]) file.copy(what[1],what[2]) end diff --git a/scripts/context/lua/mtx-server-ctx-fonttest.lua b/scripts/context/lua/mtx-server-ctx-fonttest.lua index af4d778cf..b2a993bf8 100644 --- a/scripts/context/lua/mtx-server-ctx-fonttest.lua +++ b/scripts/context/lua/mtx-server-ctx-fonttest.lua @@ -21,8 +21,11 @@ local temppath = caches.setpath("temp","mtx-server-ctx-fonttest") local basename = "mtx-server-ctx-fonttest-data.lua" local basepath = temppath -for _, suffix in ipairs { "tex", "pdf", "log" } do - os.remove(file.join(temppath,file.addsuffix(tempname,suffix))) +local remove_suffixes = { "tex", "pdf", "log" } +local what_options = { "trace", "basemode" } + +for i=1,#remove_suffixes do + os.remove(file.join(temppath,file.addsuffix(tempname,remove_suffixes[i]))) end local process_templates = { } @@ -167,18 +170,18 @@ local function showfeatures(f) local function show(what) local data = features[what] if data and next(data) then - for f,ff in pairs(data) do + for f,ff in next, data do if find(f,"<") then -- ignore aat for the moment else fea[f] = true - for s, ss in pairs(ff) do + for s, ss in next, ff do if find(s,"%*") then -- ignore * else scr[s] = true local rs = rev[s] if not rs then rs = {} rev[s] = rs end - for k, l in pairs(ss) do + for k, l in next, ss do if find(k,"%*") then -- ignore * else @@ -193,16 +196,16 @@ local function showfeatures(f) end end end - for what, v in table.sortedpairs(features) do + for what, v in table.sortedhash(features) do show(what) end local stupid = { } stupid[#stupid+1] = "var feature_hash = new Array ;" - for s, sr in pairs(rev) do + for s, sr in next, rev do stupid[#stupid+1] = format("feature_hash['%s'] = new Array ;",s) - for l, lr in pairs(sr) do + for l, lr in next, sr do stupid[#stupid+1] = format("feature_hash['%s']['%s'] = new Array ;",s,l) - for f, fr in pairs(lr) do + for f, fr in next, lr do stupid[#stupid+1] = format("feature_hash['%s']['%s']['%s'] = true ;",s,l,f) end end @@ -244,7 +247,7 @@ local function select_font() local listoffonts = { } listoffonts[#listoffonts+1] = "<table>" listoffonts[#listoffonts+1] = template_h - for k, v in table.sortedpairs(t) do + for k, v in table.sortedhash(t) do local kind = v.format if kind == "otf" or kind == "ttf" or kind == "ttc" then local fontname = v.fontname @@ -297,7 +300,9 @@ local function edit_font(currentfont,detail,tempname) local htmldata = showfeatures(specification.filename) if htmldata then local features, languages, scripts, options = { }, { }, { }, { } - for k,v in ipairs(table.sortedkeys(htmldata.scripts)) do + local sorted = table.sortedkeys(htmldata.scripts) + for k=1,#sorted do + local v = sorted[k] local s = fonts.otf.tables.scripts[v] or v if detail and v == detail.script then scripts[#scripts+1] = format("<input title='%s' id='s-%s' type='radio' name='script' value='%s' onclick='check_script()' checked='checked'/> <span id='t-s-%s'>%s</span>",s,v,v,v,v) @@ -305,7 +310,9 @@ local function edit_font(currentfont,detail,tempname) scripts[#scripts+1] = format("<input title='%s' id='s-%s' type='radio' name='script' value='%s' onclick='check_script()' /> <span id='t-s-%s'>%s</span>",s,v,v,v,v) end end - for k,v in ipairs(table.sortedkeys(htmldata.languages)) do + local sorted = table.sortedkeys(htmldata.languages) + for k=1,#sorted do + local v = sorted[k] local l = fonts.otf.tables.languages[v] or v if detail and v == detail.language then languages[#languages+1] = format("<input title='%s' id='l-%s' type='radio' name='language' value='%s' onclick='check_language()' checked='checked'/> <span id='t-l-%s'>%s</span>",l,v,v,v,v) @@ -313,7 +320,9 @@ local function edit_font(currentfont,detail,tempname) languages[#languages+1] = format("<input title='%s' id='l-%s' type='radio' name='language' value='%s' onclick='check_language()' /> <span id='t-l-%s'>%s</span>",l,v,v,v,v) end end - for k,v in ipairs(table.sortedkeys(htmldata.features)) do + local sorted = table.sortedkeys(htmldata.features) + for k=1,#sorted do + local v = sorted[k] local f = fonts.otf.tables.features[v] or v if detail and detail["f-"..v] then features[#features+1] = format("<input title='%s' id='f-%s' type='checkbox' name='f-%s' onclick='check_feature()' checked='checked'/> <span id='t-f-%s'>%s</span>",f,v,v,v,v) @@ -321,7 +330,8 @@ local function edit_font(currentfont,detail,tempname) features[#features+1] = format("<input title='%s' id='f-%s' type='checkbox' name='f-%s' onclick='check_feature()' /> <span id='t-f-%s'>%s</span>",f,v,v,v,v) end end - for k, v in ipairs { "trace", "basemode" } do + for k=1,#what_options do + local v = what_options[k] if detail and detail["o-"..v] then options[#options+1] = format("<input id='o-%s' type='checkbox' name='o-%s' checked='checked'/> %s",v,v,v) else @@ -352,7 +362,7 @@ local function process_font(currentfont,detail) -- maybe just fontname format("language=%s",detail.language or "dflt"), format("script=%s",detail.script or "dflt"), } - for k,v in pairs(detail) do + for k,v in next, detail do local f = match(k,"^f%-(.*)$") if f then features[#features+1] = format("%s=yes",f) @@ -418,15 +428,15 @@ local function show_font(currentfont,detail) result[#result+1] = format("<tr><td class='tc'>width: </td><td>%s</td></tr>",specification.width ~= "" and specification.width or "normal") result[#result+1] = "</table>" if features then - for what, v in table.sortedpairs(features) do + for what, v in table.sortedhash(features) do local data = features[what] if data and next(data) then result[#result+1] = format("<h1>%s features</h1>",what) result[#result+1] = "<table>" result[#result+1] = "<tr><th>feature</th><th>tag </th><th>script </th><th>languages </th></tr>" - for f,ff in table.sortedpairs(data) do + for f,ff in table.sortedhash(data) do local done = false - for s, ss in table.sortedpairs(ff) do + for s, ss in table.sortedhash(ff) do if s == "*" then s = "all" end if ss ["*"] then ss["*"] = nil ss.all = true end if done then @@ -504,10 +514,10 @@ local function loadstored(detail,currentfont,name) detail.title = storage.title or detail.title detail.sampletext = storage.text or detail.sampletext detail.name = name or "no name" - for k,v in pairs(storage.features) do + for k,v in next, storage.features do detail["f-"..k] = v end - for k,v in pairs(storage.options) do + for k,v in next, storage.options do detail["o-"..k] = v end end @@ -542,10 +552,11 @@ local function save_font(currentfont,detail) text = string.strip(detail.sampletext or text) name = string.strip(detail.name or name) title = string.strip(detail.title or title) - for k,v in pairs(htmldata.features) do + for k,v in next, htmldata.features do if detail["f-"..k] then features[k] = true end end - for k,v in ipairs { "trace", "basemode" } do + for k=1,#what_options do + local v = what_options[k] if detail["o-"..v] then options[k] = true end end end @@ -565,7 +576,7 @@ local function load_font(currentfont) local storage = loadbase(datafile) local result = {} result[#result+1] = format("<tr><th>del </th><th>name </th><th>font </th><th>fontname </th><th>script </th><th>language </th><th>features </th><th>title </th><th>sampletext </th></tr>") - for k,v in table.sortedpairs(storage) do + for k,v in table.sortedhash(storage) do local fontname, fontfile = get_specification(v.font) result[#result+1] = format("<tr><td><a href='mtx-server-ctx-fonttest.lua?deletename=%s'>x</a> </td><td><a href='mtx-server-ctx-fonttest.lua?loadname=%s'>%s</a> </td><td>%s </td<td>%s </td><td>%s </td><td>%s </td><td>%s </td><td>%s </td><td>%s </td></tr>", k,k,k,v.font,fontname,v.script,v.language,concat(table.sortedkeys(v.features)," "),v.title or "no title",v.text or "") @@ -653,9 +664,12 @@ function doit(configuration,filename,hashed) -- lua table and adapt - local menu = { } - for k, v in ipairs { 'process', 'select', 'save', 'load', 'edit', 'reset', 'features', 'source', 'log', 'info', 'extras'} do - menu[#menu+1] = format("<button name='action' value='%s' type='submit'>%s</button>",v,v) + local buttons = { 'process', 'select', 'save', 'load', 'edit', 'reset', 'features', 'source', 'log', 'info', 'extras'} + local menu = { } + + for i=1,#buttons do + local button = buttons[i] + menu[#menu+1] = format("<button name='action' value='%s' type='submit'>%s</button>",button,button) end variables.menu = concat(menu," ") diff --git a/scripts/context/lua/mtx-server-ctx-help.lua b/scripts/context/lua/mtx-server-ctx-help.lua index 4416a0ec1..2f072f977 100644 --- a/scripts/context/lua/mtx-server-ctx-help.lua +++ b/scripts/context/lua/mtx-server-ctx-help.lua @@ -409,8 +409,9 @@ end function document.setups.showused() local current = document.setups.current if current.root and next(current.used) then - for k,v in ipairs(table.sortedkeys(current.used)) do - xml.sprint(current.used[v]) + local sorted = table.sortedkeys(current.used) + for i=1,#sorted do + xml.sprint(current.used[sorted[i]]) end end end @@ -421,8 +422,9 @@ function document.setups.showall() for e in xml.collected(current.root,"cd:command") do list[document.setups.name(e)] = e end - for k,v in ipairs(table.sortedkeys(list)) do - xml.sprint(list[v]) + local sorted = table.sortedkeys(list) + for i=1,#sorted do + xml.sprint(list[sorted[i]]) end end end @@ -599,11 +601,14 @@ local function doit(configuration,filename,hashed) local result = { content = "error" } local names, refs, ints = document.setups.names(lastinterface), { }, { } - for k,v in ipairs(names) do + for k=1,#names do + local v = names[k] refs[k] = formats.href_in_list[lastmode]:format(v[1],lastmode,v[2]) end if lastmode ~= 2 then - for k,v in ipairs(table.sortedkeys(interfaces)) do + local sorted = table.sortedkeys(interfaces) + for k=1,#sorted do + local v = sorted[k] ints[k] = formats.interface:format(interfaces[v],lastmode,v) end end @@ -634,8 +639,9 @@ local function doit(configuration,filename,hashed) elseif lastcommand and lastcommand ~= "" then local data = document.setups.collect(lastcommand,lastinterface,lastmode) if data then - local extra = { } - for k, v in ipairs { "environment", "category", "source", "mode" } do + local what, extra = { "environment", "category", "source", "mode" }, { } + for k=1,#what do + local v = what[k] if data[v] and data[v] ~= "" then lmx.set(v, data[v]) extra[#extra+1] = v .. ": " .. data[v] diff --git a/scripts/context/lua/mtx-server.lua b/scripts/context/lua/mtx-server.lua index 871354394..dc0befcaa 100644 --- a/scripts/context/lua/mtx-server.lua +++ b/scripts/context/lua/mtx-server.lua @@ -252,7 +252,8 @@ function scripts.webserver.run(configuration) end -- locate root and index file in tex tree if not lfs.isdir(configuration.root) then - for _, name in ipairs(indices) do + for i=1,#indices do + local name = indices[i] local root = resolvers.resolve("path:" .. name) or "" if root ~= "" then configuration.root = root @@ -263,7 +264,8 @@ function scripts.webserver.run(configuration) end configuration.root = dir.expand_name(configuration.root) if not configuration.index then - for _, name in ipairs(indices) do + for i=1,#indices do + local name = indices[i] if lfs.isfile(file.join(configuration.root,name)) then configuration.index = name -- we will prepend the rootpath later break diff --git a/scripts/context/lua/mtx-texworks.lua b/scripts/context/lua/mtx-texworks.lua index 427bc9eff..73ab846cd 100644 --- a/scripts/context/lua/mtx-texworks.lua +++ b/scripts/context/lua/mtx-texworks.lua @@ -67,8 +67,8 @@ function scripts.texworks.start(indeed) logs.simple("unable to locate %s",workname) return false end - for _, subpath in ipairs(texworkspaths) do - dir.makedirs(file.join(datapath,subpath)) + for i=1,#texworkspaths do + dir.makedirs(file.join(datapath,texworkspaths[i])) end os.setenv("TW_INIPATH",datapath) os.setenv("TW_LIBPATH",datapath) diff --git a/scripts/context/lua/mtx-timing.lua b/scripts/context/lua/mtx-timing.lua index e0ea670f3..40e33cdae 100644 --- a/scripts/context/lua/mtx-timing.lua +++ b/scripts/context/lua/mtx-timing.lua @@ -55,13 +55,15 @@ local html_menu = [[ local directrun = true +local what = { "parameters", "nodes" } + function plugins.progress.make_svg(filename,other) local metadata, menudata, c = { }, { }, 0 metadata[#metadata+1] = 'outputformat := "svg" ;' - for _, kind in pairs { "parameters", "nodes" } do - local mdk = { } + for i=1,#what do + local kind, mdk = what[i], { } menudata[kind] = mdk - for n, name in pairs(plugins.progress[kind](filename)) do + for n, name in next, plugins.progress[kind](filename) do local first = plugins.progress.path(filename,name) local second = plugins.progress.path(filename,other) c = c + 1 @@ -91,11 +93,12 @@ end function plugins.progress.makehtml(filename,other,menudata,metadata) local graphics = { } local result = { graphics = graphics } - for _, kind in pairs { "parameters", "nodes" } do + for i=1,#what do + local kind, menu = what[i], { } local md = menudata[kind] - local menu = { } result[kind] = menu - for k, v in ipairs(md) do + for k=1,#md do + local v = md[k] local name, number = v[1], v[2] local min = plugins.progress.bot(filename,name) local max = plugins.progress.top(filename,name) diff --git a/scripts/context/lua/mtx-tools.lua b/scripts/context/lua/mtx-tools.lua index 5614b3c14..bf4add168 100644 --- a/scripts/context/lua/mtx-tools.lua +++ b/scripts/context/lua/mtx-tools.lua @@ -15,7 +15,9 @@ local bomb_1, bomb_2 = "^\254\255", "^\239\187\191" function scripts.tools.disarmutfbomb() local force, done = environment.argument("force"), false - for _, name in ipairs(environment.files) do + local files = environment.files + for i=1,#files do + local name = files[i] if lfs.isfile(name) then local data = io.loaddata(name) if not data then @@ -90,7 +92,7 @@ function scripts.tools.dirtoxml() local function flush(list,result,n,path) n, result = n or 1, result or { } local d = rep(" ",n) - for name, attr in table.sortedpairs(list) do + for name, attr in table.sortedhash(list) do local mode = attr.mode if mode == "file" then result[#result+1] = format("%s<file name='%s'>",d,(longname and path and join(path,name)) or name) diff --git a/scripts/context/lua/mtx-update.lua b/scripts/context/lua/mtx-update.lua index b63be6475..0a65595d3 100644 --- a/scripts/context/lua/mtx-update.lua +++ b/scripts/context/lua/mtx-update.lua @@ -207,8 +207,8 @@ function scripts.update.synchronize() local function collection_to_list_of_folders(collection, platform) local archives = {} - for _, c in ipairs(collection) do - local archive = c[1] + for i=1,#collection do + local archive = collection[i][1] archive = archive:gsub("<platform>", platform) archive = archive:gsub("<version>", version) archives[#archives+1] = archive @@ -264,7 +264,8 @@ function scripts.update.synchronize() local available_modules = get_list_of_files_from_rsync({"modules/"}) -- hash of requested modules -- local h = table.tohash(extras:split(",")) - for _, s in ipairs(available_modules) do + for i=1,#available_modules do + local s = available_modules[i] -- if extras == "all" or h[s] then if extras.all or extras[s] then scripts.update.modules[#scripts.update.modules+1] = { format("modules/%s/",s), "texmf-context" } @@ -278,7 +279,8 @@ function scripts.update.synchronize() if collection and platform then platform = scripts.update.platforms[platform] if platform then - for _, c in ipairs(collection) do + for i=1,#collection do + local c = collection[i] local archive = c[1]:gsub("<platform>", platform) local destination = format("%s/%s", texroot, c[2]:gsub("<platform>", platform)) destination = destination:gsub("\\","/") @@ -292,30 +294,32 @@ function scripts.update.synchronize() end end - for platform, _ in pairs(platforms) do + for platform, _ in next, platforms do add_collection(scripts.update.base,platform) end - for platform, _ in pairs(platforms) do + for platform, _ in next, platforms do add_collection(scripts.update.modules,platform) end - for engine, _ in pairs(engines) do - for platform, _ in pairs(platforms) do + for engine, _ in next, engines do + for platform, _ in next, platforms do add_collection(scripts.update.engines[engine],platform) end end if goodies and type(goodies) == "table" then - for goodie, _ in pairs(goodies) do - for platform, _ in pairs(platforms) do + for goodie, _ in next, goodies do + for platform, _ in next, platforms do add_collection(scripts.update.goodies[goodie],platform) end end end local combined = { } - for _, repository in ipairs(scripts.update.repositories) do + local update_repositories = scripts.update.repositories + for i=1,#update_repositories do + local repository = update_repositories if repositories[repository] then - for _, v in pairs(individual) do + for _, v in next, individual do local archive, destination = v[1], v[2] local cd = combined[destination] if not cd then @@ -327,14 +331,14 @@ function scripts.update.synchronize() end end if logs.verbose then - for k, v in pairs(combined) do + for k, v in next, combined do logs.report("update", k) - for k,v in ipairs(v) do - logs.report("update", " <= " .. v) + for i=1,#v do + logs.report("update", " <= " .. v[i]) end end end - for destination, archive in pairs(combined) do + for destination, archive in next, combined do local archives, command = concat(archive," "), "" -- local normalflags, deleteflags = states.get("rsync.flags.normal"), states.get("rsync.flags.delete") -- if environment.argument("keep") or destination:find("%.$") then @@ -376,7 +380,7 @@ function scripts.update.synchronize() end end - for platform, _ in pairs(platforms) do + for platform, _ in next, platforms do update_script('luatools',platform) update_script('mtxrun',platform) end @@ -400,7 +404,7 @@ end function table.fromhash(t) local h = { } - for k, v in pairs(t) do -- no ipairs here + for k, v in next, t do -- not indexed if v then h[#h+1] = k end end return h @@ -426,19 +430,19 @@ function scripts.update.make() local askedformats = formats local texformats = table.tohash(scripts.update.texformats) local mpformats = table.tohash(scripts.update.mpformats) - for k,v in pairs(texformats) do + for k,v in next, texformats do if not askedformats[k] then texformats[k] = nil end end - for k,v in pairs(mpformats) do + for k,v in next, mpformats do if not askedformats[k] then mpformats[k] = nil end end local formatlist = concat(table.fromhash(texformats), " ") if formatlist ~= "" then - for engine in pairs(engines) do + for engine in next, engines do if engine == "luatex" then scripts.update.run(format("context --make")) -- maybe also formatlist else @@ -511,7 +515,7 @@ if scripts.savestate then local valid = scripts.update.engines for r in gmatch(environment.argument("engine") or "all","([^, ]+)") do if r == "all" then - for k, v in pairs(valid) do + for k, v in next, valid do if k ~= "all" then states.set("engines." .. k, true) end diff --git a/scripts/context/lua/mtx-watch.lua b/scripts/context/lua/mtx-watch.lua index 8864b44ba..10f01cf86 100644 --- a/scripts/context/lua/mtx-watch.lua +++ b/scripts/context/lua/mtx-watch.lua @@ -10,7 +10,7 @@ scripts = scripts or { } scripts.watch = scripts.watch or { } local format, concat, difftime, time = string.format, table.concat, os.difftime, os.time -local pairs, ipairs, next, type = pairs, ipairs, next, type +local next, type = next, type -- the machine/instance matches the server app we use @@ -23,7 +23,7 @@ function scripts.watch.save_exa_modes(joblog,ctmname) local t= { } t[#t+1] = "<?xml version='1.0' standalone='yes'?>\n" t[#t+1] = "<exa:variables xmlns:exa='htpp://www.pragma-ade.com/schemas/exa-variables.rng'>" - for k, v in pairs(joblog.values) do + for k, v in next, joblog.values do t[#t+1] = format("\t<exa:variable label='%s'>%s</exa:variable>", k, tostring(v)) end t[#t+1] = "</exa:variables>" @@ -87,21 +87,22 @@ function scripts.watch.watch() if #paths > 0 then if environment.argument("automachine") then logpath = string.gsub(logpath,"/machine/","/"..machine.."/") - for i, path in ipairs(paths) do - paths[i] = string.gsub(path,"/machine/","/"..machine.."/") + for i=1,#paths do + paths[i] = string.gsub(paths[i],"/machine/","/"..machine.."/") end end - for _, path in ipairs(paths) do - logs.report("watch", "watching path ".. path) + for i=1,#paths do + logs.report("watch", "watching path ".. paths[i]) end local function process() local done = false - for _, path in ipairs(paths) do + for i=1,#paths do + local path = paths[i] lfs.chdir(path) local files = { } glob(files,path) table.sort(files) -- what gets sorted here, todo: by time - for name, time in pairs(files) do + for name, time in next, files do --~ local ok, joblog = xpcall(function() return dofile(name) end, function() end ) local ok, joblog = pcall(dofile,name) if ok and joblog then @@ -210,12 +211,14 @@ function scripts.watch.watch() local delta = difftime(currenttime,lasttime) if delta > cleanupdelay then lasttime = currenttime - for _, path in ipairs(paths) do + for i=1,#paths do + local path = paths[i] if string.find(path,"%.") then -- safeguard, we want a fully qualified path else local files = dir.glob(file.join(path,"*")) - for _, name in ipairs(files) do + for i=1,#files do + local name = files[i] local filetime = lfs.attributes(name,"modification") local delta = difftime(currenttime,filetime) if delta > cleanupdelay then @@ -255,10 +258,11 @@ function scripts.watch.collect_logs(path) -- clean 'm up too local files = dir.globfiles(path,false,"^%d+%.lua$") local collection = { } local valid = table.tohash({"filename","result","runtime","size","status"}) - for _, name in ipairs(files) do + for i=1,#files do + local name = files[i] local t = dofile(name) if t and type(t) == "table" and t.status then - for k, v in pairs(t) do + for k, v in next, t do if not valid[k] then t[k] = nil end @@ -270,20 +274,20 @@ function scripts.watch.collect_logs(path) -- clean 'm up too end function scripts.watch.save_logs(collection,path) -- play safe - if collection and not table.is_empty(collection) then + if collection and next(collection) then path = path or environment.argument("logpath") or "" path = (path == "" and ".") or path local filename = format("%s/collected-%s.lua",path,tostring(time())) io.savedata(filename,table.serialize(collection,true)) local check = dofile(filename) - for k,v in pairs(check) do + for k,v in next, check do if not collection[k] then logs.error("watch", "error in saving file") os.remove(filename) return false end end - for k,v in pairs(check) do + for k,v in next, check do os.remove(format("%s.lua",k)) end return true @@ -297,10 +301,11 @@ function scripts.watch.collect_collections(path) -- removes duplicates path = (path == "" and ".") or path local files = dir.globfiles(path,false,"^collected%-%d+%.lua$") local collection = { } - for _, name in ipairs(files) do + for i=1,#files do + local name = files[i] local t = dofile(name) if t and type(t) == "table" then - for k, v in pairs(t) do + for k, v in next, t do collection[k] = v end end @@ -311,12 +316,14 @@ end function scripts.watch.show_logs(path) -- removes duplicates local collection = scripts.watch.collect_collections(path) or { } local max = 0 - for k,v in pairs(collection) do + for k,v in next, collection do v = v.filename or "?" if #v > max then max = #v end end - print(max) - for k,v in ipairs(table.sortedkeys(collection)) do + -- print(max) + local sorted = table.sortedkeys(collection) + for k=1,#sorted do + local v = sorted[k] local c = collection[v] local f, s, r, n = c.filename or "?", c.status or "?", c.runtime or 0, c.size or 0 logs.report("watch", format("%s %s %3i %8i %s",string.padd(f,max," "),string.padd(s,10," "),r,n,v)) @@ -335,7 +342,8 @@ function scripts.watch.cleanup_stale_files() -- removes duplicates logs.report("watch","dryrun, use --force for real cleanup") local files = dir.glob(file.join(path,"*")) local rtime = time() - for _, name in ipairs(files) do + for i=1,#files do + local name = files[i] local mtime = lfs.attributes(name,"modification") local delta = difftime(rtime,mtime) if delta > delay then diff --git a/scripts/context/lua/mtxrun.lua b/scripts/context/lua/mtxrun.lua index 727099027..d3e50e00e 100644 --- a/scripts/context/lua/mtxrun.lua +++ b/scripts/context/lua/mtxrun.lua @@ -278,7 +278,7 @@ function string:totable() return lpegmatch(pattern,self) end ---~ for _, str in ipairs { +--~ local t = { --~ "1234567123456712345671234567", --~ "a\tb\tc", --~ "aa\tbb\tcc", @@ -286,7 +286,10 @@ end --~ "aaaa\tbbbb\tcccc", --~ "aaaaa\tbbbbb\tccccc", --~ "aaaaaa\tbbbbbb\tcccccc", ---~ } do print(string.tabtospace(str)) end +--~ } +--~ for k,v do +--~ print(string.tabtospace(t[k])) +--~ end function string.tabtospace(str,tab) -- we don't handle embedded newlines @@ -455,6 +458,8 @@ function string:split(separator) return match(c,self) end +lpeg.splitters = cache + local cache = { } function lpeg.checkedsplit(separator,str) @@ -519,7 +524,7 @@ table.join = table.concat local concat, sort, insert, remove = table.concat, table.sort, table.insert, table.remove local format, find, gsub, lower, dump, match = string.format, string.find, string.gsub, string.lower, string.dump, string.match local getmetatable, setmetatable = getmetatable, setmetatable -local type, next, tostring, tonumber, ipairs, pairs = type, next, tostring, tonumber, ipairs, pairs +local type, next, tostring, tonumber, ipairs = type, next, tostring, tonumber, ipairs local unpack = unpack or table.unpack function table.strip(tab) @@ -586,7 +591,7 @@ end table.sortedkeys = sortedkeys table.sortedhashkeys = sortedhashkeys -function table.sortedpairs(t) +function table.sortedhash(t) local s = sortedhashkeys(t) -- maybe just sortedkeys local n = 0 local function kv(s) @@ -597,6 +602,8 @@ function table.sortedpairs(t) return kv, s end +table.sortedpairs = table.sortedhash + function table.append(t, list) for _,v in next, list do insert(t,v) @@ -719,18 +726,18 @@ end -- slower than #t on indexed tables (#t only returns the size of the numerically indexed slice) -function table.is_empty(t) +function table.is_empty(t) -- obolete, use inline code instead return not t or not next(t) end -function table.one_entry(t) +function table.one_entry(t) -- obolete, use inline code instead local n = next(t) return n and not next(t,n) end -function table.starts_at(t) - return ipairs(t,1)(t,0) -end +--~ function table.starts_at(t) -- obsolete, not nice +--~ return ipairs(t,1)(t,0) +--~ end function table.tohash(t,value) local h = { } @@ -834,7 +841,7 @@ local function do_serialize(root,name,depth,level,indexed) end -- we could check for k (index) being number (cardinal) if root and next(root) then - local first, last = nil, 0 -- #root cannot be trusted here + local first, last = nil, 0 -- #root cannot be trusted here (will be ok in 5.2 when ipairs is gone) if compact then -- NOT: for k=1,#root do (we need to quit at nil) for k,v in ipairs(root) do -- can we use next? @@ -1543,13 +1550,14 @@ function io.ask(question,default,options) elseif not options then return answer else - for _,v in pairs(options) do - if v == answer then + for k=1,#options do + if options[k] == answer then return answer end end local pattern = "^" .. answer - for _,v in pairs(options) do + for k=1,#options do + local v = options[k] if find(v,pattern) then return v end @@ -2323,7 +2331,7 @@ function file.splitname(str) -- returns drive, path, base, suffix return lpegmatch(pattern,str) end --- function test(t) for k, v in pairs(t) do print(v, "=>", file.splitname(v)) end end +-- function test(t) for k, v in next, t do print(v, "=>", file.splitname(v)) end end -- -- test { "c:", "c:/aa", "c:/aa/bb", "c:/aa/bb/cc", "c:/aa/bb/cc.dd", "c:/aa/bb/cc.dd.ee" } -- test { "c:", "c:aa", "c:aa/bb", "c:aa/bb/cc", "c:aa/bb/cc.dd", "c:aa/bb/cc.dd.ee" } @@ -2768,8 +2776,9 @@ local make_indeed = true -- false if string.find(os.getenv("PATH"),";") then -- os.type == "windows" function dir.mkdirs(...) - local str, pth = "", "" - for _, s in ipairs({...}) do + local str, pth, t = "", "", { ... } + for i=1,#t do + local s = t[i] if s ~= "" then if str ~= "" then str = str .. "/" .. s @@ -2867,8 +2876,9 @@ if string.find(os.getenv("PATH"),";") then -- os.type == "windows" else function dir.mkdirs(...) - local str, pth = "", "" - for _, s in ipairs({...}) do + local str, pth, t = "", "", { ... } + for i=1,#t do + local s = t[i] if s ~= "" then if str ~= "" then str = str .. "/" .. s @@ -3049,6 +3059,7 @@ if not modules then modules = { } end modules ['l-utils'] = { local gsub = string.gsub local concat = table.concat +local type, next = type, next if not utils then utils = { } end if not utils.merger then utils.merger = { } end @@ -3124,9 +3135,10 @@ function utils.merger._self_libs_(libs,list) if type(libs) == 'string' then libs = { libs } end if type(list) == 'string' then list = { list } end local foundpath = nil - for _, lib in ipairs(libs) do - for _, pth in ipairs(list) do - pth = gsub(pth,"\\","/") -- file.clean_path + for i=1,#libs do + local lib = libs[i] + for j=1,#list do + local pth = gsub(list[j],"\\","/") -- file.clean_path utils.report("checking library path %s",pth) local name = pth .. "/" .. lib if lfs.isfile(name) then @@ -3138,7 +3150,8 @@ function utils.merger._self_libs_(libs,list) if foundpath then utils.report("using library path %s",foundpath) local right, wrong = { }, { } - for _, lib in ipairs(libs) do + for i=1,#libs do + local lib = libs[i] local fullname = foundpath .. "/" .. lib if lfs.isfile(fullname) then -- right[#right+1] = lib @@ -3491,6 +3504,8 @@ if not modules then modules = { } end modules ['trac-tra'] = { -- bound to a variable, like node.new, node.copy etc (contrary to for instance -- node.has_attribute which is bound to a has_attribute local variable in mkiv) +local debug = require "debug" + local getinfo = debug.getinfo local type, next = type, next local concat = table.concat @@ -3538,7 +3553,7 @@ function debugger.showstats(printer,threshold) local total, grandtotal, functions = 0, 0, 0 printer("\n") -- ugly but ok -- table.sort(counters) - for func, count in pairs(counters) do + for func, count in next, counters do if count > threshold then local name = getname(func) if not find(name,"for generator") then @@ -3573,7 +3588,7 @@ end --~ local total, grandtotal, functions = 0, 0, 0 --~ printer("\n") -- ugly but ok --~ -- table.sort(counters) ---~ for func, count in pairs(counters) do +--~ for func, count in next, counters do --~ if count > threshold then --~ printer(format("%8i %s", count, func)) --~ total = total + count @@ -3749,8 +3764,9 @@ end function setters.show(t) commands.writestatus("","") - for k,v in ipairs(setters.list(t)) do - commands.writestatus(t.name,v) + local list = setters.list(t) + for k=1,#list do + commands.writestatus(t.name,list[k]) end commands.writestatus("","") end @@ -4550,7 +4566,7 @@ local function copy(old,tables) if not tables[old] then tables[old] = new end - for k,v in pairs(old) do + for k,v in next, old do new[k] = (type(v) == "table" and (tables[v] or copy(v, tables))) or v end local mt = getmetatable(old) @@ -6316,7 +6332,7 @@ function xml.strip_leading_spaces(dk,d,k) -- cosmetic, for manual end --~ xml.escapes = { ['&'] = '&', ['<'] = '<', ['>'] = '>', ['"'] = '"' } ---~ xml.unescapes = { } for k,v in pairs(xml.escapes) do xml.unescapes[v] = k end +--~ xml.unescapes = { } for k,v in next, xml.escapes do xml.unescapes[v] = k end --~ function xml.escaped (str) return (gsub(str,"(.)" , xml.escapes )) end --~ function xml.unescaped(str) return (gsub(str,"(&.-;)", xml.unescapes)) end @@ -7265,7 +7281,8 @@ if not environment.jobname then environ function environment.initialize_arguments(arg) local arguments, files = { }, { } environment.arguments, environment.files, environment.sortedflags = arguments, files, nil - for index, argument in pairs(arg) do + for index=1,#arg do + local argument = arg[index] if index > 0 then local flag, value = match(argument,"^%-+(.-)=(.-)$") if flag then @@ -7298,14 +7315,15 @@ function environment.argument(name,partial) return arguments[name] elseif partial then if not sortedflags then - sortedflags = { } - for _,v in pairs(table.sortedkeys(arguments)) do - sortedflags[#sortedflags+1] = "^" .. v + sortedflags = table.sortedkeys(arguments) + for k=1,#sortedflags do + sortedflags[k] = "^" .. sortedflags[k] end environment.sortedflags = sortedflags end -- example of potential clash: ^mode ^modefile - for _,v in ipairs(sortedflags) do + for k=1,#sortedflags do + local v = sortedflags[k] if find(name,v) then return arguments[sub(v,2,#v)] end @@ -7314,9 +7332,13 @@ function environment.argument(name,partial) return nil end +environment.argument("x",true) + function environment.split_arguments(separator) -- rather special, cut-off before separator local done, before, after = false, { }, { } - for _,v in ipairs(environment.original_arguments) do + local original_arguments = environment.original_arguments + for k=1,#original_arguments do + local v = original_arguments[k] if not done and v == separator then done = true elseif done then @@ -7335,9 +7357,10 @@ function environment.reconstruct_commandline(arg,noquote) a = resolvers.resolve(a) a = unquote(a) return a - elseif next(arg) then + elseif #arg > 0 then local result = { } - for _,a in ipairs(arg) do -- ipairs 1 .. #n + for i=1,#arg do + local a = arg[i] a = resolvers.resolve(a) a = unquote(a) a = gsub(a,'"','\\"') -- tricky @@ -7358,7 +7381,8 @@ if arg then -- new, reconstruct quoted snippets (maybe better just remove the " then and add them later) local newarg, instring = { }, false - for index, argument in ipairs(arg) do + for index=1,#arg do + local argument = arg[index] if find(argument,"^\"") then newarg[#newarg+1] = gsub(argument,"^\"","") if not find(argument,"\"$") then @@ -8041,7 +8065,7 @@ if not modules then modules = { } end modules ['data-inp'] = { -- * some public auxiliary functions were made private -- -- TODO: os.getenv -> os.env[] --- TODO: instances.[hashes,cnffiles,configurations,522] -> ipairs (alles check, sneller) +-- TODO: instances.[hashes,cnffiles,configurations,522] -- TODO: check escaping in find etc, too much, too slow -- This lib is multi-purpose and can be loaded again later on so that @@ -8452,8 +8476,8 @@ local function splitpathexpr(str, t, validate) end end if trace_expansions then - for k,v in ipairs(t) do - logs.report("fileio","% 4i: %s",k,v) + for k=1,#t do + logs.report("fileio","% 4i: %s",k,t[k]) end end return t @@ -8658,7 +8682,9 @@ local function load_cnf_file(fname) end local function collapse_cnf_data() -- potential optimization: pass start index (setup and configuration are shared) - for _,c in ipairs(instance.order) do + local order = instance.order + for i=1,#order do + local c = order[i] for k,v in next, c do if not instance.variables[k] then if instance.environment[k] then @@ -8674,8 +8700,9 @@ end function resolvers.load_cnf() local function loadoldconfigdata() - for _, fname in ipairs(instance.cnffiles) do - load_cnf_file(fname) + local cnffiles = instance.cnffiles + for i=1,#cnffiles do + load_cnf_file(cnffiles[i]) end end -- instance.cnffiles contain complete names now ! @@ -8687,9 +8714,10 @@ function resolvers.load_cnf() logs.report("fileio","no cnf files found (TEXMFCNF may not be set/known)") end else - instance.rootpath = instance.cnffiles[1] - for k,fname in ipairs(instance.cnffiles) do - instance.cnffiles[k] = file.collapse_path(fname) + local cnffiles = instance.cnffiles + instance.rootpath = cnffiles[1] + for k=1,#cnffiles do + instance.cnffiles[k] = file.collapse_path(cnffiles[k]) end for i=1,3 do instance.rootpath = file.dirname(instance.rootpath) @@ -8717,8 +8745,9 @@ function resolvers.load_lua() -- yet harmless else instance.rootpath = instance.luafiles[1] - for k,fname in ipairs(instance.luafiles) do - instance.luafiles[k] = file.collapse_path(fname) + local luafiles = instance.luafiles + for k=1,#luafiles do + instance.luafiles[k] = file.collapse_path(luafiles[k]) end for i=1,3 do instance.rootpath = file.dirname(instance.rootpath) @@ -8781,7 +8810,9 @@ end -- locators function resolvers.locatelists() - for _, path in ipairs(resolvers.clean_path_list('TEXMF')) do + local texmfpaths = resolvers.clean_path_list('TEXMF') + for i=1,#texmfpaths do + local path = texmfpaths[i] if trace_locating then logs.report("fileio","locating list of '%s'",path) end @@ -8814,7 +8845,9 @@ function resolvers.loadfiles() instance.loaderror = false instance.files = { } if not instance.renewcache then - for _, hash in ipairs(instance.hashes) do + local hashes = instance.hashes + for k=1,#hashes do + local hash = hashes[k] resolvers.hashdatabase(hash.tag,hash.name) if instance.loaderror then break end end @@ -8828,8 +8861,9 @@ end -- generators: function resolvers.loadlists() - for _, hash in ipairs(instance.hashes) do - resolvers.generatedatabase(hash.tag) + local hashes = instance.hashes + for i=1,#hashes do + resolvers.generatedatabase(hashes[i].tag) end end @@ -8926,8 +8960,7 @@ end -- we join them and split them after the expansion has taken place. This -- is more convenient. -local checkedsplit = string.checkedsplit -local normalsplit = string.split +--~ local checkedsplit = string.checkedsplit local cache = { } @@ -8951,8 +8984,8 @@ local split = lpegmatch(splitter,str) end if trace_expansions then logs.report("fileio","splitting path specification '%s'",str) - for k,v in ipairs(found) do - logs.report("fileio","% 4i: %s",k,v) + for k=1,#found do + logs.report("fileio","% 4i: %s",k,found[k]) end end cache[str] = found @@ -8964,8 +8997,9 @@ end resolvers.split_kpse_path = split_kpse_path function resolvers.splitconfig() - for i,c in ipairs(instance) do - for k,v in pairs(c) do + for i=1,#instance do + local c = instance[i] + for k,v in next, c do if type(v) == 'string' then local t = split_kpse_path(v) if #t > 1 then @@ -8977,8 +9011,10 @@ function resolvers.splitconfig() end function resolvers.joinconfig() - for i,c in ipairs(instance.order) do - for k,v in pairs(c) do -- ipairs? + local order = instance.order + for i=1,#order do + local c = order[i] + for k,v in next, c do -- indexed? if type(v) == 'table' then c[k] = file.join_path(v) end @@ -9005,8 +9041,9 @@ end function resolvers.splitexpansions() local ie = instance.expansions for k,v in next, ie do - local t, h = { }, { } - for _,vv in ipairs(split_kpse_path(v)) do + local t, h, p = { }, { }, split_kpse_path(v) + for kk=1,#p do + local vv = p[kk] if vv ~= "" and not h[vv] then t[#t+1] = vv h[vv] = true @@ -9053,11 +9090,15 @@ function resolvers.serialize(files) end t[#t+1] = "return {" if instance.sortdata then - for _, k in pairs(sortedkeys(files)) do -- ipairs + local sortedfiles = sortedkeys(files) + for i=1,#sortedfiles do + local k = sortedfiles[i] local fk = files[k] if type(fk) == 'table' then t[#t+1] = "\t['" .. k .. "']={" - for _, kk in pairs(sortedkeys(fk)) do -- ipairs + local sortedfk = sortedkeys(fk) + for j=1,#sortedfk do + local kk = sortedfk[j] t[#t+1] = dump(kk,fk[kk],"\t\t") end t[#t+1] = "\t}," @@ -9169,7 +9210,9 @@ function resolvers.resetconfig() end function resolvers.loadnewconfig() - for _, cnf in ipairs(instance.luafiles) do + local luafiles = instance.luafiles + for i=1,#luafiles do + local cnf = luafiles[i] local pathname = file.dirname(cnf) local filename = file.join(pathname,resolvers.luaname) local blob = loadfile(filename) @@ -9214,7 +9257,9 @@ end function resolvers.loadoldconfig() if not instance.renewcache then - for _, cnf in ipairs(instance.cnffiles) do + local cnffiles = instance.cnffiles + for i=1,#cnffiles do + local cnf = cnffiles[i] local dname = file.dirname(cnf) resolvers.load_data(dname,'configuration') instance.order[#instance.order+1] = instance.configuration[dname] @@ -10018,13 +10063,14 @@ function resolvers.for_files(command, files, filetype, mustexist) if trace_locating then report('') -- ? end - for _, file in ipairs(files) do + for f=1,#files do + local file = files[f] local result = command(file,filetype,mustexist) if type(result) == 'string' then report(result) else - for _,v in ipairs(result) do - report(v) + for i=1,#result do + report(result[i]) -- could be unpack end end end @@ -10071,7 +10117,7 @@ end function table.sequenced(t,sep) -- temp here local s = { } - for k, v in pairs(t) do -- pairs? + for k, v in next, t do -- indexed? s[#s+1] = k .. "=" .. tostring(v) end return concat(s, sep or " | ") @@ -10103,8 +10149,9 @@ function resolvers.clean_path(str) end function resolvers.do_with_path(name,func) - for _, v in pairs(resolvers.expanded_path_list(name)) do -- pairs? - func("^"..resolvers.clean_path(v)) + local pathlist = resolvers.expanded_path_list(name) + for i=1,#pathlist do + func("^"..resolvers.clean_path(pathlist[i])) end end @@ -10113,7 +10160,9 @@ function resolvers.do_with_var(name,func) end function resolvers.with_files(pattern,handle) - for _, hash in ipairs(instance.hashes) do + local hashes = instance.hashes + for i=1,#hashes do + local hash = hashes[i] local blobpath = hash.tag local blobtype = hash.type if blobpath then @@ -10128,7 +10177,7 @@ function resolvers.with_files(pattern,handle) if type(v) == "string" then handle(blobtype,blobpath,v,k) else - for _,vv in pairs(v) do -- ipairs? + for _,vv in next, v do -- indexed handle(blobtype,blobpath,vv,k) end end @@ -10299,7 +10348,8 @@ function caches.setpath(...) caches.path = '.' end caches.path = resolvers.clean_path(caches.path) - if not table.is_empty({...}) then + local dirs = { ... } + if #dirs > 0 then local pth = dir.mkdirs(caches.path,...) return pth end @@ -10457,7 +10507,8 @@ end local function resolve(str) if type(str) == "table" then - for k, v in pairs(str) do -- ipairs + for k=1,#str do + local v = str[k] str[k] = resolve(v) or v end elseif str and str ~= "" then @@ -10470,7 +10521,7 @@ resolvers.resolve = resolve if os.uname then - for k, v in pairs(os.uname()) do + for k, v in next, os.uname() do if not prefixes[k] then prefixes[k] = function() return v end end @@ -10596,7 +10647,7 @@ end function containers.is_valid(container, name) if name and name ~= "" then local storage = container.storage[name] - return storage and not table.is_empty(storage) and storage.cache_version == container.version + return storage and storage.cache_version == container.version else return false end @@ -10700,12 +10751,13 @@ resolvers.automounted = resolvers.automounted or { } function resolvers.automount(usecache) local mountpaths = resolvers.clean_path_list(resolvers.expansion('TEXMFMOUNT')) - if table.is_empty(mountpaths) and usecache then + if (not mountpaths or #mountpaths == 0) and usecache then mountpaths = { caches.setpath("mount") } end - if not table.is_empty(mountpaths) then + if mountpaths and #mountpaths > 0 then statistics.starttiming(resolvers.instance) - for k, root in pairs(mountpaths) do + for k=1,#mountpaths do + local root = mountpaths[k] local f = io.open(root.."/url.tmi") if f then for line in f:lines() do @@ -11549,6 +11601,10 @@ function states.set_by_tag(tag,key,value,default,persistent) if not dk then dk = { } d[k] = dk + elseif type(dk) == "string" then + -- invalid table, unable to upgrade structure + -- hope for the best or delete the state file + break end d = dk end @@ -12142,7 +12198,8 @@ end function runners.save_script_session(filename, list) local t = { } - for _, key in ipairs(list) do + for i=1,#list do + local key = list[i] t[key] = environment.arguments[key] end io.savedata(filename,table.serialize(t,true)) @@ -12298,10 +12355,13 @@ function runners.execute_ctx_script(filename) environment.ownscript = fullname dofile(fullname) local savename = environment.arguments['save'] - if savename and runners.save_list and not table.is_empty(runners.save_list or { }) then - if type(savename) ~= "string" then savename = file.basename(fullname) end - savename = file.replacesuffix(savename,"cfg") - runners.save_script_session(savename, runners.save_list) + if savename then + local save_list = runners.save_list + if save_list and next(save_list) then + if type(savename) ~= "string" then savename = file.basename(fullname) end + savename = file.replacesuffix(savename,"cfg") + runners.save_script_session(savename,save_list) + end end return true end @@ -12314,7 +12374,8 @@ function runners.execute_ctx_script(filename) local result = dir.glob((string.gsub(context,"mtx%-context","mtx-*"))) -- () needed local valid = { } table.sort(result) - for _, scriptname in ipairs(result) do + for i=1,#result do + local scriptname = result[i] local scriptbase = string.match(scriptname,".*mtx%-([^%-]-)%.lua") if scriptbase then local data = io.loaddata(scriptname) @@ -12329,7 +12390,8 @@ function runners.execute_ctx_script(filename) logs.reportline() logs.simple("no script name given, known scripts:") logs.simple() - for k, v in ipairs(valid) do + for k=1,#valid do + local v = valid[k] logs.simple("%-12s %4s %s",v[1],v[2],v[3]) end end diff --git a/scripts/context/lua/x-ldx.lua b/scripts/context/lua/x-ldx.lua index 991640795..e0f21d68c 100644 --- a/scripts/context/lua/x-ldx.lua +++ b/scripts/context/lua/x-ldx.lua @@ -6,7 +6,8 @@ itself serves as an example of using <logo label='lua'/> in combination with <logo label='tex'/>. I will rewrite this using lpeg once I have the time to study that nice new -subsystem. +subsystem. On the other hand, we cannot expect proper <logo label='tex'/> +ad for educational purposed the syntax migh be wrong. --ldx]]-- banner = "version 1.0.1 - 2007+ - PRAGMA ADE / CONTEXT" @@ -126,79 +127,8 @@ ldx.make_index = true function ldx.enhance(data) -- i need to use lpeg and then we can properly autoindent -) local e = ldx.escape - for _,v in pairs(data) do - if v.code then - local dqs, sqs, com, cmt, cod = { }, { }, { }, { }, e(v.code) - cod = cod:gsub('\\"', "##d##") - cod = cod:gsub("\\'", "##s##") - cod = cod:gsub("%-%-%[%[.-%]%]%-%-", function(s) - cmt[#cmt+1] = s - return "[[[[".. #cmt .."]]]]" - end) - cod = cod:gsub("%-%-([^\n]*)", function(s) - com[#com+1] = s - return "[[".. #com .."]]" - end) - cod = cod:gsub("(%b\"\")", function(s) - dqs[#dqs+1] = s:sub(2,-2) or "" - return "<<<<".. #dqs ..">>>>" - end) - cod = cod:gsub("(%b\'\')", function(s) - sqs[#sqs+1] = s:sub(2,-2) or "" - return "<<".. #sqs ..">>" - end) - cod = cod:gsub("(%a+)",function(key) - local class = ldx.keywords.reserved[key] - if class then - return "<key class='" .. class .. "'>" .. key .. "</key>" - else - return key - end - end) - cod = cod:gsub("<<<<(%d+)>>>>", function(s) - return "<dqs>" .. dqs[tonumber(s)] .. "</dqs>" - end) - cod = cod:gsub("<<(%d+)>>", function(s) - return "<sqs>" .. sqs[tonumber(s)] .. "</sqs>" - end) - cod = cod:gsub("%[%[%[%[(%d+)%]%]%]%]", function(s) - return cmt[tonumber(s)] - end) - cod = cod:gsub("%[%[(%d+)%]%]", function(s) - return "<com>" .. com[tonumber(s)] .. "</com>" - end) - cod = cod:gsub("##d##", "\\\"") - cod = cod:gsub("##s##", "\\\'") - if ldx.make_index then - local lines = cod:split("\n") - local f = "(<key class='1'>function</key>)%s+([%w%.]+)%s*%(" - for k,v in pairs(lines) do - -- functies - v = v:gsub(f,function(key, str) - return "<function>" .. str .. "</function>(" - end) - -- variables - v = v:gsub("^([%w][%w%,%s]-)(=[^=])",function(str, rest) - local t = string.split(str, ",%s*") - for k,v in pairs(t) do - t[k] = "<variable>" .. v .. "</variable>" - end - return table.join(t,", ") .. rest - end) - -- so far - lines[k] = v - end - v.code = table.concat(lines,"\n") - else - v.code = cod - end - end - end -end - -function ldx.enhance(data) -- i need to use lpeg and then we can properly autoindent -) - local e = ldx.escape - for _,v in pairs(data) do + for k=1,#data do + local v = data[k] if v.code then local dqs, sqs, com, cmt, cod = { }, { }, { }, { }, e(v.code) cod = cod:gsub('\\"', "##d##") @@ -244,7 +174,8 @@ function ldx.enhance(data) -- i need to use lpeg and then we can properly autoin if ldx.make_index then local lines = cod:split("\n") local f = "(<key class='1'>function</key>)%s+([%w%.]+)%s*%(" - for k,v in pairs(lines) do + for k=1,#lines do + local v = lines[k] -- functies v = v:gsub(f,function(key, str) return "<function>" .. str .. "</function>(" @@ -252,8 +183,8 @@ function ldx.enhance(data) -- i need to use lpeg and then we can properly autoin -- variables v = v:gsub("^([%w][%w%,%s]-)(=[^=])",function(str, rest) local t = string.split(str, ",%s*") - for k,v in pairs(t) do - t[k] = "<variable>" .. v .. "</variable>" + for k=1,#t do + t[k] = "<variable>" .. t[k] .. "</variable>" end return table.join(t,", ") .. rest end) @@ -276,14 +207,17 @@ and by calculating the indentation we also avoid space troubles. It also makes it possible to change the indentation afterwards. --ldx]]-- -function ldx.as_xml(data) +function ldx.as_xml(data) -- ldx: not needed local t, cmode = { }, false t[#t+1] = "<?xml version='1.0' standalone='yes'?>\n" t[#t+1] = "\n<document xmlns:ldx='http://www.pragma-ade.com/schemas/ldx.rng' xmlns='http://www.pragma-ade.com/schemas/ldx.rng'>\n" - for _,v in pairs(data) do -- ldx: not needed + for k=1,#data do + local v = data[k] if v.code and not v.code:is_empty() then t[#t+1] = "\n<code>\n" - for k,v in pairs(v.code:split("\n")) do -- make this faster + local split = v.code:split("\n") + for k=1,#split do -- make this faster + local v = split[k] local a, b = v:find("^(%s+)") if v then v = v:gsub("[\n\r ]+$","") end if a and b then diff --git a/scripts/context/stubs/mswin/luatools.lua b/scripts/context/stubs/mswin/luatools.lua index e6fdd50d5..1d87322c1 100644 --- a/scripts/context/stubs/mswin/luatools.lua +++ b/scripts/context/stubs/mswin/luatools.lua @@ -269,7 +269,7 @@ function string:totable() return lpegmatch(pattern,self) end ---~ for _, str in ipairs { +--~ local t = { --~ "1234567123456712345671234567", --~ "a\tb\tc", --~ "aa\tbb\tcc", @@ -277,7 +277,10 @@ end --~ "aaaa\tbbbb\tcccc", --~ "aaaaa\tbbbbb\tccccc", --~ "aaaaaa\tbbbbbb\tcccccc", ---~ } do print(string.tabtospace(str)) end +--~ } +--~ for k,v do +--~ print(string.tabtospace(t[k])) +--~ end function string.tabtospace(str,tab) -- we don't handle embedded newlines @@ -446,6 +449,8 @@ function string:split(separator) return match(c,self) end +lpeg.splitters = cache + local cache = { } function lpeg.checkedsplit(separator,str) @@ -510,7 +515,7 @@ table.join = table.concat local concat, sort, insert, remove = table.concat, table.sort, table.insert, table.remove local format, find, gsub, lower, dump, match = string.format, string.find, string.gsub, string.lower, string.dump, string.match local getmetatable, setmetatable = getmetatable, setmetatable -local type, next, tostring, tonumber, ipairs, pairs = type, next, tostring, tonumber, ipairs, pairs +local type, next, tostring, tonumber, ipairs = type, next, tostring, tonumber, ipairs local unpack = unpack or table.unpack function table.strip(tab) @@ -577,7 +582,7 @@ end table.sortedkeys = sortedkeys table.sortedhashkeys = sortedhashkeys -function table.sortedpairs(t) +function table.sortedhash(t) local s = sortedhashkeys(t) -- maybe just sortedkeys local n = 0 local function kv(s) @@ -588,6 +593,8 @@ function table.sortedpairs(t) return kv, s end +table.sortedpairs = table.sortedhash + function table.append(t, list) for _,v in next, list do insert(t,v) @@ -710,18 +717,18 @@ end -- slower than #t on indexed tables (#t only returns the size of the numerically indexed slice) -function table.is_empty(t) +function table.is_empty(t) -- obolete, use inline code instead return not t or not next(t) end -function table.one_entry(t) +function table.one_entry(t) -- obolete, use inline code instead local n = next(t) return n and not next(t,n) end -function table.starts_at(t) - return ipairs(t,1)(t,0) -end +--~ function table.starts_at(t) -- obsolete, not nice +--~ return ipairs(t,1)(t,0) +--~ end function table.tohash(t,value) local h = { } @@ -825,7 +832,7 @@ local function do_serialize(root,name,depth,level,indexed) end -- we could check for k (index) being number (cardinal) if root and next(root) then - local first, last = nil, 0 -- #root cannot be trusted here + local first, last = nil, 0 -- #root cannot be trusted here (will be ok in 5.2 when ipairs is gone) if compact then -- NOT: for k=1,#root do (we need to quit at nil) for k,v in ipairs(root) do -- can we use next? @@ -1534,13 +1541,14 @@ function io.ask(question,default,options) elseif not options then return answer else - for _,v in pairs(options) do - if v == answer then + for k=1,#options do + if options[k] == answer then return answer end end local pattern = "^" .. answer - for _,v in pairs(options) do + for k=1,#options do + local v = options[k] if find(v,pattern) then return v end @@ -2314,7 +2322,7 @@ function file.splitname(str) -- returns drive, path, base, suffix return lpegmatch(pattern,str) end --- function test(t) for k, v in pairs(t) do print(v, "=>", file.splitname(v)) end end +-- function test(t) for k, v in next, t do print(v, "=>", file.splitname(v)) end end -- -- test { "c:", "c:/aa", "c:/aa/bb", "c:/aa/bb/cc", "c:/aa/bb/cc.dd", "c:/aa/bb/cc.dd.ee" } -- test { "c:", "c:aa", "c:aa/bb", "c:aa/bb/cc", "c:aa/bb/cc.dd", "c:aa/bb/cc.dd.ee" } @@ -2759,8 +2767,9 @@ local make_indeed = true -- false if string.find(os.getenv("PATH"),";") then -- os.type == "windows" function dir.mkdirs(...) - local str, pth = "", "" - for _, s in ipairs({...}) do + local str, pth, t = "", "", { ... } + for i=1,#t do + local s = t[i] if s ~= "" then if str ~= "" then str = str .. "/" .. s @@ -2858,8 +2867,9 @@ if string.find(os.getenv("PATH"),";") then -- os.type == "windows" else function dir.mkdirs(...) - local str, pth = "", "" - for _, s in ipairs({...}) do + local str, pth, t = "", "", { ... } + for i=1,#t do + local s = t[i] if s ~= "" then if str ~= "" then str = str .. "/" .. s @@ -3031,14 +3041,20 @@ unicode.utfname = { [4] = 'utf-32-be' } -function unicode.utftype(f) -- \000 fails ! +-- \000 fails in <= 5.0 but is valid in >=5.1 where %z is depricated + +function unicode.utftype(f) local str = f:read(4) if not str then f:seek('set') return 0 - elseif find(str,"^%z%z\254\255") then + -- elseif find(str,"^%z%z\254\255") then -- depricated + -- elseif find(str,"^\000\000\254\255") then -- not permitted and bugged + elseif find(str,"\000\000\254\255",1,true) then -- seems to work okay (TH) return 4 - elseif find(str,"^\255\254%z%z") then + -- elseif find(str,"^\255\254%z%z") then -- depricated + -- elseif find(str,"^\255\254\000\000") then -- not permitted and bugged + elseif find(str,"\255\254\000\000",1,true) then -- seems to work okay (TH) return 3 elseif find(str,"^\254\255") then f:seek('set',2) @@ -3239,6 +3255,7 @@ if not modules then modules = { } end modules ['l-utils'] = { local gsub = string.gsub local concat = table.concat +local type, next = type, next if not utils then utils = { } end if not utils.merger then utils.merger = { } end @@ -3314,9 +3331,10 @@ function utils.merger._self_libs_(libs,list) if type(libs) == 'string' then libs = { libs } end if type(list) == 'string' then list = { list } end local foundpath = nil - for _, lib in ipairs(libs) do - for _, pth in ipairs(list) do - pth = gsub(pth,"\\","/") -- file.clean_path + for i=1,#libs do + local lib = libs[i] + for j=1,#list do + local pth = gsub(list[j],"\\","/") -- file.clean_path utils.report("checking library path %s",pth) local name = pth .. "/" .. lib if lfs.isfile(name) then @@ -3328,7 +3346,8 @@ function utils.merger._self_libs_(libs,list) if foundpath then utils.report("using library path %s",foundpath) local right, wrong = { }, { } - for _, lib in ipairs(libs) do + for i=1,#libs do + local lib = libs[i] local fullname = foundpath .. "/" .. lib if lfs.isfile(fullname) then -- right[#right+1] = lib @@ -3681,6 +3700,8 @@ if not modules then modules = { } end modules ['trac-tra'] = { -- bound to a variable, like node.new, node.copy etc (contrary to for instance -- node.has_attribute which is bound to a has_attribute local variable in mkiv) +local debug = require "debug" + local getinfo = debug.getinfo local type, next = type, next local concat = table.concat @@ -3728,7 +3749,7 @@ function debugger.showstats(printer,threshold) local total, grandtotal, functions = 0, 0, 0 printer("\n") -- ugly but ok -- table.sort(counters) - for func, count in pairs(counters) do + for func, count in next, counters do if count > threshold then local name = getname(func) if not find(name,"for generator") then @@ -3763,7 +3784,7 @@ end --~ local total, grandtotal, functions = 0, 0, 0 --~ printer("\n") -- ugly but ok --~ -- table.sort(counters) ---~ for func, count in pairs(counters) do +--~ for func, count in next, counters do --~ if count > threshold then --~ printer(format("%8i %s", count, func)) --~ total = total + count @@ -3939,8 +3960,9 @@ end function setters.show(t) commands.writestatus("","") - for k,v in ipairs(setters.list(t)) do - commands.writestatus(t.name,v) + local list = setters.list(t) + for k=1,#list do + commands.writestatus(t.name,list[k]) end commands.writestatus("","") end @@ -4064,7 +4086,8 @@ if not environment.jobname then environ function environment.initialize_arguments(arg) local arguments, files = { }, { } environment.arguments, environment.files, environment.sortedflags = arguments, files, nil - for index, argument in pairs(arg) do + for index=1,#arg do + local argument = arg[index] if index > 0 then local flag, value = match(argument,"^%-+(.-)=(.-)$") if flag then @@ -4097,14 +4120,15 @@ function environment.argument(name,partial) return arguments[name] elseif partial then if not sortedflags then - sortedflags = { } - for _,v in pairs(table.sortedkeys(arguments)) do - sortedflags[#sortedflags+1] = "^" .. v + sortedflags = table.sortedkeys(arguments) + for k=1,#sortedflags do + sortedflags[k] = "^" .. sortedflags[k] end environment.sortedflags = sortedflags end -- example of potential clash: ^mode ^modefile - for _,v in ipairs(sortedflags) do + for k=1,#sortedflags do + local v = sortedflags[k] if find(name,v) then return arguments[sub(v,2,#v)] end @@ -4113,9 +4137,13 @@ function environment.argument(name,partial) return nil end +environment.argument("x",true) + function environment.split_arguments(separator) -- rather special, cut-off before separator local done, before, after = false, { }, { } - for _,v in ipairs(environment.original_arguments) do + local original_arguments = environment.original_arguments + for k=1,#original_arguments do + local v = original_arguments[k] if not done and v == separator then done = true elseif done then @@ -4134,9 +4162,10 @@ function environment.reconstruct_commandline(arg,noquote) a = resolvers.resolve(a) a = unquote(a) return a - elseif next(arg) then + elseif #arg > 0 then local result = { } - for _,a in ipairs(arg) do -- ipairs 1 .. #n + for i=1,#arg do + local a = arg[i] a = resolvers.resolve(a) a = unquote(a) a = gsub(a,'"','\\"') -- tricky @@ -4157,7 +4186,8 @@ if arg then -- new, reconstruct quoted snippets (maybe better just remove the " then and add them later) local newarg, instring = { }, false - for index, argument in ipairs(arg) do + for index=1,#arg do + local argument = arg[index] if find(argument,"^\"") then newarg[#newarg+1] = gsub(argument,"^\"","") if not find(argument,"\"$") then @@ -4840,7 +4870,7 @@ if not modules then modules = { } end modules ['data-inp'] = { -- * some public auxiliary functions were made private -- -- TODO: os.getenv -> os.env[] --- TODO: instances.[hashes,cnffiles,configurations,522] -> ipairs (alles check, sneller) +-- TODO: instances.[hashes,cnffiles,configurations,522] -- TODO: check escaping in find etc, too much, too slow -- This lib is multi-purpose and can be loaded again later on so that @@ -5251,8 +5281,8 @@ local function splitpathexpr(str, t, validate) end end if trace_expansions then - for k,v in ipairs(t) do - logs.report("fileio","% 4i: %s",k,v) + for k=1,#t do + logs.report("fileio","% 4i: %s",k,t[k]) end end return t @@ -5457,7 +5487,9 @@ local function load_cnf_file(fname) end local function collapse_cnf_data() -- potential optimization: pass start index (setup and configuration are shared) - for _,c in ipairs(instance.order) do + local order = instance.order + for i=1,#order do + local c = order[i] for k,v in next, c do if not instance.variables[k] then if instance.environment[k] then @@ -5473,8 +5505,9 @@ end function resolvers.load_cnf() local function loadoldconfigdata() - for _, fname in ipairs(instance.cnffiles) do - load_cnf_file(fname) + local cnffiles = instance.cnffiles + for i=1,#cnffiles do + load_cnf_file(cnffiles[i]) end end -- instance.cnffiles contain complete names now ! @@ -5486,9 +5519,10 @@ function resolvers.load_cnf() logs.report("fileio","no cnf files found (TEXMFCNF may not be set/known)") end else - instance.rootpath = instance.cnffiles[1] - for k,fname in ipairs(instance.cnffiles) do - instance.cnffiles[k] = file.collapse_path(fname) + local cnffiles = instance.cnffiles + instance.rootpath = cnffiles[1] + for k=1,#cnffiles do + instance.cnffiles[k] = file.collapse_path(cnffiles[k]) end for i=1,3 do instance.rootpath = file.dirname(instance.rootpath) @@ -5516,8 +5550,9 @@ function resolvers.load_lua() -- yet harmless else instance.rootpath = instance.luafiles[1] - for k,fname in ipairs(instance.luafiles) do - instance.luafiles[k] = file.collapse_path(fname) + local luafiles = instance.luafiles + for k=1,#luafiles do + instance.luafiles[k] = file.collapse_path(luafiles[k]) end for i=1,3 do instance.rootpath = file.dirname(instance.rootpath) @@ -5580,7 +5615,9 @@ end -- locators function resolvers.locatelists() - for _, path in ipairs(resolvers.clean_path_list('TEXMF')) do + local texmfpaths = resolvers.clean_path_list('TEXMF') + for i=1,#texmfpaths do + local path = texmfpaths[i] if trace_locating then logs.report("fileio","locating list of '%s'",path) end @@ -5613,7 +5650,9 @@ function resolvers.loadfiles() instance.loaderror = false instance.files = { } if not instance.renewcache then - for _, hash in ipairs(instance.hashes) do + local hashes = instance.hashes + for k=1,#hashes do + local hash = hashes[k] resolvers.hashdatabase(hash.tag,hash.name) if instance.loaderror then break end end @@ -5627,8 +5666,9 @@ end -- generators: function resolvers.loadlists() - for _, hash in ipairs(instance.hashes) do - resolvers.generatedatabase(hash.tag) + local hashes = instance.hashes + for i=1,#hashes do + resolvers.generatedatabase(hashes[i].tag) end end @@ -5725,8 +5765,7 @@ end -- we join them and split them after the expansion has taken place. This -- is more convenient. -local checkedsplit = string.checkedsplit -local normalsplit = string.split +--~ local checkedsplit = string.checkedsplit local cache = { } @@ -5750,8 +5789,8 @@ local split = lpegmatch(splitter,str) end if trace_expansions then logs.report("fileio","splitting path specification '%s'",str) - for k,v in ipairs(found) do - logs.report("fileio","% 4i: %s",k,v) + for k=1,#found do + logs.report("fileio","% 4i: %s",k,found[k]) end end cache[str] = found @@ -5763,8 +5802,9 @@ end resolvers.split_kpse_path = split_kpse_path function resolvers.splitconfig() - for i,c in ipairs(instance) do - for k,v in pairs(c) do + for i=1,#instance do + local c = instance[i] + for k,v in next, c do if type(v) == 'string' then local t = split_kpse_path(v) if #t > 1 then @@ -5776,8 +5816,10 @@ function resolvers.splitconfig() end function resolvers.joinconfig() - for i,c in ipairs(instance.order) do - for k,v in pairs(c) do -- ipairs? + local order = instance.order + for i=1,#order do + local c = order[i] + for k,v in next, c do -- indexed? if type(v) == 'table' then c[k] = file.join_path(v) end @@ -5804,8 +5846,9 @@ end function resolvers.splitexpansions() local ie = instance.expansions for k,v in next, ie do - local t, h = { }, { } - for _,vv in ipairs(split_kpse_path(v)) do + local t, h, p = { }, { }, split_kpse_path(v) + for kk=1,#p do + local vv = p[kk] if vv ~= "" and not h[vv] then t[#t+1] = vv h[vv] = true @@ -5852,11 +5895,15 @@ function resolvers.serialize(files) end t[#t+1] = "return {" if instance.sortdata then - for _, k in pairs(sortedkeys(files)) do -- ipairs + local sortedfiles = sortedkeys(files) + for i=1,#sortedfiles do + local k = sortedfiles[i] local fk = files[k] if type(fk) == 'table' then t[#t+1] = "\t['" .. k .. "']={" - for _, kk in pairs(sortedkeys(fk)) do -- ipairs + local sortedfk = sortedkeys(fk) + for j=1,#sortedfk do + local kk = sortedfk[j] t[#t+1] = dump(kk,fk[kk],"\t\t") end t[#t+1] = "\t}," @@ -5968,7 +6015,9 @@ function resolvers.resetconfig() end function resolvers.loadnewconfig() - for _, cnf in ipairs(instance.luafiles) do + local luafiles = instance.luafiles + for i=1,#luafiles do + local cnf = luafiles[i] local pathname = file.dirname(cnf) local filename = file.join(pathname,resolvers.luaname) local blob = loadfile(filename) @@ -6013,7 +6062,9 @@ end function resolvers.loadoldconfig() if not instance.renewcache then - for _, cnf in ipairs(instance.cnffiles) do + local cnffiles = instance.cnffiles + for i=1,#cnffiles do + local cnf = cnffiles[i] local dname = file.dirname(cnf) resolvers.load_data(dname,'configuration') instance.order[#instance.order+1] = instance.configuration[dname] @@ -6817,13 +6868,14 @@ function resolvers.for_files(command, files, filetype, mustexist) if trace_locating then report('') -- ? end - for _, file in ipairs(files) do + for f=1,#files do + local file = files[f] local result = command(file,filetype,mustexist) if type(result) == 'string' then report(result) else - for _,v in ipairs(result) do - report(v) + for i=1,#result do + report(result[i]) -- could be unpack end end end @@ -6870,7 +6922,7 @@ end function table.sequenced(t,sep) -- temp here local s = { } - for k, v in pairs(t) do -- pairs? + for k, v in next, t do -- indexed? s[#s+1] = k .. "=" .. tostring(v) end return concat(s, sep or " | ") @@ -6902,8 +6954,9 @@ function resolvers.clean_path(str) end function resolvers.do_with_path(name,func) - for _, v in pairs(resolvers.expanded_path_list(name)) do -- pairs? - func("^"..resolvers.clean_path(v)) + local pathlist = resolvers.expanded_path_list(name) + for i=1,#pathlist do + func("^"..resolvers.clean_path(pathlist[i])) end end @@ -6912,7 +6965,9 @@ function resolvers.do_with_var(name,func) end function resolvers.with_files(pattern,handle) - for _, hash in ipairs(instance.hashes) do + local hashes = instance.hashes + for i=1,#hashes do + local hash = hashes[i] local blobpath = hash.tag local blobtype = hash.type if blobpath then @@ -6927,7 +6982,7 @@ function resolvers.with_files(pattern,handle) if type(v) == "string" then handle(blobtype,blobpath,v,k) else - for _,vv in pairs(v) do -- ipairs? + for _,vv in next, v do -- indexed handle(blobtype,blobpath,vv,k) end end @@ -7098,7 +7153,8 @@ function caches.setpath(...) caches.path = '.' end caches.path = resolvers.clean_path(caches.path) - if not table.is_empty({...}) then + local dirs = { ... } + if #dirs > 0 then local pth = dir.mkdirs(caches.path,...) return pth end @@ -7281,7 +7337,7 @@ end function containers.is_valid(container, name) if name and name ~= "" then local storage = container.storage[name] - return storage and not table.is_empty(storage) and storage.cache_version == container.version + return storage and storage.cache_version == container.version else return false end @@ -7385,12 +7441,13 @@ resolvers.automounted = resolvers.automounted or { } function resolvers.automount(usecache) local mountpaths = resolvers.clean_path_list(resolvers.expansion('TEXMFMOUNT')) - if table.is_empty(mountpaths) and usecache then + if (not mountpaths or #mountpaths == 0) and usecache then mountpaths = { caches.setpath("mount") } end - if not table.is_empty(mountpaths) then + if mountpaths and #mountpaths > 0 then statistics.starttiming(resolvers.instance) - for k, root in pairs(mountpaths) do + for k=1,#mountpaths do + local root = mountpaths[k] local f = io.open(root.."/url.tmi") if f then for line in f:lines() do @@ -7661,7 +7718,9 @@ local function list(list,report) local instance = resolvers.instance local pat = upper(pattern or "","") local report = report or texio.write_nl - for _,key in pairs(table.sortedkeys(list)) do + local sorted = table.sortedkeys(list) + for i=1,#sorted do + local key = sorted[i] if instance.pattern == "" or find(upper(key),pat) then if instance.kpseonly then if instance.kpsevars[key] then @@ -7680,11 +7739,14 @@ function resolvers.listers.expansions() list(resolvers.instance.expansions) end function resolvers.listers.configurations(report) local report = report or texio.write_nl local instance = resolvers.instance - for _,key in ipairs(table.sortedkeys(instance.kpsevars)) do + local sorted = table.sortedkeys(instance.kpsevars) + for i=1,#sorted do + local key = sorted[i] if not instance.pattern or (instance.pattern=="") or find(key,instance.pattern) then report(format("%s\n",key)) - for i,c in ipairs(instance.order) do - local str = c[key] + local order = instance.order + for i=1,#order do + local str = order[i][key] if str then report(format("\t%s\t%s",i,str)) end @@ -7945,8 +8007,9 @@ function runners.make_format(texname) logs.simple("using uncompiled initialization file: %s",luaname) end else - for _, v in pairs({instance.luaname, instance.progname, barename}) do - v = string.gsub(v..".lua","%.lua%.lua$",".lua") + local what = { instance.luaname, instance.progname, barename } + for k=1,#what do + local v = string.gsub(what[k]..".lua","%.lua%.lua$",".lua") if v and (v ~= "") then luaname = resolvers.find_files(v)[1] or "" if luaname ~= "" then @@ -7970,7 +8033,8 @@ function runners.make_format(texname) logs.simple("using lua initialization file: %s",luaname) local mp = dir.glob(file.removesuffix(file.basename(luaname)).."-*.mem") if mp and #mp > 0 then - for _, name in ipairs(mp) do + for i=1,#mp do + local name = mp[i] logs.simple("removing related mplib format %s", file.basename(name)) os.remove(name) end diff --git a/scripts/context/stubs/mswin/mtxrun.lua b/scripts/context/stubs/mswin/mtxrun.lua index 727099027..d3e50e00e 100644 --- a/scripts/context/stubs/mswin/mtxrun.lua +++ b/scripts/context/stubs/mswin/mtxrun.lua @@ -278,7 +278,7 @@ function string:totable() return lpegmatch(pattern,self) end ---~ for _, str in ipairs { +--~ local t = { --~ "1234567123456712345671234567", --~ "a\tb\tc", --~ "aa\tbb\tcc", @@ -286,7 +286,10 @@ end --~ "aaaa\tbbbb\tcccc", --~ "aaaaa\tbbbbb\tccccc", --~ "aaaaaa\tbbbbbb\tcccccc", ---~ } do print(string.tabtospace(str)) end +--~ } +--~ for k,v do +--~ print(string.tabtospace(t[k])) +--~ end function string.tabtospace(str,tab) -- we don't handle embedded newlines @@ -455,6 +458,8 @@ function string:split(separator) return match(c,self) end +lpeg.splitters = cache + local cache = { } function lpeg.checkedsplit(separator,str) @@ -519,7 +524,7 @@ table.join = table.concat local concat, sort, insert, remove = table.concat, table.sort, table.insert, table.remove local format, find, gsub, lower, dump, match = string.format, string.find, string.gsub, string.lower, string.dump, string.match local getmetatable, setmetatable = getmetatable, setmetatable -local type, next, tostring, tonumber, ipairs, pairs = type, next, tostring, tonumber, ipairs, pairs +local type, next, tostring, tonumber, ipairs = type, next, tostring, tonumber, ipairs local unpack = unpack or table.unpack function table.strip(tab) @@ -586,7 +591,7 @@ end table.sortedkeys = sortedkeys table.sortedhashkeys = sortedhashkeys -function table.sortedpairs(t) +function table.sortedhash(t) local s = sortedhashkeys(t) -- maybe just sortedkeys local n = 0 local function kv(s) @@ -597,6 +602,8 @@ function table.sortedpairs(t) return kv, s end +table.sortedpairs = table.sortedhash + function table.append(t, list) for _,v in next, list do insert(t,v) @@ -719,18 +726,18 @@ end -- slower than #t on indexed tables (#t only returns the size of the numerically indexed slice) -function table.is_empty(t) +function table.is_empty(t) -- obolete, use inline code instead return not t or not next(t) end -function table.one_entry(t) +function table.one_entry(t) -- obolete, use inline code instead local n = next(t) return n and not next(t,n) end -function table.starts_at(t) - return ipairs(t,1)(t,0) -end +--~ function table.starts_at(t) -- obsolete, not nice +--~ return ipairs(t,1)(t,0) +--~ end function table.tohash(t,value) local h = { } @@ -834,7 +841,7 @@ local function do_serialize(root,name,depth,level,indexed) end -- we could check for k (index) being number (cardinal) if root and next(root) then - local first, last = nil, 0 -- #root cannot be trusted here + local first, last = nil, 0 -- #root cannot be trusted here (will be ok in 5.2 when ipairs is gone) if compact then -- NOT: for k=1,#root do (we need to quit at nil) for k,v in ipairs(root) do -- can we use next? @@ -1543,13 +1550,14 @@ function io.ask(question,default,options) elseif not options then return answer else - for _,v in pairs(options) do - if v == answer then + for k=1,#options do + if options[k] == answer then return answer end end local pattern = "^" .. answer - for _,v in pairs(options) do + for k=1,#options do + local v = options[k] if find(v,pattern) then return v end @@ -2323,7 +2331,7 @@ function file.splitname(str) -- returns drive, path, base, suffix return lpegmatch(pattern,str) end --- function test(t) for k, v in pairs(t) do print(v, "=>", file.splitname(v)) end end +-- function test(t) for k, v in next, t do print(v, "=>", file.splitname(v)) end end -- -- test { "c:", "c:/aa", "c:/aa/bb", "c:/aa/bb/cc", "c:/aa/bb/cc.dd", "c:/aa/bb/cc.dd.ee" } -- test { "c:", "c:aa", "c:aa/bb", "c:aa/bb/cc", "c:aa/bb/cc.dd", "c:aa/bb/cc.dd.ee" } @@ -2768,8 +2776,9 @@ local make_indeed = true -- false if string.find(os.getenv("PATH"),";") then -- os.type == "windows" function dir.mkdirs(...) - local str, pth = "", "" - for _, s in ipairs({...}) do + local str, pth, t = "", "", { ... } + for i=1,#t do + local s = t[i] if s ~= "" then if str ~= "" then str = str .. "/" .. s @@ -2867,8 +2876,9 @@ if string.find(os.getenv("PATH"),";") then -- os.type == "windows" else function dir.mkdirs(...) - local str, pth = "", "" - for _, s in ipairs({...}) do + local str, pth, t = "", "", { ... } + for i=1,#t do + local s = t[i] if s ~= "" then if str ~= "" then str = str .. "/" .. s @@ -3049,6 +3059,7 @@ if not modules then modules = { } end modules ['l-utils'] = { local gsub = string.gsub local concat = table.concat +local type, next = type, next if not utils then utils = { } end if not utils.merger then utils.merger = { } end @@ -3124,9 +3135,10 @@ function utils.merger._self_libs_(libs,list) if type(libs) == 'string' then libs = { libs } end if type(list) == 'string' then list = { list } end local foundpath = nil - for _, lib in ipairs(libs) do - for _, pth in ipairs(list) do - pth = gsub(pth,"\\","/") -- file.clean_path + for i=1,#libs do + local lib = libs[i] + for j=1,#list do + local pth = gsub(list[j],"\\","/") -- file.clean_path utils.report("checking library path %s",pth) local name = pth .. "/" .. lib if lfs.isfile(name) then @@ -3138,7 +3150,8 @@ function utils.merger._self_libs_(libs,list) if foundpath then utils.report("using library path %s",foundpath) local right, wrong = { }, { } - for _, lib in ipairs(libs) do + for i=1,#libs do + local lib = libs[i] local fullname = foundpath .. "/" .. lib if lfs.isfile(fullname) then -- right[#right+1] = lib @@ -3491,6 +3504,8 @@ if not modules then modules = { } end modules ['trac-tra'] = { -- bound to a variable, like node.new, node.copy etc (contrary to for instance -- node.has_attribute which is bound to a has_attribute local variable in mkiv) +local debug = require "debug" + local getinfo = debug.getinfo local type, next = type, next local concat = table.concat @@ -3538,7 +3553,7 @@ function debugger.showstats(printer,threshold) local total, grandtotal, functions = 0, 0, 0 printer("\n") -- ugly but ok -- table.sort(counters) - for func, count in pairs(counters) do + for func, count in next, counters do if count > threshold then local name = getname(func) if not find(name,"for generator") then @@ -3573,7 +3588,7 @@ end --~ local total, grandtotal, functions = 0, 0, 0 --~ printer("\n") -- ugly but ok --~ -- table.sort(counters) ---~ for func, count in pairs(counters) do +--~ for func, count in next, counters do --~ if count > threshold then --~ printer(format("%8i %s", count, func)) --~ total = total + count @@ -3749,8 +3764,9 @@ end function setters.show(t) commands.writestatus("","") - for k,v in ipairs(setters.list(t)) do - commands.writestatus(t.name,v) + local list = setters.list(t) + for k=1,#list do + commands.writestatus(t.name,list[k]) end commands.writestatus("","") end @@ -4550,7 +4566,7 @@ local function copy(old,tables) if not tables[old] then tables[old] = new end - for k,v in pairs(old) do + for k,v in next, old do new[k] = (type(v) == "table" and (tables[v] or copy(v, tables))) or v end local mt = getmetatable(old) @@ -6316,7 +6332,7 @@ function xml.strip_leading_spaces(dk,d,k) -- cosmetic, for manual end --~ xml.escapes = { ['&'] = '&', ['<'] = '<', ['>'] = '>', ['"'] = '"' } ---~ xml.unescapes = { } for k,v in pairs(xml.escapes) do xml.unescapes[v] = k end +--~ xml.unescapes = { } for k,v in next, xml.escapes do xml.unescapes[v] = k end --~ function xml.escaped (str) return (gsub(str,"(.)" , xml.escapes )) end --~ function xml.unescaped(str) return (gsub(str,"(&.-;)", xml.unescapes)) end @@ -7265,7 +7281,8 @@ if not environment.jobname then environ function environment.initialize_arguments(arg) local arguments, files = { }, { } environment.arguments, environment.files, environment.sortedflags = arguments, files, nil - for index, argument in pairs(arg) do + for index=1,#arg do + local argument = arg[index] if index > 0 then local flag, value = match(argument,"^%-+(.-)=(.-)$") if flag then @@ -7298,14 +7315,15 @@ function environment.argument(name,partial) return arguments[name] elseif partial then if not sortedflags then - sortedflags = { } - for _,v in pairs(table.sortedkeys(arguments)) do - sortedflags[#sortedflags+1] = "^" .. v + sortedflags = table.sortedkeys(arguments) + for k=1,#sortedflags do + sortedflags[k] = "^" .. sortedflags[k] end environment.sortedflags = sortedflags end -- example of potential clash: ^mode ^modefile - for _,v in ipairs(sortedflags) do + for k=1,#sortedflags do + local v = sortedflags[k] if find(name,v) then return arguments[sub(v,2,#v)] end @@ -7314,9 +7332,13 @@ function environment.argument(name,partial) return nil end +environment.argument("x",true) + function environment.split_arguments(separator) -- rather special, cut-off before separator local done, before, after = false, { }, { } - for _,v in ipairs(environment.original_arguments) do + local original_arguments = environment.original_arguments + for k=1,#original_arguments do + local v = original_arguments[k] if not done and v == separator then done = true elseif done then @@ -7335,9 +7357,10 @@ function environment.reconstruct_commandline(arg,noquote) a = resolvers.resolve(a) a = unquote(a) return a - elseif next(arg) then + elseif #arg > 0 then local result = { } - for _,a in ipairs(arg) do -- ipairs 1 .. #n + for i=1,#arg do + local a = arg[i] a = resolvers.resolve(a) a = unquote(a) a = gsub(a,'"','\\"') -- tricky @@ -7358,7 +7381,8 @@ if arg then -- new, reconstruct quoted snippets (maybe better just remove the " then and add them later) local newarg, instring = { }, false - for index, argument in ipairs(arg) do + for index=1,#arg do + local argument = arg[index] if find(argument,"^\"") then newarg[#newarg+1] = gsub(argument,"^\"","") if not find(argument,"\"$") then @@ -8041,7 +8065,7 @@ if not modules then modules = { } end modules ['data-inp'] = { -- * some public auxiliary functions were made private -- -- TODO: os.getenv -> os.env[] --- TODO: instances.[hashes,cnffiles,configurations,522] -> ipairs (alles check, sneller) +-- TODO: instances.[hashes,cnffiles,configurations,522] -- TODO: check escaping in find etc, too much, too slow -- This lib is multi-purpose and can be loaded again later on so that @@ -8452,8 +8476,8 @@ local function splitpathexpr(str, t, validate) end end if trace_expansions then - for k,v in ipairs(t) do - logs.report("fileio","% 4i: %s",k,v) + for k=1,#t do + logs.report("fileio","% 4i: %s",k,t[k]) end end return t @@ -8658,7 +8682,9 @@ local function load_cnf_file(fname) end local function collapse_cnf_data() -- potential optimization: pass start index (setup and configuration are shared) - for _,c in ipairs(instance.order) do + local order = instance.order + for i=1,#order do + local c = order[i] for k,v in next, c do if not instance.variables[k] then if instance.environment[k] then @@ -8674,8 +8700,9 @@ end function resolvers.load_cnf() local function loadoldconfigdata() - for _, fname in ipairs(instance.cnffiles) do - load_cnf_file(fname) + local cnffiles = instance.cnffiles + for i=1,#cnffiles do + load_cnf_file(cnffiles[i]) end end -- instance.cnffiles contain complete names now ! @@ -8687,9 +8714,10 @@ function resolvers.load_cnf() logs.report("fileio","no cnf files found (TEXMFCNF may not be set/known)") end else - instance.rootpath = instance.cnffiles[1] - for k,fname in ipairs(instance.cnffiles) do - instance.cnffiles[k] = file.collapse_path(fname) + local cnffiles = instance.cnffiles + instance.rootpath = cnffiles[1] + for k=1,#cnffiles do + instance.cnffiles[k] = file.collapse_path(cnffiles[k]) end for i=1,3 do instance.rootpath = file.dirname(instance.rootpath) @@ -8717,8 +8745,9 @@ function resolvers.load_lua() -- yet harmless else instance.rootpath = instance.luafiles[1] - for k,fname in ipairs(instance.luafiles) do - instance.luafiles[k] = file.collapse_path(fname) + local luafiles = instance.luafiles + for k=1,#luafiles do + instance.luafiles[k] = file.collapse_path(luafiles[k]) end for i=1,3 do instance.rootpath = file.dirname(instance.rootpath) @@ -8781,7 +8810,9 @@ end -- locators function resolvers.locatelists() - for _, path in ipairs(resolvers.clean_path_list('TEXMF')) do + local texmfpaths = resolvers.clean_path_list('TEXMF') + for i=1,#texmfpaths do + local path = texmfpaths[i] if trace_locating then logs.report("fileio","locating list of '%s'",path) end @@ -8814,7 +8845,9 @@ function resolvers.loadfiles() instance.loaderror = false instance.files = { } if not instance.renewcache then - for _, hash in ipairs(instance.hashes) do + local hashes = instance.hashes + for k=1,#hashes do + local hash = hashes[k] resolvers.hashdatabase(hash.tag,hash.name) if instance.loaderror then break end end @@ -8828,8 +8861,9 @@ end -- generators: function resolvers.loadlists() - for _, hash in ipairs(instance.hashes) do - resolvers.generatedatabase(hash.tag) + local hashes = instance.hashes + for i=1,#hashes do + resolvers.generatedatabase(hashes[i].tag) end end @@ -8926,8 +8960,7 @@ end -- we join them and split them after the expansion has taken place. This -- is more convenient. -local checkedsplit = string.checkedsplit -local normalsplit = string.split +--~ local checkedsplit = string.checkedsplit local cache = { } @@ -8951,8 +8984,8 @@ local split = lpegmatch(splitter,str) end if trace_expansions then logs.report("fileio","splitting path specification '%s'",str) - for k,v in ipairs(found) do - logs.report("fileio","% 4i: %s",k,v) + for k=1,#found do + logs.report("fileio","% 4i: %s",k,found[k]) end end cache[str] = found @@ -8964,8 +8997,9 @@ end resolvers.split_kpse_path = split_kpse_path function resolvers.splitconfig() - for i,c in ipairs(instance) do - for k,v in pairs(c) do + for i=1,#instance do + local c = instance[i] + for k,v in next, c do if type(v) == 'string' then local t = split_kpse_path(v) if #t > 1 then @@ -8977,8 +9011,10 @@ function resolvers.splitconfig() end function resolvers.joinconfig() - for i,c in ipairs(instance.order) do - for k,v in pairs(c) do -- ipairs? + local order = instance.order + for i=1,#order do + local c = order[i] + for k,v in next, c do -- indexed? if type(v) == 'table' then c[k] = file.join_path(v) end @@ -9005,8 +9041,9 @@ end function resolvers.splitexpansions() local ie = instance.expansions for k,v in next, ie do - local t, h = { }, { } - for _,vv in ipairs(split_kpse_path(v)) do + local t, h, p = { }, { }, split_kpse_path(v) + for kk=1,#p do + local vv = p[kk] if vv ~= "" and not h[vv] then t[#t+1] = vv h[vv] = true @@ -9053,11 +9090,15 @@ function resolvers.serialize(files) end t[#t+1] = "return {" if instance.sortdata then - for _, k in pairs(sortedkeys(files)) do -- ipairs + local sortedfiles = sortedkeys(files) + for i=1,#sortedfiles do + local k = sortedfiles[i] local fk = files[k] if type(fk) == 'table' then t[#t+1] = "\t['" .. k .. "']={" - for _, kk in pairs(sortedkeys(fk)) do -- ipairs + local sortedfk = sortedkeys(fk) + for j=1,#sortedfk do + local kk = sortedfk[j] t[#t+1] = dump(kk,fk[kk],"\t\t") end t[#t+1] = "\t}," @@ -9169,7 +9210,9 @@ function resolvers.resetconfig() end function resolvers.loadnewconfig() - for _, cnf in ipairs(instance.luafiles) do + local luafiles = instance.luafiles + for i=1,#luafiles do + local cnf = luafiles[i] local pathname = file.dirname(cnf) local filename = file.join(pathname,resolvers.luaname) local blob = loadfile(filename) @@ -9214,7 +9257,9 @@ end function resolvers.loadoldconfig() if not instance.renewcache then - for _, cnf in ipairs(instance.cnffiles) do + local cnffiles = instance.cnffiles + for i=1,#cnffiles do + local cnf = cnffiles[i] local dname = file.dirname(cnf) resolvers.load_data(dname,'configuration') instance.order[#instance.order+1] = instance.configuration[dname] @@ -10018,13 +10063,14 @@ function resolvers.for_files(command, files, filetype, mustexist) if trace_locating then report('') -- ? end - for _, file in ipairs(files) do + for f=1,#files do + local file = files[f] local result = command(file,filetype,mustexist) if type(result) == 'string' then report(result) else - for _,v in ipairs(result) do - report(v) + for i=1,#result do + report(result[i]) -- could be unpack end end end @@ -10071,7 +10117,7 @@ end function table.sequenced(t,sep) -- temp here local s = { } - for k, v in pairs(t) do -- pairs? + for k, v in next, t do -- indexed? s[#s+1] = k .. "=" .. tostring(v) end return concat(s, sep or " | ") @@ -10103,8 +10149,9 @@ function resolvers.clean_path(str) end function resolvers.do_with_path(name,func) - for _, v in pairs(resolvers.expanded_path_list(name)) do -- pairs? - func("^"..resolvers.clean_path(v)) + local pathlist = resolvers.expanded_path_list(name) + for i=1,#pathlist do + func("^"..resolvers.clean_path(pathlist[i])) end end @@ -10113,7 +10160,9 @@ function resolvers.do_with_var(name,func) end function resolvers.with_files(pattern,handle) - for _, hash in ipairs(instance.hashes) do + local hashes = instance.hashes + for i=1,#hashes do + local hash = hashes[i] local blobpath = hash.tag local blobtype = hash.type if blobpath then @@ -10128,7 +10177,7 @@ function resolvers.with_files(pattern,handle) if type(v) == "string" then handle(blobtype,blobpath,v,k) else - for _,vv in pairs(v) do -- ipairs? + for _,vv in next, v do -- indexed handle(blobtype,blobpath,vv,k) end end @@ -10299,7 +10348,8 @@ function caches.setpath(...) caches.path = '.' end caches.path = resolvers.clean_path(caches.path) - if not table.is_empty({...}) then + local dirs = { ... } + if #dirs > 0 then local pth = dir.mkdirs(caches.path,...) return pth end @@ -10457,7 +10507,8 @@ end local function resolve(str) if type(str) == "table" then - for k, v in pairs(str) do -- ipairs + for k=1,#str do + local v = str[k] str[k] = resolve(v) or v end elseif str and str ~= "" then @@ -10470,7 +10521,7 @@ resolvers.resolve = resolve if os.uname then - for k, v in pairs(os.uname()) do + for k, v in next, os.uname() do if not prefixes[k] then prefixes[k] = function() return v end end @@ -10596,7 +10647,7 @@ end function containers.is_valid(container, name) if name and name ~= "" then local storage = container.storage[name] - return storage and not table.is_empty(storage) and storage.cache_version == container.version + return storage and storage.cache_version == container.version else return false end @@ -10700,12 +10751,13 @@ resolvers.automounted = resolvers.automounted or { } function resolvers.automount(usecache) local mountpaths = resolvers.clean_path_list(resolvers.expansion('TEXMFMOUNT')) - if table.is_empty(mountpaths) and usecache then + if (not mountpaths or #mountpaths == 0) and usecache then mountpaths = { caches.setpath("mount") } end - if not table.is_empty(mountpaths) then + if mountpaths and #mountpaths > 0 then statistics.starttiming(resolvers.instance) - for k, root in pairs(mountpaths) do + for k=1,#mountpaths do + local root = mountpaths[k] local f = io.open(root.."/url.tmi") if f then for line in f:lines() do @@ -11549,6 +11601,10 @@ function states.set_by_tag(tag,key,value,default,persistent) if not dk then dk = { } d[k] = dk + elseif type(dk) == "string" then + -- invalid table, unable to upgrade structure + -- hope for the best or delete the state file + break end d = dk end @@ -12142,7 +12198,8 @@ end function runners.save_script_session(filename, list) local t = { } - for _, key in ipairs(list) do + for i=1,#list do + local key = list[i] t[key] = environment.arguments[key] end io.savedata(filename,table.serialize(t,true)) @@ -12298,10 +12355,13 @@ function runners.execute_ctx_script(filename) environment.ownscript = fullname dofile(fullname) local savename = environment.arguments['save'] - if savename and runners.save_list and not table.is_empty(runners.save_list or { }) then - if type(savename) ~= "string" then savename = file.basename(fullname) end - savename = file.replacesuffix(savename,"cfg") - runners.save_script_session(savename, runners.save_list) + if savename then + local save_list = runners.save_list + if save_list and next(save_list) then + if type(savename) ~= "string" then savename = file.basename(fullname) end + savename = file.replacesuffix(savename,"cfg") + runners.save_script_session(savename,save_list) + end end return true end @@ -12314,7 +12374,8 @@ function runners.execute_ctx_script(filename) local result = dir.glob((string.gsub(context,"mtx%-context","mtx-*"))) -- () needed local valid = { } table.sort(result) - for _, scriptname in ipairs(result) do + for i=1,#result do + local scriptname = result[i] local scriptbase = string.match(scriptname,".*mtx%-([^%-]-)%.lua") if scriptbase then local data = io.loaddata(scriptname) @@ -12329,7 +12390,8 @@ function runners.execute_ctx_script(filename) logs.reportline() logs.simple("no script name given, known scripts:") logs.simple() - for k, v in ipairs(valid) do + for k=1,#valid do + local v = valid[k] logs.simple("%-12s %4s %s",v[1],v[2],v[3]) end end diff --git a/scripts/context/stubs/unix/luatools b/scripts/context/stubs/unix/luatools index e6fdd50d5..1d87322c1 100755 --- a/scripts/context/stubs/unix/luatools +++ b/scripts/context/stubs/unix/luatools @@ -269,7 +269,7 @@ function string:totable() return lpegmatch(pattern,self) end ---~ for _, str in ipairs { +--~ local t = { --~ "1234567123456712345671234567", --~ "a\tb\tc", --~ "aa\tbb\tcc", @@ -277,7 +277,10 @@ end --~ "aaaa\tbbbb\tcccc", --~ "aaaaa\tbbbbb\tccccc", --~ "aaaaaa\tbbbbbb\tcccccc", ---~ } do print(string.tabtospace(str)) end +--~ } +--~ for k,v do +--~ print(string.tabtospace(t[k])) +--~ end function string.tabtospace(str,tab) -- we don't handle embedded newlines @@ -446,6 +449,8 @@ function string:split(separator) return match(c,self) end +lpeg.splitters = cache + local cache = { } function lpeg.checkedsplit(separator,str) @@ -510,7 +515,7 @@ table.join = table.concat local concat, sort, insert, remove = table.concat, table.sort, table.insert, table.remove local format, find, gsub, lower, dump, match = string.format, string.find, string.gsub, string.lower, string.dump, string.match local getmetatable, setmetatable = getmetatable, setmetatable -local type, next, tostring, tonumber, ipairs, pairs = type, next, tostring, tonumber, ipairs, pairs +local type, next, tostring, tonumber, ipairs = type, next, tostring, tonumber, ipairs local unpack = unpack or table.unpack function table.strip(tab) @@ -577,7 +582,7 @@ end table.sortedkeys = sortedkeys table.sortedhashkeys = sortedhashkeys -function table.sortedpairs(t) +function table.sortedhash(t) local s = sortedhashkeys(t) -- maybe just sortedkeys local n = 0 local function kv(s) @@ -588,6 +593,8 @@ function table.sortedpairs(t) return kv, s end +table.sortedpairs = table.sortedhash + function table.append(t, list) for _,v in next, list do insert(t,v) @@ -710,18 +717,18 @@ end -- slower than #t on indexed tables (#t only returns the size of the numerically indexed slice) -function table.is_empty(t) +function table.is_empty(t) -- obolete, use inline code instead return not t or not next(t) end -function table.one_entry(t) +function table.one_entry(t) -- obolete, use inline code instead local n = next(t) return n and not next(t,n) end -function table.starts_at(t) - return ipairs(t,1)(t,0) -end +--~ function table.starts_at(t) -- obsolete, not nice +--~ return ipairs(t,1)(t,0) +--~ end function table.tohash(t,value) local h = { } @@ -825,7 +832,7 @@ local function do_serialize(root,name,depth,level,indexed) end -- we could check for k (index) being number (cardinal) if root and next(root) then - local first, last = nil, 0 -- #root cannot be trusted here + local first, last = nil, 0 -- #root cannot be trusted here (will be ok in 5.2 when ipairs is gone) if compact then -- NOT: for k=1,#root do (we need to quit at nil) for k,v in ipairs(root) do -- can we use next? @@ -1534,13 +1541,14 @@ function io.ask(question,default,options) elseif not options then return answer else - for _,v in pairs(options) do - if v == answer then + for k=1,#options do + if options[k] == answer then return answer end end local pattern = "^" .. answer - for _,v in pairs(options) do + for k=1,#options do + local v = options[k] if find(v,pattern) then return v end @@ -2314,7 +2322,7 @@ function file.splitname(str) -- returns drive, path, base, suffix return lpegmatch(pattern,str) end --- function test(t) for k, v in pairs(t) do print(v, "=>", file.splitname(v)) end end +-- function test(t) for k, v in next, t do print(v, "=>", file.splitname(v)) end end -- -- test { "c:", "c:/aa", "c:/aa/bb", "c:/aa/bb/cc", "c:/aa/bb/cc.dd", "c:/aa/bb/cc.dd.ee" } -- test { "c:", "c:aa", "c:aa/bb", "c:aa/bb/cc", "c:aa/bb/cc.dd", "c:aa/bb/cc.dd.ee" } @@ -2759,8 +2767,9 @@ local make_indeed = true -- false if string.find(os.getenv("PATH"),";") then -- os.type == "windows" function dir.mkdirs(...) - local str, pth = "", "" - for _, s in ipairs({...}) do + local str, pth, t = "", "", { ... } + for i=1,#t do + local s = t[i] if s ~= "" then if str ~= "" then str = str .. "/" .. s @@ -2858,8 +2867,9 @@ if string.find(os.getenv("PATH"),";") then -- os.type == "windows" else function dir.mkdirs(...) - local str, pth = "", "" - for _, s in ipairs({...}) do + local str, pth, t = "", "", { ... } + for i=1,#t do + local s = t[i] if s ~= "" then if str ~= "" then str = str .. "/" .. s @@ -3031,14 +3041,20 @@ unicode.utfname = { [4] = 'utf-32-be' } -function unicode.utftype(f) -- \000 fails ! +-- \000 fails in <= 5.0 but is valid in >=5.1 where %z is depricated + +function unicode.utftype(f) local str = f:read(4) if not str then f:seek('set') return 0 - elseif find(str,"^%z%z\254\255") then + -- elseif find(str,"^%z%z\254\255") then -- depricated + -- elseif find(str,"^\000\000\254\255") then -- not permitted and bugged + elseif find(str,"\000\000\254\255",1,true) then -- seems to work okay (TH) return 4 - elseif find(str,"^\255\254%z%z") then + -- elseif find(str,"^\255\254%z%z") then -- depricated + -- elseif find(str,"^\255\254\000\000") then -- not permitted and bugged + elseif find(str,"\255\254\000\000",1,true) then -- seems to work okay (TH) return 3 elseif find(str,"^\254\255") then f:seek('set',2) @@ -3239,6 +3255,7 @@ if not modules then modules = { } end modules ['l-utils'] = { local gsub = string.gsub local concat = table.concat +local type, next = type, next if not utils then utils = { } end if not utils.merger then utils.merger = { } end @@ -3314,9 +3331,10 @@ function utils.merger._self_libs_(libs,list) if type(libs) == 'string' then libs = { libs } end if type(list) == 'string' then list = { list } end local foundpath = nil - for _, lib in ipairs(libs) do - for _, pth in ipairs(list) do - pth = gsub(pth,"\\","/") -- file.clean_path + for i=1,#libs do + local lib = libs[i] + for j=1,#list do + local pth = gsub(list[j],"\\","/") -- file.clean_path utils.report("checking library path %s",pth) local name = pth .. "/" .. lib if lfs.isfile(name) then @@ -3328,7 +3346,8 @@ function utils.merger._self_libs_(libs,list) if foundpath then utils.report("using library path %s",foundpath) local right, wrong = { }, { } - for _, lib in ipairs(libs) do + for i=1,#libs do + local lib = libs[i] local fullname = foundpath .. "/" .. lib if lfs.isfile(fullname) then -- right[#right+1] = lib @@ -3681,6 +3700,8 @@ if not modules then modules = { } end modules ['trac-tra'] = { -- bound to a variable, like node.new, node.copy etc (contrary to for instance -- node.has_attribute which is bound to a has_attribute local variable in mkiv) +local debug = require "debug" + local getinfo = debug.getinfo local type, next = type, next local concat = table.concat @@ -3728,7 +3749,7 @@ function debugger.showstats(printer,threshold) local total, grandtotal, functions = 0, 0, 0 printer("\n") -- ugly but ok -- table.sort(counters) - for func, count in pairs(counters) do + for func, count in next, counters do if count > threshold then local name = getname(func) if not find(name,"for generator") then @@ -3763,7 +3784,7 @@ end --~ local total, grandtotal, functions = 0, 0, 0 --~ printer("\n") -- ugly but ok --~ -- table.sort(counters) ---~ for func, count in pairs(counters) do +--~ for func, count in next, counters do --~ if count > threshold then --~ printer(format("%8i %s", count, func)) --~ total = total + count @@ -3939,8 +3960,9 @@ end function setters.show(t) commands.writestatus("","") - for k,v in ipairs(setters.list(t)) do - commands.writestatus(t.name,v) + local list = setters.list(t) + for k=1,#list do + commands.writestatus(t.name,list[k]) end commands.writestatus("","") end @@ -4064,7 +4086,8 @@ if not environment.jobname then environ function environment.initialize_arguments(arg) local arguments, files = { }, { } environment.arguments, environment.files, environment.sortedflags = arguments, files, nil - for index, argument in pairs(arg) do + for index=1,#arg do + local argument = arg[index] if index > 0 then local flag, value = match(argument,"^%-+(.-)=(.-)$") if flag then @@ -4097,14 +4120,15 @@ function environment.argument(name,partial) return arguments[name] elseif partial then if not sortedflags then - sortedflags = { } - for _,v in pairs(table.sortedkeys(arguments)) do - sortedflags[#sortedflags+1] = "^" .. v + sortedflags = table.sortedkeys(arguments) + for k=1,#sortedflags do + sortedflags[k] = "^" .. sortedflags[k] end environment.sortedflags = sortedflags end -- example of potential clash: ^mode ^modefile - for _,v in ipairs(sortedflags) do + for k=1,#sortedflags do + local v = sortedflags[k] if find(name,v) then return arguments[sub(v,2,#v)] end @@ -4113,9 +4137,13 @@ function environment.argument(name,partial) return nil end +environment.argument("x",true) + function environment.split_arguments(separator) -- rather special, cut-off before separator local done, before, after = false, { }, { } - for _,v in ipairs(environment.original_arguments) do + local original_arguments = environment.original_arguments + for k=1,#original_arguments do + local v = original_arguments[k] if not done and v == separator then done = true elseif done then @@ -4134,9 +4162,10 @@ function environment.reconstruct_commandline(arg,noquote) a = resolvers.resolve(a) a = unquote(a) return a - elseif next(arg) then + elseif #arg > 0 then local result = { } - for _,a in ipairs(arg) do -- ipairs 1 .. #n + for i=1,#arg do + local a = arg[i] a = resolvers.resolve(a) a = unquote(a) a = gsub(a,'"','\\"') -- tricky @@ -4157,7 +4186,8 @@ if arg then -- new, reconstruct quoted snippets (maybe better just remove the " then and add them later) local newarg, instring = { }, false - for index, argument in ipairs(arg) do + for index=1,#arg do + local argument = arg[index] if find(argument,"^\"") then newarg[#newarg+1] = gsub(argument,"^\"","") if not find(argument,"\"$") then @@ -4840,7 +4870,7 @@ if not modules then modules = { } end modules ['data-inp'] = { -- * some public auxiliary functions were made private -- -- TODO: os.getenv -> os.env[] --- TODO: instances.[hashes,cnffiles,configurations,522] -> ipairs (alles check, sneller) +-- TODO: instances.[hashes,cnffiles,configurations,522] -- TODO: check escaping in find etc, too much, too slow -- This lib is multi-purpose and can be loaded again later on so that @@ -5251,8 +5281,8 @@ local function splitpathexpr(str, t, validate) end end if trace_expansions then - for k,v in ipairs(t) do - logs.report("fileio","% 4i: %s",k,v) + for k=1,#t do + logs.report("fileio","% 4i: %s",k,t[k]) end end return t @@ -5457,7 +5487,9 @@ local function load_cnf_file(fname) end local function collapse_cnf_data() -- potential optimization: pass start index (setup and configuration are shared) - for _,c in ipairs(instance.order) do + local order = instance.order + for i=1,#order do + local c = order[i] for k,v in next, c do if not instance.variables[k] then if instance.environment[k] then @@ -5473,8 +5505,9 @@ end function resolvers.load_cnf() local function loadoldconfigdata() - for _, fname in ipairs(instance.cnffiles) do - load_cnf_file(fname) + local cnffiles = instance.cnffiles + for i=1,#cnffiles do + load_cnf_file(cnffiles[i]) end end -- instance.cnffiles contain complete names now ! @@ -5486,9 +5519,10 @@ function resolvers.load_cnf() logs.report("fileio","no cnf files found (TEXMFCNF may not be set/known)") end else - instance.rootpath = instance.cnffiles[1] - for k,fname in ipairs(instance.cnffiles) do - instance.cnffiles[k] = file.collapse_path(fname) + local cnffiles = instance.cnffiles + instance.rootpath = cnffiles[1] + for k=1,#cnffiles do + instance.cnffiles[k] = file.collapse_path(cnffiles[k]) end for i=1,3 do instance.rootpath = file.dirname(instance.rootpath) @@ -5516,8 +5550,9 @@ function resolvers.load_lua() -- yet harmless else instance.rootpath = instance.luafiles[1] - for k,fname in ipairs(instance.luafiles) do - instance.luafiles[k] = file.collapse_path(fname) + local luafiles = instance.luafiles + for k=1,#luafiles do + instance.luafiles[k] = file.collapse_path(luafiles[k]) end for i=1,3 do instance.rootpath = file.dirname(instance.rootpath) @@ -5580,7 +5615,9 @@ end -- locators function resolvers.locatelists() - for _, path in ipairs(resolvers.clean_path_list('TEXMF')) do + local texmfpaths = resolvers.clean_path_list('TEXMF') + for i=1,#texmfpaths do + local path = texmfpaths[i] if trace_locating then logs.report("fileio","locating list of '%s'",path) end @@ -5613,7 +5650,9 @@ function resolvers.loadfiles() instance.loaderror = false instance.files = { } if not instance.renewcache then - for _, hash in ipairs(instance.hashes) do + local hashes = instance.hashes + for k=1,#hashes do + local hash = hashes[k] resolvers.hashdatabase(hash.tag,hash.name) if instance.loaderror then break end end @@ -5627,8 +5666,9 @@ end -- generators: function resolvers.loadlists() - for _, hash in ipairs(instance.hashes) do - resolvers.generatedatabase(hash.tag) + local hashes = instance.hashes + for i=1,#hashes do + resolvers.generatedatabase(hashes[i].tag) end end @@ -5725,8 +5765,7 @@ end -- we join them and split them after the expansion has taken place. This -- is more convenient. -local checkedsplit = string.checkedsplit -local normalsplit = string.split +--~ local checkedsplit = string.checkedsplit local cache = { } @@ -5750,8 +5789,8 @@ local split = lpegmatch(splitter,str) end if trace_expansions then logs.report("fileio","splitting path specification '%s'",str) - for k,v in ipairs(found) do - logs.report("fileio","% 4i: %s",k,v) + for k=1,#found do + logs.report("fileio","% 4i: %s",k,found[k]) end end cache[str] = found @@ -5763,8 +5802,9 @@ end resolvers.split_kpse_path = split_kpse_path function resolvers.splitconfig() - for i,c in ipairs(instance) do - for k,v in pairs(c) do + for i=1,#instance do + local c = instance[i] + for k,v in next, c do if type(v) == 'string' then local t = split_kpse_path(v) if #t > 1 then @@ -5776,8 +5816,10 @@ function resolvers.splitconfig() end function resolvers.joinconfig() - for i,c in ipairs(instance.order) do - for k,v in pairs(c) do -- ipairs? + local order = instance.order + for i=1,#order do + local c = order[i] + for k,v in next, c do -- indexed? if type(v) == 'table' then c[k] = file.join_path(v) end @@ -5804,8 +5846,9 @@ end function resolvers.splitexpansions() local ie = instance.expansions for k,v in next, ie do - local t, h = { }, { } - for _,vv in ipairs(split_kpse_path(v)) do + local t, h, p = { }, { }, split_kpse_path(v) + for kk=1,#p do + local vv = p[kk] if vv ~= "" and not h[vv] then t[#t+1] = vv h[vv] = true @@ -5852,11 +5895,15 @@ function resolvers.serialize(files) end t[#t+1] = "return {" if instance.sortdata then - for _, k in pairs(sortedkeys(files)) do -- ipairs + local sortedfiles = sortedkeys(files) + for i=1,#sortedfiles do + local k = sortedfiles[i] local fk = files[k] if type(fk) == 'table' then t[#t+1] = "\t['" .. k .. "']={" - for _, kk in pairs(sortedkeys(fk)) do -- ipairs + local sortedfk = sortedkeys(fk) + for j=1,#sortedfk do + local kk = sortedfk[j] t[#t+1] = dump(kk,fk[kk],"\t\t") end t[#t+1] = "\t}," @@ -5968,7 +6015,9 @@ function resolvers.resetconfig() end function resolvers.loadnewconfig() - for _, cnf in ipairs(instance.luafiles) do + local luafiles = instance.luafiles + for i=1,#luafiles do + local cnf = luafiles[i] local pathname = file.dirname(cnf) local filename = file.join(pathname,resolvers.luaname) local blob = loadfile(filename) @@ -6013,7 +6062,9 @@ end function resolvers.loadoldconfig() if not instance.renewcache then - for _, cnf in ipairs(instance.cnffiles) do + local cnffiles = instance.cnffiles + for i=1,#cnffiles do + local cnf = cnffiles[i] local dname = file.dirname(cnf) resolvers.load_data(dname,'configuration') instance.order[#instance.order+1] = instance.configuration[dname] @@ -6817,13 +6868,14 @@ function resolvers.for_files(command, files, filetype, mustexist) if trace_locating then report('') -- ? end - for _, file in ipairs(files) do + for f=1,#files do + local file = files[f] local result = command(file,filetype,mustexist) if type(result) == 'string' then report(result) else - for _,v in ipairs(result) do - report(v) + for i=1,#result do + report(result[i]) -- could be unpack end end end @@ -6870,7 +6922,7 @@ end function table.sequenced(t,sep) -- temp here local s = { } - for k, v in pairs(t) do -- pairs? + for k, v in next, t do -- indexed? s[#s+1] = k .. "=" .. tostring(v) end return concat(s, sep or " | ") @@ -6902,8 +6954,9 @@ function resolvers.clean_path(str) end function resolvers.do_with_path(name,func) - for _, v in pairs(resolvers.expanded_path_list(name)) do -- pairs? - func("^"..resolvers.clean_path(v)) + local pathlist = resolvers.expanded_path_list(name) + for i=1,#pathlist do + func("^"..resolvers.clean_path(pathlist[i])) end end @@ -6912,7 +6965,9 @@ function resolvers.do_with_var(name,func) end function resolvers.with_files(pattern,handle) - for _, hash in ipairs(instance.hashes) do + local hashes = instance.hashes + for i=1,#hashes do + local hash = hashes[i] local blobpath = hash.tag local blobtype = hash.type if blobpath then @@ -6927,7 +6982,7 @@ function resolvers.with_files(pattern,handle) if type(v) == "string" then handle(blobtype,blobpath,v,k) else - for _,vv in pairs(v) do -- ipairs? + for _,vv in next, v do -- indexed handle(blobtype,blobpath,vv,k) end end @@ -7098,7 +7153,8 @@ function caches.setpath(...) caches.path = '.' end caches.path = resolvers.clean_path(caches.path) - if not table.is_empty({...}) then + local dirs = { ... } + if #dirs > 0 then local pth = dir.mkdirs(caches.path,...) return pth end @@ -7281,7 +7337,7 @@ end function containers.is_valid(container, name) if name and name ~= "" then local storage = container.storage[name] - return storage and not table.is_empty(storage) and storage.cache_version == container.version + return storage and storage.cache_version == container.version else return false end @@ -7385,12 +7441,13 @@ resolvers.automounted = resolvers.automounted or { } function resolvers.automount(usecache) local mountpaths = resolvers.clean_path_list(resolvers.expansion('TEXMFMOUNT')) - if table.is_empty(mountpaths) and usecache then + if (not mountpaths or #mountpaths == 0) and usecache then mountpaths = { caches.setpath("mount") } end - if not table.is_empty(mountpaths) then + if mountpaths and #mountpaths > 0 then statistics.starttiming(resolvers.instance) - for k, root in pairs(mountpaths) do + for k=1,#mountpaths do + local root = mountpaths[k] local f = io.open(root.."/url.tmi") if f then for line in f:lines() do @@ -7661,7 +7718,9 @@ local function list(list,report) local instance = resolvers.instance local pat = upper(pattern or "","") local report = report or texio.write_nl - for _,key in pairs(table.sortedkeys(list)) do + local sorted = table.sortedkeys(list) + for i=1,#sorted do + local key = sorted[i] if instance.pattern == "" or find(upper(key),pat) then if instance.kpseonly then if instance.kpsevars[key] then @@ -7680,11 +7739,14 @@ function resolvers.listers.expansions() list(resolvers.instance.expansions) end function resolvers.listers.configurations(report) local report = report or texio.write_nl local instance = resolvers.instance - for _,key in ipairs(table.sortedkeys(instance.kpsevars)) do + local sorted = table.sortedkeys(instance.kpsevars) + for i=1,#sorted do + local key = sorted[i] if not instance.pattern or (instance.pattern=="") or find(key,instance.pattern) then report(format("%s\n",key)) - for i,c in ipairs(instance.order) do - local str = c[key] + local order = instance.order + for i=1,#order do + local str = order[i][key] if str then report(format("\t%s\t%s",i,str)) end @@ -7945,8 +8007,9 @@ function runners.make_format(texname) logs.simple("using uncompiled initialization file: %s",luaname) end else - for _, v in pairs({instance.luaname, instance.progname, barename}) do - v = string.gsub(v..".lua","%.lua%.lua$",".lua") + local what = { instance.luaname, instance.progname, barename } + for k=1,#what do + local v = string.gsub(what[k]..".lua","%.lua%.lua$",".lua") if v and (v ~= "") then luaname = resolvers.find_files(v)[1] or "" if luaname ~= "" then @@ -7970,7 +8033,8 @@ function runners.make_format(texname) logs.simple("using lua initialization file: %s",luaname) local mp = dir.glob(file.removesuffix(file.basename(luaname)).."-*.mem") if mp and #mp > 0 then - for _, name in ipairs(mp) do + for i=1,#mp do + local name = mp[i] logs.simple("removing related mplib format %s", file.basename(name)) os.remove(name) end diff --git a/scripts/context/stubs/unix/mtxrun b/scripts/context/stubs/unix/mtxrun index 727099027..d3e50e00e 100755 --- a/scripts/context/stubs/unix/mtxrun +++ b/scripts/context/stubs/unix/mtxrun @@ -278,7 +278,7 @@ function string:totable() return lpegmatch(pattern,self) end ---~ for _, str in ipairs { +--~ local t = { --~ "1234567123456712345671234567", --~ "a\tb\tc", --~ "aa\tbb\tcc", @@ -286,7 +286,10 @@ end --~ "aaaa\tbbbb\tcccc", --~ "aaaaa\tbbbbb\tccccc", --~ "aaaaaa\tbbbbbb\tcccccc", ---~ } do print(string.tabtospace(str)) end +--~ } +--~ for k,v do +--~ print(string.tabtospace(t[k])) +--~ end function string.tabtospace(str,tab) -- we don't handle embedded newlines @@ -455,6 +458,8 @@ function string:split(separator) return match(c,self) end +lpeg.splitters = cache + local cache = { } function lpeg.checkedsplit(separator,str) @@ -519,7 +524,7 @@ table.join = table.concat local concat, sort, insert, remove = table.concat, table.sort, table.insert, table.remove local format, find, gsub, lower, dump, match = string.format, string.find, string.gsub, string.lower, string.dump, string.match local getmetatable, setmetatable = getmetatable, setmetatable -local type, next, tostring, tonumber, ipairs, pairs = type, next, tostring, tonumber, ipairs, pairs +local type, next, tostring, tonumber, ipairs = type, next, tostring, tonumber, ipairs local unpack = unpack or table.unpack function table.strip(tab) @@ -586,7 +591,7 @@ end table.sortedkeys = sortedkeys table.sortedhashkeys = sortedhashkeys -function table.sortedpairs(t) +function table.sortedhash(t) local s = sortedhashkeys(t) -- maybe just sortedkeys local n = 0 local function kv(s) @@ -597,6 +602,8 @@ function table.sortedpairs(t) return kv, s end +table.sortedpairs = table.sortedhash + function table.append(t, list) for _,v in next, list do insert(t,v) @@ -719,18 +726,18 @@ end -- slower than #t on indexed tables (#t only returns the size of the numerically indexed slice) -function table.is_empty(t) +function table.is_empty(t) -- obolete, use inline code instead return not t or not next(t) end -function table.one_entry(t) +function table.one_entry(t) -- obolete, use inline code instead local n = next(t) return n and not next(t,n) end -function table.starts_at(t) - return ipairs(t,1)(t,0) -end +--~ function table.starts_at(t) -- obsolete, not nice +--~ return ipairs(t,1)(t,0) +--~ end function table.tohash(t,value) local h = { } @@ -834,7 +841,7 @@ local function do_serialize(root,name,depth,level,indexed) end -- we could check for k (index) being number (cardinal) if root and next(root) then - local first, last = nil, 0 -- #root cannot be trusted here + local first, last = nil, 0 -- #root cannot be trusted here (will be ok in 5.2 when ipairs is gone) if compact then -- NOT: for k=1,#root do (we need to quit at nil) for k,v in ipairs(root) do -- can we use next? @@ -1543,13 +1550,14 @@ function io.ask(question,default,options) elseif not options then return answer else - for _,v in pairs(options) do - if v == answer then + for k=1,#options do + if options[k] == answer then return answer end end local pattern = "^" .. answer - for _,v in pairs(options) do + for k=1,#options do + local v = options[k] if find(v,pattern) then return v end @@ -2323,7 +2331,7 @@ function file.splitname(str) -- returns drive, path, base, suffix return lpegmatch(pattern,str) end --- function test(t) for k, v in pairs(t) do print(v, "=>", file.splitname(v)) end end +-- function test(t) for k, v in next, t do print(v, "=>", file.splitname(v)) end end -- -- test { "c:", "c:/aa", "c:/aa/bb", "c:/aa/bb/cc", "c:/aa/bb/cc.dd", "c:/aa/bb/cc.dd.ee" } -- test { "c:", "c:aa", "c:aa/bb", "c:aa/bb/cc", "c:aa/bb/cc.dd", "c:aa/bb/cc.dd.ee" } @@ -2768,8 +2776,9 @@ local make_indeed = true -- false if string.find(os.getenv("PATH"),";") then -- os.type == "windows" function dir.mkdirs(...) - local str, pth = "", "" - for _, s in ipairs({...}) do + local str, pth, t = "", "", { ... } + for i=1,#t do + local s = t[i] if s ~= "" then if str ~= "" then str = str .. "/" .. s @@ -2867,8 +2876,9 @@ if string.find(os.getenv("PATH"),";") then -- os.type == "windows" else function dir.mkdirs(...) - local str, pth = "", "" - for _, s in ipairs({...}) do + local str, pth, t = "", "", { ... } + for i=1,#t do + local s = t[i] if s ~= "" then if str ~= "" then str = str .. "/" .. s @@ -3049,6 +3059,7 @@ if not modules then modules = { } end modules ['l-utils'] = { local gsub = string.gsub local concat = table.concat +local type, next = type, next if not utils then utils = { } end if not utils.merger then utils.merger = { } end @@ -3124,9 +3135,10 @@ function utils.merger._self_libs_(libs,list) if type(libs) == 'string' then libs = { libs } end if type(list) == 'string' then list = { list } end local foundpath = nil - for _, lib in ipairs(libs) do - for _, pth in ipairs(list) do - pth = gsub(pth,"\\","/") -- file.clean_path + for i=1,#libs do + local lib = libs[i] + for j=1,#list do + local pth = gsub(list[j],"\\","/") -- file.clean_path utils.report("checking library path %s",pth) local name = pth .. "/" .. lib if lfs.isfile(name) then @@ -3138,7 +3150,8 @@ function utils.merger._self_libs_(libs,list) if foundpath then utils.report("using library path %s",foundpath) local right, wrong = { }, { } - for _, lib in ipairs(libs) do + for i=1,#libs do + local lib = libs[i] local fullname = foundpath .. "/" .. lib if lfs.isfile(fullname) then -- right[#right+1] = lib @@ -3491,6 +3504,8 @@ if not modules then modules = { } end modules ['trac-tra'] = { -- bound to a variable, like node.new, node.copy etc (contrary to for instance -- node.has_attribute which is bound to a has_attribute local variable in mkiv) +local debug = require "debug" + local getinfo = debug.getinfo local type, next = type, next local concat = table.concat @@ -3538,7 +3553,7 @@ function debugger.showstats(printer,threshold) local total, grandtotal, functions = 0, 0, 0 printer("\n") -- ugly but ok -- table.sort(counters) - for func, count in pairs(counters) do + for func, count in next, counters do if count > threshold then local name = getname(func) if not find(name,"for generator") then @@ -3573,7 +3588,7 @@ end --~ local total, grandtotal, functions = 0, 0, 0 --~ printer("\n") -- ugly but ok --~ -- table.sort(counters) ---~ for func, count in pairs(counters) do +--~ for func, count in next, counters do --~ if count > threshold then --~ printer(format("%8i %s", count, func)) --~ total = total + count @@ -3749,8 +3764,9 @@ end function setters.show(t) commands.writestatus("","") - for k,v in ipairs(setters.list(t)) do - commands.writestatus(t.name,v) + local list = setters.list(t) + for k=1,#list do + commands.writestatus(t.name,list[k]) end commands.writestatus("","") end @@ -4550,7 +4566,7 @@ local function copy(old,tables) if not tables[old] then tables[old] = new end - for k,v in pairs(old) do + for k,v in next, old do new[k] = (type(v) == "table" and (tables[v] or copy(v, tables))) or v end local mt = getmetatable(old) @@ -6316,7 +6332,7 @@ function xml.strip_leading_spaces(dk,d,k) -- cosmetic, for manual end --~ xml.escapes = { ['&'] = '&', ['<'] = '<', ['>'] = '>', ['"'] = '"' } ---~ xml.unescapes = { } for k,v in pairs(xml.escapes) do xml.unescapes[v] = k end +--~ xml.unescapes = { } for k,v in next, xml.escapes do xml.unescapes[v] = k end --~ function xml.escaped (str) return (gsub(str,"(.)" , xml.escapes )) end --~ function xml.unescaped(str) return (gsub(str,"(&.-;)", xml.unescapes)) end @@ -7265,7 +7281,8 @@ if not environment.jobname then environ function environment.initialize_arguments(arg) local arguments, files = { }, { } environment.arguments, environment.files, environment.sortedflags = arguments, files, nil - for index, argument in pairs(arg) do + for index=1,#arg do + local argument = arg[index] if index > 0 then local flag, value = match(argument,"^%-+(.-)=(.-)$") if flag then @@ -7298,14 +7315,15 @@ function environment.argument(name,partial) return arguments[name] elseif partial then if not sortedflags then - sortedflags = { } - for _,v in pairs(table.sortedkeys(arguments)) do - sortedflags[#sortedflags+1] = "^" .. v + sortedflags = table.sortedkeys(arguments) + for k=1,#sortedflags do + sortedflags[k] = "^" .. sortedflags[k] end environment.sortedflags = sortedflags end -- example of potential clash: ^mode ^modefile - for _,v in ipairs(sortedflags) do + for k=1,#sortedflags do + local v = sortedflags[k] if find(name,v) then return arguments[sub(v,2,#v)] end @@ -7314,9 +7332,13 @@ function environment.argument(name,partial) return nil end +environment.argument("x",true) + function environment.split_arguments(separator) -- rather special, cut-off before separator local done, before, after = false, { }, { } - for _,v in ipairs(environment.original_arguments) do + local original_arguments = environment.original_arguments + for k=1,#original_arguments do + local v = original_arguments[k] if not done and v == separator then done = true elseif done then @@ -7335,9 +7357,10 @@ function environment.reconstruct_commandline(arg,noquote) a = resolvers.resolve(a) a = unquote(a) return a - elseif next(arg) then + elseif #arg > 0 then local result = { } - for _,a in ipairs(arg) do -- ipairs 1 .. #n + for i=1,#arg do + local a = arg[i] a = resolvers.resolve(a) a = unquote(a) a = gsub(a,'"','\\"') -- tricky @@ -7358,7 +7381,8 @@ if arg then -- new, reconstruct quoted snippets (maybe better just remove the " then and add them later) local newarg, instring = { }, false - for index, argument in ipairs(arg) do + for index=1,#arg do + local argument = arg[index] if find(argument,"^\"") then newarg[#newarg+1] = gsub(argument,"^\"","") if not find(argument,"\"$") then @@ -8041,7 +8065,7 @@ if not modules then modules = { } end modules ['data-inp'] = { -- * some public auxiliary functions were made private -- -- TODO: os.getenv -> os.env[] --- TODO: instances.[hashes,cnffiles,configurations,522] -> ipairs (alles check, sneller) +-- TODO: instances.[hashes,cnffiles,configurations,522] -- TODO: check escaping in find etc, too much, too slow -- This lib is multi-purpose and can be loaded again later on so that @@ -8452,8 +8476,8 @@ local function splitpathexpr(str, t, validate) end end if trace_expansions then - for k,v in ipairs(t) do - logs.report("fileio","% 4i: %s",k,v) + for k=1,#t do + logs.report("fileio","% 4i: %s",k,t[k]) end end return t @@ -8658,7 +8682,9 @@ local function load_cnf_file(fname) end local function collapse_cnf_data() -- potential optimization: pass start index (setup and configuration are shared) - for _,c in ipairs(instance.order) do + local order = instance.order + for i=1,#order do + local c = order[i] for k,v in next, c do if not instance.variables[k] then if instance.environment[k] then @@ -8674,8 +8700,9 @@ end function resolvers.load_cnf() local function loadoldconfigdata() - for _, fname in ipairs(instance.cnffiles) do - load_cnf_file(fname) + local cnffiles = instance.cnffiles + for i=1,#cnffiles do + load_cnf_file(cnffiles[i]) end end -- instance.cnffiles contain complete names now ! @@ -8687,9 +8714,10 @@ function resolvers.load_cnf() logs.report("fileio","no cnf files found (TEXMFCNF may not be set/known)") end else - instance.rootpath = instance.cnffiles[1] - for k,fname in ipairs(instance.cnffiles) do - instance.cnffiles[k] = file.collapse_path(fname) + local cnffiles = instance.cnffiles + instance.rootpath = cnffiles[1] + for k=1,#cnffiles do + instance.cnffiles[k] = file.collapse_path(cnffiles[k]) end for i=1,3 do instance.rootpath = file.dirname(instance.rootpath) @@ -8717,8 +8745,9 @@ function resolvers.load_lua() -- yet harmless else instance.rootpath = instance.luafiles[1] - for k,fname in ipairs(instance.luafiles) do - instance.luafiles[k] = file.collapse_path(fname) + local luafiles = instance.luafiles + for k=1,#luafiles do + instance.luafiles[k] = file.collapse_path(luafiles[k]) end for i=1,3 do instance.rootpath = file.dirname(instance.rootpath) @@ -8781,7 +8810,9 @@ end -- locators function resolvers.locatelists() - for _, path in ipairs(resolvers.clean_path_list('TEXMF')) do + local texmfpaths = resolvers.clean_path_list('TEXMF') + for i=1,#texmfpaths do + local path = texmfpaths[i] if trace_locating then logs.report("fileio","locating list of '%s'",path) end @@ -8814,7 +8845,9 @@ function resolvers.loadfiles() instance.loaderror = false instance.files = { } if not instance.renewcache then - for _, hash in ipairs(instance.hashes) do + local hashes = instance.hashes + for k=1,#hashes do + local hash = hashes[k] resolvers.hashdatabase(hash.tag,hash.name) if instance.loaderror then break end end @@ -8828,8 +8861,9 @@ end -- generators: function resolvers.loadlists() - for _, hash in ipairs(instance.hashes) do - resolvers.generatedatabase(hash.tag) + local hashes = instance.hashes + for i=1,#hashes do + resolvers.generatedatabase(hashes[i].tag) end end @@ -8926,8 +8960,7 @@ end -- we join them and split them after the expansion has taken place. This -- is more convenient. -local checkedsplit = string.checkedsplit -local normalsplit = string.split +--~ local checkedsplit = string.checkedsplit local cache = { } @@ -8951,8 +8984,8 @@ local split = lpegmatch(splitter,str) end if trace_expansions then logs.report("fileio","splitting path specification '%s'",str) - for k,v in ipairs(found) do - logs.report("fileio","% 4i: %s",k,v) + for k=1,#found do + logs.report("fileio","% 4i: %s",k,found[k]) end end cache[str] = found @@ -8964,8 +8997,9 @@ end resolvers.split_kpse_path = split_kpse_path function resolvers.splitconfig() - for i,c in ipairs(instance) do - for k,v in pairs(c) do + for i=1,#instance do + local c = instance[i] + for k,v in next, c do if type(v) == 'string' then local t = split_kpse_path(v) if #t > 1 then @@ -8977,8 +9011,10 @@ function resolvers.splitconfig() end function resolvers.joinconfig() - for i,c in ipairs(instance.order) do - for k,v in pairs(c) do -- ipairs? + local order = instance.order + for i=1,#order do + local c = order[i] + for k,v in next, c do -- indexed? if type(v) == 'table' then c[k] = file.join_path(v) end @@ -9005,8 +9041,9 @@ end function resolvers.splitexpansions() local ie = instance.expansions for k,v in next, ie do - local t, h = { }, { } - for _,vv in ipairs(split_kpse_path(v)) do + local t, h, p = { }, { }, split_kpse_path(v) + for kk=1,#p do + local vv = p[kk] if vv ~= "" and not h[vv] then t[#t+1] = vv h[vv] = true @@ -9053,11 +9090,15 @@ function resolvers.serialize(files) end t[#t+1] = "return {" if instance.sortdata then - for _, k in pairs(sortedkeys(files)) do -- ipairs + local sortedfiles = sortedkeys(files) + for i=1,#sortedfiles do + local k = sortedfiles[i] local fk = files[k] if type(fk) == 'table' then t[#t+1] = "\t['" .. k .. "']={" - for _, kk in pairs(sortedkeys(fk)) do -- ipairs + local sortedfk = sortedkeys(fk) + for j=1,#sortedfk do + local kk = sortedfk[j] t[#t+1] = dump(kk,fk[kk],"\t\t") end t[#t+1] = "\t}," @@ -9169,7 +9210,9 @@ function resolvers.resetconfig() end function resolvers.loadnewconfig() - for _, cnf in ipairs(instance.luafiles) do + local luafiles = instance.luafiles + for i=1,#luafiles do + local cnf = luafiles[i] local pathname = file.dirname(cnf) local filename = file.join(pathname,resolvers.luaname) local blob = loadfile(filename) @@ -9214,7 +9257,9 @@ end function resolvers.loadoldconfig() if not instance.renewcache then - for _, cnf in ipairs(instance.cnffiles) do + local cnffiles = instance.cnffiles + for i=1,#cnffiles do + local cnf = cnffiles[i] local dname = file.dirname(cnf) resolvers.load_data(dname,'configuration') instance.order[#instance.order+1] = instance.configuration[dname] @@ -10018,13 +10063,14 @@ function resolvers.for_files(command, files, filetype, mustexist) if trace_locating then report('') -- ? end - for _, file in ipairs(files) do + for f=1,#files do + local file = files[f] local result = command(file,filetype,mustexist) if type(result) == 'string' then report(result) else - for _,v in ipairs(result) do - report(v) + for i=1,#result do + report(result[i]) -- could be unpack end end end @@ -10071,7 +10117,7 @@ end function table.sequenced(t,sep) -- temp here local s = { } - for k, v in pairs(t) do -- pairs? + for k, v in next, t do -- indexed? s[#s+1] = k .. "=" .. tostring(v) end return concat(s, sep or " | ") @@ -10103,8 +10149,9 @@ function resolvers.clean_path(str) end function resolvers.do_with_path(name,func) - for _, v in pairs(resolvers.expanded_path_list(name)) do -- pairs? - func("^"..resolvers.clean_path(v)) + local pathlist = resolvers.expanded_path_list(name) + for i=1,#pathlist do + func("^"..resolvers.clean_path(pathlist[i])) end end @@ -10113,7 +10160,9 @@ function resolvers.do_with_var(name,func) end function resolvers.with_files(pattern,handle) - for _, hash in ipairs(instance.hashes) do + local hashes = instance.hashes + for i=1,#hashes do + local hash = hashes[i] local blobpath = hash.tag local blobtype = hash.type if blobpath then @@ -10128,7 +10177,7 @@ function resolvers.with_files(pattern,handle) if type(v) == "string" then handle(blobtype,blobpath,v,k) else - for _,vv in pairs(v) do -- ipairs? + for _,vv in next, v do -- indexed handle(blobtype,blobpath,vv,k) end end @@ -10299,7 +10348,8 @@ function caches.setpath(...) caches.path = '.' end caches.path = resolvers.clean_path(caches.path) - if not table.is_empty({...}) then + local dirs = { ... } + if #dirs > 0 then local pth = dir.mkdirs(caches.path,...) return pth end @@ -10457,7 +10507,8 @@ end local function resolve(str) if type(str) == "table" then - for k, v in pairs(str) do -- ipairs + for k=1,#str do + local v = str[k] str[k] = resolve(v) or v end elseif str and str ~= "" then @@ -10470,7 +10521,7 @@ resolvers.resolve = resolve if os.uname then - for k, v in pairs(os.uname()) do + for k, v in next, os.uname() do if not prefixes[k] then prefixes[k] = function() return v end end @@ -10596,7 +10647,7 @@ end function containers.is_valid(container, name) if name and name ~= "" then local storage = container.storage[name] - return storage and not table.is_empty(storage) and storage.cache_version == container.version + return storage and storage.cache_version == container.version else return false end @@ -10700,12 +10751,13 @@ resolvers.automounted = resolvers.automounted or { } function resolvers.automount(usecache) local mountpaths = resolvers.clean_path_list(resolvers.expansion('TEXMFMOUNT')) - if table.is_empty(mountpaths) and usecache then + if (not mountpaths or #mountpaths == 0) and usecache then mountpaths = { caches.setpath("mount") } end - if not table.is_empty(mountpaths) then + if mountpaths and #mountpaths > 0 then statistics.starttiming(resolvers.instance) - for k, root in pairs(mountpaths) do + for k=1,#mountpaths do + local root = mountpaths[k] local f = io.open(root.."/url.tmi") if f then for line in f:lines() do @@ -11549,6 +11601,10 @@ function states.set_by_tag(tag,key,value,default,persistent) if not dk then dk = { } d[k] = dk + elseif type(dk) == "string" then + -- invalid table, unable to upgrade structure + -- hope for the best or delete the state file + break end d = dk end @@ -12142,7 +12198,8 @@ end function runners.save_script_session(filename, list) local t = { } - for _, key in ipairs(list) do + for i=1,#list do + local key = list[i] t[key] = environment.arguments[key] end io.savedata(filename,table.serialize(t,true)) @@ -12298,10 +12355,13 @@ function runners.execute_ctx_script(filename) environment.ownscript = fullname dofile(fullname) local savename = environment.arguments['save'] - if savename and runners.save_list and not table.is_empty(runners.save_list or { }) then - if type(savename) ~= "string" then savename = file.basename(fullname) end - savename = file.replacesuffix(savename,"cfg") - runners.save_script_session(savename, runners.save_list) + if savename then + local save_list = runners.save_list + if save_list and next(save_list) then + if type(savename) ~= "string" then savename = file.basename(fullname) end + savename = file.replacesuffix(savename,"cfg") + runners.save_script_session(savename,save_list) + end end return true end @@ -12314,7 +12374,8 @@ function runners.execute_ctx_script(filename) local result = dir.glob((string.gsub(context,"mtx%-context","mtx-*"))) -- () needed local valid = { } table.sort(result) - for _, scriptname in ipairs(result) do + for i=1,#result do + local scriptname = result[i] local scriptbase = string.match(scriptname,".*mtx%-([^%-]-)%.lua") if scriptbase then local data = io.loaddata(scriptname) @@ -12329,7 +12390,8 @@ function runners.execute_ctx_script(filename) logs.reportline() logs.simple("no script name given, known scripts:") logs.simple() - for k, v in ipairs(valid) do + for k=1,#valid do + local v = valid[k] logs.simple("%-12s %4s %s",v[1],v[2],v[3]) end end |