From a897207bc6a042ccd59fe932ca6d2d8dd27ad1e2 Mon Sep 17 00:00:00 2001
From: Robert Alessi <alessi@robertalessi.net>
Date: Sun, 3 Mar 2019 10:09:34 +0100
Subject: use datatool to map subtypes

---
 icite.dtx | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++++++--------
 1 file changed, 70 insertions(+), 9 deletions(-)

(limited to 'icite.dtx')

diff --git a/icite.dtx b/icite.dtx
index db2d34b..d0cde96 100644
--- a/icite.dtx
+++ b/icite.dtx
@@ -91,6 +91,7 @@ Running "make install" installs the files in the local TeX tree.
 %<*driver>
 \documentclass{ltxdoc}
 \usepackage[letterpaper,margin=25mm,left=50mm,nohead]{geometry}
+\usepackage{dox}
 \usepackage{\jobname}
 \usepackage{metalogox}
 \usepackage{hyperxmp}
@@ -228,14 +229,25 @@ Running "make install" installs the files in the local TeX tree.
 %    \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{etoolbox}
-\RequirePackage{xstring}
 \RequirePackage{xparse}
+\RequirePackage{datatool}
 \RequirePackage{usebib}
 %    \end{macrocode}
 % Define fields to be used by \package{icite}:
@@ -255,6 +267,27 @@ Running "make install" installs the files in the local TeX tree.
 \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} take 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
+\AtBeginDocument{
+  \DTLifdbempty{icite@indices}{\IndexSubtypeAs{\relax}{\relax}}{}
+}
+%    \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
@@ -266,8 +299,8 @@ Running "make install" installs the files in the local TeX tree.
 % Only standard citation commands are accepted, with the exception of
 % qualified citation lists or so-called \enquote*{multicite} commands.
 %    \begin{macrocode}
-\NewDocumentCommand{\icite@index}{m}{\index{#1}}
 \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}}%
@@ -295,17 +328,45 @@ Running "make install" installs the files in the local TeX tree.
     \def\@usetitle{\@indextitle}%
   \fi
   \ifx\@indexsorttitle\empty
-    \def\@sortedtitle{\@usetitle}%
+    \def\@sortedtitle{{\@usetitle}@\emph{\@usetitle}}%
   \else
-    \def\@sortedtitle{{\@indexsorttitle}@\@usetitle}%
+    \def\@sortedtitle{{\@indexsorttitle}@\emph{\@usetitle}}%
   \fi
   \IfNoValueTF{#1}%
-  {\index{\@sortedauthor!\@sortedtitle}%
-    \csname #4\endcsname{#3}}
+  {\DTLforeach*{icite@indices}{\icite@subtype=subtype,\icite@index=index}{%
+    \ifx\icite@subtype\@subtype%
+    \index[\icite@index]{\@sortedauthor!\@sortedtitle}%
+    \else%
+    \ifno@index\else
+    \ifdefault@index%
+    \index[\ic@dfltind]{\@sortedauthor!\@sortedtitle}%
+    \else%
+    \index{\@sortedauthor!\@sortedtitle}%
+    \fi\fi\fi}%
+    \csname #4\endcsname{#3}%
+  }
   {\IfNoValueTF{#2}%
-    {\index{\@sortedauthor!\@sortedtitle!#1}%
+    {\DTLforeach*{icite@indices}{\icite@subtype=subtype,\icite@index=index}{%
+        \ifx\icite@subtype\@subtype%
+        \index[\icite@index]{\@sortedauthor!\@sortedtitle!#1}%
+        \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}}
-    {\index{\@sortedauthor!\@sortedtitle!#2}%
+    {\DTLforeach*{icite@indices}{\icite@subtype=subtype,\icite@index=index}{%
+        \ifx\icite@subtype\@subtype%
+        \index[\icite@index]{\@sortedauthor!\@sortedtitle!#2}%
+        \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}}%
   }%
 }
-- 
cgit v1.2.3

