summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/context/base/tabl-xtb.mkvi
blob: 06fb472cdfd948f1277cc0bed094bd15098f77c0 (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
% macros=mkvi

%D \module
%D   [       file=tabl-xtb,
%D        version=2011.10.26,
%D          title=\CONTEXT\ Table Macros,
%D       subtitle=Xtreme,
%D         author=Hans Hagen,
%D           date=\currentdate,
%D      copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
%C
%C This module is part of the \CONTEXT\ macro||package and is
%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
%C details.

\writestatus{loading}{ConTeXt Table Macros / Xtreme}

\registerctxluafile{tabl-xtb}{1.001}

% todo:
%
% - yes or no: foregroundstyle/color <- style/color
% - template alignment
% - maybe split horizontal (a la linetables)
% - before/after and wrapping (linecorrection)
% - maybe also some before/after commands
% - maybe correction when non float usage
% - tagging needs to be checked
% - maybe only tag the box
% - scale to fit
%
%D This module started as an afternoon experiment and surprisingly could be
%D mostly finished the same evening. Of course it builds upon existing
%D functionality. The main reason for writing it is that we occasionally
%D run into pretty large tables that take tens of pages and need to be split
%D into floats. Speed is one issue there, avoiding to use vsplit is another.
%D
%D \starttyping
%D \definextable [tag] | [tag][parent]
%D \setupxtable [settings] | [tag][settings]
%D
%D \startxtable[tag|settings]
%D     \startxtablehead|\startxtablenext|\startxtablebody|\startxtablefoot
%D         \startxrowgroup[tag|settings]
%D             \startxrow[settings]
%D                 \startxcellgroup[tag|settings]
%D                     \startxcell[settings] ... \stopxcell
%D                 \stopxcellgroup
%D             \stopxrow
%D         \startxrowgroup
%D     \stopxtablehead|\stopxtablenext|\stopxtablebody|\stopxtablefoot
%D \stopxtable
%D \stoptyping
%D
%D See xtables-001.tex etc for some examples.

% We can avoid some checking by using the fastoptionalcheckcs helpers
% instead of dosingleempty but the speed gain is neglectable.

\unprotect

% option=stretch         : equal distribution
% option={stretch,width} : proportional distribution
% option={max}           : prefer max over forced width/height
%
% cells: option=fixed    : nils autostretch (not yet complete)

% \setbox\scratchbox\hbox attr \taggedattribute \attribute\taggedattribute {...}
%
% \let\tsplitbeforeresult\donothing
% \let\tsplitafterresult \donothing
% \let\tsplitinbetween   \donothing
% \let\tsplitbefore      \donothing
% \let\tsplitafter       \donothing
% \let\postprocesstsplit \donothing

\let\dotagxtablecell  \relax % names will change
\let\dotagxtablesignal\relax % names will change

\appendtoks
    \def\dotagxtablecell  {\taggedctxcommand{settagtablecell(\number\tablecellrows,\number\tablecellcolumns,\number\raggedstatus)}}%
    \def\dotagxtablesignal{\char\zerocount}% not used
\to \everyenableelements

\newdimen\d_tabl_x_width
\newdimen\d_tabl_x_height
\newdimen\d_tabl_x_depth        % not used
\newdimen\d_tabl_x_distance
\newcount\c_tabl_x_nx
\newcount\c_tabl_x_ny
\newcount\c_tabl_x_mode
\newbox  \b_tabl_x
\newcount\c_tabl_x_state        % 0=empty 1=content 3=splitleft
\newdimen\d_tabl_x_final_width
\newcount\c_tabl_x_nesting
\newcount\c_tabl_x_skip_mode    % 1 = skip
\newdimen\d_tabl_x_textwidth

\def\currentxtablerow   {\ctxcommand{x_table_r()}}
\def\currentxtablecolumn{\ctxcommand{x_table_c()}}

% \setupxtable[one][parent][a=b,c=d]
% \setupxtable[one]        [a=b,c=d]
% \setupxtable             [a=b,c=d]

\installcorenamespace{xtable}
\installcorenamespace{xtablecheck}

\installframedautocommandhandler \??xtable {xtable} \??xtable

\appendtoks
    \checkxtableparent % so we can deal with undefined settings, not that it's efficient
\to \everysetupxtable

\setupxtable[%
    \c!nr=\plusone,
    \c!nc=\plusone,
    \c!nx=\plusone, % slow
    \c!ny=\plusone, % slow
    \c!align=\v!table, % {\v!flushleft,\v!broad,\v!high}, % just as \bTABLE .. \eTABLE
    \c!frameoffset=.5\linewidth,
    \c!backgroundoffset=\v!frame,
  % \c!framecolor=\s!black,
  % \c!foregroundstyle=\xtableparameter\c!style, % not clean, better capture elsewhere
  % \c!foregroundcolor=\xtableparameter\c!color, % not clean, better capture elsewhere
  % \c!bodyfont=,
    \c!width=\v!fit,
    \c!height=\v!fit,
    \c!maxwidth=8\emwidth,
    \c!autowidth=\v!yes,              % controls framed
    \c!rulethickness=\linewidth,
    \c!strut=\v!yes,
    \c!autostrut=\v!no,
    \c!split=\v!auto,                 % a number will take that many lines
    \c!splitoffset=\zeropoint,        % extra space taken
  % \c!aligncharacter=\v!no,
  % \c!alignmentcharacter={,},
  % \c!option=,                       % \v!stretch {\v!stretch,\v!width}
  % \c!footer=,
  % \c!header=,
    \c!spaceinbetween=,
    \c!textwidth=\v!local,            % was \hsize,
    \c!textheight=\vsize,             % used for vertical spread
    \c!distance=\zeropoint,           % individual column
    \c!columndistance=\zeropoint,     % each column (whole table)
    \c!leftmargindistance=\zeropoint, % whole table
    \c!rightmargindistance=\zeropoint,% whole table
]

\unexpanded\def\startxtable
  {\dosingleempty\tabl_x_start_table}

\let\stopxtable\relax

\def\tabl_x_default_buffer{x_table_\number\c_tabl_x_nesting}
\let\tabl_x_current_buffer\empty

\unexpanded\def\tabl_x_start_table[#settings]% maybe two arguments: [tag][settings] | [tag] | [settings]
  {\bgroup
   \tabl_x_prepare{#settings}%
   \edef\tabl_x_current_buffer{\tabl_x_default_buffer}%
   \buff_pickup{\tabl_x_current_buffer}{startxtable}{stopxtable}\relax\tabl_x_process\zerocount}

\unexpanded\def\processxtablebuffer
  {\dosingleempty\tabl_x_process_buffer_directly}

% These direct buffers can be somewhat faster but it's probably neglectable.
% Anyway, no nesting is supported as we then need to catch (e.g.) rows and
% keep track of nesting and have a more complex redefinition of nested
% instanced \unknown\ it's not worth the trouble. Only use them when you
% really need them and use the embeddedxtable command when nesting them.
% Implementing nesting would be slower than not using direct buffers.

\unexpanded\def\tabl_x_process_buffer_directly[#name]%
  {\bgroup
   \let\tabl_x_start_table\tabl_x_process_buffer
   \edef\tabl_x_current_buffer{#name}%
   \tabl_x_get_buffer %      settings
   \tabl_x_process}

\unexpanded\def\tabl_x_start_ignore[#settings]%
  {}

\unexpanded\def\tabl_x_process_buffer[#settings]%
  {\tabl_x_prepare{#settings}%
   \let\tabl_x_start_table\tabl_x_start_ignore
   \gobbleuntil\stopxtable} % nested xtables are not supported,

%D A bonus: you have to use the following construct inside a macro or
%D direct buffer.

\unexpanded\def\startembeddedxtable
  {\dosingleempty\tabl_x_embedded_start}

\unexpanded\def\tabl_x_embedded_start[#settings]#content\stopembeddedxtable
  {\tabl_x_prepare{#settings}%
   \ctxcommand{assignbuffer("embedded_x_table",\!!bs\detokenize{#content}\!!es)}%
   \bgroup
   \let\tabl_x_start_table\tabl_x_process_buffer
   \edef\tabl_x_current_buffer{embedded_x_table}%
   \tabl_x_process}

\let\stopembeddedxtable\relax

%D We can also define xtables.

\appendtoks
    \setuevalue{\e!start\currentxtable}{\tabl_x_start_named{\currentxtable}}%
    \setuevalue{\e!stop \currentxtable}{\tabl_x_stop_named}%
\to \everydefinextable

\unexpanded\def\tabl_x_start_named#tag%
  {\bgroup
   \edef\currentxtable{#tag}%
   \dosingleempty\tabl_x_start_named_indeed}

\unexpanded\def\tabl_x_start_named_indeed[#settings]%
  {\advance\c_tabl_x_nesting\plusone
   \dostarttagged\t!table\empty
   \iffirstargument
     \setupcurrentxtable[#settings]%
   \fi
   \tabl_x_check_textwidth
   \forgetall
   \edef\tabl_x_current_buffer{\tabl_x_default_buffer}%
   \normalexpanded{\buff_pickup{\tabl_x_current_buffer}{\e!start\currentxtable}{\e!stop\currentxtable}\relax\tabl_x_process\zerocount}}

\unexpanded\def\tabl_x_stop_named
  {}

%D Now we come to processing:

\unexpanded\def\tabl_x_check_textwidth
  {\edef\p_textwidth{\xtableparameter\c!textwidth}%
   \ifx\p_textwidth\v!local
     \d_tabl_x_textwidth\availablehsize
   \else
     \d_tabl_x_textwidth\p_textwidth
   \fi}

\unexpanded\def\tabl_x_prepare#settings% assumes \iffirstargument to be set
  {\advance\c_tabl_x_nesting\plusone
   \dostarttagged\t!table\empty
   \iffirstargument
     \tabl_x_set_checked{#settings}%
   \fi
   \tabl_x_check_textwidth
   \forgetall}

\def\tabl_x_get_buffer
  {\ctxcommand{gettexbuffer("\tabl_x_current_buffer")}}

\let\tabl_x_start_row_yes \relax
\let\tabl_x_start_row_nop \relax
\let\tabl_x_stop_row      \relax
\let\tabl_x_start_cell_yes\relax
\let\tabl_x_start_cell_nop\relax
\let\tabl_x_stop_cell     \relax

\unexpanded\def\tabl_x_process
  {\begingroup % *
   \doifsomething{\xtableparameter\c!bodyfont}
     {\setupbodyfont[\xtableparameter\c!bodyfont]}%
   \setbox\scratchbox\vbox
     {\xtableparameter\c!spaceinbetween}%
   \ctxcommand{x_table_create {
        option              = "\xtableparameter\c!option",
        textwidth           = \number\d_tabl_x_textwidth,
        textheight          = \number\dimexpr\xtableparameter\c!textheight,
        maxwidth            = \number\dimexpr\xtableparameter\c!maxwidth,
        lineheight          = \number\openlineheight,
        columndistance      = \number\dimexpr\xtableparameter\c!columndistance,
        leftmargindistance  = \number\dimexpr\xtableparameter\c!leftmargindistance,
        rightmargindistance = \number\dimexpr\xtableparameter\c!rightmargindistance,
        rowdistance         = \number\ht\scratchbox,
        header              = "\xtableparameter\c!header",
        footer              = "\xtableparameter\c!footer",
   } }%
   %
   \letxtableparameter\c!option\empty
   % not so nice but needed as we use this in the setup
   \linewidth\xtableparameter\c!rulethickness\relax
   % so we freeze it
   \begingroup
     \let\tabl_x_start_row_yes \tabl_x_start_row_reflow_width_yes
     \let\tabl_x_start_row_nop \tabl_x_start_row_reflow_width_nop
     \let\tabl_x_stop_row      \tabl_x_stop_row_reflow_width
     \let\tabl_x_start_cell_yes\tabl_x_start_cell_reflow_width_yes
     \let\tabl_x_start_cell_nop\tabl_x_start_cell_reflow_width_nop
     \let\tabl_x_stop_cell     \tabl_x_stop_cell_reflow_width
     \settrialtypesetting
     \tabl_x_get_buffer
     \ctxcommand{x_table_reflow_width()}\relax
   \endgroup
   \begingroup
     \let\tabl_x_start_row_yes \tabl_x_start_row_reflow_height_yes
     \let\tabl_x_start_row_nop \tabl_x_start_row_reflow_height_nop
     \let\tabl_x_stop_row      \tabl_x_stop_row_reflow_height
     \let\tabl_x_start_cell_yes\tabl_x_start_cell_reflow_height_yes
     \let\tabl_x_start_cell_nop\tabl_x_start_cell_reflow_height_nop
     \let\tabl_x_stop_cell     \tabl_x_stop_cell_reflow_height
     \settrialtypesetting
     \tabl_x_get_buffer
     \ctxcommand{x_table_reflow_height()}\relax
   \endgroup
   \begingroup
     \let\tabl_x_start_row_yes \tabl_x_start_row_construct_yes
     \let\tabl_x_start_row_nop \tabl_x_start_row_construct_nop
     \let\tabl_x_stop_row      \tabl_x_stop_row_construct
     \let\tabl_x_start_cell_yes\tabl_x_start_cell_construct_yes
     \let\tabl_x_start_cell_nop\tabl_x_start_cell_construct_nop
     \let\tabl_x_stop_cell     \tabl_x_stop_cell_construct
     \tabl_x_get_buffer
     \ctxcommand{x_table_construct()}\relax
   \endgroup
   \endgroup % *
   \ifinsidesplitfloat
     \tabl_x_flush_float_split
   \else\ifinsidefloat
     \tabl_x_flush_float_normal
   \else
     \tabl_x_flush_text_checked
   \fi\fi
   \ctxcommand{x_table_cleanup()}%
   \dostoptagged
   \resetbuffer[\tabl_x_current_buffer]%
   \egroup}

% text flow split modes

\installcorenamespace{xtableflushsplit}

\unexpanded\def\tabl_x_flush_text_checked
  {\expandnamespaceparameter\??xtableflushsplit\xtableparameter\c!split\v!no}

% in text flow: headers and footers only once

\setvalue{\??xtableflushsplit\v!yes}%
  {\ctxcommand{x_table_flush{ method = "\v!split" }}}

% in text flow: headers and footers only once

\setvalue{\??xtableflushsplit\v!no}%
  {\dontleavehmode % else no leftskip etc
   \ctxcommand{x_table_flush{ method = "\v!normal" }}}

% in text flow: headers and footers get repeated

\setvalue{\??xtableflushsplit\v!repeat}%
  {\doloop
     {\ctxcommand{x_table_flush{ method = "\v!split", vsize = \number\ifdim\pagegoal=\maxdimen\textheight\else\pagegoal\fi }}%
      \ifcase\c_tabl_x_state
        \exitloop
      \else
        \page
      \fi}}

% \setvalue{\??xtableflushsplit\v!setups}%
%   {\directsetup{xtable:split:user}}
%
% \startsetups[xtable:split:user]
%     \doloop {
%         \xtablesplitflush % uses \xtablesplitvsize (a macro)
%         \ifcase\xtablesplitstate
%             \exitloop
%         \else
%             \page
%         \fi
%     }
% \stopsetups
%
% \unexpanded\def\xtablesplitflush
%   {\ctxcommand{x_table_flush{ method = "\v!split", vsize = \number\dimexpr\xtablesplitvsize\relax}}\relax}
%
% \def\xtablesplitvsize
%   {\ifdim\pagegoal=\maxdimen\textheight\else\pagegoal\fi}
%
% \let\xtablesplitstate\c_tabl_x_state

\let\extratxtablesplitheight\zeropoint % might disappear so don't depend on it

\unexpanded\def\tabl_x_flush_float_normal
  {\ctxcommand{x_table_flush{ method = "\v!normal" }}}

\unexpanded\def\tabl_x_flush_float_split
  {\resetdirecttsplit
   \edef\extrasplitfloatlines  {\xtableparameter\c!split}%
   \edef\tsplitminimumfreespace{\the\dimexpr\extratxtablesplitheight+\xtableparameter\c!splitoffset\relax}%
 % \edef\tsplitminimumfreelines{2}% not needed here as we're precise enough
   \let\tsplitdirectsplitter\tabl_x_split_splitter
   \let\tsplitdirectwidth   \d_tabl_x_final_width
   \handledirecttsplit}

\unexpanded\def\tabl_x_split_splitter#vsize%
  {\setbox\tsplitresult\vbox
     {\ctxcommand{x_table_flush{ method = "\v!split", vsize = \number\dimexpr#vsize }}}%
   \ifcase\c_tabl_x_state
     \global\setfalse\somenextsplitofffloat
   \else
     \global\settrue \somenextsplitofffloat
   \fi}

\unexpanded\def\startxrow
  {\begingroup
   \doifnextoptionalcselse\tabl_x_start_row_yes\tabl_x_start_row_nop}

\unexpanded\def\tabl_x_start_row_reflow_width_yes[#settings]%
  {\setupcurrentxtable[#settings]%
   \ctxcommand{x_table_next_row()}}

\unexpanded\def\tabl_x_start_row_reflow_width_nop
  {\ctxcommand{x_table_next_row()}}

\unexpanded\def\tabl_x_stop_row_reflow_width
  {}

\let\tabl_x_start_row_reflow_height_yes\tabl_x_start_row_reflow_width_yes
\let\tabl_x_start_row_reflow_height_nop\tabl_x_start_row_reflow_width_nop
\let\tabl_x_stop_row_reflow_height     \tabl_x_stop_row_reflow_width

\unexpanded\def\tabl_x_start_row_construct_yes[#settings]%
  {\setupcurrentxtable[#settings]%
   \dostarttagged\t!tablerow\empty
   \ctxcommand{x_table_next_row()}}

\unexpanded\def\tabl_x_start_row_construct_nop
  {\dostarttagged\t!tablerow\empty
   \ctxcommand{x_table_next_row()}}

\unexpanded\def\tabl_x_stop_row_construct
  {\dostoptagged}

\unexpanded\def\stopxrow
  {\tabl_x_stop_row
   \endgroup}

\unexpanded\def\startxcell
  {\doifnextoptionalcselse\tabl_x_start_cell_yes\tabl_x_start_cell_nop}

\unexpanded\def\stopxcell
  {\tabl_x_stop_cell}

\unexpanded\def\tabl_x_begin_of_cell
  {\inhibitblank
   \everypar{\delayedbegstrut}}

\unexpanded\def\tabl_x_end_of_cell
  {\ifhmode
     \delayedendstrut
     \par
   \else
     \par
     \ifdim\prevdepth<\zeropoint % =-1000pt ?
       \vskip-\strutdp
     \else
       \removebottomthings
     \fi
   \fi}

% For historic reasons we support both nx/nc and ny/nr : maybe nx/ny becomes
% obsolete some day. The let as well as the direct speed things up a bit. We
% could also consider a \defaultxtableparameter.
%
% \c_tabl_x_nx\defaultxtableparameter\c!nc{\defaultxtableparameter\c!nx\plusone}
% \c_tabl_x_ny\defaultxtableparameter\c!nr{\defaultxtableparameter\c!ny\plusone}
%
% Although this becomes kind of messy. It saves already time that we only check
% for it when we have settings.

\def\tabl_x_set_hsize
  {\hsize.25\maxdimen} % let's be reasonable

\unexpanded\def\tabl_x_start_cell_reflow_width_yes[#settings]%
  {\setbox\b_tabl_x\hbox\bgroup
   \ifnum\c_tabl_x_nesting>\plusone
     \letxtableparameter\c!width \v!fit  % overloads given width
     \letxtableparameter\c!height\v!fit  % overloads given height
   \fi
   %
   \letxtableparameter\c!nx\plusone
   \letxtableparameter\c!ny\plusone
   \letxtableparameter\c!nc\plusone
   \letxtableparameter\c!nr\plusone
   %
   \setupcurrentxtable[#settings]%
   %
   \c_tabl_x_nx\directxtableparameter\c!nc\relax
   \c_tabl_x_ny\directxtableparameter\c!nr\relax
   \ifnum\c_tabl_x_nx=\plusone
     \c_tabl_x_nx\directxtableparameter\c!nx\relax
   \fi
   \ifnum\c_tabl_x_ny=\plusone
     \c_tabl_x_ny\directxtableparameter\c!ny\relax
   \fi
   %
   \d_tabl_x_distance\xtableparameter\c!distance\relax
   \ctxcommand{x_table_init_reflow_width("\xtableparameter\c!option")}%
   \inheritedxtableframed\bgroup
   \tabl_x_begin_of_cell
   \tabl_x_set_hsize}

\unexpanded\def\tabl_x_start_cell_reflow_width_nop
  {\setbox\b_tabl_x\hbox\bgroup
   \ifnum\c_tabl_x_nesting>\plusone
     \letxtableparameter\c!width \v!fit  % overloads given width
     \letxtableparameter\c!height\v!fit  % overloads given height
   \fi
   \c_tabl_x_nx\plusone
   \c_tabl_x_ny\plusone
   \d_tabl_x_distance\xtableparameter\c!distance\relax
   \ctxcommand{x_table_init_reflow_width()}%
   \inheritedxtableframed\bgroup
   \tabl_x_begin_of_cell
   \tabl_x_set_hsize}

\unexpanded\def\tabl_x_stop_cell_reflow_width
  {\tabl_x_end_of_cell
   \egroup
   \egroup
   \ctxcommand{x_table_set_reflow_width()}}

\unexpanded\def\tabl_x_start_cell_reflow_height_yes[#settings]%
  {\setbox\b_tabl_x\hbox\bgroup
   \ctxcommand{x_table_init_reflow_height()}%
   \ifcase\c_tabl_x_skip_mode % can be sped up
     \ifnum\c_tabl_x_nesting>\plusone
       \letxtableparameter\c!height\v!fit  % overloads given height
     \fi
     \setupcurrentxtable[#settings]%
     \relax
     \letxtableparameter\c!width\d_tabl_x_width  % overloads given width
     \inheritedxtableframed\bgroup
     \tabl_x_begin_of_cell
   \fi}

\unexpanded\def\tabl_x_start_cell_reflow_height_nop
  {\setbox\b_tabl_x\hbox\bgroup
   \ctxcommand{x_table_init_reflow_height()}%
   \ifcase\c_tabl_x_skip_mode % can be sped up
     \ifnum\c_tabl_x_nesting>\plusone
       \letxtableparameter\c!height\v!fit  % overloads given height
     \fi
     \relax
     \letxtableparameter\c!width\d_tabl_x_width  % overloads given width
     \inheritedxtableframed\bgroup
     \tabl_x_begin_of_cell
   \fi}

\unexpanded\def\tabl_x_stop_cell_reflow_height
  {\ifcase\c_tabl_x_skip_mode
     \tabl_x_end_of_cell
     \egroup
   \fi
   \egroup
   \ctxcommand{x_table_set_reflow_height()}}

\unexpanded\def\tabl_x_start_cell_construct_yes[#settings]%
  {\dostarttagged\t!tablecell\empty % can't we just tag the box
   \setbox\b_tabl_x\hbox\bgroup
   \setupcurrentxtable[#settings]%
   \letxtableparameter\c!width \d_tabl_x_width  % overloads given width
   \letxtableparameter\c!height\d_tabl_x_height % overloads given height
   \ctxcommand{x_table_init_construct()}%
   \inheritedxtableframed\bgroup
   \tabl_x_begin_of_cell
   \dotagxtablecell}

\unexpanded\def\tabl_x_start_cell_construct_nop
  {\dostarttagged\t!tablecell\empty % can't we just tag the box
   \setbox\b_tabl_x\hbox\bgroup
   \letxtableparameter\c!width \d_tabl_x_width  % overloads given width
   \letxtableparameter\c!height\d_tabl_x_height % overloads given height (commenting it ... nice option)
   \ctxcommand{x_table_init_construct()}%
   \inheritedxtableframed\bgroup
   \tabl_x_begin_of_cell
   \dotagxtablecell}

\unexpanded\def\tabl_x_stop_cell_construct
  {\tabl_x_end_of_cell
   \egroup
   \dotagxtablesignal % harmless spot
   \egroup
   \ctxcommand{x_table_set_construct()}%
   \dostoptagged}

\unexpanded\def\startxcellgroup
  {\begingroup
   \dosingleempty\tabl_x_start_cell_group}

\unexpanded\def\stopxcellgroup
  {\endgroup}

\unexpanded\def\tabl_x_start_cell_group[#settings]%
  {\iffirstargument
     \tabl_x_set_checked{#settings}%
   \fi}

\unexpanded\def\startxrowgroup
  {\begingroup
   \dosingleempty\tabl_x_start_row_group}

\unexpanded\def\stopxrowgroup
  {\dostoptagged
   \endgroup}

\unexpanded\def\tabl_x_start_row_group[#settings]%
  {\iffirstargument
     \tabl_x_set_checked{#settings}%
   \fi}

% \def\tabl_x_set_checked#settings
%   {\doifassignmentelse{#settings}
%      {\setupcurrentxtable[#settings]}
%      {\ifcsname\namedxtablehash{#settings}\s!parent\endcsname
%         \edef\currentxtable{#settings}%
%       \fi}}

\unexpanded\def\tabl_x_set_checked#settings%
  {\ifcsname\namedxtablehash{#settings}\s!parent\endcsname
     \edef\currentxtable{#settings}%
   \else
     \setupcurrentxtable[#settings]%
   \fi}

\unexpanded\def\startxtablehead{\begingroup\c_tabl_x_mode\plusone  \dosingleempty\tabl_x_start_partition}
\unexpanded\def\startxtablefoot{\begingroup\c_tabl_x_mode\plustwo  \dosingleempty\tabl_x_start_partition}
\unexpanded\def\startxtablenext{\begingroup\c_tabl_x_mode\plusthree\dosingleempty\tabl_x_start_partition}
\unexpanded\def\startxtablebody{\begingroup\c_tabl_x_mode\plusfour \dosingleempty\tabl_x_start_partition}

\unexpanded\def\tabl_x_start_partition[#settings]%
  {\iffirstargument
     \tabl_x_set_checked{#settings}%
   \fi}

\unexpanded\def\tabl_x_stop_partition
  {\endgroup}

\let\stopxtablehead\tabl_x_stop_partition
\let\stopxtablefoot\tabl_x_stop_partition
\let\stopxtablenext\tabl_x_stop_partition
\let\stopxtablebody\tabl_x_stop_partition

%D This is an experiment! Beware: you can create loops by using nested
%D references to already chained settings.
%D
%D \startbuffer
%D \setupxtable[suffix][align=middle,foregroundcolor=red]
%D \setupxtable[blabla][foregroundstyle=slanted]
%D \setupxtable[crap]  [foregroundcolor=blue]
%D \setupxtable[bold]  [crap][foregroundstyle=bold]
%D
%D \startxtable[frame=off]
%D     \startxtablehead
%D         \startxrow[bold]
%D             \startxcell[suffix] a 0 \stopxcell
%D             \startxcell[blabla] a 1 \stopxcell
%D             \startxcell         a 2 \stopxcell
%D         \stopxrow
%D     \stopxtablehead
%D     \startxtablebody
%D         \startxrow \startxcell[suffix][ny=2] a 1 \stopxcell \startxcell b 1 \stopxcell \startxcell c 1 \stopxcell \stopxrow
%D         \startxrow                                          \startxcell b 2 \stopxcell \startxcell c 2 \stopxcell \stopxrow
%D         \startxrow \startxcell[suffix]       a 3 \stopxcell \startxcell b 3 \stopxcell \startxcell c 3 \stopxcell \stopxrow
%D         \startxrow \startxcell[suffix]       a 4 \stopxcell \startxcell b 4 \stopxcell \startxcell c 4 \stopxcell \stopxrow
%D         \startxrow \startxcell[suffix]       a 5 \stopxcell \startxcell b 5 \stopxcell \startxcell c 5 \stopxcell \stopxrow
%D     \stopxtablebody
%D \stopxtable
%D \stopbuffer
%D
%D \typebuffer \placetable{}{\getbuffer}

\appendtoks
    \letvalue{\??xtablecheck\currentxtable}\relax % faster than checking parent
\to \everysetupxtable

% groups

\unexpanded\def\startxgroup
  {\begingroup
   \doifnextoptionalcselse\tabl_x_start_group_delayed_one\relax}

\unexpanded\def\stopxgroup
  {\endgroup}

\unexpanded\def\tabl_x_start_group_delayed_one[#tag]%
% {\ifcsname\namedxtablehash{#tag}\s!parent\endcsname
  {\ifcsname\??xtablecheck#tag\endcsname
     \expandafter\tabl_x_start_group_delayed_two
   \else
     \expandafter\setupcurrentxtable
   \fi[#tag]}

\unexpanded\def\tabl_x_start_group_delayed_two[#tag]%
  {\ifx\currentxtable\empty \else
     \chaintocurrentxtable{#tag}%
   \fi
   \edef\currentxtable{#tag}%
   \doifnextoptionalcselse\setupcurrentxtable\relax}

\let\startxrowgroup \startxgroup
\let\stopxrowgroup  \stopxgroup
\let\startxcellgroup\startxgroup
\let\stopxcellgroup \stopxgroup

% cells (maybe also check for 1 etc but it becomes messy)

\unexpanded\def\startxcell
  {\begingroup
   \doifnextoptionalcselse\tabl_x_start_cell_delayed_one\tabl_x_start_cell_nop}

\unexpanded\def\tabl_x_start_cell_delayed_one[#tag]%
% {\ifcsname\namedxtablehash{#tag}\s!parent\endcsname
  {\ifcsname\??xtablecheck#tag\endcsname
     \expandafter\tabl_x_start_cell_delayed_two
   \else
     \expandafter\tabl_x_start_cell_yes
   \fi[#tag]}

\unexpanded\def\tabl_x_start_cell_delayed_two[#tag]%
  {\ifx\currentxtable\empty \else
     \chaintocurrentxtable{#tag}%
   \fi
   \edef\currentxtable{#tag}%
   \doifnextoptionalcselse\tabl_x_start_cell_yes\tabl_x_start_cell_nop}

\unexpanded\def\stopxcell
  {\tabl_x_stop_cell
   \endgroup}

% rows

\unexpanded\def\startxrow
  {\begingroup
   \doifnextoptionalcselse\tabl_x_start_row_delayed_one\tabl_x_start_row_nop}

\unexpanded\def\tabl_x_start_row_delayed_one[#tag]%
% {\ifcsname\namedxtablehash{#tag}\s!parent\endcsname
  {\ifcsname\??xtablecheck#tag\endcsname
     \expandafter\tabl_x_start_row_delayed_two
   \else
     \expandafter\tabl_x_start_row_yes
   \fi[#tag]}

\unexpanded\def\tabl_x_start_row_delayed_two[#tag]%
  {\ifx\currentxtable\empty \else
     \chaintocurrentxtable{#tag}%
   \fi
   \edef\currentxtable{#tag}%
   \doifnextoptionalcselse\tabl_x_start_row_yes\tabl_x_start_row_nop}

\unexpanded\def\stopxrow
  {\tabl_x_stop_row
   \endgroup}

\protect \endinput