% +--------------------------------------------------+
% | Typeset titlesec.tex to get the documentation.   |
% +--------------------------------------------------+
%
% Copyright (c) 1998-2002 by Javier Bezos.
% All Rights Reserved.
%
% This file is part of the titlesec distribution release 2.5
% -----------------------------------------------------------
%
% 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.

\ProvidesFile{ttlkeys.def}[2002/03/27]

% 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