% \iffalse meta-comment
% vim: textwidth=75
%<*internal>
\iffalse
%</internal>
%<*readme>
|
-------:| -----------------------------------------------------------------
  icite:| Make Indices locorum citatorum
 Author:| Robert Alessi
 E-mail:| alessi@robertalessi.net
License:| Released under the GNU General Public License v3 or later
    See:| http://www.gnu.org/licenses/


Short description:
Some text about the package: probably the same as the abstract.
%</readme>
%<*internal>
\fi
\def\nameofplainTeX{plain}
\ifx\fmtname\nameofplainTeX\else
  \expandafter\begingroup
\fi
%</internal>
%<*install>
\input docstrip.tex
\keepsilent
\askforoverwritefalse
\preamble

Copyright (C) 2019 by Robert Alessi <alessi@robertalessi.net>

Please send error reports and suggestions for improvements to Robert
Alessi <alessi@robertalessi.net>

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see
<http://www.gnu.org/licenses/>.

This work consists of the file icite.dtx and a Makefile.
Running "make" generates the derived files README, icite.pdf and icite.sty.
Running "make inst" installs the files in the user's TeX tree.
Running "make install" installs the files in the local TeX tree.

\endpreamble

\usedir{tex/latex/icite}
\generate{
  \file{\jobname.sty}{\from{\jobname.dtx}{package}}
}
%</install>
%<install>\endbatchfile
%<*internal>
\usedir{source/latex/icite}
\generate{
  \file{\jobname.ins}{\from{\jobname.dtx}{install}}
}
\nopreamble\nopostamble
\usedir{doc/latex/icite}
\generate{
  \file{README.txt}{\from{\jobname.dtx}{readme}}
}
\ifx\fmtname\nameofplainTeX
  \expandafter\endbatchfile
\else
  \expandafter\endgroup
\fi
%</internal>
% \fi
%
% \iffalse
%<*driver>
\ProvidesFile{icite.dtx}
%</driver>
%<package>\NeedsTeXFormat{LaTeX2e}[1999/12/01]
%<package>\ProvidesPackage{icite}
%<*package>
    [2019/02/22 v1.00 Make Indices locorum citatorum]
