summaryrefslogtreecommitdiff
path: root/tex/context/base/mkiv/core-sys.mkiv
blob: 4d99d8647b02d0ed6cd61a75f1ac0c92fdc3afe2 (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
%D \module
%D   [       file=core-sys, % moved from main-001
%D        version=1997.03.31,
%D          title=\CONTEXT\ Core Macros,
%D       subtitle=System,
%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.

% we need to mkiv-ize this file !

\writestatus{loading}{ConTeXt Core Macros / System}

\registerctxluafile{core-sys}{}

\unprotect

%D Version checking:

\unexpanded\def\newcontextversion#1%
  {\doifelse{#1}\contextversion
      {\let\newcontextversion\gobbleoneargument}
      {\writeline
       \writestatus{Fatal    Error}{Your format does not match the base files!}%
       \writeline
       \writestatus{Format Version}{\contextversion\space\contextmark}%
       \writestatus{Files  Version}{#1}%
       \batchmode
       \normalend}}

%D End of lines to the output. \TEX\ will map this onto the platform specific
%D line ending. I hate this mess.

\let\operatingsystem\clf_operatingsystem

%D The jobname is what gets loaded by the cont-yes stub file. This name
%D also determines the name of tuc etc files.

\let\jobfilefullname\clf_jobfilefullname
\let\jobfilename    \clf_jobfilename
\let\jobfilesuffix  \clf_jobfilesuffix

%D However, that one can itself load another file.

\let\inputfilebarename  \clf_inputfilebarename
\let\inputfilerealsuffix\clf_inputfilerealsuffix
\let\inputfilesuffix    \clf_inputfilesuffix
\let\inputfilename      \clf_inputfilename

%D The output name is only used for some checking.

\let\outputfilename\clf_outputfilename

\installcorenamespace{system}

\installdirectcommandhandler \??system {system}

\appendtoks
%   \edef\outputfilename  {\directsystemparameter\c!file      }%
%   \edef\inputfilename   {\directsystemparameter\c!inputfile }%
\to \everysetupsystem

\appendtoks
    \ifcase\directsystemparameter\c!n\relax
        %                        % 0 : unknown
    \or
        \setsystemmode\v!first   % 1 : first run
    \or
        %                        % 2 : successive run
    \or
        \setsystemmode\v!first   % 3 : first and only run
    \or
        \setsystemmode\v!last    % 4 : (extra) last run
    \fi
\to \everysetupsystem

\let\m_system_job_suffix\s!unknown

\appendtoks
    \resetsystemmode{suffix-\m_system_job_suffix}%
    \edef\m_system_job_suffix{\jobsuffix}%
    \setsystemmode{suffix-\m_system_job_suffix}%
\to \everysetupsystem

% \appendtoks
%     \ctxcommand{updatefilenames("\jobame","\inputfilename","\outputfilename")}%
% \to \everysetupsystem

\newconditional\prerollrun % when true it means that we have a forced number of runs

% Some mechanisms (see x-res-01) use either \jobfilename or
% \jobfilename.somesuffix, in which case we need to use the
% full name if given or a default (like \jobfilename.xml);
% this comes down to replacing the default tex suffix.

\def\jobfullname{\jobfilename.\jobfilesuffix}

\unexpanded\def\setjobfullname#1% #1 = default if not given
  {\doifelsenothing\jobfilename
     {\let\jobfullname\empty}
     {\doif\jobfilesuffix\c!tex{\edef\jobfullname{\jobfilename.#1}}}}

%D There are a couple of system states avaiable:
%D
%D \starttabulate [|T|T|]
%D     \NC \type{\jobname}           \NC \jobname           \NC \NR
%D     \NC \type{\jobfilename}       \NC \jobfilename       \NC \NR
%D     \NC \type{\jobfilesuffix}     \NC \jobfilesuffix     \NC \NR
%D     \NC \type{\inputfilename}     \NC \inputfilename     \NC \NR
%D     \NC \type{\inputfilebarename} \NC \inputfilebarename \NC \NR
%D     \NC \type{\inputfilesuffix}   \NC \inputfilesuffix   \NC \NR
%D     \NC \type{\outputfilename}    \NC \outputfilename    \NC \NR
%D     \NC \type{\operatingsystem}   \NC \operatingsystem   \NC \NR
%D \stoptabulate

\appendtoks
    \edef\outputresolution{\directsystemparameter\c!resolution}%
\to \everysetupsystem

%D The system modes set by the setup command can be used in
%D situations like:
%D
%D \starttyping
%D \startmode[*first]
%D   \executesystemcommand{cleanupxml text.xml clean-text.xml}
%D \stopmode
%D
%D \starttext
%D   \typefile{clean-text.xml}
%D \stoptext
%D \stoptyping

\unexpanded\def\setuprandomize[#1]%
  {\doifsomething{#1}
     {\begingroup
      % tex's time is in minutes
      \scratchcounter\normaltime
      \processaction
        [#1]
        [  \v!small=>\divide\scratchcounter 15, %  900,
          \v!medium=>\divide\scratchcounter 30, % 1800,
             \v!big=>\divide\scratchcounter 60, % 3600,
          \v!normal=>\getnewrandomseed\scratchcounter,
         \s!default=>\getnewrandomseed\scratchcounter,
         \s!unknown=>\scratchcounter#1]%
      \expanded{\setrandomseed{\the\scratchcounter}}%
    % \writestatus\m!system{randomseed: \the\scratchcounter}%
      \endgroup}}

\setupsystem
  [\c!directory=,
   \c!n=0, % 0:unknown 1: one run 2: first 3: successive 4: final run
   \c!resolution=600,% in dpi, no unit in mkiv
 % \c!random=, % obsolete here
 % \c!file=\jobname,
 % \c!inputfile=\outputfilename,
   \c!type=unix, % windows is normally less sensitive to handle
   \c!bodyfont=\normalizedlocalbodyfontsize] % of iets anders

%D Remark: windows programs normally handle \type {cr|lf|crlf} but unix
%D is more picky, so we default to the \type {cr}. I never understood why
%D \type {crlf} was not used in all systems, since it makes most sense.
%D But anyway, in \MKIV\ we avoid most of the complications anyway as we
%D deal with much at the \LUA\ end.

\unexpanded\def\start
  {\dosingleempty\syst_start}

\def\syst_start
  {\bgroup
   \iffirstargument
     \expandafter\syst_start_yes
   \else
     \expandafter\syst_start_nop
   \fi}

\def\syst_start_yes[#1]%
 {\edef\m_syst_start_stop{#1}%
  \ifx\m_syst_start_stop\empty
    \let\syst_stop_indeed\donothing
  \else\ifcsname\e!start\m_syst_start_stop\endcsname
    \expandafter\let\expandafter\syst_stop_indeed\csname\e!stop\m_syst_start_stop\endcsname
    \csname\e!start\m_syst_start_stop\expandafter\expandafter\expandafter\endcsname
  \else
    \let\syst_stop_indeed\donothing
  \fi\fi}

\def\syst_start_nop[#1]%
  {\let\syst_stop_indeed\donothing}

\unexpanded\def\stop
  {\syst_stop_indeed
   \egroup}

% \c!before \c!after \c!inbetween \c!commands \c!style \c!color

\installcorenamespace{startstop}

\installcommandhandler \??startstop {startstop} \??startstop

\appendtoks
    \setuevalue{\e!start\currentstartstop}{\syst_startstop_start {\currentstartstop}}%
    \setuevalue{\e!stop \currentstartstop}{\syst_startstop_stop  {\currentstartstop}}%
    \setuevalue        {\currentstartstop}{\syst_startstop_indeed{\currentstartstop}}%
\to \everydefinestartstop

\ifdefined\dotagconstruct \else \let\dotagconstruct\relax \fi

\unexpanded\def\syst_startstop_start#1%
  {\namedstartstopparameter{#1}\c!before\relax
   \bgroup
   \def\currentstartstop{#1}%
   % we will keep this for a while:
   \startstopparameter\c!commands\relax % obsolete
   % this is the new method:
   \usesetupsparameter\startstopparameter\relax % only in the display version
   \dostarttagged\t!construct\currentstartstop
   \usestartstopstyleandcolor\c!style\c!color
   \dotagconstruct}

\unexpanded\def\syst_startstop_stop#1%
  {\dostoptagged
   \egroup
   \namedstartstopparameter{#1}\c!after\relax}

\unexpanded\def\syst_startstop_indeed#1%
  {\groupedcommand
     {\def\currentstartstop{#1}%
      \startstopparameter\c!commands\relax % better: setups so that will show op soon
      \dostarttagged\t!construct\currentstartstop
      \usestartstopstyleandcolor\c!style\c!color
      \startstopparameter\c!left\relax}
     {\def\currentstartstop{#1}% safeguard, not really needed
      \startstopparameter\c!right\relax
      \dostoptagged
      \startstopparameter\c!inbetween\relax}}

% \definestartstop[tracing][\c!style=\tt]

% \unexpanded\def\ignorestartstop[#1]%
%   {\unexpanded\expandafter\def\csname\e!start#1\expandafter\endcsname\expandafter
%      {\expandafter\gobbleuntil\csname\e!stop#1\endcsname}}
%
% \ignorestartstop[bagger]

\installcorenamespace{highlight}

\installcommandhandler \??highlight {highlight} \??highlight % we could do with less

\setuphighlight
  [\c!command=\v!yes]

\appendtoks
    \edef\p_command{\highlightparameter\c!command}%
    \ifx\p_command\v!yes
        \setuevalue\currenthighlight{\typo_highlights_indeed{\currenthighlight}}%
    \fi
\to \everydefinehighlight

\ifdefined\dotaghighlight \else \let\dotaghighlight\relax \fi

\unexpanded\def\typo_highlights_indeed#1% inline style/color switch
  {\dontleavehmode\groupedcommand % otherwise wrong par number in tags
     {\def\currenthighlight{#1}%
      \dostarttagged\t!highlight\currenthighlight
      \usehighlightstyleandcolor\c!style\c!color
      \dotaghighlight}
     {\dostoptagged}}

\unexpanded\def\highlight[#1]%
  {\typo_highlights_indeed{#1}}

\unexpanded\def\starthighlight[#1]%
  {\begingroup
   \def\currenthighlight{#1}%
   \dostarttagged\t!highlight\currenthighlight
   \usehighlightstyleandcolor\c!style\c!color
   \dotaghighlight}

\unexpanded\def\stophighlight
  {\dostoptagged
   \endgroup}

\let\directhighlight\typo_highlights_indeed

\unexpanded\def\defineexpandable
  {\doifelsenextoptional
     {\syst_basics_define_yes\def}%
     {\syst_basics_define_nop\def}}

\unexpanded\def\define
  {\doifelsenextoptional
     {\syst_basics_define_yes{\unexpanded\def}}%
     {\syst_basics_define_nop{\unexpanded\def}}}

\unexpanded\def\syst_basics_define_yes#1[#2]#3#4%
  {\ifdefined#3%
     \showmessage\m!system4{\string#3}%
   \fi
   \ifcase0#2\relax
     #1#3{#4}\or
     #1#3##1{#4}\or
     #1#3##1##2{#4}\or
     #1#3##1##2##3{#4}\or
     #1#3##1##2##3##4{#4}\or
     #1#3##1##2##3##4##5{#4}\or
     #1#3##1##2##3##4##5##6{#4}\or
     #1#3##1##2##3##4##5##6##7{#4}\or
     #1#3##1##2##3##4##5##6##7##8{#4}\or
     #1#3##1##2##3##4##5##6##7##8##9{#4}\else
     #1#3{#4}\fi}

\unexpanded\def\syst_basics_define_nop#1#2#3%
  {\ifdefined#2%
     \showmessage\m!system4{\string#2}%
   \fi
   #1#2{#3}}

% new:
%
% \checked\def \whatever#alpha#beta{#alpha + #beta}
% \checked\edef\whatever#alpha#beta{#alpha + #beta}

\unexpanded\def\unique#1#2%
  {\ifdefined#2%
     \showmessage\m!system4{\string#2}%
     \expandafter#1\expandafter\gobbleddefinition
   \else
     \expandafter#1%
   \fi#2}

\unexpanded\def\checked#1#2%
  {\ifdefined#2%
     \showmessage\m!system4{\string#2}%
   \fi
   #1#2}

% \startluacode
%     local formatters = string.formatters
%     local contextsprint, ctxcatcodes, prtcatcodes = context.sprint, tex.ctxcatcodes, tex.prtcatcodes
%     local match, gmatch, rep = string.match, string.gmatch, string.rep
%     local empty = {
%         "single",
%         "double",
%         "triple",
%         "quadruple",
%         "quintuple",
%     }
%     local check = {
%         "first",
%         "second",
%         "third",
%         "fourth",
%         "fifth",
%     }
%     function commands.define(str)
%         -- we could store the defaults in lua and call lua instead but why bother
%         local arg, cmd = match(str,"(.*)\\(.-)$")
%         local a = { }
%         for s in gmatch(arg,"%[(.-)%]") do
%             a[#a+1] = s
%         end
%         local n = tonumber(a[#a])
%         if n then
%             a[#a] = nil
%         else
%             n = 0
%         end
%         contextsprint(ctxcatcodes,formatters["\\unexpanded\\def\\%s"](cmd))
%         if #a > 0 then
%             contextsprint(prtcatcodes,formatters["{\\do%sempty\\user_defined_%s}"](empty[#a],cmd))
%             contextsprint(prtcatcodes,formatters["\\def\\user_defined_%s"](cmd))
%             for i=1,#a do
%                 contextsprint(ctxcatcodes,formatters["[#%s]"](i))
%             end
%             contextsprint(ctxcatcodes,"{")
%             for i=#a,1,-1 do
%                 contextsprint(ctxcatcodes,formatters["\\if%sargument"](check[i]))
%                 contextsprint(prtcatcodes,formatters["\\def\\next{\\user_defined_indeed_%s"](cmd))
%                 for j=1,#a-i do
%                     contextsprint(ctxcatcodes,formatters["[%s]"](a[j]))
%                 end
%                 for j=1,i do
%                     contextsprint(ctxcatcodes,formatters["[#%s]"](j))
%                 end
%                 contextsprint(ctxcatcodes,"}")
%                 if i == 1 then
%                     contextsprint(ctxcatcodes,rep("\\fi",#a))
%                 else
%                     contextsprint(ctxcatcodes,"\\else")
%                 end
%             end
%             contextsprint(ctxcatcodes,"\\next}")
%             contextsprint(prtcatcodes,formatters["\\def\\user_defined_indeed_%s"](cmd))
%             for i=1,#a do
%                 contextsprint(ctxcatcodes,formatters["[#%s]"](i))
%             end
%         end
%         for i=1,n do
%             contextsprint(ctxcatcodes,formatters["#%s"](#a+i))
%         end
%     end
% \stopluacode
%
% \unexpanded\def\define#1#{\ctxcommand{define([[\detokenize{#1}]])}}
%
% \starttext
%     \define[2]\whatevera{#1+#2}
%     \whatevera{A}{B}
%     \define[me][too][2]\whateverb{#1+#2+#3+#4}
%     \whateverb[A]{B}{C}
%     \whateverb[A][B]{C}{D}
%     \define[alpha][beta][gamma][delta]\whateverc{#1+#2+#3+#4}
%     \whateverc[P][Q]
% \stoptext

%D This is a checked variant of \type {\getvalue}.

\unexpanded\def\macroname#1% brrr
% {\csname\ifcsname#1\endcsname#1\else\s!empty\fi\endcsname}
  {\begincsname#1\endcsname}

% %D A weird one that I probably needed once, so it might as well become
% %D obsolete.
%
% \unexpanded\def\usecommands#1%
%   {\begingroup
%    \def\docommand##1{\setbox\scratchbox\hbox{\csname\string##1\endcsname##1}}%
%    \processcommalist[#1]\docommand
%    \endgroup}

\unexpanded\def\syst_log_indeed#1#2#3%
  {\ctxcommand{systemlog("#1","#2",\!!bs#3\!!es)}}

\let\systemlog\syst_log_indeed

\unexpanded\def\systemlogfirst
  {\ifcase\directsystemparameter\c!n\relax
     \expandafter\syst_log_indeed
   \or
     \expandafter\syst_log_indeed
   \else
     \expandafter\gobblethreearguments
   \fi}

\unexpanded\def\systemloglast
  {\ifcase\directsystemparameter\c!n\relax
     \expandafter\syst_log_indeed
   \or
      \expandafter\gobblethreearguments
   \or
     \expandafter\gobblethreearguments
   \or
     \expandafter\gobblethreearguments
   \or
     \expandafter\syst_log_indeed
   \fi}

\protect \endinput