tabl-ntb.lmt /size: 3487 b    last modification: 2024-01-16 10:22
1if not modules then modules = { } end modules ['tabl-ntb'] = {
2    version   = 1.001,
3    comment   = "companion to tabl-ntb.mkxl",
4    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
5    copyright = "PRAGMA ADE / ConTeXt Development Team",
6    license   = "see context related readme files"
7}
8
9local context = context
10
11local a_tablesection = attributes.system("tablesection")
12
13local hlist_code     = nodes.nodecodes.hlist
14
15local nuts           = nodes.nuts
16local tonode         = nuts.tonode
17local getbox         = nuts.getbox
18local getlist        = nuts.getlist
19local getid          = nuts.getid
20local getnext        = nuts.getnext
21local setnext        = nuts.setnext
22local getattr        = nuts.getattr
23local copylist       = nuts.copylist
24local flushlist      = nuts.flushlist
25
26local integer_value  = tokens.values.integer
27
28local implement      = interfaces.implement
29
30local list           = { }
31local sections       = { }
32
33local function check(b)
34    local c = getbox(b)
35    local l = c and getlist(c)
36    local d = false
37    local n = 0
38    while l do
39        if getid(l) == hlist_code then
40            local line = getattr(l,a_tablesection)
41            if line and line ~= d then
42                local s = sections[line]
43                if s then
44                    local count = s[2]
45                    local last = l
46                    local next = getnext(l)
47                    while next and count > 0 do
48                        last  = next
49                        next  = getnext(next)
50                        count = count - 1
51                    end
52                    setnext(last)
53                    list[line] = { l, copylist(l) }
54                    if next then
55                        setnext(last,next)
56                    end
57                    d = line
58                end
59            end
60            n = n + 1
61        end
62        l = getnext(l)
63    end
64end
65
66local function reset(b)
67    for k, v in next, list do
68        flushlist(v[2])
69    end
70    list     = { }
71    sections = { }
72end
73
74local function locate(b)
75    local c = getbox(b)
76    local l = c and getlist(c)
77    while l do
78        if getid(l) == hlist_code then
79            local line = getattr(l,a_tablesection)
80            if line then
81                local v = list[line]
82                if v and v[1] ~= l then
83                    return line
84                end
85            end
86            return 0
87        end
88        l = getnext(l)
89    end
90    return 0
91end
92
93local function fetch(n)
94    local b = list[n]
95    if b then
96        b = copylist(b[2])
97        return tonode(b)
98    end
99end
100
101implement {
102    name      = "tabl_ntb_set_sec",
103    public    = true,
104    arguments = { "integer", "integer", "integer" },
105    actions   = function(n,m,count)
106        sections[n] = { m, count }
107    end,
108}
109
110implement {
111    name      = "tabl_ntb_get_sec",
112    public    = true,
113    arguments = "integer",
114    usage     = "value",
115    actions   = function(n)
116        local s =  sections[n]
117        return integer_value, s and s[1] or 0
118    end,
119}
120
121implement {
122    name      = "ntb_split_section_check",
123    arguments = "integer",
124    actions   = check,
125}
126
127implement {
128    name      = "ntb_split_section_reset",
129    arguments = "integer",
130    actions   = reset,
131}
132
133implement {
134    name      = "ntb_split_section_locate",
135    arguments = "integer",
136    actions   = { locate, context },
137}
138
139implement {
140    name      = "ntb_split_section_fetch",
141    arguments = "integer",
142    actions   = { fetch, context },
143}
144