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