summaryrefslogtreecommitdiff
path: root/tex/context/base/scrn-hlp.lua
blob: 99c0565a8b79260c29f9c4475810f2b1bc23eaf4 (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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
if not modules then modules = { } end modules ['scrn-hlp'] = {
    version   = 1.001,
    comment   = "companion to scrn-hlp.mkiv",
    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
    copyright = "PRAGMA ADE / ConTeXt Development Team",
    license   = "see context related readme files"
}

local tonumber = tonumber

local help           = { }
interactions.help    = help

local context        = context
local implement      = interfaces.implement

local formatters     = string.formatters

local a_help         = attributes.private("help")

local copy_nodelist  = node.copy_list
local hpack_nodelist = node.hpack

local register_list  = nodes.pool.register

local texgetbox      = tex.getbox

local nodecodes      = nodes.nodecodes

local hlist_code     = nodecodes.hlist
local vlist_code     = nodecodes.vlist

local data, references = { }, { }

local helpscript = [[
    function Hide_All_Help(prefix) {
        var n = 0
        while (true) {
            n += 1 ;
            v = this.getField(prefix + n) ;
            if (v) {
                v.hidden = true ;
                this.dirty = false ;
            } else {
                return ;
            }
        }
    }
]]

local template = "javascript(Hide_All_Help{help:}),action(show{help:%s})"

local function register(specification)
    local number = specification.number
    local name   = specification.name
    local box    = specification.box
    if number and name and box then
        if helpscript then
            interactions.javascripts.setpreamble("HelpTexts",helpscript)
            helpscript = false
        end
        local b = copy_nodelist(texgetbox(box))
        register_list(b)
        data[number] = b
        if name and name ~= "" then
            references[name] = number
            structures.references.define("",name,formatters[template](number))
        end
    end
end

local function collectused(head,used)
    while head do
        local id = head.id
        if id == hlist_code then
            local a = head[a_help]
            if a then
                if not used then
                    used = { a }
                else
                    used[#used+1] = a
                end
            else
                used = collectused(head.list,used)
            end
        elseif id == vlist_code then
            used = collectused(head.list,used)
        end
        head = head.next
    end
    return used
end

local function collect(box)
    if next(data) then
        return collectused(texgetbox(box).list)
    end
end

local function reference(name)
    return references[name] or tonumber(name) or 0
end

help.register  = register
help.collect   = collect
help.reference = reference

implement {
    name    = "registerhelp",
    actions = register,
    arguments = {
        {
            { "number", "integer" },
            { "name" },
            { "box" , "integer" }
        }
    }
}

implement {
    name      = "collecthelp",
    arguments = "integer",
    actions   = function(box)
        local used = collect(box)
        if used then
            local done = { }
            context.startoverlay()
            for i=1,#used do
                local d = data[used[i]]
                if d and not done[d] then
                    local box = hpack_nodelist(copy_nodelist(d))
                    context(false,box)
                    done[d] = true
                else
                    -- error
                end
            end
            context.stopoverlay()
        end
    end
}

implement {
    name      = "helpreference",
    arguments = "string",
    actions   = function(name)
        context(reference(name))
    end
}

implement {
    name      = "helpaction",
    arguments = "string",
    actions   = function(name)
        context(template,reference(name))
    end
}