%%
%% This is file `songproj.sty',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% songproj.dtx  (with options: `package')
%% 
%% This is a generated file.
%% 
%% Copyright (C) 2022 by Tanguy Ortolo <tanguy+latex@ortolo.eu>
%% 
%% Redistribution and use in source and binary forms, with or without
%% modification, are permitted provided that the following conditions are met:
%% 1. Redistributions of source code must retain the above copyright notice,
%%    this list of conditions and the following disclaimer.
%% 2. Redistributions in binary form must reproduce the above copyright notice,
%%    this list of conditions and the following disclaimer in the documentation
%%    and/or other materials provided with the distribution.
%% 
%% 3. Neither the name of the copyright holder nor the names of its
%%    contributors may be used to endorse or promote products derived from this
%%    software without specific prior written permission.
%% 
%% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
%% AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
%% IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
%% ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
%% LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
%% CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
%% SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
%% INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
%% CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
%% ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
%% POSSIBILITY OF SUCH DAMAGE.
%% 
\NeedsTeXFormat{LaTeX2e}[2020/10/01]
\ProvidesPackage{songproj}
    [2022/11/07 v1.0.1 Song projection]
\RequirePackage{expl3}
\RequirePackage{xparse}
\RequirePackage{verse}
\ExplSyntaxOn
\bool_new:N \g__sp_song_bool             % are we in a song?
\bool_new:N \g__sp_song_start_bool       % are we at the start of a song?
\bool_new:N \g__sp_refrain_first_bool    % does current song start with the
                                         % refrain?
\int_new:N  \g__sp_stanzas_per_slide_int % number of stanzas to show on each
                                         % slide (1 or 2)
\dim_new:N  \g__sp_linewidth_dim         % length of the longest line in current
                                         % song
\tl_new:N   \g__sp_intro_tl              % current song intro
\tl_new:N   \g__sp_refrain_tl            % current song refrain
\seq_new:N  \g__sp_couplets_seq          % current song couplets
\tl_new:N   \g__sp_final_tl              % current song final
\seq_new:N  \g__sp_couplet_indexes_seq   % indexes of couplets to include
\NewDocumentEnvironment {__sp_refrain} {}
  % The environment opening may be followed by a [length], in fact part of its
  % body, and will appear just after the opening of the verse environment and
  % constitute its optional argument.
  {
    \begin{structureenv}
    \begin{verse}
  }
  {
    \end{verse}
    \end{structureenv}
  }
\NewDocumentEnvironment {__sp_couplet} {}
  % The environment opening may be followed by a [length], in fact part of its
  % body, and will appear just after the opening of the verse environment and
  % constitute its optional argument.
  { \begin{verse} }
  { \end{verse} }
\NewDocumentEnvironment {__sp_special} {}
  % The environment opening may be followed by a [length], in fact part of its
  % body, and will appear just after the opening of the verse environment and
  % constitute its optional argument.
  {
    \begin{em}
    \begin{verse}
  }
  {
    \end{verse}
    \end{em}
  }
\tl_gset:Nn \__sp_song_refrain
  {
    % Do we know the width of the longest song line?
    \dim_compare:nNnTF \g__sp_linewidth_dim {=} {0pt}
      { \begin{__sp_refrain} }
      { \begin{__sp_refrain} [\g__sp_linewidth_dim] }
    \tl_use:N \g__sp_refrain_tl
    \end{__sp_refrain}
  }
