% publist.cbx, biblatex citation style by Juergen Spitzmueller
% requires biblatex >= 3.4
%
% 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 2003/12/01 or later.
%
% This work has the LPPL maintenance status "maintained".
% 
% This Current Maintainer of this work is Jürgen Spitzmüller.
%
% This work consists of the file publist.cbx and publist.bbx.
%
%
% This is a simple portmanteau file that just loads the standard
% "numeric" citation style. This file is provided to allow the loading
% of biblatex-publist via the "style=" (alternatively to "bibstyle=")
% option.
%
% Please send suggestions and bug reports to 
% https://github.com/jspitz/biblatex-publist
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\def\bpl@version{2.14}
\def\bpl@rdate{2025/03/10}

\ProvidesFile{publist.cbx}[\bpl@rdate\space v.\bpl@version\space
                           biblatex citation style (JSP)]

\RequireCitationStyle{numeric}

\DeclareCiteCommand{\citesecitem}
  {\defcounter{maxnames}{999}%
   \boolfalse{citetracker}%
   \boolfalse{pagetracker}%
   \usebibmacro{prenote}}
  {\usebibmacro{bpl:cite:secitem}}
  {\multicitedelim}
  {\usebibmacro{postnote}}

\DeclareCiteCommand*{\citesecitem}
  {\defcounter{maxnames}{999}%
   \boolfalse{citetracker}%
   \boolfalse{pagetracker}%
   \usebibmacro{prenote}}
  {\usebibmacro{bpl:cite:secitem}%
   \usebibmacro{bpl:cite:secitemsec}%
  }
  {\multicitedelim}
  {\usebibmacro{postnote}}

\newbibmacro*{bpl:cite:secitem}{%
  \ifcsdef{bpl@itemnumber:\thefield{entrykey}}%
     {\mkbibbrackets{\csuse{bpl@itemnumber:\thefield{entrykey}}}}%
     {\textbf{\mkbibbrackets{???}}}%
}

\newcommand*\secitemsecref[1]{%
   \mkbibparens{\bibstring{section}\addnbspace\ref{refsection:#1}}%
}

\newbibmacro*{bpl:cite:secitemsec}{%
  \ifcsdef{bpl@itemsec:\thefield{entrykey}}%
     {\addspace\secitemsecref{\csuse{bpl@itemsec:\thefield{entrykey}}}}%
     {}%
}

\DeclareCiteCommand{\citeitem}
  {\defcounter{maxnames}{999}%
   \boolfalse{citetracker}%
   \boolfalse{pagetracker}%
   \usebibmacro{prenote}}
  {\usebibmacro{bpl:cite:extern}}
  {\multicitedelim}
  {\usebibmacro{postnote}}

\newbibmacro*{bpl:cite:extern}{%
  \printtext[bibhyperref]{%
    \printfield{labelprefix}%
    \usebibmacro{bpl:cite:labelnumber}%
    \ifbool{bbx:subentry}
      {\printfield{entrysetcount}}
      {}}}

\newbibmacro*{bpl:cite:labelnumber}{%
    \printtext[labelnumberwidth]{\mkrefdesc{\thefield{labelnumber}}}%
}

\newcommand*\itemrangesep{\bibrangedash}

\newcommand*\citeitemrange[2]{%
    \citeitem{#1}\itemrangesep\citeitem{#2}%
}

\NewDocumentCommand\citesecitemrange{s m m}
{%
   \IfBooleanTF{#1}%
      {\ifcsdef{bpl@itemsec:#2}%
          {\ifcsdef{bpl@itemsec:#3}%
            {\ifcsequal{bpl@itemsec:#2}{bpl@itemsec:#3}%
                {\citesecitem{#2}\itemrangesep}%
                {\citesecitem*{#2}\,\itemrangesep\,}%
             \citesecitem*{#3}}%
             {\citesecitem*{#2}\itemrangesep\citesecitem*{#3}}%
          }%
          {\citesecitem{#2}\itemrangesep\citesecitem*{#3}}%
      }%
      {\citesecitem{#2}\itemrangesep\citesecitem{#3}}%
}

% Manual way to adjust numbering
\csdef{bpl@secstartshift}{0}
\newcommand*\shiftciteitem[1]{\csdef{bpl@secstartshift}{#1}}

% Print labelnumber as actual number, plus item total
% and probably a manual shift value
\newrobustcmd{\mkrefdesc}[1]{%
  \number\numexpr#1+\csuse{bpl@itemtotal}+\csuse{bpl@secstartshift}\relax%
}

\providebool{@bpl@reversenum}
\providebool{@bpl@numberreset}
\providebool{plnumgroup}

\AtEndOfPackage{%
\if@bpl@reversenum
  \newcounter{prevcrefsection}%
  \renewrobustcmd{\mkrefdesc}[1]{%
     \ifbool{@bpl@numberreset}{%
        % Print the labelnumber as the total number of entries in the
        % current refsection, minus the actual labelnumber, plus one
        \ifbool{plnumgroup}{%
             \number\numexpr\csuse{bpl@itemtotal}-#1+1%
                            +\csuse{bpl@secstartshift}\relax%
          }{%
             \ifcsdef{bpl@secitems:\therefsection}{%
                \number\numexpr\csuse{bpl@secitems:\therefsection}-#1+1%
                           +\csuse{bpl@secstartshift}\relax%
             }{%
                \number\numexpr\csuse{bpl@itemtotal}-#1+1%
                           +\csuse{bpl@secstartshift}\relax%
             }%
          }%
     }{%
        % Use decreasing counter
        \setcounter{prevcrefsection}{\therefsection}%
        \addtocounter{prevcrefsection}{-1}%
        \ifcsdef{bpl@items:\theprevcrefsection}{%
           \number\numexpr\csuse{bpl@itemtotal}-#1+1%
           -\csuse{bpl@items:\theprevcrefsection}%
           +\csuse{bpl@secstartshift}\relax%
        }{%
           \number\numexpr\csuse{bpl@itemtotal}-#1+1%
           +\csuse{bpl@secstartshift}\relax%
        }%
     }%
  }%
\fi%
}

%%
\endinput