% +--------------------------------------------------+
% | Typeset titlesec.tex to get the documentation.   |
% +--------------------------------------------------+
%
% Copyright (c) 1998-2007 by Javier Bezos.
% All Rights Reserved.
%
% This file is part of the titlesec distribution release 2.8
% -----------------------------------------------------------
%
% It may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either version 1.3
% of this license or (at your option) any later version.
% The latest version of this license is in
%   http://www.latex-project.org/lppl.txt
% and version 1.3 or later is part of all distributions of LaTeX
% version 2003/12/01 or later.
% 
% This work has the LPPL maintenance status "maintained".
% 
% The Current Maintainer of this work is Javier Bezos.

\ProvidesFile{ttlkeys.def}[2007/08/12]

% This command takes a list in "keyval" format, \ttl@a containing the
% star spec, and returns \ttl@a with the full string name to be defined
% and \ttl@b with the string sectioning name (without tag).  But if the
% argument has no keys then simply does the last step.  Finally, an
% error is reported if the sectioning command does not exist.  All these
% testing must be improved (still).

\@ifundefined{define@key}{\RequirePackage{keyval}}{}%

\def\ttl@getkeys#1#2{%
  \let\ttl@a\@empty
  \if\expandafter @\@gobble#1@\@empty % if there is a single token
    \edef\ttl@b{\expandafter\@gobble\string#1}%
    \let\ttl@a\ttl@b
  \else
    \ttl@labelfalse  %  A temporary flag: true if there is page key
    \setkeys{#2}{#1}%
    \ifttl@label
      \@ifundefined{ttlp@\ttl@b}{%
        \expandafter\let\csname ttlp@\ttl@b\endcsname\@empty}{}%
    \fi
    \edef\ttl@a{\ttl@b\ttl@a}%
  \fi}

\define@key{titlesec}{name}{%
  \edef\ttl@b{\expandafter\@gobble\string#1}}

\define@key{titlesec}{numberless}[true]{%
  \csname @tempswa#1\endcsname
  \if@tempswa
    \edef\ttl@a{\ttl@a/*}%
  \fi}

\define@key{titlesec}{page}{%
  \ttl@labeltrue  % Used as flag
  \edef\ttl@a{/#1\ttl@a}}

\def\ttl@extra@numberless{\ttl@labeltrue}% The actual meaning

\let\ttl@key@numberless\@empty
%\let\ttl@key@matter\@empty

\let\ttlp@append\@gobbletwo

\def\ttl@setkeys#1{%
  \def\ttl@trylist{\ttl@try{}}%
  \@for\ttl@b:=#1\do{%
    \begingroup
      \let\ttl@a\relax
      \def\ttl@try##1{%
        \noexpand\ttl@try{##1\ttl@a{\ttl@b}}%
        \noexpand\ttl@try{##1}}%
      \xdef\ttl@trylist{\ttl@trylist}%
    \endgroup}}

\ttl@setkeys{page,numberless}  % matter

%___
\if@twoside

\newcounter{ttlp@side}
\newcount\ttlp@side

\def\ttlp@theside{\ifodd\c@page o\else e\fi}

\def\ttlp@append#1#2{%
 {\let\@elt\relax
  \expandafter\xdef\csname ttlp@#1\endcsname{%
     \csname ttlp@#1\endcsname\@elt #2}}}

\def\ttlp@write#1{%
  {\let\ttlp@theside\relax
   \protected@write\@auxout{}%
     {\string\ttlp@append{#1}{\ttlp@theside}}}}

\def\ttlp@fetch#1{%
   \stepcounter{ttlp@side}%
   \global\advance\ttlp@side\@ne
   \@whilenum\ttlp@side<\c@ttlp@side\do{%
      \expandafter\@next\expandafter\@tempa\csname ttlp@#1\endcsname{}{}%
      \global\advance\ttlp@side\@ne}%
   \expandafter\@next\expandafter\ttl@b\csname ttlp@#1\endcsname{%
      \xdef\ttl@key@page{/\if\ttl@b oodd\else even\fi}%
   }{%
      \xdef\ttl@key@page{/\ifodd\c@page odd\else even\fi}%
	  \@@warning{Unsynchronized `#1' title on page \thepage}}}

%___
\else

\let\ttlp@write\@gobble
\def\ttlp@fetch#1{\gdef\ttl@key@page{/odd}}

%___
\fi

\endinput
