1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19\writestatus{loading}{ConTeXt Obsolete Modules Tokens}
20
21
22
23\continuewhenmkivmode
24
25\startluacode
26
27if not modules then modules = { } end modules ['s-obsolete-tokens'] = {
28 version = 1.001,
29 author = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
30 copyright = "PRAGMA ADE / ConTeXt Development Team",
31 license = "see context related readme files"
32}
33
34local utfbyte, utfchar, utfvalues = utf.byte, utf.char, utf.values
35local format, gsub = string.format, string.gsub
36local tostring = tostring
37
38local tokens = tokens
39local token = token
40local tex = tex
41local context = context
42local commands = commands
43
44tokens.collectors = tokens.collectors or { }
45local collectors = tokens.collectors
46
47collectors.data = collectors.data or { }
48local collectordata = collectors.data
49
50collectors.registered = collectors.registered or { }
51local registered = collectors.registered
52
53local report = logs.reporter("tokens","collectors")
54
55
56
57
58
59
60
61local get_next_token = tokens.scanners.next
62local create_token = tokens.create
63
64function collectors.install(tag,end_cs)
65 local data, d = { }, 0
66 collectordata[tag] = data
67 end_cs = gsub(end_cs,"^\\","")
68 while true do
69 local t = get_next_token()
70 if t.csname == end_cs then
71 context[end_cs]()
72 return
73 else
74 d = d + 1
75 data[d] = t
76 end
77 end
78end
79
80local simple = { letter = "letter", other_char = "other" }
81
82function collectors.show(data)
83
84
85
86 if type(data) == "string" then
87 data = collectordata[data]
88 end
89 if not data then
90 return
91 end
92 local ctx_NC = context.NC
93 local ctx_NR = context.NR
94 local ctx_bold = context.bold
95 local ctx_verbatim = context.verbatim
96 local function show(data)
97 for i=1,#data do
98 local tok = data[i]
99 if type(tok) == "table" then
100 show(tok)
101 else
102 local cmdname = tok.cmdname
103 local simple = simple[cmdname]
104 ctx_NC()
105 ctx_verbatim(simple or cmdname)
106 ctx_NC()
107 ctx_verbatim(simple and utfchar(tok.index) or tok.csname)
108 ctx_NC()
109 if tok.active then context("active ") end
110 if tok.expandable then context("expandable ") end
111 if tok.protected then context("protected ") end
112 ctx_NC()
113 ctx_NR()
114 end
115 end
116 end
117 context.starttabulate { "|Tl|Tc|Tl|" }
118 ctx_NC() ctx_bold("cmd")
119 ctx_NC() ctx_bold("meaning")
120 ctx_NC() ctx_bold("properties")
121 ctx_NC() ctx_NR()
122 context.HL()
123 show(data)
124 context.stoptabulate()
125end
126
127local function printlist(data)
128 if data and #data > 0 then
129 report("not supported (yet): printing back to tex")
130 end
131end
132
133tokens.printlist = printlist
134
135function collectors.flush(tag)
136 printlist(collectordata[tag])
137end
138
139function collectors.test(tag,handle)
140 report("not supported (yet): testing")
141end
142
143function collectors.register(name)
144 report("not supported (yet): registering")
145end
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
278commands.collecttokens = collectors.install
279commands.showtokens = collectors.show
280commands.flushtokens = collectors.flush
281commands.testtokens = collectors.test
282commands.registertoken = collectors.register
283
284
285
286
287
288
289
290
291
292collectors.dowithwords = collectors.test
293
294
295
296tokens.vbox = create_token("vbox")
297tokens.hbox = create_token("hbox")
298tokens.vtop = create_token("vtop")
299tokens.bgroup = create_token(utfbyte("{"),1)
300tokens.egroup = create_token(utfbyte("}"),2)
301
302tokens.letter = function(chr) return create_token(utfbyte(chr),11) end
303tokens.other = function(chr) return create_token(utfbyte(chr),12) end
304
305tokens.letters = function(str)
306 local t, n = { }, 0
307 for chr in utfvalues(str) do
308 n = n + 1
309 t[n] = create_token(chr, 11)
310 end
311 return t
312end
313
314function collectors.defaultwords(t,str)
315 if t then
316 local n = #t
317 n = n + 1 ; t[n] = tokens.bgroup
318 n = n + 1 ; t[n] = create_token("red")
319 for i=1,#str do
320 n = n + 1 ; t[n] = tokens.other('*')
321 end
322 n = n + 1 ; t[n] = tokens.egroup
323 end
324end
325
326\stopluacode
327
328
329
330
331
332\unprotect
333
334
335
336
337\unexpanded\def\starttokens [#1]{\ctxcommand{collecttokens("#1","stoptokens")}}
338 \let\stoptokens \relax
339\unexpanded\def\flushtokens [#1]{\ctxcommand{flushtokens("#1")}}
340\unexpanded\def\showtokens [#1]{\ctxcommand{showtokens("#1")}}
341\unexpanded\def\testtokens [#1]{\ctxcommand{testtokens("#1")}}
342\unexpanded\def\registertoken #1{\ctxcommand{registertoken("#1")}}
343
344\let\toks_show\showtokens
345
346\unexpanded\def\showtokens{\doifelsenextoptional\toks_show\normalshowtokens}
347
348\protect \endinput
349 |