%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% historian.bbx, v0.4, 2010/08/22
% A bibliography style for use with biblatex
% Developed and maintained by Sander Gliboff, 
% based on guidelines from the Turabian Manual for Writers, 7th ed.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\ProvidesFile{historian.bbx}[2010/08/22 v0.4 historian bibliography style]

   \@ifpackagelater{biblatex}{2010/08/04}
     {}
     {\PackageError{biblatex}
	{Outdated 'biblatex' package
	The 'historian' style requires biblatex v0.9b or later.\MessageBreak
	 You are using: '\csuse{ver@biblatex.sty}'.\MessageBreak
	 This is a fatal error.}%
      \endinput}

%%%%% BIBLIOGRAPHY ENVIRONMENTS AND PARAMETERS %%%%%

%This style is based on verbose-inote.bbx and authortitle.bbx, from which the following macros are taken (with small modifications):

\RequireBibliographyStyle{standard}
%\ExecuteBibliographyOptions{pagetracker}%options all in cbx file

\DeclareBibliographyOption{dashed}[true]{%
  \ifstrequal{#1}{true}
    {\ExecuteBibliographyOptions{pagetracker}%
     \renewbibmacro*{bbx:savehash}{\savefield{fullhash}{\bbx@lasthash}}}
    {\ExecuteBibliographyOptions{pagetracker=false}%
     \renewbibmacro*{bbx:savehash}{}}}

\DeclareFieldFormat{shorthandwidth}{#1}
\setlength{\bibitemsep}{0pt}

\DeclareNameAlias{author}{sortname}
\DeclareNameAlias{editor}{sortname}
\DeclareNameAlias{translator}{sortname}

\defbibenvironment{bibliography}
  {\list
     {}
     {\setlength{\leftmargin}{\bibhang}%
      \setlength{\itemindent}{-\leftmargin}%
      \setlength{\itemsep}{\bibitemsep}%
      \setlength{\parsep}{\bibparsep}}}
  {\endlist}
  {\item}

\defbibenvironment{shorthands}
  {\toggletrue{losflag}
  \list
     {\printfield[shorthandwidth]{shorthand}}
     {\setlength{\labelwidth}{\shorthandwidth}%
      \setlength{\leftmargin}{\labelwidth}%
      \setlength{\labelsep}{\biblabelsep}%
      \addtolength{\leftmargin}{\labelsep}%
      \setlength{\itemsep}{\bibitemsep}%
      \setlength{\parsep}{\bibparsep}%
      \renewcommand*{\makelabel}[1]{##1\hss}}}
  {\endlist}
  {\item}

\InitializeBibliographyStyle{%
  \global\undef\bbx@lasthash}

\newbibmacro*{bbx:savehash}{%
  \savefield{fullhash}{\bbx@lasthash}}

\newbool{bbx@inset}
\DeclareBibliographyDriver{set}{%
  \booltrue{bbx@inset}%
  \entryset{}{}%
  \finentry}

%\renewbibmacro*{author}{% Redefined extensively below
%  \ifthenelse{\ifuseauthor\AND\NOT\ifnameundef{author}}
%    {\ifthenelse{\iffieldequals{fullhash}{\bbx@lasthash}\AND
%                 \NOT\iffirstonpage\AND
%		 		\(\NOT\boolean{bbx@inset}\OR
%		   \iffieldequalstr{entrysetcount}{1}\)}
%       {\bibnamedash\setunit{\addcomma\addspace}}%Changed from authortitle to insert comma after namedash
%       {\printnames{author}%
%	\setunit{\addcomma\space}%
%        \usebibmacro{bbx:savehash}}%
%     \usebibmacro{authorstrg}}
%    {\global\undef\bbx@lasthash}}

\renewbibmacro*{editor}{%
  \usebibmacro{bbx:editor}{editorstrg}}
\renewbibmacro*{editor+others}{%
  \usebibmacro{bbx:editor}{editor+othersstrg}}
\newbibmacro*{bbx:editor}[1]{%
  \ifthenelse{\ifuseeditor\AND\NOT\ifnameundef{editor}}
    {\ifthenelse{\iffieldequals{fullhash}{\bbx@lasthash}\AND
                 \NOT\iffirstonpage\AND
		 \(\NOT\boolean{bbx@inset}\OR
		   \iffieldequalstr{entrysetcount}{1}\)}
       {\bibnamedash\setunit{\addcomma\addspace}}%Changed from authortitle to insert comma after namedash
       {\printnames{editor}%
	\setunit{\addcomma\space}%
        \usebibmacro{bbx:savehash}}%
     \usebibmacro{#1}%
     \clearname{editor}}
    {\global\undef\bbx@lasthash}}

\renewbibmacro*{translator}{%
  \usebibmacro{bbx:translator}{translatorstrg}}
\renewbibmacro*{translator+others}{%
  \usebibmacro{bbx:translator}{translator+othersstrg}}
\newbibmacro*{bbx:translator}[1]{%
  \ifthenelse{\ifusetranslator\AND\NOT\ifnameundef{translator}}
    {\ifthenelse{\iffieldequals{fullhash}{\bbx@lasthash}\AND
                 \NOT\iffirstonpage\AND
		 \(\NOT\boolean{bbx@inset}\OR
		   \iffieldequalstr{entrysetcount}{1}\)}
       {\bibnamedash\setunit{\addcomma\addspace}}%Changed from authortitle to insert comma after namedash
       {\printnames{translator}%
	\setunit{\addcomma\space}%
        \usebibmacro{bbx:savehash}}%
     \usebibmacro{#1}%
     \clearname{translator}}
    {\global\undef\bbx@lasthash}}
    

%%%%% TOGGLES AND VARIABLES %%%%%%%%%%%%%%%%%%%%%%%%%%

%Flag set in .cbx file when there appears to be a page range in the postnote
%(used for suppressing the pages field of articles in the footnotes)
\newtoggle{printpagerange}
\toggletrue{printpagerange}

%Set in the .cbx file by the footciteurllast and citerurllast commands
\newtoggle{urllastflag}
\togglefalse{urllastflag}

%used when crossreferencing from customd:
\newtoggle{printpages}
\newtoggle{printvolume}

%used for flagging repeating author names in bibliography
\newtoggle{namedashflag}
\togglefalse{namedashflag}

%Used for flagging when volume numbers or maintitles have alread been printed
\newtoggle{volumeprinted}
\togglefalse{volumeprinted}
\newtoggle{maintitleprinted}
\togglefalse{maintitleprinted}

%Used for flagging books that are reprint editions or translations, with original publication data
\newtoggle{origdataflag}
\newcommand\reprintoption{\optionnoreprints}

%Used for flagging crossreferences from customd or letter
\newtoggle{xrefflag}\togglefalse{xrefflag}


%Flags set by package and entry options
\newtoggle{printurlsflag}
\newtoggle{printseriesflag}
\newtoggle{shortincollflag}
\newtoggle{addorigflag}\togglefalse{addorigflag}
\newtoggle{addtransfromflag}\togglefalse{addtransfromflag}
\newtoggle{useshorttitles}
\newtoggle{useshortauthors}

%Flag set in list-of-shorthands environment
\newtoggle{losflag}

%Flags used for annotated bibliographies---copied from reading.bbx (new in v. 0.2)
%\newbool{bbx:entrykey}
\newbool{bbx:annotation}
%\newbool{bbx:abstract}
%\newbool{bbx:library}
%\newbool{bbx:file}


%%%%%% PACKAGE AND ENTRY OPTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%

% Define url/doi/eprint options from standard.bbx as EntryOptions as well as BibliographyOptions (New in v.0.3.)

\DeclareEntryOption{url}[true]{%
  \settoggle{bbx:url}{#1}}
\DeclareEntryOption{doi}[true]{%
  \settoggle{bbx:doi}{#1}}
\DeclareEntryOption{eprint}[true]{%
  \settoggle{bbx:eprint}{#1}}
  
% Historian-specific options---see the cbx-file for the citepages options

\DeclareBibliographyOption{printseries}[true]
	{\ifstrequal{#1}{true}
		{\toggletrue{printseriesflag}}%
		{\togglefalse{printseriesflag}}}%
		
\DeclareEntryOption{printseries}[true]
	{\ifstrequal{#1}{true}
		{\toggletrue{printseriesflag}}%
		{\togglefalse{printseriesflag}}}%

\DeclareBibliographyOption{shortincoll}[true]
	{\ifstrequal{#1}{true}
		{\toggletrue{shortincollflag}}%
		{\togglefalse{shortincollflag}}}%
		
\DeclareEntryOption{shortincoll}[true]
	{\ifstrequal{#1}{true}
		{\toggletrue{shortincollflag}}%
		{\togglefalse{shortincollflag}}}%

\DeclareBibliographyOption{reprint}[origfirst]
	{\renewcommand\reprintoption{#1}%
	\ifthenelse{\equal{\reprintoption}{\optionaddoriginal}
		\or\equal{\reprintoption}{\optionorigfirst}
		\or\equal{\reprintoption}{\optiondoubledate}
		\or\equal{\reprintoption}{\optionnoreprints}}
		{%then valid bibliography option
			\relax}%
		{%else not a valid option; don't do reprints at all
			\renewcommand\reprintoption{\optionnoreprints}}%
	}%
	
\DeclareEntryOption{reprint}[origfirst]
	{\renewcommand\reprintoption{#1}%
	\ifthenelse{\equal{\reprintoption}{\optionaddoriginal}
		\or\equal{\reprintoption}{\optionorigfirst}
		\or\equal{\reprintoption}{\optiondoubledate}
		\or\equal{\reprintoption}{\optionnoreprints}
		\or\equal{\reprintoption}{\optiontransfromorig}
		\or\equal{\reprintoption}{\optionorigtransas}}
		{%then valid entry option
			\relax}%
		{%else not a valid option; don't do reprints at all
			\renewcommand\reprintoption{\optionnoreprints}}%
	}%	
			
\DeclareEntryOption{shorttitle}[true]
	{\ifstrequal{#1}{true}
		{\toggletrue{useshorttitles}}%
		{\togglefalse{useshorttitles}}}%
			
\DeclareEntryOption{shortauthor}[true]
	{\ifstrequal{#1}{true}
		{\toggletrue{useshortauthors}}%
		{\togglefalse{useshortauthors}}}%
		
%Option declarations for annotated bibliographies (new in v. 0.2)

\DeclareBibliographyOption{annotation}[true]{%
  \csuse{bool#1}{bbx:annotation}}
\DeclareEntryOption{annotation}[true]{%
  \csuse{bool#1}{bbx:annotation}}
  

			
%%%%% CITE COMMANDS FOR CROSS-REFERENCING %%%%%%%%%%%

%For cross-referencing to collection (or book) in bibliography
\DeclareCiteCommand{\bbx@crosstocoll}[]{}{%
	\toggletrue{xrefflag}%
	\printtext
		{\bibhypertarget{\thefield{entrykey}:\the\value{instcount}}%
		{\iftoggle{shortincollflag}
			{%Then book/collection date are to be abbreviated
				\usebibmacro{getbookinfo-short}}%
			{%Else Print book/collection data in full
				 \usebibmacro{getbookinfo}}}}%
}{}{}%  

%For cross-referencing to periodical in bibliography
\DeclareCiteCommand{\bbx@crosstoper}[]{}{%
	\toggletrue{xrefflag}%
	\printtext
		{\bibhypertarget{\thefield{entrykey}:\the\value{instcount}}%
		{\usebibmacro{journal+issuetitle}}}%
}{}{}%  


%For cross-referencing to reference in bibliography.
%Normally, reference works won't be included in the bibliography, but just in case
%Treated same as xrefs to collections and books
\DeclareCiteCommand{\bbx@crosstoref}[]{}{%
	\toggletrue{xrefflag}%
\printtext
		{\bibhypertarget{\thefield{entrykey}:\the\value{instcount}}%
		{\iftoggle{shortincollflag}
			{\usebibmacro{getbookinfo-short}}%
			{\usebibmacro{getbookinfo}}}}%
}{}{}%  

%For cross-referencing to customa in bibliography
\DeclareCiteCommand{\bbx@crosstoarch}[]{}{%
	\toggletrue{xrefflag}%
	\printtext
		{\bibhypertarget{\thefield{entrykey}:\the\value{instcount}}%
		{\usebibmacro{in:}\usebibmacro{getarchiveinfo}}}%
}{}{}%  


%%%%% BIBLIOGRAPHY CATEGORIES %%%%%%%%%%%
%Certain entrytypes and subtypes need to be omitted or listed separately from the main bibliography. Set up categories for them
\DeclareBibliographyCategory{noteonly} 
\DeclareBibliographyCategory{newspaper}
\DeclareBibliographyCategory{magazine}
\DeclareBibliographyCategory{innewspaper}
\DeclareBibliographyCategory{inmagazine}
\DeclareBibliographyCategory{classic}
\DeclareBibliographyCategory{canonical}
\DeclareBibliographyCategory{biblical}
\DeclareBibliographyCategory{blog}
\DeclareBibliographyCategory{listserv}

%%%%%% PUNCTUATION AND FIELD FORMATS
% Unit punctuation is comma, block punctuation period
\renewcommand\newunitpunct{\addperiod\addspace}%

%Colons before page numbers and between titles and subtitles
\renewcommand{\bibpagespunct}{\addcolon\addspace}%
\renewcommand*{\subtitlepunct}{\addcolon\addspace}%

%...But no colon if title ends in a question mark or exclamation point
%\DefineBibliographyExtras{english}{\DeclarePunctuationPairs{colon}{*}}
\DefineBibliographyExtras{american}{\DeclarePunctuationPairs{colon}{*}}%Should allow colon only after abbreviation dot---doesn't seem to work

%%Put commas, etc. inside quotes
\uspunctuation %This makes sure American-style quote punctuation is enabled

%	Title formatting
% (Titles should have been entered in headline case)

\DeclareFieldFormat{titlecase}{#1}

\DeclareFieldFormat{booktitle}{\mkbibemph{#1}}
\DeclareFieldFormat{issuetitle}{\mkbibquote{#1}}

\DeclareFieldFormat[customd]{title}{%
	\ifthenelse{\equal{\thefield{entrysubtype}}{\subtypebooklike}}%
		{\mkbibemph{#1}}%
		{\mkbibquote{#1}}}%
\DeclareFieldFormat[periodical]{title}{%
	\mkbibemph{#1}}%
\DeclareFieldFormat[article]{title}{%
	\ifthenelse{\equal{\thefield{entrysubtype}}{\subtypepublicdocument}}%
		{\mkbibemph{#1}}%
		{\mkbibquote{#1}}}%
\DeclareFieldFormat[incollection]{title}{%
	\ifthenelse{\equal{\thefield{entrysubtype}}{\subtypebooklike}%
		\or\equal{\thefield{entrysubtype}}{\subtypepublicdocument}}%
		{\mkbibemph{#1}}%
		{\mkbibquote{#1}}}%
\DeclareFieldFormat[inreference]{title}{%
	\ifthenelse{\equal{\thefield{entrysubtype}}{\subtypebooklike}%
		\or\equal{\thefield{entrysubtype}}{\subtypepublicdocument}}%
		{\mkbibemph{#1}}%
		{\mkbibquote{#1}}}%
\DeclareFieldFormat[jurisdiction]{title}{#1}%
\DeclareFieldFormat[legal]{title}{\mkbibemph{#1}}%
\DeclareFieldFormat[legislation]{title}{\mkbibemph{#1}}%
\DeclareFieldFormat[performance]{title}{%
	\ifthenelse{\equal{\thefield{entrysubtype}}{\subtypebooklike}}%
		{\mkbibemph{#1}}%
		{\mkbibquote{#1}}}%
\DeclareFieldFormat[audio]{title}{%
	\ifthenelse{\equal{\thefield{entrysubtype}}{\subtypebooklike}}%
		{\mkbibemph{#1}}%
		{\mkbibquote{#1}}}%
\DeclareFieldFormat[music]{title}{%
	\ifthenelse{\equal{\thefield{entrysubtype}}{\subtypebooklike}}%
		{\mkbibemph{#1}}%
		{\mkbibquote{#1}}}%
\DeclareFieldFormat[movie]{title}{%
	\ifthenelse{\equal{\thefield{entrysubtype}}{\subtypebooklike}}%
		{\mkbibemph{#1}}%
		{\mkbibquote{#1}}}%
\DeclareFieldFormat[video]{title}{%
	\ifthenelse{\equal{\thefield{entrysubtype}}{\subtypeonline}}%
		{\mkbibquote{#1}}%
		{\mkbibemph{#1}}}%
\DeclareFieldFormat[artwork]{title}{%
	\ifthenelse{\equal{\thefield{entrysubtype}}{\subtypebooklike}}%
		{\mkbibemph{#1}}%
		{\mkbibquote{#1}}}%
\DeclareFieldFormat[image]{title}{%
	\ifthenelse{\equal{\thefield{entrysubtype}}{\subtypebooklike}}%
		{\mkbibemph{#1}}%
		{\mkbibquote{#1}}}%
\DeclareFieldFormat[customa]{title}{#1}%
\DeclareFieldFormat{volumetitle}{\mkbibemph{#1}}%
\DeclareFieldFormat{journalasauthor}{\mkbibemph{#1}}%
\DeclareFieldFormat[online]{title}{%
	\ifthenelse{\equal{\thefield{entrysubtype}}{\subtypedatabase}}%
		{{#1}}%
		{\ifthenelse{\equal{\thefield{entrysubtype}}{\subtypebooklike}}%
			{\mkbibemph{#1}}%
			{\mkbibquote{#1}}}}%

%Improvised use of booktitle in non-book media
\DeclareFieldFormat[online]{booktitle}{%
	\ifthenelse{\equal{\thefield{entrysubtype}}{\subtypedatabase}}%
		{{#1}}%
		{\ifthenelse{\equal{\thefield{entrysubtype}}{\subtypebooklike}}%
			{{#1}}%
			{\mkbibemph{#1}}}}%
\DeclareFieldFormat[audio]{booktitle}{%
	\ifthenelse{\equal{\thefield{entrysubtype}}{\subtypebooklike}}%
		{{#1}}%
		{\mkbibemph{#1}}}%
\DeclareFieldFormat[customd]{booktitle}{%
	\ifthenelse{\equal{\thefield{entrysubtype}}{\subtypebooklike}}%
		{{#1}}%
		{\mkbibemph{#1}}}%
		
\DeclareFieldFormat{shorthand}{\normalcolor #1\isdot}
				
%Automatic formatting of fields that need to be capitalized in bibliography, when newunitpunct is period, but not in footnotes

\DeclareFieldFormat{usera}{\ifcapital{\MakeCapital{#1}}{#1}}%
\DeclareFieldFormat{userd}{\ifcapital{\MakeCapital{#1}}{#1}}%
\DeclareFieldFormat{note}{\ifcapital{\MakeCapital{#1}}{#1}}%
\DeclareFieldFormat{eventtitle}{\mkbibquote{\ifcapital{\MakeCapital{#1}}{#1}}}%
\DeclareFieldFormat{addendum}{\ifcapital{\MakeCapital{#1}}{#1}}%
\DeclareFieldFormat{type}{\ifcapital{\MakeCapital{#1}}{#1}}%
\DeclareFieldFormat{library}{\ifcapital{\MakeCapital{#1}}{#1}}%
\DeclareFieldFormat{titleaddon}{\ifcapital{\MakeCapital{#1}}{#1}\isdot}%
\DeclareFieldFormat{booktitleaddon}{\ifcapital{\MakeCapital{#1}}{#1}}%
\DeclareFieldFormat{maintitleaddon}{\ifcapital{\MakeCapital{#1}}{#1}}%
\DeclareFieldFormat{edition}{%
  \ifinteger{#1}
    {\mkbibordinal{#1}~\bibstring{edition}}%
    {\ifcapital{\MakeCapital{#1}}{#1}}\isdot}%
\DeclareFieldFormat{howpublished}{\ifcapital{\MakeCapital{#1}}{#1}}%
%\DeclareFieldFormat{pubstate}{\ifcapital{\MakeCapital{#1}}{#1}}% 

% Get rid of default page prefix strings ``p.'' or ``pp.'' in pages field and postnote
\DeclareFieldFormat{pages}{\iffieldundef{bookpagination}{#1\isdot}{\mkpageprefix[bookpagination{#1\isdot}}}%
\DeclareFieldFormat{postnote}{\iffieldundef{pagination}{#1\isdot}{\mkpageprefix[pagination]{#1\isdot}}}%

%Change prefix for parts of book volumes
\DeclareFieldFormat{part}{%
\ifnumeral{#1}
	{\addcomma\addspace bk\adddot\addspace #1}%
	{\ifnumerals{#1}
		{\addcomma\addspace bks\adddot\addspace #1}%
		{\addcomma\addspace #1}}}%

%Automatic bibstring substitutions:
\DeclareFieldFormat{pubstate}{%
	\ifbibstring{#1}
		{\bibstring{#1}}%
		{\ifcapital
			{\MakeCapital{#1}}%
			{#1}}}%
\DeclareListFormat{location}{%
	\ifbibstring{#1}
		{\bibstring{#1}}%
		{\usebibmacro{list:delim}{#1}%
  		#1\isdot
  		\usebibmacro{list:andothers}}}
		
\DeclareListFormat{publisher}{%
	\ifbibstring{#1}
		{\bibstring{#1}}%
		{\usebibmacro{list:delim}{#1}%
  		#1\isdot
  		\usebibmacro{list:andothers}}}

%Field formats for annotated bibliographies (new in v. 0.2)
%(Annotations are printed by a redefined finentry macro)

%First need an indented environment and field formats for the texts (new in v. 0.2)
\newenvironment{indentannote}{%
 \begin{list}{}{%
  \setlength{\topsep}{0pt}%
  \setlength{\leftmargin}{1em}%
  \setlength{\rightmargin}{1em}%
  \setlength{\listparindent}{0pt}%
  \setlength{\itemindent}{0pt}%
  \setlength{\parsep}{0pt}%
 }%
 \item[]}%
{\end{list}}%

%\DeclareFieldFormat{annotation}{%
%	\begin{indentannote}
%		%\bibstring[]{annotation}\textbf{:}%
%		#1
%	\end{indentannote}}%

\DeclareNameFormat{author}{%
	\ifcitation%
		{%then in footnotes, firstname first
			\usebibmacro{name:first-last}{#1}{#3}{#5}{#7}%
			\usebibmacro{name:andothers}%
			}%end citations
		{%else bibliography or maybe list of shorthands
			\iftoggle{losflag}
				{%then list of shorthands: firstname first here, too 
					\usebibmacro{name:first-last}{#1}{#3}{#5}{#7}%
					\usebibmacro{name:andothers}%
					}%end los
				{%else bibliography
					\ifnumequal{\value{listcount}}{1}%
						{%then lastname first for first author/editor
							\usebibmacro{name:last-first}{#1}{#3}{#5}{#7}%
							\usebibmacro{name:andothers}}%
						{%else firstname first for remaining authors/editors
							\usebibmacro{name:first-last}{#1}{#3}{#5}{#7}%
							\usebibmacro{name:andothers}}%
					}%end bib
			}%
	}%

\DeclareNameFormat{editor}{%
	\ifcitation%
		{%then in footnotes, firstname first
			\usebibmacro{name:first-last}{#1}{#3}{#5}{#7}%
			\usebibmacro{name:andothers}%
			}%end citations
		{%else bibliography or maybe list of shorthands
			\iftoggle{losflag}
				{%then list of shorthands: firstname first here, too 
					\usebibmacro{name:first-last}{#1}{#3}{#5}{#7}%
					\usebibmacro{name:andothers}%
					}%end los
				{%else bibliography
					\ifnumequal{\value{listcount}}{1}%
						{%then lastname first for first author/editor
							\usebibmacro{name:last-first}{#1}{#3}{#5}{#7}%
							\usebibmacro{name:andothers}}%
						{%else firstname first for remaining authors/editors
							\usebibmacro{name:first-last}{#1}{#3}{#5}{#7}%
							\usebibmacro{name:andothers}}%
					}%end bib
			}%
	}%


\DeclareNameFormat{translator}{%
	\ifcitation%
		{%then in footnotes, firstname first
			\usebibmacro{name:first-last}{#1}{#3}{#5}{#7}%
			\usebibmacro{name:andothers}%
			}%end citations
		{%else bibliography or maybe list of shorthands
			\iftoggle{losflag}
				{%then list of shorthands: firstname first here, too 
					\usebibmacro{name:first-last}{#1}{#3}{#5}{#7}%
					\usebibmacro{name:andothers}%
					}%end los
				{%else bibliography
					\ifnumequal{\value{listcount}}{1}%
						{%then lastname first for first author/editor
							\usebibmacro{name:last-first}{#1}{#3}{#5}{#7}%
							\usebibmacro{name:andothers}}%
						{%else firstname first for remaining authors/editors
							\usebibmacro{name:first-last}{#1}{#3}{#5}{#7}%
							\usebibmacro{name:andothers}}%
					}%end bib
			}%
	}%
	
%%%%%% BIB-STRINGS %%%%%%%%%%%%%%%%

\NewBibliographyString{namedash}
\NewBibliographyString{letterto}
\NewBibliographyString{nodate}
\NewBibliographyString{noplace}
\NewBibliographyString{nopublisher}
\NewBibliographyString{online}
\NewBibliographyString{postedonline}
\NewBibliographyString{pseudonym}
\NewBibliographyString{incapitalized}
\NewBibliographyString{introto}
\NewBibliographyString{excerptfrom}
\NewBibliographyString{volumeof}
\NewBibliographyString{byline}
\NewBibliographyString{origeditionbib}
\NewBibliographyString{origeditiontitledbib}
\NewBibliographyString{origedition}
\NewBibliographyString{origeditiontitled}
\NewBibliographyString{translatedfromtitle}
\NewBibliographyString{translatedfromtitlebib}
\NewBibliographyString{numbers}
\NewBibliographyString{bycorporate}
\NewBibliographyString{bytranslatorbib}
\NewBibliographyString{translatedas}
\NewBibliographyString{translatedasbib}
%\NewBibliographyString{as	}
\NewBibliographyString{forthcoming}

\DefineBibliographyStrings{american}{%
	bycorporate		        = {by the},
	bytranslator     		= {trans.\isdot},
	bytranslatorbib     	= {translated by},
	translatedas     		= {trans.\isdot as},
	translatedasbib     	= {translated as},
%	as						= {as},
	forthcoming				= {forthcoming},
	incapitalized			= {In},
  	introto     			= {to},
	letterto     			= {to},
	excerptfrom     		= {from},
	volumeof    			= {of},
	byline      			= {by},
	online       			= {},%{available online from},
	postedonline         	= {posted},
	nodate					= {n.\isdot d.\isdot},
	noplace					= {n.\isdot p.\isdot},
	nopublisher				= {n.\isdot p.\isdot},
	origedition				= {orig. ed.\isdot}, 
	origeditiontitled		= {orig. ed.\isdot},
	origeditionbib			= {original edition},
	origeditiontitledbib	= {original edition},
	translatedfromtitle		= {originally},
	translatedfromtitlebib	= {originally published as},
	pseudonym				= {pseud.\isdot},
	numbers					= {nos\adddot},
	namedash				= {---------}
}%




%%%% CONSTANTS %%%%%%%%%%%%%%%%%

%Define comparison strings for entrysubtypes, authortypes, etc.

\newcommand\nameaddonpseud{pseudonym}
\newcommand\subtypemag{magazine}
\newcommand\subtypenewsp{newspaper}
\newcommand\subtypeclassic{classic}
\newcommand\subtypebiblical{biblical}
\newcommand\subtypeearlybook{canon}
\newcommand\subtypevideo{video}
\newcommand\entrytypearchive{customa}
\newcommand\subtypevolume{volume}
\newcommand\subtypeonline{online}
\newcommand\subtypedatabase{database}
\newcommand\subtypeblog{blog}
\newcommand\subtypelistmessage{listmessage}
\newcommand\subtypebooklike{book}
\newcommand\subtypepublicdocument{gov}
\newcommand\authortypeanon{anonymous}
\newcommand\authortypeunsure{anonymous?}
\newcommand\authortyperedundant{redundant}
\newcommand\authortypealternate{alternate}
\newcommand\authortypejournal{journal}
\newcommand\subtypeintro{to}
\newcommand\subtypeexcerpt{from}
\newcommand\subtypenone{none}
\newcommand\edtypecorp{corporate}
\newcommand\entrytypeper{periodical}
\newcommand\entrytypemanual{manual}
\newcommand\entrytypecoll{collection}
\newcommand\entrytypebook{book}
\newcommand\entrytyperef{reference}
\newcommand\entrytypeproc{proceedings}
\newcommand\entrytypereport{report}
\newcommand\entrytypebooklet{booklet}
\newcommand\entrytypemisc{misc}
\newcommand\entrytypeonline{online}
\newcommand\entrytypevideo{video}
\newcommand\entrytypeaudio{audio}
\newcommand\entrytypebookinbook{bookinbook}
\newcommand\entrytypearticle{article}
\newcommand\entrytypelegislation{legislation}
\newcommand\entrytypeletter{letter}
\newcommand\entrytypeperformance{performance}
\newcommand\optionaddoriginal{addorig}
\newcommand\optionnoreprints{none}
\newcommand\optionorigfirst{origfirst}
\newcommand\optiontransfromorig{transfrom}
\newcommand\optionorigtransas{transas}
\newcommand\optiondoubledate{doubledate}
\newcommand\noplace{np}
\newcommand\nopublisher{np}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                          %
%           BIBLIOGRAPHY DRIVERS           %
%                                          %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%      ARTICLE      %%%%%%%%%%%%%%%%%%

\DeclareBibliographyDriver{article}{%
\ifthenelse{\equal{\thefield{entrysubtype}}{\subtypenewsp}}%
	{\addtocategory{noteonly}{\thefield{entrykey}}%
	\addtocategory{innewspaper}{\thefield{entrykey}}}%
	{\ifthenelse{\equal{\thefield{entrysubtype}}{\subtypemag}}%
		{\addtocategory{inmagazine}{\thefield{entrykey}}}%
		{\relax}}%
\usebibmacro{bibindex}%
\usebibmacro{begentry}%
\ifthenelse{\equal{\thefield{authortype}}{\authortypejournal}}%
	{%Special case of journal as author: italicize author and suppress journal title
		\printtext[journalasauthor]{\printnames[lastname]{author}}%
		\setunit{\labelnamepunct}\newblock
		\clearfield{journaltitle}\clearfield{journalsubtitle}}%
	{\ifnameundef{author}
		{\relax}%
		{\usebibmacro{author}%
		\setunit{\labelnamepunct}\newblock}}%
\iffieldundef{type}
	{\relax}%
	{\printfield{type}%Needed here for certain government documents
	\newunit}%
\iffieldundef{title}
	{\iffieldundef{titleaddon}
		{\relax}%
		{\usebibmacro{title}%
		\newunit\newblock}}%
	{\usebibmacro{title}%
	\newunit\newblock}%
\iffieldundef{issuetitle}
	{%Assume any editors go with the article, not the issue
		\iffieldundef{title}
			{\iffieldundef{titleaddon}
				{\relax}% No article title--don't print the editors here after all
				{\usebibmacro{bytranslator+others}%
				\newunit\newblock
				\usebibmacro{byeditor+others}%
				\newunit\newblock}}%
			{\usebibmacro{bytranslator+others}%
			\newunit\newblock
			\usebibmacro{byeditor+others}%
			\newunit\newblock}}%
	{%Else editors come after the ``in,'' and go with issue title
		\usebibmacro{in:}}%
	%Save all the location and subtype info for after the x-ref
	\savefield{entrysubtype}{\childsubtype}%
	\savefield{issuetitle}{\childissuetitle}%
	\savefield{issuesubtitle}{\childsissueubtitle}%
	\savefield{note}{\childnote}\savefield{series}{\childseries}%
	\savefield{volume}{\childvolume}\savefield{number}{\childnumber}%
	\savefield{issue}{\childissue}%
	\savefield{year}{\childyear}\savefield{date}{\childdate}%
%Check for x-ref to periodical record
\iffieldundef{xref}%
	{%then no xref
		\usebibmacro{journal+issuetitle}}%
	{%else x-ref to the parent entry; the citecommands will pass parent data to the journal+issuetitle macro
		\printtext{\unspace}%Just to force the unit punctutation before the xref
		\iffootnote
			{%then footnote routine in cbx file
				\cbx@crosstoper{\thefield{xref}}}%
			{%else bibliography routine here in bbx
				\bbx@crosstoper{\thefield{xref}}}%
	}%endifundef xref
\newunit\newblock
\ifthenelse{\equal{\thefield{entrysubtype}}{\subtypemag}%
	\OR\equal{\thefield{entrysubtype}}{\subtypenewsp}}%
	{\setunit{\addcomma\addspace}}%
	{\setunit{\bibpagespunct}}%
\iftoggle{printpagerange}
	{\printfield{pages}}%
	{\relax}%Postnote will be printed instead of full page range
\newunit\newblock
\usebibmacro{onlinelocation}%
\newunit\newblock
\printfield{addendum}%
\newunit\newblock
\usebibmacro{pageref}%
\usebibmacro{finentry}%
}%End of article driver


%%%%%%      ARTWORK	     %%%%%%%%%%%%%%%%%%
\DeclareBibliographyAlias{artwork}{customd}

%%%%%%      AUDIO      %%%%%%
%
\DeclareBibliographyDriver{audio}{%
\usebibmacro{bibindex}%
\usebibmacro{begentry}%
\usebibmacro{author}%
\setunit{\labelnamepunct}\newblock
\usebibmacro{title}%
\newunit
\printfield{volume}%
\newunit
\iffieldundef{booktitle}
	{}%
	{\usebibmacro{in:}%
	\usebibmacro{booktitle}%
	\newunit}%
\iffieldundef{eventtitle}
	{\relax}%
	{\printtext[title]{\printfield[titlecase]{eventtitle}}}%
\setunit{\addcomma\addspace}%
\printfield{venue}%
\setunit{\addcomma\addspace}%
\printeventdate%
\newunit
\printfield{note}%
\newunit
\printlist{organization}%
\newunit
\printlist{institution}%
\newunit
\printlist{publisher}%
\newunit
\printfield{howpublished}%
\newunit
\printfield{type}%
\newunit
\printfield{usera}%
\newunit
\printfield{userd}%
\newunit
\usebibmacro{date-full}%
\newunit\newblock
\usebibmacro{onlinelocation}%
\newunit\newblock
\printfield{addendum}%
\newunit\newblock
\usebibmacro{pageref}%
\usebibmacro{finentry}%
}%end of audio driver

%%%%%%      BOOK      %%%%%%

\DeclareBibliographyDriver{book}{%
%Sort out entrysubtypes
\ifthenelse{\equal{\thefield{entrysubtype}}{\subtypeclassic}
			\OR\equal{\thefield{entrysubtype}}{\subtypebiblical}}
	{\usebibmacro{classic}}%
	{\ifthenelse{\equal{\thefield{entrysubtype}}{\subtypeearlybook}}%
		{\usebibmacro{earlybook}}%
		{%else default book routine
			\ifthenelse{\equal{\reprintoption}{origtransas}}
				{%then reverse order: orig-fields go first
					\usebibmacro{origtransas}}%
				{%else
					\relax}%
			\usebibmacro{bibindex}%
			\usebibmacro{begentry}%
			%Author Block-----------------------------------------------
			\usebibmacro{author/editor/translator}%
			\setunit*{\labelnamepunct}%
			\newblock
			%First-Level Title Block (for book or volume)--------------
			\usebibmacro{title}%
			\newunit\newblock
			%Editors and/or Translator Blocks--------------------------
%			\usebibmacro{byauthor}% Is this ever needed?
%			\newunit\newblock
			\ifthenelse{\equal{\thefield{entrytype}}{\entrytypereport}}%
				{%Special case of reports with an institutional author and an individual one
					\usebibmacro{bynamea}%
					\newunit\newblock}%
				{\relax}%
			\ifthenelse{\equal{\reprintoption}{\optionorigtransas}}
				{%then save the translator for after the publishing data
					\savename{translator}{\temptranslator}%
					\clearname{translator}}%
				{%else
					\relax}%
			\usebibmacro{byeditor+others}%
			\newunit\newblock
			%Block for vol. no., second-level title, and editors-----------------
			\usebibmacro{title+maintitle}%
			\usebibmacro{collectioneditor}%New unit puctuation set in subroutine
			\newunit\newblock
			%Edition Block -----------------------------------------------------
			\printfield{edition}%
			\newunit\newblock
			%Optional block for book-like videos--------------------------------
			\ifthenelse{\equal{\thefield{entrytype}}{\entrytypevideo}}%
				{%then certain videos have to be entered as books
					\printfield{type}%
					\newunit\newblock}%
				{%else not a video
					\relax}%
			%Series Block (including note field) --------------------------------
			\iftoggle{printseriesflag}%Print series info only when option is set
				{\iffieldundef{series}
					{\relax}%
					{\usebibmacro{series+number}%
					\setunit{\addcomma\addspace}}}%
				{\relax}%
			\printfield{note}%
			\newunit\newblock
			%Special block for book-like entrytypes-------------------
			\ifthenelse
				{\equal{\thefield{entrytype}}{\entrytypemanual}%
				\OR
				\equal{\thefield{entrytype}}{\entrytypebooklet}%
				\OR
				\equal{\thefield{entrytype}}{\entrytypemisc}%
				\OR
				\equal{\thefield{entrytype}}{\entrytypereport}}%
				{%then
					\printfield{type}%
					\newunit
					\printfield{version}%
					\newunit
					\printlist{organization}%
					\newunit\newblock}%
				{%else ordinary book
					\relax}%
			%Publishing Data Block -------------------------------------
			\iffootnote
				{%then location:publisher, year in parentheses
					\setunit{\addspace}%
					\printtext[parens]{\usebibmacro{publisher+location+year}}%
					}%
				{%else no parens and an extra field
					\usebibmacro{publisher+location+year}%
					\newunit\newblock
					\printfield{userc}%Special addendum for exhibit catalogs
					}%
			\newunit\newblock
			%Optional block for original publication data---------------------
			\ifthenelse{\equal{\reprintoption}{\optionaddoriginal}}
				{\usebibmacro{addoriginaledition}}%
				{\ifthenelse{\equal{\reprintoption}{\optiontransfromorig}}
					{\usebibmacro{addtransfrom}}%
					{\ifthenelse{\equal{\reprintoption}{\optionorigtransas}}
						{%then 
							\restorename{translator}{\temptranslator}%
							\usebibmacro{print-translation-data}}%
						{%else
							\relax}%
						}%
					}%
			\newunit\newblock
			%Online locators----------------------------------------------------------
			\usebibmacro{onlinelocation}%
			\newunit\newblock
			%Addendum and finish------------------------------------------------------
			\printfield{addendum}%
			\usebibmacro{pageref}%
			\usebibmacro{finentry}%
		}%End else; end default book routine
	}%
}%End oF book driver

%Subtype of Book for classics and other standard texts for which publication information is unimportant: footnotes only, no punctuation after author, and minimal publication information.
\newbibmacro{classic}{%-----------------------------------------------------------
%Add key to noteonly category
\addtocategory{noteonly}{\thefield{entrykey}}%
\ifthenelse{\equal{\thefield{entrysubtype}}{\subtypebiblical}}
	{\addtocategory{biblical}{\thefield{etnrykey}}}%
	{\addtocategory{classic}{\thefield{entrykey}}}%
\usebibmacro{bibindex}%
\usebibmacro{begentry}%
\iffootnote
	{\ifnameundef{shortauthor}
		{\usebibmacro{author/editor/translator}}%
		{\printnames{shortauthor}}}%
	{\usebibmacro{author/editor/translator}}%
\setunit{\addspace}\newblock
\iffootnote
	{\iffieldundef{shorttitle}
		{\ifthenelse{\equal{\thefield{entrysubtype}}{\subtypebiblical}}
			{\printfield[titlecase]{title}}%
			{\usebibmacro{title}}}%
		{\ifthenelse{\equal{\thefield{entrysubtype}}{\subtypebiblical}}
			{\printfield[titlecase]{shortitle}\isdot}%
			{\printfield[title]{shorttitle}\isdot}}}%
	{\ifthenelse{\equal{\thefield{entrysubtype}}{\subtypebiblical}}
		{\printfield[titlecase]{title}}%
		{\usebibmacro{title}}}%
\setunit{\addspace}%
\iffieldundef{edition}
	{\relax}%
	{\printtext[parens]{\printfield{edition}}%
	\newunit}%
\printfield{note}%Use note or series to identify edition, if necessary
\newunit\newblock
\printfield{addendum}%
\newunit\newblock
\usebibmacro{pageref}%
\usebibmacro{finentry}%
}%end of macro; end of classic subtype of book

%Special routine for entrysubtyp ``canon'' of book, for early English literature
\newbibmacro{earlybook}{%-----------------------------------------------------------------
%Add key to noteonly category
\addtocategory{noteonly}{\thefield{entrykey}}%
\addtocategory{canonical}{\thefield{entrykey}}%
\usebibmacro{bibindex}%
\usebibmacro{begentry}%
\iffootnote
	{%then always use shortauthor and shorttitle in footnotes, if available
		\ifnameundef{shortauthor}
			{\usebibmacro{author/editor/translator}}%
			{\printnames{shortauthor}}%
		\setunit*{\labelnamepunct}%
		\newblock
		\iffieldundef{shorttitle}%
			{\usebibmacro{title}}%
			{\printtext[title]{\printfield[titlecase]{shorttitle}}\isdot}%
		}%end footnote routine
	{%else usual author and title routines in the bibliography
		\usebibmacro{author/editor/translator}%
		\newunit\newblock
		\usebibmacro{title}%
		}%
\setunit{\addspace}%
\iffieldundef{edition}
	{\relax}%
	{\printtext[parens]{\printfield{edition}}}%
\newunit\newblock
\printfield{note}%
\newunit\newblock
\printfield{addendum}%
\newunit\newblock
\usebibmacro{pageref}%
\usebibmacro{finentry}%
}%End earlybook macro


%%%%%%      BOOKINBOOK      %%%%%%

\DeclareBibliographyAlias{bookinbook}{inbook}


%%%%%%      BOOKLET      %%%%%%

\DeclareBibliographyAlias{booklet}{book}%

%%%%%%      COLLECTION     %%%%%%%%%%%

\DeclareBibliographyDriver{collection}{%
\usebibmacro{bibindex}%
\usebibmacro{begentry}%
\ifthenelse{\equal{\reprintoption}{\optionorigtransas}}
	{%then reverse order: orig-fields go first
		\usebibmacro{origtransas}}%
	{%else
		\relax}%
%Editor Block-----------------------------------------------
\ifthenelse{\equal{\thefield{editortype}}{\edtypecorp}}
	{%then corporate editor of a proceedings: print without the editor string
  		\printnames{editor}%
		\clearname{editor}}%
  	{%else personal editor: use usual editor routine
		\usebibmacro{editor}}%
\setunit{\labelnamepunct}%
\newblock
%First-Level Title Block (for book or volume)--------------
\usebibmacro{title}%
\newunit\newblock
%Proceedings fields---------------------------------------
\usebibmacro{getproceedingsfields}%
\newunit\newblock
%Editors and/or Translator Blocks--------------------------
\ifthenelse{\equal{\reprintoption}{\optionorigtransas}}
	{%then save the translator for after the publishing data
		\savename{translator}{\temptranslator}%
		\clearname{translator}}%
	{%else
		\relax}%
\usebibmacro{byeditor+others}%
\newunit\newblock
%Block for vol. no., second-level title, and editors-----------------
\usebibmacro{title+maintitle}%
\usebibmacro{collectioneditor}%New unit puctuation set in subroutine
\newunit\newblock
%Edition Block -----------------------------------------------------
\printfield{edition}%
\newunit\newblock
%Series Block (including note field) --------------------------------
\iftoggle{printseriesflag}%Print series info only when option is set
	{\iffieldundef{series}
		{\relax}%
		{\usebibmacro{series+number}%
		\setunit{\addcomma\addspace}}}%
	{\relax}%
\printfield{note}%
\newunit\newblock
%Special fields ----------------------------------------------------
\printfield{type}%
\newunit
\printfield{version}%
\newunit\newblock
%Publishing Data Block ------------------------------------------------
\iffootnote%
	{%then location:publisher, year in parentheses
		\setunit{\addspace}%
		\printtext{\bibleftparen}%
		\usebibmacro{publisher+location+year}%
		\printtext{\bibrightparen}}%
	{%else no parens and an extra field
		\usebibmacro{publisher+location+year}%
		\newunit\newblock
		\printfield{userc}%Special addendum for exhibit catalogs
		}%
\newunit\newblock
%Optional block for original publication data----------------------------------------
\ifthenelse{\equal{\reprintoption}{\optionaddoriginal}}
	{\usebibmacro{addoriginaledition}}%
	{\ifthenelse{\equal{\reprintoption}{\optiontransfromorig}}
		{\usebibmacro{addtransfrom}}%
		{\ifthenelse{\equal{\reprintoption}{\optionorigtransas}}
			{%then 
				\restorename{translator}{\temptranslator}%
				\usebibmacro{print-translation-data}}%
			{%else
				\relax}%
			}%
		}%
\newunit\newblock
%Print urls, etc., if appropriate option\subtype is set
\newunit\newblock
%Online locators----------------------------------------------------------
\usebibmacro{onlinelocation}%
\newunit\newblock
%Addendum and finish------------------------------------------------------
\printfield{addendum}%
\usebibmacro{pageref}%
\usebibmacro{finentry}%
}% End of collection driver

  

%%%%%%      IMAGE	     %%%%%%%%%%%%%%%%%%

\DeclareBibliographyAlias{image}{customd}

%%%%%%      INBOOK      %%%%%%

%Inbook. Similar to incollection, only with author instead of editor.
\DeclareBibliographyDriver{inbook}{%
\usebibmacro{bibindex}%
\usebibmacro{begentry}%
%Author Block------------------------------------------------------
\ifthenelse{\equal{\thefield{entrysubtype}}{\subtypeearlybook}}
	{%Then for canonical works, add key to noteonly category and use shortauthor
		\addtocategory{noteonly}{\thefield{entrykey}}%
		\addtocategory{canonical}{\thefield{entrykey}}%\iffootnote
		{\ifnameundef{shortauthor}
			{\usebibmacro{author}}%
			{\printnames{shortauthor}}}%
		}%
	{%Else not canonical, use usual author routine
		\usebibmacro{author}}%
\savename{author}{\childauthor}%Need later, to compare with book author
\setunit{\labelnamepunct}%
\newblock
%Inner Title Block--------------------------------------------------------
\usebibmacro{title}%
\newunit\newblock
%Book-Title Block--------------------------------------------------------
\ifthenelse{\equal{\thefield{entrysubtype}}{\subtypevolume}%
	\OR\equal{\thefield{entrytype}}{\entrytypebookinbook}}%
	{%then1 reference is to a volume of a larger work. Print volume number here and not at the end
		\printfield{volume}\printfield{part}%
		\clearfield{volume}\clearfield{part}\toggletrue{volumeprinted}%
		\clearfield{title}%Clear title, too, to prevent repetition
		}%
	{%else1 
		\ifthenelse{\equal{\thefield{entrysubtype}}{\subtypevideo}}%
			{%then2 Special case of a video that gets bookvolume-like formatting
				\iffootnote
					{\printfield[noformat]{volume}}%
					{\printtext[noformat]{\MakeCapital{\thefield{volume}}}}%
				\clearfield{volume}\toggletrue{volumeprinted}%
				}%end then2
			{%else2 default case
				\printfield{chapter}%(Turabian doesn't actually use chapter numbers)
				}%end else2, end subtypevideo test
		}%end else1, end subtypevolume test 
\newunit\newblock
%Second-level title and editor, publishing data------------------------------------------
%But first, save info to pass to parent record in case of x-reffing
\savename{author}{\childauthor}%
\savefield{pages}{\childpages}%
\savefield{volume}{\childvolume}%
\savefield{part}{\childpart}%
\usebibmacro{in:}%Macro modifed below for subtypes that need different prepositions
%Where are the book data? 
%First check whether there is an x-ref to the book
\iffieldundef{xref}%
	{%then no xref; get book info from current entry
		\usebibmacro{getbookinfo}}%
	{%else x-ref to the parent entry; the crosstocoll citecommands will then pass parent data to the bibmacro getbookinfo, below
		\printtext[noformat]{\unspace}%Just to force the unit punctutation before the xref
		\iffootnote
			{\cbx@crosstocoll{\thefield{xref}}}%
			{\bbx@crosstocoll{\thefield{xref}}}%
	}%endiffieldundef
\newunit\newblock
%Print rest of inbook data
%Onlineblock---------------------------------------------------------------
\usebibmacro{onlinelocation}%
\newunit\newblock
%Addendum and finish-----------------------------------------------------
\printfield{addendum}%
\newunit\newblock
\usebibmacro{pageref}%
\usebibmacro{finentry}%
}%End of inbook driver
  

    
%%%%%%      INCOLLECTION     %%%%%%%

\DeclareBibliographyDriver{incollection}{%
\usebibmacro{bibindex}%
\usebibmacro{begentry}%
%Author Block------------------------------------------------------
\ifthenelse{\equal{\thefield{entrysubtype}}{\subtypepublicdocument}}%
	{\usebibmacro{author}}%
	{\usebibmacro{author/editor/translator}}%
\savename{author}{\childauthor}%Need later, to compare with book author
\setunit{\labelnamepunct}%
\newblock
%Inner Title Block--------------------------------------------------------
\usebibmacro{title}%
\newunit\newblock
%Book-Title Block--------------------------------------------------------
\ifthenelse{\equal{\thefield{entrysubtype}}{\subtypevolume}}%
	{%then1 reference is to a volume of a larger work. Print volume number here and not at the end
		\printfield{volume}\printfield{part}\toggletrue{volumeprinted}%
		\clearfield{volume}\clearfield{part}%
		}%
	{%else1
		\printfield{chapter}%Turabian doesn't use chapter numbers; not sure it goes here
		}%end else1, end subtypevolume test
\newunit\newblock
%Second-level title and editor, publishing data --------------------------------------
%Save data for after the xref
\savename{author}{\childauthor}%
\savefield{pages}{\childpages}%
\savefield{volume}{\childvolume}%
\savefield{part}{\childpart}%
%Insert preposition
\usebibmacro{in:}%
%Now where are the collection data? 
%First check whether there is an x-ref to the collection
\iffieldundef{xref}%
	{%then no xref; get collection data from the current entry
		\usebibmacro{getbookinfo}}%
	{%else x-ref to the parent entry; the citecommands will pass parent data to the bibmacro getbookinfo, below
		\printtext{\unspace}%Just to force the unit punctutation before the xref
		\iffootnote
			{\cbx@crosstocoll{\thefield{xref}}}%footnote routine in cbx file
			{\bbx@crosstocoll{\thefield{xref}}}%bibliography routine here in bbx
	}%endifundef
%Print rest of incollection data
\newunit\newblock
%Online Block--------------------------------------------------------------------
\usebibmacro{onlinelocation}%
\newunit\newblock
%Addendum and Finish--------------------------------------------------------
\printfield{addendum}%
\newunit\newblock
\usebibmacro{pageref}%
\usebibmacro{finentry}%
}%End incollection driver



%%%%%%      INPROCEEDINGS      %%%%%%

%Inproceedings are treated like incollections, even though they have some extra fields
\DeclareBibliographyAlias{inproceedings}{incollection}%
    
%%%%%%      INREFERENCE     %%%%%%%

\DeclareBibliographyDriver{inreference}{%
\usebibmacro{bibindex}%
\usebibmacro{begentry}%
\ifthenelse{\equal{\thefield{entrysubtype}}{\subtypepublicdocument}}%
	{\usebibmacro{author}}%
	{\usebibmacro{author/editor/translator}}%
\setunit{\labelnamepunct}
\newblock
%Inner Title Block--------------------------------------------------------
\usebibmacro{title}%
\newunit\newblock
%Book-Title Block--------------------------------------------------------
\ifthenelse{\equal{\thefield{entrysubtype}}{\subtypevolume}}%
	{%then1 reference is to a volume of a larger work. Print volume number here and not at the end
		\printfield{volume}\printfield{part}\toggletrue{volumeprinted}%
		\clearfield{volume}\clearfield{part}%
		}%
	{%else1
		\printfield{chapter}%Turabian doesn't use chapter numbers; not sure it goes here
		}%end else1, end subtypevolume test
\newunit\newblock
%Second-level title and editor, publishing data --------------------------------------
%Save data for after the xref
\savename{author}{\childauthor}%
\savefield{pages}{\childpages}%
\savefield{volume}{\childvolume}%
\savefield{part}{\childpart}%
%Insert preposition
\usebibmacro{in:}%
%Now where are the collection data?
\iffieldundef{xref}%
	{%then no xref
		\iffootnote
			{%Then footnote routine: just title and edition
				\usebibmacro{getbooktitle}%
				\newunit\newblock
				\printfield{edition}%
				\newunit\newblock
				\printfield{note}%
				}%End of footnote routine
			{%Else: bibliography (not normally used) or list of shorthands:
				\usebibmacro{getbookinfo}}%
		}%end then
	{%else x-ref to the parent entry; the citecommands will pass parent data to the bibmacro getbookinfo, below
		\printtext{\unspace}%Just to force the unit punctuation before the xref
		\iffootnote
			{\cbx@crosstoref{\thefield{xref}}}%footnote routine in cbx file
			{\bbx@crosstoref{\thefield{xref}}}%bibliography routine passes parent data to getbookinfo, below
		}%endiffieldundef xref
%Print rest of inreference data
\newunit\newblock
%Online Block--------------------------------------------------------------------
\usebibmacro{onlinelocation}%
\newunit\newblock
%Addendum and Finish--------------------------------------------------------
\printfield{addendum}%
\newunit\newblock
\usebibmacro{pageref}%
\usebibmacro{finentry}%
}%end inreference driver

%%%%%%      JURISDICTION      %%%%%%%%%%%%%%%%%%

\DeclareBibliographyDriver{jurisdiction}{%
\addtocategory{noteonly}{\thefield{entrykey}}%
\usebibmacro{bibindex}%
\usebibmacro{begentry}%
\printfield{type}%
\newunit
\usebibmacro{title}%
\newunit\newblock
\printfield{note}\isdot%
\setunit{\addspace}%
\printfield{pages}%
\setunit{\addspace}%
\printtext[parens]{%
	\printlist{institution}\isdot%
	\setunit{\addspace}%
	\usebibmacro{date-full}}%
\newunit\newblock
\usebibmacro{onlinelocation}%
\newunit\newblock
\printfield{addendum}%
\newunit\newblock
\usebibmacro{pageref}%
\usebibmacro{finentry}%
}%End jurisdiction driver

%%%%%%      LEGAL      %%%%%%%%%%%%%%%%%%

\DeclareBibliographyDriver{legal}{%
\addtocategory{noteonly}{\thefield{entrykey}}%
\usebibmacro{bibindex}%
\usebibmacro{begentry}%
\printfield{type}%
\newunit
\usebibmacro{title}%
\newunit\newblock
\printfield{note}\isdot%
\iffieldundef{issuetitle}
	{\relax}%
	{\usebibmacro{issue}%Prints issuetitle
	\newunit}%
\printfield{note}%
\setunit{\addcomma\addspace}%
\usebibmacro{journal}%
\iffieldundef{series}
	{\setunit*{\addspace}}%
	{\setunit{\addcomma\addspace}%
	\printfield{series}%
	\setunit{\addcomma\addspace}}%
\printfield{volume}%
\setunit{\addcomma\addspace\bibstring{number}\addspace}%
\printfield{number}%
\setunit{\addspace}%
\usebibmacro{issue+date}%
\setunit{\bibpagespunct}%
\printfield{pages}%
\newunit\newblock
\usebibmacro{onlinelocation}%
\newunit\newblock
\printfield{addendum}%
\newunit\newblock
\usebibmacro{pageref}%
\usebibmacro{finentry}%
}%End legal driver

%%%%%%      LEGISLATION      %%%%%%

\DeclareBibliographyDriver{legislation}{%
\usebibmacro{bibindex}%
\usebibmacro{begentry}%
\toggletrue{useshortauthors}%Always use shortauthor in footnotes
\usebibmacro{author}%
\setunit*{\labelnamepunct\addspace}\newblock
\printfield{type}%
\newunit
\usebibmacro{title}%
%\newunit
%\usebibmacro{byauthor}%
%\newunit
%\usebibmacro{byeditor+others}%
\newunit\newblock
\printfield{edition}%
\newunit
\iffootnote
	{\iffieldundef{shortjournal}
		{\printfield{journaltitle}
		\setunit*{\addcolon\addspace}%
		\printfield{journalsubtitle}}%
		{\printfield{shortjournal}}%
	}%
	{\printfield{journaltitle}%
	\setunit*{\addcolon\addspace}%
	\printfield{journalsubtitle}}%
\newunit\newblock
\printfield{note}\isdot%
\newunit\newblock
%Give facts of publication freeform
\printlist{institution}%
\setunit{\addcomma\addspace}%
\printlist{publisher}%
\setunit{\addcomma\addspace}%
\printfield{howpublished}
\setunit{\addcomma\addspace}%
\usebibmacro{date-full}%
\printfield{usera}%
\newunit\newblock
\printfield{userd}%
\newunit\newblock
\iffootnote
	{\printfield[noformat]{volume}%
	\printfield{part}}%
	{\printfield{volume}%
	\printfield{part}}%
\iffieldundef{volume}
	{\setunit{\addcomma\addspace}}%
  	{\setunit{\bibpagespunct}}%
\printfield{pages}%
\usebibmacro{onlinelocation}%
\newunit\newblock
\printfield{addendum}%
\usebibmacro{pageref}%
\usebibmacro{finentry}%
}%End of legislation driver

%%%%%%      LETTER      %%%%%%

%For correspondence (archival or published) 
\DeclareBibliographyDriver{letter}{%
%Add key to noteonly category
\addtocategory{noteonly}{\thefield{entrykey}}%
\usebibmacro{bibindex}%
\usebibmacro{begentry}%
\usebibmacro{author}%
%Just in case, format for bibliography, too
\iffootnote
	{\relax}%
	{\ifcitation
		{\relax}%
		{\setunit{\labelnamepunct}\newblock
		\printnames[byauthor]{author}}}%Repeat name in bibliography
%If there's a recipient in namec, print ``to namec''
\setunit{\addspace\bibstring{letterto}\addspace}%
\printnames{namec}%
\setunit{\addcomma\addspace}%
\usebibmacro{title}%Shouldn't be needed for letters, except maybe if names are missing or uncertain
\setunit{\addcomma\addspace}%
\printfield{type}%
\setunit{\addcomma\addspace}%
\printfield{venue}%
\setunit{\addcomma\addspace}%
\printfield{note}%
\setunit{\addcomma\addspace}%
\usebibmacro{date-full}%
\newunit\newblock
\iffieldundef{xref}%
	{%then no xref; just print what's likely to be available in the customd
		\iffieldundef{booktitle}
			{%then improvise
				\printfield{howpublished}%
				\setunit*{\addcomma\addspace}%
				\printlist{organization}%
				\setunit*{\addcomma\addspace}%
				\printlist{institution}%
				\setunit*{\addcomma\addspace}%
				\printfield{library}%
				\setunit*{\addcomma\addspace}%
				\printfield{userd}%
				\newunit\newblock
				\usebibmacro{volume+pages}%
				}%
			{%else assume it's from a published collection
				\usebibmacro{in:}%
				\usebibmacro{getbookinfo}%
				}% 
		\newunit\newblock
		\usebibmacro{onlinelocation}%
		}%
	{%else x-ref to collection or customa and the getbookinfo macro
	%(but only print `in' if the xref turns out to be to a collection)
		%Save data for after the xref
		\printtext{\unspace}%Just to force the unit punctutation before the xref
		\savename{author}{\childauthor}%
		\savefield{pages}{\childpages}%
		\savefield{volume}{\childvolume}%
		\savefield{part}{\childpart}%
		\iffootnote%
			{\cbx@crosstoarch{\thefield{xref}}}%
			{\bbx@crosstoarch{\thefield{xref}}}%
%		\restorefield{volume}{\childvolume}%
%		\restorefield{part}{\childpart}%
%		\restorefield{pages}{\childpages}%
	}%endif
\newunit\newblock
\printfield{library}%
\setunit{\addcomma\addspace}%
\printfield{userd}%
\newunit\newblock
\iffootnote
	{\usebibmacro{volume+pages}%
	\newunit\newblock}%
	{\relax}%
\usebibmacro{onlinelocation}%
\newunit\newblock
\printfield{addendum}%
\newunit\newblock
\usebibmacro{pageref}%
\usebibmacro{finentry}%
}%end letter driver

%%%%%%      MANUAL      %%%%%%

%Manuals are treated like books, even though they have some extra fields
\DeclareBibliographyAlias{manual}{book}%

%%%%%%      MISC      %%%%%%

\DeclareBibliographyAlias{misc}{booklet}%
  
%%%%%%      MOVIE      %%%%%%

% Currently not distinguished from performance

\DeclareBibliographyAlias{movie}{performance}%

%%%%%%      MUSIC      %%%%%%

% Currently not distinguished from audio

\DeclareBibliographyAlias{music}{audio}

%%%%%%      ONLINE      %%%%%%

\DeclareBibliographyDriver{online}{%
\usebibmacro{bibindex}%
\usebibmacro{begentry}%
\usebibmacro{author/editor/translator}%
\setunit{\labelnamepunct}\newblock
%Case of entrysubtypes
\ifthenelse{\equal{\thefield{entrysubtype}}{\subtypeblog}}%
	{%then blog entry by main author
		\usebibmacro{blog}}%
	{%else
		\ifthenelse{\equal{\thefield{entrysubtype}}{\subtypelistmessage}}
			{%then e-mail to listserve
				\usebibmacro{listmessage}}%
			{%else default web site routine
				\usebibmacro{title}%
				\newunit\newblock
				\usebibmacro{byeditor}%
				\newunit\newblock
				\printfield{version}%
				\newunit
				\usebibmacro{date-optional}%
				\newunit
				\printfield{note}%
				\newunit\newblock
				\iffieldundef{booktitle}
					{\relax}%
					{\usebibmacro{in:}%
					\usebibmacro{booktitle}%
					\newunit}%
				\printlist{organization}%
				\newunit
				\printlist{institution}%
				\newunit
				\printlist{publisher}%
				\newunit
				\printfield{howpublished}%
				\newunit
				\printfield{type}%
				\newunit
				\printfield{usera}%
				\newunit
				\printfield{userd}%
				\newunit\newblock
			}%end else; end default web site routine
	}%end case of entrysubtype
\usebibmacro{onlinelocation}%
\newunit\newblock
\printfield{addendum}%
\newunit\newblock
\usebibmacro{pageref}%
\usebibmacro{finentry}%
}%end online driver

%Subroutine of online driver
\newbibmacro{blog}{%--------------------------------------------
\usebibmacro{title}%
\newunit\newblock
\printfield{type}%
\setunit*{\addspace}%
\bibstring{postedonline}\addspace
\usebibmacro{date-full}%
\newunit
\printfield{note}%
\newunit\newblock
\printlist{organization}%
\setunit{\addcomma\space}
\printlist{institution}%
\newunit
\printfield{howpublished}%
\newunit
\printfield{usera}%
\newunit\newblock
%Add key to noteonly and blog categories
\addtocategory{noteonly}{\thefield{entrykey}}%
\addtocategory{blog}{\thefield{entrykey}}%
}%end blog macro

\newbibmacro{listmessage}{%--------------------------------------------
%No special treatment for listserv messages, at least for the present
	\relax
}%end listmessage macro

%%%%%%      PATENT      %%%%%%

% Currently not distinguished from legislation

\DeclareBibliographyAlias{paten}{legislation}

%%%%%%      PERFORMANCE     %%%%%%%%%%%%%%%%%%

\DeclareBibliographyDriver{performance}{%
%Add key to noteonly category
\addtocategory{noteonly}{\thefield{entrykey}}%
\usebibmacro{bibindex}%
\usebibmacro{begentry}%
\usebibmacro{title}%
\setunit{\addcomma\addspace\bibstring{byauthor}\addspace}%
\printnames[byauthor]{author}%
\setunit{\addcomma\addspace}%
\printfield{type}%
\setunit{\addcomma\addspace}%
\printfield{note}%
\setunit{\addcomma\addspace}%
\printfield{venue}%
\newunit
\printlist{publisher}%
\newunit
\printfield{howpublished}%
\setunit{\addcomma\addspace}%
\usebibmacro{date-full}%
\iffieldundef{origtitle}%
	{\relax}%
	{\setunit{\addspace\bibleftparen}%
	\printfield[titlecase]{origtitle}%
	\setunit*{\addspace}%
	\printorigdate
	\bibrightparen}%
\newunit\newblock
\usebibmacro{onlinelocation}%
\newunit\newblock
\printfield{addendum}%
\newunit\newblock
\usebibmacro{pageref}%
\usebibmacro{finentry}%
}%end performance driver
  
%%%%%%      PERIODICAL      %%%%%%

\DeclareBibliographyDriver{periodical}{%
\ifthenelse{\equal{\thefield{entrysubtype}}{\subtypenewsp}}%
	{\addtocategory{newspaper}{\thefield{entrykey}}}%
	{\ifthenelse{\equal{\thefield{entrysubtype}}{\subtypemag}}%
		{\addtocategory{magazine}{\thefield{entrykey}}}%
		{\relax}}%
\usebibmacro{bibindex}%
\ifnameundef{editor}
	{\setunit{\addspace}}%
	{\usebibmacro{editor}%
	\setunit{\labelnamepunct}\newblock}%
\usebibmacro{title+issuetitle}%
\newunit\newblock
\usebibmacro{onlinelocation}%
\newunit\newblock
\printfield{addendum}%
\newunit\newblock
%\usebibmacro{pageref}%
\usebibmacro{finentry}%
}%end periodical driver

%%%%%%      PROCEEDINGS      %%%%%%

%Proceedings are treated like collections, even though they have some extra fields
\DeclareBibliographyAlias{proceedings}{collection}%

%%%%%%      REFERENCE     %%%%%%%%%%%

\DeclareBibliographyDriver{reference}{%
\usebibmacro{bibindex}%
\usebibmacro{begentry}%
\iffootnote
	{%Then footnote routine: just title and edition
		\usebibmacro{title}%Uses shorttitles for references, if available
		\newunit\newblock
		\printfield{edition}%
		\newunit\newblock
		\printfield{note}%
		}%End of footnote routine
	{%Else: bibliography (not normally used) or list of shorthands:
	%(Proceed more or less as for collections, but title first)
		%First-Level Title Block (for book or volume)--------------
		\usebibmacro{title}%
		\newunit\newblock
		%Author Block-----------------------------------------------
		\usebibmacro{byauthor}%
		\newunit\newblock
		%Editors and/or Translator Blocks--------------------------
		\usebibmacro{byeditor+others}%
		\newunit\newblock
		%Block for vol. no., second-level title, and editors-----------------
		\usebibmacro{title+maintitle}%
		\usebibmacro{collectioneditor}%New unit punctuation set in subroutine
		\newunit\newblock
		%Edition Block -----------------------------------------------------
		\printfield{edition}%
		\newunit\newblock
		%Series Block (including note field) --------------------------------
		\iftoggle{printseriesflag}%Print series info only when option is set
			{\iffieldundef{series}
				{\relax}%
				{\usebibmacro{series+number}%
				\setunit{\addcomma\addspace}}}%
			{\relax}%
		\printfield{note}%
		\newunit\newblock
		%Publishing Data Block ------------------------------------------------
		\usebibmacro{publisher+location+year}%
		\newunit\newblock
		%Online locators----------------------------------------------------------
		\usebibmacro{onlinelocation}%
		}%Endif, end of bibliography routine
\newunit\newblock
%Addendum and finish------------------------------------------------------
\printfield{addendum}%
\usebibmacro{pageref}%
\usebibmacro{finentry}%
}% End of refernece driver


\renewbibmacro{byauthor}{%
  \ifboolexpr{
%    test \ifuseauthor
%    or
    test {\ifnameundef{author}}
  }
    {}
    {\usebibmacro{bytypestrg}{author}{author}%
     \setunit{\addspace}%
     \printnames[byauthor]{author}}}
     
%%%%%%      REPORT      %%%%%%

%Reports are treated like books, even though they have some extra fields
\DeclareBibliographyAlias{report}{book}%
  
%%%%%%      THESIS      %%%%%%

\DeclareBibliographyDriver{thesis}{%
\usebibmacro{bibindex}%
\usebibmacro{begentry}%
\usebibmacro{author}%
\setunit{\labelnamepunct}\newblock
\iffieldundef{title}
	{\iffieldundef{titleaddon}
		{\relax}%
		{\usebibmacro{title}}}%
	{\usebibmacro{title}}%
\iffieldundef{note}
	{\relax}
	{\newunit\newblock
	\printfield{note}}%
\iffootnote
	{%then footnote environment: open parentheses
		\setunit{\addspace\bibleftparen}}%
    {%else bibliography environmemt: just start a new unit
    		\newunit}%
\iffieldbibstring{type}
	{\bibstring{\thefield{type}}}%
	{\printfield{type}\isdot}%
\setunit{\addcomma\space}%Yes, comma here, even in bibliography
\printlist{institution}%
\setunit*{\addcomma\addspace}%
\usebibmacro{year}%
\iffootnote
	{%then footnote environment: close parentheses
		\printtext{\bibrightparen}\setunit{\addspace}}%
    {%else bibliography environmemt: just start a new block
    		\newunit\newblock}%
\usebibmacro{onlinelocation}%
\newunit\newblock
\printfield{addendum}%
\newunit\newblock
\usebibmacro{pageref}%
\usebibmacro{finentry}%
}%End thesis driver

%%%%%%      UNPUBLISHED      %%%%%%

%For unpublished sources, such as conference presentations or drafts: a little more structure and extra fields than in biblatex standard; note field before howpublished; in footnotes for conference papers, location, sponsorship and date go in parentheses
\DeclareBibliographyDriver{unpublished}{%
\usebibmacro{bibindex}%
\usebibmacro{begentry}%
\usebibmacro{author/editor/translator}%
\setunit{\labelnamepunct}\newblock
\iffieldundef{title}
	{\iffieldundef{titleaddon}
		{\relax}%
		{\usebibmacro{title}}}%
	{\usebibmacro{title}}%
\iffieldundef{note}
	{\relax}
	{\newunit\newblock
	\printfield{note}}%
\iffootnote
	{%then footnote environment: open parentheses
		\setunit{\addspace\bibleftparen}}%
    {%else bibliography environmemt: just start a new unit
    		\newunit}%
\printfield{type}%
\setunit*{\addcomma\space}%Yes, comma here, even in bibliography
\printfield{howpublished}%
\setunit*{\addcomma\addspace}%
\iffieldundef{eventtitle}
	{\relax}%
	{\printtext[title]{\printfield[titlecase]{eventtitle}}}%
\setunit*{\addcomma\addspace}%
\printlist{organization}%
\setunit*{\addcomma\addspace}
\printfield{venue}%
\setunit*{\addcomma\addspace}%
%Just one date; no separate eventdate
\usebibmacro{date-full}%
\iffootnote
	{%then footnote environment: close parentheses
		\setunit{\unspace}\printtext{\bibrightparen}}%
    {%else bibliography environmemt: just start a new block
    		\newunit\newblock
    }%endif
\usebibmacro{onlinelocation}%
\newunit\newblock
\printfield{addendum}%
\newunit\newblock
\usebibmacro{pageref}%
\usebibmacro{finentry}%
}%End unpublished driver

%%%%%%      VIDEO	     %%%%%%%%%%%%%%%%%%

\DeclareBibliographyDriver{video}{%
%Sort out entrysubtypes
\usebibmacro{bibindex}%
\usebibmacro{begentry}%
\usebibmacro{author}%
\setunit*{\labelnamepunct\addspace}\newblock
\usebibmacro{title}%
\newunit\newblock
\printfield{edition}%
\newunit
\printfield{type}%
\newunit\newblock
\printfield{note}%
\newunit\newblock
\printfield[noformat]{volume}%In case of multiple discs or cassettes in a set
\iffieldundef{booktitle}%
	{%then
		\relax}%
	{%else print title of the set
		\usebibmacro{in:}%
		\usebibmacro{booktitle}%
		\newunit\newblock}%
\ifthenelse{\equal{\thefield{entrysubtype}}{\subtypeonline}}%
	{%Unstructured formatting of publishing info for online multimedia
		\printlist{organization}%
		\newunit
		\printlist{institution}%
		\newunit
		\printlist{publisher}%
		\newunit
		\printfield{howpublished}%	
		\newunit
		\printfield{usera}%
		\newunit
		\printfield{userd}}%End online routine
	{%Else use booklike location:publisher format
		\iffootnote%
			{%then in parentheses
				\setunit{\addspace}%
				\printtext{\bibleftparen}%
				\usebibmacro{publisher+location+year}%
				\printtext{\bibrightparen}}%
			{%else without parentheses
				\usebibmacro{publisher+location+year}}%
	}%End if; end publisher/location info
\newunit\newblock
\usebibmacro{onlinelocation}%
\newunit\newblock
\printfield{addendum}%
\usebibmacro{pageref}%
\usebibmacro{finentry}%
}%end video driver    
  
%%%%%%      CUSTOMA       %%%%%%

%Custom entry type for archival collections
\DeclareBibliographyDriver{customa}{%
\usebibmacro{bibindex}%
\usebibmacro{begentry}%
\usebibmacro{author}%Prints nameaddon and labelnamepunct, too
\usebibmacro{getcustomainfo}%
\usebibmacro{pageref}%
\usebibmacro{finentry}%
}%End customa driver

%Common formatting routine for customa and crossreferences to customa
\newbibmacro{getcustomainfo}{%----------------------------------------------
\printfield[titlecase]{title}%
\newunit\newblock
\printfield{note}%
\newunit\newblock
\printlist{organization}%
\setunit*{\addcomma\space}
\printlist{institution}%
\setunit*{\addcomma\space}
\printfield{library}%
\setunit*{\addcomma\space}%
\printlist{location}%
\newunit
\printfield{type}%
\newunit
\printfield{usera}%
\newunit\newblock
\usebibmacro{onlinelocation}%
\newunit\newblock
\printfield{addendum}%
}%end getcustomainfo

%%%%%%      CUSTOMD      %%%%%%

%Custom entrytype D for archival documents (quasi `inarchive' or `incustoma') 
\DeclareBibliographyDriver{customd}{%
%Add key to noteonly category
\addtocategory{noteonly}{\thefield{entrykey}}%
\usebibmacro{bibindex}%
\usebibmacro{begentry}%
\usebibmacro{author}%
\setunit{\labelnamepunct}\newblock
\usebibmacro{title}%
\clearfield{title}\clearfield{subtitle}\clearfield{titleaddon}%
\setunit{\addspace}%
\printnames{namec}% Needed for the occasional letter, interview
\setunit{\addcomma\addspace}%
\printfield{type}%
\setunit{\addcomma\addspace}%
\printfield{venue}%
\setunit{\addcomma\addspace}%
\printfield{note}%
\setunit{\addcomma\addspace}%
\usebibmacro{date-full}%
\newunit\newblock
\iffieldundef{xref}%
	{%then no xref; just print what's likely to be available in the customd
		\iffieldundef{booktitle}
			{%then improvise
				\printfield{howpublished}%
				\setunit*{\addcomma\addspace}%
				\printlist{organization}%
				\setunit*{\addcomma\addspace}%
				\printlist{institution}%
				\setunit*{\addcomma\addspace}%
				\printfield{library}%
				\setunit*{\addcomma\addspace}%
				\printfield{userd}%
				\newunit\newblock
				\usebibmacro{volume+pages}%
				}%
			{%else assume it's from a published collection
				\usebibmacro{in:}%
				\usebibmacro{getbookinfo}%
				}% 
		\newunit\newblock
		\usebibmacro{onlinelocation}%
		}%
	{%else x-ref to collection or customa or online
		\printtext{\unspace}%Just to force the unit punctutation before the xref
		%Save page range for bibliography
			\savefield{pages}{\childpages}%
			\savefield{volume}{\childvolume}%
			\savefield{part}{\childpart}%
		\iffootnote
			{\cbx@crosstoarch{\thefield{xref}}}%
			{\bbx@crosstoarch{\thefield{xref}}}%
		\newunit\newblock
		\printfield{userd}%
		\newunit\newblock
		\iffootnote
			{\usebibmacro{volume+pages}%
			\newunit\newblock}%
			{\relax}%Volume, pages should already have been printed
		}%endif, end xref routine
\newunit\newblock
\usebibmacro{onlinelocation}%
\newunit\newblock
\printfield{addendum}%
\newunit\newblock
\usebibmacro{pageref}%
\usebibmacro{finentry}%
}%end customd driver

%%%%%	CUSTOMD AS FALLBACK TYPE		%%%%%

\DeclareBibliographyAlias{*}{customd}
  
%%%%%      OTHER TYPES, NOT EXPLICITLY SUPPORTED      %%%%%%

\DeclareBibliographyAlias{review}{article}
\DeclareBibliographyAlias{suppbook}{inbook}
\DeclareBibliographyAlias{suppcollection}{incollection}
\DeclareBibliographyAlias{suppperiodical}{article}


%%%%%	SUBROUTINES	%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


%----------------------------------------------------------------------------------
%					SUBROUTINES OF INBOOK/INCOLLECTION, etc.
%
%For adding book data to an inbook, incollection, or other subordinate record. (One set of routines, for any combination of entrytypes.)
%---------------------------------------------------------------------------------

%Get rid of colon before booktitle in incollection and allow other prepositions, depending on subtype
\renewbibmacro*{in:}{%
\ifthenelse{\equal{\thefield{entrysubtype}}{\subtypeintro}}%
	{%Then1:`to' instead of `in'
		\setunit{\addspace\bibstring{introto}\addspace}}%
	{%Else1
		\ifthenelse{\equal{\thefield{entrysubtype}}{\subtypeexcerpt}}%
			{%Then2 ``from''
				\setunit{\addspace\bibstring{excerptfrom}\addspace}}%
			{%Else2
				\ifthenelse{\equal{\thefield{entrysubtype}}{\subtypevolume}%
					\OR\equal{\thefield{entrytype}}{\entrytypebookinbook}}%
					{%Then3 ``volume...of''
						\setunit{\addspace\bibstring{volumeof}\addspace}}%
					{%Else3
						\ifthenelse{\equal{\thefield{entrysubtype}}{\subtypenone}%
							\or {\equal{\thefield{entrysubtype}}{\subtypemag}}%
							\or {\equal{\thefield{entrytype}}{\entrytypearchive}}%
							\or {\equal{\thefield{entrysubtype}}{\subtypenewsp}}%
							\or {\equal{\thefield{entrysubtype}}{\subtypevideo}}%
							\or {\equal{\thefield{entrytype}}{\entrytypeonline}}%
							\or {\equal{\thefield{entrytype}}{\entrytypeaudio}}%
							\or {\equal{\thefield{entrysubtype}}{\subtypeearlybook}}}%
							{%Then4 no linking preposition at all
								\relax}%
							{%Else4 check for special case after an xref
								\iftoggle{xrefflag}
									{%Then5 have to capitalize manually in bibliography
										\ifbibliography
											{\bibcpstring{in}\addspace}%
											{\bibstring{in}\addspace}}%
									{%else5 default ``in''
										\bibstring{in}\addspace%
									}%end else5
							}%end else4
					}%end else3
			}%end else2
	}%end else1
}%end macro in:

\newbibmacro{getbookinfo}{%------------------------------------------------------
	%Booktitle and Editor Block (including special fields for proceedings)
	\usebibmacro{getbooktitle}%
	\setunit{\addcomma\addspace}%
	%Special fields from entrytypes proceedings or inproceedings
	\usebibmacro{getproceedingsfields}%
	\setunit{\addcomma\addspace}%
	%Now find all the book-level authors and editors
	\usebibmacro{getbookauthoreditor}%
	\newunit\newblock
	%Maintitle Block, including page numbers ---------------------------------------
	%Check whether the book is part of a multivolume set
	\iffieldundef{maintitle}
		{%No maintitle (or maintitle was used instead of booktitle, then cleared)
			\relax}%
		{%\ifthenelse{\iffieldundef{title}\and\iffieldundef{titleaddon}}
			{%no separate volume title (or titleaddon for untitled excerpts) 
				\relax}%
			{\usebibmacro{booktitle+maintitle-multivolume}}}%
	\setunit{\addcomma\addspace}%
	\iffootnote
		{%then pages will come from the postnote field
			\relax}%
		{%else restore child's volume and page range for bibliography
			\restorefield{pages}{\childpages}%
			\iftoggle{volumeprinted}
				{\relax}%
				{\restorefield{volume}{\childvolume}%
				\restorefield{part}{\childpart}}%
			\usebibmacro{volume+pages}%
			}%
	\newunit\newblock
	%Edition Block------------------------------------------------------
	\printfield{edition}%
	\newunit\newblock
	%Series Block (including note field) --------------------------------
	\iftoggle{printseriesflag}%Print series info only when option is set
		{\iffieldundef{series}
			{\relax}%
			{\usebibmacro{series+number}%
			\setunit{\addcomma\addspace}}}%
		{\relax}%
	\printfield{note}%
	\newunit\newblock
	%Special fields-----------------------------------------------------
	\printfield{type}%
	\newunit
	\printfield{version}%
	\newunit\newblock
	%Location: Publisher, Year Block ------------------------------------------
	\ifthenelse{\equal{\thefield{entrysubtype}}{\subtypeearlybook}}
		{%Then no publishing data for early English literature
			\relax}%
		{%Else full publishing data, in parens in footnotes
			\iffootnote%
				{%then in parens
					\setunit{\addspace}%
					\printtext{\bibleftparen}%
					\usebibmacro{publisher+location+year}%
					\printtext{\bibrightparen}}%
				{%else no parens in bibliography
					\usebibmacro{publisher+location+year}}%
			}%
}%end macro getbookinfo

%Choose appropriate book-title routine for incollection-like entries
\newbibmacro{getbooktitle}{%----------------------------------------------------
\iftoggle{xrefflag}%
	{%then record is book/collection-like, accessed via xref: find title in title field.
		\usebibmacro{title}%
		}%endthen; end of xref routine
	{%else current record inbook/incollection-like; look in booktitle field
		\iffieldundef{booktitle}
			{%then user must have filled in maintitle instead of booktitle
				\usebibmacro{title+maintitle}%
				\clearfield{maintitle}%Prevents repetition later, in case of multivolume sets
				}%
			{%else booktitle available, but might be redundant
				\iffieldsequal{booktitle}{title}%
					{%then no distinct booktitle after all; again, use maintitle
						\usebibmacro{title+maintitle}%
						\clearfield{maintitle}%Prevents repetition later, in case of multivolume sets
						}%
					{%else normal case with distinct title and booktitle
						\usebibmacro{booktitle+maintitle}%
						}%
				}%endelse; endiffieldundef
		}%
}%end macro getbooktitle
    
\newbibmacro{getbookauthoreditor}{%--------------------------------------------------
	\ifnameundef{bookauthor}
		{%then1 No bookauthor in current record; either it's not an inbook or there's no distinct part author. Check whether current author is different from saved author from the inbook record
			\ifnameequals{author}{\childauthor}%
				{%then2 no need to print the same name again
					\relax}%
				{%else2 current record is the book and has a different overall author from the inbook
					\setunit*{\addcomma\addspace\bibstring{byline}\addspace}%
					\printnames[byauthor]{author}%
				}%end else2, end ifnameequals
		}%end then1
		{%else1 current record has a book author; must be an inbook. Print the bookauthor if distinct from the part author (the bybookauthor macro will compare)
			\setunit{\addcomma\addspace\bibstring{byline}\addspace}%
			\usebibmacro{bybookauthor}%Redefined below
		}%end else1, end iffieldundef
	\setunit{\addcomma\addspace}%
	\usebibmacro{byeditor+others}%
}%end macro getbookauthoreditor

\renewbibmacro*{bybookauthor}{%--------------------------------------------
  \ifnamesequal{author}{bookauthor}
    {%No distinct bookauthor
    		\relax}%
    {%Print book author, firstname first
    		\printnames[byeditor]{bookauthor}}%
}%end macro bybookauthor

\newbibmacro{getproceedingsfields}{%---------------------------------------------
%For now, they will be printed whenever they are present--no test for entrytype
\ifthenelse{\equal{\thefield{editortype}}{\edtypecorp}}
	{%then corporate editor already printed and organization redundant
  		\relax}%
  	{%else print the name of the organization
		\printlist{organization}}%
\setunit{\addcomma\addspace}%
\printfield[titlecase]{eventtitle}%
\setunit{\addcomma\addspace}%
\printfield{venue}%
\setunit{\addcomma\addspace}%
\iffieldundef{eventdate}
	{\relax}%
	{\printeventdate}%
}%end macro

%For inbook and incollection, when the parent is in a multivolume work.
\newbibmacro*{booktitle+maintitle}{%-------------------------------------------------
\iffieldsequal{maintitle}{booktitle}
	{%then maintitles are superfluous
		\clearfield{maintitle}%
    	\clearfield{mainsubtitle}%
		\clearfield{maintitleaddon}}%
    {%else
    		\iffieldundef{maintitle}
    			{%Then: only one title--but is reference to a volume or the whole work?	
					\usebibmacro{booktitle}%
					\iffootnote
						{\setunit{\addspace}%
						\printfield[parens]{userb}}%
						{\newunit
						\printfield{userb}}%
					\setunit{\newunitpunct}%
					\iffieldundef{volume}
						{%Then: no volume number, so must be an independent work
							\printfield{volumes}}%
						{%Else: volume specified, so give the volume- and part numbers
							\ifthenelse{\equal{\thefield{entrytype}}{\entrytypecoll}%
							\OR\equal{\thefield{entrytype}}{\entrytypebook}
							\OR\equal{\thefield{entrytype}}{\entrytyperef}
							\OR\equal{\thefield{entrytype}}{\entrytypeproc}}%
								{\printfield{volume}\printfield{part}%
								\clearfield{volume}\clearfield{part}}%
							{\relax}%
						}%endiffieldundef volume
					}%endthen
     		{%Else: Separate maintitle and volume title, so give  volume title here. Maintitle will come after the editor
				\usebibmacro{booktitle}%
				\iffootnote
					{\setunit{\addspace}%
					\printfield[parens]{userb}}%
					{\newunit
					\printfield{userb}}%
	 			}%Endif
		}%Endif
}%end macro booktitle+maintitle


\newbibmacro{booktitle+maintitle-multivolume}{%-------------------------------------
%Prints volume-titles and maintitles of multivolume incollections; 
% special case of book excerpt in an anthology also handled here
	\iffieldundef{volume}
		{%then print `in Maintitle'
			\setunit{\newunitpunct\addspace\bibstring{in}\addspace}%
			}%
		{%else print `vol. 1 of Maintitle'
			\printfield{volume}%
			\printfield{part}%
			\setunit{\addspace\bibstring{volumeof}\addspace}%
			\toggletrue{volumeprinted}%Something wrong. Doesn't work globally, so clear everything
			\clearfield{volume}\clearfield{part}\savefield{volume}{\childvolume}\savefield{part}{\childpart}%
			}%
	\usebibmacro{maintitle}%
	%Setunit in subroutine
	\usebibmacro{collectioneditor}%
}%end nmacro booktitle+maintitle-multivolume



%----------------------------------------------------------------------
%							GETBOOKINFO-SHORT
%
%	For adding book-level data from the reference entrytype
%   (Used when crossreferencing, under the shortincoll option)
%----------------------------------------------------------------------
\newbibmacro*{getbookinfo-short}{%
\ifnameundef{labelname}
    {\printfield{label}}%
    {\printnames{labelname}}%
\ifsingletitle
    {\relax}%
    {\setunit*{\addcomma\space}%
	\printtext[title]{\printfield[titlecase]{labeltitle}}}%
}%end macro getbookinfo-short


%-----------------------------------------------------------------------------------
%						SUBROUTINES OF BOOK, COLLECTION, etc.
%
%	Publisher-Location-Year: Generalized routines for original publishing data and reprint data, from all the book-like entrytypes.
%-----------------------------------------------------------------------------------

\newbibmacro*{publisher+location+year}{%---------------------------------------------
%Check whether the entry is a reprint (i.e., whether it has ``orig'' data)
\ifthenelse{\equal{\reprintoption}{\optionnoreprints}}
	{\relax}%
	{\usebibmacro{reprinttest}}%
%(New unit has been set in the subroutine)
%Print location, publisher, and year
\iflistundef{location}
	{%Then no location, no automatic n.p., no colon
		\relax}%
	{%Else location and colon
		\usebibmacro{printlocation}%
		\midsentence% In case of question mark after location
		\setunit*{\addcolon\addspace}%
		}%
\iflistundef{publisher}
	{%then use institution instead of publisher
		\iflistundef{institution}
			{%then no institution either; try howpublished
				\iffieldundef{howpublished}
					{%then no publishing data available
						\iflistundef{location}
							{%then publisher, location both blank
								\iffootnote
									{%then no puctuation before date 
										\setunit{\unspace}}%
									{%else
										\newunit}%
								}%
							{%else location, but no publisher
								\setunit{\addcomma\addspace}}%
						}%
					{%else use howpublished field
						\printfield{howpublished}%
						\setunit{\addcomma\addspace}}%
				}%
			{%else institution instead of publisher
				\printlist{institution}%
				\setunit{\addcomma\addspace}%
				}%
		}%
	{%else use publisher field and ignore howpublished and institution
		\printlist{publisher}%
		\setunit{\addcomma\addspace}%
		}%
\usebibmacro{year}%
}%end publisher+location+year

\newbibmacro{printlocation}{%-------------------------------------------------
\ifthenelse{\equal{\thelist{location}}{\noplace}}%Something wrong here. Can't get it to recognize ``np'' in the location list
	{\bibstring{noplace}}%
	{\printlist{location}\isdot}%
}%end macro printlocation

\newbibmacro{reprinttest}{%----------------------------------------------------
\toggletrue{origdataflag}%
\iffieldundef{origyear}%
	{%then no origyear
		\iflistundef{origlocation}%
			{%then no origyear or origlocation
				\iflistundef{origpublisher}
					{%then not enough orig data for default reprint routine
						\iffieldundef{origtitle}%
							{%then not enough orig data for the other options, either
								\togglefalse{origdataflag}}%
							{%else
								\relax}}%
					{%else origpublisher, but no location; entry can still be a reprint
						\usebibmacro{reprintorigfirst}}}%
			{%else origlocation but no origyear; entry can still be a reprint
				\usebibmacro{reprintorigfirst}}}%
	{%else At least origyear available; entry is a reprint
			\usebibmacro{reprintorigfirst}}%
}%end macro reprinttest

\newbibmacro{reprintorigfirst}{%-----------------------------------------------
\ifthenelse{\not\equal{\reprintoption}{\optionorigfirst}}
	{%then original data will be added later (or not at all)
		\relax}%
	{%else default reprint routine: print original data first
		\printlist{origlocation}%
		\iflistundef{origpublisher}
			{\setunit*{\addcomma\addspace}%
				}%
			{\setunit*{\addcolon\addspace}%
			\printlist{origpublisher}%
			\setunit*{\addcomma\addspace}%
				}%
		\iffieldundef{origyear}
			{\bibstring{nodate}}%
			{\printorigdate}%
		\iffootnote
			{%then Footnote environment: use semicolon and abbreviation
				\addsemicolon\addspace\bibstring{reprint}}%
			{%else bibliography: use newunitpunct and long bibstring
				\newunit\bibstring{reprint}}%
		\setunit{\addcomma\addspace}
	}%End else
}%End macro reprintorigfirst

\newbibmacro{addoriginaledition}{%--------------------------------------------
%Check whether original data were entered
\iftoggle{origdataflag}
	{%Then1 introduce orig data with appropriate string
		\iffieldundef{origtitle}
			{%then2 without origtitle
				\iffootnote
					{\bibstring{origedition}}%
					{\bibstring{origeditionbib}}%
				\setunit*{\addcomma\addspace}%
				\printlist{origlocation}%
				\setunit*{\addcomma\addspace}%
				\printorigdate}%
			{%else2 with origtitle
				\iffootnote
					{\bibstring{origeditiontitled}}%
					{\bibstring{origeditiontitledbib}}%
				\setunit{\addcomma\addspace}
				\printtext[title]{\printfield[titlecase]{origtitle}}\addspace%
				\iflistundef{origlocation}
					{\mkbibparens{\printorigdate}}%
					{\printtext[parens]%
						{\printlist{origlocation}%
						\setunit*{\addcomma\addspace}%
						\printorigdate}}%
			}%end else2
		}%End then1
	{%Else1 nothing to print
		\relax
	}%endelse1
}%End macro addoriginaledition

\newbibmacro{addtransfrom}{%--------------------------------------------------
%Check whether original data were entered
\iftoggle{origdataflag}
	{%Then1 introduce orig data with appropriate string
		\iffieldundef{origtitle}
			{%then2 without origtitle
				\iffootnote
					{\bibstring{origedition}}%
					{\bibstring{origeditionbib}}%
				\setunit{\addcomma\addspace}%
				\printlist{origlocation}%
				\setunit*{\addcomma\addspace}%
				\printorigdate}%
			{%else2 with origtitle
				\iffootnote
					{\bibstring{translatedfromtitle}}%
					{\bibstring{translatedfromtitlebib}}%
				\setunit{\addspace}%
				\printtext[title]{\printfield[titlecase]{origtitle}}\addspace%
				\iflistundef{origlocation}
					{\mkbibparens{\printorigdate}}%
					{\printtext[parens]%
						{\printlist{origlocation}%
						\setunit*{\addcomma\addspace}%
						\printorigdate}}%
			}%end else2
	}%End then1
	{%Else1 nothing to print
		\relax}%
}%End macro addtransfrom


\newbibmacro{origtransas}{%--------------------------------------------------
% Swap orig-fields for normal fields
\savefield{title}{\temptitle}\savefield{origtitle}{\temporigtitle}%
\savelist{location}{\templocation}\savelist{origlocation}{\temporiglocation}%
\savelist{publisher}{\temppublisher}\savelist{origpublisher}{\temporigpublisher}%
\iffieldundef{date}
	{\savefield{pubstate}{\tempyear}}%
	{\savefield{date}{\tempdate}}%
\savefield{origdate}{\temporigdate}%
\restorefield{title}{\temporigtitle}\restorefield{origtitle}{\temptitle}%
\restorelist{location}{\temporiglocation}\restorelist{origlocation}{\templocation}%
\restorelist{publisher}{\temporigpublisher}\restorelist{origpublisher}{\temppublisher}%
\restorefield{origdate}{\tempdate}\restorefield{date}{\temporigdate}%
}%End macro origtransas

\newbibmacro{print-translation-data}{%----------------------------------------
	\ifnameundef{translator}
		{\ifbibliography
			{\setunit{\addspace\bibstring{translatedasbib}\addspace}}%
			{\setunit{\addspace\bibstring{translatedas}\addspace}}%
			}%
		{\ifbibliography
			{\bibstring{bytranslatorbib}\addspace}%
			{\bibstring{bytranslator}}%
			\printnames[byauthor]{translator}
			\setunit{as\addspace}}%
	\printtext[title]{\printfield[titlecase]{origtitle}}%
	\ifthenelse{\iflistundef{origlocation}\and\iflistundef{origpublisher}\and\iffieldundef{origyear}}
		{%then no more orig-data
			\relax}%
		{%else orig-data go in parentheses
			\addspace\bibleftparen
			\printlist{origlocation}%
  			\iflistundef{origpublisher}
   				{\setunit*{\addcomma\space}}
				{\setunit*{\addcolon\space}}%
			\printlist{origpublisher}%
			\setunit*{\addcomma\space}%
			\printorigdate
			\bibrightparen}%
}%end macro print-translation-data


%----------------------------------------------------------------------
%							GETARCHIVEINFO 
%
%	For adding collection data from customa or other parent entrytype
%   (Used when crossreferencing from customd or letter)
%----------------------------------------------------------------------
\newbibmacro{getarchiveinfo}{%-----------------------------------------
\usebibmacro{bibindex}%
%\usebibmacro{in:}%
\ifthenelse{\equal{\thefield{entrytype}}{\entrytypearchive}}%
	{%Then archive: print name of collection, first name first, then use main archive routine
		\printnames[byeditor]{author}%
		\setunit{\addspace}%
		\printfield{nameaddon}%
		\usebibmacro{getcustomainfo}}%End main archive routine
	{%Else not an archive
		\ifthenelse{\equal{\thefield{entrytype}}{\entrytypeonline}}%
			{%Then it's an online database
				\usebibmacro{title}%
				\newunit\newblock
				\usebibmacro{byeditor}%
				\newunit\newblock
				\printfield{note}%
				\newunit\newblock
				\printlist{organization}%
				\newunit\newblock
				\iftoggle{xrefflag}
					{\relax}%
					{\usebibmacro{onlinelocation}
					\newunit\newblock}%
				\printfield{addendum}%
			}%
			{%Else use collection routine
				\usebibmacro{getbookinfo}}%
	}%Endif
}%End macro getarchiveinfo

%-----------------------------------------------------------------------------
%			TITLE and SERIES Routines (for all booklike, inbook-like entrytypes)
%
%-----------------------------------------------------------------------------

\newbibmacro*{title+maintitle}{%------------------------------------------
%Title (from Title-field) already printed. See if there is a maintitle, too.
\iffieldsequal{maintitle}{title}
	{%Then maintitle superfluous
		\clearfield{maintitle}\clearfield{mainsubtitle}\clearfield{maintitleaddon}}%
	{%Else distinct maintitle
		\relax}%
\iffieldundef{maintitle}
	{%Then there's only one title--but is it a volume or an entire work?
		\iffieldundef{volume}
			{%Then: no volume number, so must be an entire work. Give no. of volumes
				\iftoggle{volumeprinted}
					{\relax}%exception for subtype volume after x-ref
					{\printfield{volumes}}%
				}%
			{%Else: a volume no. is specified, so give the volume- and part numbers
				\printfield{volume}\printfield{part}%
				}%
		}%
  	{%Else Two-level title: print `vol. 1 of maintitle'
		\printfield{volume}\printfield{part}%
		\setunit{\addspace\bibstring{volumeof}\addspace}%
		\usebibmacro{maintitle}%
		}%
}%end macro title+maintitle


\renewbibmacro*{series+number}{%---------------------------------------------
  \printfield[titlecase]{series}%
  \setunit*{\addspace}%
  \printfield{number}%
  \newunit
}%end macro series+number

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%----------------------------------------------------------------------------
%					SUBROUTINES FOR ARTICLES, PERIODICALS
%
%----------------------------------------------------------------------------
%Put in city for newspapers, but print it in Roman
\renewbibmacro*{journal}{%---------------------------------------------------
\iffieldundef{journaltitle}
	{%then
		\relax}%
	{%else
		\printtext[noformat]
			{\printfield[journaltitle]{journaltitle}%
			\setunit{\subtitlepunct}%
			\printfield[journaltitle]{journalsubtitle}
			\iflistundef{location}
				{\relax}%
				{\setunit{\addspace}
				\printtext{\bibleftparen}
				\unspace
      		 	\printlist[location]{location}
				\unspace\printtext{\bibrightparen}
				}%end iflistundef
			\unspace
			}%end printtext
	}% end iffieldundef
}%end macro journal
	
\renewbibmacro*{journal+issuetitle}{%------------------------------------------------
%Restore any location data from the child (aritcle) record that is undefined in the parent (periodical). 
\iffieldundef{issuetitle}
	{\restorefield{issuetitle}{\childissuetitle}%
	\restorefield{issuesubtitle}{\childissuesubtitle}}%
	{\relax}%
\iffieldundef{issuetitle}
	{\relax}%
	{\usebibmacro{issue}%Prints issuetitle
	\setunit{\addcomma\addspace}%
	\usebibmacro{bytranslator+others}%Use translator and editors from parent record in any case
    \setunit{\addcomma\addspace}%
	\usebibmacro{byeditor+others}%
	\newunit}%
\iffieldundef{note}
	{\restorefield{note}{\childnote}}%
	{\relax}%
\iffieldundef{note}
	{\relax}%
	{\printfield{note}%
	\setunit{\addcomma\addspace}}%
\ifthenelse{\equal{\thefield{entrytype}}{\entrytypeper}}%
	{\usebibmacro{periodical}}%
	{\usebibmacro{journal}}%
\setunit{\addcomma\addspace}%
\iffieldundef{entrysubtype}%
	{\restorefield{entrysubtype}{\childsubtype}}%
	{\relax}%
\ifthenelse{\equal{\thefield{entrysubtype}}{\subtypemag}%
	\OR \equal{\thefield{entrysubtype}}{\subtypenewsp}}%
	{%Then cite magazines and newspapers by date, without parens
		\usebibmacro{issue+date-mag}}%
	{%Else default routine, with parens
		\iffieldundef{series}
			{\restorefield{series}{\childseries}}%
			{\relax}%
		\iffieldundef{series}
			{\setunit*{\addspace}}%
			{\setunit{\addcomma\addspace}%
			\printfield{series}%
			\setunit{\addcomma\addspace}}%
		\iffieldundef{volume}
			{\restorefield{volume}{\childvolume}}%
			{\relax}%
		\printfield{volume}%
		\iffieldundef{number}
			{\restorefield{number}{\childnumber}}%
			{\relax}%
		\iffieldnums{number}
			{%then could be a single number or a range
				\iffieldnum{number}
					{%then use singular of bibstring
						\setunit{\addcomma\addspace\bibstring{number}\addspace}}%
					{%else use the plural
						\setunit{\addcomma\addspace\bibstring{numbers}\addspace}}%
				}%
			{%else not a number at all; leave out the bibstring
				\setunit{\addcomma\addspace}%
				}%
		\printfield{number}%
		\setunit{\addspace}%
		\iffieldundef{issue}
			{\restorefield{issue}{\childissue}}%
			{\relax}%
		\iffieldundef{year}
			{\restorefield{year}{\childyear}}%
			{\relax}%
		\iffieldundef{date}
			{\restorefield{date}{\childdate}}%
			{\relax}%
		\usebibmacro{issue+date}%
	}%
}%end macro journal+issuetitle

\renewbibmacro*{issue+date}{%---------------------------------------------
\printtext[parens]{%
	\iffieldundef{issue}
      {\usebibmacro{date-full}}%
      {\printfield{issue}%
       \setunit{\addspace}%
       \usebibmacro{year}}}%
  \newunit}%end macro issue+date

\newbibmacro*{issue+date-mag}{%---------------------------------------------
	\printtext[noformat]{%Get rid of parens
		\iffieldundef{issue}
			{\usebibmacro{date-full}}%
			{\printfield{issue}%
			\setunit{\addspace}%
			\usebibmacro{year}}%
		\newunit}%
}%end macro issue+date-mag



%For entrytype periodical: adjust punctuation
\renewbibmacro*{title+issuetitle}{%-----------------------------------------
\iffieldundef{issuetitle}
	{%then untitled issue
		\relax}%
	{%else special case of issue with own title and/or editor
		\usebibmacro{issue}%
		\setunit{\addcomma\addspace}%
		\usebibmacro{byeditor+others}%
		\newunit}%
\iffieldundef{note}
	{}%
	{\printfield{note}\setunit{\addcomma\addspace}}%
\usebibmacro{periodical}%
\ifthenelse{\equal{\thefield{entrysubtype}}{\subtypemag}%
			\OR\equal{\thefield{entrysubtype}}{\subtypenewsp}}%
	{%then special treatment for newspapers and magazines
		\newunit
		\usebibmacro{issue+date-mag}%
		}%endthen; end newsp/mag routine
	{%else standard journal article
		\iffieldundef{series}
			{\setunit*{\addspace}}%
			{\setunit{\addcomma\addspace}%
			\printfield{series}%
			\setunit{\addcomma\addspace}}%
		\printfield{volume}%
		\iffieldnum{number}
			{\setunit{\addcomma\addspace\bibstring{number}\addspace}}%
			{\iffieldnums{number}
				{\setunit{\addcomma\addspace\bibstring{numbers}\addspace}}%
				{\relax}}%
		\printfield{number}%
		\setunit{\addspace}%
		\usebibmacro{issue+date}%
		}%end else; end journal article routine
	}%end macro title+issuetitle

\renewbibmacro*{periodical}{%
  \iffieldundef{title}
    {}%
    {\printtext[]{%
       \printfield[]{title}%
       \setunit{\subtitlepunct}%
       \printfield[]{subtitle}}}}

%-----------------------------------------------------------------------------
%						ONLINE LOCATORS (all entrytypes)
%
%-----------------------------------------------------------------------------

\newbibmacro{onlinelocation}{%-----------------------------------------------
\iftoggle{urllastflag}
	{%then special case (from footciteurllast command); postnote comes first
		\usebibmacro{cite:postnote}}%
	{%else let cbx do the postnote as usual
		\relax}%
\ifthenelse{\equal{\thefield{entrysubtype}}{\subtypeonline}%
	\OR\equal{\thefield{entrytype}}{\entrytypeonline}}%
	{%then override the option settings for these entrytypes/subtypes
		\printfield{doi}%
		\newunit\newblock%
		\usebibmacro{eprint}%
		\newunit\newblock%
		\iffieldundef{url}%
			{\relax}%
			{\usebibmacro{url+urldate}}%
		}%end then; end override
	{%else use standard routine for online locators
		\usebibmacro{doi+eprint+url}%
	}%end ifthenelse
}%end macro onlinelocation

%--------------------------------------------------------------------------------
%			LOWER-LEVEL ROUTINES, for title, author, editor, year, pubstate
%
%--------------------------------------------------------------------------------

%Standard routine for titles. 
\renewbibmacro*{title}{%---------------------------------------------------------
%Substitute shorttitle where required
\iffootnote
	{\iffieldundef{shorttitle}
		{\relax}%
		{\iftoggle{useshorttitles}%
			{%then shorttitle option is set: replace title with shorttitle
				\clearfield{title}\clearfield{subtitle}%
				\savefield{shorttitle}{\temptitle}%
				\restorefield{title}{\temptitle}}%
			{%else check entrysubtype
				\ifthenelse{\equal{\thefield{entrysubtype}}{\subtypeearlybook}
							\or\equal{\thefield{entrytype}}{\entrytyperef}}
					{%Use shorttitles here, too
						\clearfield{title}\clearfield{subtitle}%
						\savefield{shorttitle}{\temptitle}%
						\restorefield{title}{\temptitle}}%
					{\relax}%
			}%end iftoggle
		}%end iffeldundef
	}%end then, end footnote routine		
	{\relax}% No shorttitles in bibliography
\iffieldundef{title}%
	{%then print titleaddon even without title (e.g. for describing untitled works), without preceding punctuation
		\printtext[titlecase]{\printfield{titleaddon}}\isdot%
		\setunit{\addspace}%
		\printfield{usera}}%Special use of usera for year of anthologized article
  	{%else both title and titleaddon
		\ifthenelse{\equal{\thefield{entrysubtype}}{\subtypevolume}
			\OR\equal{\thefield{entrysubtype}}{\subtypepublicdocument}
			\OR\equal{\thefield{entrytype}}{\entrytypebookinbook}}%
			{%then special case, when incollection/inbook is a whole volume or a government document whose title needs to be italicized
				\printtext[volumetitle]{%
					\printfield[titlecase]{title}\isdot%
					\iffieldundef{subtitle}
						{\relax}%
						{\setunit*{\subtitlepunct}%
					\printfield[titlecase]{subtitle}}%
					}%end printtext
				}%end then, end subtype volume
			{%else Other subtypes without italics
				\printtext[title]{%
					\printfield[titlecase]{title}\isdot%
					\iffieldundef{subtitle}
						{\relax}%
						{\setunit*{\subtitlepunct}%
					\printfield[titlecase]{subtitle}}}%
				}%end ifthenelse
		\iffieldundef{usere}
			{\relax}%
			{\setunit*{\addspace}\printtext[brackets]{\printfield[titlecase]{usere}}\setunit*{\addcomma\addspace}}%
		\iffieldundef{titleaddon}
			{\relax}%
			{\newunit\printtext[titlecase]{\printfield{titleaddon}}\isdot}%
		\iffieldundef{usera}
			{\relax}%
			{\iffootnote
				{\setunit*{\addspace}%
				\printfield[parens]{usera}}%
				{\setunit*{\newunitpunct}%
				\printfield{usera}}}%
	}%end else, end iffieldundef{title}
}%end macro title

\renewbibmacro*{booktitle}{%
\iffieldundef{booktitle}%
	{%then
		\printtext[titlecase]{\printfield{booktitleaddon}}\isdot
		}%end then
	{%else
		\printtext[booktitle]{%
			\printfield[titlecase]{booktitle}%
			\setunit{\subtitlepunct}%
			\printfield[titlecase]{booksubtitle}}%
		}%end if
	\iffieldundef{booktitleaddon}
		{\relax}%
		{\setunit{\addcomma\addspace}%
		\printtext[titlecase]{\printfield{booktitleaddon}}\isdot}%
}%end macro booktitle


%Also print maintitleaddon even without title, and capitalize in bib
\renewbibmacro*{maintitle}{%------------------------------------------------------
\iffieldundef{maintitle}%
	{%then
		\printtext[titlecase]{\printfield{maintitleaddon}}\isdot
		}%end then
	{%else
		\printtext[maintitle]{%
			\printfield[titlecase]{maintitle}%
			\setunit{\subtitlepunct}%
			\printfield[titlecase]{mainsubtitle}}%
		}%end if
	\iffieldundef{maintitleaddon}
		{\relax}%
		{\setunit{\addcomma\addspace}%
		\printtext[titlecase]{\printfield{maintitleaddon}}\isdot}%
}%end macro maintitle
 
%Handling anonymous/uncertain authorship; nameaddon
\renewbibmacro*{author}{%--------------------------------------------------------
\ifthenelse{\ifuseauthor\AND\NOT\ifnameundef{author}}
	{\ifthenelse{\iffieldequals{namehash}{\bbx@lasthash}\AND\NOT\iffirstonpage}
		{\toggletrue{namedashflag}}%
		{\togglefalse{namedashflag}%
		\savefield{namehash}{\bbx@lasthash}}%
	\iftoggle{useshortauthors}
		{\iffootnote%
			{\ifnameundef{shortauthor}
				{\relax}%
				{\savename{shortauthor}{\tempauthor}%
				\restorename{author}{\tempauthor}}%
			}%
			{\relax}}%
		{\relax}%
	\usebibmacro{authortype+nameaddon}}%
{\global\undef\bbx@lasthash}%
}%end macro author

\newbibmacro{authortype+nameaddon}{%--------------------------------------------
\ifuseauthor
	{%then check for anonymous, but known, authorship
		\ifthenelse{\equal{\thefield{authortype}}{\authortypeanon}}
			{%then anonymous author goes in brackets; usual punctuation outside brackets
				\iftoggle{namedashflag}
					{%then dash in place of name in bibliography
						\mkbibbrackets{\bibnamedash}}%
					{%else use name as usual
						\mkbibbrackets{\printnames{author}}%
					}%end iftoggle
				}%endthen; end anonymous
			{%else check for uncertain authorship
				\ifthenelse{\equal{\thefield{authortype}}{\authortypeunsure}}
					{%then question mark in brackets; usual punctuation outside brackets
						\iftoggle{namedashflag}
							{\mkbibbrackets{\bibnamedash\addquestion}}%
							{\mkbibbrackets{\printnames{author}\addquestion}}%
						}%endthen; end uncertain authorship
					{%else no brackets; check for redundant author and suppress in footnotes
						\ifthenelse {\equal{\thefield{authortype}}{\authortyperedundant}}
							{%then
								\iffootnote
									{%then don't print redundant author in footnote
										\relax}%
									{%else print author or dashes in bibliography, even if redundant
										\iftoggle{namedashflag}
											{\bibnamedash}%
											{\printnames{author}}%
									}%end iffootnote
								}%end then
							{%else authortype not redundant, just print as usual
								\iftoggle{namedashflag}
									{\bibnamedash}%
									{\unspace%Why is this necessary?
									\printnames{author}}%
							}%end ifthenelse redundant
					}%endifthenelse unsure
			}%endifthenelse anon; end uncertain/redundant authorship
	\iffieldundef{nameaddon}
		{\relax}%
		{\setunit*{\addspace}%
		\printfield{nameaddon}%
		\iffootnote
			{\ifpunctmark{.}{\isdot}{\relax}}%
			{\relax}%
		\setunit*{\labelnamepunct\addspace}%
			}%
		}%endthen (from ifuseauthor)
    {%else Author not in use
    		\relax
	}%end ifuseauthor
}%end macro authortype+nameaddon

\newbibmacro*{bynamea}{%-----------------------------------------------------------
%Needed for government reports that give the institution as the author, but also
% credit an individual, whose name goes in namea
  \ifnameundef{namea}
    {\relax}
    {\bibstring{byauthor}%
     \setunit{\addspace}%
     \printnames[byauthor]{namea}}}%
    
\newbibmacro*{volume+pages}{%------------------------------------------------------
\iftoggle{volumeprinted}
	{\setunit{\addcomma\addspace}}%
	{\printfield[noformat]{volume}%
	\printfield{part}%
	\iffieldundef{volume}
		{\setunit{\addcomma\addspace}}%
  		{\setunit{\bibpagespunct}}}%
\printfield{pages}%
}%endmacro volume+pages


%Custom field for general editor of a collection containing an edited collection
%Needed when there is both a booktitle and a maintitle. The editora/b/c fields all go with the booktitle. Here nameb goes with the maintitle.
\newbibmacro{collectioneditor}{%----------------------------------------------------
\iffieldundef{namebtype}
	{%no descriptor in nametype field; use bibstring
%		\iffootnote
%			{\setunit{\addcomma\addspace\bibstring{byeditor}\addspace}}%
%			{\setunit{\addcomma\addspace\bibstring{byeditorbib}\addspace}}%
		\setunit{\addcomma\addspace\bibstring{byeditor}\addspace}%
		}%
	{%Get descriptor from nametype
		\setunit{\addcomma\addspace\thefield{namebtype}\addspace}}%
\printnames{nameb}%
}%endmacro collectioneditor



% Date routine, with pubstate and year as fallbacks
\newbibmacro*{date-full}{%---------------------------------------------------------------
	\ifthenelse{\iffieldundef{year}\and\iffieldundef{month}\and\iffieldundef{day}}
		{%then no date; try pubstate
			\iffieldundef{pubstate}%
				{%then nothing available: insert `n.d.'
					\bibstring{nodate}}%
				{%else'substitute pubstate for date
					\printfield{pubstate}}%
			}%end then
		{%else use the standard date routine
			\usebibmacro{date}%
			}%endif
	}%end macro date	
	

% Date routine, with pubstate and year as fallbacks
\newbibmacro*{date-optional}{%---------------------------------------------------------------
	\ifthenelse{\iffieldundef{year}\and\iffieldundef{month}\and\iffieldundef{day}}
		{%then no date; try pubstate
			\iffieldundef{pubstate}%
				{%then nothing available, but don't insert `n.d.'
					\relax}%
				{%else'substitute pubstate for date
					\printfield{pubstate}}%
			}%end then
		{%else use the standard date routine
			\usebibmacro{date}%
			}%endif
	}%end macro date-optional

%Date routine to leave out month and day
\newbibmacro{year}{%------------------------------------------------------------------
	\iffieldundef{year}%
		{%then no year; try pubstate
			\iffieldundef{pubstate}%
				{%then nothing available: insert `n.d.'
					\bibstring{nodate}}%
				{%else substitute pubstate for date
					\printfield{pubstate}}%
			}%
		{%else print the year (or range, if there's an endyear)
			\printfield{year}%
			\iffieldundef{endyear}
				{\relax}%
				{\setunit*{\bibrangedash}%
				\printfield{endyear}}%
			}%
	}% end macro year
	
	
%%%%%%%%  BIBLIOGRAPHY TWEAKS %%%%%%%%%%%%%%%%%%

%When author names repeat, Turabian calls for eight underlines and a period, instead of biblatex's dashes
\renewcommand*{\bibnamedash}{%
%  \ifdim\leftmargin<0.75em
%    \mbox{\textemdash\space}%
%  \else
%    \makebox[\leftmargin][l]{%
%      \ifdim\leftmargin<1.25em
%        \textendash
%      \else
%        \textemdash
%      \fi}%
%  \fi
%\rule[0pt]{8ex}{.5pt}\unspace
\bibstring{namedash}
  }

%%%%%	ANNOTATED BIBLIOGRAPHIES


\renewbibmacro*{finentry}{%
  \iffootnote
    {%then no annotations in the footnotes
    	\finentry}%
    {%else
    	\ifcitation
			{%then annotations in cite commands are handled in the cbx file
				\finentry}
			{%else bibliography environment: annotate if options are set
				\printtext{\finentrypunct}%
				\ifbool{bbx:annotation}
					{%then add annotation in special environment
						\begin{indentannote}
    						\usebibmacro{annotation}%
							\finentry
						\end{indentannote}%
						}%
    				{%else no annotations
    					\finentry}%
    			}%end bibliography
		}%end iffootnote
}%end macro finentry

\endinput