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

% 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\sidefloatvsize       \def\nofloatvsize{-1pt }
\newdimen\sidefloatprogress

\newbox\floatbottom

\newif\ifrightfloat
\newif\ifmarginfloat
\newif\ifedgefloat
\newif\ifroomforfloat
\newif\iffloatshort
\newif\iffloatflag
\newif\iffloatrighteqo
\newif\iffloatlefteqo

% 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\s!pt
               \ruledhbox{\strut\kern\sidefloatwidth}}%
          \fi
          \par
          \!!heighta\sidefloatvsize
          \advance\!!heighta -\pagetotal
          \ifdim\!!heighta>\zeropoint 
            \ifnum\recurselevel>100
              \exitloop
            \fi 
          \else
            \exitloop
          \fi}%
       \egroup
       \ifdim\parskip>\zeropoint
         \ifdim\sidefloatbottomskip>\parskip
           \geenwitruimte 
           \vskip\sidefloatbottomskip
         \fi 
       \fi
     \else\ifdim\sidefloatbottomskip>\parskip
       \vskip\sidefloatbottomskip
     \fi\fi
   \fi
   % == \forgetsidefloats 
   \global\sidefloatvsize\nofloatvsize
   \global\floatshortfalse
   \global\floatflagfalse}

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

\def\forgetsidefloats
  {\global\sidefloatvsize\nofloatvsize
   \global\floatshortfalse
   \global\floatflagfalse}

\let\logsidefloat=\relax

\def\pushpenalties
  {\widowpenalty\plusone
   \clubpenalty\plustwo
   \brokenpenalty\plusone
   \let\pushpenalties\relax
   \edef\poppenalties
     {\widowpenalty \the\widowpenalty
      \clubpenalty  \the\clubpenalty
      \brokenpenalty\the\brokenpenalty}}

\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
     \global\holdinginserts\zerocount
     \global\floatshortfalse
   \else
     \finalsidefloatoutput % new 
     \global\sidefloatvsize\nofloatvsize
     \poppenalties
   \fi}

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

