%D \module
%D   [       file=trac-brk,
%D        version=2023.05.23,
%D          title=\CONTEXT\ Tracing Macros,
%D       subtitle=Linebreaks,
%D         author=Hans Hagen & Mikael Sundqvist,
%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.

% these will become runtime commands

% \ifdefined\showbreakpoints \endinput \fi

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

\registerctxluafile{trac-brk}{}

% \linebreakchecks : currently a bitset but maybe just values
%
% 1 : trace
% 2 : twins

% option (list of):
%
% margin
% simple   (only badness)
% compact  (no ref counts)

%D We only support one at a time.

\unprotect

\newcount\c_line_break_check
\newcount\c_balance_check

\gdef\trac_start_show_breakpoints#1#2#3%
  {\begingroup
   \getdummyparameters[\c!option=,\c!list=,\c!offset=\zeropoint,\c!level=0,#1]%
   \ctxlua{tracers.breakpoints.start {
        option = "\dummyparameter\c!option",
        level  = "\dummyparameter\c!level",
        list   = "\dummyparameter\c!list",
        offset = \number\dimexpr\dummyparameter\c!offset\relax,
    }}%
   \endgroup
   #2#3%
   #3\plusone}

\gdef\trac_stop_show_breakpoints#1#2%
  {\par
   #2#1%
   \ctxlua{tracers.breakpoints.stop()}}

\gdef\trac_show_breakpoints#1%
  {\begingroup
   \getdummyparameters[\c!option=,#1]%
   \switchtobodyfont[6pt]%
   \dorecurse{\cldcontext{tracers.breakpoints.nofresults()}} {
     \startcolumns[\c!n=3,\c!notes=\v!no,#1,\c!option=]%
       \ctxlua{tracers.breakpoints.typesetresult(##1,"\dummyparameter\c!option")}%
     \stopcolumns
   }%
  %\ctxlua{tracers.breakpoints.reset()}%
   \endgroup}

\gdef\trac_draw_breakpoints#1%
  {\begingroup
   \getdummyparameters[%
     \c!dx=3fs,%
     \c!dy=2fs,%
     \c!sx=1.5fs,%
     \c!sy=1.125fs,%
     \c!rulethickness=0.15fs,%
     #1]%
   \startMPcode
        lua.mp.show_breakpoints(
            \todimension{\dummyparameter\c!dx},%
            \todimension{\dummyparameter\c!dy},%
            \todimension{\dummyparameter\c!sx},%
            \todimension{\dummyparameter\c!sy},%
            \todimension{\dummyparameter\c!rulethickness}%
        ) ;
   \stopMPcode
   \endgroup}

% \gdef\trac_reset_breakpoints
%   {\ctxlua{tracers.breakpoints.reset()}}

\permanent\tolerant\protected\gdef\startshowbreakpoints[#1]%
  {\trac_start_show_breakpoints{#1}\c_line_break_check\linebreakchecks}

\permanent\protected\gdef\stopshowbreakpoints
  {\trac_stop_show_breakpoints\c_line_break_check\linebreakchecks}

\permanent\tolerant\protected\gdef\showbreakpoints[#1]%
  {\trac_show_breakpoints{#1}}

\permanent\tolerant\protected\gdef\drawbreakpoints[#1]%
  {\trac_draw_breakpoints{#1}}

% \permanent\protected\gdef\resetbreakpoints
%   {\trac_reset_breakpoints}

% Also here:

\permanent\tolerant\protected\gdef\startshowbalancepoints[#1]%
  {\trac_start_show_breakpoints{#1,\c!level=1}\c_balance_check\balancechecks}

\permanent\protected\gdef\stopshowbalancepoints
  {\trac_stop_show_breakpoints\c_balance_check\balancechecks}

\permanent\tolerant\protected\gdef\showbalancepoints[#1]%
  {\trac_show_breakpoints{#1}}

\permanent\tolerant\protected\gdef\drawbalancepoints[#1]%
  {\trac_draw_breakpoints{#1}}

% \permanent\protected\gdef\resetbalancepoints
%   {\trac_reset_breakpoints}

\protect \endinput
