page-cst.mkxl /size: 26 Kb    last modification: 2023-12-21 09:44
1%D \module
2%D   [       file=page-cst,
3%D        version=2016.12.15,
4%D          title=\CONTEXT\ Page Macros,
5%D       subtitle=Page Grids (aka Column Sets),
6%D         author=Hans Hagen,
7%D           date=\currentdate,
8%D      copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
9%C
10%C This module is part of the \CONTEXT\ macro||package and is
11%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
12%C details.
13
14% todo : markings per column
15
16%D This module is work in progress and in due time it will replace
17%D columnsets.
18
19\writestatus{loading}{ConTeXt Page Macros / Page Grids}
20
21\registerctxluafile{page-cst}{}
22
23\unprotect
24
25% maybe some protected def ([esc])
26
27%D Columnsets are kind of special. They are mostly meant for special products with
28%D magazine like properties. They are normally not mixed with single column layouts
29%D and not all features of \CONTEXT\ might cooperate well with a mechanism like
30%D this. We use the name page grid because (as with other reimplementations of
31%D \MKII\ features in \MKIV, we need another namespace in order to migrate stepwise.
32%D
33%D This implementation is not neccessarily better than the previous one but it might
34%D be easier to extend it. It should be a bit more efficient.
35%D
36%D When writing this code I occasionally needed a motivational musical time||out and
37%D watching the latest Snarky Puppy DVD brought me the musically and visually videos
38%D of Jacob Collier (Piano, voice, anything) on YouTube (and yes, music keeps amazing
39%D me). It's definitely more fun to watch that than to write code like this.
40
41\definesystemconstant{pagegrid}
42
43\installcorenamespace{pagegrid}
44
45\installframedcommandhandler \??pagegrid {pagegrid} \??pagegrid
46
47\setuppagegrid
48  [\c!distance=1.5\bodyfontsize,
49   \c!n=\plustwo,
50   \c!nleft=\pagegridparameter\c!n,
51   \c!nright=\pagegridparameter\c!n,
52  %\c!align=, % inherit
53  %\c!separator=\v!none,
54  %\c!setups=,
55   \c!lines=\layoutparameter\c!lines,
56   \c!frame=\v!off,
57   \c!strut=\v!no,
58   \c!offset=\v!overlay,
59   \c!alternative=\v!local,
60   \c!width=\v!auto,
61   \c!page=,
62   \c!direction=\v!normal, % todo
63   \c!maxheight=\textheight,
64   \c!maxwidth=\makeupwidth]
65
66\appendtoks % could become an option
67    \frozen\instance\protected\edefcsname\e!start\currentpagegrid\endcsname{\startpagegrid[\currentpagegrid]}%
68    \frozen\instance\protected\edefcsname\e!stop \currentpagegrid\endcsname{\stoppagegrid}%
69    \clf_definecolumnset {
70        name {\currentpagegrid}%
71    }%
72\to \everydefinepagegrid
73
74\newdimension\d_page_grd_column_width
75\newdimension\d_page_grd_max_height
76\newdimension\d_page_grd_max_width
77\newdimension\d_page_grd_distance
78
79\newdimension\d_page_grd_reserved_height
80\newdimension\d_page_grd_reserved_width
81\newinteger  \c_page_grd_reserved_state
82
83\newdimension\d_page_grd_gap_height
84
85\newinteger  \c_page_grd_n_of_left
86\newinteger  \c_page_grd_n_of_right
87\newinteger  \c_page_grd_n_of_rows
88\newinteger  \c_page_grd_first_column
89\newinteger  \c_page_grd_last_column
90
91\newbox      \b_page_grd_collected
92\newbox      \b_page_grd_column_rest
93\newbox      \b_page_grd_column
94
95%D All the parameters are mandate!
96
97\permanent\tolerant\protected\def\setuppagegridlines[#1]#*[#2]#*[#3]#*[#4]% id page col value
98  {\clf_setcolumnsetlines{name {#1} page #2 column #3 value #4}}
99
100\permanent\tolerant\protected\def\setuppagegridstart[#1]#*[#2]#*[#3]#*[#4]% id page col value
101  {\clf_setcolumnsetstart{name {#1} page #2 column #3 value #4}}
102
103\protected\def\page_grd_check
104  {\dorecurse{\numexpr\pagegridparameter\c!nleft+\pagegridparameter\c!nright}
105     {\page_grd_check_column{##1}}}
106
107\protected\def\page_grd_check_column#1%
108  {\chaintocurrentpagegrid{\currentpagegrid:#1}%
109   \edef\p_distance{\namedpagegridparameter{\currentpagegrid:#1}\c!distance}%
110   \edef\p_width   {\namedpagegridparameter{\currentpagegrid:#1}\c!width}%
111   \clf_setcolumnsetproperties {%
112      name {\currentpagegrid}
113      column \numexpr#1\relax
114      \ifempty\p_distance\else
115        distance \p_distance
116      \fi
117      \ifempty\p_width\orelse\ifx\p_width\v!auto\else
118        width \p_width
119      \fi
120   }}
121
122\appendtoks
123   \dorecurse{\numexpr\pagegridparameter\c!nleft+\pagegridparameter\c!nright}%
124     {\chaintocurrentpagegrid{\currentpagegrid:#1}}%
125\to \everydefinepagegrid
126
127\permanent\tolerant\protected\def\startpagegrid[#S#1]#*[#S#2]%
128  {\bgroup
129   \enforced\let\startpagegrid\page_grd_start_dummy
130   \ifarguments
131     \lettonothing\currentpagegrid
132   \or
133     \ifhastok={#1}%
134       \lettonothing\currentpagegrid
135       \setupcurrentpagegrid[#1]%
136     \else
137       \cdef\currentpagegrid{#1}%
138     \fi
139   \or
140     \cdef\currentpagegrid{#1}%
141     \setupcurrentpagegrid[#2]%
142   \fi
143   \usepageparameter\pagegridparameter
144   \c_page_grd_n_of_left \pagegridparameter\c!nleft\relax
145   \c_page_grd_n_of_right\pagegridparameter\c!nright\relax
146   \c_page_grd_n_of_rows \pagegridparameter\c!lines\relax
147   \d_page_grd_max_width \pagegridparameter\c!maxwidth\relax
148   \d_page_grd_max_height\pagegridparameter\c!maxheight\relax
149   \d_page_grd_distance  \pagegridparameter\c!distance\relax
150   %
151   \automigrationmode\zerocount % for now
152   %
153   \ifcase\c_page_grd_n_of_rows
154      \getrawnoflines{\dimexpr\d_page_grd_max_height-\strutheight+\topskip\relax}%
155      \c_page_grd_n_of_rows\noflines
156   \fi
157   \edef\p_width{\pagegridparameter\c!width}%
158   \insidecolumnstrue % will be different flag in addition
159   \clf_resetcolumnset {
160        name       {\currentpagegrid}
161        nofrows    \c_page_grd_n_of_rows
162        nofleft    \c_page_grd_n_of_left
163        nofright   \c_page_grd_n_of_right
164        lineheight \strutht
165        linedepth  \strutdp
166      \ifx\p_width\v!auto
167        % sets \d_page_grd_column_width
168      \else
169        width      \p_width
170      \fi
171        distance   \d_page_grd_distance
172        maxwidth   \d_page_grd_max_width
173   }%
174   %
175   \page_grd_check
176   %
177   \clf_flushcolumnsetareas{\currentpagegrid}\relax
178   \setupoutputroutine[\s!pagegrid]%
179   \page_grd_command_set_hsize
180   \page_grd_command_set_vsize
181   %
182   \columnwidth   \d_page_grd_column_width
183   \columndistance\d_page_grd_distance
184   \nofcolumns    \c_page_grd_n_of_left    % not always ok
185   \textwidth     \d_page_grd_column_width % kind of redundant but we had it so ...
186   %
187   }%\begingroup}
188
189\tolerant\def\page_grd_start_dummy[#S#1]#*[#S#2]%
190  {\let\page_grd_stop\egroup}
191
192\permanent\protected\def\stoppagegrid
193  {\page_grd_stop}
194
195\def\page_grd_stop
196  {\endgraf % needed, else wrong vsize in one par case
197   \vfill % otherwise weird \placenotes[endnotes]
198   \page_grd_command_set_vsize % needed
199   \penalty\c_page_otr_eject_penalty
200   \page_grd_command_flush_page
201   \page_otr_fill_and_eject_page
202   \page_grd_command_set_vsize % why here
203   \egroup
204   \page_otr_command_set_vsize
205   \page_otr_command_set_hsize}
206
207\permanent\protected\def\reservepagegrid[#S#1]%
208  {\begingroup
209   \letdummyparameter\c!c\plusone
210   \letdummyparameter\c!r\plusone
211   \letdummyparameter\c!nc\plusone
212   \letdummyparameter\c!nr\plusone
213   \getdummyparameters[#1]%
214   \clf_blockcolumnset {
215     name {\currentpagegrid}
216     c  \dummyparameter\c!c
217     r  \dummyparameter\c!r
218     nc \dummyparameter\c!nc
219     nr \dummyparameter\c!nr
220   }%
221   \endgroup}
222
223\permanent\tolerant\protected\def\setpagegrid[#S#1]%
224  {\begingroup
225   \letdummyparameter\c!c\zerocount
226   \letdummyparameter\c!r\zerocount
227   \letdummyparameter\c!method\v!here
228   \letdummyparameter\c!option\v!none
229   \getdummyparameters[#1]%
230   \dowithnextboxcs\page_grd_set_indeed\hbox}
231
232\def\page_grd_set_indeed
233  {\clf_checkcolumnset {
234     name   {\currentpagegrid}
235     c      \dummyparameter\c!c
236     r      \dummyparameter\c!r
237     box    \nextbox
238     method {\dummyparameter\c!method}
239     option {\dummyparameter\c!option}
240   }%
241   \ifcase\c_page_grd_reserved_state
242     \setbox\nextbox\vpack to \d_page_grd_reserved_height \bgroup
243       \vss
244       \hpack to \d_page_grd_reserved_width \bgroup
245         \box\nextbox
246         \hss
247       \egroup
248       \vss
249     \egroup
250     \wd\nextbox\d_page_grd_reserved_width
251     \clf_putincolumnset {
252       name {\currentpagegrid}
253       box \nextbox
254     }%
255   \fi
256   \endgroup}
257
258\protected\def\page_grd_command_set_vsize
259  {\clf_setvsizecolumnset{\currentpagegrid}%
260   \ifdim\d_page_grd_gap_height<\lineheight
261     \page_grd_command_flush_page
262     \page_otr_fill_and_eject_page
263   \fi
264   \global\vsize\d_page_grd_gap_height
265   \pagegoal\vsize}
266
267\protected\def\page_grd_command_set_hsize
268  {\clf_sethsizecolumnset{\currentpagegrid}%
269   \hsize\d_page_grd_column_width
270   \textwidth\d_page_grd_column_width}
271
272\protected\def\page_grd_command_routine
273  {\ifvoid\normalpagebox \else
274     \clf_addtocolumnset{\currentpagegrid}\normalpagebox
275   \fi
276   \page_grd_command_set_vsize
277   \page_grd_command_flush_saved_floats
278   \page_grd_command_set_vsize
279   \ifdim\d_page_grd_gap_height<\lineheight
280     \page_grd_command_flush_page
281   \fi
282   \page_grd_command_set_vsize
283   \clf_flushcolumnsetrest {\currentpagegrid}\normalpagebox
284   \ifvoid\normalpagebox \else
285     \unvbox\normalpagebox
286   \fi}
287
288\installoutputroutine\synchronizepagegrid
289  {\ifvoid\normalpagebox\else
290     \clf_addtocolumnset{\currentpagegrid}\normalpagebox
291     \page_grd_command_set_vsize
292     \clf_flushcolumnsetrest{\currentpagegrid}\normalpagebox
293     \ifvoid\normalpagebox \else
294       \unvbox\normalpagebox
295     \fi
296   \fi}
297
298% todo line numbers and marks
299
300\protected\def\page_grd_command_flush_page_column#1%
301  {\privatescratchcounter#1\relax % or just currentcolumn as in page-col.mkiv
302   \clf_flushcolumnsetcolumn{\currentpagegrid}\privatescratchcounter
303   \anch_mark_column_box\b_page_grd_column\privatescratchcounter
304   \page_marks_synchronize_column\c_page_grd_first_column\c_page_grd_last_column\privatescratchcounter\b_page_grd_column
305   \ifnum\privatescratchcounter>\c_page_grd_n_of_left
306     \advanceby\privatescratchcounter-\c_page_grd_n_of_left
307     \page_lines_add_numbers_to_box\b_page_grd_column\privatescratchcounter\c_page_grd_n_of_right\plustwo
308   \else
309     \page_lines_add_numbers_to_box\b_page_grd_column\privatescratchcounter\c_page_grd_n_of_left\plustwo
310   \fi
311   \begingroup
312   \cdef\currentpagegrid{\currentpagegrid:#1}%
313   \inheritedpagegridframedbox\box\b_page_grd_column
314   \endgroup}
315
316\protected\def\page_grd_command_flush_page
317  {\deactivatecolor % puzzling, try ungrouped color \red or so
318   \setbox\b_page_grd_collected\hpack\bgroup
319     \clf_preparecolumnsetflush{\currentpagegrid}%
320     \letpagegridparameter\c!region\currentpagegrid
321     \ifcstok{\pagegridparameter\c!direction}\v!reverse
322       \dostepwiserecurse\c_page_grd_last_column\c_page_grd_first_column\minusone
323         {\page_grd_command_flush_page_column{##1}%
324          \ifnum##1>\plusone
325            \kern\namedpagegridparameter{\currentpagegrid:##1}\c!distance\relax
326          \fi}%
327     \else
328       \dostepwiserecurse\c_page_grd_first_column\c_page_grd_last_column\plusone
329         {\page_grd_command_flush_page_column{##1}%
330          \ifnum##1<\c_page_grd_last_column
331            \kern\namedpagegridparameter{\currentpagegrid:##1}\c!distance\relax
332          \fi}%
333     \fi
334     \clf_finishcolumnsetflush{\currentpagegrid}%
335   \egroup
336   \page_otr_construct_and_shipout\box\b_page_grd_collected\zerocount % three arguments
337   \clf_flushcolumnsetareas{\currentpagegrid}\relax
338   \page_grd_command_flush_saved_floats}
339
340% slow but robust
341
342\protected\def\page_grd_command_next_progress
343  {\strut
344   \page_grd_command_flush_all_floats
345   \page_otr_eject_page
346   \ifcase\clf_columnsetnoto\else
347     \expandafter\page_grd_command_next_progress
348   \fi}
349
350\protected\def\page_grd_command_handle_column
351  {\ifcase\clf_columnsetgoto{\currentpagegrid}{\page_breaks_columns_current_option}\relax\else
352     \expandafter\page_grd_command_next_progress
353   \fi}
354
355\installcolumnbreakmethod\s!pagegrid\s!unknown {\page_grd_command_handle_column}
356\installcolumnbreakmethod\s!pagegrid\v!yes     {\page_grd_command_handle_column}
357
358\protected\def\page_grd_command_next_page
359  {\ifcase\clf_columnsetgoto{\currentpagegrid}{\v!page}\relax\else
360     \page_grd_command_flush_page
361   \fi}
362
363\protected\def\page_grd_command_next_page_and_inserts
364  {\page_grd_command_flush_all_floats
365   \page_grd_command_next_page}
366
367\let\page_grd_command_flush_all_floats\page_one_command_flush_all_floats
368\let\page_grd_command_package_contents\page_one_command_package_contents
369
370\protected\def\page_grd_command_flush_saved_floats
371  {\ifconditional\c_page_floats_flushing \else
372     \ifconditional\c_page_floats_some_waiting
373       \page_grd_command_flush_saved_floats_indeed
374     \fi
375   \fi}
376
377\protected\def\page_grd_command_flush_saved_floats_indeed
378  {\page_floats_flush\s!text\plusone
379   \clf_checkcolumnset {
380     name   {\currentpagegrid}
381     method {\floatmethod}
382     width  \wd\floatbox
383     height \ht\floatbox
384   }%
385   \ifcase\c_page_grd_reserved_state
386     \page_grd_place_float_here_indeed
387     \page_grd_command_set_vsize % needed
388     \ifconditional\c_page_floats_some_waiting
389       \doubleexpandafter\page_grd_command_flush_saved_floats_indeed
390     \fi
391   \else
392     \page_floats_resave\s!text
393   \fi}
394
395% needs checking
396
397\protected\def\page_grd_command_flush_floats
398  {\wait\global\c_page_floats_flushing\conditionaltrue
399   \ifconditional\c_page_floats_some_waiting
400     \par
401     \page_grd_command_flush_floats_indeed
402   \fi
403   \global\savednoffloats\zerocount
404   \global\c_page_floats_some_waiting\conditionalfalse
405   \global\c_page_floats_flushing\conditionalfalse}
406
407\def\page_grd_command_flush_floats_indeed % much in common with OTRSET
408  {\ifconditional\c_page_floats_some_waiting
409     \ifconditional\c_page_floats_compress_flushed
410       \page_floats_collect\s!text\hsize\d_page_floats_compress_distance
411       \ifcase\nofcollectedfloats
412         \page_floats_get
413     % \or
414     %   \page_floats_get
415       \else
416         \c_page_floats_center_box\conditionalfalse % not needed as we do call directly
417         \global\setbox\floatbox\hbox to \hsize
418           {\hfil
419            \dorecurse\nofcollectedfloats
420              {\ifcase\columndirection % nog document wide
421                 \page_floats_flush\s!text\plusone
422               \else
423                 \page_floats_flush\s!text{\the\numexpr\nofcollectedfloats-\recurselevel+1\relax}%
424               \fi
425               \ifdim\wd\floatbox>\makeupwidth % \hsize
426                 \hbox to \makeupwidth{\hss\box\floatbox\hss}%
427               \else
428                 \box\floatbox
429               \fi
430               \ifnum\recurselevel<\nofcollectedfloats
431                 \hfil
432               \fi}%
433            \hfil}%
434         \fi
435     \else
436       \page_floats_get
437     \fi
438     \doplacefloatbox
439     \expandafter\page_grd_command_flush_floats_indeed
440   \fi}
441
442% so far
443
444\protected\def\page_grd_command_check_if_float_fits
445  {\clf_checkcolumnset {
446     name   {\currentpagegrid}
447     method {\floatmethod}
448   % c      \zerocount
449   % r      \zerocount
450     box    \floatbox
451   }%
452   \ifcase\c_page_grd_reserved_state
453     \global\c_page_floats_room\conditionaltrue
454   \else
455     \global\c_page_floats_room\conditionalfalse
456   \fi}
457
458\protected\def\page_grd_place_float_here_indeed
459  {\setbox\floatbox\vpack to \d_page_grd_reserved_height \bgroup
460     \vss
461     \hpack to \d_page_grd_reserved_width \bgroup
462     % \hss % no
463       \box\floatbox
464       \hss
465     \egroup
466     \vss
467   \egroup
468   \clf_putincolumnset {
469     name {\currentpagegrid}
470     box  \floatbox
471   }}
472
473\def\page_grd_place_float_slot
474  {% safeguard
475   \ifempty\floatmethod
476     \let\floatmethod\v!here
477   \fi
478   % synchronize
479   \penalty\c_page_otr_eject_penalty
480   % push
481   \setbox\savedfloatbox\box\floatbox
482   \page_grd_command_flush_saved_floats
483   \setbox\floatbox\box\savedfloatbox
484   % pop
485   \ifconditional\c_page_floats_some_waiting
486     \page_floats_save\s!text
487     \nonoindentation
488   \else
489     \clf_checkcolumnset {
490       name   {\currentpagegrid}
491       method {\floatmethod}
492     \ifempty\floatcolumn \else
493       c      \floatcolumn
494     \fi
495     \ifempty\floatrow \else
496       r      \floatrow
497     \fi
498       box    \floatbox
499     }%
500     \ifcase\c_page_grd_reserved_state
501       \page_grd_place_float_here_indeed
502     \else
503       \page_floats_save\s!text
504       \nonoindentation
505     \fi
506   \fi}
507
508\def\page_grd_place_float_fixed % todo: fallback on here
509  {\ifempty\floatcolumn
510     \let\floatmethod\v!here
511   \orelse\ifempty\floatrow
512     \let\floatmethod\v!here
513   \else
514     \let\floatmethod\v!fixed
515   \fi
516   \page_grd_place_float_slot}
517
518\def\page_grd_place_float_force
519  {% synchronize
520   \penalty\c_page_otr_eject_penalty
521   \clf_checkcolumnset {
522     name   {\currentpagegrid}
523     method {\floatmethod}
524     box    \floatbox
525   }%
526   \ifcase\c_page_grd_reserved_state
527     \page_grd_place_float_here_indeed
528   \else
529     \page_floats_save\s!text
530     \nonoindentation
531   \fi}
532
533\def\page_grd_place_float_page  {\page_grd_place_float_slot} % todo: fallback on here
534
535\def\page_grd_place_float_here  {\let\floatmethod\v!here\page_grd_place_float_slot}
536\def\page_grd_place_float_top   {\page_grd_place_float_slot}
537\def\page_grd_place_float_bottom{\page_grd_place_float_slot}
538
539\installfloatmethod \s!pagegrid \v!here        \page_grd_place_float_here
540\installfloatmethod \s!pagegrid \v!force       \page_grd_place_float_force % todo
541%installfloatmethod \s!pagegrid \v!left
542%installfloatmethod \s!pagegrid \v!right
543%installfloatmethod \s!pagegrid \v!text
544\installfloatmethod \s!pagegrid \v!top         \page_grd_place_float_top
545\installfloatmethod \s!pagegrid \v!bottom      \page_grd_place_float_bottom
546%installfloatmethod \s!pagegrid \v!auto
547%installfloatmethod \s!pagegrid \v!margin
548%installfloatmethod \s!pagegrid \v!opposite
549\installfloatmethod \s!pagegrid \v!page        \page_grd_place_float_page
550%installfloatmethod \s!pagegrid \v!leftpage
551%installfloatmethod \s!pagegrid \v!rightpage
552%installfloatmethod \s!pagegrid \v!inmargin
553%installfloatmethod \s!pagegrid \v!inleft
554%installfloatmethod \s!pagegrid \v!inright
555%installfloatmethod \s!pagegrid \v!leftmargin
556%installfloatmethod \s!pagegrid \v!rightmargin
557%installfloatmethod \s!pagegrid \v!leftedge
558%installfloatmethod \s!pagegrid \v!rightedge
559%installfloatmethod \s!pagegrid \v!somewhere
560%installfloatmethod \s!pagegrid \v!backspace
561%installfloatmethod \s!pagegrid \v!cutspace
562\installfloatmethod \s!pagegrid \s!tblr        \page_grd_place_float_slot
563\installfloatmethod \s!pagegrid \s!lrtb        \page_grd_place_float_slot
564\installfloatmethod \s!pagegrid \s!tbrl        \page_grd_place_float_slot
565\installfloatmethod \s!pagegrid \s!rltb        \page_grd_place_float_slot
566\installfloatmethod \s!pagegrid \s!fxtb        \page_grd_place_float_slot
567\installfloatmethod \s!pagegrid \s!btlr        \page_grd_place_float_slot
568\installfloatmethod \s!pagegrid \s!lrbt        \page_grd_place_float_slot
569\installfloatmethod \s!pagegrid \s!btrl        \page_grd_place_float_slot
570\installfloatmethod \s!pagegrid \s!rlbt        \page_grd_place_float_slot
571\installfloatmethod \s!pagegrid \s!fxbt        \page_grd_place_float_slot
572\installfloatmethod \s!pagegrid \s!fixd        \page_grd_place_float_fixed
573
574\protected\def\page_grd_command_side_float_output
575  {} % nothing, reset anyway
576
577\protected\def\page_grd_command_flush_side_floats
578  {\page_sides_forget_floats}
579
580\protected\def\page_grd_command_synchronize_side_floats
581  {\page_sides_forget_floats}
582
583\protected\def\page_grd_command_synchronize_hsize
584  {\page_grd_command_set_hsize}
585
586\protected\def\page_grd_command_flush_all_floats
587  {\page_one_command_flush_all_floats}
588
589\defineoutputroutine
590  [\s!pagegrid]
591  [\s!page_otr_command_routine                =\page_grd_command_routine,
592   \s!page_otr_command_package_contents       =\page_grd_command_package_contents,
593   \s!page_otr_command_set_vsize              =\page_grd_command_set_vsize,
594   \s!page_otr_command_set_hsize              =\page_grd_command_set_hsize, % tricky, goes wrong
595   \s!page_otr_command_synchronize_hsize      =\page_grd_command_synchronize_hsize,
596   \s!page_otr_command_next_page              =\page_grd_command_next_page,
597   \s!page_otr_command_next_page_and_inserts  =\page_grd_command_next_page_and_inserts,
598 % \s!page_otr_command_set_top_insertions     =\page_grd_command_set_top_insertions,
599 % \s!page_otr_command_set_bottom_insertions  =\page_grd_command_set_bottom_insertions,
600 % \s!page_otr_command_flush_top_insertions   =\page_grd_command_flush_top_insertions,
601 % \s!page_otr_command_flush_bottom_insertions=\page_grd_command_flush_bottom_insertions,
602   \s!page_otr_command_check_if_float_fits    =\page_grd_command_check_if_float_fits,
603 % \s!page_otr_command_set_float_hsize        =\page_grd_command_set_float_hsize,
604 % \s!page_otr_command_flush_float_box        =\page_grd_command_flush_float_box,
605   \s!page_otr_command_synchronize_side_floats=\page_grd_command_synchronize_side_floats,
606   \s!page_otr_command_side_float_output      =\page_grd_command_side_float_output,
607   \s!page_otr_command_flush_floats           =\page_grd_command_flush_floats,
608   \s!page_otr_command_flush_side_floats      =\page_grd_command_flush_side_floats,
609   \s!page_otr_command_flush_saved_floats     =\page_grd_command_flush_saved_floats,
610   \s!page_otr_command_flush_all_floats       =\page_grd_command_flush_all_floats,
611 % \s!page_otr_command_flush_margin_blocks    =\page_grd_command_flush_margin_blocks, % not used
612  ]
613
614% spans
615
616\installcorenamespace{pagegridspan}
617
618\installframedcommandhandler \??pagegridspan {pagegridspan} \??pagegridspan
619
620\setuppagegridspan
621  [\c!frame=\v!off,
622   \c!before=,
623   \c!after=,
624   \c!offset=\v!overlay,
625   \c!location=\v!left,
626   \c!linecorrection=\v!off,
627   \c!depthcorrection=\v!off,
628   \c!n=\plustwo,
629   \c!nlines=\zerocount,
630   \c!align=\v!normal,
631   \c!width=\d_page_grd_span_width,
632   \c!indenting=,
633   \c!indentnext=\v!yes,
634   \c!default=\v!here,
635   \c!alternative=\v!a]
636
637\newdimension\d_page_grd_span_width
638
639\let\page_grd_span_stop\relax
640
641\permanent\tolerant\protected\def\startpagegridspan[#1]#*[#2]#*[#3]% [#3] gobbles space
642  {\endgraf % else rubish output if forgotten
643   \synchronizepagegrid
644   \bgroup
645   \forgetall
646   \cdef\currentpagegridspan{#1}%
647   \clf_sethsizecolumnspan{\currentpagegrid}\pagegridspanparameter\c!n\relax
648   \setbox\scratchbox\hbox\bgroup\inheritedpagegridspanframed\bgroup
649     \def\page_grd_span_stop{\page_grd_span_stop_indeed{#2}}%
650     \usepagegridspanstyleandcolor\c!style\c!color
651     \pagegridspanparameter\c!before
652     \ignorespaces}
653
654\protected\def\page_grd_span_stop_indeed#1%
655  {\removeunwantedspaces
656   \par
657   \verticalstrut
658   \kern-2\struttotal
659   \verticalstrut
660   \endgraf
661   \pagegridspanparameter\c!after
662   \egroup\egroup
663   \setpagegrid[#1]{\box\scratchbox}%
664   % todo: push into slot
665   \egroup
666   \endgraf}
667
668\permanent\protected\def\stoppagegridspan % indirectness permits aliasing
669  {\page_grd_span_stop}
670
671\permanent\def\pagegridspanwidth#1% assumes equal distances
672  {\the\dimexpr
673     #1\d_page_grd_column_width
674    +#1\d_page_grd_distance
675    -  \d_page_grd_distance
676   \relax}
677
678% areas
679
680\installcorenamespace{pagegridarea}
681
682\installframedcommandhandler \??pagegridarea {pagegridarea} \??pagegridarea
683
684\setuppagegridarea
685  [\c!x=\plusone,
686   \c!y=\plusone,
687   \c!nx=\plusone,
688   \c!ny=\plusone,
689   \c!clipoffset=2\lineheight,
690   \c!leftoffset=\zeropoint,
691   \c!rightoffset=\zeropoint,
692   \c!offset=\v!overlay,
693   \c!strut=\v!no,
694   \c!frame=\v!off,
695  %\c!type=\v!next,
696   \c!align=\v!normal,
697   \c!page=\plusone,
698   \c!state=\v!stop]
699
700% type: both fixed left right next (not now), then better
701% lefttext and righttext or so
702
703\appendtoks
704   % \edef\p_type{}%
705   % \ifx\p_type\v!next
706   %    \doifelseoddpage
707   %        {\letpagegridareaparameter\c!type\v!right}%
708   %        {\letpagegridareaparameter\c!type\v!left}%
709   % \fi
710     \clf_registercolumnsetarea {
711        name  {\currentpagegridarea}
712      % type  {\p_type}
713      % page  \pagegridareaparameter\c!page
714        state {\pagegridareaparameter\c!state}
715        c     \pagegridareaparameter\c!x
716        r     \pagegridareaparameter\c!y
717        nc    \pagegridareaparameter\c!nx
718        nr    \pagegridareaparameter\c!ny
719     }%
720\to \everydefinepagegridarea
721
722\permanent\tolerant\protected\def\setuppagegridareatext[#1]#*[#2]%
723  {\cdef\currentpagegridarea{#1}%
724   \setpagegridareaparameter\c!text{#2}}
725
726% maybe move the left/right correction to the tex end or the offset to lua
727
728% used nowhere:
729
730\permanent\protected\def\setpagegridarea#1#2#3#4#5#6#7#8% can be optimized
731  {\begingroup
732   \cdef\currentpagegridarea{#2}%
733   \setpagegridareaparameter\c!width {#5\scaledpoint}%
734   \setpagegridareaparameter\c!height{#6\scaledpoint}%
735   \setbox\nextbox\hpack\bgroup\inheritedpagegridareaframed\bgroup
736     \usepagegridareastyleandcolor\c!style\c!color
737     \ignorespaces
738     \pagegridareaparameter\c!text
739   \egroup\egroup
740   %
741   \scratchdimen#8\scaledpoint
742   \ifdim\scratchdimen>\zeropoint
743     \setbox\scratchbox\vbox\bgroup
744       \clip
745         [     \c!offset=\pagegridareaparameter\c!clipoffset,%
746          \c!rightoffset=\pagegridareaparameter\c!rightoffset,%
747                \c!width=\scratchdimen,%
748             % \c!height=
749         ]%
750         {\copy\nextbox}%
751     \egroup
752     \clf_setcolumnsetarea{name {#1} box \scratchbox c #3 r #4}%
753     \setbox\scratchbox\vbox\bgroup
754       \hskip-\layoutparameter\c!backspace % todo: #9
755       \clip
756         [    \c!offset=\pagegridareaparameter\c!clipoffset,%
757          \c!leftoffset=\pagegridareaparameter\c!rightoffset,%
758             \c!hoffset=\scratchdimen,%
759               \c!width=\dimexpr\wd\nextbox-\scratchdimen\relax,%
760            % \c!height=
761         ]%
762         {\box\nextbox}%
763     \egroup
764     \clf_setcolumnsetarea{name {#1} box \scratchbox c #7 r #4}%
765   \else
766     \setbox\scratchbox\vbox\bgroup
767        \box\nextbox % wrapping needed
768     \egroup
769     \clf_setcolumnsetarea{name {#1} box \scratchbox c #3 r #4}%
770   \fi
771   \endgroup}
772
773% state start | repeat
774
775%D The old one, for now:
776
777\aliased\let\definecolumnset       \definepagegrid
778\aliased\let\setupcolumnset        \setuppagegrid
779\aliased\let\setupcolumnsetlines   \setuppagegridlines
780\aliased\let\setupcolumnsetstart   \setuppagegridstart
781\aliased\let\startcolumnset        \startpagegrid
782\aliased\let\stopcolumnset         \stoppagegrid
783\aliased\let\definecolumnsetspan   \definepagegridspan
784\aliased\let\setupcolumnsetspan    \setuppagegridspan
785\aliased\let\startcolumnsetspan    \startpagegridspan
786\aliased\let\stopcolumnsetspan     \stoppagegridspan
787\aliased\let\columnsetspanwidth    \pagegridspanwidth
788\aliased\let\definecolumnsetarea   \definepagegridarea
789\aliased\let\setupcolumnsetarea    \setuppagegridarea
790\aliased\let\setupcolumnsetareatext\setuppagegridareatext
791
792%D It ends here.
793
794\protect \endinput
795