Skip to content

Instantly share code, notes, and snippets.

@zr-tex8r
Last active June 5, 2017 11:10
Show Gist options
  • Save zr-tex8r/47459c9d3a80804f57c412683bed5cd0 to your computer and use it in GitHub Desktop.
Save zr-tex8r/47459c9d3a80804f57c412683bed5cd0 to your computer and use it in GitHub Desktop.
Show the list of available packages
--#!/usr/bin/env texlua
-- zrshowpkg.lua
prog_name = "zrshowpkg"
version = "0.2a"
mod_date = "2016/05/17"
verbose = false
show_path = false
do_validate = true
---------------------------------------- preparations
kpse_progname = nil
unpack = unpack or table.unpack
texlua = pcall(function()
lfs = require "lfs"
kpse = require "kpse"
end)
---------------------------------------- logging
do
local concat = table.concat
function info(...)
if not verbose then return end
local t = { prog_name, ... }
io.stderr:write(concat(t, ": ").."\n")
end
function alert(...)
local t = { prog_name, "warning", ... }
io.stderr:write(concat(t, ": ").."\n")
end
function abort(...)
verbose = true; info(...)
os.exit(-1)
end
function sure(val, a1, ...)
if val then return val end
abort((type(a1) == "number") and ("error("..a1..")") or a1, ...)
end
end
---------------------------------------- retrieve information
do
local fnnorm = function(x) return x end
local pdelim = ':'
if os.type == "windows" then
fnnorm, pdelim = string.lower, ';'
end
local function valid_path(file, dir)
if do_validate then
local kpath = kpse.find_file(file, "tex", true)
return (fnnorm(kpath) == fnnorm(dir.."/"..file)) and kpath
else return dir.."/"..file
end
end
function retrieve_info()
info("inquiring for search path")
local path = sure(kpse.expand_path("$TEXINPUTS"), "search path is empty")
path = path:explode(pdelim)
info("total found "..#path.." directories")
local pinfo, tc = {}, 0
for _, dir in ipairs(path) do
if lfs.isdir(dir) then
local dc = 0
for file in lfs.dir(dir) do
if not pinfo[file] and fnnorm(file):sub(-4) == ".sty" then
local fpath = valid_path(file, dir)
if fpath then
pinfo[file] = fpath; dc = dc + 1
end
end
end
info("found "..dc.." files in", dir)
tc = tc + dc
else alert("not a directory", dir)
end
end
info("total found "..tc.." files")
return pinfo
end
end
---------------------------------------- show information
do
function show_info(pinfo)
local lines = {}
for file, path in pairs(pinfo) do
local name = file:sub(1, -5)
local line = (show_path) and name.. " ("..path..")" or name
table.insert(lines, line)
end
table.sort(lines)
for _, line in ipairs(lines) do
print(line)
end
end
end
---------------------------------------- main
do
local function show_usage()
io.stdout:write(([[
This is %s v%s <%s> by 'ZR'
Usage: %s[.lua] [<option>...] [<prog_name>]
Options:
-p also show path
-q skip validation
-v be verbose
]]):format(prog_name, version, mod_date, prog_name))
os.exit(0)
end
function read_option()
local idx = 1
while idx <= #arg do
local opt = arg[idx]
if opt:sub(1, 1) ~= "-" then break end
if opt == "-h" or opt == "--help" then
show_usage()
elseif opt == "-v" then
verbose = true
elseif opt == "-p" then
show_path = true
elseif opt == "-q" then
do_validate = false
else
abort("invalid option", opt)
end
idx = idx + 1
end
if arg[idx] then
kpse_progname = arg[idx]; idx = idx + 1
end
sure(not arg[idx], "wrong number of arguments")
end
function main()
sure(texlua, "this script requrires TeXlua")
read_option()
kpse.set_program_name("luatex", kpse_progname)
show_info(retrieve_info())
end
end
---------------------------------------- go
main()
-- EOF
@zr-tex8r
Copy link
Author

Note:
This script must be run with TeXlua (that is, LuaTeX as Lua interpreter). You cannot run it with ordinary Lua binaries.

Usage example:

texlua zrshowpkg.lua latex

Prints the names of all the packages that can be loaded in latex execution.

texlua zrshowpkg.lua -p lualatex

Prints the names of all the packages that can be loaded in lualatex execution, with their locations.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment