%D \module
%D   [       file=page-sid,
%D        version=2000.10.20,
%D          title=\CONTEXT\ OTR Macros,
%D       subtitle=Side Floats,
%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 / Side Floats}

\unprotect

% These macro deal with side floats. We started with Daniel
% Comenetz macros as published in TUGBoat Volume 14 (1993),
% No.\ 1: Anchored Figures at Either Margin. I extended and
% patched the macros to suite our needs which results in a
% messy module. Therefore, this module badly needs an update
% because it's now a mixture of old and new macros.

% afhankelijke variabelen
%
% \overgap    vervangen door   \floatsidetopskip
% \sidegap    vervangen door   \floatsideskip
% \undergap   vervangen door   \floatsidebottomskip
%
% \prskp      vervangen door   \ctxparskip

% toegevoegde macro's/aanroepen
%
% \logsidefloat       : loginformatie
% \flushsidefloats    : nodig voor koppen

% recente wijzigingen:
%
% namen aangepast: \float... enz. i.p.v. \pic

% Pas op: \EveryPar{\EveryPar{}\margetitel{whatever}}
% \plaatsfiguur[links]{}{} moet goed gaan. In dat geval
% begint de tekst terecht wat lager.

\newdimen\sidefloatheight      % includes the topskip
\newdimen\sidefloatwidth
\newdimen\sidefloathsize
\newdimen\sidefloatshift
\newdimen\sidefloatextrashift
\newdimen\sidefloatvsize       \def\nofloatvsize{-1pt }
\newdimen\sidefloatprogress
\newdimen\sidefloatpagetotal

\newbox\floatbottom

\newcount\sidefloatsidelines
\newcount\sidefloatlinesdone

% 1 = backspace
% 2 = leftedge
% 3 = leftmargin
% 4 = leftside
% 5 = rightside
% 6 = rightmargin
% 7 = rightedge
% 8 = cutspace

\chardef\sidefloattype\zerocount

\def\backspacefloat    {\global\chardef\sidefloattype1 \putsidefloat}
\def\leftedgefloat     {\global\chardef\sidefloattype2 \putsidefloat}
\def\leftmarginfloat   {\global\chardef\sidefloattype3 \putsidefloat}
\def\leftfloat         {\global\chardef\sidefloattype4 \putsidefloat}
\def\rightfloat        {\global\chardef\sidefloattype5 \putsidefloat}
\def\rightmarginfloat  {\global\chardef\sidefloattype6 \putsidefloat}
\def\rightedgefloat    {\global\chardef\sidefloattype7 \putsidefloat}
\def\cutspacefloat     {\global\chardef\sidefloattype8 \putsidefloat}

\newif\ifroomforfloat
\newif\iffloatshort
\newif\iffloatflag
\newif\iffloatrighteqo
\newif\iffloatlefteqo

\newdimen\sidefloatleftskip
\newdimen\sidefloatrightskip
\newdimen\sidefloatmaximum

\def\checksidefloatshift
  {\ifdim\sidefloatmaximum>\zeropoint
     \ifcase\sidefloattype
       % invalid
     \or
       % backspace
     \or
       \global\sidefloatshift-\sidefloatmaximum
       \global\advance\sidefloatshift-\rightorleftpageaction \leftedgedistance \rightedgedistance
       \global\advance\sidefloatshift-\rightorleftpageaction \leftmarginwidth \rightmarginwidth
       \global\advance\sidefloatshift-\rightorleftpageaction \leftmargindistance \rightmargindistance
     \or
       \global\sidefloatshift-\sidefloatmaximum
       \global\advance\sidefloatshift-\rightorleftpageaction \leftmargindistance \rightmargindistance
     \or
       % left
     \or
       % right
     \or
       \global\sidefloatshift-\sidefloatmaximum
       \global\advance\sidefloatshift-\rightorleftpageaction \leftmargindistance \rightmargindistance
     \or
       \global\sidefloatshift-\sidefloatmaximum
       \global\advance\sidefloatshift-\rightorleftpageaction \leftedgedistance \rightedgedistance
       \global\advance\sidefloatshift-\rightorleftpageaction \leftmarginwidth \rightmarginwidth
       \global\advance\sidefloatshift-\rightorleftpageaction \leftmargindistance \rightmargindistance
     \or
       % cutspace
     \fi
   \fi}

\def\setsidefloatskips
  {\global\sidefloatrightskip\zeropoint
   \global\sidefloatleftskip \zeropoint
   \ifcase\sidefloattype
   \or % backspace
     \global        \sidefloatleftskip \rightorleftpageaction \backspace \cutspace
   \or % leftedge
     \global        \sidefloatleftskip \rightorleftpageaction \leftmargindistance\rightmargindistance
     \global\advance\sidefloatleftskip \rightorleftpageaction \leftmarginwidth\rightmarginwidth
     \global\advance\sidefloatleftskip \rightorleftpageaction \leftedgedistance\rightedgedistance
   \or % leftmargin
     \global        \sidefloatleftskip \rightorleftpageaction \leftmargindistance\rightmargindistance
   \or % leftside
   \or % rightside
   \or % rightmargin
     \global        \sidefloatrightskip\rightorleftpageaction \rightmargindistance\leftmargindistance
   \or % rightedge
     \global        \sidefloatrightskip\rightorleftpageaction \rightmargindistance\leftmargindistance
     \global\advance\sidefloatrightskip\rightorleftpageaction \rightmarginwidth\leftmarginwidth
     \global\advance\sidefloatrightskip\rightorleftpageaction \rightedgedistance\leftedgedistance
   \or % cutspace
     \global        \sidefloatrightskip\rightorleftpageaction \cutspace\backspace
   \fi
   \ifdim\sidefloatrightskip>\zeropoint
     \doglobal\advance\sidefloatrightskip\rightskip
   \fi
   \ifdim\sidefloatleftskip>\zeropoint
     \doglobal\advance\sidefloatleftskip\leftskip
   \fi}

% eq is still crap

\ifx\normalleqno\undefined

  \let\floatrighteqo=\eqno
  \let\floatleftleqo=\leqno

\else

  \let\floatrighteqo=\normaleqno
  \let\floatleftleqo=\normalleqno

\fi

% Watch it even more! In inner, gaat't mis omdat daar
% pagetotal enz niet zijn aangepast. Inner kan overigens niet
% betrouwbaar worden getest!

% \def\flushsidefloats%
%   {\par
%    \sidefloatprogress=\sidefloatvsize
%    \advance\sidefloatprogress by -\pagetotal
%    \ifdim\sidefloatprogress>\zeropoint
%      \witruimte % nog checken op interferentie
%      \kern\sidefloatprogress
%    \fi
%    \global\sidefloatvsize=\nofloatvsize
%    \global\floatflagfalse}
%
% no, too buggy, leads to top of page crap
%
%\def\flushsidefloats
%  {\par
%   \dochecksidefloat
%   \scratchcounter=-\hangafter
%   \dorecurse{\scratchcounter}{\strut\hfill\strut\par}}
%
%\def\flushsidefloats
%  {\par
%   \!!heighta\sidefloatvsize
%   \advance\!!heighta -\pagetotal
%   \ifdim\!!heighta>\zeropoint
%     % to be checked for interference
%     \witruimte
%     % will be option
%     \getnoflines\!!heighta
%     \!!heighta\noflines\lineheight
%     % so far for option
%     \kern\!!heighta
%   \fi
%   % == \forgetsidefloats
%   \global\sidefloatvsize\nofloatvsize
%   \global\floatshortfalse
%   \global\floatflagfalse}

% \def\flushsidefloats
%   {\par
%    \!!heighta\sidefloatvsize
%    \advance\!!heighta -\pagetotal
%    \ifdim\!!heighta>\zeropoint
%      % to be checked for interference
%      \witruimte
%      % will be option
%      \getnoflines\!!heighta
%      \!!heighta\noflines\lineheight
%      % so far for option
%      \ifdim\sidefloatbottomskip>\zeropoint\relax
%        \ifdim\!!heighta>\sidefloatbottomskip
%          \advance\!!heighta-\sidefloatbottomskip
%          \kern\!!heighta
%          \vskip\sidefloatbottomskip
%        \else
%          \kern\!!heighta
%        \fi
%      \else
%        \kern\!!heighta
%      \fi
%    \fi
%    % == \forgetsidefloats
%    \global\sidefloatvsize\nofloatvsize
%    \global\floatshortfalse
%    \global\floatflagfalse}

\newif\iftracesidefloats

\def\flushsidefloats
  {\par
   \!!heighta\sidefloatvsize
   \advance\!!heighta -\pagetotal
   \ifnum\!!heighta>\zeropoint
     \global\advance\sidefloatvsize -\sidefloatbottomskip
     \ifdim\!!heighta>\zeropoint
       \bgroup
       \let\flushsidefloats\relax
       \forgetall
       \doloop
         {\strut
          \iftracesidefloats
            \color[darkgray]%
              {\baselinerulefalse
               \boxrulewidth.5\points
               \ruledhbox{\strut\kern\sidefloatwidth}}%
          \fi
          \par
          \!!heighta\sidefloatvsize
          \advance\!!heighta -\pagetotal
          \ifdim\!!heighta>\zeropoint
            \ifnum\recurselevel>\plushundred
              \exitloop
            \fi
          \else
            \exitloop
          \fi}%
       \egroup
       \ifdim\parskip>\zeropoint
         \ifdim\sidefloatbottomskip>\parskip
           \nowhitespace
           \vskip\sidefloatbottomskip
         \fi
       \fi
     \else\ifdim\sidefloatbottomskip>\parskip
       \vskip\sidefloatbottomskip
     \fi\fi
   \fi
   % not entirely the same as \forgetsidefloats
   \global\sidefloatvsize\nofloatvsize
   % \global\sidefloatsidelines\zerocount % no, we flush before a side float
   \global\floatshortfalse                % so this will get lost
   % also here if used at all \global\holdinginserts\zerocount
   \global\floatflagfalse}

\def\flushsidefloatsafterpar
  {\xdef\oldpagetotal{\the\pagetotal}%
   \gdef\checksidefloat
     {\dochecksidefloat
      \ifdim\oldpagetotal=\pagetotal \else
        \global\let\checksidefloat\dochecksidefloat
        \flushsidefloats
        \global\sidefloatsidelines\zerocount % here !
      \fi}}

\def\forgetsidefloats
  {\global\sidefloatvsize\nofloatvsize
   \global\sidefloatsidelines\zerocount
   % also here if used at all \global\holdinginserts\zerocount
   \global\floatshortfalse
   \global\floatflagfalse}

\let\logsidefloat=\relax

\def\pushpenalties % needed ? and right
  {\widowpenalty\plusone
   \clubpenalty\plustwo
   \brokenpenalty\plusone
   \let\pushpenalties\relax
   \edef\poppenalties
     {\widowpenalty \the\widowpenalty
      \clubpenalty  \the\clubpenalty
      \brokenpenalty\the\brokenpenalty
      \let\poppenalties\relax}}

% shouldn;t that be:
%
% \def\pushpenalties % needed?
%   {\let\pushpenalties\relax
%    \edef\poppenalties
%      {\widowpenalty \the\widowpenalty
%       \clubpenalty  \the\clubpenalty
%       \brokenpenalty\the\brokenpenalty
%       \let\poppenalties\relax}%
%    \widowpenalty\plusone
%    \clubpenalty\plustwo
%    \brokenpenalty\plusone}

\let\poppenalties=\relax

\def\restorepenalties
  {\ifnum\outputpenalty=\!!tenthousand\else
     \penalty\outputpenalty
   \fi}

