% historische-zeitschrift.cbx, Dominik Waßenhoven 2014-2016

\ProvidesFile{historische-zeitschrift.cbx}[2016/12/01 v1.2 HZ citation style]

\DeclareBibliographyOption{postnote}{%
  \ifcsdef{hz@opt@postnote@#1}
    {\csuse{hz@opt@postnote@#1}}
    {\PackageError{historische-zeitschrift}
       {Invalid option 'postnote=#1'\MessageBreak
        Valid values are 'afterparen', 'inparen'}}}

\def\hz@opt@postnote@afterparen{%
  \renewbibmacro*{postnote}{%
    \ifbool{cbx@seenote}
      {\iffieldundef{postnote}
        {\printtext{\bibrightparen}}
        {\printtext{\bibrightparen}%
         \postnotedelim%
         \printfield{postnote}}}%
      {\iffieldundef{postnote}
        {}
        {\postnotedelim%
         \printfield{postnote}}}}}

\def\hz@opt@postnote@inparen{%
  \renewbibmacro*{postnote}{%
    \ifbool{cbx@seenote}
      {\iffieldundef{postnote}
        {\printtext{\bibrightparen}}
        {\postnotedelim%
         \printfield{postnote}%
         \printtext{\bibrightparen}}}%
      {\iffieldundef{postnote}
        {}
        {\postnotedelim%
         \printfield{postnote}}}}}

\ExecuteBibliographyOptions{
  citetracker=context,
  ibidtracker=constrict,
  singletitle=false,%<----------------
  pagetracker,
  autocite=footnote,
  postnote=afterparen
}

\providecommand*{\mkidem}[1]{#1}
\providecommand*{\mkibid}[1]{#1}
\newbool{cbx@loccit}
% to check if a 'see note' phrase is in use:
\newbool{cbx@seenote}

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

%% 'Ders.' bei inbook, wenn gleicher Autor
\renewbibmacro*{bybookauthor}{%
  \ifnamesequal{author}{bookauthor}
    {\midsentence*\bibstring[\mkidem]{idem\thefield{gender}}\addcolon}
    {\printnames{bybookauthor}}}
%    {\printnames[byauthor]{bookauthor}%
%     \newunit\newblock}}

%%%%% %%%%% %%%%% %%%%% %%%%% 
%%%%% Reformatting      %%%%% 
%%%%% %%%%% %%%%% %%%%% %%%%% 

\DeclareFieldFormat{citetitle}{#1\isdot}
\DeclareFieldFormat[article]{citetitle}{#1}
\DeclareFieldFormat[inbook]{citetitle}{#1}
\DeclareFieldFormat[incollection]{citetitle}{#1}
\DeclareFieldFormat[inproceedings]{citetitle}{#1}
\DeclareFieldFormat[patent]{citetitle}{#1}
\DeclareFieldFormat[thesis]{citetitle}{#1}
\DeclareFieldFormat[unpublished]{citetitle}{#1}

\DeclareFieldFormat{postnote}{#1}
\DeclareFieldFormat{multipostnote}{#1}

%%%%% %%%%% %%%%% %%%%% %%%%% %%%%% 
%%%%% Bibliography strings    %%%%%
%%%%% %%%%% %%%%% %%%%% %%%%% %%%%%  
\DefineBibliographyStrings{german}{%
  seenote   = {wie Anm\adddot},
}

%%%%% %%%%% %%%%% %%%%%  
%%%%% Postnote    %%%%% 
%%%%% %%%%% %%%%% %%%%% 
% has to be redefined, because the 'see note' phrase
% might be in parentheses (option 'postnote')
% \renewbibmacro*{postnote}{%
%   \ifbool{cbx@seenote}
%     {\iffieldundef{postnote}
%       {\printtext{\bibrightparen}}
%       {\postnotedelim
%        \printfield{postnote}%
%        \printtext{\bibrightparen}}}
%     {\iffieldundef{postnote}
%       {}
%       {\postnotedelim
%        \printfield{postnote}}}}

%%%%% %%%%% %%%%% %%%%% %%%%% 
%%%%% Cite bibmacros    %%%%%
%%%%% %%%%% %%%%% %%%%% %%%%% 

\newbibmacro*{cite}{%
  \global\boolfalse{cbx@seenote}% NEW!
  \iffieldundef{shorthand}
    {\usedriver
       {\DeclareNameAlias{sortname}{default}}
       {\thefield{entrytype}}}%
    {\printfield{shorthand}}}

\newbibmacro*{footcite}{%
  \global\boolfalse{cbx@loccit}%
  \global\boolfalse{cbx@seenote}% NEW!
  \ifciteseen
    {\iffieldundef{shorthand}
       {\ifthenelse{\ifciteibid\AND\NOT\iffirstonpage}
          {\usebibmacro{footcite:ibid}}
          {\usebibmacro{footcite:note}}}%
       {\usebibmacro{footcite:shorthand}}}%
    {\usebibmacro{footcite:full}%
     \usebibmacro{footcite:save}}}

\newbibmacro*{footcite:save}{%
  \ifundef\@thefnmark
    {\ifundef\@theenmark % endnotes.sty
       {\csgdef{cbx@note@\thefield{entrykey}}{%
          \BibliographyWarning{Missing footnote mark}\textbf{??}}}
       {\global\cslet{cbx@note@\thefield{entrykey}}\@theenmark}}%
    {\global\cslet{cbx@note@\thefield{entrykey}}\@thefnmark}%
  \csxdef{cbx@first@\thefield{entrykey}}{\the\value{instcount}}}

\newbibmacro*{footcite:full}{%
  \printtext{%
    \bibhypertarget{\thefield{entrykey}:\the\value{instcount}}{%
      \usedriver
	{\DeclareNameAlias{sortname}{default}}
	{\thefield{entrytype}}}}%
  \usebibmacro{shorthandintro}}

\newbibmacro*{footcite:note}{%
  \global\booltrue{cbx@seenote}% NEW!
  \ifnameundef{labelname}
    {\printfield{label}}
    {\printnames{labelname}}%
  \ifsingletitle
    {}
    {\setunit*{\addcomma\space}%
     \printfield[title]{labeltitle}}%
  \setunit*{\addspace}%<--- CHANGED
  \printtext{%
    \bibhyperlink{\thefield{entrykey}:%
                  \@nameuse{cbx@first@\thefield{entrykey}}}{%
      \printtext{\bibleftparen}%
      \bibstring{seenote}\space
      \@nameuse{cbx@note@\thefield{entrykey}}}}}

\newbibmacro*{footcite:ibid}{%
  \printtext{%
    \bibhyperlink{\thefield{entrykey}:%
                  \@nameuse{cbx@first@\thefield{entrykey}}}{%
      \bibstring[\mkibid]{ibidem}}}%
  \ifloccit
    {\global\booltrue{cbx@loccit}}
    {}}
    
\newbibmacro*{footcite:shorthand}{%
  \printtext{%
    \bibhyperlink{\thefield{entrykey}:%
                  \@nameuse{cbx@first@\thefield{entrykey}}}{%
      \printfield{shorthand}}}}

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

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

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

\DeclareCiteCommand{\footcite}[\mkbibfootnote]
  {\bibsentence
   \usebibmacro{prenote}}
  {\usebibmacro{citeindex}%
   \usebibmacro{footcite}}
  {\multicitedelim}
  {\usebibmacro{cite:postnote}}

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

\endinput