\cs_gset:Npn \__sp_song_couplet:n #1
  {
    % Do we know the width of the longest song line?
    \dim_compare:nNnTF \g__sp_linewidth_dim {=} {0pt}
      { \begin{__sp_couplet} }
      { \begin{__sp_couplet} [\g__sp_linewidth_dim] }
    \seq_item:Nn \g__sp_couplets_seq {#1}
    \end{__sp_couplet}
  }
\cs_gset:Npn \__sp_song_couplets:n #1
  {
    \begin{overprint}
    % Loop on all specified couplets
    \int_step_inline:nn
      { \seq_count:N \g__sp_couplet_indexes_seq }
      {
        % Before every #1 lines, i.e. when (##1 - 1) mod #1 == 0),
        % insert an \onslide
        \int_compare:nNnTF
          { \int_mod:nn { \int_eval:n{##1 - 1} } {#1} } { = } { 0 }
          { \onslide<+> }
          { \vskip \stanzaskip }
        \__sp_song_couplet:n { \seq_item:Nn \g__sp_couplet_indexes_seq {##1} }
      }
    \end{overprint}
  }
\tl_gset:Nn \__sp_song_intro
  {
    % Do we know the width of the longest song line?
    \dim_compare:nNnTF \g__sp_linewidth_dim {=} {0pt}
      { \begin{__sp_special} }
      { \begin{__sp_special} [\g__sp_linewidth_dim] }
    \tl_use:N \g__sp_intro_tl
    \end{__sp_special}
  }
\tl_gset:Nn \__sp_song_final
  {
    % Do we know the width of the longest song line?
    \dim_compare:nNnTF \g__sp_linewidth_dim {=} {0pt}
      { \begin{__sp_special} }
      { \begin{__sp_special} [\g__sp_linewidth_dim] }
    \tl_use:N \g__sp_final_tl
    \end{__sp_special}
  }
\tl_gset:Nn \__sp_song
  {
    % Is there a song intro?
    \tl_if_empty:NTF \g__sp_intro_tl
    {}
    {
      \visible<1> {\__sp_song_intro}
      % The combination of overprint with verse that comes next somehow adds
      % extra vertical space that needs to be removed.
      \vskip -\stanzaskip
    }

    \begin{overprint}

    % Does the song begin with the refrain?
    \bool_if:NTF \g__sp_refrain_first_bool
      {
        % If so, print an initial refrain
        \onslide<+>
        \__sp_song_refrain
      }
      {}

    % Is there a refrain?
    \tl_if_empty:NTF \g__sp_refrain_tl
      {
        % No refrain, loop on all specified couplets and insert them
        \seq_map_inline:Nn
          \g__sp_couplet_indexes_seq
          {
            \onslide<+>
            \__sp_song_couplet:n {#1}
          }
      }
      {
        % There is a refrain, loop on all specified couplets and, each time,
        % insert both a couplet and the refrain
        \seq_map_inline:Nn
          \g__sp_couplet_indexes_seq
          {
            \onslide<+>
            \__sp_song_couplet:n {#1}
            \onslide<+>
            \__sp_song_refrain
          }
      }
    \end{overprint}

    % Is there a song final?
    \tl_if_empty:NTF \g__sp_final_tl
    {}
    {
      % Add extra spacing
      \vskip \stanzaskip
      \visible<.> {\__sp_song_final}
    }
  }
\NewDocumentEnvironment {refrain} { +b }
  % The environment opening may be followed by a [length], in fact part of its
  % body, and will appear just after the opening of the __sp_refrain
  % environment and constitute its optional argument.
  {
    % Are we in a song?
    \bool_if:NTF \g__sp_song_bool
      {
        % We are in a song, are we at its start?
        \bool_if:NTF \g__sp_song_start_bool
          {
            % Indicate that we are no longer at the start of the song
            \bool_gset_false:N\g__sp_song_start_bool
            % and that the refrain comes first
            \bool_gset_true:N\g__sp_refrain_first_bool
          }
          {}
        % Anyway, store the refrain
        \tl_gset:Nn \g__sp_refrain_tl {#1}
      }
      {
        % We are not in a song, use __sp_refrain to format the refrain
          \begin{__sp_refrain}
            #1
          \end{__sp_refrain}
      }
  }
  {}
\NewDocumentEnvironment {couplet} { +b }
  % The environment opening may be followed by a [length], in fact part of its
  % body, and will appear just after the opening of the __sp_couplet
  % environment and constitute its optional argument.
  {
    % Are we in a song?
    \bool_if:NTF \g__sp_song_bool
      {
        % Are we at in a song, are we at its start?
        \bool_if:NTF \g__sp_song_start_bool
          {
            % Indicate that we are no longer at the start of the song
            \bool_gset_false:N \g__sp_song_start_bool
            % and that the refrain does not come first
            \bool_gset_false:N \g__sp_refrain_first_bool
          }
          {}
        % Anyway, store this couplet
        \seq_gput_right:Nn \g__sp_couplets_seq { {#1} }
      }
      {
        % We are not in a song, use __sp_couplet to format this couplet
          \begin{__sp_couplet}
            #1
          \end{__sp_couplet}
      }
  }
  {}
\NewDocumentEnvironment {intro} { +b }
  % The environment opening may be followed by a [length], in fact part of its
  % body, and will appear just after the opening of the __sp_special
  % environment and constitute its optional argument.
  {
    % Are we in a song?
    \bool_if:NTF \g__sp_song_bool
      {
        % We are in a song, store its intro
        \tl_gset:Nn \g__sp_intro_tl {#1}
      }
      {
        % We are not in a song, use __sp_special to format the intro
        \begin{__sp_special}
          #1
        \end{__sp_special}
      }
  }
  {}
\NewDocumentEnvironment {final} { +b }
  % The environment opening may be followed by a [length], in fact part of its
  % body, and will appear just after the opening of the __sp_special
  % environment and constitute its optional argument.
  {
    % Are we in a song?
    \bool_if:NTF \g__sp_song_bool
      {
        % We are in a song, store its intro
        \tl_gset:Nn \g__sp_final_tl {#1}
      }
      {
        % We are not in a song, use __sp_special to format the intro
        \begin{__sp_special}
          #1
        \end{__sp_special}
      }
  }
  {}
\NewDocumentCommand {\longest} { m } { \settowidth {\g__sp_linewidth_dim} {#1} }
\NewDocumentEnvironment {song} { m o }
  % {number of stanzas per slide (1 or 2)}
  % [list of couplets to include (defaults to all)]
  {
    % Put arguments into variables with understandable names
    \int_gset_eq:NN {\g__sp_stanzas_per_slide_int} {#1}
    \IfNoValueTF {#2}
      { \seq_gclear:N \g__sp_couplet_indexes_seq }
      { \seq_gset_from_clist:Nn \g__sp_couplet_indexes_seq {#2} }

    % Clear out intro, refrain, couplet, final and longest song line
    \tl_gclear:N \g__sp_intro_tl
    \tl_gclear:N \g__sp_refrain_tl
    \seq_gclear:N \g__sp_couplets_seq
    \tl_gclear:N \g__sp_final_tl
    \dim_zero:N {\g__sp_linewidth_dim}

    % Indicate that we are in a song, and at its start
    \bool_gset_true:N \g__sp_song_bool
    \bool_gset_true:N \g__sp_song_start_bool
  }
  {
    % Have we been given indexes of specific couplets to use?
    \seq_if_empty:NTF \g__sp_couplet_indexes_seq
      {
        % If not, generate it from the list of couplets
        \int_step_inline:nn
          { \seq_count:N \g__sp_couplets_seq }
          { \seq_gput_right:Nn \g__sp_couplet_indexes_seq {##1} }
      }
      {}

    % Now we actually start inserting things into the document.
    % How many stanzas per side did the user request?
    \int_compare:nNnTF \g__sp_stanzas_per_slide_int {>} {1}
      {
        % More than one stanza per slide
        %
        % Is there an intro?
        \tl_if_empty:NTF \g__sp_intro_tl
          {}
          {
            \visible<1> {\__sp_song_intro}
            % Adjust vertical spacing depending on whether the refrain or the
            % couplets follow.
            \bool_if:NTF\g__sp_refrain_first_bool
              {
                % Refrain comes next, add extra space
                \vskip \parsep
              }
              {
                % Couplets come next, the combination of their overprint and
                % verse environment somehow adds extra vertical space that
                % needs to be removed.
                \vskip -\stanzaskip
              }
          }

        % Is there a refrain?
        \tl_if_empty:NTF \g__sp_refrain_tl
          {
            % If there is no refrain, we use \__sp_song_couplets:n to write the
            % couplets, \g__sp_stanzas_per_slide_int at a time.
            \__sp_song_couplets:n { \int_use:N \g__sp_stanzas_per_slide_int }
          }
          {
            % If there is a refrain, we use \__sp_song_refrain to write the
            % refrain and \__sp_song_couplets:n to write overprint with all
            % couplets.

            % Does the song begin with the refrain?
            \bool_if:NTF\g__sp_refrain_first_bool
              {
                \__sp_song_refrain
                \vskip -\stanzaskip
                \__sp_song_couplets:n 1
              }
              {
                \__sp_song_couplets:n 1
                \vskip \stanzaskip
                \__sp_song_refrain
              }
          }

        % Is there a final?
        \tl_if_empty:NTF \g__sp_final_tl
          {}
          {
            % Adjust vertical spacing depending on whether we follow the
            % refrain or the couplets.
            \tl_if_empty:NTF \g__sp_refrain_tl
              {
                % No refrain, we follow the couplets, add extra space
                \vskip \stanzaskip
              }
              {
                % There was a refrain, did it come first?
                \bool_if:NTF \g__sp_refrain_first_bool
                {
                  % Refrain came first, we follow the couplets, add extra space
                  \vskip \stanzaskip
                }
                {
                  % Refrain came last, we follow it, add extra space
                  \vskip \parsep
                }
              }
            \visible<.> {\__sp_song_final}
          }
      }
      {
        % If the user requested one stanza per slide, we use \__sp_song to
        % write the entire song in a single overprint environment.
        \__sp_song
      }
    % Indicate that we are no longer in a song
    \bool_gset_false:N\g__sp_song_bool
  }
\NewDocumentCommand {\inputsong} { m m o }
  {
    \IfNoValueTF {#3}
      { \begin{song} {#2} }
      { \begin{song} {#2} [#3] }
    \input{#1}
    \end{song}
  }
\ExplSyntaxOff
\endinput
%%
%% End of file `songproj.sty'.
