\RequireBibliographyStyle{standard}
\RequireBiber[3]

% BibLaTeX version 3.5 or greater required
\newdimen \biblatex@version
\biblatex@version = \abx@version pt
\ifdim \biblatex@version < 3.45pt
  \PackageError{biblatex-sbl}{BibLaTeX v3.5 or greater is required}
\fi

% Require xparse
\RequirePackage{xparse}

% ------------------------------------------------------------------
% VERSION STRINGS
% ------------------------------------------------------------------

\def\sbl@abx@date{2016/10/11}
\def\sbl@abx@version{0.7}
\def\sbl@abx@bbxid{\sbl@abx@date\space v\sbl@abx@version\space biblatex-sbl bibliography style}
\def\sbl@abx@cbxid{\sbl@abx@date\space v\sbl@abx@version\space biblatex-sbl citation style}
\def\sbl@abx@lbxid{\sbl@abx@date\space v\sbl@abx@version\space biblatex-sbl localization}
\def\sbl@abx@defid{\sbl@abx@date\space v\sbl@abx@version\space biblatex-sbl definitions and macros}

\ProvidesFile{biblatex-sbl.def}[\sbl@abx@defid]

% ------------------------------------------------------------------
% CITEPAGES OPTION
% ------------------------------------------------------------------

\DeclareRangeChars{~,;-+/:}

