summaryrefslogtreecommitdiff
path: root/doc/context/sources/general/manuals/cld/cld-specialcommands.tex
blob: 37ecf45c6846b85068c590cc2429318139c3e796 (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
% language=uk

\startcomponent cld-specialcommands

\environment cld-environment

\startchapter[title=Special commands]

\index{tracing}

\startsection[title=Tracing]

There are a few functions in the \type {context} namespace that are no
macros at the \TEX\ end.

\starttyping
context.runfile("somefile.cld")
\stoptyping

Another useful command is:

\starttyping
context.settracing(true)
\stoptyping

There are a few tracing options that you can set at the \TEX\ end:

\starttyping
\enabletrackers[context.files]
\enabletrackers[context.trace]
\stoptyping

\stopsection

\startsection[title=Overloads]

A few macros have special functions (overloads) at the \LUA\ end. One of them is
\type {\char}. The function makes sure that the characters ends up right. The
same is true for \type {\chardef}. So, you don't need to mess around with \type
{\relax} or trailing spaces as you would do at the \TEX\ end in order to tell the
scanner to stop looking ahead.

\starttyping
context.char(123)
\stoptyping

Other examples of macros that have optimized functions are \type {\par},
\type{\bgroup} and \type {\egroup}. Or take this:

\startbuffer
1: \ctxlua{commands.doif(true)}{one}
2: \cldcommand{doif("a","a","two")}
3: \ctxcommand{doif(true)}{three}
\stopbuffer

\typebuffer

\startlines
\getbuffer
\stoplines

\stopsection

\startsection[title=Steps]

% added and extended in sync with an article about a generic 'execute'
% feature

We already mentioned the stepper as a very special trick so let's give
some more explanation here. When you run the following code:

\setbox0\emptybox

\starttyping
\startluacode
  context.startitemize()
    context.startitem()
      context("BEFORE 1")
    context.stopitem()
    context("\\setbox0\\hbox{!!!!}")
    context.startitem()
      context("%p",tex.getbox(0).width)
    context.stopitem()
  context.stopitemize()
\stopluacode
\stoptyping

You get a message like:

\starttyping
[ctxlua]:8: attempt to index a nil value
...
10             context("\\setbox0\\hbox{!!!!}")
11             context.startitem()
12 >>              context("%p",tex.getbox(0).width)
...
\stoptyping

due to the fact that the box is still void. All that the \CONTEXT\ commands feed
into \TEX\ happens when the code snippet has finished. You can however run a
snippet of code the following way:

\startbuffer
\startluacode
  context.stepwise (function()
    context.startitemize()
      context.startitem()
        context.step("BEFORE 1")
      context.stopitem()
      context.step("\\setbox0\\hbox{!!!!}")
      context.startitem()
        context.step("%p",tex.getbox(0).width)
      context.stopitem()
    context.stopitemize()
  end)
\stopluacode
\stopbuffer

\typebuffer

and get:

\getbuffer

A more extensive example is:

\startbuffer
\startluacode
  context.stepwise (function()
    context.startitemize()
      context.startitem()
        context.step("BEFORE 1")
      context.stopitem()
      context.step("\\setbox0\\hbox{!!!!}")
      context.startitem()
        context.step("%p",tex.getbox(0).width)
      context.stopitem()
      context.startitem()
        context.step("BEFORE 2")
      context.stopitem()
      context.step("\\setbox2\\hbox{????}")
      context.startitem()
        context.step("%p",tex.getbox(2).width)
      context.startitem()
        context.step("BEFORE 3")
      context.stopitem()
      context.startitem()
        context.step("\\copy0\\copy2")
      context.stopitem()
      context.startitem()
        context.step("BEFORE 4")
        context.startitemize()
          context.stepwise (function()
            context.step("\\bgroup")
            context.step("\\setbox0\\hbox{>>>>}")
            context.startitem()
              context.step("%p",tex.getbox(0).width)
            context.stopitem()
            context.step("\\setbox2\\hbox{<<<<}")
            context.startitem()
              context.step("%p",tex.getbox(2).width)
            context.stopitem()
            context.startitem()
              context.step("\\copy0\\copy2")
            context.stopitem()
            context.startitem()
              context.step("\\copy0\\copy2")
            context.stopitem()
            context.step("\\egroup")
          end)
        context.stopitemize()
      context.stopitem()
      context.startitem()
        context.step("AFTER 1\\par")
      context.stopitem()
      context.startitem()
        context.step("\\copy0\\copy2\\par")
      context.stopitem()
      context.startitem()
        context.step("\\copy0\\copy2\\par")
      context.stopitem()
      context.startitem()
        context.step("AFTER 2\\par")
      context.stopitem()
      context.startitem()
        context.step("\\copy0\\copy2\\par")
      context.stopitem()
      context.startitem()
        context.step("\\copy0\\copy2\\par")
      context.stopitem()
    context.stopitemize()
  end)
\stopluacode
\stopbuffer

\typebuffer

which gives:

\getbuffer

A step returns control to \TEX\ immediately and after the \TEX\ code that it
feeds back is expanded, returns to \LUA. There are some limitations due to the
input stack but normally that is no real issue.

You can run the following code:

\starttyping
\definenumber[LineCounter][way=bypage]
\starttext
\startluacode
for i=1,2000 do
    context.incrementnumber { "LineCounter" }
    context.getnumber { "LineCounter" }
    context.par()
end
\stopluacode
\stoptext
\stoptyping

You will notice however that the number is not right on each page. This is
because \TEX\ doesn't know yet that there is no room on the page. The next will
work better:

\starttyping
\definenumber[LineCounter][way=bypage]
\starttext
\startluacode
context.stepwise(function()
    for i=1,2000 do
        context.testpage { 0 }
        context.incrementnumber { "LineCounter" }
        context.getnumber { "LineCounter" }
        context.par()
        context.step()
    end
end)
\stopluacode
\stoptext
\starttyping

Instead of the \type {testpage} function you can also play directly with
registers, like:

\starttyping
if tex.pagegtotal + tex.count.lineheight > tex.pagetotal then
\starttyping

but often an already defined helper does a better job. Of course you will
probably never need this kind of hacks anyway, if only because much more is going
on and there are better ways then.

\stopsection

\stopchapter

\stopcomponent