%D \module
%D   [       file=page-imp, % was: core-pag,
%D        version=1998.01.15,
%D          title=\CONTEXT\ Core Macros,
%D       subtitle=Pagebody Building (Imposition),
%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.

% much of this can more to run time loading !

\writestatus{loading}{Context Core Macros / Pagebody Building}

\unprotect

% sizing bug:
%
% \setuppapersize[A4][A4,landscape] \setuparranging[2UP] \showframe
%
% \starttext \dorecurse{10}{\input tufte \par} \stoptext

% to be moved code:

\newif\ifclipprintbox \clipprintboxtrue
%newif\ifclippagebox  \clippageboxtrue

\def\clippedprintbox#1#2% can be made more efficient, see other clipper
  {\ifclipprintbox
     \!!widthc\pagebackgroundoffset
     \!!widtha \papierbreedte
     \advance\!!widtha \!!widthc
     \!!heighta\papierhoogte
     \advance\!!heighta\!!widthc
     \advance\!!heighta\!!widthc
     \setbox#2\vbox to \papierhoogte{\vfill\box#2}%
     \ht#2\papierhoogte
     \wd#2\papierbreedte
     \setbox#2\vbox
       {\framed
          [\c!offset=\!!widthc,\c!strut=\v!nee,\c!kader=\v!uit]
          {\box#2}}%
     \setbox#2\hbox to \papierbreedte
       {\ifcase#1\relax
          \!!widthb\zeropoint
          \hskip-\!!widthc
        \else
          \!!widthb\!!widthc
        \fi
        \lower\!!widthc\hbox
          {\clip
             [\c!breedte=\!!widtha,\c!hoogte=\!!heighta,
              \c!hoffset=\!!widthb,\c!voffset=\zeropoint]
             {\box#2}}}%
     \wd#2\papierbreedte
     \ht#2\papierhoogte
   \fi
   \box#2\relax}

\let\clippagebox \gobbleoneargument
\let\clipprintbox\gobbleoneargument

% \setuppagenumbering[alternative=doublesided]
% \setupcolors[state=start]
% \setuppapersize[A4][A4,oversized]
% \setuplayout[location=middle,clipoffset=5mm]
% \setupbackgrounds
%   [page]
%   [frame=on,rulethickness=1mm,
%    backgroundoffset=10mm,background=color,backgroundcolor=red]
% \starttext \dorecurse{10}{\input tufte \par} \stoptext

\def\clippagebox#1%
  {\ifdim\@@lyclipoffset>\zeropoint
     \!!widtha \wd#1%
     \!!heighta\ht#1%
     \!!deptha \dp#1%
     \setbox#1\hbox
       {\!!widthb \@@lyclipoffset
        \advance\!!heighta\!!deptha
        \advance\!!heighta2\!!widthb
        \advance\!!widtha  \!!widthb
        \doifbothsides
          \advance\!!widtha\!!widthb
          \!!widthc-\!!widthb
          \hskip\!!widthc
        \orsideone
          \!!widthc\zeropoint
        \orsidetwo
          \!!widthc-\!!widthb
          \hskip\!!widthc
        \od
        \lower\!!widthb\hbox
          {\clip
             [\c!hoffset=\!!widthc,
              \c!voffset=-\!!widthb,
              \c!breedte=\!!widtha,
              \c!hoogte=\!!heighta]%
             {\box#1}}}%
     \wd#1\!!widtha
     \ht#1\!!heighta
     \dp#1\!!deptha
   \fi}

% moved code:

\def\myshipout#1%
  {\voorpagina % voor de pagebody dus !
   \dontshowcomposition
   \ifarrangingpages\@EA\actualarrange\else\@EA\actualshipout\fi
     {\thisisrealpage\realfolio#1}%
   \gotonextrealpage
   \napagina}

\newbox\postponedcontent

\def\flushatshipout
  {\dowithnextbox
     {\global\setbox\postponedcontent\hbox to \zeropoint
        {%\hskip-\maxdimen % niet hier, gaat mis in acrobat (clipt)
         \box\postponedcontent\box\nextbox}%
      \global\ht\postponedcontent\zeropoint
      \global\dp\postponedcontent\zeropoint
      \global\wd\postponedcontent\zeropoint}%
   \hbox}

% \starttypen
% \def\pagestoshipout{1,3,5}
% \stoptypen

\newcount\shippedoutpages

\let\pagestoshipout\empty      % {1,3,6}
\chardef\whichpagetoshipout=0 % 0=all 1=odd 2=even

\def\actualshipout#1%
  {\global\advance\shippedoutpages\plusone
   \ifx\pagestoshipout\empty
     \ifcase\whichpagetoshipout\relax
       \donetrue
     \or % 1
       \ifodd\shippedoutpages\relax\donetrue\else\donefalse\fi
     \or % 2
       \ifodd\shippedoutpages\relax\donefalse\else\donetrue\fi
     \else
       \donetrue
     \fi
   \else % testen, aangepast / expanded nodig ?
     \expanded{\doifinsetelse{\the\shippedoutpages}{\pagestoshipout}}%
       \donetrue\donefalse
   \fi
   \ifdone
     \shipout\vbox
       {\forgetall
        \offinterlineskip
        \mindermeldingen
        \vskip-1in
        \hskip-1in
        \hbox % \setbox0=\box.. is nicer
          {\setbox0\hbox{#1}% just in case there are objects there
           \setbox\scratchbox\hbox
             {\the\everyshipout
              \ifnum\realpageno=\lastpage\relax
                \flushtextdata
                \the\everylastshipout
                \global\everylastshipout\emptytoks
              \fi}%
           \smashbox\scratchbox
           \box\scratchbox
           \box\postponedcontent % evt ver naar links !
           \box0}}%
   \else
     \message
       {[\ifarrangingpages arranged \fi page
         \ifarrangingpages\the\arrangeno\else\the\realpageno\fi\normalspace
         not flushed]}%
     \setbox0\hbox{#1}%
     \deadcycles\zerocount
   \fi}

\def\actualarrange#1%
  {\setbox0\hbox{\thisisrealpage{\realfolio}#1}%
   \pusharrangedpage0%
   \deadcycles\zerocount}

%D We need a couple of boxes for duplex printing \unknown

\newbox\arrangedpageA \newbox\arrangedpageB

%D \unknown\ and some for simulating big sheets.

\newbox\arrangedpageC \newbox\arrangedpageD
\newbox\arrangedpageE \newbox\arrangedpageF
\newbox\arrangedpageG \newbox\arrangedpageH

\newif\ifswaparranged
\newif\ifnegatearranged
\newif\ifmirrorarranged
\newif\ifdoublearranged

\newif\ifarrangingdisabled

\def\arrangedrotationO{0}
\def\arrangedrotationE{0}

\newcounter\arrangedpageN
\newcounter\arrangedpageM

\chardef\arrangedpageT=1
\chardef\arrangedpageX=1
\chardef\arrangedpageY=1

\def\calculatepaperoffsets#1%
  {\scratchdimen\getvalue{\??pp#1\c!offset}%
   \divide\scratchdimen \arrangedpageX
   \global\advance\papierbreedte -2\scratchdimen
   \scratchdimen\getvalue{\??pp#1\c!offset}%
   \divide\scratchdimen \arrangedpageY
   \global\advance\papierhoogte -2\scratchdimen}

\def\setuparranging[#1]%
  {\ifarrangingdisabled \else
     \doifelse{#1}\v!blokkeer
       {\global\arrangingdisabledtrue}
       {\global\arrangingdisabledfalse}%
     \global\arrangingpagestrue
     \global\negatearrangedfalse
     \global\mirrorarrangedfalse
     \global\doublearrangedfalse
     \gdef\arrangedrotationO{0}%
     \gdef\arrangedrotationE{180}%
     \processallactionsinset
       [#1]
       [  \v!gespiegeld=>\global\mirrorarrangedtrue,
        \v!dubbelzijdig=>\global\doublearrangedtrue,
            \v!negatief=>\global\negatearrangedtrue,
           \v!geroteerd=>\gdef\arrangedrotationO {90}\gdef\arrangedrotationE{270},
                     90=>\gdef\arrangedrotationO {90}\gdef\arrangedrotationE{270},
                    180=>\gdef\arrangedrotationO{180}\gdef\arrangedrotationE{0},
                    270=>\gdef\arrangedrotationO{270}\gdef\arrangedrotationE{90},
               \s!reset=>\global\arrangingpagesfalse,
             \s!unknown=>\checkinstalledpagearrangement\commalistelement]%
        % no \s!default=> we can have aaa,,bbb      
     \doifcommonelse{#1}{90,270,\v!geroteerd}
       {\swapmacros\horizontalcutmarks\verticalcutmarks}{}% ugly solution
     \setuppapersize
     \ifarrangingpages
       \abortutilitygeneration
     \fi
   \fi}

\def\installpagearrangement #1 %
  {\setgvalue{\??pp\??pp#1}}

\def\checkinstalledpagearrangement#1% can be empty: aaa,,bbb
  {\executeifdefined{\??pp\??pp#1}\donothing} 

\def\dosetuparrangement#1#2#3#4#5#6#7#8%
  {\global\chardef\arrangedpageX     #1%
   \global\chardef\arrangedpageY     #2%
   \global\chardef\arrangedpageT     #3%
   \global\chardef\horizontalcutmarks#4%
   \global\chardef\verticalcutmarks  #5%
   \global\let    \pusharrangedpage  #6%
   \global\let    \poparrangedpages  #7%
   \global\let    \handlearrangedpage#8}

\installpagearrangement {\v!normaal}
  {\global\arrangingpagesfalse}

\installpagearrangement 2*16
  {\dosetuparrangement{4}{4}{16}{5}{5}%
     \pusharrangedpageTHIRTYTWO\poparrangedpagesAB\relax}

\installpagearrangement 2*8
  {\dosetuparrangement{4}{2}{8}{5}{3}%
     \pusharrangedpageSIXTEEN\poparrangedpagesAB\relax}

\installpagearrangement 2*4
  {\dosetuparrangement{2}{2}{4}{3}{3}%
     \pusharrangedpageEIGHT\poparrangedpagesAB\relax}

\installpagearrangement 2*2
  {\dosetuparrangement{2}{1}{2}{3}{2}%
     \pusharrangedpageFOURA\poparrangedpagesAB\relax}

\installpagearrangement 2**2
  {\dosetuparrangement{2}{1}{2}{3}{2}%
     \pusharrangedpageFOURB\poparrangedpagesAB\relax}

\installpagearrangement 2SIDE
  {\dosetuparrangement{2}{1}{2}{3}{2}%
     \pusharrangedpageSIDETOP\poparrangedpagesTWO\handlearrangedpageSIDE}

\installpagearrangement 2TOP
  {\dosetuparrangement{1}{2}{2}{2}{3}%
     \pusharrangedpageSIDETOP\poparrangedpagesTWO\handlearrangedpageTOP}

\installpagearrangement 2UP
  {\dosetuparrangement{2}{1}{4}{3}{2}%
     \pusharrangedpageTWO\poparrangedpagesTWO\handlearrangedpageTWOUP}

\installpagearrangement 2DOWN
  {\dosetuparrangement{1}{2}{4}{2}{3}%
     \pusharrangedpageTWO\poparrangedpagesTWO\handlearrangedpageTWODOWN}

\installpagearrangement 2*4*2 % one defined by Willy Egger:
  {\dosetuparrangement{2}{2}{4}{3}{2}%
     \pusharrangedpageSIXTEENTWO\poparrangedpagesAtoD\relax}

\installpagearrangement 2*2*4 % onother one of Willy Egger
  {\dosetuparrangement{2}{1}{8}{3}{2}%
     \pusharrangedpageSIXTEENFOUR\poparrangedpagesAtoH\relax}

% \def\filluparrangedpages % beware: \realpageno is 1 ahead
%   {\ifarrangingpages
%      \scratchcounter-\realpageno
%      \divide\scratchcounter \arrangedpageT
%      \multiply\scratchcounter \arrangedpageT
%      \advance\scratchcounter \realpageno
%      \advance\scratchcounter \minusone
%      \dorecurse\scratchcounter{\noheaderandfooterlines\ejectdummypage}%
%    \fi}

\def\filluparrangedpages % beware: \realpageno is 1 ahead
  {\ifarrangingpages
     \scratchcounter\realpageno 
     \advance\scratchcounter \minusone
     \dosetmodulo\scratchcounter\arrangedpageT\scratchcounter 
     \ifcase\scratchcounter\else
       \advance\scratchcounter \plusone
       \dostepwiserecurse\scratchcounter\arrangedpageT\plusone
         {\noheaderandfooterlines\ejectdummypage}%
     \fi
   \fi}

\def\handlearrangedpageXandY#1#2#3#4#5%
  {\global\setbox#5\hbox to \arrangedpageX\papierbreedte
     {\setbox\scratchbox\vbox to \arrangedpageY\papierhoogte
        {\forgetall
         \offinterlineskip
         \mindermeldingen
         \vskip#4\papierhoogte
         \hskip#3\papierbreedte
         \dorotatebox{\ifcase#2 0\else180\fi}\hbox{\box#1}%
         \vfill}%
      \wd\scratchbox\zeropoint
      \box\scratchbox\box#5\hss}}

\def\gotonextarrangepage
  {\global\advance\arrangeno \plusone
   \def\pagecutmarksymbol{\the\arrangeno}}

\def\outputarrangedbox#1%
  {\bgroup
   \gotonextarrangepage
   \ifnum\arrangedrotationO\arrangedrotationE>\zerocount
     \setbox#1\vbox
       {\ifdoublearranged
          \ifodd\arrangeno
            \dorotatebox\arrangedrotationO\hbox{\box#1}%
          \else
            \dorotatebox\arrangedrotationE\hbox{\box#1}%
          \fi
        \else
          \dorotatebox\arrangedrotationO\hbox{\box#1}%
        \fi}%
   \fi
   \ifmirrorarranged
     \setbox#1\vbox{\domirrorbox\vbox{\box#1}}%
   \fi
   \ifnegatearranged
     \negatecolorbox{#1}%
   \fi
   \finishpagebox#1
   \actualshipout{\box#1}%
   \egroup}

%D The format file can be 16K smaller when we postpone the
%D real arrangments. Some day ...

% TOP

% 32/16/8/4/SIDE

\def\poparrangedpagesAB
  {\ifnum\arrangedpageN>\zerocount
     \mindermeldingen
     \papierbreedte\arrangedpageX\papierbreedte
     \papierhoogte\arrangedpageY\papierhoogte
     \outputarrangedbox\arrangedpageA
     \outputarrangedbox\arrangedpageB
     \doglobal\newcounter\arrangedpageN
   \fi}

\def\pusharrangedpageTHIRTYTWO#1% taco's challenge
  {\doglobal\increment\arrangedpageN
   \reportarrangedpage\arrangedpageN
   \ifcase\arrangedpageN
   \or \handlearrangedpageXandY{#1}033\arrangedpageA %  1
   \or \handlearrangedpageXandY{#1}003\arrangedpageB %  2
   \or \handlearrangedpageXandY{#1}100\arrangedpageB %  3
   \or \handlearrangedpageXandY{#1}130\arrangedpageA %  4
   \or \handlearrangedpageXandY{#1}100\arrangedpageA %  5
   \or \handlearrangedpageXandY{#1}130\arrangedpageB %  6
   \or \handlearrangedpageXandY{#1}033\arrangedpageB %  7
   \or \handlearrangedpageXandY{#1}003\arrangedpageA %  8
   \or \handlearrangedpageXandY{#1}102\arrangedpageA %  9
   \or \handlearrangedpageXandY{#1}132\arrangedpageB % 10
   \or \handlearrangedpageXandY{#1}031\arrangedpageB % 11
   \or \handlearrangedpageXandY{#1}001\arrangedpageA % 12
   \or \handlearrangedpageXandY{#1}031\arrangedpageA % 13
   \or \handlearrangedpageXandY{#1}001\arrangedpageB % 14
   \or \handlearrangedpageXandY{#1}102\arrangedpageB % 15
   \or \handlearrangedpageXandY{#1}132\arrangedpageA % 16
   \or \handlearrangedpageXandY{#1}122\arrangedpageA % 17
   \or \handlearrangedpageXandY{#1}112\arrangedpageB % 18
   \or \handlearrangedpageXandY{#1}011\arrangedpageB % 19
   \or \handlearrangedpageXandY{#1}021\arrangedpageA % 20
   \or \handlearrangedpageXandY{#1}011\arrangedpageA % 21
   \or \handlearrangedpageXandY{#1}021\arrangedpageB % 22
   \or \handlearrangedpageXandY{#1}122\arrangedpageB % 23
   \or \handlearrangedpageXandY{#1}112\arrangedpageA % 24
   \or \handlearrangedpageXandY{#1}013\arrangedpageA % 25
   \or \handlearrangedpageXandY{#1}023\arrangedpageB % 26
   \or \handlearrangedpageXandY{#1}120\arrangedpageB % 27
   \or \handlearrangedpageXandY{#1}110\arrangedpageA % 28
   \or \handlearrangedpageXandY{#1}120\arrangedpageA % 29
   \or \handlearrangedpageXandY{#1}110\arrangedpageB % 30
   \or \handlearrangedpageXandY{#1}013\arrangedpageB % 31
   \or \handlearrangedpageXandY{#1}023\arrangedpageA % 32
     \poparrangedpages
   \fi}

\def\pusharrangedpageSIXTEEN#1% changed to match the official way of doing
  {\doglobal\increment\arrangedpageN
   \reportarrangedpage\arrangedpageN
   \ifcase\arrangedpageN
   \or \handlearrangedpageXandY{#1}031\arrangedpageA %  1
   \or \handlearrangedpageXandY{#1}001\arrangedpageB %  2
   \or \handlearrangedpageXandY{#1}031\arrangedpageB %  3
   \or \handlearrangedpageXandY{#1}001\arrangedpageA %  4
   \or \handlearrangedpageXandY{#1}100\arrangedpageA %  5
   \or \handlearrangedpageXandY{#1}130\arrangedpageB %  6
   \or \handlearrangedpageXandY{#1}100\arrangedpageB %  7
   \or \handlearrangedpageXandY{#1}130\arrangedpageA %  8
   \or \handlearrangedpageXandY{#1}120\arrangedpageA %  9
   \or \handlearrangedpageXandY{#1}110\arrangedpageB % 10
   \or \handlearrangedpageXandY{#1}120\arrangedpageB % 11
   \or \handlearrangedpageXandY{#1}110\arrangedpageA % 12
   \or \handlearrangedpageXandY{#1}011\arrangedpageA % 13
   \or \handlearrangedpageXandY{#1}021\arrangedpageB % 14
   \or \handlearrangedpageXandY{#1}011\arrangedpageB % 15
   \or \handlearrangedpageXandY{#1}021\arrangedpageA % 16
     \poparrangedpages
   \fi}

\def\pusharrangedpageEIGHT#1% changed to match the official way of doing
  {\doglobal\increment\arrangedpageN
   \reportarrangedpage\arrangedpageN
   \ifcase\arrangedpageN
   \or \handlearrangedpageXandY{#1}011\arrangedpageA %  1
   \or \handlearrangedpageXandY{#1}001\arrangedpageB %  2
   \or \handlearrangedpageXandY{#1}100\arrangedpageB %  3
   \or \handlearrangedpageXandY{#1}110\arrangedpageA %  4
   \or \handlearrangedpageXandY{#1}100\arrangedpageA %  5
   \or \handlearrangedpageXandY{#1}110\arrangedpageB %  6
   \or \handlearrangedpageXandY{#1}011\arrangedpageB %  7
   \or \handlearrangedpageXandY{#1}001\arrangedpageA %  8
     \poparrangedpages
   \fi}

\def\pusharrangedpageFOURA{\pusharrangedpageFOURdo01}
\def\pusharrangedpageFOURB{\pusharrangedpageFOURdo10}

\def\pusharrangedpageFOURdo#1#2#3%
  {\doglobal\increment\arrangedpageN
   \reportarrangedpage\arrangedpageN
   \ifcase\arrangedpageN
   \or \handlearrangedpageXandY{#3}010\arrangedpageA    %  1
   \or \handlearrangedpageXandY{#3}0{#1}0\arrangedpageB %  2/3 not {1}
   \or \handlearrangedpageXandY{#3}0{#2}0\arrangedpageB %  3/2 not {1}
   \or \handlearrangedpageXandY{#3}000\arrangedpageA    %  4
     \poparrangedpages
   \fi}

\def\pusharrangedpageSIDETOP#1%
  {\doglobal\increment\arrangedpageN
   \reportarrangedpage\arrangedpageN
   \ifcase\arrangedpageN
   \or \handlearrangedpageXandY{#1}000\arrangedpageA %  1
   \or \handlearrangedpageXandY{#1}000\arrangedpageB %  2
     \poparrangedpages
   \fi}

\def\handlearrangedpageSIDE
  {\global\wd\arrangedpageA\papierbreedte
   \global\wd\arrangedpageB\papierbreedte
   \global\setbox\arrangedpageA\hbox
     {\box\arrangedpageA\box\arrangedpageB}%
   \global\ht\arrangedpageA\papierhoogte
   \global\setbox\arrangedpageB\box\scratchbox} % ?

\def\handlearrangedpageTOP
  {\global\ht\arrangedpageA\papierhoogte
   \global\ht\arrangedpageB\papierhoogte
   \global\setbox\arrangedpageA\vbox
     {\forgetall\offinterlineskip\vskip\papierhoogte
      \box\arrangedpageA\box\arrangedpageB}%
   \global\setbox\arrangedpageB\box\scratchbox} % ?

% 2UP/2DOWN / 1pt prevents overflow

\def\splitoffarrangedpagesTWO
  {\splittopskip\zeropoint
   \global\setbox\arrangedpageA\vsplit\arrangedpageB to \!!onepoint
   \scratchdimen\ht\arrangedpageB
   \advance\scratchdimen -\!!onepoint
   \ifdim\scratchdimen>\!!onepoint
     \setbox\scratchbox\vsplit\arrangedpageB to \scratchdimen
   \fi}

% \def\handlearrangedpageTWOUP
%   {\splitoffarrangedpagesTWO
%    \ifswaparranged
%      \global\setbox\arrangedpageA\hbox
%        {\box\arrangedpageA\box\arrangedpageB}%
%      \swaparrangedfalse
%    \else
%      \global\setbox\arrangedpageA\hbox
%        {\box\arrangedpageB\box\arrangedpageA}%
%      \swaparrangedtrue
%    \fi
%    \global\ht\arrangedpageA\papierhoogte
%    \global\setbox\arrangedpageB\box\scratchbox}

\def\handlearrangedpageTWOUP
  {\splitoffarrangedpagesTWO
   \ifswaparranged
     \global\setbox\arrangedpageA\hbox
       {\clippedprintbox0\arrangedpageA
        \clippedprintbox1\arrangedpageB}%
     \swaparrangedfalse
   \else
     \global\setbox\arrangedpageA\hbox
       {\clippedprintbox0\arrangedpageB
        \clippedprintbox1\arrangedpageA}%
     \swaparrangedtrue
   \fi
   \global\ht\arrangedpageA\papierhoogte
   \global\setbox\arrangedpageB\box\scratchbox}

\def\handlearrangedpageTWODOWN
  {\splitoffarrangedpagesTWO
   \global\ht\arrangedpageA\papierhoogte
   \global\ht\arrangedpageB\papierhoogte
   \ifswaparranged
     \global\setbox\arrangedpageA\vbox
       {\forgetall\offinterlineskip\vskip\papierhoogte
        \box\arrangedpageA\box\arrangedpageB}%
     \swaparrangedfalse
   \else
     \global\setbox\arrangedpageA\vbox
       {\forgetall\offinterlineskip\vskip\papierhoogte
        \box\arrangedpageB\box\arrangedpageA}%
     \swaparrangedtrue
   \fi
   \global\setbox\arrangedpageB\box\scratchbox}

\def\poparrangedpagesTWO
  {\ifnum\arrangedpageN>\zerocount
     \mindermeldingen
     \swaparrangedfalse
     \doloop
       {\handlearrangedpage
        \bgroup
        \papierbreedte\arrangedpageX\papierbreedte
        \papierhoogte\arrangedpageY\papierhoogte
        \ht\arrangedpageA\papierhoogte
        \wd\arrangedpageA\papierbreedte
        \outputarrangedbox\arrangedpageA
        \egroup
        \ifdim\ht\arrangedpageB=\zeropoint
          \exitloop
        \fi}%
     \doglobal\newcounter\arrangedpageN
   \fi}

\def\pusharrangedpageTWO#1%
  {\doglobal\increment\arrangedpageN
   \reportarrangedpage\arrangedpageN
   \global\setbox\arrangedpageB\vbox
     {\forgetall
      \offinterlineskip
      \unvbox\arrangedpageB
      \allowbreak
      \ht#1=\!!onepoint
      \dp#1=\zeropoint
      \vbox{\box#1}}}

%D Willy Egger's sheet simulations:

\def\poparrangedpagesAtoH
  {\ifnum\arrangedpageN>\zerocount
     \mindermeldingen
     \papierbreedte\arrangedpageX\papierbreedte
     \papierhoogte\arrangedpageY\papierhoogte
     \outputarrangedbox\arrangedpageA
     \outputarrangedbox\arrangedpageB
     \outputarrangedbox\arrangedpageC
     \outputarrangedbox\arrangedpageD
     \outputarrangedbox\arrangedpageE
     \outputarrangedbox\arrangedpageF
     \outputarrangedbox\arrangedpageG
     \outputarrangedbox\arrangedpageH
     \doglobal\newcounter\arrangedpageN
   \fi}

% to arrange 16 pages on 2 sheets to form one booklet

\def\poparrangedpagesAtoD
  {\ifnum\arrangedpageN>\zerocount
     \mindermeldingen
     \papierbreedte\arrangedpageX\papierbreedte
     \papierhoogte\arrangedpageY\papierhoogte
     \outputarrangedbox\arrangedpageA
     \outputarrangedbox\arrangedpageB
     \outputarrangedbox\arrangedpageC
     \outputarrangedbox\arrangedpageD
     \doglobal\newcounter\arrangedpageN
   \fi}

% to arrange 16 pages on 4 sheets to form one booklet

\def\pusharrangedpageSIXTEENFOUR#1%
  {\doglobal\increment\arrangedpageN
   \reportarrangedpage\arrangedpageN
   \ifcase\arrangedpageN
   \or \handlearrangedpageXandY{#1}010\arrangedpageA %  1
   \or \handlearrangedpageXandY{#1}000\arrangedpageB %  2
   \or \handlearrangedpageXandY{#1}010\arrangedpageC %  3
   \or \handlearrangedpageXandY{#1}000\arrangedpageD %  4
   \or \handlearrangedpageXandY{#1}010\arrangedpageE %  5
   \or \handlearrangedpageXandY{#1}000\arrangedpageF %  6
   \or \handlearrangedpageXandY{#1}010\arrangedpageG %  7
   \or \handlearrangedpageXandY{#1}000\arrangedpageH %  8
   \or \handlearrangedpageXandY{#1}010\arrangedpageH %  9
   \or \handlearrangedpageXandY{#1}000\arrangedpageG % 10
   \or \handlearrangedpageXandY{#1}010\arrangedpageF % 11
   \or \handlearrangedpageXandY{#1}000\arrangedpageE % 12
   \or \handlearrangedpageXandY{#1}010\arrangedpageD % 13
   \or \handlearrangedpageXandY{#1}000\arrangedpageC % 14
   \or \handlearrangedpageXandY{#1}010\arrangedpageB % 15
   \or \handlearrangedpageXandY{#1}000\arrangedpageA % 16
     \poparrangedpages
   \fi}

% to arrange 16 pages on 2 sheets to form one booklet

\def\pusharrangedpageSIXTEENTWO#1%
  {\doglobal\increment\arrangedpageN
   \reportarrangedpage\arrangedpageN
   \ifcase\arrangedpageN
   \or \handlearrangedpageXandY{#1}011\arrangedpageA %  1
   \or \handlearrangedpageXandY{#1}001\arrangedpageB %  2
   \or \handlearrangedpageXandY{#1}011\arrangedpageC %  3
   \or \handlearrangedpageXandY{#1}001\arrangedpageD %  4
   \or \handlearrangedpageXandY{#1}100\arrangedpageD %  5
   \or \handlearrangedpageXandY{#1}110\arrangedpageC %  6
   \or \handlearrangedpageXandY{#1}100\arrangedpageB %  7
   \or \handlearrangedpageXandY{#1}110\arrangedpageA %  8
   \or \handlearrangedpageXandY{#1}100\arrangedpageA %  9
   \or \handlearrangedpageXandY{#1}110\arrangedpageB % 10
   \or \handlearrangedpageXandY{#1}100\arrangedpageC % 11
   \or \handlearrangedpageXandY{#1}110\arrangedpageD % 12
   \or \handlearrangedpageXandY{#1}011\arrangedpageD % 13
   \or \handlearrangedpageXandY{#1}001\arrangedpageC % 14
   \or \handlearrangedpageXandY{#1}011\arrangedpageB % 15
   \or \handlearrangedpageXandY{#1}001\arrangedpageA % 16
     \poparrangedpages
   \fi}

% % handy for stickers etc, this way we can treat them as page
% 
% \setuppapersize [XY][A4]
% \setuppaper     [topspace=5mm,backspace=5mm,dx=1mm,dy=1mm,nx=2,ny=6]
% \setuplayout    [page] [topspace=5mm,backspace=5mm]
% \setuplayout    [page] 
% \setuplayout    [location=middle]
% \setuparranging [XY]
% \showframe
% 
% \starttext \dorecurse{30}{test \recurselevel \page} \stoptext

\def\pusharrangedpageXY#1%
  {\doglobal\increment\arrangedpageN
   \reportarrangedpage\arrangedpageN
   \doglobal\increment\arrangedpageM
   \global\setbox\arrangedpageB\hbox
     \ifdim\@@ppbreedte>\zeropoint to \@@ppbreedte \fi 
     {\ifvoid\arrangedpageB\else
        \unhbox\arrangedpageB\hss\hskip\@@ppdx\hss
      \fi
      \box#1}%
   \ifnum\arrangedpageM<\arrangedpageX\else
     \global\setbox\arrangedpageA\vbox
          \ifdim\@@pphoogte>\zeropoint to \@@pphoogte \fi 
       {\offinterlineskip
        \ifvoid\arrangedpageA\else
          \unvbox\arrangedpageA\vss\vskip\@@ppdy\vss
        \fi
        \box\arrangedpageB}%
     \doglobal\newcounter\arrangedpageM
   \fi 
   \ifnum\arrangedpageN<\arrangedpageT\else
     \poparrangedpages
   \fi}

\def\poparrangedpagesXY
  {\ifnum\arrangedpageN>\zerocount
     \mindermeldingen
     \papierbreedte\arrangedpageX\papierbreedte
     \papierhoogte \arrangedpageY\papierhoogte
     \outputarrangedbox\arrangedpageA
     \doglobal\newcounter\arrangedpageN
     \doglobal\newcounter\arrangedpageM
   \fi}

\installpagearrangement XY
  {\dosetuparrangement\@@ppnx\@@ppny\@@ppxy\!!zerocount\!!zerocount
     \pusharrangedpageXY\poparrangedpagesXY\relax}

\beginETEX \dimexpr 

\definepapersize
  [XY]
  [\c!breedte=\dimexpr(\dimexpr(\@@ppbreedte-\numexpr(\@@ppnx-1)\dimexpr(\@@ppdx))/\@@ppnx),
   \c!hoogte =\dimexpr(\dimexpr(\@@pphoogte -\numexpr(\@@ppny-1)\dimexpr(\@@ppdy))/\@@ppny)]

\setuppaper
  [\c!breedte=\dimexpr(\printpapierbreedte-2\dimexpr(\@@pprugwit)),
   \c!hoogte =\dimexpr(\printpapierhoogte -2\dimexpr(\@@ppkopwit))]

\endETEX 

% \definepageshift[test][horizontal][10pt,20pt,30pt,40pt,50pt]
% \definepageshift[test][vertical]  [10pt,20pt,30pt,40pt,50pt]
%
% \setuppageshift[test]
% \setuppageshift[test][test]
% \setuppageshift[test][none]
% \setuppageshift[none][test]
% \setuppageshift[paper][test][test] % arrange only
% \setuppageshift[paper][test]       % arrange only
% \setuppageshift[print][test][test]
%
% \showframe \dorecurse{100}{\input tufte \par}

% #1=name #2=horizontal|vertical #3=shiftlist

\def\definepageshift
  {\dotripleargument\dodefinepageshift}

\def\dodefinepageshift[#1][#2][#3]%
  {\setvalue{\??pt#2:#1}{#3}}

\letempty \hpageshifts \newcounter\nofhpageshifts
\letempty \vpageshifts \newcounter\nofvpageshifts

% \let\shiftprintpagebox\gobbleoneargument
% \let\shiftpaperpagebox\gobbleoneargument

\def\dogetpageshift#1#2#3% #1=\dimenx #2=\xpageshifts #3=\nofxpageshifts
  {\ifx#2\empty
     #1\zeropoint
   \else
     \doglobal\increment#3%
     \getfromcommacommand[#2][#3]%
     \ifx\commalistelement\empty
       \globallet#3\!!plusone
       \getfromcommacommand[#2][#3]%
     \fi
     \ifx\commalistelement\empty
       #1\zeropoint
     \else
       #1=\commalistelement
       \donetrue
     \fi
   \fi}

\def\shiftpagebox#1%
  {\donefalse
   \dogetpageshift{\dimen0}\hpageshifts\nofhpageshifts
   \dogetpageshift{\dimen2}\vpageshifts\nofvpageshifts
   \ifdone % see also layout offsets, maybe \movebox
     \edef\next{\wd#1\the\wd#1\ht#1\the\ht#1\dp#1\the\dp#1}%
     \setbox#1\vbox % \forgetall already done
       {\offinterlineskip\vskip\dimen2\hskip\dimen0\box#1}%
     \next
   \fi}

\def\setuppageshift
  {\dotripleempty\dosetuppageshift}

\def\dosetuppageshift[#1][#2][#3]% page|paper horizontal vertical
  {\ifthirdargument              % paper=arrange
     \let\hpageshifts\empty
     \let\vpageshifts\empty
     \let\shiftprintpagebox\gobbleoneargument
     \let\shiftpaperpagebox\gobbleoneargument
     \doifdefined{\??pt\v!horizontaal:#2}
       {\edef\hpageshifts{\getvalue{\??pt\v!horizontaal:#2}}}%
     \doifdefined{\??pt\v!vertikaal  :#3}
       {\edef\vpageshifts{\getvalue{\??pt\v!vertikaal  :#3}}}%
     \doif{#1}\v!pagina{\let\shiftprintpagebox\shiftpagebox}%
     \doif{#1}\v!papier{\let\shiftpaperpagebox\shiftpagebox}%
   \else\ifsecondargument
     \doifinsetelse{#1}{\v!pagina,\v!papier}
       {\setuppageshift[#1][#2][#2]}
       {\setuppageshift[\v!pagina][#1][#2]}%
   \else\iffirstargument
     \setuppageshift[\v!pagina][#1][#1]%
   \fi\fi}

%D One can (mis)use this mechanism, in close cooperation
%D with \PDFTEX\ to arrange pages of already produced files.
%D
%D \starttypen
%D \insertpages[file.pdf][1,3][n=30,width=18cm]
%D \stoptypen
%D
%D The pages are inserted in the text area, and even pages
%D are repositioned according to the width. In this example
%D empty pages are added after page 1 and 3.
%D
%D Selecting pages can be accomplished by:
%D
%D \starttypen
%D \filterpages[file.pdf][1,3,5][n=30,width=18cm]
%D \stoptypen
%D
%D One may pass \type {odd} or \type {even} instead of a
%D comma separated list. A third alternative is:
%D
%D \starttypen
%D \copypages[file.pdf][n=30,scale=950]
%D \stoptypen
%D
%D This macros inserts the page, according to the settings
%D provided.

\def\insertpages
  {\dotripleempty\doinsertpages}

\def\doinsertpages[#1][#2][#3]%
  {\doifassignmentelse{#2}
     {\dodoinsertpages[#1][][#2]}
     {\dodoinsertpages[#1][#2][#3]}}

\def\dodoinsertpages[#1][#2][#3]%
  {\bgroup
   \mindermeldingen
   \getfiguredimensions[#1]%
   \getparameters[\??ip][\c!n=\noffigurepages,\c!breedte=\!!zeropoint,#3]%
   \doifinset0{#2}{\null\pagina}%
   \dorecurse\@@ipn
     {\dofilterpage{#1}\recurselevel
      \doifinset\recurselevel{#2}{\null\pagina}}%
   \egroup}

\def\filterpages
  {\dotripleempty\dofilterpages}

\def\dofilterpages[#1][#2][#3]% % \noffigurepages not yet supported
  {\bgroup
   \mindermeldingen
   \getfiguredimensions[#1]%
   \getparameters[\??ip][\c!n=\noffigurepages,\c!breedte=\!!zeropoint,#3]%
   \doifelse{#2}\v!even
     {\dorecurse\@@ipn
        {\ifodd\recurselevel\relax\else\dofilterpage{#1}\recurselevel\fi}}
     {\doifelse{#2}\v!oneven
        {\dorecurse\@@ipn
           {\ifodd\recurselevel\relax\dofilterpage{#1}\recurselevel\fi}}
        {\def\dodocommando##1%
           {\ifnum##1>\@@ipn\else\dofilterpage{#1}{##1}\fi}%
         \def\docommando##1%
           {\dowithrange{##1}\dodocommando}%
         \processcommalist[#2]\docommando}}%
   \egroup}

\def\dowithrange#1#2% #2 takes number
  {\beforesplitstring#1\at:\to\fromrange
   \aftersplitstring #1\at:\to\torange
   \ifx\torange\empty\let\torange\fromrange\fi
   \dostepwiserecurse\fromrange\torange1{#2{\recurselevel}}}

\def\dofilterpage#1#2%
  {\hbox to \tekstbreedte
     {\ifdubbelzijdig\ifdim\@@ipbreedte>\zeropoint\relax\ifodd\realpageno\else
        \hfill
        \def\dowithfigure{\hskip-\@@ipbreedte}%
      \fi\fi\fi
      \setbox0\hbox
        {\externalfigure[#1][\c!pagina=#2,\c!hoogte=\teksthoogte]}%
      \wd0\zeropoint
      \box0}
   \pagina}

\def\copypages
  {\dodoubleempty\docopypages}

\def\docopypages[#1][#2]%
  {\bgroup
   \getfiguredimensions[#1]%
   \getparameters[\??ip]
     [\c!n=\noffigurepages,
      \c!markering=\v!uit,
      \c!schaal=\!!thousand,
      \c!offset=\!!zeropoint,
      #2]%
   \dorecurse\@@ipn
     {\vbox to \teksthoogte
        {\hsize\tekstbreedte
         \scratchdimen\@@ipoffset
         \centeredbox
           {\doifelse\@@ipmarkering\v!aan\cuthbox\hbox
              {\ifdim\scratchdimen>\zeropoint\relax
                 \advance\vsize -2\scratchdimen
                 \advance\hsize -2\scratchdimen
                 \externalfigure[#1][\c!pagina=\recurselevel,#2,\c!schaal=,\c!factor=\v!max,\c!offset=\v!overlay]%
               \else
                 \externalfigure[#1][\c!pagina=\recurselevel,#2,\c!offset=\v!overlay]%
               \fi}}}
      \pagina}
   \egroup}

%D \macros
%D   {combinepages}
%D
%D Yet another way of postprocessing is handles by \type
%D {\combinepages}. This macro builds a matrix of pages from a
%D file, for example:
%D
%D \starttypen
%D \setuppapersize
%D   [A4][A4] % or [A4,landscape][A4,landscape]
%D
%D \setuplayout
%D   [header=0pt,footer=1cm,
%D    backspace=1cm,topspace=1cm,
%D    width=middle,height=middle]
%D
%D \setupfootertexts
%D   [presentation---\currentdate\space---\space\pagenumber]
%D
%D \starttext
%D   \combinepages[slides][nx=2,ny=3,frame=on]
%D \stoptext
%D \starttypen
%D
%D One can influence the way the pages are combined. (This
%D will be explained some time.)

\def\combinepages
  {\dodoubleempty\docombinepages}

\def\docombinepages[#1][#2]% a=perpag b=free
  {\bgroup
   \mindermeldingen
   \getfiguredimensions[#1]%
   \getparameters
     [\??ip]
     [\c!variant=\v!a,
      \c!n=\noffigurepages,\c!nx=2,\c!ny=2,\c!start=1,\c!stop=\!!maxcard,
      \c!afstand=\bodyfontsize,
      \c!onder=\vfill,\c!boven=\vss,
      \c!links=\hss,\c!rechts=\hss,
      \c!voor=\pagina,\c!na=\pagina,\c!tussen=\blanko,
      \c!kader=,\c!achtergrond=,\c!achtergrondkleur=,
      #2]%
   \@@ipvoor
  %\doglobal\newcounter\combinedpagescounter
   \globallet\combinedpagescounter\@@ipstart
   \doifelse\@@ipvariant\v!b\!!doneafalse\!!doneatrue
   \if!!donea
     \doloop
       {\vbox to \teksthoogte
           {\hsize\tekstbreedte % ? ?
            \scratchdimen\@@ipafstand
            \!!widtha\hsize
            \advance\!!widtha  -\@@ipnx\scratchdimen
            \advance\!!widtha  \scratchdimen
            \divide \!!widtha  \@@ipnx
            \!!heighta\vsize
            \advance\!!heighta -\@@ipny\scratchdimen
            \advance\!!heighta         \scratchdimen
            \divide \!!heighta  \@@ipny
            \dorecurse\@@ipny
              {\hbox to \hsize
                 {\dorecurse\@@ipnx
                    {\vbox to \!!heighta
                       {\hsize\!!widtha
                        \vsize\!!heighta
                        \@@ipboven
                        \hbox to \hsize
                          {\@@iplinks
\ifnum\combinedpagescounter>\@@ipstop\relax
  \globallet\@@ipn\!!zerocount
\else
                           \ifnum\combinedpagescounter>\@@ipn \else
                             \externalfigure[#1]
                               [\c!object=\v!nee,
                                \c!pagina=\combinedpagescounter,
                                \c!factor=\v!max,
                                \c!achtergrond=\@@ipachtergrond,
                                \c!achtergrondkleur=\@@ipachtergrondkleur,
                                \c!kader=\@@ipkader]%
                           \fi
\fi
                           \@@iprechts}
                        \@@iponder}%
                     \doglobal\increment\combinedpagescounter
                     \hfil}%
                  \hfilneg}
               \vfil}%
           \vfilneg}%
        \ifnum\combinedpagescounter>\@@ipn \exitloop\fi}
   \else
     \doloop
       {\startbaselinecorrection
          \scratchdimen\@@ipafstand
          \!!widtha\hsize
          \advance\!!widtha -\@@ipnx\scratchdimen
          \advance\!!widtha         \scratchdimen
          \divide \!!widtha  \@@ipnx
          \hbox to \hsize
            {\dorecurse\@@ipnx
               {\doglobal\increment\combinedpagescounter
                \ifnum\combinedpagescounter>\@@ipn \else
                   \externalfigure[#1]
                     [\c!pagina=\combinedpagescounter,
                      \c!breedte=\!!widtha,
                      \c!achtergrond=\@@ipachtergrond,
                      \c!achtergrondkleur=\@@ipachtergrondkleur,
                      \c!kader=\@@ipkader]%
                \fi}}%
        \stopbaselinecorrection
        \ifnum\combinedpagescounter<\@@ipn\relax
          \@@iptussen
        \else
          \exitloop
        \fi}
   \fi
   \@@ipna
   \egroup}

%D \macros
%D   {setuppagecomment,startpagecomment}
%D
%D This command is not yet documented. Usage:
%D
%D \starttypen
%D \setuppagecomment[state=start,location=right]
%D
%D \startpagecomment
%D \input knuth
%D \stoppagecomment
%D \stoptypen

\def\setuppagecomment
  {\dosingleempty\dosetuppagecomment}

\def\dosetuppagecomment[#1]%
  {\getparameters[\??pc][#1]%
   \doifelse\@@pcstatus\v!start
     {\doifinsetelse\@@pcplaats{\v!onder,\v!boven}
        {\setuppapersize[\c!links=\hskip\@@pcoffset]%
         \adddimenmacro\papierhoogte\@@pcoffset\@@pcoffset\@@pcafstand\@@pchoogte\to\@@pcpaperheight
         \adddimenmacro\papierbreedte\@@pcoffset\@@pcoffset\to\@@pcpaperwidth
         \defineoverlay[pagecomment][\placepagecommentTB]}
        {\setuppapersize[\c!boven=\vskip\@@pcoffset]%
         \adddimenmacro\papierhoogte\@@pcoffset\@@pcoffset\to\@@pcpaperheight
         \adddimenmacro\papierbreedte\@@pcoffset\@@pcoffset\@@pcafstand\@@pcbreedte\to\@@pcpaperwidth
         \defineoverlay[pagecomment][\placepagecommentLR]}%
      \processaction
        [\@@pcplaats]
        [ \v!onder=>{\setuppapersize[\c!onder =\vss,\c!boven =\vskip\@@pcoffset]},
          \v!boven=>{\setuppapersize[\c!boven =\vss,\c!onder =\vskip\@@pcoffset]},
          \v!links=>{\setuppapersize[\c!links =\hss,\c!rechts=\hskip\@@pcoffset]},
         \v!rechts=>{\setuppapersize[\c!rechts=\hss,\c!links =\hskip\@@pcoffset]}]%
      \definepapersize
        [commentedpage]
        [\c!hoogte=\@@pcpaperheight,
         \c!breedte=\@@pcpaperwidth]%
      \let\@@pcprintpapersize\printpapersize
      \setuppapersize[\papersize][commentedpage]%
      \setupbackgrounds[\v!papier][\c!achtergrond=pagecomment]}
    {\doif\@@pcstatus\v!stop % else initialization  invokes backgrounds
       {% this should be tested first
        % \expanded{\setuppapersize[\papersize][\@@pcprintpapersize]}%
        \setupbackgrounds[\v!papier][\c!achtergrond=]}}}

\def\@@pcprintpapersize{\printpapersize}

\def\placepagecommentTB
  {\vbox to \printpapierhoogte
     {\forgetall
      \hsize\printpapierbreedte
      \vskip\@@pcoffset
      \doifelse\@@pcplaats\v!onder{\vskip\papierhoogte\vskip\@@pcafstand}\vss
      \hskip\@@pcoffset
      \vbox to \@@pchoogte
        {\forgetall
         \hsize\papierbreedte
         \ifpagecomment
           \haalbuffer[pagecomm]
           \global\pagecommentfalse
         \fi}%
      \hfill
      \doifelse\@@pcplaats\v!onder\vss{\vskip\papierhoogte\vskip\@@pcafstand}
      \vskip\@@pcoffset}}

\def\placepagecommentLR
  {\hbox to \printpapierbreedte
     {\hskip\@@pcoffset
      \doifelse\@@pcplaats\v!rechts{\hskip\papierbreedte\hskip\@@pcafstand}\hss
      \vbox to \printpapierhoogte
        {\forgetall
         \vskip\@@pcoffset
         \hsize\@@pcbreedte
         \ifpagecomment
           \haalbuffer[pagecomm]
           \global\pagecommentfalse
         \fi
         \vss}%
      \doifelse\@@pcplaats\v!rechts\hss{\hskip\papierbreedte\hskip\@@pcafstand}%
      \hskip\@@pcoffset}}

\newif\ifpagecomment

\setvalue{\e!start\e!pagecomment}%
  {\global\pagecommenttrue
   \dostartbuffer[pagecomm][\e!start\e!pagecomment][\e!stop\e!pagecomment]}

\setuppagecomment
  [\c!status=, % \v!stop would invoke background calculation
   \c!plaats=\v!onder,
   \c!offset=.5cm,
   \c!afstand=.5cm,
   \c!hoogte=5cm,
   \c!breedte=10cm]

% This macro cuts a page into n parts that can be pasted
% together.

\def\slicepages
  {\dotripleempty\doslicepages}

\def\doslicepages[#1][#2][#3]%
  {\ifthirdargument
     \dodoslicepages[#1][#2][#3]%
   \else
     \dodoslicepages[#1][#2][#2]%
   \fi}

\newcounter\slicedpagenumber

\def\dodoslicepages[#1][#2][#3]%
  {\bgroup
   \dontcomplain
   \globallet\slicedpagenumber\!!zerocount
   \getfiguredimensions[#1]
   \getparameters
     [\??ip]
     [\c!n=1,
      \c!offset=\!!zeropoint,
      \c!hoffset=\!!zeropoint,\c!voffset=\!!zeropoint,
      \c!breedte=\figurewidth,\c!hoogte=\figureheight,#2]
   \ifnum\@@ipn>\zerocount
     \definepapersize
       [\s!dummy][\c!hoogte=\@@iphoogte,\c!breedte=\@@ipbreedte]
     \setuppapersize
       [\s!dummy][\s!dummy]
     \setuplayout
       [\c!rugwit=\!!zeropoint,\c!kopwit=\!!zeropoint,
        \c!hoogte=\v!midden,\c!breedte=\v!midden,
        \c!tekstafstand=\!!zeropoint,
        \c!hoofd=\!!zeropoint,\c!voet=\!!zeropoint]
   \fi
   \dorecurse\noffigurepages
     {\global\let\slicedpagenumber\recurselevel
      \ifnum\@@ipn>\plusone
        \dorecurse\@@ipn
          {\let\xslice\recurselevel
           \dorecurse\@@ipn
             {\let\yslice\recurselevel
              \clip
                [\c!nx=\@@ipn,\c!ny=\@@ipn,\c!x=\xslice,\c!y=\yslice]
                {\schaal
                   [\c!schaal=\@@ipn000]
                   {\externalfigure[#1][\c!pagina=\slicedpagenumber]}}
              \pagina}}
      \else
        \ifodd\slicedpagenumber\relax
          \getparameters[\??ip][#2]
        \else
          \getparameters[\??ip][#3]
        \fi
        \hskip\@@ipoffset
        \clip
          [\c!hoffset=\@@iphoffset,\c!voffset=\@@ipvoffset,
           \c!hoogte=\@@iphoogte,\c!breedte=\@@ipbreedte]
          {\externalfigure[#1][\c!pagina=\slicedpagenumber]}
        \pagina
      \fi}
   \egroup}

% \starttext \slicepages[slice1.pdf][n=3] \stoptext

\protect \endinput
