% Casper Ti. Vector's biblatex style
%
% Copyright (c) 2012-2019,2022 Casper Ti. Vector
%
% This work may be distributed and/or modified under the conditions of the
% LaTeX Project Public License, either version 1.3 of this license or (at
% your option) any later version.
% The latest version of this license is in
%   https://www.latex-project.org/lppl.txt
% and version 1.3 or later is part of all distributions of LaTeX version
% 2005/12/01 or later.
%
% This work has the LPPL maintenance status `maintained'.
% The current maintainer of this work is Casper Ti. Vector.
%
% This work consists of the following files:
%   caspervector.bbx
%   caspervector.cbx
%   caspervector-ay.bbx
%   caspervector-ay.cbx
%   blx-caspervector-base.def
%   blx-caspervector-gbk.def
%   blx-caspervector-utf8.def

\ProvidesFile{blx-caspervector-base.def}%
	[2024/03/07 v0.3.7 Casper Ti. Vector's biblatex library]

\RequireBiber[3]
\newtoggle{bbx:gbk}
\newtoggle{bbx:ugly}
\newtoggle{bbx:cparen}
\newtoggle{bbx:bfdate}\toggletrue{bbx:bfdate}
\DeclareBibliographyOption{utf8}[true]{\togglefalse{bbx:gbk}}
\DeclareBibliographyOption{gbk}[true]{\toggletrue{bbx:gbk}}
\DeclareBibliographyOption{ugly}[true]{\settoggle{bbx:ugly}{#1}}
\DeclareBibliographyOption{cparen}[true]{\settoggle{bbx:cparen}{#1}}
\ExecuteBibliographyOptions{%
	gbk, ugly = false, cparen = false, autopunct = false, alldates = iso,
	minnames = 3, minitems = 3, maxcitenames = 2, mincitenames = 1%
}
\AtEndOfPackage{%
	\iftoggle{bbx:gbk}{
		\input{blx-caspervector-gbk.def}
		\ExecuteBibliographyOptions{texencoding = GBK}
	}{
		\input{blx-caspervector-utf8.def}
		\ExecuteBibliographyOptions{texencoding = UTF-8}
	}%
	\iftoggle{bbx:ugly}{
		\newcommand*{\bbx@citecomma}{\addcomma\space}
		\newcommand*{\bbx@citebcomma}{\addcomma\space}
		\newcommand*{\bbx@citescolon}{\addsemicolon\space}
		\newcommand*{\bbx@cepunct}[2]{#2}
		\DefineBibliographyStrings{english}{
			andmore = {\bbx@cietext{\bbx@cnetal}{et\addabbrvspace al\adddot}},
			andothers = {\bbx@cietext{\bbx@cnetal}{et\addabbrvspace al\adddot}},
		}
		\DeclareFieldFormat*{booktitle}{#1}
		\DeclareFieldFormat*{journaltitle}{#1}
		\DeclareFieldFormat*{title}{#1}
		\DeclareFieldFormat*{volume}{#1}
		\newbibmacro*{typespace}{\setunit*{\addspace}}
	}{
		\newcommand*{\bbx@citecomma}{\bbx@cncomma}
		\newcommand*{\bbx@citebcomma}{\bbx@cnbcomma}
		\newcommand*{\bbx@citescolon}{\bbx@cncomma}
		\newcommand*{\bbx@cepunct}[2]{\iffieldequalstr{userf}{zh}{#1}{#2}}
		\DefineBibliographyStrings{english}{
			andmore =
				{\bbx@cietext{\bbx@cnetal}{\mkbibemph{et\addabbrvspace al\adddot}}},
			andothers =
				{\bbx@cietext{\bbx@cnetal}{\mkbibemph{et\addabbrvspace al\adddot}}},
		}
		\DeclareFieldFormat*{booktitle}{\mkbibemph{#1}}
		\DeclareFieldFormat*{journaltitle}{\mkbibemph{#1}}
		\DeclareFieldFormat*{title}{\mkbibemph{#1}}
		\DeclareFieldFormat*{volume}{\mkbibemph{#1}}
		\DeclareFieldFormat[inbook, inproceedings, incollection, article]%
			{title}{\bbx@bibquote{\mkbibemph{#1}}}
		\newbibmacro*{typespace}{\ifboolexpr{test {\bbx@cepunct} and (
			test {\ifentrytype{inbook}} or test {\ifentrytype{inproceedings}} or
			test {\ifentrytype{incollection}} or test {\ifentrytype{article}}
		)}{}{\setunit*{\addspace}}}
	}%
	\ifboolexpr{not test {\iftoggle{bbx:bfdate}} or test {\iftoggle{bbx:ugly}}}%
		{\DeclareFieldFormat*{date}{#1}}%
		{\DeclareFieldFormat*{date}{%
			\ifboolexpr{test {\iffieldundef{month}} and test {\iffieldundef{day}}}%
				{\mkbibbold{#1}}{#1}%
		}}%
}

\NewBibliographyString{cnauthor, cneditor, cntranslator}
\newcommand*{\bbx@cetext}[2]{\iffieldequalstr{userf}{zh}{#1}{#2}}
\newcommand*{\bbx@cietext}[2]{\ifboolexpr{
	test {\ifcitation} or test {\iffieldequalstr{userf}{zh}}
}{#1}{#2}}
\newcommand*{\bbx@cieparen}[2]{\ifboolexpr{
	test {\ifcitation} and test {\iftoggle{bbx:cparen}}
}{#1}{#2}}
\newcommand*{\bbx@ifstrg}[3]{\ifboolexpr{%
	test {\ifstrequal{#1}{author}} and test {\iffieldundef{authortype}}
}{#3}{#2}}
\newcommand*{\bbx@cecomma}{\bbx@cepunct{\bbx@cncomma}{\addcomma\addspace}}
\newcommand*{\bbx@cecolon}{\bbx@cepunct{\bbx@cncolon}{\addcolon\addspace}}
\newcommand*{\bbx@cescolon}{\bbx@cepunct{\bbx@cnscolon}{\addsemicolon\addspace}}
\newcommand*{\bbx@ceperiod}{\bbx@cepunct{\bbx@cnperiod}{\addperiod\addspace}}
\newcommand*{\bbx@bibquote}[1]%
	{\bbx@cepunct{\bbx@cnldquot{#1}\bbx@cnrdquot}{\mkbibquote{#1}}}
\renewcommand*{\finallistdelim}%
	{\bbx@cetext{\bbx@cecomma}{\addspace\bibstring{and}\space}}
\renewcommand*{\multilistdelim}{\bbx@cecomma}
\renewcommand*{\finalnamedelim}{\ifcitation{\bbx@citebcomma}{\finallistdelim}}
\renewcommand*{\multinamedelim}{\ifcitation{\bbx@citebcomma}{\multilistdelim}}
\renewcommand*{\postnotedelim}{\bbx@citecomma}
\renewcommand*{\finentrypunct}{\ifboolexpr{
	test {\iffieldundef{addendum}} and test {\iflistundef{pageref}}
}{\bbx@cepunct{\bbx@cnperiod}{.}}{}}
\DefineBibliographyExtras{english}{%
	\renewcommand*{\finalandcomma}{}%
}
\renewcommand*{\bibleftparen}{\bbx@cieparen{\bbx@cnlparen}{\blx@postpunct(}}
\renewcommand*{\bibrightparen}%
	{\bbx@cieparen{\bbx@cnrparen}{\blx@postpunct)\midsentence}}
\DeclareDelimFormat[textcite]{nameyeardelim}{\bbx@cieparen{}{\addspace}}
\DeclareDelimFormat[textcite]{nonameyeardelim}{\bbx@cieparen{}{\addspace}}

\DeclareFieldFormat*{pages}{#1}
\DeclareFieldFormat*{url}{\url{#1}}
\DeclareFieldFormat*{urldate}{#1}
\DeclareFieldFormat*{type}{\mkbibbrackets{#1}}
\DeclareFieldFormat*{number}{\mkbibparens{#1}}
\DeclareFieldFormat[patent]{number}{#1}
\DeclareFieldFormat*{edition}{%
	\bbx@cetext%
		{\ifinteger{#1}{{\bbx@cnnr} {#1} {\bbx@cnedition}}{#1}}%
		{\ifinteger{#1}{\mkbibordedition{#1}~\bibstring{edition}}{#1\isdot}}%
}
\DeclareFieldFormat*{mypageref}{\mkbibbrackets{\^{}\,#1}}
\newcommand*{\bbx@print}[1]{\iffieldundef{#1}{}{\printfield{#1}}}
\newbibmacro*{cntypedelim}[1]{\ifandothers{#1}{}{\printtext{\andothersdelim}}}
\newbibmacro*{cein:}%
	{\printtext{\bbx@cetext{\bbx@cnin}{In}\bbx@cecolon}\printtext{}}

\newbibmacro*{cnauedstrg}[2]{%
	\printtext[#1type]{\iffieldundef{#1type}{\bibstring{cn#2}}{%
		\ifbibxstring{cn\thefield{#1type}}%
			{\bibstring{cn\thefield{#1type}}}{\thefield{#1type}}%
	}}%
}
\newbibmacro*{cnauthorstrg}{\usebibmacro{cnauedstrg}{author}{author}}
\newbibmacro*{cneditorstrg}{\usebibmacro{cnauedstrg}{editor}{editor}}
\newbibmacro*{cntranslatorstrg}{\printtext{\bibstring{cntranslator}}}

\newbibmacro*{ceauedtrans}[1]{%
	\ifboolexpr{test {\iftoggle{blx@use#1}} and not test {\ifnameundef{#1}}}{%
		\printnames{#1}\bbx@ifstrg{#1}{%
			\bbx@cetext%
				{\usebibmacro{cntypedelim}{#1}\usebibmacro{cn#1strg}}%
				{\setunit{\printdelim{#1typedelim}}\usebibmacro{#1strg}}%
		}{}\ifstrequal{#1}{author}{}{\clearname{#1}}%
	}{}%
}
\renewbibmacro*{author}{\usebibmacro{ceauedtrans}{author}}
\renewbibmacro*{editor}{\usebibmacro{ceauedtrans}{editor}}
\renewbibmacro*{translator}{\usebibmacro{ceauedtrans}{translator}}
\newbibmacro*{bookauthor/editor}{%
	\ifnameundef{bookauthor}%
		% Differs from `\usebibmacro{editor}' in caspervector-ay.
		{\usebibmacro{ceauedtrans}{editor}}{\printnames{bookauthor}}%
}

