\RequireBiber[3]

\RequirePackage{xpatch}

% ## Set up the version strings

\def\sbl@abx@date{2026-04-29}
\def\sbl@abx@version{2.0}
\def\sbl@abx@bbxid{\sbl@abx@date\space v\sbl@abx@version\space biblatex-sbl bibliography style (DCP)}
\def\sbl@abx@cbxid{\sbl@abx@date\space v\sbl@abx@version\space biblatex-sbl citation style (DCP)}
\def\sbl@abx@lbxid{\sbl@abx@date\space v\sbl@abx@version\space biblatex-sbl localization (DCP)}
\def\sbl@abx@dbxid{\sbl@abx@date\space v\sbl@abx@version\space biblatex-sbl database structure (DCP)}


% ## Utilities

% register sbl internal commands
\protected\def\sbl@blx@regimc#1{%
  \xappto\blx@blxinit{%
    \let\noexpand#1\expandafter\noexpand\csname
    sbl@blx@imc@\expandafter\@gobble\string#1\endcsname}}

\protected\def\sbl@blx@regimcs#1{\sbl@blx@regimcs@i#1&}
\def\sbl@blx@regimcs@i#1{%
  \ifx#1&\else
    \sbl@blx@regimc#1%
    \expandafter\sbl@blx@regimcs@i
  \fi}


% ## Style options

