%% ---------------------------------------------------------------
%% biblatex-unified --- A biblatex implementation of the Unified Style
%%   Sheet for Linguistics Journals. This is the house style of the
%%   journal "Semantics & Pragmatics" (A journal of the Linguistic
%%   Society of America).
%% Maintained by Kai von Fintel (fintel@mit.edu)
%% Released under the LaTeX Project Public License v1.3c or later
%% See http://www.latex-project.org/lppl.txt
%% ---------------------------------------------------------------
%%

\ProvidesFile{unified.bbx}[2022/11/08 v1.20 biblatex bibliography style]

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% biblatex by default calls biblatex.def, we add to this authoryear.bbx, which     %
% in turn loads standard.bbx. So, unified.bbx is built on top of those styles;     %
% once authoryear.bbx is loaded, we tell it not to put in dashes for repeated      %
% authors (in accordance with the Unified Stylesheet)                              %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\RequireBibliographyStyle{authoryear}
\ExecuteBibliographyOptions{dashed=false,isbn=false,eprint=true}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% NB: The Unified Style Sheet wants abbreviated "ed(s)", "edn". But using the      %
% abbreviate option also abbreviates the names of months. But then                 %
% dateabbrev=false restores the long names of months                               %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\ExecuteBibliographyOptions{%
  sorting=nyt,abbreviate,dateabbrev=false,useprefix=true}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% For backward compatibility: choose labeldate or labeldateparts                   %
% depending on the biblatex version                                                %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\@ifpackagelater{biblatex}{2016/09/09} {%
  \ExecuteBibliographyOptions{labeldateparts}} {%
  \ExecuteBibliographyOptions{labeldate}
  \def\printlabeldateextra{\printdateextralabel} }%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% If an @article entry contains Issuetitle and Editor information, we              %
% might not want to print it. The Unified Style Sheet does not offer               %
% explicit guidelines on this, but they don't have any examples where              %
% either of these pieces of information are actually printed.                      %
% Therefore, we can offer an option for the user to decide whether to              %
% print it. The default will be not to print it since the Unified                  %
% Style Sheet does not have examples where this information is                     %
% printed. The user can print it by setting `issueandeditor=true` as a             %
% package option when biblatex is called.                                          %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\newtoggle{issueandeditor}
\DeclareBibliographyOption{issueandeditor}[false]{%
  \settoggle{issueandeditor}{#1}}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Treating prefixes like "von" as part of the last name, but ignore them for       %
% sorting (relies on useprefix=true default option)                                %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\DeclareSortingNamekeyTemplate{
  \keypart{
    \namepart{family}
  }
  \keypart{
    \namepart{prefix}
  }
  \keypart{
    \namepart{given}
  }
  \keypart{
    \namepart{suffix}
  }
}

\renewbibmacro{begentry}{\midsentence}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Moving "Jr." to after the first name                                             %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\renewbibmacro*{name:family-given}[4]{%
  \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}}
  }

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%             VARIOUS BIBMACROS USED IN PRODUCING THE BIBLIOGRAPHY                 %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% We use `date+extradate` but biblatex versions before 3.8 didn't have that        %
% macro. Older versions were smart enough to complain about `\renewmacro` and      %
% use `\newmacro` instead. But the following construction works for both cases     %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\providebibmacro{date+extradate}{}
\renewbibmacro*{date+extradate}{%
     \iffieldundef{labelyear} {}
     {\printtext{\printlabeldateextra}}}% Took out parentheses around the year

\renewbibmacro*{author}{%
  \ifboolexpr{
    test \ifuseauthor
    and
    not test {\ifnameundef{author}}
  }
    {\usebibmacro{bbx:dashcheck}
       {\bibnamedash}
       {\usebibmacro{bbx:savehash}%
        \printnames{author}%
  \iffieldundef{authortype}
    {\newunit}%                               period instead of space
    {\setunit{\addcomma\space}}}%
     \iffieldundef{authortype}
       {}
       {\usebibmacro{authorstrg}%
  \newunit}}%                                 period instead of space
    {\global\undef\bbx@lasthash
     \usebibmacro{labeltitle}%
     \newunit}%                               period instead of space
  \usebibmacro{date+extradate}}

