+\def\parse@dhd#1{\@ifnextchar[{\parse@dhd@a{#1}}{\parse@dhd@c{#1}}}
+\def\parse@dhd@a#1[#2]{#1{#2}}
+\def\parse@dhd@c#1#2#3{\parse@dhd@cc{#1}{#2}{#3}#3 \q@}
+\def\parse@dhd@cc#1#2#3#4 #5\q@{#1{#4}{#2}{#3}}
+
+\newif\if@dheadfirst
+\def\dhead{\parse@dhd\dhead@}
+\def\dhead@#1#2#3{%
+ \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{}%