core-sys.mkxl /size: 15 Kb    last modification: 2023-12-21 09:44
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    \frozen\instance\protected\edefcsname        \currentstartstop\endcsname{\syst_startstop_indeed{\currentstartstop}}%
198    \frozen\instance\protected\edefcsname\e!start\currentstartstop\endcsname{\syst_startstop_start {\currentstartstop}}%
199    \frozen\instance\protected\edefcsname\e!stop \currentstartstop\endcsname{\syst_startstop_stop  {\currentstartstop}}%
200\to \everydefinestartstop
201
202\ifdefined\dotagconstruct \else \aliased\let\dotagconstruct\relax \fi
203
204\protected\def\syst_startstop_start#1%
205  {\namedstartstopparameter{#1}\c!before\relax
206   \bgroup
207   \cdef\currentstartstop{#1}%
208   % we will keep this for a while:
209   \startstopparameter\c!commands\relax % obsolete
210   % this is the new method:
211   \usesetupsparameter\startstopparameter\relax % only in the display version
212   \dostarttagged\t!construct\currentstartstop
213   \usestartstopstyleandcolor\c!style\c!color
214   \dotagconstruct}
215
216\protected\def\syst_startstop_stop#1%
217  {\dostoptagged
218   \egroup
219   \namedstartstopparameter{#1}\c!after\relax}
220
221\protected\def\syst_startstop_indeed#1%
222  {\groupedcommand
223     {\cdef\currentstartstop{#1}%
224      \startstopparameter\c!commands\relax % better: setups so that will show op soon
225      \dostarttagged\t!construct\currentstartstop
226      \usestartstopstyleandcolor\c!style\c!color
227      \startstopparameter\c!left\relax}
228     {\cdef\currentstartstop{#1}% safeguard, not really needed
229      \startstopparameter\c!right\relax
230      \dostoptagged
231      \startstopparameter\c!inbetween\relax}}
232
233% \definestartstop[tracing][\c!style=\tt]
234
235% \protected\def\ignorestartstop[#1]%
236%   {\protected\defcsname\e!start#1\expandafter\endcsname\expandafter
237%      {\expandafter\ignoreupto\csname\e!stop#1\endcsname}}
238%
239% \ignorestartstop[bagger]
240
241%D When looking at an application of \type {\beginsimplegroup} and \type I wondered
242%D about this one but it doesn't make much sense. One hould define highlights that
243%D have style and color, but on the other hand, we might want to combine a font
244%D style and some other treatment. The overhead of commalists (where we can use
245%D {\aftergrouped} for collecting stop tags) is neglectable but it makes no sense.
246%D Maybe we need a \type {command} or better (as commands can take arguments) a
247%D \type {setups} key, but no one ever asked so \unknown.
248
249% \processnextcommalist\typo_highlights_step ...
250%
251% list  : 10K single: 0.111 / 0.132 with command / 0.134 with setups
252% single: 10K single: 0.075 / 0.098 with command / 0.099 with setups
253
254\installcorenamespace{highlight}
255
256\installcommandhandler \??highlight {highlight} \??highlight % we could do with less
257
258\setuphighlight
259  [\c!define=\v!yes]
260%  \c!command=,
261%  \c!setups=]
262
263\appendtoks
264    \ifcstok{\highlightparameter\c!define}\v!yes
265        \frozen\instance\protected\edefcsname\currenthighlight\endcsname
266           {\typo_highlights_indeed{\currenthighlight}}%
267    \fi
268\to \everydefinehighlight
269
270\ifdefined\dotaghighlight \else \aliased\let\dotaghighlight\relax \fi
271
272\permanent\protected\def\typo_highlights_indeed#1% inline style/color switch
273  {\dontleavehmode\groupedcommand % otherwise wrong par number in tags
274     {\cdef\currenthighlight{#1}%
275      \dostarttagged\t!highlight\currenthighlight
276      \usehighlightstyleandcolor\c!style\c!color
277    % \usehighlightstyleandcolor\c!style\c!color
278    % \highlightparameter\c!command
279      \dotaghighlight}
280     {\dostoptagged}}
281
282\permanent\protected\def\highlight[#1]%
283  {\typo_highlights_indeed{#1}}
284
285\permanent\protected\def\starthighlight[#1]%
286  {\begingroup
287   \cdef\currenthighlight{#1}%
288   \dostarttagged\t!highlight\currenthighlight
289   \usehighlightstyleandcolor\c!style\c!color
290 % \usehighlightstyleandcolor\c!style\c!color
291 % \highlightparameter\c!command
292   \dotaghighlight}
293
294\permanent\protected\def\stophighlight
295  {\dostoptagged
296   \endgroup}
297
298\aliased\let\directhighlight\typo_highlights_indeed
299
300%D Defining commands (rather old):
301
302\permanent\protected\def\defineexpandable
303  {\integerdef\c_syst_parameter_catcode\catcode\hashasciicode
304   \catcode\hashasciicode\parametercatcode%
305   \doifelsenextoptional
306     {\syst_basics_define_yes\def}%
307     {\syst_basics_define_nop\def}}
308
309\permanent\protected\def\define
310  {\integerdef\c_syst_parameter_catcode\catcode\hashasciicode
311   \catcode\hashasciicode\parametercatcode%
312   \doifelsenextoptional
313     {\syst_basics_define_yes{\protected\def}}%
314     {\syst_basics_define_nop{\protected\def}}}
315
316\protected\def\syst_basics_define_yes#1[#2]#3#4%
317  {\ifdefined#3%
318     \showmessage\m!system4{\string#3}%
319   \fi
320   \ifcase0#2\relax
321     #1#3{#4}\or
322     #1#3##1{#4}\or
323     #1#3##1##2{#4}\or
324     #1#3##1##2##3{#4}\or
325     #1#3##1##2##3##4{#4}\or
326     #1#3##1##2##3##4##5{#4}\or
327     #1#3##1##2##3##4##5##6{#4}\or
328     #1#3##1##2##3##4##5##6##7{#4}\or
329     #1#3##1##2##3##4##5##6##7##8{#4}\or
330     #1#3##1##2##3##4##5##6##7##8##9{#4}\else
331     #1#3##1##2##3##4##5##6##7##8##9##A{#4}\else
332     #1#3##1##2##3##4##5##6##7##8##9##A##B{#4}\else
333     #1#3##1##2##3##4##5##6##7##8##9##A##B##C{#4}\else
334     #1#3##1##2##3##4##5##6##7##8##9##A##B##C##D{#4}\else
335     #1#3##1##2##3##4##5##6##7##8##9##A##B##C##D##E{#4}\else
336     #1#3{#4}\fi
337   \catcode\hashasciicode\c_syst_parameter_catcode}
338
339\protected\def\syst_basics_define_nop#1#2#3%
340  {\ifdefined#2%
341     \showmessage\m!system4{\string#2}%
342   \fi
343   #1#2{#3}%
344   \catcode\hashasciicode\c_syst_parameter_catcode}
345
346\aliased\let\redefine\define
347
348% new but maybe not needed:
349%
350% \checked\def \whatever#alpha#beta{#alpha + #beta}
351% \checked\edef\whatever#alpha#beta{#alpha + #beta}
352
353\mutable\let\gobbleddefinition\relax % only for diagnostics
354
355\permanent\protected\def\unique#1#2%
356  {\ifdefined#2%
357     \showmessage\m!system4{\string#2}%
358     \expandafter#1\expandafter\gobbleddefinition
359   \else
360     \expandafter#1%
361   \fi#2}
362
363\permanent\protected\def\checked#1#2%
364  {\ifdefined#2%
365     \showmessage\m!system4{\string#2}%
366   \fi
367   #1#2}
368
369% \startluacode
370%     local formatters = string.formatters
371%     local contextsprint, ctxcatcodes, prtcatcodes = context.sprint, tex.ctxcatcodes, tex.prtcatcodes
372%     local match, gmatch, rep = string.match, string.gmatch, string.rep
373%     local empty = {
374%         "single",
375%         "double",
376%         "triple",
377%         "quadruple",
378%         "quintuple",
379%     }
380%     local check = {
381%         "first",
382%         "second",
383%         "third",
384%         "fourth",
385%         "fifth",
386%     }
387%     function commands.define(str)
388%         -- we could store the defaults in lua and call lua instead but why bother
389%         local arg, cmd = match(str,"(.*)\\(.-)$")
390%         local a = { }
391%         for s in gmatch(arg,"%[(.-)%]") do
392%             a[#a+1] = s
393%         end
394%         local n = tonumber(a[#a])
395%         if n then
396%             a[#a] = nil
397%         else
398%             n = 0
399%         end
400%         contextsprint(ctxcatcodes,formatters["\\protected\\def\\%s"](cmd))
401%         if #a > 0 then
402%             contextsprint(prtcatcodes,formatters["{\\do%sempty\\user_defined_%s}"](empty[#a],cmd))
403%             contextsprint(prtcatcodes,formatters["\\def\\user_defined_%s"](cmd))
404%             for i=1,#a do
405%                 contextsprint(ctxcatcodes,formatters["[#%s]"](i))
406%             end
407%             contextsprint(ctxcatcodes,"{")
408%             for i=#a,1,-1 do
409%                 contextsprint(ctxcatcodes,formatters["\\if%sargument"](check[i]))
410%                 contextsprint(prtcatcodes,formatters["\\def\\next{\\user_defined_indeed_%s"](cmd))
411%                 for j=1,#a-i do
412%                     contextsprint(ctxcatcodes,formatters["[%s]"](a[j]))
413%                 end
414%                 for j=1,i do
415%                     contextsprint(ctxcatcodes,formatters["[#%s]"](j))
416%                 end
417%                 contextsprint(ctxcatcodes,"}")
418%                 if i == 1 then
419%                     contextsprint(ctxcatcodes,rep("\\fi",#a))
420%                 else
421%                     contextsprint(ctxcatcodes,"\\else")
422%                 end
423%             end
424%             contextsprint(ctxcatcodes,"\\next}")
425%             contextsprint(prtcatcodes,formatters["\\def\\user_defined_indeed_%s"](cmd))
426%             for i=1,#a do
427%                 contextsprint(ctxcatcodes,formatters["[#%s]"](i))
428%             end
429%         end
430%         for i=1,n do
431%             contextsprint(ctxcatcodes,formatters["#%s"](#a+i))
432%         end
433%     end
434% \stopluacode
435%
436% \protected\def\define#1#{\ctxcommand{define([[\detokenize{#1}]])}}
437%
438% \starttext
439%     \define[2]\whatevera{#1+#2}
440%     \whatevera{A}{B}
441%     \define[me][too][2]\whateverb{#1+#2+#3+#4}
442%     \whateverb[A]{B}{C}
443%     \whateverb[A][B]{C}{D}
444%     \define[alpha][beta][gamma][delta]\whateverc{#1+#2+#3+#4}
445%     \whateverc[P][Q]
446% \stoptext
447
448%D This is a checked variant of \type {\getvalue}.
449
450\permanent\protected\def\macroname#1% brrr
451  {\begincsname#1\endcsname}
452
453% \definedasluacommand\systemlog
454
455\permanent\protected\def\systemlogfirst
456  {\ifcase\directsystemparameter\c!n\relax
457     \expandafter\systemlog
458   \or
459     \expandafter\systemlog
460   \else
461     \expandafter\gobblethreearguments
462   \fi}
463
464\permanent\protected\def\systemloglast
465  {\ifcase\directsystemparameter\c!n\relax
466     \expandafter\systemlog
467   \or
468     \expandafter\gobblethreearguments
469   \or
470     \expandafter\gobblethreearguments
471   \or
472     \expandafter\gobblethreearguments
473   \or
474     \expandafter\systemlog
475   \fi}
476
477\protect \endinput
478