file-mod.mkvi /size: 10 Kb    last modification: 2020-07-01 14:35
1%D \module
2%D   [       file=file-mod, % was core-fil,
3%D        version=20110701, % 1997.11.15,
4%D          title=\CONTEXT\ File Macros,
5%D       subtitle=Module Support,
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 / Modules}
15
16\unprotect
17
18\registerctxluafile{file-mod}{}
19
20%D \macros
21%D   {usemodule}
22%D
23%D Most of \CONTEXT is preloaded in the format file. Some very domain specific
24%D typesetting topics are however dealt with in separate modules, e.g. typesetting
25%D of chemical structure formulas. These modules are loaded by:
26%D
27%D \showsetup{usemodule}
28%D
29%D More information on the specific modules can be found in their dedicated manuals.
30%D We use \type {\next} so that we can \type {\end} in modules.
31
32\unexpanded\def\usemodules
33  {\dotripleempty\strc_modules_use}
34
35\let\usemodule   \usemodules
36\let\usetexmodule\usemodules
37
38\def\strc_modules_use[#category][#name][#parameters]% category=t|m|x|p|...
39  {\push_macro_currentmodule
40   \push_macro_currentmodulecategory
41   \push_macro_currentmoduleparameters
42   \ifthirdargument
43     \edef\currentmodulecategory  {#category}%
44     \edef\currentmodule          {#name}%
45     \def \currentmoduleparameters{#parameters}%
46   \else\ifsecondargument
47     \doifelseassignment{#name}
48       {\let\currentmodulecategory   \empty
49        \edef\currentmodule          {#category}%
50        \edef\currentmoduleparameters{#name}}
51       {\edef\currentmodulecategory  {#category}%
52        \edef\currentmodule          {#name}%
53        \let \currentmoduleparameters\empty}%
54   \else
55     \let \currentmodulecategory  \empty
56     \edef\currentmodule          {#category}%
57     \let \currentmoduleparameters\empty
58   \fi\fi
59   \processcommacommand[\currentmodule]{\strc_modules_use_indeed\currentmodulecategory}%
60   \pop_macro_currentmoduleparameters
61   \pop_macro_currentmodulecategory
62   \pop_macro_currentmodule}
63
64\def\strc_modules_use_indeed#category#name%
65  {\ifx\currentmoduleparameters\empty\else
66     \scratchtoks\expandafter{\currentmoduleparameters}%
67     \normalexpanded{\getparameters[\??module#name:][\the\scratchtoks]}%
68   \fi
69   \clf_usemodules{#category}{#name}}
70
71\installcorenamespace{module}
72
73\let\currentmodule          \s!unknown
74\let\currentmodulecategory  \empty
75\let\currentmoduleparameters\empty
76
77\installmacrostack\currentmodule
78\installmacrostack\currentmodulecategory
79\installmacrostack\currentmoduleparameters
80
81\newcount\c_syst_modules_nesting
82\newtoks \everysetupmodule
83
84\unexpanded\def\startmodule
85  {\doifelsenextoptionalcs\syst_modules_start_yes\syst_modules_start_nop}
86
87\def\syst_modules_start_yes[#name]%
88  {\global\advance\c_syst_modules_nesting\plusone
89   \push_macro_currentmodule
90   \push_macro_currentmoduleparameters
91   \def\currentmodule{#name}}
92
93\def\syst_modules_start_nop#name %
94  {\syst_modules_start_yes[#name]}
95
96\unexpanded\def\stopmodule
97  {\ifcase\c_syst_modules_nesting
98     \writestatus\m!system{module wrapping error in '\currentmodule'}%
99   \else
100     \pop_macro_currentmoduleparameters
101     \pop_macro_currentmodule
102     \global\advance\c_syst_modules_nesting\minusone
103   \fi}
104
105\unexpanded\def\setupmodule
106  {\dodoubleempty\syst_modules_setup}
107
108\def\syst_modules_setup
109  {\ifx\currentmoduleparameters\empty
110      \expandafter\syst_modules_setup_nop
111   \else
112      \expandafter\syst_modules_setup_yes
113   \fi}
114
115\def\syst_modules_setup_nop[#name][#parameters]%
116  {\ifsecondargument
117     \getparameters[\??module#name:][#parameters]% internal (defaults)
118   \else\iffirstargument
119     \doifassignmentelse{#name}{\getparameters[\??module\currentmodule:][#name]}\donothing
120   \fi\fi
121   \the\everysetupmodule}
122
123\def\syst_modules_setup_yes[#name][#parameters]%
124  {\scratchtoks\expandafter{\currentmoduleparameters}%
125   \ifsecondargument
126     \getparameters[\??module#name:][#parameters]% internal (defaults)
127     \normalexpanded{\getparameters[\??module#name:][\the\scratchtoks]}% loadtime (user)
128   \else\iffirstargument
129     \doifassignmentelse{#name}
130       {\getparameters[\??module\currentmodule:][#name]% internal (defaults)
131        \normalexpanded{\getparameters[\??module\currentmodule:][\the\scratchtoks]}}% loadtime (user)
132       {\normalexpanded{\getparameters[\??module#1:][\the\scratchtoks]}}% loadtime (user)
133   \else
134     \normalexpanded{\getparameters[\??module\currentmodule:][\the\scratchtoks]}%
135   \fi\fi
136   \let\currentmoduleparameters\empty
137   \the\everysetupmodule}
138
139% to be tested:
140%
141% \def\syst_modules_setup[#name][#parameters]%
142%   {\ifsecondargument
143%      \getparameters[\??module#name:][#parameters]% internal (defaults)
144%      \normalexpanded{\getparameters[\??module#name:][\normalunexpanded\expandafter{\currentmoduleparameters}]}% loadtime (user)
145%    \else
146%      \getparameters[\??module\currentmodule:][#name]% internal (defaults)
147%      \normalexpanded{\getparameters[\??module\currentmodule:][\normalunexpanded\expandafter{\currentmoduleparameters}]}% loadtime (user)
148%    \fi
149%    \let\currentmoduleparameters\empty}
150
151\def\moduleparameter#name#parameter% should have been \namedmoduleparameter
152  {\begincsname\??module#name:#parameter\endcsname}
153
154\letvalue\??module\empty % so we default to empty as with all parameters
155
156\def\currentmoduleparameter{\moduleparameter\currentmodule} % no need for inlining
157
158\unexpanded\def\useluamodule  [#name]{\clf_loadluamodule{#1}} % why not use useluamodule
159\unexpanded\def\luaenvironment #name {\clf_loadluamodule{#1}}
160
161% \usemodule[newmml]
162% \usemodule[newmml][a=b]
163% \usemodule[x][newmml]
164% \usemodule[x][newmml][a=b]
165%
166% \startmodule [mathml]
167%   \setupmodule[a=c] \relax [\currentmoduleparameter{a}] % user vars will be set afterwards
168%   \setupmodule[a=c] \relax [\currentmoduleparameter{a}] % user vars are now forgotten
169% \stopmodule
170
171% one can introduce test sections with:
172%
173% \enablemode[newmml:test:\currentmoduleparameter{test}]
174% \startmode[newmml:test:yes} ... \stopmode
175%
176% these will be ignored unless test=yes
177%
178% however, a better way is:
179
180\unexpanded\def\startmoduletestsection
181  {\begingroup
182   \setupmodule % we need to make sure that the vars are set
183   \doifelse{\currentmoduleparameter\v!test}\v!yes
184     {\endgroup
185      \writestatus\currentmodule{loading experimental code}}
186     {\endgroup
187      \writestatus\currentmodule{skipping experimental code}%
188      \gobbleuntil\stopmoduletestsection}}
189
190\let\stopmoduletestsection\donothing
191
192% will become file-run
193
194%D To save memory, we implement some seldomly used commands in a lazy way. Nota
195%D bene: such runtime definitions are to be defined global.
196%D
197%D \starttyping
198%D \fetchruntimecommand \showaccents \f!file_run % last is a file name
199%D \stoptyping
200
201\installcorenamespace{runtimeloaded}
202
203\unexpanded\def\fetchruntimecommand#1#2%
204  {\unexpanded\def#1{\dofetchruntimecommand#1{#2}}}
205
206\def\dofetchruntimecommand#1#2% actually a test on #1 being define would be ok as well
207  {\ifcsname\??runtimeloaded#2\endcsname
208     % already loaded
209   \else
210     \glet#1\undefined
211     \startreadingfile
212     \startnointerference % \bgroup
213     \cleanupfeatures % better \setnormalcatcodes / test first
214     \readfile{#2.\mksuffix}\donothing\donothing
215     \stopnointerference  % \egroup
216     \stopreadingfile
217     \letgvalue{\??runtimeloaded#2}\empty
218   \fi
219   \ifx#1\undefined
220     \writestatus\m!system{command \string#1 not found in file #2}%
221     \unexpanded\gdef#1{{\infofont[unknown command \string#1]}}%
222   \fi
223   #1}
224
225%D \macros
226%D   {doifolderversionelse}
227%D
228%D We start with a macro specially for Aditya who wants to be able
229%D to use development versions of \MKIV\ for real documents.
230%D
231%D \starttyping
232%D \doifolderversionelse\contextversion{1010.10.10} {OLDER} {OKAY} => OLDER
233%D \doifolderversionelse\contextversion{2020.20.20} {OLDER} {OKAY} => OKAY
234%D \doifolderversionelse\contextversion{2020}       {OLDER} {OKAY} => OKAY
235%D \stoptyping
236%D
237%D The version pattern is \type {yyyy.mm.dd} (with mm and dd being optional).
238
239\unexpanded\def\doifelseolderversion#parent#child{\clf_doifelseolderversion{#parent}{#child}}
240\unexpanded\def\doifelseoldercontext       #child{\clf_doifelseolderversion{#child}{}}
241
242\let\doifolderversionelse\doifelseolderversion
243\let\doifoldercontextelse\doifelseoldercontext
244
245%D Relatively new (no need for a speedup here):
246
247\unexpanded\def\syst_modules_direct_lua#1#2%
248  {\edef\m_module_command_command {#1}%
249   \edef\m_module_command_function{#2}%
250   \directsetup{module:\m_module_command_command:start}%
251   \ctxlua{\m_module_command_function()}%
252   \directsetup{module:\m_module_command_command:stop}}
253
254\unexpanded\def\syst_modules_single_lua#1#2%
255  {\edef\m_module_command_command {#1}%
256   \edef\m_module_command_function{#2}%
257   \dosingleempty\syst_modules_single_lua_indeed}
258
259\unexpanded\def\syst_modules_single_lua_indeed[#1]%
260  {\directsetup{module:\m_module_command_command:start}%
261   \ctxlua{\m_module_command_function(\!!bs#1\!!es)}%
262   \directsetup{module:\m_module_command_command:stop}}
263
264\unexpanded\def\syst_modules_double_lua#1#2%
265  {\edef\m_module_command_command {#1}%
266   \edef\m_module_command_function{#2}%
267   \dodoubleempty\syst_modules_double_lua_indeed}
268
269\unexpanded\def\syst_modules_double_lua_indeed[#1][#2]%
270  {\directsetup{module:\m_module_command_command:start}%
271   \ctxlua{\m_module_command_function(\!!bs#1\!!es,\!!bs#2\!!es)}%
272   \directsetup{module:\m_module_command_command:stop}}
273
274\def\installmodulecommandlua      #1#2{\unexpanded\def#1{\normalexpanded{\syst_modules_direct_lua{\strippedcsname#1}{#2}}}}
275\def\installmodulecommandluasingle#1#2{\unexpanded\def#1{\normalexpanded{\syst_modules_single_lua{\strippedcsname#1}{#2}}}}
276\def\installmodulecommandluadouble#1#2{\unexpanded\def#1{\normalexpanded{\syst_modules_double_lua{\strippedcsname#1}{#2}}}}
277
278\unexpanded\def\syst_modules_one_lua#1#2#3%
279  {\directsetup{module:#1:start}%
280   \ctxlua{#2(\!!bs#3\!!es)}%
281   \directsetup{module:#1:stop}}
282
283\unexpanded\def\syst_modules_two_lua#1#2#3#4%
284  {\directsetup{module:#1:start}%
285   \ctxlua{#2(\!!bs#3\!!es,\!!bs#4\!!es)}%
286   \directsetup{module:#1:stop}}
287
288\def\installmodulecommandluaone#1#2{\unexpanded\def#1{\normalexpanded{\syst_modules_one_lua{\strippedcsname#1}{#2}}}}
289\def\installmodulecommandluatwo#1#2{\unexpanded\def#1{\normalexpanded{\syst_modules_two_lua{\strippedcsname#1}{#2}}}}
290
291% obsolete
292%
293% \def\documentresources{\@@erurl}
294%
295% \unexpanded\def\setupexternalresources
296%   {\dodoubleargument\getparameters[\??er]}
297%
298% \setupexternalresources
299%   [url=]
300
301% new:
302
303\unexpanded\def\useluamodule[#1]{\clf_useluamodule{#1}}
304
305\protect \endinput
306