summaryrefslogtreecommitdiff
path: root/tex/context/base/mkxl/syst-mac.lmt
blob: bd1d8476ff660bb44d10b4af0df1888c2924259f (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 pushmacrotoken = token.pushmacro
local popmacrotoken  = token.popmacro
local scancsname     = token.scancsname
local createtoken    = token.create
local gobbletoken    = 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 = pushmacrotoken(name,global)
    if s then
        insert(stack[name],s)
    else
        report("no macro %a to push",name)
        insert(stack[name],false)
    end
end

local function popmacro(name)
    local s = remove(stack[name])
    if s then
        popmacrotoken(s)
    else
        report("no macro %a to pop",name)
    end
end

tokens.pushmacro = pushmacro
tokens.popmacro  = popmacro

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

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

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

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

implement {
    name      = "showmacrostack",
    public    = true,
    protected = true,
    actions   = function()
        local n = scancsname()
        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)
        gobbletoken(createtoken(start),createtoken(stop))
        if command and command ~= "" then
            context[command]()
        end
    end
}