summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/context/base/strc-not.mkiv
blob: bf815c7281fb7d25c877b96c7791a15984d2e823 (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
%D \module
%D   [       file=strc-not,
%D        version=2008.10.20,
%D          title=\CONTEXT\ Structure Macros,
%D       subtitle=Note Handling,
%D         author=Hans Hagen,
%D           date=\currentdate,
%D      copyright=PRAGMA-ADE / Hans Hagen]
%C
%C This module is part of the \CONTEXT\ macro||package and is
%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
%C details.

\writestatus{loading}{ConTeXt Structure Macros / Note Handling}

\registerctxluafile{strc-not}{1.001}

\unprotect

% this needs a further cleanup ...
%
% -- set breakpoint in descriptions
% -- reset after trialtypesetting
% -- that way we can trick the symbol space

% removed:
%
% \pushsomestates
%
% core-ins -> obsolete
%
% saveinsertiondata
% restoreinsertiondata
% saveinsertionbox
% eraseinsertionbackup
% restoreinsertionbackup

\def\savenotedata      {} % \writestatus{todo}{save    note data}}
\def\restorenotedata   {} % \writestatus{todo}{restore note data}}
\def\savenotecontent   {} % \writestatus{todo}{save    note content}}
\def\restorenotecontent{} % \writestatus{todo}{restore note content}}
\def\erasenotebackup   {} % \writestatus{todo}{erase   note backup}}

% page-set:

\def\enablenotes    {\writestatus{todo}{enable  notes}}
\def\disablenotes   {\writestatus{todo}{disable notes}}
\def\savenotes      {\writestatus{todo}{save    notes}}
\def\flushsavednotes{\writestatus{todo}{flush   notes}}

% experiment: (compare scope=text and scope=page)
%
% \definenote[mynote][way=bytext,location=text,width=\leftmarginwidth,scope=page,rule=,before=,after=,factor=0]
% \setuptexttexts[margin][\vbox to \textheight{\placenotes[mynote]\vfill}][]

%D Footnotes are can be characterized by three components:
%D
%D \startitemize[packed]
%D \item a small number \footnote {a footnote number} or
%D      symbol {\setupfootnotes [conversion=set 2]\footnote
%D      {a footnote}}
%D \item and a similar mark at the bottom of the page
%D \item followed by some additional text
%D \stopitemize
%D
%D Because footnotes are declared at the location of their
%D reference they can be seen as a special kind of
%D floating bodies. Their placement is postponed but has to be
%D taken into account in the pagebreak calculations. This kind
%D of calculations are forced by using \type{\insert}s and dealing
%D with all cases is not trivial.

%D \macros
%D   {notesenabled}
%D
%D We need a couple of states because at some moments we don't want
%D to mess around with inserts at all. Take for instance a table
%D of contents. And so we can temporary disable footnotes by saying
%D
%D \starttyping
%D \notesenabledfalse
%D \stoptyping

\newif\ifnotesenabled \notesenabledtrue

% better mark a note .. once flushed no more flushing

%appendtoks \notesenabledfalse \to \everymarking
\appendtoks \notesenabledfalse \to \everypagebody
\appendtoks \notesenabledfalse \to \everystructurelist % quick hack

%D Often we need to process the whole set of notes and to make that
%D fast, we use a token register:

\newtoks\tobeprocessednotes

\unexpanded\def\processnotes#1% #1: \macro that uses \currentnote
  {\def\doprocesssomenote##1{\edef\currentnote{##1}\let\currentdescription\currentnote#1}%
   \the\tobeprocessednotes}

%D Notes have their own paremater handlers. The complication here
%D is that we use descriptions to typeset the note, so we have several
%D resolvers.

\let\currentnote\v!footnote

\def\noteparameter    #1{\csname\donoteparameter{\??vn\currentnote}#1\endcsname}
\def\noteparameterhash#1{\donoteparameterhash   {\??vn\currentnote}#1}

\def\namednoteparameter#1#2{\csname\donoteparameter{\??vn#1}#2\endcsname}

\def\donoteparameter    #1#2{\ifcsname#1#2\endcsname#1#2\else\expandafter\donoteparentparameter    \csname#1\s!parent\endcsname#2\fi}
\def\donoteparameterhash#1#2{\ifcsname#1#2\endcsname  #1\else\expandafter\donoteparentparameterhash\csname#1\s!parent\endcsname#2\fi}

\def\donoteparentparameter    #1#2{\ifx#1\relax\s!empty\else\donoteparameter    #1#2\fi}
\def\donoteparentparameterhash#1#2{\ifx#1\relax        \else\donoteparameterhash#1#2\fi}

\def\detokenizednoteparameter#1{\detokenize\expandafter\expandafter\expandafter{\csname\??vn#1\endcsname}}

