summaryrefslogtreecommitdiff
path: root/macros/luatex/generic/minim/minim-etex.tex
blob: bdf71def16e5166420ff4a5ada5e9d9eefddc490 (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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413

% This is a stripped-down version of etex.src, meant as companion to 
% minim-plain.tex.
%
% It follows e-TeX V2.1;0-luatex2.
%
% The following has been removed:
%  - Module loading
%  - Version checks
%
% The following has been retained:
%  - Language handling (somewhat simplified)
%  - Tracing and logging extensions
%  - The \eTeX symbol
%  - Register allocation

\catcode `\@=\catcode `\A

% Define the language-handling commands

\def \uselanguage #1%
    {\ifcsname lang@#1\endcsname
        \language=\csname lang@#1\endcsname
        \lefthyphenmin=\csname lhm@#1\endcsname
        \righthyphenmin=\csname rhm@#1\endcsname
          \ifcsname lu@texhyphen@loaded@\the\language\endcsname \else
            \global\expandafter\def\csname
              lu@texhyphen@loaded@\the\language\endcsname{}%
            \directlua{
              if not status.ini_version then
                require("luatex-hyphen")
                luatexhyphen.loadlanguage("\luatexluaescapestring{#1}",
                \the\language)
              end}%
          \fi
        \ifdefined\uselanguage@hook \uselanguage@hook{#1}\fi
     \else
        \begingroup\errorcontextlines=0\relax
        \errhelp {You are trying to use a language which has not previously
                  been defined; remember that any language you want to use
                  will need to have been specified at the time the format
                  was created.%
                 }%
        \errmessage {Language #1 undefined.}\endgroup
     \fi}

\def \addlanguage #1#2#3#4#5{% language patterns exceptions lhm rhm
    \expandafter\newlanguage \csname lang@#1\endcsname
    \expandafter\chardef \csname lhm@#1\endcsname=#4
    \expandafter\chardef \csname rhm@#1\endcsname=#5
    \csname if\directlua{% do we want to skip this language now?
          require("luatex-hyphen")
          if (luatexhyphen.lookupname("\luatexluaescapestring{#1}") == nil)
              or (\number\csname lang@#1\endcsname == 0) then
            tex.sprint("false") % no, load it (execute 'else' part)
          else
            tex.sprint("true") % yes, skip
          end}\endcsname
    \else
        \uselanguage {#1}%
        \input #2
        \if *#3*\else \input #3 \fi
        \edef \fmtversion {\fmtversion/#1}%
    \fi
    \ifdefined\addlanguage@hook \addlanguage@hook{#1}\fi}

% Decrement \count 19, because \newlanguage will increment it again

\advance \count 19 by -1

% We are about to try to process a user/site-specific file "language.def",
% which establishes a Babel-like language selection environment.

\input language.def

% All that was just to set up natural language handling...
% The "real" work of "etex.src", however, is to augment the non-primitives
% of Plain.TeX to incorporate e-TeX specific features, and to add new
% non-primitives to simplify access to new e-TeX specific primitives.

\message {Augmenting the Plain TeX definitions:}
\message {\string \tracingall;}

\def \tracingall
    {\tracingonline=\@ne
     \tracingcommands=\thr@@ % plain.tex has \tw@
     \tracingstats=\tw@
     \tracingpages=\@ne
     \tracingoutput=\@ne
     \tracinglostchars=\tw@ % plain.tex has \@ne
     \tracingmacros=\tw@
     \tracingparagraphs=\@ne
     \tracingrestores=\@ne
     \showboxbreadth=\maxdimen
     \showboxdepth=\maxdimen
     \errorstopmode
     \tracinggroups=\@ne
     \tracingifs=\@ne
     \tracingscantokens=\@ne
     \tracingnesting=\@ne
     \tracingassigns=\tw@
     }

\message {Adding new e-TeX definitions:}

\message {\string \eTeX,}
\def \eTeX {$\varepsilon$-\TeX} %%% the simple version, not suitable for maths;

\message {\string \loggingall,}
\def \loggingall {\tracingall \tracingonline=\z@}

\message {\string \tracingnone,}
\def \tracingnone
    {\tracingassigns=\z@
     \tracingnesting=\z@
     \tracingscantokens=\z@
     \tracingifs=\z@
     \tracinggroups=\z@
     \showboxdepth=\thr@@
     \showboxbreadth=5
     \tracingrestores=\z@
     \tracingparagraphs=\z@
     \tracingmacros=\z@
     \tracinglostchars=\@ne
     \tracingoutput=\z@
     \tracingpages=\z@
     \tracingstats=\z@
     \tracingcommands=\z@
     \tracingonline=\z@
    }

\message {register allocation;}

\newcount \et@xins % our insertion counter (\insc@unt is used differently)

% We have to adjust the Plain TeX register allocation counts for our
% slightly modified book-keeping:

\advance \count 10 by 1 % \count 10=23 % allocates \count registers 23, 24, ...
\advance \count 11 by 1 % \count 11=10 % allocates \dimen registers 10, 11, ...
\advance \count 12 by 1 % \count 12=10 % allocates \skip registers 10, 11, ...
\advance \count 13 by 1 % \count 13=10 % allocates \muskip registers 10, 11, ...
\advance \count 14 by 1 % \count 14=10 % allocates \box registers 10, 11, ...
\advance \count 15 by 1 % \count 15=10 % allocates \toks registers 10, 11, ...
\advance \count 16 by 1 % \count 16=0  % allocates input streams 0, 1, ...
\advance \count 17 by 1 % \count 17=0  % allocates output streams 0, 1, ...
\advance \count 18 by 1 % \count 18=4  % allocates math families 4, 5, ...
\advance \count 19 by 1 % \count 19=0  % allocates \language codes 0, 1, ...

\et@xins=\insc@unt % \et@xins=255 % allocates insertions 254, 253, ...

% We don't change the Plain TeX definitions of \newcount, etc., but the
% \alloc@ macro doing the actual work is redefined.

% When the normal register pool for \count, \dimen, \skip, \muskip,
% \box, or \toks registers is exhausted, we switch to the extended pool.

\def \alloc@ #1#2#3#4#5%
    {\ifnum \count 1#1 < #4% make sure there's still room
     	 \allocationnumber=\count 1#1
	 \global \advance \count 1#1 by \@ne
	 \global #3#5=\allocationnumber
	 \wlog {\string #5=\string #2\the \allocationnumber}%
     \else \ifnum #1 < 6
    		  \begingroup \escapechar=\m@ne
		  \expandafter \alloc@@ \expandafter {\string #2}#5%
	   \else	
	 	  \errmessage {No room for a new #2}%
	   \fi
     \fi
  }

% The \expandafter construction used here allows the generation of
% \newcount and \globcount from #1=count. Moreover (and more important)
% this construction avoids the appearance of \outer macros inside
% macro definitions or conditionals.

\def \alloc@@ #1#2%
    {\endgroup % restore \escapechar
     \message {Normal \csname#1\endcsname register pool exhausted,
						switching to extended pool.}%
     \global \expandafter 
     \let \csname new#1\expandafter \endcsname \csname glob#1\endcsname
     \csname new#1\endcsname#2%
    }

% We do change the Plain TeX definition of \newinsert

\outer \def \newinsert #1%make sure there's still room for ...
      {\ch@ck 0 \et@xins \count % ... a \count, ...
        {\ch@ck 1 \et@xins \dimen % ... \dimen, ...
     	  {\ch@ck 2 \et@xins \skip % ... \skip, ...
       	    {\ch@ck 4 \et@xins \box % ... and \box register
              {\global \advance \et@xins by \m@ne
	       \unless 
               \ifnum \insc@unt < \et@xins 
	      	      \global \insc@unt=\et@xins 
	       \fi
	       \allocationnumber=\et@xins
	       \global \chardef #1=\allocationnumber
	       \wlog {\string #1=\string \insert \the \allocationnumber}%
	      }%
            }%
          }%
        }%
      }

\def \ch@ck #1#2#3#4%
    {\ifnum \count 1#1 < #2#4\else \errmessage {No room for a new #3}\fi}

% And we define \reserveinserts, so that you can say \reserveinserts{17} 
% in order to reserve room for up to 17 additional insertion classes that will
% not be taken away by \newcount, \newdimen, \newskip, or \newbox.

\outer \def \reserveinserts#1%
      {\global \insc@unt=\numexpr \et@xins \ifnum #1 > \z@ -#1\fi \relax}

\message {extended register allocation;}

% Now, we define \globcount, \globbox, etc., so that you can say
% \globcount\foo and \foo will be defined (with \countdef) to be the
% next count register from the vastly larger but somewhat less efficient
% extended register pool. We also define \loccount, etc., but these
% register definitions are local to the current group.

\count 260=277     % globally allocates \count registers 277, 278, ...
\count 261=\@cclvi % globally allocates \dimen registers 256, 257, ...
\count 262=\@cclvi % globally allocates \skip registers 256, 257, ...
\count 263=\@cclvi % globally allocates \muskip registers 256, 257, ...
\count 264=\@cclvi % globally allocates \box registers 256, 257, ...
\count 265=\@cclvi % globally allocates \toks registers 256, 257, ...
\count 266=\@ne    % globally allocates \marks classes 1, 2, ...

\def \et@xmaxregs {32768}

\count 270=\et@xmaxregs % locally allocates \count registers 32767, 32766, ...
\count 271=\et@xmaxregs % ditto for \dimen registers
\count 272=\et@xmaxregs % ditto for \skip registers
\count 273=\et@xmaxregs % ditto for \muskip registers
\count 274=\et@xmaxregs % ditto for \box registers
\count 275=\et@xmaxregs % ditto for \toks registers
\count 276=\et@xmaxregs % ditto for \marks classes

% \count registers 256-259 and 267-269 are not (yet) used

\outer \def \globcount  {\et@xglob 0 \count  \countdef}
       \def \loccount   {\et@xloc  0 \count  \countdef}
\outer \def \globdimen  {\et@xglob 1 \dimen  \dimendef}
       \def \locdimen   {\et@xloc  1 \dimen  \dimendef}
\outer \def \globskip   {\et@xglob 2 \skip   \skipdef}
       \def \locskip    {\et@xloc  2 \skip   \skipdef}
\outer \def \globmuskip {\et@xglob 3 \muskip \muskipdef}
       \def \locmuskip  {\et@xloc  3 \muskip \muskipdef}
\outer \def \globbox    {\et@xglob 4 \box    \mathchardef}
       \def \locbox     {\et@xloc  4 \box    \mathchardef}
\outer \def \globtoks   {\et@xglob 5 \toks   \toksdef}
       \def \loctoks    {\et@xloc  5 \toks   \toksdef}
\outer \def \globmarks  {\et@xglob 6 \marks  \mathchardef}
       \def \locmarks   {\et@xloc  6 \marks  \mathchardef}

\let\newmark=\globmarks %%% 2.0;14
\let\newmarks=\globmarks

\def \et@xglob #1#2#3#4%
    {\et@xchk #1#2% make sure there's still room
    	{\allocationnumber=\count 26#1
	 \global \advance \count 26#1 by \@ne
	 \global #3#4=\allocationnumber
	 \wlog {\string #4=\string #2\the \allocationnumber}%
        }%
 }

\def \et@xloc#1#2#3#4%
    {\et@xchk #1#2% make sure there's still room
        {\advance \count 27#1 by \m@ne
         \allocationnumber=\count 27#1
 	 #3#4=\allocationnumber 
	 \wlog {\string #4=\string #2\the \allocationnumber \space (local)}%
       }%
 }

\def \et@xchk #1#2#3%
    {\ifnum \count 26#1 < \count 27#1 
        #3%
     \else 
	\errmessage {No room for a new #2}%
     \fi
    }

% Next we define \globcountblk, \loccountblk, etc., so that one can
% write \globcountblk\foo{17} and \foo will be defined (with \mathchardef)
% as the first (the zeroth?) of a block of 17 consecutive registers.
% Thus the user is intended to reference elements <\foo+0> to <\foo+n-1>,
% where n is the length of the block allocated.

\outer \def \globcountblk  {\et@xgblk 0 \count}
       \def \loccountblk   {\et@xlblk 0 \count}
\outer \def \globdimenblk  {\et@xgblk 1 \dimen}
       \def \locdimenblk   {\et@xlblk 1 \dimen}
\outer \def \globskipblk   {\et@xgblk 2 \skip}
       \def \locskipblk    {\et@xlblk 2 \skip}
\outer \def \globmuskipblk {\et@xgblk 3 \muskip}
       \def \locmuskipblk  {\et@xlblk 3 \muskip}
\outer \def \globboxblk    {\et@xgblk 4 \box}
       \def \locboxblk     {\et@xlblk 4 \box}
\outer \def \globtoksblk   {\et@xgblk 5 \toks}
       \def \loctoksblk    {\et@xlblk 5 \toks}
\outer \def \globmarksblk  {\et@xgblk 6 \marks}
       \def \locmarksblk   {\et@xlblk 6 \marks}

% And, both to provide a higher-level interface to the block allocation
% scheme, and to demonstrate possible applications of the new \...expr
% primitives, we also define \globcountvector and \loccountvector (etc)
% which allow the user to access elements with embedded arithmetic, as in

% \globcountvector \foo {12}
% \foo {\count 0 + 3} = \foo {\count 1 * 2}

\outer \def \globcountvector  {\et@xgvec 0 \count}
       \def \loccountvector   {\et@xlvec 0 \count}
\outer \def \globdimenvector  {\et@xgvec 1 \dimen}
       \def \locdimenvector   {\et@xlvec 1 \dimen}
\outer \def \globskipvector   {\et@xgvec 2 \skip}
       \def \locskipvector    {\et@xlvec 2 \skip}
\outer \def \globmuskipvector {\et@xgvec 3 \muskip}
       \def \locmuskipvector  {\et@xlvec 3 \muskip}
\outer \def \globboxvector    {\et@xgvec 4 \box}
       \def \locboxvector     {\et@xlvec 4 \box}
\outer \def \globtoksvector   {\et@xgvec 5 \toks}
       \def \loctoksvector    {\et@xlvec 5 \toks}
\outer \def \globmarksvector  {\et@xgvec 6 \marks}
       \def \locmarksvector   {\et@xlvec 6 \marks}

\def \et@xgblk #1#2#3#4%
    {\et@xchkblk #1#2{#4}% make sure there's still room
        {\allocationnumber=\count 26#1
         \global \advance \count 26#1 by #4%
         \global \mathchardef #3=\allocationnumber
         \wlog {\string #3=\string #2blk{\number #4} at \the \allocationnumber}%
      }%
    }

\def \et@xlblk #1#2#3#4%
    {\et@xchkblk #1#2{#4}% make sure there's still room
        {\advance \count 27#1 by -#4%
         \allocationnumber=\count 27#1
         \mathchardef #3=\allocationnumber
         \wlog {\string #3=\string #2blk{\number #4} 
       		at \the \allocationnumber \space (local)%
       	       }%
      }%
    }

\begingroup
\catcode `\# = 12
\gdef \et@xhash {#}
\endgroup

\def \et@xgvec #1#2#3#4%
    {\et@xchkblk #1#2{#4}% make sure there's still room
        {\allocationnumber=\count 26#1
         \global \advance \count 26#1 by #4%
         \ifx    #2\box
                 \def \2{}%
         \else   \ifx #2\marks
                      \def \2{}%
                 \else
                      \def \2{\string #2}%
                 \fi
         \fi
         \xdef #3##1{\2 \noexpand \numexpr \the \allocationnumber+##1\relax}%
         \wlog {\string #3 {\et@xhash 1} =
                  \2 {\the \allocationnumber+\et@xhash 1} (global #2 vector)%
      	     }%
      }%
    }

\def \et@xlvec #1#2#3#4%
    {\et@xchkblk #1#2{#4}% make sure there's still room
        {\advance \count 27#1 by -#4%
 	 \allocationnumber=\count27#1 
	 \mathchardef #3=\allocationnumber
	 \ifx  #2\box
               \def \2{}%
	 \else \ifx #2\marks
               	   \def \2{}%
               \else
               	   \def \2{\string #2}%
               \fi
	 \fi
	 \edef #3##1{\2 \noexpand \numexpr \the \allocationnumber+##1\relax}%
	 \wlog {\string #3 {\et@xhash 1} =
                   \2 {\the \allocationnumber+\et@xhash 1} (local #2 vector)%
               }%
        }%
    }

\def \et@xchkblk #1#2#3#4%
    {\ifnum #3 < \z@
     	   \errmessage {Negative register block size \number #3}%
     \else \ifnum \numexpr \count 26#1+#3 > \count 27#1
     		\errmessage {No room for new #2block of size \number#3}%
     	   \else 
	 	 #4%
     	   \fi 
     \fi
    }

% And finally re-catcode commercial-at

\catcode `\@=12