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

-- This is kind of tricky and might not work for all csnames but as long as we use
-- it in a controlled way, we're okay. The engine implementation might be changed
-- a bit (no need to go through strings, but fetching a cs index and passing that
-- back also takes time).

-- Another approach is to have the predefined stack operate use private stacks and
-- then the pop doesn't need the cs. But ... we then also need to store stuff in
-- the format so that complicates maters more than I'm willing to do.

local insert, remove = table.insert, table.remove

local push_macro   = token.push_macro
local pop_macro    = token.pop_macro
local scan_csname  = token.scan_csname
local create_token = token.create
local gobble_token = token.gobble

local context      = context
local implement    = interfaces.implement

local report       = logs.reporter("system","macrostack")

local stack        = table.setmetatableindex("table")

local function pushmacro(name,global)
    local s = push_macro(name,global)
    if s then
        insert(stack[name],s)
    else
        report("no macro %a",name)
        insert(stack[name],false)
    end
end

local function popmacro(name)
    local s = remove(stack[name])
    if s then
        pop_macro(s)
    else
        -- error
    end
end

tokens.pushmacro = pushmacro
tokens.popmacro  = popmacro

implement {
    name      = "localpushmacro",
    public    = true,
    protected = true,
    actions   = function()
        pushmacro(scan_csname())
    end
}

implement {
    name      = "globalpushmacro",
    public    = true,
    protected = true,
    actions   = function()
        pushmacro(scan_csname(),true)
    end
}

implement {
    name      = "localpopmacro",
    public    = true,
    protected = true,
    actions   = function()
        popmacro(scan_csname())
    end
}

implement {
    name      = "globalpopmacro",
    public    = true,
    protected = true,
    actions   = function()
        popmacro(scan_csname())
    end
}

implement {
    name      = "showmacrostack",
    public    = true,
    protected = true,
    actions   = function()
        local n = scan_csname()
        local s = stack[n]
        local m = #s
        report("%s : %i stack slots used",n,m)
        for i=1,m do
            report("% 3i %S",i,s[i])
        end
    end
}

implement {
    name      = "gobblenested",
    public    = true,
    protected = true,
    arguments = "3 strings",
    actions   = function(start,stop,command)
        gobble_token(create_token(start),create_token(stop))
        if command then
            context[command]()
        end
    end
}