% Copyright (c) 2009-2022 David Fussner.  This package is
% author-maintained.
%
% This work may be copied, distributed and/or modified under the
% conditions of the LaTeX Project Public License, either version 1.3 of
% this license or (at your option) any later version.  The latest
% version of this license is in http://www.latex-project.org/lppl.txt
% and version 1.3 or later is part of all distributions of LaTeX version
% 2005/12/01 or later.  This software is provided as is,
% without warranty of any kind, either expressed or implied, including,
% but not limited to, the implied warranties of merchantability and
% fitness for a particular purpose.

\ProvidesPackage{biblatex-chicago}[2022/07/02 v 3.18 biblatex style]

\RequirePackage{etoolbox}
\RequirePackage{nameref}% this already loads kvoptions
\RequirePackage{xstring}

\SetupKeyvalOptions{% Use kvoptions to set biblatex load-time opts
  family=cms@ldt,
  prefix=cms@ldt@}

\DeclareVoidOption{authordate}{\def\cms@style{authordate}}
\DeclareVoidOption{notes}{\def\cms@style{notes}}
\DeclareVoidOption{authordate-trad}{\def\cms@style{authordatetrad}}
\DeclareVoidOption{authordate16}{\def\cms@style{authordateold}}
\DeclareVoidOption{notes16}{\def\cms@style{notesold}}
\DeclareVoidOption{authordate-trad16}{\def\cms@style{authordatetrold}}
\DeclareDefaultOption{\eappto\cms@options{\CurrentOption,}}
\let\cms@options\empty
\setkeys{cms@ldt}{notes}%

\newtoggle{cms@nomark}
\newtoggle{cms@urlbreak}

\DeclareVoidOption{footmarkoff}{\global\toggletrue{cms@nomark}}
\DeclareVoidOption{cmsbreakurl}{\global\toggletrue{cms@urlbreak}}

\DeclareStringOption{natbib}[true]
\DeclareStringOption{mcite}[true]
\DeclareStringOption{casechanger}[auto]
\DeclareStringOption{backend}[biber]
\DeclareStringOption{datamodel}
\DeclareStringOption{cmsnameparts}[western]

\ProcessLocalKeyvalOptions{cms@ldt}

\ifdefvoid{\cms@ldt@backend}%
{\PassOptionsToPackage{backend=biber}{biblatex}}%
{\PassOptionsToPackage{backend=\cms@ldt@backend}{biblatex}%
  \ifdefstring{\cms@ldt@backend}{biber}%
  {}%
  {\def\blx@sorting@cms{1}}}

\ifx\cms@ldt@datamodel\@empty
\else
\PassOptionsToPackage{datamodel=\cms@ldt@datamodel}{biblatex}
\fi
\ifx\cms@ldt@mcite\@empty
\else
\PassOptionsToPackage{mcite=\cms@ldt@mcite}{biblatex}
\fi
\ifx\cms@ldt@natbib\@empty
\else
\PassOptionsToPackage{natbib=\cms@ldt@natbib}{biblatex}
\fi
\ifx\cms@ldt@casechanger\@empty
\else
\PassOptionsToPackage{casechanger=\cms@ldt@casechanger}{biblatex}
\fi
\ifx\cms@ldt@cmsnameparts\@empty
\else
\PassOptionsToPackage{datamodel=cms}{biblatex}
\fi

\def\cms@authordate{%
  \RequirePackage[style=chicago-authordate]{biblatex}%
  \ExecuteBibliographyOptions{%
    pagetracker=true,autocite=inline,alldates=comp,labeldateparts=true,
    citetracker=true,uniquename=minfull,useeditor=true,usetranslator=true,
    usenamec=true,alltimes=12h,urltime=24h,datecirca=true,datezeros=false,
    dateuncertain=true,timezones=true,compressyears=true,
    ibidtracker=constrict,sorting=cms,punctfont,cmslos=true,nodates,
    uniquelist=minyear,maxbibnames=10,minbibnames=7,sortcase=false,
    abbreviate=false,dateabbrev=false,avdate=true}}

\def\cms@notes{%
  \RequirePackage[style=chicago-notes]{biblatex}%
  \ExecuteBibliographyOptions{%
    pagetracker=true,autocite=footnote,abbreviate=false,alldates=comp,
    citetracker=true,ibidtracker=constrict,usetranslator=true,
    usenamec=true,loccittracker=constrict,dateabbrev=false,
    maxbibnames=10,minbibnames=7,sorting=cms,sortcase=false,
    labeltitle=true,alltimes=12h,urltime=24h,timezones=true,
    datezeros=false,datecirca=true,dateuncertain=true,compressyears=true,
    nodates=true,uniquename=minfull}}

