%D \module
%D   [       file=page-flt,
%D        version=2000.10.20,
%D          title=\CONTEXT\ OTR Macros,
%D       subtitle=Floating Bodies,
%D         author=Hans Hagen,
%D           date=\currentdate,
%D      copyright={PRAGMA / Hans Hagen \& Ton Otten}]
%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 OTR Macros / Floating Bodies}

%D This module is currently a mess, due to splitting float
%D handlers over specific otr's. Consider it work in progress.

%D Some of the sidefloat settings should move to page-sid; now it's quite
%D fuzzy the way the variables are set/reset.

\unprotect

% naar supp-box.tex

\def\voidbox{\box\voidb@x}

\def\spreadhbox#1% rebuilds \hbox{<box><hss><box><hss><box>}
  {\bgroup
   \ifhbox#1\relax
     \setbox2\voidbox
     \unhbox#1%
     \doloop
       {\unpenalty\unskip\unpenalty\unskip\unpenalty\unskip
        \setbox0\lastbox
        \ifvoid0
          \exitloop
        \else
          \setbox2\hbox
            {\ifhbox0 \spreadhbox0\else\box0\fi
             \ifvoid2 \else\hss\unhbox2\fi}%
        \fi}%
     \ifvoid2\else\unhbox2\fi
   \else
     \box#1%
   \fi
   \egroup}

\def\placefloats{\doflushfloats}  % keep this one

\startmessages  dutch  library: floatblocks
  title: plaatsblokken
      1: -- hernummerd / -- => --
      2: -- bewaard
      3: -- verplaatst
      4: -- geplaatst
      5: volgorde aangepast
      6: maximaal -- boven
      7: maximaal -- onder
      8: minder dan -- regels
      9: volgorde verstoord
     10: -- begrensd
     11: geen blok opgegeven
     12: niet gedefinieerd
\stopmessages

\startmessages  english  library: floatblocks
 title: floatblocks
      1: -- renumbered / -- => --
      2: -- saved
      3: -- moved
      4: -- placed
      5: order adapted
      6: n of top floats limited to --
      7: n of bottom floats limited to --
      8: less than -- lines
      9: order disturbed
     10: -- limited
     11: no block given
     12: undefined
\stopmessages

\startmessages  german  library: floatblocks
 title: Gleitobjektbloecke
      1: -- neu nummeriert / -- => --
      2: -- gespeichert
      3: -- verschoben
      4: -- plaziert
      5: Reihenfolge angepasst
      6: Anz. der oberen Gleitobjekte beschraengt auf --
      7: Anz. der unteren Gleitobjekte beschraengt auf  --
      8: weniger als -- zeilen
      9: Reigenfolge gestoert
     10: -- begrenzt
     11: kein Block gegeben
     12: undefiniert
\stopmessages

\startmessages  czech  library: floatblocks
 title: plovouciobjekty
      1: -- precislovano / -- => --
      2: -- ulozeno
      3: -- presunuto
      4: -- umisteno
      5: poradi prizpusobeno
      6: pocet hornich plovoucich objektu je omezen na --
      7: pocet spodnich plovoucich objektu je omezen na --
      8: radku je mene nez --
      9: poradi naruseno
     10: -- omezeno
     11: nedan zadny blok
     12: nedefinovano
\stopmessages

\startmessages  italian  library: floatblocks
 title: oggetti mobili
      1: -- rinumerato / -- => --
      2: -- salvato
      3: -- mosso
      4: -- sistemato
      5: ordine aggiustato
      6: n di top floats limitato a --
      7: n di bottom floats limitato a --
      8: meno di -- righe
      9: ordine disturbato
     10: -- limitato
     11: nessun oggetto specificato
     12: non definito
\stopmessages

\startmessages  norwegian  library: floatblocks
 title: flytblokker
      1: -- renummerert / -- => --
      2: -- lagret
      3: -- flyttet
      4: -- plassert
      5: rekkefølge tilpasset
      6: maksimalt -- flytblokker øverst
      7: maksimalt -- flytblokker nederst
      8: mindre enn -- linjer
      9: rekkefølge endret
     10: -- begrenset
     11: ingen blokk oppgitt
     12: udefinert
\stopmessages

\startmessages  romanian  library: floatblocks
 title: Blocuri
      1: -- renumerotat / -- => --
      2: -- salvat
      3: -- mutat
      4: -- plasat
      5: ordinea adaptata
      6: nr. cadrelor de sus limitat la  --
      7: nr. blocurilor de jos limitat la --
      8: mai putin de -- linii
      9: ordinea deranjata
     10: -- limitat
     11: nu este dat nici un bloc
     12: nedefinit
\stopmessages

\def\setupfloats
  {\dodoubleargument\getparameters[\??bk]}

\def\setupcaptions
  {\dodoubleargument\getparameters[\??kj]}

