% This is a biblatex citation style file, adapted from Lehman's
% authoryear-comp.cbx.  It is heavily modified, and contains the
% common code for providing inline citations (and a reference list)
% for the two author-date styles of the Chicago Manual of Style, 16th
% edition.

\ProvidesFile{chicago-dates-common16.cbx}[2022/11/16 v 3.18b biblatex
citation style]

%%%% Biblatex initialization + Chicago options + Toggles %%%%

\newbool{cbx:parens}
\newbool{cms:bracket}
\newbool{cms:extraparens}
\newbool{cms:comma}

\ifdef{\ExplSyntaxOff}{}{\RequirePackage{xparse}\RequirePackage{expl3}}%

\ExplSyntaxOn%

\cs_if_exist:NTF \__text_change_case_switch_titleonly:nnNnnnn
{}
{\cs_new:Npn \__text_change_case_switch_titleonly:nnNnnnn #1#2#3#4#5#6#7
  {
    \__text_change_case_store:n {#7}
    \__text_change_case_break:w
  }
}

\ExplSyntaxOff

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

\providetoggle{cms@inlineibid}
\providetoggle{cms@origlabel}
\providetoggle{cms@bothlabelold}
\providetoggle{cms@bothlabelnew}
\providetoggle{cms@fulldate}
\providetoggle{cms@reprint}
\providetoggle{cms@switchdates}
\providetoggle{cms@los}
\providetoggle{cms@avdate}
\providetoggle{cms@ordate}
\providetoggle{cms@nodates}
\providetoggle{cms@bc}% Attempt to provide correct dateera handling
\providetoggle{cms@alwaysrange}% Turns off decade and century handling
\providetoggle{cms@decaderange}% Only turns off decade handling
\providetoggle{cms@centuryrange}% Only turns off century handling
\providetoggle{cms@nodatebrackets}% For brackets in uncertain & circa
\providetoggle{cms@noyearbrackets}%
\providetoggle{cms@authorparens}
\providetoggle{cms@datedash}
\providetoggle{cms@modpostnote}
\providetoggle{cms@ukord}
\providetoggle{cms@fullnote}% For the legal entry types
\providetoggle{cms@shortnote}% Ditto

\providetoggle{cms@url}% These are for the field-exclusion options
\providetoggle{cms@urltime}% 17th ed.
\providetoggle{cms@doi}
\providetoggle{cms@doionly}
\providetoggle{cms@eprint}
\providetoggle{cms@isbn}
\providetoggle{cms@numbermonth}
\providetoggle{cms@bookpages}
\providetoggle{cms@addendum}
\providetoggle{cms@hidevolumes}% Modify Volume fix
\providetoggle{cms@comprange}
\providetoggle{cms@compyears}

\providetoggle{cms@jrcomma}% Comma after Jr./Sr.

\providetoggle{cms@strict}
\providetoggle{cms@headlessnote}% Keep
\providetoggle{cms@noibid}% Keep
\providetoggle{cms@namedash}
\providetoggle{cms@usecompiler}% Keep
\providetoggle{cms@origpublished}% Keep
\providetoggle{cms@annotation}% Keep
\providetoggle{cms@postposit}% Keep
\providetoggle{cms@fullshhand}%
\providetoggle{cms@allshort}% For legal types
\providetoggle{cms@noneshort}% Ditto
\providetoggle{cms@legalnotes}% Ditto
\providetoggle{cms@supranotes}% Ditto
\providetoggle{cms@vol}%
\providetoggle{cms@crossref}%
\providetoggle{cms@bookcrossref}
\providetoggle{cms@gencite}
\providetoggle{cms@genallnames}
\providetoggle{cms@xrefurl}
\providetoggle{cms@related}
\providetoggle{cms@linkit}

\AtEveryCitekey{%
  \iffieldundef{userc}%
  {}%
  {\nocite{\thefield{userc}}}%
  \global\csundef{@cmsst}%
  \iffieldequalstr{pubstate}{reprint}%
  {\toggletrue{cms@reprint}}%
  {\togglefalse{cms@reprint}}}%

\protected\def\cms@warning@noline#1{%
  \begingroup
  \blx@safe@actives
  \PackageWarningNoLine{biblatex-chicago}{#1}%
  \endgroup}%

\DeclareBibliographyOption[string]{avdate}[true]{%
  \ifcsdef{cms@opt@avdate@#1}%
  {\csuse{cms@opt@avdate@#1}}%
  {\blx@err@invopt{avdate=#1}{}}}%
\def\cms@opt@avdate@true{%
  \DeclareLabeldate[music,review,standard,suppperiodical,video]% 17th ed.
  {\field{eventdate} \field{origdate} \field{date} \field{year}%
    \field{urldate} \literal{nodate}}%
    \toggletrue{cms@avdate}}%
\def\cms@opt@avdate@false{%
  \togglefalse{cms@avdate}}%

\DeclareBibliographyOption[string]{cmsdate}[off]{% Implement origyear as
  \ifcsdef{cms@global@cmsdate@#1}% labelyear. Sorting will be an issue.
  {\csuse{cms@global@cmsdate@#1}}%
  {\csuse{cms@global@cmsdate@off}\cms@warning@noline%
    {'cmsdate=#1' isn't a valid option.\MessageBreak
      The default - 'off' - has been set.\MessageBreak
      Please see biblatex-chicago.pdf for valid\MessageBreak
      option keys}}}%
\def\cms@global@cmsdate@on{%
  \toggletrue{cms@origlabel}%
  \ExecuteBibliographyOptions{cmsorigdate=true}}%
\def\cms@global@cmsdate@new{%
  \toggletrue{cms@bothlabelnew}%
  \ExecuteBibliographyOptions{cmsorigdate=true}}%
\def\cms@global@cmsdate@old{%
  \toggletrue{cms@bothlabelold}%
  \ExecuteBibliographyOptions{cmsorigdate=true}}%
\def\cms@global@cmsdate@both{%
  \toggletrue{cms@bothlabelold}%
  \ExecuteBibliographyOptions{cmsorigdate=true}}%
\def\cms@global@cmsdate@full{}%
\def\cms@global@cmsdate@off{}%

\DeclareBibliographyOption[string]{cmsorigdate}[true]{% ? Also need new macros
  \ifcsdef{cms@opt@origdate@#1}% for printing dates.  Worth it ?
  {\csuse{cms@opt@origdate@#1}}%
  {\blx@err@invopt{cmsorigdate=#1}{}}}%
\def\cms@opt@origdate@true{%
  \DeclareLabeldate{\field{origdate} \field{origyear} \field{date}%
    \field{year} \field{eventdate} \field{urldate}%
    \literal{nodate}}%
  \DeclareLabeldate[patent]{\field{date} \field{year}%
    \field{eventdate} \field{origdate} \field{origyear}%
    \field{urldate}}%
  \global\toggletrue{cms@ordate}}%
\def\cms@opt@origdate@false{\togglefalse{cms@ordate}}%

\DeclareEntryOption[string]{cmsdate}[off]{% Trying to implement origyear as
  \ifcsdef{cms@opt@cmsdate@#1}% labelyear. Sorting will be an issue.
  {\csuse{cms@opt@cmsdate@#1}}%
  {\csuse{cms@opt@cmsdate@off}\cms@warning@noline%
    {'cmsdate=#1' isn't a valid option.\MessageBreak
      The default - 'off' - has been set.\MessageBreak
      Please see biblatex-chicago.pdf for valid\MessageBreak
      option keys}}}%
\def\cms@opt@cmsdate@on{%
  \toggletrue{cms@origlabel}}%
\def\cms@opt@cmsdate@new{%
  \toggletrue{cms@bothlabelnew}}%
\def\cms@opt@cmsdate@old{%
  \toggletrue{cms@bothlabelold}}%
\def\cms@opt@cmsdate@both{%
  \toggletrue{cms@bothlabelold}}%
\def\cms@opt@cmsdate@full{%
  \toggletrue{cms@fulldate}}%
\def\cms@opt@cmsdate@off{}%

\DeclareBiblatexOption{global,entry}[boolean]{alwaysrange}[true]{%
  \settoggle{cms@alwaysrange}{#1}}%

\DeclareBiblatexOption{global,entry}[boolean]{decaderange}[true]{%
  \settoggle{cms@decaderange}{#1}}%

\DeclareBiblatexOption{global,entry}[boolean]{centuryrange}[true]{%
  \settoggle{cms@centuryrange}{#1}}%

\DeclareBiblatexOption{global,type,entry}[boolean]{nodatebrackets}[true]{%
  \settoggle{cms@nodatebrackets}{#1}}%

\DeclareBiblatexOption{global,type,entry}[boolean]{noyearbrackets}[true]{%
  \settoggle{cms@noyearbrackets}{#1}}%

\DeclareBiblatexOption{global,entry}[boolean]{genallnames}[true]{%
  \settoggle{cms@genallnames}{#1}}%

\DeclareBiblatexOption{global,entry}[boolean]{hypertitle}[true]{%
  \settoggle{cms@linkit}{#1}}%

\DeclareBibliographyOption[boolean]{annotation}[true]{%
  \global\settoggle{cms@annotation}{#1}}%

\DeclareBibliographyOption[boolean]{dashed}[true]{%
  \settoggle{cms@namedash}{#1}}

\DeclareBibliographyOption[boolean]{short}[true]{%
  \global\settoggle{cms@allshort}{#1}}

\DeclareBiblatexOption{global,entry}[boolean]{noneshort}[true]{%
  \settoggle{cms@noneshort}{#1}}

\DeclareBibliographyOption[boolean]{legalnotes}[true]{%
  \global\settoggle{cms@legalnotes}{#1}}

\DeclareBiblatexOption{global,entry}[boolean]{supranotes}[true]{%
  \settoggle{cms@supranotes}{#1}}

\DeclareBibliographyOption[boolean]{cmslos}[true]{%
  \global\settoggle{cms@los}{#1}}%

\DeclareBibliographyOption[boolean]{noibid}[true]{%
  \global\settoggle{cms@noibid}{#1}}%

\DeclareBibliographyOption[boolean]{compresspages}[true]{%
  \global\settoggle{cms@comprange}{#1}}%

\DeclareBibliographyOption[boolean]{compressyears}[true]{%
  \global\settoggle{cms@compyears}{#1}}%

\DeclareBibliographyOption[string]{postnotepunct}[true]{%
  \ifcsdef{cms@opt@ppunct@#1}%
  {\csuse{cms@opt@ppunct@#1}}%
  {\blx@err@invopt{postnotepunct=#1}{}}}%
\def\cms@opt@ppunct@true{%
  \global\toggletrue{cms@modpostnote}}%
\def\cms@opt@ppunct@false{}%

\DeclareBiblatexOption{global,entry}[boolean]{usecompiler}[true]{%
  \settoggle{blx@usenamec}{#1}}%

\DeclareBiblatexOption{global,entry}[boolean]{nodates}[true]{%
  \settoggle{cms@nodates}{#1}}%

\DeclareBiblatexOption{global,entry}[boolean]{juniorcomma}[true]{%
  \settoggle{cms@jrcomma}{#1}}%

\DeclareBibliographyOption[boolean]{shorthandfull}[true]{%
  \settoggle{cms@fullshhand}{#1}}%

\DeclareBiblatexOption{global,entry}[string]{longcrossref}[false]{%
  \ifcsdef{cms@opt@lxref@#1}%
  {\csuse{cms@opt@lxref@#1}}%
  {\blx@err@invopt{longcrossref=#1}{}}}%
\def\cms@opt@lxref@none{%
  \togglefalse{cms@crossref}%
  \togglefalse{cms@bookcrossref}}%
\def\cms@opt@lxref@true{%
  \toggletrue{cms@crossref}}%
\def\cms@opt@lxref@false{%
  \togglefalse{cms@crossref}}%
\def\cms@opt@lxref@notes{%
  \togglefalse{cms@crossref}}%
\def\cms@opt@lxref@bib{%
  \toggletrue{cms@crossref}}%

\DeclareBiblatexOption{global,entry}[boolean]{booklongxref}[true]{%
  \ifcsdef{cms@opt@bklxref@#1}%
  {\csuse{cms@opt@bklxref@#1}}%
  {\blx@err@invopt{booklongxref=#1}{}}}%
\def\cms@opt@bklxref@true{%
  \toggletrue{cms@bookcrossref}}%
\def\cms@opt@bklxref@false{%
  \togglefalse{cms@bookcrossref}}%
\def\cms@opt@bklxref@notes{%
  \togglefalse{cms@bookcrossref}}%
\def\cms@opt@bklxref@bib{%
  \toggletrue{cms@bookcrossref}}%

\DeclareBiblatexOption{global,entry}[boolean]{xrefurl}[true]{%
  \settoggle{cms@xrefurl}{#1}}%

\DeclareBiblatexOption{global,entry}[string]{journalabbrev}[false]{%
  \ifcsdef{cms@opt@jtabb@#1}%
  {\csuse{cms@opt@jtabb@#1}}%
  {\csuse{cms@opt@jtabb@false}\cms@warning@noline%
    {'journalabbrev=#1' isn't a valid option.\MessageBreak
      The default - 'false' - has been set.\MessageBreak
      Please see biblatex-chicago.pdf for valid\MessageBreak
      option keys}}}%
\def\cms@opt@jtabb@true{%
  \toggletrue{cms@citejtabb}%
  \toggletrue{cms@bibjtabb}}%
\def\cms@opt@jtabb@false{%
  \togglefalse{cms@citejtabb}%
  \togglefalse{cms@bibjtabb}}%
\def\cms@opt@jtabb@notes{%
  \toggletrue{cms@citejtabb}%
  \togglefalse{cms@bibjtabb}}%
\def\cms@opt@jtabb@bib{%
  \togglefalse{cms@citejtabb}%
  \toggletrue{cms@bibjtabb}}%

\DeclareBiblatexOption{global,type,entry}[boolean]{related}[true]{%
  \settoggle{cms@related}{#1}}%

\DeclareBibliographyOption[boolean]{ordinalgb}[true]{%
  \settoggle{cms@ukord}{#1}}%

% Controlling punctuation before titleaddon fields %

\DeclareBiblatexOption{global,type,entry}[string]{ptitleaddon}[period]{%
  \ifcsdef{cms@opt@ptao@#1}%
    {\csuse{cms@opt@ptao@#1}}%
    {\csuse{cms@opt@ptao@period}\cms@warning@noline%
      {'ptitleaddon=#1' is not a valid option.\MessageBreak
        The default - 'period' - has been set.\MessageBreak
        Please see biblatex-chicago.pdf for valid\MessageBreak
        option keys}}}%
\def\cms@opt@ptao@none{%
  \let\ptitleaddonpunct\@empty}%
\def\cms@opt@ptao@comma{%
  \def\ptitleaddonpunct{\addcomma\addspace}}%
\def\cms@opt@ptao@colon{%
  \def\ptitleaddonpunct{\addcolon\addspace}}%
\def\cms@opt@ptao@space{%
  \def\ptitleaddonpunct{\addspace}}%
\def\cms@opt@ptao@semicolon{%
  \def\ptitleaddonpunct{\addsemicolon\addspace}}%
\def\cms@opt@ptao@period{%
  \def\ptitleaddonpunct{\addperiod\addspace}}%

\DeclareBiblatexOption{global,type,entry}[string]{ctitleaddon}[comma]{%
  \ifcsdef{cms@opt@ctao@#1}%
    {\csuse{cms@opt@ctao@#1}}%
    {\csuse{cms@opt@ctao@comma}\cms@warning@noline%
      {'ctitleaddon=#1' is not a valid option.\MessageBreak
        The default - 'comma' - has been set.\MessageBreak
        Please see biblatex-chicago.pdf for valid\MessageBreak
        option keys}}}%
\def\cms@opt@ctao@none{%
  \let\ctitleaddonpunct\@empty}%
\def\cms@opt@ctao@comma{%
  \def\ctitleaddonpunct{\addcomma\addspace}}%
\def\cms@opt@ctao@colon{%
  \def\ctitleaddonpunct{\addcolon\addspace}}%
\def\cms@opt@ctao@space{%
  \def\ctitleaddonpunct{\addspace}}%
\def\cms@opt@ctao@semicolon{%
  \def\ctitleaddonpunct{\addsemicolon\addspace}}%
\def\cms@opt@ctao@period{%
  \def\ctitleaddonpunct{\addperiod\addspace}}%

% The field-exclusion options %

\DeclareBiblatexOption{global,type,entry}[boolean]{urlstamp}[true]{%
  \settoggle{cms@urltime}{#1}}%

\DeclareBiblatexOption{global,type,entry}[boolean]{isbn}[true]{%
  \settoggle{cms@isbn}{#1}}%
\DeclareBiblatexOption{global,type,entry}[boolean]{url}[true]{%
  \settoggle{cms@url}{#1}}%
\DeclareBiblatexOption{global,type,entry}[string]{doi}[true]{%
  \ifcsdef{cms@opt@doi@#1}%
  {\csuse{cms@opt@doi@#1}}%
  {\blx@err@invopt{doi=#1}{}}}%
\def\cms@opt@doi@true{%
  \toggletrue{cms@doi}}%
\def\cms@opt@doi@false{%
  \togglefalse{cms@doi}}%
\def\cms@opt@doi@only{%
  \toggletrue{cms@doionly}}%
\DeclareBiblatexOption{global,type,entry}[boolean]{eprint}[true]{%
  \settoggle{cms@eprint}{#1}}%
\DeclareBiblatexOption{global,type,entry}[boolean]{numbermonth}[true]{%
  \settoggle{cms@numbermonth}{#1}}%
\DeclareBiblatexOption{global,type,entry}[boolean]{bookpages}[true]{%
  \settoggle{cms@bookpages}{#1}}%
\DeclareBiblatexOption{global,type,entry}[boolean]{includeall}[true]{%
  \settoggle{cms@isbn}{#1}%
  \settoggle{cms@url}{#1}%
  \settoggle{cms@urltime}{#1}% 17th ed.
  \settoggle{cms@doi}{#1}%
  \settoggle{cms@eprint}{#1}%
  \settoggle{cms@numbermonth}{#1}%
  \settoggle{cms@bookpages}{#1}}%
\DeclareBiblatexOption{global,type,entry}[boolean]{hidevolumes}[true]{%
  \settoggle{cms@hidevolumes}{#1}}%

\DeclareEntryOption[boolean]{authortitle}[true]{%
  \settoggle{cms@authortitle}{#1}}%

\ExecuteBibliographyOptions{includeall,hidevolumes,booklongxref,related,%
  ctitleaddon,ptitleaddon,journalabbrev=notes,legalnotes,supranotes,%
  dashed}%

\iftoggle{cms@legalnotes}%
{\ExecuteBibliographyOptions[jurisdiction,legal,legislation]{skipbib}}%
{}%

\AtEndPreamble{%       Automatic sorting by shorthand when it appears
  \iftoggle{cms@los}%  at the head of the entry.
  {\DeclareSourcemap{
      \maps[datatype=bibtex]{
        \map{
          \step[fieldsource=shorthand]
          \step[fieldset=sortname, origfieldval]}}}}%
  {}}

\DeclareLabeltitle[legislation]{%
  \field{shorttitle}%
  \field{titleaddon}%
  \field{title}}%

\DeclareDataInheritance{collection}{suppcollection}{%
  \inherit{title}{title}
  \inherit{subtitle}{subtitle}
  \inherit{titleaddon}{titleaddon}}

\DeclareDataInheritance{mvbook}{incollection}{%
  \inherit{title}{maintitle}
  \inherit{subtitle}{mainsubtitle}
  \inherit{titleaddon}{maintitleaddon}
  \noinherit{shorttitle}
  \noinherit{sorttitle}
  \noinherit{indextitle}
  \noinherit{indexsorttitle}
}

\DeclareDataInheritance{book}{incollection}{%
  \inherit{title}{booktitle}
  \inherit{subtitle}{booksubtitle}
  \inherit{titleaddon}{booktitleaddon}
  \noinherit{shorttitle}
  \noinherit{sorttitle}
  \noinherit{indextitle}
  \noinherit{indexsorttitle}
}

\DeclareDataInheritance{book,collection}{letter}{%
  \inherit{title}{booktitle}
  \inherit{subtitle}{booksubtitle}
  \inherit{titleaddon}{booktitleaddon}
  \noinherit{shorttitle}
  \noinherit{sorttitle}
  \noinherit{indextitle}
  \noinherit{indexsorttitle}
}

\DeclareDataInheritance{mvbook,mvcollection}{letter}{%
  \inherit{title}{maintitle}
  \inherit{subtitle}{mainsubtitle}
  \inherit{titleaddon}{maintitleaddon}
  \noinherit{shorttitle}
  \noinherit{sorttitle}
  \noinherit{indextitle}
  \noinherit{indexsorttitle}
}

\DeclareDataInheritance{*}{*}{%
  \noinherit{namea}
  \noinherit{nameb}
  \noinherit{sortyear}
  \noinherit{sortname}
  \noinherit{sorttitle}
  \noinherit{sorttitle}
  \noinherit{urlyear}
  \noinherit{urlmonth}
  \noinherit{urlday}
  \noinherit{urlyeardivision}
  \noinherit{urlhour}
  \noinherit{urlminute}
  \noinherit{doi}
  \noinherit{eprint}
  \noinherit{eprinttype}
  \noinherit{url}}

\DeclareDataInheritance{mvbook,mvcollection,mvproceedings,mvreference}%
{*}{% ???
  \noinherit{year}
  \noinherit{month}
  \noinherit{day}
  \noinherit{yeardivision}
  \noinherit{endyear}
  \noinherit{endmonth}
  \noinherit{endday}
  \noinherit{endyeardivision}
  \noinherit{origyear}
  \noinherit{origmonth}
  \noinherit{origday}
  \noinherit{origyeardivision}
  \noinherit{origendyear}
  \noinherit{origendmonth}
  \noinherit{origendday}
  \noinherit{origendyeardivision}}

% More authordate options %

\DeclareSortingTemplate{cms}{% Updated to >3.7 format
  \sort{
    \field{presort}
  }
  \sort[final]{
    \field{sortkey}
  }
  \sort{
    \field{sortname}
    \field{author}
    \field{namea}
    \field{editor}
    \field{nameb}
    \field{translator}
    \field{namec}
    \field{sorttitle}
    \field{journaltitle}
    \field{organization}
    \field{title}
  }
  \sort{
    \field{sortyear}
    \field{labelyear}
    \field{year}
    \field{origyear}
  }
  \sort{
    \field{sorttitle}
    \field{title}
  }
  \sort{
    \field{endyear}
    \field{eventendyear}
    \field{origendyear}
  }
  \sort{
    \field[padside=left,padwidth=4,padchar=0]{volume}
    \literal{0000}
  }
}

\DeclareSortExclusion{misc,online}{organization}
\DeclareSortExclusion{inreference,mvreference,reference}{%
  author,editor,namea,nameb,namec,translator}

\DeclareSortingTemplate{shortjournal}{%
  \sort{
    \field{shortjournal}
  }
}

\DeclareLabelname{\field{shortauthor} \field{author}%
  \field{shorteditor} \field{namea} \field{editor}%
  \field{nameb} \field{translator} \field{namec}}%

\DeclareEntryOption[boolean]{switchdates}[true]{%
  \settoggle{cms@switchdates}{#1}}%

\DeclareBibliographyOption[boolean]{strict}[true]{%
  \settoggle{cms@strict}{#1}%
  \iftoggle{cms@strict}%
  {\let\splitfootnoterule\footnoterule
    \renewcommand\footnoterule{}%
    \advance\skip\footins 4\p@\@plus2\p@\relax
    \gdef\split@prev{0}%
    \let\pagefootnoterule\footnoterule
    % \def\splitfootnoterule{\kern-3\p@ \hrule \kern2.6\p@}
    \def\footnoterule{\relax
      \ifnum\split@prev=\z@
      \pagefootnoterule
      \else
      \splitfootnoterule
      \fi
      \xdef\split@prev{\the\insertpenalties}%
    }}%
  {}}%

\protected\def\blx@newcunit{%
  \iftoggle{blx@keepunit}%
  {}%
  {\global\let\blx@unitpunct\newcunitpunct
    \global\toggletrue{blx@unit}}}%

\appto\blx@blxinit{%
  \let\newcunit\blx@newcunit}%

\newcommand*{\newcunitpunct}{\addcomma\space}

\def\mkbibcurdinal#1{%
  \@tempcnta0#1 \the\@tempcnta}%

\@ifpackagelater{biblatex}{2011/01/04}%
{}%
{\PackageError{biblatex}%
  {Outdated 'biblatex' package}%
  {The Chicago style requires biblatex v1.1 or later.\MessageBreak
    You are using: '\csuse{ver@biblatex.sty}'.\MessageBreak
    This is a fatal error. I'm aborting now.}%
  \endinput}%

% American-specific punctuation change for 16th edition %

\DefineBibliographyExtras{american}{%
  \DeclarePunctuationPairs{comma}{*!?}}

%%%% Macros from authoryear-comp.cbx, revised for CMS %%%%

\newbibmacro*{cite:init}{%
  \ifnumless{\value{multicitecount}}{2}%
    {\global\boolfalse{cbx:parens}%
     \global\undef\cbx@lasthash%
     \global\undef\cbx@lastyear}%
    {\iffieldundef{prenote}%
       {}%
       {\global\undef\cbx@lasthash%
	\global\undef\cbx@lastyear}}}%

\newbibmacro*{cite:reinit}{%
  \global\undef\cbx@lasthash%
  \global\undef\cbx@lastyear}%

\newbibmacro*{backref+check}{%
  \ifbibliography%
  {\backtrackerfalse}%
  {}}

\newbibmacro*{cite}{%
  \ifthenelse{\ifciteibid\AND\NOT\iffirstonpage}%
  {\usebibmacro{cite:ibid}}%
  {\iffieldequalstr{entrysubtype}{classical}% Similar to notes+bib
    {\iffieldundef{shorthand}%
      {\ifthenelse{\ifnameundef{labelname}\OR
          \ifentrytype{inreference}\OR
          \ifentrytype{reference}\OR
          \ifentrytype{mvreference}}% Simplified for CMS
        {\usebibmacro{cite:label}%
          \setunit{\cms@testspace}%
          \usebibmacro{cite:reinit}}%
        {\iffieldequals{fullhash}{\cbx@lasthash}%
          {\iffieldundef{postnote}%
            {\setunit{\multicitedelim}}%
            {}%
            \usebibmacro{cite:label}}%
          {\iffieldequals{fullhash}{\cbx@lastyear}%
            {}%
            {\usebibmacro{cmsbracketname}% For names in []
              \ifentrytype{customc}%
              {\newcunit}%
              {\setunit{\cms@testspace}}}%
            \usebibmacro{cite:label}%
            \iffieldundef{postnote}%
            {\savefield{fullhash}{\cbx@lasthash}}%
            {\savefield{fullhash}{\cbx@lastyear}}}}}%
      {\usebibmacro{cite:shorthand+title}}}%
    {\iffieldundef{shorthand}%
      {\ifthenelse{\ifnameundef{labelname}\OR
          \ifentrytype{inreference}\OR
          \ifentrytype{reference}\OR
          \ifentrytype{mvreference}}% Simplified for CMS
        {\usebibmacro{cite:label}%
          \setunit{\nameyeardelim}%
          \usebibmacro{cmscitesortdate}%
          \usebibmacro{cite:reinit}}%
        {\iffieldequals{fullhash}{\cbx@lasthash}%
          {\iffieldundef{postnote}%
            {\setunit{\compcitedelim}}%
            {}%
            \usebibmacro{cmscitesortdate}}%
          {\iffieldequals{fullhash}{\cbx@lastyear}% Is this right?
            {}%
            {\usebibmacro{cmsbracketname}%
              \setunit{\nameyeardelim}}%
            \usebibmacro{cmscitesortdate}%
            \iffieldundef{postnote}%
            {\savefield{fullhash}{\cbx@lasthash}}%
            {\savefield{fullhash}{\cbx@lastyear}}}}}%
      {\usebibmacro{cite:shorthand}}}}%
  \setunit{\multicitedelim}}% ???

\newbibmacro*{cmsbracketname}{%
  \iffieldequalstr{authortype}{anon}%
  {\printtext{\bibleftbracket\printnames{labelname}%
      \bibrightbracket}}%
  {\iffieldequalstr{authortype}{anon?}%
    {\printtext{\bibleftbracket\printnames{labelname}?%
        \bibrightbracket}}%
    {\printnames{labelname}}}}%

\newbibmacro*{citeyear}{%
  \iffieldequalstr{entrysubtype}{classical}%
  {\iffieldundef{shorthand}%
    {\usebibmacro{citeyear:noshort}}%
    {\iftoggle{cms@los}%
      {\usebibmacro{citeyear:noshort}}%
      {\usebibmacro{cite:shorthand+title}}}}%
  {\iffieldundef{shorthand}%
    {\usebibmacro{citeyear:noshort}}%
    {\iftoggle{cms@los}%
      {\usebibmacro{citeyear:noshort}}%
      {\usebibmacro{cite:shorthand}}}}%
  \setunit{\multicitedelim}}%

\newbibmacro*{citeyear:noshort}{%
  \iffieldequalstr{entrysubtype}{classical}% Altered for CMS
  {\iffieldequals{fullhash}{\cbx@lasthash}%
    {\iffieldundef{postnote}%
      {\setunit{\compcitedelim}}%
      {}%
      \usebibmacro{cite:label}}%
    {\usebibmacro{cite:label}%
      \iffieldundef{postnote}%
      {\savefield{fullhash}{\cbx@lasthash}}%
      {}}}%
  {\iffieldequals{fullhash}{\cbx@lasthash}%
    {\iffieldundef{postnote}%
      {\setunit{\compcitedelim}}%
      {}%
      \usebibmacro{cmscitesortdate}}%
    {\usebibmacro{cmscitesortdate}%
      \iffieldundef{postnote}%
      {\savefield{fullhash}{\cbx@lasthash}}%
      {}}}}%

\newbibmacro*{textcite}{%
  \iftoggle{cms@los}%
  {\usebibmacro{textcite:authshort}}%
  {\usebibmacro{textcite:citeshort}}}%

\newbibmacro*{textcite:authshort}{%
  \iffieldequals{fullhash}{\cbx@lasthash}%
  {\iffieldundef{postnote}%
    {\iffieldequals{fullhash}{\cbx@lastyear}%
      {\setunit{\multicitedelim}%
        \global\undef\cbx@lastyear}%
      {\iffieldequalstr{entrysubtype}{classical}%
        {\setunit{\multicitedelim}}%
        {\setunit{\compcitedelim}}}}%
    {\setunit{\multicitedelim}}%
    \iffieldequalstr{entrysubtype}{classical}%
    {\usebibmacro{cite:label}}%
    {\usebibmacro{cmscitesortdate}}}%
  {\iffieldundef{shorthand}%
    {\ifthenelse{\ifnameundef{labelname}\OR
        \ifentrytype{inreference}\OR
        \ifentrytype{reference}\OR
        \ifentrytype{mvreference}}%
      {\iffieldequalstr{entrysubtype}{classical}%
        {\setunit{%
            \global\booltrue{cbx:parens}%
            \cms@testspace\bibopenparen}%
          \ifnumequal{\value{citecount}}{1}%
          {\usebibmacro{prenote}}%
          {}%
          \usebibmacro{cite:label}}%
        {\usebibmacro{cite:label}%
          \ifboolexpr{%
            togl {cms@gencite}%
            and
            (
            test {\iffinalcitedelim}%
            or
            togl {cms@genallnames}%
            )
          }%
          {\thegen}%
          {}%
          \setunit{%
            \global\booltrue{cbx:parens}%
            \cms@testspace\bibopenparen}%
          \ifnumequal{\value{citecount}}{1}%
          {\usebibmacro{prenote}}%
          {}%
          \usebibmacro{cmscitesortdate}}}%
      {\printnames{labelname}%
        \ifboolexpr{%
          togl {cms@gencite}%
          and
          (
          test {\iffinalcitedelim}%
          or
          togl {cms@genallnames}%
          )
        }%
        {\thegen}%
        {}%
        \setunit{%
          \global\booltrue{cbx:parens}%
          \cms@testspace\bibopenparen}%
        \ifnumequal{\value{citecount}}{1}%
        {\usebibmacro{prenote}}%
        {}%
        \iffieldequalstr{entrysubtype}{classical}%
        {\usebibmacro{cite:label}}%
        {\usebibmacro{cmscitesortdate}}%
        \savefield{fullhash}{\cbx@lasthash}}}%
    {\printfield{shorthand}%
      \ifboolexpr{%
        togl {cms@gencite}%
        and
        (
        test {\iffinalcitedelim}%
        or
        togl {cms@genallnames}%
        )
      }%
      {\thegen}%
      {}%
      \setunit{%
        \global\booltrue{cbx:parens}%
        \cms@testspace\bibopenparen}%
      \ifnumequal{\value{citecount}}{1}%
      {\usebibmacro{prenote}}%
      {}%
      \iffieldequalstr{entrysubtype}{classical}%
      {\usebibmacro{cite:label}}%
      {\usebibmacro{cmscitesortdate}}%
      \savefield{fullhash}{\cbx@lasthash}}%
    \stepcounter{textcitecount}}% Added ???
  \setunit{%
    \ifbool{cbx:parens}%
    {\bibcloseparen\global\boolfalse{cbx:parens}}%
    {}%
    \textcitedelim}}% Not \multicitedelim ???

\newbibmacro*{textcite:citeshort}{%
  \iffieldequals{fullhash}{\cbx@lasthash}%
  {\iffieldundef{shorthand}%
    {\iffieldundef{postnote}%
      {\iffieldequals{fullhash}{\cbx@lastyear}%
        {\setunit{\multicitedelim}%
          \global\undef\cbx@lastyear}%
        {\iffieldequalstr{entrysubtype}{classical}%
          {\setunit{\multicitedelim}}%
          {\setunit{\compcitedelim}}}}%
      {\setunit{\multicitedelim}}%
      \iffieldequalstr{entrysubtype}{classical}%
      {\usebibmacro{cite:label}}%
      {\usebibmacro{cmscitesortdate}}}%
    {\iffieldundef{postnote}%
      {\iffieldequals{fullhash}{\cbx@lastyear}%
        {\setunit{\multicitedelim}%
          \global\undef\cbx@lastyear}%
        {\setunit{\compcitedelim}}}%
      {\setunit{\multicitedelim}}%
      \printtext[bibhyperref]{%
        \printfield{shorthand}}}}%
  {\ifthenelse{\ifnameundef{labelname}\OR
      \ifentrytype{inreference}\OR
      \ifentrytype{reference}\OR
      \ifentrytype{mvreference}}%
    {\iffieldundef{shorthand}%
      {\iffieldequalstr{entrysubtype}{classical}%
        {\setunit{%
            \global\booltrue{cbx:parens}%
            \cms@testspace\bibopenparen}%
          \ifnumequal{\value{citecount}}{1}%
          {\usebibmacro{prenote}}%
          {}%
          \usebibmacro{cite:label}}%
        {\usebibmacro{cite:label}%
          \ifboolexpr{%
            togl {cms@gencite}%
            and
            (
            test {\iffinalcitedelim}%
            or
            togl {cms@genallnames}%
            )
          }%
          {\thegen}%
          {}%
          \setunit{%
            \global\booltrue{cbx:parens}%
            \cms@testspace\bibopenparen}%
          \ifnumequal{\value{citecount}}{1}%
          {\usebibmacro{prenote}}%
          {}%
          \usebibmacro{cmscitesortdate}}}%
      {\printtext[bibhyperref]{%
          \printfield{shorthand}}}}%
    {\printnames{labelname}%
      \ifboolexpr{%
        togl {cms@gencite}%
        and
        (
        test {\iffinalcitedelim}%
        or
        togl {cms@genallnames}%
        )
      }%
      {\thegen}%
      {}%
      \setunit{%
        \global\booltrue{cbx:parens}%
        \cms@testspace\bibopenparen}%
      \ifnumequal{\value{citecount}}{1}%
      {\usebibmacro{prenote}}%
      {}%
      \iffieldundef{shorthand}%
      {\iffieldequalstr{entrysubtype}{classical}%
        {\usebibmacro{cite:label}}%
        {\usebibmacro{cmscitesortdate}}}%
      {\printtext[bibhyperref]{%
          \printfield{shorthand}}}%
      \savefield{fullhash}{\cbx@lasthash}}%
    \stepcounter{textcitecount}}% Added ???
  \setunit{%
    \ifbool{cbx:parens}%
    {\bibcloseparen\global\boolfalse{cbx:parens}}%
    {}%
    \textcitedelim}}% Not \multicitedelim ???

\newbibmacro*{textcite:postnote}{%
  \iffieldundef{postnote}%
  {}%
  {\savefield{fullhash}{\cbx@lastyear}%
    \setunit{\postnotewrapper}%delim}%
    \printfield{postnote}}%
  \ifthenelse{\value{multicitecount}=\value{multicitetotal}}%
  {\setunit{}%
    \printtext{%
      \ifbool{cbx:parens}%
      {\bibcloseparen\global\boolfalse{cbx:parens}}%
      {}}}%
  {\setunit{%
      \ifbool{cbx:parens}%
      {\bibcloseparen\global\boolfalse{cbx:parens}}%
      {}%
      \textcitedelim}}}% Not \multicitedelim ???

\newbibmacro*{cite:shorthand}{%
  \iftoggle{cms@los}%
  {\iffieldequals{fullhash}{\cbx@lasthash}%
    {\printtext[cmshyper]{%
        \setunit{\compcitedelim}%
        \usebibmacro{cmscitesortdate}}}%
    {\printfield{shorthand}%
      \printtext[cmshyper]{%
        \setunit{\nameyeardelim}%
        \usebibmacro{cmscitesortdate}}%
      \savefield{fullhash}{\cbx@lasthash}}}%
  {\printtext[bibhyperref]{\printfield{shorthand}}%
    \usebibmacro{cite:reinit}}}%

\newbibmacro*{cite:shorthand+title}{%
  \iftoggle{cms@los}%
  {\iffieldequals{fullhash}{\cbx@lasthash}%
    {\printtext[cmshyper]{%
        \setunit{\multicitedelim}%
        \usebibmacro{cite:label}}}%
    {\printfield{shorthand}%
      \printtext[cmshyper]{%
        \setunit{\addcomma\addspace}% FIXME: wrong in 16th _and_ 17th eds?
        \ifentrytype{manual}%
        {\printtext[cmshyper]{\printfield[citetitle]{labeltitle}}}%
        {\usebibmacro{cite:label}}}%
      \savefield{fullhash}{\cbx@lasthash}}}%
  {\printtext[bibhyperref]{\printfield{shorthand}}%
    \usebibmacro{cite:reinit}}}%

\newbibmacro*{cite:label}{% Test this
  \iffieldundef{label}%
  {\ifthenelse{\iffieldequalstr{entrysubtype}{magazine}\AND\NOT%
      \ifentrytype{periodical}}% Simplifies .bib creation
    {\ifboolexpr{%
        not test {\iffieldundef{shortjournal}}%
        and
        ((
        test {\ifcitation}%
        and
        togl {cms@citejtabb}%
        )
        or
        (
        test {\ifbibliography}%
        and
        togl {cms@bibjtabb}%
        ))
      }%
      {\printtext[cmshyper]{\printfield[shortjournal]{shortjournal}}}%
      {\printtext[cmshyper]{\printfield[journaltitle]{journaltitle}}}}%
    {\ifentrytype{manual}%
      {\printtext[cmshyper]{\printlist{organization}}}%
      {\printtext[cmshyper]{\printfield[citetitle]{labeltitle}}}}}%
  {\printtext[cmshyper]{\printfield{label}}}}%

\newbibmacro*{cite:labelyear+extrayear}{%
  \ifboolexpr{ (
    test {\ifentrytype{music}}%
    or
    test {\ifentrytype{review}}%
    or
    test {\ifentrytype{suppperiodical}}%
    or
    test {\ifentrytype{video}}%
    )
    and
    togl {cms@avdate}%
  }%
  {\usebibmacro{cite:av+labelyear+extrayear}}%
  {\iftoggle{cms@ordate}%
    {\usebibmacro{cite:origfirst+labelyear+extrayear}}%
    {\usebibmacro{cite:standard+labelyear+extrayear}}}}%

\newbibmacro*{cite:standard+labelyear+extrayear}{%
  \ifthenelse{\iffieldundef{labelyear}\OR%
    \iffieldequalstr{labelyear}{nodate}}%
  {\ifboolexpr{%
      test {\ifentrytype{misc}}%
      or
      test {\ifentrytype{inreference}}%
      or
      test {\ifentrytype{reference}}%
      or
      test {\ifentrytype{mvreference}}%
      or
      not togl {cms@nodates}%
    }%
    {}%
    {\printtext[cmsyearhyper]{\bibstring{nodate}}}}% For CMS?
  {\printtext[cmsyearhyper]{%
      \iffieldundef{year}%
      {\iffieldundef{eventyear}%
        {\iffieldundef{origyear}%
          {\printurldateextra}%
          {\printorigdateextra}}%
        {\printeventdateextra}}%
      {\printdateextra}}}}%

\newbibmacro*{cite:origfirst+labelyear+extrayear}{%
  \ifthenelse{\iffieldundef{labelyear}\OR%
    \iffieldequalstr{labelyear}{nodate}}%
  {\ifboolexpr{%
      test {\ifentrytype{misc}}%
      or
      test {\ifentrytype{inreference}}%
      or
      test {\ifentrytype{reference}}%
      or
      test {\ifentrytype{mvreference}}%
      or
      not togl {cms@nodates}%
    }%
    {}%
    {\printtext[cmsyearhyper]{\bibstring{nodate}}}}% For CMS?
  {\printtext[cmsyearhyper]{%
      \iffieldundef{origyear}%
      {\iffieldundef{year}%
        {\iffieldundef{eventyear}%
          {\printurldateextra}%
          {\printeventdateextra}}%
        {\printdateextra}}%
      {\printorigdateextra}}}}%

\newbibmacro*{cite:av+labelyear+extrayear}{%
  \ifthenelse{\iffieldundef{labelyear}\OR%
    \iffieldequalstr{labelyear}{nodate}}%
  {\ifboolexpr{%
      test {\ifentrytype{misc}}%
      or
      test {\ifentrytype{inreference}}%
      or
      test {\ifentrytype{reference}}%
      or
      test {\ifentrytype{mvreference}}%
      or
      not togl {cms@nodates}%
    }%
    {}%
    {\printtext[cmsyearhyper]{\bibstring{nodate}}}}% For CMS?
  {\printtext[cmsyearhyper]{%
      \iffieldundef{eventyear}%
      {\iffieldundef{origyear}%
        {\iffieldundef{year}%
          {\printurldateextra}%
          {\printdateextra}}%
        {\printorigdateextra}}%
      {\printeventdateextra}}}}%

\newbibmacro*{cmscitesortdate}{% Attempt to solve date-related problems
  \ifboolexpr{%
    test {\iffieldundef{origyear}}%
    or
    not test {\iffieldint{origyear}}%
  }%
  {\usebibmacro{cmsciteyear}}%
  {\iffieldint{year}%
    {\ifboolexpr{% Needed for date ranges
        test {\iffieldundef{endyear}}%
        or
        not test {\iffieldnum{endyear}}%
      }%
      {\ifthenelse{\thefield{origyear}>\thefield{year}}%
        {\toggletrue{cms@switchdates}%
          \usebibmacro{cmsciteyear}}%
        {\usebibmacro{cmsciteyear}}}%
      {\ifthenelse{\thefield{origyear}>\thefield{endyear}}%
        {\toggletrue{cms@switchdates}%
          \usebibmacro{cmsciteyear}}%
        {\usebibmacro{cmsciteyear}}}}%
    {\usebibmacro{cmsciteyear}}}}%

\newbibmacro*{cmsciteyear}{%
  \ifentrytype{patent}% Fix for double year w/cmsdate
  {\usebibmacro{cite:standard+labelyear+extrayear}}
  {\iftoggle{cms@origlabel}%
    {\usebibmacro{cite:origyear+labelyear}}%
    {\iftoggle{cms@bothlabelnew}%
      {\usebibmacro{cite:bothyear+oldstyle}}%
      {\iftoggle{cms@bothlabelold}%
        {\usebibmacro{cite:bothyear+oldstyle}}%
        {\iftoggle{cms@fulldate}%
          {\printtext[cmsyearhyper]{%
              \csuse{print\strfield{labeldatesource}date}%
              \iffieldundef{\strfield{labeldatesource}hour}%
              {}% Consistency(?)
              {\newcunit\csuse{print\strfield{labeldatesource}time}}}}%
          {\usebibmacro{cite:labelyear+extrayear}}}}}}}%
  % \ifcsdef{@cms@tempdate}%
  % {\toggletrue{\@cms@tempdate}}%
  % {}%

\newbibmacro*{cite:origyear+labelyear}{%
  \ifboolexpr{ (
    test {\ifentrytype{music}}%
    or
    test {\ifentrytype{review}}%
    or
    test {\ifentrytype{suppperiodical}}%
    or
    test {\ifentrytype{video}}%
    )
    and
    togl {cms@avdate}%
  }%
  {\usebibmacro{cite:av+labelyear+extrayear}}%
  {\iftoggle{cms@switchdates}%
    {\usebibmacro{cite:labelyear+extrayear}}%
    {\iffieldundef{origyear}%
      {\iftoggle{cms@ordate}% ???
        {}%
        {\clearfield{extradate}}%
        \usebibmacro{cite:standard+labelyear+extrayear}}%
      {\iftoggle{cms@ordate}%
        {\usebibmacro{cite:origfirst+labelyear+extrayear}}%
        {\clearfield{extradate}%
          \printtext[cmsyearhyper]{%
            \printorigdateextra}}}}}}% \usebibmacro{origyear+endyear}

\newbibmacro*{cite:bothyear+oldstyle}{%
  \ifboolexpr{ (
    test {\ifentrytype{music}}%
    or
    test {\ifentrytype{review}}%
    or
    test {\ifentrytype{suppperiodical}}%
    or
    test {\ifentrytype{video}}%
    )
    and
    togl {cms@avdate}%
  }%
  {\usebibmacro{cite:av+labelyear+extrayear}}%
  {\iftoggle{cms@switchdates}%
    {\printtext[cmsyearhyper]{%
        \bibopenparen%
        \usebibmacro{cite:labelyear+extrayear}%
        \bibcloseparen%
        \clearfield{extradate}%
        \addspace%
        \printorigdateextra}}% \usebibmacro{origyear+endyear}
    {\iffieldundef{origyear}% ???
      {\iftoggle{cms@ordate}%
        {}%
        {\clearfield{extradate}}%
        \usebibmacro{cite:standard+labelyear+extrayear}}%
      {\iftoggle{cms@ordate}% Added test for year field ???
        {\iffieldundef{year}%
          {\usebibmacro{cite:origfirst+labelyear+extrayear}}%
          {\printtext[cmsyearhyper]{%
              \bibopenparen%
              \usebibmacro{cite:origfirst+labelyear+extrayear}%
              \bibcloseparen%
              \setunit{\addspace}%\addspace% ???
              \clearfield{extradate}%
              \printdateextra}}}%\usebibmacro{year+endyear}
        {\iffieldundef{year}%
          {\usebibmacro{cite:origfirst+labelyear+extrayear}}%
          {\printtext[cmsyearhyper]{%
              \bibopenparen%
              \clearfield{extradate}%
              \printorigdateextra%
              % \usebibmacro{origyear+endyear}%
              \bibcloseparen%
              \addspace%
              \usebibmacro{cite:standard+labelyear+extrayear}}}}}}}}%

\newbibmacro*{cite:save}{%
  \savefield{entrykey}{\cbx@lastkey}}%

\newbibmacro*{cite:ibid}{%
  \iftoggle{cms@noibid}%
  {\blx@ibidreset%
    \usebibmacro{cite}}%
  {\ifthenelse{\iffieldundef{prenote}\AND%
      \iffieldundef{postnote}}%
    {\blx@ibidreset%
      \usebibmacro{cite}%
      \PackageWarning{biblatex-chicago}%
      {Empty Ibidem citation}}%
    {\toggletrue{cms@inlineibid}}}}%

%%%% Citation Commands, internal and external %%%%

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

\DeclareCiteCommand*{\cite}
  {\usebibmacro{cite:init}%
    \usebibmacro{prenote}}
  {\usebibmacro{citeindex}%
   \usebibmacro{citeyear}}
  {}%\multicitedelim
  {\usebibmacro{postnote}}

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

\DeclareCiteCommand*{\parencite}[\mkbibparens]
  {\usebibmacro{cite:init}%
    \usebibmacro{prenote}}
  {\usebibmacro{citeindex}%
   \usebibmacro{citeyear}}
  {}%\setunit{\multicitedelim}
  {\usebibmacro{postnote}}

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

\DeclareCiteCommand{\footcitetext}[\mkbibfootnotetext]
  {\usebibmacro{cite:init}%
   \usebibmacro{prenote}}
  {\usebibmacro{citeindex}%
   \usebibmacro{cite}}
  {}%
  {\usebibmacro{postnote}}

\def\cms@cicpunct{}%

%%% Commands, macros and formats for the Legal entry types %%%
  
\DeclareCiteCommand{\footfullcite}[\mkbibfootnote]
  {\usebibmacro{prenote}}%\bibsentence
  {\usebibmacro{citeindex}%
    \toggletrue{cms@fullnote}%
    \togglefalse{cms@shortnote}%
    \usebibmacro{cite:full}%
    \usebibmacro{footcite:save}%
    \usebibmacro{cite:save}}
  {\multicitedelim}
  {}

\DeclareCiteCommand{\fullcite}
  {\usebibmacro{prenote}}%\bibsentence
  {\usebibmacro{citeindex}%
    \toggletrue{cms@fullnote}%
    \togglefalse{cms@shortnote}%
    \usebibmacro{cite:full}%
    \usebibmacro{footcite:save}%
    \usebibmacro{cite:save}}
  {\multicitedelim}
  {}

\DeclareCiteCommand{\parenfullcite}[\mkbibparens]
  {\usebibmacro{prenote}}%\bibsentence
  {\usebibmacro{citeindex}%
    \toggletrue{cms@fullnote}%
    \togglefalse{cms@shortnote}%
    \usebibmacro{cite:full}%
    \usebibmacro{footcite:save}%
    \usebibmacro{cite:save}}
  {\multicitedelim}
  {}

\newbibmacro*{cite:full}{%
  \iftoggle{cms@noneshort}%
  {\ifboolexpr{%
      test {\ifciteibid}%
      and
      not test {\iffirstonpage}%
      and
      not togl {cms@noibid}%
    }%
    {\togglefalse{cms@fullnote}%
      \toggletrue{cms@shortnote}%
      \usebibmacro{legal:ibid}}%
    {\usebibmacro{cite:driver}}}%
  {\ifboolexpr{%
      test {\ifciteseen}%
      or
      togl {cms@allshort}%
    }%
    {\togglefalse{cms@fullnote}%
      \toggletrue{cms@shortnote}%
      \ifboolexpr{%
        test {\ifciteibid}%
        and
        not test {\iffirstonpage}%
        and
        not togl {cms@noibid}%
      }%
      {\usebibmacro{legal:ibid}}%
      {\usebibmacro{cite:short}}}%
    {\usebibmacro{cite:driver}}}}

\newbibmacro*{cite:driver}{%
  \printtext[bibhypertarget]{%
    \usedriver
      {\DeclareNameAlias{sortname}{default}\frenchspacing}%
      {\thefield{entrytype}}}}%

\newbibmacro*{cite:short}{%
  \iffieldundef{shorthand}%
  {\ifentrytype{jurisdiction}%
    {\usebibmacro{cite:short:jurisdiction}}%
    {\ifentrytype{legal}%
      {\usebibmacro{cite:short:legal}}%
      {\ifentrytype{legislation}%
        {\usebibmacro{cite:short:legislation}}%
        {\usebibmacro{cite}}}}}
  {\usebibmacro{cite:shorthand:legal}}}%

\newbibmacro*{cite:short:jurisdiction}{%
  \iffieldundef{labeltitle}%
  {}%
  {\ifboolexpr{%
    togl {cms@allshort}%
    or
    test {\ifbibliography}%
  }%
  {\printtext[bibhyperref]{%
      \printfield[citetitle]{labeltitle}}}%
  {\printtext[cmshyperlink]{%
      \printfield[citetitle]{labeltitle}}}}%
  \newcunit% Fixme: giving us a duplicate comma
  \printfield[jourvol]{volume}%
  \setunit*{\addnbspace}%
  \iffieldundef{shortjournal}% Test eliminates spurious comma
  {}%
  {\printtext[shortjournal]{%
      \printfield[jtsnoformat]{shortjournal}}}%
  \iffieldundef{issue}%
  {\printfield[jurisdictionser]{series}}%
  {\setunit{\addspace}%
    \printfield{issue}}% Better ideas?
  \usebibmacro{juridpostnote}}%

\newbibmacro*{cite:short:legal}{%
  \iffieldundef{labeltitle}%
  {}%
  {\ifboolexpr{%
    togl {cms@allshort}%
    or
    test {\ifbibliography}%
  }%
  {\printtext[bibhyperref]{%
      \printfield[citetitle]{labeltitle}}}%
  {\printtext[cmshyperlink]{%
      \printfield[citetitle]{labeltitle}}}}%
  \newcunit% Fixme: giving us a duplicate comma
  \printfield[jourvol]{volume}%
  \setunit*{\addnbspace}%
  \iffieldundef{shortjournal}% Test eliminates spurious comma
  {}%
  {\printtext[shortjournal]{%
      \printfield[jtsnoformat]{shortjournal}}}%
  \printfield[legalser]{series}%
  \setunit{\addspace}%
  \printfield{issue}%
  \ifboolexpr{%
    togl {cms@supranotes}%
    and
    test {\iffootnote}%
  }%
  {\newcunit%
    \printtext{\bibstring{supranote}\addnbspace%
      \ref{cbx@\csuse{cbx@f@\thefield{entrykey}}}}}% ??
  {}%
  \usebibmacro{juridpostnote}}%

\newbibmacro*{cite:short:legislation}{%
  \iffieldequalstr{entrysubtype}{constitution}%
  {\usebibmacro{clegis+news+title}%
    \setunit{\addspace}%
    \printfield[juridnum]{number}%
    \newcunit%
    \printfield{part}%
    \newcunit%
    \printfield{chapter}}%
  {\iffieldequalstr{labeltitlesource}{shorttitle}%
    {\ifboolexpr{%
        togl {cms@allshort}%
        or
        test {\ifbibliography}%
      }%
      {\printtext[bibhyperref]{%
          \printfield[citetitle]{labeltitle}}}%
      {\printtext[cmshyperlink]{%
          \printfield[citetitle]{labeltitle}}}}%
    {\ifboolexpr{%
        test {\iffieldundef{shortjournal}}%
        or
        (
        not test {\iffieldundef{volume}}%
        and
        not test {\iffieldint{volume}}%
        )
      }%
      {\ifboolexpr{%
          test {\iffieldundef{entrysubtype}}%
          and
          not test {\iflistundef{location}}
        }%
        {\printlist{location}%
          \setunit{\addspace}}%
        {}%
        \ifboolexpr{%
          togl {cms@allshort}%
          or
          test {\ifbibliography}%
        }%
        {\printtext[bibhyperref]{%
            \printfield[citetitle]{labeltitle}}}%
        {\printtext[cmshyperlink]{%
            \printfield[citetitle]{labeltitle}}}%
        \setunit{\addspace}%
        \printfield[juridnum]{number}%
        \newcunit%
        \printfield{part}%
        \newcunit%
        \printfield{chapter}}%
      {\printfield{note}%
        \newcunit%
        \printfield[jourvol]{volume}%
        \setunit*{\addnbspace}%
        \iffieldundef{shortjournal}% Test eliminates spurious comma
        {}%
        {\printtext[shortjournal]{%
            \printfield[jtsnoformat]{shortjournal}}}%
        \printfield[legislationser]{series}%
        \setunit{\addspace}%
        \printfield{issue}%
        \newcunit%
        \printfield{part}%
        \newcunit%
        \printfield{chapter}%
      }}}%
  \newcunit%
  \ifboolexpr{%
    togl {cms@supranotes}%
    and
    test {\iffootnote}%
    and
    (
    test {\iffieldequalstr{entrysubtype}{un}}%
    or
    test {\iffieldequalstr{entrysubtype}{hearing}}%
    )
  }%
  {\newcunit%
    \printtext{\bibstring{supranote}\addnbspace%
      \ref{cbx@\csuse{cbx@f@\thefield{entrykey}}}}}% ??
  {}%
  \usebibmacro{juridpostnote}}%

\newbibmacro*{cite:shorthand:legal}{%
  \iftoggle{cms@allshort}%
  {\printtext[bibhyperref]{%
      \printfield{shorthand}}}%
  {\printtext[cmshyperlink]{%
      \printfield{shorthand}}}%
  \ifboolexpr{%
    test {\iffootnote}%
    and
    togl {cms@supranotes}%
    and
    (
    test {\ifentrytype{legal}}%
    or
    (
    test {\ifentrytype{legislation}}%
    and
    (
    test {\iffieldequalstr{entrysubtype}{un}}%
    or
    test {\iffieldequalstr{entrysubtype}{hearing}}%
    )))
  }%
  {\newcunit%
    \printtext{\bibstring{supranote}\addnbspace%
      \ref{cbx@\csuse{cbx@f@\thefield{entrykey}}}%
      \newcunit}}% ??
  {\newcunit}% ??
  \iffieldundef{postnote}%
  {\global\togglefalse{cms@fullnote}%
    \global\togglefalse{cms@shortnote}}%
  {\usebibmacro{semel:postnote}%
    \global\togglefalse{cms@fullnote}%
    \global\togglefalse{cms@shortnote}}}%

\newbibmacro*{cms:shorthandintro}{% For changing the citedas phrase
  \iffieldundef{shorthand}%
  {}%
  {\iffieldundef{shorthandintro}%
    {\ifthenelse{\ifentrytype{jurisdiction}\OR\ifentrytype{legal}\OR%
        \ifentrytype{legislation}}%
      {\printtext[brackets]{%
        \bibstring{hereinafter}\addspace%
        \printfield{shorthand}}}%
      {\printtext[parens]{%
        \bibstring{citedas}\addspace%
        \printfield{shorthand}}}}%
    {%\addspace%\setunit{\addspace}% Ditto
      \printfield{shorthandintro}}}}

\newbibmacro*{footcite:save}{%
  \ifboolexpr{%
    test {\iffootnote}%
    and
    (
    test {\ifentrytype{legal}}%
    or
    (
    test {\ifentrytype{legislation}}%
    and
    (
    test {\iffieldequalstr{entrysubtype}{un}}%
    or
    test {\iffieldequalstr{entrysubtype}{hearing}}%
    )))
  }%
  {\csxdef{cbx@f@\thefield{entrykey}}{\the\value{instcount}}%
    \label{cbx@\the\value{instcount}}}%
  {}}%

\newbibmacro*{legal:ibid}{%
  \ifboolexpr{%
    togl {cms@noibid}%
    or
    test {\ifbibliography}% Needed for inheritshorthand option
  }%
  {\global\toggletrue{cms@shortnote}%
    \global\togglefalse{cms@fullnote}%
    \usebibmacro{cite:short}%
    \usebibmacro{cite:save}}%
  {\printtext[bibhyperlink]{%
      \bibsstring[\mkbibemph]{ibidem}}%\bibstring[\mkibid]{ibidem}
    \ifboolexpr{%
      test {\ifloccit}%
      or
      (
      test {\ifciteibid}%
      and
      test {\iffieldequalcs{postnote}{cms@pnsaved}}%
      and
      not test {\ifdefvoid{\blx@loccittracker}}% Package option=false
      )
    }%
    {\global\toggletrue{cms@loccit}}%
    {}}\usebibmacro{postnote}}% FIXME?

\newbibmacro*{clegis+news+title}{%
  \ifthenelse{\iffieldundef{title}\AND\iffieldundef{subtitle}\AND%
    \iffieldundef{titleaddon}}%
  {}%
  {\printtext[title]{%
      \printfield[noformat]{title}%
      \setunit{\subtitlepunct}%
      \printfield[noformat]{subtitle}}%
    \setunit{\ctitleaddonpunct}%
    \printfield{titleaddon}%
  }}%\newcunit\newblock}

\newbibmacro*{cpart+editor+translator}{%
  \ifnameundef{namea}%
  {\ifnameundef{nameb}%
    {}%
    {\bibstring{cbytranslator}\addspace%
      \printnames[bytranslator]{nameb}}}%
  {\ifthenelse{\iffieldundef{nameatype}\OR%
      \iffieldequalstr{nameatype}{editor}}%
    {\ifnamesequal{namea}{nameb}%
      {\bibstring{cbyeditortr}\addspace%
        \printnames[byeditor]{namea}}%
      {\bibstring{cbyeditor}\addspace% Need this \space here?
        \printnames[byeditor]{namea}%
        \ifnameundef{nameb}%
        {}%
        {\newcunit%
          \bibstring{cbytranslator}\addspace%
          \printnames[bytranslator]{nameb}}}}%
    {\usebibmacro{cbytypestrg}{namea}{editor}%
      \setunit{\addspace}%
      \printnames[byeditor]{namea}%
      \ifnameundef{nameb}%
      {}%
      {\newunit%
        \bibstring{cbytranslator}\addspace%
        \printnames[bytranslator]{nameb}}}}}

\newbibmacro*{juridpostnote}{%
  \iftoggle{cms@fullnote}%
  {\iffieldundef{issue}%
    {\iffieldundef{shortjournal}%
      {\setunit{\addcomma\addspace}}%
      {\ifthenelse{\iffieldundef{part}\AND\iffieldundef{chapter}}%
        {\setunit{\addspace}}%
        {\ifpunctmark{*}{\setunit{\addspace}}{\newcunit}}}%
      \printfield{pages}%
      \iffieldundef{postnote}%
      {}%
      {\ifthenelse{\ifentrytype{legislation}\OR\ifentrytype{legal}}%
        {\newcunit}%
        {\setunit*{\addcomma\addspace}}%
        \usebibmacro{semel:postnote}}}%
    {\iffieldundef{postnote}%
      {\iffieldundef{pages}%
        {}%
        {\newcunit%
          \printfield{pages}}}%
      {\newcunit%
        \usebibmacro{semel:postnote}}}}%
  {\iffieldundef{issue}%
    {\iffieldundef{postnote}%
      {\iffieldundef{pages}%
        {}%
        {\iffieldundef{shortjournal}%
          {\setunit{\addcomma\addspace}}%
          {\ifthenelse{\iffieldundef{part}\AND\iffieldundef{chapter}}%
            {\setunit{\addspace}}%
            {\ifpunctmark{*}{\setunit{\addspace}}{\newcunit}}}%
          \printfield{pages}}}%
      {\iffieldundef{shortjournal}%
        {\setunit{\addcomma\addspace}}%
        {\ifthenelse{\iffieldundef{part}\AND\iffieldundef{chapter}}%
          {\setunit{\addspace}}%
          {\ifpunctmark{*}{\setunit{\addspace}}{\newcunit}}}%
        \usebibmacro{semel:postnote}}}% See below for alternate test here
    {\iffieldundef{postnote}%
      {\iffieldundef{pages}%
        {}%
        {\newcunit%
          \printfield{pages}}}%
      {\newcunit%
        \usebibmacro{semel:postnote}}}%
    \global\togglefalse{cms@fullnote}%
    \global\togglefalse{cms@shortnote}}}%

\newbibmacro*{semel:postnote}{% Fix to print postnote only once
  \printfield{postnote}% Old form broke \ifloccit
  \global\let\cms@pnsaved\abx@field@postnote%
  \global\let\abx@field@postnote\undefined%
  \AtNextCitekey{\ifciteibid{}{\global\let\cms@pnsaved\undefined}}}%

\newbibmacro*{cite+doi+url}{% 16th ed.
  \ifboolexpr{%
    togl {cms@url}%
    and
    not test {\iffieldundef{urlyear}}%
    }%
  {\printurldate}% Date fix
  {}%
  \newcunit\newblock
  \iftoggle{cms@doionly}%
  {\iffieldundef{doi}%
    {}%
    {\printfield{doi}%
      \clearfield{url}}}%
  {\ifboolexpr{%
      togl {cms@doi}%
      and
      not test {\iffieldundef{doi}}%
    }%
    {\printfield{doi}}%
    {}}%
  \newcunit\newblock
  \ifboolexpr{%
    togl {cms@eprint}%
    and
    not test {\iffieldundef{eprint}}%
  }%
  {\usebibmacro{eprint}}%
  {}%
  \newcunit\newblock
  \ifboolexpr{%
    togl {cms@url}%
    and
    not test {\iffieldundef{url}}%
  }%
  {\printfield{url}}%
  {}}%

\DeclareListFormat[jurisdiction]{location}{%
  \iffieldundef{entrysubtype}%
  {\usebibmacro{list:delim}{#1}%
    #1\isdot%
    \usebibmacro{list:andothers}}%
  {\ifthenelse{\value{listcount}<\value{listtotal}}%
    {\mkbibparens{#1}\addspace}%
    {\mkbibparens{#1\isdot}}}}%

\DeclareListFormat[jurisdiction]{origlocation}{%
  \ifthenelse{\value{listcount}<\value{listtotal}}%
  {#1\isdot\addcomma\addspace}%
  {#1\isdot}}

\DeclareListFormat[jurisdiction]{origpublisher}{%
  \ifthenelse{\value{listcount}<\value{listtotal}}%
  {\mkbibparens{#1}\addspace}%
  {\mkbibparens{#1\isdot}}}

\newrobustcmd*{\mkjuridprefix}[1]{%
  \ifboolexpr{% FIXME: Is this always right?
    test {\iftoggle{cms@fullnote}}%
    and
    test {\ifentrytype{jurisdiction}}%
    }%
  {\iffieldundef{issue}%
    {#1}%
    {\printtext{at}\addspace *#1}}%
  {\iffieldundef{issue}%
    {\printtext{at}\addspace #1}% FIXME: Make it a bibstring?
    {\ifentrytype{jurisdiction}%
      {\printtext{at}\addspace *#1}%
      {\printtext{at}\addspace #1}}}}%

\DeclareFieldFormat[jurisdiction,legal,legislation]{postnote}{%
  \ifboolexpr{%
    togl {cms@comprange}%
    and
    test {\ifpages{#1}}%
  }%
  {\iffieldundef{pagination}%
    {\mkcomprange[\mkjuridprefix]{#1}}%
    {\mkcomprange[{\mkpageprefix[pagination]}]{#1}}}%
  {\iffieldundef{pagination}%
    {\mkjuridprefix{#1}}%
    {\mkpageprefix[pagination]{#1}}}}%

\DeclareFieldFormat[jurisdiction,legal]{title}{#1\isdot}

\DeclareFieldFormat[legislation]{title}{%
  \iffieldequalstr{entrysubtype}{hearing}%
  {\mkbibemph{#1}\isdot}%
  {#1\isdot}}

\DeclareFieldFormat[jurisdiction]{citetitle}{\mkbibemph{#1}\isdot}

\DeclareFieldFormat[jurisdiction,legal]{lostitle}{#1\isdot}

\DeclareFieldFormat[legislation]{lostitle}{%
  \iffieldequalstr{entrysubtype}{hearing}%
  {\mkbibemph{#1}\isdot}%
  {#1\isdot}}

\DeclareFieldFormat[legal]{citetitle}{#1\isdot}

\DeclareFieldFormat[legislation]{citetitle}{%
  \iffieldequalstr{entrysubtype}{hearing}%
  {\mkbibemph{#1}\isdot}%
  {#1\isdot}}

\DeclareFieldFormat[jurisdiction,legal,legislation]{journaltitle}{%
  \iffieldundef{shortjournal}%
  {#1\isdot}%
  {\mkbibemph{#1}\isdot}}

\DeclareFieldFormat[jurisdiction,legal,legislation]{shortjournal}{#1\isdot}

\DeclareFieldFormat{jurisdictionser}{%
  \ifinteger{#1}%
  {\mkjuridordinal{#1}}%
  {\ifbibstring{#1}{\bibstring{#1}}{#1}}}%

\DeclareFieldFormat{legislationser}{%
  \iffieldequalstr{entrysubtype}{hansard}%
  {\ifinteger{#1}%
    {\addspace\mkbibparens{\mkjuridordinal{#1}\addspace\bibsstring{jourser}}}%
    {\ifbibstring{#1}%
      {\addspace\mkbibparens{\bibstring{#1}}}%
      {\addspace\mkbibparens{#1}}}}%
  {\iffieldequalstr{entrysubtype}{uk}%
    {\addspace #1\isdot}%
    {\addcomma\addspace #1\isdot}}}%

\DeclareFieldFormat{legalser}{\addspace #1\isdot}%

\DeclareFieldFormat{hansardser}{%
  \ifinteger{#1}%
  {\mkbibparens{\mkjuridordinal{#1}\addspace\bibsstring{jourser}}}%
  {\ifbibstring{#1}{\mkbibparens{\bibstring{#1}}}{\mkbibparens{#1}}}}%

\DeclareFieldFormat{juridnum}{\bibcpstring{number}\addspace #1}%

\DeclareFieldFormat{jourvol}{#1}

\DeclareFieldFormat[jurisdiction,legal,legislation]{addendum}{%
  \ifcapital{\mkbibparens{\MakeCapital{#1\isdot}}}{\mkbibparens{#1\isdot}}}

\DeclareFieldFormat[legal,legislation]{part}{%
  \ifnumerals{#1}%
  {\bibsstring{part}~#1}%
  {#1\isdot}}%

\DeclareFieldFormat[legal,legislation]{chapter}{%
  \ifthenelse{\iffieldequalstr{entrysubtype}{uk}\OR%
    \iffieldequalstr{entrysubtype}{hansard}}%
  {c\adddotspace #1}%
  {\bibsstring{chapter}~#1}}%

%%% Textcite commands taken verbatim from authoryear-comp.cbx %%% 

\DeclareCiteCommand{\cbx@textcite}
  {\usebibmacro{cite:init}}
  {\usebibmacro{citeindex}%
   \usebibmacro{textcite}}
  {}%
  {\usebibmacro{textcite:postnote}}

\DeclareCiteCommand{\textcite}[\cbx@textcite@init\cbx@textcite]
  {\gdef\cbx@savedkeys{}%
   \citetrackerfalse%
   \pagetrackerfalse%
   \DeferNextCitekeyHook%
   \usebibmacro{cite:init}}
  {\ifthenelse{\iffirstcitekey\AND\value{multicitetotal}>0}%
     {\protected@xappto\cbx@savedcites{()(\thefield{multipostnote})}%
      \global\clearfield{multipostnote}}%
     {}%
   \xappto\cbx@savedkeys{\thefield{entrykey},}%
   \iffieldequals{fullhash}{\cbx@lasthash}%
     {}%
     {\stepcounter{textcitetotal}%
      \savefield{fullhash}{\cbx@lasthash}}}
  {}%
  {\protected@xappto\cbx@savedcites{%
     [\thefield{prenote}][\thefield{postnote}]{\cbx@savedkeys}}}

\newrobustcmd{\cbx@textcite@init}[2]{%
  \setcounter{textcitetotal}{0}%
  \setcounter{textcitecount}{0}%
  \def\cbx@savedcites{#1}#2\cbx@savedcites\empty}

\DeclareMultiCiteCommand{\cbx@textcites}{\cbx@textcite}{}
\DeclareMultiCiteCommand{\textcites}[\cbx@textcites@init\cbx@textcites]{\textcite}{}

\let\cbx@textcites@init\cbx@textcite@init
\pretocmd{\cbx@textcites@init}{\UseNextMultiCiteHook}{}{}

%%% The \gentextcite commands - \textcite in the genitive case %%%

\DeclareCiteCommand{\cms@gentextcite@i}
  {\usebibmacro{cite:init}%
    \toggletrue{cms@gencite}}
  {\usebibmacro{citeindex}%
   \usebibmacro{textcite}}
  {}%
  {\usebibmacro{textcite:postnote}%
    \togglefalse{cms@gencite}}%

\DeclareCiteCommand{\cms@gentextcite}[\cbx@textcite@init\cms@gentextcite@i]
  {\gdef\cbx@savedkeys{}%
   \citetrackerfalse%
   \pagetrackerfalse%
   \DeferNextCitekeyHook%
   \usebibmacro{cite:init}}
  {\ifthenelse{\iffirstcitekey\AND\value{multicitetotal}>0}%
     {\protected@xappto\cbx@savedcites{()(\thefield{multipostnote})}%
      \global\clearfield{multipostnote}}%
     {}%
   \xappto\cbx@savedkeys{\thefield{entrykey},}%
   \iffieldequals{fullhash}{\cbx@lasthash}%
     {}%
     {\stepcounter{textcitetotal}%
      \savefield{fullhash}{\cbx@lasthash}}}
  {}%
  {\protected@xappto\cbx@savedcites{%
     [\thefield{prenote}][\thefield{postnote}]{\cbx@savedkeys}}}

\DeclareMultiCiteCommand{\cms@gentextcite@is}{\cms@gentextcite@i}{}
\DeclareMultiCiteCommand{\cms@gentextcites}[\cbx@textcites@init\cms@gentextcite@is]{\cms@gentextcite}{}

\newrobustcmd*{\gentextcite}[1][]{%
  \@ifnextchar[%]
  {\gencite@i[#1]}%
  {\gencite@i[][#1]}}%

\def\gencite@i[#1][#2]{%
  \@ifnextchar[%]
  {\gencite@ii[#1][#2]}%
  {\gencite@ii[][#1][#2]}}%

\def\gencite@ii[#1][#2][#3]#4{%
  \ifblank{#1}{\def\thegen{'s}}{\def\thegen{#1}}%
  \gencite@iii[#2][#3]{#4}}%

\def\gencite@iii#1{\cms@gentextcite#1}

\newrobustcmd*{\gentextcites}[1][]{%
  \@ifnextchar(%)
  {\gencites@iv[#1]}%
  {\@ifnextchar[%]
    {\gencites@i[#1]}%
    {\gencites@i[][#1]}}}%

\def\gencites@i[#1][#2]{%
  \@ifnextchar[%]
  {\gencites@ii[#1][#2]}%
  {\gencites@ii[][#1][#2]}}%

\def\gencites@ii[#1][#2][#3]#4{%
  \ifblank{#1}{\def\thegen{'s}}{\def\thegen{#1}}%
  \gencites@iii[#2][#3]{#4}}%

\def\gencites@iii#1{\cms@gentextcites#1}%

\def\gencites@iv[#1]#2{%
  \ifblank{#1}{\def\thegen{'s}}{\def\thegen{#1}}%
  \cms@gentextcites#2}%

\newrobustcmd*{\Gentextcite}{\bibsentence\gentextcite}
\newrobustcmd*{\Gentextcites}{\bibsentence\gentextcites}

%%% End code for \gentextcite %%%

\DeclareMultiCiteCommand{\cites}{\cite}{\setunit{\multicitedelim}}

\DeclareMultiCiteCommand{\parencites}[\mkbibparens]{\parencite}%
   {\setunit{\multicitedelim}}

\DeclareMultiCiteCommand{\footcites}[\mkbibfootnote]{\footcite}%
   {\setunit{\multicitedelim}}

\DeclareMultiCiteCommand{\footcitetexts}[\mkbibfootnotetext]%
   {\footcitetext}{\setunit{\multicitedelim}}

\DeclareCiteCommand{\bibxrefcite}
  {\usebibmacro{cite:init}%
    \usebibmacro{backref+check}}%\usebibmacro{clearalmostall}} (?)
  {\usebibmacro{xref-in:}%
    \blx@ibidreset% For authordate style
    \usebibmacro{cite}}
  {}%
  {}%

\DeclareCiteCommand{\origfullcite}
  {\usebibmacro{backref+check}%
    \nopunct\unspace%
    \savebibmacro{cmsbibsortdate}%
    \renewbibmacro*{cmsbibsortdate}{}}%
  {\usedriver
    {\DeclareNameAlias{sortname}{default}\clearfield{postnote}%
      \clearname{author}\clearfield{userf}%\toggletrue{cms@fullnote}%
      \toggletrue{cms@headlessnote}\frenchspacing}%
    {\thefield{entrytype}}%
    \iflistundef{pageref}{}{\newunit\usebibmacro{pageref}}}%
  {\multicitedelim}%
  {\restorebibmacro{cmsbibsortdate}}

\DeclareCiteCommand{\origpublcite}% Similar to above, w/o title.
  {\usebibmacro{backref+check}%
    \nopunct%\unspace%
    \savebibmacro{cmsbibsortdate}%
    \renewbibmacro*{cmsbibsortdate}{}}%
  {\usedriver
    {\DeclareNameAlias{sortname}{default}\clearfield{postnote}%
      \usebibmacro{clearpublin}\usebibmacro{cms:titlehook}%
      \toggletrue{cms@headlessnote}%\toggletrue{cms@fullnote}%
      \toggletrue{cms@origpublished}\frenchspacing}%
    {\thefield{entrytype}}%
    \iflistundef{pageref}{}{\newunit\usebibmacro{pageref}}}%
  {\multicitedelim}%
  {\restorebibmacro{cmsbibsortdate}}

%%%% List Formats %%%%

\DeclareListFormat{language}{%
  \ifthenelse{\value{listcount}=1}%
  {\bibleftbracket\bibstring{inlang}%\addspace - for inflected langs.
    \ifbibstring{#1}%
    {\bibstring{#1}}%
    {\ifbibstring{lang#1}%
      {\bibstring{lang#1}}%
      {#1}}%
    \ifthenelse{\value{listtotal}=1}%
    {\bibrightbracket}%
    {}}%
  {\ifthenelse{\value{listcount}=\value{listtotal}}%
    {\multilangdelim%
      \ifbibstring{#1}%
      {\bibstring{#1}}%
      {\ifbibstring{lang#1}%
        {\bibstring{lang#1}}%
        {#1}}%
      \bibrightbracket}%
    {\multilangdelim%
      \ifbibstring{#1}%
      {\bibstring{#1}}%
      {\ifbibstring{lang#1}%
        {\bibstring{lang#1}}%
        {#1}}}}%
  \usebibmacro{langlist:andothers}}%

\DeclareListFormat{publisher}{%
  \ifthenelse{\value{listtotal}<2}%
  {#1\isdot}%
  {\ifthenelse{\value{listcount}=1}%
    {#1}%
    {\multipubsdelim #1\isdot}}}%

\DeclareListFormat{periodplace}{\mkbibparens{#1}}

\DeclareListFormat{lista}{% 
  \ifthenelse{\value{listtotal}<2}%
  {\bibsstring{subverbo}\addspace\mkbibquote{#1\isdot}}%
  {\ifthenelse{\value{listcount}=1}%
    {\bibsstring{subverbis}\addspace\mkbibquote{#1\isdot}\addcomma}%
    {\ifthenelse{\value{listcount}<\value{listtotal}}%
      {\addspace\mkbibquote{#1\isdot}\addcomma}%
      {\addspace\mkbibquote{#1\isdot}}}}}

%%%% Field Formats -- Mostly non-title %%%%

\DeclareFieldFormat{prenote}{\ifcapital{\MakeCapital{#1}}{#1}\isdot}

\DeclareFieldFormat{postnote}{% Changed for page compression option
  \ifboolexpr{%
    togl {cms@comprange}%
    and
    test {\ifpages{#1}}%
  }%
  {\iffieldundef{pagination}%
    {\mkcomprange{#1}}%
    {\mkcomprange[{\mkpageprefix[pagination]}]{#1}}}%
  {\iffieldundef{pagination}%
    {#1}%
    {\mkpageprefix[pagination]{#1}}}}%

\DeclareFieldFormat[inreference]{postnote}{%
  \ifboolexpr{%
    togl {cms@comprange}%
    and
    test {\ifpages{#1}}%
  }%
  {\iffieldundef{pagination}%
    {\bibsstring{subverbo}\addnbspace\mkbibquote{#1}}%
    {\mkcomprange[{\mkpageprefix[pagination]}]{#1}}}%
  {\iffieldundef{pagination}%
    {\bibsstring{subverbo}\addnbspace\mkbibquote{#1}}%
    {\mkpageprefix[pagination]{#1}}}}%

\DeclareFieldFormat{pages}{%
  \ifboolexpr{%
    togl {cms@comprange}%
    and
    test {\ifpages{#1}}%
  }%
  {\iffieldundef{bookpagination}%
    {\mkcomprange{#1}\isdot}%
    {\mkcomprange[{\mkpageprefix[bookpagination]}]{#1}}}%
  {\iffieldundef{bookpagination}%
    {#1\isdot}%
    {\mkpageprefix[bookpagination]{#1}}}}%

\DeclareFieldFormat{bibnote}{\MakeCapital{#1}}

\DeclareListFormat{edlang}{% Required for feminine forms in some
  \usebibmacro{list:delim}{% languages
    \ifbibstring{#1}%
    {\bibxstring{#1}}%
    {\ifbibstring{ed#1}%
      {\bibxstring{ed#1}}%
      {\ifcapital{\MakeCapital{#1}}{#1}}}}%
  \ifbibstring{#1}%
  {\bibstring{#1}}%
  {\ifbibstring{ed#1}%
    {\bibstring{ed#1}}%
    {\ifcapital{\MakeCapital{#1}}{#1}}}%
  \usebibmacro{list:andothers}}

\DeclareFieldFormat[customc]{title}{%
  \iffieldundef{nameaddon}%
  {\mkbibemph{\bibstring{see}}%
    \addspace%
    #1}%
  {#1}}

\DeclareFieldFormat[customc]{citetitle}{%
  \ifnameundef{author}%
  {#1}%
  {\iffieldundef{nameaddon}%
    {\mkbibemph{\bibstring{see}}%
      \addspace%
      #1}%
    {\printfield{nameaddon}\addspace #1}}}

\DeclareFieldFormat{shortjournal}{\mkbibemph{#1}\isdot}

\DeclareFieldFormat[periodical]{shorttitle}{\mkbibemph{#1}\isdot}

%%%% Other Field Formats %%%%

\DeclareFieldFormat{letterday}{\mkbibcurdinal{#1}}

\DeclareFieldFormat{note}{%
  \ifcapital{\MakeCapital{#1}}{#1}}%

\DeclareFieldFormat
[audio,manual,music,patent,report,suppbook,suppcollection,thesis,video]
{type}{%
  \ifbibstring{#1}%
  {\bibstring{#1}}%
  {\ifcapital%
    {\MakeCapital{#1\isdot}}%
    {#1\isdot}}}

\DeclareFieldFormat[artwork,image]{type}{%
  \ifcapital%
  {\MakeCapital{#1}}%
  {#1}}

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

\DeclareFieldFormat{doi}{%
  \textrm{doi}\addcolon
  \ifhyperref
    {\href{http://dx.doi.org/#1}{\nolinkurl{#1}}}%
    {\nolinkurl{#1}}}

\DeclareFieldFormat[music]{date}{% Generalize userd ???
  \ifthenelse{\iffieldundef{userd}\OR\NOT\iffieldundef{urlyear}%
  \OR\NOT\iffieldundef{eventyear}\OR\NOT\iffieldundef{origyear}%
  \OR\NOT\iffieldundef{urlmonth}\OR\NOT\iffieldundef{eventmonth}%
  \OR\NOT\iffieldundef{origmonth}}%
  {#1}%
  {\printfield{userd}\addspace #1}}

\DeclareFieldFormat[video]{date}{% Generalize userd ???
  \ifthenelse{\iffieldundef{userd}\OR\NOT\iffieldundef{urlyear}%
    \OR\NOT\iffieldundef{eventyear}\OR\NOT\iffieldundef{urlmonth}%
    \OR\NOT\iffieldundef{eventmonth}}%
  {#1}%
  {\printfield{userd}\addspace #1}}

\DeclareFieldFormat{date}{% Generalize userd ???
  \ifthenelse{\iffieldundef{userd}\OR\NOT\iffieldundef{urlyear}}%
  {#1}%
  {\printfield{userd}\addspace #1}}

\DeclareFieldFormat{urldate}{% 16th ed.
  \iffieldundef{userd}%
  {\bibstring{urlseen}\space #1}%
  {\printfield{userd}\addspace #1}}

\DeclareFieldFormat[video]{urldate}{% 16th ed.
  \ifthenelse{\iffieldundef{userd}\OR\NOT\iffieldundef{eventyear}}%
  {\bibstring{urlseen}\space #1}%
  {\printfield{userd}\addspace #1}}

\DeclareFieldFormat[music]{urldate}{% 16th ed.
  \ifthenelse{\iffieldundef{userd}\OR\NOT\iffieldundef{eventyear}%
    \OR\NOT\iffieldundef{origyear}}%
  {\bibstring{urlseen}\space #1}%
  {\printfield{userd}\addspace #1}}

\DeclareFieldFormat[music]{origdate}{% 16th ed.
  \iftoggle{cms@reprint}% Date fix
  {#1}%
  {\ifthenelse{\iffieldundef{userd}\OR\NOT\iffieldundef{eventyear}}%
    {\bibstring{discrecorded}\space #1}%
    {\printfield{userd}\addspace #1}}}

\DeclareFieldFormat[music]{eventdate}{% 16th ed.
  \iffieldundef{userd}%
  {\bibstring{songrecorded}\space #1}%
  {\printfield{userd}\addspace #1}}

\DeclareFieldFormat[video]{eventdate}{% 16th ed.
  \iffieldundef{userd}%
  {\bibstring{broadcast}\space #1}%
  {\printfield{userd}\addspace #1}}

\DeclareFieldAlias{userd}{titleaddon}% 16th ed.

\DeclareFieldFormat{nameaddon}{\mkbibbrackets{#1\bibsentence}}% ?!

\DeclareFieldFormat[review,suppperiodical]{nameaddon}{#1\bibsentence}% 16th ed.

\DeclareFieldFormat[customc]{nameaddon}{% For cross-refs
  \ifbibstring{#1}%
  {\mkbibemph{\bibstring{#1}}}%
  {#1}}%

\DeclareFieldFormat{edition}{% New in 0.8
  \ifinteger{#1}%
  {\mkbibordedition{#1}~\bibstring{edition}}%
  {\ifcapital
    {\MakeCapital{#1\isdot}}%
    {#1\isdot}}}

\DeclareFieldFormat{usere}{[#1]} % Better than mkbibbrackets?

\DeclareFieldFormat{titleaddon}{%
  \ifcapital{\MakeCapital{#1\isdot}}{#1\isdot}}%\custpunctc?

\DeclareFieldAlias{booktitleaddon}{titleaddon}

\DeclareFieldAlias{maintitleaddon}{titleaddon}

\DeclareFieldFormat{jourser}{%
  \ifinteger{#1}%
  {\mkbibordseries{#1}%
    \addnbspace%
    \bibstring{jourser}}%
  {\ifbibstring{#1}{\bibstring{#1}}{#1}}}

\DeclareFieldFormat{journum}{% Revised for 0.9.5
  \ifboolexpr{%
    test {\ifnumerals{#1}}%
    and
    not test {\ifnumeral{#1}}%
  }%
  {\bibstring{numbers}\addspace #1}%
  {\bibstring{number}\addspace #1}}

\DeclareFieldFormat{sernum}{%
  \ifnumeral{#1}%
  {\addnbspace #1}%
  {\addcomma\addspace #1}}

\DeclareFieldFormat{addendum}{%
  \ifcapital{\MakeCapital{#1\isdot}}{#1\isdot}}

% This works better here than in the entrytail macro -- userf use is
% no longer a problem, though the page breaking still isn't ideal.

\DeclareFieldFormat{annotation}{\par\nobreak \vskip \bibitemsep #1}

\DeclareFieldFormat{part}{%
  \ifnumerals{#1}%
  {\addcomma\addspace\bibstring{partvolume}~#1}%
  {\addcomma\addspace\ifcapital{\MakeCapital{#1}}{#1}}}

\DeclareFieldAlias[review]{volume}[article]{volume}

\DeclareFieldAlias[suppperiodical]{volume}[article]{volume}

\DeclareFieldFormat{cmshyper}{% Control the number of elements hyperlinked.
  \ifboolexpr{%
    (
    not test {\iffieldequalstr{entrysubtype}{classical}}%
    or
    togl {blx@skipbib}%
    )
    and
    not togl {cms@linkit}%
  }%
  {#1}%
  {\bibhyperref{#1}}}%

\DeclareFieldFormat{cmsyearhyper}{% Only link when in ref list
  \iftoggle{blx@skipbib}
  {#1}%
  {\bibhyperref{#1}}}%

%%%% Related field formats from biblatex.def %%%%

\DeclareFieldFormat{related:origpubas}{#1}% This and next remove parens

\DeclareFieldFormat{related:origpubin}{#1}

\DeclareFieldFormat{relatedstring:default}{% For notes + bib
  \ifboolexpr{%
    test {\iffieldundef{relatedstring}}%
    or
    test {\iffieldbibstring{relatedstring}}%
  }%
  {#1}%
  {\ifcapital%
    {\MakeCapital{#1}}%
    {#1}}%
  \ifentrytype{jurisdiction}%
  {\newcunit}%
  {\printunit{\relatedpunct}}}%

\DeclareFieldFormat{relatedstring:reprintfrom}{% For notes + bib
  \ifboolexpr{%
    test {\iffieldundef{relatedstring}}%
    or
    test {\iffieldbibstring{relatedstring}}%
  }%
  {#1}%
  {\ifcapital%
    {\MakeCapital{#1}}%
    {#1}}%
  }%\addspace

%%%% Commands, for users and internal %%%%

\newcommand*{\cbytypeeditor}{% Needed?
  \iffieldundef{editortype}%
    {\bibstring{cbytypeeditor}}%
    {\bibstring{cbytype\thefield{editortype}}}}

\renewcommand*{\multicitedelim}{\addsemicolon\space}

\newcommand*{\cms@testspace}{% FIXME
  \ifboolexpr{%
    test {\ifnumequal{\spacefactor}{\blx@sf@dot}}%
    and
    not test {\ifbibliography}%
  }%
  {\addnbspace}%
  {\addspace}}%

\renewcommand*{\iffinalcitedelim}{%
  \ifnumequal{\value{textcitecount}}{\value{textcitetotal}-1}}

\renewcommand*{\nameyeardelim}{%
  \ifboolexpr{%
    (
    togl {cms@bothlabelnew}%
    or
    togl {cms@bothlabelold}%
    )
    and
    (
    not test {\iffieldundef{year}}%
    and
    not test {\iffieldundef{origyear}}%
    )
  }%
  {\cms@testspace}%
  {\cms@pre@punct%
    \ifboolexpr{%
      test {\iffieldundef{labelyear}}%
      or
      test {\iffieldequalstr{labelyear}{nodate}}%
      or
      togl {cms@fulldate}%
      or
      bool {cms:comma}%
    }%
    {\addcomma\addspace}%
    {\iffieldundef{origyear}%
      {\iffieldundef{year}%
        {\cms@testspace}%
        {\ifboolexpr{%
            test {\iffieldnums{year}}%
            or
            test {\iffieldstart{year}{[}}%] For backward compatibility
          }%
          {\cms@testspace}%
          {\addcomma\addspace}}}%
      {\cms@testspace}}\global\boolfalse{cms:comma}}}

\newrobustcmd*{\cms@citepunct@helper}[1]{%
  \begingroup
  \blx@metadateinfo{#1}%
  \ifboolexpr{%
    test {\if@cms@bracket}%
    and
    not togl {cms@noyearbrackets}%
  }%
  {}%
  {\if@cms@circa%
    {\global\booltrue{cms:comma}}%
    {\if@cms@adfirst%
      {\iffieldnum{#1year}%
        {\ifboolexpr{%
            test {\ifdefstring\blx@dateera{christian}}%
            and
            not test {\iffieldequalstr{#1dateera}{bce}}%
            and
            test {\ifnumless{\thefield{#1year}}\blx@dateeraauto}%
          }%
          {\global\booltrue{cms:comma}}%
          {}}%
        {}}%
      {\ifboolexpr{%
          test {\ifdefstring\blx@languagename{french}}%
          or
          test {\ifdefstring\blx@languagename{dutch}}%
        }%
        {\ifboolexpr{%
            not test {\iffieldequalstr{#1dateunspecified}{yearindecade}}%
            or
            togl {cms@alwaysrange}%
            or
            togl {cms@decaderange}%
          }%
          {}%
          {\global\booltrue{cms:comma}}}%
        {\ifboolexpr{%
            test {\ifdefstring\blx@languagename{brazilian}}%
            or
            test {\ifdefstring\blx@languagename{brazil}}%
          }%
          {\ifboolexpr{%
              test {\iffieldundef{#1dateunspecified}}%
              or
              togl {cms@alwaysrange}%
            }%
            {}%
            {\ifboolexpr{%
                (
                test {\iffieldequalstr{#1dateunspecified}{yearindecade}}%
                and
                not togl {cms@decaderange}%
                )
                or
                (
                test {\iffieldequalstr{#1dateunspecified}{yearincentury}}%
                and
                not togl {cms@centuryrange}%
                )
              }%
              {\global\booltrue{cms:comma}}%
              {}}}%
          {}}}}}%
  \endgroup}

\newrobustcmd*{\cms@pre@punct}{%
  \iftoggle{cms@origlabel}%
  {\cms@citepunct@helper{orig}}%
  {\ifdefstring\abx@field@labeldatesource{year}%
    {\cms@citepunct@helper{}}%
    {\ifdefstring\abx@field@labeldatesource{origyear}%
      {\cms@citepunct@helper{orig}}%
      {\ifdefstring\abx@field@labeldatesource{nodate}%
        {}%
        {\cms@citepunct@helper{\abx@field@labeldatesource}}}}}}

\renewcommand*{\subtitlepunct}{% Follows CMS16 spec.
  \ifboolexpr{%
    test {\ifterm}%
    and
    not test {\ifcsdef{@cmsst}}%
  }%
  {\addspace}%
  {\addcolon\addspace%
    \global\csundef{@cmsst}}%
}

\newcommand{\postvolpunct}{\addcolon}%

\newcommand*{\parttrans}{%
  {\bibstring{bytranslator}\space}}%

\protected\def\partedit#1{%
  \ifcat\noexpand~\noexpand#1%
  \ifundef{\cms@tempb}{}{\appto{\cms@tempb}{#1}}%
  \def\cms@tempa{\futurelet\cms@tempa\partedit}%
  \else%
  \ifx\addnbspace#1%
  \ifundef{\cms@tempb}{}{\appto{\cms@tempb}{#1}}%
  \def\cms@tempa{\futurelet\cms@tempa\partedit}%
  \else%
  \ifx\addspace#1%
  \ifundef{\cms@tempb}{}{\appto{\cms@tempb}{#1}}%
  \def\cms@tempa{\futurelet\cms@tempa\partedit}%
  \else%
  \if#1H%
  \appto{\cms@tempb}{#1}%
  \def\cms@tempa{\futurelet\cms@tempa\partedit}%
  \else%
  \if#1h%
  \appto{\cms@tempb}{#1}%
  \def\cms@tempa{\futurelet\cms@tempa\partedit}%
  \else%
  \def\cms@tempa{\part@edit@i\lbx@initnamehook{#1}%
    \csuse{cms@tempb}#1\csundef{cms@tempb}}%
  \fi%
  \fi%
  \fi%
  \fi%
  \fi%
  \cms@tempa%
}%

\newcommand*{\part@edit@i}{\bibstring{byeditor}\addspace}%

\newcommand*{\partcomp}{%
  {\bibstring{bycompiler}\space}}%

\newcommand*{\parteditandcomp}{%
  {\bibstring{byeditorcp}\space}}%

\newcommand*{\parttransandcomp}{%
  {\bibstring{bytranslatorcp}\space}}%

\newcommand*{\partedittransandcomp}{%
  {\bibstring{byeditortrcp}\space}}%

\newcommand*{\parteditandtrans}{%
  {\bibstring{byeditortr}\space}}%

\newcommand*{\reprint}{\bibstring{reprint}}%

\newcommand*{\multipubsdelim}{\addnbspace/\addspace}

\newcommand*{\multilocsdelim}{%
  \ifthenelse{\value{listcount}<\value{liststop}}%
    {\ifthenelse{\numexpr\value{listcount}+1<\value{liststop}}%
       {\addcomma\addspace}%
       {\ifthenelse{\value{liststop}>2}%
         {\addcomma\addspace\bibstring{and}\addspace}%
         {\addspace\bibstring{and}\addspace}}}%
       {}}%

\newcommand*{\multilangdelim}{%
  \ifthenelse{\value{listtotal}<3}%
  {\addspace\bibstring{and}\addspace}%
  {\ifthenelse{\value{listcount}<\value{listtotal}}%
    {\addcomma\addspace}%
    {\addcomma\addspace\bibstring{and}\addspace}}}

\renewcommand*{\postnotedelim}{% Cf. N&B style
  \ifboolexpr{%
    test {\ifciteibid}%
    and
    (
    test {\ifentrytype{jurisdiction}}%
    or
    test {\ifentrytype{legal}}%
    or
    test {\ifentrytype{legislation}}%
    )
  }%
  {\addspace}%
  {\iftoggle{cms@inlineibid}%
    {\togglefalse{cms@inlineibid}%
      \iffieldundef{prenote}% Bug fix
      {}%
      {\cms@testspace}}%
    {\iffieldequalstr{entrysubtype}{classical}% For Notes+Bib, too?
      {\DeclareNumChars*{abcdeABCDE:}%
        \iffieldpages{postnote}%
        {\cms@testspace}%
        {\addcomma\addspace}}%
      {\addcomma\addspace}\DeclareNumChars{.}}}}

\newcommand*{\postnotewrapper}{%
  \iftoggle{cms@modpostnote}%
  {\ifboolexpr{%
      test {\iffieldstart{postnote}{,}}%
      or
      test {\iffieldstart{postnote}{\bibrangessep}}%
    }%
    {\addcomma}% w/ or w/o \addspace?
    {\ifboolexpr{%
        test {\iffieldstart{postnote}{;}}%
        or
        test {\iffieldstart{postnote}{:}}%
        or
        test {\iffieldstart{postnote}{.}}%
      }%
      {}{\postnotedelim}}}%
  {\postnotedelim}}%

\newrobustcmd*{\iffieldstart}[2]{% Philipp Lehman's code, from
  \begingroup%                     comp.text.tex
  \edef\@tempa{%
    \long\def\noexpand\iffieldstart@i####1\detokenize{#2}####2}%
  \@tempa\@nil{\endgroup\ifblank{##1}}%
  \savefield*{#1}{\@tempa}%
  \expandafter\iffieldstart@i\detokenize
  \expandafter\expandafter\expandafter{%
    \expandafter\@tempa\detokenize{#2}}\@nil}

\newcommand*{\editordelim}{% Otherwise you get an inaccurate comma.
  \iffieldequalstr{editortype}{none}%
  {\newunit}%\addperiod\addspace
  {\newcunit}}%\addcomma\addspace

\newcommand*{\nameadelim}{% Otherwise you get an inaccurate comma.
  \iffieldequalstr{nameatype}{none}%
  {\newunit}%\addperiod\addspace
  {\newcunit}}%\addcomma\addspace

\DeclareDelimFormat{dateeradelim}{\addnbspace}%
\DeclareDelimFormat{dateaddelim}{\addnbspace}%

\DeclareListFormat{cfromoriglanguage}{% Needed for notes, to provide
  \begingroup% final {by}.
  \blx@bibstringnormal%
  \usebibmacro{list:delim}{%
    \ifbibstring{from#1}%
      {\bibxlstring{cfrom#1}}%
      {\ifbibstring{clang#1}%
         {\bibxlstring{lang#1}}%
         {#1}}}%
  \ifbibstring{cfrom#1}%
  {\ifboolexpr{%
      test {\ifnumless{\value{listcount}}{\value{liststop}}}%
      or
      test {\ifmoreitems}%
     }%
     {\bibstring{from#1}}%
     {\bibstring{cfrom#1}}}%
    {\ifbibstring{lang#1}%
       {\biblstring{lang#1}}%
       {#1}}%
 \usebibmacro{list:andotherlangs}%
 \endgroup}

\newbibmacro*{list:andotherlangs}{% Final {by} after {andmore}
  \ifboolexpr{%
    test {\ifnumequal{\value{listcount}}{\value{liststop}}}%
    and
    test {\ifmoreitems}%
  }%
    {\ifnumgreater{\value{liststop}}{1}%
       {\finalandcomma}%
       {}%
     \printdelim{andmoredelim}\bibstring{andmore}\addspace\bibstring{by}}%
    {}}

\newcommand*{\lbx@cfromlang}{% Because the cbytranslator string can't
  \iffieldundef{userf}% have {by} in it unless there's an origlanguage.
  {\iflistundef{origlanguage}%
    {\unspace}%
    {\printlist[cfromoriglanguage]{origlanguage}}}%
  {\unspace}}%

\renewcommand*{\lbx@fromlang}{%
  \iffieldundef{userf}%
  {\iflistundef{origlanguage}%
    {\unspace}%
    {\printlist[lfromoriglanguage]{origlanguage}}}%
  {\unspace}}%

\renewcommand*{\lbx@lfromlang}{%
  \iffieldundef{userf}%
  {\iflistundef{origlanguage}%
    {\unspace}%
    {\printlist[lfromoriglanguage]{origlanguage}}}%
  {\unspace}}%

\renewcommand*{\lbx@sfromlang}{%
  \iffieldundef{userf}%
  {\iflistundef{origlanguage}%
    {\unspace}%
    {\printlist[sfromoriglanguage]{origlanguage}}}%
  {\unspace}}%

%%%% Formatting macros, called both by cbx and bbx %%%%

\newbibmacro*{finentry}{%{\finentry} To make annotated bibliography
  \ifbibliography
    {\usebibmacro{entrytail}}%
    {}%
  \finentry}

\newbibmacro*{entrytail}{% From reading.bbx, for annotated bibliography
  \newunit\newblock
  \iftoggle{cms@annotation}%
    {\usebibmacro{annotation}%
     \newunit\newblock}%
    {}}%

\newbibmacro*{author+holder}{%
  \ifnameundef{author}%
    {\let\bbx@lasthash\undefined}%
    {\usebibmacro{author/editor}%
     \ifthenelse{\ifnameundef{holder}\OR
                 \ifnamesequal{author}{holder}}%
       {}%
       {\setunit{\addspace}%
        \printtext[parens]{\printnames{holder}}}}}

\renewbibmacro*{byauthor}{%
  \ifthenelse{\ifuseauthor\OR
              \ifnameundef{author}}%
    {}%
    {\bibstring{by}\addspace%
     \printnames[byauthor]{author}}}

\newbibmacro*{byauthorpunct}{%
  \ifthenelse{\ifuseauthor\OR\ifnameundef{author}}%
  {\addperiod\addspace}%
  {\newcunit}}

\renewbibmacro*{bybookauthor}{%
  \ifnameundef{bookauthor}%
    {}%
    {\ifnamesequal{author}{bookauthor}%
      {}%
      {\bibstring{by}\addspace\printnames[default]{bookauthor}%
     \newcunit\newblock}}}

\newbibmacro*{editorpunct}{%
  \ifthenelse{\(\iffieldundef{booktitle}\AND%
    \iffieldundef{maintitle}\AND\iffieldundef{issuetitle}\)%
    \OR\iffieldsequal{booktitle}{title}%  Changed these for crossrefed
    \OR\iffieldsequal{maintitle}{title}}% entries.  Create problems?
  {\ifentrytype{video}%
    {\newcunit\newblock}%
    {\newunit\newblock}}%
  {\newcunit\newblock}}

\newbibmacro*{edition}{%
  \printfield{edition}%
    \clearfield{edition}}%

\newbibmacro*{inforaft}{%
  \ifnameundef{introduction}%
  {\ifnameundef{afterword}%
    {\ifnameundef{foreword}%
      {\printfield{type}}%
      {\bibstring{forewordto}%
        \clearname{foreword}}}%
    {\bibstring{afterwordto}%
      \clearname{afterword}}}%
  {\bibstring{introductionto}%
    \clearname{introduction}}}

\newbibmacro*{langlist:andothers}{%
  \ifthenelse{\value{listcount}=\value{liststop}\AND
              \ifmoreitems}%
    {\ifnum\value{liststop}>1 \finalandcomma\fi
     \andmoredelim\bibstring{andmore}\bibrightbracket}%
    {}}

\newbibmacro*{reference+title}{%
  \ifboolexpr{%
    test {\iffieldequals{title}{\bbx@lasthash}}%
    and
    not test {\iffirstonpage}%
    and
    togl {cms@namedash}%
  }%
  {\printtext{\bibnamedash}}%
  {\usebibmacro{italtitle+stitle}%
    \savefield{title}{\bbx@lasthash}}}

\newbibmacro*{mag+news+author}{%
  \ifboolexpr{%
    (
    test {\ifnameundef{author}}%
    or
    not test {\ifuseauthor}%
    )
    and
    not togl {cms@origpublished}%
  }%
  {\ifboolexpr{%
      test {\iffieldequals{journaltitle}{\bbx@lasthash}}%
      and
      not test {\iffirstonpage}%
      and
      togl {cms@namedash}%
    }%
    {\printtext{\bibnamedash}}%
    {\usebibmacro{journal+sub}%
      \setunit*{\addspace}%
      \printlist[periodplace]{location}%
      \savefield{journaltitle}{\bbx@lasthash}}}%
  {\usebibmacro{author}}}

\newbibmacro*{cmag+news+author}{%
  \ifnameundef{author}%
  {\usebibmacro{journal+sub}%
    \setunit*{\addspace}%
    \printlist[periodplace]{location}}%
  {\usebibmacro{author}}}

\newbibmacro*{type+inst+year}{%
  \printfield{type}%
  \newcunit
  \printlist{institution}%
  \setunit*{\addcomma\space}%
  \printdate}% Was printfield{year}?

\newbibmacro*{institution+organization}{%
  \iflistundef{organization}%
  {\iflistundef{institution}%
    {}%
    {\printlist{institution}}}%
  {\printlist{organization}%
    \newcunit%
    \printlist{institution}}}

\newbibmacro*{author+org}{%
  \ifnameundef{author}%
  {\ifnameundef{editor}%
    {\iflistundef{organization}%
      {\let\bbx@lasthash\undefined}%
      {\ifboolexpr{%
          test {\iflistequals{organization}{\bbx@lasthash}}%
          and
          not test {\iffirstonpage}%
          and
          togl {cms@namedash}%
        }%
        {\printtext{\bibnamedash}}%
        {\iftoggle{cms@authorparens}%
          {\bibopenparen\printlist{organization}\bibcloseparen}%
          {\printlist{organization}}%
          \savelist{organization}{\bbx@lasthash}}}}%
    {\usebibmacro{editor}}}%
  {\usebibmacro{author/editor}}}

\newbibmacro*{cbytypestrg}[2]{%
  \iffieldundef{#1type}%
    {\bibstring{by#2}}%
    {\bibstring{by\thefield{#1type}}}}

\newbibmacro*{cbyeditor}{%
  \ifnameundef{editor}%
    {}%
    {\usebibmacro{cbytypestrg}{editor}{editor}%
     \setunit{\addspace}%
     \printnames[byeditor]{editor}%
     \newcunit}%
  \usebibmacro{cbyeditorx}}

\newbibmacro*{cbyeditorx}{%
  \ifnameundef{editora}%
    {}%
    {\usebibmacro{cbytypestrg}{editora}{editor}%
     \setunit{\addspace}%
     \printnames[byeditora]{editora}%
     \newcunit}%
  \ifnameundef{editorb}%
    {}%
    {\usebibmacro{cbytypestrg}{editorb}{editor}%
     \setunit{\addspace}%
     \printnames[byeditorb]{editorb}%
     \newcunit}%
  \ifnameundef{editorc}%
    {}%
    {\usebibmacro{cbytypestrg}{editorc}{editor}%
     \setunit{\addspace}%
     \printnames[byeditorc]{editorc}%
     \newcunit}}

\newbibmacro*{cbytranslator}{%
  \ifnameundef{translator}%
  {}%
  {\bibstring{bytranslator}%
    \addspace
    \printnames[bytranslator]{translator}}}

\newbibmacro*{cbycompiler}{%
  \ifnameundef{namec}%
    {}%
    {\bibstring{cbycompiler}\addspace%
     \printnames[bycompiler]{namec}}}

\newbibmacro*{cbyredactor}{%
  \ifnameundef{redactor}%
    {}%
    {\bibstring{cbyredactor}\addspace%
     \printnames[byredactor]{redactor}}}

\newbibmacro*{cwithcommentator}{%
  \ifnameundef{commentator}%
    {}%
    {\bibstring{withcommentator}\addspace%
     \printnames[withcommentator]{commentator}}}

\newbibmacro*{cwithannotator}{%
  \ifnameundef{annotator}%
    {}%
    {\bibstring{withannotator}\addspace%
     \printnames[withannotator]{annotator}}}

\newbibmacro*{cwithintroduction}{%
  \ifnameundef{introduction}%
    {}%
    {\bibstring{withintroduction}\addspace%
     \printnames[withintroduction]{introduction}}}

\newbibmacro*{cwithforeword}{%
  \ifnameundef{foreword}%
    {}%
    {\bibstring{withforeword}\addspace%
     \printnames[withforeword]{foreword}}}

\newbibmacro*{cwithafterword}{%
  \ifnameundef{afterword}%
    {}%
    {\bibstring{withafterword}\addspace%
     \printnames[withafterword]{afterword}}}

\newbibmacro*{cbyeditor+others}{%
  \ifthenelse{\NOT\ifnameundef{editor}\AND
    \(\iffieldundef{editortype}\OR
    \iffieldequalstr{editortype}{editor}\)}%
  {\def\@tempa{cbyeditor}%
    \ifnamesequal{editor}{translator}%
    {\edef\@tempa{\@tempa tr}%
      \clearname{translator}}%
    {}%
    \ifnamesequal{editor}{namec}%
    {\edef\@tempa{\@tempa cp}%
      \clearname{namec}}%
    {}%
    \ifnamesequal{editor}{commentator}%
    {\edef\@tempa{\@tempa co}%
      \clearname{commentator}}%
    {\ifnamesequal{editor}{annotator}%
      {\edef\@tempa{\@tempa an}%
        \clearname{annotator}}%
      {}}%
    \ifnamesequal{editor}{introduction}%
    {\edef\@tempa{\@tempa in}%
      \clearname{introduction}}%
    {\ifnamesequal{editor}{foreword}%
      {\edef\@tempa{\@tempa fo}%
        \clearname{foreword}}%
      {\ifnamesequal{editor}{afterword}%
        {\edef\@tempa{\@tempa af}%
          \clearname{afterword}}%
        {}}}%
    \bibstring{\@tempa}\space%
    \printnames[byeditor]{editor}%
    \clearname{editor}%
    \newcunit%
    \usebibmacro{cbyeditorx}}%
  {\usebibmacro{cbyeditor}}%
  \usebibmacro{cbytranslator+others}}

\newbibmacro*{cbytranslator+others}{%
  \ifnameundef{translator}%
    {}%
    {\def\@tempa{cbytranslator}%
      \ifnamesequal{translator}{namec}%
      {\edef\@tempa{\@tempa cp}%
        \clearname{namec}}%
      {}%
     \ifnamesequal{translator}{commentator}%
       {\edef\@tempa{\@tempa co}%
        \clearname{commentator}}%
       {\ifnamesequal{translator}{annotator}%
          {\edef\@tempa{\@tempa an}%
           \clearname{annotator}}%
          {}}%
     \ifnamesequal{translator}{introduction}%
       {\edef\@tempa{\@tempa in}%
        \clearname{introduction}}%
       {\ifnamesequal{translator}{foreword}%
          {\edef\@tempa{\@tempa fo}%
           \clearname{foreword}}%
          {\ifnamesequal{translator}{afterword}%
             {\edef\@tempa{\@tempa af}%
              \clearname{afterword}}%
             {}}}%
     \bibstring{\@tempa}\space%
     \printnames[bytranslator]{translator}%
     \clearname{translator}%
     \newcunit}%
  \usebibmacro{cbycompiler+others}}

\newbibmacro*{cbycompiler+others}{%
  \ifnameundef{namec}%
    {}%
    {\def\@tempa{cbycompiler}%
     \ifnamesequal{namec}{commentator}%
       {\edef\@tempa{\@tempa co}%
        \clearname{commentator}}%
       {\ifnamesequal{namec}{annotator}%
          {\edef\@tempa{\@tempa an}%
           \clearname{annotator}}%
          {}}%
     \ifnamesequal{namec}{introduction}%
       {\edef\@tempa{\@tempa in}%
        \clearname{introduction}}%
       {\ifnamesequal{namec}{foreword}%
          {\edef\@tempa{\@tempa fo}%
           \clearname{foreword}}%
          {\ifnamesequal{namec}{afterword}%
             {\edef\@tempa{\@tempa af}%
              \clearname{afterword}}%
             {}}}%
     \bibstring{\@tempa}\space%
     \printnames[bycompiler]{namec}%
     \clearname{namec}%
     \newcunit}%
  \usebibmacro{cbyothers}}

\newbibmacro*{cbyothers}{%
  \usebibmacro{cbytranslator}%
  \setunit*{\addcomma\addspace}%
  \usebibmacro{cbycompiler}%
  \setunit*{\addcomma\addspace}%
  \usebibmacro{cbyredactor}%
  \setunit*{\addcomma\addspace}%
  \usebibmacro{cwithcommentator}%
  \setunit*{\addcomma\addspace}%
  \usebibmacro{cwithannotator}%
  \setunit*{\addcomma\addspace}%
  \usebibmacro{cwithintroduction}%
  \setunit*{\addcomma\addspace}%
  \usebibmacro{cwithforeword}%
  \setunit*{\addcomma\addspace}%
  \usebibmacro{cwithafterword}}

\newbibmacro*{cms-in:}{% Fix for 0.9a compat.
  \iftoggle{cms@origpublished}%
  {}%
  {\bibstring{in}%
    \setunit{\addspace}}}

\newbibmacro*{xref-in:}{%
  \iffieldundef{volume}{}{\savefield{volume}{\cbx@incollvol}}%
  \iffieldundef{part}{}{\savefield{part}{\cbx@incollpart}}%
  \bibstring{in}\setunit{\addspace}}% Changed for related:reprintfrom

\newbibmacro*{chapincoll}{%
  \iffieldundef{chapter}%
  {}%
  {\printfield{chapter}\addspace%
    \clearfield{chapter}}}%

\newbibmacro*{chapinscore}{%
  \iffieldundef{chapter}%
  {\ifboolexpr{%
      test {\ifentrytype{music}}%
      and
      not test {\iffieldundef{booktitle}}%
      and
      not togl {cms@origpublished}%
    }%
    {\bibstring{on}\setunit{\addspace}}%
    {}}%
  {\printfield{chapter}\clearfield{chapter}%
    \iffieldundef{booktitle}%
    {}%
    {\addspace\bibstring{of}\setunit{\addspace}}}}%

\newbibmacro*{music+ser+num}{%
  \iffieldundef{series}%
  {\iffieldundef{number}%
    {}%
    {\printfield{number}}}%
  {\printfield{series}%
    \setunit{\addspace}%
    \printfield{number}}}%

\newbibmacro*{music+publisher}{%
  \iffieldundef{howpublished}%
  {\iffieldundef{pubstate}%
    {}%
    {\printfield{pubstate}}}%
  {\printfield{howpublished}}}%

\newbibmacro*{music+origdate}{%
  \iftoggle{cms@reprint}% 16th ed.
  {}%
  {\iftoggle{cms@switchdates}% Date fix
    {\ifthenelse{\iffieldundef{year}\AND\iffieldundef{month}\AND%
        \iffieldundef{yeardivision}}%
      {}%
      {\usebibmacro{cmsorigdate}}}%
    {\ifthenelse{\iffieldundef{origyear}\AND\iffieldundef{origmonth}\AND%
        \iffieldundef{origyeardivision}}%
      {}%
      {\usebibmacro{cmsorigdate}}}}}%

\newbibmacro*{music+eventdate}{% Date fix
  \ifthenelse{\iffieldundef{eventyear}\AND\iffieldundef{eventmonth}}%
  {}%
  {\printeventdate}}

\newbibmacro*{ser+num}{%
  \printfield{series}%
  \printfield[sernum]{number}%
  \newunit}

\newbibmacro*{language+transtitle}{%
  \iffieldundef{usere}%
  {\printlist[][-\value{listtotal}]{language}}%
  {\printfield{usere}}}

\newbibmacro*{publ+loc+year}{%
  \printlist{location}%
  \iflistundef{publisher}%
  {\setunit*{\addcomma\addspace}}%
  {\setunit*{\addcolon\addspace}}%
  \printlist{publisher}%
  \setunit*{\addcomma\addspace}%
  \usebibmacro{date}% For the author-date style.  Tricky.
}%

\newbibmacro*{origpubl+loc+year}{% 16th ed.
  \printlist{origlocation}%
  \iflistundef{origpublisher}%
  {\setunit*{\addcomma\addspace}}%
  {\setunit*{\addcolon\addspace}}%
  \printlist{origpublisher}}%

\newbibmacro*{howpubl+loc+year}{%
  \printlist{location}%
  \iffieldundef{howpublished}%
  {\setunit*{\addcomma\space}}%
  {\setunit*{\addcolon\space}}%
  \printfield{howpublished}%
  \setunit*{\addcomma\space}%
  \usebibmacro{date}%
}%

\newbibmacro*{inst+loc+year}{%
  \printlist{location}%
  \iflistundef{institution}%
  {\setunit*{\addcomma\space}}%
  {\setunit*{\addcolon\space}}%
  \printlist{institution}%
  \setunit*{\addcomma\space}%
  \usebibmacro{date}%
}%

\newbibmacro*{originally+published+as}{% Punctuation fix now in 
  \iffieldundef{userf}%                  \origfullcite for 0.8e. 
  {\iffieldundef{reprinttitle}%
    {}%
    {\usebibmacro{begrelated}%
      \bibstring{reprintfrom}% ?
      \origpublcite{\thefield{reprinttitle}}%
      \usebibmacro{endrelated}%
      \newunit}}%
  {\usebibmacro{begrelated}%
    \iflistundef{origlanguage}%
    {\bibstring{origpub}%
      \origfullcite{\thefield{userf}}%
      \usebibmacro{endrelated}%
      \newunit}%
    {\iftoggle{cms@postposit}%
      {\bibstring{origedition}%
        \setunit{\addspace}%
        \printlist[edlang]{origlanguage}%
        \addcolon%
        \origfullcite{\thefield{userf}}%
        \usebibmacro{endrelated}%
        \newunit}%
      {\printlist[edlang]{origlanguage}%
        \setunit{\addspace}%
        \bibstring{origedition}%
        \origfullcite{\thefield{userf}}%
        \usebibmacro{endrelated}%
        \newunit}}}}

\newbibmacro*{org+publ+loc+year}{% What was wrong with \ifthenelse here?
  \printlist{location}%
  \iflistundef{organization}%
  {\iflistundef{publisher}%
    {\setunit*{\addcomma\addspace}}%
    {\setunit*{\addcolon\addspace}}}%
  {\setunit*{\addcolon\addspace}}%
  \printlist{organization}%
  \setunit*{\addcomma\space}%
  \printlist{publisher}%
  \setunit*{\addcomma\addspace}%
  \usebibmacro{date}}

\newbibmacro*{year+in+parens}{%
  \iffieldundef{volume}%
  {noformat}%
  {parens}}

\newbibmacro*{letter+date}{% New for 0.9
  \iflistundef{origlocation}%
  {}%
  {\printlist{origlocation}%
    \newcunit\newblock}%
  \iftoggle{cms@switchdates}%
  {\ifthenelse{\iffieldundef{year}\AND\iffieldundef{month}\AND%
      \iffieldundef{yeardivision}}%
    {}%
    {\cms@datelong{}}}%
  {\ifthenelse{\iffieldundef{origyear}\AND\iffieldundef{origmonth}\AND%
      \iffieldundef{origyeardivision}}%
    {}%
    {\cms@datelong{orig}}}}

\newbibmacro*{unpubl+letter+date}{% For the Misc type.
  \iflistundef{origlocation}%
  {}%
  {\printlist{origlocation}%
    \newcunit\newblock}%
  \iftoggle{cms@switchdates}%
  {\ifthenelse{\iffieldundef{year}\AND\iffieldundef{month}\AND%
      \iffieldundef{yeardivision}}%
    {}%
    {\cms@datelong{}}}%
  {\ifthenelse{\iffieldundef{origyear}\AND\iffieldundef{origmonth}\AND%
      \iffieldundef{origyeardivision}}%
    {\printdate}% For interviews and other dated non-letters
    {\cms@datelong{orig}}}}

\newbibmacro*{cmsbookdate}{%
  \ifthenelse{\iffieldundef{year}\AND\iffieldundef{origyear}}%
  {\newunit}%
  {\iftoggle{cms@switchdates}%
    {\iffieldundef{origyear}%
      {\newunit}%
      {\newcunit\printorigdate}}%
    {\iffieldundef{year}%
      {\newunit}%
      {\newcunit\printdate}}}}

\renewbibmacro*{date}{% Adding the test solved some issues in 0.9 with
  \iftoggle{cms@switchdates}%
  {\ifthenelse{\iffieldundef{origyear}\AND\iffieldundef{origmonth}%
    \AND\iffieldundef{origday}\AND\iffieldundef{origyeardivision}}%
    {}%
    {\printorigdate}}%
  {\ifthenelse{\iffieldundef{year}\AND\iffieldundef{month}%
    \AND\iffieldundef{day}\AND\iffieldundef{yeardivision}}% Punctuation in some
    {}%  entry types (Misc).  The whole \printdate thing may need further work.
    {\printdate}}}

\newbibmacro*{time+stamp}{% 17th ed.
  \iftoggle{cms@switchdates}%
  {\iffieldundef{orighour}%
    {}%
    {\newcunit\printorigtime}}%
  {\iffieldundef{hour}%
    {}%
    {\newcunit\printtime}}}

\newcommand*{\cms@datelong}[1]{% Modified for 17th ed.
  \begingroup
  \protected\def\mkbibdatelong##1##2##3{%
    \iffieldundef{##3}%
    {}%
    {\iftoggle{cms@ukord}%
      {\mkbibordinal{\thefield{##3}}}%
      {\stripzeros{\thefield{##3}}}%
      \iffieldundef{##2}{}{\nobreakspace}}%
    \iffieldundef{##2}%
    {}%
    {\mkbibmonth{\thefield{##2}}%
      \iffieldundef{##1}{}{\space}}%
    \iffieldbibstring{##1}%
    {\bibstring{\thefield{##1}}}%
    {\cmsdateeraprintpre{##1}\stripzeros{\thefield{##1}}}}%
  \cms@mkbibrangetrunc{long}{#1}%
  \endgroup}%

\newrobustcmd*{\letterdatelong}{% For users: 17th ed.
  \ifboolexpr{%
    test {\ifdefstring{\blx@languagename}{american}}%
    or
    test {\ifdefstring{\blx@languagename}{english}}% i.e., no babel
  }%
  {\iftoggle{cms@switchdates}%
    {\cms@datelong{}}%
    {\cms@datelong{orig}}}%
  {\iftoggle{cms@switchdates}%
    {\printdate}%
    {\printorigdate}}}%

\newbibmacro*{journal+sub}{%
  \iffieldundef{journaltitle}%
    {}%
    {\ifboolexpr{%
        not test {\iffieldundef{shortjournal}}%
        and
        ((
        test {\ifcitation}%
        and
        togl {cms@citejtabb}%
        )
        or
        (
        test {\ifbibliography}%
        and
        togl {cms@bibjtabb}%
        ))
      }%
      {\clearlist{location}\printtext[shortjournal]{%
          \printfield[jtsnoformat]{shortjournal}}}%
      {\printtext[journaltitle]{%
          \printfield[jtnoformat]{journaltitle}%
          \setunit{\subtitlepunct}%
          \printfield[sjtnoformat]{journalsubtitle}}}}}%

\newbibmacro*{chap+pag}{%
  \printfield{chapter}%
  \setunit*{\addcomma\space}%
  \printfield{pages}}

\newbibmacro*{mag+news+date}{%
  \ifboolexpr{%
    (
    test {\ifnameundef{author}}%
    or
    not test {\ifuseauthor}%
    )
    and
    not togl {cms@origpublished}%
  }%
  {\usebibmacro{date+issue}}%
  {\usebibmacro{mag+date+issue}}}

\newbibmacro*{date+issue}{%
  \iffieldundef{issue}%
  {\iffieldundef{number}%
    {\usebibmacro{date}}%
    {\iftoggle{cms@numbermonth}%
      {\usebibmacro{date}}%
      {\usebibmacro{cmsyear}}%
      \setunit*{\addcomma\addspace}% Starred version for when the 
      \printfield[journum]{number}}}% month isn't printed because of
  {\printfield{issue}% the toggle.
    \setunit{\addspace}%
    \usebibmacro{cmsyear}}}

\newbibmacro*{mag+date+issue}{%
  \usebibmacro{journal+sub}%
  \setunit*{\addspace}%
  \printlist[periodplace]{location}%
  \newcunit\newblock
  \printfield{usera}% For network ID and possible section of newspaper.
  \newcunit\newblock
  \usebibmacro{date+issue}}

\newbibmacro*{cmsyear}{%
  \iftoggle{cms@switchdates}%
  {\clearfield{extradate}\printorigdateextra}% \printfield{origyear}
  {\clearfield{extradate}\printdateextra}}% \printfield{year}

\newbibmacro*{cmsorigdate}{% New for 0.9
  \iftoggle{cms@switchdates}%
  {\printdate}%
  {\printorigdate}}

\newbibmacro*{cperiodical+ser+vol+num}{% For periodical entries,
  \ifboolexpr{% article subtype
    togl {cms@citejtabb}%
    and
    test {\ifcitation}%
    and
    not test {\iffieldundef{shorttitle}}%
  }%
  {\clearlist{location}\printtext[shorttitle]{%
      \printfield[tnoformat]{shorttitle}}}%
  {\ifboolexpr{%
      togl {cms@bibjtabb}%
      and
      test {\ifbibliography}%
      and
      not test {\iffieldundef{shorttitle}}%
    }%
    {\clearlist{location}\printtext[shorttitle]{%
        \printfield[tnoformat]{shorttitle}}}%
    {\printtext[title]{%
        \printfield[tnoformat]{title}%
        \setunit{\subtitlepunct}%
        \printfield[stnoformat]{subtitle}}}}%
  \setunit*{\addspace}%
  \printlist[periodplace]{location}%
  \setunit*{\addspace}%
  \iffieldundef{series}%
  {}%
  {\newcunit
    \printfield[jourser]{series}%
    \newcunit}%\setunit*{\addspace}?
  \ifthenelse{\iffieldundef{year}\AND\iffieldundef{month}\AND%
    \iffieldundef{issue}\AND\iffieldundef{yeardivision}}% New, more accurate
  {\iffieldundef{volume}% test, also in article
    {\newcunit%
      \printfield[journum]{number}%
      \clearfield{number}%
      \setunit{\addcomma\addspace}}%
    {\printfield[jourvol]{volume}%
      \setunit{\addcomma\addspace}}}%
  {\printfield[jourvol]{volume}%
    \setunit{\addcomma\addspace}% need * here?
    \printfield[journum]{number}%
    \clearfield{number}%
    \setunit{\addcomma\addspace}}%
  \printfield{eid}%
  \newunit}

\newbibmacro*{periodical+date+issue}{% For periodical type &
  \ifboolexpr{% magazine subtype
    togl {cms@citejtabb}%
    and
    test {\ifcitation}%
    and
    not test {\iffieldundef{shorttitle}}%
  }%
  {\clearlist{location}\printtext[shorttitle]{%
      \printfield[tnoformat]{shorttitle}}}%
  {\ifboolexpr{%
      togl {cms@bibjtabb}%
      and
      test {\ifbibliography}%
      and
      not test {\iffieldundef{shorttitle}}%
    }%
    {\clearlist{location}\printtext[shorttitle]{%
        \printfield[tnoformat]{shorttitle}}}%
    {\printtext[title]{%
        \printfield[tnoformat]{title}%
        \setunit{\subtitlepunct}%
        \printfield[stnoformat]{subtitle}}}}%
  \setunit*{\addspace}%
  \printlist[periodplace]{location}%
  \newcunit\newblock
  \printfield{usera}% For network ID and possible section of newspaper.
  \newcunit\newblock
  \usebibmacro{date+issue}}

\renewbibmacro*{postnote}{%
  \iffieldundef{postnote}%
  {}%
  {\setunit{\postnotewrapper}%delim}%
  \printfield{postnote}}}

\newbibmacro*{part+editor+translator}{%
  \ifnameundef{namea}%
  {\ifnameundef{nameb}%
    {}%
    {\bibstring{bytranslator}\space%
      \printnames[bytranslator]{nameb}}}%
  {\ifthenelse{\iffieldundef{nameatype}\OR%
      \iffieldequalstr{nameatype}{editor}}%
    {\ifnamesequal{namea}{nameb}%
      {\bibstring{byeditortr}\space%
        \printnames[byeditor]{namea}}%
      {\bibstring{byeditor}\space%
        \printnames[byeditor]{namea}%
        \ifnameundef{nameb}%
        {}%
        {\newunit
          \bibstring{bytranslator}\space%
          \printnames[bytranslator]{nameb}}}}%
    {\usebibmacro{bytypestrg}{namea}{editor}%
      \setunit{\addspace}%
      \printnames[byeditor]{namea}%
      \ifnameundef{nameb}%
      {}%
      {\newunit%
        \bibstring{bytranslator}\addspace%
        \printnames[bytranslator]{nameb}}}}}

\newbibmacro*{compilestrg}{%
  \ifthenelse{\value{namec}>1\OR\ifandothers{namec}}%
  {\bibstring{compilers}}%
  {\bibstring{compiler}}%
  \clearname{namec}}

\newbibmacro*{transstrg}{%
  \ifthenelse{\value{translator}>1\OR\ifandothers{translator}}%
    {\ifnamesequal{translator}{namec}%
      {\bibstring{transcompilers}%
        \clearname{namec}}%
      {\bibstring{translators}}}%
    {\ifnamesequal{translator}{namec}%
      {\bibstring{transcompiler}%
        \clearname{namec}}%
      {\bibstring{translator}}}%
    \clearname{translator}}

\newbibmacro*{parttransstrg}{%
  \ifthenelse{\value{nameb}>1\OR\ifandothers{nameb}}%
    {\ifnamesequal{nameb}{namec}%
      {\bibstring{transcompilers}%
        \clearname{namec}}%
      {\bibstring{translators}}}%
    {\ifnamesequal{nameb}{namec}%
      {\bibstring{transcompiler}%
        \clearname{namec}}%
      {\bibstring{translator}}}%
    \clearname{nameb}}

\newbibmacro*{editstrg}{% Test added for 0.9
  \ifthenelse{\iffieldundef{editortype}\OR
    \iffieldequalstr{editortype}{editor}}%
  {\ifthenelse{\value{editor}>1\OR\ifandothers{editor}}%
    {\ifthenelse{\ifnamesequal{editor}{translator}\AND
        \ifnamesequal{editor}{namec}}%
      {\bibstring{editortranscompilers}%
        \clearname{translator}%
        \clearname{namec}}%
      {\ifnamesequal{editor}{namec}%
        {\bibstring{editorcompilers}%
          \clearname{namec}}%
        {\ifnamesequal{editor}{translator}%
          {\bibstring{editortranslators}%
            \clearname{translator}}%
          {\bibstring{editors}}}}}%
    {\ifthenelse{\ifnamesequal{editor}{translator}\AND
        \ifnamesequal{editor}{namec}}%
      {\bibstring{editortranscompiler}%
        \clearname{translator}%
        \clearname{namec}}%
      {\ifnamesequal{editor}{namec}%
        {\bibstring{editorcompiler}%
          \clearname{namec}}%
        {\ifnamesequal{editor}{translator}%
          {\bibstring{editortranslator}%
            \clearname{translator}}%
          {\bibstring{editor}}}}}}%
  {\ifthenelse{\value{editor}>1\OR\ifandothers{editor}}%
    {\bibstring{\thefield{editortype}s}}%
    {\bibstring{\thefield{editortype}}}}%
  \clearname{editor}}

\newbibmacro*{parteditstrg}{%
  \ifthenelse{\iffieldundef{nameatype}\OR%
    \iffieldequalstr{nameatype}{editor}}%
  {\ifthenelse{\value{namea}>1\OR\ifandothers{namea}}%
    {\ifthenelse{\ifnamesequal{namea}{nameb}\AND
        \ifnamesequal{namea}{namec}}%
      {\bibstring{editortranscompilers}%
        \clearname{nameb}%
        \clearname{namec}}%
      {\ifnamesequal{namea}{namec}%
        {\bibstring{editorcompilers}%
          \clearname{namec}}%
        {\ifnamesequal{namea}{nameb}%
          {\bibstring{editortranslators}%
            \clearname{nameb}}%
          {\bibstring{editors}}}}}%
    {\ifthenelse{\ifnamesequal{namea}{nameb}\AND
        \ifnamesequal{namea}{namec}}%
      {\bibstring{editortranscompiler}%
        \clearname{nameb}%
        \clearname{namec}}%
      {\ifnamesequal{namea}{namec}%
        {\bibstring{editorcompiler}%
          \clearname{namec}}%
        {\ifnamesequal{namea}{nameb}%
          {\bibstring{editortranslator}%
            \clearname{nameb}}%
          {\bibstring{editor}}}}}}%
  {\ifthenelse{\value{namea}>1\OR\ifandothers{namea}}%
    {\bibstring{\thefield{nameatype}s}}%
    {\bibstring{\thefield{nameatype}}}}%
  \clearname{namea}}

\newbibmacro*{clearpublin}{%
  \clearname{author}\clearfield{shorthand}%
  \ifthenelse{\ifentrytype{collection}\OR\ifentrytype{proceedings}\OR%
    \ifentrytype{mvcollection}\OR\ifentrytype{mvproceedings}}%
  {}%
  {\clearname{namea}%
    \clearname{nameb}}%
  \clearfield{nameaddon}%
  \ifthenelse{\(\ifentrytype{periodical}\OR\ifentrytype{mvbook}\OR%
    \ifentrytype{mvcollection}\OR\ifentrytype{mvproceedings}\OR%
    \ifentrytype{mvreference}\OR\ifentrytype{proceedings}\OR%
    \ifentrytype{collection}\OR\ifentrytype{reference}\OR%
    \ifentrytype{suppbook}\OR\ifentrytype{suppcollection}\)\OR%
    \(\(\ifentrytype{audio}\OR\ifentrytype{music}\OR%
    \ifentrytype{video}\)\AND\iffieldundef{booktitle}\)}%
  {}%
  {\clearfield{title}%
    \clearfield{subtitle}%
    \clearfield{titleaddon}%
    \clearfield{usere}%
    \clearlist{language}%
    \ifentrytype{music}%
    {\clearfield{note}\clearfield{howpublished}\clearfield{eventyear}%
      \clearfield{eventmonth}}%
    {}}%
  \ifentrytype{letter}%
  {\iftoggle{cms@switchdates}%
    {\clearfield{year}\clearfield{month}\clearfield{yeardivision}}%
    {\clearfield{origyear}\clearfield{origmonth}\clearfield{origyeardivision}}%
    \clearfield{origlocation}}%
  {}%
  \clearfield{reprinttitle}%
}

%%%% Related macros from biblatex.def %%%%

\renewbibmacro*{related:origpubas}[1]{%
  \entrydata*{#1}{%
    \usebibmacro{at+every+item}%
    \usebibmacro{cite:origfull}}}%

\renewbibmacro*{related:reprintfrom}[1]{%
  \entrydata*{#1}{%
    \usebibmacro{at+every+item}%
    \nopunct% ???
    \usebibmacro{cite:origpubl}}}%

\renewbibmacro*{related:origpubin}[1]{%
  \entrydata*{#1}{%
    \usebibmacro{at+every+item}%
    \usebibmacro{cmsyear}%
    \ifboolexpr{%
      test {\iflistsequal{publisher}{savedpublisher}}%
      or
      test {\iflistundef{publisher}}%
    }%
      {}%
      {\setunit{\addspace\bibstring[\mkrelatedstringtext]{bypublisher}\space}%
       \printlist{publisher}%
       \setunit{\addcomma\space}%
       \iflistsequal{location}{savedlocation}%
         {}%
         {\printlist{location}}}}}

\renewbibmacro*{related:bytranslator}[1]{%
  \entrydata{#1}{%
    \usebibmacro{at+every+item}%
    \renewbibmacro*{name:hook}[1]{%
      \ifnumequal{\value{listcount}}{1}%
      {\begingroup
        \mkrelatedstringtext{%
          \lbx@initnamehook{##1}}%
        \endgroup}%
      {}}%
    \printnames[bytranslator]{translator}%
    \setunit*{\addspace\bibstring[\mkrelatedstringtext]{astitle}\space}%
    \clearname{translator}%
    \usebibmacro{cite:origfull}}}%

\renewbibmacro*{related:default}[1]{%
  \entrydata*{#1}{%
    \usedriver
    {\ifnameundef{savedauthor}%
      {\ifnameundef{savededitor}%
        {}%
        {\ifnamesequal{editor}{savededitor}%
          {\clearname{editor}}%
          {}}}%
      {\ifnamesequal{author}{savedauthor}%
        {\clearname{author}}%
        {}}%
      \usebibmacro{at+every+item}%
      \renewbibmacro*{cmsbibsortdate}{}%
      \renewbibmacro*{related:init}{}%
      \DeclareNameAlias{sortname}{default}%
      \renewbibmacro*{pageref}{}}%
    {\thefield{entrytype}}}}

\letbibmacro*{related:maintitlenc}{related:multivolume}% From N & B
\letbibmacro*{related:maintitle}{related:multivolume}%

\renewbibmacro*{related:multivolume}[1]{%
  \entrydata*{#1}{%
    \usebibmacro{at+every+item}%
    \ifthenelse{\iffieldundef{volume}\AND\iffieldundef{part}}%
    {}%
    {\printtext{%
        \printfield{volume}%
        \printfield{part}}%
      \setunit{\addcolon\space}}%
    \usebibmacro{italtitle+stitle}%
    \ifboolexpr{%
      test {\ifnamesequal{author}{savedauthor}}%
      or
      test {\ifnameundef{author}}%
    }%
    {}%
    {\usebibmacro{bytypestrg}{author}{author}%
      \setunit{\addspace}%
      \printnames[byauthor]{author}%
      \newcunit\newblock}%
    \ifboolexpr{%
      test {\ifnamesequal{namea}{savednamea}}%
      or
      test {\ifnameundef{namea}}%
    }%
    {\ifboolexpr{%
        test {\ifnamesequal{editor}{savededitor}}%
        or
        test {\ifnameundef{editor}}%
      }%
      {\ifboolexpr{%
          test {\ifnamesequal{nameb}{savednameb}}%
          or
          test {\ifnameundef{nameb}}%
        }%
        {}%
        {\bibstring{bytranslator}\addspace%
          \printnames[bytranslator]{nameb}\newcunit}}%
      {\usebibmacro{byeditor+others}%
        \newcunit\newblock}}%
    {\usebibmacro{part+editor+translator}%
      \newcunit}%
    \usebibmacro{date}}}%

% \providetoggle{cms@otherlang}

% \apptocmd\blx@opt@autolang@other%
% {\toggletrue{cms@otherlang}}%
% {\blx@info@noline{Patching 'autolang=other' option}}%
% {\PackageWarningNoLine{biblatex-chicago}%
%   {Upgrading biblatex to >v2.7a recommended,\MessageBreak
%     especially if you are using the "related"\MessageBreak
%     functionality}}

\newbibmacro*{cite:origfull}{%
  \printtext[cmshypertarget]{%
    \usedriver
    {\DeclareNameAlias{sortname}{default}\clearfield{postnote}%
      \clearname{author}\clearfield{userf}\clearfield{shorthand}%
      \toggletrue{cms@headlessnote}\frenchspacing%
      \renewbibmacro*{cmsbibsortdate}{}}%
    {\thefield{entrytype}}}}

\newbibmacro*{cite:origpubl}{%
  \printtext[cmshypertarget]{%
    \usedriver
    {\DeclareNameAlias{sortname}{default}\clearfield{postnote}%
      \usebibmacro{clearpublin}\clearfield{shorthand}%
      \toggletrue{cms@headlessnote}\toggletrue{cms@origpublished}%
      \frenchspacing\renewbibmacro*{cmsbibsortdate}{}%
      \usebibmacro{cms:titlehook}}%
    {\thefield{entrytype}}}}

\newbibmacro*{at+every+item}{%
  \iffieldequalstr{pubstate}{reprint}%
  {\toggletrue{cms@reprint}}%
  {\togglefalse{cms@reprint}}%
  \ifboolexpr{% AD requires this here because cmsbibsortdate=null
    test {\iffieldundef{origyear}}%
    or
    test {\iffieldundef{year}}%
    or
    not test {\iffieldint{year}}%
    or
    togl {cms@switchdates}%
  }%
  {}%
  {\ifboolexpr{% Needed for open-ended ranges
      test {\iffieldundef{endyear}}%
      or
      not test {\iffieldnum{endyear}}%
    }%
    {\ifthenelse{\thefield{origyear}>\thefield{year}}%
      {\toggletrue{cms@switchdates}}%
      {}}%
    {\ifthenelse{\thefield{origyear}>\thefield{endyear}}%
      {\toggletrue{cms@switchdates}}%
      {}}}}%

\endinput
