%D \module
%D   [       file=page-inf,   % moved from page-ini
%D        version=2011.12.07, % 2000.10.20,
%D          title=\CONTEXT\ Page Macros,
%D       subtitle=Tracing Info,
%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 / Tracing Info}

% This needs a real cleanup .. will be a more detailed list.

\unprotect

\installcorenamespace{layoutinfo}

\newtoks\everyresetversion
\newtoks\everysetupversion

\let\currentversioninfo\empty
\let\currentversiontext\empty

\unexpanded\def\setupversion
  {\dosingleargument\page_info_setup}

\let\version\setupversion

\let\page_info_add_to_box\gobbleoneargument

\def\page_info_setup[#1]%
  {\the\everyresetversion
   \doifelseassignment{#1}%
     {\getdummyparameters[\c!alternative=,\c!text=,#1]%
      \edef\currentversioninfo{\dummyparameter\c!alternative}%
      \edef\currentversiontext{\dummyparameter\c!text}}%
     {\edef\currentversioninfo{#1}%
      \let \currentversiontext\empty}%
   \ifcsname\??layoutinfo\currentversioninfo\endcsname
     \let\page_info_add_to_box\page_info_add_to_box_indeed
   \else
     \let\currentversioninfo\empty
     \let\page_info_add_to_box\gobbleoneargument
   \fi
   \the\everysetupversion}

\unexpanded\def\page_info_place_info % at the bottom of the page
  {\begincsname\??layoutinfo\currentversioninfo\endcsname}

\unexpanded\def\installversioninfo#1#2%
  {\setvalue{\??layoutinfo#1}{#2}}

\unexpanded\def\includeversioninfo#1%
  {\begincsname\??layoutinfo#1\endcsname}

\installversioninfo\v!final
  {\let\currentversioninfo\empty
   \let\page_info_add_to_box\gobbleoneargument}

\installversioninfo\v!concept
  {\vskip6\points
   \hbox to \makeupwidth
     {\infofont
      \rlap{\v!concept:\space\currentdate}%
      \hss
      \currentversiontext
      \hss
      \llap{\page_adapts_status_info}}}

\installversioninfo\v!file
  {\vskip6\points
   \hbox to \makeupwidth
     {\infofont
      \getmessage\m!system{27}:\space\currentdate\quad
      \donefalse
      \doif\currentproject\currentproduct{\doif\currentproduct\currentcomponent\donetrue}%
      \ifdone
        \donefalse
      \else
        \ifx\currentproject  \empty \else \space\v!project  :\space\currentproject  \space \donetrue \fi
        \ifx\currentproduct  \empty \else \space\v!product  :\space\currentproduct  \space \donetrue \fi
        \ifx\currentcomponent\empty \else \space\v!component:\space\currentcomponent\space \donetrue \fi
      \fi
        \ifdone                     \else \space\v!file     :\space\jobname         \space           \fi
      \hss
      \currentversiontext
      \quad
      \page_adapts_status_info}}

\installversioninfo\v!temporary
  {\includeversioninfo\v!file}

\appendtoks
    \overfullrule\zeropoint
\to \everyresetversion

\appendtoks
    \ifx\currentversioninfo\v!temporary
        \overfullrule5\points
    \fi
\to \everysetupversion

\newbox\b_page_versions

\def\page_info_add_to_box_indeed#1%
  {\scratchdimen\wd#1%
   \setbox\b_page_versions\vpack to \ht#1%
     {\vfill
      \settexthoffset
      \hsize\dimexpr\scratchdimen-2\texthoffset\relax
      \hkern\texthoffset % brrrr
      \vbox to \zeropoint{\vss\page_info_place_info}%
      \vkern\bodyfontsize}%
   \dp\b_page_versions\zeropoint
   \wd\b_page_versions\scratchdimen
   \setbox#1\hpack{\box#1\hkern-\scratchdimen\box\b_page_versions}}

\setupversion % resets
  [\v!final]

\let\page_info_add_to_box\gobbleoneargument

\protect \endinput
