% $Id$
% Copyright 2016 TeX Users Group
% Copyright 1990-1999 Eitan M. Gurari          
%                                                      
% This program can redistributed and/or modified under 
% the terms of the LaTeX Project Public License        
% Distributed from CTAN archives in directory          
% macros/latex/base/lppl.txt; either version 1 of the  
% License, or (at your option) any later version.      
%                                                      
% This work has the LPPL maintenance status "maintained".
%
% The Current Maintainer of this work
% is the TeX4ht Project <http://tug.org/tex4ht>.
% 
% If you modify this program, changing the 
% version identification would be appreciated.
\immediate\write-1{version 2016-07-04}

\expandafter\edef\csname :RestoreCatcodes\endcsname{%
   \catcode`\noexpand :=\the\catcode`:%
   \catcode`\noexpand @=\the\catcode`@%
   \catcode`\noexpand -=\the\catcode`-%
   \catcode`\noexpand +=\the\catcode`+%
   \catcode`\noexpand ^=\the\catcode`^%
   \let\expandafter\noexpand\csname:RestoreCatcodes\endcsname=
                                   \noexpand\undefined}
\catcode`\:=11 \catcode`\@=11  \catcode`\^=7
\def\:latexpg{\ifx\pageno\:undefined
  \let\pageno=\c@page \fi}
\:latexpg
\def\:minus{-}  \catcode`\-=13  \catcode`\+=13
\def\Code#1#2{%
  {\:TraceDef#1{#2}}%
  \ifx #1\:undefined   \else  \def#1{}%
   \:warn{\string\Code\space redefines \string#1}\fi
\ifx #1\:undefined   \ModifyCode{#1}\fi
  \gdef#1{-{#2}{\string#1}}\AppendCode#1}\def\ModifyCode#1{}

\def\:warn#1{\immediate \write16{l.\the\inputlineno
   \space\:minus\string- Warning \:minus\string- #1}}
\def\:CheckCommand#1#2{%
   \def-##1##2{\let\:undefined=\relax}%
   \def+##1##2{}%
   #2\relax
   \ifx \:undefined\relax     \else
      \errmessage{\string#1\string#2 without
                  \string\Code\string#2}\fi}
\def\AppendCode#1{%
   \begingroup
      \:CheckCommand\AppendCode#1%
      \:defaultcats
      \def\:ScanAppend##1{\endgroup
         \:ifempty{##1}\else
   \global\advance\:CodeNumber by 1
   {\def\:TagCs{\string#1.\the\:CodeNumber}%
 \:WriteTrc{DefineCode}}%
   \:ExpandDef#1{+{##1}}{\noexpand
      {\CodeId{\string#1}{\the\:CodeNumber}\noexpand}}%
   \AfterCode{#1}%
\fi}%
      \ModifyAppendCode{#1}\relax
      \:ScanAppend}\def\ModifyAppendCode#1{} \def\AfterCode#1{}

\def\:ifempty#1{\def\:parm{#1}%
   \ifx \:parm\empty}\def\CodeId#1#2{#1.#2} \newcount\:CodeNumber
\newtoks\:tokens
\def\:ExpandDef#1#2#3{%
   \:tokens=\expandafter{#1#2}%
   \xdef#1{\the\:tokens #3}}

\def\:defaultcats{%
  \:NoSpecials{12}{0}{255}%
  \:NoSpecials{11}{65}{90}%
  \:NoSpecials{11}{97}{122}%
  \catcode`\\=0  \catcode`\{=1    \catcode`\}=2
  \catcode`\ =13 \catcode`\^^M=13 \catcode`\^^I=13 }

\newtoks\:tokensi
\newtoks\:tokensii
\def\:ExpandDefSrcLineNo#1#2#3{%
   \:tokens=\expandafter{#1}%
   \:tokensii={#2}%
   \edef\@a@{\noexpand\:tokensi={\CodeInputLine}}\@a@
   \xdef#1{\the\:tokens\the\:tokensi\the\:tokensii #3}}
\def\:srclinebol{\%}
\def\srclineBOL#1{\gdef\:srclinebol{#1}}
\def\CodeInputLine{%
  ^^J\:srclinebol line \CodeInputLineNo\space "./\jobname.tex"^^J}%
\def\SetOutputWithLineNo{\let\:ExpandDef\:ExpandDefSrcLineNo}
\let\CodeLineNo\SetOutputWithLineNo

\def\:NoSpecials#1#2#3{\edef\:temp{\the\:CodeNumber}%
  \global\:CodeNumber=#3
  \def\:next{\catcode\:CodeNumber=#1
    \ifnum \:CodeNumber=#2 \let\:next=\relax \fi
    \global\advance\:CodeNumber by \:minus1 \:next}\:next
  \global\:CodeNumber=\:temp  }
\def-#1#2{{\CodeFont \WindowTitle{#1}}}
\def+#1#2{}
\def\WindowTitle#1{\leavevmode \hbox{$\triangleleft
   \triangleleft$}~#1~\hbox{$\triangleright
   \triangleright$}}
\let\CodeFont=\tt  
\def\ShowCode#1{%
   \def\:test{#1}%
   \ifx \:test\:minus  \expandafter\:ShowCode  \else
                                   \:ShowCode#1\fi}\def\:ShowCode#1{%
   \begingroup  \:ShowCommands
      \:SpTbLn
\def\\{\char 92 }%
\def\{{\char 123 }%
\def\}{\char 125 }%
\ifvmode
   \def\:Sp{\leavevmode\phantom{x}}%
   \def\:Tb{\:Sp\:Sp}%
   \def\:Ln{\leavevmode\par}%
\else
   \def\:Sp{ }%
   \def\:Tb{ \ignorespaces}%
   \def\:Ln{\:Tb}%
\fi
      \ifvmode  \everypar={}%
   \:CodeSkip
   \parskip=0em  \parindent=\:minus3em
   \advance \leftskip by 6em
   %
   \def\:TopTitle##1{{\:TitleShift
            \noindent \hfil \:FrontTitle{##1}\par
            \nobreak \smallskip}}%
   \def\:BottomTitle{{\par  \nobreak  \:TitleShift
            \noindent  \hfil \:BackTitle\par}}%
\else
   \def\:TopTitle##1{\:FrontTitle{##1} }%
   \def\:BottomTitle{ \:BackTitle}%
   \def\:CodeSkip{}%
\fi
\def\:FrontTitle##1{\PortTitle{##1}\:AppendSign}%
\def\:BackTitle{\hbox{$\diamond\diamond\diamond$}}%
%
\def\PortTitle##1{\leavevmode \hbox{$\triangleright
   \triangleright$}~##1~\hbox{$\triangleleft
   \triangleleft$}}%
\def-##1##2{}%
\def+##1##2{\let+=\:next}%
\def\:next##1##2{\def\:AppendSign{\char43 }}%
\def\:AppendSign{}%
#1\relax
      \def-##1##2{}%
\def+##1##2{\def\:LastCodeLabel{##2}}%
\def\:LastCodeLabel{}%
#1\relax
\ifx\:LastCodeLabel\empty\else
   {\def\:TagCs{\:LastCodeLabel}\:WriteTrc{ShowCode}}%
\fi
      \CodeFont
      \:CheckCommand{\ShowCode}{#1}%
      \ModifyShowCode{#1}\relax
      \ifx \:test\:minus \else
   \def-##1##2{\:TopTitle{##1}}%
   \def+##1##2{}%
   \ModifyFrontTitle{#1}\relax
   #1\relax
\fi
      \def-##1##2{}%
\def+##1##2{\def\:LastCode{##1}}%
\def\:LastCode{}%
\ModifyLastCode{#1}\relax
#1\relax\def-##1##2{{\def\:TagCs{##2}\:WriteTrc{ShowTitle}}%
   \WindowTitle{##1}}%
\def+##1##2{}%
\ModifyShowLastCode{#1}\relax
\:LastCode
      \ifx \:test\:minus \else
   \:BottomTitle
\fi
\:CodeSkip
   \endgroup}

\def\ModifyShowCode#1{}
\def\:CodeSkip{\ifdim \CodeSkipAmount>0pt
                     \vskip\:minus\lastskip \fi
  \vskip\CodeSkipAmount}
\def\CodeSkipAmount{\medskipamount}
\def\ShiftTitle#1{%   
   \def\:TitleShift{\advance \leftskip by #1}}
\ShiftTitle{\:minus6em}
\def\ModifyFrontTitle#1{}\def\ModifyLastCode#1{}
\def\ModifyShowLastCode#1{}\begingroup
   \catcode`\.=10  \catcode`\^^I=13  \catcode`\^^M=13%
   \catcode`\ =13%
...\gdef\:SpTbLn{%
......\def {\:Sp}\def^^M{\:Ln}\def^^I{\:Tb}}%
\endgroup\begingroup
  \catcode`\{=12  \catcode`\}=12
  \catcode`\[=1   \catcode`\]=2
     \gdef\:lbrc[{]  \gdef\:rbrc[}]
  \catcode`\,=0  \catcode`\\=12
     ,gdef,:backslash[\]
,endgroup
\catcode`\%=12 \catcode`\@=14
\def\:ShowCommands{@         \tempor 
   \ifvmode
      \linepenalty=100
      \def\NL%{$\rightarrow\rightarrow$\hfill\break}@
      \def\%##1\%{{\CommentFont          
         \def\:Tb{ \ignorespaces}@
         \def\:Sp{ }@
         \def\:Ln{\hfill\break}@
         ##1}}@
   \else
      \def\NL%{}@
      \def\%##1\%{{\CommentFont ##1}}@
   \fi}
\def\CommentFont{\it}
\def\:OutCommands{\def\NL%{}\def\%##1\%{}}
\catcode`\%=14  \catcode`\@=11 \def\:OutputCode#1{%
   \begingroup \:OutCommands
      \:CheckCommand{\OutputCode}{#1}%
      \:latexpg
      {\def\:TagCs{\string#1}\:WriteTrc{OutputCode}}%
      \def\Tag##1##2{}\def\Ref##1{}%
      \:SpTbLn      
\def\:Sp{ }%
\def\:Tb{\:Sp\:Sp}%
\newlinechar`\^^J     \def\:Ln{^^J}%
\def\\{\:backslash}%
\def\{{\:lbrc}%
\def\}{\:rbrc}%
      \def\Extension{prg}%
      \def\:WriteCode{\immediate\write15{#1}}%
      % rules for expanding the code macros
         \def-##1##2{}%
         \def+##1##2{\:comment{##2\ifx \CodeId\:gobbleii\else...\fi}##1%
                     \:comment{\ifx \CodeId\:gobbleii\else...\fi##2}}%
      % name for the auxiliary code file
         \def\:gobble##1{}%
         \def\:FileName{\expandafter
            \:gobble\string#1.\Extension}%
      % writing the file
         \ModifyOutputCode{#1}\relax
         \immediate\openout15=\:FileName  \:WriteCode
         \immediate\closeout15
      % notifying the user
         \ProTexMssg{\string-\string-\string-
            Writing file    \string-\string-\string-
            \:FileName\space\string-\string-\string-}%
   \endgroup }
\def\:gobbleii#1#2{}
\def\ProTexMssg{\immediate\write16}

\def\:ParOutputCode#1]#2{{%
   \let\:ModifyOutputCode=\ModifyOutputCode
   \def\ModifyOutputCode##1{\:ModifyOutputCode{##1}%
      \def\Extension{#1}}%
   \:OutputCode #2}}

\def\OutputCode#1{%
   \ifx#1[\let\:next=\:ParOutputCode
    \else \def\:next{\:OutputCode#1}\fi \:next}

\def\ModifyOutputCode#1{}\def\:comment#1{}
\def\Comment{\begingroup
   \:defaultcats
   \::comment}

\def\::comment#1#2{\endgroup
   \def\:flag{}%
   \:ifempty{#1}\else \def\:flag{.}\fi
   \:ifempty{#2}\else \def\:flag{.}\fi
   \ifx \:flag\empty  \def\:comment##1{}%
\else              \def\:comment##1{#1##1#2}\fi}
\def\Ref#1{%
   \:ifempty{#1}\:warn{Empty field in \noexpand\Ref}%
   \else
      \expandafter\ifx \csname  cw:#1\endcsname \relax
         \:NoTag{#1}%
      \else \expandafter\csname cw:#1\endcsname \fi
    \fi}
\def\Tag#1#2{%
  \:ifempty{#1}\:warn{First field
                         empty in \noexpand\Tag}%
  \else
     \begingroup
        \:latexpg
        \edef\:head{\:TagCs\space{#1}{#2}}%
        \def\:write{\write\:refout}%
        \expandafter \expandafter
           \expandafter\:write
              \expandafter{\:head{\the\pageno}}%
     \endgroup
  \fi}

\let\:trc=\Tag
\def\:TagCs{\string\:CrossWord}                                \catcode`\-=12   \catcode`\+=12
\ifx \:refout\:UnDef
   \openin15    \jobname.xref
   \ifeof15     \immediate\write16{No file \jobname.xref}
                \def\:NoTag#1{\hbox{$???$}}
   \else
      \begingroup
         \def\:CrossWord#1#2#3{%
   \def\:parm{#2}%
   \ifx \:parm\empty  \:AddTag{#1}{#3}\else
                      \:AddTag{#1}{#2}\fi}

\def\:AddTag#1#2{%
   \expandafter \ifx \csname  cw:#1\endcsname \relax
         \def \:entries{#2}%
   \else \edef\:entries{\csname  cw:#1\endcsname , #2}%
   \fi
   \expandafter \xdef \csname cw:#1\endcsname{\:entries}}
         \input \jobname.xref
      \endgroup
      \def\:NoTag#1{\hbox{$???$}%
                    \:warn{\string\Ref\string{\string#1\string}?}}
   \fi
   \closein15
\fi
                                 \catcode`\-=13   \catcode`\+=13
\ifx \:refout\:UnDef
   \csname newwrite\endcsname\:refout
          \openout\:refout=\jobname.xref
\fi 
\newwrite\:trcfile
\immediate\openout\:trcfile=\jobname.trc

\def\:WriteTrc#1{{\let\:refout=\:trcfile
   \:trc{\string#1}{\the\inputlineno}}}
\def\:TraceDef#1#2{\:tokens={#2}%
   \immediate\write\:trcfile{%
      \string#1\space{DefineTitle}{{\the
                   \:tokens};;\the\inputlineno}{}}}
\def\:AlProTex{\ifx \:next[
  \def\AlProTex[##1]##2{\egroup \def\AlProTex{##2}\input ##1 }
  \else
  \def\AlProTex##1{\egroup \def\AlProTex{##1}\input AlProTex.sty }
  \fi        \let\:AlProTex=\:undefined        \AlProTex}
\def\AlProTex{\bgroup  \catcode`\@=12     \catcode`\^=7  \catcode`\_=8
  \futurelet\:next\:AlProTex}
\ifx \inputlineno\undefined
  \csname newcount\endcsname\inputlineno
  \let\:oldNoSpecials=\:NoSpecials
  \def\:NoSpecials#1#2#3{\ifnum #3>127
     \:oldNoSpecials{#1}{#2}{127}\else
     \:oldNoSpecials{#1}{#2}{#3}\fi }
\fi 
\:RestoreCatcodes \endinput
