%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Part of the ptx2pdf macro package for formatting USFM text
% copyright (c) 2007-2008 by SIL International
% written by Jonathan Kew
%
% Permission is hereby granted, free of charge, to any person obtaining  
% a copy of this software and associated documentation files (the  
% "Software"), to deal in the Software without restriction, including  
% without limitation the rights to use, copy, modify, merge, publish,  
% distribute, sublicense, and/or sell copies of the Software, and to  
% permit persons to whom the Software is furnished to do so, subject to  
% the following conditions:
%
% The above copyright notice and this permission notice shall be  
% included in all copies or substantial portions of the Software.
%
% 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.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% ptx-toc.tex
% Table of Contents generation

% Usage: \GenerateTOC[title]{filename}
% where [title] is optional, default is "Table of Contents"
% Writes TOC entries from \toc1, \toc2, \toc3 markers
% to the given file, which can be used with \ptxfile{...}
% or renamed and edited as needed to customize the TOC.

\def\GenerateTOC{\edef\s@vebracketcat{\the\catcode`\[}%
  \catcode`\[=12 \futurelet\n@xt\g@ntoc}
\def\g@ntoc{\ifx\n@xt[\let\n@xt\g@ntoc@rg
  \else\let\n@xt\g@ntocn@@rg\fi\n@xt}
\def\g@ntocn@@rg#1{\g@ntoc@rg[Table of Contents]{#1}}
\def\g@ntoc@rg[#1]#2{\def\t@ctitle{#1}\def\t@cfilename{#2}%
  \catcode`\[=\s@vebracketcat\relax}
\let\t@cfilename\empty

\newwrite\t@cfile

\addtoinithooks{\set@ptocmarkers}
\addtoendhooks{\closet@cfile}
\newif\ifuseTOCthree\useTOCthreetrue
\newif\ifTOCthreetab\TOCthreetabtrue
\newif\ifuseTOCtwo\useTOCtwotrue
\newif\ifuseTOCone\useTOConetrue


\m@kedigitsletters
\def\set@ptocmarkers{
%  \def\toc1{\bgroup\deactiv@tecustomch@rs\obeylines\t@cA}
%  \def\toc2{\bgroup\deactiv@tecustomch@rs\obeylines\t@cB}
%  \def\toc3{\bgroup\deactiv@tecustomch@rs\obeylines\t@cC}
  \def\toc1{\endlastp@rstyle{toc1}\bgroup\obeylines\t@cA}
  \def\toc2{\endlastp@rstyle{toc2}\bgroup\obeylines\t@cB}
  \def\toc3{\endlastp@rstyle{toc3}\bgroup\obeylines\t@cC}
  \def\zthumbtab{\endlastp@rstyle{zthumbtab}\bgroup\obeylines\th@mt@b} %Manual control of thumbtab text, from usfm file.

}
\m@kedigitsother

{\obeylines%
 \gdef\t@cA #1^^M{\ifuseTOCone\gdef\toc@A{#1}\fi\egroup}%%\message{TOC1 defined \toc@A}}%
 \gdef\t@cB #1^^M{\ifuseTOCtwo\gdef\toc@B{#1}\fi\egroup}%
 \gdef\t@cC #1^^M{\ifuseTOCthree\gdef\toc@C{#1}\fi\ifTOCthreetab\ifx\b@okShort\empty\gdef\b@okShort{#1}\fi\fi\egroup}%
 \gdef\th@mt@b #1^^M{\gdef\b@okShort{#1}\ifx\b@okShort\empty\gdef\b@okShort{\relax}\fi\egroup}%
}

\gdef\toc@A{}%
\gdef\toc@B{}%
\gdef\toc@C{}%
\newcount\tocc@l
\newif\ift@copen
\t@copenfalse

\newif\ifnotocwrite
\catcode`\[=1 \catcode`\]=2 \catcode`\}=12 \catcode`\{=12
\gdef\maket@cline[%
   %\message[Writing toc]%
   \ifx\t@cfilename\empty
   \else\ifnotocwrite\else
     \ift@copen\else
       \immediate\openout\t@cfile=\t@cfilename \global\t@copentrue
       %\write\t@cfile[\string\id \space FRT -- autogenerated --]
       %\write\t@cfile[\string\unprepusfm \string\catcode `\string\@ =11]
       \immediate\write\t@cfile[\string\defTOC{main}{]%
     \fi
     \begingroup
       \pr@tectspecials % protect \ZWSP etc from expansion in the TOC text
       \pr@tectcustomch@rs
       \deactiv@tecustomch@rs
       \ifdiglot\edef\tmp[{\id@@@\c@rrdstat}]\else\edef\tmp[{\id@@@}]\fi
       \edef\tmp[\string\doTOCline\tmp]%
       \x@\addto@macro\x@\tmp\x@[\x@{\toc@A}]%
       \x@\addto@macro\x@\tmp\x@[\x@{\toc@B}]%
       \x@\addto@macro\x@\tmp\x@[\x@{\toc@C}]%
       \let\ttmp\tmp
       \x@\x@\x@\def\x@\x@\x@\tmp\x@\x@\x@[\x@\detokenize\x@[\ttmp ]]%
       \x@\def\x@\t@cline\x@[\tmp{\the\pageno }]%
       \trace[t][\t@cline]\x@\write\x@\t@cfile\x@[\t@cline]%
     \endgroup
     \gdef\toc@A[]%
     \gdef\toc@B[]%
     \gdef\toc@C[]%
   \fi\fi
]
\def\closet@cfile[%
  \ift@copen
      \immediate\write\t@cfile[}]
      %\immediate\write\t@cfile[\string\catcode `\string\@ =12 \string\prepusfm]
      %\immediate\write\t@cfile[\string\id \space FRT -- autogenerated --]
      %\immediate\write\t@cfile[\string\is \space\t@ctitle]
      %\immediate\write\t@cfile[\string\p]
      %\immediate\write\t@cfile[\string\zTOC]
      %\immediate\write\t@cfile[\string\p]
    \immediate\closeout\t@cfile
    \global\t@copenfalse
  \fi
]
\catcode`\{=1 \catcode`\}=2 \catcode`\[=12 \catcode`\]=12

\def\checkfort@c{%
  \let\n@xt\relax
  \ifx\toc@A\empty \else \let\n@xt\maket@cline \fi
  \ifx\toc@B\empty \else \let\n@xt\maket@cline \fi
  \ifx\toc@C\empty \else \let\n@xt\maket@cline \fi
  \n@xt}

\def\tcb@se#1{cat:toc|tc#1\ifdiglot\c@rrdstat\fi}%
\newif\ifTOCRTL
\def\fnname{doTOCline}
\newcount\t@ccount %unique count, as there are \globals around.
\x@\def\csname\fnname\endcsname#1#2#3#4#5{%
% ID, cell1, cell2, cell3, pagenum
  \trace{t}{\fnname\space #1 #2 #3 #4 #5}%
  \def\tablecategory{toc}\tr
  \def\t@mpa{#2}%
  \def\t@mpb{#3}%
  \def\t@mpc{#4}%
  \global\t@ccount=1
  \ifx\t@mpa\empty\else
    \ifcsname tocalign\the\t@ccount\endcsname
      \edef\t@mpd{tc\csname tocalign\the\t@ccount\endcsname\the\t@ccount}%
    \else
      \edef\t@mpd{tc\the\t@ccount}%
    \fi
    \trace{t}{A: \t@mpd \space \t@mpa}%
    \ifx\int@c\empty\else\msg{TOC[\int@c ] col \the\t@ccount \space \t@mpd}\fi
    \csname \t@mpd\endcsname \t@mpa
    \ifcsname \tcb@se{\the\t@ccount}:fill\endcsname\else\d@ftoc{\the\t@ccount}{fill}{\hfill}\fi
    \ifcsname \tcb@se{\the\t@ccount}:verticalalign\endcsname\else\d@ftoc{\the\t@ccount}{verticalalign}{b}\fi
    \ifcsname \tcb@se{\the\t@ccount}:rightmargin\endcsname\else\d@ftoc{\the\t@ccount}{rightmargin}{0}\fi
    \ifnum\t@ccount>1 \ifcsname \tcb@se{\the\t@ccount}:leftmargin\endcsname\else \d@ftoc{\the\t@ccount}{leftmargin}{0}\fi\fi
    \global\advance\t@ccount by 1
  \fi
  \ifx\t@mpb\empty\else
    \ifcsname tocalign\the\t@ccount\endcsname
      \edef\t@mpd{tc\csname tocalign\the\t@ccount\endcsname\the\t@ccount}%
      \trace{t}{Found tocalign\the\t@ccount}%
    \else
      \edef\t@mpd{tc\the\t@ccount}%
    \fi
    \trace{t}{B: \t@mpd \space \t@mpb}%
    \ifx\int@c\empty\else\msg{TOC[\int@c ] col \the\t@ccount \space \t@mpd}\fi
    \csname \t@mpd\endcsname \t@mpb
    \ifcsname \tcb@se{\the\t@ccount}:fill\endcsname\else\d@ftoc{\the\t@ccount}{fill}{\hfill}\fi
    \ifcsname \tcb@se{\the\t@ccount}:verticalalign\endcsname\else\d@ftoc{\the\t@ccount}{verticalalign}{b}\fi
    \ifcsname \tcb@se{\the\t@ccount}:rightmargin\endcsname\else\d@ftoc{\the\t@ccount}{rightmargin}{0}\fi
    \ifnum\t@ccount>1\ifcsname \tcb@se{\the\t@ccount}:leftmargin\endcsname\else\d@ftoc{\the\t@ccount}{leftmargin}{0}\fi\fi
    \global\advance\t@ccount by 1
  \fi
  \ifx\t@mpc\empty\else
    \ifcsname tocalign\the\t@ccount\endcsname
      \edef\t@mpd{tc\csname tocalign\the\t@ccount\endcsname\the\t@ccount}%
    \else
      \edef\t@mpd{tc\the\t@ccount}%
    \fi
    \trace{t}{C: \t@mpd \space \t@mpc}%
    \ifx\int@c\empty\else\msg{TOC[\int@c ] col \the\t@ccount \space \t@mpd}\fi
    \csname \t@mpd\endcsname \t@mpc
    \ifcsname \tcb@se{\the\t@ccount}:fill\endcsname\else\d@ftoc{\the\t@ccount}{fill}{\hfill}\fi
    \ifcsname \tcb@se{\the\t@ccount}:verticalalign\endcsname\else\d@ftoc{\the\t@ccount}{verticalalign}{b}\fi
    \ifcsname \tcb@se{\the\t@ccount}:rightmargin\endcsname\else\d@ftoc{\the\t@ccount}{rightmargin}{0}\fi
    \ifnum\t@ccount>1\ifcsname \tcb@se{\the\t@ccount}:leftmargin\endcsname\else\d@ftoc{\the\t@ccount}{leftmargin}{0}\fi\fi
    \global\advance\t@ccount by 1
  \fi
  \edef\t@mpd{tcr\the\t@ccount}%
  \ifx\int@c\empty\else\msg{TOC[\int@c ] col \the\t@ccount \space \t@mpd}\fi
  \ifcsname l@drs\endcsname
    \trace{t}{Refactor for last column \the\t@ccount, \tcb@se{\the\t@ccount}}%
    \deftocalign{\the\t@ccount}{r}%
    \x@\gdef\csname cat:toc|tc\the\t@ccount :fill\endcsname{\hskip 0pt}%
    \ifcsname \tcb@se{tc\the\t@ccount}:verticalalign\endcsname\else\d@ftoc{\the\t@ccount}{verticalalign}{b}\fi
    \ifcsname \tcb@se{\the\t@ccount}:rightmargin\endcsname\else\d@ftoc{\the\t@ccount}{rightmargin}{0}\fi
    \ifnum\t@ccount>1\ifcsname \tcb@se{\the\t@ccount}:leftmargin\endcsname\else\d@ftoc{\the\t@ccount}{leftmargin}{0}\fi\fi
  \fi
  \csname\t@mpd\endcsname #5\@ndcell\@ndrow
  \trace{t}{\noexpand\tcr\the\t@ccount \space \t@mpd}%
  \gdef\tablecategory{}%
  \global\let\int@c\empty
}

\addtoeveryparhooks{\checkfort@c}

\let\tocf@@t=\empty
\let\tocaft@r=\empty
\let\tocb@fore=\empty
\def\d@ftoc#1#2#3{\should@xist{cat:toc|}{#1\ifdiglot\c@rrdstat\fi}\expandafter\xdef\csname \tcb@se{#1}:#2\endcsname{#3}}
\def\d@ftocx#1#2{\should@xist{cat:toc|}{#1\ifdiglot\c@rrdstat\fi}\expandafter\xdef\csname cat:toc|#1\ifdiglot\c@rrdstat\fi\endcsname{#2}}
\def\defTOC#1#2{%
  \gdef\int@c{#1}%
  \ifRTL\x@\def\csname ms:ztoc=#1\endcsname{\TOCRTLtrue\gentoche@dfoot\global\keeptriggertrue \toch@ad \tocb@fore #2\tocaft@r \tocf@@t \endt@ble\global\let\tocf@@t\empty\global\let\tocaft@r\empty}%
  \else\x@\def\csname ms:ztoc=#1\endcsname{\TOCRTLfalse\gentoche@dfoot\global\keeptriggertrue \toch@ad \tocb@fore #2\tocaft@r \tocf@@t \endt@ble\global\let\tocf@@t\empty\global\let\tocaft@r\empty}%
  \fi}

\def\ztocafter#1\ztocafter*{\x@\gdef\x@\tocaft@r\x@{\tocaft@r #1}}
\def\ztocbefore#1\ztocbefore*{\x@\gdef\x@\tocb@fore\x@{\tocb@fore #1}}

\def\addt@tmptok#1#2{\x@\tmpt@ks\x@{\the\tmpt@ks \csname #1\endcsname #2}}%
\def\gentoche@dfoot{\let\toch@ad\empty\let\tocf@@t\empty
  \tmpt@ks{\tr }%
  \tempfalse
  \get@ttribute{h1}\ifx\relax\attr@b\let\attr@b\empty\else\temptrue\edef\tmp{{th1}{\attr@b}}\x@\addt@tmptok\tmp\fi
  \get@ttribute{h2}\ifx\relax\attr@b\let\attr@b\empty\else\temptrue\edef\tmp{{th2}{\attr@b}}\x@\addt@tmptok\tmp\fi
  \get@ttribute{h3}\ifx\relax\attr@b\let\attr@b\empty\else\temptrue\edef\tmp{{th3}{\attr@b}}\x@\addt@tmptok\tmp\fi
  \get@ttribute{h4}\ifx\relax\attr@b\let\attr@b\empty\else\temptrue\edef\tmp{{th4}{\attr@b}}\x@\addt@tmptok\tmp\fi
  \iftemp
    \x@\def\x@\toch@ad\x@{\the\tmpt@ks}%
  \fi
  \tmpt@ks{\tr }\tempfalse%
  \get@ttribute{f1}\ifx\relax\attr@b\let\attr@b\empty\else\temptrue\edef\tmp{{th1}{\attr@b}}\x@\addt@tmptok\tmp\fi
  \get@ttribute{f2}\ifx\relax\attr@b\let\attr@b\empty\else\temptrue\edef\tmp{{th2}{\attr@b}}\x@\addt@tmptok\tmp\fi
  \get@ttribute{f3}\ifx\relax\attr@b\let\attr@b\empty\else\temptrue\edef\tmp{{th3}{\attr@b}}\x@\addt@tmptok\tmp\fi
  \get@ttribute{f4}\ifx\relax\attr@b\let\attr@b\empty\else\temptrue\edef\tmp{{th4}{\attr@b}}\x@\addt@tmptok\tmp\fi
  \iftemp
    \x@\def\x@\tocf@@t\x@{\the\tmpt@ks}%
  \fi
}

\newbox\l@drsbox
\def\l@drscts{\relax}
\def\tocleadershrule#1#2{\global\let\l@drscts\relax\gdef\l@drs{\xleaders #1 \hfill}\t@cleaders{#1}{#2}}
\def\tocleaders#1#2{\gdef\l@drscts{#1}\global\setbox\l@drsbox\hbox{#1}\gdef\l@drs{\xleaders\copy\l@drsbox\hfill}\t@cleaders{#1}{#2}}
\def\t@cleaders#1#2{%
  \trace{t}{t@cleaders #1 #2}%
  \Marker cat:toc|tc1\relax
  \StyleType Character\relax
  \Marker cat:toc|tc2\relax
  \StyleType Character\relax
  \Marker cat:toc|tc3\relax
  \StyleType Character\relax
  \Marker cat:toc|tc4\relax
  \StyleType Character\relax
  \dimen0=\IndentUnit \dimen1=#2 \divide\dimen0 by \dimen1
  \ifdim\dimen0>1sp \dimen1=1pt \divide\dimen1 by \dimen0\else\dimen0=0pt \fi
  \edef\rm@{\strip@pt{\dimen1}}%
  \global\setbox\l@drsbox\hbox{\s@tfont{ldrs}{ldrs}\l@drscts}%
  \t@ccount=0
  \loop
    \advance\t@ccount by 1
    %\d@ftoc{\the\t@ccount}{rightmargin}{\rm@}
    \ifnum\t@ccount>1 \ifcsname \tcb@se{\the\t@ccount}:rightmargin\endcsname\else \d@ftoc{\the\t@ccount}{rightmargin}{\rm@}\fi\fi
    \trace{t}{Setting leaders-tc\the\t@ccount}%
%    \d@ftocx{leaders-tcr\the\t@ccount}{\ifx\l@drscts\relax\relax\l@drs\else\rightskip=0pt \leftskip=0pt \xleaders\hbox{\noexpand\s@tfont{ldrs}{ldrs+tcr\the\t@ccount+\noexpand\styst@k}\l@drscts}\hfill\fi}
    \d@ftocx{leaders-tc\the\t@ccount}{\ifx\l@drscts\relax\relax\l@drs\else{\rightskip=0pt \leftskip=0pt \xleaders\hbox{\noexpand\s@tfont{ldrs}{ldrs+tc\the\t@ccount+\noexpand\styst@k}\l@drscts}\hfill}\fi}
%    \d@ftocx{leaders-tcc\the\t@ccount}{\ifx\l@drscts\relax\relax\l@drs\else\rightskip=0pt \leftskip=0pt \xleaders\hbox{\noexpand\s@tfont{ldrs}{ldrs+tcc\the\t@ccount+\noexpand\styst@k}\l@drscts}\hfill\fi}
    \trace{t}{Filling \tcb@se{\the\t@ccount} with leaders}%
    \d@ftoc{\the\t@ccount}{fill}{\ifx\l@drscts\relax\l@drs\else\xleaders\hbox{\noexpand\s@tfont{ldrs}{ldrs+tc\the\t@ccount+\noexpand\styst@k}\l@drscts}\hfill\fi}
  \ifnum\t@ccount<5\repeat
}
\def\deftocalign#1#2{\x@\def\csname tocalign#1\endcsname{#2}}
