summaryrefslogtreecommitdiff
path: root/macros/plain/formats/lollipop/tex/lollipop-tools.tex
blob: f7b11454b5cf205c6d93208fa7077ee31983af5e (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
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
% lollipop-tools.tex copyright 1992/3 Victor Eijkhout
%                copyright 2014--2016 Vafa Khalighi
%
%
%    This program is free software: you can redistribute it and/or modify
%    it under the terms of the GNU General Public License as published by
%    the Free Software Foundation, either version 3 of the License, or
%    (at your option) any later version.
%
%    This program is distributed in the hope that it will be useful,
%    but WITHOUT ANY WARRANTY; without even the implied warranty of
%    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
%    GNU General Public License for more details.
%
%    You should have received a copy of the GNU General Public License
%    along with this program.  If not, see <http://www.gnu.org/licenses/>.
%
%
%% anonymous control sequences
\newif\iftempa \newif\iftempb \newif\iftempc \newif\iftempd

\newdimen\tempdima \newdimen\tempdimb \newdimen\tempdimc

\newskip\tempskipa \newskip\tempskipb \newskip\tempskipc

\newcount\tempcounta \newcount\tempcountb \newcount\tempcountc

\newbox\tempboxa \newbox\tempboxb \newbox\tempboxc \newbox\tempboxd

\newtoks\temptoksa \newtoks\temptoksb \newtoks\temptoksc
%\newtoks\temptoksd \newtoks\temptokse \newtoks\temptoksf

%% auxiliaries
\def\@meld#1{{\immediate\write16{\trace@prefix #1}}}
\def\Dmessage#1{\ifDiagnose \@meld{#1}\fi}
\def\Wmessage#1{\@meld{--- Warning: #1}}
\def\Emessage#1{{\errorcontextlines\m@ne
    \errmessage{<><><><><><><><> #1 <><><><><><><><>}}}

\showboxdepth=7 \showboxbreadth=20
\newlinechar=`\^^J %nodig?

\chardef\busje`\\
\def\cs#1{{\tt\char\busje#1}}

\let\xp\expandafter \let\nxp\noexpand
\let\csn\csname \let\ecs\endcsname

% Remove or select tokens
% is used in several places
%
\long\def\id@#1{#1}
\long\def\take@former#1#2{#1} \long\def\take@latter#1#2{#2}
\long\def\take@one#1{} \long\def\take@two#1#2{}
\long\def\take@three#1#2#3{}
\long\def\keep@one#1{#1} \long\def\take@to@dollar#1${}
\def\Xignorespaces#1{#1}

{\noesc 
\xp\xp\xp\gdef\xp\xp\xp\meaning@take@zero
   \xp\string\csn macro:\xp\xp\xp\xp\xp\xp\xp\ecs
   \xp\xp\xp\xp\xp\xp\xp#\xp\xp\xp1\xp\string\csn->\ecs{}
}\def\real@meaning{\xp\meaning@take@zero\meaning}

\def\first@of@three#1#2#3{#1}
\def\second@of@three#1#2#3{#2}
\def\third@of@three#1#2#3{#3}
\def\first@two@of@three#1#2#3{{#1}{#2}}


% Check if this file is being loaded from IniTeX, or
% by \input'ting on top of an already existing format.
% This is to avoid double definitions and major accidents.
% 
\def\lollipop@name{Lollipop}
\expandafter\ifx\csname fmtname\endcsname\lollipop@name
    \let\ifini\take@one
    \let\ifnin\keep@one
    \message{! Loading Lollipop on top of format !}
\else
    \let\ifini\keep@one %iniTeX run or on top of plain TeX
    \let\ifnin\take@one
    \everyjob{\let\ifini\take@one\let\ifnin\keep@one}
\fi

%%%%%%%%%%%%%%%% forward definitions
\def\new@@counter#1{\csarg\newcount{\counter@name{#1}}}
\def\counter@name#1{#1@C}

%%%%%%%%%%%%%%%% Special characters

% At sign
%

\def\makeatletter{\catcode`\@=11\relax}
\def\makeatother{\catcode`\@=12\relax}

% Catcode (re)storing
%
\def\storecat#1%
   {\noesc
    \csarg\edef{restorecat\string#1}%
          {\catcode`\string#1=
                \the\catcode\expandafter`\string#1}%
    \catcode\expandafter`\string#1=12\relax
    \normalesc}
\def\restorecat#1%
   {\noesc
    \csname restorecat\string#1\endcsname
    \normalesc}

% save writing.

\def\@space{ }\def\@empty{} \newtoks\empty@toks \empty@toks{}
\def\sc@led{scaled}
\def\nl{\hfil\break}
\def\IniTeX{Ini\TeX}
\def\LaTeX{L\kern-.3em\raise.35ex\hbox{\script A}\kern-.1em\TeX}
\def\AmsTeX{$\cal A_MS$-\TeX}
%my old def
%\def\LamsTeX{L\kern-.2em\raise.4ex\hbox
%   {$\scriptstyle\cal A$}\kern-.2em$\cal_M$\kern-.2em$\cal S\,$\TeX}
%Spivak's old def
%\def\LamSTeX{L\kern-.4em\raise.3ex\hbox{$\ssize\Cal A$}\kern-.25em
% \lower.4ex\hbox{\eightsy M}\kern-.1em{$\Cal S$}-\TeX}
\def\LamsTeX{L\kern-.4em\raise.3ex\hbox{\script$\cal A$}\kern-.25em
 \lower.4ex\hbox{\script$\cal M$}\kern-.1em{$\cal S$}-\TeX}

\def\CorporateLogo{\TeX\kern-.15em \raise.5ex\hbox{T}\kern-.25em
             ECHNIQUE}


\hsize=15cm 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%% T O O L S %%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Construct a control sequence inside \edef
%
\def\CSname#1{\expandafter\noexpand\csname#1\endcsname}

% Construct a control sequence out of #2, and give it to #1
%
\def\csarg#1#2{\expandafter#1\csname#2\endcsname}

% Pick an escapecharacter
% this presumes that its argument is a character.
%
\def\pickescape#1{\ifnum`#1=\escapechar\else#1\fi}

% Equality of strings.
% the fast approach: does not work in expansion-only environment.
%
\def\EqualString#1#2{00\fi\def\eqs@a{#1}\def\eqs@b{#2}%
    \ifx\eqs@a\eqs@b}

% Test by expansion only
%
\def\EqualStringX#1#2{00\fi
    \csname if\@EqualStringX#1&$#2&$\endcsname}
\def\@EqualStringX#1#2$#3#4${\ifx#1#3%
        \ifx#1&true\else\hop@ES\@EqualStringX#2$#4$\fi
    \else false\fi}
\def\hop@ES#1\fi#2\fi{\fi\fi#1}

% Lexicographic ordering
% note the use of \def
% also note the characters 0,127,255 that are used,
% these are not supposed to appear in the strings
%
{\catcode0=12 \catcode255=12 \catcode127=12
\gdef\StringBeforeNC#1#2{\00\fi
  \CharsBefore#1^^@^^?#2^^ff^^?}
\gdef\CharsBeforeNC#1#2^^?#3#4^^?{%
  \ifcat#1\relax\def\next{\CharsBefore#2^^@^^?#3#4^^ff^^?}%
  \else\ifcat#3\relax\def\next{\CharsBefore#1#2^^@^^?#4^^ff^^?}%
       \else\ifnum\lccode`#1<\lccode`#3
              \def\next{\csname iftrue\endcsname}%
            \else\ifnum\lccode`#1>\lccode`#3
                   \def\next{\csname iffalse\endcsname}%
                 \else\def\next{\CharsBefore#2^^?#4^^?}%
  \fi  \fi  \fi  \fi
  \next}
\gdef\StringBefore#1#2{\00\fi
  \CharsBefore#1^^@^^?#2^^ff^^?}
\gdef\CharsBefore#1#2^^?#3#4^^?{%
  \ifnum`#1<`#3
       \def\next{\csname iftrue\endcsname}%
  \else\ifnum`#1>`#3
       \def\next{\csname iffalse\endcsname}%
       \else\def\next{\CharsBefore#2^^?#4^^?}%
  \fi\fi
  \next}
}


% Null argument test
%
\def\ifempty#1{00\fi\xp\ifx\csname#1null\endcsname\null}
\def\ifEmptyX#1{\xp\ifx\csname#1null\endcsname\null}
%\def\ifEmpty#1{\def\cs@a{#1}\ifx\cs@a\empty}
\def\IsEmptyList#1{00\fi\def\cs@a{#1}\ifx\cs@a\empty}

% Next character test
%
\def\NextChar#1#2#3{00\fi
    \let\nxt@ch#1\def\nxt@a{#2}\def\nxt@b{#3}%
    \futurelet\nxt@c\@ifnxtc}
\def\ifNextChar#1#2#3{%
    \let\nxt@ch#1\def\nxt@a{#2}\def\nxt@b{#3}%
    \futurelet\nxt@c\@ifnxtc}
\let\ifnextchar\ifNextChar
\def\@ifnxtc{\ifx\nxt@ch\nxt@c \expandafter\nxt@a 
    \else \expandafter\nxt@b \fi}

% Test for defined-ness of string as control sequence.
% works inside \edef
%
%outdated: \def\ifnotdefined#1{\csarg\ifx{#1}\relax}
\def\UndefinedCS#1{00\fi\csarg\ifx{#1}\relax}

% Define uniquely defined dummy macros,
% usually to serve as bumper blocks at the end of a list
%
\newcount\dummies
\def\NewDummy#1{
    \if\UndefinedCS{#1}\csarg\edef{#1}{dum\the\dummies}
        \advance\dummies\@ne
    \else
        \ifini{\Wmessage{Attempt at second definition of `#1'}}
    \fi}

% string case statement
% use: \switch \iftest{...} in:
%      item1 action1
%      ...
%      itemn actionn
%      default defaultaction [optional]
%      \endswitch
% inspired by Jonathan Fine: Some Basic Control Macros
% Tugboat 12 #4
%
\let\@fi\fi \let\endswitch\relax \NewDummy{default}
\def\switch@exit #1 \@fi #2 \endswitch {\fi #1}
\def\switch #1#2#3{\csarg\ifx{#2}\default\switch@exit #3 \@fi
  #1{#2}\switch@exit #3 \@fi
  \switch {#1}
}
% this version puts the test object in {}
\def\oswitch #1 in: #2 #3; {\csarg\ifx{#2}\default\switch@exit #3 \@fi
  #1{#2}\switch@exit #3 \@fi
  \switch #1 in:
}
% this version assumes that the test object is just one character
\def\cswitch #1 in: #2 #3; {\csarg\ifx{#2}\default\switch@exit #3 \@fi
  #1#2\switch@exit #3 \@fi
  \cswitch #1 in:
}
% this version assumes that the test object can have an optional space
\def\bswitch #1 in: #2 #3; {\csarg\ifx{#2}\default\switch@exit #3 \@fi
  #1#2 \switch@exit #3 \@fi
  \bswitch #1 in:
}
% this version can cope with significant spaces at the start of #3;
% #2 is macro argument
\def\mswitch #1 in: #2:#3; {\csarg\ifx{#2}\default\switch@exit #3 \@fi
  #1{#2}\switch@exit #3 \@fi
  \mswitch #1 in:
}
% idem; #2 does not have to be enclosed in braces
\def\fswitch #1 in: #2:#3; {\csarg\ifx{#2}\default\switch@exit #3 \@fi
  #1#2 \switch@exit #3 \@fi
  \fswitch #1 in:
}

% Routines for taking the value of a string: 
% if the string is defined as control sequence
% take the value of that, otherwise take the string itself

% String or value of control sequence
%
\def\w@w#1{\if\UndefinedCS{#1}#1\else \the\csname#1\endcsname\fi}

% String or control sequence
%
\def\@ww#1{\if\UndefinedCS{#1}#1\else \csname#1\endcsname \fi}

% String or \noexpand-ed control sequence;
% for use in \edef
%
\def\@w@w#1{\if\UndefinedCS{#1}#1\else 
            \ifin@label\noexpand\protect\fi \CSname{#1}\fi}
\newtoks\are@these@correct
\def\t@w@w#1{%
    \if\UndefinedCS{#1}#1%
        \ifdefining\append@to@list\are@these@correct{#1 }\fi
    \else 
        \ifin@label\noexpand\protect\fi \CSname{#1}\fi}
% see below what happens to \are@these@correct

% Allocation
%
\def\new@count#1#2{\csarg\newcount{#1}\global\set@value{#1}{#2}}
\def\new@skip#1#2{\csarg\newskip{#1}\global\set@value{#1}{#2}}
\def\new@dimen#1#2{\csarg\newdimen{#1}\global\set@value{#1}{#2}}

% Assignment of Values
%
\def\set@value#1#2{\csname#1\endcsname #2\relax}
\def\increase@value#1#2{\advance\csname#1\endcsname #2\relax}
\def\decrease@value#1#2{\advance\csname#1\endcsname -#2\relax}

% Hex
%
\def\hexdigit#1{\ifcase#10\or 1\or 2\or 3\or 4\or
    5\or 6\or 7\or 8\or 9\or A\or B\or C\or D\or E\or F\fi}

% Log 10
%
\def\LogTen#1{\switch{\ifnum#1<}
    {10 }{1} {100 }{2} {1000 }{3} {10000 }{4} {default}{5}
    \endswitch}

% List Manipulation ................
% this assumes that #1 is a list of tokens, #2 is the name of a list
%
% Names of lists (and of items to pop into) are given as strings;
% not as control sequences.
%
\newtoks\toks@lista \newtoks\toks@listb
% basic append/prepend macro
\long\def\@append@to@cslist#1#2#3{\begingroup\toks@lista=#2{#3}%
  \global#1=\xp\xp\xp{\xp\the\xp#1\the\toks@lista}\endgroup}
\long\def\@prepend@to@cslist#1#2#3{\begingroup\toks@lista=#2{#3}%
  \global#1=\xp\xp\xp{\xp\the\xp\toks@lista\the#1}\endgroup}
\def\@append@to@list{\csarg\@append@to@cslist}
% variations:
\long\def\append@to@list#1#2{\@append@to@list{#1}{}{#2}}
\def\append@list@to@list#1#2{%
  \@append@to@list{#1}{\xp\xp\xp}{\xp\the\csn#2\ecs}}
\def\append@cslist@to@cslist#1#2{%
  \@append@to@cslist{#1}\xp{\the#2}}
\def\append@toks@cs@to@list#1#2{\@append@to@list{#1}\xp{\the#2}}

% A few list macros for the user
\def\NewList:#1 {\csarg\newtoks{#1}\global\csn#1\ecs{}}
\def\EmptyList:#1 {\global\csn#1\ecs{}}
\long\def\AppendToList:#1=#2 {\@append@to@list{#1}{}{#2}}
\def\TheList:#1 {\let\oldwbuskip\willbeunhskip \let\willbeunhskip\@empty
    \if\UndefinedCS{#1}\message{List <#1> undefined}
    \else\csarg\the{#1}\fi
    \let\willbeunhskip\oldwbuskip}

\def\del@tok@from@list#1#2{\begingroup
    \long\def\cs@liste##1#2##2\tok@SM
       {\toks@lista{##1}\toks@listb{##2}%
        \edef\cs@listb{\global\CSname{#1}=
                {\the\toks@lista\the\toks@listb}}%
        \cs@listb}%
    \edef\cs@lista{\nxp\cs@liste{}\xp\the\csn#1\ecs\nxp\tok@SM}%
    \cs@lista \endgroup}
\def\in@front@of@list#1{\csarg{\let\xp\cs@lista}{#1}%
  \in@front@of@cslist\cs@lista}
\long\def\in@front@of@cslist#1#2{\begingroup\toks@lista={#2}%
  \global#1=\xp\xp\xp{\xp\the\xp\toks@lista \the#1}%
  \endgroup}
\long\def\local@in@front@of@list#1#2{\toks@lista={#2}%
    \csname#1\endcsname\xp\xp\xp\xp\xp\xp\xp
                {\xp\xp\xp\the\xp\xp\xp\toks@lista 
                 \xp\the\csname#1\endcsname}%
    }
\def\set@list#1{\csarg{\let\xp\cs@lista}{#1}%
  \set@cslist\cs@lista}
\long\def\set@cslist#1#2{\begingroup\toks@lista={#2}%
   \global#1=\xp{\the\toks@lista}\endgroup}

%%%%%%%%%%%%%%%% Stack macros
%
% push in terms of prepending of lists
\def\push@cs@onto@cs#1#2{\@prepend@to@cslist#1\xp{\xp\\\xp{\the#2}}}
\def\push@onto#1{\csarg\push@onto@cs{#1}}
\def\push@onto@cs#1#2{\@prepend@to@cslist#1{}{\\{#2}}}
\def\local@push@onto#1#2{\let\\=\relax 
    \local@in@front@of@list{#1}{\\{#2}}}
% pop via auxiliary macro: #1 is result, #2 is stack
% two cs arguments
\def\pop@cs@into#1#2{\edef\cs@e
   {\nxp\@@popinto\nxp#1\nxp#2\the#2\nxp\@@pop}\cs@e}
% two string arguments
\def\pop@into#1#2{\edef\cs@e
   {\nxp\@@popinto\CSname{#1}\CSname{#2}\csarg\the{#2}\nxp\@@pop}\cs@e}
\long\def\@@popinto#1#2\\#3#4\@@pop{#1{#3}#2{#4}}
\def\copy@stacktop#1#2{%
    \edef\cs@e{\noexpand\@copy@stacktop
                   {#1}\csarg\the{#2}\noexpand\@@pop}%
    \cs@e}
\def\@copy@stacktop#1\\#2#3\@@pop{\csname#1\endcsname#2\relax}
\newcount\stack@length \newtoks\empty@stack \empty@stack{\\{}}
\def\length@of@stack#1{\csarg\length@of@csstack{#1}}
\def\length@of@csstack#1{\def\\##1{\advance\stack@length\@ne}%
  \stack@length\m@ne \the#1}
% turn a stack upside down
\def\invert@csstack#1{\length@of@csstack{#1}%
  \tempcountb\z@ \temptoksa\empty@stack
  \loop\ifnum\tempcountb<\stack@length
    \pop@cs@into\temptoksb#1\advance\stack@length\m@ne
    \push@cs@onto@cs\temptoksa\temptoksb
%    \append@cslist@to@cslist\temptoksb\temptoksa% append a to b
%    \temptoksa\temptoksb
    \repeat% copy b to a
  #1\temptoksa \temptoksa\empty@toks}
% invert a stack into a list
\def\x@stack@to@list#1{\length@of@stack{#1}%
  \tempcountb\z@ \temptoksa\empty@toks
  \loop\ifnum\tempcountb<\stack@length
    \pop@into{temptoksb}{#1}\advance\stack@length\m@ne
    \append@list@to@list{temptoksb}{temptoksa}\repeat
  \csn#1\ecs\temptoksa \temptoksa\empty@toks}
    

% Bookkeeping and Householding lists................
% Generic Defaults
% to be executed each time a definition starts
%
\ifini{\newtoks\generic@defaults}
\def\adds@generic@default#1{
   \append@to@list{generic@defaults}{#1}}
\def\add@generic@default#1{\ifini
   {\append@to@list{generic@defaults}{#1}}}

% Document Start Commands
% to be executed by \Start
%
\ifini{\newtoks\start@commands}
\def\add@start@command#1{\ifini
   {\append@to@list{start@commands}{#1}}}
\def\adds@start@command#1{%
    \append@to@list{start@commands}{#1}}

% Document Stop Commands
% to be executed by \Stop
%
\ifini{\newtoks\stop@commands}
\def\add@stop@command#1{\ifini
   {\append@to@list{stop@commands}{#1}}}
\def\adds@stop@command#1{%
    \append@to@list{stop@commands}{#1}}

% Style Start Commands
% to be executed by \StyleDefinition
%
\ifini{\newtoks\style@start@commands}
\def\add@style@start@command#1{\ifini
   {\append@to@list{style@start@commands}{#1}}}

% Style Stop Commands
% to be executed by \StyleDefinitionStop
%
\ifini{\newtoks\style@stop@commands}
\def\add@style@stop@command#1{\ifini
   {\append@to@list{style@stop@commands}{#1}}}
\add@style@stop@command{\style@stop@commands{}
    \style@start@commands{}}

\def\check@existence#1{\Wmessage{Used \nxp\check@existence. Check}}
%    \add@style@stop@command{\if\UndefinedCS{#1}\Wmessage
%        {Some trouble involving #1; check definitions carefully!}\fi}

%\add@style@start@command{\are@these@correct{}}
%\add@style@stop@command{\if\IsEmptyList{\the\are@these@correct}\else
%    \Wmessage{Check if the following are literals: \the\are@these@correct}
%    \fi}

% Tracing
%
% if trace@all=0 and \trace@foo is >0, every \Tmessage[foo] is written.
% if \Trace:no, then all trace is disabled through \trace@all=-1
% if \Trace:yes then all traces are written through \trace@all=1
% by default: \trace@all=0
%
\def\Tmessage{\ifnum\trace@all>\m@ne\xp\t@message\else\xp\del@message\fi}
\def\del@message[#1]#2{}\let\trace@prefix\@empty
\def\t@message[#1]#2{%
  \ifnum\trace@all>\z@ \def\trace@prefix{[T]}\@meld{#2}%
  \else\csarg\ifnum{trace@#1}>\z@ 
    \def\trace@prefix{[T:#1]}\@meld{#2}\fi
  \fi\global\let\trace@prefix\@empty}

\def\Trace:#1 {\switch{\if\EqualString{#1}}
    {yes} {\trace@all\@ne\message{Switching ON all tracing}}
    {no}  {\trace@all\m@ne\message{Switching OFF all tracing}}
    {default} {\if\UndefinedCS{trace@#1}\Wmessage{Strange trace: #1}\else
            \csname trace@#1\endcsname\@ne \trace@all\z@ \fi}
    \endswitch}
\def\NewTrace:#1 {\new@count{trace@#1}\m@ne}
\new@count{trace@all}\z@
\NewTrace:gen % traces everything in this file
\NewTrace:def % definitions of constructs etc

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%% Generic Constructs %%%%%%%%%%%%%%%%%%%%%

% \@GenericConstruct{Foo} =
%     \newtoks\Foo@defaults
%     \Foo@defaults={}
%     \def\add@Foo@default#1{...}
%     \def\DefineFoo#1 =
%                \@FooDefaults      % has to be defined explicitly
%                \the\Foo@defaults  % constructed cumulatively
%                \@GenericDefaults  % constructed cumulatively
%                \@GetOptions
%                \@DefineFoo        % has to be defined explicitly
%     \def\@FooOption ...           % same syntax as \@GenericOption
%
% Options are either generic (the ones below)
% defined by \@GenericOption,
% or specific for some construct,
% defined by \@FooOption
%
\ifini{\newtoks\@gencons}
\newif\ifdefining@a@construct
\def\@GenericConstruct#1{
    \ifini{\append@to@list{@gencons}{\\#1;}
           \csarg\newtoks{#1@defaults}
           \csn #1@defaults\ecs{}}
    \csarg\def{add@#1@default}##1{\ifini
                   {\append@to@list{#1@defaults}{##1}}}
    \csarg\def{adds@#1@default}##1{\append@to@list{#1@defaults}{##1}}
    \Install@Noops{#1}
    \csarg\def{Define#1}:##1 {\defining@a@constructyes
        \def\@name{##1}\def\@class{#1}
        \Tmessage[def]{Defining a #1: ##1}
        \the\generic@defaults
        \csarg\the{#1@defaults}
        \Get@Items}
    \csarg\def{@#1Option}##1{\csarg\def{#1@##1}####1####2}
    }
\add@generic@default{\let\pre@fix\@empty}
\def\@command{\ifx\pre@fix\@empty\else\pre@fix @\fi\@name}
\def\stop@command@suffix{Stop}
\def\stop@command{\@command\stop@command@suffix}
\def\@DefineStopCommand{\csarg\edef{\stop@command}}

%%%% Abbreviated Closing:
% every group defines its own \default@stop@command;
% on the outer level this is an error msg.
% \> closes whatever is open, \>] closes everything that is open.
\def\outer@stop@command{\Emessage{Vacuous group closing}}
\let\default@stop@command\outer@stop@command
\def\>{\default@stop@command%[fool the editor
    \ifNextChar]{\ifx\default@stop@command\outer@stop@command
                       \xp\take@one
                 \else \xp\>\fi}{}}
\newif\if@implicitclose
\add@generic@default{\@implicitcloseyes}
% the next line replicated below
%\@GenericOption{noimplicitclose}{\@implicitcloseno}
\def\install@stop{\if@implicitclose
        \def\nxp\default@stop@command{\CSname{\stop@command}}%
    \else \let\nxp\default@stop@command\nxp\outer@stop@command 
    \fi}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%% Here is the main
%%%%%%%%%%%%%%%% I T E M   A N A L Y S I S
%%%%%%%%%%%%%%%% loop
%%%%

% This macro is recursive, but with some detours.
% first filter out empty arguments
%
\def\Get@Items#1 {\if\IsEmptyList{#1}\let\get@next@item\Get@Items
    \else\def\get@next@item{\@Get@Items#1 }\fi
    \get@next@item}

% Check if the argument is `Stop', in which case you
% have reached the end of a generic definition. 
% Otherwise filter the special case of a colon, and process an item
%
\NewDummy{Stop}
\def\@Get@Items#1 {\let\get@next@item=\Get@Items
    \csarg\ifx{#1}\Stop
          \the\generic@stop@defaults
          \let\get@next@item=\relax
    \else \if\EqualString{#1}{:}\@add@toks{:}
          \else \Item@or@Macro#1::. \fi 
    \fi \get@next@item}

% Default actions to be executed when GetItems has finished
%
\def\add@generic@stop@default#1{\ifini
   {\append@to@list{generic@stop@defaults}{#1}}}
\ifini{\newtoks\generic@stop@defaults}
\add@generic@stop@default{\csname @Define\@class\endcsname}

% Now check successive possibilities:
% first it can be a macro.
%
\def\OptionsMacro:#1=#2Stop {\global\csarg\def{opt@mac@#1}{#2}}
\def\split@at@is#1=#2=#3.{{#1}{#2}}
\def\Item@or@Macro#1:#2:#3.{
    \switch {\if\EqualString{#1}}
      {macro} {\if\UndefinedCS{opt@mac@#2}
            \Wmessage{Unknown macro: `#2'}
            \else \edef\get@next@item{\nxp\Get@Items \csn opt@mac@#2\ecs }
            \fi}
    {default} {\edef\cs@e{\nxp\Option@Item{#1}\split@at@is#2==.}
               \cs@e}
    \endswitch}

% The option #1 is not a Lollipop style macro. It can be
% - a specific option
% - a generic option
% - a character
% - a defined control sequence
%
% Is it an option of some sort?
%
\def\Option@Item#1#2#3{
    \if\UndefinedCS{\@class @#1}
        \if\UndefinedCS{\gen@option@name{#1}}
              \Character@CS@Item{#1}{#2}{#3}
        \else \if\EqualString{#2}{title}
                 \edef\cs@e{\CSname{\gen@option@name{#1}}\CSname{#3Title}}
                 \cs@e
              \else \csn \gen@option@name{#1}\ecs{#2}{#3}
              \fi
              \Tmessage[gen]{Generic option `#1' for `\@class':
                             `#2' = `#3'}
        \fi
    \else \if\EqualString{#2}{title}
                 \edef\cs@e{\CSname{\@class @#1}\CSname{#3Title}}
                 \cs@e
          \else \csn \@class @#1\ecs{#2}{#3}
          \fi
          \Tmessage[gen]{Specific option `#1' for `\@class': `#2'}
    \fi}

% If it is not an option, is it a character or control sequence?
%
% Commands are either `\Command', `\Command:arg', `\Command:arg1=arg2'
% See what arguments are nonempty in order to attach them;
% in case 2 and 3 append space token.
%
\def\Character@CS@Item#1#2#3{
    \def\cs@b{#1}\def\cs@c##1##2<<<{\def\cs@c{##1}}\cs@c#1<<<
    \ifx\cs@b\cs@c \@add@toks{#1}
          \Tmessage[gen]{Single character `#1' for `\@class'}
    \else
        \if\UndefinedCS{#1}
            \Wmessage{Unknown option for `\@class': `#1' in `\@name'.}
            \@add@toks{\csn#1\ecs}
        \else 
            \cs@in@ref@protect{#1}%
            \edef\cs@e{\nxp\@add@toks{\CSname{#1}}}\cs@e
        \fi
        \if\IsEmptyList{#2} 
        \else \if\IsEmptyList{#3}\@add@toks{:#2 }
              \else             
                 \if\EqualString{#2}{title}
                       \edef\cs@e{\nxp\@add@toks{:\CSname{#3Title} }}
                       \cs@e
                 \else \@add@toks{:#2={#3} }
                 \fi
              \fi
        \fi
        \Tmessage[gen]{Command `#1' (#2,#3)}
    \fi}



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% G E N E R I C   O P T I O N S
%%%%
%
% All generic constructs get these options.
% They are at liberty to redefine them.
%
\newtoks\@GenericOptions
\def\gen@option@name#1{Option@#1}
\def\@GenericOption#1{
    \append@to@list{@GenericOptions}{\\#1;}
    \csarg\def{\gen@option@name{#1}}##1##2}
\def\Noop@Error#1#2{\Emessage{Option #2 is not allowed for class #1}}

\def\Install@Noops#1{
    \csarg\def{@#1NOOP}##1{\csarg\def{#1@##1}{\Noop@Error{#1}{##1}}}
    }
\newtoks\@main@options@list
\newtoks\before@coms \newtoks\after@coms
\add@generic@default
   {\def\@current@options@list{@main@options@list}%
    \@main@options@list{}
    \before@coms{} \after@coms{}}
\long\def\@add@toks#1{\append@to@list{\@current@options@list}{#1}}
\def\toks@add@toks#1{
    \edef\cs@e{\nxp\append@to@list
                 {\nxp\@current@options@list}{\xp\the\csn#1\ecs}}
    \cs@e \let\cs@e\relax}
\def\switch@to@options@list#1{\def\@current@options@list{#1}}
\def\temp@options@list#1{
    \xdef\saved@options@list{\@current@options@list}
    \def\@current@options@list{#1}}
\def\revert@options@list{
    \xdef\@current@options@list{\saved@options@list}
    \global\let\saved@options@list\@empty}

\newtoks\z@toks \z@toks={0pt}

% the next line should be higher, 
% but \@GenericOption wasn't defined yet there
\@GenericOption{noimplicitclose}{\@implicitcloseno}

%%%%%%%%%%%%%%%% Universal textblock 
%
% These macros will only be used inside an \edef.
% They are meant for text blocks, but the \outer@start@commands
% are also necessary for output routines, hence the test
% if this is a text-like construct.
%
\newif\iftext@construct
\add@generic@default{\text@constructyes}

% Level of nesting
%
\newcount\nest@depth

% Open command
%
\def\@gen@open{\outer@start@commands 
    \begingroup \inner@start@commands}

\def\outer@start@commands{%
    \iftext@construct
        \ifleft@embedded@construct \install@h@before@penalty
        \else \nxp\leavehmode \nxp\v@buffer{\the\@whitebefore}%
        % backspace previous white space while it's visible
              \nxp\if@headed\nxp\else\install@v@before@penalty
              \nxp\fi
    \fi \fi
    \ifhas@counter \nxp\StepCounter:\expandafter\@name\@space
        % This sets the \current@label by default
        \ifhas@marks
            \edef\nxp\cs@e{\nxp\nxp\nxp\refresh@mark@item
                         {\@name Counter}{\CSname{\@name Counter}}}%
            \nxp\cs@e      
        \fi
        \fi
    \iflabel@defined 
        \global\current@label={\the\@labelcoms}\fi
    \ifhas@title \install@title@code
        % subtle: the title has to be installed after any page break
        % otherwise footlines may contain the wrong title.
        % also the title has to be global.
    \fi
    \ifhas@marks\nxp\ifnin{\nxp\place@mark}\fi
        %otherwise IniTeX'ing Lollipop will output a page
    \nxp\xx@label\the\extern@toks\penalty\@M
        % also subtle: if this white space would be higher, it would
        % be invisible because of marks et cetera.
        % insert nobreak after marks/writes to prevent page breaks.
    \iftext@construct
        \ifleft@embedded@construct
              \else \nxp\@vwhite{\the\@whitebefore}\fi
    \fi
    }
\def\inner@start@commands{%
    \nxp\Open@Group\CSname{\@class}\CSname{\@name}%
    \install@stop
    \ifleft@embedded@construct
    \else \nxp\hold@parskip 
        \nxp\default@everypar
        \ifwhiteleft@defined 
            \advance@skip\leftskip{\the\@whiteleft}\fi
        \ifwhiteright@defined 
            \advance@skip\rightskip{\the\@whiteright}\fi
        \nxp\let\nxp\par=\nxp\@par %explain to me again why this is necessary...
        \inside@indent \first@indent
    \fi
    \advance\nest@depth\@ne
    }

\def\@gen@close{\inner@end@commands \endgroup \outer@end@commands}
\def\inner@end@commands{%
    \nxp\Close@Group\CSname{\@class}\CSname{\@name}%
    \the\late@extern@toks
    \ifright@embedded@construct \install@h@after@penalty
    \else \nxp\leavehmode \install@v@after@penalty
          \nxp\@vwhite{\the\@whiteafter}\fi
    }
\def\outer@end@commands{%
    \the\after@toks
    \ifright@embedded@construct \nxp\@headedno
    \else \after@indent \nxp\dono@parskip
    \fi}
\let\esp@hack\relax
\def\bsp@hack{\let\esp@hack\relax
    \ifhmode\ifdim\lastskip>\z@\let\esp@hack\ignorespaces\fi\fi}

%%%%%%%%%%%%%%%% Breaking

% Constructs such as 'line' or 'rule' introduce breakpoints.
% Here's how we deal with that. The \ifat@breakpoint test is set
% by options such as 'line' or 'vwhite'. Similarly, these options
% test for this condition and if necessary protect.
% After 'text' in a text block the condition is set too.
\newif\ifat@breakpoint
\add@generic@default{\at@breakpointno}
\def\protect@breakpoint{\ifat@breakpoint\@add@toks{\nobreak}\fi}

% Break Before
% user specifies yes/no, numeric value, or name of value
% yes also does a fill
% default: place zero penalty
%
\newif\ifdefault@break@before
\adds@generic@default{\default@break@beforeyes}
\@GenericOption{breakbefore}{\default@break@beforeno
    \switch {\if\EqualString{#1}}
    {no}  {\def\@beforepenalty{\penalty\@M }}
    {yes} {\def\@beforepenalty{\CSname{\h@or@v fil}\penalty-\@M}}
    {0}   {\def\@beforepenalty{}}
    {default} 
      {\edef\cs@e{\def\nxp\@beforepenalty
                     {\penalty\@w@w{#1}\relax}}
       \cs@e}
    \endswitch}
\def\install@v@before@penalty
   {\ifdefault@break@before\penalty\z@
    \else\nxp\ifnum\lastpenalty=\z@\@beforepenalty\nxp\fi
    \fi}
\def\install@h@before@penalty
   {\ifdefault@break@before
    \else\nxp\ifnum\lastpenalty=\z@\@beforepenalty\nxp\fi
    \fi}

% Break After
% same story
%
\newif\ifdefault@break@after
\adds@generic@default{\default@break@afteryes}% Heading overrides
\@GenericOption{breakafter}{\default@break@afterno
    \switch {\if\EqualString{#1}}
    {no}  {\def\@afterpenalty{\penalty\@M }}
    {yes} {\def\@afterpenalty{\CSname{\h@or@v fil}\penalty-\@M}}
    {0}   {\def\@afterpenalty{}}
    {default} 
      {\edef\cs@e{\def\nxp\@afterpenalty
                     {\penalty\@w@w{#1}\relax}}
       \cs@e}
    \endswitch}
\def\install@v@after@penalty
   {\ifdefault@break@after\penalty\z@ \else\@afterpenalty \fi}
\def\install@h@after@penalty
   {\ifdefault@break@after \else\@afterpenalty \fi}

%%%%%%%%%%%%%%%% Surrounding White Space 
% WhiteBefore
% amount is put in token list \@whitebefore,
% the \@define... macro uses this to its own discretion
% For the three text construct this happens in the general macros
% In ExternalItem a different default is used
%
\ifini{\newtoks\@whitebefore \newskip\whitebefore}
\add@generic@default{\@whitebefore={whitebefore}}
\whitebefore=6pt plus 6pt\relax
\@GenericOption{whitebefore}{
%    \edef\cs@e{\@whitebefore={\@w@w{#1}}}\cs@e}
    \@whitebefore={#1}}

% WhiteAfter
% same story with \@whiteafter
%
\ifini{\newtoks\@whiteafter \newskip\whiteafter}
\add@generic@default{\@whiteafter={whiteafter}}
\whiteafter=6pt\relax
\@GenericOption{whiteafter}{
%    \edef\cs@e{\@whiteafter={\@w@w{#1}}}\cs@e}
    \@whiteafter={#1}}

% WhiteLeft
% same story with \@whiteleft
%
\ifini{\newtoks\@whiteleft}
\newif\ifwhiteleft@defined
\add@generic@default{\@whiteleft\z@toks \whiteleft@definedno}
\@GenericOption{whiteleft}{
%    \edef\cs@e{\nxp\@whiteleft={\@w@w{#1}}}\cs@e
    \@whiteleft={#1}
    \whiteleft@definedyes}

% WhiteRight
% same story with \@whiteright
%
\ifini{\newtoks\@whiteright}
\newif\ifwhiteright@defined
\add@generic@default{\@whiteright\z@toks \whiteright@definedno}
\@GenericOption{whiteright}{
    \edef\cs@e{\nxp\@whiteright={\@w@w{#1}}}\cs@e
    \whiteright@definedyes}

%%%%%%%%%%%%%%%% Group Bookkeeping
\def\@@nulklasse{????}\def\@@nulnaam{????}
\let\@@groepklasse=\@@nulklasse \let\@Group@Name=\@@nulnaam

% Grouping level during definition
%
\new@@counter{group} 
\add@generic@default{\start@counter{group}}
\add@generic@stop@default{\if\TestCounter:group >0 
    \Emessage{There are groups open in <\@name>}\fi}

\add@generic@default{\def\current@def@group{>>OUTER LEVEL<<}}
\def\b@group{\edef\@outer@options@list{\@current@options@list}
  \bgroup\step@counter{group}
  \ifnextchar[\def@def@group{}%]
  }
\def\def@def@group[#1]{\def\current@def@group{#1}}
\def\e@group{\ifnextchar[\test@def@group\egroup%]
    }
\def\test@def@group[#1]{\def\current@test@group{#1}
    \ifx\current@def@group\current@test@group\egroup
    \else\Emessage{Closing `\current@def@group' with `\current@test@group'
                   in \@class: \@name}
    \fi}


% Grouping during execution
%
\def\@@testklasse{OUTER}\def\@@testnaam{OUTER}
\def\@DEFgroepsluit{\nxp\Close@Group{\@class}{\@name}}
\def\@groepERR#1#2{\Wmessage{Unexpected close of 
    <\expandafter\string#1; expected: \expandafter\string#2.}}
\newcount\group@open@lineno
\def\Open@Group#1#2{\def\@@groepklasse{#1}\def\@Group@Name{#2}%
    \Tmessage[gen]{Open \string#1\@space \string#2 @ \the\inputlineno}
    \group@open@lineno\inputlineno}
\def\Close@Group#1#2{\def\@@testklasse{#1}\def\@@testnaam{#2}%
    \ifx\@@testnaam\@Group@Name 
        \Tmessage[gen]{Close \string#1\@space \string#2}%
    \else \@groepERR{\@@testnaam}{\@Group@Name}\fi}
\def\Close@all@groups
   {\ifx\@Group@Name\@@nulnaam
    \else \Wmessage{Group not closed: \expandafter\string\@Group@Name; 
            start @\the\group@open@lineno}
          \endgroup
          \Close@all@groups
    \fi}

\def\@groeptester{\ifx\@Group@Name\@@nulnaam
    \else \Wmessage{Group still open: \expandafter\string\@Group@Name}\fi}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%% Counters

% Counter name
% has been abstracted.
% this may be removed later for efficiency.
%
%\def\counter@name#1{#1@C}%defined above
\def\cs@counter@name#1{\csn#1@C\ecs}
\def\counter@@name#1{\CSname{#1@C}}
\def\counter@value#1{\xp\the\csn#1@C\ecs}

%
% \NewCounter:COUNTER
%     \newcount\COUNTER@C
%     \CounterRepresentation:COUNTER=1  ; print arabic numeral
%     \StartCounter:COUNTER             ; set to 0
%     \COUNTER@RL={}                    ; create reset list
%
% \CounterRepresentation:THING=W
%     if W is another counter, make synonym, otherwise
%     \THING@R <- representation command
%     \THINGCounter = \THING@R \THING@C
% #1 = naam van een nieuw te definieren teller; 
% #2 = telwijze of naam van teller waar de nieuwe teller synoniem
%      mee moet worden.
%
\def\NewCounter:#1 {\new@counter{#1}}
\def\new@counter#1{
    \new@@counter{#1}\csarg\edef{#1Value}{\counter@@name{#1}}
    \CounterRepresentation:{#1}=1
    \StartCounter:{#1}
    }
% given above
%\def\new@@counter#1{\csarg\newcount{\counter@name{#1}}}
\def\AdaptiveCounter:#1 
   {\if\UndefinedCS{\counter@name{#1}}\new@counter{#1}\fi
    \adds@stop@command
      {\ifWriteExtern\immediate\write\aux@file
         {\string\SetCounter:#1={\counter@value{#1}}} \fi
         }}

% Counter allocation in the generic constructs
%     counter:x
% indicates the mode of representation of the counter
% of the construct being defined. The counter is first allocated.
% A switch is thrown that will generate automatic step instructions.
%
\newif\ifhas@counter
\@GenericOption{HasCounter}{
    \switch {\if\EqualString{#1}}
    {yes} {\has@counteryes\has@labelyes
           \if\IsEmptyList{#2}\else\def\this@counter@repr{#2}\fi}
    {no}  {\has@counterno}
    {default} {\has@counteryes}
    \endswitch}
\@GenericOption{counter}{
    \ifin@label \edef\cs@e{\nxp\@add@toks{\CSname{\@name Counter}}}
         \cs@e
    \else\has@counteryes\has@labelyes \def\this@counter@repr{#1}
    \fi}
\def\install@counter#1{\new@counter{\@name}%
    \ifhas@marks\xp\add@mark@item\xp{\@name Counter}\fi
    \if\IsEmptyList{#1}\else\CounterRepresentation:\@name=#1 \fi
    }

% Counter-related defaults: initially there is no counter,
% if a counter starts it counts Arabic unless otherwise indicated,
% and the countername associated with this construct is
% made available as an option; 
% later it will be a command, and the option is removed again.
%
\add@generic@default{\has@counterno\has@labelno
    \def\this@counter@repr{1}
    \csarg\def{\gen@option@name{\@name Counter}}{%
        \edef\cs@e{\nxp\@add@toks{%\nxp\cprotect
                           \CSname{\@name Counter}}}\cs@e
        \global\has@counteryes\global\has@labelyes}}
\add@generic@stop@default
   {\csarg\let{\gen@option@name{\@name Counter}}\relax}
\add@generic@stop@default{\ifhas@counter
    \xp\install@counter\xp{\this@counter@repr}\fi}

%titles are handled below but the actions have to be
%in this order.
\add@generic@stop@default
  {\ifhas@title \ifshort@title \@sTitelize\@command
                \else \@Titelize{\@command}
   \fi\fi}
\add@generic@stop@default{\ifhas@label \@Labelize{\@command}\fi}


%%%%%%%%%%%%%%%% Counter Representaton
% main use: indicate representation of numerical value of counter
% control sequence \FooCounter <- Foo@R\Foo@C
% two-step approach necessary because of symbolic reference:
% not all representations work purely by expansion.
%

% 1 arabic, i lowercase roman, I uppercase roman
% a lowercase character, A uppercase Character
%
\def\@arabic#1{\number#1 }
\def\@lcascii#1{{\tempcounta=#1\relax \advance\tempcounta by 96
    \char\tempcounta\relax}}
\def\@ucascii#1{{\tempcounta=#1\relax \advance\tempcounta by 64
    \char\tempcounta\relax}}
\def\@roman#1{\romannumeral#1 }
\def\@Roman#1{\uppercase\expandafter{\romannumeral#1}}

% Representation has been abstracted
%
\def\counter@repr#1{#1@R}

% Give the representing control sequence (@roman, ...)
% inside an \edef
%
\def\counter@@repr#1{\xp\xp\xp\string\csname#1@R\endcsname}

% Counter Representation (user command)
% decides if this is a real representation command,
% or a synonym declaration
%
\def\CounterRepresentation:#1=#2 {
    \if\UndefinedCS{\counter@name{#2}}%is this counter a synonym?
          \represent@counter{#1}{#2}
    \else \@SynonymCounter{#1}{#2}
    \fi}
\def\represent@counter#1#2{
    \edef\cs@e{@\if#2iroman\else
       \if#2IRoman\else \if#2alcascii\else
       \if#2Aucascii\else arabic\fi\fi\fi\fi}
%    \csarg\xdef{\counter@repr{#1}}{\CSname{\cs@e}}
    \csarg\edef{\counter@repr{#1}}{\CSname{\cs@e}}
    \csarg\xdef{#1Counter}{\nxp\cprotect %\xp\string when writing label out
          \CSname{\counter@repr{#1}}{\nxp\number\counter@@name{#1}}}
    }
\def\cprotect{}

\def\PrintCounter:#1 {\csn#1Counter\ecs}

% Synonym Counters
% share the \FooCounter command, internal name, and reset list
%
\def\@SynonymCounter#1#2{
    \edef\cs@b{\nxp\let\counter@@name{#1}=\counter@@name{#2}
               \nxp\let\CSname{#1Counter}=\CSname{#2Counter}
               \nxp\let\CSname{#1@RL}=\CSname{#2@RL}}
    \cs@b
    }

% Synonym as generic option
%     sharecounter:xyz
% Don't allocate a counter, but share the counter of the
% 'xyz' construct.
%
\@GenericOption{sharecounter}{\CounterRepresentation:\@name=#1 }

% Governing counters
% If a counter governs another counter, the other is reset
% whenever the first is altered.
%
\def\GoverningCounter:#1=#2 {\tempano
  \if\UndefinedCS{\counter@name{#1}}
    \Wmessage{No such counter <#1>}
  \else\if\UndefinedCS{\counter@name{#2}}
            \Wmessage{No such counter <#2>}
         \else\tempayes
  \fi\fi
  \iftempa\if\UndefinedCS{#2@RL}\csarg\newtoks{#2@RL}
             \csn #2@RL\ecs={}\fi
    \append@to@list{#2@RL}{\\#1;}
  \fi}
\def\reset@subordinates#1{\if\UndefinedCS{#1@RL}\else
    \def\\##1;{\start@counter{##1}}%
    \the\csname #1@RL\endcsname  \let\\=\relax
  \fi}

% User counter manipulation
% should be global, and should reset subordinate counters
% and define references.
%
\def\StartCounter:#1 {\handle@user@counter{#1}{start}{}}
\let\ResetCounter\StartCounter
\def\StepCounter:#1 {\handle@user@counter{#1}{step}{}}
\def\AddToCounter:#1=#2 {\handle@user@counter{#1}{addto}{#2}}
\def\BackStepCounter:#1 {\handle@user@counter{#1}{back@step}{}}
\def\SetCounter:#1=#2 {\handle@user@counter{#1}{set}{#2}}
\def\handle@user@counter#1#2#3{\if\UndefinedCS{\counter@name{#1}}
          \Wmessage{Unknown counter: #1}
    \else \begingroup\globaldefs\@ne
          \csn#2@counter\ecs{#1}{#3}%
          \reset@subordinates{#1}\define@reference{#1}%
          \endgroup
    \fi}

% Numeric manipulation
%
\def\CounterValue:#1 {%
  \if\UndefinedCS{\counter@name{#1}}0
  \else \cs@counter@name{#1}\fi
  }
\def\TestCounter:#1 {00\fi\ifnum\cs@counter@name{#1}}

% Internal counter manipulation
% is local and has no side effects
%
\def\step@counter#1{\increase@value{\counter@name{#1}}\@ne}
\def\addto@counter#1#2{\edef\cs@e
    {\nxp\increase@value{\nxp\counter@name{#1}}{\w@w{#2}}}\cs@e}
\def\back@step@counter#1{\increase@value{\counter@name{#1}}\m@ne}
\def\start@counter#1{\set@value{\counter@name{#1}}\z@}
\def\set@counter#1#2{\edef\cs@e
    {\nxp\set@value{\nxp\counter@name{#1}}{\w@w{#2}}}\cs@e}


%%%%%%%%%%%%%%%% Title
%
% All constructs can have titles.
% By using the option 'title' the user indicates that
% this particular option has a title.
% A control sequence \constructTitle is defined.
%
% In case the \constructTitle is only used in a macro,
% the user has to set HasTitle:yes as an option.
%
\def\@@@title#1{#1Title}\def\@@title{\@@@title\@name}
\newif\ifhas@title\add@generic@default{\has@titleno}
\newif\ifshort@title\add@generic@default{\short@titleno}
\add@generic@stop@default % we cannot use @@title here!
   {\ifhas@marks\ifhas@title\xp\add@mark@item\xp{\@name Title}\fi\fi}
\def\give@title{\global\has@titleyes
  \global\csarg\let\@@title\@space 
         %to define it until first call
  }
\@GenericOption{title}{\give@title
  \ifin@external \if\IsEmptyList{#1}
      \external@handle@title{\@name}
    \else \external@handle@title{#1}\fi
  \else \if\IsEmptyList{#1}
      \edef\cs@e{\nxp\@add@toks{\CSname{\@@title}}}\cs@e
    \else\Wmessage{Parametrized titles only in external}\fi
  \fi
  }
\@GenericOption{HasTitle}{
    \switch {\if\EqualString{#1}}
    {yes} {\give@title}
    {no}  {\global\has@titleno}
    {short} {\give@title \global\short@titleyes}
    {default} {\give@title}
    \endswitch
    }

% this code appears in \outer@start@commands;
% it will be executed in an \edef
%
\newtoks\title@toks
\def\install@title@code
   {%\nxp\xp\gdef\nxp\xp\CSname{\@name Title}\nxp\xp{%
    %       \nxp\xp\nxp\maybe@uppercase\nxp\xp{\nxp\the\title@toks}}%
  \nxp\xp\gdef\nxp\xp\CSname{\@name Title}\nxp\xp
         {\nxp\the\title@toks}%
  \ifhas@marks
    \edef\nxp\cs@e{\nxp\nxp\nxp\refresh@mark@item
                         {\@name Title}{\nxp\the\title@toks}}%
    \nxp\cs@e
  \fi}

% Give a macro a title
%
%\add@generic@stop@default{\ifhas@title \@Titelize{\@name}\fi} %above!
%
\newcount\extra@args \add@generic@default{\extra@args\z@}
\def\ttl@name#1{ttl@#1}
\def\@Titelize#1{\edef\cs@e{\let\CSname{\ttl@name{#1}}=\CSname{#1}}\cs@e
    \ifcase\extra@args %0:
        \csarg\edef{#1}##1\par
             {\title@toks\nxp\xp{\nxp\id@##1\nxp\willbeunhskip}%
%             {\title@toks{##1\nxp\protect\nxp\unhskip}%
              \CSname{\ttl@name{#1}}}
    \or %1:
        \csarg\edef{#1}##1##2\par
             {\title@toks\nxp\xp{\nxp\id@##2\nxp\willbeunhskip}%
%             {\title@toks{##2\nxp\protect\nxp\unhskip}%
              \CSname{\ttl@name{#1}}{##1}}
    \or %2:
        \csarg\edef{#1}##1##2##3\par
             {\title@toks\nxp\xp{\nxp\id@##3\nxp\willbeunhskip}%
%             {\title@toks{##3\nxp\protect\nxp\unhskip}%
              \CSname{\ttl@name{#1}}{##1}{##2}}
    \else \Wmessage{Sorry, too many extra arguments %
                    for `\@class' : `\@name'}
    \fi}
\begingroup\catcode`\^^M\active \endlinechar-1\relax%
% trick: with an active ^^M we can let it expand to \@space
% not a 100\% solution, true.
\gdef\@sTitelize#1{
  \edef\cs@e{\let\CSname{x@\ttl@name{#1}}=\CSname{#1}}\cs@e
  \csarg\edef{#1}{\begingroup\endlinechar`\nxp\^^M
      \catcode\endlinechar\active\CSname{\ttl@name{#1}}}
% short titles can have no unwanted trailing space
  \ifcase\extra@args %0:
      \csarg\edef{\ttl@name{#1}}##1^^M
         {\endgroup\let\nxp^^M\nxp\@space
          \title@toks\nxp\xp{\nxp\id@##1}%
%          \title@toks{##1\nxp\protect\nxp\unhskip}%
          \CSname{x@\ttl@name{#1}}}
  \or %1:
      \csarg\edef{\ttl@name{#1}}##1##2^^M
         {\endgroup\let\nxp^^M\nxp\@space
          \title@toks\nxp\xp{\nxp\id@##2}%
%          \title@toks{##2\nxp\protect\nxp\unhskip}%
          \CSname{x@\ttl@name{#1}}{##1}}
  \or %2:
      \csarg\edef{\ttl@name{#1}}##1##2##3^^M
         {\endgroup\let\nxp^^M\nxp\@space
          \title@toks\nxp\xp{\nxp\id@##3}%
%          \title@toks{##3\nxp\protect\nxp\unhskip}%
          \CSname{x@\ttl@name{#1}}{##1}{##2}}
  \else \Wmessage{Sorry, too many extra arguments %
                    for `\@class' : `\@name'}
  \fi}
\endgroup

% Uppercase titles are a big problem
% after \MakeUpperCase
% any appearing title (including when it comes out of a mark)
% is uppercased
%
\newif\ifUpperCasing \def\MakeUpperCase{\UpperCasingyes}
\def\maybe@uppercase{\ifUpperCasing\uppercase\xp\fi}

%%%%%%%%%%%%%%%% M O D E S
%
% controls whether a construct can start/end as part of a paragraph.
% chooses between a lot of horizontal/vertical commands.
% 
% this should appear as one of the first options.
% maybe generate an error msg if it doesn't
%
\newif\ifleft@embedded@construct
\newif\ifright@embedded@construct
\add@generic@default{\left@embedded@constructno\right@embedded@constructno}
\newif\if@hmode \newif\if@vmode
\add@generic@default{\@hmodeno\@vmodeyes}
\@GenericOption{embedded}{\switch {\if\EqualString{#1}}
    {yes}   {\left@embedded@constructyes\right@embedded@constructyes}
    {no}    {\left@embedded@constructno \right@embedded@constructno}
    {left}  {\left@embedded@constructyes\right@embedded@constructno}
    {right} {\left@embedded@constructno \right@embedded@constructyes}
    {default} {\Wmessage{Strange modifier: `embedded:#1'}}
    \endswitch
    \ifleft@embedded@construct \@hmodeyes \@vmodeno \fi}

% Mode dependent
%
\def\h@or@v{\ifleft@embedded@construct h\else v\fi}
\def\append@no@hv@break
   {\edef\cs@e{\nxp\@add@toks{%\if@hmode \if@vmode\else\nxp\leavevmode\fi\fi
                         %\if@vmode \if@hmode\else\nxp\leavehmode\fi\fi
                         \nxp\nobreak}}
    \cs@e}

%%%%%%%%%%%%%%%% Boxes
%
% Line
% is a \hbox to \hsize
% mainly used to determine the width of stuff inside it.
%
\newif\ifin@line
\add@generic@default{\in@lineno}
\newcount\text@in@line \newtoks\line@toks \newbox\line@box
\def\rm@to@end@of@box{\everyvbox{}%
    \setbox\tempboxa\vbox\bgroup
    \aftergroup\rm@tmp@box}
\def\rm@tmp@box{\setbox\tempboxa\box\voidb@x\egroup}
\def\definitely@to@hsize{\def\maybe@to@hsize{ to\hsize}}
\@GenericOption{line}{
    \switch {\if\EqualString{#1}}
    {start} {\protect@breakpoint
          \b@group[line]\in@lineyes\line@toks{}
          \temp@options@list{line@toks}
          \@add@toks{\hbox \maybe@to@hsize\bgroup}\text@in@line\z@
          }
    {stop}  {\@add@toks{\hfil\egroup}\revert@options@list
          \ifnum\text@in@line>\z@ 
              \edef\cs@e{\nxp\@add@toks
                           {\line@toks={\the\line@toks}}}\cs@e
              \@add@toks{\begingroup\everyvbox{\rm@to@end@of@box}%
                         \let\maybe@to@hsize\relax
                         \setbox\line@box\the\line@toks
                         \tempdima\hsize\advance\tempdima-\wd\line@box
                         \everyvbox{}\definitely@to@hsize
                         \everyvbox{\hsize\tempdima}\the\line@toks
                         \endgroup}
          \else \@add@toks{\definitely@to@hsize}
              \toks@add@toks{line@toks} 
          \fi
          \e@group[line]\at@breakpointyes}
    {default} {\Wmessage{Strange modifier: `line:#1'}}
    \endswitch}

% Tekstkolom. Is een \vbox of \vtop.
%     Openen met 'begin' 'ophangen', 'topregel' of 'voetregel',
%        start,topregel : \vtop
%        voetregel      : \vbox
%        ophangen       : \vtop{\kern\z@
%     Eindigen met 'stop'
\@GenericOption{textcolumn}{
    \switch {\if\EqualString{#1}}
    {stop}     {\@add@toks{\egroup}\global\BlockWidth\hsize
             \e@group[textcolumn]\at@breakpointyes}
    {start}    {\open@text@column\vtop{}}
    {topline}  {\open@text@column\vtop{}}
    {hang}     {\open@text@column\vtop{\kern\z@}}
    {footline} {\open@text@column\vbox{}}
    {default} {\Wmessage{Strange modifier: `textcolumn:#1=#2'}}
    \endswitch}
\def\open@text@column#1#2%
   {\ifin@line\advance\text@in@line\@ne\else\protect@breakpoint\fi
    \@add@toks{#1\bgroup#2\leftskip\z@ \Indent:no \leavevmode}
    \b@group[textcolumn]
    \ifin@line\else\aftergroup\at@breakpointyes\fi
    }

% Depth strut: for use in line
\@GenericOption{depth}{
    \edef\cs@e{\nxp\@add@toks{\vrule width\z@ height\z@ depth \@w@w{#1}}}
    \cs@e}

% Block
% is an hbox
% opening
%   1/ blok:start --> \setbox\block@box=\hbox{
%           and \block@closing = }
%   2/ blok:hang  --> \setbox\block@box=\hbox{\vtop{\kern\z@\hbox{
%           and \block@closing = }}}
% closing
%   1/ blok:stop  --> \block@closing \box\block@box
%   2/ stickout:left/right --> \block@closing \l/rlap{\box\block@box}
%   3/ fillupto:foo --> \block@closing 
%                           \hbox to \foo{\unhbox\block@box\hfil}
%
\newbox\block@box \newif\ifin@box
\add@generic@default{\in@boxno
    \def\block@closing{\ifin@box\else
        \Wmessage{Closing `block', but not opened. This is serious!}\fi}
    }
\def\block@closing{}
\newdimen\BlockWidth
\def\Measure@Block{\global\BlockWidth\wd\block@box}
\@GenericOption{block}{
    \switch {\if\EqualString{#1}}
        {stop}  {\stop@block}
        {hang}  {\start@block
                    {\vtop\bgroup\kern\z@\hbox\bgroup}
                    {\egroup\egroup}}
        {start} {\start@block{}{}}
        {default} {\Wmessage{Strange option: `block:#1'}}
    \endswitch}
\def\start@block#1#2{
    \ifin@line\else\protect@breakpoint\fi
    \@add@toks{\setbox\block@box=\hbox\bgroup#1}
    \b@group[block]
    \ifin@line\else\aftergroup\at@breakpointyes\fi
    \def\block@closing{\@add@toks{#2\egroup\Measure@Block}}
    }
\def\stop@block{\block@closing
    \ifin@box \@add@toks{\advance\hsize by -\wd\block@box} \fi
    \@add@toks{\box\block@box}
    \e@group[block]}

\@GenericOption{fillupto}{
    \block@closing \e@group[block]
    \edef\cs@e{\nxp\@add@toks{\hbox to \@w@w{#1}{\unhbox\block@box\hfil}}}
    \cs@e
    \ifin@line \edef\cs@e{\nxp\@add@toks{\advance\hsize by -\@w@w{#1}}}
               \cs@e \fi
    }
\@GenericOption{stickout}{
   \block@closing \e@group[block]
   \switch {\if\EqualString{#1}}
      {right} {\stickout@right{#2}}
      {left}  {\stickout@left{#2}}
      {default} {\Wmessage{Unknown qualifier (stickout:) `#1'}}
    \endswitch
   }
\def\stickout@left#1{
    \@add@toks{\llap}
    \if\IsEmptyList{#1}
          \@add@toks{{\box\block@box}}
    \else \edef\cs@e{\nxp\@add@toks{{\hbox to \@w@w{#1}
                     {\box\block@box\hss}}}}
          \cs@e
    \fi}
\def\stickout@right#1{
    \@add@toks{\rlap}
    \if\IsEmptyList{#1}
          \@add@toks{{\box\block@box}}
    \else \edef\cs@e{\nxp\@add@toks{{\hbox to \@w@w{#1}
                     {\hss\box\block@box}}}}
          \cs@e
    \fi}

%%%%%%%%%%%%%%%% Tidbits

% Commands
%
\@GenericOption{command}{
  \ifin@external \external@handle@expandable{#1}{\csn#1\ecs}{<#1>}
  \else
    \if\UndefinedCS{#1}\Wmessage{Undefined command: <#1>}\fi
    \edef\cs@e{\nxp\@add@toks{\CSname{#1}}}\cs@e
  \fi}
\@GenericOption{CSname}{
  \edef\cs@e{\nxp\@add@toks{\CSname{#1}}}\cs@e}

% Literal text.
% only problem if this is a colon.
%
\@GenericOption{literal}{\@add@toks{#1}}

% Arguments.
%
\@GenericOption{arg}{\edef\cs@e
   {\nxp\@add@toks{\iftrue{\else}\fi#1\iffalse{\else}\fi}}
   \cs@e}
\@GenericOption{OpenBrace}{\edef\cs@e
   {\nxp\@add@toks{\iftrue{\else}\fi}}
   \cs@e}
\@GenericOption{CloseBrace}{\edef\cs@e
   {\nxp\@add@toks{\iffalse{\else}\fi}}
   \cs@e}
% Extra Commands before and after
%
\newtoks\before@toks \add@generic@default{\before@toks{}}
\@GenericOption{commandbefore}{
    \edef\cs@e{\nxp\append@to@list{before@toks}{\CSname{#1}}}
    \cs@e}
\newtoks\after@toks  \add@generic@default{\after@toks{}}
\def\add@after@command#1{\append@to@list{after@toks}{#1}}
\@GenericOption{commandafter}{
    \edef\cs@e{\nxp\add@after@command{\CSname{#1}}}
    \cs@e}

\endinput

% 2016/04/13 Chnaged \lolli@name to \lollipop@name
% 2014/04/18 Fixed the wrong numbering of nested enumerate lists
% 92/11/03 Improved penalty handling for embedded environments
%     \ifini test extended to make loading on top of plain possible
% 92/11/08 Breakpoint protection into line/block/textcolumn
% 92/11/14 Resetlist generation made conditional
% 92/11/21 option 'arg'
% 92/12/12 \sTitelize, \give@title
% 93/07/30 late@after@toks, inputlineno in group errmsg