%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.

\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\stelplaatsblokkenin
  {\dodoubleargument\getparameters[\??bk]}

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

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

\def\stelplaatsblokin
  {\dodoubleargument\dostelplaatsblokin}

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

\def\stelblokkopjein
  {\dodoubleargument\dostelblokkopjein}

\def\doleegblok#1%
  {\localframed
     [\??fl#1][\c!kader=\v!aan]%
     {\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!figuur}
     {\doifundefined{\??fl#1\c!default}{\let\floattype\v!figuur}}%
   \doifelsenothing{#2}
     {\edef\floatlocation{\getvalue{\??fl\floattype\c!default}}}
     {\edef\floatlocation{#2}}%
   \expanded{\dodocomplexplaatsblok[\floattype][\floatlocation]}}

\def\dodocomplexplaatsblok[#1][#2][#3]#4%
  {\flushnotes
   \ifsomefloatwaiting
     % this was \checkwaitingfloats spread all over
     \doifinsetelse\v!altijd{#2}
       {\showmessage\m!floatblocks5\empty}
       {\doifcommonelse
          {#2}
          {\v!links,\v!rechts,\v!binnen,\v!buiten,%
           \v!inlinker,\v!inrechter,\v!inmarge,%
           \v!linkermarge,\v!rechtermarge,\v!linkerrand,\v!rechterrand,%
           \v!binnenmarge,\v!buitenmarge,\v!binnenrand,\v!buitenrand,%
           \v!tekst,\v!naast}% \v!pagina
          {\doflushfloats}
          {}}%
     % but which should be done before using box \floatbox
   \fi
   \ifmargeblokken % waarschijnlijk gebroken ! ! ! !
     \doifinset\v!marge{#2}
       {\endgraf 
        \bgroup\everypar{\egroup\the\everypar}%
        \hsize\@@mbbreedte}%
   \fi
   \global\insidefloattrue
   \begingroup % **
   \the\everyinsidefloat
   \let\@@extrafloat\empty
   \presetmorefloatvariables{#2}%
   \dowithnextboxcontent % better a \the\everyfloattoks
     {\setlocalfloathsize
      \getvalue{\??fl#1\c!binnen}%
      \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!tekst{#2}{\carryoverpar\endgroup}% 
      \ifparfloat
        \doifinset\v!reset{#2}\forgetsidefloats
        \doinhibitblank
      \fi}% better move this to side floats 
     \vbox}

%\def\xxdocompletefloat#1#2%
%  {\setbox\nextbox\hbox{\signalrightpage\flushnextbox}%
%   \doifrightpageelse{\let\@@extrafloat#1}{\let\@@extrafloat#2}}

\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)

\def\presetfloatvariables#1#2#3#4% 
  {\doifcommonelse
     {#2}
     {\v!links,\v!rechts,\v!binnen,\v!buiten,%
      \v!inlinker,\v!inrechter,\v!inmarge,%
      \v!binnenmarge,\v!buitenmarge,\v!binnenrand,\v!buitenrand,%
      \v!linkermarge,\v!linkerrand,\v!rechtermarge,\v!rechterrand}
     {\global\parfloattrue}
     {\global\parfloatfalse}%
   \ifbinnenkolommen
     \global\parfloatfalse
   \fi
   \global\sidefloatshift\zeropoint  
   \global\chardef\sidefloatmethod\getvalue{\??fl#1\c!zijmethode}%
   \global\chardef\textfloatmethod\getvalue{\??fl#1\c!tekstmethode}%
   \global\chardef\sidefloatalign\zerocount
   \calculatefloatskips{#1}%
   \ifparfloat
     \processaction
       [\getvalue{\??fl#1\c!zijuitlijnen}]
       [\v!hoogte=>\global\chardef\sidefloatalign\plusone,%
         \v!regel=>\global\chardef\sidefloatalign\plustwo,% (***)
        \v!diepte=>\global\chardef\sidefloatalign\plusthree,%
          \v!grid=>\global\chardef\sidefloatalign4]%
     \ifcase\sidefloatalign\relax
       \doifinset\v!hoogte{#2}{\global\chardef\sidefloatalign\plusone}%
       \doifinset\v!regel {#2}{\global\chardef\sidefloatalign\plustwo}%
       \doifinset\v!diepte{#2}{\global\chardef\sidefloatalign\plusthree}%
       \doifinset\v!grid  {#2}{\global\chardef\sidefloatalign4}%
     \fi
     \doifinset\v!hoog{#2}{\global\sidefloattopskip   \zeropoint}%
     \doifinset\v!laag{#2}{\global\sidefloatbottomskip\zeropoint}%
     \doifinset\v!passend{#2}
       {\global\sidefloattopskip   \zeropoint
        \global\sidefloatbottomskip\zeropoint
        \global\floatsideskip      \zeropoint}%
   \fi
   \doifinsetelse\v!geennummer{#2}
     {\global\nofloatnumbertrue}
     {\doifelsevalue{\??kj#1\c!nummer}\v!ja
        {\global\nofloatnumberfalse}
        {\global\nofloatnumbertrue}}%
   \ConvertToConstant\doifelse{#4}{}
     {\global\emptyfloatcaptiontrue}
     {\global\emptyfloatcaptionfalse}%
   \doifinsetelse\v!geen{#2}
     {\global\nofloatcaptiontrue}
     {\ConvertToConstant\doifelse{#4}\v!geen
        {\global\nofloatcaptiontrue}
        {\global\nofloatcaptionfalse}}%
   \ifemptyfloatcaption \ifnofloatnumber
     \global\nofloatcaptiontrue
   \fi \fi}


% documenteren in details 

\def\presetmorefloatvariables#1%
  {\doifelse\@@bklokaal\v!ja % fout keyword
     \globalcenterfloatboxtrue
     \globalcenterfloatboxfalse
   \ifglobalcenterfloatbox
     \localcenterfloatboxtrue
   \else
     \doifinsetelse\v!lokaal{#1}
       \localcenterfloatboxtrue
       \localcenterfloatboxfalse
   \fi
   \doifnotcommon{\v!altijd,\v!hier,\v!forceer}{#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}

\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=
  {\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!regel=>\docommando+,%
            +\v!regel=>\docommando+,%
            -\v!regel=>\docommando-]}
      \def\docommando##1%
        {\ifdone\else\global\sidefloatdownshift\zeropoint\donetrue\fi
         \global\advance\sidefloatdownshift##1\lineheight}%
      \expanded{\dorepeatwithcommand[#1]}\movedownsidefloat}%
   \egroup}

\def\xdocompletefloat#1#2#3#4#5#6%
  {\ifextrafloatactions 
     \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!binnen=>\xxdocompletefloat\v!links       \v!rechts,
             \v!buiten=>\xxdocompletefloat\v!rechts      \v!links,
        \v!binnenmarge=>\xxdocompletefloat\v!linkermarge \v!rechtermarge,
        \v!buitenmarge=>\xxdocompletefloat\v!rechtermarge\v!linkermarge,
         \v!binnenrand=>\xxdocompletefloat\v!linkerrand  \v!rechterrand,
         \v!buitenrand=>\xxdocompletefloat\v!rechterrand \v!linkerrand,
               ininner=>\xxdocompletefloat\v!inlinker    \v!inrechter,
               inouter=>\xxdocompletefloat\v!inrechter   \v!inlinker,
              \v!regel=>, % 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
%   {\ifextrafloatactions
%      \doifvaluesomething{\??fl#1\c!minbreedte}
%        {\scratchdimen\getvalue{\??fl#1\c!minbreedte}\relax
%         \ifdim\wd#3<\scratchdimen
%           #4\setbox#3\hbox to \scratchdimen
%             {\doifnotvalue{\??fl#1\c!plaats}\v!links \hss
%              \box#3%
%              \doifnotvalue{\??fl#1\c!plaats}\v!rechts\hss}%
%         \fi}%
%      \doifvaluesomething{\??fl#1\c!maxbreedte}
%        {\scratchdimen\getvalue{\??fl#1\c!maxbreedte}\relax
%         \ifdim\wd#3>\scratchdimen
%           #4\setbox#3\hbox to \scratchdimen
%             {\doifcommonelse{\v!rechts,\v!inrechter,\v!links,\v!inlinker}{#2}
%                {\doifnotcommon{\v!rechts,\v!inrechter}{#2}\hss
%                 \box#3%
%                 \doifnotcommon{\v!links ,\v!inlinker }{#2}\hss}%
%                {\doifnotvalue{\??fl#1\c!plaats}\v!links\hss
%                 \box#3%
%                 \doifnotvalue{\??fl#1\c!plaats}\v!rechts\hss}}%
%         \fi}%
%    \fi}

\newdimen\sidefloatextrashift

\def\setlocalfloatdimensions#1#2#3#4% experimental / #3 box number #4 prefix
  {\global\sidefloatshift\zeropoint\relax % duplicate  
   \ifextrafloatactions
     \ifdim\sidefloatdownshift=\zeropoint\else
       \global\setbox\floatbox\vbox
         {\vskip\sidefloatdownshift\nointerlineskip\box\floatbox}%
     \fi
     \doifvaluesomething{\??fl#1\c!minbreedte}
       {\scratchdimen\getvalue{\??fl#1\c!minbreedte}\relax
        \ifdim\wd#3<\scratchdimen
          #4\setbox#3\hbox to \scratchdimen
            {\doifnotvalue{\??fl#1\c!plaats}\v!links \hss
             \box#3%
             \doifnotvalue{\??fl#1\c!plaats}\v!rechts\hss}%
        \fi}%
     \doifinset\v!hangend{#2}
       {\doifcommonelse{\v!inlinker,\v!linkermarge}{#2}
          {\letvalue{\??fl#1\c!maxbreedte}\linkermargebreedte}% 
          {\doifcommon{\v!inrechter,\v!rechtermarge}{#2}
             {\letvalue{\??fl#1\c!maxbreedte}\rechtermargebreedte}}}% 
     \doifvaluesomething{\??fl#1\c!maxbreedte}
       {\scratchdimen\getvalue{\??fl#1\c!maxbreedte}\relax
        \ifdim\wd#3>\scratchdimen
          \doifcommonelse{\v!inrechter,\v!rechtermarge}{#2}  
            {\global\sidefloatshift-\scratchdimen      
             \global\advance\sidefloatshift-\rechtermargeafstand}
            {\doifcommonelse{\v!inlinker,\v!linkermarge}{#2}
               {\global\sidefloatshift-\scratchdimen      
                \global\advance\sidefloatshift-\linkermargeafstand}
               {#4\setbox#3\hbox to \scratchdimen
                 {\doifcommonelse{\v!rechts,\v!links}{#2}
                    {\doifnotinset\v!rechts{#2}\hss
                     \box#3%
                     \doifnotinset\v!links{#2}\hss}%
                    {\doifnotvalue{\??fl#1\c!plaats}\v!links\hss
                     \box#3%
                     \doifnotvalue{\??fl#1\c!plaats}\v!rechts\hss}}}}%
        \fi}%
    \advance\sidefloatshift\sidefloatextrashift
  \fi
  \global\sidefloatdownshift \zeropoint
  \global\sidefloatextrashift\zeropoint}
 
\def\docomplexstarttekstblok[#1][#2][#3]%
  {\flushnotes
   \flushsidefloats % hoort eigenlijk niet hier
   \docomplexplaatsblok[#1][\v!tekst,#2,\v!links][#3]}

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

\def\docomplexstartreserveertekstblok[#1][#2][#3][#4]%
  {\flushsidefloats % hoort eigenlijk niet hier
   \docomplexreserveerblok[#1][#2][\v!tekst,#3,\v!links][#4]}

\def\definieerplaatsblok
  {\dotripleempty\dodefinieerplaatsblok}

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

\def\dododefinieerplaatsblok[#1][#2]%
  {\presetlocalframed[\??fl#1]%
   \stelplaatsblokin
     [#1]
     [\c!breedte=15\korpsgrootte,
      \c!hoogte=10\korpsgrootte,
      \c!minbreedte=,
      \c!maxbreedte=,
      \c!maxhoogte=,
      \c!criterium=,
      \c!zijvoorwit=\@@bkzijvoorwit,
      \c!zijnawit=\@@bkzijnawit,
      \c!zijuitlijnen=\@@bkzijuitlijnen, % \v!regel
      \c!marge=\@@bkmarge,
      \c!linkermargeafstand=\@@bklinkermargeafstand,
      \c!rechtermargeafstand=\@@bkrechtermargeafstand,
      \c!kader=\@@bkkader,
      \c!straal=\@@bkstraal,
      \c!hoek=\@@bkhoek,
      \c!plaats=\@@bkplaats,
      \c!achtergrond=\@@bkachtergrond,
      \c!achtergrondraster=\@@bkachtergrondraster,
      \c!achtergrondkleur=\@@bkachtergrondkleur,
      \c!achtergrondoffset=\@@bkachtergrondoffset,
      \c!bovenkader=\@@bkbovenkader,
      \c!onderkader=\@@bkonderkader,
      \c!linkerkader=\@@bklinkerkader,
      \c!rechterkader=\@@bkrechterkader,
      \c!kaderoffset=\@@bkkaderoffset,
     %\c!lokaal=\@@bklokaal,
      \c!paginaovergangen=,
      \c!tekstmethode=\@@bktekstmethode,
      \c!zijmethode=\@@bkzijmethode,
      \c!default=]%
   \stelblokkopjein
     [#1]
     [\c!plaats=\@@kjplaats,
     %\c!voor=\@@kjvoor,
      \c!tussen=\@@kjtussen,
     %\c!na=\@@kjna,
      \c!breedte=\@@kjbreedte,
      \c!kopletter=\@@kjkopletter,
      \c!kopkleur=\@@kjkopkleur,
      \c!tekstletter=\@@kjtekstletter,
      \c!tekstkleur=\@@kjtekstkleur,
      \c!letter=\@@kjletter,
      \c!kleur=\@@kjkleur,
      \c!uitlijnen=\@@kjuitlijnen,
      \c!nummer=\@@kjnummer,
      \c!wijze=\@@kjwijze,
      \c!blokwijze=\@@kjblokwijze,
      \c!sectienummer=\@@kjsectienummer,
      \c!afstand=\@@kjafstand,
      \c!scheider=\@@kjscheider,
      \c!conversie=\@@kjconversie]%
   \definieernummer % \doorlabelen
     [#1]
     [\c!tekst=#1,
      \c!plaats=\v!intekst,
      \c!wijze=\getvalue{\??kj#1\c!wijze},
      \c!blokwijze=\getvalue{\??kj#1\c!blokwijze},
      \c!sectienummer=\getvalue{\??kj#1\c!sectienummer},
      \c!conversie=\getvalue{\??kj#1\c!conversie}]%
   \presetlabeltext[#1=\Word{#1}~]%
   \dodefinefloatcommands[#1][#2]}

\def\dodefinefloatcommands[#1][#2]%
  {\definieerlijst[#1]%
   \presetheadtext[#2=\Word{#2}]%
   \setvalue{\e!plaats\e!lijstmet#2}%
     {\dodoubleempty\doplaatslijst[#1]}%
   \setvalue{\e!volledige\e!lijstmet#2}%
     {\dotripleempty\dodovolledigelijst[#1][#2]}%
   \setvalue{\e!plaats#1}%
     {\dotripleempty\docomplexplaatsblok[#1]}%
   \setvalue{\e!reserveer#1}%
     {\doquadrupleempty\docomplexreserveerblok[#1]}%
   \setvalue{\e!start#1\e!tekst}%
     {\dotripleempty\docomplexstarttekstblok[#1]}%
   \setvalue{\e!stop#1\e!tekst}%
     {\dostoptextfloat}%
   \setvalue{\e!start\e!reserveer#1\e!tekst}%
     {\doquadrupleempty\docomplexstartreserveertekstblok[#1]}%
   \setvalue{\e!stop\e!reserveer#1\e!tekst}%
     {\dostoptextfloat}%
   \setvalue{\e!lege#1}%
     {\doleegblok{#1}}%
   \setvalue{\e!leeg#1}%
     {\doleegblok{#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!breedte,\c!hoogte,%\c!lokaal,
      \c!maxbreedte,\c!maxhoogte,\c!minbreedte,
      \c!marge,\c!zijvoorwit,\c!zijnawit,\c!zijuitlijnen,
      \c!linkermargeafstand,\c!rechtermargeafstand,\c!criterium,
      \c!kader,\c!straal,\c!hoek,\c!plaats,\c!achtergrond,\c!kaderkleur,
      \c!achtergrondraster,\c!achtergrondkleur,\c!achtergrondoffset,
      \c!bovenkader,\c!onderkader,\c!linkerkader,\c!rechterkader,
      \c!kaderoffset,\c!paginaovergangen,\c!default,
      \c!tekstmethode,\c!zijmethode]%
   \copyparameters[\??kj#1][\??kj#3]
     [\c!plaats,\c!voor,\c!tussen,\c!na,
      \c!breedte,\c!kopletter,\c!kopkleur,\c!letter,\c!kleur,
      \c!tekstletter,\c!tekstkleur,
      \c!uitlijnen,\c!nummer,\c!wijze,\c!blokwijze,
      \c!sectienummer,\c!scheider,\c!afstand,\c!conversie]%
   \definieernummer[#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\dogetfloatdata % precedes save !
%   {\doglobal\increment\noffloatpages
%    \gettwopassdata{\s!float\s!data}%
%    \iftwopassdatafound
%      \globallet\twopassfloatdata\twopassdata
%    \else
%      \globallet\twopassfloatdata\realpageno % \realfolio
%    \fi}
% 
% \def\dosavefloatdata
%   {\bgroup
%    \edef\dosavefloatdata
%      {\writeutilitycommand
%         {\twopassentry
%            {\s!float\s!data}%
%            {\noffloatdata}%
%            {\noexpand\realfolio}}}% later {}{}{}{} and \getfirst...
%    \dosavefloatdata
%    \egroup}

\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 \startuitstellen...\stopuitstellen 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

%\def\checkwaitingfloats#1%
%  {\ifsomefloatwaiting
%     \doifinsetelse{\v!altijd}{#1}
%       {\showmessage{\m!floatblocks}{5}{}}
%       {\doflushfloats}%
%   \fi}

\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 \@@bknawit is present
  {\bgroup
   \setbox0\vbox{\strut\blanko[\@@bkvoorwit]\strut}%
   \setbox2\vbox{\strut\blanko[\@@bknawit  ]\strut}%
   \ifdim\ht0>\ht2
     \blanko[-\@@bknawit,\@@bkvoorwit]
   \fi
   \egroup}

\def\doplacefloatbox
  {%\forgetall % NJET!
   \witruimte
   \blanko[\@@bkvoorwit]
   \flushfloatbox
   \blanko[\@@bknawit]}

\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 \teksthoogte
        {\doifnotinset\v!hoog{#1}\vfill
         \box\floatbox
         \doifnotinset\v!laag{#1}\vfill}%
      \goodbreak}%
   \doinsertfloatinfo}

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

\def\dodostoptextfloat[#1]%  % de tekst kan beter in een soort
  {\egroup                   % kadertekst zonder kader, is flexibeler
   \doifnotinset\v!lang{#1}% en beter
     {\ifdim\ht\floattext<\floatheight
        \floattextheight\floatheight
      \else
        \floattextheight\ht\floattext
      \fi}%
   \setbox\floatbox\vbox to \floattextheight
     {\hsize\floatwidth
      \doifinsetelse\v!beide{#1}%
        {\doifinsetelse\v!laag{#1}
           {\vfill\box\floatbox}
           {\doifinsetelse\v!midden{#1}
              {\vfill\box\floatbox\vfill}
              {\box\floatbox\vfill}}}
        {\box\floatbox\vfill}}%
    \setbox\floattext\vbox to \floattextheight
     {\hsize\floattextwidth
      \doifinsetelse\v!laag{#1}
        {\vfill
         \box\floattext
         \doifinset\c!offset{#1}{\witruimte\blanko}}
        {\doifinsetelse\v!midden{#1}
           {\vfill
            \box\floattext
            \vfill}
           {\doifinset\v!offset{#1}{\witruimte\blanko}%
            \box\floattext
            \vfill}}}%
   \doifinsetelse\v!rechts{#1}%
     {\setbox\floatbox\hbox to \hsize
        {\box\floattext
         \hfill
         \box\floatbox}}
     {\setbox\floatbox\hbox to \hsize
        {\box\floatbox
         \hfill
         \box\floattext}}%
   \baselinecorrection
   \witruimte
   \blanko[\@@bkvoorwit]%
   \doifnotinset\v!lang{#1}%
     {\dp\floatbox\openstrutdepth}% dp\strutbox}%      % toegevoegd
   \box\floatbox
   \blanko[\@@bknawit]%
   \endgroup % **
   \doinsertfloatinfo}

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

\def\someelsefloat[#1]%
  {\doifinsetelse\v!hier{#1}
     {\doifinsetelse\v!altijd{#1}
        {\pagina[\v!voorkeur]%
         \docheckiffloatfits
         \ifroomforfloat
           \placesomeherefloat[#1]%
         \else
           \showmessage\m!floatblocks9\empty
           \doreversesavefloat
         \fi}
        {\ifsomefloatwaiting
           \dosavefloat
         \else
           \pagina[\v!voorkeur]%
           \docheckiffloatfits
           \ifroomforfloat
             \placesomeherefloat[#1]%
           \else
             \dosavefloat
           \fi
         \fi}}
     {\doifinsetelse\v!altijd{#1}
        {\docheckiffloatfits
         \ifroomforfloat
           \processallactionsinset
             [#1]
             [   \v!boven=>{\placesometopsfloat[#1]},
                 \v!onder=>{\placesomebotsfloat[#1]},
               \s!unknown=>{\placesomeherefloat[#1]},
               \s!default=>{\placesomeherefloat[#1]}]%
         \else
           \showmessage\m!floatblocks9\empty
           \doreversesavefloat
         \fi}
        {\docheckiffloatfits
         \ifroomforfloat
           \processallactionsinset
             [#1]
             [  \v!boven=>{\placesometopsfloat[#1]},
                \v!onder=>{\placesomebotsfloat[#1]},
              \s!unknown=>{\placesomeherefloat[#1]},
              \s!default=>{\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} % {\dp\strutbox}

\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!geen
        {\global#1\zeropoint}
        {\setbox0\vbox{\witruimte\@EA\blanko\@EA[#2]}%
         \global#1\ht0}}}

\def\calculatefloatskips#1%
  {{\docalculatefloatskip\floattopskip       \@@bkvoorwit
    \docalculatefloatskip\floatbottomskip    \@@bknawit
    \docalculatefloatskip\sidefloattopskip   {\getvalue{\??fl#1\c!zijvoorwit}}% \@@bkzijvoorwit
    \docalculatefloatskip\sidefloatbottomskip{\getvalue{\??fl#1\c!zijnawit}}% \@@bkzijnawit
    \gdef\sidefloattopoffset{\openstrutdepth}% was \def
    \global\floatsideskip\getvalue{\??fl#1\c!marge}%
    \global\sidefloatleftshift \getvalue{\??fl#1\c!linkermargeafstand}%
    \global\sidefloatrightshift\getvalue{\??fl#1\c!rechtermargeafstand}%
    \global\noftopfloats \@@bknboven\relax
    \global\nofbotfloats \@@bknonder\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!letter\c!kleur{#3}}}%
%    \doifelsevalue{\??kj#1\c!nummer}{\v!ja}
%      {\def\dofloatnummer%
%         {{\xdef\floatcaptionnumber{#1}%
%           \hbox{\doattributes{\??kj#1}\c!kopletter\c!kopkleur
%              {\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
     \verhoognummer[#1]%
     \maakhetnummer[#1]%
     \globallet\flhetnummer\hetnummer
     \setgvalue{@fl@r@#1}%
       {\dofloatreference
        \redofloatorder{#1}%
        \doschrijfnaarlijst{#1}{\flhetnummer}{#3}{#1}%
        \doglobal\convertargument#3\to\flasciititle % \asciititle is global
        \doifsomething{#2}{\rawreference\s!flt{#2}{{\flhetnummer}{\flasciititle}}}%
        \letgvalue{@fl@r@#1}\relax}% nills
     \setgvalue{@fl@t@#1}%
       {\preparethenumber{\??kj#1}\flhetnummer\preparednumber
        \doattributes{\??kj#1}\c!letter\c!kleur
          {\doattributes{\??kj#1}\c!kopletter\c!kopkleur
             {\labeltexts{#1}{\preparednumber}}%
           \doattributes{\??kj#1}\c!tekstletter\c!tekstkleur
             {\dotfskip\@@kjkjafstand#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!breedte=\@@bkbreedte,
      \c!hoogte=\@@bkhoogte,
      \c!plaats=\v!normaal,
      \c!offset=\@@bkoffset]%
     {\box\floatbox}}

\newbox\captionbox

\def\putcompletecaption#1#2#3#4%
  {\noindent
   \xdef\floatcaptionnumber{#1}%
   \dostartattributes{\??kj#1}\c!letter\c!kleur\empty
     \ifnofloatnumber
     \else
       \hbox{\doattributes{\??kj#1}\c!kopletter\c!kopkleur{\strut#2\floatcaptionsuffix}}%
       \ifnofloatcaption \else \ifemptyfloatcaption \else
         \ifcase#4\relax
           \scratchskip\@@kjkjafstand\relax
           \dotfskip\scratchskip\emergencystretch.5\scratchskip
         \else
           \ifx\@@kjkjtussen\empty\else\unskip\@@kjkjtussen\fi
         \fi
       \fi \fi
     \fi
     \ifnofloatcaption \else
       \doattributes{\??kj#1}\c!tekstletter\c!tekstkleur
         {\begstrut#3\endstrut\endgraf}%
     \fi
   \dostopattributes}

% new

\newbox\tempfloatbox
\newbox\tempcaptionbox

%\stelblokkopjesin[\c!breedte=5cm]
%\stelblokkopjesin[\c!uitlijnen=\v!links]
%\stelblokkopjesin[\c!uitlijnen=\v!rechts]

% \def\dosetpagfloat#1#2#3#4% \copy wegwerken
%   {\bgroup
% \setlocalfloathsize
%    \forgetall
%    \postponefootnotes
%    \mindermeldingen
%    \setbox\tempfloatbox\vbox{\borderedfloatbox{#4}}%
%    \def\locatefloat{\doregelplaats\@@flflplaats}%
%    \ifnofloatcaption
%      \dopreparenocaption{#1}{#2}{#3}{#4}
%      \edef\width{\the\wd\floatbox}%
%    \else
%      \setbox\tempcaptionbox\hbox
%        {\footnotesenabledfalse\putcompletecaption{#4}{#2}{#3}{0}}%
%      \doifinsetelse\@@kjkjplaats{\v!hoog,\v!midden,\v!laag}
%        {\dopreparesidecaption{#1}{#2}{#3}{#4}}
%        {\doifelse\@@kjkjbreedte\v!max
%           {\dopreparestackcaptionmax{#1}{#2}{#3}{#4}}
%           {\ifdim\wd\tempcaptionbox>\wd\tempfloatbox % wider caption
%              \doifelse\@@kjkjbreedte\v!passend
%                {\dopreparestackcaptionaut{#1}{#2}{#3}{#4}}
%                {\dopreparestackcaptionwid{#1}{#2}{#3}{#4}}%
%            \else
%              \dopreparestackcaptionmin{#1}{#2}{#3}{#4}%
%            \fi}}%
%      \edef\width{\the\wd\tempfloatbox}%
%      \buildfloatbox
%    \fi
%    \doglobal\addlocalbackgroundtobox\floatbox
%    \postcenterfloatbox\width
%    \egroup}

\def\dosetpagfloat#1#2#3#4% \copy wegwerken
  {\bgroup
\setlocalfloathsize
   \forgetall
   \postponefootnotes
   \mindermeldingen
   \setbox\tempfloatbox\vbox{\borderedfloatbox{#4}}%
   \def\locatefloat{\doregelplaats\@@flflplaats}%
   \ifnofloatcaption
     \dopreparenocaption{#1}{#2}{#3}{#4}
     \edef\width{\the\wd\floatbox}%
\doglobal\addlocalbackgroundtobox\floatbox
   \else
     \setbox\tempcaptionbox\hbox
       {\footnotesenabledfalse\putcompletecaption{#4}{#2}{#3}{0}}%
     \doifinsetelse\@@kjkjplaats{\v!hoog,\v!midden,\v!laag}
       {\dopreparesidecaption{#1}{#2}{#3}{#4}}
       {\doifelse\@@kjkjbreedte\v!max
          {\dopreparestackcaptionmax{#1}{#2}{#3}{#4}}
          {\ifdim\wd\tempcaptionbox>\wd\tempfloatbox % wider caption
             \doifelse\@@kjkjbreedte\v!passend
               {\dopreparestackcaptionaut{#1}{#2}{#3}{#4}}
               {\dopreparestackcaptionwid{#1}{#2}{#3}{#4}}%
           \else
             \dopreparestackcaptionmin{#1}{#2}{#3}{#4}%
           \fi}}%
     \edef\width{\the\wd\tempfloatbox}%
\doglobal\addlocalbackgroundtobox\tempfloatbox
\doglobal\addlocalbackgroundtobox\tempcaptionbox
      \buildfloatbox
   \fi
%   \doglobal\addlocalbackgroundtobox\floatbox
   \postcenterfloatbox\width
   \egroup}

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

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

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

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

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

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

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

\def\dopreparesidecaption#1#2#3#4%
  {\dimen0\hsize
   \advance\dimen0 -\wd\tempfloatbox
   \advance\dimen0 -\@@bkmarge\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
        [\@@kjkjplaats]
        [ \v!boven=>\locatefloat{\box\tempcaptionbox}%
                    \endgraf\@@kjkjtussen
                    \locatefloat{\box\tempfloatbox},
          \v!onder=>\locatefloat{\box\tempfloatbox}%
                    \endgraf\@@kjkjtussen
                    \locatefloat{\box\tempcaptionbox},
           \v!hoog=>\locatefloat
                      {\doifelse\@@flflplaats\v!links
                         {\box\tempfloatbox
                          \dotfskip\@@kjkjafstand
                          \vbox to\ht\tempfloatbox{\@@kjkjtussen\box\tempcaptionbox\vfill}}
                         {\vbox to\ht\tempfloatbox{\@@kjkjtussen\box\tempcaptionbox\vfill}%
                          \tfskip
                          \box\tempfloatbox}},
           \v!laag=>\locatefloat
                      {\doifelse\@@flflplaats\v!links
                         {\box\tempfloatbox
                          \dotfskip\@@kjkjafstand
                          \vbox to\ht\tempfloatbox
                            {\vfill\box\tempcaptionbox\@@kjkjtussen}}
                         {\vbox to\ht\tempfloatbox
                            {\vfill\box\tempcaptionbox\@@kjkjtussen}%
                          \dotfskip\@@kjkjafstand
                          \box\tempfloatbox}},
         \v!midden=>\locatefloat
                      {\doifelse\@@flflplaats\v!links
                         {\box\tempfloatbox
                          \dotfskip\@@kjkjafstand
                          \vbox to\ht\tempfloatbox{\vfill\box\tempcaptionbox\vfill}}
                         {\vbox to\ht\tempfloatbox{\vfill\box\tempcaptionbox\vfill}%
                          \dotfskip\@@kjkjafstand
                          \box\tempfloatbox}},
        \s!unknown=>\locatefloat{\box\tempfloatbox},
           \v!geen=>\locatefloat{\box\tempfloatbox}]}}

\newif\ifpostponecolumnfloats \postponecolumnfloatsfalse % don't change

%\def\postcenterfloatbox#1%
%  {\ifbinnenkolommen
%     \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%
%  {\ifbinnenkolommen
%     \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%
  {\ifbinnenkolommen
     \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
     {\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
%    \mindermeldingen
%    %\showcomposition
%    \setbox4\vbox{\borderedfloatbox{#4}}%
%    \ifnofloatcaption
%      \global\setbox\floatbox\vbox{\box4}%
%    \else
%      \setbox2\hbox
%        {\forgetall\putcompletecaption{#4}{#2}{#3}{0}}%
%      \doifelse\@@kjkjbreedte\v!max
%        {\dosetraggedvbox\@@kjkjuitlijnen
%         \setbox2\raggedbox
%           {\hsize\wd4\putcompletecaption{#4}{#2}{#3}{0}}}%
%        {\doifelse\@@kjkjbreedte\v!passend
%           {\ifdim\wd2>\wd4\relax
%              \setbox2\vbox
%                {\forgetall\hsize\wd4\putcompletecaption{#4}{#2}{#3}{0}}%
%            \else
%              \setbox2\hbox to \wd4
%                {\hss\box2\hss}%
%            \fi}
%           {\dosetraggedvbox\@@kjkjuitlijnen
%            \setbox2\raggedbox
%              {\hsize\wd4\putcompletecaption{#4}{#2}{#3}{0}}}}%
%      \global\setbox\floatbox\vbox
%        {\processaction
%           [\@@kjkjplaats]
%           [ \v!boven=>\box2\endgraf\@@kjkjtussen\box4,
%             \v!onder=>\box4\endgraf\@@kjkjtussen\box2,
%              \v!geen=>\box4,
%           \s!unknown=>\box4\endgraf\@@kjkjtussen\box2]}%
%    \fi
%    \doglobal\addlocalbackgroundtobox\floatbox
%    \egroup}

\def\dosetparfloat#1#2#3#4%
  {\bgroup
   \forgetall
   \postponefootnotes
   \mindermeldingen
   %\showcomposition
   \setbox\tempfloatbox\vbox{\borderedfloatbox{#4}}%
\doglobal\addlocalbackgroundtobox\tempfloatbox
   \ifnofloatcaption
     \global\setbox\floatbox\vbox{\box\tempfloatbox}%
   \else
     \setbox\tempcaptionbox\hbox
       {\forgetall\putcompletecaption{#4}{#2}{#3}{0}}%
     \doifelse\@@kjkjbreedte\v!max
       {\dosetraggedvbox\@@kjkjuitlijnen
        \setbox\tempcaptionbox\raggedbox
          {\hsize\wd\tempfloatbox\putcompletecaption{#4}{#2}{#3}{0}}}%
       {\doifelse\@@kjkjbreedte\v!passend
          {\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\@@kjkjuitlijnen
           \setbox\tempcaptionbox\raggedbox
             {\hsize\wd\tempfloatbox\putcompletecaption{#4}{#2}{#3}{0}}}}%
\doglobal\addlocalbackgroundtobox\tempcaptionbox
     \global\setbox\floatbox\vbox
       {\processaction
          [\@@kjkjplaats]
          [ \v!boven=>\box\tempcaptionbox\endgraf\@@kjkjtussen\box\tempfloatbox,
            \v!onder=>\box\tempfloatbox\endgraf\@@kjkjtussen\box\tempcaptionbox,
             \v!geen=>\box\tempfloatbox,
          \s!unknown=>\box\tempfloatbox\endgraf\@@kjkjtussen\box\tempcaptionbox]}%
   \fi
%   \doglobal\addlocalbackgroundtobox\floatbox
   \egroup}

\newif\ifparfloat

\long\def\dosetfloatbox#1#2#3#4% todo : \global\setbox
  {\ifvisible
     \par
     \edef\@@kjkjbreedte  {\getvalue{\??kj#4\c!breedte  }}%
      \def\@@kjkjtussen   {\getvalue{\??kj#4\c!tussen   }}%  no \edef
     \edef\@@kjkjafstand  {\getvalue{\??kj#4\c!afstand  }}%
     \edef\@@kjkjplaats   {\getvalue{\??kj#4\c!plaats   }}%
     \edef\@@kjkjuitlijnen{\getvalue{\??kj#4\c!uitlijnen}}%
     \edef\@@flflplaats   {\getvalue{\??fl#4\c!plaats   }}%
     \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!marge{#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<\teksthoogte
     \else
       \global\floatheight\teksthoogte
       \global\advance\floatheight -\lineheight
       \ht\floatbox\floatheight
       \dp\floatbox\zeropoint
       \showmessage\m!floatblocks{10}{\the\totalnoffloats}%
     \fi
   \fi}

\newcounter\noxfloatlocations

% \def\dogetfloatbox#1#2%
%   {\ifvisible
%      \let\next\relax % ivm eetex
%      \processfirstactioninset
%        [#1]
%        [    \v!hier=>\def\next{\someelsefloat[#1]},
%          \v!forceer=>\def\next{\somefixdfloat[#1]},
%            \v!links=>\def\next{\somesidefloat[#1]\presetindentation},
%           \v!rechts=>\def\next{\somesidefloat[#1]},
%            \v!tekst=>\def\next{\sometextfloat[#1]},
%            \v!boven=>\def\next{\someelsefloat[#1]\nonoindentation}, % !
%            \v!onder=>\def\next{\someelsefloat[#1]},
%            \v!marge=>\def\next{\somenextfloat[#1]\nonoindentation}, % !
%           \v!pagina=>\def\next{\somepagefloat[#1]},
%            \v!naast=>\def\next{\somefacefloat[#1]},
%          \v!inmarge=>\def\next{\somesidefloat[#1]},
%         \v!inlinker=>\def\next{\somesidefloat[#1]},
%        \v!inrechter=>\def\next{\somesidefloat[#1]},
%      \v!linkermarge=>\def\next{\somesidefloat[#1]},
%     \v!rechtermarge=>\def\next{\somesidefloat[#1]},
%       \v!linkerrand=>\def\next{\somesidefloat[#1]},
%      \v!rechterrand=>\def\next{\somesidefloat[#1]},
%          \s!default=>\def\next{\someelsefloat[\v!hier,#1]},
%          \s!unknown=>\def\next{\someelsefloat[\v!hier,#1]}]%
%      \next
%    \fi}

\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!hier        % see details/pascal
      \else
        \dogetfloatbox{#1}\empty
      \fi}}

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

% \long\def\docompletefloat#1#2#3#4#5#6#7% #7 = box number
%   {\flushsidefloats
%    \presetfloatvariables{#1}{#4}{#2}{#6}%
%    \bgroup
%   %\setlocalfloatdimensions{#1}{#4}{#7}\relax
%    \global\setbox\floatbox\vbox{\box#7}%
%    \xdef\naturalfloatheight{\the\ht\floatbox}%
%    \xdef\naturalfloatwidth {\the\wd\floatbox}%
%    \xdef\naturalfloatdepth {\the\dp\floatbox}%
%    \dimen0 \ht\floatbox
%    \advance\dimen0 \dp\floatbox
%    \ifdim\dimen0=\zeropoint
%      \showmessage\m!floatblocks{11}\empty
%      \global\setbox\floatbox\vbox{\getvalue{\e!lege#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
% %     \doifelsevalue{\??kj#1\c!nummer}\v!ja
%      \ifnofloatnumber
%        \global\setbox\floatbox\vbox
%          {\unvbox\floatbox % no \vss, keep the depth
%           \rawreference\s!flt{#2}{{}{\asciititle}}}%
%        \egroup
%        \dofloat{#4}{}{#6}{#1}%
%      \else
%        \verhoognummer[#1]%
%        \maakhetnummer[#1]%
%        \global\setbox\floatbox\vbox
%          {\unvbox\floatbox % no \vss, keep the depth
%           \dofloatreference
%           \redofloatorder{#1}%
%           \rawreference\s!flt{#2}{{\hetnummer}{\asciititle}}%
%           \doschrijfnaarlijst{#3}{\hetnummer}{#6}{#3}}%
%        \egroup
%        \preparethenumber{\??kj#1}\hetnummer\preparednumber
%        \dofloat{#4}{\labeltexts{#5}{\preparednumber}}{#6}{#1}%
%      \fi
%    \fi
%    \global\insidefloatfalse}

\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
   \presetfloatvariables{#1}{#4}{#2}{#6}%
   \bgroup
   \setnaturalfloatdimensions#7% 
   \global\setbox\floatbox\vbox
     {\executeifdefined{\??fl#1\c!commando}\firstofoneargument{\box#7}}%
   \setnaturalfloatdimensions\floatbox
   \dimen0 \ht\floatbox
   \advance\dimen0 \dp\floatbox
   \ifdim\dimen0=\zeropoint
     \showmessage\m!floatblocks{11}\empty
     \global\setbox\floatbox\vbox{\getvalue{\e!lege#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
       \verhoognummer[#1]%
       \maakhetnummer[#1]%
       \global\setbox\floatbox\vbox
         {\unvbox\floatbox % no \vss, keep the depth
          \dofloatreference
          \redofloatorder{#1}%
          \rawreference\s!flt{#2}{{\hetnummer}{\asciititle}}%
          \doschrijfnaarlijst{#3}{\hetnummer}{#6}{#3}}%
       \egroup
       \preparethenumber{\??kj#1}\hetnummer\preparednumber
       \dofloat{#4}{\labeltexts{#5}{\preparednumber}}{#6}{#1}%
     \fi
   \fi
   \global\insidefloatfalse}

\newif\ifmargeblokken

\def\dostelmargeblokkenin[#1]%
  {\getparameters[\??mb][#1]%
   \doifelse\@@mbstatus\v!start
     {\showmessage\m!layouts4\empty
      \margeblokkentrue
      \let\somenextfloat\dosomenextfloat
      \let\startmargeblok\dostartmargeblok
      \let\stopmargeblok\dostopmargeblok}%
     {\showmessage\m!layouts5\empty
      \margeblokkenfalse
      \def\somenextfloat[##1]%
        {\someelsefloat[##1,\v!hier]}%
      \let\startmargeblok\dontstartmargeblok
      \let\stopmargeblok\dontstopmargeblok}}

\def\stelmargeblokkenin
  {\dosingleargument\dostelmargeblokkenin}

\newbox\marginbox

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

\newbox\preparedmarginbox

\def\reshapemargin
  {\ifdim\ht\preparedmarginbox>\zeropoint
     \beginofshapebox
     \unvbox\preparedmarginbox
     \endofshapebox
     \reshapebox
       {\box\shapebox}%
     \setbox\preparedmarginbox\vbox to \teksthoogte
       {\@@mbboven
        \flushshapebox
        \@@mbonder}%
   \fi}

\def\plaatsrechtermargeblok
  {\hskip\rechtermargebreedte}

\def\plaatslinkermargeblok
  {\hskip\linkermargebreedte}

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

\def\docheckmargeblokken % erg inefficient
  {\setbox\preparedmarginbox\vbox
     {\forgetall
      \splittopskip\topskip
      \ifvoid\marginbox\else
        \ifdim\ht\marginbox>\teksthoogte
          \vsplit\marginbox to \teksthoogte
        \else
          \unvbox\marginbox
        \fi
      \fi}%
   \reshapemargin
   \setbox\preparedmarginbox\vbox
      {\@@mbvoor\box\preparedmarginbox\@@mbna}%
   \def\rightmarginbox
     {\def\plaatsrechtermargeblok
        {\setbox\preparedmarginbox\hbox to \rechtermargebreedte
           {\@@mblinks\box\preparedmarginbox\@@mbrechts}%
         \vsmashbox\preparedmarginbox
         \box\preparedmarginbox}}%
   \def\leftmarginbox
     {\def\plaatslinkermargeblok
        {\setbox\preparedmarginbox\hbox to \linkermargebreedte
           {\@@mbrechts\box\preparedmarginbox\@@mblinks}%
         \vsmashbox\preparedmarginbox
         \box\preparedmarginbox}}%
   \processaction % traag
     [\@@mbplaats]
     [ \v!inmarge=>\doifbothsidesoverruled
                     \rightmarginbox
                   \orsideone
                     \rightmarginbox
                   \orsidetwo
                     \leftmarginbox
                   \od,
        \v!midden=>\doifbothsidesoverruled
                     \rightmarginbox
                   \orsideone
                     \leftmarginbox
                   \orsidetwo
                     \rightmarginbox
                   \od,
         \v!links=>\leftmarginbox,
        \v!rechts=>\rightmarginbox,
       \s!unknown=>\setbox\preparedmarginbox\hbox{}]}

\def\dostartmargeblok % 2 maal \vbox ivm \unvbox elders
  {\global\setbox\marginbox\vtop\bgroup\vbox\bgroup
     \hsize\@@mbbreedte
     \ifvoid\marginbox\else
       \unvbox\marginbox
       \@@mbtussen
     \fi
     \steluitlijnenin[\@@mbuitlijnen]%
     \dostartattributes\??mb\c!letter\c!kleur{}%
     \begstrut\ignorespaces}

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

\def\dontstartmargeblok
  {\@@mbvoor
   \bgroup
   \dostartattributes\??mb\c!letter\c!kleur\empty}

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

\newif\ifinuitstellen

\newevery\everytopofpage\relax

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

% \def\douitstellen
%   {\the\everytopofpage
%    \ifvoid\collectedpagefloats\else
%      % message
%      \unvbox\collectedpagefloats
%    \fi
%    \ifinuitstellen\else\ifcase\nofpostponedblocks\else % The \nof-test is
%      \bgroup % we need the color/font switch, else problems inside split verbatim
%      \setnormalcatcodes % uitstellen in verbatim
%      \edef\savedtopofpagecolor{\topofpagecolor}%
%      \doifsomething\savedtopofpagecolor\restorecolormode % \stopcolormode
%      \restoreglobalbodyfont
%      \global\pagetotal\zeropoint                   % recently added and
%      \global\inuitstellentrue                      % definitely needed else
%      \dorecurse\nofpostponedblocks                 % we can loose or disorder
%        {\haalbuffer[buf-\recurselevel]}            % floats; anyhow, this
%      \doflushfloats % new but potential dangerous  % mechanism is still
%      \doglobal\newcounter\nofpostponedblocks       % suboptimal and needs a
%      \global\inuitstellenfalse                     % proper analysis
%      \doifsomething\savedtopofpagecolor\startcolormode\savedtopofpagecolor
%      \egroup
%    \fi\fi}
% 
% \setvalue{\e!start\e!uitstellen}%
%   {\doglobal\increment\nofpostponedblocks
%    \showmessage\m!layouts3\nofpostponedblocks
%    \dostartbuffer[buf-\nofpostponedblocks]
%      [\e!start\e!uitstellen][\e!stop\e!uitstellen]}

% \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

\def\douitstellen
  {\the\everytopofpage
   \ifvoid\collectedpagefloats\else
     % message
     \unvbox\collectedpagefloats
   \fi
   \donefalse
   \ifinuitstellen \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 % uitstellen in verbatim
     \edef\savedtopofpagecolor{\topofpagecolor}%
     \doifsomething\savedtopofpagecolor\restorecolormode % \stopcolormode
     \restoreglobalbodyfont                        % The \nof-test is
     \global\pagetotal\zeropoint                   % recently added and
     \global\inuitstellentrue                      % definitely needed else
     \the\postponedpageblocks                      % we can loose or disorder
     \dorecurse\nofpostponedblocks                 % floats; anyhow, this
       {\haalbuffer[pbuf-\recurselevel]}           % mechanism is still
     \doflushfloats % new but potential dangerous  % suboptimal and needs a
     \doglobal\newcounter\nofpostponedblocks       % proper analysis
     \global\inuitstellenfalse                     
     \doifsomething\savedtopofpagecolor\startcolormode\savedtopofpagecolor
     \egroup
   \fi}

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

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

\setvalue{\e!start\e!uitstellen}%
  {\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\e!uitstellen][\e!stop\e!uitstellen]}

\def\dodostartpostponing
  {\doglobal\increment\nofpostponedblocks
   \showmessage\m!layouts3\nofpostponedblocks
   \dostartbuffer[pbuf-\nofpostponedblocks]%
     [\e!start\e!uitstellen][\e!stop\e!uitstellen]}

\definieernummer
  [\??si]
  [\c!wijze=\v!per\v!tekst,
   \c!conversie=\@@siconversie]

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

% ook (continued)

\def\dosplitsplaatsblok[#1]#2% nog dubbele refs
  {\ifbinnenkolommen         % tzt ook nog figuren splitten
     % not yet supported
   \else
     \bgroup
     \insidefloattrue
     \getparameters[\??si][#1]%
     \resetnummer[\??si]%
     \def\floatcaptionsuffix{\nummer[\??si]}%
     \TABLEcaptionheight=\@@siregels\lineheight % brrr
     \simplifypagebreak % \page becomes \goodbreak
     \dowithnextbox
       {\forgetall
        \mindermeldingen
        \doloop
          {\setbox2\vsplit\nextbox to \lineheight
           \setbox2\vbox{\unvbox2}
           \ifdim\ht2>\lineheight
             \verhoognummer[\??si]%
             \ifnum\ruwenummer[\??si]=1 \ifdim\nextboxht=\zeropoint
               \let\floatcaptionsuffix\empty
             \fi \fi
             \bgroup
             #2{\unvbox2}
             \egroup
             \ifdim\nextboxht>\zeropoint
               \pagina
               \verlaagnummer[\floatcaptionnumber]%
             \fi
           \fi
           \ifdim\nextboxht>\zeropoint\else
             \expandafter\exitloop
           \fi}%
        \egroup}
     \vbox
   \fi}

\def\splitsplaatsblok%
  {\dosingleempty\dosplitsplaatsblok}

\def\dooutput{\sidefloatoutput}           % redefinition of \dooutput

\stelmargeblokkenin
  [\c!status=\v!start,
   \c!plaats=\v!inmarge,
   \c!breedte=\rechtermargebreedte,
   \c!letter=,
   \c!kleur=,
   \c!uitlijnen=,
   \c!links=,
   \c!rechts=,
   \c!boven=,
   \c!tussen=\blanko,
   \c!onder=\vfill,
   \c!voor=,
   \c!na=]

\definieerplaatsblok
  [\v!figuur]
  [\v!figuren]

\definieerplaatsblok
  [\v!tabel]
  [\v!tabellen]

\stelplaatsblokin
  [\v!tabel]
  [\c!kader=\v!uit]

\definieerplaatsblok
  [\v!intermezzo]
  [\v!intermezzos]

\definieerplaatsblok
  [\v!grafiek]
  [\v!grafieken]

\stelblokkopjesin
  [\c!plaats=\v!onder,
   \c!voor=\blanko,
   \c!tussen={\blanko[\v!middel]},
   \c!na=\blanko,
   \c!breedte=\v!passend,
   \c!kopletter=\v!vet,
   \c!kopkleur=,
   \c!letter=\v!normaal,
   \c!kleur=,
   \c!tekstletter=,
   \c!tekstkleur=,
   \c!uitlijnen=,
   \c!nummer=\v!ja,
   \c!wijze=\@@nrwijze,
   \c!blokwijze=\@@nrblokwijze,
   \c!sectienummer=\@@nrsectienummer,
   \c!scheider=\@@koscheider,
   \c!afstand=1em,
   \c!conversie=\v!cijfers]

\stelplaatsblokkenin
  [\c!plaats=\v!midden,
   \c!breedte=\v!passend,
   \c!hoogte=\v!passend,
   \c!offset=\v!overlay,
   \c!kader=\v!uit,
   \c!straal=.5\korpsgrootte,
   \c!hoek=\v!recht,
   \c!achtergrond=,
   \c!achtergrondraster=\@@rsraster,
   \c!achtergrondkleur=,
   \c!achtergrondoffset=\!!zeropoint,
   \c!bovenkader=,
   \c!onderkader=,
   \c!linkerkader=,
   \c!rechterkader=,
   \c!kaderoffset=\!!zeropoint,
   \c!voor=,
   \c!na=,
   \c!voorwit=\v!groot,
   \c!nawit=\v!groot,
   \c!zijvoorwit=\@@bkvoorwit,
   \c!zijnawit=\@@bknawit,
   \c!zijuitlijnen=\v!normaal,
   \c!tekstmethode=\ifgridsnapping2\else0\fi, % 0=raw 1=safe (.99pg) 2=tight (-1pt) 
   \c!zijmethode=\ifgridsnapping2\else1\fi,   % 0=raw 1=safe (.99pg) 2=tight (-1pt) 
   \c!springvolgendein=\v!nee,
   \c!marge=1em,
   \c!linkermargeafstand=\zeropoint,
   \c!rechtermargeafstand=\@@bklinkermargeafstand,
   \c!nboven=2,
   \c!nonder=0,
   \c!nregels=4,
   \c!lokaal=,
   \c!default=\v!figuur]

\stelplaatsbloksplitsenin
  [\c!conversie=\v!letter, % \v!romeins
   \c!regels=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!hier}%
     \getvalue{\string\floatmethod\floatmethod}[#1]%
   \fi}

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

\installfloathandler \v!hier         \someherefloat
\installfloathandler \v!forceer      \somefixdfloat
\installfloathandler \v!links        \someleftsidefloat
\installfloathandler \v!rechts       \somerightsidefloat
\installfloathandler \v!tekst        \sometextfloat
\installfloathandler \v!boven        \sometopfloat
\installfloathandler \v!onder        \somebottomfloat
\installfloathandler \v!marge        \somemarginfloat
\installfloathandler \v!naast        \somefacefloat
\installfloathandler \v!pagina       \somepagefloat
\installfloathandler \v!inmarge      \someinmarginfloat
\installfloathandler \v!inlinker     \someinleftmarginfloat
\installfloathandler \v!inrechter    \someinrightmarginfloat
\installfloathandler \v!linkermarge  \someinleftmarginfloat
\installfloathandler \v!rechtermarge \someinrightmarginfloat
\installfloathandler \v!linkerrand   \someinleftedgefloat
\installfloathandler \v!rechterrand  \someinrightedgefloat

\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!hier,#1]}

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