%D \module
%D   [       file=trac-vis, % replaces supp-vis plus some s-* modules
%D        version=2112.06.23, % 1996.10.21,
%D          title=\CONTEXT\ Tracing Macros,
%D       subtitle=Visualization,
%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.

%D The old visual debugger dates from 1996 and started out as joke. In practice
%D it's not used that often although the \type {\ruledhbox} cum suis macros come
%D in handy when writing code. In the process of luafication some additional
%D tracing options were tested, for instance showing font kerns. As part of the
%D \MKIV\ cleanup the decision was made to reimplement, reintegrate and reconsider
%D all these features. The old code is gone and the new code will get extended
%D when needed. We still provide placeholders for some old visualization commands
%D but they might go away.
%D
%D Control over what gets visualized happens with the tracker command:
%D
%D \enabletrackers[visualizer.*]
%D
%D Possible values are: \type {fontkern}, \type {kern}, \type {glue}, \type
%D {penalty}, \type {hbox}, \type {vbox}, \type {all}, \type {reset}, \type
%D {makeup}, \type {whatsit}, \type{glyph}.
%D
%D In due time some special visualzation of math will move here as well.

\writestatus{loading}{ConTeXt Tracing Macros / Visualization}

\registerctxluafile{trac-vis}{1.001}

\unprotect

\newconstant\c_syst_visualizers_state
\newtoks    \t_syst_visualizers_optimize

\definesystemattribute[visual][public,global] % global ?

% no, but can become an option:
%
% \appendtoks
%     \attribute\visualattribute\attributeunsetvalue
% \to \everybeforepagebody

%D We only provide box visualizers as they can come in handy for testing
%D macros. In due time we will move some of the m-visual code here too.

\let\syst_visualizers_hbox\hbox
\let\syst_visualizers_vbox\vbox
\let\syst_visualizers_vtop\vtop

\unexpanded\def\ruledhbox{\syst_visualizers_hbox attr \visualattribute \ctxcommand{visual("simplehbox")} }
\unexpanded\def\ruledvbox{\syst_visualizers_vbox attr \visualattribute \ctxcommand{visual("simplevbox")} }
\unexpanded\def\ruledvtop{\syst_visualizers_vtop attr \visualattribute \ctxcommand{visual("simplevtop")} } % special case
\unexpanded\def\ruledtopv{\syst_visualizers_vtop attr \visualattribute \ctxcommand{visual("vtop")} }

\appendtoks
    \ifcase\c_syst_visualizers_state\else
        \syst_visualizers_speedup
    \fi
\to \everyshipout

\appendtoks
    \global\let\syst_visualizers_speedup\relax
\to \t_syst_visualizers_optimize

\def\syst_visualizers_speedup{\the\t_syst_visualizers_optimize}

\appendtoks
    \normalexpanded {%
        \unexpanded\xdef\ruledhbox\expandafter{\ruledhbox}%
        \unexpanded\xdef\ruledvbox\expandafter{\ruledvbox}%
        \unexpanded\xdef\ruledvtop\expandafter{\ruledvtop}%
        \unexpanded\xdef\ruledtopv\expandafter{\ruledtopv}%
    }%
\to \t_syst_visualizers_optimize

\unexpanded\def\showmakeup
  {\ctxcommand{setvisual("makeup")}%
   \let\normalvtop\ruledtopv
   \let\vtop      \ruledtopv}

\unexpanded\def\showallmakeup
  {\ctxcommand{setvisual("all")}%
   \let\normalvtop\ruledtopv
   \let\vtop      \ruledtopv
   \showstruts}

\unexpanded\def\showboxes
  {\ctxcommand{setvisual("boxes")}%
   \let\normalvtop\ruledtopv
   \let\vtop      \ruledtopv}

\unexpanded\def\showglyphs
  {\ctxcommand{setvisual("glyph")}}

\unexpanded\def\showfontkerns
  {\ctxcommand{setvisual("fontkern")}}

\unexpanded\def\setvisualizerfont
  {\dosingleempty\syst_visualizers_setfont}

\def\syst_visualizers_setfont[#1]% somename at 4pt
  {\begingroup
   \doifelsenothing{#1}{\definedfont[Mono at 4pt]}{\definedfont[#1]}%
   \ctxlua{nodes.visualizers.setfont()}%
   \endgroup}

%D New (these might also be a visualizers):

\definecolor[f:r:t][a=1,t=.25,r=1]
\definecolor[f:g:t][a=1,t=.25,g=1]
\definecolor[f:b:t][a=1,t=.25,b=1]
\definecolor[f:c:t][a=1,t=.25,c=1]
\definecolor[f:m:t][a=1,t=.25,m=1]
\definecolor[f:y:t][a=1,t=.25,y=1]
\definecolor[f:k:t][a=1,t=.25,s=0]

% \def\node_backgrounds_boxes_add#1[#2]%
%   {\node_backgrounds_boxes_initialize
%    #1\backgroundcolorattr{#2}}

\def\syst_visualizers_filled_hbox#1%
  {\node_backgrounds_boxes_initialize\hbox\backgroundcolorattr{#1}}

\unexpanded\def\filledhboxr{\syst_visualizers_filled_hbox{f:r:t}}
\unexpanded\def\filledhboxg{\syst_visualizers_filled_hbox{f:g:t}}
\unexpanded\def\filledhboxb{\syst_visualizers_filled_hbox{f:b:t}}
\unexpanded\def\filledhboxc{\syst_visualizers_filled_hbox{f:c:t}}
\unexpanded\def\filledhboxm{\syst_visualizers_filled_hbox{f:m:t}}
\unexpanded\def\filledhboxy{\syst_visualizers_filled_hbox{f:y:t}}
\unexpanded\def\filledhboxk{\syst_visualizers_filled_hbox{f:k:t}}

%D Overload:

% \def\spac_struts_vide_hbox
%   {\hbox attr \visualattribute \ctxcommand{visual("strut")} }
%
% \def\spac_struts_vide_hbox
%   {\xdef\spac_struts_vide_hbox{\hbox attr \visualattribute \ctxcommand{visual("strut")} }%
%    \spac_struts_vide_hbox}

\unexpanded\def\spac_struts_vide_hbox
  {\hbox attr \visualattribute \ctxcommand{visual("strut")} }

\appendtoks
    \normalexpanded{\unexpanded\xdef\spac_struts_vide_hbox\expandafter{\spac_struts_vide_hbox}}%
\to \t_syst_visualizers_optimize

%D For manuals:

\unexpanded\def\markfonts#1%
  {\dontleavehmode
   \begingroup
   \setbox\scratchbox\hbox{\getbuffer}%
   \ctxcommand{markfonts(\number\scratchbox)}%
   \unhbox\scratchbox
   \endgroup}

%D We keep this one:

\unexpanded\def\dontinterfere
  {\reseteverypar
   \parindent\zeropoint
   \parskip  \zeropoint
   \leftskip \zeropoint
   \rightskip\zeropoint
   \relax}

%D We keep these for a while:

\let\ruledvcenter   \vcenter
\let\ruledhss       \hss
\let\ruledhfil      \hfil
\let\ruledhfill     \hfill
\let\ruledhfilll    \hfilll
\let\ruledhfilneg   \hfilneg
\let\ruledhfillneg  \hfillneg
\let\ruledhfilllneg \hfilllneg
\let\ruledvss       \vss
\let\ruledvfil      \vfil
\let\ruledvfill     \vfill
\let\ruledvfilll    \vfilll
\let\ruledvfilneg   \vfilneg
\let\ruledvfillneg  \vfillneg
\let\ruledvfilllneg \vfilllneg
\let\ruledhskip     \hskip
\let\ruledvskip     \vskip
\let\ruledkern      \kern
\let\ruledhglue     \hglue
\let\ruledvglue     \vglue
\let\ruledmkern     \mkern
\let\ruledmskip     \mskip
\let\ruledpenalty   \penalty

\protect \endinput
