From ddfe426573957fa52102832a51582bb134ee7b42 Mon Sep 17 00:00:00 2001
From: Mark Wooding <mdw@distorted.org.uk>
Date: Thu, 1 Oct 2015 11:49:58 +0100
Subject: [PATCH] doc/: Initial work on an index.

---
 doc/Makefile.am |  4 +++-
 doc/sod.sty     | 22 ++++++++++++++++++++--
 doc/sod.tex     |  7 +++++++
 3 files changed, 30 insertions(+), 3 deletions(-)

diff --git a/doc/Makefile.am b/doc/Makefile.am
index a6894be..1037032 100644
--- a/doc/Makefile.am
+++ b/doc/Makefile.am
@@ -28,7 +28,7 @@ include $(top_srcdir)/vars.am
 doc_DATA		 =
 TEX_FILES		 =
 
-CLEANFILES		+= *.aux *.out *.log *.toc
+CLEANFILES		+= *.aux *.out *.log *.toc *.ind *.idx *.ilg
 EXTRA_DIST		+= $(TEX_FILES)
 
 ###--------------------------------------------------------------------------
@@ -70,5 +70,7 @@ doc_DATA		+= sod.pdf
 sod.pdf: $(TEX_FILES)
 	cd $(srcdir) && pdflatex --output-directory=$(abs_builddir) sod.tex
 	cd $(srcdir) && pdflatex --output-directory=$(abs_builddir) sod.tex
+	makeindex sod.idx
+	cd $(srcdir) && pdflatex --output-directory=$(abs_builddir) sod.tex
 
 ###----- That's all, folks --------------------------------------------------
diff --git a/doc/sod.sty b/doc/sod.sty
index 1f3655d..acc4370 100644
--- a/doc/sod.sty
+++ b/doc/sod.sty
@@ -63,7 +63,10 @@
 
 %% Literal code is in sans face.
 \let\codeface\sffamily
-\def\code#1{\ifmmode\hbox\fi{\normalfont\codeface\/#1\/}}
+\DeclareRobustCommand\code[1]{%
+  \ifmmode\hbox\else\leavevmode\fi%
+  {\normalfont\codeface\/#1\/}%
+}
 \def\ulitleft{\normalfont\codeface}
 \let\ulitright\empty
 
@@ -157,6 +160,7 @@
 \definedescribecategory{af-meth}{after method}
 \definedescribecategory{cls}{class}
 \definedescribecategory{ty}{type}
+\definedescribecategory{type}{type}
 \definedescribecategory{mac}{macro}
 \definedescribecategory{lmac}{local macro}
 \definedescribecategory{parse}{parser spec}
@@ -178,12 +182,26 @@
   \if@dheadfirst\global\@dheadfirstfalse\else\relax\\[\smallskipamount]\fi%
   {\let\protect\@empty\def\@uscore{_\@gobble}\message{#2:#1}%
    \def\@uscore{-\@gobble}\edef\@tempa{\noexpand\label{#2:#1}}\@tempa}%
+  {\begingroup\lccode`\~=`\_\lowercase{\endgroup\def~{_}}%
+   \protected@edef\@tempa##1{%
+     \noexpand\index{%
+       #1@{\noexpand\code{#1}}!%
+       \csname cat!#2\endcsname%
+       ##1%
+     }%
+   }%
+   \@tempa{|(}%
+   \toks@\expandafter{\after@desc}%
+   \toks\tw@\expandafter{\@tempa{|)}}%
+   \xdef\after@desc{\the\toks@\the\toks\tw@}}%
   \rlap{\hb@xt@\linewidth{\hfil\normalfont\bfseries
       [\describecategoryname{#2}]}}%
   #3%
 }
 
 \def\desc@begin#1{%
+  \let\saved@after@desc\after@desc%
+  \gdef\after@desc{}%
   \normalfont%
   \if@nobreak\else\par\goodbreak\fi%
   \global\@dheadfirsttrue%
@@ -196,7 +214,7 @@
   \penalty\@M\@afterheading%
   \list{}{\rightmargin\z@\topsep\z@}\item%
 }
-\let\desc@end\endlist
+\def\desc@end{\endlist\after@desc\global\let\after@desc\saved@after@desc}
 
 \@namedef{describe*}#1{\desc@begin{#1}}
 \expandafter\let\csname enddescribe*\endcsname\desc@end
diff --git a/doc/sod.tex b/doc/sod.tex
index 3ee1a5d..df1e4c8 100644
--- a/doc/sod.tex
+++ b/doc/sod.tex
@@ -42,6 +42,8 @@
 
 \usepackage{sod}
 
+\makeindex
+
 \title{A Sensible Object Design for C}
 \author{Mark Wooding}
 
@@ -148,6 +150,11 @@
 
 \include{cutting-room-floor}
 
+%%%--------------------------------------------------------------------------
+\backmatter
+
+\input{sod.ind}
+
 %%%----- That's all, folks --------------------------------------------------
 \end{document}
 
-- 
2.11.0

