% +----------------------------------------+
% | See documentation in the readme file.  |
% +----------------------------------------+
%
% This file is copyrighted (c) 1998 1999 by Javier Bezos.
% All Rights Reserved.
%
% This file is part of the titlesec distribution release 2.1.
% -----------------------------------------------------------
%
% This program can be 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 any later version.

\ProvidesPackage{titletoc}[1999/04/22 v1.1 TOC entries]

% The following tags are used:
% ttl@  : the generic one, shared with titlesec
% ttle@ : ending puntuation for block format
% ttll@ : section level
% ttlx@ : file extension
% 
% Fistly, comes the
% common stuff with titlesec
% ~~~~~~~~~~~~~~~~~~~~~~~~~~

\newif\ifttl@numsec
\newif\ifttl@fromblock

\providecommand\titleline{%
  \@ifnextchar[{\ttl@line}{\ttl@line[s]}}

\def\ttl@line[#1]#2{%
  \vskip\topskip
  \hrule \@height \z@
  \nobreak
  \vskip-\topskip
  \begingroup    
    \parindent\z@
    \everypar{}%
    \leftskip\z@
    \rightskip\z@
    \@makebox[\hsize][#1]{\ttl@makeline{#2}}%
    \par
  \endgroup
  \hrule height \z@
  \nobreak}
  
% Fillers:

\providecommand\titlerule{\@ifstar{\ttl@row}{\ttl@rule}}

\let\ttl@leaders\xleaders

\def\ttl@row{\@ifnextchar[{\ttl@row@i}{\ttl@row@i[\wd\z@]}}
\def\ttl@row@i[#1]#2{%
  \ifvmode\expandafter\titleline\fi
  {\sbox\z@{#2}%
   \hspace{-#1}%
   \hskip\wd\z@
   \ttl@leaders\hb@xt@#1{\hss\box\z@}%
   \hfill\kern\z@}}

\def\ttl@rule{\@ifnextchar[{\ttl@rule@i}{\ttl@rule@i[.4pt]}}
\def\ttl@rule@i[#1]{%
  \ifvmode\expandafter\titleline\fi
  {\leaders\hrule height #1\hfill\kern\z@}}

\providecommand\filright{%
  \gdef\es@filleft##1{\hskip##1}%
  \gdef\es@filright##1{\hfill}%
  \let\\\@centercr
  \advance\rightskip\z@ \@plus 1fil\relax}
\providecommand\filleft{%
  \gdef\es@filleft##1{\hfill}%
  \gdef\es@filright##1{\hskip##1}%
  \let\\\@centercr
  \advance\leftskip\z@ \@plus 1fil
  \parfillskip\z@}
\providecommand\filcenter{\filleft\filright
  \gdef\es@filleft##1{\hfill}}
\providecommand\fillast{%
  \gdef\es@filleft##1{\hfill}%
  \gdef\es@filright##1{\hfill}%
  \let\\\@centercr
  \filleft\advance\rightskip\z@ \@plus -1fil
  \parfillskip\z@ \@plus 2fil\relax}
  
% Now, the specific titletoc part

% User interface
% ~~~~~~~~~~~~~~
% Tools:

\DeclareOption{dotinlabels}{\def\ttl@idot{.}}
\DeclareOption{nodotinlabels}{\let\ttl@idot\@empty}

\DeclareOption{leftlabels}{%
  \renewcommand\numberline[1]{\hb@xt@\@tempdima{#1\ttl@idot\hfil}}%
  \newcommand\contentslabel[2][\thecontentslabel\ttl@idot]{%
    \hspace*{-#2}\hb@xt@#2{#1\hfil}}}

\DeclareOption{rightlabels}{%
  \renewcommand\numberline[1]{\hb@xt@\@tempdima{\hss#1\ttl@idot\enspace}}%
  \let\contentslabel\relax
  \newcommand\contentslabel[2][\thecontentslabel\ttl@idot\enspace]{%
    \hspace*{-#2}\hb@xt@#2{\hfil#1}}}

\newcommand\contentspage[1][\thecontentspage]{%
  \hb@xt@\@pnumwidth{\hfil#1}%
  \hspace*{-\@pnumwidth}}

\newcommand\contentspush[1]{%
  \sbox\z@{#1}%
  \advance\leftskip\wd\z@
  \leavevmode\llap{\box\z@}}

% General commands
% A level register. Explicit numbers because they are
% used in csnames.

\@ifundefined{chapter}
 {\def\ttll@part{0}}
 {\def\ttll@part{-1}%
  \def\ttll@chapter{0}}
\def\ttll@section{1}
\def\ttll@subsection{2}
\def\ttll@subsubsection{3}
\def\ttll@paragraph{4}
\def\ttll@subparagraph{5}

% We make sure that a series of * entries are finished and
% that a \titlecontents in the middle of a document is
% written to the right file.

\newcommand\contentsuse[2]{%
  \expandafter\def\csname ttlx@#1\endcsname{#2}%
  \expandafter\def\csname ttll@#1\endcsname{-1000}%
  \AtEndDocument{\addtocontents{#2}{\protect\ttl@finish}}}

\contentsuse{figure}{lof}
\contentsuse{table}{lot}
\AtEndDocument{\addtocontents{toc}{\protect\ttl@finish}}

\def\ttl@finish{%
  \ifttl@fromblock
    \csname ttle@\ttl@lasttoc\endcsname
    \@@par
    \endgroup
    \global\ttl@fromblockfalse
  \fi}


% There is a further tag
% ttle@:  ending punctuacion
% 
% The two basic commands:

\newcommand\contentsmargin[1][\z@]{%
  \def\ttl@corr{#1}\def\@pnumwidth}

\newcommand\ttl@margin[2][\z@]{%
  \def\ttl@corr{#1}%
  \advance\rightskip-\@pnumwidth\relax
  \advance\rightskip#1\relax
  \def\@pnumwidth{#2}}

%% titlecontents deals with concepts, not commands; hence no escape

\newcommand\titlecontents{%
  \@ifstar{\ttl@contents{\z@}}
          {\ttl@contents{\@ne}}}

\def\ttl@contents#1#2{%
  \@ifnextchar[{\ttl@contents@x{#1}{#2}}
               {\PackageError{titletoc}{Unimplemented}%
                {The optional argument is currently mandatory}}}%

\def\ttl@contents@x#1#2[#3]#4#5#6#7{%
  \@ifnextchar[{\ttl@contents@i{#1}{#2}{#3}{#4}{#5}{#6}{#7}}%
               {\ttl@contents@i{#1}{#2}{#3}{#4}{#5}{#6}{#7}[]}}

\def\ttl@contents@i#1#2#3#4#5#6#7[#8]{%
  \ifcase#1
    \expandafter\@firstoftwo
  \else
    \expandafter\@secondoftwo
  \fi
  {\@ifnextchar[{\ttl@contents@ii{#1}{#2}{#3}{#4}{#5}{#6}{#7}{#8}}
                {\ttl@contents@ii{#1}{#2}{#3}{#4}{#5}{#6}{#7}{#8}[]}}
  {\ttl@contents@ii{#1}{#2}{#3}{#4}{#5}{#6}{#7}{#8}[]}}

\def\ttl@contents@ii#1#2#3#4#5#6#7#8[#9]{%
  \expandafter\def\csname l@#2\endcsname
    {\ttl@tocentry{#1}{#2}{#3}{#4}{{#5}{#6}}{#7}{#8}}
  \expandafter\def\csname ttle@#2\endcsname{#9}}

\begingroup
\catcode`\-=12\catcode`\>=12
\gdef\ttl@strip#1->#2\@@#3{\def#3{#2}}
\endgroup

\AtBeginDocument{%
  \let\ttl@change\ttl@contents@ii
  \def\ttl@contents@ii#1#2#3#4#5#6#7#8[#9]{%
    \def\ttl@a{\ttl@change{#1}{#2}{#3}{#4}{#5}{#6}{#7}{#8}[#9]}
    \expandafter\ttl@strip\meaning\ttl@a\@@\ttl@a
    \addtocontents
     {\expandafter\ifx\csname ttlx@#2\endcsname\relax
        toc%
      \else
        \csname ttlx@#2\endcsname
      \fi}%
     {\ttl@a}}}

% Printing the toc entry
% ~~~~~~~~~~~~~~~~~~~~~~

\def\ttl@lasttoc{figure} % An inital dummy assignment

% 1 ifblock, 2 sect name, 3 left, 4 before,
% 5 {with}{without}, 6 filler/page, 7 after 8 title 9 pageno

\def\ttl@tocentry#1#2#3#4#5#6#7#8#9{%
  \edef\ttl@b{\csname ttll@#2\endcsname}%
  \ifnum\ttl@b>\c@tocdepth\else
    \edef\ttl@a{\csname ttll@\ttl@lasttoc\endcsname}%
    \gdef\thecontentspage{#9}%
    \global\let\thecontentslabel\@empty
    \ttl@numsecfalse
    \sbox\z@{%  Unused box. It just catch the numberline
      \def\numberline##1{\global\ttl@numsectrue\gdef\thecontentslabel{##1}}%
      #8}%
    \ifttl@fromblock
      \ifcase#1\relax
        {#7}%
        \ifnum\ttl@b=\ttl@a\else
        \PackageError{titletoc}{Mixing of levels}%
           {You cannot mix entries of different levels in a paragraph}%
        \fi
      \else
        \csname ttle@\ttl@lasttoc\endcsname
        \@@par
        \endgroup
        \@firstoftwo
      \fi
    \else
      \begingroup
      \ifnum\ttl@b>\ttl@a
        \nobreak
      \else\ifnum\ttl@b<\ttl@a
        \addpenalty{\@secpenalty}%
      \else
        \addpenalty{\z@}% 
      \fi\fi
      \ifcase#1\relax\else\interlinepenalty\@M\fi
      \parindent\z@
      \setlength\leftskip{#3}%
      \setlength\rightskip{\@pnumwidth}%
      \let\contentsmargin\ttl@margin
      \def\ttl@makeline##1{##1}%
      #4%
      \addtolength{\parfillskip}{-\ttl@corr}%
      \addtolength{\rightskip}{\ttl@corr}%
      \vskip \z@ \@plus .1\p@
      \nobreak
      \let\ttl@leaders\leaders
    \fi
    \def\numberline##1{\ignorespaces}%
    \ifttl@numsec
      \leavevmode\strut\@firstoftwo#5{#8}\strut\kern\z@
    \else
      \leavevmode\strut\@secondoftwo#5{#8}\strut\kern\z@
    \fi
    {#6}%
    \ifcase#1\relax
      \global\ttl@fromblocktrue
    \else
      \@@par
      \nobreak
      #7%
      \endgroup
      \global\ttl@fromblockfalse
    \fi
    \xdef\ttl@lasttoc{#2}%
  \fi
  \ignorespaces}

\ExecuteOptions{leftlabels,nodotinlabels}

\ProcessOptions

\let\ttl@savel@chapter\l@chapter
\def\l@chapter{\ttl@finish\ttl@savel@chapter}

\let\ttl@savel@section\l@section
\def\l@section{\ttl@finish\ttl@savel@section}

\let\ttl@savel@subsection\l@subsection
\def\l@subsection{\ttl@finish\ttl@savel@subsection}

\let\ttl@savel@subsubsection\l@subsubsection
\def\l@subsubsection{\ttl@finish\ttl@savel@subsubsection}

\let\ttl@savel@paragraph\l@paragraph
\def\l@paragraph{\ttl@finish\ttl@savel@paragraph}

\let\ttl@savel@subparagraph\l@subparagraph
\def\l@subparagraph{\ttl@finish\ttl@savel@subparagraph}

\@tempskipa\@pnumwidth
\edef\@pnumwidth{\the\@tempskipa}
\advance\@tempskipa-\@tocrmarg
\edef\ttl@corr{-\the\@tempskipa}

\endinput