\renewbibmacro*{editor}{%
  \usebibmacro{bbx:editor}{editorstrg}}
\renewbibmacro*{editor+others}{%
  \usebibmacro{bbx:editor}{editor+othersstrg}}
\renewbibmacro*{bbx:editor}[1]{%
  \ifboolexpr{
    test \ifuseeditor
    and
    not test {\ifnameundef{editor}}
  }
    {\usebibmacro{bbx:dashcheck}
       {\bibnamedash}
       {\printnames{editor}%
  \setunit{\addspace}%
  \usebibmacro{bbx:savehash}}%
     \printtext[parens]{\usebibmacro{#1}}%
     \clearname{editor}%
     \newunit}%                                         period instead of space
    {\global\undef\bbx@lasthash
     \usebibmacro{labeltitle}%
     \newunit}%                                         period instead of space
  \usebibmacro{date+extradate}}

\renewbibmacro*{translator}{%
  \usebibmacro{bbx:translator}{translatorstrg}}
\renewbibmacro*{translator+others}{%
  \usebibmacro{bbx:translator}{translator+othersstrg}}
\renewbibmacro*{bbx:translator}[1]{%
  \ifboolexpr{
    test \ifusetranslator
    and
    not test {\ifnameundef{translator}}
  }
    {\usebibmacro{bbx:dashcheck}
       {\bibnamedash}
       {\printnames{translator}%
  \setunit{\addcomma\space}%
  \usebibmacro{bbx:savehash}}%
     \usebibmacro{translator+othersstrg}%
     \clearname{translator}%
     \newunit}%                                     period instead of space
    {\global\undef\bbx@lasthash
     \usebibmacro{labeltitle}%
     \newunit}%                                     period instead of space
  \usebibmacro{date+extradate}}

\renewbibmacro*{journal}{%
  \iffieldundef{journaltitle}
    {}
    {\printtext{%
       \printfield{journaltitle}%
       \setunit{\subtitlepunct}%
       \printfield{journalsubtitle}}}}

\newbibmacro*{journal+issuetitle+editor}{%
  \usebibmacro{journal}%
  \setunit*{\addspace}%
  \iffieldundef{series}
    {}
    {\newunit
     \printfield{series}%
     \setunit{\addspace}}%
  \usebibmacro{volume+number+eid}%
  \iftoggle{issueandeditor}
    {\setunit{\addspace}%
     \usebibmacro{issue+date}%
     \setunit{\addcolon\space}%
     \usebibmacro{issue}
     % The following three lines were originally not included inside of
     % the journal+issuetitle bibmacro. They have been moved inside of
     % this macro in order to allow them to be controlled by the toggle
     % `issuetitle` that is defined at the top of this style file.
     \newunit
     \usebibmacro{byeditor+others}%
     \newunit}
    {}%
  \newunit}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% The next three bib macros are for printing the maintitle and                     %
% booktitle fields of an @inproceedings entry with an ISSN as an                   %
% article in accordance with the unified style sheet guidelines.                   %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% 1. maintitle

\newbibmacro*{unified:proc-as-article:maintitle}{%
  \ifboolexpr{
    test {\iffieldundef{maintitle}}
    and
    test {\iffieldundef{mainsubtitle}}
  }
    {}
    {\printtext{%
       \printfield[maintitle]{maintitle}%
       \setunit{\subtitlepunct}%
       \printfield[maintitle]{mainsubtitle}}%
     \newunit}%
  \printfield{maintitleaddon}}

% 2. booktitle

\newbibmacro*{unified:proc-as-article:booktitle}{%
  \ifboolexpr{
    test {\iffieldundef{booktitle}}
    and
    test {\iffieldundef{booksubtitle}}
  }
    {}
    {\printtext{%
       \printfield[booktitle]{booktitle}%
       \setunit{\subtitlepunct}%
       \printfield[booktitle]{booksubtitle}}%
     \newunit}%
  \printfield{booktitleaddon}}

% 3. maintitle+booktitle

\newbibmacro*{unified:proc-as-article:maintitle+booktitle}{%
  \iffieldundef{maintitle}
    {}
    {\usebibmacro{unified:proc-as-article:maintitle}%
     \newunit\newblock}
  \usebibmacro{unified:proc-as-article:booktitle}%
  \setunit{\addspace}}

\renewbibmacro*{volume+number+eid}{%
  \printfield{volume}%
%  \setunit*{\adddot}%
  \printfield[parens]{number}%  parentheses instead of dot before issue number
  \setunit{\addcomma\space}%
  \printfield{eid}}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% This is for printing the volume field of a proceedings with an ISSN              %
% as an article in accordance with the unified style sheet guidelines.             %
% It depends on the declared field format below.                                   %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\newbibmacro*{unified:proc-as-article:volume+number+eid}{%
  \printfield[volume:unified:proc-as-article]{volume}%
  \printfield[parens]{number}%
  \setunit{\addcomma\space}%
  \printfield{eid}}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Because of the weird format "3 May, 2007" specified in the Unified               %
% Stylesheet for URL access dates, we need a special way to format the             %
% urldate                                                                          %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\newcommand{\mkbibdateunified}[3]{% Year-Month-Day as input --> xx Month, Year
  \iffieldundef{#3}
    {}
    {\stripzeros{\thefield{#3}}%
     \nobreakspace}%
  \iffieldundef{#2}
    {\iffieldundef{#1}%
      {}%
      {\stripzeros{\thefield{#1}}}}%
    {\mkbibmonth{\thefield{#2}}%
     \iffieldundef{#1}%
      {}%
      {\iffieldundef{#3}%
           {}%
           {,}%
         \space\stripzeros{\thefield{#1}}}%
    }%
  }%

\renewbibmacro*{url+urldate}{%
  \printfield{url}%
  \iffieldundef{urlyear}%
    {}%
    {\setunit*{\addspace}%
     \printtext[parens]{\mkbibdateunified{urlyear}{urlmonth}{urlday}}}%
  }

\renewbibmacro*{series+number}{%
  \iffieldundef{series}
    {}
    {\printtext[parens]{%
      \printfield{series}%
      \setunit*{\addspace}%
      \printfield{number}}%
    }}

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

\renewbibmacro*{chapter+pages}{%
  \iffieldundef{chapter}%
  {}%
  {\printfield{chapter}%
  \setunit{\addcomma\space}}%
  \printfield{pages}%
  \newunit}

\renewbibmacro*{note+pages}{%
  \iffieldundef{note}%
  {}%
  {\printfield{note}%
  \setunit{\addcomma\space}}%
  \printfield{pages}%
  \newunit}

\newbibmacro*{institution+location+type+date}{%
  \printlist{location}%
  \iflistundef{institution}
    {}
    {\setunit*{\addcolon\space}}%
  \printlist{institution}%
  \setunit{\addspace}%
  \printfield{type}%
  \setunit*{\addcomma\space}%
  \usebibmacro{date}%
  \newunit}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% The following is a hack to satisfy the Unified Stylesheet's decision             %
% to give the edition right after the OED as used as a sortlabel.                  %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\renewbibmacro*{labeltitle}{%
  \iffieldundef{label}
    {\iffieldundef{shorttitle}
       {\printfield{title}%
        \setunit{\addcomma\space}%     Here it comes, preparing for the edition
        \printfield{edition}%          Here's the edition
        \clearfield{title}%
        \clearfield{edition}}%         Clearing the edition field
       {\printfield[title]{shorttitle}}}
    {\printfield{label}}}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                         PUNCTUATION & FORMATTING                                 %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% This gets rid of the Oxford comma in name lists and uses the                     %
% ampersand rather than "and":                                                     %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\renewcommand*{\finalnamedelim}{\addnbspace\&\addspace}
\renewcommand*{\finallistdelim}{\addnbspace\&\addspace}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% no colon after "In" in incollection entries (overriding                          %
% biblatex.def):                                                                   %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\renewcommand{\intitlepunct}{\addspace}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Subtitles are indicated with a colon after the main title, unless the title      %
% ends in a punctuation mark. %                                                    %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\renewcommand{\subtitlepunct}{\addcolon\space}
\DefineBibliographyExtras{english}{\DeclarePunctuationPairs{colon}{*}}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% No comma before pages, just the usual new unit period                            %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\renewcommand*{\bibpagespunct}{\newunitpunct}

\DefineBibliographyStrings{english}{%
  edition = {edn\adddot},
  phdthesis = {dissertation},
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% basically everything is in sentence case, other than journals and                %
% book series (recurring titles)                                                   %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\DeclareFieldFormat[%
  article,book,collection,incollection,inproceedings,thesis,unpublished]%
  {titlecase}{\MakeSentenceCase*{#1}}%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% No quotes around titles                                                          %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\DeclareFieldFormat[%
  article,inbook,incollection,inproceedings,patent,thesis,unpublished]%
  {title}{#1}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Just like book titles, thesis titles are in italics                              %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\DeclareFieldFormat[thesis]{title}{\mkbibemph{#1}}

\DeclareFieldFormat{pages}{#1}     % no pp. prefix, took \mkpageprefix out [kvf]

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% This is for printing the volume field of a proceedings with an ISSN              %
% as an article in accordance with the unified style sheet guidelines              %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\DeclareFieldFormat{volume:unified:proc-as-article}{#1}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Display of DOIs etc.                                                             %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% The unified style displays DOIs according to the Crossref guidelines as full
% `https://doi.org/...'' links. But the user can choose to give the `compactlinks
% option when loading biblatex, which will use a more compact display.

\newtoggle{unified:compactlinks}
\DeclareBibliographyOption[boolean]{compactlinks}[true]{%
  \settoggle{unified:compactlinks}{#1}
  \settoggle{bbx:eprint}{#1}}

\DeclareFieldFormat{doi}{%
  \iftoggle{unified:compactlinks}%
    {\mkbibacro{DOI}\addcolon\space\href{https://doi.org/#1}{\nolinkurl{#1}}}%
    {\url{https://doi.org/#1}}}

\DeclareFieldFormat{url}{\url{#1}}

% Redefine jstor and googlebooks eprint formats (full links unless compactlinks option is set)

\DeclareFieldFormat{eprint:jstor}{%
  \iftoggle{unified:compactlinks}%
    {\mkbibacro{JSTOR}\addcolon\space\href{http://www.jstor.org/stable/#1}{\nolinkurl{#1}}}
    {\url{http://www.jstor.org/stable/#1}}}

\DeclareFieldFormat{eprint:googlebooks}{%
    \iftoggle{unified:compactlinks}%
      {Google Books\addcolon\space{\href{http://books.google.com/books?id=#1}{\nolinkurl{#1}}}}
      {\url{http://books.google.com/books?id=#1}}}

% Print LingBuzz entries using an eprint field
\DeclareFieldFormat{eprint:lingbuzz}{%
  \iftoggle{unified:compactlinks}%
    {LingBuzz\addcolon\space\href{https://ling.auf.net/lingbuzz/#1}{\nolinkurl{#1}}}
    {\url{https://ling.auf.net/lingbuzz/#1}}}
\DeclareFieldAlias{eprint:LingBuzz}{eprint:lingbuzz}

% Print Rutgers Optimality Archive entries using an eprint field
\DeclareFieldFormat{eprint:roa}{%
  \iftoggle{unified:compactlinks}%
  {\mkbibacro{ROA}\addcolon\space\href{https://roa.rutgers.edu/article/view/#1}{\nolinkurl{#1}}}
  {\url{https://roa.rutgers.edu/article/view/#1}}}
\DeclareFieldAlias{eprint:ROA}{eprint:roa}

% Unless the user issues the option `alllinks`, if there's a DOI, only that will be
% displayed, and if there's no DOI but an eprint, only that will be displayed. If
% `alllinks` is issued, all links (up to three) will be displayed

\newtoggle{unified:alllinks}
\DeclareBibliographyOption[boolean]{alllinks}[true]{%
  \settoggle{unified:alllinks}{#1}}

% We first check whether the url field contains a link to a DOI or an established eprint source
% If so, we put that info into the DOI or eprint fields
\AtBeginDocument{%
  \DeclareSourcemap{
    \maps[datatype=bibtex]{
      \map{\step[fieldsource=url,match=\regexp{(http|https):\/\/(dx\.)?doi\.org\/(.*)},final]
           \step[fieldset=doi, fieldvalue={$3}]
           \step[fieldset=url,null]
           \step[fieldset=urldate,null]}
      \map{\step[fieldsource=url,match=\regexp{(http|https):\/\/ling\.auf\.net\/lingbuzz\/(.*)},final]
           \step[fieldset=eprint, fieldvalue={$2}]
           \step[fieldset=eprinttype, fieldvalue={lingbuzz}]
           \step[fieldset=url,null]
           \step[fieldset=urldate,null]}
      \map{\step[fieldsource=url,match=\regexp{(http|https):\/\/www\.jstor\.org\/stable\/(.*)},final]
           \step[fieldset=eprint, fieldvalue={$2}]
           \step[fieldset=eprinttype, fieldvalue={jstor}]
           \step[fieldset=url,null]
           \step[fieldset=urldate,null]}
      \map{\step[fieldsource=url,match=\regexp{(http|https):\/\/books\.google\.com\/books\?id=(.*)},final]
           \step[fieldset=eprint, fieldvalue={$2}]
           \step[fieldset=eprinttype, fieldvalue={googlebooks}]
           \step[fieldset=url,null]
           \step[fieldset=urldate,null]}
      \map{\step[fieldsource=url,match=\regexp{(http|https):\/\/roa\.rutgers\.edu\/article\/view\/(.*)},final]
           \step[fieldset=eprint, fieldvalue={$2}]
           \step[fieldset=eprinttype, fieldvalue={roa}]
           \step[fieldset=url,null]
           \step[fieldset=urldate,null]}
       }}
  \iftoggle{unified:alllinks}
  {}
  {\DeclareSourcemap{
        \maps[datatype=bibtex]{
          \map{\step[fieldsource=doi,final]
               \step[fieldset=eprint,null]
               \step[fieldset=eprinttype,null]
               \step[fieldset=url,null]
               \step[fieldset=urldate,null]}
          \map{\step[fieldsource=eprint,final]
               \step[fieldset=url,null]
               \step[fieldset=urldate,null]}}}}}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% biblatex doesn't like things like "forthcoming'' in the year/date field. The     %
% canonical way to do this is to put "forthcoming" in the `pubstate` field. Since  %
% we want that to be treated as the label, we need to adjust some things. What we  %
% do here is suggested at https://tex.stackexchange.com/a/444414/70432.            %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\DeclareLabeldate{%
  \field{date}
  \field{year}
  \field{eventdate}
  \field{origdate}
  \field{urldate}
  \field{pubstate}
  \literal{nodate}
}

\renewbibmacro*{addendum+pubstate}{%
  \printfield{addendum}%
  \newunit\newblock
  \iffieldequalstr{labeldatesource}{pubstate}
    {}
    {\printfield{pubstate}}}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                         THE BIBLIOGRAPHY DRIVERS                                 %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% First, the entry types used in the Unified Test Bibliography.                    %
% Could rely on standard.bbx for all others as a fallback.                         %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% For the article type, the only departure from standard.bbx is that               %
% we don't use a literal "In: " before the journal title; other                    %
% formatting departures are done in the format specs and bibmacros                 %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\DeclareBibliographyDriver{article}{%
  \usebibmacro{bibindex}%
  \usebibmacro{begentry}%
  \usebibmacro{author/translator+others}%
  \setunit{\labelnamepunct}\newblock
  \usebibmacro{title}%
  \newunit
  \printlist{language}%
  \newunit\newblock
  \usebibmacro{byauthor}%
  \newunit\newblock
  \usebibmacro{bytranslator+others}%
  \newunit\newblock
  \printfield{version}%
  \newunit\newblock
  \usebibmacro{journal+issuetitle+editor}%
  \newblock%                          \newblock ensures period before pages
  \usebibmacro{note+pages}%
  \newunit\newblock
  \iftoggle{bbx:isbn}
    {\printfield{issn}}
    {}%
  \newunit\newblock
  \usebibmacro{doi+eprint+url}%
  \newunit\newblock
  \usebibmacro{addendum+pubstate}%
  \setunit{\bibpagerefpunct}\newblock
  \usebibmacro{pageref}%
  \newunit\newblock
  \iftoggle{bbx:related}
    {\usebibmacro{related:init}%
     \usebibmacro{related}}
    {}%
  \usebibmacro{finentry}}

\DeclareBibliographyDriver{book}{%
  \usebibmacro{bibindex}%
  \usebibmacro{begentry}%
  \usebibmacro{author/editor+others/translator+others}%
  \setunit{\labelnamepunct}\newblock
  \usebibmacro{maintitle+title}%
  \newunit
  \printlist{language}%
  \newunit\newblock
  \usebibmacro{byauthor}%
  \newunit\newblock
  \usebibmacro{byeditor+others}%
  \newunit\newblock
  \printfield{edition}%
  \newunit
  \iffieldundef{maintitle}
    {\printfield{volume}%
     \printfield{part}}
    {}%
  \newunit
  \printfield{volumes}%
  \setunit{\addspace}% 
  \usebibmacro{series+number}%
  \newunit\newblock
  \printfield{note}%
  \newunit\newblock
  \usebibmacro{publisher+location+date}%
  \newunit\newblock
  \usebibmacro{chapter+pages}%
  \newunit
  \printfield{pagetotal}%
  \newunit\newblock
  \iftoggle{bbx:isbn}
    {\printfield{isbn}}
    {}%
  \newunit\newblock
  \usebibmacro{doi+eprint+url}%
  \newunit\newblock
  \usebibmacro{addendum+pubstate}%
  \setunit{\bibpagerefpunct}\newblock
  \usebibmacro{pageref}%
  \newunit\newblock
  \iftoggle{bbx:related}
    {\usebibmacro{related:init}%
     \usebibmacro{related}}
    {}%
  \usebibmacro{finentry}}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Aliased to ensure no period between the title and the series.                    %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\DeclareBibliographyAlias{collection}{book}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% @inbook entries look terrible as-is, and have fields very similar to             %
% @incollection anyway                                                             %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\DeclareBibliographyAlias{inbook}{incollection}

\DeclareBibliographyAlias{incollection}{inproceedings}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Given the guidelines in the unified style sheet, we should print                 %
% conference proceedings and working papers as @article's just in case             %
% the publication has an ISSN. So, rather than have users handle this              %
% in the database by changing the entry type, we can implement this by             %
% checking if the inproceedings entry has an ISSN. If the field is                 %
% undefined, the driver will do what driver did for @inproceedings                 %
% entries as of 512f11657199a6044f7663da454f3eac338bdbd5 except that               %
% \printlist{organization} and \newunit have been removed. On the                  %
% other hand, if the ISSN field is not undefined, then we will do                  %
% largely the exact same thing that we do in the @article driver,                  %
% except that we use the two macros                                                %
% \usebibmacro{unified:proc-as-article:maintitle+booktitle} and                    %
% \usebibmacro{unified:proc-as-article:volume+number+eid} instead of               %
% \usebibmacro{journal+issuetitle} and except that                                 %
% \usebibmacro{byeditor+others} has been removed so as to ensure that              %
% the editors are not listed even if they are present in the database.             %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\DeclareBibliographyDriver{inproceedings}{%
  \usebibmacro{bibindex}%
  \usebibmacro{begentry}%
  \iffieldundef{issn}
    {\usebibmacro{author/translator+others}%
     \setunit{\labelnamepunct}\newblock
     \usebibmacro{title}%
     \newunit
     \printlist{language}%
     \newunit\newblock
     \usebibmacro{byauthor}%
     \newunit\newblock
     \usebibmacro{in:}%
     \ifnameundef{editor}
       {\setunit{\addspace}}
       {\usebibmacro{byeditor+others}\setunit{\addcomma\space}}
     \usebibmacro{maintitle+booktitle}%
     \setunit{\addcomma\space}
     \printfield{edition}%
     \setunit{\addcomma\space}
     \iffieldundef{maintitle}
       {\printfield{volume}%
        \printfield{part}}
       {}%
     \setunit{\addcomma\space}
     \printfield{volumes}%
     \setunit{\addspace}%
     \usebibmacro{series+number}%
     \setunit{\addcomma\space}
     \usebibmacro{chapter+pages}%
     \newunit\newblock
     \printfield{note}%
     \newunit\newblock
     \usebibmacro{publisher+location+date}%
     \newunit\newblock
     \iftoggle{bbx:isbn}
       {\printfield{isbn}}
       {}%
     \newunit\newblock
     \usebibmacro{doi+eprint+url}%
     \newunit\newblock
     \usebibmacro{addendum+pubstate}%
     \setunit{\bibpagerefpunct}\newblock
     \usebibmacro{pageref}%
     \newunit\newblock
     \iftoggle{bbx:related}
       {\usebibmacro{related:init}%
        \usebibmacro{related}}
       {}}
    {\usebibmacro{author/translator+others}%
     \setunit{\labelnamepunct}\newblock
     \usebibmacro{title}%
     \newunit
     \printlist{language}%
     \newunit\newblock
     \usebibmacro{byauthor}%
     \newunit\newblock
     \usebibmacro{bytranslator+others}%
     \newunit\newblock
     \printfield{version}%
     \newunit\newblock
     \usebibmacro{unified:proc-as-article:maintitle+booktitle}%
     \usebibmacro{unified:proc-as-article:volume+number+eid}
     \newunit\newblock%
     \usebibmacro{note+pages}%
     \newunit\newblock
     \iftoggle{bbx:isbn}
       {\printfield{issn}}
       {}%
     \newunit\newblock
     \usebibmacro{doi+eprint+url}%
     \newunit\newblock
     \usebibmacro{addendum+pubstate}%
     \setunit{\bibpagerefpunct}\newblock
     \usebibmacro{pageref}%
     \newunit\newblock
     \iftoggle{bbx:related}
       {\usebibmacro{related:init}%
        \usebibmacro{related}}
       {}}
  \usebibmacro{finentry}}

\DeclareBibliographyDriver{thesis}{%
  \usebibmacro{bibindex}%
  \usebibmacro{begentry}%
  \usebibmacro{author}%
  \setunit{\labelnamepunct}\newblock
  \usebibmacro{title}%
  \newunit
  \printlist{language}%
  \newunit\newblock
  \usebibmacro{byauthor}%
  \newunit\newblock
  \printfield{note}%
  \newunit\newblock
  \usebibmacro{institution+location+type+date}%
  \newunit\newblock
  \usebibmacro{chapter+pages}%
  \newunit
  \printfield{pagetotal}%
  \newunit\newblock
  \iftoggle{bbx:isbn}
    {\printfield{isbn}}
    {}%
  \newunit\newblock
  \usebibmacro{doi+eprint+url}%
  \newunit\newblock
  \usebibmacro{addendum+pubstate}%
  \setunit{\bibpagerefpunct}\newblock
  \usebibmacro{pageref}%
  \newunit\newblock
  \iftoggle{bbx:related}
    {\usebibmacro{related:init}%
     \usebibmacro{related}}
    {}%
  \usebibmacro{finentry}}

\DeclareBibliographyDriver{unpublished}{%
  \usebibmacro{bibindex}%
  \usebibmacro{begentry}%
  \usebibmacro{author}%
  \setunit{\labelnamepunct}\newblock
  \usebibmacro{title}%
  \newunit
  \printlist{language}%
  \newunit\newblock
  \usebibmacro{byauthor}%
  \newunit\newblock
  \printfield{howpublished}%
  \newunit\newblock
  \printfield{note}%
  \newunit\newblock
  \usebibmacro{location+date}%
  \newunit\newblock
  \usebibmacro{doi+eprint+url}%
  \newunit\newblock
  \usebibmacro{addendum+pubstate}%
  \setunit{\bibpagerefpunct}\newblock
  \usebibmacro{pageref}%
  \newunit\newblock
  \iftoggle{bbx:related}
    {\usebibmacro{related:init}%
     \usebibmacro{related}}
    {}%
  \usebibmacro{finentry}}