From 7e94c5fe2eff01fe81fde1f54c53d3eefb254ba0 Mon Sep 17 00:00:00 2001
Message-Id: <7e94c5fe2eff01fe81fde1f54c53d3eefb254ba0.1718982825.git.mdw@distorted.org.uk>
From: Mark Wooding <mdw@chiark.greenend.org.uk>
Date: Sun, 30 Aug 2015 10:58:38 +0100
Subject: [PATCH] doc/sod.sty: Overhaul `describe' environment.
Organization: Straylight/Edgeware

From: Mark Wooding <mdw@distorted.org.uk>

  * Split out the machinery which parses and sets the heading to make a
    new environment `describe*' which can have multiple headings.

  * Remove the `tabular' layer in the headings, by using `\rlap'
    instead.

  * Fix spacing around the header lines.

  * Insert penalties and drive the `\if@nobreak' and `\@afterheading'
    machinery correctly so that we mostly don't get bad page breaks.
---
 doc/sod.sty | 51 +++++++++++++++++++++++++++++++++------------------
 1 file changed, 33 insertions(+), 18 deletions(-)

diff --git a/doc/sod.sty b/doc/sod.sty
index ff63620..71b72fe 100644
--- a/doc/sod.sty
+++ b/doc/sod.sty
@@ -159,26 +159,41 @@
 \def\nlret{\\\hspace{4em}\returns}
 
 \def\q@{\q@}
-\newenvironment{describe}[3][\q@]{%
+\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}%
+  \rlap{\rightline{\normalfont\bfseries[\describecategoryname{#2}]}}%
+  #3%
+}
+
+\def\desc@begin#1{%
   \normalfont%
-  \par\goodbreak%
-  \vspace{\bigskipamount}%
-  \setbox\z@\hbox{\bfseries[\describecategoryname{#2}]}%
-  \dimen@\linewidth\advance\dimen@-\wd\z@%
-  \def\@temp##1 ##2\q@{%
-    \message{#2:##1}%
-    {\def\@uscore####1{-}\edef\@tempb{\noexpand\label{#2:##1}}\@tempb}%
-  }%
-  \def\@tempa{#1}\ifx\@tempa\q@\@temp#3 \q@\else\@temp{#1} \q@\fi%
-  \edef\@temp{{\the\linewidth}{@{}p{\the\dimen@}%
-      @{\extracolsep{\fill}}l@{\extracolsep{0pt}}}}%
-  \noindent\csname tabular*\expandafter\endcsname\@temp%
-  \tabbing\codeface#3\endtabbing&\unhbox\z@\\\endtabular%
-%  \@afterheading%
-  \list{}{\rightmargin\z@}\item%
-}{%
-  \endlist%
+  \if@nobreak\else\par\goodbreak\fi%
+  \global\@dheadfirsttrue%
+  \begingroup%
+    \codeface%
+    \let\@endparenv\relax%
+    \clubpenalty\@M \widowpenalty\@M \interlinepenalty50%
+    \tabbing#1\endtabbing%
+  \endgroup%
+  \penalty\@M\@afterheading%
+  \list{}{\rightmargin\z@\topsep\z@}\item%
 }
+\let\desc@end\endlist
+
+\@namedef{describe*}#1{\desc@begin{#1}}
+\expandafter\let\csname enddescribe*\endcsname\desc@end
+\def\describe{\parse@dhd\desc@}
+\def\desc@#1#2#3{\desc@begin{\dhead@{#1}{#2}{#3}}}
+\let\enddescribe\desc@end
 
 %%%----- That's all, folks --------------------------------------------------
 \endinput
-- 
[mdw]