\def\cms@authordatetrad{%
  \RequirePackage[style=chicago-authordate-trad]{biblatex}%
  \ExecuteBibliographyOptions{%
    pagetracker=true,autocite=inline,alldates=comp,labeldateparts=true,
    citetracker=true,uniquename=minfull,useeditor=true,usetranslator=true,
    usenamec=true,alltimes=12h,urltime=24h,datecirca=true,datezeros=false,
    dateuncertain=true,timezones=true,compressyears=true,
    ibidtracker=constrict,sorting=cms,punctfont,cmslos=true,nodates,
    uniquelist=minyear,maxbibnames=10,minbibnames=7,sortcase=false,
    abbreviate=false,dateabbrev=false,avdate=true}}

\def\cms@authordateold{%
  \RequirePackage[style=chicago-authordate16]{biblatex}%
  \ExecuteBibliographyOptions{%
    pagetracker=true,autocite=inline,alldates=comp,labeldateparts=true,
    citetracker=true,uniquename=minfull,useeditor=true,usetranslator=true,
    usenamec=true,alltimes=12h,urltime=24h,datecirca=true,datezeros=false,
    dateuncertain=true,timezones=true,compressyears=true,
    ibidtracker=constrict,sorting=cms,punctfont,cmslos=true,nodates,
    uniquelist=minyear,maxbibnames=10,minbibnames=7,sortcase=false,
    abbreviate=false,dateabbrev=false,avdate=true}}

\def\cms@notesold{%
  \RequirePackage[style=chicago-notes16]{biblatex}%
  \ExecuteBibliographyOptions{%
    pagetracker=true,autocite=footnote,abbreviate=false,alldates=comp,
    citetracker=true,ibidtracker=constrict,usetranslator=true,
    usenamec=true,loccittracker=constrict,dateabbrev=false,
    maxbibnames=10,minbibnames=7,sorting=cms,sortcase=false,
    labeltitle=true,alltimes=12h,urltime=24h,timezones=true,
    datezeros=false,datecirca=true,dateuncertain=true,compressyears=true,
    nodates=true,uniquename=minfull}}

\def\cms@authordatetrold{%
  \RequirePackage[style=chicago-authordate-trad16]{biblatex}%
  \ExecuteBibliographyOptions{%
    pagetracker=true,autocite=inline,alldates=comp,labeldateparts=true,
    citetracker=true,uniquename=minfull,useeditor=true,usetranslator=true,
    usenamec=true,alltimes=12h,urltime=24h,datecirca=true,datezeros=false,
    dateuncertain=true,timezones=true,compressyears=true,
    ibidtracker=constrict,sorting=cms,punctfont,cmslos=true,nodates,
    uniquelist=minyear,maxbibnames=10,minbibnames=7,sortcase=false,
    abbreviate=false,dateabbrev=false,avdate=true}}

\csuse{cms@\cms@style}
\undef\cms@authordate
\undef\cms@notes
\undef\cms@authordatetrad
\undef\cms@authordateold
\undef\cms@notesold
\undef\cms@authordatetrold
\expandafter\ExecuteBibliographyOptions\expandafter{\cms@options}

\setlength{\bibitemsep}{0.5\baselineskip plus 0.5\baselineskip}
\setlength{\bibhang}{2em}
\setlength{\lositemsep}{0.25\baselineskip plus 0.25\baselineskip}

\urlstyle{rm}
\setcounter{biburllcpenalty}{5000}
\setcounter{biburlucpenalty}{9000}
\setcounter{biburlnumpenalty}{9000}
\setcounter{mincomprange}{100}%
\setcounter{mincompwidth}{10}%

\renewcommand*{\bibnamedash}{\rule[.4ex]{3em}{.6pt}}

