Debian Codegraph @

texmf-dist/tex/optex/pkg/minim.opm

raw
1 % This is used by OpTeX packages minim-mp, minim-pdf, see http://petr.olsak.net/optex
2 % minim.opm, Michal Vlasák <[email protected]>, 2022
3 % see also https://github.com/vlasakm/optex-minim
4
5 \_def\_optexminim_version{0.1}
6 \_codedecl \optexminim_loaded {Minim compatibility for \OpTeX/ (v\_optexminim_version)}
7
8 \_doc
9 When we want to use minim with \OpTeX/, we need to accommodate for their
10 differences. Particularly in allocations. Because of Esger Renkema's
11 graciousness, minim has been modified in ways that make it easier to
12 integrate with \OpTeX/. So even when the two formats normally disagree on a
13 lot of core things (allocations, callbacks) they can end up working together.
14
15 \OpTeX/ defines most allocation macros in `alloc.opm` and some Lua
16 allocation functions in `optex.lua`. Minim \"packages" are not standalone,
17 they all depend on core routines defined in `minim-alloc.tex` and
18 `minim-alloc.lua` respectively. Minim as a format preloads a stripped
19 version of `etex.src` so the Lua code makes some assumptions about that
20 (i.e. expects local allocators).
21
22 Both \OpTeX/ and minim want to make it possible to register more functions
23 for a single callback, by chaining their calls and `callback.register()`ing
24 only a proxy function. While minim stays close to the `callback` interface
25 from \LuaTeX/, \OpTeX/ is a subset of the \LaTeX/ `luatexbase` interface.
26 But minim now can work on top of ltluatex by using its functions instead of
27 `callback.register()`, so we don't have to do anything to support it in
28 \OpTeX/!
29
30 Firstly, because we only change what is defined by others, we actually need
31 a dummy macro for `\_codedecl`, to prevent loading this file more than once.
32 \_cod
33
34 \_catcode`\@=11
35 % dummy macro to signalize that we are loaded
36 \_let\optexminim_loaded=\empty
37
38 \_doc
39 In general, there are four allocator types expected by minim:
40
41 \begitems
42 * Knuth allocators from `plain.tex` (like `\newcount`). These are already
43 defined by \OpTeX/. Although minim itself sets the old `\allocationnumber`
44 counter, which is not even defined in \OpTeX/.
45
46 * Global allocators from `etex.src` (like `\globcount`). These are not
47 defined by \OpTeX/, since they no longer make sense (\LuaTeX/ doesn't use
48 sparse arrays for registers). Minim defines them to be the classic Knuth
49 allocators if it doesn't find them on the TeX side, but expects them from
50 the Lua side.
51
52 * Local allocators from `etex.src` (like `\loccount`). Concept of local
53 allocators is completely missing in \OpTeX/. The semantics of local
54 allocation in Lua is weird too, so we try to avoid these, since minim also
55 doesn't define their Lua variants.
56
57 * \LuaTeX/ allocators from `ltluatex.tex` (like `\newattribute`). Subset of
58 these is in \OpTeX/ (only attributes, which are also allocatable in Lua, and
59 catcode tables). But minim tries to be compatible with \LaTeX/ and patches
60 its routines if it detects them.
61 \enditems
62
63 For defining \TeX/ commands implemented in Lua, \OpTeX/ has
64 `define_lua_command`, which actually does the allocation and definition at
65 the same time, and allows to do so only from Lua end.
66
67 Historically (in my opinion unfortunately) \LaTeX/ made the allocations of
68 these functions available from \TeX/ end, and the \"lua define" operation is
69 thus a two step process which involves synchronization with \TeX/.
70
71 `minim-alloc` actually defines a `luadef` function which is like
72 `define_lua_command`, but is backed by the minim allocator. To make this
73 work, we just need to set the \LaTeX/ register to the index of last
74 allocated function, since it allocates at counter plus one. Then minim will
75 start where \OpTeX/ stopped, and we will later define `define_lua_command`
76 to be just minim's `luadef`.
77
78 We tell the number of allocated function by going through the table of
79 actually used functions. This is not that robust, because while
80 `define_lua_command` allocates sequentially, the provided functions may be
81 `nil`, which breaks the code below.
82 \_cod
83
84 \_newcount\allocationnumber
85
86 % for synchronisation of allocated Lua functions
87 \_ea\_newcount\_csname e@alloc@luafunction@count\_endcsname
88
89 \directlua{
90 local function_table = lua.get_functions_table()
91 % minim allocates at count + 1 for "new" allocators, so this works
92 tex.setcount("global", "e@alloc@luafunction@count", \csstring\#function_table)
93 }
94
95 \_doc
96 Both \LaTeX/ and the minim inspired catcode table allocators initialize the
97 catcode tables with `\initcatcodetable` (i.e. ini\TeX/ catcodes). \OpTeX/
98 merely allocates the registers. \LuaTeX/ doesn't allow to activate
99 uninitialized catcode table, therefore activation with either
100 `\initcatcodetable` or `\savecatcodetable` is necessary before use. To
101 ensure compatibility with foreign macros, we also issue `\initcatcodetable`
102 on allocation in the public version of `\newcatcodetable`.
103 \_cod
104
105 \_def\newcatcodetable#1{\_newcatcodetable#1\_initcatcodetable#1}
106
107 \_doc
108 We also get PDF resources out of the way now. Minim is ready to use \OpTeX's
109 PDF resource management, but also has compatibility layer for PGF, which is
110 not needed in \OpTeX/. We prevent loading the problematic \TeX/ file.
111 \_cod
112
113 \_let\minimpdfresourcesloaded=\_empty
114
115 \_doc
116 The preparations are over. We load `minim-alloc.tex`.
117 \_cod
118
119 \input minim-alloc
120
121 \_doc
122 By now, the Knuthian allocators are dealt with. \eTeX/ global and local
123 allocators are undefined, but the global ones are expected in minim's Lua
124 code with their hardcoded counter register numbers. This is unacceptable,
125 since in this range (`\count260` to `\count266`) \OpTeX/ has already made
126 allocations. Thus we need to replace these Lua functions with similar
127 definitions. For some, \OpTeX/ also has a different idea whether the counter
128 represent the last or next allocated register number, so we correct that as
129 well.
130
131 We also don't forget to actually set `define_lua_command` to be minim's
132 `luadef`.
133 \_cod
134
135 \directlua{
136 local minimalloc = require("minim-alloc")
137
138 define_lua_command = minimalloc.luadef
139
140 % these are allocators already defined in OpTeX that we need to repair
141 local toreplace = {
142 "count",
143 "dimen",
144 "skip",
145 "muskip",
146 "box",
147 "toks",
148 "marks",
149 "attribute",
150 "catcodetable",
151 }
152
153 for _, alloc in ipairs(toreplace) do
154 local cache = {}
155 local countername = string.format("_\_pcent salloc", alloc)
156 minimalloc["new_"..alloc] = function(id)
157 local n = cache[id]
158 if not n then
159 n = tex.getcount(countername) + 1
160 tex.setcount("global", countername, n)
161 if id then
162 cache[id] = n
163 end
164 minimalloc.log(
165 "\_nbb\_pcent s\_pcent d : \_pcent s", alloc, n, id or "<unnamed>")
166 end
167 return n
168 end
169 end
170 }
171
172 \_doc
173 We also need to do something about `minim-hooks.tex`, which hooks into
174 `\shipout`, but the default \OpTeX/ output routine (and perhaps also the
175 user ones) use `\_shipout`.
176
177 Minim also adds to `\everypar`, but that is fine.
178 \_cod
179
180 \_let\shipout\_shipout
181
182 \_isfile{minim-hooks}\_iftrue \_input minim-hooks \_fi
183
184 \_catcode`\:=11
185 \_let\_shipout\minim:shipout:new
186
187 % catcodes changes don't propagate, since this file is loaded with \opinput
188 \_endcode