% $Id: fiwi.cbx,v 1.3 2015/06/30 Simon Spiegel

\ProvidesFile{fiwi.cbx}
[\abx@cbxid $Id: fiwi.cbx,v 1.3 2015/06/30 spiegel $]


\ExecuteBibliographyOptions{labeldate=true,autocite=inline,firstinits=false,uniquename=full,uniquelist=true,ibidtracker=constrict}


\RequireBibliographyStyle{fiwi}  
  
  

\newbool{cbx@bool}
\newbool{cbx:parens}
\newbool{cbx:loccit}
\providetoggle{citefullfilm} 
\newcommand*{\fullcitefilm}{\settoggle{citefullfilm}{true}}
\DeclareBibliographyOption{fullcitefilm}[false]{\settoggle{citefullfilm}{#1}}

\providetoggle{citecompletefilm}
\newcommand*{\completecitefilm}{\settoggle{citecompletefilm}{true}}

\DeclareBibliographyOption{citefilm}[normal]{%
\ifstrequal{#1}{normal}
{}
{}
\ifstrequal{#1}{full}
{\settoggle{citefullfilm}{true}}
{}
\ifstrequal{#1}{complete}
{\settoggle{citecompletefilm}{true}}
{}
}%


\renewcommand*{\nameyeardelim}{\addspace}
\renewcommand*{\bibindexnamedelimi}{\,}
\renewcommand*{\bibindexinitdelim}{\,}

\providetoggle{xindy}
\DeclareBibliographyOption{xindy}[true]{\settoggle{xindy}{#1}}

\DeclareBibliographyOption{ibidpage}[true]{%
  \ifstrequal{#1}{true}
    {\ExecuteBibliographyOptions{loccittracker=constrict}}
    {\ExecuteBibliographyOptions{loccittracker=false}}}


\DeclareFieldFormat{citeyear}{\mkbibparens{#1}}
\DeclareFieldFormat{citetitle:incollection}{\emph{#1}\isdot}
\DeclareFieldFormat[misc,video,movie]{citetitle}{\film{#1}}
\DeclareIndexFieldFormat[movie,misc,video]{indextitle}{%
  \usebibmacro{index:filmtitle}{\index}{#1}}
\DeclareIndexNameFormat{default}{%
  \iftoggle{xindy}{\usebibmacro{xindy:name}{\index}{#1}{#3}{#5}{#7}}
  {\usebibmacro{index:name}{\index}{#1}{#3}{#5}{#7}}}

\DeclareNameFormat{labelname}{%
  \ifcase\value{uniquename}%
    \usebibmacro{name:last}{#1}{#3}{#5}{#7}%
  \or
    \iftoggle{citeprefix}
      {\usebibmacro{name:first-last}{#1}{#4}{#5}{#8}}
      {\usebibmacro{name:first-last}{#1}{#4}{#6}{#8}}%
  \or
    \usebibmacro{name:first-last}{#1}{#3}{#5}{#7}%
  \fi
  \usebibmacro{name:andothers}}

\newbibmacro*{cite:postnote}{%
  \ifbool{cbx:loccit}
    {}
    {\usebibmacro{postnote}}}

\renewbibmacro*{name:last}[4]{%
  \iftoggle{citeprefix}
    {\usebibmacro{name:delim}{#3#1}%
     \usebibmacro{name:hook}{#3#1}%
     \ifblank{#3}
       {}
       {\ifcapital
          {\mkbibnameprefix{\MakeCapital{#3\isdot}}}
          {\mkbibnameprefix{#3\isdot}}%
        \ifpunctmark{'}{}{\bibnamedelimc}}}
    {\ifuseprefix
    {\usebibmacro{name:delim}{#3#1}%
     \usebibmacro{name:hook}{#3#1}%
     \ifblank{#3}
       {}
       {\ifcapital
          {\mkbibnameprefix{\MakeCapital{#3\isdot}}}
          {\mkbibnameprefix{#3\isdot}}%
        \ifpunctmark{'}{}{\bibnamedelimc}}}
    {\usebibmacro{name:delim}{#1}%
     \usebibmacro{name:hook}{#1}}}%
  \mkbibnamelast{#1}}%  
  
  


\newcommand*{\xindy}{\toggletrue{xindy}}% Indizierung für xindy ohne actual-Teil

\newbibmacro*{index:filmtitle}[2]{%
  \iftoggle{xindy}{\usebibmacro{xindy:field}}{\usebibmacro{index:field}}{#1}{\thefield{indexsorttitle}}{\film{#2}}}  
  
\renewbibmacro*{index:title}[2]{%
  \iftoggle{xindy}{\usebibmacro{xindy:field}}{\usebibmacro{index:field}}{#1}{\thefield{indexsorttitle}}{\emph{#2}}}

\renewcommand{\postnotedelim}{\addcolon\addspace}
\renewcommand{\sqspace}{\addnbthinspace}

%\newrobustcmd*{\Textcites}{\bibsentence\textcites}
\newcommand{\citets}{\textcites}
\newrobustcmd*{\Citets}{\Textcites}
\newcommand{\citealts}{\cites}
\newcommand{\Citealts}{\Cites}

\newbibmacro*{cite:ibid}{%
  \printtext[bibhyperref]{\bibstring[\mkibid]{ibidem}}%
  \ifloccit
    {\global\booltrue{cbx:loccit}}
    {}}

\renewbibmacro*{citeindex}{%
  \ifciteindex{%
  \indexnames[indexname]{labelname}}
  {}}%

% Zitierbefehle für Filme und Serien

\newbibmacro*{cite:tv}{%
\ifciteseen{\ifboolexpr{ test {\ifnameundef{director}}
and test {\iffieldundef{maintitle}}}
{\iffieldundef{extratitle}
{\usebibmacro{cite:filmtitle}}
{\usebibmacro{cite:filmtitle}\addspace\mkbibparens{\printdate}}}
{\usebibmacro{cite:filmtitle}}}
{\usebibmacro{cite:filmtitle}
\addspace\mkbibparens{%
\iflistundef{organization}
{\printlist{location}}
{\printlist{organization}\addcomma}
\printdate}}%
}


\newbibmacro*{cite:serial}{%
\ifciteseen
{\iffieldundef{shorttitle}
  {\printtext[bibhyperref]{\printfield[film]{title}}}  
  {\printtext[bibhyperref]{\printfield[film]{shorttitle}}}}
{\printtext[bibhyperref]{\printfield[film]{title}
\addspace\mkbibparens{%
\iflistundef{organization}
{\printlist{location}}
{\printlist{organization}}
\printfield{year}}}}%
}

\newbibmacro*{cite:filmtitle}
{\iffieldundef{maintitle}
{\printtext[bibhyperref]{\printfield[film]{title}}}%
{\printtext[bibhyperref]{\printfield[tvmaintitle]{maintitle}%
\printfield[film]{title}}}%
}

\newbibmacro*{cite:serialfull}{%
\printtext[bibhyperref]{\printfield[film]{title}
\addspace\mkbibparens{%
\printlist{location}\iflistundef{organization}{}{\addcomma\addspace\printlist{organization}}\addspace\printfield[]{year}\addcomma\addspace%
\printnames[director]{director}\unspace}}}%

\newbibmacro*{cite:film}{%
\iftoggle{citefullfilm}%
   {\usebibmacro{cite:film:directorcountry}}
  {\iftoggle{citecompletefilm}%
   {\usebibmacro{cite:film:directorcountrytranstitle}}
   {\usebibmacro{cite:filmtitle}%
   \addspace\printfield[citeyear]{year}}}}
   
\newbibmacro*{cite:episode}
{\ifboolexpr{not test {\iffieldequalstr{entrysubtype}{tv}}
and not test {\ifciteseen}
and test {\iftoggle{citefullfilm}}}
{\usebibmacro{cite:film:directorcountry}}
{\ifboolexpr{not test {\iffieldequalstr{entrysubtype}{tv}}
and not test {\ifciteseen}
and test {\iftoggle{citecompletefilm}}}
{\usebibmacro{cite:film:directorcountrytranstitle}}
{\printtext[bibhyperref]{\printfield[film]{title}}%
\ifciteseen{}{%
\iffieldequalstr{entrysubtype}{tv}
{\addspace\mkbibparens{%
\iflistundef{organization}
{\printlist{location}}%
{\printlist{organization}\addcomma}
\printdate}}{%
\addspace\mkbibparens{%
\iflistundef{organization}
{\printlist{location}}
{\printlist{organization}}
\printfield{year}}}}}%
}%
}   
   
\newbibmacro*{cite:film:seen}{%
  	\iffieldundef{shorttitle}
  	{\usebibmacro{cite:filmtitle}}  
  	{\printtext[bibhyperref]{\printfield[film]{shorttitle}}}}   

\newbibmacro*{cite:film:countryear}{%
  \printtext[bibhyperref]{\printfield[film]{title}
   \addspace\mkbibparens{\printlist{location}\addspace\printfield{year}}}}

\newbibmacro*{cite:film:directorcountrytranstitle}{% Film mit Regisseur, Land und dt. Titel
  \printtext[bibhyperref]{\printfield[film]{title}}%
   \addspace\printtext[citeyear]%
   {\printfield[plain]{subtitle}\setunit*{\addcomma\addspace}%
   \printnames[director]{director}\addcomma\addthinspace\printlist{location}%
   \addspace\printfield{year}}
   }

\newbibmacro*{cite:film:directorcountry}{% Film mit Regisseur
  \printtext[bibhyperref]{\printfield[film]{title}}%
   \addspace\mkbibparens{%
   \printnames{director}%
   \addcomma\addthinspace\printlist{location}\addspace\printfield{year}}}
   
    
\newbibmacro*{cite}{%
  \global\boolfalse{cbx:loccit}%
  \iffieldundef{shorthand}
    {\ifthenelse{\ifciteibid\AND\NOT\iffirstonpage}
       {\usebibmacro{cite:ibid}}
       {\ifthenelse{\ifnameundef{labelname}\OR\iffieldundef{labelyear}}
          {\usebibmacro{cite:label}%
           \setunit{\addspace}}
          {\printnames{labelname}%
           \setunit{\nameyeardelim}}%
        \usebibmacro{cite:labelyear+extrayear}}}
    {\usebibmacro{cite:shorthand}}}    
    
    
\newbibmacro*{textcite}{%
  \global\boolfalse{cbx:loccit}%
  \ifnameundef{labelname}
    {\iffieldundef{shorthand}
       {\usebibmacro{cite:label}%
        \setunit{%
          \global\booltrue{cbx:parens}%
          \addspace\bibopenparen}%
        \ifnumequal{\value{citecount}}{1}
          {\usebibmacro{prenote}}
          {}%
        \usebibmacro{cite:labelyear+extrayear}}
       {\usebibmacro{cite:shorthand}}}
    {\printnames{labelname}%
     \setunit{%
       \global\booltrue{cbx:parens}%
       \addspace\bibopenparen}%
     \ifnumequal{\value{citecount}}{1}
       {\usebibmacro{prenote}}
       {}%
     \iffieldundef{shorthand}
       {\ifthenelse{\ifciteibid\AND\NOT\iffirstonpage}
          {\usebibmacro{cite:ibid}}
          {\iffieldundef{labelyear}
             {\usebibmacro{cite:label}}
             {\usebibmacro{cite:labelyear+extrayear}}}}
       {\usebibmacro{cite:shorthand}}}}
    
\newbibmacro*{citeyear}{%
  \global\boolfalse{cbx:loccit}%
  \iffieldundef{shorthand}
    {\ifthenelse{\ifciteibid\AND\NOT\iffirstonpage}
       {\usebibmacro{cite:ibid}}
       {\iffieldundef{labelyear}
          {\usebibmacro{cite:label}}
          {\usebibmacro{cite:labelyear+extrayear}}}}
    {\usebibmacro{cite:shorthand}}}    

\newbibmacro*{cite:shorthand}{%
  \printtext[bibhyperref]{\printfield{shorthand}}}

%\newbibmacro*{cite:label}{%
%  \ifnameundef{labelname}
%    {\BibliographyWarning{Missing author/editor+year or label}}
%    {\printtext[bibhyperref]{\printnames{labelname}}}}

\newbibmacro*{cite:label}{%
  \iffieldundef{label}
    {\printtext[bibhyperref]{\printfield[citetitle]{labeltitle}}}
    {\printtext[bibhyperref]{\printfield{label}}}}

\newbibmacro*{cite:labelyear+extrayear}{%
  \printtext[bibhyperref]{%
    \iffieldundef{year}%
    {\bibstring[\mkbibbrackets]{nodate}\printfield{extrayear}}%
    {\printfield{labelyear}}%
    \printfield{extrayear}}}    
    
\newbibmacro*{xindy:name}[5]{%
  \begingroup
  \ifuseprefix
    {\edef\theindexentry{%
       \unexpanded{#1}{%
         \ifblank{#4}{}{\unexpanded{#4} }%
         \unexpanded{#2}%
         \ifblank{#5}{}{ \unexpanded{#5}}%
         \ifblank{#3}{}{, \unexpanded{#3}}}}}
    {\edef\theindexentry{%
       \unexpanded{#1}{%
         \unexpanded{#2}%
         \ifblank{#5}{}{ \unexpanded{#5}}%
         \ifblank{#3#4}{}{,}%
         \ifblank{#3}{}{ \unexpanded{#3}}%
         \ifblank{#4}{}{ \unexpanded{#4}}}}}%
  \theindexentry
  \endgroup}
   
   
\newbibmacro*{xindy:field}[3]{%
  \begingroup
  \protected@edef\theindexentry{%
    \unexpanded{#1}{#3}}%
  \theindexentry
  \endgroup}

\newbibmacro*{textcite:postnote}{%
  \ifthenelse{\iffieldundef{postnote}\OR\boolean{cbx:loccit}}
    {\ifbool{cbx:parens}
       {\bibcloseparen}
       {}}
    {\ifbool{cbx:parens}
       {\postnotedelim}
       {\addspace\bibopenparen}%
     \printfield{postnote}\bibcloseparen}}  
  
\DeclareCiteCommand{\fullcite}
  {{}%
  \usebibmacro{prenote}}%
  {\usedriver%
     {}
     {\thefield{entrytype}}}
  {\multicitedelim\unspace}% aus unklaren Gruenden hat es hier ein Space zu viel
  {\usebibmacro{cite:postnote}} 
 

\DeclareCiteCommand{\cite}
  {\usebibmacro{prenote}}
  {\usebibmacro{citeindex}%
   \usebibmacro{cite}}
  {\multicitedelim}
  {\usebibmacro{cite:postnote}}

\DeclareCiteCommand*{\cite}
  {\usebibmacro{prenote}}
  {\usebibmacro{citeindex}%
   \usebibmacro{citeyear}}
  {\multicitedelim}
  {\usebibmacro{cite:postnote}}

\DeclareCiteCommand{\parencite}[\mkbibparens]
  {\usebibmacro{prenote}}
  {\usebibmacro{citeindex}%
   \usebibmacro{cite}}
  {\multicitedelim}
  {\usebibmacro{cite:postnote}}

\DeclareCiteCommand*{\parencite}[\mkbibparens]
  {\usebibmacro{prenote}}
  {\usebibmacro{citeindex}%
   \usebibmacro{citeyear}}
  {\multicitedelim}
  {\usebibmacro{cite:postnote}}

\DeclareCiteCommand{\inparencite}[]
 {}
 {\usebibmacro{citeindex}%
  \printnames{labelname}
  \bibopenparen%
  \usebibmacro{prenote}%
  \usebibmacro{citeyear}}%
 {\multicitedelim}%
 {\usebibmacro{cite:postnote}
 \unspace\bibcloseparen}

\DeclareCiteCommand{\citetitle}
  {\citetrackerfalse
   \pagetrackerfalse
   \usebibmacro{prenote}}
  {\indexfield{indextitle}%
   \printtext[bibhyperref]{\printfield[citetitle]{labeltitle}}}
  {\multicitedelim}
  {\usebibmacro{cite:postnote}}

\DeclareCiteCommand{\citeyear}
  {\usebibmacro{prenote}}
  {\usebibmacro{citeyear}}
  {\multicitedelim}
  {\usebibmacro{cite:postnote}}

\DeclareCiteCommand{\citeurl}
  {\usebibmacro{prenote}}
  {\printfield[citeurl]{url}}
  {\multicitedelim}
  {\usebibmacro{cite:postnote}}

\DeclareCiteCommand{\citealtnoibidem}
  {\usebibmacro{prenote}}
  {\usebibmacro{citeindex}%
   \usebibmacro{cite}}
  {\multicitedelim}
  {\usebibmacro{cite:postnote}}

\DeclareCiteCommand{\citetnoibidem}
  {\usebibmacro{prenote}}
  {\usebibmacro{citeindex}%
   \usebibmacro{textcite}}
  {\multicitedelim}
  {\iffieldundef{postnote}
     {\ifbool{cbx@bool}
        {\bibcloseparen}
	{}}
     {\ifbool{cbx@bool}
        {\postnotedelim}
	{\addspace\bibopenparen}%
      \printfield{postnote}\bibcloseparen}}

\DeclareCiteCommand{\footcite}[\mkbibfootnote]
  {\bibsentence
   \usebibmacro{prenote}}
  {\usebibmacro{citeindex}%
   \usebibmacro{textcite}}
  {\multicitedelim}
  {\iffieldundef{postnote}
     {\ifbool{cbx@bool}
        {\bibcloseparen}
	{}}
     {\ifbool{cbx@bool}
        {\postnotedelim}
	{\addspace\bibopenparen}%
      \printfield{postnote}\bibcloseparen}}

\DeclareCiteCommand{\textcite}
  {\boolfalse{cbx:parens}}
  {\usebibmacro{citeindex}%
   \iffirstcitekey
     {\setcounter{textcitetotal}{1}}
     {\stepcounter{textcitetotal}%
      \textcitedelim}%
   \usebibmacro{textcite}}
  {\ifbool{cbx:parens}
     {\bibcloseparen\global\boolfalse{cbx:parens}}
     {}}
  {\usebibmacro{textcite:postnote}}


      
\DeclareCiteCommand{\citeepisode}
  {\usebibmacro{prenote}}
  {\indexfield{indextitle}%
  \usebibmacro{cite:episode}}
  {\multicitedelim}
  {\usebibmacro{cite:postnote}}
      
\DeclareCiteCommand{\citefilm}
  {\usebibmacro{prenote}}
  {\indexfield{indextitle}%
  \iffieldequalstr{entrysubtype}{serial}%
  {\usebibmacro{cite:serial}}%
  {\iffieldequalstr{entrysubtype}{tv}%
  {\usebibmacro{cite:tv}}{\ifciteseen{\usebibmacro{cite:film:seen}}
   {\usebibmacro{cite:film}}}}}
  {\multicitedelim}
  {\usebibmacro{cite:postnote}}

\DeclareCiteCommand{\citecfilm}
  {\usebibmacro{prenote}}
  {\indexfield{indextitle}%
   \usebibmacro{cite:film:countryear}}
  {\multicitedelim}
  {\usebibmacro{cite:postnote}}

\DeclareCiteCommand{\citefullfilm}
  {\usebibmacro{prenote}}
  {\indexfield{indextitle}%
  \iffieldequalstr{entrysubtype}{serial}
  {\usebibmacro{cite:serialfull}}
  {\usebibmacro{cite:film}}}
  {\multicitedelim}
  {\usebibmacro{cite:postnote}}

\DeclareCiteCommand{\citefilmnoindex}
  {\usebibmacro{prenote}}
  {\usebibmacro{cite:film}}
  {\multicitedelim}
  {\usebibmacro{cite:postnote}}

\endinput
