% # Copyright (C) 2002 by Monty Taylor
% # Translate ply files into LaTeX.

% #     This program is free software; you can redistribute it and/or modify
% #     it under the terms of the GNU General Public License as published by
% #     the Free Software Foundation; either version 2 of the License, or
% #     (at your option) any later version.

% #     This program is distributed in the hope that it will be useful,
% #     but WITHOUT ANY WARRANTY; without even the implied warranty of
% #     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
% #     GNU General Public License for more details.

% #     You should have received a copy of the GNU General Public License
% #     along with this program; if not, write to the Free Software
% #     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

%%%%%%%
%
% Things that are changed between Monty-Style and Standard American:
%
% \addname
% \line
% And the \raggedright invocation at the bottom



\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{plywood}[2002/05/07 A LaTeX package for typesetting plays]
\usepackage{fancyheadings}
\usepackage[dvips]{color}

\definecolor{grey}{gray}{0.40}

\newdimen{\nameindent}
\newdimen{\directindent}
\newdimen{\stageindent}

\newlength{\actheadwidth}
\setlength{\actheadwidth}{\textwidth}
\addtolength{\actheadwidth}{0.7in}

\addtolength{\marginparwidth}{0.5in}
\addtolength{\headwidth}{\marginparsep}
\addtolength{\headwidth}{\marginparwidth}
%%%%%
%
% Override LaTeX's mark system to support line break markings
%

% LaTeX uses TeX's \mark to store two marks, the left and the right mark.
% We extend that system to store a third mark - the break mark.  The \line
% macro sets the break mark _around_ a line of dialog, and then clears it
% after it is done.  So the break mark will only be set at output time if
% a line of dialog has been split across pages.  A new output routine tests
% for this and places a symbol on the output page indicating the line break.

% This method for detecting line breaks by setting marks and testing for
% them in the output routine is based on Exercise 23.6 in The TeXbook by
% Donald E. Knuth.

\makeatletter

% Support for extracting any of the three marks

\long\def\@firstofthree#1#2#3{#1}
\long\def\@secondofthree#1#2#3{#2}
%\long\def\@thirdofthree#1#2#3{#3}   % this definition already exists

\let\@leftmark\@firstofthree
\let\@rightmark\@secondofthree
\let\@breakmark\@thirdofthree

% New definition of markboth and markright, to preserve the 
% third mark (line break)

