summaryrefslogtreecommitdiff
path: root/macros/luatex/generic/minim/minim-alloc.tex
blob: 66f08c90459e896908c801a66764356fbe4a783f (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

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

% 1 settings

\suppressoutererror = 1
\frenchspacing

% 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

% repair ltluatex, which, without apparent reason, resets all allocation 
% counters, even if they already exist
\def\minim:ltltx:file{ltluatex.tex}
\let\ProvidesFile:saved=\ProvidesFile
\def\ProvidesFile#1#2[#3]{%
    \ifdefined\ProvidesFile:saved \ProvidesFile:saved{#1}#2[#3]\fi
    \def\minim:ltltx:arg{#1}%
    \ifx\minim:ltltx:file\minim:ltltx:arg
        \let\ProvidesFile=\ProvidesFile:saved
        \let\endinput:saved=\endinput
        \protected\edef\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}%
            % also patch in remembering allocations to lua
            \alloc:patch \noexpand\attribute
            \alloc:patch \noexpand\whatsit
            \alloc:patch \noexpand\luabytecode
            \alloc:patch \noexpand\luachunkname
            \alloc:patch \noexpand\catcodetable
            \alloc:patch \noexpand\userrule
            % restore register allocation functions
            \directlua{ local C = require('minim-callbacks')
                callback.register, callback.find, callback.list
                    = C.register, C.find, C.list }%
            \let\noexpand\endinput=\noexpand\endinput:saved
            \noexpand\endinput}\fi}
\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}
\protected\def\alloc:patch#1{%
    \expandafter\expandafter\expandafter\let
        \expandafter\expandafter\csname\csstring#1:ltluatex:new\endcsname
            \csname new\csstring#1\endcsname
    \expandafter\edef\csname new\csstring#1\endcsname##1{%
        \noexpand\minim:rememberalloc{\csstring#1}{\noexpand\csstring##1}%
        \expandafter\noexpand\csname\csstring#1:ltluatex:new\endcsname{##1}}}

% 1 allocation

% 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

% new allocation macros

\def\alloc:makenew#1#2#3{%
    \unless\ifcsname#3\endcsname
        \expandafter\newcount \csname#3\endcsname
        \csname#3\endcsname 0\fi
    \ifcsname new\csstring#1\endcsname
        \alloc:patch#1\else
        \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}%
    % minim:rememberalloc will be defined from lua side
    \minim:rememberalloc{\csstring#1}{\csstring#4}}

% 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}
\csname e@alloc@ccodetable@count\endcsname = 4 % because ltluatex allocates 4...

% 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}'}}

% patch in remembering box allocations
\def\minim:boxchardef#1{%
    \minim:rememberalloc{box}{\csstring#1}%
    \mathchardef#1}
\edef\newbox{%
    \expandafter\noexpand\csname et@xglob\endcsname
    4\box\noexpand\minim:boxchardef}

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

% 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