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