page-mis.mkii / last modification: 2020-01-30 14:15
%D \module
%D   [       file=page-mis,
%D        version=2008.11.17, % was part of page-flt.tex / 2000.10.20
%D          title=\CONTEXT\ Page Macros,
%D       subtitle=Misc Float Things,
%D         author=Hans Hagen,
%D           date=\currentdate,
%D      copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
%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 Page Macros / Misc Float Things}

\unprotect

\newif\ifmargeblokken

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

\def\setupmarginblocks
  {\dosingleargument\dosetupmarginblocks}

\newbox\marginbox

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

\newbox\preparedmarginbox

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

\def\plaatsrechtermargeblok
  {\hskip\rightmarginwidth}

\def\plaatslinkermargeblok
  {\hskip\leftmarginwidth}

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

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

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

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

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

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

\newif\ifinpostponing

\newevery\everytopofpage\relax

\appendtoks   \the\everytopofpage          \to\everystarttext
\appendtoks\global\everytopofpage\emptytoks\to\everystoptext

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

\newtoks   \postponedpageblocks
\newcounter\nofpostponedpageblocks

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

\def\flushpagefloats
  {\doifoddpageelse
     {\ifvoid\collectedleftpagefloats
        \ifvoid\collectedrightpagefloats\else
           \unvbox\collectedrightpagefloats
           \page
          %\the\everytopofpage
        \fi
      \fi}
     {\ifvoid\collectedleftpagefloats\else
        \unvbox\collectedleftpagefloats
        \page
       %\the\everytopofpage
      \fi
      \ifvoid\collectedrightpagefloats\else
         \unvbox\collectedrightpagefloats
         \page
        %\the\everytopofpage
      \fi}%
   \ifvoid\collectedpagefloats\else
     % message
     \unvbox\collectedpagefloats
   \fi}

% \def\flushrestfloats
%   {\doif{\floatsharedparameter\c!cache}\v!no\doflushfloats}

% \let\flushrestfloats\relax

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

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

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

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

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

\def\dodostartpostponing
  {\doglobal\increment\nofpostponedblocks
   \showmessage\m!layouts3\nofpostponedblocks
   \expanded{\dostartbuffer[pbuf-\nofpostponedblocks][\e!start\v!postponing][\e!stop\v!postponing]}}

% Setups:

\setupmarginblocks
  [\c!state=\v!start,
   \c!location=\v!inmargin,
   \c!width=\rightmarginwidth,
   \c!style=,
   \c!color=,
   \c!align=,
   \c!left=,
   \c!right=,
   \c!top=,
   \c!inbetween=\blank,
   \c!bottom=\vfill,
   \c!before=,
   \c!after=]

\protect \endinput