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

%D This is just the good old \CONTEXT\ output routine, which
%D has been there right from the start.

\unprotect

% OTRONE: basic single column

\activateotr{ONE}{} % the default one

\newtoks\OTRONEoutput

\def\OTRONEgotonextpage
  {\ejectpage}

\def\OTRONEgotonextpageX % will become obsolete
  {\superejectpage}

\def\OTRONEsethsize
  {\global\hsize\textwidth}

% keep (original one)
%
% \def\OTRONEsetvsize
%    {\ifdim\vsize=\teksthoogte \else
%       \bgroup
%       \scratchdimen-\vsize
%       \advance\scratchdimen \teksthoogte
%       \global\advance\vsize \scratchdimen
%       \relax \ifdim\pagegoal<\maxdimen
%         \advance\scratchdimen \pagegoal
%         \global\pagegoal\scratchdimen
%       \fi
%       \egroup
%    \fi}
%
% no (keep)
%
% \def\OTRONEsetvsize
%   {\ifdim\vsize=\teksthoogte \else
%      \bgroup
%      \scratchdimen-\vsize
%      \advance\scratchdimen \teksthoogte
%      \ifgridsnapping
%        \getrawnoflines\scratchdimen
%        \scratchdimen\noflines\openlineheight
%        \ifdim\scratchdimen>\noflinesheight % available afterwards
%          \advance\scratchdimen-\openlineheight
%        \fi
%        \ifdim\scratchdimen<\zeropoint
%          \scratchdimen\zeropoint
%        \fi
%      \fi
%      \global\advance\vsize \scratchdimen
%      \relax \ifdim\pagegoal<\maxdimen
%        \advance\scratchdimen \pagegoal
%        \global\pagegoal\scratchdimen
%      \fi
%      \egroup
%   \fi}

% \def\OTRONEsetvsize
%   {\ifgridsnapping
%      \ifcase\layoutlines
%        \getrawnoflines\teksthoogte
%      \else
%        \noflines\layoutlines
%      \fi
%      \global\vsize\noflines\openlineheight
%    \else
%      \global\vsize\teksthoogte
%    \fi
%    \ifdim\pagegoal<\maxdimen
%      \global\pagegoal\vsize
%    \fi}

\newdimen\oldvsize

\def\OTRONEsetvsize
  {\ifgridsnapping
     \ifcase\layoutlines
       \getrawnoflines\textheight
     \else
       \noflines\layoutlines
     \fi
     \global\vsize\noflines\openlineheight
   \else
     \global\vsize\textheight
   \fi
   \ifdim\pagegoal<\maxdimen
     \ifdim\oldvsize=\vsize
       % let's assume that the layout didn't change
     \else
       \bgroup
         \global\oldvsize\vsize
         \advance\vsize-\topinserted
         \advance\vsize-\botinserted
         \global\pagegoal\vsize
       \egroup
     \fi
   \fi}

\chardef\kindofpagetextareas=2 % whole page (public variable! never change)

