%% Copyright 2014-… 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 realauthor.dbx, realauthor.bbx
\ProvidesFile{realauthor.dbx}
[2017/11/19 v2.7.1a biblatex bibliography style (MR)]

\ifdef{\DeclareSortingTemplate}%
  {}
  {\let\DeclareSortingTemplate\DeclareSortingScheme}
\ifdef{\biblatexmultipledm@bibstyle}
	{\RequireBibliographyStyle{\biblatexmultipledm@bibstyle}}
	{\RequireBibliographyStyle{verbose}}

\RequirePackage{xpatch}

% LabelName


\DeclareLabelname{% 
  \field{shortauthor}
  \field{author}
  \field{realauthor}
  \field{shorteditor}
  \field{editor}
  \field{realeditor}%
  \field{translator}}

% Command for being redefined

\newcommand{\mkbibrealauthor}[1]{%
	\ifnameundef{author}%
		{\mkrealauthor{#1}}%
		{\mkrealauthor{\realauthorequalsign#1}}%
}
\newcommand{\mkrealauthor}[1]{\mkbibbrackets{#1}}
\newcommand{\realauthorequalsign}{\addnbthinspace=\addnbthinspace}

\newcommand{\mkbibrealeditor}[1]{%
	\ifnameundef{editor}%
		{\mkrealeditor{#1}}%
		{\mkrealeditor{\realeditorequalsign#1}}%
}
\newcommand{\mkrealeditor}[1]{\mkrealauthor{#1}}%
\newcommand{\realeditorequalsign}{\realauthorequalsign}%
% A format which could be overrided

\DeclareNameFormat{realauthor}{%
 \nameparts{#1}%
 \mkbibrealauthor{%
   \ifboolexpr{test {\ifbibliography} and test {\ifnameundef{author}}}%
   {\ifgiveninits%
    {\usebibmacro{name:family-given}{\namepartfamily}{\namepartgiveni}{\namepartprefix}{\namepartsuffix}}%
    {\usebibmacro{name:family-given}{\namepartfamily}{\namepartgiven}{\namepartprefix}{\namepartsuffix}}%
   }%
   {\ifgiveninits%
    {\usebibmacro{name:given-family}{\namepartfamily}{\namepartgiveni}{\namepartprefix}{\namepartsuffix}}%
    {\usebibmacro{name:given-family}{\namepartfamily}{\namepartgiven}{\namepartprefix}{\namepartsuffix}}%
   }%
  \usebibmacro{name:andothers}}}
  
\DeclareNameFormat{realeditor}{%
 \nameparts{#1}%
 \mkbibrealeditor{%
   \ifboolexpr{test {\ifbibliography} and test {\ifnameundef{editor}}}%
   {\ifgiveninits%
    {\usebibmacro{name:family-given}{\namepartfamily}{\namepartgiveni}{\namepartprefix}{\namepartsuffix}}%
    {\usebibmacro{name:family-given}{\namepartfamily}{\namepartgiven}{\namepartprefix}{\namepartsuffix}}%
   }%
   {\ifgiveninits%
    {\usebibmacro{name:given-family}{\namepartfamily}{\namepartgiveni}{\namepartprefix}{\namepartsuffix}}%
    {\usebibmacro{name:given-family}{\namepartfamily}{\namepartgiven}{\namepartprefix}{\namepartsuffix}}%
   }%
  \usebibmacro{name:andothers}}} 
 
% Patching some standard bibmacros
\newcommand{\realauthor@failpatch}[1]{\PackageError{realauthor}{Fail to patch `#1` bibmacro}{}}

\xapptobibmacro{author}{%
	  \ifnameundef{author}{}{%
		  \ifnameundef{realauthor}{}{\addspace\nopunct}%
	  }%
	  \printnames{realauthor}%
	{}%
}
  {}
  {\realauthor@failpatch{author}}

\xpatchbibmacro{author/editor+others}
	{not test {\ifnameundef{author}}}
	{not (test {\ifnameundef{author}} and test {\ifnameundef{realauthor}})}
	{}
	{\realauthor@failpatch{author/editor+others}}


\xpatchbibmacro{author/translator+others}
	{not test {\ifnameundef{author}}}
	{not (test {\ifnameundef{author}} and test {\ifnameundef{realauthor}})}
	{}
	{\realauthor@failpatch{author/translator+others}}

\xpatchbibmacro{author/editor+others/translator+others}
	{not test {\ifnameundef{author}}}
	{not (test {\ifnameundef{author}} and test {\ifnameundef{realauthor}})}
	{}
	{\realauthor@failpatch{author/editor+others/translator+others}}


\xpatchbibmacro{author/editor+others/translator+others}
  {\ifboolexpr{
       test \ifuseeditor
       and
       not test {\ifnameundef{editor}}
     }%
  }
  {\ifboolexpr{
    (test \ifuseeditor%
      and
      not test {\ifnameundef{editor}}%
    )
    or
    (test \ifuserealeditor%
      and 
      not test {\ifnameundef{realeditor}}%
    )%
    }%
  }
  {}%
  {\realauthor@failpatch{author/editor+others/translator+others}}




\xpatchbibmacro{bbx:editor}
  {\ifboolexpr{
    test \ifuseeditor
    and
    not test {\ifnameundef{editor}}
  }}
  {\ifboolexpr{
    (test \ifuseeditor%
      and
      not test {\ifnameundef{editor}}%
    )
    or
    (test \ifuserealeditor%
      and 
      not test {\ifnameundef{realeditor}}%
    )%
    }%
  }
  {}
  {\realauthor@failpatch{bbx:editor}}

\xpatchbibmacro{bbx:editor}
  {\clearname{editor}}
  {\clearname{editor}
   \clearname{realeditor}
  }
  {}
  {\realauthor@failpatch{bbx:editor}}

\xpatchbibmacro{bbx:editor}
  {\printnames{editor}}%
  {\printnames{editor}%
  \usebibmacro{printeditor}%
  }%
  {}
  {\realauthor@failpatch{bbx:editor}}


\xpatchbibmacro{byeditor}
 {\ifnameundef{editor}}%
 {\ifboolexpr{test{\ifnameundef{editor}} and test{\ifnameundef{realeditor}}}}%
 {}
 {\realauthor@failpatch{byeditor}}

\xpatchbibmacro{byeditor}
  {\printnames[byeditor]{editor}}%
  {\usebibmacro{printbyeditor+realeditor}}
  {}
  {\realauthor@failpatch{byeditor}}

\xpatchbibmacro{byeditor+others}
 {\ifnameundef{editor}}%
 {\ifboolexpr{test{\ifnameundef{editor}} and test{\ifnameundef{realeditor}}}}%
 {}
 {\realauthor@failpatch{byeditor+others}}

\xpatchbibmacro{byeditor+others}
  {\printnames[byeditor]{editor}}%
  {\usebibmacro{printbyeditor+realeditor}}
  {}
  {\realauthor@failpatch{byeditor+others}}

\newbibmacro{printbyeditor+realeditor}{%
  \printnames[byeditor]{editor}%
  \ifnameundef{editor}{}{%
		  \ifnameundef{realeditor}{}{\addspace\nopunct}%
	}%
	\printnames{realeditor}%
}

\newbibmacro{printeditor}{%
	  \ifnameundef{editor}{}{%
		  \ifnameundef{realeditor}{}{\addspace\nopunct}%
	  }%
	  \printnames{realeditor}%
}

%% For the shorter version

\newbibmacro{realauthor:as:labelname}{%
  \ifboolexpr{%
    test {\ifnamesequal{labelname}{realauthor}}%
    or
    test {\ifnamesequal{labelname}{realeditor}}%
   }{%
      \ifnamesequal{labelname}{realauthor}%
       {\printnames{realauthor}}%
       {\printnames{realeditor}}%
    }%
    {%
      \printnames{labelname}%
      \ifnamesequal{labelname}{author}{%
        \ifnameundef{realauthor}%
          {}%
          {\addspace\nopunct\printnames{realauthor}}%
      }{}% 
      \ifnamesequal{labelname}{editor}{%
        \ifnameundef{realeditor}%
        {}%
        {\addspace\nopunct\printnames{realeditor}}%
      }{}%
    }%
}
\AtBeginDocument{%
  \ifcsvoid{abx@macro@cite:name}{}{%
    \xpatchbibmacro{cite:name}%
      {\printnames{labelname}}
      {\usebibmacro{realauthor:as:labelname}}
      {}
      {\realauthor@failpatch{cite:name}}
 }%
}

% For the dash on the bibliography, in the case two real author use the same pseudonym

\AtBeginDocument{
\xapptobibmacro{bbx:savehash}{%
  \ifboolexpr{%
    test {\ifnamesequal{labelname}{author}}%
    and 
    not test {\ifnameundef{realauthor}}%
  }%
    {%
      \xappto\bbx@lasthash{\thefield{realeauthorfullhash}}%
    }%
    {}%
  \ifboolexpr{%
    test {\ifnamesequal{labelname}{editor}}%
    and 
    not test {\ifnameundef{realeditor}}%
  }%
    {%
      \xappto\bbx@lasthash{\thefield{realeditorfullhash}}%
    }%
    {}%
}
{}{\realauthor@failpatch{bbx:savehash}}

\xpretobibmacro{bbx:dashcheck}{%
  \savefield{fullhash}{\bbx@this@hash}%
  \ifboolexpr{%
    test {\ifnamesequal{labelname}{author}}%
    and 
    not test {\ifnameundef{realauthor}}%
  }%
    {%
      \eappto\bbx@this@hash{\thefield{realauthorfullhash}}%
    }%
    {}%
  \ifboolexpr{%
    test {\ifnamesequal{labelname}{editor}}%
    and 
    not test {\ifnameundef{realeditor}}%
  }%
    {%
      \eappto\bbx@this@hash{\thefield{realeditorfullhash}}%
    }%
    {}%
}
{}{\realauthor@failpatch{bbx:dashchek}}

\xpatchbibmacro{bbx:dashcheck}%
  {\iffieldequals{fullhash}{\bbx@lasthash}}%
  {\ifdefequal{\bbx@this@hash}{\bbx@lasthash}}%
{}{\realauthor@failpatch{bbx:dashchek}}

}
% Sorting

\def\bbx@realauthor@sorting{
  \sort{
   \field{sortname}
   \field{author}
   \field{realauthor}
   \field{editor}
   \field{realeditor}
   \field{translator}
   \field{sorttitle}
   \field{title}
  }
  \sort{
   \field{sorttitle}
   \field{title}
  }
  \sort{
   \field{sortyear}
   \field{year}
  }
  \sort{
   \field[padside=left,padwidth=4,padchar=0]{volume}
   \literal{0000}
  }
}

\def\bbx@realauthor@sorting@anonymous{
  \sort{
   \field{sortname}
   \field{author}
   \field{realauthor}
   \field{editor}
   \field{realeditor}
   \field{translator}
   \literal{0000}
  }
  \sort{
   \field{sorttitle}
   \field{title}
  }
  \sort{
   \field{sortyear}
   \field{year}
  }
  \sort{
   \field[padside=left,padwidth=4,padchar=0]{volume}
   \literal{0000}
  }
}

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

\DeclareSortingTemplate{anonymous+realauthor}{
 \sort{
   \field{presort}
  }
  \sort[final]{
   \field{sortkey}
  }
  \bbx@realauthor@sorting@anonymous
}


\AtEndPreamble{
  \ifundef{\bbx@manuscripts@sorting}{}%
    {
      \DeclareSortingTemplate{realauthor+manuscripts}{
        \sort{
          \field{presort}
        }
        \sort[final]{
        \field{sortkey}
        }   
        \bbx@realauthor@sorting
        \bbx@manuscripts@sorting
      }
      \DeclareSortingTemplate{anonymous+realauthor+manuscripts}{
        \sort{
          \field{presort}
        }
        \sort[final]{
        \field{sortkey}
        }   
        \bbx@realauthor@sorting@anonymous
        \bbx@manuscripts@sorting
      }
    }
}

\endinput
