scrn-hlp.lua /size: 3840 b    last modification: 2021-10-28 13:50
1if not modules then modules = { } end modules ['scrn-hlp'] = {
2    version   = 1.001,
3    comment   = "companion to scrn-hlp.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 tonumber = tonumber
10
11local help            = { }
12interactions.help     = help
13
14local context         = context
15local implement       = interfaces.implement
16
17local formatters      = string.formatters
18
19local a_help          = attributes.private("help")
20
21local copy_node_list  = nodes.copylist
22local hpack_node_list = nodes.hpack
23
24local register_list   = nodes.pool.register
25
26local texgetbox       = tex.getbox
27
28local nodecodes       = nodes.nodecodes
29
30local hlist_code      = nodecodes.hlist
31local vlist_code      = nodecodes.vlist
32
33local data, references = { }, { }
34
35local helpscript = [[
36    function Hide_All_Help(prefix) {
37        var n = 0
38        while (true) {
39            n += 1 ;
40            v = this.getField(prefix + n) ;
41            if (v) {
42                v.hidden = true ;
43                this.dirty = false ;
44            } else {
45                return ;
46            }
47        }
48    }
49]]
50
51local template = "javascript(Hide_All_Help{help:}),action(show{help:%s})"
52
53local function register(specification)
54    local number = specification.number
55    local name   = specification.name
56    local box    = specification.box
57    if number and name and box then
58        if helpscript then
59            interactions.javascripts.setpreamble("HelpTexts",helpscript)
60            helpscript = false
61        end
62        local b = copy_node_list(texgetbox(box))
63        register_list(b)
64        data[number] = b
65        if name and name ~= "" then
66            references[name] = number
67            structures.references.define("",name,formatters[template](number))
68        end
69    end
70end
71
72local function collectused(head,used)
73    while head do
74        local id = head.id
75        if id == hlist_code then
76            local a = head[a_help]
77            if a then
78                if not used then
79                    used = { a }
80                else
81                    used[#used+1] = a
82                end
83            else
84                used = collectused(head.list,used)
85            end
86        elseif id == vlist_code then
87            used = collectused(head.list,used)
88        end
89        head = head.next
90    end
91    return used
92end
93
94local function collect(box)
95    if next(data) then
96        return collectused(texgetbox(box).list)
97    end
98end
99
100local function reference(name)
101    return references[name] or tonumber(name) or 0
102end
103
104help.register  = register
105help.collect   = collect
106help.reference = reference
107
108implement {
109    name    = "registerhelp",
110    actions = register,
111    arguments = {
112        {
113            { "number", "integer" },
114            { "name" },
115            { "box" , "integer" }
116        }
117    }
118}
119
120implement {
121    name      = "collecthelp",
122    arguments = "integer",
123    actions   = function(box)
124        local used = collect(box)
125        if used then
126            local done = { }
127            context.startoverlay()
128            for i=1,#used do
129                local d = data[used[i]]
130                if d and not done[d] then
131                    local box = hpack_node_list(copy_node_list(d))
132                    context(false,box)
133                    done[d] = true
134                else
135                    -- error
136                end
137            end
138            context.stopoverlay()
139        end
140    end
141}
142
143implement {
144    name      = "helpreference",
145    arguments = "string",
146    actions   = function(name)
147        context(reference(name))
148    end
149}
150
151implement {
152    name      = "helpaction",
153    arguments = "string",
154    actions   = function(name)
155        context(template,reference(name))
156    end
157}
158