\def\sbl@blx@nonlatintitleformat{title}
\DeclareBiblatexOption{entry}[boolean]{nonlatintitle}[true]{%
  \ifstrequal{#1}{true}
    {\def\sbl@blx@nonlatintitleformat{nonlatin}}
    {\def\sbl@blx@nonlatintitleformat{title}}}

\def\sbl@blx@nonlatinbooktitleformat{booktitle}
\DeclareBiblatexOption{entry}[boolean]{nonlatinbooktitle}[true]{%
  \ifstrequal{#1}{true}
    {\def\sbl@blx@nonlatinbooktitleformat{nonlatin}}
    {\def\sbl@blx@nonlatinbooktitleformat{booktitle}}}

\def\sbl@blx@nonlatinmaintitleformat{maintitle}
\DeclareBiblatexOption{entry}[boolean]{nonlatinmaintitle}[true]{%
  \ifstrequal{#1}{true}
    {\def\sbl@blx@nonlatinmaintitleformat{nonlatin}}
    {\def\sbl@blx@nonlatinmaintitleformat{maintitle}}}

\newtoggle{sbl@blx@partsinvols}
\DeclareBiblatexOption{entry}[boolean]{partsinvols}[true]{%
  \settoggle{sbl@blx@partsinvols}{#1}}

\def\sbl@blx@shorthandformat{shorthand}
\DeclareBiblatexOption{entry}[string]{shorthandformat}{%
  \def\sbl@blx@shorthandformat{#1}}

\newtoggle{sbl@blx@usetitle}
\DeclareBiblatexOption{type,entry}[boolean]{usetitle}[true]{%
  \settoggle{sbl@blx@usetitle}{#1}}

\newtoggle{sbl@blx@usexref}
\toggletrue{sbl@blx@usexref}
\DeclareBiblatexOption{entry}[boolean]{usexref}[true]{%
  \settoggle{sbl@blx@usexref}{#1}}

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

\def\blx@ifciteidem@citation{%
  \ifbool{citetracker}
    {\iftoggle{blx@footnote}
       {\blx@ifmpfncheckidem
          {\blx@imc@iffieldequals{fullhash}\blx@lasthash@foot}
          {\@secondoftwo}}
       {\iffirstcitekey
          {\@secondoftwo}
          {\blx@imc@iffieldequals{fullhash}\blx@lasthash@text}}}
    {\@secondoftwo}}

\def\blx@ifmpfncheckidem{%
  \ifnum\numexpr\value\@mpfn-\blx@lastmpfn<\@ne
    \expandafter\@firstoftwo
  \else
    \expandafter\@secondoftwo
  \fi}

% set a field value
\newrobustcmd*{\sbl@blx@imc@setfield}[3][]{%
  \ifstrequal{#1}{overwrite}
    {\csdef{abx@field@#2}{#3}}
    {\iffieldundef{#2}
       {\csdef{abx@field@#2}{#3}}
       {}}}

% test if bibmacro defined
\newrobustcmd*{\sbl@blx@imc@ifdefbibmacro}[1]{%
  \ifcsdef{abx@macro@#1}
    {\@firstoftwo}
    {\@secondoftwo}}

\sbl@blx@regimcs{\setfield \ifdefbibmacro}

% preserve semicolons in page ranges (default uses \bibgrangessep)
\begingroup
\catcode`\&=3
\global\def\blx@range@chunk@semcol#1;#2&{%
  \notblank{#1}
    {\blx@range@chunk@comma#1,&}
    {}%
  \notblank{#2}
    {\notblank{#1}{\blx@range@out@delim{\addsemicolon\space}}{}%
     \blx@range@chunk@semcol#2&}
    {}}
\endgroup


% ## Name tracker

\newbool{nametracker}

% define global and context name trackers for labelname
\def\sbl@blx@nametracker@global{%
  \ifbool{nametracker}
    {\ifdef{\abx@field@namehash}
       {\xifinlistcs\abx@field@namehash{sbl@blx@name@bsee@\the\c@refsection}
          {}
          {\listcsxadd{sbl@blx@name@bsee@\the\c@refsection}\abx@field@namehash}}
       {}}
    {}}

\def\sbl@blx@nametracker@context{%
  \ifbool{nametracker}
    {\ifdef{\abx@field@namehash}
       {\iftoggle{blx@footnote}
          {\xifinlistcs\abx@field@namehash{sbl@blx@name@fsee@\the\c@refsection}
             {}
             {\listcsxadd{sbl@blx@name@fsee@\the\c@refsection}\abx@field@namehash}}
          {\xifinlistcs\abx@field@namehash{sbl@blx@name@bsee@\the\c@refsection}
             {}
             {\listcsxadd{sbl@blx@name@bsee@\the\c@refsection}\abx@field@namehash}}}
       {}}
    {}}

% define global and context tests for if a labelname has previously been seen
\def\sbl@blx@ifnameseen@global{%
  \ifbool{nametracker}
    {\xifinlistcs{\abx@field@namehash}{sbl@blx@name@bsee@\the\c@refsection}}
    {\@secondoftwo}}

\def\sbl@blx@ifnameseen@context{%
  \ifbool{nametracker}
    {\iftoggle{blx@footnote}
       {\xifinlistcs{\abx@field@namehash}{sbl@blx@name@fsee@\the\c@refsection}}
       {\xifinlistcs{\abx@field@namehash}{sbl@blx@name@bsee@\the\c@refsection}}}
    {\@secondoftwo}}

% define command to reset the name tracker
\newrobustcmd*{\sbl@blx@namereset@force}{%
  \global\cslet{sbl@blx@name@bsee@\the\c@refsection}\@empty
  \global\cslet{sbl@blx@name@fsee@\the\c@refsection}\@empty}

% patch \citereset so that it also resets the sbl name tracker
\patchcmd{\citereset}
  {\blx@ibidreset@force}
  {\sbl@blx@namereset@force
   \blx@ibidreset@force}
  {}
  {}

% disable name tracker in floats unless the trackfloats option is set
\AtEndPreamble{%
  \iftoggle{blx@trackfloats}
    {}
    {\apptocmd\@floatboxreset
       {\boolfalse{nametracker}}
       {}
       {\blx@err@patch{floats}}}}

\DeclareBiblatexOption{global,type,entry}[string]{nametracker}[true]{%
  \ifcsdef{sbl@blx@opt@nametracker@#1}
    {\csuse{sbl@blx@opt@nametracker@#1}}
    {\blx@err@invopt{nametracker=#1}{}}}
\def\sbl@blx@opt@nametracker@true{%
  \let\sbl@blx@imc@ifnameseen\sbl@blx@ifnameseen@global
  \let\sbl@blx@imc@nametracker\sbl@blx@nametracker@global
  \booltrue{nametracker}}
\def\sbl@blx@opt@nametracker@false{%
  \let\sbl@blx@imc@ifnameseen\@secondoftwo
  \let\sbl@blx@imc@nametracker\relax}
\def\sbl@blx@opt@nametracker@context{%
  \let\sbl@blx@imc@ifnameseen\sbl@blx@ifnameseen@context
  \let\sbl@blx@imc@nametracker\sbl@blx@nametracker@context
  \booltrue{nametracker}}
\def\sbl@blx@opt@nametracker@strict{%
  \let\sbl@blx@imc@ifnameseen\sbl@blx@ifnameseen@global
  \def\sbl@blx@imc@nametracker{%
    \blx@ifcitesingle{\sbl@blx@nametracker@global}{}}%
  \booltrue{nametracker}}
\def\sbl@blx@opt@nametracker@constrict{%
  \let\sbl@blx@imc@ifnameseen\sbl@blx@ifnameseen@context
  \def\sbl@blx@imc@nametracker{%
    \blx@ifcitesingle{\sbl@blx@nametracker@context}{}}%
  \booltrue{nametracker}}

\sbl@blx@regimcs{\ifnameseen \nametracker}


% ## Bibliography record manipulation

% flag to control whether to insert subtitle punctuation (the biblatex
% punctation tracker isn't quite smart enough for for SBL style)
\newtoggle{sbl@blx@addsubtitlepunct}

\DeclareStyleSourcemap{
  \maps{
    % Copy title fields to journaltitle fields for @periodical entry types and
    % then issuetitle fields to title; makes life easier
    \map[overwrite]{
      \pertype{periodical}
      \step[fieldsource=title, fieldtarget=journaltitle]
      \step[fieldsource=subtitle, fieldtarget=journalsubtitle]
      \step[fieldsource=shorttitle, fieldtarget=shortjournal]
      \step[fieldsource=titleaddon, fieldtarget=journaltitleaddon]
      \step[fieldsource=issuetitle, fieldtarget=title]
      \step[fieldsource=issuesubtitle, fieldtarget=subtitle]
      \step[fieldsource=shortissue, fieldtarget=shorttitle]
      \step[fieldsource=issuetitleaddon, fieldtarget=titleaddon]
      \step[fieldset=issuetitle, null]
      \step[fieldset=issuesubtitle, null]
      \step[fieldset=shortissue, null]
      \step[fieldset=issuetitleaddon, null]
    }
    % Split titles into titles and subtitles (ignoring braced colons)
    \map[overwrite]{
      \step[notfield=subtitle, final]
      \step[fieldsource=title, match=\regexp{(.*?)(?=(?:[^{}]*\{[^{}]*\})*[^{}]*$):\s(.*)}, final]
      \step[fieldset=title, fieldvalue={$1}]
      \step[fieldset=subtitle, fieldvalue={$2}]
    }
    \map[overwrite]{
      \step[notfield=revdsubtitle, final]
      \step[fieldsource=revdtitle, match=\regexp{(.*?)(?=(?:[^{}]*\{[^{}]*\})*[^{}]*$):\s(.*)}, final]
      \step[fieldset=revdtitle, fieldvalue={$1}]
      \step[fieldset=revdsubtitle, fieldvalue={$2}]
    }
    \map[overwrite]{
      \step[notfield=translatedsubtitle, final]
      \step[fieldsource=translatedtitle, match=\regexp{(.*?)(?=(?:[^{}]*\{[^{}]*\})*[^{}]*$):\s(.*)}, final]
      \step[fieldset=translatedtitle, fieldvalue={$1}]
      \step[fieldset=translatedsubtitle, fieldvalue={$2}]
    }
    % Populate shorttitle from translatedtitle, removing *A, And, The*
    \map{
      \pernottype{ancienttext}
      \step[fieldsource=translatedtitle, match=\regexp{^(A|An|The)\s+(.+)}, final]
      \step[fieldset=shorttitle, fieldvalue={$2}]
    }
    \map{
      \pernottype{ancienttext}
      \step[fieldsource=translatedtitle, final]
      \step[fieldset=shorttitle, origfieldval]
    }
    % Remove *A, An, The* from title and place in shorttitle and sorttitle
    \map{
      \pernottype{ancienttext}
      \step[fieldsource=title, match=\regexp{^(A|An|The)\s+(.+)}, final]
      \step[fieldset=shorttitle, fieldvalue={$2}]
    }
    \map{
      \step[fieldsource=title, match=\regexp{^(A|An|The)\s+(.+)}, final]
      \step[fieldset=sorttitle, fieldvalue={$2}]
    }
    \map{
      \pertype{ancienttext}
      \step[fieldsource=entrysubtype, final]
      \step[fieldsource=title, match=\regexp{^(A|An|The)\s+(.+)}, final]
      \step[fieldset=shorttitle, fieldvalue={$2}]
      \step[fieldset=sorttitle, fieldvalue={$2}]
    }
    \map{
      \step[fieldsource=revdtitle, match=\regexp{^(A|An|The)\s+(.+)}, final]
      \step[fieldset=revdshorttitle, fieldvalue={$2}]
    }
    % add toggle to control subtitle punctuation (taken from biblatex-chicago)
    \map[overwrite]{
      \step[fieldsource=subtitle, final]
      \step[fieldsource=title,
      match=\regexp{\\(mkbib|en)quote\{.+((\?|\!)\})$}, final]
      \step[fieldset=title, fieldvalue=\global\toggletrue{sbl@blx@addsubtitlepunct}, append]
    }
    \map[overwrite]{
      \step[fieldsource=booksubtitle, final]
      \step[fieldsource=booktitle,
      match=\regexp{\\(mkbib|en)quote\{.+((\?|\!)\})$}, final]
      \step[fieldset=booktitle, fieldvalue=\global\toggletrue{sbl@blx@addsubtitlepunct}, append]
    }
    \map[overwrite]{
      \step[fieldsource=mainsubtitle, final]
      \step[fieldsource=maintitle,
      match=\regexp{\\(mkbib|en)quote\{.+((\?|\!)\})$}, final]
      \step[fieldset=maintitle, fieldvalue=\global\toggletrue{sbl@blx@addsubtitlepunct}, append]
    }
    % articles with an integer issue and no volume treat issue like volume
    \map{
      \pertype{article}
      \step[notfield=volume, final]
      \step[fieldsource=issue, match=\regexp{\d+}, final]
      \step[fieldset=volume, origfieldval]
      \step[fieldset=issue, null]
    }
    % articles with an integer issue and volume drop issue
    \map{
      \pertype{article}
      \step[fieldsource=volume, final]
      \step[fieldsource=issue, match=\regexp{\d+}, final]
      \step[fieldset=issue, null]
    }
    % incommentary and incollection with an xref uses inreference driver
    \map[overwrite]{
      \step[fieldsource=xref, final]
      \step[typesource=incommentary, typetarget=inreference]
      \step[typesource=incollection, typetarget=inreference]
    }
    % ensure all entries (included related clones) are considered for the list of abbreviations
    \map[overwrite]{
      \step[fieldsource=options, match=\regexp{(.*)}]
      \step[fieldset=options, fieldvalue={skipbiblist=false,}]
      \step[fieldset=options, fieldvalue={$1}, append]
    }
    % ancienttext without entrysubtype not in abbreviations
    \map[overwrite]{
      \pertype{ancienttext}
      \step[fieldsource=entrysubtype, final]
      \step[fieldsource=options, match=\regexp{(.*)}]
      \step[fieldset=options, fieldvalue={skipbiblist,}]
      \step[fieldset=options, fieldvalue={$1}, append]
    }
    % entries with shorthand not in bibliography
    \map[overwrite]{
      \step[fieldsource=shorthand, final]
      \step[fieldsource=options, match=\regexp{(.*)}]
      \step[fieldset=options, fieldvalue={skipbib,}]
      \step[fieldset=options, fieldvalue={$1}, append]
    }
    % @series entries not in bibliography
    \map[overwrite]{
      \pertype{series}
      \step[fieldsource=options, match=\regexp{(.*)}]
      \step[fieldset=options, fieldvalue={skipbib,}]
      \step[fieldset=options, fieldvalue={$1}, append]
    }
    % @periodical entries without a title not in bibliography
    \map[overwrite]{
      \pertype{periodical}
      \step[notfield=title, final]
      \step[fieldsource=options, match=\regexp{(.*)}]
      \step[fieldset=options, fieldvalue={skipbib,}]
      \step[fieldset=options, fieldvalue={$1}, append]
    }
    % blog and comment entrysubtypes do not appear in the bibliography
    \map[overwrite]{
      \pertype{online}
      \step[fieldsource=entrysubtype, final]
      \step[fieldsource=options, match=\regexp{(.*)}]
      \step[fieldset=options, fieldvalue={skipbib,}]
      \step[fieldset=options, fieldvalue={$1}, append]
    }
    % map number to volume in ancienttext
    \map{
      \pertype{ancienttext}
      \step[fieldsource=number, final]
      \step[fieldset=volume, origfieldval]
    }
    % create new entry when there is both a shorthand and a shortseries
    \map[overwrite]{
      \step[fieldsource=shortseries, final]
      \step[fieldsource=shorthand, final]
      \step[fieldsource=entrykey, match=\regexp{(.*)}]
      \step[entrynew=series-$1, entrynewtype=series, entrynocite]
      \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]
    }
  }
}


% ## Bibliography driver aliases

\DeclareBibliographyAlias{commentary}{book}
\DeclareBibliographyAlias{mvcommentary}{commentary}
\DeclareBibliographyAlias{incommentary}{inbook}
\DeclareBibliographyAlias{proceedings}{collection}
\DeclareBibliographyAlias{mvproceedings}{collection}
\DeclareBibliographyAlias{reference}{book}
\DeclareBibliographyAlias{mvreference}{reference}
\DeclareBibliographyAlias{series}{collection}
\DeclareBibliographyAlias{mvseries}{mvcollection}


% ## Data inheritance rules

\DeclareDataInheritance{mvbook,mvreference}{book,inbook,bookinbook,suppbook}{%
  \inherit{author}{mainauthor}
  \inherit{title}{maintitle}
  \inherit{subtitle}{mainsubtitle}
  \inherit{translatedtitle}{translatedmaintitle}
  \inherit{translatedsubtitle}{translatedmainsubtitle}
  \inherit{titleaddon}{maintitleaddon}
  \inherit{editor}{maineditor}
  \inherit{translator}{maintranslator}
  \inherit{withauthor}{withmainauthor}
  \inherit{witheditor}{withmaineditor}
  \noinherit{shorttitle}
  \noinherit{sorttitle}
  \noinherit{indextitle}
  \noinherit{indexsorttitle}
}

\DeclareDataInheritance{book,reference}{inbook,bookinbook,suppbook}{%
  \inherit{author}{bookauthor}
  \inherit{title}{booktitle}
  \inherit{subtitle}{booksubtitle}
  \inherit{titleaddon}{booktitleaddon}
  \inherit{translatedtitle}{translatedbooktitle}
  \inherit{translatedsubtitle}{translatedbooksubtitle}
  \inherit{author}{bookauthor}
  \inherit{editor}{bookeditor}
  \inherit{translator}{booktranslator}
  \inherit{withauthor}{withbookauthor}
  \inherit{witheditor}{withbookeditor}
  \noinherit{shorttitle}
  \noinherit{sorttitle}
  \noinherit{indextitle}
  \noinherit{indexsorttitle}
}

\DeclareDataInheritance{mvcollection}{collection,incollection}{%
  \inherit{author}{mainauthor}
  \inherit{title}{maintitle}
  \inherit{subtitle}{mainsubtitle}
  \inherit{titleaddon}{maintitleaddon}
  \inherit{translatedtitle}{translatedmaintitle}
  \inherit{translatedsubtitle}{translatedmainsubtitle}
  \inherit{editor}{maineditor}
  \inherit{translator}{maintranslator}
  \inherit{withauthor}{withmainauthor}
  \inherit{witheditor}{withmaineditor}
  \noinherit{shorttitle}
  \noinherit{sorttitle}
  \noinherit{indextitle}
  \noinherit{indexsorttitle}
}

\DeclareDataInheritance{collection}{incollection}{%
  \inherit{author}{bookauthor}
  \inherit{title}{booktitle}
  \inherit{subtitle}{booksubtitle}
  \inherit{titleaddon}{booktitleaddon}
  \inherit{translatedtitle}{translatedbooktitle}
  \inherit{translatedsubtitle}{translatedbooksubtitle}
  \inherit{author}{bookauthor}
  \inherit{editor}{bookeditor}
  \inherit{translator}{booktranslator}
  \inherit{withauthor}{withbookauthor}
  \inherit{witheditor}{withbookeditor}
  \noinherit{shorttitle}
  \noinherit{sorttitle}
  \noinherit{indextitle}
  \noinherit{indexsorttitle}
}

\DeclareDataInheritance{mvreference}{reference,inreference}{%
  \inherit{author}{mainauthor}
  \inherit{title}{maintitle}
  \inherit{subtitle}{mainsubtitle}
  \inherit{titleaddon}{maintitleaddon}
  \inherit{translatedtitle}{translatedmaintitle}
  \inherit{translatedsubtitle}{translatedmainsubtitle}
  \inherit{editor}{maineditor}
  \inherit{translator}{maintranslator}
  \inherit{withauthor}{withmainauthor}
  \inherit{witheditor}{withmaineditor}
  \noinherit{shorttitle}
  \noinherit{sorttitle}
  \noinherit{indextitle}
  \noinherit{indexsorttitle}
}

\DeclareDataInheritance{reference}{inreference}{%
  \inherit{author}{bookauthor}
  \inherit{title}{booktitle}
  \inherit{subtitle}{booksubtitle}
  \inherit{titleaddon}{booktitleaddon}
  \inherit{translatedtitle}{translatedbooktitle}
  \inherit{translatedsubtitle}{translatedbooksubtitle}
  \inherit{author}{bookauthor}
  \inherit{editor}{bookeditor}
  \inherit{translator}{booktranslator}
  \inherit{withauthor}{withbookauthor}
  \inherit{witheditor}{withbookeditor}
  \noinherit{shorttitle}
  \noinherit{sorttitle}
  \noinherit{indextitle}
  \noinherit{indexsorttitle}
}

\DeclareDataInheritance{mvcommentary}{commentary,incommentary}{%
  \inherit{author}{mainauthor}
  \inherit{title}{maintitle}
  \inherit{subtitle}{mainsubtitle}
  \inherit{titleaddon}{maintitleaddon}
  \inherit{translatedtitle}{translatedmaintitle}
  \inherit{translatedsubtitle}{translatedmainsubtitle}
  \inherit{editor}{maineditor}
  \inherit{translator}{maintranslator}
  \inherit{withauthor}{withmainauthor}
  \inherit{witheditor}{withmaineditor}
  \noinherit{shorttitle}
  \noinherit{sorttitle}
  \noinherit{indextitle}
  \noinherit{indexsorttitle}
}

\DeclareDataInheritance{commentary}{incommentary}{%
  \inherit{author}{bookauthor}
  \inherit{title}{booktitle}
  \inherit{subtitle}{booksubtitle}
  \inherit{titleaddon}{booktitleaddon}
  \inherit{translatedtitle}{translatedbooktitle}
  \inherit{translatedsubtitle}{translatedbooksubtitle}
  \inherit{author}{bookauthor}
  \inherit{editor}{bookeditor}
  \inherit{translator}{booktranslator}
  \inherit{withauthor}{withbookauthor}
  \inherit{witheditor}{withbookeditor}
  \noinherit{shorttitle}
  \noinherit{sorttitle}
  \noinherit{indextitle}
  \noinherit{indexsorttitle}
}

\DeclareDatafieldSet{names}{
  \member[datatype=name, fieldtype=list]
}


% ## Localisation

\DeclareLanguageMapping{american}{american-sbl}
\DeclareLanguageMapping{english}{english-sbl}
\DeclareLanguageMapping{german}{german-sbl}
\DeclareLanguageMapping{hebrew}{english-sbl}
\DeclareLanguageMapping{polytonicgreek}{english-sbl}
\DeclareLanguageMapping{spanish}{spanish-sbl}

\NewBibliographyString{article}
\NewBibliographyString{bydirector}
\NewBibliographyString{bytranslatorrev}
\NewBibliographyString{director}
\NewBibliographyString{directors}
\NewBibliographyString{fragment}
\NewBibliographyString{fragments}
\NewBibliographyString{inparts}
\NewBibliographyString{obverse}
\NewBibliographyString{of}
\NewBibliographyString{paper}
\NewBibliographyString{parts}
\NewBibliographyString{printing}
\NewBibliographyString{released}
\NewBibliographyString{reverse}
\NewBibliographyString{subverbis}
\NewBibliographyString{subverbo}
\NewBibliographyString{to}
\NewBibliographyString{with}
\NewBibliographyString{withassistance}
\NewBibliographyString{withpreface}


% ## URLs

% Use obeyspaces option from URL package
\begingroup \lccode`+=32 \lowercase
 {\endgroup \def\Url@ObeySp{\Url@Edit\Url@String{ }{+}}}
 \def\Url@space{\penalty\Url@sppen\ }

% use Roman style for URLs
\urlstyle{same}

% SBL line breaking rules
\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


% ## Citation delimiters and punctuation

\renewcommand*{\bibellipsis}{\textellipsis\midsentence}
\renewcommand*{\newunitpunct}{\addcomma\space}
\renewcommand*{\bibpagespunct}{\printdelim{bibpagesdelim}}
\renewcommand*{\begrelateddelim}{\relateddelim}
\newcommand*{\begrelateddelimmultivolarticle}{\addspace}
\renewcommand*{\intitlepunct}{\addspace}
\newcommand*{\locationpublisherdelim}{\addcolon\space}
\newcommand*{\multiseriesdelim}{\ifbibliography{\addsemicolon\space}{\addcomma\space}}
\newcommand*{\oftitlepunct}{\addspace}
\newcommand*{\postnominaldelim}{\addcomma\bibnamedelimc}
\newcommand*{\publisherdelim}{\addsemicolon\space}
\renewcommand*{\relateddelim}{\addsemicolon\space}
\newcommand*{\relateddelimmultivolarticle}{\addsemicolon\space}
\newcommand*{\reprintdelim}{\addsemicolon\space}
\newcommand*{\reprintpunct}{\addcomma\space}
\renewcommand*{\revsdnamedelim}{\addcomma}
% intelligently handle subtitle punctuation (taken from biblatex-chicago)
\renewcommand*{\subtitlepunct}{%
  \ifboolexpr{
    test {\ifterm}
    and
    not togl {sbl@blx@addsubtitlepunct}
  }
    {\addspace}
    {\addcolon\space
     \global\togglefalse{sbl@blx@addsubtitlepunct}}}
\newcommand*{\seriesnumberdelim}{%
  \iffieldundef{shortseries}
    {\addspace}
    {\addnbspace}}
\newcommand*{\titleaddonpunct}{%
  \ifboolexpr{
    test {\ifentrytype{ancienttext}}
    and
    test {\iffieldbegpages{titleaddon}}
  }
    {\addspace}
    {\newunitpunct}}
\newcommand*{\titlevolumedelim}{%
  \ifpaginationequalstr{none}
    {\addspace}
    {\addcomma\space}}
\newcommand*{\totitlepunct}{\intitlepunct}
\newcommand*{\translatedtitlepunct}{\addspace}
\renewcommand*{\volcitedelim}{%
  \iffieldbegpages{volcitepages}
    {\ifboolexpr{
       test {\ifpaginationequalstr{none}}
       or
       test {\ifpaginationequalstr{page}}
       or
       test {\ifpaginationequalstr{subverbo}}
     }
       {\addcolon}
       {\adddot}}
    {\addcomma\space}}

\DeclareDelimFormat{bibpagesdelim}{\addcomma\space}
\DeclareDelimFormat[shorthand]{bibpagesdelim}{%
  \iffieldundef{volumes}
    {\addcomma}
    {}%
  \space}
\DeclareDelimFormat[volume]{bibpagesdelim}{%
  \ifboolexpr{
    test {\ifpaginationequalstr{none}}
    or
    test {\ifpaginationequalstr{page}}
  }
    {\addcolon}
    {\addcomma\space}}
\DeclareDelimFormat[volume:article]{bibpagesdelim}{%
  \iffieldundef{eid}
    {\addcolon}
    {\addcomma}%
  \space}
\DeclareDelimFormat{languagedelim}{\addspace}
\DeclareDelimFormat{namedashdelim}{\addcomma\space}
\DeclareDelimFormat{postnotedelim}{%
  \ifboolexpr{
    not test {\iffieldundef{shorthand}}
    and
    (
      not test {\iffieldundef{volcitevolume}}
      or
      (
        not test {\ifpaginationequalstr{none}}
        and
        not test {\ifpaginationequalstr{page}}
        and
        not test {\ifpaginationequalstr{subverbo}}
      )
    )
  }
    {}
    {\addcomma}%
  \space}
\DeclareDelimFormat[series]{postnotedelim}{\addspace}
\DeclareDelimFormat[volume]{postnotedelim}{%
  \ifpaginationequalstr{none}
    {\ifboolexpr{
       test {\iffieldbegpages{postnote}}
       and
       test {\iffieldundef{pages}}
       and
       test {\iffieldundef{volcitevolume}}
     }
       {\addcolon}
       {\addcomma\space}}
    {\addcomma\space}}
\DeclareDelimFormat[volume:article]{postnotedelim}{%
  \ifpaginationequalstr{none}
    {\ifboolexpr{
       test {\iffieldbegpages{postnote}}
       and
       test {\iffieldundef{pages}}
       and
       test {\iffieldundef{volcitevolume}}
       and
       test {\iffieldundef{eid}}
     }
       {\addcolon}
       {\addcomma}}
    {\addcomma}%
  \space}
\DeclareDelimFormat{voltextdelim}{\addperiod}


% ## Field formats

\DeclareFieldFormat[review,incommentary,inreference,inseries]{citetitle}{\mkbibquote{#1\isdot}}
\DeclareFieldFormat[ancienttext]{citetitle}{%
  \begingroup
  \frenchspacing
  \ifboolexpr{
    test {\iffieldequalstr{entrysubtype}{inancientcollection}}
    or
    test {\iffieldequalstr{entrysubtype}{inancientbook}}
  }
    {\mkbibquote{#1}}
    {\ifboolexpr{
       test {\ifnameundef{author}}
       and
       not test {\iffieldequalstr{entrysubtype}{ancientbook}}
     }
       {#1}
       {\mkbibemph{#1}}}%
  \endgroup
  \isdot}
\DeclareFieldFormat[online]{citetitle}{%
  \ifboolexpr{
    test {\iffieldequalstr{entrysubtype}{blog}}
    and
    test {\iffieldundef{website}}
  }
    {\mkbibemph{#1\isdot}}
    {\mkbibquote{#1\isdot}}}
\DeclareFieldFormat[video]{citetitle}{%
  \iffieldequalstr{entrysubtype}{invideo}
    {\mkbibquote{#1}}
    {\mkbibemph{#1}}}
\DeclareFieldFormat{datemodifier}{\ifbibstring{#1}{\bibstring{#1}}{#1}}
\DeclareFieldFormat{doi}{\url{https://doi.org/#1}}
\DeclareFieldFormat{edition}{%
  \ifinteger{#1}
    {\mkbibordedition{#1}~\bibsstring{edition}}
    {\ifcapital{\MakeCapital{#1}}{#1}\isdot}}
\DeclareFieldFormat{eid}{\bibsstring{article}~#1}
\DeclareFieldFormat{eprint:ebook}{#1 \biblstring{edition}}
\DeclareFieldFormat{eprint:hethiter}{%
  \url{http://hethiter.net/:\%20#1}%
  \iffieldundef{eprintclass}
    {}
    {\addspace\mkbibparens{\thefield{eprintclass}}}}
\DeclareFieldFormat{nonlatin}{#1}
\DeclareFieldFormat{number}{\mknormrange{#1}}
\DeclareFieldFormat{pages}{\mkpageprefix[pagination][\mkcomprange]{#1}}
\DeclareFieldFormat[article,inbook,incollection,inreference,review]{pages}{%
  \def\abx@str{abx@sstr}%
  \iffieldundef{eid}
    {}
    {\setfield{pagination}{page}}%
  \mkpageprefix[pagination][\mkcomprange]{#1}}
\DeclareFieldFormat{pagesin}{%
  \renewcommand*{\ppspace}{\addspace}%
  \mkpageprefix[bookpagination][\mkcomprange]{#1}\space\bibstring{in}}
\DeclareFieldFormat{part}{\bibstring{part}~#1}
\DeclareFieldFormat{parts}{#1~\bibstring{parts}}
\DeclareFieldFormat{inparts}{\bibstring{in}\space#1~\bibstring{inparts}}
\DeclareFieldFormat{postnote}{%
  \def\abx@str{abx@sstr}%
  \ifboolexpr{
    not test {\ifciteseen}
    and
    not test {\iffieldundef{eid}}
  }
    {\setfield{pagination}{page}}
    {}%
  \ifboolexpr{
    test {\ifpaginationequalstr{subverbo}}
    and
    not test {\ifbegpages{#1}}
  }
    {\mksubverbo{#1}}
    {\mkpageprefix[pagination][\mkcomprange]{#1}}}
\DeclareFieldFormat{printing}{%
  \ifinteger{#1}
    {\mkbibordedition{#1}~\bibsstring{printing}}
    {\ifcapital{\MakeCapital{#1}}{#1}\isdot}}
\DeclareFieldFormat{revdtitle}{\mkbibemph{#1}}
\DeclareFieldFormat{roman}{#1}
\DeclareFieldFormat{inseries}{\bibstring{in}\space#1~\bibstring{inseries}}
\DeclareFieldFormat[article]{series}{#1}
\DeclareFieldFormat{shorthand}{\mkbibemph{#1}}
\DeclareFieldFormat[abbreviation]{shorthand}{%
  \begingroup
  \frenchspacing
  #1%
  \endgroup
  \isdot}
\DeclareFieldFormat[series]{shorthand}{#1}
\DeclareFieldFormat[review,incommentary,inreference,inseries]{title}{\mkbibquote{#1}}
\DeclareFieldFormat[ancienttext]{title}{%
  \ifboolexpr{
    test {\iffieldequalstr{entrysubtype}{inancientcollection}}
    or
    test {\iffieldequalstr{entrysubtype}{inancientbook}}
  }
    {\mkbibquote{#1}}
    {\ifboolexpr{
       test {\ifnameundef{author}}
       and
       not test {\iffieldequalstr{entrysubtype}{ancientbook}}
     }
       {#1}
       {\mkbibemph{#1}}}}
\DeclareFieldFormat[online]{title}{%
  \ifboolexpr{
    test {\iffieldequalstr{entrysubtype}{blog}}
    and
    test {\iffieldundef{website}}
  }
    {\mkbibemph{#1}}
    {\mkbibquote{#1}}}
\DeclareFieldFormat[series]{title}{#1}
\DeclareFieldFormat[video]{title}{%
  \iffieldequalstr{entrysubtype}{invideo}
    {\mkbibquote{#1}}
    {\mkbibemph{#1}}}
\DeclareFieldFormat[ancienttext]{titleaddon}{\mknormrange{#1}}
\DeclareFieldFormat[suppbook]{titlecase}{\ifcapital{\MakeCapital{#1}}{#1}}
\DeclareFieldFormat[thesis,unpublished]{type}{\ifbibstring{#1}{\bibsstring{#1}}{#1}}
\DeclareFieldFormat{url}{\url{#1}}
\DeclareFieldFormat{volcitepages}{%
  \ifboolexpr{
    test {\ifpaginationequalstr{subverbo}}
    and
    not test {\ifbegpages{#1}}
  }
    {\mksubverbo{#1}}
    {\mkcomprange{#1}}}
\DeclareFieldFormat{volcitevolume}{%
  \ifboolexpr{
    test {\iffieldundef{shorthand}}
    and
    not test {\iffieldbegpages{volcitepages}}
  }
    {\bibsstring{volume}\volspace#1}
    {#1}}
\DeclareFieldFormat{volume}{\bibsstring{volume}\volspace#1}
\DeclareFieldFormat[review,video]{volume}{#1}
\DeclareFieldFormat{volume:postnote}{%
  \ifpaginationequalstr{none}
    {\ifboolexpr{
       test {\iffieldbegpages{postnote}}
       or
       not test {\iffieldundef{pages}}
       or
       not test {\iffieldundef{shorthand}}
     }
       {#1}
       {\bibsstring{volume}\volspace#1}}
    {\bibsstring{volume}\volspace#1}}
\DeclareFieldFormat{volumes}{#1~\bibsstring{volumes}}
\DeclareFieldFormat{involumes}{\bibstring{in}\space#1~\bibsstring{involumes}}
\DeclareFieldFormat{website}{%
  \iffieldequalstr{entrysubtype}{blog}
    {\mkbibemph{#1}}
    {#1}}

\DeclareFieldFormat{shorthandtarget}{%
  \iffieldundef{shorthand}
    {#1}
    {\bibhypertarget{shorthand:\strfield{shorthand}}{#1}}%
  \isdot}
\DeclareFieldFormat{shorthandlink}{%
  \bibhyperlink{shorthand:\strfield{shorthand}}{#1}%
  \isdot}
\DeclareFieldFormat{shortmaintitlelink}{%
  \begingroup
  \frenchspacing
  \bibhyperlink{shorttitle:\strfield{shortmaintitle}}{#1}%
  \endgroup
  \isdot}
\DeclareFieldFormat{shorttitletarget}{%
  \iffieldundef{shorttitle}
    {#1}
    {\bibhypertarget{shorttitle:\strfield{shorttitle}}{#1}}%
  \isdot}
\DeclareFieldFormat{shorttitlelink}{%
  \bibhyperlink{\iffootnote{f}{t}:\cbx@resetcount:\thefield{entrykey}}{#1}}
\DeclareFieldFormat[ancienttext]{shorttitlelink}{%
  \iffieldundef{entrysubtype}
    {\begingroup
     \frenchspacing
     \bibhyperlink{shorttitle:\strfield{shorttitle}}{#1}%
     \endgroup
     \isdot}
    {#1}}
\DeclareFieldFormat{shortjournaltarget}{%
  \iffieldundef{shortjournal}
    {#1}
    {\bibhypertarget{shortjournal:\strfield{shortjournal}}{#1}}%
  \isdot}
\DeclareFieldFormat{shortjournallink}{%
  \bibhyperlink{shortjournal:\strfield{shortjournal}}{#1}%
  \isdot}
\DeclareFieldFormat{shortseriestarget}{%
  \iffieldundef{shortseries}
    {#1}
    {\bibhypertarget{shortseries:\strfield{shortseries}}{#1}}%
  \isdot}
\DeclareFieldFormat{shortserieslink}{%
  \bibhyperlink{shortseries:\strfield{shortseries}}{#1}%
  \isdot}

\DeclareFieldAlias{shorthandwidth}{shorthand}
\DeclareFieldAlias[abbreviation]{shorthandwidth}[abbreviation]{shorthand}
\DeclareFieldAlias[series]{shorthandwidth}[series]{shorthand}
\DeclareFieldAlias{shortjournalwidth}{journaltitle}
\DeclareFieldAlias{shortserieswidth}{series}
\DeclareFieldFormat{shorttitlewidth}{}
\DeclareFieldFormat[ancienttext]{shorttitlewidth}{%
  \iffieldundef{entrysubtype}
    {\begingroup
     \frenchspacing
     \ifnameundef{author}
       {#1}
       {\mkbibemph{#1}}
     \endgroup
     \isdot}
    {}}
\DeclareFieldFormat[ancienttext]{shortmaintitlewidth}{
  \begingroup
  \frenchspacing
  \mkbibemph{#1}%
  \endgroup
  \isdot}

\DeclareListWrapperFormat{language}{\mkbibbrackets{#1}}

% set formats based on options
\xapptocmd{\blx@setoptions@entry}
  {\ifdefstring{\sbl@blx@shorthandformat}{shorthand}
     {}
     {\DeclareFieldAlias{shorthand}{\sbl@blx@shorthandformat}%
      \DeclareFieldAlias{shorthandwidth}{\sbl@blx@shorthandformat}}}
  {}
  {}


% ## Name formats

\DeclareNameAlias{author}{default}
\DeclareNameAlias{editor}{default}
\DeclareNameAlias{translator}{default}
\DeclareNameAlias{withauthor}{default}
\DeclareNameAlias{witheditor}{default}
\DeclareNameAlias{withpreface}{default}

\DeclareNameWrapperFormat{withauthor}{%
  \usebibmacro{withtypestrg}{withauthor}{with}\space #1}
\DeclareNameWrapperFormat{witheditor}{%
  \usebibmacro{withtypestrg}{witheditor}{with}\space #1}

\newbibmacro*{withtypestrg}[2]{%
  \iffieldundef{#1type}
    {\bibstring{#2}}
    {\ifbibxstring{\thefield{#1type}}
       {\bibstring{\thefield{#1type}}}
       {\printtext{\thefield{#1type}}}}}

\DeclareNameFormat{labelname}{%
  \ifdefempty{\namepartshortfamily}
    {}
    {\def\namepartfamily{\namepartshortfamily}}%
  \ifcase\value{uniquename}%
    \usebibmacro{name:family}
      {\namepartfamily}
      {\namepartgiven}
      {\namepartprefix}
      {\namepartsuffix}%
  \or
    \ifuseprefix
      {\usebibmacro{name:given-family}
        {\namepartfamily}
        {\namepartgiveni}
        {\namepartprefix}
        {\namepartsuffixi}}
      {\usebibmacro{name:given-family}
        {\namepartfamily}
        {\namepartgiveni}
        {\namepartprefixi}
        {\namepartsuffixi}}%
  \or
    \usebibmacro{name:given-family}
      {\namepartfamily}
      {\namepartgiven}
      {\namepartprefix}
      {\namepartsuffix}%
  \fi
  \usebibmacro{name:andothers}}

% Patch in postnominal name part
\DeclareNameFormat{given-family}{%
  \ifgiveninits
    {\usebibmacro{name:given-family}
      {\namepartfamily}
      {\namepartgiveni}
      {\namepartprefix}
      {\namepartsuffix}}
    {\usebibmacro{name:given-family}
      {\namepartfamily}
      {\namepartgiven}
      {\namepartprefix}
      {\namepartsuffix}}%
  \ifdefempty{\namepartpostnominal}
    {}
    {\postnominaldelim\namepartpostnominal\isdot}%
  \usebibmacro{name:andothers}}

\DeclareNameFormat{family-given/given-family}{%
  \ifnumequal{\value{listcount}}{1}
    {\ifgiveninits
       {\usebibmacro{name:family-given}
         {\namepartfamily}
         {\namepartgiveni}
         {\namepartprefix}
         {\namepartsuffix}}
       {\usebibmacro{name:family-given}
         {\namepartfamily}
         {\namepartgiven}
         {\namepartprefix}
         {\namepartsuffix}}%
     \ifboolexpe{%
       test {\ifdefvoid\namepartgiven}
       and
       test {\ifdefvoid\namepartprefix}}
       {}
       {\usebibmacro{name:revsdelim}}}
    {\ifgiveninits
       {\usebibmacro{name:given-family}
         {\namepartfamily}
         {\namepartgiveni}
         {\namepartprefix}
         {\namepartsuffix}}
       {\usebibmacro{name:given-family}
         {\namepartfamily}
         {\namepartgiven}
         {\namepartprefix}
         {\namepartsuffix}}}%
  \ifdefempty{\namepartpostnominal}
    {}
    {\postnominaldelim\namepartpostnominal\isdot}%
  \usebibmacro{name:andothers}}

% Family, Given von, Junior
\renewbibmacro*{name:family-given}[4]{%
  \ifuseprefix
    {\usebibmacro{name:delim}{#3#1}%
     \usebibmacro{name:hook}{#3#1}%
     \mkbibcompletenamefamilygiven{%
       \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}%
     \mkbibcompletenamefamilygiven{%
       \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 format

% New sorting template that includes withauthor and witheditor
\DeclareSortingTemplate{nwty}{
  \sort{
    \field{presort}
  }
  \sort[final]{
    \field{sortkey}
  }
  \sort{
    \field{sortname}
    \field{author}
    \field{editor}
    \field{translator}
    \field{sorttitle}
    \field{title}
  }
  \sort{
    \field{withauthor}
    \field{witheditor}
  }
  \sort{
    \field{sorttitle}
    \field{title}
  }
  \sort{
    \field{sortyear}
    \field{year}
  }
  \sort{
    \field{volume}
    \literal{0}
  }
}

\newbibmacro*{sbl:set:bib:format}{%
  % do not abbreviate strings
  \def\abx@str{abx@lstr}%
  % punctuation
  \renewcommand*{\newunitpunct}{\addperiod\space}%
  \renewcommand*{\relateddelim}{\newunitpunct}%
  \renewcommand*{\reprintdelim}{\newunitpunct}%
  % name formats
  \DeclareNameAlias{author}{sortname}%
  \DeclareNameAlias{editor}{sortname}%
  \DeclareNameAlias{translator}{sortname}%
}

\renewcommand*{\bibnamedash}{%
  \printtext{\leavevmode\raise 0.6ex\hbox to 3em{\hrulefill}}%
  \setunit{\printdelim{namedashdelim}}}


% ## crossref tracking

\def\sbl@blx@crossreftracker@global{%
  \ifbool{citetracker}
    {\ifdef{\abx@field@crossref}
       {\xifinlistcs\abx@field@crossref{blx@bsee@\the\c@refsection}
          {}
          {\listcsxadd{blx@bsee@\the\c@refsection}\abx@field@crossref}}
       {}}
    {}}
\def\sbl@blx@crossreftracker@context{%
  \ifbool{citetracker}
    {\ifdef{\abx@field@crossref}
       {\iftoggle{blx@footnote}
          {\xifinlistcs\abx@field@crossref{blx@fsee@\the\c@refsection}
             {}
             {\listcsxadd{blx@fsee@\the\c@refsection}\abx@field@crossref}}
          {\xifinlistcs\abx@field@crossref{blx@bsee@\the\c@refsection}
             {}
             {\listcsxadd{blx@bsee@\the\c@refsection}\abx@field@crossref}}}
       {}}
    {}}
\appto\blx@opt@citetracker@true{%
  \let\sbl@blx@crossreftracker\sbl@blx@crossreftracker@global}
\appto\blx@opt@citetracker@false{%
  \let\sbl@blx@crossreftracker\relax}
\appto\blx@opt@citetracker@context{%
  \let\sbl@blx@crossreftracker\sbl@blx@crossreftracker@context}
\appto\blx@opt@citetracker@strict{%
  \def\sbl@blx@crossreftracker{%
    \blx@ifcitesingle{\sbl@blx@crossreftracker@global}{}}}
\appto\blx@opt@citetracker@constrict{%
  \def\sbl@blx@crossreftracker{%
    \blx@ifcitesingle{\sbl@blx@crossreftracker@context}{}}}

% add crossref tracker to citation printing code
\xpatchcmd{\blx@citeprint}
  {\blx@citetracker}
  {\blx@citetracker
   \sbl@blx@crossreftracker}
  {}
  {}

% \ifcrossrefseen{<true>}{<false>}
\protected\def\sbl@blx@imc@ifcrossrefseen{%
  \iffieldundef{crossref}
    {\@secondoftwo}
    {\ifentryseen{\thefield{crossref}}
       {\@firstoftwo}
       {\@secondoftwo}}}

\sbl@blx@regimcs{\ifcrossrefseen}

\def\sbl@blx@trackentry@global#1{%
  \ifbool{citetracker}
    {\blx@xsanitizeafter{\def\sbl@blx@tempa}{#1}%
     \xifinlistcs{\sbl@blx@tempa}{blx@bsee@\the\c@refsection}
       {}
       {\listcsxadd{blx@bsee@\the\c@refsection}{\sbl@blx@tempa}}}
    {}}
\let\sbl@blx@trackentry\sbl@blx@trackentry@global


% ## Citation format

% remove \blx@postcode from citation printing code
\xpatchcmd{\blx@citeprint}
  {\blx@postcode}
  {}
  {}
  {}

% use pagination=none and bookpagination=page by default
\def\sbl@blx@pagination{none}
\def\sbl@blx@bookpagination{page}

\newrobustcmd*{\sbl@blx@imc@ifpaginationequalstr}[1]{%
  \iffieldequalstr{pagination}{#1}
    {\@firstoftwo}
    {\ifboolexpr{
       test {\ifcsstring{sbl@blx@pagination}{#1}}
       and
       test {\iffieldundef{pagination}}
     }
       {\@firstoftwo}
       {\@secondoftwo}}}

\sbl@blx@regimcs{\ifpaginationequalstr}

\xpatchcmd{\blx@imc@mkpageprefix}
  {\def\blx@tempa{\blx@mkpageprefix{page}}}
  {\ifcsstring{sbl@blx@#1}{none}
     {\def\blx@tempa{\blx@mkpageprefix@i}}
     {\def\blx@tempa{\blx@mkpageprefix{\csuse{sbl@blx@#1}}}}}
  {}
  {}

% page range compression
\setcounter{mincompwidth}{10}
% if first page in range is multiple of 100 don't compress
\def\blx@comprange@check#1#2{%
  \blx@imc@ifinteger{#1}
    {\blx@imc@ifinteger{#2}
       {\@firstoftwo}
       {\@secondoftwo}}
    {\@secondoftwo}
    {\blx@tempcnta=#1
     \divide\blx@tempcnta by 100
     \multiply\blx@tempcnta by 100
     \multiply\blx@tempcnta by -1
     \advance\blx@tempcnta by #1\relax
     \ifnum\blx@tempcnta=0
       \blx@normrange@process{#1}{#2}%
     \else
       \blx@comprange@comp{#1}{#2}%
     \fi}
    {\begingroup
     \protected@edef\blx@tempc{\endgroup
       \blx@range@out@value{%
         \blx@range@out@item@process{\unexpanded{#1}}%
         \noexpand\bibrangedash
         \blx@range@out@item@process{\unexpanded{#2}}}}%
     \blx@tempc}}

% \ifbegpages and \iffieldbegpages macros
% building on https://tex.stackexchange.com/q/316730/87678
\ExplSyntaxOn
% this function ensures a field beginning with \pnfmt always gives true for
% \ifbegpages
\cs_new_protected:Nn \__sblblx_haspnfmt:n
  {
    \tl_if_blank:nF { #1 }
      {
        \tl_if_head_eq_meaning:nNT { #1 } \pnfmt
          { \def\sbl@blx@tempa { 1 } }
      }
  }
\cs_generate_variant:Nn \__sblblx_haspnfmt:n { o }
\cs_set_eq:NN \sbl@blx@haspnfmt \__sblblx_haspnfmt:o
\ExplSyntaxOff
\def\sbl@blx@firstword#1{\sbl@blx@firstword@i#1 \@nil}
\def\sbl@blx@firstword@i#1 #2\@nil{\unexpanded{#1}}%
\protected\def\sbl@blx@imc@ifbegpages#1{%
  \edef\sbl@blx@tempa{\sbl@blx@firstword{#1}}%
  \sbl@blx@haspnfmt{\sbl@blx@tempa}%
  \expandafter\blx@imc@ifpages
    \expandafter{\sbl@blx@tempa}}
\protected\def\sbl@blx@imc@iffieldbegpages#1{%
  \blx@imc@iffieldundef{#1}
    {\@secondoftwo}
    {\expandafter\expandafter
     \expandafter\ifbegpages
     \expandafter\expandafter
     \expandafter{\csname abx@field@#1\endcsname}}}

\sbl@blx@regimcs{\ifbegpages \iffieldbegpages}

% define macro to suppress formatting of postnote
% (useful if you want 12,13 without a space)
\appto\blx@blxinit{%
  \let\nopnfmt\unexpanded}%

% redefine \pnfmt so it uses current field format rather than hard coding
% postnote
\appto\blx@blxinit{%
  \protected\def\pnfmt{\printtext[\currentfield]}}%

% define s.v. and s.vv. helper macros:
\appto\blx@blxinit{%
  \def\sv{\bibstring{subverbo}}%
  \def\svv{\bibstring{subverbis}}}

% define line and lines helper macros:
\appto\blx@blxinit{%
  \def\lno{\bibstring{line}}%
  \def\llno{\bibstring{lines}}%
  \def\lineno{\biblstring{line}}%
  \def\linesno{\biblstring{lines}}}

% define col. and cols. helper macros:
\appto\blx@blxinit{%
  \def\colno{\bibstring{column}}%
  \def\colsno{\bibstring{columns}}}

% define § and §§ helper macros:
\appto\blx@blxinit{%
  \def\sno{\bibstring{section}}%
  \def\ssno{\bibstring{sections}}}

% define obv. and rev. helper macros:
\appto\blx@blxinit{%
  \def\obv{\bibstring{obverse}}%
  \def\rev{\bibstring{reverse}}}

% \ppspace
\appto\blx@blxinit{%
  \def\ppspace{%
  \ifpaginationequalstr{section}
    {}
    {\addnbspace}}}

% \volspace
\appto\blx@blxinit{%
  \def\volspace{\addnbspace}}

\DeclarePageCommands*{\lno\llno\lineno\linesno\colno\colsno\sno\ssno}

% Add ′ (prime) as number for Unicode engines
\ExplSyntaxOn
\bool_lazy_or:nnT { \sys_if_engine_luatex_p: } { \sys_if_engine_xetex_p: }
  {
    \DeclareNumChars* { ′ }
  }
\ExplSyntaxOff

%  Custom \mksubverbo command

\ExplSyntaxOn
\cs_new_protected:Nn \__sblblx_mksubverbo:n
  {
    \bool_lazy_any:nTF
      {
        { \tl_if_head_eq_meaning_p:nN { #1 } \sv }
        { \tl_if_head_eq_meaning_p:nN { #1 } \svv }
        { \tl_if_head_eq_meaning_p:nN { #1 } \nopp }
      }
      { #1 }
      {
        \clist_set:Nn \l_tmpa_clist { #1 }
        \clist_if_empty:NF \l_tmpa_clist
          {
            \int_set:Nn \l_tmpa_int { \clist_count:N \l_tmpa_clist }
            \int_compare:nNnTF { \l_tmpa_int } = { \c_one_int }
              { \bibstring { subverbo } }
              { \bibstring { subverbis } }
            \ppspace
            \clist_map_inline:Nn \l_tmpa_clist
              {
                \mkbibquote { ##1 }
                \int_compare:nNnT \l_tmpa_int > \c_one_int
                  { \addcomma \space }
                \int_decr:N \l_tmpa_int
              }
          }
      }
  }

\appto \blx@blxinit
  {
    \def \mksubverbo { \__sblblx_mksubverbo:n }
  }

\ExplSyntaxOff


% ## Bibliography

\xpatchcmd{\printbibliography}
  {\begingroup}
  {\begingroup
   \blx@key@bibcheck{bibliography}}
  {}
  {}

\DeclareBiblistFilter{bibliography}{
  \filter[type=nottype,filter=abbreviation]
}

\defbibcheck{bibliography}{%
  \sbl@blx@skipentries
}

\def\sbl@blx@skipentries{}
\protected\def\sbl@blx@imc@addskipentry#1{%
  \edef\X{%
    \noexpand\ifboolexpr{
      test {\noexpand\iffieldequalstr{entrykey}{#1}}
      and
      not test {\noexpand\ifentryseen{#1}}
    }
      {\noexpand\toggletrue{blx@skipentry}}
      {}}%
  \expandafter\g@addto@macro\expandafter\sbl@blx@skipentries\expandafter{\X}}

\sbl@blx@regimcs{\addskipentry}


% ## List of abbreviations

\DeclareBibliographyDriver{abbreviations}{%
  \usebibmacro{sbl:set:bib:format}%
  \ifentrytype{abbreviation}
    {\renewcommand*{\finentrypunct}{}}
    {}%
  \renewbibmacro*{bbx:savehash}{}%
  \usebibmacro{begentry}%
  \iffieldundef{shorthand}
    {\ifentrytype{ancienttext}
       {\usebibmacro{abbrev:combined+author+title}
         {\csuse{\sbl@blx@hash{shorttitle\therefsection\strfield{shorttitle}}}}}
       {\usebibmacro{abbrev:longfield}{shortseries}{series}%
        \usebibmacro{abbrev:longfield}{shortjournal}{journaltitle}}}
    {\usedriver{}{\thefield{entrytype}}}%
  \usebibmacro{finentry}}

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

\DeclareSortingTemplate{abbreviations}{
  \sort[final]{
    \field{sortshorthand}
  }
  \sort{
    \field{shorthand}
    \field{shortjournal}
    \field{shortseries}
    \field{shorttitle}
  }
  \sort{
    \field{sortname}
    \field{author}
    \field{editor}
  }
  \sort{
    \field{journaltitle}
    \field{series}
    \field{sorttitle}
    \field{title}
  }
}

\IfDocumentMetadataTF{
  \defbibenvironment{abbreviations}
    {\list[label-align=left]%
       {\usebibmacro{abbrev:printabbrev}}
       {\usebibmacro{setabbrevwidth}%
        \setlength{\labelwidth}{\sblabbrevwidth}%
        \setlength{\leftmargin}{\labelwidth}%
        \setlength{\labelsep}{\biblabelsep}%
        \addtolength{\leftmargin}{\labelsep}%
        \setlength{\itemsep}{\bibitemsep}%
        \setlength{\parsep}{\bibparsep}}}
    {\endlist}
    {\item}
}{
  \defbibenvironment{abbreviations}
    {\list
       {\usebibmacro{abbrev:printabbrev}}
       {\usebibmacro{setabbrevwidth}%
        \setlength{\labelwidth}{\sblabbrevwidth}%
        \setlength{\leftmargin}{\labelwidth}%
        \setlength{\labelsep}{\biblabelsep}%
        \addtolength{\leftmargin}{\labelsep}%
        \setlength{\itemsep}{\bibitemsep}%
        \setlength{\parsep}{\bibparsep}%
        \renewcommand*{\makelabel}[1]{##1\hss}}}
    {\endlist}
    {\item}
}

\defbibcheck{abbreviations}{%
  \iffieldundef{shorthand}
    {\ifboolexpr{
       test {\ifentrytype{ancienttext}}
       and
       test {\iffieldundef{entrysubtype}}
     }
       {\usebibmacro{abbrev:checkshorttitle}}
       {\ifboolexpr{
          test {\iffieldundef{shortjournal}}
          and
          test {\iffieldundef{shortseries}}
        }
          {\skipentry}
          {\usebibmacro{abbrev:checkshortfield}{shortseries}{series}%
           \usebibmacro{abbrev:checkshortfield}{shortjournal}{journaltitle}}}}
    {}}

\newlength{\sblabbrevwidth}

\newcommand*{\sbl@blx@imc@setmaxlength}[2]{%
  \ifdim\dimexpr#2>\dimexpr#1
    \setlength{#1}{#2}%
  \fi}

\sbl@blx@regimcs{\setmaxlength}

\newbibmacro*{setabbrevwidth}{%
  \setlength{\sblabbrevwidth}{0pt}%
  \setmaxlength{\sblabbrevwidth}{\shorthandwidth}%
  \setmaxlength{\sblabbrevwidth}{\shortserieswidth}%
  \setmaxlength{\sblabbrevwidth}{\shortjournalwidth}%
  \setmaxlength{\sblabbrevwidth}{\shorttitlewidth}%
}

\ExplSyntaxOn

\cs_set_eq:NN \sbl@blx@hash \str_mdfive_hash:e

\cs_new_protected:Nn \__sblblx_combined_author_title:n
  {
    \clist_map_inline:nn { #1 }
      {
        \setunit* { \addsemicolon \space }
        \entrydata* { ##1 }
          {
            \printnames { author }
            \setunit { \addcomma \space }
            \printfield { title }
          }
      }
    \renewcommand* { \finentrypunct } { }
  }

\newbibmacro* { abbrev:combined+author+title } [1]
  {
    \exp_args:Ne \__sblblx_combined_author_title:n { #1 }
  }

\ExplSyntaxOff

\newbibmacro*{abbrev:checkshorttitle}{%
  \ifcsdef{shorttitle@check@\therefsection\strfield{shorttitle}}
    {\skipentry
     \csxdef{shorttitle@\therefsection\strfield{shorttitle}}{%
       \csuse{shorttitle@\therefsection\strfield{shorttitle}},
       \strfield{entrykey}}}
    {\savefieldcs{shorttitle}{shorttitle@check@\therefsection\strfield{shorttitle}}%
     \csxdef{shorttitle@\therefsection\strfield{shorttitle}}{%
       \strfield{entrykey}}}%
  \protected@write\@auxout{}{%
    \string\csgdef{\sbl@blx@hash{shorttitle\therefsection\strfield{shorttitle}}}%
      {\csuse{shorttitle@\therefsection\strfield{shorttitle}}}}}

\newbibmacro*{abbrev:checkshortfield}[2]{%
  \iffieldundef{#1}
    {}
    {\iffieldundef{#2}
       {}
       {\ifcsdef{#1check@\therefsection\strfield{#1}=\strfield{#2}}
          {\skipentry}
          {\savefieldcs{#2}{#1check@\therefsection\strfield{#1}=\strfield{#2}}}}}}

\newbibmacro*{abbrev:printabbrev}{%
  \ifentrytype{ancienttext}
    {\printtext[shorttitletarget]{\printfield[citetitle]{shorttitle}}}
    {\iffieldundef{shorthand}
       {\printtext[shortseriestarget]{\printfield[series]{shortseries}}%
        \printtext[shortjournaltarget]{\printfield[journaltitle]{shortjournal}}}
       {\printtext[shorthandtarget]{\printfield{shorthand}}}}}

\newbibmacro*{abbrev:longfield}[2]{%
  \iffieldundef{#1}
    {}
    {\printfield{#2}%
     \renewcommand*{\finentrypunct}{}}}

\newbibmacro*{abbrev:shortfield}[2]{%
  \iffieldundef{#1}
    {\printfield{#2}}
    {\printtext[#1link]{\printtext[#2]{\printfield{#1}}}}}


% ## Indices

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

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


% ## List macros

% merge two lists together
\newcounter{lista:current}
\newcounter{listb:current}
\newcounter{lista:total}
\newcounter{listb:total}

\DeclareListFormat{lista}{%
  \setcounter{lista:total}{\value{listtotal}}%
  \usebibmacro{list:delim}{#1}%
  #1\isdot
  \ifnumequal{\value{lista:current}}{\value{listtotal}}
    {\setcounter{lista:current}{0}}
    {\stepcounter{lista:current}}}
\DeclareListFormat{listb}{%
  \setcounter{listb:total}{\value{listtotal}}%
  \usebibmacro{list:delim}{#1}%
  #1\isdot
  \ifnumequal{\value{listb:current}}{\value{listtotal}}
    {\setcounter{listb:current}{0}}
    {\stepcounter{listb:current}}}

\newbibmacro*{init:mergelists}[2]{%
  \setcounter{lista:total}{0}%
  \setcounter{listb:total}{0}%
  \iflistundef{#1}
    {\setcounter{lista:current}{0}}
    {\setcounter{lista:current}{1}}%
  \iflistundef{#2}
    {\setcounter{listb:current}{0}}
    {\setcounter{listb:current}{1}}}

\newbibmacro*{loop:mergelists}[4]{%
  \ifnumequal{\value{lista:current}}{0}
    {}
    {\printlist[lista][\value{lista:current}-\value{lista:current}]{#1}%
     \setunit*{#3}}%
  \ifnumequal{\value{listb:current}}{0}
    {}
    {\printlist[listb][\value{listb:current}-\value{listb:current}]{#2}}%
  \setunit*{#4}%
  \ifboolexpr{
    test {\ifnumgreater{\value{lista:total}}{1}}
    and
    test {\ifnumgreater{\value{lista:current}}{\value{listb:total}}}
  }
    {\setcounter{lista:current}{0}}
    {}%
  \ifboolexpr{
    test {\ifnumgreater{\value{lista:current}}{0}}
    or
    test {\ifnumgreater{\value{listb:current}}{0}}
  }
    {\usebibmacro{loop:mergelists}{#1}{#2}{#3}{#4}}
    {}}

% \usebibmacro{mergelists}{<lista>}{<listb>}{<listalistbdelim>}{<listgroupdelim>}
\newbibmacro*{mergelists}[4]{%
  \usebibmacro{init:mergelists}{#1}{#2}%
  \usebibmacro{loop:mergelists}{#1}{#2}{#3}{#4}}


% ## Name macros

% counter to save the number of translators when smuggling from xref to parent
\newcounter{sbl@savedtranslator}

% patch editor+othersstrg to use plural string when witheditor defined
\xpatchbibmacro{editor+othersstrg}
  {test {\ifandothers{editor}}}
  {test {\ifandothers{editor}}
   or
   not test {\ifnameundef{witheditor}}}
  {}
  {}

\renewbibmacro*{bbx:savehash}{%
  \savefield{fullhash}{\bbx@lasthash}%
  \setfield{withauthorfullhash}{}%
  \savefield{withauthorfullhash}{\sbl@bbx@lastwithauthorhash}%
  \setfield{witheditorfullhash}{}%
  \savefield{witheditorfullhash}{\sbl@bbx@lastwitheditorhash}%
}

\renewbibmacro*{bbx:dashcheck}[2]{%
  \setfield{withauthorfullhash}{}%
  \setfield{witheditorfullhash}{}%
  \ifboolexpr{
    test {\ifbibliography}
    and
    test {\iffieldequals{fullhash}{\bbx@lasthash}}
    and
    (
      test {\iffieldequals{withauthorfullhash}{\sbl@bbx@lastwithauthorhash}}
      or
      test {\iffieldequals{witheditorfullhash}{\sbl@bbx@lastwitheditorhash}}
    )
    and
    not test \iffirstonpage
    and
    (
       not bool {bbx@inset}
       or
       test {\iffieldequalstr{entrysetcount}{1}}
    )
  }
    {#1}
    {#2}}

\renewbibmacro*{author}{%
  \ifboolexpr{
    test \ifuseauthor
    and
    not test {\ifnameundef{author}}
  }
    {\usebibmacro{bbx:dashcheck}
       {\bibnamedash}
       {\printnames{author}%
        \setunit{\printdelim{authortypedelim}}%
        \usebibmacro{bbx:savehash}%
        \usebibmacro{authorstrg}%
        \setunit{\addcomma\space}%
        \printnames{withauthor}}}
    {\global\undef\bbx@lasthash}}

\renewbibmacro*{bbx:editor}[1]{%
  \ifboolexpr{
    test \ifuseeditor
    and
    not test {\ifnameundef{editor}}
  }
    {\usebibmacro{bbx:dashcheck}
       {\bibnamedash}
       {\printnames{editor}%
        \setunit{\addcomma\space}%
        \printnames{witheditor}%
        \setunit{\printdelim{editortypedelim}}%
        \usebibmacro{bbx:savehash}}%
     \usebibmacro{#1}%
     \clearname{witheditor}%
     \clearname{editor}}
    {\global\undef\bbx@lasthash}}

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

\renewbibmacro*{bybookauthor}{%
  \ifboolexpr{
    test {\ifnameundef{bookauthor}}
    or
    (
      test {\ifnamesequal{bookauthor}{author}}
      and
      test {\ifnameundef{editor}}
      and
      test {\ifnameundef{bookeditor}}
    )
  }
    {}
    {\usebibmacro{bytypestrg}{bookauthor}{author}%
     \setunit{\addspace}%
     \printnames[byauthor]{bookauthor}}}

\newbibmacro*{bybookeditor}{%
  \ifboolexpr{
    test {\ifnameundef{bookeditor}}
    or
    test {\ifnamesequal{bookeditor}{editor}}
  }
    {}
    {\usebibmacro{bytypestrg}{bookeditor}{editor}%
     \setunit{\addspace}%
     \printnames[byeditor]{bookeditor}%
     \newunit}}

\newbibmacro*{bymainauthor}{%
  \ifboolexpr{
    test {\ifnameundef{mainauthor}}
    or
    (
      test {\ifnamesequal{mainauthor}{author}}
      and
      test {\ifnameundef{editor}}
      and
      test {\ifnameundef{maineditor}}
    )
    or
    (
      test {\ifnamesequal{mainauthor}{bookauthor}}
      and
      test {\ifnameundef{editor}}
      and
      test {\ifnameundef{maineditor}}
    )
  }
    {}
    {\usebibmacro{bytypestrg}{mainauthor}{author}%
     \setunit{\addspace}%
     \printnames[byauthor]{mainauthor}}}

\newbibmacro*{bymaineditor}{%
  \ifboolexpr{
    test {\ifnameundef{maineditor}}
    or
    test {\ifnamesequal{maineditor}{editor}}
    or
    test {\ifnamesequal{maineditor}{bookeditor}}
  }
    {}
    {\usebibmacro{bytypestrg}{maineditor}{editor}%
     \setunit{\addspace}%
     \printnames[byeditor]{maineditor}%
     \newunit}}

\newbibmacro*{withpreface}{%
  \ifnameundef{preface}
    {}
    {\bibstring{withpreface}%
     \setunit{\addspace}%
     \printnames[withpreface]{preface}}}

\renewbibmacro*{withothers}{%
  \newunit
  \usebibmacro{withcommentator}%
  \clearname{commentator}%
  \newunit
  \usebibmacro{withannotator}%
  \clearname{annotator}%
  \newunit
  \usebibmacro{withintroduction}%
  \clearname{introduction}%
  \newunit
  \usebibmacro{withforeword}%
  \clearname{foreword}%
  \newunit
  \usebibmacro{withafterword}%
  \clearname{afterword}%
  \newunit
  \usebibmacro{withpreface}%
  \clearname{preface}}


% ## Title macros

\renewbibmacro*{title}{%
  \ifboolexpr{
    test {\iffieldundef{title}}
    and
    test {\iffieldundef{subtitle}}
  }
    {}
    {\iffieldundef{translatedtitle}
       {\printtext[title]{%
        \printfield[titlecase]{title}%
        \setunit{\subtitlepunct}%
        \printfield[titlecase]{subtitle}}}
       {\clearlist{language}%
        \printtext[\sbl@blx@nonlatintitleformat]{%
          \printfield[titlecase]{title}%
          \setunit{\subtitlepunct}%
          \printfield[titlecase]{subtitle}}%
        \setunit{\translatedtitlepunct}%
        \printtext[brackets]{%
          \printtext[title]{%
            \printfield[titlecase]{translatedtitle}%
            \setunit{\subtitlepunct}%
            \printfield[titlecase]{translatedsubtitle}}}}%
     \setunit{\titleaddonpunct}}%
  \printfield{titleaddon}}

\renewbibmacro*{booktitle}{%
  \ifboolexpr{
    test {\iffieldundef{booktitle}}
    and
    test {\iffieldundef{booksubtitle}}
  }
    {}
    {\iffieldundef{translatedbooktitle}
       {\printtext[booktitle]{%
        \printfield[titlecase]{booktitle}%
        \setunit{\subtitlepunct}%
        \printfield[titlecase]{booksubtitle}}}
       {\printtext[\sbl@blx@nonlatinbooktitleformat]{%
          \printfield[titlecase]{booktitle}%
          \setunit{\subtitlepunct}%
          \printfield[titlecase]{booksubtitle}}%
        \setunit{\translatedtitlepunct}%
        \printtext[brackets]{%
          \printtext[booktitle]{%
            \printfield[titlecase]{translatedbooktitle}%
            \setunit{\subtitlepunct}%
            \printfield[titlecase]{translatedbooksubtitle}}}}%
      \setunit{\titleaddonpunct}}%
  \printfield{booktitleaddon}}

\renewbibmacro*{maintitle}{%
  \ifboolexpr{
    test {\iffieldundef{maintitle}}
    and
    test {\iffieldundef{mainsubtitle}}
  }
    {}
    {\iffieldundef{translatedmaintitle}
       {\printtext[maintitle]{%
        \printfield[titlecase]{maintitle}%
        \setunit{\subtitlepunct}%
        \printfield[titlecase]{mainsubtitle}}}
       {\printtext[\sbl@blx@nonlatinmaintitleformat]{%
          \printfield[titlecase]{maintitle}%
          \setunit{\subtitlepunct}%
          \printfield[titlecase]{mainsubtitle}}%
        \setunit{\translatedtitlepunct}%
        \printtext[brackets]{%
          \printtext[maintitle]{%
            \printfield[titlecase]{translatedmaintitle}%
            \setunit{\subtitlepunct}%
            \printfield[titlecase]{translatedmainsubtitle}}}}%
      \setunit{\titleaddonpunct}}%
  \printfield{maintitleaddon}}

\newbibmacro{booktitle+bybookauthor+bybookeditor}{%
  \iffieldundef{booktitle}
    {}
    {\usebibmacro{booktitle}%
     \newunit
     \usebibmacro{bybookauthor}%
     \newunit
     \usebibmacro{bybookeditor}%
     \newunit\newblock}}

\newbibmacro{booktitle+bybookeditor}{%
  \iffieldundef{booktitle}
    {}
    {\usebibmacro{booktitle}%
     \newunit
     \usebibmacro{bybookeditor}%
     \newunit\newblock}}

\newbibmacro{maintitle+bymainauthor+bymaineditor}{%
  \iffieldundef{maintitle}
    {}
    {\usebibmacro{maintitle}%
     \newunit
     \usebibmacro{bymainauthor}%
     \newunit
     \usebibmacro{bymaineditor}}}

\newbibmacro{maintitle+bymaineditor}{%
  \iffieldundef{maintitle}
    {}
    {\usebibmacro{maintitle}%
     \newunit
     \usebibmacro{bymaineditor}}}


% ## Book macros

\newbibmacro*{edition}{%
  \printfield{edition}%
  \setunit*{\addcomma\space}%
  \printfield{printing}}

\newbibmacro*{of:}{%
  \bibstring{of}%
  \setunit{\oftitlepunct}}

\newbibmacro*{to:}{%
  \bibstring{to}%
  \setunit{\totitlepunct}}

\newbibmacro*{pages+in:}{%
  \ifbibliography
    {\printfield[pagesin]{pages}%
     \clearfield{pages}}
    {\bibstring{in}}
  \setunit{\intitlepunct}}

\newbibmacro*{inseries}{%
  \iffieldundef{volumes}
    {}
    {\iffieldint{series}
       {\setunit{\addspace}%
        \printfield[inseries]{series}%
        \clearfield{series}}
       {}}}

\newbibmacro*{parts}{%
  \iffieldundef{volumes}
    {\printfield{parts}%
     \clearfield{parts}}
    {}}

\newbibmacro*{volume+part+of:}{%
  \iffieldundef{volume}
    {}
    {\clearfield{volumes}}%
  \ifbibliography
    {\printfield{volume}%
     \clearfield{volume}%
     \setunit*{\addcomma\space}%
     \printfield{part}%
     \clearfield{part}%
     \iffieldundef{maintitle}
       {}
       {\setunit{\addspace}%
        \usebibmacro{of:}}}
    {\iffieldundef{part}
       {\printfield{volume}%
        \clearfield{volume}%
        \iffieldundef{maintitle}
          {}
          {\setunit{\addspace}%
           \usebibmacro{of:}}}
       {}}}

\newbibmacro*{volume+part:}{%
  \iffieldundef{volume}
    {\printfield{part}%
     \clearfield{part}%
     \setunit*{\addcomma\space}}
    {\printfield{volume}%
     \clearfield{volume}%
     \setunit*{\addcomma\space}%
     \printfield{part}%
     \clearfield{part}%
     \setunit{\addcolon\space}}}

\newbibmacro*{volumes+parts}{%
  \ifboolexpr{
    test {\iffieldundef{volumes}}
    or
    test {\iffieldundef{parts}}
  }
    {\printfield{volumes}%
     \usebibmacro{inseries}%
     \newunit
     \printfield{parts}}
    {\iftoggle{sbl@blx@partsinvols}
       {\printfield{parts}%
        \setunit{\addspace}%
        \printfield[involumes]{volumes}%
        \usebibmacro{inseries}}
       {\printfield{volumes}%
        \setunit{\addspace}%
        \printfield[inparts]{parts}}}}


% ## Journal macros

% remove in: from journal articles
\renewbibmacro*{in:}{%
  \ifboolexpr{
    test {\ifentrytype{article}}
    or
    test {\ifentrytype{review}}
  }
    {}
    {\bibstring{in}%
     \setunit{\intitlepunct}}}

% allow for no date
\renewbibmacro*{issue+date}{%
  \ifboolexpr{
    test {\iffieldundef{issue}}
    and
    test {\iffieldundef{year}}
  }
    {}
    {\ifboolexpr{
       test {\iffieldundef{volume}}
       and
       test {\iffieldundef{number}}
     }
       {}
       {\setunit{\addspace}%
        \printtext{\bibopenparen}}%
     \printfield{issue}%
     \setunit*{\addcomma\space}%
     \usebibmacro{date}%
     \ifboolexpr{
       test {\iffieldundef{volume}}
       and
       test {\iffieldundef{number}}
     }
       {}
       {\printtext{\bibcloseparen}}%
     \newunit}}

% use shortjournal if available or treat series like journal if not journaltitle
\newbibmacro*{journal+issuetitle/series}{%
  \iffieldundef{journaltitle}
    {\usebibmacro{series+number}}
    {\usebibmacro{abbrev:shortfield}{shortjournal}{journaltitle}%
     \setunit*{\addspace}%
     \iffieldundef{series}
       {}
       {\printfield{series}%
        \setunit{\slash}}%
     \usebibmacro{multivolarticle}%
     \usebibmacro{volume+number}}%
  \newunit
  \printfield{website}%
  \setunit{\addcomma\space}%
  \usebibmacro{issue+date}%
  \setunit{\addcolon\space}%
  \printfield{eid}%
  \newunit}

\newbibmacro*{volume+number}{%
  \printfield{volume}%
  \setunit*{\adddot}%
  \printfield{number}}

\newbibmacro*{multivolarticle}{%
  \ifboolexpr{
    test {\iffieldequalstr{relatedtype}{multivolarticle}}
    and
    test {\iftoggle{bbx:related}}
  }
    {\csundef{bbx:relatedloop}%
     \usebibmacro{related}%
     \togglefalse{bbx:related}%
     \begingroup
     \delimcontext{volume:article}%
     \usebibmacro{cite:postnote}%
     \endgroup
     % clear postnote
     \renewbibmacro*{cite:postnote}{}}
    {}}

% ## review macros

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

\newbibmacro*{revdlabeltitle}{%
  \iffieldundef{shortrevdtitle}
    {\iffieldundef{revdtitle}
       {}
       {\printtext[revdtitle]{\printfield[titlecase]{revdtitle}}}}%
    {\printtext[revdtitle]{\printfield[titlecase]{revdshorttitle}}}}

\newbibmacro*{byrevdauthor}{%
  \ifnameundef{revdauthor}
    {}
    {\bibstring{byauthor}%
     \setunit{\addspace}%
     \printnames[byrevdauthor]{revdauthor}}}

\newbibmacro*{byrevdeditor}{%
  \ifnameundef{revdeditor}
    {}
    {\bibsstring{byeditor}%
     \setunit{\addspace}%
     \printnames[byrevdeditor]{revdeditor}}}

\newbibmacro*{revdlabelname}{%
  \setunit{\addspace}%
  \ifnameundef{revdauthor}
    {\ifnameundef{revdeditor}
       {}
       {\printtext[parens]{%
          \bibstring{byeditor}%
          \setunit{\addspace}%
          \printnames[labelname]{revdeditor}}}}%
    {\printtext[parens]{%
       \bibsstring{byauthor}%
       \setunit{\addspace}%
       \printnames[labelname]{revdauthor}}}}

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


% ## Series macros

% use shortseries if available
\renewbibmacro*{series+number}{%
  \usebibmacro{abbrev:shortfield}{shortseries}{series}%
  \setunit*{\seriesnumberdelim}%
  \printfield{number}%
  \newunit}


% ## publisher macros

% clear location if date is recent
\newbibmacro*{clearrecentlocations}{%
  \iffieldundef{year}
    {}
    {\ifnumgreater{\thefield{year}}{1899}
       {\clearlist{location}}
       {}}%
  \iffieldundef{origyear}
    {}
    {\ifnumgreater{\thefield{origyear}}{1899}
       {\clearlist{origlocation}}
       {}}}

% add datemodifier
\renewbibmacro*{date}{%
  \iffieldundef{datemodifier}
    {}
    {\newunit
     \printfield{datemodifier}%
     \setunit{\addspace}}%
  \printdate}

% add parentheses in citations
\renewbibmacro*{publisher+location+date}{%
  \ifboolexpr{
    test {\ifbibliography}
    or
    (
      test {\iflistundef{origpublisher}}
      and
      test {\iflistundef{origlocation}}
      and
      test {\iffieldundef{origyear}}
      and
      test {\iflistundef{publisher}}
      and
      test {\iflistundef{location}}
      and
      test {\iffieldundef{year}}
      and
      test {\iffieldundef{pubstate}}
      and
      not test {\iffieldequalstr{relatedtype}{reprint}}
    )
  }
    {}
    {\setunit{\addspace}%
     \printtext{\bibopenparen}}%
  \usebibmacro{clearrecentlocations}%
  \usebibmacro{origpublisher+origlocation+origdate}%
  \usebibmacro{mergelists}{location}{publisher}{\locationpublisherdelim}{\publisherdelim}%
  \ifboolexpr{
    not test {\ifbibliography}
    and
    test {\iflistundef{location}}
    and
    test {\iflistundef{publisher}}
  }
    {\setunit*{\nopunct}}
    {\setunit*{\addcomma\space}}%
  \usebibmacro{date}%
  \usebibmacro{pubstate}%
  \usebibmacro{reprint}%
  \ifboolexpr{
    test {\ifbibliography}
    or
    (
      test {\iflistundef{origpublisher}}
      and
      test {\iflistundef{origlocation}}
      and
      test {\iffieldundef{origyear}}
      and
      test {\iflistundef{publisher}}
      and
      test {\iflistundef{location}}
      and
      test {\iffieldundef{year}}
      and
      test {\iffieldundef{pubstate}}
      and
      not test {\iffieldequalstr{relatedtype}{reprint}}
    )
  }
    {}
    {\printtext{\bibcloseparen}}%
  \clearfield{pubstate}%
  \newunit}

\newbibmacro*{origpublisher+origlocation+origdate}{%
  \ifboolexpr{
    test {\iflistundef{origpublisher}}
    and
    test {\iflistundef{origlocation}}
  }
    {}
    {\usebibmacro{clearrecentlocations}%
     \usebibmacro{mergelists}{origlocation}{origpublisher}{\locationpublisherdelim}{\publisherdelim}%
     \ifboolexpr{
       not test {\ifbibliography}
       and
       test {\iflistundef{origlocation}}
       and
       test {\iflistundef{origpublisher}}
     }
       {\setunit*{\nopunct}}
       {\setunit*{\addcomma\space}}}%
  \iffieldundef{origyear}
    {}
    {\usebibmacro{origdate}%
     \setunit{\reprintdelim}%
     \bibsstring{reprint}%
     \setunit{\reprintpunct}}}

\newbibmacro*{pubstate}{%
  \iffieldundef{pubstate}
    {}
    {\setunit{\addcomma\space}%
     \printfield{pubstate}}}

\newbibmacro*{reprint}{%
  \ifboolexpr{
    test {\iffieldequalstr{relatedtype}{reprint}}
    and
    test {\iftoggle{bbx:related}}
  }
    {\csundef{bbx:relatedloop}%
     \usebibmacro{related}%
     \togglefalse{bbx:related}%
    }
    {}}

\renewbibmacro*{organization+location+date}{%
  \ifboolexpr{
    test {\ifbibliography}
    or
    (
      test {\iflistundef{organization}}
      and
      test {\iflistundef{location}}
      and
      test {\iffieldundef{year}}
    )
  }
    {}
    {\setunit{\addspace}%
     \printtext{\bibopenparen}}%
  \usebibmacro{clearrecentlocations}%
  \usebibmacro{mergelists}{location}{organization}{\locationpublisherdelim}{\publisherdelim}%
  \setunit*{\addcomma\space}%
  \usebibmacro{date}%
  \ifboolexpr{
    test {\ifbibliography}
    or
    (
      test {\iflistundef{organization}}
      and
      test {\iflistundef{location}}
      and
      test {\iffieldundef{year}}
    )
  }
    {}
    {\printtext{\bibcloseparen}}%
  \newunit}

% ## thesis macros

\newbibmacro*{type+institution+location+date}{%
  \ifbibliography
    {}
    {\setunit{\addspace}%
     \printtext{\bibopenparen}}%
  \usebibmacro{clearrecentlocations}%
  \printfield{type}%
  \setunit*{\addcomma\space}%
  \usebibmacro{mergelists}{location}{institution}{\locationpublisherdelim}{\publisherdelim}%
  \setunit*{\addcomma\space}%
  \usebibmacro{date}%
  \ifbibliography
    {}
    {\printtext{\bibcloseparen}}%
  \newunit}


% ## unpublished macros

\newbibmacro*{type+event+venue+date}{%
  \ifbibliography
    {}
    {\setunit{\addspace}%
     \printtext{\bibopenparen}}%
  \printfield{type}%
  \setunit*{\addspace}%
  \printfield{eventtitle}%
  \newunit
  \printfield{eventtitleaddon}%
  \newunit
  \printfield{venue}%
  \setunit*{\addcomma\space}%
  \printeventdate
  \ifbibliography
    {}
    {\printtext{\bibcloseparen}}%
  \newunit}


% ## online macros

\newbibmacro*{website+date}{%
  \printfield{website}%
  \setunit*{\addcomma\space}%
  \usebibmacro{date}}

% ## Pages macros

\newbibmacro*{volume+part+pages}{%
  \printfield[volume:postnote]{volume}%
  \setunit*{\addperiod}%
  \printfield[noformat]{part}%
  \begingroup
  \iffieldundef{volume}
    {}
    {\delimcontext{volume}}%
  \setunit{\bibpagespunct}%
  \printfield{pages}%
  \printfield{eid}%
  \usebibmacro{cite:postnote}%
  \endgroup}

\newbibmacro*{volume+text+pages}{%
  \iffieldundef{volume}
    {}
    {\printfield[volume:postnote]{volume}%
     \setunit{\printdelim{voltextdelim}}}%
  \printfield{text}%
  \begingroup
  \iffieldundef{volume}
    {}
    {\delimcontext{volume}}%
  \setunit{\bibpagespunct}%
  \printfield{pages}%
  \usebibmacro{cite:postnote}%
  \endgroup}

\renewbibmacro*{chapter+pages}{%
  \printfield{chapter}%
  \setunit{\bibpagespunct}%
  \usebibmacro{volume+part+pages}%
  \newunit}

% flexible control of \bibpagespunct in articles
\renewbibmacro*{note+pages}{%
  \printfield{note}%
  \begingroup
  \iffieldundef{volume}
    {}
    {\delimcontext{volume:article}}%
  \setunit{\bibpagespunct}%
  \printfield{pages}%
  \usebibmacro{cite:postnote}%
  \endgroup
  \newunit}

\newbibmacro*{inseries:note+pages}{%
  \printfield{note}%
  \begingroup
  \iffieldundef{series}
    {}
    {\delimcontext{volume:article}}%
  \setunit{\bibpagespunct}%
  \printfield{pages}%
  \usebibmacro{cite:postnote}%
  \endgroup
  \newunit}


% ## Date macros

\newbibmacro*{origdate}{\printorigdate}


% ## Digital edition macros

\renewbibmacro*{eprint}{%
  \iftoggle{bbx:eprint}
    {\iffieldundef{eprinttype}
       {\printfield{eprint}}
       {\printfield[eprint:\strfield{eprinttype}]{eprint}}}
    {}}

\newbibmacro*{doi+url}{%
  \iftoggle{bbx:doi}
    {\printfield{doi}}
    {}%
  \newunit\newblock
  \iftoggle{bbx:url}
    {\usebibmacro{url+urldate}}
    {}}


% ## Related macros

% don't print the postnote within related entries
\renewbibmacro*{related:init}{%
  \renewbibmacro*{cite:postnote}{}%
  \csundef{bbx:relatedloop}}

% make sure editor isn't printed before title when author is cleared in
% related entry
\xpatchbibmacro{related:default}
  {\clearname{author}}
  {\clearname{author}%
   \renewbibmacro*{editor+others}{}}
  {}
  {}

\newbibmacro*{related:reprint}[1]{%
  \entrydata*{#1}{%
    \usedriver
      {\renewbibmacro*{related:init}{}%
       \renewbibmacro*{cite:postnote}{}%
       \renewbibmacro*{publisher+location+date}{%
         \usebibmacro{clearrecentlocations}%
         \usebibmacro{mergelists}{location}{publisher}{\locationpublisherdelim}{\publisherdelim}%
         \setunit*{\addcomma\space}%
         \usebibmacro{date}%
         \newunit}%
       }
      {\thefield{entrytype}}}}

\newbibmacro*{related:multivolarticle}[1]{%
  \entrydata*{#1}{%
    \usebibmacro{cite:full:citepages}%
    \usebibmacro{volume+number}%
    \setunit{\addspace}%
    \usebibmacro{issue+date}%
    \setunit{\addcolon\space}%
    \printfield{eid}%
    \begingroup
    \iffieldundef{volume}
      {}
      {\delimcontext{volume:article}}%
    \setunit{\bibpagespunct}%
    \printfield{pages}%
    \endgroup}}

% ## Indexing macros

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

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