%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                   %
%      Made by Gabriel Ruprecht     %
%                                   %
% This work is published under the  %
% LaTeX project public license 1.3  %
%                                   %
%       Package version 1.1.0       %
%            2025-01-20             %
%                                   %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\ProvidesPackage{liederbuch-listofsongs}[version 1.1.0]

% Default names; Has Babel support in liederbuch.sty
\def\listofsongsname{List of Songs}
\def\LBsongComposer{composer}%
\def\LBsongLyricist{lyricist}%
\def\LBsongEditor{editor}%
\def\LBsongYear{year}%
\def\LBsongCopyright{copyright}%
\def\LBsongEditorialOffice{editorial office}%
\def\LBsongEditon{edition}%
\def\LBsongPrint{print}%


% The command, that outputs the list of songs. Mostly taken from listoffigures
\def\listofsongs{%
    \newif\ifGFM@LB@chapterExists%
    \GFM@LB@chapterExiststrue%
    %
    \ifx\undefined\chapter\relax%
        \GFM@LB@chapterExistsfalse%
    \fi%
    %
    \if@twocolumn%
        \@restonecoltrue%
        \onecolumn %
    \else%
        \@restonecolfalse%
    \fi%
    \ifGFM@LB@chapterExists\relax%
    %starred/unstarred?%
        \chapter*{\listofsongsname}%
    \else%
        \section*{\listofsongsname}%
    \fi%
    \@mkboth{\MakeUppercase \listofsongsname }{\MakeUppercase \listofsongsname }%
    %Print nothing for one run, when the los style is changed
    \ifx\listofsongsstyleprevrun\listofsongsstylevalue%
    \else%
    \def\l@song##1##2{}%
    \fi%
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    %Most important line is here:
    \@starttoc{los}%
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    \if@restonecol%
        \twocolumn%
    \fi%
}


% This is needed to check, if the same style is used than last
% time. If not, there will be no output for one compilation.
\AtEndDocument{
\write\@auxout{\noexpand\gdef\noexpand\listofsongsstyleprevrun{\listofsongsstylevalue}}
}


% For checking, if a valid style had been selected.
\newif\iflistofsongsstyleExists\relax
\listofsongsstyleExistsfalse