\def\sidefloatoutput
  {\iffloatshort
     \unvbox\normalpagebox
     \setbox\floatbottom\lastbox
     \ifdim\wd\floatbottom>\sidefloathsize
       \penalty-201
       \box\floatbottom
     \else
       \ifvoid\floatbottom
       \else
         \restoreleftindent
         \ifdim\wd\floatbottom<\sidefloathsize
           \parskip\zeropoint
           %\noindent
           \vadjust{\penalty\minusone}%
           \iffloatlefteqo
             \global\floatlefteqofalse
           \else
             \global\advance\sidefloathsize -\wd\floatbottom
             \iffloatrighteqo
               \global\floatrighteqofalse
             \else
               \global\divide\sidefloathsize \plustwo
             \fi
             \hskip\sidefloathsize
           \fi
         \fi
         \box\floatbottom
         \restorepenalties
       \fi
     \fi
     % why was this \global\holdinginserts\zerocount
     \global\floatshortfalse
   \else
     \finalsidefloatoutput % new
     \global\sidefloatvsize\nofloatvsize
     \global\sidefloatsidelines\zerocount
     % we can end up in fig lock, so \global\holdinginserts\zerocount here too ?
     \poppenalties
   \fi}

\def\finalsidefloatoutput% new
  {\finaloutput\unvbox\normalpagebox}

\def\restoreleftindent
  {\relax \ifnum\sidefloattype>4 \else
     \parskip\zeropoint % here ?
     \ifdim\sidefloatwidth>\zeropoint % new, see prikkels
       \noindent
       \vadjust{\penalty\minusone}%
       \hskip\sidefloatwidth
    %\else
    %  we have a margin or edge float
     \fi
   \fi}

\ifx\normaleqno\undefined

  \def\normaleqno
    {\iffloatshort
       \global\floatrighteqotrue
     \fi
     \floatrighteqo}

\else

  \def\eqno
    {\iffloatshort
       \global\floatrighteqotrue
     \fi
     \floatrighteqo}

\fi

