1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
|
-- texdoclib-util.tlu: utility functions for texdoc
--
-- The TeX Live Team, GPLv3, see texdoclib.tlu for details
-- dependencies
local texdoc = {
const = require 'texdoclib-const',
config = require 'texdoclib-config',
}
-- shortcuts
local M = {}
local C = texdoc.const
-- lowercase and change '/' to '\' on windows for display
-- Note: Internal representation of files always use forward slashes.
-- This function should be called only before displaying a path.
if os.type == 'windows' then
function M.w32_path(path)
return (string.gsub(string.lower(path), '/', '\\'))
end
else
function M.w32_path(path)
return path
end
end
-- remove the last directory component of a path
if os.type == 'windows' then
function M.path_parent(path)
return string.match(path, '^(.*)[\\/]')
end
else
function M.path_parent(path)
return string.match(path, '^(.*)/')
end
end
-- generic log display function
local function log(label, msg, ...)
io.stderr:write('texdoc ' .. label .. ': ' .. msg:format(...) .. '\n')
end
-- generic error display function (see the err_priority constant)
function M.err_print(lvl, msg, ...)
-- be careful: maybe config item "verbosity_level" has not set yet
local verbosity_level = texdoc.config.get_value('verbosity_level')
or tonumber(C.def_verbosity)
-- print if the priority is higher than current verbosity level
if C.err_priority[lvl] <= verbosity_level then
log(lvl, msg, ...)
end
end
local err_print = M.err_print
do -- begin scope of active_debugs
local active_debugs
-- set active_debugs
local function set_active_debugs()
local debug_list = texdoc.config.get_value('debug_list')
if not debug_list then return end
active_debugs = {}
-- all debug options imply version info
if debug_list[1] then
active_debugs.version = true
else
return
end
-- if 'all' is the first keyword, just activate all categories
if debug_list[1] == 'all' then
for dbg in pairs(C.known_debugs) do
active_debugs[dbg] = true end
return
end
-- activate options from the list
for _, dbg in ipairs(debug_list) do
local deps = C.known_debugs[dbg]
if deps then
active_debugs[dbg] = true
for _, d in ipairs(deps) do active_debugs[d] = true end
else
err_print('warning', 'Unknown debug category "' .. dbg .. '".')
end
end
end
-- generic debug function
function M.dbg_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
log('debug-' .. cat, msg, ...)
end
end
end -- end scope of active_debugs
-- if filename is base .. '.' .. zip with zip in zipext_list, return: base, zip
-- otherwise, return: filename, nil
function M.parse_zip(filename)
local zip
for _, zip in ipairs(texdoc.config.get_value('zipext_list')) do
local l = #zip + 1
if string.sub(filename, -l, -1) == '.' .. zip then
return string.sub(filename, 1, -l - 1), zip
end
end
return filename, nil
end
-- take a known extension according to ext_list
function M.get_ext(filename)
filename = M.parse_zip(filename)
for _, e in ipairs(texdoc.config.get_value('ext_list')) do
if e == '*' then
local dot = filename:find('.', 1, true)
if not dot then
return ''
else
return filename:sub(-dot + 1)
end
elseif (e == '') then
if not filename:find('.', 1, true) then
return ''
end
else
local dot_e = '.' .. e
if filename:sub(-#dot_e) == dot_e then
return dot_e:sub(2)
end
end
end
end
-- print a usage message
function M.print_usage()
local groups = {
action = {},
mode = {},
interaction = {},
debug = {},
}
for _, opt in ipairs(C.options) do
local line = ''
local shortopt = ''
local longopt = ''
if opt['short'] then
if opt['short'] == 'D' then
opt['long'] = 'debug'
end
if opt['metavar'] then
shortopt = '-' .. opt['short'] .. ' ' .. opt['metavar']
else
shortopt = '-' .. opt['short']
end
end
if opt['long'] then
if opt['metavar'] then
local sep = '='
if opt['group'] == 'action' then
sep = ' '
end
longopt = '--' .. opt['long'] .. sep .. opt['metavar']
else
longopt = '--' .. opt['long']
end
end
if #shortopt > 0 and #longopt > 0 then
line = ' ' .. shortopt .. ', ' .. longopt
elseif #shortopt > 0 then
line = ' ' .. shortopt
elseif #longopt > 0 then
line = ' ' .. longopt
end
if #line > 20 then
line = line .. "\n"
end
for _ = 1, 20 - #string.gsub(line, ".*\n", '') do
line = line .. ' '
end
line = line .. opt['desc']
table.insert(groups[opt['group']], line)
end
local usage_msg = C.usage_msg
for k, v in pairs(groups) do
usage_msg = string.gsub(usage_msg, '{{' .. k .. '}}', table.concat(v, "\n"))
end
print(usage_msg)
end
function M.print_zsh_completion()
local groups = {
action = {},
mode = {},
interaction = {},
debug = {},
}
local option = ''
local complete = ''
local choices = {}
local prefix = ''
for _, opt in ipairs(C.options) do
local stop_completions = {opt['group']}
if opt['group'] == 'action' then
stop_completions = {'-', ':', '*'}
end
prefix = '"(' .. table.concat(stop_completions, ' ') .. ')"'
if opt['short'] and opt['long'] then
if opt['long'] == 'debug' then
option = '{-' .. opt['short'] .. ',--' .. opt['long'] .. '=-}'
else
option = '{-' .. opt['short'] .. ',--' .. opt['long'] .. '}'
end
elseif opt['short'] then
option = '-' .. opt['short']
elseif opt['long'] then
option = '--' .. opt['long']
end
option = prefix .. option .. '"[' .. opt['desc'] .. ']'
if opt['type'] == 'string' then
if opt['complete'] == 'options' then
choices = {}
for _, v in pairs(C.known_options) do
v = string.gsub(v, '%.%*', '')
table.insert(choices, v)
end
elseif opt['complete'] == 'debugs' then
choices = {}
for i, _ in pairs(C.known_debugs) do
table.insert(choices, i)
end
elseif type(opt['complete']) == 'table' then
choices = opt['complete']
end
complete = '(' .. table.concat(choices, ' ') .. ')'
if opt['complete'] == 'files' then
opt['metavar'] = ' '
complete = '_files'
end
option = option .. ':' .. string.lower(opt['metavar']) .. ':' .. complete
end
option = option .. '"'
table.insert(groups[opt['group']], option)
end
local completion = C.zsh_completion
for k, v in pairs(groups) do
completion = string.gsub(completion, '{{' .. k .. '}}', table.concat(v, "\n "))
end
print(completion)
end
return M
-- vim: ft=lua:
|