mdwref.dtx: Include help text with errors.
[mdwtools] / mdwref.dtx
index f1da45c..66c2d07 100644 (file)
@@ -1,38 +1,30 @@
-% \begin{meta-comment}
-%
-% $Id: mdwref.dtx,v 1.3 2003/11/10 14:43:48 mdw Exp $
-%
-% Slightly fancy cross-referencing stuff
-%
-% (c) 2007 Mark Wooding
-%
-% \end{meta-comment}
-%
 % \begin{meta-comment} <general public licence>
 %%
 %% mdwref package -- slightly fancy cross-referencing stuff
-%% Copyright (c) 2007 Mark Wooding
+%% Copyright (c) 2007, 2019 Mark Wooding
 %%
-%% This program is free software; you can redistribute it and/or modify
-%% it under the terms of the GNU General Public License as published by
-%% the Free Software Foundation; either version 2 of the License, or
-%% (at your option) any later version.
+%% This file is part of the `mdwtools' LaTeX package collection.
 %%
-%% This program is distributed in the hope that it will be useful,
-%% but WITHOUT ANY WARRANTY; without even the implied warranty of
-%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-%% GNU General Public License for more details.
+%% `mdwtools' is free software: you can redistribute it and/or modify it
+%% under the terms of the GNU General Public License as published by the
+%% Free Software Foundation; either version 2 of the License, or (at your
+%% option) any later version.
+%%
+%% `mdwtools' is distributed in the hope that it will be useful, but
+%% WITHOUT ANY WARRANTY; without even the implied warranty of
+%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+%% General Public License for more details.
 %%
 %% You should have received a copy of the GNU General Public License
-%% along with this program; if not, write to the Free Software
-%% Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+%% along with `mdwtools'.  If not, write to the Free Software Foundation,
+%% Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 %%
 % \end{meta-comment}
 %
 % \begin{meta-comment} <Package preambles>
 %<+package>\NeedsTeXFormat{LaTeX2e}
 %<+package>\ProvidesPackage{mdwref}
-%<+package>                [2007/04/09 1.01 Cross-referencing]
+%<+package>                [2020/09/06 1.14.0 Cross-referencing]
 % \end{meta-comment}
 %
 % ^^A\CheckSum{96}
 %\newcommand{\Xref}[1]{\xref[\toupper]{#1}}
 % \end{listing}
 %
+% \DescribeMacro\formatxref
+% The reference itself is typeset by calling
+% \syntax{"\\formatxref{"<mangle>"}{"<string>"}{"<label>"}"}, which can do as
+% it pleases: the \<mangle> token is from the |\xref| invocation; the
+% \<string> is category of thing being referred to (as established by
+% |\defxref| below); and \<label> is the label, again from |\xref| .  The
+% default behaviour is to print
+% \syntax{<mangle>"{"<string>"}~\\ref{"<label>"}"}, but this can be
+% overridden.
+% (Not quite true: in fact, the default does something better if
+% \package{hyperref} is detected, but the idea is basically the same.)
+%
 % All that remains is to define the strings to be typeset for various kinds
 % of labels.
 % \DescribeMacro\defxref
 %    \end{macrocode}
 % \end{macro}
 %
+% \begin{macro}{\formatxref}
+% Output a cross-reference in the right way.
+%    \begin{macrocode}
+\def\formatxref#1#2#3{%
+  \ifx\hyperref\@@undefined #1{#2}~\ref{#3}%
+  \else \hyperref[#3]{#1{#2}~\ref*{#3}}\fi%
+}
+\def\xref@fallback#1{\formatxref\relax{?\texttt{#1}}{#1}}
+%    \end{macrocode}
+% \end{macro}
+%
 % \begin{macro}{\xref}
 % We're meant to typeset a reference.  The first job is to see whether
 % there's an optional argument.  If so, grab it; otherwise |\relax| will do.
   \def\@tempa{#2}\def\@tempb{#3}%
   \ifx\@tempb\q@delim%
     \PackageError{xref}{Bad ref syntax}%
+      {A reference name doesn't contain a `:'-delimited prefix.  Did you %
+       mean to use plain \string\ref here?}%
+     \xref@fallback{#2}%
   \else%
     \expandafter\let\expandafter\@tempa\csname xref$#2\endcsname%
     \ifx\@tempa\relax%
       \PackageError{xref}{Unknown ref kind `#2'}%
+        {The ref name's prefix `#2' is unknown: either it's been mistyped %
+         or there's a missing \string\defxref somewhere.}%
+     \xref@fallback{#2:#3}%
     \else%
-      \toks@{#1}\toks\tw@\expandafter{\@tempa}%
-      \edef\next@{\the\toks@{\the\toks\tw@}}%
-      \next@~\ref{#2:#3}%
+      \toks@\expandafter{\@tempa}%
+      \edef\next@##1{##1{\the\toks@}}%
+      \next@{\formatxref{#1}}{#2:#3}%
     \fi%
   \fi%
 }