% --------------------------------------------------------------------------
% the LEADSHEETS package
% 
%   typesetting leadsheets and songbooks
%
% --------------------------------------------------------------------------
% Clemens Niederberger
% E-Mail: contact@mychemistry.eu
% --------------------------------------------------------------------------
% Copyright 2014--2019 Clemens Niederberger
%
% 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 Clemens Niederberger.
% --------------------------------------------------------------------------
\LeadsheetsExplLibrary{templates}
  {2014/08/10 template mechanism for song titles and verses}

\msg_new:nnn {leadsheets} {unknown-template}
  { Unknown~ #1~ template~ `#2'~ \msg_line_context: }

\tl_new:N    \l__leadsheets_songtitle_template_tl
\prop_new:N  \l__leadsheets_songtitle_template_prop

% \NewDocumentCommand \songtitle {} { \leadsheets_songtitle: }

% the following is shamelessly adapted from the `needspace' package by
% Peter Wilson and Herries Press:
\cs_new_protected:Npn \__leadsheets_needspace:n #1
  {
    \group_begin:
      \dim_set:Nn \l__leadsheets_tmpa_dim { #1 }
      \skip_vertical:n { \c_zero_dim + \l__leadsheets_tmpa_dim }
      \tex_penalty:D -100
      \skip_vertical:n { \c_zero_dim - \l__leadsheets_tmpa_dim }
      \skip_vertical:N \l__leadsheets_tmpa_dim
      \tex_penalty:D 9999
      \skip_vertical:n { - \l__leadsheets_tmpa_dim }
      \skip_vertical:N \c_zero_dim
    \group_end:
  }

\cs_new_protected:Npn \__leadsheets_songtitle:
  {
    \__leadsheets_use_songtitle_template:V \l__leadsheets_songtitle_template_tl
    \__leadsheets_needspace:n { 3\baselineskip }
  }

\cs_new_protected:Npn \__leadsheets_use_songtitle_template:n #1
  {
    \prop_get:NnNTF \l__leadsheets_songtitle_template_prop
      {#1}
      \l__leadsheets_tmpa_tl
      { \l__leadsheets_tmpa_tl }
      { \msg_error:nnnn {leadsheets} {unknown-template} {songtitle} {#1} }
  }
\cs_generate_variant:Nn \__leadsheets_use_songtitle_template:n { V }

\cs_new_protected:Npn \leadsheets_define_songtitle_template:nn #1#2
  { \prop_put:Nnn \l__leadsheets_songtitle_template_prop {#1} {#2} }


\NewDocumentCommand \definesongtitletemplate {m+m}
  { \leadsheets_define_songtitle_template:nn {#1} {#2} }

\NewDocumentCommand \expandcode {+m} { \use:x {#1} }


% verse-type templates
\prop_new:N \l__leadsheets_verse_begin_template_prop
\prop_new:N \l__leadsheets_verse_end_template_prop

% #1: template name
% #2: begin code
% #3: end code
\cs_new_protected:Npn \leadsheets_define_verse_template:nnn #1#2#3
  {
    \prop_put:Nnn \l__leadsheets_verse_begin_template_prop {#1} {#2}
    \prop_put:Nnn \l__leadsheets_verse_end_template_prop {#1} {#3}
  }

\cs_new_protected:Npn \leadsheets_use_verse_template:nn #1#2
  {
    \exp_args:Nnc
    \prop_get:cVNTF {l__leadsheets_verse_#1_template_prop}
      {l__leadsheets_#2_template_tl}
      \l__leadsheets_tmpa_tl
      { \tl_use:N \l__leadsheets_tmpa_tl }
      {
        \msg_error:nnxx {leadsheets} {unknown-template}
          {verse-type}
          {\use:c{l__leadsheets_#2_template_tl}}
      }
  }

\DeclareExpandableDocumentCommand \ifobeylines {}
  { \bool_if:NTF \l__leadsheets_obey_lines_bool }

\NewDocumentCommand \defineversetypetemplate {m+m+m}
  { \leadsheets_define_verse_template:nnn {#1} {#2} {#3} }

\file_input_stop:
