hybrid-mkvi.tex /size: 7450 b    last modification: 2023-12-21 09:43
1% language=us
2
3\startcomponent hybrid-mkvi
4
5\environment hybrid-environment
6
7\startchapter[title={Upto \ConTeXt\ MkVI}]
8
9\startsection [title={Introduction}]
10
11No, this is not a typo: \MKVI\ is the name of upcoming functionality but with an
12experimental character. It is also a playground. Therefore this is not the final
13story.
14
15\stopsection
16
17\startsection [title={Defining macros}]
18
19When you define macros in \TEX, you use the \type {#} to indicate variables. So,
20you code can end up with the following:
21
22\startbuffer
23\def\MyTest#1#2#3#4%
24  {\dontleavehmode
25   \dostepwiserecurse{#1}{#2}{#3}
26     {\ifnum\recurselevel>#1 \space,\fi
27      \recurselevel: #4\space}%
28   .\par}
29\stopbuffer
30
31\typebuffer
32
33This macro is called with 4 arguments:
34
35\starttyping
36\MyTest{3}{8}{1}{Hi}
37\stoptyping
38
39However, using numbers as variable identifiers might not have your preference. It
40makes perfect sense if you keep in mind that \TEX\ supports delimited arguments
41using arbitrary characters. But in practice, and especially in \CONTEXT\ we use
42only a few well defined variants. \ This is why you can also imagine:
43
44\startbuffer
45\def\MyTest#first#last#step#text%
46  {\dontleavehmode
47   \dostepwiserecurse{#first}{#last}{#step}
48     {\ifnum\recurselevel>#first \space,\fi
49      \recurselevel: #text}%
50   .\par}
51\stopbuffer
52
53\typebuffer
54
55In order for this to work, you need to give your file the suffix \type {mkvi} or
56you need to put a directive on the first line:
57
58\starttyping
59% macros=mkvi
60\stoptyping
61
62You can of course use delimited arguments as well, given that
63the delimiters are not letters.
64
65\startbuffer
66\def\TestOne[#1]%
67  {this is: #1}
68
69\def\TestTwo#some%
70  {this is: #some}
71
72\def\TestThree[#whatever][#more]%
73  {this is: #more and #whatever}
74
75\def\TestFour[#one]#two%
76  {\def\TestFive[#alpha][#one]%
77     {#one, #two, #alpha}}
78\stopbuffer
79
80\typebuffer \mkvibuffer
81
82You can also use the following variant which is already present for a while but
83not that much advertised. This method ignores all spaces in definitions so if you
84need one, you have to use \type {\space}.
85
86\startbuffer
87\starttexdefinition TestSix #oeps
88
89  here: #oeps
90
91\stoptexdefinition
92\stopbuffer
93
94\typebuffer \mkvibuffer
95
96These commands work as expected:
97
98\startbuffer
99\startlines
100  \TestOne  [one]
101  \TestTwo  {one}
102  \TestThree[one][two]
103  \TestFour [one]{two}
104  \TestFive [one][two]
105  \TestSix  {one}
106\stoplines
107\stopbuffer
108
109\typebuffer
110
111% We need to obey catcode changes (we can use \getbuffer
112% instead).
113
114\getbuffer
115
116You can use buffers to collect definitions. In that case you can force
117preprocessing of the buffer with \type {\mkvibuffer[name]}.
118
119\stopsection
120
121\startsection[title={Implementation}]
122
123This functionality is not hard codes in the \LUATEX\ engine as this is not needed
124at all. We just preprocess the file before it gets loaded and this is something
125that is relatively easy to implement. Already early in the development of
126\LUATEX\ we have decided that instead of hard coding solutions, opening up makes
127more sense.
128
129One of the first mechanisms that were opened up was file IO. This means that when
130a file is opened, you can decide to intercept lines and process them before
131passing them to the traditional built in input parser. The user can be completely
132unaware of this. In fact, as \LUATEX\ only accepts \UTF-8 preprocessing will
133likely happen already when other input encodings are used.
134
135The following helper functions are available:
136
137\starttyping
138local result = resolvers.macros.preprocessed(str)
139\stoptyping
140
141This function returns a string with all named parameters
142replaced.
143
144\starttyping
145resolvers.macros.convertfile(oldname,newname)
146\stoptyping
147
148This function converts a file into a new one.
149
150\starttyping
151local result = resolvers.macros.processmkvi(str,filename)
152\stoptyping
153
154This function converts the string but only if the suffix of the filename is \type
155{mkvi} or when the first line of the string is a comment line containing \type
156{macros=mkvi}. Otherwise the original string is returned. The filename is
157optional.
158
159\stopsection
160
161\startsection[title=A few details]
162
163Imagine that you want to do this:
164
165\starttyping
166\def\test#1{before#1after}
167\stoptyping
168
169When we use names this could look like:
170
171\starttyping
172\def\test#inbetween{before#inbetweenafter}
173\stoptyping
174
175and that is not going to work out well. We could be more liberal with spaces,
176like
177
178\starttyping
179\def\test #inbetween {before #inbetween after}
180\stoptyping
181
182but then getting spaces in the output before or after variables would get more
183complex. However, there is a way out:
184
185\starttyping
186\def\test#inbetween{before#{inbetween}after}
187\stoptyping
188
189As the sequence \type +#{+ has a rather low probablility of showing up in a \TEX\
190source file, this kind of escaping is part of the game. So, all the following
191cases are valid:
192
193\starttyping
194\def\test#oeps{... #oeps ...}
195\def\test#oeps{... #{oeps} ...}
196\def\test#{main:oeps}{... #{main:oeps} ...}
197\def\test#{oeps:1}{... #{oeps:1} ...}
198\def\test#{oeps}{... #oeps ...}
199\stoptyping
200
201When you use the braced variant, all characters except braces are acceptable as
202name, optherwise only lowercase and uppercase characters are permitted.
203
204Normally \TEX\ uses a couple of special tokens like \type {^} and \type {_}. In a
205macro definition file you can avoid these by using primitives:
206
207\starttabulate[|cT|lT|]
208\NC \letterampersand           \NC \tex{aligntab}          \NC \NR
209\NC \letterhash                \NC \tex{alignmark}         \NC \NR
210\NC \letterhat                 \NC \tex{Usuperscript}      \NC \NR
211\NC \letterunderscore          \NC \tex{Usubscript}        \NC \NR
212\NC \letterdollar              \NC \tex{Ustartmath}        \NC \NR
213\NC \letterdollar              \NC \tex{Ustopmath}         \NC \NR
214\NC \letterdollar\letterdollar \NC \tex{Ustartdisplaymath} \NC \NR
215\NC \letterdollar\letterdollar \NC \tex{Ustopdisplaymath}  \NC \NR
216\stoptabulate
217
218Especially the \type {aligntab} is worth noticing: using that one directly in a
219macro definition can result in unwanted replacements, depending whether a match
220can be found. In practice the following works out well
221
222\starttyping
223\def\test#oeps{test:#oeps \halign{##\cr #oeps\cr}}
224\stoptyping
225
226You can use \UTF-8\ characters as well. For practical reasons this is only
227possible with the braced variant.
228
229\starttyping
230\def\blä#{blá}{blà:#{blá}}
231\stoptyping
232
233There will probably be more features in future versions but each of them needs
234careful consideration in order to prevent interferences.
235
236\stopsection
237
238\startsection[title=Utilities]
239
240There is currently one utility (or in fact an option to an existing utility):
241
242\starttyping
243mtxrun --script interface --preprocess whatever.mkvi
244\stoptyping
245
246This will convert the given file(s) to new ones, with the default suffix
247\type{tex}. Existing files will not be overwritten unless \type {---force} is
248given. You can also force another suffix:
249
250\starttyping
251mtxrun --script interface --preprocess whatever.mkvi --suffix=mkiv
252\stoptyping
253
254A rather plain module \type {luatex-preprocessor.lua} is provided for other
255usage. That variant provides a somewhat simplified version.
256
257Given that you have a \type {luatex-plain} format you can run:
258
259\starttyping
260luatex --fmt=luatex-plain luatex-preprocessor-test.tex
261\stoptyping
262
263Such a plain format can be made with:
264
265\starttyping
266luatex --ini luatex-plain
267\stoptyping
268
269You probably need to move the format to a proper location in your \TEX\ tree.
270
271\stopsection
272
273\stopchapter
274
275\stopcomponent
276