\iftoggle{cms@urlbreak}
{\renewcommand*{\biburlsetup}{%
    \Urlmuskip\biburlbigskip
    \mathchardef\UrlBigBreakPenalty=\value{biburlbigbreakpenalty}\relax
    \mathchardef\UrlBreakPenalty=\value{biburlbreakpenalty}\relax
    \def\UrlBigBreaks{\do\:}%
    \def\UrlBreaks{%
      \do\@\do\\\do\!\do\|\do\;\do\>\do\]\do\)\do\}%
    \do\'\do\+\do\=\do\$\do\&\do\*\do\^\do\"}%
  \ifnumgreater{\value{biburlnumpenalty}}{0}
  {\def\do##1{%
      \appto\UrlSpecials{%
        \do##1{%
          \mathchar`##1
          \mskip\biburlnumskip
          \penalty\value{biburlnumpenalty}}}}%
    \do\1\do\2\do\3\do\4\do\5\do\6\do\7\do\8\do\9\do\0}
  {}%
  \ifnumgreater{\value{biburlucpenalty}}{0}
  {\def\do##1{%
      \appto\UrlSpecials{%
        \do##1{%
          \mathchar`##1
          \mskip\biburlucskip
          \penalty\value{biburlucpenalty}}}}%
    \do\A\do\B\do\C\do\D\do\E\do\F\do\G\do\H\do\I\do\J
    \do\K\do\L\do\M\do\N\do\O\do\P\do\Q\do\R\do\S\do\T
    \do\U\do\V\do\W\do\X\do\Y\do\Z}
  {}%
  \ifnumgreater{\value{biburllcpenalty}}{0}
  {\def\do##1{%
      \appto\UrlSpecials{%
        \do##1{%
          \mathchar`##1
          \mskip\biburllcskip
          \penalty\value{biburllcpenalty}}}}%
    \do\a\do\b\do\c\do\d\do\e\do\f\do\g\do\h\do\i\do\j
    \do\k\do\l\do\m\do\n\do\o\do\p\do\q\do\r\do\s\do\t
    \do\u\do\v\do\w\do\x\do\y\do\z}
  {}%
  \let\do=\noexpand}}%
{}

\iftoggle{cms@nomark}
{}
{\@ifclassloaded{memoir}%
  {\blx@warning@noline{%
      Since you are using the 'memoir' class,\MessageBreak
      I'm leaving the formatting of the foot- and/or\MessageBreak
      end-note mark and text to you.}}%
  {\renewcommand\@makefntext[1]{% Provides in-line footnote marks
      \setlength\parindent{1em}%
      \noindent
      \makebox[2.3em][r]{\@thefnmark.\,\,}#1}
    \@ifpackageloaded{endnotes}% Provides in-line endnote marks
    {\def\enotesize{\small}% This size recommended by the Manual
      \renewcommand{\enoteformat}{%
        \renewcommand{\makeenmark}{%
          \hbox{\theenmark.\,\,}}
        \rightskip\z@ \leftskip\z@ \parindent=2.3em
        \leavevmode\llap{\makeenmark}}}
    {}}}

\@ifpackageloaded{babel}
{\ifthenelse{\equal{\languagename}{american}}
  {\DeclareLanguageMapping{american}{cms-american}}%
  {\ifthenelse{\equal{\languagename}{english}}%
    {\DeclareLanguageMapping{english}{cms-american}}%
    {\DeclareLanguageMapping{american}{cms-american}}}}%
{\DeclareLanguageMapping{english}{cms-american}}

\DeclareLanguageMapping{brazilian}{cms-brazilian}
\DeclareLanguageMapping{british}{cms-british}
\DeclareLanguageMapping{dutch}{cms-dutch}
\DeclareLanguageMapping{german}{cms-german}
\DeclareLanguageMapping{french}{cms-french}
\DeclareLanguageMapping{finnish}{cms-finnish}
\DeclareLanguageMapping{ngerman}{cms-ngerman}
\DeclareLanguageMapping{icelandic}{cms-icelandic}
\DeclareLanguageMapping{norsk}{cms-norsk}
\DeclareLanguageMapping{norwegian}{cms-norwegian}
\DeclareLanguageMapping{nynorsk}{cms-nynorsk}
\DeclareLanguageMapping{romanian}{cms-romanian}
\DeclareLanguageMapping{spanish}{cms-spanish}
\DeclareLanguageMapping{swedish}{cms-swedish}

\ifundef\bbl@loaded{\let\bbl@loaded\@empty}{}% For old versions of babel

\patchcmd{\blx@comprange@check}% Audrey Boruvka's code from StackExchange
{\blx@comprange@comp{#1}{#2}}% Moved here from cbx
{\blx@tempcnta=#1
  \divide\blx@tempcnta100
  \multiply\blx@tempcnta100
  \ifnumequal{\blx@tempcnta}{#1}
  {\blx@range@out@value{#1\bibrangedash#2}}
  {\blx@comprange@comp{#1}{#2}}}
{}{}%

\newrobustcmd*{\suppressbibfield}[2][]{%
  \ifblank{#1}%
  {\ifblank{#2}%
    {}%
    {\DeclareSourcemap{
        \maps[datatype=bibtex]{
          \map{
            \renewcommand*{\do}[1]{
              \step[fieldset=##1, null]}\docsvlist{#2}}}}}}%
  {\ifblank{#2}%
    {\DeclareSourcemap{
        \maps[datatype=bibtex]{
          \map{
            \renewcommand*{\do}[1]{%
              \pertype{##1}}\docsvlist{#1}%
            \step[entrynull]}}}}%
    {\DeclareSourcemap{
        \maps[datatype=bibtex]{
          \map{
            \renewcommand*{\do}[1]{%
              \pertype{##1}}\docsvlist{#1}%
            \renewcommand*{\do}[1]{%
              \step[fieldset=##1, null]}\docsvlist{#2}}}}}}}

\endinput
