diff options
author | Manuel Pégourié-Gonnard <mpg@elzevir.fr> | 2011-06-06 04:24:43 +0000 |
---|---|---|
committer | Manuel Pégourié-Gonnard <mpg@elzevir.fr> | 2011-06-06 04:24:43 +0000 |
commit | ccd2c7338d8bcbacffe531d9c43493f7f8035517 (patch) | |
tree | 2b28f03c11c0f7c7fb5f8ac70de88f992199b019 /Master/texmf/scripts/texdoc | |
parent | e1ed0075e33f4d74cc66bf8e13f2e2ccd3d8fdbf (diff) |
texdoc 0.81
git-svn-id: svn://tug.org/texlive/trunk@22812 c570f23f-e606-0410-a88d-b1316a301751
Diffstat (limited to 'Master/texmf/scripts/texdoc')
-rw-r--r-- | Master/texmf/scripts/texdoc/alias.tlu | 31 | ||||
-rw-r--r-- | Master/texmf/scripts/texdoc/config.tlu | 218 | ||||
-rw-r--r-- | Master/texmf/scripts/texdoc/constants.tlu | 70 | ||||
-rw-r--r-- | Master/texmf/scripts/texdoc/functions.tlu | 48 | ||||
-rw-r--r-- | Master/texmf/scripts/texdoc/main.tlu | 93 | ||||
-rw-r--r-- | Master/texmf/scripts/texdoc/score.tlu | 40 | ||||
-rw-r--r-- | Master/texmf/scripts/texdoc/search.tlu | 47 | ||||
-rwxr-xr-x | Master/texmf/scripts/texdoc/texdoc.tlu | 95 | ||||
-rw-r--r-- | Master/texmf/scripts/texdoc/texdoclib.tlu | 90 | ||||
-rw-r--r-- | Master/texmf/scripts/texdoc/view.tlu | 32 |
10 files changed, 340 insertions, 424 deletions
diff --git a/Master/texmf/scripts/texdoc/alias.tlu b/Master/texmf/scripts/texdoc/alias.tlu index a99a8b51058..1ae150b5b1c 100644 --- a/Master/texmf/scripts/texdoc/alias.tlu +++ b/Master/texmf/scripts/texdoc/alias.tlu @@ -1,15 +1,6 @@ --- configuration handling for texdoc ---[[ -Copyright 2008, 2009 Manuel Pégourié-Gonnard -Distributed under the terms of the GNU GPL version 3 or later. -See texdoc.tlu for details. ---]] - --- Load a private environment for this submodule (see texdoc.tlu). -local L = {} -load_env(L, { - 'config', -}) +-- alias.tlu: configuration handling for texdoc +-- +-- Manuel Pégourié-Gonnard, GPLv3, see texdoclib.tlu for details --[[ structure of the alias table @@ -53,7 +44,7 @@ function add_alias(key, value, score) table.insert(alias[k], make_alias(value, score)) end --- prevent a key from begin further aliased +-- prevent a key from being further aliased function stop_alias(key) local k = string.lower(key) alias[k] = alias[k] or {} @@ -65,8 +56,7 @@ function get_patterns(name, no_alias) local n = string.lower(name) -- get normal aliases local res - if config.mode ~= 'regex' and config.alias_switch - and not no_alias and alias[n] then + if alias[n] and not no_alias then res = alias[n] else res = { make_alias(name, false) } @@ -119,9 +109,8 @@ function aliased_names() end end --- finally export a few symbols -export_symbols(L, { - 'confline_to_alias', - 'get_patterns', - 'aliased_names', -}) +return { + confline_to_alias = confline_to_alias, + get_patterns = get_patterns, + aliased_names = aliased_names, +} diff --git a/Master/texmf/scripts/texdoc/config.tlu b/Master/texmf/scripts/texdoc/config.tlu index 1e6aa7110b3..7681425e9bb 100644 --- a/Master/texmf/scripts/texdoc/config.tlu +++ b/Master/texmf/scripts/texdoc/config.tlu @@ -1,17 +1,6 @@ --- configuration handling for texdoc ---[[ -Copyright 2008, 2009 Manuel Pégourié-Gonnard -Distributed under the terms of the GNU GPL version 3 or later. -See texdoc.tlu for details. ---]] - --- Load a private environment for this submodule (see texdoc.tlu). -local L = {} -load_env(L, { - 'os', 'kpse', 'lfs', 'io', 'setmetatable', 'print', - 'w32_path', 'confline_to_alias', 'confline_to_score', 'print_usage', - 'config', -}) +-- config.tlu: configuration handling for texdoc +-- +-- Manuel Pégourié-Gonnard, GPLv3, see texdoclib.tlu for details -------------------------- hide the config table --------------------------- @@ -44,7 +33,13 @@ function set_config_element (key, value, context) -- warn and exit if key is not a known option if not is_known then config_warn(key, nil, context, true) return end -- exit if key is already set (/!\ must test for nil, not false) - if not (config[key] == nil) then return nil end + if not (config[key] == nil) then + if context.src ~= 'def' then + deb_print('config', "Ignoring '"..key.."="..value.."' " + ..context_to_string(context)..'.') + end + return nil + end -- record the source of the setting real_set_config(key..'_src', context.src) -- detect the type of the key @@ -86,17 +81,6 @@ function set_config_element (key, value, context) else -- string real_set_config(key, value) end - -- alias_switch is depecated - if key == 'alias_switch' and context.src ~= 'def' then - err_print('warning', - 'Command-line options -a, --alias, -A, --noalias, as well as') - err_print('warning', - 'configuration item alias_switch, are deprecated') - err_print('warning', - 'and will soon be removed.') - err_print('warning', - 'Please protest on the texdoc mailing list if you need it.') - end -- special case: if we just set debug_list, print version info now if key == 'debug_list' then deb_print('version', C.fullname..' version '..C.version) @@ -152,22 +136,8 @@ function setup_config_from_cl(arg) end while arg[1] and string.match(arg[1],'^%-') do curr_arg = table.remove(arg,1) - -- special options - if (curr_arg == '-h') or (curr_arg == '--help') then - setup_config_from_files() - setup_config_from_defaults() - print_usage() - os.exit(C.exit_ok) - elseif (curr_arg == '-V') or (curr_arg == '--version') then - print (C.progname .. ' ' .. C.version ) - os.exit(C.exit_ok) - elseif (curr_arg == '-f') or (curr_arg == '--files') then - print (C.progname .. ' ' .. C.version ) - setup_config_from_files () - show_config_files (print, true) - os.exit(C.exit_ok) - -- options related to mode - elseif (curr_arg == '-w') or (curr_arg == '--view') then + -- mode + if (curr_arg == '-w') or (curr_arg == '--view') then set_config_elt('mode', 'view') elseif (curr_arg == '-m') or (curr_arg == '--mixed') then set_config_elt('mode', 'mixed') @@ -175,12 +145,6 @@ function setup_config_from_cl(arg) set_config_elt('mode', 'list') elseif (curr_arg == '-s') or (curr_arg == '--showall') then set_config_elt ('mode', 'showall') - elseif (curr_arg == '--search') then - set_config_elt ('mode', 'showall') - err_print('warning', '--search has been renamed to --showall') - err_print('warning', 'Please use the new name.') - elseif (curr_arg == '-r') or (curr_arg == '--regex') then - set_config_elt ('mode', 'regex') -- interaction elseif (curr_arg == '-I') or (curr_arg == '--nointeract') then set_config_elt('interact_switch', 'false') @@ -189,11 +153,6 @@ function setup_config_from_cl(arg) -- output format elseif (curr_arg == '-M') or (curr_arg == '--machine') then set_config_elt('machine_switch', 'true') - -- alias - elseif (curr_arg == '-A') or (curr_arg == '--noalias') then - set_config_elt('alias_switch', 'false') - elseif (curr_arg == '-a') or (curr_arg == '--alias') then - set_config_elt('alias_switch', 'true') -- debug elseif (curr_arg == '-d') or (curr_arg == '--debug') then set_config_elt('debug_list', 'all') @@ -203,60 +162,19 @@ function setup_config_from_cl(arg) elseif string.match(curr_arg, '^%-%-debug=') then local value = string.gsub(curr_arg, '^%-%-debug=', '') set_config_elt('debug_list', value) - -- quiet + -- verbosity elseif (curr_arg == '-q') or (curr_arg == '--quiet') then set_config_elt('verbosity_level', C.min_verbosity) - -- verbose - elseif string.match(curr_arg, '^%-%-verbose') then + elseif (curr_arg == '-v') or (curr_arg == '--verbose') then set_config_elt('verbosity_level', C.max_verbosity) - elseif string.match(curr_arg, '^%-v') then - local value = string.gsub(curr_arg, '^%-v=?', '') - if value == '' then - set_config_elt('verbosity_level', C.max_verbosity) - else -- compatibility syntax - err_print('warning', - 'The -v option does not accept arguments any more.') - err_print('warning', - 'Please use the new syntax -v, --verbose (without argument).') - set_config_elt('verbosity_level', value) - end - elseif string.match(curr_arg, '^%-%-verbosity') then - -- compatibility syntax - local value = string.gsub(curr_arg, '^%-%-verbosity=?', '') - err_print('warning', - '--verbosity is deprecated and will be removed soon.') - err_print('warning', - 'Please use the new syntax -v, --verbose (without argument).') - set_config_elt('verbosity_level', value) - -- extensions list (deprecated) - elseif string.match(curr_arg, '^%-e') then - local value = string.gsub(curr_arg, '^%-e=?', '') - set_config_elt('ext_list', value) - err_print('warning', - '-e is deprecated and will likely be removed soon.') - err_print('warning', - 'ext_list should be set in a configuration file instead.') - err_print('warning', - 'Please protest on the texdoc mailing list if you need this.') - elseif string.match(curr_arg, '^%-%-extensions') then - local value = string.gsub(curr_arg, '^%-%-extensions=?', '') - set_config_elt('ext_list', value) - err_print('warning', - '--extensions is deprecated and will likely be removed soon.') - err_print('warning', - 'ext_list should be set in a configuration file instead.') - err_print('warning', - 'Please protest on the texdoc mailing list if you need this.') - -- just view (undocumented option fur use with texdoctk) - elseif curr_arg == '--just-view' then - real_set_config('just_view', true) -- problem else err_print('error', "unknown option: "..curr_arg) err_print('error', C.error_msg) - os.exit(C.exit_usage) + return false end end + return true end ------------------------- config from environment -------------------------- @@ -320,20 +238,30 @@ function confline_to_config(line, file, pos) return false end --- return the list of configuration files and the index of the recommended file +-- return the list of configuration files function get_config_files () + -- get names local platform = string.match (kpse.var_value ('SELFAUTOLOC'), '.*/(.*)$') - local TEXMFHOME = kpse.var_value ('TEXMFHOME') - local TEXMFLOCAL = kpse.var_value ('TEXMFLOCAL') - local TEXMFMAIN = kpse.var_value ('TEXMFMAIN') - return { - TEXMFHOME .. '/texdoc/texdoc-'..platform..'.cnf', - TEXMFHOME .. '/texdoc/texdoc.cnf', - TEXMFHOME .. '/texdoc/texdoc-dist.cnf', - TEXMFLOCAL .. '/texdoc/texdoc-'..platform..'.cnf', - TEXMFLOCAL .. '/texdoc/texdoc.cnf', - TEXMFMAIN .. '/texdoc/texdoc.cnf', - }, 2 + local names = { + 'texdoc-'..platform..'.cnf', + 'texdoc.cnf', + 'texdoc-dist.cnf', + } + -- get dirs + local sep = (os.type == 'windows') and ';' or ':' + local texmf_texdoc = kpse.expand_path('$TEXMF/texdoc') + local dirs = texmf_texdoc:explode(sep) + -- merge them + local ret = {} + for _, dir in ipairs(dirs) do + for _, name in ipairs(names) do + local pathname = dir .. '/' .. name + if lfs.isfile(pathname) then + table.insert(ret, pathname) + end + end + end + return ret end -- the config_files table is shared by the next two functions @@ -342,15 +270,12 @@ local config_files = {} -- set config/aliases from all config files function setup_config_from_files () - local file_list, good_index = get_config_files() + local file_list = get_config_files() for i, file in ipairs (file_list) do - local found = lfs.isfile(file) - local status = found and (config.lastfile_switch - and 'disabled' or 'active') or 'absent' + local status = config.lastfile_switch and 'disabled' or 'active' config_files[i] = { path = file, status = status, - good = (i == good_index), } if status == 'active' then read_config_file (file) @@ -359,17 +284,16 @@ function setup_config_from_files () end -- now a special information function (see -f,--file option) -function show_config_files (print_fun, prefix) +function show_config_files (print_fun, verbose) + local pref = verbose and ' ' or '' print_fun("Configuration files are:") for i, file in ipairs (config_files) do - local home = prefix - and (file.good and "(*) " or " ") - or '' - print_fun (home..file.status..'\t'..w32_path(file.path)) + print_fun(pref..file.status..'\t'..w32_path(file.path)) end - if prefix then - print("(*) This is the recommended configuration file " - .. "for your personal preferences.") + if verbose then + local texmfhome = kpse.var_value('TEXMFHOME') + print_fun('Recommended file for personal settings:') + print_fun(pref..texmfhome..'/texdoc/texdoc.cnf') end end @@ -444,12 +368,9 @@ function desktop_environment_viewer() end end --- set some fall-back default values if no previous value is set -function setup_config_from_defaults() +-- set viewers from defaults (done only if necessary) +function get_default_viewers() local function set_config_ls(ls) set_config_list(ls, {src='def'}) end - local function set_config_elt(key, val) - set_config_element(key, val, {src='def'}) - end if (os.type == "windows") then set_config_ls { -- Use 'start' to get file associations. @@ -525,7 +446,15 @@ function setup_config_from_defaults() } } end - -- then various, platform independant, stuff +end + +-- set some fall-back default values if no previous value is set +function setup_config_from_defaults() + local function set_config_ls(ls) set_config_list(ls, {src='def'}) end + local function set_config_elt(key, val) + set_config_element(key, val, {src='def'}) + end + -- various, platform independant, stuff set_config_ls { mode = 'view', interact_switch = 'true', @@ -539,8 +468,6 @@ function setup_config_from_defaults() debug_list = '', max_lines = '20', } - -- must be set after mode! - set_config_elt ('alias_switch', alias_from_mode(config.mode)) -- zip-related options set_config_ls { zipext_list = '', @@ -549,32 +476,16 @@ function setup_config_from_defaults() } end --- the default value of config.alias_switch depends on the mode as follows -function alias_from_mode (mode) -- /!\ returns a string! - if (mode == 'regex') then - return 'false' - else - return 'true' - end -end - -------------------------- set all configuration --------------------------- -- populate the config and alias arrays function setup_config_and_alias(arg) -- setup config from all sources - setup_config_from_cl(arg) + local ret = setup_config_from_cl(arg) setup_config_from_env() setup_config_from_files() setup_config_from_locale() setup_config_from_defaults() - -- regex mode is deprecated - if config.mode == 'regex' then - err_print('warning', - 'Regex mode is deprecated and will likely be removed soon.') - err_print('warning', - 'Please protest on the texdoc mailing list if you need this.') - end -- machine mode implies no interaction if config.machine_switch == true then real_set_config('interact_switch', false) @@ -585,9 +496,12 @@ function setup_config_and_alias(arg) end -- we were waiting for config.debug_list to be known to do this show_config_files(function(s) deb_print('files', s) end) + -- propagated return value of _from_cl() + return ret end --- finally export a few symbols -export_symbols(L, { - 'setup_config_and_alias', -}) +return { + setup_config_and_alias = setup_config_and_alias, + get_default_viewers = get_default_viewers, + show_config_files = show_config_files, +} diff --git a/Master/texmf/scripts/texdoc/constants.tlu b/Master/texmf/scripts/texdoc/constants.tlu index a8c107034cc..0a24aded4bd 100644 --- a/Master/texmf/scripts/texdoc/constants.tlu +++ b/Master/texmf/scripts/texdoc/constants.tlu @@ -1,36 +1,34 @@ --- Global "constants" for texdoc. ---[[ -Copyright 2008, 2009 Manuel Pégourié-Gonnard -Distributed under the terms of the GNU GPL version 3 or later. -See texdoc.tlu for details. ---]] - --- Load a private environment. --- A name 'foo' defined here will be globally visible as 'C.foo', see EOF. -local L = {} -load_env(L, { - 'setmetatable', 'arg', -}) +-- constants.tlu: global "constants" for texdoc. +-- +-- Manuel Pégourié-Gonnard, GPLv3, see texdoclib.tlu for details + +-- use an empty environment that will become texdoc_env.C (see EOF) +local constants = {} +local kpse = kpse +local setfenv = setfenv +local texdoc_env = getfenv() +setfenv(1, constants) + +-- BEGIN constants -- progname and version -fullname = arg[0] +fullname = kpse.find_file('texdoc/texdoclib', 'lua') progname = 'texdoc' -version = '0.80' +version = '0.81' -- make sure to update setup_config_from_cl() accordingly -- and set a default value in setup_config_from_defaults() if relevant usage_msg = [[ -Usage: texdoc [OPTION]... [NAME]... +Usage: texdoc [OPTION]... NAME... + or: texdoc ACTION Try to find appropriate TeX documentation for the specified NAME(s). -With no NAME, print information about texdoc (--help, --version, --files). +Alternatively, perform the given ACTION and exit. Current settings: - -h, --help Print this help message. - -V, --version Print the version number. - -f, --files Print the list of configuration files used. - +Actions: +Options: -w, --view Use view mode: start a viewer. (default) -m, --mixed Use mixed mode (view or list). -l, --list Use list mode: show a list of results. @@ -42,12 +40,19 @@ Current settings: -q, --quiet Suppress warnings and most error messages. -v, --verbose Print additional information (eg, viewer command). - -d, --debug[=list] Print debug info for selected items (default: all). + -d, --debug[=list] Activate debug output (restricted to list). Environment: PAGER, BROWSER, PDFVIEWER, PSVIEWER, DVIVIEWER. Files: <texmf>/texdoc/texdoc.cnf, see output of the --files option. -Homepage: http://tug.org/texdoc/ -Manual: displayed by `texdoc texdoc'.]] +Report bugs to <texdoc@tug.org>. +Full manual available via `texdoc texdoc'.]] + +copyright_msg = [[ +Copyright (C) 2008-2011 Manuel P'egouri'e-Gonnard. +License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>. +This is free software: you are free to change and redistribute it.]] + +actions_ph = 'Actions:\n' usage_settings_ph = 'Current settings:' -- keep ../doc/texdoc wrapper in sync! usage_settings = { @@ -62,7 +67,6 @@ known_options = { 'mode', 'interact_switch', 'machine_switch', - 'alias_switch', 'ext_list', 'basename_list', 'badext_list', @@ -96,10 +100,6 @@ min_verbosity='0' max_verbosity='3' def_verbosity='2' -exit_ok = 0 -exit_error = 1 -- apparently hard-coded in Lua -exit_usage = 2 - known_debugs = { version = {}, files = {}, @@ -112,18 +112,22 @@ known_debugs = { -- various cache or non-cache files cache_name = 'texdoc/cache-tlpdb.lua' -- relative to TEXMFVAR -data_meta_name = 'Data.meta.lua' -data_tlpdb_name = 'Data.tlpdb.lua' +data_tlpdb_name = 'texdoc/Data.tlpdb.lua' place_holder = '%%s' -- used for viewer commands --- Make global C a read-only proxy to the local environment. +-- END constants + +-- get our previous environment back +setfenv(1, texdoc_env) + +-- Make global C a read-only proxy to the local <constants>. -- Note this is not deep read-only: C.known_debugs is read-only, but -- C.known_debugs.version isn't, for instance. assert(next(C) == nil, 'Internal error: table of constants should be empty at this point') setmetatable(C, { - __index = L, + __index = constants, __newindew = function () error('Internal error: attempt to modify a constant.') end diff --git a/Master/texmf/scripts/texdoc/functions.tlu b/Master/texmf/scripts/texdoc/functions.tlu index 4167b4563a9..f1ff54eca0b 100644 --- a/Master/texmf/scripts/texdoc/functions.tlu +++ b/Master/texmf/scripts/texdoc/functions.tlu @@ -1,16 +1,6 @@ --- General use functions for texdoc ---[[ -Copyright 2008, 2009 Manuel Pégourié-Gonnard -Distributed under the terms of the GNU GPL version 3 or later. -See texdoc.tlu for details. ---]] - --- Load a private environment for this submodule (see texdoc.tlu). -local L = {} -load_env(L, { - 'io', 'os', 'print', - 'config', -}) +-- 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. @@ -102,7 +92,9 @@ function parse_zip(file) end -- print a usage message, with the proper 'active values' line -function print_usage() +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]] @@ -112,22 +104,30 @@ function print_usage() 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..'.' - local usage_msg = string.gsub(C.usage_msg, C.usage_settings_ph, settings) + 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 --- finally export a few symbols -export_symbols(L, { - 'err_print', - 'deb_print', - 'w32_path', - 'parse_zip', - 'print_usage', - 'path_parent', -}) +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, +} diff --git a/Master/texmf/scripts/texdoc/main.tlu b/Master/texmf/scripts/texdoc/main.tlu index 9fc20ae1182..8ab4648e770 100644 --- a/Master/texmf/scripts/texdoc/main.tlu +++ b/Master/texmf/scripts/texdoc/main.tlu @@ -1,34 +1,68 @@ --- texdoc's main() ---[[ -Copyright 2008, 2009 Manuel Pégourié-Gonnard -Distributed under the terms of the GNU GPL version 3 or later. -See texdoc.tlu for details. ---]] - --- Load a private environment for this submodule (see texdoc.tlu). -local L = {} -load_env(L, { - 'os', 'arg', 'print', - 'setup_config_and_alias', 'init_databases', 'print_usage', - 'get_doclist', 'deliver_results', 'aliased_names', - 'config', 'view_file', -}) - --- get started -setup_config_and_alias(arg) -init_databases() +-- main.tlu: default command-line interface of texdoc +-- +-- Manuel Pégourié-Gonnard, GPLv3, see texdoclib.tlu for details + +-- load texdoclib (kpse initialized by the wrapper) +local texdoc = require('texdoc.texdoclib') + +-- exit codes +local exit_ok = 0 +local exit_error = 1 -- apparently hard-coded in Lua +local exit_usage = 2 + +-- action command-line options, not treated by setup_config_and_alias +local action_help = [[ + -h, --help Print this help message. + -V, --version Print the version number. + -f, --files Print the list of configuration files used. + --just-view file Display file, given with full path (no searching).]] +local action_opts = { + ['-h'] = 'help', + ['--help'] = 'help', + ['-V'] = 'version', + ['--version'] = 'version', + ['-f'] = 'files', + ['--files'] = 'files', + ['--just-view'] = 'view', +} + +-- detect action options, but do not act now (some need setup_config) +local action +if arg[1] then + action = action_opts[arg[1]] + if action then table.remove(arg, 1) end +end + +-- get configuration +if not texdoc.setup_config_and_alias(arg) then + os.exit(exit_usage) +end + +-- handle action options +if action == 'help' then + texdoc.print_usage(action_help) + os.exit(exit_ok) +elseif action == 'version' then + print(texdoc.const.progname .. ' ' .. texdoc.const.version) + print('\n' .. texdoc.const.copyright_msg) + os.exit(exit_ok) +elseif action =='files' then + print(texdoc.const.fullname .. ' ' .. texdoc.const.version) + texdoc.show_config_files(print, true) + os.exit(exit_ok) +elseif action == 'view' then + local ok = texdoc.view_file(arg[1]) + os.exit(ok and exit_ok or exit_error) +end -- make sure we actually have argument(s) if not arg[1] then - print_usage() - os.exit(C.exit_usage) + texdoc.print_usage() + os.exit(exit_usage) end --- special case for just view -if config.just_view then - local ok = view_file(arg[1]) - os.exit(ok and C.exit_ok or C.exit_error) -end +-- initialise databases +texdoc.init_databases() -- main loop local docname @@ -36,7 +70,10 @@ for _, docname in ipairs(arg) do -- do we have more then one argument? local multiarg = not not arg[2] -- get results - local doclist = get_doclist(docname) + local doclist = texdoc.get_doclist(docname) -- deliver results to the user - deliver_results(docname, doclist, multiarg) + texdoc.deliver_results(docname, doclist, multiarg) end + +-- the end +os.exit(exit_ok) diff --git a/Master/texmf/scripts/texdoc/score.tlu b/Master/texmf/scripts/texdoc/score.tlu index 75022e41288..697f0cf1b2a 100644 --- a/Master/texmf/scripts/texdoc/score.tlu +++ b/Master/texmf/scripts/texdoc/score.tlu @@ -1,15 +1,6 @@ --- scoring functions for texdoc ---[[ -Copyright 2008, 2009 Manuel Pégourié-Gonnard -Distributed under the terms of the GNU GPL version 3 or later. -See texdoc.tlu for details. ---]] - --- Load a local environment. See texdoc.tlu for details. -local L = {} -load_env(L, { - 'config', 'parse_zip', -}) +-- score.tlu: scoring functions for texdoc +-- +-- Manuel Pégourié-Gonnard, GPLv3, see texdoclib.tlu for details -- shared variables local global_adjscore, spec_adjscore = {}, {} @@ -57,8 +48,6 @@ end -- set the score of a docfile function set_score(df, original_kw) - -- scoring is irrelevant in regex mode - if config.mode == 'regex' then df.score = 0 return end -- scoring is case-insenstitive (patterns are already lowercased) local name = string.lower(df.shortname) deb_print('score', '----------') @@ -96,10 +85,11 @@ function set_score(df, original_kw) if df.details then if string.find(string.lower(df.details), 'readme') then score = score + 0.1 + deb_print('score', 'Catalogue "readme" bonus: +0.1') else score = score + 2.5 + deb_print('score', 'Catalogue details bonus: +2.5') end - deb_print('score', string.format('New score: %g from metadata', score)) end -- adjust from keyword-specific tables if df.tree > -1 and spec_adjscore[original_kw] then @@ -157,14 +147,14 @@ function heuristic_score(file, pat) end end end - -- if extension is bad, score gets < 0 + -- if extension is bad, score becomes an epsilon local ext = config.ext_list[ext_pos(file)] if ext and config.badext_list_inv[ext] and score > 0 then - upscore(-1, 'bad extension', true) + upscore(0.1, 'bad extension', true) end -- if basename is bad, score gets < 0 if has_bad_basename(file) and score > 0 then - upscore(-1, 'bad basename', true) + upscore(0.1, 'bad basename', true) end -- bonus for being in the right directory if string.find('/'..file, '/'..pat..'/', 1, true) and not slash then @@ -287,10 +277,10 @@ function sort_doclist(dl, original_kw) table.sort(dl, docfile_order) end --- export a few symbols -export_symbols(L, { - 'sort_doclist', - 'docfile_quality', - 'ext_pos', - 'confline_to_score', -}) +return { + sort_doclist = sort_doclist, + docfile_quality = docfile_quality, + ext_pos = ext_pos, + is_exact = is_exact, + confline_to_score = confline_to_score, +} diff --git a/Master/texmf/scripts/texdoc/search.tlu b/Master/texmf/scripts/texdoc/search.tlu index 11203fd97c5..1499c65414a 100644 --- a/Master/texmf/scripts/texdoc/search.tlu +++ b/Master/texmf/scripts/texdoc/search.tlu @@ -1,19 +1,6 @@ --- File searching functions for texdoc. ---[[ -Copyright 2008, 2009 Manuel Pégourié-Gonnard -Distributed under the terms of the GNU GPL version 3 or later. -See texdoc.tlu for details. ---]] - --- Load a private environment for this submodule (see texdoc.tlu). -local L = {} -load_env(L, { - 'os', 'lfs', 'kpse', 'io', 'setmetatable', 'print', - 'dofile', 'loadfile', 'setfenv', 'rawget', 'rawset', - 'parse_zip', 'path_parent', - 'get_patterns', 'sort_doclist', 'docfile_quality', 'ext_pos', - 'config', -}) +-- search.tlu: file searching functions for texdoc. +-- +-- Manuel Pégourié-Gonnard, GPLv3, see texdoclib.tlu for details -- Warning: every single function here assumes init_databases() has been called. @@ -362,13 +349,21 @@ function check_ext(file) return false end +-- say if a file (with its path) matches a pattern +function matches(pattern, file) + if pattern.original then + return string.find(file:lower(), pattern.name:lower(), 1, true) + else + return is_exact(file, pattern.name) + end +end + -- return a docfile object if file "matches", nil ortherwise function process_file(patlist, file, pathfile, code) local docfile local pattern for _, pattern in ipairs(patlist) do - if string.find(string.lower(pathfile), string.lower(pattern.name), - 1, config.mode ~= 'regex') then + if matches(pattern, pathfile) then local info = { name = pathfile, tree = code, @@ -568,12 +563,6 @@ function get_tlpinfo_from_cache(filename) s_meta, tlp_from_runfile, tlp_doclist = dofile(filename) end --- get pre-hashed meta from a file -function get_meta_hack() - local f = assert(kpse.find_file(C.data_meta_name, 'texmfscripts')) - s_meta = dofile(f) -end - -- get pre-hashed tlpdb info from a pseudo-cache file function get_tlpinfo_from_dist() local f = assert(kpse.find_file(C.data_tlpdb_name, 'texmfscripts')) @@ -633,7 +622,6 @@ end -- separate sty patterns from the rest function normal_vs_sty(list) - if config.mode == 'regex' then return list, {} end local normal, sty = {}, {} for _, p in ipairs(list) do if string.match(string.lower(p.name), '%.([^/.]*)$') == 'sty' then @@ -645,8 +633,7 @@ function normal_vs_sty(list) return normal, sty end --- finally export a few symbols -export_symbols(L, { - 'init_databases', - 'get_doclist', -}) +return { + init_databases = init_databases, + get_doclist = get_doclist, +} diff --git a/Master/texmf/scripts/texdoc/texdoc.tlu b/Master/texmf/scripts/texdoc/texdoc.tlu index 875450ff8ff..919e381ec33 100755 --- a/Master/texmf/scripts/texdoc/texdoc.tlu +++ b/Master/texmf/scripts/texdoc/texdoc.tlu @@ -1,96 +1,7 @@ #!/usr/bin/env texlua ---[[ -Copyright 2008, 2009 Manuel Pégourié-Gonnard. -This program is free software: you can redistribute it and/or modify it under -the terms of the GNU General Public License as published by the Free Software -Foundation, either version 3 of the License, or (at your option) any later -version. +-- texdoc.tlu: small wrapper around main.tlu +-- (makes it easier to install a new version of texdoc in TEXMFHOME) -This program is distributed in the hope that it will be useful, but WITHOUT ANY -WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A -PARTICULAR PURPOSE. See the GNU General Public License for more details. - -You should have received a copy of the GNU General Public License along with -this program. If not, see <http://www.gnu.org/licenses/>. - -Previous work in the public domain: -- Contributions from Reinhard Kotucha (2008). -- First texlua versions by Frank Küster (2007). -- Original shell script by Thomas Esser, David Aspinall, and Simon Wilkinson. ---]] - ----------------------- functions handling submodules ----------------------- - --- texdoc is divided in submodules (files). --- Most submodules use a private environment: that is, the functions and --- variables defined in a file won't be globally visible, unless explicitly --- exported at the end of the file. --- Also, symbols from the global environment (hence from other submodules) --- aren't imported in the local environment, except if explicitly requested and --- a few default symbols. - --- load a local environment, importing symbols from (this function's) _G --- usage: local L = {} load_env(L, {'a', 'b'}) -function load_env(l, symbols) - local default = { - 'export_symbols', - 'string', 'table', 'pairs', 'ipairs', 'tonumber', 'tostring', 'next', - 'assert', 'error', 'err_print', 'deb_print', - 'C', - } - for _, symb in ipairs(default) do - l[symb] = _G[symb] - end - local _, symb - for _, symb in ipairs(symbols) do - assert(_G[symb] ~= nil, - 'Internal error: trying to import undefined symbol '..symb..'.') - l[symb] = _G[symb] - end - setfenv(2, l) -end - --- export symbols from a local environment to (this fonction's) _G -function export_symbols(l, symbols) - local _, symb - for _, symb in ipairs(symbols) do - assert(l[symb] ~= nil, - 'Internal error: trying to export undefined symbol '..symb..'.') - assert(_G[symb] == nil, - 'Internal error: trying to export existing symbol '..symb..'.') - _G[symb] = l[symb] - end -end - --- execute a submodule of texdoc -function texdoc_do(name) - local f = kpse.find_file(name..'.tlu', 'texmfscripts') - assert(f, 'Internal error: unable to find texdoc module '..name..'.') - dofile(f) -end - ------------------------- initilisation & main code ------------------------- - --- initialize kpathsea kpse.set_program_name(arg[-1], 'texdoc') - --- declare global variables; --- they will be made read-only as soon as they are set -C = {} -- constants -config = {} -- configuration settings - --- actually load the submodules now -texdoc_do('constants') -texdoc_do('functions') -texdoc_do('alias') -texdoc_do('score') -texdoc_do('config') -texdoc_do('search') -texdoc_do('view') - --- execute main() -texdoc_do('main') - --- the end -os.exit(C.exit_ok) +require('texdoc.main') diff --git a/Master/texmf/scripts/texdoc/texdoclib.tlu b/Master/texmf/scripts/texdoc/texdoclib.tlu new file mode 100644 index 00000000000..54311ae4de8 --- /dev/null +++ b/Master/texmf/scripts/texdoc/texdoclib.tlu @@ -0,0 +1,90 @@ +-- texdoclib.tlu: the texdoc library + +--[[ +Copyright 2008, 2009, 2010, 2011 Manuel Pégourié-Gonnard. + +This program is free software: you can redistribute it and/or modify it under +the terms of the GNU General Public License as published by the Free Software +Foundation, either version 3 of the License, or (at your option) any later +version. + +This program is distributed in the hope that it will be useful, but WITHOUT ANY +WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A +PARTICULAR PURPOSE. See the GNU General Public License for more details. + +You should have received a copy of the GNU General Public License along with +this program. If not, see <http://www.gnu.org/licenses/>. + +Previous work in the public domain: +- Contributions from Reinhard Kotucha (2008). +- First texlua versions by Frank Küster (2007). +- Original shell script by Thomas Esser, David Aspinall, and Simon Wilkinson. +--]] + +------------------------ functions for submodules ------------------------ + +-- texdoc is divided in submodules (files). Each module is loaded in a private +-- copy of a shared environment, and can export symbols to this environments +-- by returning a table to be merge with this environment. + +-- return a simple (not deep) copy of a table +local function simple_copy(t) + local c = {} + for sym, val in pairs(t) do + c[sym] = val + end + return c +end + +-- initialise the shared environment with a copy of the global environment +local prv_env = simple_copy(_G) +setfenv(1, prv_env) + +-- import symbols from a table to the share environment +local function import_symbols(symbols, name) + for sym, val in pairs(symbols) do + assert(val ~= nil, + 'Internal error: '..name..'exporting undefined symbol '..sym..'.') + assert(prv_env[sym] == nil, + 'Internal error: '..name..'exporting existing symbol '..sym..'.') + prv_env[sym] = val + end +end + +-- load a submodule of texdoc +local function texdoc_do(name) + local pathname = kpse.find_file('texdoc/'..name, 'lua') + assert(pathname, 'Internal error: missing submodule: '..name) + local submod = assert(loadfile (pathname)) + setfenv(submod, simple_copy(prv_env)) + local symbols = submod() + if symbols then import_symbols(symbols, name) end +end + +------------------------ initilisation & main code ------------------------- + +-- pre-declare variables in the shared environment +C = {} -- constants +config = {} -- configuration settings + +-- actually load the submodules now +texdoc_do('constants') +texdoc_do('functions') +texdoc_do('alias') +texdoc_do('score') +texdoc_do('config') +texdoc_do('search') +texdoc_do('view') + +return { + setup_config_and_alias = setup_config_and_alias, + init_databases = init_databases, + print_usage = print_usage, + get_doclist = get_doclist, + deliver_results = deliver_results, + aliased_names = aliased_names, + show_config_files = show_config_files, + config = config, + view_file = view_file, + const = C, +} diff --git a/Master/texmf/scripts/texdoc/view.tlu b/Master/texmf/scripts/texdoc/view.tlu index 8c327b00a72..b51bea4c570 100644 --- a/Master/texmf/scripts/texdoc/view.tlu +++ b/Master/texmf/scripts/texdoc/view.tlu @@ -1,16 +1,6 @@ --- view a document and/or display the list of results in texdoc ---[[ -Copyright 2008, 2009 Manuel Pégourié-Gonnard -Distributed under the terms of the GNU GPL version 3 or later. -See texdoc.tlu for details. ---]] - --- Load a private environment for this submodule (see texdoc.tlu). -local L = {} -load_env(L, { - 'os', 'io', 'print', - 'config', 'parse_zip', 'w32_path', -}) +-- view.tlu view a document and/or display the list of results in texdoc +-- +-- Manuel Pégourié-Gonnard, GPLv3, see texdoclib.tlu for details ----------------------------- view a document ------------------------------ @@ -68,6 +58,11 @@ function view_file (filename) -- FIXME: hardcoding such cases this way is not very clean if viewext == 'texlive' then viewext = 'txt' end if viewext == 'htm' then viewext = 'html' end + -- get a viewer from built-in defaults if not already set + if not config['viewer_'..viewext] then + get_default_viewers() + end + -- still no viewers? use txt as a fallback if not config['viewer_'..viewext] then err_print('warning', "No viewer_"..viewext.." defined, using viewer_txt.") @@ -167,7 +162,7 @@ function deliver_results(name, doclist, many) return end -- shall we show all of them or only the "good" ones? - local showall = (config.mode == 'regex') or (config.mode == 'showall') + local showall = (config.mode == 'showall') if not showall and doclist[1].quality ~= 'good' then showall = true err_print('info', 'No good result found, showing all results.') @@ -185,8 +180,7 @@ function deliver_results(name, doclist, many) end end --- finally export a few symbols -export_symbols(L, { - 'view_file', - 'deliver_results', -}) +return { + view_file = view_file, + deliver_results = deliver_results, +} |