\def\markboth#1#2{{\let\protect\@unexpandable@protect
     \let\label\relax \let\index\relax \let\glossary\relax
     \expandafter\@markboth\@themark
     {#1}{#2}\mark{\@themark}}\if@nobreak\ifvmode\nobreak\fi\fi}
\def\@markboth#1#2#3#4#5{\gdef\@themark{{#4}{#5}{#3}}}
\def\markright#1{{\let\protect\@unexpandable@protect
     \let\label\relax \let\index\relax \let\glossary\relax
     \expandafter\@markright\@themark
     {#1}\mark{\@themark}}\if@nobreak\ifvmode\nobreak\fi\fi}
\def\@markright#1#2#3#4{\gdef\@themark{{#1}{#4}{#3}}}

% New macro to set the line break mark

\def\markbreak#1{{\let\protect\@unexpandable@protect
     \let\label\relax \let\index\relax \let\glossary\relax
     \expandafter\@markbreak\@themark
     {#1}\mark{\@themark}}\if@nobreak\ifvmode\nobreak\fi\fi}
\def\@markbreak#1#2#3#4{\gdef\@themark{{#1}{#2}{#4}}}
\def\leftmark{\expandafter\@leftmark\botmark{}{}{}}
\def\rightmark{\expandafter\@rightmark\firstmark{}{}{}}
\def\@themark{{}{}{}}

% Override the output routine.

\edef\oldoutput{\the\output}%

% This adds a symbol to the output page

\def\addtell{%
\setbox255=\vbox{\unvbox255\kern-6pt\hbox to 0pt{\kern\textwidth\kern1em$\triangleright$}}%
}%

% This is the definition to use for Monty-style plays

%% \def\addname#1{%
%% \setbox1=\hbox{\textsc{X}}% Find capheight for intelligent guessing
%% \setbox0=\hbox{\textsc{[#1]  }}%
%% \setlength{\nameoutdent}{\wd0}%
%% \addtolength{\nameoutdent}{\namegap}%
%% \setbox255=\vbox{\kern3pt\kern\ht1\kern-\ht0\vbox to 0pt{\hbox to 0pt{\kern-\nameoutdent\hbox to \nameoutdent{\textcolor{grey}{\copy0}\hfil}}}\kern-3pt\kern-\ht1\kern\ht0\kern-\dp0\nointerlineskip\unvbox255}%
%% }%

% This is the definition for Standard American style

\def\namelook#1{%
\begin{rmfamily}\begin{mdseries}\begin{scshape}#1\end{scshape}\end{mdseries}\end{rmfamily}}

\long\def\addname#1{%
\setbox255=\vbox{\leftskip=0pt\hskip\nameindent{\namelook{\textcolor{grey}{[#1]}}}\endgraf\unvbox255}%
}%

\newcommand{\thetitle}{\@title}
\newcommand{\theauthor}{\@author}

\long\def\playtitlepage#1{%
{\begin{titlepage}
\vspace*{\stretch{1}}
\begin{center}
\large\@title\\%[5mm]
\vspace{18pt}
#1\\
by\\
\vspace{6pt}
\@author\\
\vspace*{\stretch{2}}
\textsc{\today}
\markbreak{}
\end{center}
\end{titlepage}}%
}

\newcommand{\ps@newact}{%
  \addtolength{\headwidth}{\marginparsep}
  \addtolength{\headwidth}{\marginparwidth}
  \renewcommand{\@oddhead}{\kern-6pt\hbox to 0pt{\kern\actheadwidth\kern1em{\bfseries\Roman{act}-\arabic{scene}-\thepage}}}
  \renewcommand{\@evenhead}{\@oddhead}
  \renewcommand{\@evenfoot}{}
  \renewcommand{\@oddfoot}{\@evenfoot}}

\makeatother

\newenvironment{CharList}
{%
\begin{titlepage}
  \hskip\nameindent\textsc{Characters}\markbreak{Characters}\par\nopagebreak
  \vspace{12pt}
  \begin{tabular}{@{}p{1.2in}p{3.4in}}
}
{\end{tabular}\markbreak{}\end{titlepage}}

% This checks to see if the output page has a line broken across pages

\def\contcheck#1#2#3\end{\def\next{#3}\ifx\next\empty\else\addtell\fi}%

% Check if top of the page has the second part of a continued line

\def\secondcontcheck#1#2#3\end{\def\next{#3}\ifx\next\empty\else\addname{#3}\fi}%

% The topmark on the first page isn't going to match our {}{}{} pattern,
% So we check to see if it is void first

\def\safemarkcheck#1\end{\def\next{#1}\ifx\next\empty\else\expandafter\secondcontcheck#1\end\fi}%

% The new output routine
\output={\expandafter\contcheck\botmark\end\expandafter\safemarkcheck\topmark\end\oldoutput}%

%
% End support for line break markings
%
%%%%%


% Needed for \line version 3
\newlength{\nameoutdent}

% Set to whatever length you want the gap to be
\newlength{\namegap}
\setlength{\namegap}{0pt}

% A line of dialog:

% Monty-Style:

%%\def\line#1#2
%%{
%%  \setbox0=\hbox{\textsc{#1  }}
%%  \setlength{\nameoutdent}{\wd0}
%%  \addtolength{\nameoutdent}{\namegap}
%%  \leavevmode\kern-\nameoutdent\hbox to \nameoutdent{\box0\hfil}\markbreak{#1}#2
%%  \par
%%  \markbreak{}
%%}

% Standard American:

\long\def\line#1#2
{
\pagebreak[2]
\hskip\nameindent\textsc{#1}\markbreak{#1}\par\nopagebreak
#2\par
\markbreak{}
\addvspace{12pt}
}

\long\def\realdirection#1{
\begin{rmfamily}\begin{itshape}\begin{small}{\hangindent=0em\hangafter=1#1}\end{small}\end{itshape}\end{rmfamily}}

%%\long\def\direction#1{
%%\begin{sffamily}\begin{small}\textcolor{grey}{#1}\end{small}\end{sffamily}}

\long\def\direction#1{
{\par\leftskip=\directindent\realdirection{#1}\par}}

\long\def\longdirection#1{
{\par\leftskip=\directindent\begin{small}\begin{itshape}{\hangindent=0em\hangafter=1#1}\end{itshape}\end{small}\par}
\addvspace{12pt}
}

\long\def\stagedirection#1#2{
{\par\leftskip=\stageindent\begin{small}{\parindent=-4em\textsc{#1}\textrm{#2}}\end{small}\par}\vspace{12pt}}

\newcounter{act}
\newcounter{scene}[act]
\newcounter{beat}[scene]

\newcommand{\beat}{%\stepcounter{beat}
%\mbox{}\marginpar{\raggedleft\arabic{scene}-\arabic{beat}}
\begin{picture}(720,12)(0,8)
\linethickness{0.5pt}
\put(0,5){\qbezier(-200,0)(360,0)(720,0)}
\end{picture}
\\}

\renewcommand{\thescene}{Scene \arabic{scene}}
\newcommand{\newscene}{
\pagebreak\realnewscene}

\newcommand{\realnewscene}{
{\hspace{\nameindent}\stepcounter{scene}\textsc{\thescene}}\vspace{18pt}}

\renewcommand{\theact}{Act \Roman{act}
}
\newcommand{\newact}{
{\pagebreak\hspace{\nameindent}\stepcounter{act}\thispagestyle{newact}\textsc{\theact}}

\vspace{12pt}\realnewscene}

\def\ppar{\addvspace{\baselineskip}\goodbreak}

\long\def\lyric#1#2
{
\pagebreak[2]
\hskip\nameindent\textsc{#1}\markbreak{#1}\par\nopagebreak
\bgroup
\reallyric{#2\par}
\markbreak{}\egroup\ppar}

\long\def\reallyric#1
{
\def\pskip{\hspace*{1em}}
\def\dpskip{\hspace*{2em}}
%\addvspace{12pt plus20pt}\goodbreak
\parindent=0pt
\leftskip=0.5in
\obeylines
\hangindent=2em
\hangafter=1
%\let\oldpar=\par
%\def\mypar{\hangindent=2em\hangafter=1\oldpar}
%\let\par=\mypar\obeylines
\begin{sffamily}
#1
\addvspace{12pt}
\end{sffamily}
}

\newcommand{\HRule}{\rule{\linewidth}{0.1mm}}

\long\def\titlepg{%
{\begin{titlepage}
\vspace*{\stretch{1}}
\begin{flushright}
\HRule\\
  \Huge\thetitle\\[5mm]
  \Large\theauthor\\
\end{flushright}
\HRule
\vspace*{\stretch{2}}
\markbreak{}
\begin{center}
 \large\textsc{\today}
\end{center}
\end{titlepage}
}}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% End definitions, begin customization:
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\pagestyle{fancyplain}

\nameindent=0.475\textwidth
%2.125in
\directindent=0.5\nameindent
\stageindent=0.75\nameindent

\parindent 0pt

\setlength{\namegap}{0pt}

\setcounter{secnumdepth}{-2}


\lhead[\bfseries\thepage]   {\bfseries\thetitle}
\chead[\bfseries\leftmark]  {\bfseries\leftmark}
\rhead[\bfseries\rightmark] {\bfseries\Roman{act}-\arabic{scene}-\thepage}
\cfoot[]{}



%%%%
%
% Ragged right is only for Standard American
%

%\raggedright


