summaryrefslogtreecommitdiff
path: root/tex/context/base/mkii/spec-ini.mkii
blob: 9361ed2225a59cdef056840919133656f28a610d (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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
%D \module
%D   [       file=spec-ini,
%D        version=1996.01.25,
%D          title=\CONTEXT\ Special Macros,
%D       subtitle=Initialization,
%D         author=Hans Hagen,
%D           date=\currentdate,
%D      copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
%C
%C This module is part of the \CONTEXT\ macro||package and is
%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
%C details.

% todo: make this one more plain so that i can use it in plain jobs
% todo: no args, named vars instead
% maybe also drop multiple drivers and make simplify the default, then simplier defs

\newif\ifsimplifyspecials \simplifyspecialstrue % see later

%D We will forbid loading specials after the first page is
%D shipped out.

\ifx\realpageno\undefined
  \countdef\realpageno=0 \realpageno=1
\fi

%D Specials are \TEX's channel to the outside world. They make
%D \TEX\ even more platform independant and permit easy
%D adaption to new developments. One major drawback of specials
%D is that they have to be supported by printer drivers. We've
%D tried to overcome this problem by implementing specials as
%D a sort of drivers themselves.

\writestatus{loading}{ConTeXt Special Macros / Initialization}

\unprotect

%D \TEX\ produces files in the \DVI\ format. This format is
%D well defined and stable. In this format one||byte commands
%D are used which can optionally be followed by length
%D specifiers and arguments. The \DVI||format incorporates a
%D channel to the outside world. This channel is activated by
%D the \TEX\ primitive \type {\special}. The sequence
%D
%D \starttyping
%D \special{Hello here I am.}
%D \stoptyping
%D
%D results in \DVI||codes:
%D
%D \starttyping
%D xxx1 16 Hello here I am.
%D \stoptyping
%D
%D The \type {xxx1} is represented in byte code 239 and the
%D number of following bytes in a~1, 2, 3 or~4 byte number. So
%D here we get $1+1+16$ bytes of code.
%D
%D Translating these codes is upto the \DVI\ driver. It's
%D common use to ignore specials that cannot be interpreted, so
%D the example string should have no consequences for the
%D output.

%D \macros
%D   {jobsuffix}
%D
%D By default, \TEX\ produces \DVI\ files which can be
%D converted to other filetypes. Sometimes it is handy to
%D know what the target file will be. In other driver
%D modules we wil set \type {\jobsuffix} to \type {pdf}.

\def\jobsuffix{dvi}

\def\setjobsuffix#1%
  {\resetsystemmode\jobsuffix
   \edef\jobsuffix{#1}%
   \setsystemmode\jobsuffix}

%D \macros
%D   {everyresetspecials}
%D
%D Now what will this one do? We'll see in a few lines.

\newtoks \everyresetspecials

\appendtoksonce
  \ifx\setjobsuffix\undefined\else\setjobsuffix{dvi}\fi
\to \everyresetspecials

%D A rather fundamental difference between special and direct
%D settings is that the latter don't interfere with typesetting
%D but must be set before the first shipout, while the specials
%D must be packaged in the shipped out box in such a way that
%D they don't interfere.

\newif\ifspecialbasedsettings \specialbasedsettingstrue

\appendtoksonce
  \specialbasedsettingstrue
\to \everyresetspecials

%D Because there is no standardization in the use of specials,
%D more than one driver or program can be supported. The
%D specials are grouped in libraries. Some of these are
%D general, such as the \type{postscript} library, some are
%D tuned to a special kind of program, like the \type{pdf}
%D ones, and some support a specific driver, as we can see in
%D the \type{yandy} library. A library is build with the
%D commands:
%D
%D \starttyping
%D \startspecials[name][inheritance]
%D
%D \definespecial\none{...}
%D \definespecial\onlyone#1{...}
%D \definespecial\alot#1#2#3#4{...}
%D
%D \stopspecials
%D \stoptyping
%D
%D Because drivers can have overlap in low level macros, a
%D mechanism of inheritance is implemented. The libraries
%D defined as second argument are loaded first.
%D
%D Every special has to be predefined first. We  do this with
%D the command:
%D
%D \starttyping
%D \installspecial [\none]    [and] [0]
%D \installspecial [\onlyone] [and] [1]
%D \installspecial [\alot]    [or]  [4]
%D \stoptyping
%D
%D This means as much as: there is a special names
%D \type{\none} which has no arguments and has more than one
%D appearance. The special \type{\alot} on the other hand has
%D four arguments and is only defined once. Every instance in
%D the libraries of a special of category \type{and} is
%D executed when called upon, but only one special of
%D category \type{or} can be active. Most of the
%D \type{postscript}||specials are of category \type{or},
%D because they tend to interfere with driver specific ones.
%D The interactive specials of \type{dviwindo} and \type{pdf}
%D are an example of specials that can be called both.
%D
%D A library is defined in a file with the name
%D \type{spec-...}. We load a library with the command:
%D
%D \starttyping
%D \usespecials [list]
%D \stoptyping
%D
%D where the list can contain one or more file tags, the
%D \type{...} in the filename. The keyword \type{reset}
%D resets all loaded specials. This is equivalent to
%D \type{\resetspecials}.

%D Although a mechanism of nesting can be implemented, we
%D prefer to use a inheritance mechanism as mentioned. Calls
%D upon \type{\usespecials} within a \type{\startspecials}
%D would lead to confusion and errors.

\newif\ifinheritspecials

%D We define some local constants and variables. They look a
%D bit horrible but we don't want conflicts.

\def\@@specfil@@{@@spcfil@@}
\def\@@speclst@@{@@spclst@@}
\def\@@speccat@@{@@spccat@@}
\def\@@specarg@@{@@spcarg@@}
\def\@@specexc@@{@@spcexc@@}

% not faster
%
% \def\@@specfil@@{@sp@f@}
% \def\@@speclst@@{@sp@l@}
% \def\@@speccat@@{@sp@c@}
% \def\@@specarg@@{@sp@a@}
% \def\@@specexc@@{@sp@e@}

\let\currentspecial    \empty
\let\currentspecialfile\empty

%D \macros
%D   {startspecials}
%D
%D Every library has a unique name, which is given as the first
%D argument to \type{\startspecials}. When another library is
%D defined with the same name, previous specials can be
%D overruled. The name may differ from the file||tag.
%D
%D The optional second argument can consist of a list of
%D libraries that are to be loaded first.

\def\dostartspecials[#1][#2]%
  {\doifsomething{#2}
     {\processcommalist[#2]\dousespecials}%
   \doifelsenothing{#1}
     {\let\currentspecial\s!unknown}
     {\def\currentspecial{#1}}%
   \unprotect}

\def\startspecials
  {\localpushmacro\currentspecial
   \dodoubleempty\dostartspecials}

\def\stopspecials
  {\localpopmacro\currentspecial
   \protect}

%D \macros
%D   {installspecial,
%D    resetspecials}
%D
%D We have to install specials before we can define and use
%D them. The command itself is defined as a call to another
%D command that executes one or more user||defined specials,
%D depending of it's category: \type{or} versus \type{and}.
%D
%D The command \type{\installspecial} takes three
%D (non||optional) arguments: the name of the command, the
%D category it belongs to and the number of arguments it
%D takes.
%D
%D With \type{\resetspecials} we can unload the predefined
%D specials. Special reset actions |<|look in \type{spec-mis}
%D for an example|>| can be assigned to the token register
%D \type{\everyresetspecials}.

\let\@@allspecials=\empty

\def\doinstallspecial[#1][#2][#3]%
  {\letvalue{\@@speclst@@\string#1}\empty
   \setvalue{\@@speccat@@\string#1}{#2}%
   \setvalue{\@@specarg@@\string#1}{#3}%
   \addtocommalist{\string#1}\@@allspecials
   \def#1{\executespecial#1}}

\def\installspecial
  {\dotripleargument\doinstallspecial}

\def\resetspecials
  {\the\everyresetspecials
   \def\docommand##1%
     {\letvalue{\@@speclst@@##1}\empty}%
   \processcommacommand[\@@allspecials]\docommand}

%D \macros
%D   {definespecial}
%D
%D The command \type{\definespecial} take the place of
%D \type{\def} in the definition of a special. Just to be
%D sure, we first check if the command is permitted, i.e.
%D installed. If not, we give a warning and gobble the
%D illegal command in an quite elegant way.
%D
%D If the command can be combined (\type{and}) with others,
%D we append it to a list, otherwise (\type{or}) it becomes
%D the only item in the list.

\def\definespecial#1%
  {\ifx#1\undefined
     \showmessage\m!specials4{\string#1}%
     \def\next
       {\def\@@illegalspecial@@}%
   \else
     \def\next
       {\doifelsevalue{\@@speccat@@\string#1}{or}
          {\edef\@@newspeclst@@{\currentspecial}}
          {\edef\@@newspeclst@@{\getvalue{\@@speclst@@\string#1}}%
           \addtocommalist\currentspecial\@@newspeclst@@}%
        \setevalue{\@@speclst@@\string#1}{\@@newspeclst@@}%
        \setvalue{\currentspecial\string#1}}%
   \fi
   \next}

%D \macros
%D   {usespecials}
%D
%D We use \type{\usespecials} to load a specific library.
%D This command is only permitted outside the definition part.

\def\dousespecials#1%
  {\doifelse{#1}\v!reset
     {\resetspecials}
     {\doifdefinedelse{\@@specfil@@#1}
        {\edef\currentspecialfile{\getvalue{\@@specfil@@#1}}}
        {\edef\currentspecialfile{#1}}%
      \makeshortfilename[\truefilename{\f!specialprefix\currentspecialfile}]%
      \startreadingfile
        \readsysfile{\shortfilename.mkii}{\showmessage\m!specials5\currentspecialfile}\donothing
      \stopreadingfile}}

\def\usespecials[#1]%
  {\ifnum\realpageno<2
     \doifelsenothing\currentspecial
       {\processcommalist[#1]\dousespecials}
       {\showmessage\m!specials6\empty}%
   \fi}

%D \macros
%D   {executespecials}
%D
%D The command \type{\executespecials} is used to execute the
%D defined specials. Once a special is installed, the special
%D itself calls for this command, so it's not needed outside
%D this module. One can use it if wanted.
%D
%D A former implementation grouped the execution. Recent
%D additions however |<|like the specials that implement object
%D handling|>| asked for non||grouped execution.

%D \starttyping
%D \def\executespecials#1#2%
%D   {\def\doonespecial##1%
%D      {\getvalue{##1\string#1}#2\relax}%
%D    \processcommacommand
%D      [\getvalue{\@@speclst@@\string#1}]\doonespecial}
%D
%D \def\executespecial#1%
%D   {\expandafter\ifcase\getvalue{\@@specarg@@\string#1}\relax
%D      \def\next%
%D        {\executespecials#1{}}%
%D    \or
%D      \def\next##1%
%D        {\executespecials#1{{##1}}}%
%D    \or
%D      \def\next##1##2%
%D        {\executespecials#1{{##1}{##2}}}%
%D    \or
%D      \def\next##1##2##3%
%D        {\executespecials#1{{##1}{##2}{##3}}}%
%D    \or
%D      \def\next##1##2##3##4%
%D        {\executespecials#1{{##1}{##2}{##3}{##4}}}%
%D    \or
%D      \def\next##1##2##3##4##5%
%D        {\executespecials#1{{##1}{##2}{##3}{##4}{##5}}}%
%D    \or
%D      \def\next##1##2##3##4##5##6%
%D        {\executespecials#1{{##1}{##2}{##3}{##4}{##5}{##6}}}%
%D    \or
%D      \def\next##1##2##3##4##5##6##7%
%D        {\executespecials#1{{##1}{##2}{##3}{##4}{##5}{##6}{##7}}}%
%D    \or
%D      \def\next##1##2##3##4##5##6##7##8%
%D        {\executespecials#1{{##1}{##2}{##3}{##4}{##5}{##6}{##7}{##8}}}%
%D    \or
%D      \def\next##1##2##3##4##5##6##7##8##9%
%D        {\executespecials#1{{##1}{##2}{##3}{##4}{##5}{##6}{##7}{##8}{##9}}}%
%D    \else
%D      \def\next%
%D        {\message{illegal special: \string#1}}%
%D    \fi
%D    \next}
%D \stoptyping
%D
%D Because specials happen quite often, we will use a bit more
%D brute force. Keep in mind that we have to collect the
%D arguments because we want to support more drivers at once.
%D
%D I tested this on the next test. Where the previous alternative
%D took about 32 seconds, the new alternative takes 25 seconds.
%D
%D \starttyping
%D \testfeature{10000}{\setbox0=\hbox{test \color[red]{oeps} test}}
%D \stoptyping

\def\@@exsp{exsp}

\setvalue{\@@exsp0}{{}}
\setvalue{\@@exsp1}#1{{{#1}}}
\setvalue{\@@exsp2}#1#2{{{#1}{#2}}}
\setvalue{\@@exsp3}#1#2#3{{{#1}{#2}{#3}}}
\setvalue{\@@exsp4}#1#2#3#4{{{#1}{#2}{#3}{#4}}}
\setvalue{\@@exsp5}#1#2#3#4#5{{{#1}{#2}{#3}{#4}{#5}}}
\setvalue{\@@exsp6}#1#2#3#4#5#6{{{#1}{#2}{#3}{#4}{#5}{#6}}}
\setvalue{\@@exsp7}#1#2#3#4#5#6#7{{{#1}{#2}{#3}{#4}{#5}{#6}{#7}}}
\setvalue{\@@exsp8}#1#2#3#4#5#6#7#8{{{#1}{#2}{#3}{#4}{#5}{#6}{#7}{#8}}}
\setvalue{\@@exsp9}#1#2#3#4#5#6#7#8#9{{{#1}{#2}{#3}{#4}{#5}{#6}{#7}{#8}{#9}}}

%D \starttyping
%D \def\executespecials#1%
%D   {\def\doonespecial##1%
%D      {\csname##1\xspecialcommand\endcsname#1\relax}%
%D    \@EA\rawprocesscommalist\@EA
%D      [\csname\@@speclst@@\xspecialcommand\endcsname]\doonespecial}
%D
%D \def\executespecial#1%
%D   {\def\xspecialcommand{\string#1}%
%D    \@EA\@EA\@EA\executespecials\csname\@@exsp\csname\@@specarg@@\xspecialcommand\endcsname\endcsname}
%D \stoptyping

%D Some more speed can be gained by using a dedicated string
%D processing routine. Now we can bring down the execution
%D time to 21 seconds, one third less than the original run time.

\def\executespecials#1%
  {\@EA\let\@EA\speciallist\csname\@@speclst@@\xspecialcommand\endcsname
   \ifx\speciallist\empty\else
     \def\doonespecial##1%
       {\csname##1\xspecialcommand\endcsname#1\relax}%
     \@EA\dodoonespecial\speciallist,\end,%
   \fi}

\def\executespecial#1%
  {\def\xspecialcommand{\string#1}%
   \@EA\@EA\@EA\executespecials\csname\@@exsp\csname\@@specarg@@\xspecialcommand\endcsname\endcsname}

\def\dodoonespecial#1,%
  {\ifx\end#1\else
     \doonespecial{#1}\expandafter\dodoonespecial
   \fi}

%D This kind of saving only shows up when making interative
%D documents with lots of color switches. In such documents
%D tens of thousands of special calls are rather normal.
%D On a 650 Mhz Pentium, the previous test takes 15 seconds
%D less (on about 65 seconds). When processing 2000 page
%D interactive documents this saving can be neglected.

%D In the previous macros, the \type{{{...}}} are needed
%D because we pass all those arguments to the specials support
%D macro.

\let\openspecialfile  \relax
\let\closespecialfile \relax

%D \macros
%D   {doifspecialavailableelse}
%D
%D For testing purposes (this was first needed when object
%D support was implemented) we have:
%D
%D \starttyping
%D \doifspecialavailableelse\specialcommand{true}{false}
%D \stoptyping
%D
%D e.g:
%D
%D \starttyping
%D \doifspecialavailableelse\doinsertobject{...}{...}
%D \stoptyping

\def\doifspecialavailableelse#1#2#3%
  {\doifelsevaluenothing{\@@speclst@@\string#1}{#3}{#2}}

%D So far for the macros that deal with installing specials.
%D In the file \type {spec-def} you will find the predefined
%D specials.

%D Now that we have seen the flexible way (permitting
%D special chains) we will implement a faster and flat
%D alternative. But only if flag si set.

\ifsimplifyspecials

  \def\doinstallspecial[#1][#2][#3]%
    {\appendtoks\forgetspecial#1{#3}\to\everyresetspecials
     \@EA\chardef\csname\@@speclst@@\string#1\endcsname\zerocount
     \forgetspecial#1{#3}}

  \def\forgetspecial#1#2%
    {\ifcase#2\relax
        \let#1\relax                \or
        \let#1\gobbleoneargument    \or
        \let#1\gobbletwoarguments   \or
        \let#1\gobblethreearguments \or
        \let#1\gobblefourarguments  \or
        \let#1\gobblefivearguments  \or
        \let#1\gobblesixarguments   \or
        \let#1\gobblesevenarguments \or
        \let#1\gobbleeightarguments \or
        \let#1\gobbleninearguments  \or
        \let#1\gobbletenarguments   \fi}

  \def\resetspecials
    {\the\everyresetspecials}

  \def\definespecial#1%
    {\@EA\chardef\csname\@@speclst@@\string#1\endcsname=1
     \def#1}

  \def\doifspecialavailableelse#1%
    {\ifcase\csname\@@speclst@@\string#1\endcsname
       \expandafter\secondoftwoarguments
     \else
       \expandafter\firstoftwoarguments
     \fi}

\fi

%D For quite some time the \CONTEXT\ way of specifying the
%D output format has been:
%D
%D \starttyping
%D \usespecials[ps,yy,win,pdf]
%D \stoptyping
%D
%D Because at \PRAGMA\ we use \DVIPSONE, this was a suitable
%D setting, but with \CONTEXT\ going public, the next sequence
%D is more suitable for \DVIPS\ users:
%D
%D \starttyping
%D \usespecials[reset,ps,tr,pdf]
%D \stoptyping
%D
%D On the other hand, for \PDFTEX\ we needed:
%D
%D \starttyping
%D \usespecials[tpd]
%D \stoptyping
%D
%D To simplify things, I decided to provide a higher level
%D command.
%D
%D \starttyping
%D \defineoutput[name][specials]
%D \setupoutput[name,...]
%D \stoptyping
%D
%D In a \type {spec-def} you can find some examples.

\def\defineoutput
  {\dodoubleargument\dodefineoutput}

\def\dodefineoutput[#1][#2]%
  {\setvalue{\??ui#1}{#2}}

\def\dosetupoutput#1%
  {\doifdefinedelse{\??ui#1}
     {\processcommacommand[\getvalue{\??ui#1}]\dousespecials}
     {\doifdefinedelse{\@@specfil@@#1}
        {\dousespecials{#1}}
        {\showmessage\m!specials7{#1}}}}

% Beware, from now on changing the (default) driver files demands
% remaking the format (no big deal, since only i adapt the driver
% and need delayed loading).

\let\currentoutput\empty

\def\setupoutput[#1]%
  {\doifnot{#1}{\currentoutput}
     {\ifnum\realpageno<\plustwo % new
        \resetspecials\processcommacommand[#1]\dosetupoutput
        \edef\currentoutput{#1}%
      \fi}}

\def\preloadspecials % it's nicer to report this
  {\doifsomething\currentoutput
     {\showmessage\m!specials1\currentoutput}}

\appendtoks
   \savecurrentvalue\usedoutputdriver\currentoutput
\to \everyfirstshipout

\protect \endinput