summaryrefslogtreecommitdiff
path: root/macros/luatex/generic/minim/minim-callbacks.lua
blob: 3ac075dc01faf68ac18b55c143ecb5f9db4b7777 (plain)
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

local M = {}

local function log(msg, ...)
    texio.write_nl('log', string.format(msg, ...))
end

--1 capturing the callback mechanism

-- if ltluatex is loaded, we must get callback.register back
if luatexbase ~= nil then
	local luatex_base = luatexbase
	luatexbase.uninstall ()
	luatexbase = luatex_base
end

local primitives = { }
M.primitives = primitives
primitives.register = callback.register
primitives.find     = callback.find
primitives.list     = callback.list

local own_callbacks = {}
local callback_lists = {}
local callback_stacks = {}

--1 finding callbacks

function M.find (name)
    local f = own_callbacks[name]
    if f == nil then
        return primitives.find(name)
    else
        return f
    end
end

function M.list (name)
    local t = {}
    for n,f in pairs(callback_lists) do
        if f then
            t[n] = #f
        else
            t[n] = false
        end
    end
    for n,f in pairs(own_callbacks) do
        if f then
            t[n] = t[n] or true
        else
            t[n] = t[n] or false
        end
    end
    for n,f in pairs(primitives.list()) do
        if f then
            t[n] = t[n] or true
        else
            t[n] = t[n] or false
        end
    end
    return t
end

--1 registering callbacks

local function register_simple (cb,f)
    -- prefer user-defined callbacks over built-in
    local x = own_callbacks[cb]
    if x == nil then
        return primitives.register (cb, f)
    else
        -- default to false because nil would delete the callback itself
        own_callbacks[cb] = f or false
        return -1
    end
end

-- will be redefined later
local function announce_callback(cb, f) end

function M.register (cb, f)
    announce_callback(cb, f)
    local list = callback_lists[cb]
    local stack = callback_stacks[cb]
    if stack then
        if f == nil then -- pop
            local p = stack[#stack]
            stack[#stack] = nil
            return register_simple (cb,p)
        else             -- push
            stack[#stack+1] = M.find (cb)
            return register_simple (cb,f)
        end
    elseif list ~= nil then
        list[#list+1] = f
        return -2
    else
        return register_simple (cb,f)
    end
end


--1 lists of callback functions

local function call_list_node (lst)
    return function (head, ...)
        local list = callback_lists[lst]
        for _,f in ipairs(list) do
            local newhead = f(head,...)
            if node.is_node(newhead) then
                head = newhead
            elseif newhead == false then
                return false
            end
        end
        return head
    end
end

local function call_list_data (lst)
    return function (str)
        local list = callback_lists[lst]
        for _,f in ipairs(list) do
            str = f(str) or str
        end
        return str
    end
end

local function call_list_simple (lst)
    return function (...)
        local list = callback_lists[lst]
        for _,f in ipairs(list) do
            f(...)
        end
    end
end

--1 creating and calling callbacks

local function register_list (lst, fn)
    M.register (lst, fn(lst))
    callback_lists[lst] = {}
end

local function stack_callback (cb)
    callback_stacks[cb] = {}
end

function M.new_callback (name, prop)
    own_callbacks[name] = false -- false means empty here
    if prop == 'stack' then
        stack_callback (name)
    elseif prop == 'node' then
        register_list (name, call_list_node)
    elseif prop == 'simple' then
        register_list (name, call_list_simple)
    elseif prop == 'data' then
        register_list (name, call_list_data)
    end
end

function M.call_callback (name, ...)
    local f = own_callbacks[name]
    if f then
        return f (...)
    else
        return false
    end
end

--1 initialisation

-- save all registered callbacks
local saved = {}
for n,s in pairs(primitives.list()) do
    if s then
        log('save callback: %s', n)
        saved[n] = callback.find(n)
    end
end

-- replace the primitive registering
callback.register = M.register
callback.find     = M.find
callback.list     = M.list

-- string processing callbacks
register_list ('process_input_buffer', call_list_data)
register_list ('process_output_buffer', call_list_data)
register_list ('process_jobname', call_list_data)

-- node list processing callbacks
register_list ('pre_linebreak_filter', call_list_node)
register_list ('post_linebreak_filter', call_list_node)
--register_list ('append_to_vlist_filter', call_list_node) -- TODO this breaks something
register_list ('hpack_filter', call_list_node)
register_list ('vpack_filter', call_list_node)
register_list ('pre_output_filter', call_list_node)

-- mlist_to_mlist and mlist_to_mlist
M.new_callback ('mlist_to_mlist', 'node')
M.new_callback ('mlist_to_hlist', 'stack')
M.register ('mlist_to_hlist', node.mlist_to_hlist )
primitives.register ('mlist_to_hlist', function (head, ...)
    local newhead = M.call_callback ('mlist_to_mlist', head, ...)
    if newhead ~= true then
        head = newhead or head
    end
    newhead = M.call_callback ('mlist_to_hlist', head, ...)
    return newhead
end)

-- simple listable callbacks
register_list ('contribute_filter', call_list_simple)
register_list ('pre_dump', call_list_simple)
register_list ('wrapup_run', call_list_simple)
register_list ('finish_pdffile', call_list_simple)
register_list ('finish_pdfpage', call_list_simple)
register_list ('insert_local_par', call_list_simple)

register_list ('ligaturing', call_list_simple)
register_list ('kerning', call_list_simple)

-- stack callbacks
stack_callback ('hpack_quality')
stack_callback ('vpack_quality')
stack_callback ('hyphenate')
stack_callback ('linebreak_filter')
stack_callback ('buildpage_filter')
stack_callback ('build_page_insert')

-- process_rule
M.new_callback ('process_rule', 'simple')
primitives.register ('process_rule', function (rule, ...)
    local p = own_callbacks[rule.index]
    if p then
        p (rule, ...)
    else
        M.call_callback ('process_rule')
    end
end)

-- restore all registered callbacks
for n,f in pairs(saved) do
    log('restore callback: %s', n)
    M.register (n,f)
end
saved = nil


local function announce_callback(cb, f)
    if f then
        log('callback added: %s', cb)
    else
        log('callback removed: %s', cb)
    end
end


--

return M