summaryrefslogtreecommitdiff
path: root/doc/context/presentations/context/2021/context-2021-localcontrol.tex
blob: 72c1114be9f89ce51f397094f44aa546d03ba8b9 (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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
% language=us

% TAKE FROM LOWLEVEL-EXPANSION.TEX

\usemodule[system-tokens]

\usemodule[present-boring,abbreviations-logos]

\definehighlight[nb][style=bold,color=middlegray,define=no]

\startdocument
  [title={PROGRAMMING},
   banner={local control},
   location={context\enspace {\bf 2021}\enspace meeting}]

\starttitle[title=Expansion]

\TEX\ can be in several so called input reading modes:

Users mostly see it reading from the source file(s). Characters are picked up and
interpreted. Depending on what token it becomes some action takes place.

\starttyping
1 \setbox0\hbox to 10pt{2} \count0=3 \the\count0 \multiply\count0 by 4
\stoptyping

\startitemize

\startitem
    The \type {1} gets typeset because characters like that are seen as text.
\stopitem

\startitem
    The \type {\setbox} primitive triggers picking up a register number, then
    goes on scanning for a box specification and that itself will typeset a
    sequence of whatever until the group ends.
\stopitem

\startitem
    The \type {count} primitive triggers scanning for a register number (or
    reference) and then scans for a number; the equal sign is optional.
\stopitem

\startitem
    The \type {the} primitive injects some value into the current input stream
    (it does so by entering a new input level).
\stopitem

\startitem
    The \type {multiply} primitive picks up a register specification and
    multiplies that by the next scanned number. The \type {by} is optional.
\stopitem

\startitem
    Printing from \LUA\ and scanning tokens with e.g.\ \type {\scantokens} is like
    reading (pseudo) files.
\stopitem

\stopitemize

\stoptitle

\starttitle[title=Expansion]

\startbuffer[def]
\def\TestA {1 \setbox0\hbox{2} \count0=3 \the\count0}
\stopbuffer

\startbuffer[edef]
\edef\TestB{1 \setbox0\hbox{2} \count0=3 \the\count0}
\stopbuffer

\typebuffer[def]
\typebuffer[edef]

\getbuffer[def]
\getbuffer[edef]

\blank[2*big]

\startcolumns[n=2] \switchtobodyfont[8pt]
\luatokentable\TestA
\column
\luatokentable\TestB
\stopcolumns

\stoptitle

\starttitle[title=Local control]

\startbuffer[edef]
\edef\TestB{1 \setbox0\hbox{2} \count0=3 \the\count0}
\stopbuffer

\startbuffer[ldef]
\edef\TestC{1 \setbox0\hbox{2} \localcontrolled{\count0=3} \the\count0}
\stopbuffer

\typebuffer[edef]
\typebuffer[ldef]

\getbuffer[edef]
\getbuffer[ldef]

\blank[2*big]

\startcolumns[n=2] \switchtobodyfont[8pt]
\luatokentable\TestB
\column
\luatokentable\TestC
\stopcolumns

\stoptitle

\starttitle[title=Side effects]

\startbuffer[edef]
\edef\TestB{1 \setbox0\hbox{2} \count0=3 \the\count0}
\stopbuffer

\startbuffer[ldef]
\edef\TestD{\localcontrolled{1 \setbox0\hbox{2} \count0=3 \the\count0}}
\stopbuffer

\typebuffer[edef]
\typebuffer[ldef]

\getbuffer[edef]\getbuffer[ldef]\quad{\darkgray\leftarrow\space Watch how the results end up here!}

\blank[2*big]

\startcolumns[n=2] \switchtobodyfont[8pt]
\luatokentable\TestB
\column
\luatokentable\TestD
\stopcolumns

\stoptitle

\starttitle[title=Usage]

\startbuffer[def]
\def\WidthOf#1%
  {\beginlocalcontrol
   \setbox0\hbox{#1}%
   \endlocalcontrol
   \wd0 }
\stopbuffer

\startbuffer[use]
\scratchdimen\WidthOf{The Rite Of Spring}

\the\scratchdimen
\stopbuffer

\typebuffer[def]
\typebuffer[use]

\getbuffer[def]\getbuffer[use]

\stoptitle

\starttitle[title=Not always pretty]

\startbuffer[def]
\def\WidthOf#1%
  {\dimexpr
      \beginlocalcontrol
        \begingroup
          \setbox0\hbox{#1}%
          \expandafter
        \endgroup
      \expandafter
      \endlocalcontrol
      \the\wd0
   \relax}
\stopbuffer

\startbuffer[use]
\scratchdimen\WidthOf{The Rite Of Spring}

\the\scratchdimen
\stopbuffer

\typebuffer[def]
\typebuffer[use]

\getbuffer[def]\getbuffer[use]

\stoptitle

\starttitle[title=The \LUA\ end]

Right from the start the way to get something into \TEX\ from \LUA\ has been the
print functions. But we can also go local (immediate). There are several methods:

\startitemize
\startitem via a set token register \stopitem
\startitem via a defined macro \stopitem
\startitem via a string \stopitem
\stopitemize

Among the things to keep in mind are catcodes, scope and expansion (especially in
when the result itself ends up in macros).

\stoptitle

\starttitle[title=Via a token register]

% runlocal : macro|toks expand group

\startbuffer[set]
\toks0={\setbox0\hbox{The Rite Of Spring (Igor Stravinsky)}}
\toks2={\setbox0\hbox{The Rite Of Spring (Joe Parrish)}}
\stopbuffer

\typebuffer[set]

\startbuffer[run]
\startluacode
tex.runlocal(0) context("[1: %p]",tex.box[0].width)
tex.runlocal(2) context("[2: %p]",tex.box[0].width)
\stopluacode
\stopbuffer

\typebuffer[run]

\start \getbuffer[set,run] \stop

\stoptitle

\starttitle[title=Via a token macro]

\startbuffer[set]
\def\TestA{\setbox0\hbox{The Rite Of Spring (Igor Stravinsky)}}
\def\TestB{\setbox0\hbox{The Rite Of Spring (Joe Parrish)}}
\stopbuffer

\typebuffer[set]

\startbuffer[run]
\startluacode
tex.runlocal("TestA") context("[3: %p]",tex.box[0].width)
tex.runlocal("TestB") context("[4: %p]",tex.box[0].width)
\stopluacode
\stopbuffer

\typebuffer[run]

\start \getbuffer[set,run] \stop

\stoptitle

\starttitle[title=Via a string]

\startbuffer[run]
\startluacode
tex.runstring([[\setbox0\hbox{The Rite Of Spring (Igor Stravinsky)}]])

context("[5: %p]",tex.box[0].width)

tex.runstring([[\setbox0\hbox{The Rite Of Spring (Joe Parrish)}]])

context("[6: %p]",tex.box[0].width)
\stopluacode
\stopbuffer

\typebuffer[run]

\start \getbuffer[run] \stop

\blank[2*big]

A bit more high level:

\starttyping
context.runstring([[\setbox0\hbox{(Here \bf 1.2345)}]])
context.runstring([[\setbox0\hbox{(Here \bf   %.3f)}]],1.2345)
\stoptyping

\stoptitle

\starttitle[title=Locked in \LUA]

\startbuffer[run]
\startluacode
token.setmacro("TestX",[[\setbox0\hbox{The Rite Of Spring (Igor)}]])
tex.runlocal("TestX")
context("[7: %p]",tex.box[0].width)
\stopluacode
\stopbuffer

\typebuffer[run]

\start \getbuffer[run] \stop

\startbuffer[run]
\startluacode
tex.scantoks(0,tex.ctxcatcodes,[[\setbox0\hbox{The Rite Of Spring (Joe)}]])
tex.runlocal(0)
context("[8: %p]",tex.box[0].width)
\stopluacode
\stopbuffer

\typebuffer[run]

\start \getbuffer[run] \stop

\stoptitle

\starttitle[title=Order matters]

A lot this relates to pushing stuff into the input which is stacked. Compare:

\startbuffer[run]
\startluacode
context("[HERE 1]")
context("[HERE 2]")
\stopluacode
\stopbuffer

\typebuffer[run]

\start \getbuffer[run] \stop

with this:

\startbuffer[run]
\startluacode
tex.pushlocal() context("[HERE 1]") tex.poplocal()
tex.pushlocal() context("[HERE 2]") tex.poplocal()
\stopluacode
\stopbuffer

\typebuffer[run]

\start \getbuffer[run] \stop

% \startluacode
% tex.runlocal(function() context("[Here 1]") end)
% context("[Here 12]")
% tex.runlocal(function() context("[Here 2]") end)
% \stopluacode

% tex.pushlocal % swaps order
% tex.poplocal
% tex.quitlocal

% token.expandmacro

% str   : serialized
% true  : wrap next {}
% table : {serialzed} {serialzed} {serialzed}
% token : inject
% number: catcode table

% tex.expandasvalue:
%     (kind, like expand_macro)

% tex.runstring:
%  [catcode] string expand grouped

% tex.runlocal
%     function|number(register)|string(macro)|userdata(token) / expand / grouped

% mplib.expandtex
%     mpx, kind, macro, arguments

\stopdocument