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

local context = context

local a_tablesection = attributes.system("tablesection")

local hlist_code     = nodes.nodecodes.hlist

local nuts           = nodes.nuts
local tonode         = nuts.tonode
local getbox         = nuts.getbox
local getlist        = nuts.getlist
local getid          = nuts.getid
local getnext        = nuts.getnext
local setnext        = nuts.setnext
local getattr        = nuts.getattr
local copylist       = nuts.copylist
local flushlist      = nuts.flushlist

local integer_value  = tokens.values.integer

local implement      = interfaces.implement

local list           = { }
local sections       = { }

local function check(b)
    local c = getbox(b)
    local l = c and getlist(c)
    local d = false
    local n = 0
    while l do
        if getid(l) == hlist_code then
            local line = getattr(l,a_tablesection)
            if line and line ~= d then
                local s = sections[line]
                if s then
                    local count = s[2]
                    local last = l
                    local next = getnext(l)
                    while next and count > 0 do
                        last  = next
                        next  = getnext(next)
                        count = count - 1
                    end
                    setnext(last)
                    list[line] = { l, copylist(l) }
                    if next then
                        setnext(last,next)
                    end
                    d = line
                end
            end
            n = n + 1
        end
        l = getnext(l)
    end
end

local function reset(b)
    for k, v in next, list do
        flushlist(v[2])
    end
    list     = { }
    sections = { }
end

local function locate(b)
    local c = getbox(b)
    local l = c and getlist(c)
    while l do
        if getid(l) == hlist_code then
            local line = getattr(l,a_tablesection)
            if line then
                local v = list[line]
                if v and v[1] ~= l then
                    return line
                end
            end
            return 0
        end
        l = getnext(l)
    end
    return 0
end

local function fetch(n)
    local b = list[n]
    if b then
        b = copylist(b[2])
        return tonode(b)
    end
end

implement {
    name      = "tabl_ntb_set_sec",
    public    = true,
    arguments = { "integer", "integer", "integer" },
    actions   = function(n,m,count)
        sections[n] = { m, count }
    end,
}

implement {
    name      = "tabl_ntb_get_sec",
    public    = true,
    arguments = "integer",
    usage     = "value",
    actions   = function(n)
        local s =  sections[n]
        return integer_value, s and s[1] or 0
    end,
}

implement {
    name      = "ntb_split_section_check",
    arguments = "integer",
    actions   = check,
}

implement {
    name      = "ntb_split_section_reset",
    arguments = "integer",
    actions   = reset,
}

implement {
    name      = "ntb_split_section_locate",
    arguments = "integer",
    actions   = { locate, context },
}

implement {
    name      = "ntb_split_section_fetch",
    arguments = "integer",
    actions   = { fetch, context },
}