summaryrefslogtreecommitdiff
path: root/tex/context/base/mkxl/file-mod.mklx
blob: a64529dd6d908062a5a8534035fdc1f9c97a238f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
%D \module
%D   [       file=file-mod, % was core-fil,
%D        version=20110701, % 1997.11.15,
%D          title=\CONTEXT\ File Macros,
%D       subtitle=Module Support,
%D         author=Hans Hagen,
%D           date=\currentdate,
%D      copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
%C
%C This module is part of the \CONTEXT\ macro||package and is
%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
%C details.

\writestatus{loading}{ConTeXt File Macros / Modules}

\unprotect

\registerctxluafile{file-mod}{autosuffix}

%D \macros
%D   {usemodule}
%D
%D Most of \CONTEXT is preloaded in the format file. Some very domain specific
%D typesetting topics are however dealt with in separate modules, e.g. typesetting
%D of chemical structure formulas. These modules are loaded by:
%D
%D \showsetup{usemodule}
%D
%D More information on the specific modules can be found in their dedicated manuals.
%D We use \type {\next} so that we can \type {\end} in modules.

% \enabledirectives[logs.errors=*]
% \enabledirectives[logs.errors=missing modules]
%
% \usemodule[letsquit]
%
% \starttext
%     test
% \stoptext

\installcorenamespace{module}

\mutable\let\currentmodule\s!unknown

\mutable\lettonothing\currentmodulecategory
\mutable\lettonothing\currentmoduleparameters

\installmacrostack\currentmodule
\installmacrostack\currentmodulecategory
\installmacrostack\currentmoduleparameters

\permanent\tolerant\protected\def\usemodules[#category]#spacer[#name]#spacer[#parameters]% category=t|m|x|p|...
  {\push_macro_currentmodule
   \push_macro_currentmodulecategory
   \push_macro_currentmoduleparameters
   \ifparameters
     \lettonothing\currentmodule
   \or
     \lettonothing\currentmodulecategory
     \edef\currentmodule{#category}%
     \lettonothing\currentmoduleparameters
   \or
     \ifhastok={#name}%
       \lettonothing\currentmodulecategory
       \edef\currentmodule{#category}%
       \edef\currentmoduleparameters{#name}%
     \else
       \edef\currentmodulecategory{#category}%
       \edef\currentmodule{#name}%
       \lettonothing\currentmoduleparameters
     \fi
   \or
     \edef\currentmodulecategory  {#category}%
     \edef\currentmodule          {#name}%
     \def \currentmoduleparameters{#parameters}%
   \fi
   \processcommacommand[\currentmodule]{\strc_modules_use\currentmodulecategory}%
   \pop_macro_currentmoduleparameters
   \pop_macro_currentmodulecategory
   \pop_macro_currentmodule}

\def\strc_modules_use#category#name%
  {\ifempty\currentmoduleparameters\else
     \scratchtoks\expandafter{\currentmoduleparameters}%
     \normalexpanded{\getparameters[\??module#name:][\the\scratchtoks]}%
   \fi
   \clf_usemodules{#category}{#name}}

\aliased\let\usemodule   \usemodules
\aliased\let\usetexmodule\usemodules

\newinteger\c_syst_modules_nesting
\newtoks   \everysetupmodule

\permanent\tolerant\protected\def\startmodule[#1]#;#2 %
  {\global\advanceby\c_syst_modules_nesting\plusone
   \push_macro_currentmodule
   \push_macro_currentmoduleparameters
   \def\currentmodule{#1#2}}

\permanent\protected\def\stopmodule
  {\ifcase\c_syst_modules_nesting
     \writestatus\m!system{module wrapping error in '\currentmodule'}%
   \else
     \pop_macro_currentmoduleparameters
     \pop_macro_currentmodule
     \global\advanceby\c_syst_modules_nesting\minusone
   \fi}

\permanent\protected\def\setupmodule % to be lmtx'd
  {\ifempty\currentmoduleparameters
      \expandafter\syst_modules_setup_nop
   \else
      \expandafter\syst_modules_setup_yes
   \fi}

\tolerant\def\syst_modules_setup_nop[#name]#spacer[#parameters]%
  {\ifarguments
     % nothing
   \or
     \doifassignmentelse{#name}{\getparameters[\??module\currentmodule:][#name]}\donothing
   \or
     \getparameters[\??module#name:][#parameters]% internal (defaults)
   \fi
   \the\everysetupmodule}

\tolerant\def\syst_modules_setup_yes[#name]#spacer[#parameters]%
  {\scratchtoks\expandafter{\currentmoduleparameters}%
   \ifparameters
     \normalexpanded{\getparameters[\??module\currentmodule:][\the\scratchtoks]}%
   \or
     \ifhastok={#name}%
       \getparameters[\??module\currentmodule:][#name]% internal (defaults)
       \normalexpanded{\getparameters[\??module\currentmodule:][\the\scratchtoks]}% loadtime (user)
     \else
       \normalexpanded{\getparameters[\??module#1:][\the\scratchtoks]}% loadtime (user)
     \fi
   \or
     \getparameters[\??module#name:][#parameters]% internal (defaults)
     \normalexpanded{\getparameters[\??module#name:][\the\scratchtoks]}% loadtime (user)
   \fi
   \lettonothing\currentmoduleparameters
   \the\everysetupmodule}

\permanent\def\moduleparameter#name#parameter% should have been \namedmoduleparameter
  {\begincsname\??module#name:#parameter\endcsname}

\letvalue\??module\empty % so we default to empty as with all parameters

\permanent\def\currentmoduleparameter{\moduleparameter\currentmodule} % no need for inlining

%permanent\protected\def\useluamodule[#name]{\clf_loadluamodule{#1}} % why not use useluamodule

\permanent\protected\def\useluamodule  [#name]{\clf_useluamodule{#1}}
\permanent\protected\def\luaenvironment #name {\clf_loadluamodule{#1}}

% \usemodule[newmml]
% \usemodule[newmml][a=b]
% \usemodule[x][newmml]
% \usemodule[x][newmml][a=b]
%
% \startmodule [mathml]
%   \setupmodule[a=c] \relax [\currentmoduleparameter{a}] % user vars will be set afterwards
%   \setupmodule[a=c] \relax [\currentmoduleparameter{a}] % user vars are now forgotten
% \stopmodule

% one can introduce test sections with:
%
% \enablemode[newmml:test:\currentmoduleparameter{test}]
% \startmode[newmml:test:yes} ... \stopmode
%
% these will be ignored unless test=yes
%
% however, a better way is:

\permanent\protected\def\startmoduletestsection
  {\begingroup
   \setupmodule % we need to make sure that the vars are set
   \doifelse{\currentmoduleparameter\v!test}\v!yes
     {\endgroup
      \writestatus\currentmodule{loading experimental code}}
     {\endgroup
      \writestatus\currentmodule{skipping experimental code}%
      \gobbleuntil\stopmoduletestsection}}

\aliased\let\stopmoduletestsection\donothing

% will become file-run

%D \macros
%D   {doifolderversionelse}
%D
%D We start with a macro specially for Aditya who wants to be able
%D to use development versions of \MKIV\ for real documents.
%D
%D \starttyping
%D \doifolderversionelse\contextversion{1010.10.10} {OLDER} {OKAY} => OLDER
%D \doifolderversionelse\contextversion{2020.20.20} {OLDER} {OKAY} => OKAY
%D \doifolderversionelse\contextversion{2020}       {OLDER} {OKAY} => OKAY
%D \stoptyping
%D
%D The version pattern is \type {yyyy.mm.dd} (with mm and dd being optional).

\permanent\protected\def\doifelseolderversion#parent#child{\clf_doifelseolderversion{#parent}{#child}}
\permanent\protected\def\doifelseoldercontext       #child{\clf_doifelseolderversion{#child}{}}

\aliased\let\doifolderversionelse\doifelseolderversion
\aliased\let\doifoldercontextelse\doifelseoldercontext

%D Relatively new (no need for a speedup here). Can't this now be done nicer?

\permanent\protected\def\syst_modules_direct_lua#1#2%
  {\edef\m_module_command_command {#1}%
   \edef\m_module_command_function{#2}%
   \directsetup{module:\m_module_command_command:start}%
   \ctxlua{\m_module_command_function()}%
   \directsetup{module:\m_module_command_command:stop}}

\permanent\protected\def\syst_modules_single_lua#1#2%
  {\edef\m_module_command_command {#1}%
   \edef\m_module_command_function{#2}%
   \dosingleempty\syst_modules_single_lua_indeed}

\permanent\protected\def\syst_modules_single_lua_indeed[#1]%
  {\directsetup{module:\m_module_command_command:start}%
   \ctxlua{\m_module_command_function(\!!bs#1\!!es)}%
   \directsetup{module:\m_module_command_command:stop}}

\permanent\protected\def\syst_modules_double_lua#1#2%
  {\edef\m_module_command_command {#1}%
   \edef\m_module_command_function{#2}%
   \dodoubleempty\syst_modules_double_lua_indeed}

\permanent\protected\def\syst_modules_double_lua_indeed[#1][#2]%
  {\directsetup{module:\m_module_command_command:start}%
   \ctxlua{\m_module_command_function(\!!bs#1\!!es,\!!bs#2\!!es)}%
   \directsetup{module:\m_module_command_command:stop}}

\permanent\protected\def\installmodulecommandlua      #1#2{\enforced\permanent\protected\def#1{\normalexpanded{\syst_modules_direct_lua{\csstring#1}{#2}}}}
\permanent\protected\def\installmodulecommandluasingle#1#2{\enforced\permanent\protected\def#1{\normalexpanded{\syst_modules_single_lua{\csstring#1}{#2}}}}
\permanent\protected\def\installmodulecommandluadouble#1#2{\enforced\permanent\protected\def#1{\normalexpanded{\syst_modules_double_lua{\csstring#1}{#2}}}}

\protected\def\syst_modules_one_lua#1#2#3%
  {\directsetup{module:#1:start}%
   \ctxlua{#2(\!!bs#3\!!es)}%
   \directsetup{module:#1:stop}}

\protected\def\syst_modules_two_lua#1#2#3#4%
  {\directsetup{module:#1:start}%
   \ctxlua{#2(\!!bs#3\!!es,\!!bs#4\!!es)}%
   \directsetup{module:#1:stop}}

\permanent\protected\def\installmodulecommandluaone#1#2{\enforced\permanent\protected\def#1{\normalexpanded{\syst_modules_one_lua{\csstring#1}{#2}}}}
\permanent\protected\def\installmodulecommandluatwo#1#2{\enforced\permanent\protected\def#1{\normalexpanded{\syst_modules_two_lua{\csstring#1}{#2}}}}

%D This replaces \type {\fetchruntimecommand}:
%D
%D \starttyping
%D \fetchmodulecommand \csname {module}
%D \stoptyping

\permanent\protected\def\fetchmodulecommand#1#2%
  {\mutable\protected\def#1{\syst_fetch_module_command#1{#2}}}

\def\syst_fetch_module_command#1#2% actually a test on #1 being define would be ok as well
  {%writestatus\m!systems{fetching \string#1}%
   \usemodule[#2]%
   #1}

\aliased\let\fetchruntimecommand\fetchmodulecommand % obsolete

% \permanent\protected\def\moduleoverloaded
%   {\enforced}

\protect \endinput