% Tobias Weh, 2011

% THANKS to Audrey form TeX.SX for building the
% quoted-in-cite-code. His code is marked with the
% pseudo tag <audrey>.
% See http://tex.stackexchange.com/q/27964/4918
\ProvidesFile{musuos.bbx}[2011/09/10 v1.0 Musikwissenschaften Uni Osnabrueck]

% Ausgagsstiel w�hlen
\RequireBibliographyStyle{authortitle}

% Auf die neue Sprachdatei umleiten
\DeclareLanguageMapping{german}{german-musuos}

% Ausgabemacros anpassen
\renewbibmacro*{editorstrg}{%
   \iffieldundef{editortype}
     {\ifthenelse{\value{editor}>1\OR\ifandothers{editor}}
        {\mkbibparens{\bibstring{editors}}}
        {\mkbibparens{\bibstring{editor}}}}
}

\newbibmacro*{publisher+maintitle+booktitle}{%
  \iflistundef{publisher}
    {\setunit*{\addcomma\space}}
    {\setunit*{\addcolon\space}}%
  \printlist{publisher} \usebibmacro{editorstrg}%
  \setunit{\addcolon\space}%
  \usebibmacro{maintitle+booktitle}%
  \newunit}
  
\renewcommand{\labelnamepunct}{\addcolon\space}

\newbibmacro{opus}{%
\iffieldundef{usera}{}{%
  \iffieldundef{userb}{%
    \newunit{\addcomma\space}{\biblcstring{opus}%
    \addspace\printfield{usera}}
  }{%
    \newunit{\addcomma\space}{\printfield{userb}%
    \addspace\printfield{usera}}%
  }%
}}

\renewbibmacro*{location+date}{%
  \printlist{location}%
  \setunit*{\addcomma\space}%
  \usebibmacro{cite:labelyear+extrayear}%
  \newunit}

\renewbibmacro*{editorstrg}{%
  \ifthenelse{\value{editor}>1\OR\ifandothers{editor}}
  {\mkbibparens{\bibstring{editors}}}
  {\mkbibparens{\bibstring{editor}}}%
}


\renewbibmacro*{editor+othersstrg}{%
  \iffieldundef{editortype}
    {\ifboolexpr{
       test {\ifnumgreater{\value{editor}}{1}}
       or
       test {\ifandothers{editor}}
     }
       {\def\abx@tempa{editors}}
       {\def\abx@tempa{editor}}}
    {\ifboolexpr{
       test {\ifnumgreater{\value{editor}}{1}}
       or
       test {\ifandothers{editor}}
     }
       {\edef\abx@tempa{\thefield{editortype}s}}
       {\edef\abx@tempa{\thefield{editortype}}}}%
  \let\abx@tempb=\empty
  \ifnamesequal{editor}{translator}
    {\appto\abx@tempa{tr}%
     \appto\abx@tempb{\clearname{translator}}}
    {}%
  \ifnamesequal{editor}{commentator}
    {\appto\abx@tempa{co}%
     \appto\abx@tempb{\clearname{commentator}}}
    {\ifnamesequal{editor}{annotator}
       {\appto\abx@tempa{an}%
	\appto\abx@tempb{\clearname{annotator}}}
       {}}%
  \ifnamesequal{editor}{introduction}
    {\appto\abx@tempa{in}%
     \appto\abx@tempb{\clearname{introduction}}}
    {\ifnamesequal{editor}{foreword}
       {\appto\abx@tempa{fo}%
	\appto\abx@tempb{\clearname{foreword}}}
       {\ifnamesequal{editor}{afterword}
          {\appto\abx@tempa{af}%
           \appto\abx@tempb{\clearname{afterword}}}
          {}}}%
  \ifbibxstring{\abx@tempa}
    {\printtext[editortype]{\mkbibparens{\bibstring{\abx@tempa}}}\abx@tempb}
    {\usebibmacro{editorstrg}}%
}

\renewbibmacro*{editor}{%
  \ifboolexpr{
    test \ifuseeditor
    and
    not test {\ifnameundef{editor}}
  }
    {\printnames{editor}%
     \setunit{\addspace}%
     \usebibmacro{editorstrg}%
     \clearname{editor}}
    {}%
}

\renewbibmacro*{editor+others}{%
  \ifboolexpr{
    test \ifuseeditor
    and
    not test {\ifnameundef{editor}}
  }
    {\printnames{editor}%
     \setunit{\addspace}%
     \usebibmacro{editor+othersstrg}%
     \clearname{editor}}
    {}}

\renewbibmacro*{byeditor+others}{%
  \ifnameundef{editor}
    {}
    {\usebibmacro{byeditor+othersstrg}%
     \setunit{\addspace}%
     \printnames[byeditor]{editor}%
     \clearname{editor}%
     \newunit}%
  \usebibmacro{byeditorx}%
  \usebibmacro{bytranslator+others}}

\renewbibmacro*{finentry}{%
  \ifbibliography{%
    \iffieldundef{shorthand}{}{%
      \bibstring{bibcitedas}\addspace\mkbibquote{\printfield{shorthand}}%
    }%
    \ifboolexpr{ test {\ifcategory{quotee}}
                 and not test {\ifcategory{primary}} }
      {\newunit%
       \bibstring{quotedin}%
       \addspace%
       \global\toggletrue{cbx@isquote}%
       \fullcite{\csuse{cbx@quoter@\thefield{entrykey}}}%
       \global\togglefalse{cbx@isquote}}
      {}}
   {}%
  \finentry%
}

