summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/context/base/mkiv/node-ppt.lua
blob: 485e742b64a31adde4fe2f82b8784ebdfe4faafe (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
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
if not modules then modules = { } end modules ['node-ppt'] = {
    version   = 1.001,
    comment   = "companion to node-ini.mkiv",
    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
    copyright = "PRAGMA ADE / ConTeXt Development Team",
    license   = "see context related readme files"
}

-- This is all very exeperimental and likely to change.

local next, type, unpack, load = next, type, table.unpack, load

local serialize = table.serialize
local formatters = string.formatters

local report           = logs.reporter("properties")
local report_setting   = logs.reporter("properties","setting")
local trace_setting    = false trackers.register("properties.setting", function(v) trace_setting = v end)

-- report("using experimental properties")

local nuts             = nodes.nuts
local tonut            = nuts.tonut
local tonode           = nuts.tonode
local getid            = nuts.getid
local getnext          = nuts.getnext
local getprev          = nuts.getprev
local getsubtype       = nuts.getsubtype
local getlist          = nuts.getlist
local setlist          = nuts.setlist
local getprop          = nuts.getprop

local removenode       = nuts.remove

local nextnode         = nuts.traversers.node
local nextwhatsit      = nuts.traversers.whatsit

local nodecodes        = nodes.nodecodes
local whatsitcodes     = nodes.whatsitcodes

local whatsit_code     = nodecodes.whatsit
local hlist_code       = nodecodes.hlist
local vlist_code       = nodecodes.vlist

local userdefinedwhatsit_code = whatsitcodes.userdefined

local nodepool         = nodes.pool
local new_usernode     = nodepool.usernode

local variables        = interfaces.variables
local v_before         = variables.before
local v_after          = variables.after
local v_here           = variables.here

local property_id      = nodepool.userids["property"]

local properties       = nodes.properties
local propertydata     = properties.data

local starttiming      = statistics.starttiming
local stoptiming       = statistics.stoptiming

if not propertydata then
    return
end

-- management

local function register(where,data,...)
    if not data then
        data  = where
        where = v_after
    end
    if data then
        nofslots = nofslots + 1
        return new_usernode(property_id,{ where, data, ... })
    end
end

local writenode = node.write
local flushnode = context.nodes.flush

function commands.deferredproperty(...)
--  context(register(...))
    flushnode(register(...))
end

function commands.immediateproperty(...)
    writenode(register(...))
end

commands.attachproperty = commands.deferredproperty

local actions = { } properties.actions = actions

table.setmetatableindex(actions,function(t,k)
    report("unknown property action %a",k)
    return function() end
end)

local f_delayed   = formatters["return function(target,head,where,propdata,parent) %s end"]
local f_immediate = formatters["return function(target,head,where,propdata) %s end"]

local nofdelayed  = 0 -- better is to keep track of it per page ... we can have deleted nodes with properties
local nofslots    = 0

function actions.delayed(target,head,where,propdata,code,...) -- this one is used at the tex end
    if code then
        local delayed = propdata.delayed
        if delayed then
            delayed[#delayed+1] = { where, code, ... }
        else
            propdata.delayed = { { where, code, ... } }
            nofdelayed = nofdelayed + 1
        end
    end
end

function actions.fdelayed(target,head,where,propdata,code,...) -- this one is used at the tex end
    if code then
        local delayed = propdata.delayed
        if delayed then
            delayed[#delayed+1] = { false, code, ... }
        else
            propdata.delayed = { { false, code, ... } }
            nofdelayed = nofdelayed + 1
        end
    end
end

function actions.immediate(target,head,where,propdata,code,...) -- this one is used at the tex end
    local kind = type(code)
    if kind == "string" then
        local f = f_immediate(code)
        local okay, err = load(f)
        if okay then
            local h = okay()(target,head,where,propdata,...)
            if h and h ~= head then
                return h
            end
        end
    elseif kind == "function" then
        local h = code()(target,head,where,propdata,...)
        if h and h ~= head then
            return h
        end
    end
end

local function delayed(head,parent) -- direct based
    for target, id in nextnode, head do
        local p = propertydata[target]
        if p then
            local delayed = p.delayed
            if delayed then
                for i=1,#delayed do
                    local d = delayed[i]
                    local code  = d[2]
                    local kind = type(code)
                    if kind == "string" then
                        code, err = load(f_delayed(code))
                        if code then
                            code = code()
                        end
                    end
                    local where = d[1]
                    if where then
                        local h = code(target,where,head,p,parent,unpack(d,3)) -- target where propdata head parent
                        if h and h ~= head then
                            head = h
                        end
                    else
                        code(unpack(d,3))
                    end
                end
                p.delayed = nil
                if nofdelayed == 1 then
                    nofdelayed = 0
                    return head
                else
                    nofdelayed = nofdelayed - 1
                end
            end
        end
        if id == hlist_code or id == vlist_code then
            local list = getlist(target)
            if list then
                local done = delayed(list,parent)
                if done then
                    setlist(target,done)
                end
                if nofdelayed == 0 then
                    return head
                end
            end
        else
            -- maybe also some more lists? but we will only use this for some
            -- special cases .. who knows
        end
    end
    return head
end

function properties.delayed(head) --
    if nofdelayed > 0 then
     -- if next(propertydata) then
            starttiming(properties)
            head = delayed(head)
            stoptiming(properties)
     -- else
     --     delayed = 0
     -- end
    end
    return head
end

-- more explicit ones too

local anchored = {
    [v_before] = function(n)
        while n do
            n = getprev(n)
            if getid(n) == whatsit_code and getsubtype(n) == user_code and getprop(n,"id") == property_id then
                -- continue
            else
                return n
            end
        end
    end,
    [v_after] = function(n)
        while n do
            n = getnext(n)
            if getid(n) == whatsit_code then
                local subtype = getsubtype(n)
                if (subtype == userdefinedwhatsit_code and getprop(n,"id") == property_id) then
                    -- continue
                else
                    return n
                end
            else
                return n
            end
        end
    end,
    [v_here] = function(n)
        -- todo
    end,
}

table.setmetatableindex(anchored,function(t,k)
    local v = anchored[v_after]
    t[k] = v
    return v
end)

function properties.attach(head)

    if nofslots <= 0 then
        return head
    end

    local last = nil

    starttiming(properties)

    for source, subtype in nextwhatsit, head do
        if subtype == userdefinedwhatsit_code then
            if last then
                removenode(head,last,true)
                last = nil
            end
            if getprop(source,"id") == property_id then
                local data = getprop(source,"data")
                if data then
                    local where  = data[1]
                    local target = anchored[where](source)
                    if target then
                        local first    = data[2]
                        local method   = type(first)
                        local p_target = propertydata[target]
                        local p_source = propertydata[source]
                        if p_target then
                            if p_source then
                                for k, v in next, p_source do
                                    p_target[k] = v
                                end
                            end
                            if method == "table" then
                                for k, v in next, first do
                                    p_target[k] = v
                                end
                            elseif method == "function" then
                                first(target,head,where,p_target,unpack(data,3))
                            elseif method == "string" then
                                actions[first](target,head,where,p_target,unpack(data,3))
                            end
                        elseif p_source then
                            if method == "table" then
                                propertydata[target] = p_source
                                for k, v in next, first do
                                    p_source[k] = v
                                end
                            elseif method == "function" then
                                propertydata[target] = p_source
                                first(target,head,where,p_source,unpack(data,3))
                            elseif method == "string" then
                                propertydata[target] = p_source
                                actions[first](target,head,where,p_source,unpack(data,3))
                            end
                        else
                            if method == "table" then
                                propertydata[target] = first
                            elseif method == "function" then
                                local t = { }
                                propertydata[target] = t
                                first(target,head,where,t,unpack(data,3))
                            elseif method == "string" then
                                local t = { }
                                propertydata[target] = t
                                actions[first](target,head,where,t,unpack(data,3))
                            end
                        end
                        if trace_setting then
                            report_setting("node %i, id %s, data %s",
                                target,nodecodes[getid(target)],serialize(propertydata[target],false))
                        end
                    end
                    if nofslots == 1 then
                        nofslots = 0
                        last = source
                        break
                    else
                        nofslots = nofslots - 1
                    end
                end
                last = source
            end
        end
    end

    if last then
        removenode(head,last,true)
    end

    stoptiming(properties)

    return head

end

-- maybe better hard coded in-place

statistics.register("properties processing time", function()
    return statistics.elapsedseconds(properties)
end)

-- only for development

-- local tasks = nodes.tasks
--
-- local function show(head,level,report)
--     for target in nextnode, head do
--         local p = propertydata[target]
--         if p then
--             report("level %i, node %i, id %s, data %s",
--                 level,target,nodecodes[getid(target)],serialize(propertydata[target],false))
--         end
--         local id = getid(target)
--         if id == hlist_code or id == vlist_code then
--             local list = getlist(target)
--             if list then
--                 show(list,level+1,report)
--             end
--         else
--             -- maybe more lists
--         end
--     end
--     return head, false
-- end
--
-- local report_shipout    = logs.reporter("properties","shipout")
-- local report_processors = logs.reporter("properties","processors")
--
-- function properties.showshipout   (head) return tonode(show(tonut(head),1,report_shipout   )), true end
-- function properties.showprocessors(head) return tonode(show(tonut(head),1,report_processors)), true end
--
-- tasks.prependaction("shipouts","before","nodes.properties.showshipout")
-- tasks.disableaction("shipouts","nodes.properties.showshipout")
--
-- trackers.register("properties.shipout",function(v)
--     tasks.setaction("shipouts","nodes.properties.showshipout",v)
-- end)
--
-- tasks.appendaction ("processors","after","nodes.properties.showprocessors")
-- tasks.disableaction("processors","nodes.properties.showprocessors")
--
-- trackers.register("properties.processors",function(v)
--     tasks.setaction("processors","nodes.properties.showprocessors",v)
-- end)