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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
|
--
-- This is file `luamcallbacks.lua',
-- generated with the docstrip utility.
--
-- The original source files were:
--
-- luamcallbacks.dtx (with options: `lua')
-- This is a generated file.
--
-- Copyright (C) 2009 by Elie Roux <elie.roux@telecom-bretagne.eu>
--
-- This work is under the CC0 license.
--
-- This work consists of the main source file luamcallbacks.dtx
-- and the derived files
-- luamcallbacks.lua, luamcallbacks-test.tex, luamcallbacks.pdf.
--
luamcallbacks = { }
luamcallbacks.module = {
name = "luamcallbacks",
version = 0.93,
date = "2009/09/18",
description = "Module to register several functions in a callback.",
author = "Hans Hagen & Elie Roux",
copyright = "Hans Hagen & Elie Roux",
license = "CC0",
}
luatextra.provides_module(luamcallbacks.module)
luamcallbacks.callbacklist = luamcallbacks.callbacklist or { }
luamcallbacks.lua_callbacks_defaults = { }
local format = string.format
local list = 1
local data = 2
local first = 3
local simple = 4
luamcallbacks.callbacktypes = luamcallbacks.callbacktypes or {
buildpage_filter = simple,
token_filter = first,
pre_output_filter = list,
hpack_filter = list,
process_input_buffer = data,
mlist_to_hlist = list,
vpack_filter = list,
define_font = first,
open_read_file = first,
linebreak_filter = list,
post_linebreak_filter = list,
pre_linebreak_filter = list,
start_page_number = simple,
stop_page_number = simple,
start_run = simple,
show_error_hook = simple,
stop_run = simple,
hyphenate = simple,
ligaturing = simple,
kerning = data,
find_write_file = first,
find_read_file = first,
find_vf_file = data,
find_map_file = data,
find_format_file = data,
find_opentype_file = data,
find_output_file = data,
find_truetype_file = data,
find_type1_file = data,
find_data_file = data,
find_pk_file = data,
find_font_file = data,
find_image_file = data,
find_ocp_file = data,
find_sfd_file = data,
find_enc_file = data,
read_sfd_file = first,
read_map_file = first,
read_pk_file = first,
read_enc_file = first,
read_vf_file = first,
read_ocp_file = first,
read_opentype_file = first,
read_truetype_file = first,
read_font_file = first,
read_type1_file = first,
read_data_file = first,
}
if tex.luatexversion > 42 then
luamcallbacks.callbacktypes["process_output_buffer"] = data
end
luamcallbacks.internalregister = luamcallbacks.internalregister or callback.register
local callbacktypes = luamcallbacks.callbacktypes
luamcallbacks.log = luamcallbacks.log or function(...)
luatextra.module_log('luamcallbacks', format(...))
end
luamcallbacks.info = luamcallbacks.info or function(...)
luatextra.module_info('luamcallbacks', format(...))
end
luamcallbacks.warning = luamcallbacks.warning or function(...)
luatextra.module_warning('luamcallbacks', format(...))
end
luamcallbacks.error = luamcallbacks.error or function(...)
luatextra.module_error('luamcallbacks', format(...))
end
function luamcallbacks.str_to_type(str)
if str == 'list' then
return list
elseif str == 'data' then
return data
elseif str == 'first' then
return first
elseif str == 'simple' then
return simple
else
return nil
end
end
function luamcallbacks.create(name, ctype, default)
if not name then
luamcallbacks.error(format("unable to call callback, no proper name passed", name))
return nil
end
if not ctype or not default then
luamcallbacks.error(format("unable to create callback '%s', callbacktype or default function not specified", name))
return nil
end
if callbacktypes[name] then
luamcallbacks.error(format("unable to create callback '%s', callback already exists", name))
return nil
end
local temp = luamcallbacks.str_to_type(ctype)
if not temp then
luamcallbacks.error(format("unable to create callback '%s', type '%s' undefined", name, ctype))
return nil
end
ctype = temp
luamcallbacks.lua_callbacks_defaults[name] = default
callbacktypes[name] = ctype
end
function luamcallbacks.call(name, ...)
if not name then
luamcallbacks.error(format("unable to call callback, no proper name passed", name))
return nil
end
if not luamcallbacks.lua_callbacks_defaults[name] then
luamcallbacks.error(format("unable to call lua callback '%s', unknown callback", name))
return nil
end
local l = luamcallbacks.callbacklist[name]
local f
if not l then
f = luamcallbacks.lua_callbacks_defaults[name]
else
if callbacktypes[name] == list then
f = luamcallbacks.listhandler(name)
elseif callbacktypes[name] == data then
f = luamcallbacks.datahandler(name)
elseif callbacktypes[name] == simple then
f = luamcallbacks.simplehandler(name)
elseif callbacktypes[name] == first then
f = luamcallbacks.firsthandler(name)
else
luamcallbacks.error("unknown callback type")
end
end
return f(...)
end
function luamcallbacks.add (name,func,description,priority)
if type(func) ~= "function" then
luamcallbacks.error("unable to add function, no proper function passed")
return
end
if not name or name == "" then
luamcallbacks.error("unable to add function, no proper callback name passed")
return
elseif not callbacktypes[name] then
luamcallbacks.error(
format("unable to add function, '%s' is not a valid callback",
name))
return
end
if not description or description == "" then
luamcallbacks.error(
format("unable to add function to '%s', no proper description passed",
name))
return
end
if luamcallbacks.get_priority(name, description) ~= 0 then
luamcallbacks.warning(
format("function '%s' already registered in callback '%s'",
description, name))
end
local l = luamcallbacks.callbacklist[name]
if not l then
l = {}
luamcallbacks.callbacklist[name] = l
if not luamcallbacks.lua_callbacks_defaults[name] then
if callbacktypes[name] == list then
luamcallbacks.internalregister(name, luamcallbacks.listhandler(name))
elseif callbacktypes[name] == data then
luamcallbacks.internalregister(name, luamcallbacks.datahandler(name))
elseif callbacktypes[name] == simple then
luamcallbacks.internalregister(name, luamcallbacks.simplehandler(name))
elseif callbacktypes[name] == first then
luamcallbacks.internalregister(name, luamcallbacks.firsthandler(name))
else
luamcallbacks.error("unknown callback type")
end
end
end
local f = {
func = func,
description = description,
}
priority = tonumber(priority)
if not priority or priority > #l then
priority = #l+1
elseif priority < 1 then
priority = 1
end
if callbacktypes[name] == first and (priority ~= 1 or #l ~= 0) then
luamcallbacks.warning(format("several callbacks registered in callback '%s', only the first function will be active.", name))
end
table.insert(l,priority,f)
luamcallbacks.log(
format("inserting function '%s' at position %s in callback list for '%s'",
description,priority,name))
end
function luamcallbacks.get_priority (name, description)
if not name or name == "" or not callbacktypes[name] or not description then
return 0
end
local l = luamcallbacks.callbacklist[name]
if not l then return 0 end
for p, f in pairs(l) do
if f.description == description then
return p
end
end
return 0
end
function luamcallbacks.remove (name, description)
if not name or name == "" then
luamcallbacks.error("unable to remove function, no proper callback name passed")
return
elseif not callbacktypes[name] then
luamcallbacks.error(
format("unable to remove function, '%s' is not a valid callback",
name))
return
end
if not description or description == "" then
luamcallbacks.error(
format("unable to remove function from '%s', no proper description passed",
name))
return
end
local l = luamcallbacks.callbacklist[name]
if not l then
luamcallbacks.error(format("no callback list for '%s'",name))
return
end
for k,v in ipairs(l) do
if v.description == description then
table.remove(l,k)
luamcallbacks.log(
format("removing function '%s' from '%s'",description,name))
if not next(l) then
luamcallbacks.callbacklist[name] = nil
if not luamcallbacks.lua_callbacks_defaults[name] then
luamcallbacks.internalregister(name, nil)
end
end
return
end
end
luamcallbacks.warning(
format("unable to remove function '%s' from '%s'",description,name))
end
function luamcallbacks.reset (name)
if not name or name == "" then
luamcallbacks.error("unable to reset, no proper callback name passed")
return
elseif not callbacktypes[name] then
luamcallbacks.error(
format("reset error, '%s' is not a valid callback",
name))
return
end
if not luamcallbacks.lua_callbacks_defaults[name] then
luamcallbacks.internalregister(name, nil)
end
local l = luamcallbacks.callbacklist[name]
if l then
luamcallbacks.log(format("resetting callback list '%s'",name))
luamcallbacks.callbacklist[name] = nil
end
end
function luamcallbacks.listhandler (name)
return function(head,...)
local l = luamcallbacks.callbacklist[name]
if l then
local done = true
for _, f in ipairs(l) do
-- the returned value can be either true or a new head plus true
rtv1, rtv2 = f.func(head,...)
if type(rtv1) == 'boolean' then
done = rtv1
elseif type (rtv1) == 'userdata' then
head = rtv1
end
if type(rtv2) == 'boolean' then
done = rtv2
elseif type(rtv2) == 'userdata' then
head = rtv2
end
if done == false then
luamcallbacks.error(format(
"function \"%s\" returned false in callback '%s'",
f.description, name))
end
end
return head, done
else
return head, false
end
end
end
function luamcallbacks.datahandler (name)
return function(data,...)
local l = luamcallbacks.callbacklist[name]
if l then
for _, f in ipairs(l) do
data = f.func(data,...)
end
end
return data
end
end
function luamcallbacks.firsthandler (name)
return function(...)
local l = luamcallbacks.callbacklist[name]
if l then
local f = l[1].func
return f(...)
else
return nil, false
end
end
end
function luamcallbacks.simplehandler (name)
return function(...)
local l = luamcallbacks.callbacklist[name]
if l then
for _, f in ipairs(l) do
f.func(...)
end
end
end
end
callback.add = luamcallbacks.add
callback.remove = luamcallbacks.remove
callback.reset = luamcallbacks.reset
callback.create = luamcallbacks.create
callback.call = luamcallbacks.call
callback.get_priority = luamcallbacks.get_priority
callback.register = function (...)
luamcallbacks.error("function callback.register has been deleted by luamcallbacks, please use callback.add instead.")
end
--
-- End of File `luamcallbacks.lua'.
|