% This is file cwebhmac.tex, part of the CWEBx distribution.

% This file can be loaded after cwebxmac.tex, in order to automatically
% insert \specials for use by the xdvi previewer, or any other HyperTeX
% dvi-file processing tool. Current version is alpha release.

% Copyright 1996, 2006 Marc van Leeuwen
% Last modified: 4 december 2006

% The following macros from cwebxmac.tex are redefined
%   \stsec
%   \note<text><section list>.
%   \finnote<text><section list>.
%   \X<number>:<module name>\X

\ifx\hyperloaded\undefined \let\hyperloaded\relax \else \endinput\fi


% Low level interface to hyperlinks and targets

% \hyperanchor<attributes><anchor text>
% general mechanism to define <achor text> as anchor with <attributes>
\long\def\hyperanchor#1#2{\special{html:<A #1>}{#2}\special{html:</A>}}

% \hyperlink<URL><active text>
% makes <active text> an active link referring to <URL>
\def\hyperlink#1{\hyperanchor{href="#1"}}

% \hypertarget<tag><target text>
% defines a local target for hyperlinks, that can be referenced by <tag>
\def\hypertarget#1{\hyperanchor{name="#1"}}

% \hyperdef<kind><number><target text>
% defines a hypertarget classified by <kind> and sequence <number>
\def\hyperdef#1#2{\hypertarget{#1.#2}}

