summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/scripts/texdoc/functions.tlu
diff options
context:
space:
mode:
Diffstat (limited to 'Master/texmf-dist/scripts/texdoc/functions.tlu')
-rw-r--r--Master/texmf-dist/scripts/texdoc/functions.tlu133
1 files changed, 133 insertions, 0 deletions
diff --git a/Master/texmf-dist/scripts/texdoc/functions.tlu b/Master/texmf-dist/scripts/texdoc/functions.tlu
new file mode 100644
index 00000000000..f1ff54eca0b
--- /dev/null
+++ b/Master/texmf-dist/scripts/texdoc/functions.tlu
@@ -0,0 +1,133 @@
+-- functions.tlu: general-use functions for texdoc
+--
+-- Manuel Pégourié-Gonnard, GPLv3, see texdoclib.tlu for details
+
+-- change '/' to '\' on windows
+-- Use: internal representation of files always use forward slashes.
+-- This function should be called only before displaying a file name.
+if os.type == "windows" then
+ function w32_path (path)
+ return (string.gsub (path, '/', '\\'))
+ end
+else
+ function w32_path (path)
+ return path
+ end
+end
+
+-- remove the last direcory component of a path
+if os.type == 'windows' then
+ function path_parent(path)
+ return string.match(path, '^(.*)[\\/]')
+ end
+else
+ function path_parent(path)
+ return string.match(path, '^(.*)/')
+ end
+end
+
+-- generic error display function (see the error_priority constant)
+function err_print (lvl, msg)
+ -- be careful: maybe config.verbosity_level is not set yet
+ local verbosity_level = config.verbosity_level or tonumber(C.def_verbosity)
+ if C.err_priority[lvl] <= verbosity_level then
+ io.stderr:write ("texdoc "..lvl..": "..msg.."\n")
+ end
+end
+
+do --scope of active_debugs
+local active_debugs
+
+-- generic debug function
+function deb_print(cat, msg)
+ -- make sure active_debugs is set
+ if not active_debugs then set_active_debugs() end
+ -- print message it belongs to an active category
+ if active_debugs and active_debugs[cat] or cat == 'XXX' then
+ io.stderr:write ("texdoc debug-"..cat..": "..msg.."\n")
+ end
+end
+
+-- set active_debugs
+function set_active_debugs()
+ if not config.debug_list then return end
+ active_debugs = {}
+ -- all debug options imply version info
+ if config.debug_list[1] then
+ active_debugs.version = true
+ else
+ return
+ end
+ -- if 'all' is the first keyword, just activate all categories
+ if config.debug_list[1] == 'all' then
+ for deb in pairs(C.known_debugs) do
+ active_debugs[deb] = true end
+ return
+ end
+ -- activate options from the list
+ for _, deb in ipairs(config.debug_list) do
+ local deps = C.known_debugs[deb]
+ if deps then
+ active_debugs[deb] = true
+ for _, d in ipairs(deps) do active_debugs[d] = true end
+ else
+ err_print('warning', "Unknown debug category '"..deb.."'.")
+ end
+ end
+end
+
+end -- scope of active_debugs
+
+-- if file is base..'.'..zip with zip in zipext_list, return: base, zip
+-- otherwise, return: file, nil
+function parse_zip(file)
+ local zip
+ for _, zip in ipairs(config.zipext_list) do
+ local l = #zip + 1
+ if string.sub(file, -l, -1) == '.'..zip then
+ return string.sub(file, 1, -l - 1), zip
+ end
+ end
+ return file, nil
+end
+
+-- print a usage message, with the proper 'active values' line
+function print_usage(actions)
+ local usage_msg = C.usage_msg
+ -- get current settings
+ local default, files = {}, {}
+ for _, param in ipairs(C.usage_settings) do
+ local display = param[config[param.name]]
+ if config[param.name..'_src'] == 'def' then
+ table.insert(default, display)
+ elseif config[param.name..'_src'] == 'file' then
+ table.insert(files, display)
+ end
+ end
+ -- format and include them
+ def = table.concat(default, ', ')
+ conf = table.concat(files, ', ')
+ if not (def == '') then def = def..' (default)' end
+ if not (conf == '') then conf = conf..' (user config)' end
+ local sep = (def == '' or conf == '') and '' or '; '
+ local settings = C.usage_settings_ph..' '..def..sep..conf..'.'
+ usage_msg = usage_msg:gsub(C.usage_settings_ph, settings)
+ -- include actions if any
+ if actions then
+ usage_msg = usage_msg:gsub(C.actions_ph,
+ C.actions_ph .. actions .. '\n\n')
+ else
+ usage_msg = usage_msg:gsub(C.actions_ph, '')
+ end
+ -- finally print it
+ print(usage_msg)
+end
+
+return {
+ err_print = err_print,
+ deb_print = deb_print,
+ w32_path = w32_path,
+ parse_zip = parse_zip,
+ print_usage = print_usage,
+ path_parent = path_parent,
+}