file-job.mklx /size: 11 Kb    last modification: 2023-12-21 09:44
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\writestatus{loading}{ConTeXt File Macros / Job Handling}
15
16\unprotect
17
18%D This module delegates most of the work to \LUA\ and therefore also let it
19%D define the commands, which is more efficient.
20
21\ifdefined\textlevel \else \newinteger\textlevel \fi % used at lua end
22
23\registerctxluafile{data-hsh}{autosuffix}
24\registerctxluafile{file-job}{autosuffix}
25
26%D Here are some helpers for processing and path control. In the following example
27%D the lookup order is \type {.}, {/foo}, \type {foo/bar} as one can always
28%D explicitly prefix a wanted file.
29%D
30%D \starttyping
31%D \pushpath[foo]
32%D     \pushpath[foo/bar]
33%D         \input test
34%D     \poppath
35%D \poppath
36%D \stoptyping
37
38% \usepath           [#path] % defined at lua end
39% \usesubpath        [#path] % defined at lua end
40% \pushpath          [#path] % defined at lua end
41% \poppath                   % defined at lua end
42% \resetpath                 % defined at lua end (not the pushed paths)
43% \allinputpaths             % defined at lua end
44% \doifelseinputfile #name   % defined at lua end
45% \processfile       #name   % defined at lua end
46
47\aliased\let\doifinputfileelse\doifelseinputfile
48
49% helper (not really needed nowadays)
50
51\mutable\lettonothing\locatedfilepath
52
53\permanent\protected\def\locatefilepath#name%
54  {\edef\locatedfilepath{\clf_locatefilepath{#name}}} % todo: set at the lua end, catcodes and such
55
56% % maybe:
57%
58% % \dirlist{c:/data/temporary/foo}{images/bar.jpg}
59% % \dirfile{c:/data/temporary/foo}{images/bar.jpg}
60%
61% \def\dirlist#1#2{dirlist://#1/**/#2}
62% \def\dirfile#1#2{dirlist://#1/**/#2?option=fileonly}
63
64% zip files are tree'd
65% exa stuff might become obsolete:
66
67% \usezipfile   [#zipname]#spacer[#subtree] % defined at lua end
68% \loadexamodes [#name]                     % defined at lua end
69
70\protected\def\syst_files_load
71  {\syst_files_load_indeed\f!newfilename  % new code, to be integrated at some point, plus fixes posted on the list
72   \syst_files_load_indeed\f!locfilename  % new code, somewhat experimental, not distributed (outside the dev group)
73   \syst_files_load_indeed\f!expfilename  % new code, very experimental, can be engine specific, mostly for me only
74   \syst_files_load_indeed\f!sysfilename  % local settings, but probably not that good an idea to use
75  %\syst_files_load_indeed\f!fntfilename  % maybe some day, can load goodies and patches
76  }
77
78\def\syst_files_load_indeed#name% from now on we assume a suffix to be part of the name
79  {\readsysfile{#name}{\showmessage\m!system2{#name}}\donothing}
80
81% document structure
82
83\ifdefined\strc_pagenumbers_flush_final_page \else \let\strc_pagenumbers_flush_final_page\relax \fi % ugly
84
85\permanent\protected\def\dostarttext
86  {\enforced\glet\dostarttext\relax
87   \expand\everystarttext
88   \global\everystarttext\emptytoks}
89
90\permanent\protected\def\dostoptext
91  {\enforced\glet\dostoptext\relax
92   \strc_pagenumbers_flush_final_page
93   \page % hm, bonus
94   \expand\everystoptext
95   \global\everystoptext\emptytoks
96   \expand\everybye
97   \global\everybye\emptytoks
98   \expand\everygoodbye
99   \global\everygoodbye\emptytoks
100   \ifempty\everynotabene\else
101     \writeline
102     \expand\everynotabene
103     \writeline
104   \fi
105   % tex's \end:
106   \normalend}
107
108% \starttext     % defined at lua end
109% \stoptext      % defined at lua end
110% \forcequitjob  % defined at lua end
111% \autostarttext % defined at lua end
112% \autostoptext  % defined at lua end
113
114\pushoverloadmode
115    \aliased\let\end\forceendjob
116\popoverloadmode
117
118\aliased\let\finishjob\stoptext % nicer in luatex call commandline
119
120\newtoks\everystartnotext
121\newtoks\everystopnotext
122
123\permanent\protected\def\startnotext{\expand\everystartnotext}
124\permanent\protected\def\stopnotext {\expand\everystopnotext\normalend}
125
126% protect structure
127
128% \processfilemany  {#name} % defined at lua end
129% \processfileonce  {#name} % defined at lua end
130% \processfilenone  {#name} % defined at lua end
131
132% \useproject       [#name] % defined at lua end
133% \useproduct       [#name] % defined at lua end
134% \usecomponent     [#name] % defined at lua end
135% \useenvironment   [#name] % defined at lua end
136
137% \project          [#name] or #name<space> % defined at lua end
138% \product          [#name] or #name<space> % defined at lua end
139% \component        [#name] or #name<space> % defined at lua end
140% \environment      [#name] or #name<space> % defined at lua end
141
142% \startproject     [#name] or #name<space> % defined at lua end
143% \startproduct     [#name] or #name<space> % defined at lua end
144% \startcomponent   [#name] or #name<space> % defined at lua end
145% \startenvironment [#name] or #name<space> % defined at lua end
146
147% \currentproject            % defined at lua end
148% \currentproduct            % defined at lua end
149% \currentcomponent          % defined at lua end
150% \currentenvironment        % defined at lua end
151% \processedfile             % defined at lua end
152% \processedfiles            % defined at lua end
153%
154% \dostarttextfile    #name  % defined at lua end
155% \dostoptextfile            % defined at lua end
156%
157% \loadtexfile       [#name] % defined at lua end
158% \loadluafile       [#name] % defined at lua end
159% \loadcldfile       [#name] % defined at lua end
160% \loadanyfile       [#name] % defined at lua end
161%
162% \loadtexfileonce   [#name] % defined at lua end
163% \loadluafileonce   [#name] % defined at lua end
164% \loadcldfileonce   [#name] % defined at lua end
165% \loadanyfileonce   [#name] % defined at lua end
166
167%D Handy for modules that have a test/demo appended (true added).
168
169% \permanent\protected\def\continueifinputfile#name{\clf_doifelsecontinuewithfile{#name}\relax\endinput} % we cannot do \endinput via lua
170
171% \continueifinputfile #name % defined at lua end
172
173% \startproject test
174%     1: \startmode[*project] project \stopmode \endgraf
175%     2: \startmode[*product] product \stopmode \endgraf
176% \stopproject
177
178% A bit weird place:
179
180\neverypar\emptytoks
181
182%D Wrappers. The \type {\endinput} is tricky because we call macros at the \LUA\
183%D end. In \LMTX\ we now do an immediate one, so the ending happens here now. It
184%D is different that in \MKIV, but you can compare the code.
185
186\newsystemmode\v!project
187\newsystemmode\v!product
188\newsystemmode\v!component
189\newsystemmode\v!environment
190
191\permanent\protected\defcsname\e!start:\v!project:\v!global\endcsname
192  {%starttext
193   \pushsystemmode\v!project
194   \setsystemmode\v!project}
195
196\permanent\protected\defcsname\e!stop:\v!project:\v!global\endcsname
197  {\popsystemmode\v!project
198   %stoptext
199   \endinput}
200
201\permanent\protected\defcsname\e!start:\v!product:\v!global\endcsname
202  {\starttext
203   \pushsystemmode\v!product
204   \setsystemmode\v!product}
205
206\permanent\protected\defcsname\e!stop:\v!product:\v!global\endcsname
207  {\popsystemmode\v!product
208   \stoptext
209   \endinput}
210
211\permanent\protected\defcsname\e!start:\v!component:\v!global\endcsname
212  {\starttext
213   \pushreferenceprefix\currentcomponent
214   \pushsystemmode\v!component
215   \setsystemmode\v!component}
216
217\permanent\protected\defcsname\e!stop:\v!component:\v!global\endcsname
218  {\popsystemmode\v!component
219   \popreferenceprefix
220   \stoptext
221   \endinput}
222
223\permanent\protected\defcsname\e!start:\v!component:\v!local\endcsname
224  {\starttext
225   \pushreferenceprefix\currentcomponent
226   \pushsystemmode\v!component
227   \setsystemmode\v!component}
228
229\permanent\protected\defcsname\e!stop:\v!component:\v!local\endcsname
230  {\popsystemmode\v!component
231   \popreferenceprefix
232   \stoptext}
233
234\permanent\protected\defcsname\e!start:\v!environment:\v!global\endcsname
235  {\pushsystemmode\v!environment
236   \setsystemmode\v!environment}
237
238\permanent\protected\defcsname\e!stop:\v!environment:\v!global\endcsname
239  {\popsystemmode\v!environment
240   \endinput}
241
242%D Upgraded after the ctx 2020 meeting:
243%D
244%D \starttyping
245%D \definedocument[thesis]
246%D
247%D \setupdocument[a=b,e=f]
248%D \setupdocument[thesis][a=x,c=d]
249%D
250%D % \startsetups[thesis:start]
251%D \startsetups[document:start]
252%D     START
253%D \stopsetups
254%D
255%D \startsetups[thesis:stop]
256%D % \startsetups[document:stop]
257%D     STOP
258%D \stopsetups
259%D
260%D % \startdocument[thesis]
261%D \startthesis
262%D     (\currentdocument/a): \documentvariable{a}\par
263%D     (\currentdocument/c): \documentvariable{c}\par
264%D     (\currentdocument/e): \documentvariable{e}\par
265%D \stopthesis
266%D % \stopdocument
267%D \stoptyping
268
269\installcorenamespace {document}
270
271\installcommandhandler \??document \s!document \??document
272
273\setupdocument
274  [\c!before=\directsetup{\s!document:\v!start},
275   \c!after =\directsetup{\s!document:\v!stop}]
276
277% to be discussed:
278%
279% \setupdocument
280%   [metadata:title=\documentvariable\c!title,
281%    metadata:subject=\documentvariable\c!subtitle,
282%    metadata:subtitle=\documentvariable\c!subtitle,
283%    metadata:author=\documentvariable\c!author,
284%    metadata:authors=\documentvariable\c!author,
285%    metadata:keyword=\documentvariable\c!keyword,
286%    metadata:keywords=\documentvariable\c!keyword,
287%    metadata:date=\documentvariable\c!date]
288
289\def\syst_document_setup#1%
290  {\directsetup{\doifelsesetups{\currentdocument:\v!start}\currentdocument\s!document:\v!start}}
291
292\appendtoks
293    \normalexpanded {%
294        \startsetups[\currentdocument:\v!start]\noexpand\directsetup{\ifempty\currentdocumentparent\s!document\else\currentdocumentparent\fi:\v!start}\stopsetups
295        \startsetups[\currentdocument:\v!stop ]\noexpand\directsetup{\ifempty\currentdocumentparent\s!document\else\currentdocumentparent\fi:\v!stop }\stopsetups
296        \permanent\defcsname\e!start\currentdocument\endcsname{\startdocument[\currentdocument]}%
297        \permanent\defcsname\e!stop \currentdocument\endcsname{\stopdocument}
298    }%
299\to \everydefinedocument
300
301% todo: dostarttagged\t!document
302
303\permanent\tolerant\protected\def\startdocument[#S#namespace]#spacer[#S#settings]%
304  {\pushmacro\currentdocument
305   \ifparameters
306   \or
307     \ifhastok={#namespace}%
308       \lettonothing\currentdocument
309       \setupcurrentdocument[#namespace]%
310     \else
311       \cdef\currentdocument{#namespace}%
312     \fi
313   \or
314     \cdef\currentdocument{#namespace}%
315     \setupcurrentdocument[#settings]%
316   \fi
317   \ifempty\currentdocument\else\setmode\currentdocument\fi
318   \starttext
319   \expand\everystartdocument
320   \documentvariable\c!before}
321
322\permanent\protected\def\stopdocument % todo: dostoptagged\t!document
323  {\documentvariable\c!after
324   \expand\everystopdocument
325   \stoptext
326   \ifempty\currentdocument\else\resetmode\currentdocument\fi
327   \popmacro\currentdocument}
328
329\aliased            \let\documentvariable           \documentparameter
330\permanent\protected\def\unexpandeddocumentvariable{\documentparameter}
331
332\permanent\protected\def\doifelsedocumentvariable#name{\doifelsesomething{\documentvariable{#name}}}
333\permanent\protected\def\doifdocumentvariable    #name{\doifsomething    {\documentvariable{#name}}}
334\permanent\protected\def\doifnotdocumentvariable #name{\doifnot          {\documentvariable{#name}}}
335
336\aliased\let\doifdocumentvariableelse\doifelsedocumentvariable
337
338\permanent\tolerant\protected\def\presetdocument[#S#namespace]#spacer[#S#settings]%
339  {\pushmacro\currentdocument
340   \ifparameters
341   \or
342     \ifhastok={#namespace}%
343       \lettonothing\currentdocument
344       \setupcurrentdocument[#namespace]%
345     \fi
346   \or
347     \cdef\currentdocument{#namespace}%
348     \setupcurrentdocument[#settings]%
349   \fi
350   \popmacro\currentdocument}
351
352% Bonus:
353
354% \installcorenamespace{samplefile}
355%
356% \protected\def\samplefile#1%
357%   {\ifcsname\??samplefile#1\endcsname \else
358%      \xdefcsname\??samplefile#1\endcsname{\cldloadfile{#1}}%
359%    \fi
360%    \lastnamedcs}
361
362% \permanent\protected\def\samplefile#1{\clf_samplefile{#1}} % defined at the lua end
363
364% \adddefaultsuffix[foo,{},crap]
365% \adddefaultsuffix[{}]
366%
367% \typefile{./crapcrap}
368% \typefile{crapcrap}
369
370
371\protect \endinput
372