strc-itm.lmt /size: 2896 b    last modification: 2025-02-21 11:03
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 <const> = tex.iscount("c_strc_itemgroups_max_items")
33local d_strc_itemgroups_max_width <const> = 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,upcoming)
44    local n = counts[name]
45    local u = false
46    if level == 1 then
47        n = n + 1
48        counts[name] = n
49        u = upcoming and true
50    end
51    local items = 0
52    local width = 0
53    local itemgroup = collected[name]
54    if itemgroup then
55        local entry = itemgroup[n]
56        if entry then
57            local l = entry[level]
58            if l then
59                items = l[1] or 0
60                width = l[2] or 0
61            end
62        end
63    end
64    if u then
65        counts[name] = n - 1
66    end
67    texsetcount(c_strc_itemgroups_max_items,items)
68    texsetdimen(d_strc_itemgroups_max_width,width)
69end
70
71local function registeritemgroup(name,level,nofitems,maxwidth)
72    local n = counts[name]
73    if not trialtypesetting() then
74        local itemgroup = tobesaved[name]
75        if not itemgroup then
76            itemgroup       = { }
77            tobesaved[name] = itemgroup
78        end
79        local entry = itemgroup[n]
80        if not entry then
81            entry        = { }
82            itemgroup[n] = entry
83        end
84        entry[level] = { nofitems, maxwidth }
85    elseif level == 1 then
86        counts[name] = n - 1
87    end
88end
89
90implement {
91    name      = "analyzeitemgroup",
92    actions   = analyzeitemgroup,
93    arguments = { "string", "integer" }
94}
95
96implement {
97    name      = "analyzeupcomingitemgroup",
98    actions   = analyzeitemgroup,
99    arguments = { "string", "integer", true }
100}
101
102implement {
103    name      = "registeritemgroup",
104    actions   = registeritemgroup,
105    arguments = { "string", "integer", "integer", "dimen" }
106}
107