%:skip
% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,  
% EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF  
% MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND  
% NONINFRINGEMENT. IN NO EVENT SHALL SIL INTERNATIONAL BE LIABLE FOR  
% ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF  
% CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION  
% WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
%
% Except as contained in this notice, the name of SIL International  
% shall not be used in advertising or otherwise to promote the sale,  
% use or other dealings in this Software without prior written  
% authorization from SIL International.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%+tracing-codes
%:
% There is nothing special that says the tracing code has to be a single character.
% It is also better to have more identifiers than less to allow for more targetted tracing.
% Trace identifiers allocated in ptx macros:
%
% | Identifier | Trace
% |------------|------
% | a | all traces
% | A | Attributes 
% | b | balancing (info for external page break analysis)
% | B | underfull pages
% | C | cutouts
% | c | Chapters 
% | ch | Active characters
% | cov | covers
% | d | diglot construction
% | dP | diglot Page use
% | D | Diglot gory details
% | De | Diglot repetative commands (each@col)
% | Df | Diglot footnote separation
% | Dh | Diglot availht extra details
% | Ds | Diglot stylesheet
% | dm | Diglot module
% | dmp | Diglot module: polyglot
% | e | Extended(sidebar)
% | eg | Sidebar gridding 
% | eb | Extended borders
% | ebi | Border style inheritance
% | eh | sidebar-headings
% | et | textborders
% | f | footnotes (output)
% | fp | footnote reparagraphing
% | F | fonts
% | Fe | font effects
% | g | figures
% | G | Shipping pages
% | gI | figure inserts - tracking saving / restoring of inserts
% | h | headings
% | hv | hanging verses
% | H | Headers, marks, etc
% | i | inserts (tracking the value of holdinginserts)
% | I | ifchecks
% | j | paragraph adjustments
% | m | milestones
% | mx | milestones: expand content
% | M | markers
% | ma | mark accounting
% | n | notes creation/save/restore
% | nS | note saving / restoring
% | o | output routines / rebalancing
% | oh | other(not stylesheet) hooks
% | orn | ornaments
% | p | page output
% | pt | Page tabs
% | P | Piclists
% | per | periph handling
% | s | stylesheet
% | sa | stylesheet additional debugging
% | sb | stylesheet boxed character styles
% | spv | stylesheet parameter lookups that return void
% | ss | style start/stop
% | sP | Stylesheet everypar/chapter 
% | sc | Stylesheet category
% | sC | Stylesheet Cache
% | sh | Stylesheet-related hooks
% | sk| Style stacK  and option stack operations
% | sko |  Style stack option-list building
% | S | side-dependent swapping 
% | t | Tables and table of contents
% | tim | Timing
% | T | Triggers
% | u | 'Unprintable' pages (e.g. triggered images  don't fit)
% | V | verse numbers
% | v | marginal verses
% | x | Processes run at exit
%
% Magic command to build tracelist. from above. in vim, go to colon in the next line and type "ay$@a
% :/| *a *|/+1,.! awk -F\| '(NF==3){b=$2;gsub("[ \t]","",b);A=A b ","}{print}END{print "%\n\\def\\tracelist{" A "}\n"}'
\def\tracelist{A,b,B,C,c,ch,cov,d,dP,D,De,Df,Dh,Ds,dm,dmp,dmc,e,eg,eb,ebi,eh,et,f,fp,F,Fe,g,G,gI,h,hv,H,i,imt,I,j,m,mx,M,ma,n,nS,o,oh,orn,p,pt,P,per,s,sa,sb,spv,ss,sP,sc,sC,sh,sk,sko,S,t,tim,T,u,V,v,x,}

%-tracing-codes

%+tracing
% Now modified so that multiple \tracing{} commands can be issued.
\newcount\TRACEcount %Numbered tracing for is useful for diglot. May be for something else too.
\def\y@s{y}
\def\tracing#1{\x@\xdef\csname tr@cemode-#1\endcsname{\y@s}\let\trace=\tr@cerfast \x@\let\csname tr@ce-#1\endcsname\tr@ceout}%
\def\untracing#1{\x@\xdef\csname tr@cemode-#1\endcsname{}\x@\let\csname tr@ce-#1\endcsname\notr@cer}%
\def\notr@cer#1#2{\relax}%
\untracing{a} % Fallback is nothing happens

%Make registered tracing codes call tr@ce-a by default 
\def\RegisterTrace#1{\def\tmpa{a}\def\tmp{#1}\ifx\tmp\tmpa\else\message{#1}\x@\def\csname tr@ce-#1\endcsname{\csname tr@ce-a\endcsname}\fi}
\def\trdefaults#1,#2\E{\RegisterTrace{#1}\def\tmp{#2}\ifx\tmp\empty\else \trdefaults#2\E\fi}
\message{Tracing routines registered for:}\x@\trdefaults\tracelist\E
\def\FastTracing{\let\trace\tr@cerfast}
\def\AllowAllTracing{\let\trace\tr@cer}

\addtoendhooks{\ifx\trace\tr@cer\ifx\tracemoan\empty\message{No unregistered trace flags. \string\FastTracing\space can be used}\else\message{*** Unregistered trace flags: \tracemoan. Don't use \string\FastTracing}\fi\fi}

\def\tracemoan{}
%\def\tr@cer#1#2{%
%    \ifcsname tr@ce-#1\endcsname\csname tr@ce-#1\endcsname{#1}{#2}\else\csname tr@ce-a\endcsname{#1}{#2}\fi}
%\def\tr@cer#1#2{%
    %\ifcsname tr@ce-#1\endcsname\csname tr@ce-#1\endcsname{#1}{#2}\else\edef\tracemoan{\tracemoan,#1}\csname tr@ce-a\endcsname{#1}{#2}\fi}
\def\tr@cer#1{%
    \ifcsname tr@ce-#1\endcsname \x@\let\x@\tr@cetmp\csname tr@ce-#1\endcsname \else \edef\tracemoan{\tracemoan,#1}\x@\let\x@\tr@cetmp\csname tr@ce-a\endcsname\x@\let\csname tr@ce-#1\endcsname\tr@cetmp\fi \tr@cetmp{#1}}
\def\tr@cerfast#1{\csname tr@ce-#1\endcsname{#1}} % Faster, but no fallback

\def\tr@ceout#1#2{%
    \immediate\write-1{#1> #2}}%
\def\traceNum#1#2{\global\advance\TRACEcount by 1\trace{#1}{+\the\TRACEcount: #2}}
\def\notr@ceout#1#2{\relax}%
\let\trace=\notr@cer
\def\IfTr@ce#1#2{\x@\ifx\csname tr@cemode-#1\endcsname\y@s #2\fi}
%-tracing

%+tracing-support
\def\pagetracing{0}
\def\tc@mmands{commands,pages,output,macros,paragraphs,ifs,restores}
\def\s@vens@ttrace#1{\x@\xdef\csname tracing-#1\endcsname{\csname #1\endcsname}\csname #1\endcsname=1}
\def\r@storetrace#1{\csname #1\endcsname=\csname tracing-#1\endcsname}
\def\alltrace#1{\ifnum#1>0\let\wh@t=\s@vens@ttrace\else\let\wh@t=\r@storetrace\fi\x@\pr@cessSp@cific \tc@mmands,\E}

\def\traceifset#1{\trace{I}{Ifset: #1=\the\currentiflevel}\x@\xdef\csname checkif@#1\endcsname{\the\currentiflevel}}
\def\traceifcheck#1{\xdef\tr@ceiflevel{\the\currentiflevel}\trace{I}{Ifcheck: #1 \tr@ceiflevel==\csname checkif@#1\endcsname}\x@\ifnum\csname checkif@#1\endcsname=\tr@ceiflevel\else
    \message{IF CHECK #1 Failed. Entered at \csname checkif@#1\endcsname \space != exit at \tr@ceiflevel}\fi}
% verbose tracing on/off macros to help with fault isolation
\def\tracetimset#1{\x@\ifx\csname tr@cemode-tim\endcsname\y@s \x@\xdef\csname @t-#1@\endcsname{\the\elapsedtime}\fi}
\def\tracetimcheck#1#2{\x@\ifx\csname tr@cemode-tim\endcsname\y@s \relax\edef\tim@check{\the\numexpr \elapsedtime - \csname @t-#1@\endcsname \relax}\tr@ceout{tim}{[#1] #2 \tim@check}\fi}

\def\ztmon{\tracingassigns=1 \tracingmacros=1}
\def\ztmoff{\tracingassigns=0 \tracingmacros=0}
\def\ztvalon{\tracinggroups=1 \tracingassigns=1 \tracingrestores=1}
\def\ztvaloff{\tracinggroups=0 \tracingassigns=0 \tracingrestores=0}
%-tracing-support
