core-sys.mkxl /size: 16 Kb    last modification: 2025-02-21 11:03
1%D \module
2%D   [       file=core-sys, % moved from main-001
3%D        version=1997.03.31,
4%D          title=\CONTEXT\ Core Macros,
5%D       subtitle=System,
6%D         author=Hans Hagen,
7%D           date=\currentdate,
8%D      copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
9%C
10%C This module is part of the \CONTEXT\ macro||package and is
11%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
12%C details.
13
14% we need to mkiv-ize this file !
15
16\writestatus{loading}{ConTeXt Core Macros / System}
17
18\registerctxluafile{core-sys}{autosuffix}
19
20\unprotect
21
22%D Version checking:
23
24\permanent\protected\def\newcontextversion#1%
25  {\ifcstok{#1}\contextversion
26     \aliased\let\newcontextversion\gobbleoneargument
27   \else
28     \writeline
29     \writestatus{Fatal    Error}{Your format does not match the base files!}%
30     \writeline
31     \writestatus{Format Version}{\contextversion\space\contextmark}%
32     \writestatus{Files  Version}{#1}%
33     \batchmode
34     \normalend
35   \fi}
36
37%D End of lines to the output. \TEX\ will map this onto the platform specific
38%D line ending. I hate this mess.
39
40
41% \operatingsystem % defined at lua end
42
43%D The jobname is what gets loaded by the cont-yes stub file. This name also
44%D determines the name of tuc etc files.
45
46% \jobfilefullname  % defined at lua end
47% \jobfilename      % defined at lua end
48% \jobfilesuffix    % defined at lua end
49
50%D However, that one can itself load another file.
51
52% \inputfilebarename    % defined at lua end
53% \inputfilerealsuffix  % defined at lua end
54% \inputfilesuffix      % defined at lua end
55% \inputfilename        % defined at lua end
56
57%D The output name is only used for some checking.
58
59% \outputfilename % defined at lua end
60
61\installcorenamespace{system}
62
63\installdirectcommandhandler \??system {system}
64
65\appendtoks
66%   \edef\outputfilename  {\directsystemparameter\c!file      }%
67%   \edef\inputfilename   {\directsystemparameter\c!inputfile }%
68\to \everysetupsystem
69
70\appendtoks
71    \ifcase\directsystemparameter\c!n\relax
72        %                        % 0 : unknown
73    \or
74        \setsystemmode\v!first   % 1 : first run
75    \or
76        %                        % 2 : successive run
77    \or
78        \setsystemmode\v!first   % 3 : first and only run
79    \or
80        \setsystemmode\v!last    % 4 : (extra) last run
81    \fi
82\to \everysetupsystem
83
84\newconditional\prerollrun % when true it means that we have a forced number of runs
85
86% Some mechanisms (see x-res-01) use either \jobfilename or
87% \jobfilename.somesuffix, in which case we need to use the full name if given or a
88% default (like \jobfilename.xml); this comes down to replacing the default tex
89% suffix.
90
91\permanent\def\jobfullname{\jobfilename.\jobfilesuffix}
92
93\permanent\protected\def\setjobfullname#1% #1 = default if not given
94  {\ifempty\jobfilename
95     \lettonothing\jobfullname
96   \orelse\ifcstok\jobfilesuffix\c!tex
97     \edef\jobfullname{\jobfilename.#1}%
98   \fi}
99
100%D There are a couple of system states avaiable:
101%D
102%D \starttabulate [|T|T|]
103%D     \NC \type{\jobname}           \NC \jobname           \NC \NR
104%D     \NC \type{\jobfilename}       \NC \jobfilename       \NC \NR
105%D     \NC \type{\jobfilesuffix}     \NC \jobfilesuffix     \NC \NR
106%D     \NC \type{\inputfilename}     \NC \inputfilename     \NC \NR
107%D     \NC \type{\inputfilebarename} \NC \inputfilebarename \NC \NR
108%D     \NC \type{\inputfilesuffix}   \NC \inputfilesuffix   \NC \NR
109%D     \NC \type{\outputfilename}    \NC \outputfilename    \NC \NR
110%D     \NC \type{\operatingsystem}   \NC \operatingsystem   \NC \NR
111%D \stoptabulate
112
113% \appendtoks
114%     \edef\outputresolution{\directsystemparameter\c!resolution}%
115% \to \everysetupsystem
116
117%D The system modes set by the setup command can be used in situations like:
118%D
119%D \starttyping
120%D \startmode[*first]
121%D   \executesystemcommand{cleanupxml text.xml clean-text.xml}
122%D \stopmode
123%D
124%D \starttext
125%D   \typefile{clean-text.xml}
126%D \stoptext
127%D \stoptyping
128
129\permanent\protected\def\setuprandomize[#1]%
130  {\ifempty{#1}\else
131     \begingroup
132     % tex's time is in minutes
133     \scratchcounter\normaltime
134     \processaction
135       [#1]
136       [  \v!small=>\divideby\scratchcounter 15, %  900,
137         \v!medium=>\divideby\scratchcounter 30, % 1800,
138            \v!big=>\divideby\scratchcounter 60, % 3600,
139         \v!normal=>\scratchcounter\newrandomseed,
140        \s!default=>\scratchcounter\newrandomseed,
141        \s!unknown=>\scratchcounter#1]%
142     \setrandomseed\scratchcounter
143    %\writestatus\m!system{randomseed: \the\scratchcounter}%
144     \endgroup
145   \fi}
146
147\setupsystem
148  [\c!directory=,
149   \c!n=0, % 0:unknown 1: one run 2: first 3: successive 4: final run
150 % \c!resolution=600,% in dpi, no unit in mkiv, not used in lmtx
151 % \c!random=, % obsolete here
152 % \c!file=\jobname,
153 % \c!inputfile=\outputfilename,
154   \c!type=unix, % windows is normally less sensitive to handle
155   \c!bodyfont=\normalizedlocalbodyfontsize] % of iets anders
156
157%D Remark: windows programs normally handle \type {cr|lf|crlf} but unix is more
158%D picky, so we default to the \type {cr}. I never understood why \type {crlf} was
159%D not used in all systems, since it makes most sense. But anyway, in \MKIV\ we
160%D avoid most of the complications anyway as we deal with much at the \LUA\ end.
161
162\permanent\tolerant\protected\def\start[#1]%
163  {\bgroup
164   \ifparameters
165     \expandafter\syst_start_nop
166   \else
167     \edef\m_syst_start_stop{#1}%
168     \expandafter\syst_start_yes
169   \fi}
170
171\let\syst_stop_indeed\donothing
172
173\def\syst_start_yes
174 {\ifempty\m_syst_start_stop
175    \let\syst_stop_indeed\donothing
176  \orelse\ifcsname\e!start\m_syst_start_stop\endcsname
177    \expandafter\let\expandafter\syst_stop_indeed\csname\e!stop\m_syst_start_stop\endcsname
178    \csname\e!start\m_syst_start_stop\expandafter\expandafter\expandafter\endcsname
179  \else
180    \let\syst_stop_indeed\donothing
181  \fi}
182
183\def\syst_start_nop
184  {\let\syst_stop_indeed\donothing}
185
186\permanent\protected\def\stop
187  {\syst_stop_indeed
188   \egroup}
189
190% \c!before \c!after \c!inbetween \c!commands \c!style \c!color
191
192\installcorenamespace{startstop}
193
194\installcommandhandler \??startstop {startstop} \??startstop
195
196\appendtoks
197    \ifcstok{\startstopparameter\c!arguments}\v!yes
198        \frozen\instance\protected\edefcsname\e!start\currentstartstop\endcsname{\syst_startstop_start_yes {\currentstartstop}}%
199        \frozen\instance\protected\edefcsname\e!stop \currentstartstop\endcsname{\syst_startstop_stop_yes  }%
200        \frozen\instance\protected\edefcsname        \currentstartstop\endcsname{\syst_startstop_indeed_yes{\currentstartstop}}%
201    \else
202        \frozen\instance\protected\edefcsname\e!start\currentstartstop\endcsname{\syst_startstop_start_nop {\currentstartstop}}%
203        \frozen\instance\protected\edefcsname\e!stop \currentstartstop\endcsname{\syst_startstop_stop_nop  {\currentstartstop}}%
204        \frozen\instance\protected\edefcsname        \currentstartstop\endcsname{\syst_startstop_indeed_nop{\currentstartstop}}%
205    \fi
206\to \everydefinestartstop
207
208\ifdefined\dotagconstruct \else \aliased\let\dotagconstruct\relax \fi
209
210\protected\def\syst_startstop_start_indeed
211  {% we will keep this for a while:
212   \startstopparameter\c!commands\relax % obsolete
213   % this is the new method:
214   \usesetupsparameter\startstopparameter\relax % only in the display version
215   \dostarttagged\t!construct\currentstartstop
216   \usestartstopstyleandcolor\c!style\c!color
217   \dotagconstruct}
218
219% Here before and after are grouped!
220
221\tolerant\protected\def\syst_startstop_start_yes#1#*[#S#2]%
222  {\begingroup
223   \cdef\currentstartstop{#1}%
224   \ifparameter#2\or\setupcurrentstartstop[#2]\fi
225   \startstopparameter\c!before\relax
226   \syst_startstop_start_indeed}
227
228\tolerant\protected\def\syst_startstop_indeed_yes#1#*[#S#2]%
229  {\groupedcommand
230     {\cdef\currentstartstop{#1}%
231      \ifparameter#2\or\setupcurrentstartstop[#2]\fi
232      \startstopparameter\c!commands\relax % better: setups so that will show op soon
233      \dostarttagged\t!construct\currentstartstop
234      \usestartstopstyleandcolor\c!style\c!color
235      \dostarttaggednodetail\t!constructleft
236      \startstopparameter\c!left\relax
237      \dostoptagged
238      \dostarttaggednodetail\t!constructcontent}%
239     {\dostoptagged
240      \cdef\currentstartstop{#1}% safeguard, not really needed
241      \dostarttaggednodetail\t!constructright
242      \startstopparameter\c!right\relax
243      \dostoptagged
244      \dostoptagged
245      \startstopparameter\c!inbetween\relax}}
246
247\protected\def\syst_startstop_stop_yes
248  {\dostoptagged
249   \startstopparameter\c!after\relax
250   \endgroup}
251
252% Here before and after are not grouped!
253
254\protected\def\syst_startstop_start_nop#1%
255  {\namedstartstopparameter{#1}\c!before\relax
256   \bgroup
257   \cdef\currentstartstop{#1}%
258   \syst_startstop_start_indeed}
259
260\protected\def\syst_startstop_stop_nop#1%
261  {\dostoptagged
262   \egroup
263   \namedstartstopparameter{#1}\c!after\relax}
264
265\protected\def\syst_startstop_indeed_nop#1%
266  {\syst_startstop_indeed_yes{#1}[]}
267
268% \definestartstop[tracing][\c!style=\tt]
269
270% \protected\def\ignorestartstop[#1]%
271%   {\protected\defcsname\e!start#1\expandafter\endcsname\expandafter
272%      {\expandafter\ignoreupto\csname\e!stop#1\endcsname}}
273%
274% \ignorestartstop[bagger]
275
276%D When looking at an application of \type {\beginsimplegroup} and \type I wondered
277%D about this one but it doesn't make much sense. One hould define highlights that
278%D have style and color, but on the other hand, we might want to combine a font
279%D style and some other treatment. The overhead of commalists (where we can use
280%D {\aftergrouped} for collecting stop tags) is neglectable but it makes no sense.
281%D Maybe we need a \type {command} or better (as commands can take arguments) a
282%D \type {setups} key, but no one ever asked so \unknown.
283
284% \processnextcommalist\typo_highlights_step ...
285%
286% list  : 10K single: 0.111 / 0.132 with command / 0.134 with setups
287% single: 10K single: 0.075 / 0.098 with command / 0.099 with setups
288
289\installcorenamespace{highlight}
290
291\installcommandhandler \??highlight {highlight} \??highlight % we could do with less
292
293\setuphighlight
294  [\c!define=\v!yes]
295%  \c!command=,
296%  \c!setups=]
297
298\appendtoks
299    \ifcstok{\highlightparameter\c!define}\v!yes
300        \frozen\instance\protected\edefcsname\currenthighlight\endcsname
301           {\typo_highlights_indeed{\currenthighlight}}%
302    \fi
303\to \everydefinehighlight
304
305\ifdefined\dotaghighlight \else \aliased\let\dotaghighlight\relax \fi
306
307\permanent\protected\def\typo_highlights_indeed#1% inline style/color switch
308  {\dontleavehmode\groupedcommand % otherwise wrong par number in tags
309     {\cdef\currenthighlight{#1}%
310      \dostarttagged\t!highlight\currenthighlight
311      \usehighlightstyleandcolor\c!style\c!color
312    % \usehighlightstyleandcolor\c!style\c!color
313    % \highlightparameter\c!command
314      \dotaghighlight}
315     {\dostoptagged}}
316
317\permanent\protected\def\highlight[#1]%
318  {\typo_highlights_indeed{#1}}
319
320\permanent\protected\def\starthighlight[#1]%
321  {\begingroup
322   \cdef\currenthighlight{#1}%
323   \dostarttagged\t!highlight\currenthighlight
324   \usehighlightstyleandcolor\c!style\c!color
325 % \usehighlightstyleandcolor\c!style\c!color
326 % \highlightparameter\c!command
327   \dotaghighlight}
328
329\permanent\protected\def\stophighlight
330  {\dostoptagged
331   \endgroup}
332
333\aliased\let\directhighlight\typo_highlights_indeed
334
335%D Defining commands (rather old):
336
337\permanent\protected\def\defineexpandable
338  {\integerdef\c_syst_parameter_catcode\catcode\hashasciicode
339   \catcode\hashasciicode\parametercatcode%
340   \doifelsenextoptional
341     {\syst_basics_define_yes\def}%
342     {\syst_basics_define_nop\def}}
343
344\permanent\protected\def\define
345  {\integerdef\c_syst_parameter_catcode\catcode\hashasciicode
346   \catcode\hashasciicode\parametercatcode%
347   \doifelsenextoptional
348     {\syst_basics_define_yes{\protected\def}}%
349     {\syst_basics_define_nop{\protected\def}}}
350
351\protected\def\syst_basics_define_yes#1[#2]#3#4%
352  {\ifdefined#3%
353     \showmessage\m!system4{\string#3}%
354   \fi
355   \ifcase0#2\relax
356     #1#3{#4}\or
357     #1#3##1{#4}\or
358     #1#3##1##2{#4}\or
359     #1#3##1##2##3{#4}\or
360     #1#3##1##2##3##4{#4}\or
361     #1#3##1##2##3##4##5{#4}\or
362     #1#3##1##2##3##4##5##6{#4}\or
363     #1#3##1##2##3##4##5##6##7{#4}\or
364     #1#3##1##2##3##4##5##6##7##8{#4}\or
365     #1#3##1##2##3##4##5##6##7##8##9{#4}\else
366     #1#3##1##2##3##4##5##6##7##8##9##A{#4}\else
367     #1#3##1##2##3##4##5##6##7##8##9##A##B{#4}\else
368     #1#3##1##2##3##4##5##6##7##8##9##A##B##C{#4}\else
369     #1#3##1##2##3##4##5##6##7##8##9##A##B##C##D{#4}\else
370     #1#3##1##2##3##4##5##6##7##8##9##A##B##C##D##E{#4}\else
371     #1#3{#4}\fi
372   \catcode\hashasciicode\c_syst_parameter_catcode}
373
374\protected\def\syst_basics_define_nop#1#2#3%
375  {\ifdefined#2%
376     \showmessage\m!system4{\string#2}%
377   \fi
378   #1#2{#3}%
379   \catcode\hashasciicode\c_syst_parameter_catcode}
380
381\aliased\let\redefine\define
382
383% new but maybe not needed:
384%
385% \checked\def \whatever#alpha#beta{#alpha + #beta}
386% \checked\edef\whatever#alpha#beta{#alpha + #beta}
387
388\mutable\let\gobbleddefinition\relax % only for diagnostics
389
390\permanent\protected\def\unique#1#2%
391  {\ifdefined#2%
392     \showmessage\m!system4{\string#2}%
393     \expandafter#1\expandafter\gobbleddefinition
394   \else
395     \expandafter#1%
396   \fi#2}
397
398\permanent\protected\def\checked#1#2%
399  {\ifdefined#2%
400     \showmessage\m!system4{\string#2}%
401   \fi
402   #1#2}
403
404% \startluacode
405%     local formatters = string.formatters
406%     local contextsprint, ctxcatcodes, prtcatcodes = context.sprint, tex.ctxcatcodes, tex.prtcatcodes
407%     local match, gmatch, rep = string.match, string.gmatch, string.rep
408%     local empty = {
409%         "single",
410%         "double",
411%         "triple",
412%         "quadruple",
413%         "quintuple",
414%     }
415%     local check = {
416%         "first",
417%         "second",
418%         "third",
419%         "fourth",
420%         "fifth",
421%     }
422%     function commands.define(str)
423%         -- we could store the defaults in lua and call lua instead but why bother
424%         local arg, cmd = match(str,"(.*)\\(.-)$")
425%         local a = { }
426%         for s in gmatch(arg,"%[(.-)%]") do
427%             a[#a+1] = s
428%         end
429%         local n = tonumber(a[#a])
430%         if n then
431%             a[#a] = nil
432%         else
433%             n = 0
434%         end
435%         contextsprint(ctxcatcodes,formatters["\\protected\\def\\%s"](cmd))
436%         if #a > 0 then
437%             contextsprint(prtcatcodes,formatters["{\\do%sempty\\user_defined_%s}"](empty[#a],cmd))
438%             contextsprint(prtcatcodes,formatters["\\def\\user_defined_%s"](cmd))
439%             for i=1,#a do
440%                 contextsprint(ctxcatcodes,formatters["[#%s]"](i))
441%             end
442%             contextsprint(ctxcatcodes,"{")
443%             for i=#a,1,-1 do
444%                 contextsprint(ctxcatcodes,formatters["\\if%sargument"](check[i]))
445%                 contextsprint(prtcatcodes,formatters["\\def\\next{\\user_defined_indeed_%s"](cmd))
446%                 for j=1,#a-i do
447%                     contextsprint(ctxcatcodes,formatters["[%s]"](a[j]))
448%                 end
449%                 for j=1,i do
450%                     contextsprint(ctxcatcodes,formatters["[#%s]"](j))
451%                 end
452%                 contextsprint(ctxcatcodes,"}")
453%                 if i == 1 then
454%                     contextsprint(ctxcatcodes,rep("\\fi",#a))
455%                 else
456%                     contextsprint(ctxcatcodes,"\\else")
457%                 end
458%             end
459%             contextsprint(ctxcatcodes,"\\next}")
460%             contextsprint(prtcatcodes,formatters["\\def\\user_defined_indeed_%s"](cmd))
461%             for i=1,#a do
462%                 contextsprint(ctxcatcodes,formatters["[#%s]"](i))
463%             end
464%         end
465%         for i=1,n do
466%             contextsprint(ctxcatcodes,formatters["#%s"](#a+i))
467%         end
468%     end
469% \stopluacode
470%
471% \protected\def\define#1#{\ctxcommand{define([[\detokenize{#1}]])}}
472%
473% \starttext
474%     \define[2]\whatevera{#1+#2}
475%     \whatevera{A}{B}
476%     \define[me][too][2]\whateverb{#1+#2+#3+#4}
477%     \whateverb[A]{B}{C}
478%     \whateverb[A][B]{C}{D}
479%     \define[alpha][beta][gamma][delta]\whateverc{#1+#2+#3+#4}
480%     \whateverc[P][Q]
481% \stoptext
482
483%D This is a checked variant of \type {\getvalue}.
484
485\permanent\protected\def\macroname#1% brrr
486  {\begincsname#1\endcsname}
487
488% \definedasluacommand\systemlog
489
490\permanent\protected\def\systemlogfirst
491  {\ifcase\directsystemparameter\c!n\relax
492     \expandafter\systemlog
493   \or
494     \expandafter\systemlog
495   \else
496     \expandafter\gobblethreearguments
497   \fi}
498
499\permanent\protected\def\systemloglast
500  {\ifcase\directsystemparameter\c!n\relax
501     \expandafter\systemlog
502   \or
503     \expandafter\gobblethreearguments
504   \or
505     \expandafter\gobblethreearguments
506   \or
507     \expandafter\gobblethreearguments
508   \or
509     \expandafter\systemlog
510   \fi}
511
512\protect \endinput
513