catc-ini.lmt /size: 1550 b    last modification: 2021-10-28 13:50
1if not modules then modules = { } end modules ['catc-ini'] = {
2    version   = 1.001,
3    comment   = "companion to catc-ini.mkiv",
4    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
5    copyright = "PRAGMA ADE / ConTeXt Development Team",
6    license   = "see context related readme files"
7}
8
9catcodes         = catcodes         or { }
10catcodes.numbers = catcodes.numbers or { }
11catcodes.names   = catcodes.names   or { }
12
13storage.register("catcodes/numbers", catcodes.numbers, "catcodes.numbers")
14storage.register("catcodes/names",   catcodes.names,   "catcodes.names")
15
16local numbers = catcodes.numbers
17local names   = catcodes.names
18
19-- We register the catcode tables in the \type {tex} namespace as we have been doing
20-- right from the start (when there wasn't much in that namespace). Normally
21-- registration of a table only happens when we're in so called ini mode. Also, the
22-- number of tables is rather small.
23
24function catcodes.register(name,number)
25    numbers[name] = number
26    local cnn = names[number]
27    if cnn then
28        cnn[#cnn+1] = name
29    else
30        names[number] = { name }
31    end
32    tex[name] = number -- downward compatible
33end
34
35-- At runtime we need to populate the \type {tex} namespace again.
36
37for name, number in next, numbers do
38    tex[name] = number -- downward compatible
39end
40
41-- We catch wrong usage.
42
43table.setmetatableindex(numbers,function(t,k) if type(k) == "number" then t[k] = k return k end end)
44table.setmetatableindex(names,  function(t,k) if type(k) == "string" then t[k] = k return k end end)
45