% \hyperref<assignments><kind><number><active text>
% creates a hyperlink to local target created by \hyperdef<kind><number>
% <assignments> are optional assignments for expansion of <kind> and <number>
{\catcode`\#=12 \gdef\sharp{#}} % to access neutralised '#' character
\def\hyperref#1#2#3%
{{#1\xdef\next{{\sharp#2.#3}}}\expandafter\hyperlink\next}


% High level interface attached to cweavex-generated macros (or to those
% accessed by them via the definitions in cwebxmac.tex)

% Target attached to beginning of each section.
% Calls \hyperdef<kind><number><section number text>
% with <kind>=sec, <number>=\secno
% Replaces:
%\def\stsec % start up section
% {\endcodemode\noindent{\let\*=\lapstar\bf\secstar.\quad}}

\def\stsec
 {\endcodemode\noindent
  \hyperdef{sec}\secno{\let\*=\lapstar\bf\secstar.\quad}%
 }
\let\startsection=\stsec % this one is used; provides hook for extra actions

% List of links attached to the end of certain sections.
% Calls \hyperref<assignments><kind><number><active text> for each number n
% with <assignments>={\let\*=\empty}, <kind>=sec, <number>=n, <active text>=n
% Replaces:
%\def\note#1#2.% for cross-referencing notes, as at the end of a section
%{\Y\noindent{\hangindent2\indentunit\baselineskip10pt\eightrm#1~#2.\par}}
\def\note#1#2.%
{\Y\noindent{\hangindent2\indentunit\baselineskip10pt\eightrm
 #1~\hypersecrefs#2, .\par}}

% Same, but for references from the list of module names.
% Calls \hyperref<assignments><kind><number><active text> for each number n
% with <assignments>={\let\*=\empty}, <kind>=sec, <number>=n, <active text>=n
% Replaces:
%\def\finnote#1#2.{\quad{\eightrm#1~#2.}}
\def\finnote#1#2.{\quad{\eightrm#1~\hypersecrefs#2, .}}

% the following two are auxiliary macros that implement reference lists.

% \hypersecrefs<number>, <number>, ..., <number><ETcode><number>, .
% or \hypersecrefs<number>, .
% make list of hyperlinks to each of then mentioned sections
\def\hypersecrefs#1, #2.%
{\ifx|#2|\def\next{\finalsecrefs#1\ET\ET.#1\ETs\ETs}% final `.' follows below
 \else\hyperref{\let\*=\empty}{sec}{#1}{#1}, \let\next=\hypersecrefs
 \fi\next#2.%
}

% \finalsecref<n><ETcode><m>\ET\ET.<n><ETcode><m>\ETs\ETs.
% or \finalsecref<n>\ET\ET.<n>\ETs\ETs.
\def\finalsecrefs#1\ET#2\ET#3.#4\ETs#5\ETs#6.%
{\ifx|#2#5|\hyperref{\let\*=\empty}{sec}{#1}{#1}% just 1 reference
 \else % <n>\ET<m> or <n>\ETs<m>
    \edef\next{\ifx|#5|#1\else#4\fi}% <n>
    \toks0={\let\*=\empty}%
    \edef\next{{\the\toks0}{sec}{\next}{\next}}%
    \expandafter\hyperref\next% link to sec.<n> with text <n>
    #3#6% one is empty, the other is \ET or \ETs, as appropriate
    \hyperref{\let\*=\empty}{sec}{#2#5}{#2#5}% similar trick, link to sec.<m>
 \fi
}

% we must change \ATP slightly, so that \X can recognise it and avoid a link
% Replaces:
%\def\ATP{\X\kern-.5em:Preprocessor directives\X}
\def\ATP{\X\ATP\kern-.5em:Preprocessor directives\X}

% now \X can be redefined; \X is used for module names
% Calls \hyperref<assignments><kind><number><active text>
% for module names not generated by \ATP, which have a unique number n
% with <assignments>={\let\*=\empty}, <kind>=sec, <number>=n, <active text>=n
% Replaces:
%\def\X#1:#2\X{\langle\,${#2\eightrm\enspace#1}$\,\rangle} % module name
\def\X#1:#2\X % module name
{\langle\,${#2\eightrm\enspace\ifx\ATP#1\else\hypersecrefs#1, .\fi}$\,\rangle}

% auxiliary to make underscores plain
{\catcode`_=12 \gdef\usplain{\def\_{_}}}

% prepare for modifying \Cident; its old meaning will be saved
\let\plainCident=\Cident

% replacement \Cident must work to get identifier together
% Calls \hyperref<assignments><kind><number><active text>
% with <assignments>=\usplain, <kind>=id, <number>=plain identifier
% and <active text>=identifier as formatted by \Cident
\def\Cident#1{\def\idstart{#1}\futurelet\next\testid}

% we must undo special care for \\
% Replaces:
%\def\\#1{\leavevmode\hbox\Cident{#1}} % robust version
\let\\=\Cident % cwebx only; a \hbox would prevent us from seeing "subscript"

% Code for analysing and reassembling identifiers
\def\testid
{\ifx_\next
   \def\next_##1%
    {\hyperref\usplain{id}{\idstart##1}{\plainCident\idstart_{##1}}}%
 \else \hyperref\usplain{id}\idstart{\plainCident\idstart}%
   \let\next=\empty
 \fi\next
}

% Alternative implementation of \testid that will be used in the index
\def\indextestid
{\ifx_\next
   \def\next_##1%
    {{\usplain\xdef\next{{id}{\idstart##1}}}%
     \expandafter\hyperdef\next{\plainCident\idstart_{##1}}%
    }%
 \else
   {\usplain\xdef\next{{id}{\idstart}}}%
   \expandafter\hyperdef\next{\plainCident\idstart}%
   \let\next=\empty
 \fi\next
}

% Redefine \inx by adding to its end '\let\testid=\indextestid'
% Replaces a rather long definition
{\let \inxentry=\relax % temporarily remove \outer-ness
 \toks0=\expandafter{\inx \let\testid=\indextestid}\xdef\inx{\the\toks0 }
}


% Link attached to section numbers n in each index entry
% Calls \hyperref<assignments><kind><number><active text>
% with <assignments>={\let\*=\empty\def\[#1]{#1}}, <kind>=sec, <number>=n
% and <active text>=n
% Replaces:
%\outer\def\inxentry#1#2, % index entry; #1 is `h' or `m' for horiz/math mode
% {\par\hangindent2em\noindent\if#1m$#2$\else#2\fi:\kern1em}
\outer\def\inxentry#1#2, #3.% #1 is `h' or `m' for horiz/math mode
 {\par\hangindent2em\noindent
  \if#1m$#2$\else#2\relax\fi % \relax avoids fatal \next=\fi in \indextestid
  :\kern1em\indexrefs#3, .%
 }

\def\indexrefs#1, #2.%
{\hyperref{\let\*=\empty\def\[##1]{##1}}{sec}{#1}{#1}%
 \ifx|#2|\let\next=\empty\else, \let\next=\indexrefs\fi\next#2.%
}

% And links to table of contents entries.
% Replaces:
%\def\contentsline#1#2#3#4% #1=depth, #2=title, #3=secno, #4=pageno
% {\line{\kern#1\indentunit \ignorespaces#2 \leaders\hbox to .5em{.\hfil}\hfil
%        \ #3\hbox to3em{\hss#4}%
%       }%
% }
\def\contentsline#1#2#3#4% #1=depth, #2=title, #3=secno, #4=pageno
 {\line{\kern#1\indentunit \hyperref{}{sec}{#3}{\ignorespaces#2}
        \leaders\hbox to .5em{.\hfil}\hfil
        \ \hyperref{}{sec}{#3}{#3}\hbox to3em{\hss\hyperref{}{sec}{#3}{#4}}%
       }%
 }
