\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{jurafoo}[2023-03-04 JMU presets for KOMA-Script and biblatex-jura2]

% Sections

% Part: On one line
\renewcommand*{\partformat}{\partname~\thepart:~}
\RedeclareSectionCommand[
 runin=false,
 afterskip=0pt,
 beforeskip=0pt,
 font=\normalsize,
 afterindent=false,
 style=section,
]{part}
\RedeclareSectionCommand[
 runin=false,
 afterskip=0pt,
 beforeskip=0pt,
 font=,
 tocindent=0cm,
 tocnumwidth=2em,
 counterwithin=part,
 afterindent=false,
]{section}
\RedeclareSectionCommand[
 runin=false,
 afterskip=0pt,
 beforeskip=0pt,
 font=,
 tocindent=0.5cm,
 tocnumwidth=2em,
 afterindent=false,
]{subsection}
\RedeclareSectionCommand[
 runin=false,
 afterskip=0pt,
 beforeskip=0pt,
 font=,
 tocindent=1cm,
 tocnumwidth=2em,
 afterindent=false,
]{subsubsection}
\DeclareSectionCommand[
 afterskip=0pt,
 beforeskip=0pt,
 font=,
 tocstyle=subsection,
 style=section,
 level=3,
 indent=0pt,
 afterindent=false,
 runin=false,
 tocindent=1.5cm,
 tocnumwidth=2em,
 counterwithin=subsubsection
]{subsectionthree}
\DeclareSectionCommand[
 afterskip=0pt,
 beforeskip=0pt,
 font=,
 tocstyle=subsection,
 style=section,
 level=4,
 indent=0pt,
 afterindent=false,
 runin=false,
 tocindent=2cm,
 tocnumwidth=2em,
 counterwithin=subsectionthree
]{subsectionfour}
\DeclareSectionCommand[
 afterskip=0pt,
 beforeskip=0pt,
 font=,
 tocstyle=subsection,
 style=section,
 level=5,
 indent=0pt,
 afterindent=false,
 runin=false,
 tocindent=2.5cm,
 tocnumwidth=2em,
 counterwithin=subsectionfour
]{subsectionfive}
\DeclareSectionCommand[
 afterskip=0pt,
 beforeskip=0pt,
 font=,
 tocstyle=subsection,
 style=section,
 level=6,
 indent=0pt,
 afterindent=false,
 runin=false,
 tocindent=3cm,
 tocnumwidth=2em,
 counterwithin=subsectionfive
]{subsectionsix}
\setcounter{secnumdepth}{6}
\setcounter{tocdepth}{6}

\renewcommand*\thesection{\Alph{section}.}
\renewcommand*\thesubsection{\Roman{subsection}.}
\renewcommand*\thesubsubsection{\arabic{subsubsection}.}
\renewcommand*\thesubsectionthree{\alph{subsectionthree})}
\renewcommand*\thesubsectionfour{\alph{subsectionfour}\alph{subsectionfour})}
\renewcommand*\thesubsectionfive{(\arabic{subsectionfive})}
\renewcommand*\thesubsectionsix{\greek{subsectionsix})}

% Add command to add fake section.
% Adding a real section was not that easy.
\newcommand*{\frage}[1]{\addcontentsline{toc}{section}{Frage #1}%
\noindent\textbf{Frage #1}}

% Bibliography style overrides for biblatex-jura2

% Global: Use commas for everything
\DeclareDelimFormat[bib]{nametitledelim}{\addcomma\space}
\renewcommand*{\newunitpunct}{\addcomma\space}
% Global: Print all names in reverse order
\DeclareNameAlias{default}{sortname}
% Global: Do not use biblatex-jura2’s sorting template
\DeclareSortingTemplate{nty}{
 \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}
 }
}

% Commentary (citedbytitle): Don’t print title in italic (upstream doesn’t know themselves?)
\DeclareFieldFormat[commentary]{maintitle}{\normalfont{#1}}
\DeclareFieldFormat[commentary]{title}{\normalfont{#1}}
\DeclareFieldFormat[commentary]{author}{\mkbibemph{#1}}
% Commentary (citedbytitle): Print author in italic
\renewcommand*{\mkbibnamefamily}[1]{\mkbibemph{#1}}
\renewcommand*{\mkbibnamegiven}[1]{\mkbibemph{#1}}
\renewcommand*{\mkbibnameprefix}[1]{\mkbibemph{#1}}
\renewcommand*{\mkbibnamesuffix}[1]{\mkbibemph{#1}}
% Commentary (citedbytitle) and incollection: Print names separated by /
\renewcommand*{\multinamedelim}{\addslash}
\renewcommand*{\finalnamedelim}{\addslash}

% Article: Do not print in: before journal name
\renewbibmacro{in:}{\ifentrytype{article}{}{\printtext{\bibstring{in}\intitlepunct}}}

% Article: Show p./pp. before pages, do not use paranthesis for postnote
\DeclareFieldFormat[article]{postnote}{#1}
\DeclareFieldFormat[article]{pages}{\mkpageprefix[bookpagination][\mknormrange]{#1}}
% Article/Jurisdiction: Place comma before postnote (modified from jura2.cbx)
\renewcommand*{\postnotedelim}{\ifentrytype{commentary}{}{\addcomma\addspace}}

% Incollection: No shorttitle in footnote, caveat: Makes footnote no longer clickable
\newbibmacro*{cite:title:incollection}{}

\newbibmacro{fshowcited}{%
 \iftoggle{tnbcbx@fshowcited}
  {\iffieldundef{shorttitle}
   {}
   {\setunit{\addspace}%
    \printtext[parens]{%
     \bibstring{zitiertals}%
     \setunit{\space}%
     \printnames[labelname]{author}%
     \setunit{\addcomma\space}%
     \printfield{shorttitle}%
     }}}%
  {}}%

\renewbibmacro{finentry}{%
 \ifentrytype{commentary}{%
  \usebibmacro{howcited}\newunit\newblock}%
  {}%
 \ifentrytype{book}{%
  \usebibmacro{fshowcited}\newunit\newblock}%
  {}%
 \ifentrytype{incollection}{%
  \usebibmacro{fshowcited}\newunit\newblock}%
  {}%
 \ifentrytype{online}{%
  \usebibmacro{fshowcited}\newunit\newblock}%
  {}%
 \finentry}%
