| 1 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 2 |
% |
| 3 |
% Module: ZzTeX Register Facilities |
| 4 |
% |
| 5 |
% Synopsis: This module provides facilities for defining and manipulating |
| 6 |
% TeX registers. |
| 7 |
% |
| 8 |
% Author: Paul C. Anagnostopoulos |
| 9 |
% Created: 13 August 1989 |
| 10 |
% |
| 11 |
% Copyright 1989--2020 by Paul C. Anagnostopoulos |
| 12 |
% under The MIT License (opensource.org/licenses/MIT) |
| 13 |
% |
| 14 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 15 |
|
| 16 |
% Register Definition |
| 17 |
% -------- ---------- |
| 18 |
|
| 19 |
|
| 20 |
% The following constant specifies the number of the first insert register |
| 21 |
% set. Such a set is composed of a count, dimen, skip, and box. |
| 22 |
|
| 23 |
\chardef \zfirstinsert = 245 |
| 24 |
|
| 25 |
% Establish counters to keep track of the most recently allocated |
| 26 |
% registers. The registers are initialized to the first available |
| 27 |
% register. |
| 28 |
|
| 29 |
\count10 = 20 % Count registers. |
| 30 |
\count11 = 0 % Dimen registers. |
| 31 |
\count12 = 0 % Skip registers. |
| 32 |
\count13 = 0 % Muskip registers. |
| 33 |
\count14 = 1 % Box registers (Plain TeX uses box 0). |
| 34 |
\count15 = 0 % Toks registers. |
| 35 |
\count16 = 0 % Read streams. |
| 36 |
\count17 = 0 % Write streams. |
| 37 |
\count18 = 0 % Math families. |
| 38 |
\count19 = \zfirstinsert % Insertion register sets. |
| 39 |
|
| 40 |
|
| 41 |
% There is one macro for each type of register, used to allocate one. |
| 42 |
|
| 43 |
\def \definecount #1#2{% {\name}{initial} |
| 44 |
\zalloc{\count}{10}{\zfirstinsert}{\countdef}{#1}% |
| 45 |
#1 = #2} |
| 46 |
|
| 47 |
\def \definedimen #1#2{% {\name}{initial} |
| 48 |
\zalloc{\dimen}{11}{\zfirstinsert}{\dimendef}{#1}% |
| 49 |
#1 = #2} |
| 50 |
|
| 51 |
\def \defineskip #1#2{% {\name}{initial} |
| 52 |
\zalloc{\skip}{12}{\zfirstinsert}{\skipdef}{#1}% |
| 53 |
#1 = #2} |
| 54 |
|
| 55 |
\def \definemuskip #1#2{% {\name}{initial} |
| 56 |
\zalloc{\muskip}{13}{255}{\muskipdef}{#1}% Muskip 255 is scratch. |
| 57 |
#1 = #2} |
| 58 |
|
| 59 |
\def \definebox #1{% {\name} |
| 60 |
\zalloc{\box}{14}{\zfirstinsert}{\chardef}{#1}} |
| 61 |
|
| 62 |
\def \definetoks #1{% {\name} |
| 63 |
\zalloc{\toks}{15}{256}{\toksdef}{#1}% |
| 64 |
#1 = {}} |
| 65 |
|
| 66 |
\def \defineread #1{% {\name} |
| 67 |
\zalloc{\read}{16}{16}{\chardef}{#1}} |
| 68 |
|
| 69 |
\def \definewrite #1{% {\name} |
| 70 |
\zalloc{\write}{17}{16}{\chardef}{#1}} |
| 71 |
|
| 72 |
% Families are not defined directly by the user, but via \definetypestyle. |
| 73 |
|
| 74 |
\def \zdeffam #1{% {\name} |
| 75 |
\if \lssp{\count18}{15}% |
| 76 |
\zalloc{\fam}{18}{16}{\chardef}{#1}% |
| 77 |
\else |
| 78 |
\global\chardef #1=\count18 |
| 79 |
\fi} |
| 80 |
|
| 81 |
\def \defineinsert #1{% {\name} |
| 82 |
\zalloc{\insert}{19}{255}{\chardef}{#1}} % Registers 255 are scratch. |
| 83 |
|
| 84 |
|
| 85 |
\def \zalloc #1#2#3#4#5{% {type}{counter}{limit}{\def-er}{\name} |
| 86 |
\if \eqlp{\count#2}{#3}% |
| 87 |
\error{noregsleft}{All `\string#1' registers have been allocated}% |
| 88 |
\fi |
| 89 |
\if \definedp{#5}% |
| 90 |
\error{dupreg}{The register `\string#5' is already defined}% |
| 91 |
\fi |
| 92 |
\global#4 #5=\count#2 |
| 93 |
\global\increment {\count#2}} |
| 94 |
|
| 95 |
% Register Statistics |
| 96 |
% -------- ---------- |
| 97 |
|
| 98 |
|
| 99 |
{\catcode`\_ = \catactive |
| 100 |
|
| 101 |
\gdef \zregstats {% |
| 102 |
{\def _{\space}% |
| 103 |
\writelog{}% |
| 104 |
\writelog{Register Statistics:}% |
| 105 |
\writelog{}% |
| 106 |
\writelog{Type_____Used / Total}% |
| 107 |
\writelog{----_____------------}% |
| 108 |
\zrsone{Count_}{10}{\zfirstinsert}{}% |
| 109 |
\zrsone{Dimen_}{11}{\zfirstinsert}{}% |
| 110 |
\zrsone{Skip__}{12}{\zfirstinsert}{}% |
| 111 |
\zrsone{Muskip}{13}{255}{}% |
| 112 |
\zrsone{Box___}{14}{\zfirstinsert}{}% |
| 113 |
\zrsone{Toks__}{15}{256}{}% |
| 114 |
\zrsone{Read__}{16}{16}{}% |
| 115 |
\zrsone{Write_}{17}{16}{}% |
| 116 |
\zrsone{Family}{18}{256}{(15 for math)}% |
| 117 |
\zrsone{Insert}{19}{255}{(starts at \the\zfirstinsert)}% |
| 118 |
\writelog{}}} |
| 119 |
|
| 120 |
\gdef \zrsone #1#2#3#4{% {name}{counter}{ex-limit}{comment} |
| 121 |
\tcounta = #3 |
| 122 |
\advance \tcounta by -1 |
| 123 |
\writelog{#1___\the\count#2 _/_\the\tcounta_#4}} |
| 124 |
|
| 125 |
} % \catcode |
| 126 |
|
| 127 |
% Constant Registers |
| 128 |
% -------- --------- |
| 129 |
|
| 130 |
|
| 131 |
\definecount{\maxcount}{2147483647} |
| 132 |
\definecount{\mincount}{-\maxcount} |
| 133 |
\zremovePlaindef \maxdimen |
| 134 |
\definedimen{\maxdimen}{16383.99999pt} |
| 135 |
\definedimen{\mindimen}{-\maxdimen} |
| 136 |
\definedimen{\naturalwidth}{\mindimen} |
| 137 |
|
| 138 |
\defineskip{\centerindent}{0pt plus 10000pt} |
| 139 |
\let \centering = \centerindent |
| 140 |
|
| 141 |
\defineskip{\normalparfillskip}{0pt plus 1fil} |
| 142 |
|
| 143 |
\definebox{\voidbox} % Permanently void box register. |
| 144 |
|
| 145 |
% Temporary Internal Registers |
| 146 |
% --------- -------- --------- |
| 147 |
|
| 148 |
|
| 149 |
\definecount{\tcounta}{0} |
| 150 |
\definecount{\tcountb}{0} |
| 151 |
\definecount{\tcountc}{0} |
| 152 |
|
| 153 |
\definedimen{\tdimena}{0pt} |
| 154 |
\definedimen{\tdimenb}{0pt} |
| 155 |
\definedimen{\tdimenc}{0pt} |
| 156 |
\definedimen{\tdimend}{0pt} |
| 157 |
|
| 158 |
\defineskip{\zskipa}{0pt} |
| 159 |
\defineskip{\zskipb}{0pt} |
| 160 |
\defineskip{\zskipc}{0pt} |
| 161 |
|
| 162 |
\definemuskip{\zmuskipa}{0mu} |
| 163 |
\definemuskip{\zmuskipb}{0mu} |
| 164 |
\definemuskip{\zmuskipc}{0mu} |
| 165 |
|
| 166 |
\definebox{\zboxa} |
| 167 |
\definebox{\zboxb} |
| 168 |
\definebox{\zboxc} |
| 169 |
|
| 170 |
\definetoks{\ztoksa} |
| 171 |
\definetoks{\ztoksb} |
| 172 |
\definetoks{\ztoksc} |
| 173 |
|
| 174 |
\defineread{\zreada} |
| 175 |
|
| 176 |
\definewrite{\zwritea} |
| 177 |
|
| 178 |
% Manipulating Boxes |
| 179 |
% ------------ ----- |
| 180 |
|
| 181 |
|
| 182 |
\def \shiftboxbaseline #1#2{% {\box}{amount} |
| 183 |
{\tdimena = #2% |
| 184 |
\tdimenb = \ht#1% |
| 185 |
\advance \tdimenb by \tdimena |
| 186 |
\ht#1= \tdimenb |
| 187 |
\tdimenb = \dp#1% |
| 188 |
\advance \tdimenb by -\tdimena |
| 189 |
\dp#1= \tdimenb}} |
| 190 |
|
| 191 |
% Lists |
| 192 |
% ----- |
| 193 |
|
| 194 |
|
| 195 |
% These macros support the manipulation of lists in the following format: |
| 196 |
% |
| 197 |
% \:{<element-0>}\:{<element-1>}\: ... |
| 198 |
% |
| 199 |
% All lists are global! |
| 200 |
|
| 201 |
% We need two toks registers, which will be used just for lists. |
| 202 |
|
| 203 |
\definetoks{\zlta} |
| 204 |
\definetoks{\zltb} |
| 205 |
|
| 206 |
|
| 207 |
\def \setlist #1=#2{% \listname = {value} |
| 208 |
\zlta = \expandafter{#2}% |
| 209 |
\xdef #1{\the\zlta}} |
| 210 |
|
| 211 |
\long\def \append #1#2{% {item}{\list-name} |
| 212 |
{\zlta = {\:{#1}}% |
| 213 |
\zltb = \expandafter{#2}% |
| 214 |
\xdef #2{\the\zltb\the\zlta}}} |
| 215 |
|
| 216 |
\long\def \appendlf #1#2{% {\list-name}{item} |
| 217 |
{\zlta = {\:{#2}}% |
| 218 |
\zltb = \expandafter{#1}% |
| 219 |
\xdef #1{\the\zltb\the\zlta}}} |
| 220 |
|
| 221 |
\long\def \prepend #1#2{% {item}{\list-name} |
| 222 |
{\zlta = {\:{#1}}% |
| 223 |
\zltb = \expandafter{#2}% |
| 224 |
\xdef #2{\the\zlta\the\zltb}}} |
| 225 |
|
| 226 |
\let \push = \prepend |
| 227 |
|
| 228 |
\def \pop #1#2{% {\return-macro}{\list-name} |
| 229 |
\expandafter\zpop #2\zmark#1#2} |
| 230 |
|
| 231 |
\long\def \zpop \:#1#2\zmark#3#4{% |
| 232 |
\def #3{#1}% |
| 233 |
\gdef #4{#2}} |
| 234 |
|
| 235 |
\def \listtop #1#2{% {\return-macro}{\list-name} |
| 236 |
\expandafter\zlisttop #2\zmark#1} |
| 237 |
|
| 238 |
\long\def \zlisttop \:#1#2\zmark#3{% |
| 239 |
\def #3{#1}} |
| 240 |
|
| 241 |
\definecount{\zmapldepth}{0} |
| 242 |
|
| 243 |
\def \maplist #1#2{% {body}{\listname} |
| 244 |
\increment \zmapldepth |
| 245 |
\withname\let{\zmapl\romannumeral\zmapldepth}=\:% |
| 246 |
\long\def \:##1{#1}% |
| 247 |
#2% |
| 248 |
\expandafter\let |
| 249 |
\expandafter\:\csname zmapl\romannumeral\zmapldepth\endcsname |
| 250 |
\decrement \zmapldepth} |
| 251 |
|
| 252 |
\def \maplistlf #1#2{% {\listname}{body} |
| 253 |
\increment \zmapldepth |
| 254 |
\withname\let{\zmapl\romannumeral\zmapldepth}=\:% |
| 255 |
\long\def \:##1{#2}% |
| 256 |
#1% |
| 257 |
\expandafter\let |
| 258 |
\expandafter\:\csname zmapl\romannumeral\zmapldepth\endcsname |
| 259 |
\decrement \zmapldepth} |
| 260 |
|
| 261 |
\def \exitmaplist {\def \:##1{}} |
| 262 |
|
| 263 |
\def \listlength #1#2{% {\result-count}{\list} |
| 264 |
#1= 0 |
| 265 |
\maplist{\increment #1}{#2}} |
| 266 |
|
| 267 |
\def \member #1#2#3{% {\result-flag}{item}{\list} |
| 268 |
\setflag #1= \false |
| 269 |
\maplist{\stringeql{#1}{##1}{#2}\if #1\exitmaplist \fi} |
| 270 |
{#3}} |
| 271 |
|
| 272 |
\def \commalist #1#2{% {\result-name}{comma-list} |
| 273 |
\setlist #1={}% |
| 274 |
\expandafter\zcomma #2,\zmark#1} |
| 275 |
|
| 276 |
\def \zcomma #1,#2\zmark#3{% |
| 277 |
\if \notp{\emptyargp{#1}}\append{#1}{#3}\fi |
| 278 |
\if \notp{\emptyargp{#2}}% |
| 279 |
\def \znext {\zcomma #2\zmark#3}% |
| 280 |
\else |
| 281 |
\let \znext = \relax |
| 282 |
\fi |
| 283 |
\znext} |
| 284 |
|
| 285 |
\def \inclusionlist #1#2#3{% {\result}{item}{inclusion-list} |
| 286 |
\zincla {#1}{#2}#3\zmark} |
| 287 |
|
| 288 |
\let \all = \relax |
| 289 |
\let \allbut = \relax |
| 290 |
\def \zallname {\all} |
| 291 |
\def \zallbutname {\allbut} |
| 292 |
|
| 293 |
\def \zincla #1#2#3#4\zmark{% {\result}{item}{token1}{tokens...} |
| 294 |
\def \znext {#3}% |
| 295 |
\if \tokeqlp{\znext}{\zallname}% |
| 296 |
\setflag #1= \true |
| 297 |
\else \if \tokeqlp{\znext}{\zallbutname}% |
| 298 |
\commalist{\zincllist}{#4}% |
| 299 |
\member{#1}{#2}{\zincllist}% |
| 300 |
\setflag #1= {\notp{#1}}% |
| 301 |
\else |
| 302 |
\commalist{\zincllist}{#3#4}% |
| 303 |
\member{#1}{#2}{\zincllist}% |
| 304 |
\fi\fi} |
| 305 |
|
| 306 |
% New Expression Evaluation |
| 307 |
% --- ---------- ---------- |
| 308 |
|
| 309 |
|
| 310 |
% These macros provide a rudimentary expression evaluation facility. |
| 311 |
% The accumulator can be a count, dimen, or skip register. The expression |
| 312 |
% is in the form {operand0,op1,operand1,op2,operand2,...}. |
| 313 |
% |
| 314 |
% WARNING: The \register cannot be used in the expression except possibly |
| 315 |
% as the first operand. |
| 316 |
|
| 317 |
|
| 318 |
\definedimen{\zcalct}{0pt} |
| 319 |
|
| 320 |
|
| 321 |
\def \calculate #1=#2{% \register = {expression} |
| 322 |
\let \zaccum = #1% |
| 323 |
\zcalca =,#2,;,,\zmark} |
| 324 |
|
| 325 |
\def \gcalculate #1=#2{% \register = {expression} |
| 326 |
\let \zaccum = #1% |
| 327 |
\zcalca =,#2,;,,\zmark |
| 328 |
\global \zaccum = \zaccum} |
| 329 |
|
| 330 |
\def \zcalca #1,#2,#3\zmark{% |
| 331 |
\def \znext{\zcalca #3\zmark}% |
| 332 |
\if #1=\zaccum = #2% % Assignment |
| 333 |
\else\if #1+\advance \zaccum #2% % Addition |
| 334 |
\else\if #1-\advance \zaccum -#2% % Subtraction |
| 335 |
\else\if #1*\multiply \zaccum #2% % Multiplication |
| 336 |
\else\if #1/\divide \zaccum #2% % Division |
| 337 |
\else\if #1N\if \dimlssp{#2}{\zaccum}\zaccum = #2\fi % Minimum |
| 338 |
\else\if #1P\divide \zaccum 100 % Percentage |
| 339 |
\expandafter\zaccum #2\zaccum |
| 340 |
\else\if #1R\zcalct = #2% % Ratio |
| 341 |
\divide \zcalct by 4096 |
| 342 |
\divide \zaccum by \zcalct |
| 343 |
\multiply \zaccum by 16\relax |
| 344 |
\else\if #1S\zaccum = \thefactor#2\zaccum % Scale |
| 345 |
\else\if #1X\if \dimgtrp{#2}{\zaccum}\zaccum = #2\fi % Maximum |
| 346 |
\else\if #1;\let \znext = \relax % Done |
| 347 |
\else |
| 348 |
\error{invoper}{Invalid operator `#1' in expression}% |
| 349 |
\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi |
| 350 |
\znext} |
| 351 |
|
| 352 |
% Calculating Values |
| 353 |
% ----------- ------ |
| 354 |
|
| 355 |
%%%\defineskip{\zcaccum}{0pt} |
| 356 |
%%%\setflag \zexprpos = \true |
| 357 |
%%% |
| 358 |
%%%\def \calculate #1=#2{% {\reg} = {expression} |
| 359 |
%%% \commalist{\zexprlist}{#2}% |
| 360 |
%%% \zcaccum = 0pt |
| 361 |
%%% \def \zoper {+}% |
| 362 |
%%% \setflag \zexprpos = \true |
| 363 |
%%% \maplist{\zcalc{##1}}{\zexprlist}% |
| 364 |
%%% #1=\zcaccum} |
| 365 |
%%% |
| 366 |
%%%\def \gcalculate #1=#2{% {\reg} = {expression} |
| 367 |
%%% \commalist{\zexprlist}{#2}% |
| 368 |
%%% \zcaccum = 0pt |
| 369 |
%%% \def \zoper {+}% |
| 370 |
%%% \setflag \zexprpos = \true |
| 371 |
%%% \maplist{\zcalc{##1}}{\zexprlist}% |
| 372 |
%%% \global #1=\zcaccum} |
| 373 |
%%% |
| 374 |
%%%\def \zcalc #1{% {token} |
| 375 |
%%% \if \zexprpos |
| 376 |
%%% \zcalcb{#1}% |
| 377 |
%%% \setflag \zexprpos = \false |
| 378 |
%%% \else |
| 379 |
%%% \def \zoper {#1}% |
| 380 |
%%% \setflag \zexprpos = \true |
| 381 |
%%% \fi} |
| 382 |
%%% |
| 383 |
%%%\def \zcalcb #1{% {operand} |
| 384 |
%%% \if \codeeqlp{\zoper}{+}% % Add. |
| 385 |
%%% \advance \zcaccum by #1% |
| 386 |
%%% \else\if \codeeqlp{\zoper}{-}% % Subtract. |
| 387 |
%%% \advance \zcaccum by -#1% |
| 388 |
%%% \else\if \codeeqlp{\zoper}{*}% % Multiply. |
| 389 |
%%% \multiply \zcaccum by #1% |
| 390 |
%%% \else\if \codeeqlp{\zoper}{/}% % Divide. |
| 391 |
%%% \divide \zcaccum by #1% |
| 392 |
%%% \else\if \codeeqlp{\zoper}{P}% |
| 393 |
%%% \divide \zcaccum by 100 |
| 394 |
%%% \expandafter \zcaccum #1\zcaccum |
| 395 |
%%% \else\if \codeeqlp{\zoper}{R}% % Compute ratio (in points). |
| 396 |
%%% \zcalct = #1% |
| 397 |
%%% \divide \zcalct by 4096 |
| 398 |
%%% \divide \zcaccum by \zcalct |
| 399 |
%%% \multiply \zcaccum by 16\relax |
| 400 |
%%% \else\if \codeeqlp{\zoper}{S}% % Scale (in points). |
| 401 |
%%% \zcaccum = \thefactor#1\zcaccum |
| 402 |
%%% \else |
| 403 |
%%% \error{invoper}{Invalid operator `\zoper' in expression}% |
| 404 |
%%% \fi\fi\fi\fi\fi\fi\fi} |
| 405 |
|
| 406 |
% The Vertical Context |
| 407 |
% --- -------- ------- |
| 408 |
|
| 409 |
|
| 410 |
% This is in here because we need registers. Otherwise it would be |
| 411 |
% in ZZTEX.TEX |
| 412 |
|
| 413 |
|
| 414 |
\definecount{\zvcontextdepth}{0} |
| 415 |
|
| 416 |
\definetoks{\zeveryvcontext} |
| 417 |
\global\zeveryvcontext = {} |
| 418 |
|
| 419 |
|
| 420 |
\def \zdeclareeveryvcontext #1{% |
| 421 |
\global\zeveryvcontext = \expandafter{\the\zeveryvcontext #1}} |
| 422 |
|
| 423 |
\def \zpushvcontext {% |
| 424 |
\global\increment \zvcontextdepth |
| 425 |
\withname\gdef{\zvcontextstack\romannumeral\zvcontextdepth}{}% |
| 426 |
\the\zeveryvcontext} |
| 427 |
|
| 428 |
\def \zpopvcontext {% |
| 429 |
{\globaldefs = 1\relax |
| 430 |
\name{\zvcontextstack\romannumeral\zvcontextdepth}}% |
| 431 |
\global\decrement \zvcontextdepth} |
| 432 |
|
| 433 |
\def \zsavevcontext #1{% |
| 434 |
{\ztoksa = \expandafterthrice{% |
| 435 |
\name{\zvcontextstack\romannumeral\zvcontextdepth}}% |
| 436 |
\withname\xdef{\zvcontextstack\romannumeral\zvcontextdepth}% |
| 437 |
{\the\ztoksa #1}}} |