summaryrefslogtreecommitdiff
path: root/tex/context/modules/mkiv/s-system-macros.mkxl
blob: 91aad763af16262832f5239515a126fc89e96be4 (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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
%D \module
%D   [      file=s-system-tokens.mkxl,
%D        version=2020.06.02,
%D          title=\CONTEXT\ Style File,
%D       subtitle=System Tokens Checking,
%D         author=Hans Hagen,
%D           date=\currentdate,
%D      copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
%C
%C This module is part of the \CONTEXT\ macro||package and is
%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
%C details.

%D This file is for Wolfgang and Hans and it's used in the transition from \MKIV\
%D to the more robust \LMTX.

% todo:  \permanent\protected\let\select\directselect : same value (add ref)

% todo: when mutable, remove immutable

\startmodule[system-macros]

\continueifinputfile{s-system-macros.mkxl}

\usemodule[article-basic] \setuplayout[tight] \setupbodyfont[7pt,tt]

\setuppapersize
  [A4,landscape]
  [A4,landscape]

\starttext

\startluacode
    local context  = context
    local ctx_NC   = context.NC
    local ctx_NR   = context.NR
    local ctx_bold = context.bold

    local find     = string.find
    local gsub     = string.gsub
    local create   = token.create

    local crap     = "[$>%|%&%#" .. string.char(0xEF) .. "]"

    local basefile  = resolvers.findfile("context.mkxl")
    local swapnames = { }

    local pattern   = file.join(file.pathpart(basefile),"/*.mk*")
        local filenames = dir.glob(pattern)
    for i=1,#filenames do
        swapnames[file.basename(filenames[i])] = true
    end

    local pattern   = file.join(file.pathpart(basefile),"/*.l**")
    local filenames = dir.glob(pattern)
    for i=1,#filenames do
        swapnames[file.basename(filenames[i])] = true
    end

    local hashnames = table.setmetatableindex(function(t,k)
        local s = file.suffix(k)
        local v = k
        if s == "mkiv" then
            local n = file.replacesuffix(k,"mkxl")
            if swapnames[n] then
                v = n
            end
        elseif s == "mkvi" then
            local n = file.replacesuffix(k,"mklx")
            if swapnames[n] then
                v = n
            end
        elseif s == "lua" then
            local n = file.replacesuffix(k,"lmt")
            if swapnames[n] then
                v = n
            end
        end
        t[k] = v
        return v
    end)

    require("mtx-interface")

    local data    = scripts.interface.editor("data")
    local files   = data and data.common and data.common.filenames or { }
    local flagged = 0
    local total   = 0
    local list    = tex.hashtokens()

    table.sort(list)

    local function show(visible, check)
     -- context.starttabulate { "|l|l|l|lj2|l|l|l|l|l|l|l|l|" }
        context.starttabulate { "|l|l|l|lj2|l|l|l|l|l|l|l|l|" }
        for i=1,#list do
            local k = list[i]
            if check(k) then
                local v = create(k)
                local frozen     = v.frozen     and "frozen"
                local permanent  = v.permanent  and "permanent"
                local immutable  = v.immutable  and "immutable"
                local primitive  = v.primitive  and "primitive"
                local mutable    = v.mutable    and "mutable"
                local noaligned  = v.noaligned  and "noaligned"
                local instance   = v.instance   and "instance"
                local parameters = v.parameters
                local dealtwith = mutable or immutable or frozen or permanent or primitive -- beware: we can have combinations
                local cscommand = gsub(v.cmdname or "","_"," ")
                local whatever  = find(k,"^[a-z][a-z][a-z]+_")
                local filename  = files[k]
                local csname    = context.escape(k)
                ctx_NC() if dealtwith  then context("+")
                     elseif whatever   then context("-")
                     elseif instance   then context("!")        end
                ctx_NC() if primitive  then ctx_bold(csname)    else
                                            context(csname)     end
                ctx_NC() if parameters then context(parameters > 0 and parameters or "-") end
                ctx_NC()                    context(cscommand)
                ctx_NC() if primitive  then context(primitive)  end
                ctx_NC() if permanent  then context(permanent)  end
                ctx_NC() if frozen     then context(frozen)     end
                ctx_NC() if immutable  then context(immutable)  end
                ctx_NC() if mutable    then context(mutable)    end
                ctx_NC() if instance   then context(instance)   end
                ctx_NC() if noaligned  then context(noaligned)  end
                ctx_NC() if filename   then context(hashnames[filename]) end
                ctx_NC() ctx_NR()
                if visible then
                    total = total + 1
                    if dealtwith then
                        flagged = flagged + 1
                    end
                end
            end
        end
        context.stoptabulate()
    end

    show(true, function(k) return find(k,"^[a-zA-Z]+$") end)

    context.page()

    show(false, function(k) return not find(k,"^[a-zA-Z]+$") and not find(k,crap) end)

    tokens.setters.macro("NumberOfVisible",  total)
    tokens.setters.macro("NumberOfFlagged", flagged)
\stopluacode

\page

\starttabulate[|c|l|]
\HL
\NC \type {+}   \NC mutable, immutable, frozen, permanent, primitive \NC \NR
\NC \type {-}   \NC protected by obscurity (underscores) \NC \NR
\NC \type {!}   \NC instance (can be frozen) \NC \NR
\NC \type {0-9} \NC the number of arguments (argument grabbing can be delegated) \NC \NR
\HL
\stoptabulate

\start \hsize 15cm \veryraggedright
Often frozen commands are redefined on demand and they can be overloaded. Mutable
doesn't mean that a user can change it without consequences, for instance the
\type {\current...} ones. Primitives come with the engine, permanent macros are
the core of \CONTEXT. Noaligned macros are special and dealt deep down when
scanning for alignment specific primitives.
\par \stop

\starttabulate
\BC number of visible macros \NC \NumberOfVisible \NC \NR
\BC number of flagged macros \NC \NumberOfFlagged \NC \NR
\stoptabulate

\stoptext

\stopmodule