\def\restoreleftindent
  {\ifrightfloat \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\leftmarginfloat#1%
  {\global\rightfloatfalse\marginfloattrue\edgefloatfalse\putsidefloat{#1}}

\def\rightmarginfloat#1%
  {\global\rightfloattrue\marginfloattrue\edgefloatfalse\putsidefloat{#1}}

\def\leftedgefloat#1%
  {\global\rightfloatfalse\marginfloatfalse\edgefloattrue\putsidefloat{#1}}

\def\rightedgefloat#1%
  {\global\rightfloattrue\marginfloatfalse\edgefloattrue\putsidefloat{#1}}

\def\leftfloat#1%
  {\global\rightfloatfalse\edgefloatfalse\marginfloatfalse\putsidefloat{#1}}

\def\rightfloat#1%
  {\global\rightfloattrue\marginfloatfalse\edgefloatfalse\putsidefloat{#1}}

% \def\putsidefloat#1% crap macro 
%   {\par
%    \witruimte
%    % moved here dec 2001 
%    {\everypar\emptytoks\forgetall\vbox{\strut}\vskip-\lineheight}% moved
%    %
%    \previoussidefloat
%    \stallsidefloat
%    \setbox\floatbox\hbox{\vbox % pretty ugly, will be rewritten
%      {\vskip
%         \sidefloatdownshift
%       \vskip
%         \ifmarginfloat
%           -\sidefloattopskip
%         \else\ifedgefloat
%           -\sidefloattopskip
%         \else
%           +\sidefloattopoffset
%         \fi\fi
%         #1}}
%    \global\sidefloatdownshift\zeropoint
%    \measuresidefloat
%    \ifroomforfloat 
%    \else
%      \tosssidefloat
%      \measuresidefloat
%      \stallsidefloat
%    \fi
%    \setsidefloat}

\def\putsidefloat#1% crap macro 
  {\par
   \witruimte
   % moved here dec 2001 
   {\everypar\emptytoks\forgetall\vbox{\strut}\vskip-\lineheight}% moved
   \ifdim\sidefloatshift=\zeropoint\else
     \global \marginfloatfalse
     \global \edgefloatfalse 
   \fi 
   \ifcase\sidefloatalign \else
     \global\sidefloattopskip\zeropoint
   \fi
   \previoussidefloat
   \stallsidefloat
\global
   \setbox\floatbox\hbox
     {\ifmarginfloat\else\ifrightfloat\else\kern\sidefloatleftshift\fi\fi
      \hskip\sidefloatshift
      \scratchdimen
        \ifmarginfloat           
          \sidefloattopskip
        \else\ifedgefloat 
          \sidefloattopskip
        \else
          \zeropoint
        \fi\fi
      % the top of the box is at the previous baseline 
      \ifcase\sidefloatalign 
         % normal 
        \advance\scratchdimen\strutdepth % == \sidefloattopoffset
      \or % height  
        \advance\scratchdimen\strutdepth % == \sidefloattopoffset
      \or % line
%        \advance\scratchdimen\lineheight
      \or % depth 
        \advance\scratchdimen\lineheight
        \advance\scratchdimen\strutdepth
      \fi
      % equivalent but more compact:
%      \advance\scratchdimen\strutdepth\relax 
%      \ifcase\sidefloatalign                
%      \or
%      \or \advance\scratchdimen\strutheight
%      \or \advance\scratchdimen\lineheight
%      \fi 
      \vbox{\vskip\scratchdimen\nointerlineskip#1}% 
      % no \hskip, but \kern here 
      \ifmarginfloat\else\ifrightfloat\kern\sidefloatrightshift\fi\fi}%
   \ifmarginfloat
     \global\sidefloattopskip\zeropoint 
   \else\ifedgefloat
     \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}

\newdimen\sidefloatpagetotal

\def\measuresidefloat
  {\global\floatflagtrue
   \global\sidefloatpagetotal \pagetotal % global 
   \ifmarginfloat
     \global        \sidefloatwidth \zeropoint
   \else\ifedgefloat
     \global        \sidefloatwidth \zeropoint
   \else
     \global        \sidefloatwidth \wd\floatbox
     \global\advance\sidefloatwidth \floatsideskip
   \fi\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
   \ifdim\dimen0>.99\pagegoal \relax
     \roomforfloatfalse
   \else
     \dimen0=\pagegoal
     \advance\dimen0 -\sidefloatvsize
     \ifdim\dimen0<\sidefloatbottomskip
       \global\advance\sidefloatvsize \dimen0
       \global\floatshorttrue
       \pushpenalties
       \global\holdinginserts\plusone
     \else
       \global\advance\sidefloatvsize \sidefloatbottomskip
       \global\floatshortfalse
     \fi
     \roomforfloattrue
   \fi}

% \def\setsidefloat% nilling everypar saves time and redudant pos's 
%   {% removed here dec 2001   
%   %{\everypar\emptytoks\forgetall\vbox{\strut}\vskip-\lineheight}%
%    %
%    \kern\sidefloattopskip
%    \edef\presidefloatdepth{\the\prevdepth}%
%    \nointerlineskip
%    \bgroup
%    \everypar\emptytoks
%    \parskip\zeropoint
%    \logsidefloat
%    \ifrightfloat
%      \hfill
%      \ifmarginfloat
% %       \rlap{\hskip\rechtermargeafstand\hskip\rightskip\unhbox\floatbox}%
% \rlap{\tbox{\hskip\rechtermargeafstand\hskip\rightskip\unhbox\floatbox}}%
%      \else\ifedgefloat
% \rlap{\tbox{\hskip\rechtermargeafstand\hskip\rechtermargebreedte
%             \hskip\rechterrandafstand\hskip\rightskip\unhbox\floatbox}}%
%      \else
%        \unhbox\floatbox
%      \fi\fi
%    \else
%      \noindent
%      \ifmarginfloat
% %       \llap{\unhbox\floatbox\hskip\leftskip\hskip\linkermargeafstand}%
% \llap{\tbox{\unhbox\floatbox\hskip\leftskip\hskip\linkermargeafstand}}%
%      \else\ifedgefloat
% \llap{\tbox{\unhbox\floatbox\hskip\leftskip\hskip\linkermargeafstand
%             \hskip\linkermargebreedte\hskip\linkerrandafstand}}%
%      \else
%        \unhbox\floatbox
%      \fi\fi
%      \hfill
%    \fi
%    \egroup
%    \par
%    \kern-\sidefloatheight
%    \penalty10001 % oeps, this will change 
%    \normalbaselines
%    \prevdepth\presidefloatdepth
%    %\noindent
%    \resetsidefloatparagraph
%    \ignorespaces}

\def\setsidefloat% nilling everypar saves time and redudant pos's 
  {% removed here dec 2001   
   % {\everypar\emptytoks\forgetall\vbox{\strut}\vskip-\lineheight}%
   \ifmarginfloat \else \ifedgefloat \else
     \kern\sidefloattopskip
   \fi \fi
   \edef\presidefloatdepth{\the\prevdepth}%
   \nointerlineskip
   \bgroup
   \everypar\emptytoks
   \parskip\zeropoint
   \logsidefloat
   \ifrightfloat
     \hfill
     \ifmarginfloat
       \rlap{\hskip\rechtermargeafstand\hskip\rightskip\unhbox\floatbox}%
     \else\ifedgefloat
       \rlap{\hskip\rechtermargeafstand\hskip\rechtermargebreedte
             \hskip\rechterrandafstand\hskip\rightskip\unhbox\floatbox}%
     \else
       \unhbox\floatbox 
     \fi\fi
   \else
     \noindent
     \ifmarginfloat
       \llap{\unhbox\floatbox\hskip\leftskip\hskip\linkermargeafstand}%
     \else\ifedgefloat
       \llap{\unhbox\floatbox\hskip\leftskip\hskip\linkermargeafstand
             \hskip\linkermargebreedte\hskip\linkerrandafstand}%
     \else
       \unhbox\floatbox 
     \fi\fi
     \hfill
   \fi
   \egroup
   \par
   \kern-\sidefloatheight
   \penalty10001 % oeps, this will change 
   \normalbaselines
   \prevdepth\presidefloatdepth
   % \noindent
   \resetsidefloatparagraph
   \ignorespaces} % not really needed 

\newcount\sidefloatparagraph

\def\iffirstsidefloatparagraph
  {\ifnum\sidefloatparagraph=1 }

\def\setsidefloatparagraph
  {%\advance\sidefloatprogress \sidefloatbottomskip
   %\!!counta\sidefloatprogress
\scratchdimen\sidefloatprogress
\advance\scratchdimen \ht\strutbox
\!!counta\scratchdimen
   \divide\!!counta \baselineskip
   \ifnum\!!counta>0
     \hangindent \ifrightfloat-\fi\sidefloatwidth
     \hangafter-\!!counta
   \fi
   \global\advance\sidefloatparagraph \plusone
   \iftracesidefloats
     \hskip-\sidefloatwidth
     \color[darkgray]%
       {\vrule\!!height.5\s!pt\!!depth.5\s!pt\!!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\tussenwit}

\def\checksidefloat
  {\dochecksidefloat}

\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 
