%
% Poetry utilities
% For use with poetry package
%
% Reuben Thomas   started 18/12/00
%

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{poem}
    [2020/09/07 v1.15 Poetry utilities]

\RequirePackage{poetry}
\RequirePackage{version}
\RequirePackage{makecmds}
\RequirePackage{ifthen}

% Switches
\newif\ifemptypoemtitlespace\emptypoemtitlespacefalse

% Options
\DeclareOption{nonotes}{\excludeversion{notes}\renewcommand{\footnote}[1]{}}
\DeclareOption{noauthor}{\renewcommand{\poemauthor}[1]{}}
\DeclareOption{notitle}{\renewcommand{\poemtitle}[1]{}}
\DeclareOption{nodate}{\renewcommand{\poemdate}[1]{}}
\DeclareOption{noepigraph}{\renewcommand{\poemepigraph}[1]{}}
\DeclareOption{nodedication}{\renewcommand{\poemdedication}[1]{}}
\DeclareOption{noinfo}{\renewcommand{\poeminfo}[1]{}}
\DeclareOption{poemlines}{\poem@addtohook{\obeypoemlines}}
\DeclareOption{emptypoemtitlespace}{\emptypoemtitlespacetrue}

% Typesetting parameters
% FIXME: allow poem lines to be centred without requiring \centerline
\renewcommand{\poemline}{} % turn off line numbering
\newcommand{\tab}{\hspace*{4ex}}
\frenchspacing

% Change commands
\renewcommand{\poemtitle}[1]{%
    \nl%
    \nonumber%
    \hfill\makebox[0mm]{\normalfont\bfseries#1}\hfill%
    \\*[\bigskipamount]}
\renewcommand{\poemauthor}[1]{\poeminfo{#1}}
\renewcommand{\stanzaname}[1]{{\bfseries#1}}
\renewcommand{\labelstanza}{\arabic{stanza}}

% New commands
% FIXME: \poeminfo lines affect the poem’s indentation
\newcommand{\emptypoemtitle}{\ifemptypoemtitlespace\poemtitle{}\fi}
\newboolean{po@minfo}
\newcommand{\poeminfo}[1]{%
  \ifthenelse{\boolean{po@minfo}}{\nl*[-1.5ex]}{\nl*[\bigskipamount]}%
  \nonumber\hfill\normalfont\itshape#1\\
  \setboolean{po@minfo}{true}}
\newcommand{\poemdate}[1]{\poeminfo{#1}}
\provideenvironment{notes}[1][4ex]%
    {\vspace{#1}\em\small\noindent}%
    {}
\newenvironment{epigr@ph}[1][2ex]%
    {\vspace{#1}\em\noindent}%
    {}
\newcommand{\poemepigr@ph}[2][2ex]{\begin{epigr@ph}[#1]\hfill\makebox[0mm][c]{#2}\hfill\end{epigr@ph}}
% FIXME: Use xparse and \NewDocumentCommand to avoid duplication of default optional argument
\newcommand{\poemepigraph}[2][2ex]{\poemepigr@ph[#1]{#2}}
\newcommand{\poemdedication}[2][2ex]{\poemepigr@ph[#1]{#2}}
\newenvironment{poem}{\setboolean{po@minfo}{false}\begin{xpoem}}{\end{xpoem}}

% Process options at end to allow commands to be overridden
\ProcessOptions
