%D \module
%D   [       file=pack-bck, % moved from  pack-rul,
%D        version=20111115, % 1998.10.16,
%D          title=\CONTEXT\ Packaging Macros,
%D       subtitle=Simple Backgrounds,
%D         author=Hans Hagen \& Wolfgang Schuster,
%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 Packaging Macros / Simple Backgrounds}

%D The code here is already pretty old and is used for simple
%D backgrounds. As it is still used WS adapted the code to the
%D new command handler methods so that it gets a second life. So
%D it made sense to mkvi ir as well. Some more code is moved here
%D too.

%D \macros
%D   {setupbackground,startbackground,background}
%D
%D The section deals with backgrounds in the running text. This
%D means that texts is to be collected and split over pages. To
%D show what can be done, we provide this part of the
%D documentation with some gray background and a red frame.
%D Both the background and frame can have all characteristics
%D of \type{\framed}.
%D
%D \starttyping
%D \setupbackground
%D   [backgroundoffset=4pt,
%D    background=color,
%D    frame=on,
%D    framecolor=red,
%D    leftoffset=2pt]
%D \stoptyping
%D
%D The implementation is not that sophisticated, but suffices.
%D The main problem with this kind of functionality is to get
%D the spacing all right.
%D
%D Specifying the background is more or less the same as
%D specifying a framed box.
%D
%D \showsetup{setupbackground}
%D
%D {\em This mechanism is sort of obsolete and textbackgrounds should
%D be used instead.}

\unprotect

\installcorenamespace {background}

\installframedcommandhandler \??background {background} \??background

% The mode is not that public an dmostlu an initial mode:
%
% 0 : no split
% 1 : no split, honoring leftskip (messy)
% 2 : split
% 3 : split, honoring leftskip (messy)
%
% While processing the modes will change from 3->1 or 2->0. Don't
% depend on the mode being public currently as it could become a
% key.

\setnewconstant\backgroundsplitmode\plusthree

\appendtoks
    \setuevalue{\e!start\currentbackground}{\pack_backgrounds_start [\currentbackground]}%
    \setuevalue{\e!stop \currentbackground}{\pack_backgrounds_stop                      }%
    \setuevalue        {\currentbackground}{\pack_backgrounds_direct[\currentbackground]}%
\to \everydefinebackground