% \def\putsidefloat#1% grid (4) is rather experimental
%   {\par
%    \witruimte
%    % moved here dec 2001
%    {\everypar\emptytoks\forgetall\verticalstrut\vskip-\struttotal}% moved
%    \checksidefloatshift
%    \ifdim\sidefloatshift=\zeropoint \relax
%      \ifnum\sidefloattype=4
%        \global\advance\sidefloatshift\sidefloatextrashift
%        \global\sidefloatextrashift\zeropoint
%      \else\ifnum\sidefloattype=5
%        \global\advance\sidefloatshift\sidefloatextrashift
%        \global\sidefloatextrashift\zeropoint
%      \fi\fi
%    \else
%      \ifnum\sidefloattype<4
%        \global\chardef\sidefloattype4
%      \else\ifnum\sidefloattype>5
%        \global\chardef\sidefloattype5
%      \fi\fi
%    \fi
%    \previoussidefloat
%    \stallsidefloat
%   %\global\setbox\floatbox\hbox
%   %  {\ifmarginfloat\else\ifrightfloat\else\kern\sidefloatleftshift\fi\fi
%   %   \hskip\ifrightfloat-\sidefloatextrashift\else\sidefloatshift\fi
%   %   \vbox{#1\ifnum\sidefloatalign=4 \removedepth\fi}%
%   %   \kern\ifrightfloat\sidefloatshift\else-\sidefloatextrashift\fi
%   %   \ifmarginfloat\else\ifrightfloat\kern\sidefloatrightshift\fi\fi}%
%    \global\setbox\floatbox\hbox % no \hskip, but \kern here
%      {\ifnum\sidefloattype=4
%         \hskip\sidefloatleftshift
%       \else\ifnum\sidefloattype=1
%         \hskip\sidefloatleftshift
%       \fi\fi
%       \ifnum\sidefloattype>4
%         \hskip-\sidefloatextrashift
%       \else
%         \hskip\sidefloatshift
%       \fi
%       \vbox{#1\ifnum\sidefloatalign=4 \removedepth\fi}%
%       \ifnum\sidefloattype>4
%         \kern\sidefloatshift
%       \else
%         \kern-\sidefloatextrashift
%       \fi
%       \ifnum\sidefloattype=8
%         \kern\sidefloatrightshift
%       \else\ifnum\sidefloattype=5
%         \kern\sidefloatrightshift
%       \fi\fi}%
%    \ifnum\sidefloatalign=4
%      \getnoflines{\ht\floatbox}%
%      \scratchdimen\noflines\lineheight
%      \advance\scratchdimen-\strutdepth
%      \getrawnoflines\sidefloattopskip
%      \advance\scratchdimen\noflines\lineheight
%      % todo: maybe rounding problem here
%      % \global\setbox\floatbox\hbox{\lower\lineheight\box\floatbox}%
%      \global\setbox\floatbox\hbox{\lower\strutdepth\box\floatbox}%
%      \global\ht\floatbox\scratchdimen
%      \global\dp\floatbox\zeropoint
%    \fi
%    \ifcase\sidefloatalign \else
%      \global\sidefloattopskip\zeropoint
%    \fi
%    \scratchdimen
%      \ifnum\sidefloattype<4
%        \sidefloattopskip
%      \else\ifnum\sidefloattype>5
%        \sidefloattopskip
%      \else
%        \zeropoint
%      \fi\fi
%    % the top of the box is at the previous baseline
%    \ifcase\sidefloatalign
%        % 0 normal
%      \advance\scratchdimen\strutdepth % == \sidefloattopoffset
%    \or % 1 height
%      \advance\scratchdimen\strutdepth % == \sidefloattopoffset
%    \or % 2 line
%    \or % 3 depth
%      \advance\scratchdimen\lineheight
%      \advance\scratchdimen\strutdepth
%    \or % 4 grid
%      \scratchdimen\zeropoint
%    \fi
%    % new
%    \global\sidefloatlinesdone\zerocount
%    \ifnum\sidefloatsidelines>\zerocount
%      \advance\scratchdimen\sidefloatsidelines\lineheight
%    \fi
%    % new
%    \global\setbox\floatbox\hbox
%      {\vbox{\vskip\scratchdimen\nointerlineskip\box\floatbox}}%
%    \ifnum\sidefloattype<4
%      \global\sidefloattopskip\zeropoint
%    \else\ifnum\sidefloattype>5
%      \global\sidefloattopskip\zeropoint
%    \fi\fi
%    \global\sidefloatdownshift\zeropoint
%    \measuresidefloat
%    \ifroomforfloat \else
%      \tosssidefloat
%      \measuresidefloat
%      \stallsidefloat
%    \fi
%    \setsidefloat}

% externfiguur     -> grid        =ja|hoogte|diepte|halveregel|passend -> helemaal in details
% stelplaatsblokin -> zijuitlijnen=hoogte|diepte|regel|halveregel|grid -> halveregel in 'details'

\def\putsidefloat#1% grid (4) is rather experimental
  {\par
   \whitespace
   % moved here dec 2001
   {\everypar\emptytoks\forgetall\verticalstrut\vskip-\struttotal}% moved
   \checksidefloatshift
   \ifdim\sidefloatshift=\zeropoint \relax
     \ifnum\sidefloattype=4
       \global\advance\sidefloatshift\sidefloatextrashift
       \global\sidefloatextrashift\zeropoint
     \else\ifnum\sidefloattype=5
       \global\advance\sidefloatshift\sidefloatextrashift
       \global\sidefloatextrashift\zeropoint
     \fi\fi
   \else
     \ifnum\sidefloattype<4
       \global\chardef\sidefloattype4
     \else\ifnum\sidefloattype>5
       \global\chardef\sidefloattype5
     \fi\fi
   \fi
   \previoussidefloat
   \stallsidefloat
  %\global\setbox\floatbox\hbox
  %  {\ifmarginfloat\else\ifrightfloat\else\kern\sidefloatleftshift\fi\fi
  %   \hskip\ifrightfloat-\sidefloatextrashift\else\sidefloatshift\fi
  %   \vbox{#1\ifnum\sidefloatalign=4 \removedepth\fi}%
  %   \kern\ifrightfloat\sidefloatshift\else-\sidefloatextrashift\fi
  %   \ifmarginfloat\else\ifrightfloat\kern\sidefloatrightshift\fi\fi}%
   \global\setbox\floatbox\hbox % no \hskip, but \kern here
     {\ifnum\sidefloattype=4
        \hskip\sidefloatleftshift
      \else\ifnum\sidefloattype=1
        \hskip\sidefloatleftshift
      \fi\fi
      \ifnum\sidefloattype>4
        \hskip-\sidefloatextrashift
      \else
        \hskip\sidefloatshift
      \fi
      \vbox{#1\ifnum\sidefloatalign=4 \removedepth\fi}%
      \ifnum\sidefloattype>4
        \kern\sidefloatshift
      \else
        \kern-\sidefloatextrashift
      \fi
      \ifnum\sidefloattype=8
        \kern\sidefloatrightshift
      \else\ifnum\sidefloattype=5
        \kern\sidefloatrightshift
      \fi\fi}%
   \ifnum\sidefloatalign=4
     \getnoflines{\ht\floatbox}%
     \scratchdimen\noflines\lineheight
     \advance\scratchdimen-\strutdepth
     \getrawnoflines\sidefloattopskip
     \advance\scratchdimen\noflines\lineheight
     % todo: maybe rounding problem here
     % \global\setbox\floatbox\hbox{\lower\lineheight\box\floatbox}%
     \global\setbox\floatbox\hbox{\lower\strutdepth\box\floatbox}%
     \global\ht\floatbox\scratchdimen
     \global\dp\floatbox\zeropoint
   \fi
   \ifcase\sidefloatalign \else
     \global\sidefloattopskip\zeropoint
   \fi
   \scratchdimen
     \ifnum\sidefloattype<4
       \sidefloattopskip
     \else\ifnum\sidefloattype>5
       \sidefloattopskip
     \else
       \zeropoint
     \fi\fi
   % the top of the box is at the previous baseline
   \ifcase\sidefloatalign
       % 0 normal
     \advance\scratchdimen\strutdepth % == \sidefloattopoffset
   \or % 1 height
     \advance\scratchdimen\strutdepth % == \sidefloattopoffset
   \or % 2 line
   \or % 3 depth
     \advance\scratchdimen\lineheight
     \advance\scratchdimen\strutdepth
   \or % 4 grid
     \scratchdimen\zeropoint
   \or
     \advance\scratchdimen\strutheight
   \fi
   % new
   \global\sidefloatlinesdone\zerocount
   \ifnum\sidefloatsidelines>\zerocount
     \advance\scratchdimen\sidefloatsidelines\lineheight
   \fi
   % new
   \global\setbox\floatbox\hbox
     {\vbox
        {\vskip\scratchdimen
         \nointerlineskip
         \box\floatbox
         \ifnum\sidefloatalign=5 \vskip-\lineheight \fi}}%
   \ifnum\sidefloattype<4
     \global\sidefloattopskip\zeropoint
   \else\ifnum\sidefloattype>5
     \global\sidefloattopskip\zeropoint
   \fi\fi
   \global\sidefloatdownshift\zeropoint
   \measuresidefloat
   \ifroomforfloat \else
     \tosssidefloat
     \measuresidefloat
     \stallsidefloat
   \fi
   \setsidefloat}

\def\progresssidefloat
  {\sidefloatprogress\sidefloatvsize
   \iffloatflag
     \advance\sidefloatprogress -\sidefloatpagetotal
     \global\floatflagfalse
   \else
     \advance\sidefloatprogress -\pagetotal
   \fi}

\def\tosssidefloat
  {\vfill\eject}

\def\measuresidefloat
  {\global\floatflagtrue
   \global\sidefloatpagetotal \pagetotal % global
   \ifnum\sidefloattype<4
     \global        \sidefloatwidth \zeropoint
   \else\ifnum\sidefloattype>5
     \global        \sidefloatwidth \zeropoint
   \else
     \global        \sidefloatwidth \wd\floatbox
     \global\advance\sidefloatwidth \floatsideskip
   \fi\fi
   \ifdim\sidefloatwidth<\zeropoint
     \global\sidefloatwidth\zeropoint
   \fi
   \global        \sidefloathsize  \hsize
   \global\advance\sidefloathsize -\sidefloatwidth
   \global        \sidefloatheight \ht\floatbox
   \global\advance\sidefloatheight \dp\floatbox
   \global\advance\sidefloatheight \sidefloattopskip
   \global        \sidefloatvsize  \sidefloatheight
   \global\advance\sidefloatvsize  \sidefloatpagetotal
   \dimen0\sidefloatvsize
   \dimen2\pagegoal
   \relax
   \ifcase\sidefloatmethod
     % method 0 : raw
   \or
     % method 1 : safe
     \dimen2 .99\pagegoal
   \or
     % method 2 : tight
     \advance\dimen0 -\onepoint
   \fi
   \relax % really needed ! ! ! !
   \ifdim\dimen0>\dimen2
     \global\roomforfloatfalse
   \else
     \dimen0=\pagegoal
     \advance\dimen0 -\sidefloatvsize
     \ifdim\dimen0<\sidefloatbottomskip
       \global\advance\sidefloatvsize \dimen0
       \global\floatshorttrue
       \pushpenalties
       % why was this \global\holdinginserts\plusone
     \else
       \global\advance\sidefloatvsize \sidefloatbottomskip
       \global\floatshortfalse
     \fi
     \global\roomforfloattrue
   \fi}

\def\setsidefloat% nilling everypar saves time and redudant pos's
  {% removed here dec 2001
   % {\everypar\emptytoks\forgetall\verticalstrut\vskip-\struttotal}%
   \ifnum\sidefloattype=4 \kern\sidefloattopskip \fi
   \ifnum\sidefloattype=5 \kern\sidefloattopskip \fi
   \edef\presidefloatdepth{\the\prevdepth}%
   \nointerlineskip
   \bgroup
   \everypar\emptytoks
   \parskip\zeropoint
  %\checksidefloatshift
   \setsidefloatskips
   \logsidefloat
   \relax
   \ifcase\sidefloattype
     % invalid
   \or % backspace
     \noindent\llap{\rlap{\box\floatbox}\hskip\sidefloatleftskip}\hfill
   \or % leftedge
     \noindent\llap{\box\floatbox\hskip\sidefloatleftskip}\hfill
   \or % leftmargin
     \noindent\llap{\box\floatbox\hskip\sidefloatleftskip}\hfill
   \or % leftside
     \noindent\box\floatbox\hfill
   \or % rightside
     \hfill\box\floatbox
   \or % rightmargin
     \hfill\rlap{\hskip\sidefloatrightskip\box\floatbox}%
   \or % rightedge
     \hfill\rlap{\hskip\sidefloatrightskip\box\floatbox}%
   \or % cutspace
     \hfill\rlap{\hskip\sidefloatrightskip\llap{\box\floatbox}}%
   \fi
   \egroup
   \par
   \kern-\sidefloatheight
   \penalty10001 % oeps, this will change
   \normalbaselines
   \prevdepth\presidefloatdepth
   % \noindent
   \resetsidefloatparagraph
   \ignorespaces} % not really needed

\newcount\sidefloatparagraph

\def\iffirstsidefloatparagraph
  {\ifcase\sidefloatparagraph\or}

% without sidefloat lines
%
% \def\setsidefloatparagraph
%   {%\advance\sidefloatprogress \sidefloatbottomskip
%    %\!!counta\sidefloatprogress
%    \scratchdimen\sidefloatprogress
%    \advance\scratchdimen \strutht
%    \advance\scratchdimen -\roundingeps % new, needed in prikkels, prevent rounding errors
%    \!!counta\scratchdimen
%    \divide\!!counta \baselineskip
%    \ifnum\!!counta>0
%      \hangindent \ifnum\sidefloattype>4 -\fi\sidefloatwidth
%      \hangafter-\!!counta
%    \fi
%    \global\advance\sidefloatparagraph \plusone
%    \iftracesidefloats
%      \hskip-\sidefloatwidth
%      \color[darkgray]%
%        {\vrule\!!height.5\points\!!depth.5\points\!!width\sidefloatwidth
%        %\hskip-.25\bodyfontsize\showstruts\strut\hskip.25\bodyfontsize}%
%         \llap{\showstruts\strut\hskip.25\bodyfontsize}}%
%    \fi}

\def\setsidefloatparagraph
  {%\advance\sidefloatprogress \sidefloatbottomskip
   %\!!counta\sidefloatprogress
   \scratchdimen\sidefloatprogress
   \advance\scratchdimen \strutht
   \advance\scratchdimen -\roundingeps % new, needed in prikkels, prevent rounding errors
   \!!counta\scratchdimen
   \divide\!!counta \baselineskip\relax
   \ifnum\!!counta>0
     % new from here
     \ifcase\sidefloatsidelines\else
       \ifcase\sidefloatlinesdone
         \global\sidefloatlinesdone\!!counta
       \else
         \scratchcounter\sidefloatlinesdone
         \advance\scratchcounter-\!!counta
         \global\advance\sidefloatsidelines-\scratchcounter
       \fi
     \fi
     \ifnum\sidefloatsidelines>\zerocount
       \scratchtoks\emptytoks
       \scratchcounter\sidefloatsidelines
       \scratchdimen\hsize
       \advance\scratchdimen-\sidefloatwidth
       \dorecurse\sidefloatsidelines
         {\appendtoks \zeropoint \hsize \to \scratchtoks}%
       \ifnum\!!counta>\sidefloatsidelines
         \advance\!!counta -\sidefloatsidelines\relax
         \advance\scratchcounter\!!counta
         \dorecurse\!!counta
           {\ifnum\sidefloattype>4
              \appendtoks \zeropoint \scratchdimen \to \scratchtoks
            \else
              \appendtoks \sidefloatwidth \scratchdimen \to \scratchtoks
            \fi}%
       \fi
       \advance\scratchcounter \plusone
       \parshape \scratchcounter \the\scratchtoks \zeropoint \hsize\relax
     \else
       % new till here
       \hangindent \ifnum\sidefloattype>4 -\fi\sidefloatwidth
       \hangafter-\!!counta
     \fi
   \fi
   \global\advance\sidefloatparagraph \plusone
   \iftracesidefloats
     \hskip-\sidefloatwidth
     \color[darkgray]%
       {\vrule\!!height.5\points\!!depth.5\points\!!width\sidefloatwidth
       %\hskip-.25\bodyfontsize\showstruts\strut\hskip.25\bodyfontsize}%
        \llap{\showstruts\strut\hskip.25\bodyfontsize}}%
   \fi}

\def\resetsidefloatparagraph
  {\global\sidefloatparagraph\zerocount }

\def\dochecksidefloat
  {\progresssidefloat
   \ifdim\sidefloatprogress>\zeropoint
     \setsidefloatparagraph
   \else
     \resetsidefloatparagraph
   \fi
   \parskip\ctxparskip}

\def\checksidefloat
  {\dochecksidefloat}

\def\synchronizesidefloats
  {\ifinner \else \checksidefloat \fi}

\def\doadjustsidefloatdisplaylines
  {\par
   \vskip-\parskip
   \noindent
   \ignorespaces}

\def\adjustsidefloatdisplaylines
  {\aftergroup\doadjustsidefloatdisplaylines}

\def\previoussidefloat
  {\progresssidefloat
   \ifdim\sidefloatprogress>\zeropoint \relax
     \iffloatshort
       \global\floatshortfalse
       \tosssidefloat
     \else
       \kern\sidefloatprogress
     \fi
   \fi}

\def\stallsidefloat
  {\!!counta\pageshrink
   \divide\!!counta \baselineskip
   \advance\!!counta \plusone
   \parskip\zeropoint
   \dorecurse\!!counta{\line{}}
   \kern-\!!counta\baselineskip
   \penalty\zerocount }

\protect \endinput
