cldf-stp.lua /size: 2210 b    last modification: 2020-07-01 14:35
1if not modules then modules = { } end modules ['cldf-stp'] = {
2    version   = 1.001,
3    comment   = "companion to cldf-ini.mkiv",
4    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
5    copyright = "PRAGMA ADE / ConTeXt Development Team",
6    license   = "see context related readme files"
7}
8
9-- limitation: input levels
10
11-- context.stepwise (function()
12--     ...
13--     context.step(nil|...)
14--     ...
15--     context.step(nil|...)
16--     ...
17--     context.stepwise (function()
18--         ...
19--         context.step(nil|...)
20--         ...
21--         context.step(nil|...)
22--         ...
23--     end)
24--     ...
25--     context.step(nil|...)
26--     ...
27--     context.step(nil|...)
28--     ...
29-- end)
30
31local context = context
32
33local create  = coroutine.create
34local yield   = coroutine.yield
35local resume  = coroutine.resume
36local status  = coroutine.status
37
38local stepper = nil
39local stack   = { } -- will never be deep so no gc needed
40local depth   = 0
41
42local function nextstep()
43    if status(stepper) == "dead" then
44        stepper      = stack[depth]
45        depth        = depth - 1
46        stack[depth] = false
47    end
48    resume(stepper)
49end
50
51interfaces.implement {
52    name    = "step",
53    actions = nextstep,
54}
55
56local ctx_resume = context.protected.cs.clf_step
57
58local closeinput  = texio.closeinput -- experiment
59local closeindeed = true
60local stepsindeed = true
61
62directives.register("context.steps.nosteps",function(v) stepsindeed = not v end)
63directives.register("context.steps.noclose",function(v) closeindeed = not v end)
64
65if closeinput then
66
67    function context.step(first,...)
68        if first ~= nil then
69            context(first,...)
70        end
71if stepper then
72        ctx_resume()
73        yield()
74        if closeindeed then
75            closeinput()
76        end
77end
78    end
79
80else
81
82    function context.step(first,...)
83        if first ~= nil then
84            context(first,...)
85        end
86if stepper then
87        ctx_resume()
88        yield()
89end
90    end
91
92end
93
94function context.stepwise(f)
95    if stepsindeed then
96        depth = depth + 1
97        stack[depth] = stepper
98        stepper = create(f)
99     -- ctx_resume(stepper)
100        ctx_resume()
101    else
102        f()
103    end
104end
105