\unexpanded\def\pack_backgrounds_start[#tag]%
  {\endgraf
   \begingroup
   \def\currentbackground{#tag}%
   \doifelse{\backgroundparameter\c!state}\v!start
     {\dosingleempty\pack_backgrounds_start_indeed}
     {\dosingleempty\pack_backgrounds_start_ignore}}

\def\pack_backgrounds_start_indeed[#settings]%
  {\setupcurrentbackground[#settings,\c!state=\v!start,\c!offset=\v!overlay]%
   \let\pack_backgrounds_stop\pack_backgrounds_stop_indeed
   \setbox0\vbox\bgroup
     \vbox to \lineheight{}\vskip\zeropoint
     \blank[\v!disable]
     \leftskip \backgroundparameter\c!leftoffset
     \rightskip\backgroundparameter\c!rightoffset}

\def\pack_backgrounds_start_ignore[#settings]%
  {\let\pack_backgrounds_stop\pack_backgrounds_stop_ignore}

\let\pack_backgrounds_stop\relax

\unexpanded\def\pack_backgrounds_stop_indeed % we shoul duse the fast background variant of framed
  {\endgraf
   \removelastskip
   \egroup
   \dimen2\leftskip % new **
   \forgetall
   \ifinsidefloat
     \backgroundsplitmode\zerocount
   \fi
   \ifcase\backgroundsplitmode
     \inheritedbackgroundframed{\box0}%
   \or
     \hskip\dimen2
     \inheritedbackgroundframed{\box0}%
   \else
     \splitmaxdepth\boxmaxdepth
     \splittopskip\topskip
     \setbox2\vsplit0 to \lineheight % get rid of fake line
     \doloop
       {\ifdim\pagetotal=\zeropoint % empty page
          \scratchdimen\textheight
          \backgroundsplitmode\plusone % split to max height
        \else
          \setbox2\vbox{\backgroundparameter\c!before}%
          \scratchdimen\dimexpr\pagegoal-\ht2-\pagetotal\relax
          \backgroundsplitmode\plustwo % split to partial height
        \fi
        \advance\scratchdimen\dimexpr-\backgroundparameter\c!topoffset-\backgroundparameter\c!bottomoffset\relax
        \ifdim\scratchdimen>2\lineheight\relax % reasonable, will be configurable
          \ifdim\ht0>\scratchdimen % larger than page
            \setbox2\vsplit0 to \scratchdimen
          \else
            \setbox2\box0
            \backgroundsplitmode\zerocount % no split
          \fi
          \setbox2\vbox \ifcase\backgroundsplitmode\or to \textheight \fi % max split
            {\vskip\backgroundparameter\c!topoffset
             \unvcopy2
             \prevdepth\dp2
             \obeydepth
             \vskip\backgroundparameter\c!bottomoffset
             \vfill}
          \backgroundparameter\c!before
          \ifcase\backgroundsplitmode\or\or % partial split
            \ifdim\pagegoal<\maxdimen
              \pagegoal=1.2\pagegoal % be a bit more tolerant
            \fi
          \fi
          \startlinecorrection
            \ifnum\backgroundsplitmode=\plusthree
              \hskip\dimen2 % new **
            \fi
            \inheritedbackgroundframed{\box2}%
          \stoplinecorrection
          \ifcase\backgroundsplitmode % no split
            \backgroundparameter\c!after
          \else % some split
            \page_otr_fill_and_eject_page % no \page !
          \fi
        \else
          \page
        \fi
        \ifdim\ht0>\zeropoint \else
          \exitloop
        \fi}%
   \fi
   \endgroup
   \endgraf}

\unexpanded\def\pack_backgrounds_stop_ignore
  {\endgroup
   \endgraf}

\unexpanded\def\pack_backgrounds_direct[#tag]%
  {\begingroup
   \def\currentbackground{#tag}%
   \doifelse{\backgroundparameter\c!state}\c!start
     \pack_backgrounds_direct_indeed
     \pack_backgrounds_direct_ignore}

\def\pack_backgrounds_direct_indeed
  {\dowithnextboxcs\pack_backgrounds_direct_finish\vbox}

\def\pack_backgrounds_direct_finish
  {\setupcurrentbackground[\c!offset=\v!overlay]%
   \inheritedbackgroundframed{\flushnextbox}%
   \endgroup}

\def\pack_backgrounds_direct_ignore
  {\endgroup}

\definebackground[\v!background]

\ifdefined\startbackground \else
    \expandafter\let\startbackground\csname\e!start\v!background\endcsname
    \expandafter\let\stopbackground \csname\e!stop \v!background\endcsname
    \expandafter\let\background     \csname        \v!background\endcsname
\fi

\setupbackground
  [\c!leftoffset=.5\bodyfontsize,
   \c!rightoffset=\backgroundparameter\c!leftoffset,
   \c!topoffset=\zeropoint,
   \c!bottomoffset=\backgroundparameter\c!topoffset,
   \c!state=\v!start,
   \c!radius=.5\bodyfontsize,
   \c!corner=\v!rectangular,
   \c!frame=\v!off,
   \c!depth=\zeropoint,
 % \c!color=,
   \c!background=\v!color,
   \c!backgroundcolor=lightgray,
   \c!before=,
   \c!after=]

%D \startbuffer
%D \setupbackground[state=start,frame=on]
%D
%D \startbackground
%D \input ward
%D \stopbackground
%D
%D \background{\input ward\par}
%D
%D \setupbackground[state=stop]
%D
%D \startbackground
%D \input ward
%D \stopbackground
%D
%D \background{\input ward\par}
%D \stopbuffer
%D
%D \typebuffer \getbuffer

%D \macros
%D   {backgroundline}
%D
%D For the moment an undocumented feature, but a candidate for going public.

\unexpanded\def\backgroundline[#color]%
  {\dontleavehmode
   \dowithnextbox{\pack_backgrounds_add_to_nextbox{#color}}\hbox}

\unexpanded\def\pack_backgrounds_add_to_nextbox#color% handy helper
  {\hbox
     {\dousecolorparameter{#color}%
      \vrule
        \!!width \nextboxwd
        \!!height\nextboxht
        \!!depth \nextboxdp
      \hskip-\nextboxwd
      \flushnextbox}}

\protect \endinput
