%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% SONG.STY %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%
%%% Author: Stephen Savitzky
%%%
%%% Song environment:
%%%	\begin{song}{title}			% starts on a new page
%%%
%%%	  \subtitle{subtitle, set in smaller type}
%%%	  \notice{centered copyright notice}
%%%             \CcByNcSa			% Creative Commons license
%%%	  \dedication{centered dedication}
%%%	  \description{description}		% defines \theDescription
%%%		(The description is used only for liner notes, etc.; it
%%%		 does not appear in typeset lyrics)
%%%	  \tags{tag1,tag2...}			% defines \theTags
%%%       \category{tag1,tag2...}		% obsolete version of tags
%%%	  \key{C\; (A \capo 3)}			% defines \theKey
%%%	  \timing{mm:ss}			% defines \theTiming
%%%	  \created{yymmdd}			% defines \theCrDate
%%%	  \cvsid{$Id ...}			% defines \theCVSid
%%%	  \lyrics{lyricist}			% specifies theLyricist
%%%	  \music{composer}			% specifies theComposer
%%%	  \arranger{arranger}			% specifies theArranger
%%%	  \performer{performer}			% specifies thePerformer
%%%	  \credits{credits}			% specifies theCredits
%%%       \performance{performance notes}       % specifies perf. notes
%%%
%%%	  \begin{note}
%%%		left-justified, paragraphed text set in smaller type
%%%	  \end{note}
%%%
%%%	  lines of song separated by newline
%%%	  \; indented single line with no extra space around it.
%%%	  chords like th[A]is
%%%	  \verse or \\
%%%	  between verses
%%%	  \spoken{spoken parts in slanted type}
%%%	  \begin{chorus}
%%%	    indented chorus with verse spacing
%%%	  \end{chorus}
%%%       NOTE:  you can use ``refrain'' instead of chorus; it's technically
%%%              incorrect, but I've been using it.
%%%	  \begin{bridge}
%%%	      indented (2em) bridge with verse spacing
%%%	  \end{bridge}
%%%	  \inset{1-line instructions in italics, indented 1em}
%%%	\end{song}
%%%	\tailnote{Note text at the bottom of the page}
%%%	\link{url}{anchor text}
%%%
%%%	  \vfiller{n}		insert n lines of filler to balance
%%%				columns in twocolumns environment
%%%
%%%	[chord]			set in a 0-width raised box
%%%	\chord{set as chord but not raised}
%%%	  Chords are set in math mode (to get superscripts and special chars.)
%%%	  The following chord elements are defined:
%%%	    \flat 	modified b
%%%	    \sharp	modified #
%%%	    \dim 	subscript circle
%%%	    \aug	superscript plus
%%%	    \maj	{\rm maj}
%%%	    \sus4	{\rm sus}4	(also 2) 
%%%		sometimes changed to superscript to save horizontal space
%%%	    \min	prints as m
%%%	    \add	prints as &
%%%	    \up		prints as double uparrow, e.g. F(D\up3)
%%%		used to indicate barre chords and the like.
%%%	    *		indicates chords with B or E string raised
%%%	    \capo	{\it capo} used in, e.g.  \key{F\; (D\capo3)}
%%%	    \;		em space
%%%
%%%
%%%  \file{songfile}	input a song file; set \theFile to the filename
%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\usepackage{etoolbox}           % additional programming constructs.

%%%
%%% Informational Components (notice, etc.)
%%%
%%%    If we are making four-page songs with title pages, these are output
%%%    on the title page by \makesongtitle.  Otherwise, they are typeset
%%%    immediately, on the first page of the lyrics.  This is controlled
%%%    by the ``looseleaf'' style option.
%%%
\def\notice#1{\global\def\theCopyrightNotice{#1}
  \ifcompact\begin{center}\footnotesize #1\end{center}\fi}
\def\dedication#1{\global\def\theDedication{#1}
  \ifcompact\begin{center}\footnotesize #1\end{center}\fi}
\def\subtitle#1{\global\def\theSubtitle{#1}
  \ifcompact\begin{center}\bf #1\end{center}\fi}
\def\license#1{\global\def\theLicense{#1}
  \ifcompact\begin{center}\small #1\end{center}\fi}

\global\def\theCopyrightNotice{}
\global\def\theDedication{}
\global\def\theSubtitle{}
\global\def\theLicense{}

%%% Macros for use in the copyright notice.

\def\SomeRightsReserved{Some Rights Reserved\footnote{\CcByNcSa}}
\def\CcByNcSa{This work is licensed under a Creative Commons
Attribution-Noncommercial-Share Alike 4.0 License.}  
\def\ttto#1{{\sc ttto:} {\em #1\/}}

\def\link#1#2{#2\footnote{$<$#1$>$}}

%%% define macros and global variables for the rest of the metadata.
%%% Note that some aren't used by LaTeX; they're for other formats, too.

