file-mod.mklx /size: 9824 b    last modification: 2023-12-21 09:44
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}{autosuffix}
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% \enabledirectives[logs.errors=*]
33% \enabledirectives[logs.errors=missing modules]
34%
35% \usemodule[letsquit]
36%
37% \starttext
38%     test
39% \stoptext
40
41\installcorenamespace{module}
42
43\mutable\let\currentmodule\s!unknown
44
45\mutable\lettonothing\currentmodulecategory
46\mutable\lettonothing\currentmoduleparameters
47
48\installmacrostack\currentmodule
49\installmacrostack\currentmodulecategory
50\installmacrostack\currentmoduleparameters
51
52\permanent\tolerant\protected\def\usemodules[#category]#spacer[#S#name]#spacer[#S#parameters]% category=t|m|x|p|...
53  {\push_macro_currentmodule
54   \push_macro_currentmodulecategory
55   \push_macro_currentmoduleparameters
56   \ifparameters
57     \lettonothing\currentmodule
58   \or
59     \lettonothing\currentmodulecategory
60     \cdef\currentmodule{#category}%
61     \lettonothing\currentmoduleparameters
62   \or
63     \ifhastok={#name}%
64       \lettonothing\currentmodulecategory
65       \cdef\currentmodule{#category}%
66       \edef\currentmoduleparameters{#name}%
67     \else
68       \edef\currentmodulecategory{#category}%
69       \cdef\currentmodule{#name}%
70       \lettonothing\currentmoduleparameters
71     \fi
72   \or
73     \edef\currentmodulecategory  {#category}%
74     \cdef\currentmodule          {#name}%
75     \def \currentmoduleparameters{#parameters}%
76   \fi
77   \processcommacommand[\currentmodule]{\strc_modules_use\currentmodulecategory}%
78   \pop_macro_currentmoduleparameters
79   \pop_macro_currentmodulecategory
80   \pop_macro_currentmodule}
81
82\def\strc_modules_use#category#name%
83  {\ifempty\currentmoduleparameters\else
84     \scratchtoks\expandafter{\currentmoduleparameters}%
85     \normalexpanded{\getparameters[\??module#name:][\the\scratchtoks]}%
86   \fi
87   \clf_usemodules{#category}{#name}}
88
89\aliased\let\usemodule   \usemodules
90\aliased\let\usetexmodule\usemodules
91
92\newinteger\c_syst_modules_nesting
93\newtoks   \everysetupmodule
94
95\permanent\tolerant\protected\def\startmodule[#1]#;#2 %
96  {\global\advanceby\c_syst_modules_nesting\plusone
97   \push_macro_currentmodule
98   \push_macro_currentmoduleparameters
99   \cdef\currentmodule{#1#2}}
100
101\permanent\protected\def\stopmodule
102  {\ifcase\c_syst_modules_nesting
103     \writestatus\m!system{module wrapping error in '\currentmodule'}%
104   \else
105     \pop_macro_currentmoduleparameters
106     \pop_macro_currentmodule
107     \global\advanceby\c_syst_modules_nesting\minusone
108   \fi}
109
110\permanent\protected\def\setupmodule % to be lmtx'd
111  {\ifempty\currentmoduleparameters
112      \expandafter\syst_modules_setup_nop
113   \else
114      \expandafter\syst_modules_setup_yes
115   \fi}
116
117\tolerant\def\syst_modules_setup_nop[#S#name]#spacer[#S#parameters]%
118  {\ifarguments
119     % nothing
120   \or
121     \ifhastok={#name}%
122       \getparameters[\??module\currentmodule:][#name]%
123     \fi
124   \or
125     \getparameters[\??module#name:][#parameters]% internal (defaults)
126   \fi
127   \expand\everysetupmodule}
128
129\tolerant\def\syst_modules_setup_yes[#S#name]#spacer[#S#parameters]%
130  {\scratchtoks\expandafter{\currentmoduleparameters}%
131   \ifparameters
132     \normalexpanded{\getparameters[\??module\currentmodule:][\the\scratchtoks]}%
133   \or
134     \ifhastok={#name}%
135       \getparameters[\??module\currentmodule:][#name]% internal (defaults)
136       \normalexpanded{\getparameters[\??module\currentmodule:][\the\scratchtoks]}% loadtime (user)
137     \else
138       \normalexpanded{\getparameters[\??module#1:][\the\scratchtoks]}% loadtime (user)
139     \fi
140   \or
141     \getparameters[\??module#name:][#parameters]% internal (defaults)
142     \normalexpanded{\getparameters[\??module#name:][\the\scratchtoks]}% loadtime (user)
143   \fi
144   \lettonothing\currentmoduleparameters
145   \expand\everysetupmodule}
146
147\permanent\def\moduleparameter#name#parameter% should have been \namedmoduleparameter
148  {\begincsname\??module#name:#parameter\endcsname}
149
150\letvalue\??module\empty % so we default to empty as with all parameters
151
152\permanent\def\currentmoduleparameter{\moduleparameter\currentmodule} % no need for inlining
153
154%permanent\protected\def\useluamodule[#name]{\clf_loadluamodule{#1}} % why not use useluamodule
155
156\permanent\protected\def\useluamodule  [#name]{\clf_useluamodule{#1}}
157\permanent\protected\def\luaenvironment #name {\clf_loadluamodule{#1}}
158
159% \usemodule[newmml]
160% \usemodule[newmml][a=b]
161% \usemodule[x][newmml]
162% \usemodule[x][newmml][a=b]
163%
164% \startmodule [mathml]
165%   \setupmodule[a=c] \relax [\currentmoduleparameter{a}] % user vars will be set afterwards
166%   \setupmodule[a=c] \relax [\currentmoduleparameter{a}] % user vars are now forgotten
167% \stopmodule
168
169% one can introduce test sections with:
170%
171% \enablemode[newmml:test:\currentmoduleparameter{test}]
172% \startmode[newmml:test:yes} ... \stopmode
173%
174% these will be ignored unless test=yes
175%
176% however, a better way is:
177
178\permanent\protected\def\startmoduletestsection
179  {\begingroup
180   \setupmodule % we need to make sure that the vars are set
181   \ifcstok{\currentmoduleparameter\v!test}\v!yes
182     \endgroup
183     \writestatus\currentmodule{loading experimental code}%
184   \else
185     \endgroup
186     \writestatus\currentmodule{skipping experimental code}%
187     \expandafter\ignoreupto\expandafter\stopmoduletestsection
188   \fi}
189
190\aliased\let\stopmoduletestsection\donothing
191
192% will become file-run
193
194%D \macros
195%D   {doifolderversionelse}
196%D
197%D We start with a macro specially for Aditya who wants to be able
198%D to use development versions of \MKIV\ for real documents.
199%D
200%D \starttyping
201%D \doifolderversionelse\contextversion{1010.10.10} {OLDER} {OKAY} => OLDER
202%D \doifolderversionelse\contextversion{2020.20.20} {OLDER} {OKAY} => OKAY
203%D \doifolderversionelse\contextversion{2020}       {OLDER} {OKAY} => OKAY
204%D \stoptyping
205%D
206%D The version pattern is \type {yyyy.mm.dd} (with mm and dd being optional).
207
208\permanent\protected\def\doifelseolderversion#parent#child{\clf_doifelseolderversion{#parent}{#child}}
209\permanent\protected\def\doifelseoldercontext       #child{\clf_doifelseolderversion{#child}{}}
210
211\aliased\let\doifolderversionelse\doifelseolderversion
212\aliased\let\doifoldercontextelse\doifelseoldercontext
213
214%D Relatively new (no need for a speedup here). Can't this now be done nicer?
215
216\permanent\protected\def\syst_modules_direct_lua#1#2%
217  {\edef\m_module_command_command {#1}%
218   \edef\m_module_command_function{#2}%
219   \directsetup{module:\m_module_command_command:start}%
220   \ctxlua{\m_module_command_function()}%
221   \directsetup{module:\m_module_command_command:stop}}
222
223\permanent\protected\def\syst_modules_single_lua#1#2%
224  {\edef\m_module_command_command {#1}%
225   \edef\m_module_command_function{#2}%
226   \dosingleempty\syst_modules_single_lua_indeed}
227
228\permanent\protected\def\syst_modules_single_lua_indeed[#1]%
229  {\directsetup{module:\m_module_command_command:start}%
230   \ctxlua{\m_module_command_function(\!!bs#1\!!es)}%
231   \directsetup{module:\m_module_command_command:stop}}
232
233\permanent\protected\def\syst_modules_double_lua#1#2%
234  {\edef\m_module_command_command {#1}%
235   \edef\m_module_command_function{#2}%
236   \dodoubleempty\syst_modules_double_lua_indeed}
237
238\permanent\protected\def\syst_modules_double_lua_indeed[#1][#2]%
239  {\directsetup{module:\m_module_command_command:start}%
240   \ctxlua{\m_module_command_function(\!!bs#1\!!es,\!!bs#2\!!es)}%
241   \directsetup{module:\m_module_command_command:stop}}
242
243\permanent\protected\def\installmodulecommandlua      #1#2{\enforced\permanent\protected\def#1{\normalexpanded{\syst_modules_direct_lua{\csstring#1}{#2}}}}
244\permanent\protected\def\installmodulecommandluasingle#1#2{\enforced\permanent\protected\def#1{\normalexpanded{\syst_modules_single_lua{\csstring#1}{#2}}}}
245\permanent\protected\def\installmodulecommandluadouble#1#2{\enforced\permanent\protected\def#1{\normalexpanded{\syst_modules_double_lua{\csstring#1}{#2}}}}
246
247\protected\def\syst_modules_one_lua#1#2#3%
248  {\directsetup{module:#1:start}%
249   \ctxlua{#2(\!!bs#3\!!es)}%
250   \directsetup{module:#1:stop}}
251
252\protected\def\syst_modules_two_lua#1#2#3#4%
253  {\directsetup{module:#1:start}%
254   \ctxlua{#2(\!!bs#3\!!es,\!!bs#4\!!es)}%
255   \directsetup{module:#1:stop}}
256
257\permanent\protected\def\installmodulecommandluaone#1#2{\enforced\permanent\protected\def#1{\normalexpanded{\syst_modules_one_lua{\csstring#1}{#2}}}}
258\permanent\protected\def\installmodulecommandluatwo#1#2{\enforced\permanent\protected\def#1{\normalexpanded{\syst_modules_two_lua{\csstring#1}{#2}}}}
259
260%D This replaces \type {\fetchruntimecommand}:
261%D
262%D \starttyping
263%D \fetchmodulecommand \csname {module}
264%D \stoptyping
265
266\permanent\protected\def\fetchmodulecommand#1#2%
267  {\mutable\protected\def#1{\syst_fetch_module_command#1{#2}}}
268
269\def\syst_fetch_module_command#1#2% actually a test on #1 being define would be ok as well
270  {%writestatus\m!systems{fetching \string#1}%
271   \usemodule[#2]%
272   #1}
273
274\aliased\let\fetchruntimecommand\fetchmodulecommand % obsolete
275
276% \permanent\protected\def\moduleoverloaded
277%   {\enforced}
278
279\protect \endinput
280