%% Copyright 2016 Daniel Ballester Marques
%%
%% 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 Daniel Ballester Marques.

\ProvidesFile{abnt-ibid.cbx}%
[2018-11-17\space v3.4\space ABNT BibLaTeX citation style]%

% Init >>>1

\RequirePackage{xparse}%
\RequirePackage{xpatch}%
\RequirePackage{expl3}%
\RequirePackage{l3regex}%

% <<<1

% Format >>>1

\newcommand*{\UpperOrSCCite}[1]{% >>>2
  \iftoggle{sccite}{%
    \textsc{\MakeLowercase{#1}}%
  }{%
    \MakeUppercase{#1}%
  }%
}% <<<2

\newcommand*{\NormalOrSCCite}[1]{% >>>2
  \iftoggle{sccite}{%
    \textsc{\MakeLowercase{#1}}%
  }{%
    #1%
  }%
}% <<<2

\newcommand*{\NormalOrACCiteOrSCCite}[1]{% >>>2
  \iftoggle{accite}{%
    \MakeUppercase{#1}}%
  }{%
    \iftoggle{sccite}{%
      \textsc{\MakeLowercase{#1}}%
    }{%
      #1%
  }%
}% <<<2 

% \IfInitial >>>2

\ExplSyntaxOn

\NewDocumentCommand{\IfInitial}{mmm}{%
  \regex_match:nVTF {\w\.} \namepartgiven {#2} {#3}%
}
\cs_generate_variant:Nn \regex_match:nnTF { nV }

\ExplSyntaxOff

% <<<2

\renewbibmacro*{name:family}[4]{% >>>2
  \ifuseprefix
    {\usebibmacro{name:delim}{#3#1}%
     \usebibmacro{name:hook}{#3#1}%
     \ifdefvoid{#3}{}{%
       \ifcapital{%
          \iftoggle{sccite}{%
            \mkbibnameprefix{#3}\isdot%
          }{%
            \mkbibnameprefix{\MakeCapital{#3}}\isdot%
          }
       }{%
          \mkbibnameprefix{#3}\isdot%
       }%
       \ifprefchar{}{\bibnamedelimc}}%
     \mkbibnamefamily{#1}\isdot
     \ifdefvoid{#4}{}{\bibnamedelimd\mkbibnamesuffix{#4}\isdot}}
    {\usebibmacro{name:delim}{#1}%
     \usebibmacro{name:hook}{#1}%
     \mkbibnamefamily{#1}\isdot
     \ifdefvoid{#4}{}{\bibnamedelimd\mkbibnamesuffix{#4}\isdot}}}% <<<2

\DeclareNameFormat{labelname}{% >>>2
  % Use family-given instead of given-family
  \ifcase\value{uniquename}%
    \usebibmacro{name:family}%
      {\namepartfamily}%
      {\namepartgiven}%
      {\namepartprefix}%
      {\namepartsuffix}%
  \or%
    \ifuseprefix%
      {\usebibmacro{name:family-given}%
        {\namepartfamily}%
        {\namepartgiveni}%
        {\namepartprefix}%
        {\namepartsuffixi}}%
      {\usebibmacro{name:family-given}%
        {\namepartfamily}%
        {\namepartgiveni}%
        {\namepartprefixi}%
        {\namepartsuffixi}}%
  \or%
    \usebibmacro{name:family-given}%
      {\namepartfamily}%
      {\namepartgiven}%
      {\namepartprefix}%
      {\namepartsuffix}%
  \fi%
  \usebibmacro{name:andothers}}% <<<2

\let\origmkbibnamefamily\mkbibnamefamily%
\let\origmkbibnamegiven\mkbibnamegiven%
\let\origmkbibnameprefix\mkbibnameprefix%
\let\origmkbibnamesuffix\mkbibnamesuffix%

\renewcommand*{\mkbibnamefamily}[1]{% >>>2
  \NormalOrACCiteOrSCCite{#1}%
}% <<<2
\renewcommand*{\mkbibnamegiven}[1]{% >>>2
  \ifnumequal{\value{uniquename}}{2}{%
      \IfInitial{#1}{\NormalOrSCCite{#1}}{#1}%
  }{%
      \NormalOrSCCite{#1}%
  }%
}% <<<2
\renewcommand*{\mkbibnameprefix}[1]{% >>>2
  \ifuseprefix{%
    \UpperOrSC{#1}%
  }{%
    \ifnumequal{\value{uniquename}}{2}{#1}{\NormalOrSCCite{#1}}%
  }
}% <<<2
\renewcommand*{\mkbibnamesuffix}[1]{% >>>2
  \NormalOrACCiteOrSCCite{#1}%
}% <<<2

\DeclareFieldFormat{firstword}{%% >>>2
  \iftoggle{sccite}{%
    \normalfont\textsc{\MakeLowercase{\FirstWord{#1}}}%
  }{%
    \normalfont\MakeUppercase{\FirstWord{#1}}%
  }%
}%% <<<2

\renewcommand*{\nameyeardelim}{\addcomma\addspace}%

% <<<1

% Options >>>1

\newtoggle{sccite}%

\DeclareBibliographyOption{sccite}[true]{
  \settoggle{sccite}{#1}%
}

\InitializeCitationStyle{\numgdef{\cbx@resetcount}{\cbx@resetcount+1}}%

\DeclareFieldFormat{bibhyperlink}{%
  \bibhyperlink{\iffootnote{f}{t}:\cbx@resetcount:\thefield{entrykey}}{#1}}%
\DeclareFieldFormat{bibhypertarget}{%
  \bibhypertarget{\iffootnote{f}{t}:\cbx@resetcount:\thefield{entrykey}}{#1}}%

\newcommand*{\cbx@resetcount}{0}%
\providecommand*{\mkidem}[1]{#1}%
\providecommand*{\mkibid}[1]{#1}%
\providecommand*{\mkopcit}[1]{#1}%
\providecommand*{\mkloccit}[1]{#1}%
\renewcommand*{\iffinalcitedelim}{%
  \ifnumequal{\value{textcitecount}}{\value{textcitetotal}-1}}%

\newtoggle{cbx:loccit}%

\newbibmacro*{footcite:citepages}{}%
\newbibmacro*{footcite:full:citepages}{}%
\newbibmacro*{footcite:postnote}{}%

\DeclareBibliographyOption[boolean]{ibidpage}[true]{%
  \ifstrequal{#1}{true}%
    {\ExecuteBibliographyOptions{loccittracker=constrict}}%
    {\ExecuteBibliographyOptions{loccittracker=false}}}%

\DeclareBibliographyOption[string]{citepages}[permit]{%
  \ifcsdef{cbx@opt@citepages@#1}%
    {\csuse{cbx@opt@citepages@#1}}%
    {\PackageError{biblatex}%
       {Invalid option 'citepages=#1'}%
       {Valid values are 'permit', 'suppress', 'omit', 'separate'.}}}%

\providebibmacro*{footcite:citepages}{}%
\providebibmacro*{footcite:full:citepages}{}%
\providebibmacro*{footcite:postnote}{}%

\def\cbx@opt@citepages@permit{% >>>2
  \renewbibmacro*{footcite:citepages}{}%
  \renewbibmacro*{footcite:full:citepages}{}%
  \renewbibmacro*{footcite:postnote}{%
    \usebibmacro{footcite:postnote:ibidpage}}}% <<<2

\def\cbx@opt@citepages@suppress{% >>>2
  \renewbibmacro*{footcite:citepages}{}%
  \renewbibmacro*{footcite:full:citepages}{%
    \clearfield{pages}%
    \clearfield{pagetotal}}%
  \renewbibmacro*{footcite:postnote}{%
    \usebibmacro{footcite:postnote:ibidpage}}}% <<<2

\def\cbx@opt@citepages@omit{% >>>2
  \renewbibmacro*{footcite:citepages}{}%
  \renewbibmacro*{footcite:full:citepages}{%
    \ifboolexpr{%
      test {\ifnumequal{\value{footcitecount}}{\value{footcitetotal}}}%
      and%
      test {\iffieldpages{postnote}}%
    }%
      {\clearfield{pages}%
       \clearfield{pagetotal}}%
      {}}%
  \renewbibmacro*{footcite:postnote}{%
    \usebibmacro{footcite:postnote:ibidpage}}}% <<<2

\def\cbx@opt@citepages@separate{% >>>2
  \providetoggle{cbx:fullcite}%
  \renewbibmacro*{footcite:citepages}{%
    \global\togglefalse{cbx:fullcite}}%
  \renewbibmacro*{footcite:full:citepages}{%
    \global\toggletrue{cbx:fullcite}}%
  \renewbibmacro*{footcite:postnote}{%
    \ifboolexpr{%
      togl {cbx:fullcite}%
      and%
      test {\iffieldpages{postnote}}%
      and%
      test {\ifnumequal{\value{footcitecount}}{\value{footcitetotal}}}%
      and%
      (%
	not test {\iffieldundef{pages}}%
	or%
	not test {\iffieldundef{pagetotal}}%
      )%
    }%
      {\usebibmacro{footcite:postnote:pages}}%
      {\usebibmacro{footcite:postnote:ibidpage}}}%
  \providebibmacro*{footcite:postnote:pages}{%
    \setunit{\postnotedelim}%
    \bibstring{thiscite}%
    \setunit{\addspace}%
    \printfield{postnote}}}% <<<2

\ExecuteBibliographyOptions{% >>>2
  loccittracker=constrict,%
  citetracker=context,%
  ibidtracker=constrict,%
  idemtracker=constrict,%
  opcittracker=constrict,%
  pagetracker,%
  autocite=footnote,%
  citepages=permit,%
  labeldateparts,%
  labeldate=year,%
  sorting=none,%
}% <<<2

% <<<1

% Macros >>>1

\newbibmacro*{footcite}{% >>>2
  \usebibmacro{footcite:citepages}%
  \global\togglefalse{cbx:loccit}%
  \ifciteseen{%
    \iffieldundef{shorthand}{%
      \ifboolexpr{%
        test {\ifciteibid}%
        and%
        not test {\iffirstonpage}%
      }{%
        \usebibmacro{footcite:ibid}%
      }{%
        \ifboolexpr{%
          test {\ifciteidem}%
          and%
          not test {\iffirstonpage}%
        }{%
          \usebibmacro{footcite:idem}%
        }{%
          \ifboolexpr{%
            test {\ifloccit}%
            and%
            not test {\iffirstonpage}%
          }{%
            \usebibmacro{footcite:loccit}%
          }{%
            \ifboolexpr{%
              test {\ifopcit}%
              and%
              not test {\iffirstonpage}%
            }{%
              \usebibmacro{footcite:opcit}%
            }{%
              \usebibmacro{footcite:short}%
            }%
          }%
        }%
      }%
    }{%
      \usebibmacro{footcite:shorthand}%
    }%
  }{%
    \usebibmacro{footcite:full}%
  }%
}% <<<2

\newbibmacro*{footcitestar}{% >>>2
  \usebibmacro{footcite:citepages}%
  \global\togglefalse{cbx:loccit}%
  \iffieldundef{shorthand}{%
    \ifboolexpr{%
      test {\ifciteibid}%
      and%
      not test {\iffirstonpage}%
    }{%
      \usebibmacro{footcite:ibid}%
    }{%
      \ifboolexpr{%
        test {\ifciteidem}%
        and%
        not test {\iffirstonpage}%
      }{%
        \usebibmacro{footcite:idem}%
      }{%
        \ifboolexpr{%
          test {\ifloccit}%
          and%
          not test {\iffirstonpage}%
        }{%
          \usebibmacro{footcite:loccit}%
        }{%
          \ifboolexpr{%
            test {\ifopcit}%
            and%
            not test {\iffirstonpage}%
          }{%
            \usebibmacro{footcite:opcit}%
          }{%
            \usebibmacro{footcite:short}%
          }%
        }%
      }%
    }%
  }{%
    \usebibmacro{footcite:shorthand}%
  }%
}% <<<2

\newbibmacro*{footcite:full}{% >>>2
  \usebibmacro{footcite:full:citepages}%
  \printtext[bibhypertarget]{%
    \usedriver%
      {\DeclareNameAlias{sortname}{default}}%
      {\thefield{entrytype}}}%
  \usebibmacro{shorthandintro}}%

\newbibmacro*{footcite:short}{%
  \ifnameundef{labelname}{%
    \printtext[bibhyperref]{%
      \printfield[firstword]{labeltitle}%
    }%
  }{%
    \printnames[LAST]{labelname}%
  }%
  \setunit*{\nameyeardelim}%
  \iffieldundef{labelyear}{%
  }{%
    \printtext[bibhyperlink]{%
      \printfield{labelyear}%
      \printfield{extradate}%
    }%
  }%
}% <<<2

\newbibmacro*{footcite:loccit}{% >>>2
  \ifnameundef{labelname}{%
    \printtext[bibhyperref]{%
      \printfield[firstword]{labeltitle}%
    }%
  }{%
    \printnames[LAST]{labelname}%
  }%
  \setunit*{\nameyeardelim}%
  \printtext[bibhyperlink]{\bibstring[\mkloccit]{loccit}}%
  \global\toggletrue{cbx:loccit}
}% <<<2

\newbibmacro*{footcite:opcit}{% >>>2
  \ifnameundef{labelname}{%
    \printtext[bibhyperref]{%
      \printfield[firstword]{labeltitle}%
    }%
  }{%
    \printnames[LAST]{labelname}%
  }%
  \setunit*{\nameyeardelim}%
  \printtext[bibhyperlink]{\bibstring[\mkopcit]{opcit}}%
}% <<<2

\newbibmacro*{footcite:idem}{% >>>2
  \printtext[bibhyperlink]{\bibstring[\mkidem]{idem}}%
  \setunit*{\nameyeardelim}%
  \iffieldundef{labelyear}{%
  }{%
    \printtext[bibhyperlink]{%
      \printfield{labelyear}%
      \printfield{extradate}%
    }%
  }%
}% <<<2

\newbibmacro*{footcite:ibid}{% >>>2
  \printtext[bibhyperlink]{\bibstring[\mkibid]{ibidem}}%
  \ifloccit%
    {\global\toggletrue{cbx:loccit}}%
    {}}% <<<2

\newbibmacro*{footcite:shorthand}{% >>>2
  \printtext[bibhyperlink]{%
    \printfield{shorthand}}}% <<<2

\newbibmacro*{footcite:postnote:ibidpage}{% >>>2
  \iftoggle{cbx:loccit}%
    {}%
    {\MakeLowercase{\usebibmacro{postnote}}}}% <<<2

% <<<1

% Commands >>>1

\DeclareCiteCommand{\cite}[\mkbibfootnote]% >>>2
  {\usebibmacro{prenote}}%
  {\usebibmacro{citeindex}%
   \usebibmacro{footcite}}%
  {\multicitedelim}%
  {\usebibmacro{footcite:postnote}}% <<<2

\DeclareCiteCommand*{\cite}[\mkbibfootnote]% >>>2
  {\usebibmacro{prenote}}%
  {\usebibmacro{citeindex}%
   \usebibmacro{footcite:citepages}%
   \usebibmacro{footcite:short}}%
  {\multicitedelim}%
  {\usebibmacro{footcite:postnote}}% <<<2

\DeclareCiteCommand{\parencite}[\mkbibparens]% >>>2
  {\usebibmacro{prenote}}%
  {\usebibmacro{citeindex}%
   \usebibmacro{footcite}}%
  {\multicitedelim}%
  {\usebibmacro{footcite:postnote}}% <<<2

\DeclareCiteCommand{\footcite}[\mkbibfootnote]% >>>2
  {\usebibmacro{prenote}}%
  {\usebibmacro{citeindex}%
   \usebibmacro{footcite}}%
  {\multicitedelim}%
  {\usebibmacro{footcite:postnote}}% <<<2

\DeclareCiteCommand*{\footcite}[\mkbibfootnote]% >>>2
  {\usebibmacro{prenote}}%
  {\usebibmacro{citeindex}%
   \usebibmacro{footcitestar}}%
  {\multicitedelim}%
  {\usebibmacro{footcite:postnote}}% <<<2

\DeclareCiteCommand{\footcitetext}[\mkbibfootnotetext]% >>>2
  {\usebibmacro{prenote}}%
  {\usebibmacro{citeindex}%
   \usebibmacro{footcite}}%
  {\multicitedelim}%
  {\usebibmacro{footcite:postnote}}% <<<2

\DeclareCiteCommand{\smartcite}[\iffootnote\mkbibparens\mkbibfootnote]% >>>2
  {\usebibmacro{prenote}}%
  {\usebibmacro{citeindex}%
   \usebibmacro{footcite}}%
  {\multicitedelim}%
  {\usebibmacro{footcite:postnote}}% <<<2

% <<<1

% apud >>>1

\newcommand{\addapud}{%% >>>2
  \renewcommand*{\multicitedelim}{%
    \ifnumequal{\value{multicitecount}}{\value{multicitetotal}}%
      {\space\bibstring{apud}}%
      {\addsemicolon}%
    \space}%
  \renewcommand*{\textcitedelim}{%
    \ifnumequal{\value{multicitecount}}{\value{multicitetotal}}%
      {\addspace\bibstring{apud}}%
      {\addsemicolon}%
    \space}%
}% <<<2

\DeclareCiteCommand{\@apud}% >>>2
	{\iffieldundef{prenote}{}{%
      \ifnumgreater{\value{multicitetotal}}{1}{%
        \usebibmacro{prenote}%
      }{%
        \printfield[uppercasecite]{prenote}%
        \isdot\addspace\bibstring{apud}\addspace%
      }%
    }%
  }%
	{\usebibmacro{citeindex}%
	\usebibmacro{footcite:short}}%
	{\setunit{\multicitedelim}}%
	{\usebibmacro{postnote}}% <<<2

\DeclareMultiCiteCommand{\apud}[\addapud\mkbibfootnote]% >>>2
	{\@apud}%
	{\setunit{\multicitedelim}%
}% <<<2

% <<<1

% textcite >>>1

\newbibmacro*{textcite}{% >>>2
  \ifnameundef{labelname}%
    {\printfield[firstword]{labeltitle}}%
    {\printnames{labelname}}}% <<<2

\newbibmacro*{textcite:init}{% >>>2
  \citetrackerfalse%
  \pagetrackerfalse%
  \iffirstcitekey%
    {\global\undef\cbx@lasthash}%
    {}}% <<<2

\newbibmacro*{textcite:count}{% >>>2
  \stepcounter{textcitetotal}%
  \ifnumgreater{\value{uniquelist}}{\value{maxnames}}%
    {\ifnumgreater{\value{uniquelist}}{\value{textcitemaxnames}}%
       {\setcounter{textcitemaxnames}{\value{uniquelist}}}%
       {}}%
    {\ifnumless{\value{labelname}}{\value{maxnames}}%
       {\ifnumgreater{\value{labelname}}{\value{textcitemaxnames}}%
          {\setcounter{textcitemaxnames}{\value{labelname}}}%
          {}}%
       {\ifnumgreater{\value{maxnames}}{\value{textcitemaxnames}}%
          {\setcounter{textcitemaxnames}{\value{maxnames}}}%
          {}}}}% <<<2

\DeclareCiteCommand{\cbx@textcite}% >>>2
  {\usebibmacro{textcite:init}}%
  {\iffieldequals{namehash}{\cbx@lasthash}%
     {}%
     {\iffirstcitekey%
        {}%
        {\textcitedelim}%
      \stepcounter{textcitecount}%
      \usebibmacro{textcite}%
      \savefield{namehash}{\cbx@lasthash}}}%
  {}%
  {}% <<<2

\DeclareCiteCommand{\textcite}[\cbx@textcite@init\cbx@textcite\footcite]% >>>2
  {\usebibmacro{textcite:init}%
   \gdef\cbx@savedkeys{}%
   \DeferNextCitekeyHook}%
  {\ifthenelse{\iffirstcitekey\AND\value{multicitetotal}>0}%
     {\protected@xappto\cbx@footcite@args{%
        (\thefield{multiprenote})(\thefield{multipostnote})}}%
     {}%
   \xappto\cbx@savedkeys{\thefield{entrykey},}%
   \iffieldequals{namehash}{\cbx@lasthash}%
     {}%
     {\usebibmacro{textcite:count}%
      \savefield{namehash}{\cbx@lasthash}}%
   \ifnumequal{\value{citecount}}{\value{citetotal}}{%
      \protected@xappto\cbx@textcite@args{{\cbx@savedkeys}}%
      \protected@xappto\cbx@footcite@args{%
        [\thefield{prenote}][\thefield{postnote}]{\cbx@savedkeys}%
      }%
      \iflastcitekey{%
         \protected@xappto\cbx@textcite@args{\thefield{postpunct}}%
         \protected@xappto\cbx@footcite@args{\nopunct}%
      }{}%
    }{}}%
  {}%
  {}% <<<2

% textcite has nested \DeclareCiteCommand definitions for
% textcite and we want to use the normal textcite context
\DeclareDelimcontextAlias{cbx@textcite}{textcite}%

\newrobustcmd{\cbx@textcite@init}[3]{% >>>2
  \setcounter{textcitetotal}{0}%
  \setcounter{textcitecount}{0}%
  \setcounter{textcitemaxnames}{0}%
  \def\cbx@textcite@args{#1}\def\cbx@footcite@args{#2}#3%
  \cbx@textcite@args\@empty\cbx@footcite@args\@empty}% <<<2

\DeclareMultiCiteCommand{\cbx@textcites}{\cbx@textcite}{}%
\DeclareMultiCiteCommand{\textcites}%
  [\cbx@textcites@init\cbx@textcites\footcites]{\textcite}{}%

\let\cbx@textcites@init\cbx@textcite@init% >>>2
\pretocmd{\cbx@textcites@init}{%
  \UseNextMultiCiteHook%
  \AtNextMultiCite{%
    \renewbibmacro{multiprenote}{}%
    \renewbibmacro{multipostnote}{}}}{}{}% <<<2

\xpretobibmacro{textcite}{% >>>3
  \let\multinamedelim\multinamedelimorig%
  \let\finalnamedelim\finalnamedelimorig%
  \let\mkbibnamefamily\origmkbibnamefamily%
  \let\mkbibnamegiven\origmkbibnamegiven%
  \let\mkbibnameprefix\origmkbibnameprefix%
  \let\mkbibnamesuffix\origmkbibnamesuffix%
}{}{}% <<<3

% <<<1

\endinput%


% vim: set foldmarker=\ >>>,\ <<< :