\def\dosetupfloat[#1][#2]%
  {\def\docommando##1{\getparameters[\??fl##1][#2]}%
   \processcommalist[#1]\docommando}

\def\setupfloat
  {\dodoubleargument\dosetupfloat}

\def\dosetupcaption[#1][#2]%
  {\def\docommando##1{\getparameters[\??kj##1][#2]}%
   \processcommalist[#1]\docommando}

\def\setupcaption
  {\dodoubleargument\dosetupcaption}

\def\doemptyblock#1%
  {\localframed
     [\??fl#1][\c!frame=\v!on]%
     {\getmessage\m!floatblocks{12}}}

% A complication is that we may have to handle a pagebreak
% first, which in turn may issue a (postponed) float.
% Therefore we may not trust on variable assignments before
% we're realy dealing with the float. Some day I'll root out
% the global settings.

\def\docomplexplaatsblok[#1][#2]% [#3]#4%
  {\edef\floattype{#1}%
   \doifelsenothing\floattype
     {\let\floattype\v!figure}
     {\doifundefined{\??fl#1\c!default}{\let\floattype\v!figure}}%
   \doifelsenothing{#2}
     {\edef\floatlocation{\getvalue{\??fl\floattype\c!default}}}
     {\edef\floatlocation{#2}}%
   \expanded{\dodocomplexplaatsblok[\floattype][\floatlocation]}}

\def\dodocomplexplaatsblok[#1][#2][#3]#4%
  {\flushnotes
\flushsidefloats % here !
   \ifsomefloatwaiting
     % this was \checkwaitingfloats spread all over
     \doifinsetelse\v!always{#2}
       {\showmessage\m!floatblocks5\empty}
       {\doifcommonelse
          {#2}
          {\v!left,\v!right,\v!inner,\v!outer,%
           \v!backspace,\v!cutspace,%
           \v!inleft,\v!inright,\v!inmargin,%
           \v!leftmargin,\v!rightmargin,\v!leftedge,\v!rightedge,%
           \v!innermargin,\v!outermargin,\v!inneredge,\v!outeredge,%
           \v!text,\v!opposite}% \v!page
          {\doflushfloats}
          {}}%
     % but which should be done before using box \floatbox
   \fi
   \ifmargeblokken % waarschijnlijk gebroken ! ! ! !
     \doifinset\v!margin{#2}
       {\endgraf
        \bgroup\everypar{\egroup\the\everypar}%
        \hsize\@@mbwidth}%
   \fi
   \global\insidefloattrue
   \begingroup % **
   \the\everyinsidefloat
   \let\@@extrafloat\empty
   \presetmorefloatvariables{#2}%
   \dowithnextboxcontent % better a \the\everyfloattoks
     {\setlocalfloathsize
      \getvalue{\??fl#1\c!inner}%
      \fuzzysnappingfalse
      \postponefootnotes} % new
     {\xdocompletefloat{#1}{#3}{#1}{#2}{#1}{#4}% ** not yet done
       % we need to carry over the par because of side floats
      \doifnotinset\v!text{#2}{\carryoverpar\endgroup}%
      \global\sidefloatdownshift \zeropoint
      \global\sidefloatextrashift\zeropoint
      \ifparfloat
        \doifinset\v!reset{#2}\forgetsidefloats
        \doinhibitblank
      \fi}% better move this to side floats
     \vbox}

\def\xxdocompletefloat#1#2%
  {\rightorleftpageaction{\let\@@extrafloat#1}{\let\@@extrafloat#2}}

\chardef\textfloatmethod=0 % 0=raw 1=safe (.99) 2=tight (-1pt)
\chardef\sidefloatmethod=1 % 0=raw 1=safe (.99) 2=tight (-1pt)

\let\floatrotation\!!zerocount

\def\presetfloatvariables#1#2#3#4%
  {\doifcommonelse
     {#2}
     {\v!left,\v!right,\v!inner,\v!outer,%
      \v!inleft,\v!inright,\v!inmargin,%
      \v!backspace,\v!cutspace,%
      \v!innermargin,\v!outermargin,\v!inneredge,\v!outeredge,%
      \v!leftmargin,\v!leftedge,\v!rightmargin,\v!rightedge}
     {\global\parfloattrue}
     {\global\parfloatfalse}%
   \ifinsidecolumns
     \global\parfloatfalse
   \fi
   \global\sidefloatshift\zeropoint
   \global\sidefloatmaximum\zeropoint
   \global\chardef\sidefloatmethod\getvalue{\??fl#1\c!sidemethod}%
   \global\chardef\textfloatmethod\getvalue{\??fl#1\c!textmethod}%
   \global\chardef\sidefloatalign\zerocount
   \globallet\floatrotation\!!zerocount
   \calculatefloatskips{#1}%
   \ifparfloat
     \processaction
       [\getvalue{\??fl#1\c!sidealign}]
       [\v!height=>\global\chardef\sidefloatalign\plusone,%
          \v!line=>\global\chardef\sidefloatalign\plustwo,% (***)
         \v!depth=>\global\chardef\sidefloatalign\plusthree,%
          \v!grid=>\global\chardef\sidefloatalign4,%
      \v!halfline=>\global\chardef\sidefloatalign5]%
% todo (test first): \doifinset\v!lokaal{#2}{\chardef\sidefloatalign\zerocount}%
     \ifcase\sidefloatalign\relax % todo: optie v!lokaal => \else
       \doifinset\v!height  {#2}{\global\chardef\sidefloatalign\plusone}%
       \doifinset\v!line    {#2}{\global\chardef\sidefloatalign\plustwo}%
       \doifinset\v!depth   {#2}{\global\chardef\sidefloatalign\plusthree}%
       \doifinset\v!grid    {#2}{\global\chardef\sidefloatalign4}%
       \doifinset\v!halfline{#2}{\global\chardef\sidefloatalign5}% meant for 'none'
     \fi
     \doifinset\v!high{#2}{\global\sidefloattopskip   \zeropoint}%
     \doifinset\v!low {#2}{\global\sidefloatbottomskip\zeropoint}%
     \doifinset\v!fit {#2}
       {\global\sidefloattopskip   \zeropoint
        \global\sidefloatbottomskip\zeropoint
        \global\floatsideskip      \zeropoint}%
   \else
     \processallactionsinset
       [#2]
       [ 90=>\globallet\floatrotation\commalistelement,%
        180=>\globallet\floatrotation\commalistelement,%
        270=>\globallet\floatrotation\commalistelement]%
   \fi
   \doifinsetelse\v!nonumber{#2}
     {\global\nofloatnumbertrue}
     {\doifelsevalue{\??kj#1\c!number}\v!yes
        {\global\nofloatnumberfalse}
        {\global\nofloatnumbertrue}}%
   \ConvertToConstant\doifelse{#4}{}
     {\global\emptyfloatcaptiontrue}
     {\global\emptyfloatcaptionfalse}%
   \doifinsetelse\v!none{#2}
     {\global\nofloatcaptiontrue}
     {\ConvertToConstant\doifelse{#4}\v!none
        {\global\nofloatcaptiontrue}
        {\global\nofloatcaptionfalse}}%
   \ifemptyfloatcaption \ifnofloatnumber
     \global\nofloatcaptiontrue
   \fi \fi}

% documenteren in details

\def\presetmorefloatvariables#1%
  {\doifelse\@@bklocal\v!yes % fout keyword
     \globalcenterfloatboxtrue
     \globalcenterfloatboxfalse
   \ifglobalcenterfloatbox
     \localcenterfloatboxtrue
   \else
     \doifinsetelse\v!local{#1}
       \localcenterfloatboxtrue
       \localcenterfloatboxfalse
   \fi
   \doifnotcommon{\v!always,\v!here,\v!force}{#1} % ! ! ! ! ! !
     {\globalcenterfloatboxfalse
      \localcenterfloatboxfalse}}

\def\setlocalfloathsize
  {\iflocalcenterfloatbox
     \seteffectivehsize
     \hsize\localhsize
   \fi}

\newevery \everyinsidefloat \relax

\appendtoks
  \everyinsidefloat\emptytoks % in case it's called earlier
  \dogetfloatdata
\to \everyinsidefloat

%\appendtoks
%  \fuzzysnappingfalse
%\to \everyinsidefloat

%\def\doifrightpagefloatelse % watch out: other default ! ! !
%  {\ifdubbelzijdig
%     \ifodd\purenumber\twopassfloatdata\space
%       \@EAEAEA\firstoftwoarguments
%     \else
%       \@EAEAEA\secondoftwoarguments
%     \fi
%   \else
%     \@EA\firstoftwoarguments
%   \fi}

\def\doifrightpagefloatelse
  {\ifdubbelzijdig
     \ifenkelzijdig
       \@EAEAEA\firstoftwoarguments
     \else
       \@EAEAEA\doifoddfloatpageelse
     \fi
   \else
     \@EA\firstoftwoarguments
   \fi}

\def\doifoddfloatpageelse
  {\ifodd\purenumber\twopassfloatdata\space
     \@EA\firstoftwoarguments
   \else
     \@EA\secondoftwoarguments
   \fi}

\appendtoks
  \let\rightorleftpageaction\doifrightpagefloatelse
\to \everyinsidefloat

\newif\ifextrafloatactions \extrafloatactionstrue

% \let\movesidefloat\gobbleoneargument

% new : \place...[leftmargin,-2*line]; we need to catch fxtb:2*3
% watch out: line alone aligns on the line ! ! !

\def\movesidefloat[#1]% (-)line|x=,y=
  {\global\sidefloatdownshift \zeropoint
   \global\sidefloatextrashift\zeropoint
   \doifassignmentelse{#1}%
     {\bgroup
      \getparameters[\??fl][\c!x=\zeropoint,\c!y=\zeropoint,#1]%
      \ifgridsnapping
        \getnoflines\@@fly
        \global\sidefloatdownshift\noflines\lineheight
      \else
        \global\sidefloatdownshift\@@fly
      \fi
      \global\sidefloatextrashift\@@flx
      \egroup}
     {\movedownsidefloat[#1]}}

\def\movedownsidefloat[#1]% already in core
  {\bgroup
   \cleanupfeatures
   \doifinstringelse{:}{#1}
     \donothing
     {\donefalse
      \def\movedownsidefloat##1%
        {\processaction
           [##1]%
           [ \v!line=>\docommando+,%
            +\v!line=>\docommando+,%
            -\v!line=>\docommando-]}%
      \def\docommando##1%
        {\ifdone\else\global\sidefloatdownshift\zeropoint\donetrue\fi
         \global\advance\sidefloatdownshift##1\lineheight}%
      \expanded{\dorepeatwithcommand[#1]}\movedownsidefloat}%
   \egroup}

\def\hangsidefloat[#1]%
  {\global\sidefloatsidelines#1\relax}

\def\xdocompletefloat#1#2#3#4#5#6%
  {\ifextrafloatactions
     \doifinsetelse\v!text{#4}
       {% fuzzy, text overloads left, since then it's a directive
        \docompletefloat{#1}{#2}{#3}{#4}{#5}{#6}\nextbox}
       {\let\@@extrafloat\empty
        % \sidefloatdownshift will be reset afterwards, and can
        % already be set at this point
        \processallactionsinset
          [#4] % ininner/inouter : for old times sake
          [     \v!inner=>\xxdocompletefloat\v!left       \v!right,
                \v!outer=>\xxdocompletefloat\v!right      \v!left,
           \v!innermargin=>\xxdocompletefloat\v!leftmargin \v!rightmargin,
           \v!outermargin=>\xxdocompletefloat\v!rightmargin\v!leftmargin,
            \v!inneredge=>\xxdocompletefloat\v!leftedge  \v!rightedge,
            \v!outeredge=>\xxdocompletefloat\v!rightedge \v!leftedge,
                \v!backspace=>\xxdocompletefloat\v!backspace      \v!cutspace,
               \v!cutspace=>\xxdocompletefloat\v!cutspace     \v!backspace,
                 \v!left=>\xxdocompletefloat\v!left       \v!left,
                \v!right=>\xxdocompletefloat\v!right      \v!right,
                 \v!line=>, % only -n*line is handled (see ***)
               \s!unknown=>{\movedownsidefloat[\commalistelement]}]%
        \ifx\@@extrafloat\empty
          \docompletefloat{#1}{#2}{#3}{#4}{#5}{#6}\nextbox
        \else
          \docompletefloat{#1}{#2}{#3}{\@@extrafloat,#4}{#5}{#6}\nextbox
        \fi}%
   \else % downward compatible
     \docompletefloat{#1}{#2}{#3}{#4}{#5}{#6}\nextbox
   \fi}

% pas op, maxbreedte niet instellen als plaats=links/rechts

\def\setlocalfloatdimensions#1#2#3#4% experimental / #3 box number #4 prefix
  {\global\sidefloatshift  \zeropoint       % duplicate
   \global\sidefloatmaximum\zeropoint\relax % duplicate
   \ifextrafloatactions
     \ifdim\sidefloatdownshift=\zeropoint\else
       #4\setbox#3\vbox
        {\vskip\sidefloatdownshift\nointerlineskip\box#3}%
     \fi
     \doifvaluesomething{\??fl#1\c!minwidth}
       {\scratchdimen\getvalue{\??fl#1\c!minwidth}\relax
        \ifdim\wd#3<\scratchdimen
          #4\setbox#3\hbox to \scratchdimen
            {\doifnotvalue{\??fl#1\c!location}\v!left \hss
             \box#3%
             \doifnotvalue{\??fl#1\c!location}\v!right\hss}%
        \fi}%
     % todo: rand / rug
     \doifinset\v!hanging{#2}
       {\doifcommonelse{\v!inleft,\v!leftmargin}{#2}
          {\letvalue{\??fl#1\c!maxwidth}\leftmarginwidth}%
          {\doifcommon{\v!inright,\v!rightmargin}{#2}
             {\letvalue{\??fl#1\c!maxwidth}\rightmarginwidth}}}%
     \doifvaluesomething{\??fl#1\c!maxwidth}
       {\scratchdimen\getvalue{\??fl#1\c!maxwidth}\relax
        \ifdim\wd#3>\scratchdimen
          \doifcommonelse{\v!inright,\v!rightmargin,\v!rightedge
                          \v!inleft,\v!leftmargin,\v!leftedge}{#2}
            {\global\sidefloatmaximum\scratchdimen}
            {#4\setbox#3\hbox to \scratchdimen
              {\doifcommonelse{\v!right,\v!left}{#2}
                 {\doifnotinset\v!right{#2}\hss
                  \box#3%
                  \doifnotinset\v!left{#2}\hss}%
                 {\doifnotvalue{\??fl#1\c!location}\v!left\hss
                  \box#3%
                  \doifnotvalue{\??fl#1\c!location}\v!right\hss}}}%
        \fi}%
  \fi}

\def\docomplexstarttextblock[#1][#2][#3]%
  {\flushnotes
   \flushsidefloats % hoort eigenlijk niet hier
   \docomplexplaatsblok[#1][\v!text,#2,\v!left][#3]}

\def\docomplexreserveblock[#1][#2][#3][#4]#5%
  {\getvalue{\e!place#1}[#3][#4]{#5}{\localframed[\??fl#1][#2]{#1}}}

\def\docomplexstartreservetextblock[#1][#2][#3][#4]%
  {\flushsidefloats % hoort eigenlijk niet hier
   \docomplexreserveblock[#1][#2][\v!text,#3,\v!left][#4]}

\def\definefloat
  {\dotripleempty\dodefinefloat}

\def\dodefinefloat[#1][#2][#3]% #1=naam #2=meervoud #3=parent
  {\ifthirdargument
     \redodefinieerplaatsblok[#1][#2][#3]%
   \else\ifsecondargument
     \dododefinefloat[#1][#2]%
   \else
     \dododefinefloat[#1][#1]%
   \fi\fi}

\def\dododefinefloat[#1][#2]%
  {\presetlocalframed[\??fl#1]%
   \setupfloat
     [#1]
     [\c!width=8\lineheight, % 15\korpsgrootte,
      \c!height=6\lineheight,  % 10\korpsgrootte,
      \c!minwidth=,
      \c!maxwidth=,
      \c!maxheight=,
      \c!criterium=,
      \c!sidespacebefore=\@@bksidespacebefore,
      \c!sidespaceafter=\@@bksidespaceafter,
      \c!sidealign=\@@bksidealign, % \v!line
      \c!margin=\@@bkmargin,
      \c!leftmargindistance=\@@bkleftmargindistance,
      \c!rightmargindistance=\@@bkrightmargindistance,
      \c!frame=\@@bkframe,
      \c!radius=\@@bkradius,
      \c!corner=\@@bkcorner,
      \c!location=\@@bklocation,
      \c!background=\@@bkbackground,
      \c!backgroundscreen=\@@bkbackgroundscreen,
      \c!backgroundcolor=\@@bkbackgroundcolor,
      \c!backgroundoffset=\@@bkbackgroundoffset,
      \c!topframe=\@@bktopframe,
      \c!bottomframe=\@@bkbottomframe,
      \c!leftframe=\@@bkleftframe,
      \c!rightframe=\@@bkrightframe,
      \c!frameoffset=\@@bkframeoffset,
     %\c!local=\@@bklocal,
      \c!pageboundaries=,
      \c!textmethod=\@@bktextmethod,
      \c!sidemethod=\@@bksidemethod,
      \c!default=]%
   \setupcaption
     [#1]
     [\c!location=\@@kjlocation,
      \c!grid=\@@kjgrid,
     %\c!before=\@@kjbefore,
      \c!inbetween=\@@kjinbetween,
     %\c!after=\@@kjafter,
      \c!width=\@@kjwidth,
      \c!minwidth=\@@kjminwidth,
      \c!headstyle=\@@kjheadstyle,
      \c!headcolor=\@@kjheadcolor,
      \c!textstyle=\@@kjtextstyle,
      \c!textcolor=\@@kjtextcolor,
      \c!style=\@@kjstyle,
      \c!color=\@@kjcolor,
      \c!align=\@@kjalign,
      \c!number=\@@kjnumber,
      \c!way=\@@kjway,
      \c!blockway=\@@kjblockway,
      \c!sectionnumber=\@@kjsectionnumber,
      \c!distance=\@@kjdistance,
      \c!separator=\@@kjseparator,
      \c!command=\@@kjcommand,
      \c!conversion=\@@kjconversion]%
   \definenumber % \definelabel
     [#1]
     [\c!text=#1,
      \c!location=\v!intext,
      \c!way=\getvalue{\??kj#1\c!way},
      \c!blockway=\getvalue{\??kj#1\c!blockway},
      \c!sectionnumber=\getvalue{\??kj#1\c!sectionnumber},
      \c!conversion=\getvalue{\??kj#1\c!conversion}]%
   \presetlabeltext[#1=\Word{#1}~]%
   \dodefinefloatcommands[#1][#2]}

\def\dodefinefloatcommands[#1][#2]%
  {\definelist[#1]%
   \presetheadtext[#2=\Word{#2}]%
   \setvalue        {\e!place\e!listof#2}{\dodoubleempty\doplacelist[#1]}%
   \setvalue     {\e!complete\e!listof#2}{\dotripleempty\dodocompletelist[#1][#2]}%
   \setvalue                   {\e!place#1}{\dotripleempty\docomplexplaatsblok[#1]}%
   \setvalue                {\e!reserve#1}{\doquadrupleempty\docomplexreserveblock[#1]}%
   \setvalue            {\e!start#1\e!text}{\dotripleempty\docomplexstarttextblock[#1]}%
   \setvalue             {\e!stop#1\e!text}{\dostoptextfloat}%
   \setvalue{\e!start\e!reserve#1\e!text}{\doquadrupleempty\docomplexstartreservetextblock[#1]}%
   \setvalue {\e!stop\e!reserve#1\e!text}{\dostoptextfloat}%
   \setvalue                 {\e!emptyone#1}{\doemptyblock{#1}}%
   \setvalue                 {\e!emptytwo#1}{\doemptyblock{#1}}}

% \setupfloat[...][leftmargindistance=1cm,default={left,none}]

\def\redodefinieerplaatsblok[#1][#2][#3]% same label/number
  {\presetlocalframed[\??fl#1]%
   \copylocalframed[\??fl#1][\??fl#3]%
   \copyparameters[\??fl#1][\??fl#3]
     [\c!width,\c!height,%\c!local,
      \c!maxwidth,\c!maxheight,\c!minwidth,
      \c!margin,\c!sidespacebefore,\c!sidespaceafter,\c!sidealign,
      \c!leftmargindistance,\c!rightmargindistance,\c!criterium,
      \c!frame,\c!radius,\c!corner,\c!location,\c!background,\c!framecolor,
      \c!backgroundscreen,\c!backgroundcolor,\c!backgroundoffset,
      \c!topframe,\c!bottomframe,\c!leftframe,\c!rightframe,
      \c!frameoffset,\c!pageboundaries,\c!default,
      \c!textmethod,\c!sidemethod]%
   \copyparameters[\??kj#1][\??kj#3]
     [\c!location,\c!before,\c!inbetween,\c!after,
      \c!width,\c!headstyle,\c!headcolor,\c!style,\c!color,
      \c!textstyle,\c!textcolor,\c!minwidth,
      \c!align,\c!number,\c!way,\c!blockway,
      \c!sectionnumber,\c!separator,\c!distance,\c!conversion]%
   \definenumber[#1][#3]%
   \presetlabeltext[#1=\labeltext{#3}]%
   \dodefinefloatcommands[#1][#2]}

\def\placefloat % \plaatsplaatsblok
  {\dotripleempty\docomplexplaatsblok}

\installinsertion\topins \newdimen\topinserted
\installinsertion\botins \newdimen\botinserted

%D Extra float registers.

\newif\ifsomefloatwaiting     \somefloatwaitingfalse
\newif\ifroomforfloat         \roomforfloattrue
\newif\ifnofloatpermitted     \nofloatpermittedfalse

\newcount\totalnoffloats      \totalnoffloats =0
\newcount\savednoffloats      \savednoffloats =0
\newcount\noffloatinserts     \noffloatinserts=0

\newbox\floatlist
\newbox\savedfloatlist

\newif\ifflushingfloats \flushingfloatsfalse

\newbox\floattext

\newdimen\floattextwidth
\newdimen\floattextheight

\newbox\floatbox
\newbox\savedfloatbox

\newdimen\floatwidth
\newdimen\floatheight

% the tricky part of getting float related two pass data is
% that we should fetch is early but can only save it with
% the composed float box; this determines the order: get it
% before saving it

\definetwopasslist{\s!float\s!data} \newcounter\noffloatdata

\let\twopassfloatdata\realpageno

\def\dosavefloatdata
  {\bgroup
   \edef\dosavefloatdata
     {\writeutilitycommand
        {\twopassentry
           {\s!float\s!data}%
           {\noffloatdata}%
           {\noffloatpages::\noexpand\realfolio}}}% later {}{}{}{} and \getfirst...
   \dosavefloatdata
   \egroup}

\def\dogetfloatdata % precedes save !
  {\doglobal\increment\noffloatpages
   \findtwopassdata{\s!float\s!data}{\noffloatpages::}%
   \iftwopassdatafound
     \globallet\twopassfloatdata\twopassdata
   \else
     \globallet\twopassfloatdata\realpageno % \realfolio
   \fi}

% Er wordt bij \v!altijd als dat nodig is hernummerd.
% Daarbij wordt gebruik gemaakt van de opgeslagen nummers en
% volgorde.

\definetwopasslist\s!float

\def\dofloatreference
  {\doglobal\increment\numberedfloat
   \edef\dodofloatreference
     {\writeutilitycommand
        {\twopassentry
           {\s!float}%
           {\numberedfloat}%
           {\hetnummer}}}%
   \dodofloatreference}

\def\redofloatorder#1%
  {\doglobal\increment\nofplacedfloats\relax
   \gettwopassdata\s!float
   \iftwopassdatafound
     \doifnot\hetnummer\twopassdata
       {\edef\oldhetnummer{\hetnummer}%
        \xdef\hetnummer{\twopassdata}%
        \showmessage\m!floatblocks1{\nofplacedfloats,#1 \oldhetnummer,\hetnummer}}%
   \fi}

% In \dofloatinfomessage wordt {{ }} gebruikt omdat anders
% binnen \startpostponing...\stoppostponing geen goede
% melding in de marge volgt: \ifinner is dan namelijk true.

\def\dofloatinfomessage#1#2#3%
  {\bgroup
   \showmessage\m!floatblocks{#2}{#3}%
   \@EA\floatinfo\@EA#1\@EA{\currentmessagetext}%
   \egroup}

\def\dosavefloatinfo
  {\dofloatinfomessage>2{\the\totalnoffloats}}

\def\dofloatflushedinfo
  {\bgroup
   \!!counta\totalnoffloats
   \advance\!!counta -\savednoffloats
   \dofloatinfomessage<3{\the\!!counta}%
   \egroup}

\def\doinsertfloatinfo
  {\dofloatinfomessage<4{\the\totalnoffloats}}

\def\dogetfloat
  {\ifsomefloatwaiting
     \global\setbox\floatlist\vbox
       {\unvbox\floatlist
        \global\setbox\globalscratchbox\lastbox}%
     \ifcenterfloatbox
       \ifdim\wd\globalscratchbox<\hsize
         \setbox\floatbox\hbox to \hsize{\hss\box\globalscratchbox\hss}%
       \else
         \setbox\floatbox\box\globalscratchbox % local !
       \fi
     \else
       \setbox\floatbox\box\globalscratchbox % local !
     \fi
     \global\advance\savednoffloats \minusone
     \ifcase\savednoffloats
       \global\somefloatwaitingfalse
     \fi
   \else
     \global\savednoffloats\zerocount
     \global\setbox\floatbox\box\voidb@x
   \fi}

\def\uncenteredfloatbox
  {\ifcenterfloatbox
     \ifhbox\floatbox\relax % remove centering
       \ifdim\wd\floatbox=\hsize
         \ifhbox\floatbox
           \setbox\scratchbox\hbox
             {\unhbox\floatbox
              \unskip\unskip
              \global\setbox\globalscratchbox\lastbox}%
           \box\globalscratchbox
         \else
           \box\floatbox
         \fi
       \else
         \box\floatbox
       \fi
     \else
       \box\floatbox
     \fi
   \else
     \box\floatbox
   \fi}

\def\dosavefloat
  {\global\setbox\floatlist\vbox
     {\nointerlineskip
      \uncenteredfloatbox
      \unvbox\floatlist}%
   \global\advance\savednoffloats \plusone
   \global\somefloatwaitingtrue
   \dosavefloatinfo
   \nonoindentation}

\def\doresavefloat
  {\global\setbox\floatlist\vbox
     {\nointerlineskip
      \unvbox\floatlist
      \uncenteredfloatbox}%
   \global\advance\savednoffloats \plusone
   \global\somefloatwaitingtrue}

\def\doreversesavefloat
  {\global\setbox\floatlist\vbox
     {\nointerlineskip
      \unvbox\floatlist
      \uncenteredfloatbox}%
   \global\advance\savednoffloats \plusone
   \global\somefloatwaitingtrue
   \dosavefloatinfo}

\def\dosavefloatstatus
  {\global\setbox\savedfloatlist\copy\floatlist
   \global\setbox\savedfloatbox \copy\floatbox
   \xdef\dorestorefloatstatus
     {\global\setbox\floatlist\box\savedfloatlist
      \global\setbox\floatbox \box\savedfloatbox
      \global\savednoffloats\the\savednoffloats}}

\let\dorestorefloatstatus\relax

\ifx\doflushfloats\undefined \let\doflushfloats\relax \fi
\ifx\flushfloatbox\undefined \let\flushfloatbox\relax \fi

\newif\iftopofinsert
\newif\iftestfloatbox
\newif\ifcenterfloatbox       \centerfloatboxtrue
\newif\iflocalcenterfloatbox  \localcenterfloatboxfalse
\newif\ifglobalcenterfloatbox \globalcenterfloatboxfalse

% beter de laatste skip buiten de \insert uitvoeren,
% bovendien bij volle flush onder baseline.

\def\betweenfloatblanko% assumes that \@@bkspaceafter is present
  {\bgroup
   \setbox0\vbox{\strut\blank[\@@bkspacebefore]\strut}%
   \setbox2\vbox{\strut\blank[\@@bkspaceafter  ]\strut}%
   \ifdim\ht0>\ht2
     \blank[-\@@bkspaceafter,\@@bkspacebefore]
   \fi
   \egroup}

\def\doplacefloatbox
  {%\forgetall % NJET!
   \whitespace
   \blank[\@@bkspacebefore]
   \flushfloatbox
   \blank[\@@bkspaceafter]}

\ifx\someherefloat\undefined \let\someherefloat\doplacefloatbox \fi
\ifx\somefixdfloat\undefined \let\somefixdfloat\doplacefloatbox \fi
\ifx\somepagefloat\undefined \let\somepagefloat\doplacefloatbox \fi
\ifx\sometopsfloat\undefined \let\sometopsfloat\doplacefloatbox \fi
\ifx\somebotsfloat\undefined \let\somebotsfloat\doplacefloatbox \fi

\ifx\somesidefloat\undefined \let\somesidefloat\doplacefloatbox \fi
\ifx\somefacefloat\undefined \let\somefacefloat\doplacefloatbox \fi
\ifx\sometextfloat\undefined \let\sometextfloat\doplacefloatbox \fi

% brr, wordt deze niet overladen in page-one? weg er mee

\def\somepagefloat[#1]%  links, rechts, midden, hoog, midden, laag
  {%\checkwaitingfloats{#1}%
   \global\setbox\collectedpagefloats\vbox
     {\unvbox\collectedpagefloats
      \vbox to \textheight
        {\doifnotinset\v!high{#1}\vfill
         \box\floatbox
         \doifnotinset\v!low{#1}\vfill}%
      \goodbreak}%
   \doinsertfloatinfo}

\def\sometextfloat[#1]%  lang, links, rechts, hoog, midden, laag, offset
  {%\checkwaitingfloats{#1}%
   \gdef\dostoptextfloat{\dodostoptextfloat[#1]}% brr global
   \global\floattextwidth\hsize
   \global\floatwidth\wd\floatbox
   \global\floatheight\ht\floatbox % forget about the depth
   \global\advance\floattextwidth -\floatwidth
   \global\advance\floattextwidth -\@@bkmargin\relax % was \tfskipsize
   \doifinsetelse\v!tall{#1}
     {\floattextheight\pagegoal
      \advance\floattextheight -\pagetotal
      \advance\floattextheight -\bigskipamount     % lelijk
      \ifdim\floattextheight>\textheight
        \floattextheight\textheight
      \fi
      \boxmaxdepth\zeropoint \relax            % toegevoegd
      \ifdim\floattextheight<\floatheight
        \floattextheight\floatheight
      \fi
      \setbox\floattext\vbox to \floattextheight}
     {\setbox\floattext\vbox}%
   \bgroup
   \forgetall \setupblank \setupwhitespace % new, also needed for footnotes
   \blank[\v!disable]
   \hsize\floattextwidth
   \ignorespaces}

\def\dodostoptextfloat[#1]%  % de tekst kan beter in een soort
  {\egroup                   % kadertekst zonder kader, is flexibeler
   \doifnotinset\v!tall{#1}% en beter
     {\ifdim\ht\floattext<\floatheight
        \floattextheight\floatheight
      \else
        \floattextheight\ht\floattext
      \fi}%
   \setbox\floatbox\vbox to \floattextheight
     {\hsize\floatwidth
      \doifinsetelse\v!both{#1}%
        {\doifinsetelse\v!low{#1}
           {\vfill\box\floatbox}
           {\doifinsetelse\v!middle{#1}
              {\vfill\box\floatbox\vfill}
              {\box\floatbox\vfill}}}
        {\box\floatbox\vfill}}%
    \setbox\floattext\vbox to \floattextheight
     {\hsize\floattextwidth
      \doifinsetelse\v!low{#1}
        {\vfill
         \box\floattext
         \doifinset\c!offset{#1}{\whitespace\blank}}
        {\doifinsetelse\v!middle{#1}
           {\vfill
            \box\floattext
            \vfill}
           {\doifinset\v!offset{#1}{\whitespace\blank}%
            \box\floattext
            \vfill}}}%
   \doifinsetelse\v!right{#1}%
     {\setbox\floatbox\hbox to \hsize
        {\box\floattext
         \hfill
         \box\floatbox}}
     {\setbox\floatbox\hbox to \hsize
        {\box\floatbox
         \hfill
         \box\floattext}}%
   \baselinecorrection
   \whitespace
   \blank[\@@bkspacebefore]%
   \doifnotinset\v!tall{#1}%
     {\dp\floatbox\openstrutdepth}% dp\strutbox}%      % toegevoegd
   \box\floatbox
   \blank[\@@bkspaceafter]%
   \endgroup % **
   \doinsertfloatinfo}

\def\somefacefloat[#1]%  links, rechts, midden, hoog, midden, laag
  {%\checkwaitingfloats{#1}%
   \startopposite\box\floatbox\stopopposite
   \doinsertfloatinfo}

\def\someelsefloat[#1]%
  {\doifinsetelse\v!here{#1}
     {\doifinsetelse\v!always{#1}
        {\page[\v!preference]%
         \docheckiffloatfits
         \ifroomforfloat
           \placesomeherefloat[#1]%
         \else
           \showmessage\m!floatblocks9\empty
           \doreversesavefloat
         \fi}
        {\ifsomefloatwaiting
           \dosavefloat
         \else
           \page[\v!preference]%
           \docheckiffloatfits
           \ifroomforfloat
             \placesomeherefloat[#1]%
           \else
             \dosavefloat
           \fi
         \fi}}
     {\doifinsetelse\v!always{#1}
        {\docheckiffloatfits
         \ifroomforfloat
           \doifinsetelse\v!top{#1}
            {\placesometopsfloat[#1]}
            {\doifinsetelse\v!bottom{#1}
               {\placesomebotsfloat[#1]}
               {\placesomeherefloat[#1]}}%
         % more compact and efficient, but quite ugly
         % \doifinsetelse\v!top{#1}
         %   \placesometopsfloat
         %   {\doifinsetelse\v!bottom{#1}
         %      \placesomebotsfloat
         %      \placesomeherefloat}[#1]%
         \else
           \showmessage\m!floatblocks9\empty
           \doreversesavefloat
         \fi}
        {\docheckiffloatfits
         \ifroomforfloat
           \doifinsetelse\v!top{#1}
             {\placesometopsfloat[#1]}
             {\doifinsetelse\v!bottom{#1}
                {\placesomebotsfloat[#1]}
                {\placesomeherefloat[#1]}}%
         \else
           \dosavefloat
         \fi}}}

% De onderstaande macro wordt gebruikt bij de macros
% voor het plaatsen van tabellen en figuren (klopt niet
% meer).
%
% \dofloat         {plaats} {label1} {label2} {kader}
%
% \docompletefloat {nummer} {referentie} {lijst}
%                  {plaats} {label1} {label2} {inhoud}
%
% \box\floatbox    inhoud+referentie
%
% \do???float#1    #1 = boxnummer
%
% \ifinsidefloat   wordt \true gezet voor \docompletefloat en \false
%                  na float plaatsen; kan worden gebruikt om in
%                  andere commando's witruimte te onderdrukken

\newdimen\floatsideskip         \floatsideskip  =12pt
\newdimen\floattopskip          \floattopskip   =\floattopskip
\newdimen\floatbottomskip       \floatbottomskip=\floattopskip

\newdimen\sidefloattopskip      \sidefloattopskip   =\floattopskip
\newdimen\sidefloatbottomskip   \sidefloatbottomskip=\floatbottomskip

\newskip\sidefloatdownshift
\newskip\sidefloatleftshift
\newskip\sidefloatrightshift

\def\sidefloattopoffset         {\openstrutdepth} % {\strutdp}

\newcount\noftopfloats          \noftopfloats=2
\newcount\nofbotfloats          \nofbotfloats=0

\newif\ifnofloatcaption
\newif\ifnofloatnumber
\newif\ifemptyfloatcaption

\def\docalculatefloatskip#1#2%
  {\doifelsenothing{#2}
     {\global#1\zeropoint}
     {\doifelse{#2}\v!none
        {\global#1\zeropoint}
        {\setbox0\vbox{\whitespace\@EA\blank\@EA[#2]}%
         \global#1\ht0}}}

\def\calculatefloatskips#1%
  {{\docalculatefloatskip\floattopskip       \@@bkspacebefore
    \docalculatefloatskip\floatbottomskip    \@@bkspaceafter
    \docalculatefloatskip\sidefloattopskip   {\getvalue{\??fl#1\c!sidespacebefore}}% \@@bksidespacebefore
    \docalculatefloatskip\sidefloatbottomskip{\getvalue{\??fl#1\c!sidespaceafter}}% \@@bksidespaceafter
    \gdef\sidefloattopoffset{\openstrutdepth}% was \def
    \global\floatsideskip\getvalue{\??fl#1\c!margin}%
    \global\sidefloatleftshift \getvalue{\??fl#1\c!leftmargindistance}%
    \global\sidefloatrightshift\getvalue{\??fl#1\c!rightmargindistance}%
    \global\noftopfloats \@@bkntop\relax
    \global\nofbotfloats \@@bknbottom\relax}}

\newif\ifinsidefloat

\let\floatcaptionsuffix\empty % an optional suffix
\let\floatcaptionnumber\empty % a logical counter

% obsolete ?
%
% \def\dosetfloatcaption#1#2#3% name will change
%   {\def\dofloattekst%
%      {{\doattributes{\??kj#1}\c!sttle\c!color{#3}}}%
%    \doifelsevalue{\??kj#1\c!number}\v!yes
%      {\def\dofloatnummer%
%         {{\xdef\floatcaptionnumber{#1}%
%           \hbox{\doattributes{\??kj#1}\c!headstyle\c!headcolor
%              {\strut#2\floatcaptionsuffix}}}%
%           \ConvertToConstant\doifnot{#3}{}
%             {\tfskip\emergencystretch=.5em}}}
%      {\let\dofloatnummer\empty}}

% Quite experimental !

% the split is needed when for instance the float goes into
% a multi page field and the list of figs becomes larger than
% one page: cycle between 'only flush when object ref ok'
% and 'one/many page fig list'; see "uguide finometer"
%
% potential sync bug with sectionblocks, see uguide.tex

\def\placefloatcaption
  {\dodoubleempty\doplacefloatcaption}

\def\doplacefloatcaption[#1][#2]#3%
  {\setfloatcaption[#1][#2]{#3}%
   \placefloatcaptiontext[#1]%
   \placefloatcaptionreference[#1]}

\def\setfloatcaption % \dosetfloatcaption already in use
  {\dodoubleempty\dodosetfloatcaption} % beware, name clash

\def\dodosetfloatcaption[#1][#2]#3% to do namespace for number/ascii
  {\ifnofloatnumber               % also handle trialtypesetting
     \letgvalue{@fl@r@#1}\relax
     \letgvalue{@fl@t@#1}\relax
   \else
     \incrementnumber[#1]%
     \maakhetnummer[#1]%
   % \globallet\flhetnummer\hetnummer % beware, not unique should be done on a per class base
     \letgvalue{@fl@n@#1}\hetnummer
     % indirect macro can be more efficient
     \setgvalue{@fl@r@#1}%
       {\dofloatreference
        \redofloatorder{#1}%
      % \dowritetolist{#1}{\flhetnummer}{#3}{#1}%
        \dowritetolist{#1}{\getvalue{@fl@n@#1}}{#3}{#1}%
        \doglobal\convertargument#3\to\flasciititle % \asciititle is global
      % \doifsomething{#2}{\rawreference\s!flt{#2}{{\flhetnummer}{\flasciititle}}}%
        \doifsomething{#2}{\rawreference\s!flt{#2}{{\getvalue{@fl@n@#1}}{\flasciititle}}}%
        \letgvalue{@fl@r@#1}\relax}% nils
     \setgvalue{@fl@t@#1}%
     % {\preparethenumber{\??kj#1}\flhetnummer\preparednumber
       {\preparethenumber{\??kj#1}{\getvalue{@fl@n@#1}}\preparednumber
        \doattributes{\??kj#1}\c!style\c!color
          {\doattributes{\??kj#1}\c!headstyle\c!headcolor
             {\labeltexts{#1}{\preparednumber}}%
           \doattributes{\??kj#1}\c!textstyle\c!textcolor
             {\dotfskip{\getvalue{\??kj#1\c!distance}}#3}}}%
   \fi}

\def\placefloatcaptiontext     [#1]{\getvalue{@fl@t@#1}}
\def\placefloatcaptionreference[#1]{\getvalue{@fl@r@#1}}

% still needed for uguide

\let\placefloatlabel          \placefloatcaption
\let\placefloatlabeltext      \placefloatcaptiontext
\let\placefloatlabelreference \placefloatcaptionreference

\def\borderedfloatbox#1%
  {\localframed
     [\??fl#1]
     [\c!width=\@@bkwidth,
      \c!height=\@@bkheight,
      \c!location=\v!normal,
      \c!offset=\@@bkoffset]%
     {\box\floatbox}}

\newbox\captionbox

% \floatparameter

% \def\putcompletecaption#1#2#3#4%
%   {\noindent
%    \xdef\lastcaptiontag{\strut#2\floatcaptionsuffix}%
%    \xdef\floatcaptionnumber{#1}%
%    \dostartattributes{\??kj#1}\c!style\c!color\empty
%      \ifnofloatnumber
%      \else
%        \hbox{\doattributes{\??kj#1}\c!headstyle\c!headcolor{\strut#2\floatcaptionsuffix}}%
%        \ifnofloatcaption \else \ifemptyfloatcaption \else
%          \ifcase#4\relax
%            \scratchskip\@@kjkjdistance\relax
%            \dotfskip\scratchskip\emergencystretch.5\scratchskip
%          \else
%            \ifx\@@kjkjinbetween\empty\else\unskip\@@kjkjinbetween\fi
%          \fi
%        \fi \fi
%      \fi
%      \ifnofloatcaption \else
%        \doattributes{\??kj#1}\c!textstyle\c!textcolor
%          {\begstrut#3\endstrut\endgraf}%
%      \fi
%    \dostopattributes}

\def\putcompletecaption#1#2#3#4%
  {\noindent
   \xdef\lastcaptiontag{\strut#2\floatcaptionsuffix}%
   \xdef\floatcaptionnumber{#1}%
   \dostartattributes{\??kj#1}\c!style\c!color\empty
     \ifnofloatnumber
     \else
       \hbox{\doattributes{\??kj#1}\c!headstyle\c!headcolor{\strut#2\floatcaptionsuffix}}%
       \ifnofloatcaption \else \ifemptyfloatcaption \else
         \ifcase#4\relax
           \scratchskip\@@kjkjdistance\relax
           \dotfskip\scratchskip\emergencystretch.5\scratchskip
         \else
           \ifx\@@kjkjinbetween\empty\else\unskip\@@kjkjinbetween\fi
         \fi
       \fi \fi
     \fi
     \ifnofloatcaption
       \globallet\lastcaptionht\!!zeropoint
       \globallet\lastcaptiondp\!!zeropoint
     \else
       \doattributes{\??kj#1}\c!textstyle\c!textcolor
         {\xdef\lastcaptionht{\strutheight}%
          \xdef\lastcaptiondp{\strutdepth}%
          \begstrut#3\endstrut\endgraf}%
     \fi
   \dostopattributes}

\let\lastcaptionht\!!zeropoint
\let\lastcaptiondp\!!zeropoint

% new

\newbox\tempfloatbox
\newbox\tempcaptionbox

%\stelblokkopjesin[\c!width=5cm]
%\stelblokkopjesin[\c!align=\v!left]
%\stelblokkopjesin[\c!align=\v!right]

\def\docheckcaptioncontent#1#2#3#4%
  {\ifnofloatcaption \else
     \setbox\tempcaptionbox\hbox
       {\trialtypesettingtrue\notesenabledfalse\putcompletecaption{#4}{#2}{#3}{0}}%
     % new, \placefigure{\XMLflush{somecaption}}{} passes earlier empty check
     % so here we misuse the scratch box; actually this means that the previous
     % test can go away (some day, when i redo this module)
     \ifdim\wd\tempcaptionbox=\zeropoint
       \global\emptyfloatcaptiontrue
       \ifnofloatnumber
         \global\nofloatcaptiontrue
       \fi
     \fi
   \fi}

% minwidth=fit,width=max : no overshoot, as wide as graphic

\ifx\moveboxontogrid\undefined \let\movecaptionontogrid\gobblethreearguments \fi

\def\dosetpagfloat#1#2#3#4% \copy wegwerken
  {\bgroup
   \setlocalfloathsize
\ifnum\floatrotation>0
  \swapdimens\hsize\vsize
\fi
   \forgetall
   \postponefootnotes
   \dontcomplain
   \setbox\tempfloatbox\vbox{\borderedfloatbox{#4}}%
   \def\locatefloat
     {\chardef\alignstrutmode\zerocount
      \alignedline\@@flfllocation\v!middle}%
   \docheckcaptioncontent{#1}{#2}{#3}{#4}%
   \ifnofloatcaption
     \dopreparenocaption{#1}{#2}{#3}{#4}%
     \edef\width{\the\wd\floatbox}%
     \doglobal\addlocalbackgroundtobox\floatbox
   \else
     % todo: installable maken, variant/method=auto vs macro
     \doifinsetelse\@@kjkjlocation{\v!high,\v!middle,\v!low}
       {\dopreparesidecaption{#1}{#2}{#3}{#4}}
       {\doifelse\@@kjkjminwidth\v!fit
          {\doifelse\@@kjkjwidth\v!max
             {\dopreparestackcaptionmax{#1}{#2}{#3}{#4}}
             {\ifdim\wd\tempcaptionbox>\wd\tempfloatbox % wider caption
                \doifelse\@@kjkjwidth\v!fit
                  {\dopreparestackcaptionaut{#1}{#2}{#3}{#4}}
                  {\dopreparestackcaptionwid{#1}{#2}{#3}{#4}}%
              \else
                \dopreparestackcaptionmin{#1}{#2}{#3}{#4}%
              \fi}}
          {\dopreparestackcaptionfix{#1}{#2}{#3}{#4}}}% new, special effects (see icare)
     \edef\width{\the\wd\tempfloatbox}%
     \addlocalbackgroundtobox\tempfloatbox
     \setbox\tempcaptionbox\hbox{\@@kjkjcommand{\box\tempcaptionbox}}%
     \moveboxontogrid\tempcaptionbox\@@kjkjgrid\lastcaptionht
     \addlocalbackgroundtobox\tempcaptionbox
     \buildfloatbox
   \fi
   \ifnum\floatrotation>0
     \global\setbox\floatbox\vbox
        {\rotate[\c!rotation=\floatrotation]{\box\floatbox}}%
         \edef\width{\the\wd\tempfloatbox}%
   \else
     \postcenterfloatbox\width
   \fi
   \egroup}

\ifx\addlocalbackgroundtobox\undefined
  \def\addlocalbackgroundtobox{\resetglobal\gobbleoneargument}%
\fi

\def\captionminwidth  {15\korpsgrootte}
\def\captionovershoot {2em}

\def\dopreparenocaption#1#2#3#4%
  {\global\setbox\floatbox\vbox % pas op als wd groter dan hsize
     {\ifinsidecolumns\ifdim\wd\tempfloatbox>\hsize
        \let\locatefloat\relax
      \fi\fi
      \locatefloat{\copy\tempfloatbox}}}

\def\dopreparestackcaptionfix#1#2#3#4%
  {\dosetraggedvbox\@@kjkjalign
   \setbox\tempcaptionbox\raggedbox
     {\hsize\@@kjkjminwidth % special effects
      \putcompletecaption{#4}{#2}{#3}{0}}}

\def\dopreparestackcaptionmax#1#2#3#4%
  {\dosetraggedvbox\@@kjkjalign
   \setbox\tempcaptionbox\raggedbox
     {\hsize\wd\tempfloatbox
      \putcompletecaption{#4}{#2}{#3}{0}}}

\def\dopreparestackcaptionwid#1#2#3#4%
  {\dosetraggedvbox\@@kjkjalign
   \setbox\tempcaptionbox\raggedbox
     {\hsize\@@kjkjwidth
      \putcompletecaption{#4}{#2}{#3}{0}}}

\def\dopreparestackcaptionmin#1#2#3#4%
  {\raggedcenter                    % the default
   \dosetraggedvbox\@@kjkjalign     % when given
   \setbox\tempcaptionbox\raggedbox % vbox, keeps footnotes
     {\hsize\wd\tempfloatbox
      \putcompletecaption{#4}{#2}{#3}{0}}}

\def\dopreparestackcaptionaut#1#2#3#4%
  {\doifsomething\@@kjkjalign
     {\ExpandBothAfter\doifnotinset\v!middle\@@kjkjalign
        {\let\captionovershoot\!!zeropoint}}%
   \edef\captionhsize{\the\wd\tempfloatbox}%
   \ifdim\captionhsize>\hsize
     % float is wider than \hsize
     \dosetraggedvbox\@@kjkjalign
     \setbox\scratchbox\raggedbox % trial run
       {\hsize\captionhsize
        \notesenabledfalse
        \putcompletecaption{#4}{#2}{#3}{0}}%
     \ifdim\ht\scratchbox>\lineheight % more lines
       \dosetraggedvbox\@@kjkjalign
       \setbox\tempcaptionbox\raggedbox
         {\hsize\captionhsize
          \advance\hsize -\captionovershoot\relax
          \ifdim\hsize<\captionminwidth\relax
            \hsize\captionhsize
          \fi
          \putcompletecaption{#4}{#2}{#3}{0}}%
     \else
       \setbox\tempcaptionbox\raggedbox
         {\hsize\captionhsize
          \putcompletecaption{#4}{#2}{#3}{0}}%
     \fi
   \else
     % float is smaller of equal to \hsize
     \ifdim\captionhsize<\captionminwidth\relax
       \scratchdimen\captionminwidth % float smaller than min width
       \edef\captionhsize{\the\scratchdimen}%
     \fi
     \setbox\scratchbox\vbox     % test with overshoot
       {\scratchdimen\captionhsize
        \advance\scratchdimen \captionovershoot
        \advance\scratchdimen 3em % an average word length
        \ifdim\scratchdimen<\hsize \hsize\scratchdimen \fi
        \notesenabledfalse
        \putcompletecaption{#4}{#2}{#3}{0}}%
     \ifdim\ht\scratchbox>\lineheight
       % at least an average word longer than a line
       \dosetraggedvbox\@@kjkjalign
       \setbox\tempcaptionbox\raggedbox
         {\scratchdimen\captionhsize
          \advance\scratchdimen \captionovershoot
          \ifdim\scratchdimen<\hsize \hsize\scratchdimen \fi
          \putcompletecaption{#4}{#2}{#3}{0}}%
     \else
       % just over a line, don't use an overshoot
       \expanded{\doifcommonelse{\@@kjkjalign}}{\v!left,\v!right,\v!flushleft,\v!flushright}
         {\dosetraggedvbox\@@kjkjalign
          \setbox\tempcaptionbox\raggedbox
            {\hsize\captionhsize
             % strange : \raggedcenter
             \putcompletecaption{#4}{#2}{#3}{0}}}
         {% nicer
          \setbox\tempcaptionbox\cbox
            {\hsize\captionhsize
             \putcompletecaption{#4}{#2}{#3}{0}}}%
     \fi
   \fi}

\def\dopreparesidecaption#1#2#3#4%
  {\dimen0\hsize
   \advance\dimen0 -\wd\tempfloatbox
   \advance\dimen0 -\@@bkmargin\relax % was \tfskipsize\relax
   \ifdim\wd\tempcaptionbox>\dimen0
     \dimen2=1.3\dimen0
     \ifdim\wd\tempcaptionbox<\dimen2
       \dimen0=0.8\dimen0
     \fi
   \fi
   \setbox\tempcaptionbox\vbox
     {\hsize\dimen0
      \raggedright
      \putcompletecaption{#4}{#2}{#3}{1}}}

\def\buildfloatbox
  {\global\setbox\floatbox\vbox
     {\setlocalfloathsize
      \forgetall
      \processaction
        [\@@kjkjlocation]
        [   \v!top=>\locatefloat{\box\tempcaptionbox}%
                    \endgraf\nointerlineskip\@@kjkjinbetween
                    \locatefloat{\box\tempfloatbox},
         \v!bottom=>\locatefloat{\box\tempfloatbox}%
                    \endgraf\nointerlineskip\@@kjkjinbetween
                    \locatefloat{\box\tempcaptionbox},
           \v!high=>\locatefloat
                      {\doifelse\@@flfllocation\v!left
                         {\box\tempfloatbox
                          \dotfskip\@@kjkjdistance
                          \vbox to\ht\tempfloatbox{\@@kjkjinbetween\box\tempcaptionbox\vfill}}
                         {\vbox to\ht\tempfloatbox{\@@kjkjinbetween\box\tempcaptionbox\vfill}%
                          \tfskip
                          \box\tempfloatbox}},
            \v!low=>\locatefloat
                      {\doifelse\@@flfllocation\v!left
                         {\box\tempfloatbox
                          \dotfskip\@@kjkjdistance
                          \vbox to\ht\tempfloatbox
                            {\vfill\box\tempcaptionbox\@@kjkjinbetween}}
                         {\vbox to\ht\tempfloatbox
                            {\vfill\box\tempcaptionbox\@@kjkjinbetween}%
                          \dotfskip\@@kjkjdistance
                          \box\tempfloatbox}},
         \v!middle=>\locatefloat
                      {\doifelse\@@flfllocation\v!left
                         {\box\tempfloatbox
                          \dotfskip\@@kjkjdistance
                          \vbox to\ht\tempfloatbox{\vfill\box\tempcaptionbox\vfill}}
                         {\vbox to\ht\tempfloatbox{\vfill\box\tempcaptionbox\vfill}%
                          \dotfskip\@@kjkjdistance
                          \box\tempfloatbox}},
        \s!unknown=>\locatefloat{\box\tempfloatbox},
           \v!none=>\locatefloat{\box\tempfloatbox}]}}

\newif\ifpostponecolumnfloats \postponecolumnfloatsfalse % don't change

%\def\postcenterfloatbox#1%
%  {\ifinsidecolumns
%     \ifpostponecolumnfloats
%       \scratchdimen=\zetbreedte
%     \else
%       \scratchdimen=#1\relax
%     \fi
%   \else\ifdim#1>\hsize
%     \scratchdimen=\hsize
%   \else
%     \scratchdimen=\wd\floatbox
%   \fi\fi
%   \global\setbox\floatbox=\hbox to \scratchdimen
%     {\hfill\box\floatbox\hfill}} % geen \hss, gaat mis in kolommen !

% \def\postcenterfloatbox#1% 2 of 4
%   {\global\floatwidth\wd\ifdim\wd2>\wd4 2 \else 4 \fi
%    \ifdim\floatwidth>\zetbreedte
%      \global\floatwidth\zetbreedte
%    \else\ifdim\floatwidth<\hsize
%      \global\floatwidth\hsize
%    \fi\fi
%    \global\setbox\floatbox=\hbox to \floatwidth
%      {\hss\box\floatbox\hss}}

% \def\postcenterfloatbox#1% 2 of 4
%   {\global\setbox\floatbox=\hbox to \width % \wd\ifdim\wd2>\wd4 2\else4\fi
%        {\hss\box\floatbox\hss}%
%    \ifdim\wd\floatbox>\zetbreedte
%      \global\setbox\floatbox=\hbox to \zetbreedte
%        {\hss\box\floatbox\hss}%
%    \else\ifcenterfloatbox\ifdim\wd\floatbox<\hsize
% %     \global\setbox\floatbox=\hbox to \hsize
% %       {\hss\box\floatbox\hss}%
%    \fi\fi\fi
%    \global\floatwidth\wd\floatbox}

%\def\postcenterfloatbox#1%
%  {\ifinsidecolumns
%     \ifpostponecolumnfloats
%       \scratchdimen\zetbreedte
%     \else
%       \scratchdimen#1\relax
%     \fi
%   \else\ifdim#1>\hsize
%     \scratchdimen\hsize
%   \else
%     \scratchdimen\wd\floatbox
%   \fi\fi
%   \global\setbox\floatbox\hbox to \scratchdimen
%   % {\hfill\box\floatbox\hfill}} % geen \hss, gaat mis in kolommen !
%     {\hss  \box\floatbox\hss  }} % wel \hss, anders mis in colset

\def\postcenterfloatbox#1%
  {\ifinsidecolumns
     \ifpostponecolumnfloats
       \scratchdimen\makeupwidth
     \else
       \scratchdimen#1\relax
     \fi
   \else\ifdim#1>\hsize
     \scratchdimen\hsize
   \else
     \scratchdimen\wd\floatbox
   \fi\fi
   \global\setbox\floatbox\hbox to \scratchdimen
   % {\hfill\box\floatbox\hfill}} % geen \hss, gaat mis in kolommen !
   % {\hss  \box\floatbox\hss  }} % wel \hss, anders mis in colset
     {\ifglobalcenterfloatbox
        \donetrue
      \else\iflocalcenterfloatbox
        \donetrue
      \else
        \donefalse
      \fi\fi
      \ifdim\scratchdimen>\effectivehsize
        \donefalse
      \fi
      \hss\ifdone\hskip\effectiveleftskip\fi
      \box\floatbox
      \ifdone\hskip\effectiverightskip\fi\hss}}

\def\dosetparfloat#1#2#3#4%
  {\bgroup
   \forgetall
   \postponefootnotes
   \dontcomplain
   %\showcomposition
   \setbox\tempfloatbox\vbox{\borderedfloatbox{#4}}%
   \addlocalbackgroundtobox\tempfloatbox % no \doglobal
   \docheckcaptioncontent{#1}{#2}{#3}{#4}%
   \ifnofloatcaption
     \global\setbox\floatbox\vbox{\box\tempfloatbox}%
   \else
     \doifelse\@@kjkjwidth\v!max
       {\dosetraggedvbox\@@kjkjalign
        \setbox\tempcaptionbox\raggedbox
          {\hsize\wd\tempfloatbox\putcompletecaption{#4}{#2}{#3}{0}}}%
       {\doifelse\@@kjkjwidth\v!fit
          {\ifdim\wd\tempcaptionbox>\wd\tempfloatbox\relax
             \setbox\tempcaptionbox\vbox
               {\forgetall\hsize\wd\tempfloatbox\putcompletecaption{#4}{#2}{#3}{0}}%
           \else
             \setbox\tempcaptionbox\hbox to \wd\tempfloatbox
               {\hss\box\tempcaptionbox\hss}%
           \fi}
          {\dosetraggedvbox\@@kjkjalign
           \setbox\tempcaptionbox\raggedbox
             {\hsize\wd\tempfloatbox\putcompletecaption{#4}{#2}{#3}{0}}}}%
     \setbox\tempcaptionbox\hbox{\@@kjkjcommand{\box\tempcaptionbox}}%
     \moveboxontogrid\tempcaptionbox\@@kjkjgrid\lastcaptionht
     \addlocalbackgroundtobox\tempcaptionbox % no \doglobal
     \global\setbox\floatbox\vbox
       {\processaction
          [\@@kjkjlocation]
          [   \v!top=>\box\tempcaptionbox
                      \endgraf\nointerlineskip\@@kjkjinbetween
                      \box\tempfloatbox,
           \v!bottom=>\box\tempfloatbox
                      \endgraf\nointerlineskip\@@kjkjinbetween
                      \box\tempcaptionbox,
             \v!none=>\box\tempfloatbox,
          \s!unknown=>\box\tempfloatbox
                      \endgraf\nointerlineskip\@@kjkjinbetween
                      \box\tempcaptionbox]}%
   \fi
%   \doglobal\addlocalbackgroundtobox\floatbox
   \egroup}

\newif\ifparfloat

\long\def\dosetfloatbox#1#2#3#4% todo : \global\setbox
  {\ifvisible
     \par
     \edef\@@kjkjlocation {\getvalue{\??kj#4\c!location}}%
     \edef\@@kjkjgrid     {\getvalue{\??kj#4\c!grid}}%       new
      \def\@@kjkjinbetween{\getvalue{\??kj#4\c!inbetween}}%  no \edef
      \def\@@kjkjcommand  {\getvalue{\??kj#4\c!command}}%    no \edef
     \edef\@@kjkjwidth    {\getvalue{\??kj#4\c!width}}%
     \edef\@@kjkjminwidth {\getvalue{\??kj#4\c!minwidth}}%   in geval van automatisch
     \edef\@@kjkjdistance {\getvalue{\??kj#4\c!distance}}%
     \edef\@@kjkjalign    {\getvalue{\??kj#4\c!align}}%
     \edef\@@flfllocation {\getvalue{\??fl#4\c!location}}%
     \ifparfloat
       \@EA\dosetparfloat % {#1}{#2}{#3}{#4}%
     \else
       \@EA\dosetpagfloat % {#1}{#2}{#3}{#4}%
     \fi{#1}{#2}{#3}{#4}%
     \setlocalfloatdimensions{#4}{#1}\floatbox\global % tzt arg 3/4 weg
     \setbox\floatbox\hbox
       {\dosavefloatdata\restoretextcolor{\box\floatbox}}%
     \global\floatheight\ht\floatbox
     \global\advance\floatheight \dp\floatbox
     \global\floatwidth\wd\floatbox
     \global\advance\totalnoffloats \plusone
     \doifnotinset\v!margin{#1} % gaat namelijk nog fout
       {\setbox\floatbox\vbox
          {\parindent\zeropoint
           \ifvoorlopig
             \inleftmargin{\framed{\infofont\the\totalnoffloats}}%
           \fi
          \box\floatbox}}%
     \wd\floatbox\floatwidth
     \dimen0=\floatheight
     \advance\dimen0 \lineheight
     \ifdim\dimen0<\textheight
     \else
       \global\floatheight\textheight
       \global\advance\floatheight -\lineheight
       \ht\floatbox\floatheight
       \dp\floatbox\zeropoint
       \showmessage\m!floatblocks{10}{\the\totalnoffloats}%
     \fi
   \fi}

\newcounter\noxfloatlocations

\long\def\dofloat#1#2#3#4%
  {\dosetfloatbox{#1}{#2}{#3}{#4}%
   \doifelsevaluenothing{\??fl#4\c!criterium}
     {\dogetfloatbox{#1}\empty}
     {\ifdim\wd\floatbox>\getvalue{\??fl#4\c!criterium}\relax
        \postcenterfloatbox{\wd\floatbox}% else we get left aligned
        \dogetfloatbox{#1}\v!here        % see details/pascal
      \else
        \dogetfloatbox{#1}\empty
      \fi}}

\let\naturalfloatheight\!!zeropoint
\let\naturalfloatwidth \!!zeropoint
\let\naturalfloatdepth \!!zeropoint

\def\setnaturalfloatdimensions#1%
  {\xdef\naturalfloatheight{\the\ht#1}%
   \xdef\naturalfloatwidth {\the\wd#1}%
   \xdef\naturalfloatdepth {\the\dp#1}}

\long\def\docompletefloat#1#2#3#4#5#6#7% #7 = box number
  {%\flushsidefloats % moved
   \presetfloatvariables{#1}{#4}{#2}{#6}%
   \bgroup
   \setnaturalfloatdimensions#7%
   \global\setbox\floatbox\vbox
     {\executeifdefined{\??fl#1\c!command}\firstofoneargument{\box#7}}%
   \setnaturalfloatdimensions\floatbox
   \dimen0 \ht\floatbox
   \advance\dimen0 \dp\floatbox
   \ifdim\dimen0=\zeropoint
     \showmessage\m!floatblocks{11}\empty
     \global\setbox\floatbox\vbox{\doemptyblock{#3}}%
   \fi
   \ifnofloatcaption
     \global\setbox\floatbox\vbox
       {\unvbox\floatbox
        \vss % gets rid of the depth (unless tabulate)
        \rawpagereference\s!flt{#2}}%
      \egroup
      \dofloat{#4}{}{#6}{#1}%
   \else
     \doglobal\convertargument#6\to\asciititle % \asciititle is global
     \ifnofloatnumber
       \global\setbox\floatbox\vbox
         {\unvbox\floatbox % no \vss, keep the depth
          \rawreference\s!flt{#2}{{}{\asciititle}}}%
       \egroup
       \dofloat{#4}{}{#6}{#1}%
     \else
       \incrementnumber[#1]%
       \maakhetnummer[#1]%
       \global\setbox\floatbox\vbox
         {\unvbox\floatbox % no \vss, keep the depth
          \dofloatreference
          \redofloatorder{#1}%
          \rawreference\s!flt{#2}{{\hetnummer}{\asciititle}}%
          \dowritetolist{#3}{\hetnummer}{#6}{#3}}%
       \egroup
       \preparethenumber{\??kj#1}\hetnummer\preparednumber
       \dofloat{#4}{\labeltexts{#5}{\preparednumber}}{#6}{#1}%
     \fi
   \fi
   \global\insidefloatfalse}

\newif\ifmargeblokken

\def\dosetupmarginblocks[#1]%
  {\getparameters[\??mb][#1]%
   \doifelse\@@mbstate\v!start
     {\showmessage\m!layouts4\empty
      \margeblokkentrue
      \let\somenextfloat\dosomenextfloat
      \let\startmarginblock\dostartmarginblock
      \let\stopmarginblock\dostopmarginblock}%
     {\showmessage\m!layouts5\empty
      \margeblokkenfalse
      \def\somenextfloat[##1]%
        {\someelsefloat[##1,\v!here]}%
      \let\startmarginblock\dontstartmargeblok
      \let\stopmarginblock\dontstopmargeblok}}

\def\setupmarginblocks
  {\dosingleargument\dosetupmarginblocks}

\newbox\marginbox

\def\dosomenextfloat[#1]%
  {\global\setbox\marginbox\vbox
     {\hsize\@@mbwidth
      \unvcopy\marginbox
      \ifvoid\marginbox\else\expandafter\@@mbinbetween\fi
      \box\floatbox\filbreak}%
   \ifdim\ht\marginbox>\textheight
     \dosavefloatinfo
   \else
     \doinsertfloatinfo
   \fi}

\newbox\preparedmarginbox

\def\reshapemargin
  {\ifdim\ht\preparedmarginbox>\zeropoint
     \beginofshapebox
     \unvbox\preparedmarginbox
     \endofshapebox
     \reshapebox
       {\box\shapebox}%
     \setbox\preparedmarginbox\vbox to \textheight
       {\@@mbtop
        \flushshapebox
        \@@mbbottom}%
   \fi}

\def\plaatsrechtermargeblok
  {\hskip\rightmarginwidth}

\def\plaatslinkermargeblok
  {\hskip\leftmarginwidth}

\def\checkmargeblokken
  {\ifvoid\marginbox\else\docheckmargeblokken\fi}

\def\docheckmargeblokken % erg inefficient
  {\setbox\preparedmarginbox\vbox
     {\forgetall
      \splittopskip\topskip
      \ifvoid\marginbox\else
        \ifdim\ht\marginbox>\textheight
          \vsplit\marginbox to \textheight
        \else
          \unvbox\marginbox
        \fi
      \fi}%
   \reshapemargin
   \setbox\preparedmarginbox\vbox
      {\@@mbbefore\box\preparedmarginbox\@@mbafter}%
   \def\rightmarginbox
     {\def\plaatsrechtermargeblok
        {\setbox\preparedmarginbox\hbox to \rightmarginwidth
           {\@@mbleft\box\preparedmarginbox\@@mbright}%
         \vsmashbox\preparedmarginbox
         \box\preparedmarginbox}}%
   \def\leftmarginbox
     {\def\plaatslinkermargeblok
        {\setbox\preparedmarginbox\hbox to \leftmarginwidth
           {\@@mbright\box\preparedmarginbox\@@mbleft}%
         \vsmashbox\preparedmarginbox
         \box\preparedmarginbox}}%
   \processaction % traag
     [\@@mblocation]
     [ \v!inmargin=>\doifbothsidesoverruled
                     \rightmarginbox
                   \orsideone
                     \rightmarginbox
                   \orsidetwo
                     \leftmarginbox
                   \od,
        \v!middle=>\doifbothsidesoverruled
                     \rightmarginbox
                   \orsideone
                     \leftmarginbox
                   \orsidetwo
                     \rightmarginbox
                   \od,
         \v!left=>\leftmarginbox,
        \v!right=>\rightmarginbox,
       \s!unknown=>\setbox\preparedmarginbox\hbox{}]}

\def\dostartmarginblock % 2 maal \vbox ivm \unvbox elders
  {\global\setbox\marginbox\vtop\bgroup\vbox\bgroup
     \hsize\@@mbwidth
     \ifvoid\marginbox\else
       \unvbox\marginbox
       \@@mbinbetween
     \fi
     \setupalign[\@@mbalign]%
     \dostartattributes\??mb\c!style\c!color{}%
     \begstrut\ignorespaces}

\def\dostopmarginblock
  {\unskip\endstrut
   \dostopattributes
   \egroup
   \egroup}

\def\dontstartmargeblok
  {\@@mbbefore
   \bgroup
   \dostartattributes\??mb\c!style\c!color\empty}

\def\dontstopmargeblok
  {\dostopattributes
   \egroup
   \@@mbafter}

\newcounter\nofpostponedblocks

\newif\ifinpostponing

\newevery\everytopofpage\relax

\appendtoks\the\everytopofpage     \to\everystarttext
\appendtoks\global\everytopofpage{}\to\everystoptext

% \startpostponing [pagenumber] [+pageoffset]
%
% \startpostponing[2]
%   PAGE 2 \blank
% \stoppostponing
%
% \startpostponing[+1]
%   PAGE +1 \blank
% \stoppostponing
%
% \startpostponing[+2]
%   PAGE +2 \blank
% \stoppostponing
%
% \starttext \dorecurse{4}{\input tufte \page} \stoptext

\newtoks   \postponedpageblocks
\newcounter\nofpostponedpageblocks

% \ifinpostponing: handhaven, want gebruikt in stijlen ! ! ! ! !

\def\dopostponeblock
  {\bgroup % new may 2004
   \setsystemmode\v!postponing % new may 2004
   \the\everytopofpage
   \ifvoid\collectedpagefloats\else
     % message
     \unvbox\collectedpagefloats
   \fi
   \donefalse
   \ifinpostponing \else
     \ifcase\nofpostponedblocks     \else \donetrue \fi
     \ifcase\nofpostponedpageblocks \else \donetrue \fi
   \fi
   \ifdone
     \bgroup % we need the color/font switch, else problems inside split verbatim
     \setnormalcatcodes % postponing in verbatim
     \edef\savedtopofpagecolor{\topofpagecolor}%
     \doifsomething\savedtopofpagecolor\restorecolormode % \stopcolormode
     \restoreglobalbodyfont                        % The \nof-test is
     \global\pagetotal\zeropoint                   % recently added and
     \global\inpostponingtrue                      % definitely needed else
     \the\postponedpageblocks                      % we can loose or disorder
     \dorecurse\nofpostponedblocks                 % floats; anyhow, this
       {\getbuffer[pbuf-\recurselevel]}           % mechanism is still
     \doflushfloats % new but potential dangerous  % suboptimal and needs a
     \doglobal\newcounter\nofpostponedblocks       % proper analysis
     \global\inpostponingfalse
     \doifsomething\savedtopofpagecolor\startcolormode\savedtopofpagecolor
     \egroup
   \fi
   \egroup} % new may 2004

\def\getpostponedblock#1#2%
  {\doif{#1}\realfolio{\getbuffer[rbuf-#2]}} % no \ifnum, avoid \fi

% beware, \dosingleempty conflicts with buffers (feeds back the \par)

\setvalue{\e!start\v!postponing}%
  {\bgroup
   \obeylines
   \doifnextcharelse[%
     {\egroup\nodostartpostponing}{\egroup\dodostartpostponing}}

\def\nodostartpostponing[#1]%
  {\doglobal\increment\nofpostponedpageblocks
   \bgroup % a little bit of misusing grouping
   \doifinstring{+}{#1}\advance \realpageno#1\relax % ugly but efficient
   \doglobal\appendetoks\noexpand\getpostponedblock
     {\realfolio}{\nofpostponedpageblocks}\to\postponedpageblocks
   \egroup
   \showmessage\m!layouts3\nofpostponedpageblocks
   \dostartbuffer[rbuf-\nofpostponedpageblocks]%
     [\e!start\v!postponing][\e!stop\v!postponing]}

\def\dodostartpostponing
  {\doglobal\increment\nofpostponedblocks
   \showmessage\m!layouts3\nofpostponedblocks
   \dostartbuffer[pbuf-\nofpostponedblocks]%
     [\e!start\v!postponing][\e!stop\v!postponing]}

\definenumber
  [\??si]
  [\c!way=\v!by\v!text,
   \c!conversion=\@@siconversion]

\def\setupfloatsplitting
  {\dodoubleargument\getparameters[\??si]}

% ook (continued)

\def\dosplitfloat[#1]#2% nog dubbele refs
  {\ifinsidecolumns         % tzt ook nog figuren splitten
     % not yet supported
   \else
     \bgroup
     \insidefloattrue
     \getparameters[\??si][#1]%
     \resetnumber[\??si]%
     \def\floatcaptionsuffix{\convertednumber[\??si]}%
     \TABLEcaptionheight\@@silines\lineheight % brrr
     \simplifypagebreak % \page becomes \goodbreak
     \dowithnextbox
       {\forgetall
        \dontcomplain
        \doloop
          {\setbox2\vsplit\nextbox to \lineheight
           \setbox2\vbox{\unvbox2}
           \ifdim\ht2>\lineheight
             \incrementnumber[\??si]%
             \ifcase\rawnumber[\??si]\or \ifdim\nextboxht=\zeropoint
               \let\floatcaptionsuffix\empty
             \fi \fi
             \bgroup
             #2{\unvbox2}
             \egroup
             \ifdim\nextboxht>\zeropoint
               \page
               \decrementnumber[\floatcaptionnumber]%
             \fi
           \fi
           \ifdim\nextboxht>\zeropoint\else
             \expandafter\exitloop
           \fi}%
        \egroup}
     \vbox
   \fi}

\def\splitfloat
  {\dosingleempty\dosplitfloat}

\def\dooutput{\sidefloatoutput}           % redefinition of \dooutput

\setupmarginblocks
  [\c!state=\v!start,
   \c!location=\v!inmargin,
   \c!width=\rightmarginwidth,
   \c!style=,
   \c!color=,
   \c!align=,
   \c!left=,
   \c!right=,
   \c!top=,
   \c!inbetween=\blank,
   \c!bottom=\vfill,
   \c!before=,
   \c!after=]

\definefloat
  [\v!figure]
  [\v!figures]

\definefloat
  [\v!table]
  [\v!tables]

\setupfloat
  [\v!table]
  [\c!frame=\v!off]

\definefloat
  [\v!intermezzo]
  [\v!intermezzi]

\definefloat
  [\v!graphic]
  [\v!graphics]

\setupcaptions
  [\c!location=\v!bottom,
   \c!grid=,
   \c!before=\blank,
   \c!inbetween={\blank[\v!medium]},
   \c!after=\blank,
   \c!width=\v!fit,
   \c!minwidth=\v!fit, % id est: the width of the floatbox in some cases
   \c!headstyle=\v!bold,
   \c!headcolor=,
   \c!style=\v!normal,
   \c!color=,
   \c!textstyle=,
   \c!textcolor=,
   \c!align=,
   \c!number=\v!yes,
   \c!way=\@@nrway,
   \c!blockway=\@@nrblockway,
   \c!sectionnumber=\@@nrsectionnumber,
   \c!separator=\@@koseparator,
   \c!distance=1em,
   \c!command=,
   \c!conversion=\v!numbers]

\setupfloats
  [\c!location=\v!middle,
   \c!width=\v!fit,
   \c!height=\v!fit,
   \c!offset=\v!overlay,
   \c!frame=\v!off,
   \c!radius=.5\korpsgrootte,
   \c!corner=\v!rectangular,
   \c!background=,
   \c!backgroundscreen=\@@rsscreen,
   \c!backgroundcolor=,
   \c!backgroundoffset=\!!zeropoint,
   \c!topframe=,
   \c!bottomframe=,
   \c!leftframe=,
   \c!rightframe=,
   \c!frameoffset=\!!zeropoint,
   \c!before=,
   \c!after=,
   \c!spacebefore=\v!big,
   \c!spaceafter=\v!big,
   \c!sidespacebefore=\@@bkspacebefore,
   \c!sidespaceafter=\@@bkspaceafter,
   \c!sidealign=\v!normal,
   \c!textmethod=\ifgridsnapping2\else0\fi, % 0=raw 1=safe (.99pg) 2=tight (-1pt)
   \c!sidemethod=\ifgridsnapping2\else1\fi,   % 0=raw 1=safe (.99pg) 2=tight (-1pt)
   \c!indentnext=\v!no,
   \c!margin=1em,
   \c!leftmargindistance=\zeropoint,
   \c!rightmargindistance=\@@bkleftmargindistance,
   \c!ntop=2,
   \c!nbottom=0,
   \c!nlines=4,
   \c!local=,
   \c!default=\v!figure]

\setupfloatsplitting
  [\c!conversion=\v!character, % \v!romannumerals
   \c!lines=3]

% float strategy, replaces some of the above macros

\let\floatmethod\empty
\let\floatcolumn\empty
\let\floatrow   \empty

\def\dogetfloatbox#1#2%
  {\ifvisible
     \doifelsenothing{#2}
       {\getfromcommalist[#1][1]%
        \@EA\beforesplitstring\commalistelement\at:\to\floatmethod
        \@EA\aftersplitstring \commalistelement\at:\to\floatcolumn
        \@EA\aftersplitstring \floatcolumn\at*\to\floatrow
        \@EA\beforesplitstring\floatcolumn\at*\to\floatcolumn
        % todo: nog algemeen otr
        \ifx\OTRSETsetpreferedcolumnslot\undefined\else
          \OTRSETsetpreferedcolumnslot\floatcolumn\floatrow
        \fi}
       {\let\floatcolumn\empty
        \let\floatrow\empty
        \edef\floatmethod{#2}}%
     \doifundefined{\string\floatmethod\floatmethod}
        {\let\floatmethod\v!here}%
     \getvalue{\string\floatmethod\floatmethod}[#1]%
   \fi}

\def\installfloathandler#1#2% #1=keyword #2=handler
  {\setvalue{\string\floatmethod#1}{#2}}

\installfloathandler \v!here        \someherefloat
\installfloathandler \v!force       \somefixdfloat
\installfloathandler \v!left        \someleftsidefloat
\installfloathandler \v!right       \somerightsidefloat
\installfloathandler \v!text        \sometextfloat
\installfloathandler \v!top         \sometopfloat
\installfloathandler \v!bottom      \somebottomfloat
\installfloathandler \v!margin      \somemarginfloat
\installfloathandler \v!opposite    \somefacefloat
\installfloathandler \v!page        \somepagefloat
\installfloathandler \v!inmargin    \someinmarginfloat
\installfloathandler \v!inleft      \someinleftmarginfloat
\installfloathandler \v!inright     \someinrightmarginfloat
\installfloathandler \v!leftmargin  \someinleftmarginfloat
\installfloathandler \v!rightmargin \someinrightmarginfloat
\installfloathandler \v!leftedge    \someinleftedgefloat
\installfloathandler \v!rightedge   \someinrightedgefloat

\installfloathandler \v!backspace   \somebackspacefloat
\installfloathandler \v!cutspace    \somecutspacefloat

\installfloathandler {tblr} \someslotfloat
\installfloathandler {lrtb} \someslotfloat
\installfloathandler {tbrl} \someslotfloat
\installfloathandler {rltb} \someslotfloat
\installfloathandler {btlr} \someslotfloat
\installfloathandler {lrbt} \someslotfloat
\installfloathandler {btrl} \someslotfloat
\installfloathandler {rlbt} \someslotfloat
\installfloathandler {fxtb} \someslotfloat
\installfloathandler {fxbt} \someslotfloat

\def\placesomeslotfloat {\OTRcommand\someslotfloat}
\def\placesomeherefloat {\OTRcommand\someherefloat}
\def\placesomefixdfloat {\OTRcommand\somefixdfloat}
\def\placesomepagefloat {\OTRcommand\somepagefloat}
\def\placesometopsfloat {\OTRcommand\sometopsfloat}
\def\placesomebotsfloat {\OTRcommand\somebotsfloat}
\def\placesomesidefloat {\OTRcommand\somesidefloat}
\def\placesomefacefloat {\OTRcommand\somefacefloat}

\def\someleftsidefloat     [#1]{\somesidefloat[#1]\presetindentation}
\def\somerightsidefloat    [#1]{\somesidefloat[#1]}
\def\sometopfloat          [#1]{\someelsefloat[#1]\nonoindentation}
\def\somebottomfloat       [#1]{\someelsefloat[#1]}
\def\somemarginfloat       [#1]{\somenextfloat[#1]\nonoindentation}
\def\someinleftmarginfloat [#1]{\somesidefloat[#1]}
\def\someinrightmarginfloat[#1]{\somesidefloat[#1]}
\def\someinleftedgefloat   [#1]{\somesidefloat[#1]}
\def\someinrightedgefloat  [#1]{\somesidefloat[#1]}
\def\someinmarginfloat     [#1]{\somesidefloat[#1]}
\def\someherefloat         [#1]{\someelsefloat[\v!here,#1]}

\def\somebackspacefloat    [#1]{\somesidefloat[#1]}
\def\somecutspacefloat     [#1]{\somesidefloat[#1]}

\def\somefixdfloat {\placesomefixdfloat}
\def\somepagefloat {\placesomepagefloat}
\def\somefacefloat {\placesomefacefloat}
\def\someslotfloat {\placesomeslotfloat}

\protect \endinput
