%%
%% This is file `thalie.sty',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% thalie.dtx  (with options: `package')
%% Copyright 2013 Louis Paternault
%% 
%% This work 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 2005/12/01 or later.
%% 
%% This work has the LPPL maintenance status `maintained'.
%% 
%% The Current Maintainer of this work is Louis Paternault
%% 
%% This work consists of the files thalie.dtx and thalie.ins
%% and the derived file thalie.sty.
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{thalie}
    [2013/06/08 v0.5 A package to typeset drama plays]
\RequirePackage{ifthen}
\RequirePackage{etoolbox}
\RequirePackage{suffix}
\RequirePackage{longtable}
\RequirePackage{xspace}
\RequirePackage{pgfopts}

\pgfkeys{
  % Character style
  /THALIE/.cd,
  characterstyle/.value required,
  characterstyle/.default=simple,
  characterstyle/.is choice,
  characterstyle/bold/.code=\def\@characterstyle{bold},
  characterstyle/center/.code=\def\@characterstyle{center},
  characterstyle/margin/.code=\def\@characterstyle{margin},
  characterstyle/simple/.code=\def\@characterstyle{simple},
  characterstyle,
}

\pgfkeys{
  % play style
  /THALIE/.cd,
  playstyle/.value required,
  playstyle/.default=box,
  playstyle/.is choice,
  playstyle/center/.code=\def\@playstyle{center},
  playstyle/bigcenter/.code=\def\@playstyle{bigcenter},
  playstyle/box/.code=\def\@playstyle{box},
  playstyle/custom/.code=\def\@playstyle{custom},
  playstyle,
}
\pgfkeys{
  % act style
  /THALIE/.cd,
  actstyle/.value required,
  actstyle/.default=bigcenter,
  actstyle/.is choice,
  actstyle/center/.code=\def\@actstyle{center},
  actstyle/bigcenter/.code=\def\@actstyle{bigcenter},
  actstyle/box/.code=\def\@actstyle{box},
  actstyle/custom/.code=\def\@actstyle{custom},
  actstyle,
}
\pgfkeys{
  % scene style
  /THALIE/.cd,
  scenestyle/.value required,
  scenestyle/.default=center,
  scenestyle/.is choice,
  scenestyle/center/.code=\def\@scenestyle{center},
  scenestyle/bigcenter/.code=\def\@scenestyle{bigcenter},
  scenestyle/box/.code=\def\@scenestyle{box},
  scenestyle/custom/.code=\def\@scenestyle{custom},
  scenestyle,
}

\pgfkeys{
  % play level
  /THALIE/.cd,
  playlevel/.value required,
  playlevel/.default=chapter,
  playlevel/.store in=\@playlevel,
  playlevel,
}
\pgfkeys{
  % act level
  /THALIE/.cd,
  actlevel/.value required,
  actlevel/.default=section,
  actlevel/.store in=\@actlevel,
  actlevel,
}
\pgfkeys{
  % scene level
  /THALIE/.cd,
  scenelevel/.value required,
  scenelevel/.default=subsection,
  scenelevel/.store in=\@scenelevel,
  scenelevel,
}
\pgfkeys{
  % interlude level
  /THALIE/.cd,
  interludelevel/.value required,
  interludelevel/.default=act,
  interludelevel/.is choice,
  interludelevel/play/.code=\def\@interludelevel{play},
  interludelevel/act/.code=\def\@interludelevel{act},
  interludelevel/scene/.code=\def\@interludelevel{scene},
  interludelevel,
}

\ProcessPgfPackageOptions{/THALIE}
\newcommand{\playname}{Play}
\newcommand{\actname}{Act}
\newcommand{\scenename}{Scene}
\newcommand{\interludename}{Interlude}
\newcommand{\curtainname}{Curtain}
\newcommand{\pausename}{Pause}
\@ifpackageloaded{babel}{
    \addto\captionsfrench{%
      \renewcommand{\playname}{Pi\`ece}
      \renewcommand{\actname}{Acte}
      \renewcommand{\scenename}{Sc\`ene}
      \renewcommand{\interludename}{Interm\`ede}
      \renewcommand{\curtainname}{Rideau}
      \renewcommand{\pausename}{Pause}
    }
    \addto\captionsenglish{%
      % Useless: these are the default...
    }
}{}
\newcommand{\playmark}[1]{%
  \markboth{\MakeUppercase{#1}}{}%
}
\newcommand{\actmark}[1]{%
  \markright{\MakeUppercase{#1}}%
}
\newcommand{\scenemark}[1]{%
}
\newcounter{play}
\renewcommand{\theplay}{\arabic{play}}
\newcounter{act}[play]
\renewcommand{\theact}{\Roman{act}}
\newcounter{scene}[act]
\renewcommand{\thescene}{\arabic{scene}}
\newcommand{\@displaytitle}[3]{
  % Arguments:
  % - Style
  % - Label (none = not in toc)
  % - Title
  \ifthenelse{\equal{#1}{center}}{
    \begin{center}
      \textsc{#2}

      #3
    \end{center}
  }{\ifthenelse{\equal{#1}{bigcenter}}{
    \begin{center}
      \Large
      \textsc{#2}

      #3
    \end{center}
  }{\ifthenelse{\equal{#1}{box}}{
    \begin{center}
      \framebox{\begin{minipage}{0.7\textwidth}
      \begin{center}
        \Large \bfseries
      \vspace{0.5em}

      #2
      \ifthenelse{\equal{#3}{} \OR \equal{#2}{}}{}{---}
      #3

      \vspace{0.5em}
    \end{center}
    \end{minipage}}
    \end{center}
    \vspace{1em}
  }{}}}
}
\newcommand\@clearpage[1]{%
  % Clear page if necessary
  \ifthenelse{\equal{#1}{part} \OR \equal{#1}{chapter}}{
    \cleardoublepage
    \thispagestyle{empty}
  }{}
}

\newcommand{\play}[2][]{%
  \refstepcounter{play}
  \ifthenelse{\equal{#1}{}}{
    \def\@short{#2}
  }{
    \def\@short{#1}
  }
  \@clearpage{\@playlevel}
  \playmark{\@short}
  \addcontentsline{toc}{\@playlevel}{\@short}
  \ifthenelse{\equal{\@playstyle}{custom}}{
    \customplay{\theplay}{#2}
  }{
    \@displaytitle{\@playstyle}{}{#2}
  }
}
\WithSuffix\newcommand\play*[1]{%
  \@clearpage{\@playlevel}
  \ifthenelse{\equal{\@playstyle}{custom}}{
    \customplay*{#1}
  }{
    \@displaytitle{\@playstyle}{}{#1}
  }
}
\newcommand{\act}[2][]{%
  \refstepcounter{act}
  \ifthenelse{\equal{#1}{}}{
    \def\@short{#2}
  }{
    \def\@short{#1}
  }
  \ifthenelse{\equal{\@short}{}}{
    \def\@label{\actname{} \theact{}}
  }{
    \def\@label{\actname{} \theact{}: }
  }
  \@clearpage{\@actlevel}
  \actmark{\@label\@short}
  \addcontentsline{toc}{\@actlevel}{\@label\@short}
  \ifthenelse{\equal{\@actstyle}{custom}}{
    \customact{\theact}{#2}
  }{
    \@displaytitle{\@actstyle}{\actname{} \theact}{#2}
  }
}
\WithSuffix\newcommand\act*[1]{%
  \@clearpage{\@actlevel}
  \ifthenelse{\equal{\@actstyle}{custom}}{
    \customact*{#1}
  }{
    \@displaytitle{\@actstyle}{}{#1}
  }
}
\newcommand{\scene}[2][]{%
  \refstepcounter{scene}
  \ifthenelse{\equal{#1}{}}{
    \def\@short{#2}
  }{
    \def\@short{#1}
  }
  \ifthenelse{\equal{\@short}{}}{
    \def\@label{\scenename{} \thescene{}}
  }{
    \def\@label{\scenename{} \thescene{}: }
  }
  \@clearpage{\@scenelevel}
  \scenemark{\@label\@short}
  \addcontentsline{toc}{\@scenelevel}{\@label\@short}
  \ifthenelse{\equal{\@scenestyle}{custom}}{
    \customscene{\thescene}{#2}
  }{
    \@displaytitle{\@scenestyle}{\scenename{} \thescene}{#2}
  }
}
\WithSuffix\newcommand\scene*[1]{%
  \@clearpage{\@scenelevel}
  \ifthenelse{\equal{\@scenestyle}{custom}}{
    \customscene*{#1}
  }{
    \@displaytitle{\@scenestyle}{}{#1}
  }
}
\newcommand{\interlude}[2][]{%
  \ifthenelse{\equal{#1}{}}{
    \def\@short{#2}
  }{
    \def\@short{#1}
  }
  \ifthenelse{\equal{\@short}{}}{
    \def\@label{\interludename{}}
  }{
    \def\@label{\interludename{}: }
  }
  \ifthenelse{\equal{\@interludelevel}{play}}{
    \@clearpage{\@playlevel}
    \playmark{\@label\@short}
    \addcontentsline{toc}{\@playlevel}{\@label\@short}
    \@displaytitle{\@playstyle}{\interludename}{#2}
  }{\ifthenelse{\equal{\@interludelevel}{act}}{
    \@clearpage{\@actlevel}
    \actmark{\@label\@short}
    \addcontentsline{toc}{\@actlevel}{\@label\@short}
    \@displaytitle{\@actstyle}{\interludename}{#2}
  }{% \@interludelevel is scene
    \@clearpage{\@scenelevel}
    \scenemark{\@label\@short}
    \addcontentsline{toc}{\@scenelevel}{\@label\@short}
    \@displaytitle{\@scenestyle}{\interludename}{#2}
  }}
}
\WithSuffix\newcommand\interlude*[1]{%
  \ifthenelse{\equal{\@interludelevel}{play}}{
    \@clearpage{\@playlevel}
    \@displaytitle{\@playstyle}{\interludename}{#1}
  }{\ifthenelse{\equal{\@interludelevel}{act}}{
    \@clearpage{\@actlevel}
    \@displaytitle{\@actstyle}{\interludename}{#1}
  }{% \@interludelevel is scene
    \@clearpage{\@scenelevel}
    \@displaytitle{\@scenestyle}{\interludename}{#1}
  }}
}
\newcommand\curtain{
  \begin{center}
    \sffamily\LARGE\bfseries \textsc{\curtainname}
  \end{center}
}
\newcommand{\@speaks}[2][]{%
  \ifthenelse{\equal{#1}{}}{%
    \speakswithoutdirection{#2}%
  }{%
    \speakswithdirection{#2}{#1}%
  }\xspace%
}
\ifthenelse{\equal{\@characterstyle}{bold}}{%
  % Bold style
  \newcommand\speakswithdirection[2]{%
    \noindent%
    {\bfseries\sffamily #1} \did{#2}{\bfseries\sffamily :}%
  }
  \newcommand\speakswithoutdirection[1]{%
    \noindent%
    {\bfseries\sffamily #1 :}%
  }%
}{}%
\ifthenelse{\equal{\@characterstyle}{center}}{%
  % Center style
  \newcommand\speakswithdirection[2]{%
    \begin{center}%
    \textsc{#1},\\\emph{#2}%
    \end{center}%
  }%
  \newcommand\speakswithoutdirection[1]{%
    \begin{center}%
    \textsc{#1}%
    \end{center}%
  }%
}{}%
\ifthenelse{\equal{\@characterstyle}{simple}}{%
  % Simple style
  \newcommand\speakswithdirection[2]{%
    \medskip%
    \indent\textsc{#1}, \emph{#2} :%
  }%
  \newcommand\speakswithoutdirection[1]{%
    \medskip%
    \indent\textsc{#1} :%
  }%
}{}%
\ifthenelse{\equal{\@characterstyle}{margin}}{%
  % Margin style
  \setlength{\leftskip}{3cm}
  \newcommand\speakswithdirection[2]{%
    \hspace{-3cm} #1 #2
  }
  \newcommand\speakswithoutdirection[1]{%
    \hspace{-3cm} #1
  }%
}{}%
\newcommand{\@dramatis@clear}{}
\provideboolean{@dramatis@hidden}
\newenvironment{dramatis}[1][]{
  \@dramatis@clear{}
  \undef{\@dramatis@clear}
  \undef{\@dramatis@hook}
  \newcommand{\@dramatis@hook}{}
  \ifthenelse{\equal{#1}{hidden}}{
    \setboolean{@dramatis@hidden}{true}
  }{\ifthenelse{\equal{#1}{}}{
    \setboolean{@dramatis@hidden}{false}
    \gappto{\@dramatis@hook}{\begin{longtable}{l}}
  }{
    \ClassError{thalie}{%
      Environment dramatis does not accept option #1.%
    }{
    }
  }
  }
}{%
  \ifthenelse{\boolean{@dramatis@hidden}}{%
  }{%
    \gappto{\@dramatis@hook}{\end{longtable}}
    \@dramatis@hook{}%
  }
}
\newcommand{\@character}[2]{%
  \ifthenelse{\( \equal{#1}{} \) \AND \( \equal{#2}{} \)}{%
    ~%
  }{}%
  \ifthenelse{\( \NOT \equal{#1}{} \) \AND \( \equal{#2}{} \)}{%
    #1%
  }{}%
  \ifthenelse{\( \equal{#1}{} \) \AND \( \NOT \equal{#2}{} \)}{%
    #2%
  }{}%
  \ifthenelse{\( \NOT \equal{#1}{} \) \AND \( \NOT \equal{#2}{} \)}{%
    #1, #2%
  }{}%
  \tabularnewline
}
\newcommand{\characterspace}{%
  \gappto{\@dramatis@hook}{\@character{}{~}}
}
\newenvironment{charactergroup}[1]{%
  \gdef\@groupname{#1}
  \gappto{\@dramatis@hook}{
    \begin{math}\left.
    \begin{tabular}{ll}
  }
}{%
  \gappto{\@dramatis@hook}{
    \end{tabular}
    \right\} \end{math}
  }
  \protected@xappto{\@dramatis@hook}{\@groupname}
  \gappto{\@dramatis@hook}{\tabularnewline}
}
\newcommand{\setcharactername}[2]{%
    \expandafter\gdef\csname#1name\endcsname{%
      #2\xspace%
    }%
    \expandafter\gdef\csname#1\endcsname{%
      \@ifnextchar[{%
        \defcharcommand@with{#2}%
      }{%
        \defcharcommand@without{#2}%
      }%
    }%
    \xappto{\@dramatis@clear}{%
      \global\noexpand\csundef{#1}%
      \global\noexpand\csundef{#1name}%
    }%
}
\newcommand{\@definecharactercommand}[2]{%
  \@ifundefined{#1}{%
  }{%
    \ClassError{thalie}{%
      A command named \@backslashchar#1 already exists. We cannot define a new
      one.%
    }{%
      Choose another command name to introduce character #2's lines.%
    }%
  }%
  \@ifundefined{#1name}{%
  }{%
    \ClassError{thalie}{%
      A command named \@backslashchar#1name already exists. We cannont define a
      new one.%
    }{%
      Choose another command name to introduce character #2's lines, such that
      when a new command is defined by adding "name" to it, it does not
      conflict with an existing one.
    }%
  }%
  \setcharactername{#1}{#2}%
}
\def\defcharcommand@with#1[#2]{\@speaks[#2]{#1}}
\def\defcharcommand@without#1{\@speaks{#1}}
\pgfkeys{
  % Character definition
  /CHARACTER/.is family, /CHARACTER,
  cmd/.value required,
  cmd/.store in=\@cmd,
  drama/.value required,
  drama/.store in=\@drama,
  desc/.value required,
  desc/.store in=\@desc,
}
\newcommand{\character}[2][]{
  \undef{\@drama}
  \undef{\@cmd}
  \undef{\@desc}
  \pgfkeys{/CHARACTER, #1}%
 % Forbidden combinations
  \ifthenelse{
    \( \( \NOT \isundefined{\@cmd} \) \AND \equal{#2}{} \) \OR
    \( \equal{#2}{} \AND \isundefined{\@cmd} \AND \isundefined{\@desc} \AND \isundefined{\@drama} \) \OR
    \( \isundefined{\@cmd} \AND \( \NOT \equal{#2}{} \) \AND \( \NOT \isundefined{\@drama} \) \)
  }{
    \ClassError{thalie}{Invalid character definition.}{All combination of omitted arguments are not allowed. See the documentation for more information}
  }{
    % Defining character command
    \ifthenelse{\( \NOT \isundefined{\@cmd} \) \AND \( \NOT \equal{#2}{} \)}{
      \@definecharactercommand{\@cmd}{#2}
    }{
    }
    \ifthenelse{
      \( \NOT \equal{#2}{} \) \AND \( \NOT \isundefined{\@cmd} \) \AND \( \NOT \isundefined{\@drama} \) \AND \isundefined{\@desc}
    }{
      \ifthenelse{\equal{\@drama}{}}{
      % Hidden character. Nothing added to dramatis personae
      }{
      % Populating dramatis personae
        \protected@xappto{\@dramatis@hook}{\noexpand\@character{\@drama}{}}
      }
    }{
      % Populating dramatis personae
      \@ifundefined{@desc}{\def\@desc{}}{}
      \@ifundefined{@drama}{\def\@drama{#2}}{}
      \protected@xappto{\@dramatis@hook}{\noexpand\@character{\@drama}{\@desc}}
    }
  }
}
\newcommand{\disposablecharacter}[2][]{%
  \@speaks[#1]{#2}%
}
\newcommand{\onstage}[1]{{\centering \emph{#1}\\}}
\newcommand{\did}[1]{\emph{(#1)} }
\newenvironment{dida}{%
  \medskip
  \begin{quote}
  \begin{em}
  }{%
  \end{em}
  \end{quote}
}
\newcommand\pause{\did{\pausename}}
\endinput
%%
%% End of file `thalie.sty'.
