%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

\mutable\lettonothing\currentversioninfo
\mutable\lettonothing\currentversiontext

\let\page_info_add_to_box\gobbleoneargument

\permanent\tolerant\protected\def\setupversion[#1]%
  {\expand\everyresetversion
   \ifhastok={#1}%
     \getdummyparameters[\c!alternative=,\c!text=,#1]%
     \edef\currentversioninfo{\dummyparameter\c!alternative}%
     \edef\currentversiontext{\dummyparameter\c!text}%
   \else
     \edef\currentversioninfo{#1}%
     \lettonothing\currentversiontext
   \fi
   \ifcsname\??layoutinfo\currentversioninfo\endcsname
     \let\page_info_add_to_box\page_info_add_to_box_indeed
   \else
     \lettonothing\currentversioninfo
     \let\page_info_add_to_box\gobbleoneargument
   \fi
   \expand\everysetupversion}

\aliased\let\version\setupversion

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

\permanent\protected\def\installversioninfo#1#2%
  {\defcsname\??layoutinfo#1\endcsname{#2}}

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

\installversioninfo\v!final
  {\lettonothing\currentversioninfo
   \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
      \ifcstok{\currentproject}{\currentproduct}%
        \ifcstok{\currentproduct}{\currentcomponent}%
          \donetrue
        \fi
      \fi
      \ifdone
        \donefalse
      \else
        \ifempty\currentproject   \else \space\v!project  :\space\currentproject  \space \donetrue \fi
        \ifempty\currentproduct   \else \space\v!product  :\space\currentproduct  \space \donetrue \fi
        \ifempty\currentcomponent \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{\scratchdimen-2\texthoffset}%
      \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