% Asugabe der Eintragstypen anpassen
\DeclareBibliographyDriver{incollection}{%
  \usebibmacro{bibindex}%
  \usebibmacro{begentry}%
  \usebibmacro{author/translator+others}%
  \setunit{\labelnamepunct}\newblock
  \usebibmacro{title}%
  \newunit
  \printlist{language}%
  \newunit\newblock
  \usebibmacro{byauthor}%
  \newunit\newblock
  \usebibmacro{in:}%
%  \usebibmacro{publisher+maintitle+booktitle}%
  \usebibmacro{maintitle+booktitle}%
  \newunit\newblock
  \usebibmacro{byeditor+others}%
  \newunit\newblock
  \printfield{edition}%
  \newunit
  \iffieldundef{maintitle}
    {\printfield{volume}%
     \printfield{part}}
    {}%
  \newunit
  \printfield{volumes}%
  \newunit\newblock
  \usebibmacro{series+number}%
  \newunit\newblock
  \printfield{note}%
  \newunit\newblock
  \usebibmacro{location+date}%
  \newunit\newblock
  \usebibmacro{chapter+pages}%
  \newunit\newblock
  \iftoggle{bbx:isbn}
    {\printfield{isbn}}
    {}%
  \newunit\newblock
  \usebibmacro{doi+eprint+url}%
  \newunit\newblock
  \usebibmacro{addendum+pubstate}%
  \setunit{\bibpagerefpunct}\newblock
  \usebibmacro{pageref}%
  \usebibmacro{finentry}}


\DeclareBibliographyDriver{music}{%
  \usebibmacro{bibindex}%
  \usebibmacro{begentry}%
  \usebibmacro{author/translator+others}%
  \setunit{\labelnamepunct}\newblock
  \usebibmacro{maintitle+title}%
  \usebibmacro{opus}%
  \newunit\newblock
  \printfield{note}%
  \newunit
  \usebibmacro{byeditor+others}%
  \setunit{[=\addspace}
  \printfield{series}%
  \setunit*{\unspace]\adddot\addspace}
  \usebibmacro{publisher+location+date}%
  \newunit\newblock
  \usebibmacro{doi+eprint+url}%
  \newunit\newblock
  \usebibmacro{finentry}}
 
\DeclareBibliographyDriver{booklet}{%
  \usebibmacro{bibindex}%
  \usebibmacro{begentry}%
  \usebibmacro{author/editor+others/translator+others}%
  \setunit{\labelnamepunct}\newblock
  \usebibmacro{title}%
  \newunit
  \printlist{language}%
  \newunit\newblock
  \usebibmacro{byauthor}%
  \newunit\newblock
  \usebibmacro{byeditor+others}%
  \newunit\newblock
  \printfield{howpublished}%
  \newunit\newblock
  \printfield{type}%
  \newunit\newblock
  \printfield{note}%
  \newunit\newblock
  \usebibmacro{location+date}%
  \newunit\newblock
  \usebibmacro{chapter+pages}%
  \newunit
  \printfield{pagetotal}%
  \newunit\newblock
  \usebibmacro{doi+eprint+url}%
  \newunit\newblock
  \usebibmacro{addendum+pubstate}%
  \setunit{\bibpagerefpunct}\newblock
  \usebibmacro{pageref}%
  \usebibmacro{finentry}}

% Ausgabeformate anpassen
\DeclareFieldFormat{citetitle}{\mkbibemph{#1\isdot}}
\DeclareFieldFormat[article]{citetitle}{\mkbibemph{#1}}
\DeclareFieldFormat[incollection]{citetitle}{\mkbibemph{#1}}
\DeclareFieldFormat{title}{\mkbibemph{#1\isdot}}
\DeclareFieldFormat[article]{title}{\mkbibemph{#1}}
\DeclareFieldFormat[incollection]{title}{\mkbibemph{#1}}

% Aussehen der Bibliography anpassen
\setlength{\bibhang}{0pt}
\setlength{\bibitemsep}{0.5\baselineskip}

%<audrey>
% ben�tigt f�r Sekund�rzitate
\DeclareBibliographyCategory{quoter}
\DeclareBibliographyCategory{quotee}
\DeclareBibliographyCategory{primary}

\defbibfilter{onlyprimary}{%
  category=quoter or category=primary}
  
\newtoggle{cbx@isquote}% Current citation quote?
\newtoggle{cbx@iflastquote}% Last quotecite quotecite?
\newtoggle{cbx@iflastibid}% Last quotecite ibid?
\newtoggle{cbx@ifquoteepost}% Ibid quotecite outputs quotee postnote?
\newtoggle{cbx@ifquoterpost}% Ibid quotecite outputs quoter postnote?
\newtoggle{cbx@fullquote}% Print a full quotecite?

\newcommand*{\quoteepostdelim}{%
  \addcomma\addspace\bibstring{quotedfrom}\space}

\newcommand*{\quoterpostdelim}{%
  \addcomma\space\bibstring{quotedon}\space}

\newcommand*{\quotedelim}{%
  \adddot\space\bibsentence\bibstring{quotedin}\space}

\AtEveryCitekey{%
  \iftoggle{cbx@isquote}
    {}
    {\addtocategory{primary}{\thefield{entrykey}}%
     \global\togglefalse{cbx@iflastquote}}}
%</audrey>
\endinput