strc-itm.lmt /size: 2631 b    last modification: 2024-01-16 10:22
1if not modules then modules = { } end modules ['strc-itm'] = {
2    version   = 1.001,
3    comment   = "companion to strc-itm.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
9local allocate    = utilities.storage.allocate
10local implement   = interfaces.implement
11
12local texsetcount = tex.setcount
13local texsetdimen = tex.setdimen
14
15local itemgroups = structures.itemgroups
16
17local collected = allocate()
18local tobesaved = allocate()
19
20itemgroups.collected = collected
21itemgroups.tobesaved = tobesaved
22
23local function initializer()
24    collected = itemgroups.collected
25    tobesaved = itemgroups.tobesaved
26end
27
28if job then
29    job.register("structures.itemgroups.collected", tobesaved, initializer)
30end
31
32local c_strc_itemgroups_max_items = tex.iscount("c_strc_itemgroups_max_items")
33local d_strc_itemgroups_max_width = tex.isdimen("d_strc_itemgroups_max_width")
34
35-- We keep the counter at the Lua end so we can group the items within
36-- an itemgroup which in turn makes for less passes when one itemgroup
37-- entry is added or removed.
38
39local counts = table.setmetatableindex("number")
40
41local trialtypesetting = context.trialtypesetting
42
43local function analyzeitemgroup(name,level)
44    local n = counts[name]
45    if level == 1 then
46        n = n + 1
47        counts[name] = n
48    end
49    local items = 0
50    local width = 0
51    local itemgroup = collected[name]
52    if itemgroup then
53        local entry = itemgroup[n]
54        if entry then
55            local l = entry[level]
56            if l then
57                items = l[1] or 0
58                width = l[2] or 0
59            end
60        end
61    end
62    texsetcount(c_strc_itemgroups_max_items,items)
63    texsetdimen(d_strc_itemgroups_max_width,width)
64end
65
66local function registeritemgroup(name,level,nofitems,maxwidth)
67    local n = counts[name]
68    if not trialtypesetting() then
69        local itemgroup = tobesaved[name]
70        if not itemgroup then
71            itemgroup       = { }
72            tobesaved[name] = itemgroup
73        end
74        local entry = itemgroup[n]
75        if not entry then
76            entry        = { }
77            itemgroup[n] = entry
78        end
79        entry[level] = { nofitems, maxwidth }
80    elseif level == 1 then
81        counts[name] = n - 1
82    end
83end
84
85implement {
86    name      = "analyzeitemgroup",
87    actions   = analyzeitemgroup,
88    arguments = { "string", "integer" }
89}
90
91implement {
92    name      = "registeritemgroup",
93    actions   = registeritemgroup,
94    arguments = { "string", "integer", "integer", "dimen" }
95}
96