% arthistory-bonn --%
% Copyright (c) 2017-2018 Lukas C. Bossert | Thorsten Kemper
%  
% 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.
\ProvidesFile{arthistory-bonn.cbx}%
  [\arthist@cbxid]
  \RequireCitationStyle{authoryear-ibid}
%-----------------------
\ExecuteBibliographyOptions{
  uniquename=false,%
  autocite=footnote,%
  }
\RequirePackage{xpatch}
\newbool{cbx:firstcitefull}
\newbool{cbx:namelinked}
\newtoggle{cbx:citepages}
\newbool{cbx:allnamesfamilygiven}

\AtEveryCitekey{\global\togglefalse{cbx:citepages}}
\DeclareBibliographyOption{firstcitefull}[true]{\csuse{bool#1}{cbx:firstcitefull}}
\DeclareBibliographyOption{allnamesfamilygiven}[true]{\csuse{bool#1}{cbx:allnamesfamilygiven}
  \DeclareNameAlias{editor}{default}}
\DeclareBibliographyOption{namelinked}[true]{\csuse{bool#1}{cbx:namelinked}}

\DeclareFieldFormat{postnote}{%
  \ifbool{bbx:pagesfull}
    {\mkpageprefix[pagination]{#1}}
    {#1}}
    
\DeclareNameAlias{default}{family-given}
\DeclareNameAlias{sortname}{family-given}
\DeclareNameAlias{editor}{given-family}
\DeclareNameAlias{byeditor}{editor}
  
\newbibmacro{cite:exhibcatalog}{%
  \bibstring{exhibcatalog}%
  \setunit{\addspace}%
  \iffieldundef{venue}%
  {\printfield{exhibfirstlocation}%
    \iffieldundef{exhibsecondlocation}%
    {}%
    {\setunit{\multilistdelim}%
    \printfield{exhibsecondlocation}
    \iffieldundef{exhibthirdlocation}%
  	  {}%
      {\setunit{\multilistdelim}%
      \printfield{exhibthirdlocation}}}}
  {\printfield{venue}}%
    \setunit{\addspace}%
    \printtext[bibhyperref]{\printeventdate}}
    
\newbibmacro*{cite:reference}{%
  \iffieldundef{shorttitle}%
  {\printfield{title}}%
  {\printfield{shorttitle}}}



\newbibmacro{cite:catalog}{%
  \bibstring{catalog}%
  \setunit{\addspace}%
  \printfield{label}%
  \setunit{\addspace}%
  \printtext[bibhyperref]{\printdate}}

\newbibmacro*{cite:various}{%
\printtext{%
  \ifentrytype{catalog}%
    {\usebibmacro{cite:catalog}}%
    {\ifentrytype{exhibcatalog}%
      {\usebibmacro{cite:exhibcatalog}}%
      {\ifentrytype{reference}%
        {\usebibmacro{cite:reference}}%
        {\printnames{labelname}%
         \setunit{\printdelim{nameyeardelim}}%
        \printtext[bibhyperref]{\usebibmacro{date+extradate}}}}}}}

\newbibmacro*{cite:normal}{%
 \global\boolfalse{cbx:loccit}%
 \ifthenelse{\ifciteibid\AND\NOT\iffirstonpage}%
   {\usebibmacro{cite:ibid}}%
   {\iffieldundef{shorthand}%
     {\ifthenelse{\ifnameundef{labelname}\OR\iffieldundef{labelyear}}%
     {\usebibmacro{cite:label}%
         \setunit{\printdelim{nonameyeardelim}}}%
     {\usebibmacro{cite:various}}}%
   {\usebibmacro{cite:shorthand}}}}

\renewbibmacro*{cite}{%
  \ifboolexpr{bool {cbx:firstcitefull} and not test {\ifciteseen}}%
   {\usedriver%
      {\DeclareNameAlias{sortname}{default}}%
      {\thefield{entrytype}}}%
   {\ifbool{cbx:namelinked}
    {\printtext[bibhyperref]{\usebibmacro{cite:normal}}}
    {\usebibmacro{cite:normal}}}}


\renewbibmacro*{cite:postnote}{%
  \ifbool{cbx:loccit}
    {}
    {\iffieldundef{urlyear}
      {\usebibmacro{postnote}}
      {\usebibmacro{postnote}%
      \newunit%
      \bibstring{urlseen}%
      \setunit{\addspace}%
      \usebibmacro{urldate}}}}

\DeclareCiteCommand{\smartcite}[\iffootnote\textnormal\mkbibfootnote]
  {\usebibmacro{prenote}}%
  {\usebibmacro{citeindex}%
   \usebibmacro{cite}}%
  {\multicitedelim}%
  {\usebibmacro{postnote}}

\DeclareMultiCiteCommand{\smartcites}[\iffootnote\textnormal\mkbibfootnote]
{\smartcite}%
{\multicitedelim}%


\renewbibmacro*{postnote}{%
  \iffieldundef{postnote}%
    {}%
    {\ifboolexpr{%
       test {\iftoggle{cbx:citepages}}%
       and
       test {\iffieldpages{postnote}}%
     }%
       {\setunit{\addcomma\addspace}%
        \bibstring{thiscite}%
        \setunit{\addspace}}%
       {\setunit{\postnotedelim}}%
     \printfield{postnote}}}

%http://tex.stackexchange.com/a/158481/98739
\xpatchbibmacro{date+extradate}{%
  \printtext[parens]%
}{%
  \printtext%
}{}{}
   
\endinput
%% End of file `arthistory-bonn.cbx'.