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

-- limitation: input levels

-- context.stepwise (function()
--     ...
--     context.step(nil|...)
--     ...
--     context.step(nil|...)
--     ...
--     context.stepwise (function()
--         ...
--         context.step(nil|...)
--         ...
--         context.step(nil|...)
--         ...
--     end)
--     ...
--     context.step(nil|...)
--     ...
--     context.step(nil|...)
--     ...
-- end)

local context = context

local create  = coroutine.create
local yield   = coroutine.yield
local resume  = coroutine.resume
local status  = coroutine.status

local stepper = nil
local stack   = { } -- will never be deep so no gc needed
local depth   = 0

local function nextstep()
    if status(stepper) == "dead" then
        stepper      = stack[depth]
        depth        = depth - 1
        stack[depth] = false
    end
    resume(stepper)
end

interfaces.implement {
    name    = "step",
    actions = nextstep,
}

local ctx_resume = context.protected.cs.clf_step

local closeinput  = texio.closeinput -- experiment
local closeindeed = true
local stepsindeed = true

directives.register("context.steps.nosteps",function(v) stepsindeed = not v end)
directives.register("context.steps.noclose",function(v) closeindeed = not v end)

if closeinput then

    function context.step(first,...)
        if first ~= nil then
            context(first,...)
        end
if stepper then
        ctx_resume()
        yield()
        if closeindeed then
            closeinput()
        end
end
    end

else

    function context.step(first,...)
        if first ~= nil then
            context(first,...)
        end
if stepper then
        ctx_resume()
        yield()
end
    end

end

function context.stepwise(f)
    if stepsindeed then
        depth = depth + 1
        stack[depth] = stepper
        stepper = create(f)
     -- ctx_resume(stepper)
        ctx_resume()
    else
        f()
    end
end