/ [texdoc] / trunk / functions.tlu
To checkout: svn checkout http://svn.gnu.org.ua/sources/texdoc/trunk/functions.tlu
Puszcza

Annotation of /trunk/functions.tlu

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2 - (hide annotations)
Thu Mar 2 10:41:29 2017 UTC (4 years, 10 months ago) by cereda
File size: 4097 byte(s)
First commit
1 cereda 2 -- functions.tlu: general-use functions for texdoc
2     --
3     -- Manuel Pégourié-Gonnard, GPLv3, see texdoclib.tlu for details
4    
5     -- change '/' to '\' on windows
6     -- Use: internal representation of files always use forward slashes.
7     -- This function should be called only before displaying a file name.
8     if os.type == "windows" then
9     function w32_path (path)
10     return (string.gsub (path, '/', '\\'))
11     end
12     else
13     function w32_path (path)
14     return path
15     end
16     end
17    
18     -- remove the last direcory component of a path
19     if os.type == 'windows' then
20     function path_parent(path)
21     return string.match(path, '^(.*)[\\/]')
22     end
23     else
24     function path_parent(path)
25     return string.match(path, '^(.*)/')
26     end
27     end
28    
29     -- generic error display function (see the error_priority constant)
30     function err_print (lvl, msg)
31     -- be careful: maybe config.verbosity_level is not set yet
32     local verbosity_level = config.verbosity_level or tonumber(C.def_verbosity)
33     if C.err_priority[lvl] <= verbosity_level then
34     io.stderr:write ("texdoc "..lvl..": "..msg.."\n")
35     end
36     end
37    
38     do --scope of active_debugs
39     local active_debugs
40    
41     -- generic debug function
42     function deb_print(cat, msg)
43     -- make sure active_debugs is set
44     if not active_debugs then set_active_debugs() end
45     -- print message it belongs to an active category
46     if active_debugs and active_debugs[cat] or cat == 'XXX' then
47     io.stderr:write ("texdoc debug-"..cat..": "..msg.."\n")
48     end
49     end
50    
51     -- set active_debugs
52     function set_active_debugs()
53     if not config.debug_list then return end
54     active_debugs = {}
55     -- all debug options imply version info
56     if config.debug_list[1] then
57     active_debugs.version = true
58     else
59     return
60     end
61     -- if 'all' is the first keyword, just activate all categories
62     if config.debug_list[1] == 'all' then
63     for deb in pairs(C.known_debugs) do
64     active_debugs[deb] = true end
65     return
66     end
67     -- activate options from the list
68     for _, deb in ipairs(config.debug_list) do
69     local deps = C.known_debugs[deb]
70     if deps then
71     active_debugs[deb] = true
72     for _, d in ipairs(deps) do active_debugs[d] = true end
73     else
74     err_print('warning', "Unknown debug category '"..deb.."'.")
75     end
76     end
77     end
78    
79     end -- scope of active_debugs
80    
81     -- if file is base..'.'..zip with zip in zipext_list, return: base, zip
82     -- otherwise, return: file, nil
83     function parse_zip(file)
84     local zip
85     for _, zip in ipairs(config.zipext_list) do
86     local l = #zip + 1
87     if string.sub(file, -l, -1) == '.'..zip then
88     return string.sub(file, 1, -l - 1), zip
89     end
90     end
91     return file, nil
92     end
93    
94     -- print a usage message, with the proper 'active values' line
95     function print_usage(actions)
96     local usage_msg = C.usage_msg
97     -- get current settings
98     local default, files = {}, {}
99     for _, param in ipairs(C.usage_settings) do
100     local display = param[config[param.name]]
101     if config[param.name..'_src'] == 'def' then
102     table.insert(default, display)
103     elseif config[param.name..'_src'] == 'file' then
104     table.insert(files, display)
105     end
106     end
107     -- format and include them
108     def = table.concat(default, ', ')
109     conf = table.concat(files, ', ')
110     if not (def == '') then def = def..' (default)' end
111     if not (conf == '') then conf = conf..' (user config)' end
112     local sep = (def == '' or conf == '') and '' or '; '
113     local settings = C.usage_settings_ph..' '..def..sep..conf..'.'
114     usage_msg = usage_msg:gsub(C.usage_settings_ph, settings)
115     -- include actions if any
116     if actions then
117     usage_msg = usage_msg:gsub(C.actions_ph,
118     C.actions_ph .. actions .. '\n\n')
119     else
120     usage_msg = usage_msg:gsub(C.actions_ph, '')
121     end
122     -- finally print it
123     print(usage_msg)
124     end
125    
126     return {
127     err_print = err_print,
128     deb_print = deb_print,
129     w32_path = w32_path,
130     parse_zip = parse_zip,
131     print_usage = print_usage,
132     path_parent = path_parent,
133     }

Send suggestions and bug reports to Sergey Poznyakoff
ViewVC Help
Powered by ViewVC 1.1.20