file-job.mkvi /size: 13 Kb    last modification: 2021-10-28 13:50
1%D \module
2%D   [       file=file-job, % copied from main-001, later core-job
3%D        version=1997.03.31,
4%D          title=\CONTEXT\ Core Macros,
5%D       subtitle=Job Handling,
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%D This module is still to be split and documented.
15
16\writestatus{loading}{ConTeXt File Macros / Job Handling}
17
18\unprotect
19
20\registerctxluafile{file-job}{}
21
22% processing
23
24\unexpanded\def\doifelseinputfile#name%
25  {\clf_doifelseinputfile{#name}}
26
27\let\doifinputfileelse\doifelseinputfile
28
29\unexpanded\def\processfile#name%
30  {\clf_processfile{#name}}
31
32% path control
33
34%D In the following example the lookup order is \type {.}, {/foo}, \type
35%D {foo/bar} as one can always explicitly prefix a wanted file.
36%D
37%D \starttyping
38%D \pushpath[foo]
39%D     \pushpath[foo/bar]
40%D         \input test
41%D     \poppath
42%D \poppath
43%D \stoptyping
44
45\unexpanded\def\usepath[#path]%
46  {\clf_usepath{#path}}
47
48\unexpanded\def\usesubpath[#path]%
49  {\clf_usesubpath{#path}}
50
51\unexpanded\def\pushpath[#path]%
52  {\clf_pushpath{#path}}
53
54\unexpanded\def\poppath
55  {\clf_poppath}
56
57\unexpanded\def\resetpath % not the pushed paths
58  {\clf_resetpath}
59
60\def\allinputpaths
61  {\clf_allinputpaths}
62
63% helper (not really needed nowadays)
64
65\let\locatedfilepath\empty
66
67\unexpanded\def\locatefilepath#name% no [] ?
68  {\edef\locatedfilepath{\clf_locatefilepath{#name}}}
69
70% % maybe:
71%
72% % \dirlist{c:/data/temporary/foo}{images/bar.jpg}
73% % \dirfile{c:/data/temporary/foo}{images/bar.jpg}
74%
75% \def\dirlist#1#2{dirlist://#1/**/#2}
76% \def\dirfile#1#2{dirlist://#1/**/#2?option=fileonly}
77
78% zip files are tree'd
79
80\unexpanded\def\usezipfile
81  {\dodoubleempty\syst_files_use_zip_file}
82
83\def\syst_files_use_zip_file[#zipname][#subtree]%
84  {\clf_usezipfile{#zipname}{#subtree}}
85
86% exa stuff might become obsolete:
87
88\unexpanded\def\loadexamodes
89  {\dosingleempty\syst_files_load_exa_modes}
90
91\def\syst_files_load_exa_modes[#name]%
92  {\clf_loadexamodes{#name}}
93
94% runtime files (maybe also do this in lua?)
95
96% \def\doloadsystemfile#1% only mkiv files
97%   {\readfile{sys:///#1.\mksuffix}{\showmessage\m!system2{#1.\mksuffix}}\donothing}
98
99\unexpanded\def\syst_files_load
100  {\syst_files_load_indeed\f!newfilename  % new code, to be integrated at some point, plus fixes posted on the list
101   \syst_files_load_indeed\f!locfilename  % new code, somewhat experimental, not distributed (outside the dev group)
102   \syst_files_load_indeed\f!expfilename  % new code, very experimental, can be engine specific, mostly for me only
103   \syst_files_load_indeed\f!sysfilename  % local settings, but probably not that good an idea to use
104  %\syst_files_load_indeed\f!fntfilename  % maybe some day, can load goodies and patches
105  }
106
107\def\syst_files_load_indeed#name% from now on we assume a suffix to be part of the name
108  {\readsysfile{#name}{\showmessage\m!system2{#name}}\donothing}
109
110% document structure
111
112\ifdefined\textlevel                         \else \newcount\textlevel                          \fi % might go away
113\ifdefined\strc_pagenumbers_flush_final_page \else \let\strc_pagenumbers_flush_final_page\relax \fi % ugly
114
115\unexpanded\def\dostarttext
116  {\glet\dostarttext\relax
117   \the\everystarttext
118   \global\everystarttext\emptytoks}
119
120\unexpanded\def\dostoptext
121  {\glet\dostoptext\relax
122   \strc_pagenumbers_flush_final_page
123   \page % hm, bonus
124   \the\everystoptext
125   \global\everystoptext\emptytoks
126   \the\everybye
127   \global\everybye\emptytoks
128   \the\everygoodbye
129   \global\everygoodbye\emptytoks
130   \doifelsesometoks\everynotabene{\writeline\the\everynotabene\writeline}\donothing
131   \normalend}           % tex's \end
132
133\unexpanded\def\starttext    {\clf_starttext}
134\unexpanded\def\stoptext     {\clf_stoptext}
135\unexpanded\def\forcequitjob {\clf_forcequitjob}
136\unexpanded\def\autostarttext{\clf_autostarttext}
137\unexpanded\def\autostoptext {\clf_autostoptext}
138
139\pushoverloadmode
140
141\unexpanded\def\end          {\clf_forceendjob}
142
143\popoverloadmode
144
145\unexpanded\def\finishjob{\stoptext} % nicer in luatex call commandline
146
147\newtoks\everystartnotext
148\newtoks\everystopnotext
149
150\unexpanded\def\startnotext  {\the\everystartnotext}
151\unexpanded\def\stopnotext   {\the\everystopnotext \normalend}
152
153% protect structure
154
155\unexpanded\def\processfilemany#name{\clf_processfilemany{#name}}
156\unexpanded\def\processfileonce#name{\clf_processfileonce{#name}}
157\unexpanded\def\processfilenone#name{\clf_processfilenone{#name}}
158
159\unexpanded\def\project    {\doifelsenextoptionalcs\useproject    \syst_structure_arg_project}
160\unexpanded\def\product    {\doifelsenextoptionalcs\useproduct    \syst_structure_arg_product}
161\unexpanded\def\component  {\doifelsenextoptionalcs\usecomponent  \syst_structure_arg_component}
162\unexpanded\def\environment{\doifelsenextoptionalcs\useenvironment\syst_structure_arg_environment}
163
164\def\syst_structure_arg_project    #name {\clf_useproject    {#name}}
165\def\syst_structure_arg_product    #name {\clf_useproduct    {#name}}
166\def\syst_structure_arg_component  #name {\clf_usecomponent  {#name}}
167\def\syst_structure_arg_environment#name {\clf_useenvironment{#name}}
168
169\unexpanded\def\startproject    {\doifelsenextoptionalcs\syst_structure_start_opt_project    \syst_structure_start_arg_project    }
170\unexpanded\def\startproduct    {\doifelsenextoptionalcs\syst_structure_start_opt_product    \syst_structure_start_arg_product    }
171\unexpanded\def\startcomponent  {\doifelsenextoptionalcs\syst_structure_start_opt_component  \syst_structure_start_arg_component  }
172\unexpanded\def\startenvironment{\doifelsenextoptionalcs\syst_structure_start_opt_environment\syst_structure_start_arg_environment}
173
174\def\syst_structure_start_arg_project    #name {\clf_startproject    {#name}}
175\def\syst_structure_start_arg_product    #name {\clf_startproduct    {#name}}
176\def\syst_structure_start_arg_component  #name {\clf_startcomponent  {#name}}
177\def\syst_structure_start_arg_environment#name {\clf_startenvironment{#name}}
178
179\unexpanded\def\useproject    [#name]{\clf_useproject    {#name}}
180\unexpanded\def\useproduct    [#name]{\clf_useproduct    {#name}}
181\unexpanded\def\usecomponent  [#name]{\clf_usecomponent  {#name}}
182\unexpanded\def\useenvironment[#name]{\clf_useenvironment{#name}}
183
184\unexpanded\def\syst_structure_start_opt_project    [#name]{\clf_startproject    {#name}}
185\unexpanded\def\syst_structure_start_opt_product    [#name]{\clf_startproduct    {#name}}
186\unexpanded\def\syst_structure_start_opt_component  [#name]{\clf_startcomponent  {#name}}
187\unexpanded\def\syst_structure_start_opt_environment[#name]{\clf_startenvironment{#name}}
188
189\newconditional\endofinput % hack, needed because \endinput happens one level down in the input when we write from lua
190
191\unexpanded\def\signalendofinput#1% argument just for tracing
192  {\global\settrue\endofinput}
193
194\unexpanded\def\honorendofinput
195  {\ifconditional\endofinput
196     \global\setfalse\endofinput
197     \endinput
198   \fi}
199
200\unexpanded\def\stopproject              {\clf_stopproject    \honorendofinput}
201\unexpanded\def\stopproduct              {\clf_stopproduct    \honorendofinput}
202\unexpanded\def\stopcomponent            {\clf_stopcomponent  \honorendofinput}
203\unexpanded\def\stopenvironment          {\clf_stopenvironment\honorendofinput}
204
205           \let\currentproject            \clf_currentproject
206           \let\currentproduct            \clf_currentproduct
207           \let\currentcomponent          \clf_currentcomponent
208           \let\currentenvironment        \clf_currentenvironment
209           \let\processedfile             \clf_processedfile
210           \let\processedfiles            \clf_processedfiles
211
212\unexpanded\def\dostarttextfile     #name{\clf_dostarttextfile{name}}
213\unexpanded\def\dostoptextfile           {\clf_dostoptextfile}
214
215\unexpanded\def\loadtexfile       [#name]{\clf_usetexfile{#name}}
216\unexpanded\def\loadluafile       [#name]{\clf_useluafile{#name}}
217\unexpanded\def\loadcldfile       [#name]{\clf_usecldfile{#name}}
218\unexpanded\def\loadanyfile       [#name]{\clf_useanyfile{#name}}
219
220\unexpanded\def\loadtexfileonce   [#name]{\clf_usetexfileonce{#name}}
221\unexpanded\def\loadluafileonce   [#name]{\clf_useluafileonce{#name}}
222\unexpanded\def\loadcldfileonce   [#name]{\clf_usecldfileonce{#name}}
223\unexpanded\def\loadanyfileonce   [#name]{\clf_useanyfileonce{#name}}
224
225%D Handy for modules that have a test/demo appended (true added).
226
227\unexpanded\def\continueifinputfile#name%
228  {\clf_doifelsecontinuewithfile{#name}\relax\endinput} % we cannot do \endinput via lua
229
230% \startproject test
231%     1: \startmode[*project] project \stopmode \endgraf
232%     2: \startmode[*product] product \stopmode \endgraf
233% \stopproject
234
235% replaced by modes:
236%
237% \setvalue{\e!start\v!localenvironment}[#1]%
238%   {\let\loadedlocalenvironments\empty
239%    \def\docommand##1%
240%      {\beforesplitstring##1\at.\to\someevironment
241%       \addtocommalist\someevironment\loadedlocalenvironments}%
242%    \processcommalist[#1]\docommand
243%    \doifcommonelse{\currentproject,\currentproduct,\currentcomponent,\currentenvironment}\loadedlocalenvironments
244%      {\letvalue{\e!stop\v!localenvironment}\relax}
245%      {\grabuntil{\e!stop\v!localenvironment}\gobbleoneargument}}
246%
247% \setvalue{\v!localenvironment}{\doifnextoptionalcselse\uselocalenvironment\redolocalenvironment}
248%
249% \def\redolocalenvironment#1 {\uselocalenvironment[#1]}
250% \def\uselocalenvironment[#1]{\doexecutefileonce{#1}}
251
252% weird place:
253
254\neverypar\emptytoks
255
256%D Wrappers (the signal is a synchronization with lua hack):
257
258\newsystemmode\v!project
259\newsystemmode\v!product
260\newsystemmode\v!component
261\newsystemmode\v!environment
262
263\unexpanded\def\startprojectindeed
264  {%starttext
265   \pushsystemmode\v!project
266   \setsystemmode\v!project}
267
268\unexpanded\def\stopprojectindeed
269  {\popsystemmode\v!project
270   %stoptext
271   \signalendofinput\v!project}
272
273\unexpanded\def\startproductindeed
274  {\starttext
275   \pushsystemmode\v!product
276   \setsystemmode\v!product}
277
278\unexpanded\def\stopproductindeed
279  {\popsystemmode\v!product
280   \stoptext
281   \signalendofinput\v!product}
282
283\unexpanded\def\startcomponentindeed
284  {\starttext
285   \pushreferenceprefix\currentcomponent
286   \pushsystemmode\v!component
287   \setsystemmode\v!component}
288
289\unexpanded\def\stopcomponentindeed
290  {\popsystemmode\v!component
291   \popreferenceprefix
292   \stoptext
293   \signalendofinput\v!component}
294
295\unexpanded\def\startenvironmentindeed
296  {\pushsystemmode\v!environment
297   \setsystemmode\v!environment}
298
299\unexpanded\def\stopenvironmentindeed
300  {\popsystemmode\v!environment
301   \signalendofinput\v!environment}
302
303%D Upgraded after the ctx 2020 meeting:
304%D
305%D \starttyping
306%D \definedocument[thesis]
307%D
308%D \setupdocument[a=b,e=f]
309%D \setupdocument[thesis][a=x,c=d]
310%D
311%D % \startsetups[thesis:start]
312%D \startsetups[document:start]
313%D     START
314%D \stopsetups
315%D
316%D \startsetups[thesis:stop]
317%D % \startsetups[document:stop]
318%D     STOP
319%D \stopsetups
320%D
321%D % \startdocument[thesis]
322%D \startthesis
323%D     (\currentdocument/a): \documentvariable{a}\par
324%D     (\currentdocument/c): \documentvariable{c}\par
325%D     (\currentdocument/e): \documentvariable{e}\par
326%D \stopthesis
327%D % \stopdocument
328%D \stoptyping
329
330\installcorenamespace {document}
331
332\installcommandhandler \??document \s!document \??document
333
334\setupdocument
335  [\c!before=\directsetup{\s!document:\v!start},
336   \c!after =\directsetup{\s!document:\v!stop}]
337
338\def\syst_document_setup#1%
339  {\directsetup{\doifelsesetups{\currentdocument:\v!start}\currentdocument\s!document:\v!start}}
340
341\appendtoks
342    \normalexpanded {%
343        \startsetups[\currentdocument:\v!start]\noexpand\directsetup{\ifx\empty\currentdocumentparent\s!document\else\currentdocumentparent\fi:\v!start}\stopsetups
344        \startsetups[\currentdocument:\v!stop ]\noexpand\directsetup{\ifx\empty\currentdocumentparent\s!document\else\currentdocumentparent\fi:\v!stop }\stopsetups
345        \setvalue{\e!start\currentdocument}{\dodoubleargument\syst_document_start[\currentdocument]}%
346        \letvalue{\e!stop \currentdocument}\stopdocument
347    }%
348\to \everydefinedocument
349
350\unexpanded\def\startdocument % todo: dostarttagged\t!document
351  {\dodoubleargument\syst_document_start}
352
353\unexpanded\def\syst_document_start[#namespace][#settings]%
354  {\pushmacro\currentdocument
355   \ifsecondargument
356     \edef\currentdocument{#namespace}%
357     \setupcurrentdocument[#settings]%
358   \else\iffirstargument
359     \doifelseassignment{#namespace}%
360       {\let\currentdocument\empty
361        \setupcurrentdocument[#namespace]}%
362       {\edef\currentdocument{#namespace}}%
363   \fi\fi
364   \ifx\empty\currentdocument\else\setmode\currentdocument\fi
365   \starttext
366   \the\everystartdocument
367   \documentvariable\c!before}
368
369\unexpanded\def\stopdocument % todo: dostoptagged\t!document
370  {\documentvariable\c!after
371   \the\everystopdocument
372   \stoptext
373   \ifx\empty\currentdocument\else\resetmode\currentdocument\fi
374   \popmacro\currentdocument}
375
376           \let\documentvariable           \documentparameter
377\unexpanded\def\unexpandeddocumentvariable{\documentparameter}
378
379\unexpanded\def\doifelsedocumentvariable#name{\doifelsesomething{\documentvariable{#name}}}
380\unexpanded\def\doifdocumentvariable    #name{\doifsomething    {\documentvariable{#name}}}
381\unexpanded\def\doifnotdocumentvariable #name{\doifnot          {\documentvariable{#name}}}
382
383\let\doifdocumentvariableelse\doifelsedocumentvariable
384
385\unexpanded\def\presetdocument
386  {\dodoubleargument\syst_document_preset}
387
388\unexpanded\def\syst_document_preset[#namespace][#settings]%
389  {\pushmacro\currentdocument
390   \ifsecondargument
391     \edef\currentdocument{#namespace}%
392     \setupcurrentdocument[#settings]%
393   \else\iffirstargument
394     \doifelseassignment{#namespace}%
395       {\let\currentdocument\empty
396        \setupcurrentdocument[#namespace]}%
397       {}%
398   \fi\fi
399   \popmacro\currentdocument}
400
401% Bonus:
402
403% \installcorenamespace{samplefile}
404%
405% \unexpanded\def\samplefile#1%
406%   {\ifcsname\??samplefile#1\endcsname \else
407%      \setxvalue{\??samplefile#1}{\cldloadfile{#1}}%
408%    \fi
409%    \lastnamedcs}
410
411\unexpanded\def\samplefile#1%
412  {\clf_samplefile{#1}}
413
414\protect \endinput
415