%%% LaTeX macros for processing LilyPond music examples

%%% You can include Lilypond music as a whole, one staff at a time, or
%%% a range of systems.

%%% The following are utilities for handling LilyPond content
%%%
%%% \lilyHeaderField{reference number}{field} -- Define a macro to hold
%%%     the contents of field, if field is defined for the song identified
%%%     by the reference number.  The defined macro is \lilypond<field>
%%%     (e.g. \lilypondtitle), and is defined to be like \relax if the
%%%     field is not defined for the song.  The macro is redefined with
%%%     each use.
%%%
%%% \lilyInputSingle{reference number}{index} -- This macro includes a
%%%     single lilypond staff system.
%%%
%%% \lilyInputRange{reference number}{system count}{first system} -- This
%%%     macro includes {system count} lilypond staff systems starting with
%%%     the first system.  Systems are numbered starting with 1, which
%%%     typically contains the title block.
%%%
%%%     For example, a song has 8 staves, and it titled. Lilypond will
%%%     create 9 eps files lily-reference#-1.eps to
%%%     lily-reference#-9.eps.  The first file contains the typeset
%%%     title block.  The remaining files 2..9 contain the music
%%%     staves.  To include just the staves, use
%%%     \lilyInputRange{reference#}{8}{2}
%%%
%%% \makeHymnalSongTitle{reference number} -- This macro is a fairly
%%%     elaborate and complete example of using the \lilyHeaderField
%%%     macros to create song titles with TOC and index entries for a
%%%     song book.
%%%
%%% The following macros are the main entry points for processing lilypond
%%% music examples, whether staff systems, or whole songs. 
%%%
%%% \lilyInputTexTitledExample{reference number}{system count} -- This
%%%     macro provides a mechansim for using LaTeX to typeset titles, TOC
%%%     and indexing, while including lilypond staff systems for the rest
%%%     of the music.
%%%
%%% \lilyInputPlainExample{reference number}{system count} -- This macro
%%%     allows lilypond to typeset the song title as well as the music
%%%     staves.
%%%
%%% \lilyInputExample{reference number}{system count} -- This macro is the
%%%     one actually called by the lilypondbook-generated code.  It should
%%%     be redefined (using \let) to select one of the above macros, or
%%%     one supplied by the user.  It is permissible to change the
%%%     definition of this macro within the document body, permitting some
%%%     music to be handled one way, and other music to be handled another
%%%     way. 

\makeatletter

% input a header field.  The file contains _just_ the header contents,
% with no additional markup of any kind.  This macro defines
% \lilypond<header>, containing the header field text.
% TODO: handle field markup.  The markup should probably be translated
% by an external program.
\newread\lilyheaderfile
\newcommand{\lilyHeaderField}[2]{%reference#, field
  \IfFileExists{lily-#1.#2}{
    \openin\lilyheaderfile=lily-#1.#2
    \read\lilyheaderfile to\lilyheaderbuffer
    \expandafter\gdef\csname lilypond#2\endcsname{\lilyheaderbuffer}
    \closein\lilyheaderfile
  }
  {
    \expandafter\let\csname lilypond#2\endcsname=\relax
  }
}


% input the whole music system.
\newcommand\lilyInputPlainExample[2]{%reference number, system count(unused)
\parindent 0pt
\catcode`\@=12
\ifx\preLilyPondExample \undefined
  \relax
\else
  \preLilyPondExample
\fi
\def\lilypondbook{}%
\input {lily-#1-systems.tex}
\ifx\postLilyPondExample \undefined
  \relax
\else
  \postLilyPondExample
\fi
}

% input a single staff-system
\newcommand\lilyInputSingle[2]{%% example id, system index
  \ifFileExists{lily-#1-#2.eps}{%
    \includegraphics{lily-#1-#2.eps}%
    \ifx\betweenLilyPondSystem \undefined%
      \linebreak%
    \else%
      \betweenLilyPondSystem{#2}%
    \fi%
  }%
  {\linebreak\centering error: no such file, lily-#1-#2.eps\linebreak}%
}

%% Variables for iteration control
\newcount\lilysystemlimit
\newcount\currentlilysystem
% input a range of lilypond staff systems
\newcommand{\lilyInputRange}[3]{%reference#, # of systems to include, first system
  \currentlilysystem=#3% ID's start with 1
  \lilysystemlimit=#3%
  \advance\lilysystemlimit by#2% so the limit is one past the final id.
  \loop\ifnum\currentlilysystem<\lilysystemlimit%
    \IfFileExists{lily-#1-\number\currentlilysystem.eps}{
      \includegraphics{lily-#1-\number\currentlilysystem.eps}
    }{}
    \ifx\betweenLilyPondSystem \undefined
      \linebreak
    \else
      \betweenLilyPondSystem{\currentlilysystem}
    \fi
  \advance\currentlilysystem by1\repeat
}

% Prototype song header macros.  Each successive song is numbered,
% added to the TOC, and indexed by title.  It could also be indexed by
% composer, meter, etc.
\newcounter{songnumber}% song counter
% contents line aux record
\newcommand{\song}[1]{\addcontentsline{toc}{song}{\arabic{songnumber} #1}}
% contents line format
\newcommand{\l@song}{\@dottedtocline{1}{3em}{3em}}
% Make a title block.
\newcommand\makeHymnalSongTitle[1]{
  % load the fields
  \lilyHeaderField{#1}{dedication}
  \lilyHeaderField{#1}{title}
  \lilyHeaderField{#1}{subtitle}
  \lilyHeaderField{#1}{subsubtitle}
  \lilyHeaderField{#1}{copyright}
  \lilyHeaderField{#1}{poet}
  \lilyHeaderField{#1}{texttranslator}
  \lilyHeaderField{#1}{meter}
  \lilyHeaderField{#1}{composer}
  \lilyHeaderField{#1}{opus}
  \lilyHeaderField{#1}{arranger}
  \lilyHeaderField{#1}{instrument}
  \lilyHeaderField{#1}{piece}
  % arrange the content.  Undefined fields are skipped.
  \begin{center}
    \ifx\lilyponddedication\relax
    \else
      {\normalsize\lilyponddedication\\}
    \fi
    \bfseries
     \ifx\lilypondtitle\relax
     \else
       {\huge\stepcounter{songnumber}\index{\lilypondtitle}
        \ifodd\thepage%
            {\hfill\lilypondtitle\hfill\arabic{songnumber}}%
        \else%
            {\arabic{songnumber}\hfill\lilypondtitle\hspace*{\fill}}%
        \fi
        \song{\lilypondtitle}\\
      }
     \fi
    \ifx\lilypondsubtitle\relax
    \else
      {\Large\lilypondsubtitle\\}
    \fi
    \ifx\lilypondsubsubtitle\relax
    \else
      {\large\lilypondsubsubtitle\\}
    \fi
   \ifx\lilypondcopyright\relax
   \else
     {\normalsize\lilypondcopyright\\}
   \fi
  \end{center}

  \begin{minipage}[t]{0.45\textwidth}
    \ifx\lilypondpoet\relax
    \else
      {\lilypondpoet\\}
    \fi
    \ifx\lilypondtexttranslator\relax
    \else
      {\lilypondtexttranslator\\}
    \fi
    \ifx\lilypondmeter\relax
    \else
      {\lilypondmeter\\}
    \fi
  \end{minipage}
  \hspace{\fill}
  \begin{minipage}[t]{0.45\textwidth}
    \begin{flushright}
      \ifx\lilypondcomposer\relax
      \else
        {\large\normalfont\scshape\lilypondcomposer\\}
      \fi
      \ifx\lilypondopus\relax
      \else
        {\lilypondopus\\}
      \fi
      \ifx\lilypondarranger\relax
      \else
        {\lilypondarranger\\}
      \fi
    \end{flushright}
  \end{minipage}

  \par

  \ifx\lilypondinstrument\relax
  \else
    {\center{\large\lilypondinstrument}
     \par}
  \fi

  \ifx\lilypondpiece\relax
  \else
    {\flushleft{\large\normalfont\scshape\lilypondpiece}
     \par}
  \fi

}

% input the whole music system, but let LaTeX make the title block.
\newcommand\lilyInputTexTitledExample[2]{%reference number, system count
\parindent 0pt
\ifx\preLilyPondExample \undefined
  \relax
\else
  \preLilyPondExample
\fi
\def\lilypondbook{}%
\makeHymnalSongTitle{#1}
\lilyInputRange{#1}{#2}{2}
\ifx\postLilyPondExample \undefined
  \relax
\else
  \postLilyPondExample
\fi
}

% Use plain titling by default.
\let\lilyInputExample=\lilyInputPlainExample

% Source Transformation:
% \lilypondfile[staffsize=24]{BattleBelongsToTheLordBook.ly}
% becomes
% \lilyInputExample{2098660014}{8}

\makeatother

%%% Local Variables: 
%%% mode: latex
%%% TeX-master: t
%%% End: 