\def\timing#1{\global\def\theTiming{#1}}\def\theTiming{}
\def\tags#1{\global\def\theTags{#1}}\def\theTags{}
\def\description#1{\global\def\theDescription{#1}}\def\theDescription{}
\def\key#1{\global\def\theKey{#1}}\def\theKey{}
\def\created#1{\global\def\theCrDate{#1}}\def\theCrDate{}
\def\cvsid#1{\global\def\theCVSid{#1}}\def\theCVSid{}
\def\lyrics#1{\global\def\theLyricist{#1}}\def\theLyricist{}
\def\music#1{\global\def\theComposer{#1}}\def\theComposer{}
\def\arranger#1{\global\def\theArranger{#1}}\def\theArranger{}
\def\performer#1{\global\def\thePerformer{#1}}\def\thePerformer{}
\def\credits#1{\global\def\theCredits{#1}}\def\theCredits{}
\def\performance#1{\global\def\thePerformance{#1}}\def\thePerformance{}

\global\def\theSong{}

%%%
%%% note, headnote, tailnote
%%%
\def\note{\quotation\footnotesize %
	\catcode`\^^M=5 % ascii return is end-line
}
\let\endnote=\endquotation
\long\def\headnote#1{\begin{note}#1\end{note}}
\long\def\tailnote#1{\vfill\begin{note}#1\end{note}}

%%%
%%% \defaultsonglength{L/S} Set the default song length.
%%%
%%%   Short (single-page) songs can start on either even or odd (left or
%%%   right) pages when typesetting a songbook in compact mode.

%%%   Can be overridden at any time between songs; the
%%%   default is long.  
%%%
\def\defaultsonglength#1{\def\song@default@length{#1}}
\defaultsonglength{L}
\def\short@length{S}

%%% The song environment: \begin[long/short]{song}
%%%
%%%   The optional parameter is either [L] or [S]; the default is L (long)
%%%   but that can be changed with \defaultsonglength.  If a song is short and
%%%   we are in compact mode, we don't care whether it starts on an odd or
%%%   even page.
%%%    
\newenvironment{song}[2][]{
  \global\def\theSong{#2}
  % We want to allow the optional parameter to be either [short] or [long],
  % with a default that can be set in the songbook.  The idea is that a short
  % song only occupies a single page, so if we're in compact mode we don't
  % care whether it's on an odd or even page.
  \def\@empty{}
  \def\song@length{#1}
  \ifx\song@length\empty
    \if\song@default@length\short@length
       \def\song@length{S}
    \else
       \def\song@length{L}
    \fi
  \fi
  \if@twoside
    \ifcompact                  % compact formats
      \ifx\short@length\song@length
         \clearpage             % compact: short songs can start anywhere
      \else
         \cleardoublepageleft   % compact: longer songs start on even (left) page
      \fi
    \else
      \cleardoublepage          % Looseleaf format
    \fi
  \else
    \clearpage                  % onesided (compact)
  \fi
  % tags, creation date, file, etc. want to be put out at the beginning of the lyrics.
  \global\def\theTags{}\global\def\theTiming{}
  \global\def\theCrDate{}\global\def\theKey{}
  \begingroup
  \parskip 0pt plus 1pt
  \parindent 0pt
  \let\\=\verse
  \obeylines\catcode`\[=13}{% \end{song}
  \endgroup
  % Clear optional metadata, e.g. performance and description.  Doing it here
  % lets us define them outside the song environment, which we might want to
  % do if the song is being included in an album or setlist. 

  \global\def\theCopyrightNotice{}
  \global\def\theDedication{}
  \global\def\theSubtitle{}
  \global\def\theLicense{}
}

%%%
%%% Parts of songs
%%%
%%%    refrain (indent 1em)
%%%    chorus  (indent 1em)
%%%    bridge  (indent 2em)
%%%    indent  (indent 6em)
%%%    Indent  (indent 8em)
%%%
\newenvironment{refrain}{\list{}{\listparindent 1em
 \itemindent\listparindent
 \labelwidth 0pt
 \rightmargin 0pt
 \parsep 0pt plus 1pt}\item[]}{\endlist}

\newenvironment{chorus}{\list{}{\listparindent 1em
 \itemindent\listparindent
 \labelwidth 0pt
 \rightmargin 0pt
 \parsep 0pt plus 1pt}\item[]}{\endlist}

\newenvironment{bridge}{\list{}{\listparindent 2em
 \itemindent\listparindent
 \labelwidth 0pt
 \rightmargin 0pt
 \parsep 0pt plus 1pt}\item[]}{\endlist}

\renewenvironment{indent}{\list{}{\listparindent 6em
 \itemindent\listparindent
 \labelwidth 0pt
 \rightmargin 0pt
 \parsep 0pt plus 1pt}\item[]}{\endlist}

\newenvironment{Indent}{\list{}{\listparindent 8em
 \itemindent\listparindent
 \labelwidth 0pt
 \rightmargin 0pt
 \parsep 0pt plus 1pt}\item[]}{\endlist}

%%%
%%% Annotations for songs
%%%

\def\inset#1{{\hskip1em\it #1}}
\def\inst#1{{\it #1}}
\def\singer#1{{\hskip-.5em\tt #1}} %\hskip-1em seems excessive
\def\spoken#1{{\sl #1}}
\def\verse{\par\medskip\par}

\def\;{\hskip 1em}  % used for indentation, esp; to continue a line.
                    % many uses can be replaced by begin{refrain}


\def\vfiller#1{\vbox to #1\baselineskip{}}


%%%
%%% Replace the old twocolumns environment with multicol
%%%
\usepackage{multicol}

\newenvironment{twocolumns}{
  \begin{multicols}{2}}{\end{multicols}}

%%% Formats:
%%%    Broadside:  oneside,  compact -- default for publishing on the web
%%%    Looseleaf:  twoside, !compact -- default for single songs
%%%                \makesongtitle used to create (odd) title page.
%%%                Song occupies pages 2 and 3; tailnotes on 4.
%%%    Songbook:   twoside,  compact; Song starts on even page
%%%                Tailnotes skipped to keep page count down.
%%%
%%%    In the compact formats the subtitle, copyright notice, description,
%%%    and dedication are printed at the start of the song.  Most other
%%%    metadata:  performance notes, headnote, etc., are skipped.  Tail
%%%    notes are skipped in Songbook mode to keep the page count down;
%%%    they are included in Broadside.

\newif\ifcompact
\if@twoside
  \compactfalse                 % two-sided songs default to Looseleaf
\else
  \compacttrue                  % one-sided songs default to Broadside
\fi

%%% default for compact is false if two-sided, true otherwise
\if@twoside\compactfalse\else\compacttrue\fi


%%% \cleardoublepageleft:  like \cleardoublepage except that it
%%%    goes to an even (left-side) page instead of an odd one.
%%%
\newcommand*{\cleardoublepageleft}{%
  \clearpage
  \if@twoside
    \ifodd\c@page
      \hbox{}\newpage
      \if@twocolumn
        \hbox{}\newpage
      \fi
    \fi
  \fi
}

%%% \makesongtitle -- put out song title page if not compact
%%%
\newcommand{\makesongtitle}{
  \ifcompact % nothing to do if we're in compact mode
  \else
      \begin{center}
        \vfiller{10}        
        {\Huge \theSong}\par        
        {\large \theSubtitle}\par        
        \theCopyrightNotice\par
        \clearpage
        \theSubtitle\par
        \theCopyrightNotice
      \end{center}
  \fi
}
  
%%%
%%% Table of contents is supposed to look like a song
%%%
\def\l@Song#1#2{#1\par}
\def\tableofcontents{\begin{song}{LIST OF SONGS}
 \@starttoc{toc}\end{song}}

%%%
%%% \file{filename} Input a file containing a song. 
%%%	We force a pagebreak here to ensure that the previous song
%%%	doesn't pick up the new filename in its header and footer.
%%%     Songs now consist only of a song environment, so we don't have to
%%%     turn off \documentstyle and \begin{document}...\end{document}.
%%%
%%%     Reset the footnote counter so that footnote numbers in the songbook
%%%     will match the ones in the looseleaf format.  They're mostly used
%%%     as links to a CC license; having them increase without limit is
%%%     silly.
%%%
\def\file#1{\newpage\begingroup
  \def\theFile{#1}
  \def\thisPage{\thepage}
  \setcounter{footnote}{0}
  \input{#1}
  \addcontentsline{toc}{Song}{\hbox{\hbox to 2em{\hss\thisPage}%
      \hbox to 3em{\hss\theTiming}\;\hbox{\theSong\hss}}}
\endgroup}
%
% \theFile defaults to \jobname, so that it doesn't have to be provided in
% the common case where we're just formatting a single song.
%
\def\theFile{\jobname}

%%%
%%% \comment{stuff}	Ignore some stuff.
%%%	A crude form of extended, nestable comment
%%%	frequently useful for cancelling out lists of songs.
%%%

\long\def\comment#1{}

%%%
%%% Chords.
%%%	Note that chords are set in math mode
%%%
\def\chordfont{\sf}
\def\chord #1{{\footnotesize\strut$\chordfont #1$}}
\def\@chord #1{\makebox[0pt][l]{\raisebox{.9em}{\chord{#1}}}}

\def\dim{_\circ}
\def\aug{^+}
\def\maj{\mbox{maj}}
\def\min{\mbox{m}}
\def\sus{\mbox{sus}}
\def\capo{\mbox{\ \it capo\ }}
\def\drop{\mbox{\ \it drop\ }}
\def\up{\Uparrow}
\def\add{\&}
%\def\sharp{\#}
%\def\flat{b}

\catcode`\[=13
\def[#1]{\@chord{#1}}
\catcode`\[=12


