%D \module
%D   [       file=core-ins,
%D        version=2002.04.16,
%D          title=\CONTEXT\ Insertion Macros,
%D       subtitle=Insertions,
%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 Core Macros / Insertions}

%D Insertions are special data collections that are associated
%D to \TEX's internal page builder. When multiple footnote
%D classes were introduced, I decided to isolate some of the
%D functionality in a module.

\unprotect

\newtoks\@@insertionlist

\def\processinsertions{\the\@@insertionlist}

\let\doprocessinsert\gobbleoneargument

%\def\installinsertion#1%
%  {\ifx#1\undefined
%     \newinsert#1%
%     \count#1\plusthousand
%     \skip #1\zeropoint
%     \dimen#1\maxdimen
%     \appendtoks\doprocessinsert#1\to\@@insertionlist
%   \fi}

\def\installinsertion#1%
  {\ifx#1\undefined
     \let#1\relax
   \fi
   \ifx#1\relax % permits \csname...\endcsname
     \newinsert#1%
     \count#1\plusthousand
     \skip #1\zeropoint
     \dimen#1\maxdimen
     \appendtoks\doprocessinsert#1\to\@@insertionlist
   \fi}


\def\synchronizeinsertions
  {\def\doprocessinsert##1{\ifvoid##1\else\insert##1{\unvbox##1}\fi}%
   \processinsertions}

%D For instance, when we postpone footnotes, we need to save
%D some data related to the inserts. The next methods are
%D far from ideal, but better than nothing. We save and
%D restore box content and associated data independently.
%D The box content is only restores when non||void.

\def\backupinsertion#1%
  {\csname\string#1\endcsname}

\def\installbackupinsertion#1%
  {\expandafter\newinsert\csname\string#1\endcsname
   \count\backupinsertion#1\zerocount
   \skip \backupinsertion#1\zeropoint
   \dimen\backupinsertion#1\maxdimen}

\def\saveinsertionbox#1%
  {\ifdim\ht#1>\zeropoint % hm, actually unknown
     \global\setbox\backupinsertion#1\box#1%
   \else
     \global\setbox\backupinsertion#1\emptybox
   \fi}

\def\restoreinsertionbox#1%
  {\ifvoid\backupinsertion#1\else % if void, we keep the content
     \global\setbox#1\box\backupinsertion#1%
   \fi}

\def\eraseinsertionbackup#1%
  {\global\setbox\backupinsertion#1\emptybox}

\def\saveinsertiondata#1%
  {\global\skip \backupinsertion#1\skip #1%
   \global\count\backupinsertion#1\count#1%
   \global\dimen\backupinsertion#1\dimen#1}

\def\restoreinsertiondata#1%
  {\global\skip #1\skip \backupinsertion#1%
   \global\count#1\count\backupinsertion#1%
   \global\dimen#1\dimen\backupinsertion#1}

%D Auxiliary macros:

\def\addinsertionheight#1\to#2%
  {\ifvoid#1\else
     \advance#2 1\skip#1\relax
     \advance#2  \ht  #1\relax
   \fi}

\protect \endinput
