summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/context/base/scrn-hlp.lua
blob: 81d68840b15b6c0f33f15e7342e96048750fdcda (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
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 format = string.format

local help           = { }
interactions.help    = help

local a_help         = attributes.private("help")

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

local register_list  = nodes.pool.register

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})"

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

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

function help.collect(box)
    if next(data) then
        return collect(tex.box[box].list)
    end
end

commands.registerhelp = help.register

function commands.collecthelp(box)
    local used = help.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

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

function commands.helpreference(name)
    context(references[name] or tonumber(name) or 0)
end

function commands.helpaction(name)
    context(template,references[name] or tonumber(name) or 0)
end