summaryrefslogtreecommitdiff
path: root/macros/luatex/generic/minim/minim-alloc.tex
blob: f83e043ad4049932f39c08328ae836371c199ad5 (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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206

\ifdefined \minimloaded
    \wlog{(skipped)}
    \expandafter\endinput\fi
\chardef\minimloaded=\catcode`\:
\catcode`\:=11

% 1 settings

\suppressoutererror = 1
\frenchspacing

% unicode space characters
\def\hairspace{\kern .1em }
\def\threeperemspace{\kern .33333em }
\def\fourperemspace {\kern .25em }
\catcode"00A0 = \active \letcharcode"00A0 = ~          % no-break
\catcode"2002 = \active \letcharcode"2002 = \enskip
\catcode"2003 = \active \letcharcode"2003 = \quad
\catcode"2004 = \active \letcharcode"2004 = \threeperemspace
\catcode"2005 = \active \letcharcode"2005 = \fourperemspace
\catcode"2006 = \active \letcharcode"2006 = \thinspace % six-per-em
\catcode"2008 = \active \letcharcode"2008 = \thinspace % punctuation
\catcode"2009 = \active \letcharcode"2009 = \thinspace % thin
\catcode"200A = \active \letcharcode"200A = \hairspace % hair
\catcode"202F = \active \letcharcode"202F = \thinspace % narrow no-break

% 1 programming macros

\newbox\voidbox \setbox0=\box\voidbox

\long\def\ignore#1{}
\long\def\unbrace#1{#1}
\long\def\firstoftwo#1#2{#1}
\long\def\secondoftwo#1#2{#2}

% \spacechar is equivalent to a space character
{\let\@\relax \global\futurelet\spacechar\@ \relax}

% \nextif c {yes} {no}
\protected\def\nextif{\nextif:\if}
% \nextifx c {yes} {no}
\protected\def\nextifx{\nextif:\ifx}
% \nextifcat c {yes} {no}
\protected\def\nextifcat{\nextif:\ifcat}

\long\def\nextif:#1#2#3#4{\begingroup % \if c {yes} {no}
    \def\nextif:test{#1\nextif:token#2\relax
        \expandafter\firstoftwo\else
        \expandafter\secondoftwo\fi
        {\endgroup#3}{\endgroup#4}}%
    \futurelet\nextif:token\nextif:test}

% \withoptions [default] {code}
\protected\def\withoptions[#1]#2{\nextifx[{#2}{#2[#1]}} %]

% \splitcommalist {code} {list}
\def\splitcommalist#1#2{\splitcommalist:parse{#1}#2,\splitcommalist:end,}
\def\splitcommalist:parse#1#2,{% command partial-list,
    \ifx\splitcommalist:end#2\empty\else % test for end-of-list
        \expandafter\splitcommalist:item
            \expandafter{\scantextokens{#2}}{#1}\fi} % remove spaces
\def\splitcommalist:item#1#2\fi{\fi
    \ifx\splitcommalist:end#1\splitcommalist:end\else#2{#1}\fi
    \splitcommalist:parse{#2}}
\protected\def\splitcommalist:end{\splitcommalist:end}

% \decompressedpdf
\def\decompressedpdf{%
    \pdfvariable compresslevel    = 0
    \pdfvariable objcompresslevel = 0
    \pdfvariable recompress       = 1
}

% \unset
\newcount\unset \unset = -"7FFFFFFF

% 1 ltluatex compatibility

% \ProvidesFile identifies most problematic files
\ifdefined \ProvidesFile
    \let\minim:providesfile=\ProvidesFile\else
    \def\minim:providesfile#1#2[#3]{%
        \wlog{Loading file #1 version #3.}}\fi
\def\ProvidesFile#1{\begincsname minim:intercept:#1\endcsname
    \minim:providesfile{#1}}

% repair ltluatex, which, without apparent reason, resets all allocation 
% counters, even if they already exist
\expandafter\def\csname minim:intercept:ltluatex.tex\endcsname
    {\wlog{minim: applying ltluatex patches.}%
     \edef\endinput{\let\noexpand\endinput=\noexpand\minim:endinput
        \minim:ltltx:fix{e@alloc@attribute@count}%
        \minim:ltltx:fix{e@alloc@whatsit@count}%
        \minim:ltltx:fix{e@alloc@bytecode@count}%
        \minim:ltltx:fix{e@alloc@luafunction@count}%
        \minim:ltltx:fix{e@alloc@luachunk@count}%
        \minim:ltltx:fix{e@alloc@ccodetable@count}%
        % restore register allocation functions
        \noexpand\directlua{ callback.register =
            require('minim-callbacks').primitiveregister }%
        \noexpand\endinput}}
\let\minim:endinput = \endinput
\def\minim:ltltx:fix#1{%
    \ifnum0<\the\csname#1\endcsname
        \wlog{Restoring \csname#1\endcsname to previous value \the\csname#1\endcsname}%
        \expandafter\noexpand\csname#1\endcsname =\the\csname#1\endcsname\relax\fi}

% 1 allocation

% used on the lua side by make_alloc_old
\newcount \alloc:globoffset
\alloc:globoffset = 260

\ifdefined \documentclass
    % latex has no \globcount etc.
    \alloc:globoffset = 10
    \ifnum\count10<256 \count10=256 \fi
    \ifnum\count11<256 \count11=256 \fi
    \ifnum\count12<256 \count12=256 \fi
    \ifnum\count14<256 \count14=256 \fi
\else \ifdefined \globcount
    % use global allocation (see etex.src)
    \let\newcount  = \globcount
    \let\newdimen  = \globdimen
    \let\newskip   = \globskip
    \let\newmuskip = \globmuskip
    \let\newbox    = \globbox
    \let\newtoks   = \globtoks
    \let\newmarks  = \globmarks
\fi \fi

% new allocation macros

\def\alloc:makenew#1#2#3{%
    \unless\ifcsname#3\endcsname
        \expandafter\newcount \csname#3\endcsname
        \csname#3\endcsname 0\fi
    \unless\ifcsname new\csstring#1\endcsname
        \expandafter\edef\csname new\csstring#1\endcsname{%
            \noexpand\minim:alloc\noexpand#1\noexpand#2%
            \expandafter\noexpand\csname#3\endcsname}\fi}

\def\minim:alloc#1#2#3#4{% \register \chardef \alloccount \name
    \global\advance#31%
    \allocationnumber#3%
    \global#2#4\allocationnumber
    \wlog{\string#4=\string#1\the\allocationnumber}}

% all names and counters below are identical to those from ltluatex
% note: we cannot use \newluafunction, or ltluatex will not load
\alloc:makenew \function     \chardef         {e@alloc@luafunction@count}
\alloc:makenew \attribute    \attributedef    {e@alloc@attribute@count}
\alloc:makenew \whatsit      \chardef         {e@alloc@whatsit@count}
\alloc:makenew \luabytecode  \chardef         {e@alloc@bytecode@count}
\alloc:makenew \luachunkname \chardef:chunk   {e@alloc@luachunk@count}
\alloc:makenew \catcodetable \catcode:chardef {e@alloc@ccodetable@count}
\alloc:makenew \userrule     \chardef         {e@alloc@rule@count}

% initialise new catcode tables
\def\catcode:chardef#1#2{\chardef#1#2\initcatcodetable#2}
% ltluatex initialises the catcode tables 1–4, so we make sure not to claim 
% those ourselves:
\ifnum\csname e@alloc@ccodetable@count\endcsname = 0
    \csname e@alloc@ccodetable@count\endcsname = 4 \fi
% an empty table
\newcatcodetable \minim:initcatcodes

% set initial chunk name value
\def\chardef:chunk#1#2{\chardef#1#2\directlua{lua.name[\the#2]='\csstring#1'}}

% \setluachunkname
\protected\def\setluachunkname#1#2{\newluachunkname#1%
    \directlua{lua.name[\the#1]='\luaescapestring{#2}'}}

% undefine our helper functions
\let\alloc:makenew=\undefined

% patch in our callback to \uselanguage
%  \minim:uselanguagecallback is defined from Lua
\ifcsname uselanguage@hook\endcsname
    \expandafter\let
        \expandafter\minim:uselanguagehook
            \lastnamedcs \fi
\expandafter\edef\csname uselanguage@hook\endcsname#1{%
    % our uselanguage callback
    \noexpand\minim:uselanguagecallback{#1}%
    % previous definitions
    \ifdefined\minim:uselanguagehook
        \noexpand\minim:uselanguagehook{#1}\fi}

% 1 format file compatibility

% all other work is done at the lua end
\directlua { require ('minim-alloc') }
\directlua { require ('minim-callbacks') }

% restore lua modules (the macro will be defined from the lua end)
\toksapp\everyjob{\minim:restoremodules
    \message{... all done.}}

% 

\catcode`\:=\minimloaded
\endinput