\newbibmacro*{cebyauedbase}[2]{%
	\bbx@cetext{%
		\printnames[by#1]{#1}\usebibmacro{cntypedelim}{#1}%
		\usebibmacro{cnauedstrg}{#1}{#2}%
	}{%
		\usebibmacro{bytypestrg}{#1}{#2}%
		\setunit{\addspace}\printnames[by#1]{#1}%
	}%
}
\renewbibmacro*{byauthor}{%
	\ifboolexpr{test {\ifuseauthor} or test {\ifnameundef{author}}}%
		{}{\usebibmacro{cebyauedbase}{author}{author}}%
}
\renewbibmacro*{byeditor}{%
	\ifnameundef{editor}{}%
		{\usebibmacro{cebyauedbase}{editor}{editor}\setunit*{\bbx@cescolon}}%
	\ifnameundef{editora}{}%
		{\usebibmacro{cebyauedbase}{editora}{editor}\setunit*{\bbx@cescolon}}%
	\ifnameundef{editorb}{}%
		{\usebibmacro{cebyauedbase}{editorb}{editor}\setunit*{\bbx@cescolon}}%
	\ifnameundef{editorc}{}%
		{\usebibmacro{cebyauedbase}{editorc}{editor}\setunit*{\bbx@cescolon}}%
}
\renewbibmacro*{bytranslator}{%
	\ifnameundef{translator}{}{%
		\bbx@cetext{%
			\printnames[bytranslator]{translator}%
			\usebibmacro{cntypedelim}{translator}\usebibmacro{cntranslatorstrg}%
		}{%
			\printtext{\bibstring{bytranslator}}%
			\setunit{\addspace}\printnames[bytranslator]{translator}%
		}%
	}%
}
\newbibmacro*{byauthor+byeditor+bytranslator}{%
	\usebibmacro{byauthor}\setunit*{\bbx@cescolon}%
	\usebibmacro{byeditor}\setunit*{\bbx@cescolon}%
	\usebibmacro{bytranslator}%
}

\providebibmacro*{bbx:ifmergeddate}[2]{#2}
\newbibmacro*{journaltitle/title}{%
	\iffieldundef{journaltitle}%
		{\bbx@print{title}}{\printfield[title]{journaltitle}}%
}
\newbibmacro*{location+publisher+date+note+pages}{%
	\printlist{location}\setunit*{\bbx@cecolon}\printlist{publisher}%
	\ifboolexpr{test {\iflistundef{location}} and test {\iflistundef{publisher}}}%
		{}{\setunit{\bbx@cecomma}}%
	\usebibmacro{date}\setunit*{\bbx@cecomma}\printfield{note}%
	\ifboolexpr{
		(test {\usebibmacro{bbx:ifmergeddate}} or test {\ifdateunknown}) and
		test {\iffieldundef{note}}
	}{}{\setunit{\bbx@cecolon}}%
	\printfield{pages}%
}
\newbibmacro*{date+volume+number+note+pages}{%
	\usebibmacro{date}%
	\iffieldundef{volume}{}{\setunit*{\bbx@cecomma}\printfield{volume}}%
	\bbx@print{number}\setunit*{\bbx@cecomma}\printfield{note}%
	\ifboolexpr{
		(test {\usebibmacro{bbx:ifmergeddate}} or test {\ifdateunknown}) and
		test {\iffieldundef{volume}} and test {\iffieldundef{number}} and
		test {\iffieldundef{note}}
	}{}{\setunit{\bbx@cecolon}}%
	\printfield{pages}%
}
\newbibmacro*{eid+url+urldate}{%
	\printfield{eid}\setunit*{\bbx@cecomma}%
	\printfield{url}\setunit*{\bbx@cecomma}%
	\iffieldundef{urlyear}{}{%
		\printtext{\bbx@cetext{\bbx@cnretr}{accessed on}\addspace}%
		\printurldate%
	}%
}
\renewbibmacro*{pageref}{%
	\iflistundef{pageref}{}{%
		\bbx@cepunct{}{\setunit{\addspace}}%
		\printtext[mypageref]{\printlist[pageref][-\value{listtotal}]{pageref}}%
	}%
}
\newbibmacro*{addendum+pageref}{\printfield{addendum}\usebibmacro{pageref}}

\DeclareBibliographyDriver{book}{%
	\usebibmacro{bibindex}\usebibmacro{begentry}%
	\usebibmacro{author/editor/translator}%
	\printunit*{\bbx@ceperiod}\newblock
	\bbx@print{title}\usebibmacro{typespace}%
	\bbx@print{type}\printunit{\bbx@ceperiod}\newblock
	\usebibmacro{byauthor+byeditor+bytranslator}%
	\printunit{\bbx@ceperiod}\newblock
	\printfield{edition}\printunit{\bbx@ceperiod}\newblock
	\usebibmacro{location+publisher+date+note+pages}%
	\printunit{\bbx@ceperiod}\newblock
	\usebibmacro{eid+url+urldate}\printunit{\bbx@ceperiod}\newblock
	\usebibmacro{addendum+pageref}\usebibmacro{finentry}%
}

\DeclareBibliographyDriver{incollection}{%
	\usebibmacro{bibindex}\usebibmacro{begentry}%
	\usebibmacro{author/translator}\printunit*{\bbx@ceperiod}\newblock
	\bbx@print{title}\usebibmacro{typespace}%
	\bbx@print{type}\printunit{\bbx@ceperiod}\newblock
	\usebibmacro{byauthor}\setunit*{\bbx@cescolon}%
	\usebibmacro{bytranslator}\printunit{\bbx@ceperiod}\newblock
	\usebibmacro{cein:}\newblock
	\usebibmacro{bookauthor/editor}\printunit*{\bbx@ceperiod}\newblock
	\printfield{booktitle}\printunit{\bbx@ceperiod}\newblock
	\usebibmacro{byeditor}\printunit{\bbx@ceperiod}\newblock
	\printfield{edition}\printunit{\bbx@ceperiod}\newblock
	\usebibmacro{location+publisher+date+note+pages}%
	\printunit{\bbx@ceperiod}\newblock
	\usebibmacro{eid+url+urldate}\printunit{\bbx@ceperiod}\newblock
	\usebibmacro{addendum+pageref}\usebibmacro{finentry}%
}

\DeclareBibliographyDriver{periodical}{%
	\usebibmacro{bibindex}\usebibmacro{begentry}%
	\usebibmacro{author/editor/translator}%
	\printunit*{\bbx@ceperiod}\newblock
	\usebibmacro{journaltitle/title}\usebibmacro{typespace}%
	\bbx@print{type}\printunit{\bbx@ceperiod}\newblock
	\usebibmacro{byauthor+byeditor+bytranslator}%
	\printunit{\bbx@ceperiod}\newblock
	\usebibmacro{date+volume+number+note+pages}%
	\printunit{\bbx@ceperiod}\newblock
	\printlist{location}\setunit*{\bbx@cecolon}%
	\printlist{publisher}\printunit{\bbx@ceperiod}\newblock
	\usebibmacro{eid+url+urldate}\printunit{\bbx@ceperiod}\newblock
	\usebibmacro{addendum+pageref}\usebibmacro{finentry}%
}

\DeclareBibliographyDriver{article}{%
	\usebibmacro{bibindex}\usebibmacro{begentry}%
	\usebibmacro{author/translator}\printunit*{\bbx@ceperiod}\newblock
	\bbx@print{title}\usebibmacro{typespace}%
	\bbx@print{type}\printunit{\bbx@ceperiod}\newblock
	\usebibmacro{byauthor+byeditor+bytranslator}%
	\printunit{\bbx@ceperiod}\newblock
	\printfield{journaltitle}\setunit*{\bbx@cecomma}%
	\usebibmacro{date+volume+number+note+pages}%
	\printunit{\bbx@ceperiod}\newblock
	\usebibmacro{eid+url+urldate}\printunit{\bbx@ceperiod}\newblock
	\usebibmacro{addendum+pageref}\usebibmacro{finentry}%
}

\DeclareBibliographyDriver{patent}{%
	\usebibmacro{bibindex}\usebibmacro{begentry}%
	\usebibmacro{author/editor/translator}%
	\printunit*{\bbx@ceperiod}\newblock
	\bbx@print{title}\usebibmacro{typespace}%
	\bbx@print{type}\printunit{\bbx@ceperiod}\newblock
	\usebibmacro{byauthor+byeditor+bytranslator}%
	\printunit{\bbx@ceperiod}\newblock
	\printlist{location}\setunit*{\bbx@cecolon}%
	\printfield{number}\printunit{\bbx@ceperiod}\newblock
	\usebibmacro{date}\setunit*{\bbx@cecomma}%
	\printfield{note}\printunit{\bbx@ceperiod}\newblock
	\usebibmacro{eid+url+urldate}\printunit{\bbx@ceperiod}\newblock
	\usebibmacro{addendum+pageref}\usebibmacro{finentry}%
}

\DeclareBibliographyDriver{customf}{%
	\usebibmacro{bibindex}\usebibmacro{begentry}%
	\usebibmacro{addendum+pageref}\usebibmacro{finentry}%
}

\DeclareBibliographyAlias{booklet}{book}
\DeclareBibliographyAlias{collection}{book}
\DeclareBibliographyAlias{manual}{book}
\DeclareBibliographyAlias{misc}{book}
\DeclareBibliographyAlias{online}{book}
\DeclareBibliographyAlias{proceedings}{book}
\DeclareBibliographyAlias{report}{book}
\DeclareBibliographyAlias{thesis}{book}
\DeclareBibliographyAlias{unpublished}{book}
\DeclareBibliographyAlias{inbook}{incollection}
\DeclareBibliographyAlias{inproceedings}{incollection}
\DeclareBibliographyAlias{*}{book}

\DeclareSortingTemplate{ecnyt}{
	\sort{\field{presort}}
	\sort[final]{\field{sortkey}}
	\sort{
		\field{userf}
	}
	\sort{
		\field{sortname}
		\field{author}
		\field{editor}
		\field{translator}
		\field{sorttitle}
		\field{title}
	}
	\sort{
		\field{sortyear}
		\field{year}
	}
	\sort{
		\field{sorttitle}
		\field{title}
	}
	\sort{
		\field{volume}
		\literal{0}
	}
}

\DeclareSortingTemplate{cenyt}{
	\sort{\field{presort}}
	\sort[final]{\field{sortkey}}
	\sort[direction = descending]{
		\field{userf}
	}
	\sort{
		\field{sortname}
		\field{author}
		\field{editor}
		\field{translator}
		\field{sorttitle}
		\field{title}
	}
	\sort{
		\field{sortyear}
		\field{origyear}
		\field{year}
	}
	\sort{
		\field{sorttitle}
		\field{title}
	}
	\sort{
		\field{volume}
		\literal{0}
	}
}

\DeclareStyleSourcemap{
	\maps{
		\map{
			\step[
				fieldsource = language, match = \regexp{^chinese$},
				fieldset = userf, fieldvalue = {zh}
			]
		}
		\map{
			\step[fieldset = userf, fieldvalue = {en}]
		}
	}
}

% vim:ft=tex:ts=2:sw=2
