summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/scripts/texdoc/texdoclib.tlu
blob: 5de6996aa5f22d29fd400bc8264b2ee88f72d42d (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
-- texdoclib.tlu: the texdoc library

--[[
Copyright 2008-2018 Manuel Pégourié-Gonnard, Takuto Asakura, Karl Berry, and
Norbert Preining.

This program is free software: you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later
version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.  See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with
this program.  If not, see <http://www.gnu.org/licenses/>.

Previous work in the public domain:
- Contributions from Reinhard Kotucha (2008).
- First texlua versions by Frank Küster (2007).
- Original shell script by Thomas Esser, David Aspinall, and Simon Wilkinson.
--]]

------------------------   functions for submodules   ------------------------

-- texdoc is divided in submodules (files). Each module is loaded in a private
-- copy of a shared environment, and can export symbols to this environments
-- by returning a table to be merge with this environment.

-- return a simple (not deep) copy of a table
local function simple_copy(t)
    local c = {}
    for sym, val in pairs(t) do
        c[sym] = val
    end
    return c
end

-- initialise the shared environment with a copy of the global environment
local prv_env = simple_copy(_G)
if setfenv then
    setfenv(1, prv_env)
else
    _ENV = prv_env
end

-- import symbols from a table to the share environment
local function import_symbols(symbols, name)
    for sym, val in pairs(symbols) do
        assert(val ~= nil,
            'Internal error: '..name..' exporting undefined symbol '..sym..'.')
        assert(prv_env[sym] == nil,
            'Internal error: '..name..' exporting existing symbol '..sym..'.')
        prv_env[sym] = val
    end
end

-- load a submodule of texdoc
local function texdoc_do(name)
    local pathname = kpse.find_file('texdoc/'..name, 'lua')
    assert(pathname, 'Internal error: missing submodule: '..name)
    local submod
    if setfenv then
        submod = assert(loadfile (pathname))
        setfenv(submod, simple_copy(prv_env))
    else
        local f = assert(io.open(pathname))
        local s = f:read('*a')
        f:close()
        submod = assert(load(s, pathname, 'bt', simple_copy(prv_env)))
        -- submod = assert(loadfile(pathname, 'bt', simple_copy(prv_env)))
    end
    local symbols = submod()
    if symbols then import_symbols(symbols, name) end
end

------------------------   initilisation & main code   -------------------------

-- pre-declare variables in the shared environment
C = {}      -- constants
config = {} -- configuration settings

-- actually load the submodules now
texdoc_do('constants')
texdoc_do('functions')
texdoc_do('alias')
texdoc_do('score')
texdoc_do('config')
texdoc_do('search')
texdoc_do('view')

return {
    setup_config_and_alias = setup_config_and_alias,
    read_config_file       = read_config_file,
    init_databases         = init_databases,
    print_usage            = print_usage,
    get_doclist            = get_doclist,
    deliver_results        = deliver_results,
    aliased_names          = aliased_names,
    show_config_files      = show_config_files,
    config                 = config,
    view_file              = view_file,
    err_print              = err_print,
    const                  = C,
}

-- vim: ft=lua: