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

local allocate    = utilities.storage.allocate
local implement   = interfaces.implement

local texsetcount = tex.setcount
local texsetdimen = tex.setdimen

local itemgroups = structures.itemgroups

local collected = allocate()
local tobesaved = allocate()

itemgroups.collected = collected
itemgroups.tobesaved = tobesaved

local function initializer()
    collected = itemgroups.collected
    tobesaved = itemgroups.tobesaved
end

if job then
    job.register("structures.itemgroups.collected", tobesaved, initializer)
end

local c_strc_itemgroups_max_items = tex.iscount("c_strc_itemgroups_max_items")
local d_strc_itemgroups_max_width = tex.isdimen("d_strc_itemgroups_max_width")

-- We keep the counter at the Lua end so we can group the items within
-- an itemgroup which in turn makes for less passes when one itemgroup
-- entry is added or removed.

local counts = table.setmetatableindex("number")

local trialtypesetting = context.trialtypesetting

local function analyzeitemgroup(name,level)
    local n = counts[name]
    if level == 1 then
        n = n + 1
        counts[name] = n
    end
    local items = 0
    local width = 0
    local itemgroup = collected[name]
    if itemgroup then
        local entry = itemgroup[n]
        if entry then
            local l = entry[level]
            if l then
                items = l[1] or 0
                width = l[2] or 0
            end
        end
    end
    texsetcount(c_strc_itemgroups_max_items,items)
    texsetdimen(d_strc_itemgroups_max_width,width)
end

local function registeritemgroup(name,level,nofitems,maxwidth)
    local n = counts[name]
    if not trialtypesetting() then
        local itemgroup = tobesaved[name]
        if not itemgroup then
            itemgroup       = { }
            tobesaved[name] = itemgroup
        end
        local entry = itemgroup[n]
        if not entry then
            entry        = { }
            itemgroup[n] = entry
        end
        entry[level] = { nofitems, maxwidth }
    elseif level == 1 then
        counts[name] = n - 1
    end
end

implement {
    name      = "analyzeitemgroup",
    actions   = analyzeitemgroup,
    arguments = { "string", "integer" }
}

implement {
    name      = "registeritemgroup",
    actions   = registeritemgroup,
    arguments = { "string", "integer", "integer", "dimen" }
}