%% Copyright 2013-… Maïeul Rouquette
%
% 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
%   http://www.latex-project.org/lppl.txt
% and version 1.3 or later is part of all distributions of LaTeX
% version 2005/12/01 or later.
%
% This work has the LPPL maintenance status `maintained'.
%
% The Current Maintainer of this work is Maïeul Rouquette
% This work consists of the files manuscripts*.dbx, manuscripts*.bbx, manuscripts*.lbx

\ProvidesFile{manuscripts-commons}[2023/05/01 v2.1.4 biblatex bibliography style (MR)]%

\ifdef{\biblatexmultipledm@bibstyle}
	{\RequireBibliographyStyle{\biblatexmultipledm@bibstyle}}
	{\RequireBibliographyStyle{verbose}}

% This file contains code shared by manuscript.bbx and manuscript-noauthoshorthand.
% This file is organized from the must close of user to the less close.
% 1. Some commands to be redefined




\newcommand{\collectionshelfmarkpunct}{\addspace}
\newcommand{\datingpagespunct}{\addcomma\addspace}
\newcommand{\librarycollectionpunct}{\addcomma\addspace}


\newcommand{\mkcolumns}[1]{\addspace\mkbibparens{#1}}
\newcommand{\mklayer}[1]{\addspace\mkbibparens{#1}}
\newcommand{\mkcolumnslayer}[1]{\addspace\mkbibparens{#1}}%
\newcommand{\mklocation}[1]{\mkbibnamefamily{#1}}
\newcommand{\mkmanuscriptdescriptionlabel}[1]{\textbf{#1}\manuscriptdescriptionlabelpunct}
\newcommand{\mkmanuscriptdescriptionlabelparagraphed}[1]{\textbf{#1}\par}
\newcommand{\mkshcite}[1]{#1}

\newcommand{\locationlibrarypunct}{\addcolon\addspace}
\newcommand{\manuscriptdescriptionlabelpunct}{\addcolon\addspace}
\newcommand{\moreinterpunct}{\addsemicolon\addspace}
\newcommand{\pagetotalpagespunct}{\addcolon\addspace}
\newcommand{\columnslayerpunct}{\addsemicolon\addspace}%

\newcommand{\multidetailscitedelim}{\endgraf}

\def\recto{r}
\def\verso{v}



% 2. Some macros


\newbibmacro{manuscript:annotation}{%
    \printfield{annotation}%
}

\newbibmacro{manuscript:contents}{%
    \printfield{contents}%
}%
\newbibmacro{manuscript:more}{%
    \printlist{origin}%
    \setunit*{\moreinterpunct}%
    \printnames{scribe}%
    \setunit*{\moreinterpunct}%
    \printnames{owner}%
}
\newbibmacro{manuscript:more+annotation+catalog}{%
  \iftoggle{shorthand:details}{%
    \setunit{\finentrypunct\par}%
	\usebibmacro{manuscript:more}%
	\setunit{\finentrypunct\par}%
	\usebibmacro{manuscript:contents}%
	\setunit{\finentrypunct\par}%
	\usebibmacro{manuscript:annotation}%
	\setunit{\finentrypunct\par}%
	\usebibmacro{manuscript:catalog}%
  }{}%
}

\newbibmacro{manuscript:catalog}{%
  \printfield{catalog}%
}

\newbibmacro{manuscript:collection+shelfmark}{%
  \printtext[collection+shelfmark]{%
	  \printfield{collection}%
	  \setunit*{\collectionshelfmarkpunct}%
	  \printfield{shelfmark}%
	}%
}

\newbibmacro{manuscript:date/dating}{%
	\iffieldundef{dating}{\usebibmacro{date}}
	  {\printfield{dating}}%
}

\newbibmacro{location+library+collection+shelfmark}{%
	\printlist{location}%
	\setunit{\locationlibrarypunct}%
	\printfield{library}%
	\setunit{\librarycollectionpunct}%
	\usebibmacro{manuscript:collection+shelfmark}%
}

\newbibmacro{manuscript:pages}{%
	\printfield{pagetotal}%
	\iffieldundef{pages}%
    {%
    \usebibmacro{manuscript:columns+layer}%
    }%
    {%
    \usebibmacro{manuscript:columns}%
	  \setunit{\pagetotalpagespunct}%
	  \printfield{pages}%
	  \usebibmacro{manuscript:layer}%
	  }%
}

\newbibmacro{manuscript:columns+layer}{%
 \ifboolexpr{test {\iffieldundef{columns}} or test{\iffieldundef{layer}}}{%
   \usebibmacro{manuscript:columns}%
   \usebibmacro{manuscript:layer}%
 }%
 {%
   \printtext[columns+layer]{%
     \bibstring{\strfield{columns}column}%
     \setunit{\columnslayerpunct}%
     \bibstring{\strfield{layer}layer}%
   }%
 }%
}%

\newbibmacro{manuscript:columns}{%
  \iffieldundef{columns}{}{\bibstring[\mkcolumns]{\strfield{columns}column}}%
}%

\newbibmacro{manuscript:layer}{%
  \iffieldundef{layer}{}{\bibstring[\mklayer]{\strfield{layer}layer}}%
}%


% 3. Some Field or List Format
\DeclareFieldFormat{catalog}{%
    \bibstring[\mkmanuscriptdescriptionlabel]{catalog}%
  #1%
}
\DeclareFieldFormat{collection+shelfmark}{%
  \printtext[title]{#1}%
}
\DeclareFieldFormat{columns+layer}{\mkcolumnslayer{#1}}%
\DeclareFieldFormat{dating}{#1\isdot}%

\DeclareFieldFormat{support}{\ifbibstring{#1}{\bibstring{#1}}{#1}}%
\DeclareListFormat[]{script}{%
  \ifnumgreater{\value{listcount}}{\value{liststart}}%
    {\multilistdelim}%
    {}%
  \ifbibstring{#1}{\bibstring{#1}}{#1}%
}%

\DeclareNameFormat{manuscript}{%
  \nameparts{#1}%
  \ifnumgreater{\value{listcount}}{\value{liststart}}%
    {\multilistdelim}%
    {\bibstring[\mkmanuscriptdescriptionlabel]{\currentname}}%
  \ifuseprefix
    {\usebibmacro{name:given-family}{\namepartfamily}{\namepartgiven}{\namepartprefix}{\namepartsuffixi}}
    {\usebibmacro{name:given-family}{\namepartfamily}{\namepartgiven}{\namepartprefixi}{\namepartsuffixi}}%
}
\DeclareNameAlias{scribe}{manuscript}
\DeclareNameAlias{owner}{manuscript}
\DeclareListFormat[manuscript]{location}{%
  \usebibmacro{list:delim}{\mklocation{#1}}%
  \mklocation{#1}\isdot%
  \usebibmacro{list:andothers}}

\DeclareListFormat[manuscript]{origin}{%
  \ifnumgreater{\value{listcount}}{\value{liststart}}%
    {\multilistdelim}%
    {\bibstring[\mkmanuscriptdescriptionlabel]{origin}}%
  #1%
  }

\DeclareFieldFormat{manuscript:paragraphedfield}{%
  \bibstring[\mkmanuscriptdescriptionlabelparagraphed]{\currentfield}%
  #1%
}%
\DeclareFieldAlias{contents}{manuscript:paragraphedfield}%
\DeclareFieldAlias{annotation}{manuscript:paragraphedfield}%

% 4. Printshorthands Environment

\newtoggle{shorthand:details}
\defbibenvironment{details}
  {\toggletrue{shorthand:details}
  \list
     {\printfield[shorthandwidth]{shorthand}}
     {\setlength{\listparindent}{\parindent}%
      \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\iffieldundef{shorthand}{\hspace{-\labelwidth}\hspace{-\labelsep}}}


\defbibenvironment{details-noshorthand}
  {\toggletrue{shorthand:details}%
  \list{}{%
     \leftmargin\bibhang
     \itemindent-\leftmargin
     \itemsep\bibitemsep
     \parsep\bibparsep}}
  {\endlist}
  {\item}
% 5. Others
%% 5.1 Driver
\DeclareBibliographyDriver{manuscript}{%
  \usebibmacro{bibindex}%
  \usebibmacro{begentry}%
  \usebibmacro{location+library+collection+shelfmark}%
  \newunit%
  \printlist{language}%
  \newunit\newblock%
  \printfield{howpublished}%
  \newunit\newblock
  \printfield{note}%
  \newunit\newblock%
  \printfield{support}%
  \newunit%
  \printlist{script}%
  \newunit%
  \usebibmacro{manuscript:date/dating}%
  \setunit{\datingpagespunct}%
  \usebibmacro{manuscript:pages}%
  \iftoggle{bbx:related}%
    {\newunit\newblock%
     \usebibmacro{related:init}%
     \usebibmacro{related}}
    {}%
  \newunit\newblock%
  \iftoggle{bbx:url}%
    {\usebibmacro{url+urldate}}%
    {}%
  \usebibmacro{manuscript:more+annotation+catalog}%
  \usebibmacro{finentry}%
  }

\DeclareBibliographyDriver{cite:manuscript}{%For biblatex-chicago styles
  \usebibmacro{bibindex}%
  \usebibmacro{location+library+collection+shelfmark}%
  \newunit%
  \printlist{language}%
  \newunit\newblock%
  \printfield{howpublished}%
  \newunit\newblock%
  \printfield{note}%
  \newunit\newblock%
  \usebibmacro{manuscript:support+dating}%
  \newunit\newblock%
  \setunit{\datingpagespunct}%
  \usebibmacro{manuscript:pages}%
  \usebibmacro{manuscript:more+annotation+catalog}%
  \usebibmacro{finentry}%
}

%% 5.2 Citation commands
\DeclareCiteCommand{\shcite}[\mkshcite]
  {\usebibmacro{prenote}}
  {\usebibmacro{citeindex}%
   \usebibmacro{cite:shorthand}}
  {\multicitedelim}
  {\usebibmacro{cite:postnote}}


\DeclareCiteCommand{\detailscite}%
  {\usebibmacro{prenote}}%
  {\usedriver%
     {\toggletrue{shorthand:details}}%
     {\thefield{entrytype}}}%
  {\multidetailscitedelim}%
  {\usebibmacro{postnote}}%

\DeclareMultiCiteCommand{\detailscites}{\detailscite}{\multidetailscitedelim}%
%% 5.3 NumCheckSetup (should not be changed)


\NumCheckSetup{\def\verso{}\def\recto{}}

%% 5.4 Language mapping

\DeclareLanguageMapping{english}{english-manuscripts}
\DeclareLanguageMapping{french}{french-manuscripts}
\DeclareLanguageMapping{italian}{italian-manuscripts}
\DeclareLanguageMapping{latin}{latin-manuscripts}

% 5.5 bibcheck

\defbibcheck{withshorthand}{\iffieldundef{shorthand}{\skipentry}{}}
\defbibcheck{withoutshorthand}{\iffieldundef{shorthand}{}{\skipentry}}


%% 5.6 Sorting
\ifdef{\DeclareSortingTemplate}%
  {}
  {\let\DeclareSortingTemplate\DeclareSortingScheme}

\def\bbx@manuscripts@sorting{
  \sort{
  	\field{location}
	  \literal{0000}
  }
  \sort{
    \field{library}
    \literal{0000}
  }
  \sort{
    \field{collection}
    \literal{0000}
  }
  \sort{
  	\field[padside=left,padwidth=50,padchar=0]{sortshelfmark}
    \field[padside=left,padwidth=50,padchar=0]{shelfmark}
  }
}


\DeclareSortingTemplate{manuscripts}{
  \sort{
    \field{presort}
  }
  \sort[final]{
    \field{sortkey}
  }
  \bbx@manuscripts@sorting
}

% Adaptation from biblatex.def

\DeclareSortingTemplate{nty+manuscripts}{
  \sort{
    \field{presort}
  }
  \sort[final]{
    \field{sortkey}
  }
  \sort{
    \field{sortname}
    \field{author}
    \field{editor}
    \field{translator}
    \field{sorttitle}
    \field{title}
  }
  \sort{
    \field{sorttitle}
    \field{title}
  }
  \sort{
    \field{sortyear}
    \field{year}
  }
  \sort{
    \field{volume}
    \literal{0}
  }
  \bbx@manuscripts@sorting
}

\DeclareSortingTemplate{nyt+manuscripts}{
  \sort{
    \field{presort}
  }
  \sort[final]{
    \field{sortkey}
  }
  \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}
  }
  \bbx@manuscripts@sorting
}

\DeclareSortingTemplate{nyvt+manuscripts}{
  \sort{
    \field{presort}
  }
  \sort[final]{
    \field{sortkey}
  }
  \sort{
    \field{sortname}
    \field{author}
    \field{editor}
    \field{translator}
    \field{sorttitle}
    \field{title}
  }
  \sort{
    \field{sortyear}
    \field{year}
  }
  \sort{
    \field{volume}
    \literal{0}
  }
  \sort{
    \field{sorttitle}
    \field{title}
  }
  \bbx@manuscripts@sorting
}



% 6. Prevent trouble with abbreviated form (verbose family)

\newcommand{\biblatexms@patchfail}[1]{\PackageError{biblatex-manuscripts-philology}{Fail to patch #1}{}}
\RequirePackage{xpatch}
\newbibmacro{manuscript:add:shortened}{\ifentrytype{manuscript}{\usebibmacro{location+library+collection+shelfmark}}{}}
\newcommand{\manuscriptaddshortened}[1]{%
	\def\do##1{%
		  \xpretobibmacro{##1}%
		    {\usebibmacro{manuscript:add:shortened}}%
		    {}%
		    {\ifcsvoid{abx@macro@##1}%
		      {}%
		      {\biblatexms@patchfail{##1}}%
		    }%
		  }%
	\docsvlist{#1}%
}
\AtBeginDocument{%
	\manuscriptaddshortened{cite:title,cite:short,cite:opcit}%
}

% New bibliography string
\RequirePackage{manuscripts-NewBibliographyString}
\endinput
