+%% \definedescribecategory{CAT}[KIND]{NAME}
+%%
+%% Define a category of things to document. CAT is a short label identifying
+%% the category to `\describe', `\descref', and friends. The NAME is the
+%% text to show in the description headline; it is a macro body which is
+%% passed an argument `#1' that applies a modifier to some portion of the
+%% text.
+%%
+%% The KIND allows additional arguments to be collected, included in label
+%% strings, and mentioned in the index. The default is `plain'.
+\def\definedescribecategory#1{%
+ \@ifnextchar[{\def@desc{#1}}{\def@desc{#1}[plain]}}
+\def\def@desc#1[#2]#3{\@namedef{cat!#1}##1{#3}\@namedef{catsw!#1}{#2}}
+
+%% \describecategoryname[MOD]{CAT}
+%%
+%% Typeset the category name for CAT, modified by MOD.
+\def\describecategoryname{\@ifnextchar[\@descname@i{\@descname@i[]}}
+\def\@descname@i[#1]#2{%
+ \expandafter\let\expandafter\@tempa\csname cat!#2\endcsname%
+ \expandafter\let\expandafter\@tempb\csname modcat/#1\endcsname%
+ \ifx\@tempa\relax\@tempb{#2}\else\@tempa\@tempb\fi}
+
+%% Call a modifier method.
+\def\@mod@dispatch#1#2{\csname #1/#2\endcsname}
+
+%% Call a description method given the category.
+\def\@desc@dispatch#1#2{%
+ \csname #1/%
+ \expandafter\ifx\csname catsw!#2\endcsname\relax plain%
+ \else \csname catsw!#2\endcsname \fi%
+ \endcsname%
+}
+
+%% Modifier methods for the default empty modifier.
+\@namedef{modcat/}#1{#1}
+\@namedef{modlabel/}#1{#1}
+\@namedef{modindex/}#1{#1@\noexpand\code{#1}}
+
+%% Modifier methods for `setf'. The name text prefixes the relevant word
+%% with `setf-'.
+\@namedef{modcat/setf}#1{\code{setf}-#1}
+\@namedef{modlabel/setf}#1{setf/#1}
+\@namedef{modindex/setf}#1{#1@\noexpand\code{#1}}
+
+%% Modifier methods for words with *earmuffs*. Sort into the index without
+%% the earmuffs.
+\@namedef{modcat/muffs}#1{#1}
+\@namedef{modlabel/muffs}#1{*#1*}
+\@namedef{modindex/muffs}#1{#1@\noexpand\code{*#1*}}
+
+%% Modifier methods for :keywords. Sort into the index without the `:'.
+\@namedef{modcat/kwd}#1{#1}
+\@namedef{modlabel/kwd}#1{:#1}
+\@namedef{modindex/kwd}#1{#1@\noexpand\code{:#1}}
+
+%% Category-kind methods for plain categories.
+\@namedef{descargs/plain}#1{#1{}}
+\@namedef{desclabel/plain}#1#2#3{#1:\@mod@dispatch{modlabel}{#2}{#3}}
+\@namedef{descindex/plain}#1#2#3{%
+ \@mod@dispatch{modindex}{#2}{#3}!%
+ \protect\describecategoryname[#2]{#1}%
+}
+
+%% Category-kind methods for `method' categories. Collect an extra argument
+%% listing the specializers: include them in the label, and typeset them in
+%% the index.
+\def\@fmtspecs#1#2{%
+ \if!#2!\else\count@\z@\toks@{}#1\@fmtspecs@i#2,\q@,\fi}
+\def\@fmtspecs@i#1,{%
+ \def\@tempa{#1}%
+ \ifx\@tempa\q@%
+ \ifcase\count@(no args?!)%
+ \or% nothin' doin
+ \or \space and %
+ \else, and %
+ \fi%
+ \the\toks@%
+ \else%
+ \ifnum\count@>\@ne, \fi%
+ \the\toks@%
+ \toks@{\code{#1}}\advance\count@\@ne%
+ \expandafter\@fmtspecs@i%
+ \fi%
+}
+\@namedef{descargs/method}#1#2{#1{{#2}}}
+\@namedef{desclabel/method}#1#2#3#4%
+ {#1:\@mod@dispatch{modlabel}{#2}{#3}(#4)}
+\@namedef{descindex/method}#1#2#3#4{%
+ \@mod@dispatch{modindex}{#2}{#3}!%
+ \protect\describecategoryname[#2]{#1}%
+ \protect\@fmtspecs{ specialized at }{#4}%
+}
+
+%% Some magic strings.