\NumCheckSetup{\def\mkbibparens#1{#1}}

\renewbibmacro*{postnote}{}
\newbibmacro*{pages}{}

\DeclareBibliographyOption[string]{citepages}[permit]{%
  \ifcsdef{cbx@opt@citepages@#1}
    {\csuse{cbx@opt@citepages@#1}}
    {\PackageError{biblatex}
       {Invalid option 'citepages=#1'}
       {Valid values are 'sbl', 'permit', 'omit', 'separate'.}}}

\providebibmacro*{postnote}{}
\providebibmacro*{pages}{}

\def\cbx@opt@citepages@sbl{%
  \renewbibmacro*{pages}{%
    \iffieldundef{pages}
      {}
      {\printfield{pages}%
       \ifboolexpr{
         test {\iffieldundef{postnote}}
         or
         bool {bbx@inset}
       }
         {}
         {\iffieldpages{postnote}
            {}
            {\setunit{\addspace}%
             \printtext[parens]{\printfield{postnote}}}}}}%
  \renewbibmacro*{postnote}{%
    \iffieldundef{postnote}
      {\printfield{pages}}
      {\iffieldpages{postnote}
         {\printfield{postnote}}
         {\iffieldundef{pages}
            {\printfield{postnote}}
            {\printfield{pages}%
             \setunit{\addspace}%
             \printtext[parens]{\printfield{postnote}}}}}}}

\def\cbx@opt@citepages@permit{%
  \renewbibmacro*{pages}{%
    \iffieldundef{pages}
      {}
      {\printfield{pages}%
       \ifboolexpr{
         test {\iffieldundef{postnote}}
         or
         bool {bbx@inset}
       }
         {}
         {\setunit{\addspace}%
          \printtext[parens]{\printfield{postnote}}}}}%
  \renewbibmacro*{postnote}{%
    \iffieldundef{postnote}
      {\printfield{pages}}
      {\iffieldpages{postnote}
         {\printfield{postnote}}
         {\iffieldundef{pages}
            {\newunit
             \printfield{postnote}}
            {\printfield{pages}%
             \setunit*{\addspace}%
             \printtext[parens]{\printfield{postnote}}}}}}}

\def\cbx@opt@citepages@omit{%
  \renewbibmacro*{pages}{%
    \iffieldundef{pages}
      {}
      {\ifboolexpr{
         test {\iffieldundef{postnote}}
         or
         bool {bbx@inset}
       }
         {\printfield{pages}}
         {\iffieldpages{postnote}
            {\printfield{postnote}}
            {\printfield{pages}%
             \setunit*{\addspace}%
             \printtext[parens]{\printfield{postnote}}}}}}%
  \renewbibmacro*{postnote}{%
    \iffieldundef{postnote}
      {\printfield{pages}}
      {\iffieldpages{postnote}
         {\printfield{postnote}}
         {\iffieldundef{pages}
            {\newunit
             \printfield{postnote}}
            {\printfield{pages}%
             \setunit*{\addspace}%
             \printtext[parens]{\printfield{postnote}}}}}}}

\def\cbx@opt@citepages@separate{%
  \renewbibmacro*{pages}{%
    \iffieldundef{pages}
      {}
      {\printfield{pages}%
       \ifboolexpr{
         test {\iffieldundef{postnote}}
         or
         bool {bbx@inset}
       }
         {}
         {\setunit{\addspace}%
           \printtext[parens]{%
            \iffieldpages{postnote}
              {\bibstring{thiscite}%
               \setunit{\addspace}}
              {}%
            \printfield{postnote}}}}}%
  \renewbibmacro*{postnote}{%
    \iffieldundef{postnote}
      {\printfield{pages}}
      {\iffieldpages{postnote}
         {\printfield{postnote}}
         {\iffieldundef{pages}
            {\printfield{postnote}}
            {\printfield{pages}%
             \setunit*{\addspace}%
             \printtext[parens]{%
               \iffieldpages{postnote}
                 {\bibstring{thiscite}%
                  \setunit{\addspace}}
                 {}%
               \printfield{postnote}}}}}}}


% ------------------------------------------------------------------
% IDEM MACROS
% ------------------------------------------------------------------

% only use idem within footnote
\let\cbx@ifmpfncheck\blx@ifmpfncheck
\patchcmd{\cbx@ifmpfncheck}{\tw@}{\@ne}{}{}
\let\cbx@ifciteidem@constrict\blx@ifciteidem@constrict
\patchcmd{\cbx@ifciteidem@constrict}{\blx@ifmpfncheck}{\cbx@ifmpfncheck}{}{}

\def\blx@opt@idemtracker@constrict{%
  \let\blx@imc@ifciteidem\cbx@ifciteidem@constrict
  \let\blx@idemtracker\blx@idemtracker@constrict
  \let\blx@idemreset\blx@idemreset@context
  \booltrue{citetracker}}

\newbibmacro*{ifidemused}{%
  \ifciteidem
    {\bibstring[\mkibid]{idem\thefield{gender}}%
     \@firstoftwo}
    {\@secondoftwo}}


% ------------------------------------------------------------------
% COMMENTARY OPTIONS
% ------------------------------------------------------------------

\newtoggle{fullbibrefs}

\DeclareBibliographyOption{fullbibrefs}[true]{%
  \ifstrequal{#1}{true}
    {\toggletrue{fullbibrefs}}
    {\togglefalse{fullbibrefs}}}


% ------------------------------------------------------------------
% ENTRY OPTIONS
% ------------------------------------------------------------------

\newtoggle{blx@skipbiblistseries}
\newtoggle{blx@skipbiblistshorthand}
\newtoggle{blx@usefullcite}
\newtoggle{blx@usevolume}
\newtoggle{blx@useshorttitle}

\DeclareTypeOption{skipbiblistseries}[true]{%
  \settoggle{blx@skipbiblistseries}{#1}}
\DeclareEntryOption{skipbiblistseries}[true]{%
  \settoggle{blx@skipbiblistseries}{#1}}

\DeclareTypeOption{skipbiblistshorthand}[true]{%
  \settoggle{blx@skipbiblistshorthand}{#1}}
\DeclareEntryOption{skipbiblistshorthand}[true]{%
  \settoggle{blx@skipbiblistshorthand}{#1}}

\DeclareBibliographyOption{useshorttitle}[true]{%
  \settoggle{blx@useshorttitle}{#1}}
\DeclareTypeOption{useshorttitle}[true]{%
  \settoggle{blx@useshorttitle}{#1}}
\DeclareEntryOption{useshorttitle}[true]{%
  \settoggle{blx@useshorttitle}{#1}}

\DeclareBibliographyOption{usefullcite}[true]{%
  \settoggle{blx@usefullcite}{#1}}
\DeclareTypeOption{usefullcite}[true]{%
  \settoggle{blx@usefullcite}{#1}}
\DeclareEntryOption{usefullcite}[true]{%
  \settoggle{blx@usefullcite}{#1}}

\DeclareBibliographyOption{usevolume}[true]{%
  \settoggle{blx@usevolume}{#1}}
\DeclareTypeOption{usevolume}[true]{%
  \settoggle{blx@usevolume}{#1}}
\DeclareEntryOption{usevolume}[true]{%
  \settoggle{blx@usevolume}{#1}}

\def\blx@shorthand{}
\DeclareEntryOption[string]{shorthand}[true]{%
  \def\blx@shorthand{#1}}


% ------------------------------------------------------------------
% STYLE OPTIONS
% ------------------------------------------------------------------

% footnote style
\let\orig@makefntext\@makefntext
\DeclareBibliographyOption{sblfootnotes}[true]{%
  \ifstrequal{#1}{true}
    {\@ifpackageloaded{footmisc}
       {\renewcommand\@makefntext[1]{%
          \parindent\footnotemargin%
          \@thefnmark.\@\space
          \footnotelayout
          ##1}}
       {\renewcommand\@makefntext[1]{%
          \parindent 1em%
          \@thefnmark.\@\space
          ##1}}}
    {\let\@makefntext\orig@makefntext}}

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

\ExecuteBibliographyOptions{%
  citetracker,
  punctfont,
  alldates=comp,
  releasedate=comp,
  dateabbrev=false,
  autocite=footnote,
  url,
  related,
  minxrefs=1,
  isbn=false,
  ibidtracker=false,
  ibidpage=false,
  maxbibnames=99,
  maxcitenames=3,
  citepages=sbl,
  fullbibrefs=false,
  sblfootnotes,
  usefullcite,
  useshorttitle,
  usevolume
}

\ExecuteBibliographyOptions[classictext,ancienttext]{%
  skipbib
}

\ExecuteBibliographyOptions[inlexicon]{%
  skipbib
}

\urlstyle{same}
\def\UrlBreaks{\do\@\do\\\do\/\do\!\do\_\do\|\do\;\do\>\do\]%
 \do\)\do\,\do\?\do\'\do+\do\=\do\#}%
\def\UrlSpecials{\do\.{\penalty\UrlBreakPenalty\mathchar`.}%
  \do\-{\penalty\UrlBreakPenalty\mathchar`-}%
  \do\ {\Url@space}\do\%{\Url@percent}\do\^^M{\Url@space}%
  \Url@force@Tilde}% package option may force faked text-ascii-tilde

\NewBibliographyString{to}
\NewBibliographyString{of}
\NewBibliographyString{with}
\NewBibliographyString{withassistance}
\NewBibliographyString{withpreface}
\NewBibliographyString{paperpresented}
\NewBibliographyString{released}

\DeclareLanguageMapping{english}{sbl-american}
\DeclareLanguageMapping{american}{sbl-american}
\DeclareLanguageMapping{british}{sbl-british}
\DeclareLanguageMapping{german}{sbl-german}
\DeclareLanguageMapping{spanish}{sbl-spanish}

\renewcommand*{\subtitlepunct}{\addcolon\space}
\newcommand*{\namedashpunct}{\adddot\space}
\newcommand*{\lexiconfinalnamedelim}{\addcomma\space}
\renewcommand*{\relateddelim}{\addsemicolon\space}

\setcounter{mincompwidth}{10}

\DeclareFieldFormat{doi}{%
  \printtext{doi}\addcolon
  \ifhyperref
    {\href{http://dx.doi.org/#1}{\nolinkurl{#1}}}
    {\nolinkurl{#1}}}
\DeclareFieldFormat{edition}{%
  \ifinteger{#1}
    {\mkbibordedition{#1}~\bibsstring{edition}}
    {\ifcapital{\MakeCapital{#1}}{#1}\isdot}}
\DeclareFieldFormat{eprint:ebook}{#1 \biblstring{edition}}
\DeclareFieldFormat{eprint}{%
  \iffieldundef{eprinttype}
    {eprint}
    {\thefield{eprinttype}}%
  \newunitpunct
  \ifhyperref
    {\url{#1}}
    {\nolinkurl{#1}}%
  \iffieldundef{eprintclass}
    {}
    {\addspace\mkbibparens{\thefield{eprintclass}}}}
\DeclareFieldFormat{eprint:arxiv}{%
  arXiv\addcolon\space
  \ifhyperref
    {\href{http://arxiv.org/\abx@arxivpath/#1}{%
       \nolinkurl{#1}%
       \iffieldundef{eprintclass}
         {}
         {\addspace\mkbibbrackets{\thefield{eprintclass}}}}}
    {\nolinkurl{#1}
     \iffieldundef{eprintclass}
       {}
       {\addspace\mkbibbrackets{\thefield{eprintclass}}}}}
\DeclareFieldFormat{eprint:hethiter}{%
  \printtext{doi}\addcolon
  \ifhyperref
    {\href{http://hethiter.net/:\%20#1}{%
       hethiter\slash\addcolon\space
       \nolinkurl{#1}%
       \iffieldundef{eprintclass}
         {}
         {\addspace\mkbibparens{\thefield{eprintclass}}}}}
    {hethiter\slash\addcolon\space
     \nolinkurl{#1}
     \iffieldundef{eprintclass}
       {}
       {\addspace\mkbibparens{\thefield{eprintclass}}}}}
\renewcommand*{\volcitedelim}{\addcolon}
\DeclareFieldFormat{volcitevolume}{#1}
\DeclareFieldFormat{volcitepages}{\mkcomprange{#1}}
\DeclareFieldFormat{part}{#1}
\DeclareFieldFormat{pt}{\bibstring{part}~#1}
\DeclareFieldFormat{postnote}{\mkcomprange{#1}}
\DeclareFieldFormat{multipostnote}{#1}
\DeclareFieldFormat{byauthor}{\bibstring{byauthor} #1}
\DeclareFieldFormat{revdtitle}{\biblstring{reviewof} \mkbibemph{#1}}
\DeclareFieldFormat{revdshorttitle}{\biblstring{reviewof} \mkbibemph{#1}}
\DeclareFieldFormat{shorttitle}{\mkbibemph{#1}}
\DeclareFieldFormat{shortbooktitle}{\mkbibemph{#1}}
\DeclareFieldFormat{shortmaintitle}{\mkbibemph{#1}}
\DeclareFieldFormat{shortjournal}{\mkbibemph{#1}}
\DeclareFieldFormat{seriesseries}{#1}

\DeclareFieldFormat{pages}{\mkcomprange{#1}}
\DeclareFieldFormat{pagesin}{\mkcomprange[\mkpageprefix]{#1} \usebibmacro{in}}
\DeclareFieldFormat{volume}{#1}
\DeclareFieldFormat{vol}{\bibsstring{volume}~#1}
\DeclareFieldFormat{volumeof}{\bibsstring{volume}~#1 \bibstring{of}}
\DeclareFieldFormat{volumes}{#1\ifnumeral{#1}{~\bibsstring{volumes}}{}}
\DeclareFieldFormat{partof}{\bibstring{part}~#1 \bibstring{of}}
\DeclareFieldFormat{no}{\bibsstring{number}~#1}
\DeclareFieldFormat{url}{\url{#1}}
\DeclareFieldFormat{urldate}{}
\DeclareFieldFormat{releasedate}{\bibstring{released}\space#1}

\DeclareFieldFormat{editortype}{\ifcapital{\MakeCapital{#1}}{#1}}
\DeclareFieldFormat{withauthortype}{#1}
\DeclareFieldFormat{witheditortype}{#1}
\DeclareFieldFormat{withtranslatortype}{#1}
\DeclareFieldFormat{withbookauthortype}{#1}
\DeclareFieldFormat{withbookeditortype}{#1}
\DeclareFieldFormat{withbooktranslatortype}{#1}
\DeclareFieldFormat{withmainauthortype}{#1}
\DeclareFieldFormat{withmaineditortype}{#1}
\DeclareFieldFormat{withmaintranslatortype}{#1}

\DeclareFieldFormat{cptype}{\ifbibstring{#1}{\bibcpstring{#1}}{#1}}
\DeclareFieldFormat{quote}{\mkbibquote{#1}}

\DeclareNameAlias{withauthor}{given-family}
\DeclareNameAlias{witheditor}{given-family}
\DeclareNameAlias{withtranslator}{given-family}
\DeclareNameAlias{withbookauthor}{given-family}
\DeclareNameAlias{withbookeditor}{given-family}
\DeclareNameAlias{withbooktranslator}{given-family}
\DeclareNameAlias{withmainauthor}{given-family}
\DeclareNameAlias{withmaineditor}{given-family}
\DeclareNameAlias{withmaintranslator}{given-family}

\DeclareFieldFormat{relatedstring:reprint}{#1\printunit{\addcomma\space}}

\DeclareFieldFormat[incommentary,inreference,inlexicon,online,review,seminarpaper,conferencepaper]{title}{\mkbibquote{#1}}
\DeclareFieldFormat[incommentary,inreference,inlexicon,online,review,seminarpaper,conferencepaper]{shorttitle}{\mkbibquote{#1}}
\DeclareFieldFormat{howpublished}{\ifbibstring{#1}{\bibstring{#1}}{#1}}
\DeclareFieldFormat[ancienttext]{title}{%
  \ifboolexpr{
    test {\iffieldequalstr{entrysubtype}{inscription}}
    or
    test {\iffieldequalstr{entrysubtype}{chronicle}}
  }
    {#1}
    {\iffieldequalstr{entrysubtype}{churchfather}
       {\mkbibemph{#1}}
       {\mkbibquote{#1}}}}

\DeclareBibliographyAlias{cite:mvbook}{cite:book}
\DeclareBibliographyAlias{cite:mvcollection}{cite:collection}
\DeclareBibliographyAlias{collection}{book}
\DeclareBibliographyAlias{mvcollection}{collection}
\DeclareBibliographyAlias{cite:collection}{cite:book}
\DeclareBibliographyAlias{mvreference}{mvbook}
\DeclareBibliographyAlias{cite:mvreference}{cite:mvbook}
\DeclareBibliographyAlias{reference}{book}
\DeclareBibliographyAlias{cite:reference}{cite:book}
\DeclareBibliographyAlias{lexicon}{reference}
\DeclareBibliographyAlias{cite:lexicon}{cite:reference}
\DeclareBibliographyAlias{mvlexicon}{mvreference}
\DeclareBibliographyAlias{cite:mvlexicon}{cite:mvreference}
\DeclareBibliographyAlias{inbook}{incollection}
\DeclareBibliographyAlias{cite:inbook}{cite:incollection}
\DeclareBibliographyAlias{commentary}{book}
\DeclareBibliographyAlias{mvcommentary}{book}
\DeclareBibliographyAlias{cite:mvcommentary}{cite:mvbook}
\DeclareBibliographyAlias{cite:online}{cite:article}
\DeclareBibliographyAlias{online}{article}
\DeclareBibliographyAlias{cite:manual}{cite:book}
\DeclareBibliographyAlias{manual}{book}
\DeclareBibliographyAlias{cite:unpublished}{cite:misc}
\DeclareBibliographyAlias{unpublished}{misc}
\DeclareBibliographyAlias{proceedings}{collection}
\DeclareBibliographyAlias{mvproceedings}{mvcollection}
\DeclareBibliographyAlias{inproceedings}{incollection}
\DeclareBibliographyAlias{cite:proceedings}{cite:collection}
\DeclareBibliographyAlias{cite:mvproceedings}{cite:mvcollection}
\DeclareBibliographyAlias{cite:inproceedings}{cite:incollection}

\DeclareLabeldate{%
  \field{releasedate}
}

\renewcommand*{\newunitpunct}{\addcomma\space}
\newcommand*{\volpostnotedelim}{\addcolon}

\providecommand*{\mkibid}[1]{#1}

\renewbibmacro*{name:family-given}[4]{%
  \ifuseprefix
    {\usebibmacro{name:delim}{#3#1}%
     \usebibmacro{name:hook}{#3#1}%
     \ifdefvoid{#3}{}{%
       \ifcapital
         {\mkbibnameprefix{\MakeCapital{#3}}\isdot}
         {\mkbibnameprefix{#3}\isdot}%
       \ifprefchar{}{\bibnamedelimc}}%
     \mkbibnamefamily{#1}\isdot
     \ifdefvoid{#2}{}{\revsdnamepunct\bibnamedelimd\mkbibnamegiven{#2}\isdot}%
     \ifdefvoid{#4}{}{\revsdnamepunct\bibnamedelimd\mkbibnamesuffix{#4}\isdot}}
    {\usebibmacro{name:delim}{#1}%
     \usebibmacro{name:hook}{#1}%
     \mkbibnamefamily{#1}\isdot
     \ifboolexpe{%
       test {\ifdefvoid{#2}}
       and
       test {\ifdefvoid{#3}}}
       {}
       {\revsdnamepunct}%
     \ifdefvoid{#2}{}{\bibnamedelimd\mkbibnamegiven{#2}\isdot}%
     \ifdefvoid{#3}{}{\bibnamedelimd\mkbibnameprefix{#3}\isdot}%
     \ifdefvoid{#4}{}{\revsdnamepunct\bibnamedelimd\mkbibnamesuffix{#4}\isdot}}}


% ------------------------------------------------------------------
% BIBLIOGRAPHY
% ------------------------------------------------------------------

\renewrobustcmd*{\printbibliography}{%
  \begingroup
  \blx@key@bibcheck{bibliography}
  \edef\on@line{\on@line}%
  \@ifnextchar[%]
    {\blx@printbibliography}
    {\blx@printbibliography[]}}
\defbibcheck{bibliography}{%
  \blx@skipentries
  \blx@includeentries
}
\def\blx@skipentries{}
\def\blx@includeentries{}
\def\addskipentry#1{%
  \edef\X{%
    \noexpand\iffieldequalstr{entrykey}{#1}
      {\noexpand\toggletrue{blx@skipentry}}
      {}}%
  \expandafter\g@addto@macro\expandafter\blx@skipentries\expandafter{\X}}
\def\addincludeentry#1{%
  \edef\X{%
    \noexpand\iffieldequalstr{entrykey}{#1}
      {\noexpand\togglefalse{blx@skipentry}}
      {}}%
  \expandafter\g@addto@macro\expandafter\blx@includeentries\expandafter{\X}}


% ------------------------------------------------------------------
% LIST OF ABBREVIATIONS
% ------------------------------------------------------------------

\newtoggle{blx@abbrevcite}

\newlength{\abbrevwidth}

\def\setmaxlength#1#2{%
  \ifdim\dimexpr#2>\dimexpr#1
    \global\setlength{#1}{#2}%
  \fi
}

\defbibenvironment{abbreviations}
  {\list
     {\printfield[shorthandwidth]{shortjournal}%
      \iffieldundef{shorthand}
        {\printfield[shorthandwidth]{shortseries}}
        {\iffieldsequal{shorthand}{shorttitle}
           {\printtext{\mkbibemph{\printfield[shorthandwidth]{shorthand}}}}
           {\printfield[shorthandwidth]{shorthand}}}}
     {\setmaxlength{\abbrevwidth}{\shorthandwidth}%
      \setmaxlength{\abbrevwidth}{\shortserieswidth}%
      \setmaxlength{\abbrevwidth}{\shortjournalwidth}%
      \addtolength{\abbrevwidth}{\biblabelsep}%
      \setlength{\labelwidth}{\abbrevwidth}%
      \setlength{\leftmargin}{\labelwidth}%
      \setlength{\labelsep}{0pt}%
      \addtolength{\leftmargin}{\labelsep}%
      \setlength{\itemsep}{\bibitemsep}%
      \setlength{\parsep}{\bibparsep}%
      \renewcommand*{\makelabel}[1]{##1\hss}}}
  {\endlist}
  {\item}

\DeclareBibliographyDriver{abbreviations}{%
  \usebibmacro{begentry}%
  \citereset
  \def\abx@str{abx@lstr}%
  \renewcommand*{\finentrypunct}{}%
  \iffieldundef{shortjournal}
    {}
    {\bibhypertarget{\strfield{shortjournal}}{}%
     \printfield{journaltitle}}%
  \iffieldundef{shorthand}
    {\iffieldundef{shortseries}
       {}
       {\bibhypertarget{\strfield{shortseries}}{}%
        \printfield{series}}
       }
    {\bibhypertarget{\strfield{shorthand}}{}%
     \iffieldsequal{shorthand}{shorttitle}
       {\toggletrue{blx@abbrevcite}}
       {}%
     \usebibmacro{bibentrycite}}%
  \usebibmacro{finentry}}

\DeclareBiblistFilter{abbreviations}{
  \filteror{
    \filter[type=field,filter=shorthand]
    \filter[type=field,filter=shortjournal]
    \filter[type=field,filter=shortseries]
  }
}

\defbibcheck{abbreviations}{%
  \blx@setoptions@entry
  \iftoggle{blx@skipbiblist}{\skipentry}{}%
  \iffieldundef{shorthand}
    {\iffieldundef{shortseries}
       {}
       {\iftoggle{blx@skipbiblistseries}{\skipentry}{}}}
    {\iftoggle{blx@skipbiblistshorthand}{\skipentry}{}}%
  \iftoggle{blx@skipentry}
    {}
    {\iffieldundef{shortjournal}
       {}
       {\ifcsdef{sbl\therefsection\strfield{shortjournal}=\strfield{journaltitle}}
          {\skipentry}
          {\savefieldcs{journaltitle}{sbl\therefsection\strfield{shortjournal}=\strfield{journaltitle}}}}%
     \iffieldundef{shorthand}
       {\iffieldundef{shortseries}
          {}
          {\ifcsdef{sbl\therefsection\strfield{shortseries}=\strfield{series}}
             {\skipentry}
             {\savefieldcs{series}{sbl\therefsection\strfield{shortseries}=\strfield{series}}}}}
       {\ifcsdef{sbl\therefsection\strfield{shorthand}=\strfield{title}}
          {\skipentry}
          {\savefieldcs{shorthand}{sbl\therefsection\strfield{shorthand}=\strfield{title}}}}}}

\DeclareSortingScheme{abbreviations}{%
  \sort{%
    \field{shorthand}%
    \field{shortjournal}%
    \field{shortseries}%
  }%
}

% ------------------------------------------------------------------
% SOURCE MAPS
% ------------------------------------------------------------------
%   - cut titles at colons to create short titles
%   - copy titles to shorttitles if they are empty
% ------------------------------------------------------------------

\DeclareSourcemap{
  \maps{
    \map{
      \step[fieldsource=title, match=\regexp{(.*?):}, final]
      \step[fieldset=shorttitle, fieldvalue={$1}]
    }
    \map{
      \step[fieldsource=revdtitle, match=\regexp{(.*?):}, final]
      \step[fieldset=revdshorttitle, fieldvalue={$1}]
    }
    \map{
      \step[fieldsource=entrysubtype, match=\regexp{ANRW}, final]
      \step[fieldset=options, fieldvalue={skipbib=false}]
    }
    \map[overwrite]{
      \pertype{ancienttext}
      \pertype{classictext}
      \step[fieldsource=related, final]
      \step[fieldset=relatedtype, fieldvalue=ancienttext]
      \step[fieldsource=relatedoptions, match=\regexp{(.*)}]
      \step[fieldset=relatedoptions, fieldvalue={skipbib=false,skipbiblist=false,}]
      \step[fieldset=relatedoptions, fieldvalue={$1}, append]
    }
    \map[overwrite]{
      \step[fieldsource=shortseries, final]
      \step[fieldsource=shorthand, final]
      \step[fieldsource=entrykey, match=\regexp{(.*)}]
      \step[fieldset=xref, fieldvalue=series-$1]
      \step[entrynew=series-$1, entrynewtype=misc]
      \step[fieldsource=shortseries]
      \step[fieldset=shortseries, origfieldval, entrytarget=series-$1]
      \step[fieldsource=series]
      \step[fieldset=series, origfieldval, entrytarget=series-$1]
      \step[fieldset=options, fieldvalue={skipbib}, entrytarget=series-$1]
    }
  }
}

% ------------------------------------------------------------------
% DATA INHERITANCE
% ------------------------------------------------------------------

\DeclareDataInheritance{mvbook,mvcollection,mvreference,mvlexicon,mvcommentary}
    {book,inbook,collection,incollection,reference,inreference,bookinbook,
     lexicon,commentary,inlexicon,incommentary,classictext}{%
  \inherit{title}{maintitle}
  \inherit{subtitle}{mainsubtitle}
  \inherit{titleaddon}{maintitleaddon}
  \inherit{shorttitle}{shortmaintitle}
  \inherit{editor}{maineditor}
  \inherit{translator}{maintranslator}
  \noinherit{sorttitle}
  \noinherit{indextitle}
  \noinherit{indexsorttitle}
  \noinherit{endyear}
}

\DeclareDataInheritance{book,collection,reference,lexicon,commentary}
    {inbook,incollection,inreference, inlexicon,incommentary,
     bookinbook,classictext}{%
  \inherit{title}{booktitle}
  \inherit{subtitle}{booksubtitle}
  \inherit{titleaddon}{booktitleaddon}
  \inherit{shorttitle}{shortbooktitle}
  \inherit{author}{bookauthor}
  \inherit{editor}{bookeditor}
  \inherit{translator}{booktranslator}
  \noinherit{sorttitle}
  \noinherit{indextitle}
  \noinherit{indexsorttitle}
  \noinherit{endyear}
}


% ------------------------------------------------------------------
% LANGUAGE
% ------------------------------------------------------------------
%   - use literals for origlanguage if no translation found
% ------------------------------------------------------------------

\renewcommand*{\lbx@lfromlang}{%
  \iffieldundef{origlanguage}
    {\unspace}
    {\ifbibxstring{from\abx@field@origlanguage}
      {\biblstring{from\thefield{origlanguage}}}
      {from \printfield{origlanguage}}}}

\renewcommand*{\lbx@sfromlang}{%
  \iffieldundef{origlanguage}
    {\unspace}
    {\ifbibstring{from\thefield{origlanguage}}
       {\bibsstring{from\thefield{origlanguage}}}
       {from \printfield{origlanguage}}}}

% ------------------------------------------------------------------
% SET UP DELIMITER MACROS
% ------------------------------------------------------------------

\ExplSyntaxOn
\cs_new_protected:Nn \sbl_ifinstringt:nnn {
   \tl_if_in:nnT {#2}{#1}{#3}}
\cs_generate_variant:Nn \sbl_ifinstringt:nnn { non }
\cs_new_eq:NN \IfInStringT \sbl_ifinstringt:non
\ExplSyntaxOff

\newbibmacro*{setpostnotedelim}{%
  \iffieldundef{postnote}
    {}
    {\IfInStringT{.}{\abx@field@postnote}
       {\renewcommand*{\postnotedelim}{\addspace}}%
     \IfInStringT{:}{\abx@field@postnote}
       {\renewcommand*{\postnotedelim}{\addspace}}%
     \IfInStringT{§}{\abx@field@postnote}
       {\renewcommand*{\postnotedelim}{\addspace}}%
     \IfInStringT{\textsection}{\abx@field@postnote}
       {\renewcommand*{\postnotedelim}{\addspace}}}}

\newbibmacro*{setaltpostnotedelim}{%
  \iffieldundef{altpostnote}
    {}
    {\IfInStringT{.}{\abx@field@altpostnote}
       {\renewcommand*{\postnotedelim}{\addspace}}%
     \IfInStringT{:}{\abx@field@altpostnote}
       {\renewcommand*{\postnotedelim}{\addspace}}%
     \IfInStringT{§}{\abx@field@altpostnote}
       {\renewcommand*{\postnotedelim}{\addspace}}%
     \IfInStringT{\textsection}{\abx@field@altpostnote}
       {\renewcommand*{\postnotedelim}{\addspace}}}}

\newbibmacro*{setpagesdelim}{%
  \iffieldundef{pages}
    {}
    {\IfInStringT{.}{\abx@field@pages}
       {\setunit{\addspace}}%
     \IfInStringT{:}{\abx@field@pages}
       {\setunit{\addspace}}%
     \IfInStringT{§}{\abx@field@pages}
       {\setunit{\addspace}}%
     \IfInStringT{\textsection}{\abx@field@pages}
       {\setunit{\addspace}}}}

% ------------------------------------------------------------------
% JOINING WORD MACROS
% ------------------------------------------------------------------

\newbibmacro*{in}{%
  \printtext{\bibstring{in}}%
  \setunit{\addspace}}

\newbibmacro*{to}{%
  \printtext{\bibstring{to}}%
  \setunit{\addspace}}

\newbibmacro*{of}{%
  \printtext{\bibstring{of}}%
  \setunit{\addspace}}

% ------------------------------------------------------------------
% TITLE MACROS
% ------------------------------------------------------------------

\newbibmacro*{shorttitle}{%
  \iftoggle{blx@useshorttitle}
    {\iffieldundef{shorttitle}
       {\iffieldundef{title}
          {}
          {\printtext[bibhyperlink]{%
             \printtext[title]{\printfield[titlecase]{title}}}}}
       {\printtext[bibhyperlink]{%
          \printtext[title]{\printfield[titlecase]{shorttitle}}}}%
     \newunit}
    {}}

\newbibmacro*{shorttitlenohyperlink}{%
  \iftoggle{blx@useshorttitle}
    {\iffieldundef{shorttitle}
       {\iffieldundef{title}
          {}
          {\printtext[title]{\printfield[titlecase]{title}}}}
       {\printtext[title]{\printfield[titlecase]{shorttitle}}}%
     \newunit}
    {}}

\newbibmacro*{shortbooktitle}{%
  \iffieldundef{shortbooktitle}
    {\iffieldundef{booktitle}
       {}
       {\printtext[booktitle]{\printfield[titlecase]{booktitle}}}}
    {\printtext[booktitle]{\printfield[titlecase]{shortbooktitle}}}%
  \newunit}

\newbibmacro*{shortmaintitle}{%
  \iffieldundef{shortmaintitle}
    {\iffieldundef{maintitle}
       {}
       {\printtext[maintitle]{\printfield[titlecase]{maintitle}}}}
    {\printtext[maintitle]{\printfield[titlecase]{shortmaintitle}}}%
  \newunit}

\renewbibmacro*{title}{%
  \ifciteseen
    {\ifboolexpr{
       test {\ifnameundef{author}}
       and
       not test {\ifnameundef{editor}}
     }
       {}
       {\usebibmacro{shorttitle}}}
    {\ifboolexpr{
       test {\iffieldundef{title}}
       and
       test {\iffieldundef{subtitle}}
     }
       {}
       {\printtext[title]{%
          \printfield[titlecase]{title}%
          \setunit{\subtitlepunct}%
          \printfield[titlecase]{subtitle}}%
        \newunit}%
     \printfield{titleaddon}}}

\renewbibmacro*{booktitle}{%
  \ifciteseen
    {\usebibmacro{shortbooktitle}}
    {\ifboolexpr{
       test {\iffieldundef{booktitle}}
       and
       test {\iffieldundef{booksubtitle}}
     }
       {}
       {\printtext[booktitle]{%
          \printfield[titlecase]{booktitle}%
          \setunit{\subtitlepunct}%
          \printfield[titlecase]{booksubtitle}}%
        \newunit}%
     \printfield{booktitleaddon}}}

\renewbibmacro*{maintitle}{%
  \ifciteseen
    {\usebibmacro{shortmaintitle}}
    {\ifboolexpr{
       test {\iffieldundef{maintitle}}
       and
       test {\iffieldundef{mainsubtitle}}
     }
       {}
       {\printtext[maintitle]{%
          \printfield[titlecase]{maintitle}%
          \setunit{\subtitlepunct}%
          \printfield[titlecase]{mainsubtitle}}%
        \newunit}%
     \printfield{maintitleaddon}}}

\newbibmacro*{booktitle+maintitle}{%
  \iffieldundef{booktitle}
    {\iffieldundef{maintitle}
      {}
      {\usebibmacro{maintitle}%
       \clearfield{maintitle}%
       \clearfield{mainsubtitle}%
       \clearfield{maintitleaddon}
       \newunit}}
    {\usebibmacro{booktitle}%
     \clearfield{booktitle}%
     \clearfield{booksubtitle}%
     \clearfield{bootitleaddon}%
     \newunit}}

\newbibmacro*{shortbooktitle+shortmaintitle}{%
  \iffieldundef{booktitle}
    {\iffieldundef{maintitle}
      {}
      {\usebibmacro{shortmaintitle}
       \newunit}}
    {\usebibmacro{shortbooktitle}%
     \newunit}}

\newbibmacro*{revdshorttitle}{%
  \iffieldundef{revdshorttitle}
     {\iffieldundef{revdtitle}
        {}
        {\printtext[bibhyperlink]{%
           \printtext[revdtitle]{\printfield[titlecase]{revdtitle}}}}}
     {\printtext[bibhyperlink]{%
        \printtext[revdtitle]{\printfield[titlecase]{revdshorttitle}}}}%
   \newunit}

\newbibmacro*{revdtitle}{%
  \ifciteseen
    {\usebibmacro{revdshorttitle}}
    {\ifboolexpr{
       test {\iffieldundef{revdtitle}}
       and
       test {\iffieldundef{revdsubtitle}}
     }
       {}
       {\printtext[revdtitle]{%
          \printfield[titlecase]{revdtitle}%
          \setunit{\subtitlepunct}%
          \printfield[titlecase]{revdsubtitle}}%
        \newunit}%
     \printfield{revdtitleaddon}}}

\newbibmacro*{xrefshortbooktitle+xrefshortmaintitle}{%
  \iffieldundef{xref}
    {\usebibmacro{shortbooktitle+shortmaintitle}}
    {\entrydata{\thefield{xref}}{%
       \iffieldundef{shorttitle}
         {\usebibmacro{shorttitle}}
         {\printtext[shorttitle]{%
            \bibhyperlink{\strfield{shorthand}}{\thefield{shorttitle}}}}}}}

\newbibmacro*{xrefshortmaintitle}{%
  \iffieldundef{xref}
    {\usebibmacro{shortmaintitle}}
    {\entrydata{\thefield{xref}}{%
       \iffieldundef{shorttitle}
         {\usebibmacro{shorttitle}}
         {\printtext[shorttitle]{%
            \bibhyperlink{\strfield{shorthand}}{\thefield{shorttitle}}}}}}}

\newbibmacro*{shortjournal}{%
  \iffieldundef{shortjournal}
    {\printfield{journaltitle}}
    {\printtext[shortjournal]{%
       \bibhyperlink{\strfield{shortjournal}}{\thefield{shortjournal}}}}}

\newbibmacro*{shortjournal+issuetitle}{%
  \usebibmacro{shortjournal}%
  \iffieldundef{series}
    {}
    {\newunit
     \printfield{series}}%
  \usebibmacro{volume+number+eid}%
  \usebibmacro{issue+date}%
  \ifboolexpr{
    test {
      \ifboolexpr{
        not test {\iffieldundef{journaltitle}}
        and
        not test {\iffieldundef{shortjournal}}}}
    and
    test {\iffieldundef{volume}}
    and
    test {\iffieldundef{date}}
  }
    {\usebibmacro{setpagesdelim}}
    {}}

\renewbibmacro*{issue+date}{%
  \ifboolexpr{
    test {\iffieldundef{year}}
    and
    test {\iffieldundef{issue}}
    and
    test {\iffieldundef{date}}
    and
    test {\iffieldundef{month}}
    and
    test {\iffieldundef{day}}
  }
    {}
    {\iffieldundef{volume}
      {\setunit{\addcomma\space}%
        \iffieldundef{issue}
          {\usebibmacro{date}}
          {\printfield{issue}%
           \setunit*{\addspace}%
           \usebibmacro{date}}
        \setunit{\addcomma\space}}
       {\setunit{\addspace}%
        \printtext[parens]{%
          \iffieldundef{issue}
            {\usebibmacro{date}}
            {\printfield{issue}%
             \setunit*{\addspace}%
             \usebibmacro{date}}}%
        \setunit{\addcolon\space}}}}

\renewbibmacro*{volume+number+eid}{%
  \setunit*{\addspace}%
  \printfield{volume}%
  \setunit*{\addperiod}%
  \printfield{number}%
  \setunit{\addcomma\space}%
  \printfield{eid}}

\newbibmacro*{volume+part}{%
  \iffieldundef{volume}
    {}
    {\iffieldundef{part}
       {\usebibmacro{volumeof}}
       {\printfield[vol]{volume}%
        \setunit{\addcomma\addspace}%
        \iffieldundef{maintitle}
          {\printfield[pt]{part}%
           \newunit}
          {\printfield[partof]{part}
           \setunit{\addspace}}}}}

\newbibmacro*{involume+part}{%
  \ifboolexpr{
    test {\iffieldundef{booktitle}}
    and
    not test {\iffieldundef{volume}}
    and
    test {\iffieldundef{pages}}
  }
    {\bibstring{in}%
     \setunit{\space}}
    {}%
  \usebibmacro{volume+part}}

% ------------------------------------------------------------------
% NAME MACROS
% ------------------------------------------------------------------

\newbibmacro*{withname}[1]{%
  \ifnameundef{with#1}
    {}
    {\setunit{\addcomma\space}%
     \iffieldundef{with#1type}
       {\bibstring{with}}
       {\ifbibxstring{with\thefield{with#1type}}
          {\bibstring{with\thefield{with#1type}}}
          {\ifbibxstring{\thefield{with#1type}}
             {\bibstring{\thefield{with#1type}}}
             {\printtext[with#1type]{\thefield{with#1type}}}}}%
     \setunit{\space}%
     \printnames{with#1}%
     \clearname{with#1}}}

\renewbibmacro*{bytypestrg}[2]{%
  \iffieldundef{#1type}
    {\bibstring{by#2}}
    {\ifbibxstring{by\thefield{#1type}}
       {\bibstring{by\thefield{#1type}}}
       {\ifbibxstring{\thefield{#1type}}
          {\bibstring{\thefield{#1type}}}
          {\printtext[editortype]{\thefield{#1type}}}}}}

\renewbibmacro*{editorstrg}{%
  \printtext[editortype]{%
    \iffieldundef{editortype}
      {\ifboolexpr{
         test {\ifnumgreater{\value{editor}}{1}}
         or
         test {\ifandothers{editor}}
       }
         {\bibsstring{editors}}
         {\bibsstring{editor}}}
      {\ifbibxstring{\thefield{editortype}}
         {\ifboolexpr{
            test {\ifnumgreater{\value{editor}}{1}}
            or
            test {\ifandothers{editor}}
          }
            {\bibstring{\thefield{editortype}s}}
            {\bibstring{\thefield{editortype}}}}
         {\thefield{editortype}}}}}

\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]{\bibsstring{\abx@tempa}}\abx@tempb}
    {\usebibmacro{editorstrg}}}

\renewbibmacro*{translatorstrg}{%
  \ifboolexpr{
    test {\ifnumgreater{\value{translator}}{1}}
    or
    test {\ifandothers{translator}}
  }
    {\bibsstring{translators}}
    {\bibsstring{translator}}}
    
\renewbibmacro*{translator+othersstrg}{%
  \ifboolexpr{
    test {\ifnumgreater{\value{translator}}{1}}
    or
    test {\ifandothers{translator}}
  }
    {\def\abx@tempa{translators}}
    {\def\abx@tempa{translator}}%
  \ifnamesequal{translator}{commentator}
    {\appto\abx@tempa{co}%
     \clearname{commentator}}
    {\ifnamesequal{translator}{annotator}
       {\appto\abx@tempa{an}%
        \clearname{annotator}}
       {}}%
  \ifnamesequal{translator}{introduction}
    {\appto\abx@tempa{in}%
     \clearname{introduction}}
    {\ifnamesequal{translator}{foreword}
       {\appto\abx@tempa{fo}%
        \clearname{foreword}}
       {\ifnamesequal{translator}{afterword}
          {\appto\abx@tempa{af}%
           \clearname{afterword}}
          {}}}%
  \bibsstring{\abx@tempa}}

\renewbibmacro*{author}{%
  \ifboolexpr{
    test \ifuseauthor
    and
    not test {\ifnameundef{author}}
  }
    {\ifciteseen{%
       \usebibmacro{ifidemused}
         {}
         {\printnames[labelname]{author}}%
     }{%
       \iffieldundef{authortype}
         {\renewcommand*{\namedashpunct}{\adddot\space}}
         {\renewcommand*{\namedashpunct}{\addcomma\space}}%
       \usebibmacro{dashcheck}
         {\bibnamedash}
         {\usebibmacro{ifidemused}
            {}
            {\printnames{author}%
             \usebibmacro{savehash}}}%
       \iffieldundef{authortype}
         {}
         {\setunit{\addcomma\space}%
          \usebibmacro{authorstrg}}%
       \usebibmacro{withname}{author}}}
    {\global\undef\bbx@lasthash}}

\renewbibmacro*{bybookauthor}{%
  \ifboolexpr{
    test {\ifnameundef{bookauthor}}
    or
    test {\ifnamesequal{author}{bookauthor}}
  }
    {}
    {\usebibmacro{bytypestrg}{author}{author}%
     \setunit{\addspace}%
     \printnames[byauthor]{bookauthor}%
     \usebibmacro{withname}{author}}%
}
     
\renewbibmacro*{editor+others}{%
  \ifboolexpr{
    test \ifuseeditor
    and
    not test {\ifnameundef{editor}}
  }
    {\ifciteseen{%
       \usebibmacro{ifidemused}
         {}
         {\printnames[labelname]{editor}}%
     }{%
       \renewcommand*{\namedashpunct}{\addcomma\space}%
       \usebibmacro{dashcheck}
         {\bibnamedash}
         {\usebibmacro{ifidemused}
            {}
            {\printnames{editor}%
             \usebibmacro{savehash}}}%
       \setunit{\addcomma\space}%
       \usebibmacro{editor+othersstrg}%
       \usebibmacro{withname}{editor}%
     }%
     \clearname{editor}}
    {}}

\renewbibmacro*{translator+others}{%
  \ifboolexpr{
    test \ifusetranslator
    and
    not test {\ifnameundef{translator}}
  }
    {\ifciteseen{%
       \usebibmacro{ifidemused}
         {}
         {\printnames[labelname]{translator}}%
     }{%
       \renewcommand{\namedashpunct}{\addcomma\space}%
       \usebibmacro{dashcheck}
         {\bibnamedash}
         {\usebibmacro{ifidemused}
            {}
            {\printnames{translator}%
             \usebibmacro{savehash}}}%
       \setunit{\addcomma\space}%
       \usebibmacro{translator+othersstrg}%
       \usebibmacro{withname}{translator}%
     }%
     \clearname{translator}}
    {}}

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

\renewbibmacro*{bytranslator}{%
  \ifnameundef{translator}
    {}
    {\bibstring{bytranslator}%
     \setunit{\addspace}%
     \printnames[bytranslator]{translator}%
     \usebibmacro{withname}{translator}}}

\renewbibmacro*{bytranslator+others}{%
  \ifnameundef{translator}
    {}
    {\usebibmacro{bytranslator+othersstrg}%
     \setunit{\addspace}%
     \printnames[bytranslator]{translator}%
     \clearname{translator}%
     \usebibmacro{withname}{translator}%
     \newunit}%
  \usebibmacro{withothers}}

\newbibmacro*{bybookeditor+others}{%
  \ifnameundef{editor}
    {}
    {\usebibmacro{byeditor+othersstrg}%
     \setunit{\addspace}%
     \printnames[byeditor]{editor}%
     \clearname{editor}%
     \usebibmacro{withname}{editor}%
     \newunit}%
  \ifnameundef{bookeditor}
    {}
    {\usebibmacro{byeditor+othersstrg}%
     \setunit{\addspace}%
     \printnames[byeditor]{bookeditor}%
     \clearname{bookeditor}%
     \usebibmacro{withname}{bookeditor}%
     \newunit}%
  \usebibmacro{byeditorx}%
  \usebibmacro{bybooktranslator+others}}

\newbibmacro*{bymaineditor+others}{%
  \ifnameundef{editor}
    {}
    {\usebibmacro{byeditor+othersstrg}%
     \setunit{\addspace}%
     \printnames[byeditor]{editor}%
     \clearname{editor}%
     \usebibmacro{withname}{editor}%
     \newunit}%
  \ifnameundef{maineditor}
    {}
    {\usebibmacro{byeditor+othersstrg}%
     \setunit{\addspace}%
     \printnames[byeditor]{maineditor}%
     \clearname{maineditor}%
     \usebibmacro{withname}{maineditor}%
     \newunit}%
  \usebibmacro{byeditorx}%
  \usebibmacro{bymaintranslator+others}}

\newbibmacro*{bybooktranslator+others}{%
  \ifnameundef{translator}
    {}
    {\usebibmacro{bytranslator+othersstrg}%
     \setunit{\addspace}%
     \printnames[bytranslator]{translator}%
     \clearname{translator}%
     \usebibmacro{withname}{translator}%
     \newunit}%
  \ifnameundef{booktranslator}
    {}
    {\usebibmacro{bytranslator+othersstrg}%
     \setunit{\addspace}%
     \printnames[bytranslator]{booktranslator}%
     \clearname{booktranslator}%
     \usebibmacro{withname}{booktranslator}%
     \newunit}%
  \usebibmacro{withothers}}

\newbibmacro*{bymaintranslator+others}{%
  \ifnameundef{translator}
    {}
    {\usebibmacro{bytranslator+othersstrg}%
     \setunit{\addspace}%
     \printnames[bytranslator]{translator}%
     \clearname{translator}%
     \usebibmacro{withname}{translator}%
     \newunit}%
  \ifnameundef{maintranslator}
    {}
    {\usebibmacro{bytranslator+othersstrg}%
     \setunit{\addspace}%
     \printnames[bytranslator]{maintranslator}%
     \clearname{maintranslator}%
     \usebibmacro{withname}{maintranslator}%
     \newunit}%
  \usebibmacro{withothers}}

\newbibmacro*{revdauthor}{%
  \ifboolexpr{
    test \ifuseauthor
    and
    not test {\ifnameundef{revdauthor}}
  }
    {\bibstring{byauthor}%
     \setunit{\addspace}%
     \ifciteseen{%
       \printnames[labelname]{revdauthor}%
     }{%
       \printnames{revdauthor}%
       \iffieldundef{authortype}
         {}
         {\setunit{\addcomma\space}%
          \usebibmacro{authorstrg}}}}
    {}}

\newbibmacro*{revdeditor}{%
  \ifboolexpr{
    test \ifuseeditor
    and
    not test {\ifnameundef{revdeditor}}
  }
    {\bibsstring{byeditor}%
     \setunit{\addspace}%
     \ifciteseen{%
       \printnames[labelname]{revdeditor}%
     }{%
       \printnames{revdeditor}%
     }%
     \clearname{revdeditor}}
    {}}

\newbibmacro*{revdauthor/revdeditor}{%
  \ifboolexpr{
    test \ifuseauthor
    and
    not test {\ifnameundef{revdauthor}}
  }
    {\usebibmacro{revdauthor}}
    {\usebibmacro{revdeditor}}}


\newbibmacro*{revdauthor/revdeditor+revdtitle}{%
  \biblstring{reviewof}%
  \setunit{\addspace}%
  \usebibmacro{revdauthor/revdeditor}%
  \ifciteseen
    {}
    {\setunit{\addcomma\space}%
     \usebibmacro{revdtitle}}}

\newbibmacro*{shortseries}{%
  \iffieldundef{shortseries}
    {\printfield{series}}
    {\printtext[shortseries]{%
       \bibhyperlink{\strfield{shortseries}}{\thefield{shortseries}}}}%
}

\newbibmacro*{shortseries+number}{%
  \usebibmacro{shortseries}%
  \setunit{\addspace}%
  \iffieldundef{seriesseries}
    {}
    {\printfield{seriesseries}%
     \printtext{/}}%
  \printfield{number}%
  \newunit}

\newbibmacro*{eventtitle}{%
  \iffieldundef{eventtitle}
    {}
    {\ifbibliography
       {}
       {\midsentence}%
     \bibstring{paperpresented}%
     \setunit{\addspace}%
     \printfield{eventtitle}}}

\newbibmacro*{venue+eventdate}{%
  \iffieldundef{venue}
    {\printlist{location}}
    {\printfield{venue}}%
  \setunit{\addcomma\space}%
  \iffieldundef{eventyear}
    {\printdate}
    {\printeventdate}}

\newbibmacro*{parens+publisher+location+date}{%
  \setunit{\space}%
  \printtext[parens]{%
    \usebibmacro{publisher+location+date}%
    \iffieldequalstr{relatedtype}{reprint}
      {\renewcommand*{\newunitpunct}{\addsemicolon\space}%
       \newunit
       \usebibmacro{related:init}%
       \usebibmacro{related}%
       \global\toggletrue{relatedseen}}
      {}}}

\newcounter{currentpublisher}
\newcounter{currentlocation}
\newcounter{publishertotal}
\newcounter{locationtotal}

\DeclareListFormat{publisher}{%
  \setcounter{publishertotal}{\value{listtotal}}%
  \usebibmacro{list:delim}{#1}%
  #1\isdot
  \ifnumequal{\value{currentpublisher}}{\value{listtotal}}
    {\setcounter{currentpublisher}{0}}
    {\addtocounter{currentpublisher}{1}}}

\DeclareListFormat{location}{%
  \setcounter{locationtotal}{\value{listtotal}}%
  \usebibmacro{list:delim}{#1}%
  #1\isdot
  \ifnumequal{\value{currentlocation}}{\value{listtotal}}
    {\setcounter{currentlocation}{0}}
    {\addtocounter{currentlocation}{1}}}

\newbibmacro*{loop:location+publisher}{%
  \ifnumequal{\value{currentlocation}}{0}
    {}
    {\printlist[][\value{currentlocation}-\value{currentlocation}]{location}%
     \setunit*{\addcolon\space}}%
  \ifnumequal{\value{currentpublisher}}{0}
    {}
    {\printlist[][\value{currentpublisher}-\value{currentpublisher}]{publisher}}%
  \setunit*{\addsemicolon\space}%
  \ifboolexpr{
    test {\ifnumgreater{\value{locationtotal}}{1}}
    and
    test {\ifnumgreater{\value{currentlocation}}{\value{publishertotal}}}
  }
    {\setcounter{currentlocation}{0}}
    {}%
  \ifboolexpr{
    test {\ifnumgreater{\value{currentpublisher}}{0}}
    or
    test {\ifnumgreater{\value{currentlocation}}{0}}
  }
    {\usebibmacro{loop:location+publisher}}
    {}}

\newbibmacro*{init:location+publisher}{%
  \iflistundef{location}
    {\setcounter{currentlocation}{0}}
    {\setcounter{currentlocation}{1}}%
  \iflistundef{publisher}
    {\setcounter{currentpublisher}{0}}
    {\setcounter{currentpublisher}{1}}}

\renewbibmacro*{publisher+location+date}{%
  \usebibmacro{reprint}%
  \usebibmacro{init:location+publisher}%
  \usebibmacro{loop:location+publisher}%
  \setunit{\addcomma\space}%
  \usebibmacro{date}%
  \newunit}

\newbibmacro*{origpublisher+location+date}{%
  \printlist{origlocation}%
  \iflistundef{origpublisher}
    {\setunit*{\addcomma\space}}
    {\setunit*{\addcolon\space}}%
  \printlist{origpublisher}%
  \setunit*{\addcomma\space}%
  \usebibmacro{origdate}%
  \newunit}

\newbibmacro*{volume}{%
  \iffieldundef{volume}
    {}
    {\setunit{\addspace}%
     \global\booltrue{usevolpostnotedelim}%
     \printfield{volume}%
     \newunit}}

\newbibmacro*{volumeof}{%
  \iffieldundef{volume}
    {}
    {\iffieldundef{part}
      {\iffieldundef{maintitle}
        {\ifboolexpr{
           test {\iftoggle{blx@usevolume}}
           or
           test {\ifbibliography}
         }
           {\printfield[vol]{volume}%
            \newunit}
           {}}
        {\printfield[volumeof]{volume}%
         \setunit{\addspace}}}
      {}}}%

\newbibmacro*{volume+pages}{%
  \iffieldundef{volume}
    {}
    {\setunit{\addspace}%
     \printfield{volume}%
     \setunit{\volpostnotedelim}}
  \usebibmacro{pages}}

\newbibmacro*{volume+part+pages}{%
  \ifboolexpr{
    test {\iffieldundef{volume}}
    or
    test {\iffieldundef{part}}
  }
    {}
    {\printfield{volume}%
     \setunit{\addperiod}%
     \printfield{part}%
     \setunit{\volpostnotedelim}}
  \usebibmacro{pages}}

\newbibmacro*{volume+part+nomaintitle}{%
  \iffieldundef{volume}
    {}
    {\iftoggle{blx@usevolume}
       {}
       {\printfield{volume}%
        \setunit{\addperiod}%
        \printfield{part}%
        \global\booltrue{usevolpostnotedelim}}}}

\renewbibmacro*{url+urldate}{%
  \iffieldundef{urlyear}
    {}
    {\printurldate}%
  \newunit
  \printfield{url}}

\newbibmacro*{ebookedition}{%
  \iffieldundef{ebookedition}
    {}
    {\printfield{ebookedition}}}

\newbibmacro*{reprint}{%
  \ifboolexpr{
    not test {\iffieldundef{origpublisher}}
    or
    not test {\iffieldundef{origlocation}}
    or
    not test {\iffieldundef{origyear}}
  }
    {\usebibmacro{origpublisher+location+date}%
     \ifbibliography
       {\setunit{\addperiod\space}}
       {\setunit{\addsemicolon\space}}%
     \bibstring{reprint}%
     \setunit{\addcomma\addspace}}
    {}}

\renewbibmacro*{date}{%
  \printdate
  \setunit{\addcomma\addspace}%
  \printfield{pubstate}}

\newbibmacro*{origdate}{\printorigdate}

\newbibmacro*{releasedate}{\printreleasedate}


% ------------------------------------------------------------------
% RELATED MACROS
% ------------------------------------------------------------------

\csundef{abx@ffd@*@related:multivolume}
\csundef{abx@ffd@*@related:origpubin}
\csundef{abx@ffd@*@related:origpubas}
\csundef{abx@ffd@*@relatedstring:reprintfrom}
\csundef{abx@macro@related:multivolume}
\csundef{abx@macro@related:bytranslator}
\csundef{abx@macro@related:origpubas}
\csundef{abx@macro@related:origpubin}
\csundef{abx@macro@related:reprintfrom}

\renewbibmacro*{begrelated}{%
  \global\booltrue{bbx@inset}}

\renewbibmacro*{endrelated}{%
  \global\boolfalse{bbx@inset}%
  \usebibmacro*{savehash}}

\renewbibmacro*{related:default}[1]{%
  \entrydata*{#1}{%
    \usedriver
      {\ifnameundef{savedauthor}
         {\ifnameundef{savededitor}
            {}
            {\ifnamesequal{editor}{savededitor}
               {\clearname{editor}}
               {}}}
         {\ifnamesequal{author}{savedauthor}
            {\clearname{author}}
            {}}%
       \renewbibmacro*{related:init}{%
         \ifnameundef{savedauthor}
           {}
           {\restorename{author}{\abx@name@savedauthor}}%
         \ifnameundef{savededitor}
           {}
           {\restorename{editor}{\abx@namesavededitor}}}%
       \DeclareNameAlias{sortname}{default}}%
      {\ifbibliography
         {\thefield{entrytype}}
         {cite:\thefield{entrytype}}}%
    \setunit{\relateddelim}%
    \usebibmacro{related}}}

\newbibmacro*{related:reprint}[1]{%
  \entrydata*{#1}{%
    \usedriver
      {\renewbibmacro*{related:init}{}%
       \ifbibliography
         {\renewbibmacro*{parens+publisher+location+date}{%
            \newunit
            \usebibmacro{publisher+location+date}}}
         {}}
      {\ifbibliography
         {\thefield{entrytype}}
         {cite:\thefield{entrytype}}}}}

\newbibmacro*{related:ancienttext}[1]{%
  \global\togglefalse{blx@testpostnotedelim}%
  \togglefalse{blx@citeindex}%
  \ifciteseen{\let\ifciteseen\@firstoftwo}{}%
  \entrydata*{#1}{%
    \usebibmacro{ifuseshorthand}
      {\usebibmacro{cite:shorthand}%
       \usebibmacro{volume}%
       \setunit{\addperiod}%
       \printfield{part}%
       \global\toggletrue{blx@testpostnotedelim}}
      {\usedriver{}{cite:\thefield{entrytype}}%
       \ifboolexpr{
         test {\ifciteseen}
         and
         not togl {blx@useshorttitle}
       }
         {\global\toggletrue{blx@testpostnotedelim}}
         {}}}}

\newbibmacro*{ifuseshorthand}{%
  \iffieldundef{shorthand}
    {\@secondoftwo}
    {\ifcsstring{blx@shorthand}{false}
       {\@secondoftwo}
       {\ifciteseen
          {\@firstoftwo}
          {\ifcsstring{blx@shorthand}{short}
             {\@secondoftwo}
             {\@firstoftwo}}}}}


% ------------------------------------------------------------------
% ANCIENT AND CLASSIC TEXT MACROS
% ------------------------------------------------------------------

\newtoggle{blx@testpostnotedelim}

\newbibmacro*{volume+part+postnote}{%
  \usebibmacro{volume}%
  \iffieldundef{part}
    {}
    {\setunit{\addperiod}%
     \printfield{part}}%
  \iftoggle{blx@testpostnotedelim}{\usebibmacro{setpostnotedelim}}{}%
  \ifbool{usevolpostnotedelim}
    {\setunit{\volpostnotedelim}}
    {\setunit{\postnotedelim}}%
  \usebibmacro{postnote}%
  \global\booltrue{suppresspostnote}}

\newbibmacro*{classic:translator+series}{%
  \ifboolexpr{
    not test {\ifnameundef{translator}}
    or
    not test {\iffieldundef{series}}
  }
    {\setunit{\addspace}%
     \printtext[parens]{%
       \printnames[labelname]{translator}%
       \newunit
       \usebibmacro{shortseries+number}}}
    {}}


% ------------------------------------------------------------------
% ALTERNATE POSTNOTE MACROS
% ------------------------------------------------------------------

\ExplSyntaxOn
\cs_generate_variant:Nn \__xparse_split_argument:nnn { nno }
\cs_new_eq:NN \eSplitArgument  \__xparse_split_argument:nno
\DeclareDocumentCommand \splitpostnote
{ > { \eSplitArgument { 1 } { | } } m }
{ \savepostnotes #1 }
\DeclareDocumentCommand{\savepostnotes}{mm}{%
  \IfNoValueTF{#2}
    {\gdef\postnotelast{#1}}
    {\tl_if_empty:nF {#1}
       {\gdef\postnotefirst{#1}}%
     \tl_if_empty:nF {#2}
       {\gdef\postnotelast{#2}}}}
\ExplSyntaxOff

\def\setuppostnotes{%
  \global\undef\postnotefirst
  \global\undef\postnotelast
  \splitpostnote{\abx@field@postnote}}

\renewbibmacro*{prenote}{%
  \iffieldundef{postnote}
    {}
    {\setuppostnotes
     \restorefield{postnote}{\postnotelast}%
     \restorefield{altpostnote}{\postnotefirst}}%
  \iffieldundef{prenote}
    {}
    {\printfield{prenote}%
     \setunit{\prenotedelim}}}

\DeclareFieldFormat{altpostnote}{\mkcomprange{#1}}

% ------------------------------------------------------------------
% BIBLIOGRAPHY REPEATED AUTHOR MACROS
% ------------------------------------------------------------------

\newbool{bbx@inset}

\renewcommand*{\bibnamedash}{%
  \leavevmode\raise 0.6ex\hbox to 3em{\hrulefill}\namedashpunct}

\newbibmacro*{bbx:dashcheck}[2]{%
  \ifboolexpr{
    test {\iffieldequals{fullhash}{\bbx@lasthash}}
    and
    not test \iffirstonpage
    and
    (
       not bool {bbx@inset}
       or
       test {\iffieldequalstr{entrysetcount}{1}}
    )
  }
    {#1}
    {#2}}

\newbibmacro*{cbx:dashcheck}[2]{#2}

\newbibmacro*{dashcheck}[2]{%
  \usebibmacro{cbx:dashcheck}{#1}{#2}}

\newbibmacro*{savehash}{}

% ------------------------------------------------------------------
% ENTRY SET MACROS
% ------------------------------------------------------------------

\def\cbx@entryset#1{%
  \blx@ifdata{#1}
    {\begingroup
     \blx@imc@clearlist{pageref}%
     \blx@getdata{#1}%
     \blx@setoptions@type\abx@field@entrytype
     \def\abx@field@entrysetcount{1}%
     \blx@entryset@precode
     \blx@driver{cite:\blx@imc@thefield{entrytype}}%
     \blx@entryset@postcode
     \endgroup}
    {}%
  \let\do\cbx@entryset@i}

\def\cbx@entryset@i#1{%
  \blx@ifdata{#1}
    {\begingroup
     \blx@resetdata
     \blx@getdata{#1}%
     \blx@entrysetcount
     \blx@setoptions@type\abx@field@entrytype
     \blx@setoptions@entry
     \addtocounter{instcount}\@ne
     \blx@execute
     \blx@beglangbib
     \blx@begunit
     \blx@entryset@precode
     \blx@driver{cite:\blx@imc@thefield{entrytype}}
     \blx@entryset@postcode
     \blx@endunit
     \blx@endlangbib
     \endgroup}
    {\blx@nounit}}

\def\bbx@entryset#1{%
  \blx@ifdata{#1}
    {\begingroup
     \blx@imc@clearlist{pageref}%
     \blx@getdata{#1}%
     \blx@setoptions@type\abx@field@entrytype
     \def\abx@field@entrysetcount{1}%
     \blx@entryset@precode
     \blx@driver{\blx@imc@thefield{entrytype}}%
     \blx@entryset@postcode
     \endgroup}
    {}%
  \let\do\bbx@entryset@i}

\def\bbx@entryset@i#1{%
  \blx@ifdata{#1}
    {\begingroup
     \blx@resetdata
     \blx@getdata{#1}%
     \blx@entrysetcount
     \blx@setoptions@type\abx@field@entrytype
     \blx@setoptions@entry
     \addtocounter{instcount}\@ne
     \blx@execute
     \blx@beglangbib
     \blx@begunit
     \blx@entryset@precode
     \blx@driver{\blx@imc@thefield{entrytype}}
     \blx@entryset@postcode
     \blx@endunit
     \blx@endlangbib
     \endgroup}
    {\blx@nounit}}


\def\blx@entryset#1{\cbx@entryset{#1}}
\def\blx@entryset@i#1{\cbx@entryset@i{#1}}

% ------------------------------------------------------------------
% INDEXING MACROS
% ------------------------------------------------------------------

\renewbibmacro*{bibindex}{%
  \ifbibindex
    {\indexnames{labelname}}
    {}}

\renewbibmacro*{citeindex}{%
  \ifciteindex
    {\indexnames{labelname}}
    {}}