\def\OTRONEregisteredtextarea#1%
  {\ifregistertextareas
     \setbox0\vbox{#1}%
     \wd0\makeupwidth % somehow a space creeps in
     \vbox{\registeredtextarea000}%
   \else
     #1%
   \fi}

\let\OTRONEregisteredtextareaA\firstofoneargument
\let\OTRONEregisteredtextareaB\firstofoneargument

\def\OTRONEdopagecontents#1#2% \box<n> \unvbox<n>
  {\bgroup             % niet breedte zetten, kan fractie zijn!
   \ifcase\kindofpagetextareas
   \or % partial page
     \let\OTRONEregisteredtextareaA\OTRONEregisteredtextarea
   \or % whole page
     \let\OTRONEregisteredtextareaB\OTRONEregisteredtextarea
   \fi
   \setbox0\vbox \ifbottomnotes to \textheight \fi
     {\edef\currentpagedepth{\the\dp#2}% still to be derived from #1
      \dotopinsertions
      \ifgridsnapping
        \OTRONEregisteredtextareaA{#1#2}%
        \vskip-\currentpagedepth\vskip\openstrutdepth
        \pushproperties % moved from just after #1#2
        \prevdepth\openstrutdepth
        \dobotinsertions
        \vfil
      \else\ifr@ggedbottom
        \OTRONEregisteredtextareaA{#1#2}%
        \vskip-\currentpagedepth\vskip\openstrutdepth
        \pushproperties % moved from just after #1#2
        \prevdepth\openstrutdepth
        \dobotinsertions
        \vfil
      \else\ifb@selinebottom
        \OTRONEregisteredtextareaA{#1#2}%
        \kern-\currentpagedepth\kern\maxdepth
        \pushproperties % moved from just after #1#2
        \dobotinsertions
      \else
        \OTRONEregisteredtextareaA{#1#2}%
        \pushproperties % moved from just after #1#2
        \dobotinsertions % added
      \fi\fi\fi
      \fakepagenotes}% was \fakenotes, but wrong! (check with \setupalign[height])
   \ifbottomnotes
     \ifgridsnapping
       \getnoflines\textheight
       \advance\noflines \minusone
       \scratchdimen\noflines\lineheight
       \advance\scratchdimen \topskip
     \else
       \scratchdimen\ht0
     \fi
   \else
     \scratchdimen\zeropoint
   \fi
   \setbox2\hbox
     {\checksinglecolumnfootnotes
      \lower\scratchdimen\vbox{\placebottomnotes}}%
   \smashbox2% % needed here
   \ifbottomnotes
     \ht0\zeropoint
   \fi
   \OTRONEregisteredtextareaB
     {\vbox to \textheight
        {\box0\box2\ifbottomnotes\else\vfill\fi}}%
   \egroup}

\def\OTRONEfinalsidefloatoutput
  {\finaloutput\unvbox\normalpagebox}

\OTRONEoutput
  {\sidefloatoutput}

%D Insertions

\newif\iftopofinsert

\def\OTRONEdosettopinserts
  {\bgroup
   \ifsomefloatwaiting
     \noffloatinserts\zerocount
     \let\totaltopinserted\!!zeropoint
     \OTRONEdodosettopinserts
     \ifnum\@@bknbottom=\zerocount
       \ifnum\@@bknlines>\zerocount
         \ifdim\totaltopinserted>\zeropoint\relax
           \dimen0=\lineheight
           \dimen0=\@@bknlines\dimen0
           \advance\dimen0 \totaltopinserted\relax
           \ifdim\dimen0>\textheight
             \showmessage\m!floatblocks8\@@bknlines
             \vfilll\eject
           \fi
         \fi
       \fi
     \fi
   \fi
   \egroup}

\def\OTRONEdodosettopinserts
  {\ifnum\noffloatinserts<\noftopfloats
     \dogetfloat
     \ifdim\topinserted=\zeropoint
       \topofinserttrue
     \else
       \topofinsertfalse
     \fi
     \global\advance\topinserted \ht\floatbox
     \global\advance\topinserted \dp\floatbox
     \global\advance\topinserted \floatbottomskip\relax
     \ifdim\topinserted<\textheight\relax
       \xdef\totaltopinserted{\the\topinserted}%
       \insert\topins
         {\forgetall
          \iftopofinsert
            \topskipcorrection % [xx] new: see icare topbleed
            \kern-\lineskip\par
            \prevdepth\maxdimen
          \else
            %\blank[-\@@bkspaceafter,\@@bkspacebefore]% inserts can't look back
            \betweenfloatblanko
          \fi
          \flushfloatbox
          \blank[\@@bkspaceafter]}%
       \ifsomefloatwaiting
         \advance\noffloatinserts \plusone
       \else
         \noffloatinserts\noftopfloats\relax
       \fi
       \dofloatflushedinfo
     \else
       \doresavefloat
       \noffloatinserts\noftopfloats\relax
     \fi
   \else
     \ifsomefloatwaiting
       \showmessage\m!floatblocks6{\the\noftopfloats}%
     \fi
     \let\OTRONEdodosettopinserts\relax
   \fi
   \OTRONEdodosettopinserts}

\def\OTRONEdosetbotinserts
  {\bgroup
   \ifsomefloatwaiting
     \noffloatinserts\zerocount
     \OTRONEdodosetbotinserts
   \fi
   \egroup}

\def\OTRONEdodosetbotinserts
  {\ifnum\noffloatinserts<\nofbotfloats\relax
     \dogetfloat
     \global\advance\botinserted \ht\floatbox\relax
     \global\advance\botinserted \dp\floatbox\relax
     \global\advance\botinserted \floattopskip\relax
     \ifdim\botinserted<\pagegoal\relax
       \insert\botins
         {\forgetall
          \blank[\@@bkspacebefore]%
          \flushfloatbox}%
       \ifsomefloatwaiting
         \advance\noffloatinserts \plusone
       \else
         \noffloatinserts\nofbotfloats
       \fi
       \dofloatflushedinfo
     \else
       \doresavefloat
       \noffloatinserts\nofbotfloats\relax
     \fi
     \global\nofloatpermittedtrue % vgl topfloats s!
   \else
     \ifsomefloatwaiting
       \showmessage\m!floatblocks7{\the\nofbotfloats}%
     \fi
     \let\OTRONEdodosetbotinserts\relax
   \fi
   \OTRONEdodosetbotinserts}

\def\OTRONEdosetbothinserts
  {\global\topinserted\zeropoint
   \global\botinserted\zeropoint
   \ifflushingfloats \else
     \OTRONEdosettopinserts
     \OTRONEdosetbotinserts
   \fi}

% \def\OTRONEdotopinsertions
%   {\ifvoid\topins\else
%      \ifgridsnapping
%        \box\topins % not \topsnaptogrid{\box\topins}%
%      \else
%        \unvbox\topins
%      \fi
%    \fi
%    \global\topinserted\zeropoint}

\def\OTRONEdotopinsertions
  {\ifvoid\topins\else
     \ifgridsnapping
       \box\topins
       \vskip-\topskip \vskip\strutheight % [xx] new: see icare topbleed
     \else
       \unvbox\topins
     \fi
   \fi
   \global\topinserted\zeropoint}

\def\OTRONEdobotinsertions
  {\ifvoid\botins\else
     \ifgridsnapping
       \snaptogrid\hbox{\box\botins}%
     \else
       \unvbox\botins
     \fi
   \fi
   \global\botinserted\zeropoint
   \global\nofloatpermittedfalse}

\def\OTRONEdoflushfloats
  {\global\flushingfloatstrue
   \ifsomefloatwaiting
     \par
     % if kept, then option and definitely off in gridmode ! ! ! !
     % \ifvmode \prevdepth\maxdimen \fi % prevents whitespace; problematic in icare tests
     \OTRONEdodoflushfloats
   \fi
   \global\savednoffloats\zerocount
   \global\somefloatwaitingfalse
   \global\flushingfloatsfalse}

\def\OTRONEflushfloatbox % nog verder doorvoeren en meer info in marge
  {\ifcenterfloatbox \ifdim\wd\floatbox<\hsize
     \setbox\floatbox\hbox to \hsize{\hss\box\floatbox\hss}%
   \fi \fi
   \snaptogrid\hbox{\iftestfloatbox\ruledhbox\fi{\copy\floatbox}}}

% \def\OTRONEdodoflushfloats % moet nog beter: als precies passend, niet onder baseline
%   {\ifsomefloatwaiting
%      \bgroup % \box\floatbox can be in use!
%      \dogetfloat
%      \doplacefloatbox
%      \egroup
%      \dofloatflushedinfo
%      \expandafter\OTRONEdodoflushfloats
%    \fi}

\def\OTRONEdodoflushfloats % much in common with OTRSET
  {\ifsomefloatwaiting
     \ifpackflushedfloats
       \centerfloatboxfalse
       \dogetfloat
       \ifdim\wd\floatbox>\makeupwidth
         \global\setbox\floatbox\hbox to \makeupwidth{\hss\box\floatbox\hss}%
       \fi
       \OTRONEsetvsize
       \!!widtha\wd\floatbox
       \dofloatflushedinfo
       \doloop
         {\ifsomefloatwaiting
            \dosavefloatstatus
            \dogetfloat
            \advance\!!widtha 1em % variable
            \advance\!!widtha \wd\floatbox\relax
            \ifdim\!!widtha>\hsize
              \dorestorefloatstatus
              \global\somefloatwaitingtrue
              \exitloop
            \else
              \global\setbox\floatbox\hbox
                {\ifcase\columndirection % nog document wide
                   \ifvoid\savedfloatbox\else
                     \ifhbox\savedfloatbox\unhbox\else\box\fi\savedfloatbox\hfil
                   \fi
                   \ifhbox\floatbox\unhbox\else\box\fi\floatbox
                 \else
                   \ifhbox\floatbox\unhbox\else\box\fi\floatbox
                   \ifvoid\savedfloatbox\else
                     \hfil\ifhbox\savedfloatbox\unhbox\else\box\fi\savedfloatbox
                   \fi
                 \fi}%
              \dofloatflushedinfo
            \fi
          \else
            \exitloop
          \fi}%
       \global\setbox\floatbox\hbox to \hsize
         {\hfil\ifhbox\floatbox\unhbox\else\box\fi\floatbox\hfil}%
     \else
      %\bgroup % \box\floatbox can be in use!? messy
       \dogetfloat
      %\doplacefloatbox
      %\egroup
       \dofloatflushedinfo
     \fi
     % there is a chance that due to rounding errors, the float
     % fits on a page where it was first rejected, in which case
     % the prevdepth is -maxdimen and we cannot obey the grid
     \doplacefloatbox
     \expandafter\OTRONEdodoflushfloats
   \fi}

% \def\OTRONEdocheckiffloatfits % vervangen ivm downward comp
%   {\ifnofloatpermitted
%      \global\roomforfloatfalse
%    \else
%      \dimen0         \pagetotal
%      \advance\dimen0 \ht\floatbox
%      \advance\dimen0 \dp\floatbox
%      \advance\dimen0 \floattopskip
% %      \advance\dimen0 -\pageshrink  % toegevoegd
%     %\message{c:\the\mofcolumns,t:\the\pagetotal,g:\the\pagegoal}%\wait
%      \ifdim\dimen0>\pagegoal
%        \global\roomforfloatfalse
%      \else
%        \global\roomforfloattrue
%      \fi
%    \fi}

% \def\OTRONEdocheckiffloatfits % vervangen ivm downward comp
%   {\ifnofloatpermitted
%      \global\roomforfloatfalse
%    \else
%      \dimen0         \pagetotal
%      \advance\dimen0 \ht\floatbox
%      \advance\dimen0 \dp\floatbox
%      \advance\dimen0 \floattopskip
%      \advance\dimen0 -\pageshrink  % toegevoegd
%     %\message{c:\the\mofcolumns,t:\the\pagetotal,g:\the\pagegoal}%\wait
%      \dimen2\pagegoal
%      \relax % needed
%      \ifcase\textfloatmethod
%        % 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
%        \global\roomforfloattrue
%      \fi
%    \fi}

\def\OTRONEdocheckiffloatfits % vervangen ivm downward comp
  {\ifnofloatpermitted
     \global\roomforfloatfalse
   \else
     % new per 31/5/2004, should be an option, only one column mode
     \begingroup
     \scratchdimen\pagetotal
     \advance\scratchdimen\lineheight % maybe strutheight
     \ifdim\scratchdimen>\pagegoal
       \goodbreak % hack ? needed in icare-az
     \fi
     % should be an option
     \endgroup
     \dimen0         \pagetotal
     \advance\dimen0 \ht\floatbox
     \advance\dimen0 \dp\floatbox
     \advance\dimen0 \floattopskip
     \advance\dimen0 -\pageshrink  % toegevoegd
    %\message{c:\the\mofcolumns,t:\the\pagetotal,g:\the\pagegoal}%\wait
     \dimen2\pagegoal
     \relax % needed
     \ifcase\textfloatmethod
       % 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
       \global\roomforfloattrue
     \fi
   \fi}

\def\OTRONEflushsavedfloats
  {\dosetbothinserts}

% TODO: TEST FIRST, NO CORRECTION NEEDED IN GRID MODE, EVT OPTION

\def\OTRONEsomeherefloat[#1]% spacing between two successive must be better
  {\baselinecorrection                           % not really needed in grid mode:
  %\ifgridsnapping \else \baselinecorrection \fi % ! ! ! test test test ! ! ! !
   \doplacefloatbox
   \doinsertfloatinfo
   \dochecknextindentation\??bk}

\def\OTRONEsomefixdfloat % [#1]
  {\docheckiffloatfits
   \ifroomforfloat\else
     \goodbreak
   \fi
   \showmessage\m!floatblocks9\empty
   \someherefloat} % [#1]

\def\OTRONEsomesidefloat[#1]%  links, rechts     NOG TESTEN EN AANPASSEN
  {\ifinsidecolumns
     \someelsefloat[\v!here]%
   \else
    %\checkwaitingfloats{#1}%
     \def\logsidefloat
       {\doinsertfloatinfo}%
     \setbox\floatbox\vbox{\box\floatbox}%
     \wd\floatbox\floatwidth
     \processfirstactioninset
       [#1]
       [     \v!left=>\leftfloat       {\box\floatbox},
            \v!right=>\rightfloat      {\box\floatbox},
          \v!inleft=>\leftmarginfloat {\box\floatbox},
         \v!inright=>\rightmarginfloat{\box\floatbox},
       \v!leftmargin=>\leftmarginfloat {\box\floatbox},
      \v!rightmargin=>\rightmarginfloat{\box\floatbox},
        \v!leftedge=>\leftedgefloat   {\box\floatbox},
       \v!rightedge=>\rightedgefloat  {\box\floatbox},
            \v!backspace=>\backspacefloat  {\box\floatbox},
           \v!cutspace=>\cutspacefloat   {\box\floatbox},
           \v!inmargin=>{\doinmargenormal\leftmarginfloat
                       \rightmarginfloat{\box\floatbox}}]%
     \doifinset\v!tall{#1}\flushsidefloatsafterpar
   \fi}

\def\OTRONEsomepagefloat[#1]%
  {%\checkwaitingfloats{#1}%
   \global\setbox\collectedpagefloats\vbox
     {\unvbox\collectedpagefloats
      \vbox to \textheight
        {\doifnotinset\v!high{#1}\vfill
         \box\floatbox
         \doifnotinset\v!low{#1}\vfill}%
      \goodbreak}%
   \doinsertfloatinfo}

\def\OTRONEsometopsfloat[#1]%
  {\ifdim\topinserted=\zeropoint
     \topofinserttrue
   \else
     \topofinsertfalse
   \fi
   \global\advance\topinserted \ht\floatbox
   \global\advance\topinserted \dp\floatbox
   \global\advance\topinserted \floatbottomskip
   \insert\topins
     {\forgetall
      \iftopofinsert
        \topskipcorrection % [xx] new: see icare topbleed
        \kern-\lineskip\par\prevdepth\maxdimen
      \else
        %\blank[-\@@bkspaceafter,\@@bkspacebefore]% inserts can't look back
        \betweenfloatblanko
      \fi
      \flushfloatbox
      \blank[\@@bkspaceafter]}%
   \doinsertfloatinfo}

\def\OTRONEsomebotsfloat[#1]%
  {\global\advance\botinserted \ht\floatbox
   \global\advance\botinserted \dp\floatbox
   \global\advance\botinserted \floattopskip
   \insert\botins
     {\forgetall
      \blank[\@@bkspacebefore]%
      \flushfloatbox}%
   %\global\nofloatpermittedtrue
   \doinsertfloatinfo}

\def\OTRONEsomefacefloat[#1]% untested
  {\startopposite\flushfloatbox\stopopposite}

\def\OTRONEnextcolumn[#1]%
  {}

\protect \endinput
