summaryrefslogtreecommitdiff
path: root/tex/context/base/mult-aux.mkiv
blob: 444418a907f859bd20e3c6cfe90c670c856c06df (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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
%D \module
%D   [       file=mult-aux,
%D        version=2010.08.2,
%D          title=\CONTEXT\ Multilingual Macros,
%D       subtitle=helpers,
%D         author=Hans Hagen,
%D           date=\currentdate,
%D      copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
%C
%C This module is part of the \CONTEXT\ macro||package and is
%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
%C details.

%D A generalization of \MKIV-like inheritance. Just something to play
%D with (interface might change). The code here evolved in an email
%D exchange between me and Wolgang Schuster.

\writestatus{loading}{ConTeXt Multilingual Macros / Helpers}

\registerctxluafile{mult-aux}{1.001}

\unprotect

%D \starttyping
%D \unprotect
%D     \def\????aa{@@@@aa}
%D
%D     \installparameterhandler   \????aa {whatever}
%D     \installsetuphandler       \????aa {whatever}
%D     \installdefinehandler      \????aa {whatever} \????aa % #3 == defaultroot
%D     \installfontandcolorhandler\????aa {whatever}
%D
%D   % \installcommandhandler     \????aa {whatever} \????aa
%D \protect
%D
%D % \whateverparameter \c!test
%D % \whateverparameterhash \c!test
%D % \namedwhateverparameter \mycurrentwhatever \c!test
%D % \dosetwhateverstyleandcolor \c!style \c!color
%D % \everydefinewhatever (sets \currentwhatever)
%D % \everypresetwhatever (can be used to reset parameters as we can redefine)
%D % \everysetupwhatever (sets \currentwhatever)
%D
%D \starttext
%D     \definewhatever[first] \definewhatever[second][first]
%D                                           test: \def\currentwhatever{first}  \whateverparameter{method} \par
%D     \setupwhatever        [method=unset]  test: \def\currentwhatever{first}  \whateverparameter{method} \par
%D     \setupwhatever[first] [method=first]  test: \def\currentwhatever{first}  \whateverparameter{method} \par
%D                                           test: \def\currentwhatever{second} \whateverparameter{method} \par
%D     \setupwhatever[second][method=second] test: \def\currentwhatever{second} \whateverparameter{method} \par
%D \stoptext
%D \stoptyping

% problem: every* could clash
%
% There can be less {} in the following definitions if we assume \??aa and \c!somecs
%
% todo: \def\detokenized...parameter#1{\detokenize\expandafter\expandafter\expandafter{\csname#1#2\endcsname}} % always root
%
% it might be more efficient to do this at the lua and
%
% watch the push/pop and predefinition of current .. this is needed for nested
% definitions and overloaded defines using the predefined one

% todo: add (relaxed) postsetup and postdefine hooks, just after the everys

% Start of experimental code: especially tables can have many assignments
% and although most time is spent in the typesetting anyway, we can squeeze
% out a little bit. Of course having 500 rows of 50 columns each with some
% setting does not happen that often. One should keep in mind that in the
% average document having some 500 assignments is no exception but there we're
% talking of neglectable runtime for them. Of course in the definitions below
% there is no real gain, only in the generated \setup* commands. Another
% situation with many assignments is \XML\ where we can pass attributes
% and normally don't do testing of them making sense.
%
% \testfeatureonce{100000}{\getparameters[bla][a=111,b=222,c=333]}% 1.669s
% \testfeatureonce{100000}{\interfaces_get_parameters{bla} [a=111,b=222,c=333]}% 1.529s
% \testfeatureonce{100000}{\def\current_parameter_namespace{bla}\interfaces_get_parameters_indeed[a=111,b=222,c=333]}% 1.466s
%
% \get_parameter                  {...}                      [...]
% \def\current_parameter_namespace{...} \get_parameter_indeed[...]

\def\interfaces_get_parameters#1[#2%
  {\if\noexpand#2]%
     \expandafter\gobbleoneargument
   \else
     \def\current_parameter_namespace{#1}%
     \expandafter\interfaces_get_parameters_indeed
   \fi#2}

% \def\interfaces_get_parameters#1% we can assume that the test already happened
%   {\def\current_parameter_namespace{#1}%
%    \interfaces_get_parameters_indeed}

\def\interfaces_get_parameters_indeed#1]% namespace already set
  {\interfaces_get_parameters_item#1,],\@relax@}

\def\interfaces_get_parameters_item#1,#2% #2 takes space before ,
  {\if,#1,% dirty trick for testing #1=empty
     \expandafter\interfaces_get_parameters_item
   \else\if]#1%
     \doubleexpandafter\gobbleoneargument
   \else
     \interfaces_get_parameters_assign#1==\empty\@relax@
     \doubleexpandafter\interfaces_get_parameters_item
   \fi\fi#2}

\def\interfaces_get_parameters_error#1#2#3%
  {\showassignerror{#2}{\the\inputlineno\space(#1)}}

\def\interfaces_get_parameters_assign#1=#2=#3#4\@relax@
  {\ifx\empty#1\empty
     \expandafter\interfaces_get_parameters_error
   \else\ifx#3\empty
     \doubleexpandafter\interfaces_get_parameters_error
   \else
     \doubleexpandafter\dosetvalue
   \fi\fi
   \current_parameter_namespace{#1}{#2}}

% End of experimental code.

\unexpanded\def\interfaces_install_parameter_handler#1#2#3#4#5#6#7#8#9% inlining \csname*\endcsname is more efficient (#3 and #6 only)
  {\ifx#2\relax\let#2\empty\fi                                        % it is hardly faster but produces less expansion tracing
  %\def#3##1{\csname#4{#1#2}{##1}\endcsname}%
   \def#3##1{\csname\ifcsname#1#2:##1\endcsname#1#2:##1\else\expandafter#5\csname#1#2:\s!parent\endcsname{##1}\fi\endcsname}%
   \def#4##1##2{\ifcsname##1:##2\endcsname##1:##2\else\expandafter#5\csname##1:\s!parent\endcsname{##2}\fi}%
   \def#5##1##2{\ifx##1\relax\s!empty\else#4{##1}{##2}\fi}% is {} needed around ##1 ?
  %\def#6##1##2{\csname#4{#1##1}{##2}\endcsname}%
   \def#6##1##2{\csname\ifcsname#1##1:##2\endcsname#1##1:##2\else\expandafter#5\csname#1##1:\s!parent\endcsname{##2}\fi\endcsname}%
   \def#7##1{\detokenize\expandafter\expandafter\expandafter{\csname#1#2:##1\endcsname}}% always root, no backtrack
   \def#8##1{\csname\ifcsname#1#2:##1\endcsname#1#2:##1\else\s!empty\fi\endcsname}%
   \def#9##1{\csname#1#2:##1\endcsname}}

\unexpanded\def\installparameterhandler#1#2%
  {\normalexpanded
     {\interfaces_install_parameter_handler
        {\noexpand#1}% \??aa
        \expandafter\noexpand\csname current#2\endcsname
        \expandafter\noexpand\csname #2parameter\endcsname
        \expandafter\noexpand\csname do#2parameter\endcsname
        \expandafter\noexpand\csname do#2parentparameter\endcsname
        \expandafter\noexpand\csname named#2parameter\endcsname
        \expandafter\noexpand\csname detokenized#2parameter\endcsname
        \expandafter\noexpand\csname strict#2parameter\endcsname       % checked
        \expandafter\noexpand\csname direct#2parameter\endcsname}}     % unchecked

\unexpanded\def\interfaces_install_root_parameter_handler#1#2#3%
  {\def#2##1{\detokenize\expandafter\expandafter\expandafter{\csname#1:##1\endcsname}}% always root
   \def#3##1{\csname\ifcsname#1:##1\endcsname#1:##1\else\s!empty\fi\endcsname}}

\unexpanded\def\installrootparameterhandler#1#2%
  {\normalexpanded
     {\interfaces_install_root_parameter_handler
        {\noexpand#1}% \??aa
        \expandafter\noexpand\csname detokenizedroot#2parameter\endcsname
        \expandafter\noexpand\csname root#2parameter\endcsname}}

\unexpanded\def\interfaces_install_parameter_hash_handler#1#2#3#4#5#6#7#8#9%
  {\ifx#2\relax\let#2\empty\fi
   \def#3##1{#1#4{#1#2}{##1}:}% leading #1 was missing .. is this one used?
   \def#4##1##2{\ifcsname##1:##2\endcsname##1\else\expandafter#5\csname##1:\s!parent\endcsname{##2}\fi}%
   \def#5##1##2{\ifx##1\relax\else#4{##1}{##2}\fi}%
   \def#6{#1#2:}%
   \def#7##1{#1##1:}%
   \def#8{\ifx#2\empty\else\ifcsname#1#2:\s!parent\endcsname\else\expandafter\let\csname#1#2:\s!parent\endcsname#1\fi\fi}%
   \unexpanded\def#9##1{\expandafter\edef\csname#1##1:\s!parent\endcsname{#1#2}}}

\unexpanded\def\installparameterhashhandler#1#2%
  {\expandafter\let\csname#2namespace\endcsname#1%
   \normalexpanded
     {\interfaces_install_parameter_hash_handler
        {\noexpand#1}% \??aa
        \expandafter\noexpand\csname current#2\endcsname
        \expandafter\noexpand\csname #2parameterhash\endcsname
        \expandafter\noexpand\csname do#2parameterhash\endcsname
        \expandafter\noexpand\csname do#2parentparameterhash\endcsname
        \expandafter\noexpand\csname current#2hash\endcsname
        \expandafter\noexpand\csname named#2hash\endcsname
        \expandafter\noexpand\csname check#2parent\endcsname
        \expandafter\noexpand\csname chaintocurrent#2\endcsname}}

% In \MKIV\ we can probably use the english variant for all other
% languages too.

\unexpanded\def\interfaces_install_parameter_set_handler#1#2#3#4#5#6% we can speed this up for english
  {\ifx#2\relax\let#2\empty\fi
   \unexpanded\def#3{\dosetvalue {#1#2:}}%        ##1 {##2} (braces are mandate)
   \unexpanded\def#4{\dosetevalue{#1#2:}}%        ##1 {##2} (braces are mandate)
   \unexpanded\def#5{\doletvalue {#1#2:}}%        ##1 ##2
   \unexpanded\def#6{\doletvalue {#1#2:}\empty}}% ##1

\startinterface english

    \unexpanded\def\interfaces_install_parameter_set_handler#1#2#3#4#5#6%
      {\ifx#2\relax\let#2\empty\fi
       \unexpanded\def#3##1{\expandafter \def\csname#1#2:##1\endcsname}%        ##1 {##2} (braces are mandate)
       \unexpanded\def#4##1{\expandafter\edef\csname#1#2:##1\endcsname}%       ##1 {##2} (braces are mandate)
       \unexpanded\def#5##1{\expandafter \let\csname#1#2:##1\endcsname}%        ##1 ##2
       \unexpanded\def#6##1{\expandafter \let\csname#1#2:##1\endcsname\empty}}% ##1

\stopinterface

\unexpanded\def\installparametersethandler#1#2%
  {\normalexpanded
     {\interfaces_install_parameter_set_handler
        {\noexpand#1}% \??aa
        \expandafter\noexpand\csname current#2\endcsname
        \expandafter\noexpand\csname set#2parameter\endcsname
        \expandafter\noexpand\csname setexpanded#2parameter\endcsname
        \expandafter\noexpand\csname let#2parameter\endcsname
        \expandafter\noexpand\csname reset#2parameter\endcsname}}

\let\dousecurrentstyleparameter\relax
\let\dousecurrentcolorparameter\relax

\let\currentstyleparameter\empty
\let\currentcolorparameter\empty

\unexpanded\def\interfaces_install_style_and_color_handler#1#2#3#4%
  {\unexpanded\def#2##1##2% style color
     {\edef\currentstyleparameter{#1{##1}}% this name is public
      \edef\currentcolorparameter{#1{##2}}% this name is public
      \ifx\currentstyleparameter\empty\else\dousecurrentstyleparameter\fi
      \ifx\currentcolorparameter\empty\else\dousecurrentcolorparameter\fi}%
   \unexpanded\def#3##1% style
     {\edef\currentstyleparameter{#1{##1}}% this name is public
      \ifx\currentstyleparameter\empty\else\dousecurrentstyleparameter\fi}%
   \unexpanded\def#4##1% color
     {\edef\currentcolorparameter{#1{##1}}% this name is public
      \ifx\currentcolorparameter\empty\else\dousecurrentcolorparameter\fi}}

\unexpanded\def\installstyleandcolorhandler#1#2%
  {\normalexpanded
     {\interfaces_install_style_and_color_handler
        \expandafter\noexpand\csname #2parameter\endcsname
        \expandafter\noexpand\csname use#2styleandcolor\endcsname % maybe an alias use#2styleandcolorparameters
        \expandafter\noexpand\csname use#2styleparameter\endcsname
        \expandafter\noexpand\csname use#2colorparameter\endcsname}}

\let\definehandlerparent\empty

\unexpanded\def\interfaces_install_define_handler#1#2#3#4#5#6#7#8%
  {\ifx#4\relax\let#4\empty\fi
   \unexpanded\def#2{\dotripleempty#5}%
   \newtoks#6%
   \newtoks#7%
   \def#5[##1][##2][##3]% [child][parent][settings] | [child][settings] | [child][parent] | [child]
     {\let\saveddefinewhatever#4%
      \edef#4{##1}%
      \the#6% predefine
      \ifthirdargument
        \edef#8{##2}%
      % \getparameters[#1#4:][\s!parent=#1##2,##3]%
        \interfaces_get_parameters{#1#4:}[\s!parent=#1##2,##3]%
      \else\ifsecondargument
        \doifassignmentelse{##2}
          {\let#8\empty
         % \getparameters[#1#4:][\s!parent=#3,##2]}
           \interfaces_get_parameters{#1#4:}[\s!parent=#3,##2]}
          {\edef#8{##2}%
         % \getparameters[#1#4:][\s!parent=#1##2]}%
           \interfaces_get_parameters{#1#4:}[\s!parent=#1##2]}%
      \else
        \let#8\empty
      % \getparameters[#1#4:][\s!parent=#3]%
        \interfaces_get_parameters{#1#4:}[\s!parent=#3]%
      \fi\fi
      \the#7%
      \let#4\saveddefinewhatever}}

\unexpanded\def\installdefinehandler#1#2#3%
  {\normalexpanded
     {\interfaces_install_define_handler
        {\noexpand#1}% \??aa
        \expandafter\noexpand\csname define#2\endcsname
        {\noexpand#3}% root
        \expandafter\noexpand\csname current#2\endcsname
        \expandafter\noexpand\csname d@define#2\endcsname % sort of public
        \expandafter\noexpand\csname everypreset#2\endcsname
        \expandafter\noexpand\csname everydefine#2\endcsname
        \expandafter\noexpand\csname current#2parent\endcsname}}

\unexpanded\def\interfaces_install_setup_handler#1#2#3#4#5#6%
  {\ifx#3\relax\let#3\empty\fi
   \unexpanded\def#2{\dodoubleempty#4}%
 % \unexpanded\def#6{\getparameters[#1#3:]}% no every ! don't change it
   \unexpanded\def#6{\interfaces_get_parameters{#1#3:}}% no every ! don't change it
   \newtoks#5%
   \def#4[##1][##2]% maybe helper
     {\let\savedsetupwhatever#3%
      \ifsecondargument
        \def\docommand####1% we will have a simple one as well
          {\edef#3{####1}%
         % \getparameters[#1#3:][##2]%
           \interfaces_get_parameters{#1#3:}[##2]%
           \the#5}%
        \processcommalist[##1]\docommand
      \else
        \let#3\empty
      % \getparameters[#1:][##1]%
        \interfaces_get_parameters{#1:}[##1]%
        \the#5%
      \fi
      \let#3\savedsetupwhatever}}

\unexpanded\def\installsetuphandler#1#2%
  {\normalexpanded
     {\interfaces_install_setup_handler
        {\noexpand#1}% \??aa
        \expandafter\noexpand\csname setup#2\endcsname
        \expandafter\noexpand\csname current#2\endcsname
        \expandafter\noexpand\csname d@setup#2\endcsname % sort of public
        \expandafter\noexpand\csname everysetup#2\endcsname
        \expandafter\noexpand\csname setupcurrent#2\endcsname}}

\unexpanded\def\interfaces_install_switch_setup_handler#1#2#3#4#5#6%
  {\ifx#3\relax\let#3\empty\fi
   \unexpanded\def#2{\dodoubleempty#4}%
 % \unexpanded\def#6{\getparameters[#1#3:]}%
   \unexpanded\def#6{\interfaces_get_parameters{#1#3:}}%
   \newtoks#5%
   \def#4[##1][##2]% maybe helper
     {\ifsecondargument % no commalist here
        \let\savedsetupwhatever#3%
        \edef#3{##1}%
      % \getparameters[#1#3:][##2]%
        \interfaces_get_parameters{#1#3:}[##2]%
        \the#5%
        \let#3\savedsetupwhatever
      \else\iffirstargument
        \doifassignmentelse{##1}
          {\let\savedsetupwhatever#3%
           \let#3\empty
         % \getparameters[#1:][##1]%
           \interfaces_get_parameters{#1:}[##1]%
           \the#5%
           \let#3\savedsetupwhatever}
          {\edef#3{##1}% this will catch reset
           \the#5}%
      \else
        \let#3\empty
        \the#5%
      \fi\fi}}

\unexpanded\def\installswitchsetuphandler#1#2%
  {\normalexpanded
     {\interfaces_install_switch_setup_handler
        {\noexpand#1}% \??aa
        \expandafter\noexpand\csname setup#2\endcsname
        \expandafter\noexpand\csname current#2\endcsname
        \expandafter\noexpand\csname d@setup#2\endcsname
        \expandafter\noexpand\csname everysetup#2\endcsname
        \expandafter\noexpand\csname setupcurrent#2\endcsname}}

\unexpanded\def\interfaces_install_auto_setup_handler#1#2#3#4#5#6#7%
  {\ifx#3\relax\let#3\empty\fi
   \unexpanded\def#2{\dotripleempty#4}%
 % \unexpanded\def#6{\getparameters[#1#3:]}%
   \unexpanded\def#6{\interfaces_get_parameters{#1#3:}}%
   \newtoks#5%
   \def#4[##1][##2][##3]%
     {\let\savedsetupwhatever#3%
      \ifthirdargument
        \def\docommand####1%
          {\edef#3{####1}%
        %  \getparameters[#1#3:][\s!parent=#1##2,##3]%
           \interfaces_get_parameters{#1#3:}[\s!parent=#1##2,##3]% always sets parent
           \the#5}%
        \processcommalist[##1]\docommand
      \else\ifsecondargument
        \def\docommand####1%
          {\edef#3{####1}%
           #7% checks parent and sets if needed
        %  \getparameters[#1#3:][##2]%
           \interfaces_get_parameters{#1#3:}[##2]%
           \the#5}%
        \processcommalist[##1]\docommand
      \else
        \let#3\empty
      % \getparameters[#1:][##1]%
        \interfaces_get_parameters{#1:}[##1]%
        \the#5%
      \fi\fi
      \let#3\savedsetupwhatever}}

\unexpanded\def\installautosetuphandler#1#2%
  {\normalexpanded
     {\interfaces_install_auto_setup_handler
        {\noexpand#1}% \??aa
        \expandafter\noexpand\csname setup#2\endcsname
        \expandafter\noexpand\csname current#2\endcsname
        \expandafter\noexpand\csname d@setup#2\endcsname % sort of public
        \expandafter\noexpand\csname everysetup#2\endcsname
        \expandafter\noexpand\csname setupcurrent#2\endcsname
        \expandafter\noexpand\csname check#2parent\endcsname}}

\unexpanded\def\installbasicparameterhandler#1#2%
  {\installparameterhandler    {#1}{#2}%
   \installparameterhashhandler{#1}{#2}%
   \installparametersethandler {#1}{#2}%
   \installrootparameterhandler{#1}{#2}}

\unexpanded\def\installbasicautosetuphandler#1#2#3% \??self name \??parent (can be \??self)
  {\installbasicparameterhandler{#1}{#2}%
   \installautosetuphandler     {#1}{#2}}

\unexpanded\def\installcommandhandler#1#2#3% \??self name \??parent (can be \??self)
  {\installbasicparameterhandler{#1}{#2}%
   \installdefinehandler        {#1}{#2}{#3}%
   \installsetuphandler         {#1}{#2}%
   \installstyleandcolorhandler {#1}{#2}}

\unexpanded\def\installswitchcommandhandler#1#2#3% \??self name \??parent (can be \??self)
  {\installbasicparameterhandler{#1}{#2}%
   \installdefinehandler        {#1}{#2}{#3}%
   \installswitchsetuphandler   {#1}{#2}%
   \installstyleandcolorhandler {#1}{#2}}

\unexpanded\def\installautocommandhandler#1#2#3% automatically defined cloned setups
  {\installbasicparameterhandler{#1}{#2}%
   \installdefinehandler        {#1}{#2}{#3}%
   \installautosetuphandler     {#1}{#2}%
   \installstyleandcolorhandler {#1}{#2}}

\unexpanded\def\installsimplecommandhandler#1#2#3% no define (experiment) - use \check*parent when defining
  {\installbasicparameterhandler{#1}{#2}%
   \installsetuphandler         {#1}{#2}%
   \installstyleandcolorhandler {#1}{#2}}

%D Many mechanisms have some kind of inheritance in place, and these are
%D the speed||critical ones. Therefore there is no reason to stick to
%D \type {\@@xxkey} for the sake of performance. For this reason we also
%D provide a direct variant. This permits a more consistent treatment of
%D namespaces. A \type {\whateverparameter} call is three times slower
%D and a \type {\directwhateverparameter} call two times but for some
%D 100K expansions we only loose some .1 second which is neglectable
%D given the small amount of expansions in real runs.

%D We don't need colons for such simple cases.

\unexpanded\def\interfaces_install_direct_parameter_handler#1#2#3#4%
  {\def#2##1{\csname\ifcsname#1##1\endcsname#1##1\else\s!empty\fi\endcsname}%
   \def#3##1{\detokenize\expandafter\expandafter\expandafter{\csname#1##1\endcsname}}%
   \def#4##1{\csname#1##1\endcsname}}

\unexpanded\def\installdirectparameterhandler#1#2%
  {\normalexpanded
     {\interfaces_install_direct_parameter_handler
        {\noexpand#1}%
        \expandafter\noexpand\csname #2parameter\endcsname
        \expandafter\noexpand\csname detokenized#2parameter\endcsname
        \expandafter\noexpand\csname direct#2parameter\endcsname}}

\unexpanded\def\interfaces_install_direct_setup_handler#1#2#3#4%
  {\unexpanded\def#2{\dosingleempty#3}%
   \newtoks#4%
   \def#3[##1]%
     {\interfaces_get_parameters#1[##1]%
      \the#4}}

\unexpanded\def\installdirectsetuphandler#1#2%
  {\normalexpanded
     {\interfaces_install_direct_setup_handler
        {\noexpand#1}% \??aa
        \expandafter\noexpand\csname setup#2\endcsname
        \expandafter\noexpand\csname d@setup#2\endcsname
        \expandafter\noexpand\csname everysetup#2\endcsname}}

\unexpanded\def\interfaces_install_direct_parameter_set_handler#1#2#3#4#5%
  {\unexpanded\def#2{\dosetvalue #1}%
   \unexpanded\def#3{\dosetevalue#1}%
   \unexpanded\def#4{\doletvalue #1}%
   \unexpanded\def#5{\doletvalue #1\empty}}%

\startinterface english

    \unexpanded\def\interfaces_install_direct_parameter_set_handler#1#2#3#4#5%
      {\unexpanded\def#2##1{\expandafter \def\csname#1##1\endcsname}%
       \unexpanded\def#3##1{\expandafter\edef\csname#1##1\endcsname}%
       \unexpanded\def#4##1{\expandafter \let\csname#1##1\endcsname}%
       \unexpanded\def#5##1{\expandafter \let\csname#1##1\endcsname\empty}}%

\stopinterface

\unexpanded\def\installdirectparametersethandler#1#2%
  {\normalexpanded
     {\interfaces_install_direct_parameter_set_handler
        {\noexpand#1}% \??aa
        \expandafter\noexpand\csname set#2parameter\endcsname
        \expandafter\noexpand\csname setexpanded#2parameter\endcsname
        \expandafter\noexpand\csname let#2parameter\endcsname
        \expandafter\noexpand\csname reset#2parameter\endcsname}}

\let\installdirectstyleandcolorhandler\installstyleandcolorhandler

\unexpanded\def\installdirectcommandhandler#1#2%
  {\installdirectparameterhandler    {#1}{#2}%
   \installdirectsetuphandler        {#1}{#2}%
   \installdirectparametersethandler {#1}{#2}%
   \installdirectstyleandcolorhandler{#1}{#2}}

% Experiment:

% \installcorenamespace {one}
% \installcorenamespace {two}
%
% \installcommandhandler \??one {one} \??one
% \installcommandhandler \??two {two} \??two
%
% \defineone[test] \setupone[test][alpha=first]
% \definetwo[test] \setuptwo[test][beta=second]
%
% \protect
%
% \def\currentone{test}
% \def\currenttwo{test}
%
% \relateparameterhandlers {two} {test} {one} {test}
%
% yes:\oneparameter{alpha}\par
% nop:\oneparameter{beta}\par
% yes:\twoparameter{alpha}\par
% yes:\twoparameter{beta}\par

\unexpanded\def\relateparameterhandlers#1#2#3#4% {from} {instance} {to} {instance}
  {\expandafter\edef\csname\csname#1namespace\endcsname#2:\s!parent\endcsname{\csname#3namespace\endcsname#4}}

% First we had, in tune with the regular system variables:
%
% \starttyping
% \unexpanded\def\installnamespace#1{\setvalue{????#1}{@@@@#1}}
% \stoptyping
%
% The following variant is nicer and in principle faster but that gets
% unnoticed unless lots of expansion happens. Also, we can use long tags
% but the internal expansion will be relatively small (and unlikely more
% than 4 characters). For instance, \??xx used to expand to @@xx but now
% becomes for instance 123::. This is one character more but in quite some
% cases we had : after such a tag in the old situation. In the new situation
% we create more namespaces and don't need that : any more, so we end up
% with on the average the same amount of tokens and definitely less when
% we consider cases like \??xx:\c!align: which now is just \??somealign and
% therefore has length 5 now (instead of 4+1+5+1=10).
%
% Eventualy we will have a verbose \blablanamespace and the difference between
% core and regular can go ... after all, \xxxparameter can already clash between
% the two prefix groups .. if users use this mechanism a lot they should use
% verbose names anyway (the old two character names were mostly an optimization
% as they also expanded to these characters).

% todo: register namespaces at lua end for logging and reverse resolve
% todo: move this to syst-ini so that we can use it real early

\newcount\interfaces_n_of_namespaces

\def\v_interfaces_prefix_template{\number    \interfaces_n_of_namespaces::}
\def\v_interfaces_prefix_template{\characters\interfaces_n_of_namespaces::}
\def\v_interfaces_prefix_template{\number    \interfaces_n_of_namespaces>}
\def\v_interfaces_prefix_template{\characters\interfaces_n_of_namespaces>}

\def\v_interfaces_prefix_template % consistently %03i>
  {\ifnum\interfaces_n_of_namespaces<\plusten00\else\ifnum\interfaces_n_of_namespaces<\plushundred0\fi\fi
   \number\interfaces_n_of_namespaces>}

\unexpanded\def\installnamespace#1% for modules and users
  {\ifcsname ????#1\endcsname
     \writestatus\m!system{duplicate user namespace '#1'}\wait
   \else
     \global\advance\interfaces_n_of_namespaces\plusone
     \expandafter\edef\csname ????#1\endcsname{\v_interfaces_prefix_template}%
   \fi}

\unexpanded\def\installcorenamespace#1%
  {\ifcsname ??#1\endcsname
     \writestatus\m!system{duplicate core namespace '#1'}\wait
   \else
     \global\advance\interfaces_n_of_namespaces\plusone
     \expandafter\edef\csname ??#1\endcsname{\v_interfaces_prefix_template}%
     \ctxcommand{registernamespace(\number\interfaces_n_of_namespaces,"#1")}%
   \fi}

% \enabletrackers[interfaces.namespaces,context.flush]
%
% \definenamespace
%   [xy]
%   [type=module,
%    comment=test module,
%    version=1,
%    name=test,
%    style=yes,
%    command=yes,
%    setup=list,
%    set=yes,
%    parent=xy]
%
% \unprotect
% \getparameters
%   [\????xy]
%   [text=]
% \protect
%
% \definetest[one]
%
% \starttext
%
%   “\testparameter{text}”
%
%   \setuptest[text=foo]
%
%   “\testparameter{text}”
%
%   \setuptest[one][text=bar]
%
%   “\testparameter{text}”
%
% \stoptext
%
% This is a user (module) command:

\unexpanded\def\definenamespace
  {\dodoubleargument\interfaces_define_name_space}

\def\interfaces_define_name_space[#1][#2]% namespace settings
  {\ctxlua{interfaces.namespaces.define(\!!bs#1\!!es,\!!bs#2\!!es)}}

\def\listnamespaces
  {\ctxlua{interfaces.namespaces.list()}}

%D Helper:
%D
%D \starttyping
%D \showparentchain{@@am}{left}
%D \stoptyping

\unexpanded\def\showparentchain#1#2%
  {\writestatus\m!system{chain: [ \interfaces_show_parent_chain{#1#2}]}}

\def\interfaces_show_parent_chain#1%
  {#1 => %
   \ifcsname#1:\s!parent\endcsname
      \expandafter\interfaces_show_parent_chain\csname#1:\s!parent\endcsname
   \fi}

%D Conventions:
%D
%D \starttyping
%D \newcount       \c_class_whatever
%D \newconditional \c_class_whatever
%D \newconstant    \c_class_whatever
%D \newdimen       \d_class_whatever
%D \newskip        \s_class_whatever
%D \newmuskip      \s_class_whatever
%D \newbox         \b_class_whatever
%D \newtoks        \t_class_whatever
%D
%D \edef\p_class_whatever{\classparameter\c!whatever}
%D \edef\m_class_whatever{whatever}
%D \stoptyping

\protect