%</package>
%<*driver>
\documentclass{ltxdoc}
\usepackage{filecontents}
\begin{filecontents*}{\jobname.bib}
@software{usebib,
  title = {The Usebib package},
  subtitle = {A simple bibliography processor},
  author = {Gregorio, Enrico},
  publisher = {CTAN},
  url = {http://www.ctan.org/pkg/usebib},
  date = {2012-04-13},
  version = {1.0a}
}
\end{filecontents*}
\usepackage[english]{babel}
\usepackage[letterpaper,margin=25mm,left=50mm,nohead]{geometry}
\usepackage{fontspec}
\setmainfont{Old Standard}[RawFeature={+ss05;+ss06}]
\usepackage{newunicodechar}
\newunicodechar{ǧ}{ǧ} % Old Standard does not include ǧ/Ǧ
\newunicodechar{Ǧ}{Ǧ} %
\usepackage{hyperxmp}
\usepackage{uri}
\usepackage[numbered]{hypdoc}
\hypersetup{unicode=true, colorlinks, allcolors=blue,
  linktocpage=true, pdfauthor={Robert Alessi}, pdftitle={The
    icite package}, pdfcontactemail={alessi@robertalessi.net},
  pdfcontacturl={http://www.robertalessi.net/icite},
  pdfcopyright={Copyright (C) 2019 Robert Alessi
    <alessi@robertalessi.net>. This document is licensed under the
    Creative Commons Attribution-ShareAlike 4.0 International
    License. To view a copy of this license, visit
    http://creativecommons.org/licenses/by-sa/4.0/ or send a letter to
    Creative Commons, PO Box 1866, Mountain View, CA 94042, USA.},
  pdflicenseurl={https://creativecommons.org/licenses/by-sa/4.0/legalcode},
  pdfmetalang={en-US}, pdftype={Text}, pdfkeywords={bibtex, biblatex,
    indexing}}
\CodelineIndex
\usepackage[citecmd=footcite,defaultindex]{\jobname}
\bibinput{icite}
\usepackage[nonewpage,xindy]{imakeidx}
\indexsetup{level=\section}
\makeindex[intoc]
\makeindex[name=loccit, title=Modern Authors]
\usepackage[scale=1.5]{ccicons}
\usepackage{dox}
\doxitem{Option}{option}{options}
\usepackage{enumitem}
\setlist{nosep}
\setlist[itemize]{label=\textendash}
\setlist[enumerate,1]{label=(\alph*)}
\setlist[enumerate,2]{label=\roman*.}
\usepackage[lot]{multitoc}
\usepackage{nameref}
\usepackage{csquotes}
\usepackage[style=oxnotes]{biblatex}
\addbibresource{icite.bib}
\usepackage{hologo,metalogox}
\usepackage{relsize}
\usepackage{units}
\usepackage{minted}
\usepackage[contents]{colordoc}
\newcommand{\package}[1]{\textsf{#1}\index{#1=#1 (package)}}
\usepackage{tikz}
\usepackage[breakable, skins, xparse, minted]{tcolorbox}
\tcbset{colback=white, boxrule=.15mm, colframe=red!50!white, left=6mm,
  breakable}
\newtcblisting{iexample}{minted options=linenos}
\newtcblisting{icode}{minted options=linenos, listing only}
\usepackage{etoc}
\etocsettocdepth{paragraph}
\newcommand{\icitetableofcontents}{%
  \begingroup
  \etocsetstyle{section}{}{}
  {\etocsavedsectiontocline{%
      \numberline{\etocnumber}\etocname}{\etocpage}}{}
  \etocsetstyle{subsection}{}{}
  {\etocsavedsubsectiontocline{%
      \numberline{\etocnumber}\etocname}{\etocpage}}{}%
  \etocsetstyle{subsubsection}{}{}
  {\etocsavedsubsubsectiontocline{%
      \numberline{\etocnumber}\etocname}{\etocpage}}{}%
  \etocsetstyle{paragraph}{}{\leftskip2cm\rightskip 2.2em \parfillskip
    0pt plus 1fil\relax \nobreak}
  {\noindent\etocname{} \etocpage{} }{\par}%
  \etocmulticolstyle[2]{\section*{Contents}}
  \pdfbookmark[1]{Contents}{toc}
  \tableofcontents
  \endgroup}
\EnableCrossrefs
%\CodelineIndex
\RecordChanges
%\OnlyDescription
\begin{document}
 \DocInput{\jobname.dtx}
 \PrintChanges
 \indexprologue{Numbers written in italic refer to the page where the
   corresponding entry is described; numbers underlined refer to the
   code line of the definition; numbers in roman refer to the code
   lines where the entry is used.}
 \printindex
 %\PrintIndex
\end{document}
%</driver>
% \fi
%
% \CheckSum{0}
%
% \CharacterTable
%  {Upper-case    \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
%   Lower-case    \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z
%   Digits        \0\1\2\3\4\5\6\7\8\9
%   Exclamation   \!     Double quote  \"     Hash (number) \#
%   Dollar        \$     Percent       \%     Ampersand     \&
%   Acute accent  \'     Left paren    \(     Right paren   \)
%   Asterisk      \*     Plus          \+     Comma         \,
%   Minus         \-     Point         \.     Solidus       \/
%   Colon         \:     Semicolon     \;     Less than     \<
%   Equals        \=     Greater than  \>     Question mark \?
%   Commercial at \@     Left bracket  \[     Backslash     \\
%   Right bracket \]     Circumflex    \^     Underscore    \_
%   Grave accent  \`     Left brace    \{     Vertical bar  \|
%   Right brace   \}     Tilde         \~}
%
%   \makeatletter
%   \let\org@changes@\changes@
%   \def\my@changes v#1.#2.#3\@nil{%
%   \org@changes@{v#1.\six@digits{#2}.#3=v#1.#2.#3}%
% }%
%   \newcommand*{\six@digits}[1]{%
%   \ifnum#1<100000 0\fi
%   \ifnum#1<10000 0\fi
%   \ifnum#1<1000 0\fi
%   \ifnum#1<100 0\fi
%   \two@digits{#1}%
% }%
%   \renewcommand*{\changes@}[1]{%
%   \my@changes#1.\@nil
% }%
%   \makeatother
%
% \GetFileInfo{\jobname.dtx}
% \DoNotIndex{\newcommand,\newenvironment}
%
% \title{\tcbox[colframe=black, enhanced, tikznode, drop lifted
% shadow, colback=white, boxrule=.25mm]{%
% The \textsf{icite} package\\
% \emph{Indices locorum citatorum}\\
% \fileversion\ --- \filedate}}
%  
% \author{Robert Alessi \\
% \href{mailto:alessi@robertalessi.net?Subject=icite package}%
% {\texttt{alessi@robertalessi.net}}}
% 
% \date{}
%
% \maketitle
% \footnotesize
% \icitetableofcontents
% \normalsize
%
%\changes{v1.00}{2019/02/22}{First public release}
%
% \begin{abstract}
%   \package{icite} is designed to produce from \hologo{BibTeX} or
%   Bib\LaTeX\ bibliographic databases the different indices of cited
%   authors and works which are called \emph{indices locorum
%   citatorum}. It relies on a specific \cs{icite} command and can
%   operate with either \hologo{BibTeX} or Bib\LaTeX.
% \end{abstract}
%
% \section*{License and disclamer}
% \addcontentsline{toc}{section}{License and disclamer}
% \subsection*{License applicable to the software}
% \label{sec:license-software}
%
% \package{icite} --- \emph{Indices locorum citatorum}\\
% Copyright \textcopyright\ 2019  Robert Alessi
%
% Please send error reports and suggestions for improvements to Robert
% Alessi:
% \begin{itemize}
% \item email: \mailto[icite package]{alessi@roberalessi.net}
% \item website: \url{http://www.robertalessi.net/icite}
% \item development: \url{http://git.robertalessi.net/icite}
% \item comments, feature requests, bug reports:
% \url{https://gitlab.com/ralessi/icite/issues}
% \end{itemize}
%
% \marginpar{\texttt{gpl3+}}
% This program is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% This program is distributed in the hope that it will be useful, but
% WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
% General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with this program.  If not, see
% <http://www.gnu.org/licenses/>.
%
% This release of \package{icite} consists of the following
% source files:
% \begin{itemize}
% \item |icite.dtx|
% \item |Makefile|
% \end{itemize}
% 
% \subsection*{License applicable to this document}
% \label{sec:documentation-license}
% Copyright \textcopyright\ 2019  Robert Alessi
%
% \ccbysa\marginpar{\texttt{CC BY-SA 4.0}}
% This document is licensed under the Creative Commons
% Attribution-ShareAlike 4.0 International License. To view a copy of
% this license, visit
% \url{http://creativecommons.org/licenses/by-sa/4.0/} or send a
% letter to Creative Commons, PO Box 1866, Mountain View, CA 94042,
% USA.
%
% \section{Introduction}
% \label{sec:introduction}
% Bib\LaTeX\ features a very powerful internal mechanism which
% supports indexing of citations and bibliographic entries and can be
% activated by a simple |indexing| option in the preamble. However, by
% default, only the authors and the works cited are inserted in the
% index. Furthermore, authors and works are indexed separately and
% only inserted in the global index that is generated by
% \cs{makeindex}. Another limitation is that the references are also
% excluded from the index.
%
% Notwithstanding these limitations, it is quite possible to typeset
% \emph{indices locorum citatorum} with Bib\LaTeX, but this remains an
% intricate business and requires knowing how to redefine and/or patch
% standard and internal Bib\LaTeX\ commands.
%
% The \package{icite} package is but a modest piece of software which
% addresses this situation. It relies on citation commands, but does
% not require Bib\LaTeX. Instead, \hologo{BibTeX} can be used as
% \package{icite} uses the \package{usebib} package\icite{usebib} to
% build the indices by drawing directly from the bibliographic
% database.
%
% \printindex[loccit]
% \printbibliography[heading=bibnumbered]
% 
%\StopEventually{}
%
% \section{Implementation}
%
% \iffalse
%<*package>
% \fi
% Declare the global options, and define them:
%    \begin{macrocode}
\RequirePackage{xkeyval}
\DeclareOptionX{citecmd}[cite]{\def\ic@dfltcit{#1}}
\newif\ifdefault@index
\newif\ifno@index
\DeclareOptionX{defaultindex}[loccit]{
  \edef\@tempa{#1}
  \edef\@none{none}
  \ifx\@tempa\@none
  \no@indextrue
  \else
  \default@indextrue
  \def\ic@dfltind{#1}
  \fi
}
\ExecuteOptionsX{citecmd}
\ProcessOptionsX\relax
%    \end{macrocode}
% The following packages are required by \package{icite}:
%    \begin{macrocode}
\RequirePackage{xparse}
\RequirePackage{datatool}
\RequirePackage{usebib}
%    \end{macrocode}
% Define fields to be used by \package{icite}:
%    \begin{macrocode}
\define@reuse@key{author}
\define@reuse@key{indexauthor}
\define@reuse@key{sortname}
\define@reuse@key{title}
\define@reuse@key{shorttitle}
\define@reuse@key{indextitle}
\define@reuse@key{indexsorttitle}
\define@reuse@key{entrysubtype}
%    \end{macrocode}
% This is the same as \cs{usebibentry} from \package{ebib}, but it
% does not return an error if the entry field is not found:
%    \begin{macrocode}
\def\get@bibentry#1#2{\@ifundefined{reuse@#1@#2}{}
  {\@nameuse{reuse@#1@#2}}}
%    \end{macrocode}
% Create a new database which \package{icite} will use to connect
% Bib\LaTeX\ \enquote*{subtypes} to indices.
%    \begin{macrocode}
\DTLnewdb{icite@indices}
%    \end{macrocode}
% \DescribeMacro{\IndexSubtypeAs} \cs{IndexSubtypeAs} takes two
% mandatory arguments: 1. Any given keyword used to specify an
% \enquote*{entrysubtype} in the bibliographical database and 2. The
% index in which the authors matching that subtype must go. This
% command is to be found in the preamble only.
%    \begin{macrocode}
\NewDocumentCommand{\IndexSubtypeAs}{m m}{%
  \DTLnewrow{icite@indices}
  \DTLnewdbentry{icite@indices}{subtype}{#1}
  \DTLnewdbentry{icite@indices}{index}{#2}
}
\@onlypreamble\IndexSubtypeAs
%    \end{macrocode}
% \DescribeMacro{\SetTitleStyle} By default, titles are printed in
% italics. This can be changed in the preamble by \cs{SetTitleStyle}.
%    \begin{macrocode}
\NewDocumentCommand{\TitleStyle}{m}{\emph{#1}}
\NewDocumentCommand{\SetTitleStyle}{m}{%
  \RenewDocumentCommand{\TitleStyle}{m}{#1}
}
\@onlypreamble\SetTitleStyle
%    \end{macrocode}
% 
% \DescribeMacro{\icite} \cs{icite} both inserts a formatted citation
% and an entry in the \emph{index locorum citatorum}. It is to be used
% in place of any \hologo{BibTeX} or Bib\LaTeX\ citation command the
% syntax of which is
% \cs{command}\oarg{pre}\oarg{post}\marg{key}. \cs{icite} further
% accepts an optional argument should one wish to specify the
% citation command to be used, like so:\\
% \cs{icite}\oarg{pre}\oarg{post}\marg{key}\oarg{command}\\
% Only standard citation commands are accepted, with the exception of
% qualified citation lists or so-called \enquote*{multicite} commands.
%    \begin{macrocode}
\NewDocumentCommand{\icite}{o o m O{\ic@dfltcit}}{%
  \edef\@subtype{\get@bibentry{#3}{entrysubtype}}%
  \edef\@author{\get@bibentry{#3}{author}}%
  \edef\@indexauthor{\get@bibentry{#3}{indexauthor}}%
  \edef\@sortname{\get@bibentry{#3}{sortname}}%
  \edef\@indexsorttitle{\get@bibentry{#3}{indexsorttitle}}%
  \edef\@indextitle{\get@bibentry{#3}{indextitle}}%
  \edef\@shorttitle{\get@bibentry{#3}{shorttitle}}%
  \edef\@title{\get@bibentry{#3}{title}}%
  \ifx\@indexauthor\empty
      \def\@useauthor{\@author}%
    \else
      \def\@useauthor{\@indexauthor}%
    \fi
  \ifx\@sortname\empty
    \def\@sortedauthor{\@useauthor}%
  \else
    \def\@sortedauthor{{\@sortname}@\@useauthor}%
  \fi
  \ifx\@indextitle\empty
    \ifx\@shorttitle\empty
      \def\@usetitle{\@title}%
    \else
      \def\@usetitle{\@shorttitle}%
    \fi
  \else
    \def\@usetitle{\@indextitle}%
  \fi
  \ifx\@indexsorttitle\empty
    \def\@sortedtitle{{\@usetitle}@\TitleStyle{\@usetitle}}%
  \else
    \def\@sortedtitle{{\@indexsorttitle}@\TitleStyle{\@usetitle}}%
  \fi
  \IfNoValueTF{#1}%
  {\DTLifdbempty{icite@indices}{%
      \ifno@index\else
      \ifdefault@index%
      \index[\ic@dfltind]{\@sortedauthor!\@sortedtitle}%
      \else%
      \index{\@sortedauthor!\@sortedtitle}%
      \fi\fi%
    }{%
      \DTLforeach*{icite@indices}{%
        \icite@subtype=subtype,\icite@index=index}{%
        \ifx\@subtype\icite@subtype%
        \index[\icite@index]{\@sortedauthor!\@sortedtitle}%
        \dtlbreak%
        \else%
        \ifno@index\else
        \ifdefault@index%
        \index[\ic@dfltind]{\@sortedauthor!\@sortedtitle}%
        \else%
        \index{\@sortedauthor!\@sortedtitle}%
        \fi\fi%
        \fi}}
    \csname #4\endcsname{#3}%
  }
  {\IfNoValueTF{#2}%
    {\DTLifdbempty{icite@indices}{%
        \ifno@index\else
        \ifdefault@index%
        \index[\ic@dfltind]{\@sortedauthor!\@sortedtitle!#1}%
        \else%
        \index{\@sortedauthor!\@sortedtitle!#1}%
        \fi\fi%
      }{%
        \DTLforeach*{icite@indices}{%
          \icite@subtype=subtype,\icite@index=index}{%
          \ifx\@subtype\icite@subtype%
          \index[\icite@index]{\@sortedauthor!\@sortedtitle!#1}%
          \dtlbreak%
          \else%
          \ifno@index\else
          \ifdefault@index%
          \index[\ic@dfltind]{\@sortedauthor!\@sortedtitle!#1}%
          \else%
          \index{\@sortedauthor!\@sortedtitle!#1}%
          \fi\fi%
          \fi}}%
      \csname #4\endcsname[{#1}]{#3}}
    {\DTLifdbempty{icite@indices}{%
        \ifno@index\else
        \ifdefault@index%
        \index[\ic@dfltind]{\@sortedauthor!\@sortedtitle!#2}%
        \else%
        \index{\@sortedauthor!\@sortedtitle!#2}%
        \fi\fi%
      }{%
        \DTLforeach*{icite@indices}{%
          \icite@subtype=subtype,\icite@index=index}{%
          \ifx\@subtype\icite@subtype%
          \index[\icite@index]{\@sortedauthor!\@sortedtitle!#2}%
          \dtlbreak%
          \else%
          \ifno@index\else
          \ifdefault@index%
          \index[\ic@dfltind]{\@sortedauthor!\@sortedtitle!#2}%
          \else%
          \index{\@sortedauthor!\@sortedtitle!#2}%
          \fi\fi%
          \fi}}%
      \csname #4\endcsname[#1][{#2}]{#3}}%
  }%
}
%    \end{macrocode}
% \iffalse
%</package>
% \fi
%\Finale
\endinput