% Selects the style. Contains the default styles.
\def\listofsongsstyle#1{%
    \edef\listofsongsstylevalue{#1}%
    \def\GFM@compareB{twolines}%
    \ifx\listofsongsstylevalue\GFM@compareB\relax%
    \listofsongsstyleTwolines\relax%
    \listofsongsstyleExiststrue%
    \fi%
    \def\GFM@compareB{semitwolines}%
    \ifx\listofsongsstylevalue\GFM@compareB\relax%
    \listofsongsstyleSemiTwolines\relax%
    \listofsongsstyleExiststrue%
    \fi%
    \def\GFM@compareB{simple}%
    \ifx\listofsongsstylevalue\GFM@compareB\relax%
    \listofsongsstyleSimple\relax%
    \listofsongsstyleExiststrue%
    \fi%
    \def\GFM@compareB{simple with number}%
    \ifx\listofsongsstylevalue\GFM@compareB\relax%
    \listofsongsstyleSimplewithnumber\relax%
    \listofsongsstyleExiststrue%
    \fi%
    \def\GFM@compareB{simple with document number}%
    \ifx\listofsongsstylevalue\GFM@compareB\relax%
    \listofsongsstyleSimplewithdocumentnumber\relax%
    \listofsongsstyleExiststrue%
    \fi%
    % TODO: create style registering and 
    \iflistofsongsstyleExists%
    \else%
    \PackageError{liederbuch}{liederbuch-listofsongs.sty, line 102: You tried to call a style for the list of songs, that is not defined.}{Did you make a typo? The default style is 'simple' and will automatically be used, if you don't use the command listofsongsstyle.}%
    \fi%
    \listofsongsstyleExistsfalse%
}

\@onlypreamble\listofsongsstyle

% Default style twolines:
% The definion of this is 90% taken from l@figure:
\def\listofsongsstyleTwolines{%
    \newdimen\GFM@LB@losDimA\relax%
    \newdimen\GFM@LB@losDimB\relax%
    \GFM@LB@losDimA=1.5em\relax%
    \GFM@LB@losDimB=2.3em\relax%
    %
%    \let\l@song\l@figure
    \def\l@song##1##2{%
        \show##1\relax%
        \wlog{This is a marker}
%        \show##2\relax%
        \def\GFM@LB@extract####1\csname ####2@title\endcsname####3{
            \def\GFM@LB@expandedNamePre{####1}%
            \def\GFM@LB@expandedName{####2}%
            \def\GFM@LB@expandedNameAfter{####3}%
        }%
        \expandafter\GFM@LB@extract##1\relax%
%
%%        \def\split####1={%
% %           \def\GFM@LB@expandedName{####1}%
%  %      }\split##1@title=\relax%
        \show\GFM@LB@expandedName%
        \vskip 0.5em plus 0.05em minus 0.02em\relax%
        \vskip \z@ \@plus .2\p@ %
        {\leftskip \GFM@LB@losDimA\relax\rightskip \@tocrmarg \parfillskip -\rightskip \parindent \GFM@LB@losDimA\relax \@afterindenttrue \interlinepenalty \@M \leavevmode \@tempdima \GFM@LB@losDimB\relax \advance \leftskip \@tempdima \null \nobreak \hskip -\leftskip {\csname \GFM@LB@expandedName @title\endcsname }\newline\null%
        \expandafter\ifx\csname \GFM@LB@expandedName @composer\endcsname \empty%
        \else%
        \csname \GFM@LB@expandedName @composer\endcsname\ -- \fi\csname%
        \GFM@LB@expandedName @lyricist\endcsname%
        \nobreak \leaders \hbox {$\m@th \mkern \@dotsep mu\hbox {.}\mkern \@dotsep mu$}\hfill \nobreak \hb@xt@ \@pnumwidth {\hfil \normalfont \normalcolor ##2\kern -\p@ \kern \p@ }\par }%
    }%
    \def\GFM@LB@addcontentsline{%
        \addcontentsline{los}{song}{\unexpanded{\unexpanded{\csname}}\GFM@LB@expandedName @title\unexpanded{\unexpanded{\endcsname}}}%
    }%
}

% Default style semitwolines
% The definion of this is 90% taken from l@figure:
\def\listofsongsstyleSemiTwolines{%
    \newdimen\GFM@LB@losDimA\relax%
    \newdimen\GFM@LB@losDimB\relax%
    \GFM@LB@losDimA=1.5em\relax%
    \GFM@LB@losDimB=2.3em\relax%
    %
    \def\l@song##1##2{%
        \def\split####1&####2&####3={%
            \def\GFM@LB@losTitle{####1}%
            \def\GFM@LB@losComposer{####2}%
            \def\GFM@LB@losLyricist{####3}%
        }\split##1=\relax%
        \vskip 0.5em plus 0.05em minus 0.02em\relax%
        \vskip \z@ \@plus .2\p@ %
        {\leftskip \GFM@LB@losDimA\relax\rightskip \@tocrmarg \parfillskip -\rightskip \parindent \GFM@LB@losDimA\relax \@afterindenttrue \interlinepenalty \@M \leavevmode \@tempdima \GFM@LB@losDimB\relax \advance \leftskip \@tempdima \null \nobreak \hskip -\leftskip {\GFM@LB@losTitle}\ifx\GFM@LB@losComposer\empty\else\newline\null\GFM@LB@losComposer\ -- \fi\GFM@LB@losLyricist%
        \nobreak \leaders \hbox {$\m@th \mkern \@dotsep mu\hbox {.}\mkern \@dotsep mu$}\hfill \nobreak \hb@xt@ \@pnumwidth {\hfil \normalfont \normalcolor ##2\kern -\p@ \kern \p@ }\par }%
    }%
    \def\GFM@LB@addcontentsline{%
        \addcontentsline{los}{song}{%
            \csname \GFM@LB@expandedName @title\endcsname &\csname \GFM@LB@expandedName @composer\endcsname &\csname \GFM@LB@expandedName @lyricist\endcsname%
        }%
    }%
}


% Default style simple:
\def\listofsongsstyleSimple{%
    \let\l@song\l@figure\relax%
    \def\GFM@LB@addcontentsline{%
        \addcontentsline{los}{song}{\csname \GFM@LB@expandedName @title\endcsname }%
    }%
}

% Default style simple with number
\def\listofsongsstyleSimplewithnumber{%
    \let\l@song\l@figure\relax%
    \def\GFM@LB@addcontentsline{%
        \addcontentsline{los}{song}{\numberline{\GFM@LB@number }\csname \GFM@LB@expandedName @title\endcsname }%
    }%
}

% Default style simple with document number
\def\listofsongsstyleSimplewithdocumentnumber{%
    \let\l@song\l@figure\relax%
    \def\GFM@LB@addcontentsline{%
        \addcontentsline{los}{song}{\numberline {\arabic{liedcounter}}\csname \GFM@LB@expandedName @title\endcsname }%
    }%
}

%default value:
\listofsongsstyle{simple}