\def\dosetnoteattributes#1#2% style color
  {\edef\fontattributehash {\noteparameterhash#1}%
   \edef\colorattributehash{\noteparameterhash#2}%
   \ifx\fontattributehash \empty\else\dosetfontattribute \fontattributehash #1\fi
   \ifx\colorattributehash\empty\else\dosetcolorattribute\colorattributehash#2\fi}

%D \macros
%D   {setupnote,setupnotedefinition}
%D
%D We can influence footnote typesetting with the setup
%D command:
%D
%D \showsetup{setupnotes}
%D \showsetup{setupnote}
%D
%D The definition command indicate that we can frame the footnote
%D area. The footnotes themselves are treated as descriptions.
%D
%D \showsetup{definenote}
%D
%D It's sort of a custom to precede footnotes by a horizontal
%D rule and although fancy rules like
%D
%D \starttyping
%D \hbox to 10em{\hskip-3em\dotfill}
%D \stoptyping
%D
%D Are quite ligitimate, we default to a simple one 20\% of the
%D text width.

\unexpanded\def\setupnotes
  {\dodoubleargument\getparameters[\??vn]}

\setupnotes
  [\c!location=\v!page,
   \c!way=\v!by\v!part,
  %\c!conversion=,
   \c!rule=\v!on,
   \c!before=\blank,
   \c!bodyfont=\v!small,
  %\c!style=,
  %\c!color=,
  %\c!after=,
  %\c!rulecolor=,
   \c!rulethickness=\linewidth,
   \c!frame=\v!off,
   \c!margindistance=.5em,
   \c!columndistance=1em,
   \c!distance=.125em,
   \c!align=\v!normal,
   \c!tolerance=\v!tolerant,
   \c!split=\v!tolerant,
  %\c!width=\makeupwidth,
  %\c!width=\ifdim\hsize<\makeupwidth\hsize\else\makeupwidth\fi,
   \c!width=\defaultnotewidth,
   \c!height=\textheight,
   \c!numbercommand=\high,
   \c!command=, % \noteparameter\c!numbercommand, % (command in enumeration) too messy, called twice
   \c!separator=,% \@@koseparator,
   \c!textcommand=\high,
   \c!textstyle=\tx,
  %\c!textcolor=,
   \c!interaction=\v!yes,
  %\c!factor=,
  %\c!scope=, % \v!text \v!page
   \c!prefixconnector=.,
  %\c!next=\autoinsertnextspace,
   \c!prefix=\v!no,
  %\c!continue=\v!no,
   \c!paragraph=\v!no,
   \c!inbetween=\hskip1em,
   \c!n=1]

\setupnotes
  [\c!expansion=\v!no,
   \c!xmlsetup=,
   \s!catcodes=,
   \c!saveinlist=\v!yes]

\def\@@defaultnotedefloc{\v!inleft}
\def\@@defaultnotedefdis{\!!zeropoint}

\unexpanded\def\startnotedef{\resetdescriptions\csname\e!start\??vn\??vn\currentnote\endcsname}
\unexpanded\def\stopnotedef                   {\csname\e!stop \??vn\??vn\currentnote\endcsname}

\def\currentnoteins{\csname\??vn:\currentnote\endcsname}

\newtoks \everysetupnote

\unexpanded\def\definenote
  {\dotripleempty\dodefinenote}

\def\dodefinenote[#1][#2][#3]% note [parent] [settings]
  {\edef\currentnote{#1}%
   \ifcsname\??vn:\currentnote\endcsname\else
     \@EA\installinsertion\csname\??vn:\currentnote\endcsname\relax
     \appendtoks\doprocesssomenote{#1}\to\tobeprocessednotes
   \fi
   \ifsecondargument
     \doifassignmentelse{#2}{\dodefinenotewithsettings{#2}}{\dodefinenotewithparent{#2}{#3}}%
   \else
     \dodefinenotesimple
   \fi
   \ctxlua{structures.notes.define("\currentnote","insert",\number\csname\??vn:\currentnote\endcsname)}%
   \the\everysetupnote
   \dochecknote}

\def\dodefinenotesimple
  {\defineenumeration
     [\currentnote]
     [\s!handler=\v!note,
      \c!text=,
      \c!location=\@@defaultnotedefloc,
      \c!distance=\@@defaultnotedefdis,
      \c!width=\v!fit,
      \c!headstyle=\noteparameter\c!style,
      \c!headcolor=\noteparameter\c!color,
      \c!before=,
      \c!after=]%
   \doredefinenotecommands
   \presetlocalframed
     [\??vn\currentnote]%
   \setupenumerations
     [\currentnote]
     [\s!parent=\??vn\currentnote,
      \c!number=\v!yes]% no inheritance from decriptions which is okay
   \getparameters
     [\??vn\currentnote]
     [\s!parent=\??vn]}

\def\dodefinenotewithsettings#1%
  {\defineenumeration % description
     [\currentnote]
     [\s!handler=\v!note,
      \c!text=,
      \c!location=\@@defaultnotedefloc,
      \c!distance=\@@defaultnotedefdis,
      \c!width=\v!fit,
      \c!headstyle=\noteparameter\c!style,
      \c!headcolor=\noteparameter\c!color,
      \c!before=,
      \c!after=]%
   \doredefinenotecommands
   \presetlocalframed
     [\??vn\currentnote]%
   \setupenumerations
     [\currentnote]
     [\s!parent=\??vn\currentnote,
      \c!number=\v!yes]%
   \getparameters
     [\??vn\currentnote]
     [\s!parent=\??vn,#1]}

\def\dodefinenotewithparent#1#2%
  {%setevalue{\??vn:#2:kids}{\ifcsname\??vn:#2:kids\endcsname\csname\??vn:#2:kids\endcsname,\fi#1}%
   \defineenumeration
     [\currentnote]
     [\s!handler=\v!note,
      \c!text=]%
   \doredefinenotecommands
   \presetlocalframed
     [\??vn\currentnote]%
   \setupenumerations
     [\currentnote]
     [\s!parent=\??dd#1,
      \c!number=\v!yes]%
   \getparameters
     [\??vn\currentnote]
     [\s!parent=\??vn#1,#2]}

% \starttext
%     text \startfootnote Test.\stopfootnote
%     test \footnote{xxxx} \subfootnote{xxxx}
%     test \footnote{xxxx} \subfootnote{xxxx}
% \stoptext

\def\dodoredefinenotecommands#1#2#3%
  {\unexpanded\expandafter\def\csname\e!start#3#1\expandafter\endcsname\expandafter
     {\expandafter\dosingleempty\csname\s!do\e!start#3#1\endcsname}%
   \unexpanded\expandafter\def\csname\s!do\e!start#3#1\expandafter\endcsname
        \expandafter[\expandafter##\expandafter1\expandafter]\expandafter##\expandafter2\csname\e!stop#3#1\endcsname
     {\begingroup
      \doenumerationinit{#1}{#2}{#3}%
      \@@notemakedescription[##1]{}{##2}%
      \expandafter\endgroup\noteparameter\c!next}} % WS: added \noteparameter\c!next

\def\doredefinenotecommands
  {\normalexpanded{\noexpand\dodoredefinenotecommands{\currentnote}{1}{}}%
   \let\@@subslevel\empty
   \dostepwiserecurse{2}{\descriptionparameter\c!levels}{1}
     {\normalexpanded{\noexpand\dodoredefinenotecommands{\currentnote}{\recurselevel}{\@@subslevel\v!sub}}%
      \edef\@@subslevel{\@@subslevel\v!sub}}}

\let\setupnotedefinition\setupenumerations

% \def\dodowithcurrentnoteandkids#1#2% action kid
%   {\pushmacro\currentnote
%    \def\currentnote{#2}%
%    \dowithcurrentnoteandkids{#1}% recursive
%    \popmacro\currentnote}%

\appendtoks
    \setupenumerations[\currentnote][]%
\to \everysetupnote

\unexpanded\def\setupnote
  {\dodoubleempty\dosetupnote}

\def\dosetupnote[#1][#2]%
  {\edef\currentnote{#1}%
   \ifsecondargument
     \getparameters[\??vn\currentnote][#2]%
     \the\everysetupnote
   \fi
   \dochecknote}

\appendtoks
  \setvalue{\??vn\c!rule:c:\currentnote}{\normalnoterule}% hm
  \letvalue{\??vn\c!rule:a:\currentnote}\v!left
\to \everysetupnote

\appendtoks
  \expanded{\processallactionsinset
    [\noteparameter\c!rule]}
    [     \v!on=>\setvalue{\??vn\c!rule:c:\currentnote}{\normalnoterule}, % no let as it can be changed afterwards
      \v!normal=>\setvalue{\??vn\c!rule:c:\currentnote}{\normalnoterule},
        \v!left=>\setvalue{\??vn\c!rule:a:\currentnote}{l2r},
       \v!right=>\setvalue{\??vn\c!rule:a:\currentnote}{r2l},
         \v!off=>\letvalue{\??vn\c!rule:c:\currentnote}\relax,
     \s!default=>\letvalue{\??vn\c!rule:c:\currentnote}\relax,
     \s!unknown=>\setvalue{\??vn\c!rule:c:\currentnote}{\noteparameter\c!rule}]%
\to \everysetupnote

\appendtoks
  \processaction % todo
    [\noteparameter\c!split]
    [  \v!tolerant=>\notepenalty\zeropoint,
         \v!strict=>\notepenalty9999,
     \v!verystrict=>\notepenalty\maxdimen,
        \s!default=>\notepenalty\zeropoint,
        \s!unknown=>\notepenalty\commalistelement]%
\to \everysetupnote

%D The following switch can be used to disable limiting the
%D height of the footnote area, something that is needed in
%D multi column balancing. Use this switch with care.

\newif\ifnotelimit \notelimittrue % shared

% bottomnotes endnotes
% clevernotes

\appendtoks
  \doifsomething{\noteparameter\c!factor}
    {\ifnum\noteparameter\c!factor<\zerocount\else
       \count\currentnoteins\noteparameter\c!factor
     \fi}%
\to \everysetupnote

% compatibility (will go away)

\newif\ifbottomnotes % still used in page-one.mkiv
%newif\endnotes      % no longer used

% locations:

\def\s!noteloc{nodeloc} % 1=page 2=columns 3=lastcolumn 4=firstcolumn 5=none
\def\s!notepos{nodepos} % 0=nothing 1=high 2=bottom
\def\s!notefmt{nodefmt} % 1 text
\def\s!notecol{nodecol}

\def\clevernotes % compatibility hack, will be redone
  {\numexpr\ifcase\namednoteparameter\v!footnote\s!noteloc\or0\or2\or2\or1\else0\fi\relax}

\def\setnotelocation  #1{\expandafter\let\csname\??vn\currentnote\s!noteloc\endcsname#1\relax}
\def\setnoteposition  #1{\expandafter\let\csname\??vn\currentnote\s!notepos\endcsname#1\relax}
\def\setnoteformatting#1{\expandafter\let\csname\??vn\currentnote\s!notefmt\endcsname#1\relax}

\def\currentnofcolumns{\@@kln}

\setvalue{\??vn @\v!page       }{\setnotelocation\plusone}
\setvalue{\??vn @\v!columns    }{\setnotelocation\plustwo}
\setvalue{\??vn @\v!lastcolumn }{\setnotelocation\plusthree}
\setvalue{\??vn @\v!firstcolumn}{\setnotelocation\plusfour}
\setvalue{\??vn @\v!none       }{\setnotelocation\plusfive}
\setvalue{\??vn @\v!text       }{\setnotelocation\plusfive \setnoteformatting\plusone} % test
\setvalue{\??vn @\v!high       }{\setnoteposition\plusone}
\setvalue{\??vn @\v!bottom     }{\setnoteposition\plustwo}

\def\dosetcheckednote#1{\csname\??vn @#1\endcsname}

\newtoks\everychecknote

\def\dochecknote
  {% node states
   \setnotelocation\plusone % page
   \setnoteposition\plustwo % high
   \normalexpanded{\rawprocesscommalist[\noteparameter\c!location]}\dosetcheckednote
   % compatibility hack
   %ifnum\noteparameter\s!noteloc=\plusfive \endnotestrue    \else \endnotesfalse    \fi
   \ifnum\noteparameter\s!notepos=\plustwo  \bottomnotestrue \else \bottomnotesfalse \fi
   % set column multiplier
   \edef\currentnotenofcolumns{\noteparameter\c!n}%
   \ifx\currentnotenofcolumns\empty
     \let\currentnotenofcolumns\!!plusone
   \fi
   \ifcase\noteparameter\s!noteloc
     % can't happen
     \scratchcounter\plusone
   \or
     % page
     \scratchcounter \currentnotenofcolumns
   \or
     % columns
     \scratchcounter\ifnum\currentnofcolumns=\zerocount \plusone \else \currentnotenofcolumns \fi \relax
   \or
     % firstcolumn
     \scratchcounter\plusone
   \or
     % lastcolumn
     \scratchcounter\plusone
   \or
     % text
     \scratchcounter\currentnotenofcolumns
   \fi
   % column factor
   \ifcase\scratchcounter
     \scratchcounter\plusone
   \fi
   \global\count\currentnoteins\plusthousand
   \global\count\currentnoteins\numexpr\plusthousand/\scratchcounter\relax
   % maximize height
   \ifnotelimit
     \global\dimen\currentnoteins\dimexpr\noteparameter\c!height*\scratchcounter\relax
   \fi
   % distance -> tricky as this might depend on a font switch so we need a fast checker
   \dosetnotedistance
   % play safe
   \ifnum\noteparameter\s!noteloc=\plusfive
     \ctxlua{structures.notes.setstate("\currentnote","store")}%
     % text notes (e.g. end notes) but we don't use inserts anyway
     \global\dimen\currentnoteins\maxdimen
     \global\count\currentnoteins\zerocount
     \global\skip \currentnoteins\zeropoint
   \fi
   \the\everychecknote}

\def\dosetnotedistance
  {\begingroup
   \setbox\scratchbox\vbox
     {\forgetall
%   \setnotebodyfont
      \dontcomplain
      \noteparameter\c!before
      \placenoterule
      \noteparameter\c!after}%
   \global\skip\currentnoteins\ht\scratchbox
   \endgroup}

% \def\checknotes % no longer needed
%   {\processnotes\dochecknote}
%
% \def\checknotedistances
%   {\processnotes\dosetnotedistance}
%
% fails but not that much needed anyway:
%
% \appendtoks
%     \checknotedistances
% \to \everyglobalbodyfont

% D When \type{n} exceeds~1, footnotes are typeset in
% D multi||columns, using the algoritm presented on page~397
% D of \TEX book. Footnotes can be places on a per page basis
% D or whereever suitable. When we set~\type{n} to~0, we get a
% D rearanged paragraph, typeset by the algoritms on pages 398
% D and~389 (at least in \MKII). We definitely did not reinvent
% D that wheel.

% Example of using factor:
%
% \definenote[mynote][way=bypage,location=text,width=\marginwidth,rule=,before=,factor=0]
% \setuplayout[backspace=5cm,margin=3cm,margindistance=.5cm,width=middle]
% \setuptexttexts[margin][\vbox to \textheight{\placenotes[mynote]\vfill}][]
% \starttext
% \dorecurse{10}{test \mynote{one one one one one one} \input zapf \mynote{one one one one one one} }
% \stoptext

%D The noterule can be a graphic and therefore calling this
%D setup macro at every skipswitch is tricky (many many MP
%D runs). Let's just reserve a few points, that probably match
%D those of the stretch component.

%D A bit messy:

\unexpanded\def\placenoterule
  {\bgroup
   \setupalign[\getvalue{\??vn\c!rule:a:\currentnote}]%
   \righttoleft
   \getvalue{\??vn\c!rule:c:\currentnote}%
   \par
   \egroup}

\unexpanded\def\normalnoterule
  {\ifvmode
     \dontleavehmode \blackrule
       [ \c!color=\noteparameter\c!rulecolor,
         \c!width=.2\hsize,
        \c!height=\noteparameter\c!rulethickness,
        \c!depth=\zeropoint]%
     \endgraf
     \kern\strutdepth
   \fi}

\ifx\setnotehsize\undefined

  \unexpanded\def\setnotehsize{\hsize\noteparameter\c!width\relax} % can be overloaded

\fi

%D The formatting depends on the width of the table, so we
%D have to set \type {n} to zero.
%D
%D \starttyping
%D \startbuffer
%D \bTABLE
%D \bTR \bTD one \footnote{\dorecurse{10}{abcd }} \eTD \bTD two \eTD \eTR
%D \bTR \bTD three fout five six seven eight nine \eTD \bTD ten \eTD \eTR
%D \eTABLE
%D \stopbuffer
%D
%D \startlocalfootnotes[n=0,location={text,none}]
%D \placelegend[n=2]{\getbuffer}{\placelocalfootnotes}
%D \stoplocalfootnotes
%D \stoptyping

%D \macros
%D   {footnote}
%D
%D A footnote can have a reference as optional argument and
%D therefore its formal specification looks like:
%D
%D \showsetup{footnote}
%D
%D This command has one optional command: the reference. By
%D saying \type{[-]} the number is omitted. The footnote
%D command is not that sensitive to spacing, so it's quite
%D legal to say:
%D
%D \startbuffer
%D Users of \CONTEXT\ must keep both feet \footnote{Given they
%D have two.} on the ground and not get confused \footnote{Or
%D even crazy.} by all those obscure \footnote{But fortunately
%D readable.} parameters.
%D \stopbuffer
%D
%D \typebuffer
%D
%D When setting the \type{conversion} to \type{set 2} we get
%D something like:
%D
%D \bgroup
%D \startnarrower
%D \setupfootnotes[conversion=set 1]
%D \getbuffer
%D \stopnarrower
%D \egroup
%D
%D Typesetting footnotes is, at least for the moment, disabled
%D when reshaping boxes.
%D
%D The additional macro \type {\footnotetext} and the
%D associated \type {\note} macro were implemented at
%D request of users on the mailing list and a suggestion by
%D taco to split of the symbol placement. I decided to
%D merge this functionality with the existing \type {\note}
%D functionality.

%D The next implementation runs on top of enumerations (only in \MKIV).
%D
%D \starttyping
%D \setupenumerations
%D   [footnote]
%D   [     style=\type{(es)},
%D     headstyle=\type{(hs)}]
%D
%D \setupnote
%D   [footnote]
%D   [        style=\type{(s)},
%D          command=\type{(c)},
%D      textcommand=\type{(tc)},
%D        textstyle=\type{(ts)},
%D      numberstyle=\type{(ns)},
%D    numbercommand=\type{(nc)}]
%D
%D \setuplayout[backspace=6cm,marginwidth=cm,width=middle]
%D
%D \starttext
%D     \dorecurse{9}{This\footnote{Hello World #1} is a test.\par }
%D \stoptext
%D \stoptyping

% TODO: \ifnotesenabled

\newif\ifnotesymbol \notesymboltrue

\newconditional\skipnoteplacement

\unexpanded\def\setnote    [#1]{\getvalue{#1}}
\unexpanded\def\setnotetext[#1]{\global\settrue\skipnoteplacement\getvalue{#1}}

\def\domovednote#1#2#3#4%
  {\ifcase\ctxlua{structures.notes.deltapage("#1",#2)}\or\symbol[#3]\or\symbol[#4]\fi}

\setvalue{\??dd:\v!note:\s!handler:\s!text  }{\@@donotetext}
\setvalue{\??dd:\v!note:\s!handler:\s!number}{\@@donotenumber}
\setvalue{\??dd:\v!note:\s!handler          }{\@@donotehandler}
\setvalue{\??dd:\v!note:\s!handler:\s!do    }{\@@somenotedescription}
\setvalue{\??dd:\v!note:\s!handler:\s!start }{\@@startsomenotedescription}

\let\@@donotehandler\@@dodescriptionhandler

\def\@@somenotedescription     {\@@notemakedescription}
\def\@@startsomenotedescription{\@@notemakedescription}

\def\@@notemakedescription[#1]#2#3%
  {\ifnotesenabled
     \edef\currentdescriptionreference{#1}%
     \iftrialtypesetting
        \doenumerationcheckconditions
        \let\currentnote\currentdescriptionmain
        \typesetdummynotesymbol
     \else
       \begingroup
       \doenumerationcheckconditions
       \let\currentnote\currentdescriptionmain
       \dodescriptioncomponent[\c!reference=#1,\c!label={\descriptionparameter\c!text},\c!title={#3},\c!list=,\c!bookmark=,][]%
       \xdef\currentnotenumber{\ctxlua{structures.notes.store("\currentnote",\currentdescriptionnumberentry)}}%
       \settrue\processingnote
       \ifconditional\skipnoteplacement
         \globallet\lastnotesymbol\dolastnotesymbol
       \else
         \iftypesettinglines % otherwise problems with \type <crlf> {xxx}
           \ignorelines % makes footnotes work in \startlines ... \stoplines
         \fi
         \ifnotesymbol
           \dolastnotesymbol
         \else
           \unskip\unskip
           \globallet\lastnotesymbol\dolastnotesymbol
         \fi
       \fi
       \ifconditional\postponingnotes % todo: per note class
         \global\settrue\postponednote
       \else\ifconditional\inlocalnotes % todo: per note class
         \global\settrue\postponednote
       \else
         \handlenoteinsert\currentnote\currentnotenumber
       \fi\fi
       \endgroup
     \fi
   \fi
   \ifconditional\skipnoteplacement
     \global\setfalse\skipnoteplacement
   \else
     \kern\notesignal\relax % \relax is needed to honor spaces
   \fi}

\def\dolastnotesymbol
  {\typesetsomenotesymbol\currentnote\currentnotenumber\currentdescriptionnumberentry}

\let\dotagsetnotesymbol\relax

\def\dotypesetsomenotesymbol#1#2#3% running text (messy: #1 and current mixed)
  {\dodonotesymbol
     {\synchronizesomenotesymbol{#1}{#2}{#3}%
%     \ctxlua{structures.notes.number("\currentnote",\currentnotenumber)}% \currentdescriptionnumberentry
      \dostarttagged\t!descriptionsymbol\currentnote
      \dotagsetnotesymbol
      \ctxlua{structures.notes.number("#1",#2)}%
      \dostoptagged
      \domovednote{#1}{#2}\v!previouspage\v!nextpage}}

\unexpanded\def\typesetsomenotesymbol#1#2#3% running text
  {\removeunwantedspaces
   \doifitalicelse\/\donothing % Charles IV \footnote{the fourth}
   \ifdim\lastkern=\notesignal
     \dodonotesymbol{\kern\noteparameter\c!distance}% gets the font right, hack !
   \fi
   \nobreak
   \doifelse{\noteparameter\c!interaction}\v!no
     {\dotypesetsomenotesymbol{#1}{#2}{#3}}
     {\directgotobox{\dotypesetsomenotesymbol{#1}{#2}{#3}}[page(\ctxlua{structures.notes.getnumberpage("#1",\number#2)})]}% f:
   \globallet\lastnotesymbol\relax}

\unexpanded\def\typesetdummynotesymbol % temp hack
  {\removeunwantedspaces
   \doifitalicelse\/\donothing % Charles IV \footnote{the fourth}
   \ifdim\lastkern=\notesignal
     \dodonotesymbol{\kern\noteparameter\c!distance}% gets the font right, hack !
   \fi
   \nobreak
   \hbox to .5em{}%
   \globallet\lastnotesymbol\relax}

\def\currentnotedescriptiontext % todo: can be other number
  {\ctxlua{structures.notes.title("\currentnote",\currentdescriptionnumberentry)}}

\def\@@donotetext
  {\ifconditional\enumerationnumberenabled
     \iftrialtypesetting
       \doenumerationfullnumber\showdntext
       \doenumerationcouplingsymbol
     \else
       \doenumerationregistercoupling
       \doenumerationfullnumber\showdntext
       \doenumerationcouplingsymbol
     \fi
   \else
     \doenumerationfullnumber\showdnpuretext
   \fi}

% \def\currentnoteenumerationfullnumber
\def\@@donotenumber
  {\doifelse{\noteparameter\c!interaction}\v!no
     {\docurrentnoteenumerationfullnumber}%
     {\directgotobox
        {\docurrentnoteenumerationfullnumber}%
        [page(\ctxlua{structures.notes.getsymbolpage("\currentnote",\currentdescriptionnumberentry)})]}}

\def\docurrentnoteenumerationfullnumber
  {\noteparameter\c!numbercommand
     {\dosetnoteattributes\c!numberstyle\c!numbercolor
      \ctxlua{structures.notes.number("\currentnote",\currentdescriptionnumberentry)}%
      \domovednote\currentdescription\currentdescriptionnumberentry\v!nextpage\v!previouspage}}

\def\synchronizesomenotesymbol#1#2#3% called more often than needed
  {\iftrialtypesetting\else
     \normalexpanded{\noexpand\ctxlatelua{structures.notes.setsymbolpage("#1",#2,#3)}}%
   \fi}

\def\handlenoteinsert#1#2% tg, id
  {\begingroup
   \edef\currentnote{#1}%
   \the\everybeforenoteinsert
   \insert\currentnoteins\bgroup
     \the\everyinsidenoteinsert
     \doprocesslocalsetups{\noteparameter\c!setups}% experimental
     \doifelse{\noteparameter\c!paragraph}\v!yes
        {\nointerlineskip
         \startvboxtohbox
         \handlenoteitself{#1}{#2}%
         % add some slack
         \stopvboxtohbox}
        {\handlenoteitself{#1}{#2}}%
   \egroup
   \the\everyafternoteinsert
   \endgroup}

\def\betweennoteitself#1% tg
  {\edef\currentnote{#1}%
   \doif{\noteparameter\c!paragraph}\v!yes\dobetweenparagraphnotes}

\def\handlenoteitself#1#2% tg, id
  {\edef\currentdescription{#1}%
   \edef\currentnote{#1}%
   \edef\currentdescriptionnumberentry{#2}%
   \edef\currentdescriptionlistentry{\ctxlua{tex.write(structures.notes.listindex("#1",#2))}}%
   % as we can have collected notes (e.g. in tables) we need to recover
   % \currentdescriptionattribute and \currentdescriptionsynchronize
   \reinstatedescriptionnumberentry\currentdescriptionlistentry % we could store the number in the entry
   %
   \dontcomplain % should be done in startstoreddescription instead
   \dostartstoreddescription\begstrut\currentnotedescriptiontext\endstrut\dostopstoreddescription}

\def\dostartstoreddescription
  {\bgroup\@@dostartdescriptionindeed}

\def\dostopstoreddescription
  {\@@stopdescription}

%D The main typesetting routine is more or less the same as the
%D \PLAIN\ \TEX\ one, except that we only handle one type while
%D \PLAIN\ also has something \type{\v...}. In most cases
%D footnotes can be handled by a straight insert, but we do so
%D by using an indirect call to the \type{\insert} primitive.

%D Making footnote numbers active is not always that logical,
%D especially when we keep the reference and text at one page.
%D On the other hand we need interactivity when we refer to
%D previous notes or use end notes. Therefore we support
%D interactive footnote numbers in two ways \footnote{This
%D feature was implemented years after we were able to do so,
%D mainly because endnotes had to be supported.} that is,
%D automatically (vise versa) and by user supplied reference.

\let\startpushnote\relax
\let\stoppushnote \relax

\newsignal\notesignal
\newcount \notepenalty

\notepenalty=0 % needed in order to split in otrset

\newconditional\processingnote
\newconditional\postponednote

\newtoks\everybeforenoteinsert
\newtoks\everyinsidenoteinsert
\newtoks\everyafternoteinsert

\appendtoks
   \let\flushnotes\relax
   \let\postponenotes\relax
   \forgetall
   \resetallattributes % new, we don't want color bleed into notes
\to \everybeforenoteinsert

\appendtoks
  \doif{\noteparameter\c!scope}\v!page{\floatingpenalty\maxdimen}% experiment
  \penalty\notepenalty
  \forgetall
  \setnotebodyfont
  \redoconvertfont % to undo \undo calls in in headings etc
  \splittopskip\strutht  % not actually needed here
  \splitmaxdepth\strutdp % not actually needed here
  \leftmargindistance\noteparameter\c!margindistance
  \rightmargindistance\leftmargindistance
%   \ifnum\noteparameter\c!n=\zerocount % no ifcase new 31-07-99 ; always ?
%     \doifnotinset{\noteparameter\c!width}{\v!fit,\v!broad}\setnotehsize % ?
%   \fi
\to \everyinsidenoteinsert

% not: \appendtoks \setnotehsize \to \everyinsidenoteinsert (spoils columns)

\let\lastnotesymbol\relax

%D \macros
%D   {note}
%D
%D Refering to a note is accomplished by the rather short
%D command:
%D
%D \showsetup{note}
%D
%D This command is implemented rather straightforward as:

\unexpanded\def\notesymbol
  {\dodoubleempty\donotesymbol}

\def\donotesymbol[#1][#2]%
  {\bgroup
   \ifnotesenabled
     \edef\currentnote{#1}%
     \ifsecondargument
       \unskip
       \dodonotesymbol{\in[#2]}%
     \else
       \dodonotesymbol\lastnotesymbol
     \fi
   \fi
   \egroup}

\def\dodonotesymbol#1%
  {\noteparameter\c!textcommand{\dosetnoteattributes\c!textstyle\c!textcolor#1}}

%D Normally footnotes are saved as inserts that are called upon
%D as soon as the pagebody is constructed. The footnote
%D insertion routine looks just like the \PLAIN\ \TEX\ one,
%D except that we check for the end note state.

% testcase for split bottom alignment see (a) below
%
% \dorecurse{6}{\input tufte\footnote{\input ward \input tufte \relax}}

\unexpanded\def\placenoteinserts
  {\processnotes\doplacenoteinserts}

\unexpanded\def\unvboxed {\ifvmode\unvbox \else\box \fi}
\unexpanded\def\unvcopied{\ifvmode\unvcopy\else\copy\fi}

% idea: tag with attr and then just flush them again

\def\dobetweenparagraphnotes % should not be too much
  {\noteparameter\c!inbetween}

\def\doflushgobalnotes
  {\doifelse{\noteparameter\c!paragraph}\v!yes
     {\vbox
        {\beginofshapebox
         \iftrialtypesetting\unvcopied\else\unvboxed\fi\currentnoteins
         \endofshapebox
        %\doreshapebox{\box\shapebox}{}{}{}% get rid of penalties etc
         \gdef\dodobetweenparagraphnotes{\glet\dodobetweenparagraphnotes\dobetweenparagraphnotes}% shape works reverse
         \doreshapebox{\hbox{\unhbox\shapebox\dodobetweenparagraphnotes}}{}{}{}% get rid of penalties etc
         \innerflushshapebox
         \convertvboxtohbox}}
     {\iftrialtypesetting\unvcopied\else\unvboxed\fi\currentnoteins}}

\def\dodoplacenoteinserts
  {\ifcase\noteparameter\c!n\relax
     % should not happen
   \or
     \doglobalflushnotesnormal
   \else
     \doglobalflushnotescolums
   \fi}

\def\doglobalflushnotesnormal
  {%\iftrialtypesetting\copy\else\box\fi\currentnoteins
   \doflushgobalnotes
   \obeydepth} % (a) added , since split footnotes will not align properly

\def\doglobalflushnotescolums
  {\edef\currentnotewidth{\noteparameter\c!width}%
   \doifdimensionelse\currentnotewidth\donothing
     {\edef\currentnotewidth{\the\hsize}}%
   \startsimplecolumns[\c!distance=\noteparameter\c!columndistance,\c!n=\noteparameter\c!n,\c!width=\currentnotewidth]%
    %\iftrialtypesetting\unvcopied\else\unvboxed\fi\currentnoteins % compare with local
     \doflushgobalnotes
   \stopsimplecolumns}

\def\doplacenoteinserts
  {\relax\ifdim\ht\currentnoteins>\zeropoint\relax
     \ifnum\noteparameter\s!noteloc=\plusfive
     \else
       \endgraf
       \ifvmode
         \whitespace
         \noteparameter\c!before
       \fi
        %        \bgroup
        %        \setupalign[\noteparameter\c!align]%
       \placenoterule  % alleen in ..mode
        %        \par
        %        \egroup
       \bgroup
       \setnotebodyfont
       \setbox\scratchbox\hbox
         {\dodoplacenoteinserts}%
       \setbox\scratchbox\hbox
         {\localframed
            [\??vn\currentnote]
            [\c!width=\v!fit,
             \c!height=\v!fit,
             \c!strut=\v!no,
             \c!offset=\v!overlay]
            {\ifdim\dp\scratchbox=\zeropoint         % this hack is needed because \vadjust
               \hbox{\lower\strutdp\box\scratchbox}% % in margin number placement
             \else                                   % hides the (always) present depth
               \box\scratchbox
             \fi}}%
       \setbox\scratchbox\hbox{\lower\strutdepth\box\scratchbox}%
       \dp\scratchbox\strutdepth % so we know that it has the note bodyfont depth
       \box\scratchbox
       \egroup
       \endgraf
       \ifvmode
         \noteparameter\c!after
       \fi
     \fi
   \fi}

%D Supporting end notes is surprisingly easy. Even better, we
%D can combine this feature with solving the common \TEX\
%D problem of disappearing inserts when they're called for in
%D deeply nested boxes. The general case looks like:
%D
%D \starttyping
%D \postponenotes
%D \.box{whatever we want with footnotes}
%D \flushnotes
%D \stoptyping
%D
%D This alternative can be used in headings, captions, tables
%D etc. The latter one sometimes calls for notes local to
%D the table, which can be realized by saying
%D
%D \starttyping
%D \setlocalfootnotes
%D some kind of table with local footnotes
%D \placelocalfootnotes
%D \stoptyping
%D
%D Postponing is accomplished by simply redefining the (local)
%D insert operation. A not too robust method uses the
%D \type{\insert} primitive when possible. This method fails in
%D situations where it's not entirely clear in what mode \TEX\
%D is. Therefore the auto method can is to be overruled when
%D needed.

\newconditional\postponingnotes

% we need a proper state: normal, postponing, flushing

\def\postponenotes
  {\ifconditional\postponingnotes\else
     \global\settrue\postponingnotes
     \global\let\flushnotes\doflushnotes
     \ctxlua{structures.notes.postpone()}%
   \fi}

\let\flushnotes\relax

\def\doflushnotes
  {\ifconditional\postponingnotes
     \begingroup
     \let\flushnotes   \relax
     \let\postponenotes\relax
     \ctxlua{structures.notes.flushpostponed()}% this also resets the states !
     \global\setfalse\postponednote
     \global\setfalse\postponingnotes
     \global\let\flushnotes\relax
     \endgroup
   \fi}

%D \macros
%D   {startlocalfootnotes,placelocalfootnotes}
%D
%D The next two macros can be used in for instance tables, as
%D we'll demonstrate later on.
%D
%D \showsetup{startlocalfootnotes}
%D \showsetup{placelocalfootnotes}

% todo: compatibility mode: when first arg is assignment or missing, then all

\newtoks\everyplacelocalnotes

\appendtoks
    \let\flushnotes   \relax
    \let\postponenotes\relax
\to \everyplacelocalnotes

\def\defaultnotewidth{\makeupwidth} % {\ifdim\hsize<\makeupwidth\hsize\else\makeupwidth\fi}

\newconditional\inlocalnotes

\unexpanded\def\startlocalnotes
  {\dosingleempty\dostartlocalnotes}

\def\dostartlocalnotes[#1]%
  {\def\localnoteslist{#1}%
   \settrue\inlocalnotes
   \processcommacommand[\localnoteslist]\dodostartlocalnotes}

\unexpanded\def\stoplocalnotes
  {\processcommacommand[\localnoteslist]\dodostoplocalnotes
   \setfalse\inlocalnotes}

\def\dodostartlocalnotes#1%
  {\doifnot{\noteparameter\c!continue}\v!yes
     {\savestructurecounter[#1]%
      \resetstructurecounter[#1]}%
   \ctxlua{structures.notes.save("#1","store")}}

\def\dodostoplocalnotes#1%
  {\doifnot{\noteparameter\c!continue}\v!yes
     {\restorestructurecounter[#1]}%
   \ctxlua{structures.notes.restore("#1")}}

\unexpanded\def\placelocalnotes
  {\dodoubleempty\doplacelocalnotes}

\def\doplacelocalnotes[#1][#2]%
  {\doif{\ctxlua{structures.notes.getstate("#1")}}{store}{\dodoplacelocalnotes{#2}{#1}}}

\def\dodoplacelocalnotes#1#2% settings note
  {\begingroup
   \the\everyplacelocalnotes
   % beware, we cannot trust setting \currentnote here
   \getparameters[\??vn#2][\c!width=\v!fit,\c!height=\v!fit,\c!strut=\v!no,\c!offset=\v!overlay,#1]% we only need a selective one
   \dolocalnotealternative{#2}%
   \endgroup
   \dochecknote} % we need to restore the old state

%D These commands can be used like:
%D
%D \startbuffer
%D \startlocalnotes[width=.3\hsize,n=0]
%D   \placetable
%D     {Some Table}
%D     \placeontopofeachother
%D       {\starttable[|l|r|]
%D        \HL
%D        \VL Nota\footnote{Bene} \VL Bene\footnote{Nota} \VL\SR
%D        \VL Bene\footnote{Nota} \VL Nota\footnote{Bene} \VL\SR
%D        \HL
%D        \stoptable}
%D       {\placelocalnotes}
%D \stoplocalnotes
%D \stopbuffer
%D
%D \typebuffer
%D
%D Because this table placement macro expect box content, and
%D thanks to the grouping of the local footnotes, we don't need
%D additional braces.
%D
%D \getbuffer

%D \macros
%D   {placefootnotes}
%D
%D We still have no decent command for placing footnotes
%D somewhere else than at the bottom of the page (for which no
%D user action is needed). Footnotes (endnotes) can be
%D placed by using
%D
%D \showsetup{placefootnotes}

\unexpanded\def\placebottomnotes
  {\processnotes\placenoteinserts}

\unexpanded\def\placenotes
  {\dodoubleempty\doplacenotes}

\def\doplacenotes[#1][#2]%
  {\processcommalist[#1]{\dodoplacenotes{#2}}}

\def\dodoplacenotes#1#2% settings note
  {\edef\currentnote{#2}%
   \doifelse{\ctxlua{structures.notes.getstate("#2")}}{store}
     \dodoplacelocalnotes
     \dodoplaceglobalnotes
       {#1}{#2}}

\def\dodoplaceglobalnotes#1#2%
  {\begingroup
   \setupnote[#2][#1]%
   \doplacenoteinserts
   \endgroup
   \the\everysetupnote} % to be checkes

%D Placement

\long\def\installnotealternative#1#2%
  {\setvalue{\??vn:\c!alternative:#1}{#2}}

\def\doifnotescollected#1%
  {\ctxlua{structures.notes.doifcontent("#1")}}

\def\dolocalnotealternative#1%
  {\edef\currentnote{#1}%
   \doifnotescollected\currentnote
     {\endgraf
      \ifvmode
        \whitespace
        \noteparameter\c!before
      \fi
      \begingroup
      \setnotebodyfont
      \getvalue{\??vn:\c!alternative:\noteparameter\c!alternative}%
      \endgroup
      \ifvmode
        \noteparameter\c!after
      \fi}}

\setvalue{\??vn:\c!alternative:}{\getvalue{\??vn:\c!alternative:\v!none}}

%D A stupid alternative is also provided:
%D
%D \starttyping
%D \setupfootnotes[location=text,alternative=none]
%D \stoptyping

% \def\flushlocalnotes#1{\ctxlua{structures.notes.flush("#1","store")}}
\def\flushlocalnotes#1{\ctxlua{structures.notes.flush("#1","store","\noteparameter\c!criterium")}}

\installnotealternative \v!none
  {\flushlocalnotes\currentnote}

\installnotealternative \v!grid % test if n > 0
  {\snaptogrid\hbox
    {\localframed
       [\??vn\currentnote]
       {\flushlocalnotes\currentnote}}}

\installnotealternative \v!fixed % test if n > 0
  {\localframed
     [\??vn\currentnote]
     {\flushlocalnotes\currentnote}}

\installnotealternative \v!columns % redundant
  {\localframed
     [\??vn\currentnote]
     {\edef\currentnotewidth{\noteparameter\c!width}%
      \doifdimensionelse\currentnotewidth\donothing
        {\edef\currentnotewidth{\the\hsize}}%
%       \setupinmargin[\c!align=\v!left]%
      \startsimplecolumns[\c!distance=\noteparameter\c!columndistance,\c!n=\noteparameter\c!n,\c!width=\currentnotewidth]%
        \flushlocalnotes\currentnote
      \stopsimplecolumns}}

%D \macros
%D   {fakenotes}

    % is this ok? endnotes and such

    \unexpanded\def\fakenotes
      {\ifhmode\endgraf\fi\ifvmode
         \calculatetotalclevernoteheight
         \ifdim\totalnoteheight>\zeropoint \kern\totalnoteheight \fi
       \fi}

    \unexpanded\def\fakepagenotes
      {\ifhmode\endgraf\fi\ifvmode
         \calculatetotalpagenoteheight
         \ifdim\totalnoteheight>\zeropoint \kern\totalnoteheight \fi
       \fi}

    \newdimen\totalnoteheight

    \def\doaddtototalnoteheight#1%
      {\ifdim\ht#1>\zeropoint
         \ifcase\count#1\else
           % todo: divide by count
           \advance\totalnoteheight\ht  #1%
           \advance\totalnoteheight\skip#1%
         \fi
       \fi}

    \def\docalculatetotalnoteheight
      {\ifcase\clevernotes % tricky here ! ! ! to be sorted out ! ! !
         \doaddtototalnoteheight\currentnoteins
       \else
%        \doaddtototalnoteheight\currentbackupnoteins
       \fi}

    \def\docalculatetotalclevernoteheight
      {\ifcase\clevernotes \else % tricky here ! ! ! to be sorted out ! ! !
         \doaddtototalnoteheight\currentnoteins
       \fi}

    \def\docalculatetotalpagenoteheight
      {\doaddtototalnoteheight\currentnoteins}

    \def\calculatetotalnoteheight      {\totalnoteheight\zeropoint\processnotes\docalculatetotalnoteheight}
    \def\calculatetotalclevernoteheight{\totalnoteheight\zeropoint\processnotes\docalculatetotalclevernoteheight}
    \def\calculatetotalpagenoteheight  {\totalnoteheight\zeropoint\processnotes\docalculatetotalpagenoteheight}

    \newif\ifnotespresent

    \def\dochecknotepresence
      {\ifdim\ht\currentnoteins>\zeropoint
         \notespresenttrue
       \fi}

    \def\checknotepresence
      {\notespresentfalse
       \processnotes\dochecknotepresence}

%D Now how can this mechanism be hooked into \CONTEXT\ without
%D explictly postponing footnotes? The solution turned out to
%D be rather simple:
%D
%D \starttyping
%D \everypar  {...\flushnotes...}
%D \neverypar {...\postponenotes}
%D \stoptyping
%D
%D and
%D
%D \starttyping
%D \def\ejectinsert%
%D   {...
%D    \flushnotes
%D    ...}
%D \stoptyping
%D
%D We can use \type{\neverypar} because in most commands
%D sensitive to footnote gobbling we disable \type{\everypar}
%D in favor for \type{\neverypar}. In fact, this footnote
%D implementation is the first to use this scheme.

%D This is a nasty and new secondary footnote flusher. It
%D can be hooked into \type {\everypar} like:
%D
%D \starttyping
%D \appendtoks \synchronizenotes \to \everypar
%D \stoptyping

    % \def\dosynchronizenotes
    %   {\ifvoid\currentnoteins\else\insert\currentnoteins{\unvbox\currentnoteins}\fi}
    %
    % \def\synchronizenotes
    %   {\processnotes\dosynchronizenotes}

\let\synchronizenotes\relax

%D When typesetting footnotes, we have to return to the
%D footnote specific bodyfont size, which is in most cases derived
%D from the global document bodyfont size. In the previous macros
%D we already used a footnote specific font setting macro.

\def\setnotebodyfont
  {\let\setnotebodyfont\relax
   \restoreglobalbodyfont
   \switchtobodyfont[\noteparameter\c!bodyfont]%
   \setuptolerance[\noteparameter\c!tolerance]%
   \setupalign[\noteparameter\c!align]}

%D The footnote mechanism defaults to a traditional one
%D column way of showing them. By default we precede them by
%D a small line.

\ifx\v!endnote\undefined \def\v!endnote{endnote} \fi

\definenote [\v!footnote]
\definenote [\v!endnote ] [\c!location=\v!none] % else no break

%D Compatibility macros:

\unexpanded\def\setupfootnotedefinition{\setupnotedefinition                [\v!footnote]}
\unexpanded\def\setupfootnotes         {\setupnote                          [\v!footnote]}
%unexpanded\def\footnote               {\setnote                            [\v!footnote]}
\def\footnotetext                      {\setnotetext                        [\v!footnote]}
%unexpanded\def\note                   {\dodoubleempty\notesymbol           [\v!footnote]} %  alleen footnote
\unexpanded\def\placefootnotes         {\dodoubleempty\doplacefootnotes     [\v!footnote]}
\unexpanded\def\placelocalfootnotes    {\dodoubleempty\doplacelocalfootnotes[\v!footnote]}
\unexpanded\def\startlocalfootnotes    {\startlocalnotes                    [\v!footnote]} %  alleen footnote
\unexpanded\def\stoplocalfootnotes     {\stoplocalnotes }

\def\doplacefootnotes     [#1][#2]{\ifsecondargument\placenotes     [#1][#2,\c!height=\textheight]\else\placenotes     [#1]\fi}
\def\doplacelocalfootnotes[#1][#2]{\ifsecondargument\placelocalnotes[#1][#2,\c!height=\textheight]\else\placelocalnotes[#1]\fi}

\unexpanded\def\note{\dodoubleempty\donote}

\def\donote[#1][#2]{\ifsecondargument\donotesymbol[#1][#2]\else\secondargumenttrue\donotesymbol[\v!footnote][#1]\fi}

%D Goodies:
%D
%D \starttyping
%D \dorecurse {100} {
%D     test \footnote{\doifnoteonsamepageelse[footnote]{ibidem}{aaa}}
%D }
%D \stoptyping

\def\doifnoteonsamepageelse[#1]{\ctxlua{structures.notes.doifonsamepageasprevious("#1")}}

%D New trickery:

\def\ownnotesymbol#1% #1 gets number passed
  {\executeifdefined{\??vn::\currentnote}\empty}

\unexpanded\def\setnotesymbol[#1]#2#3%
  {\prewordbreak % prevent lookback
   \setgvalue{\??vn::#1}{#3}
   \dolastnotesymbol}

\unexpanded\def\ownnote[#1]#2#3#4%
  {\setnotesymbol[#1]{#2}{#3}%
   \setnotetext  [#1]{#4}}

\defineconversion
  [ownnote]
  [\ownnotesymbol]

\protect \endinput