% standard macros for WEB listings (in addition to PLAIN.TEX)
% pdfTeX adjustments provided by Han The Thanh <thanh@pdftex.org>.
% XeTeX adjustments provided by Khaled Hosny <khaledhosny@eglug.org>.
% HiTeX adjustments provided by Martin Ruckert <ruckert@cs.hm.edu>.
% macro integration \`a la CWEB by Andreas Scherer <andreas_tex@freenet.de>.
% This file is not copyrighted and can be used freely.
\ifx\renewenvironment\undefined\else\endinput\fi % LaTeX will use other macros
\xdef\fmtversion{\fmtversion+PWEBMAC5.1}

\parskip 0pt % no stretch between paragraphs
\parindent 1em % for paragraphs and for the first line of Pascal text

\font\eightrm=cmr8
\let\sc=\eightrm % for smallish caps (NOT a caps-and-small-caps font)
\let\mainfont=\tenrm
\font\titlefont=cmr7 scaled\magstep4 % title on the contents page
\font\ttitlefont=cmtt10 scaled\magstep2 % typewriter type in title
\font\tentex=cmtex10 % TeX extended character set (used in strings)
\fontdimen7\tentex=0pt % no double space after sentences

\def\\#1{\leavevmode\hbox{\it#1\/\kern.05em}} % italic type for identifiers
\def\|#1{\leavevmode\hbox{$#1$}} % one-letter identifiers look better this way
\def\&#1{\leavevmode\hbox{\bf
  \def\_{\kern.04em\vbox{\hrule width.3em height .6pt}\kern.08em}%
  #1\/\kern.05em}} % boldface type for reserved words
\def\.#1{\leavevmode\hbox{\tentex % typewriter type for strings
  \let\\=\BS % backslash in a string
  \let\'=\RQ % right quote in a string
  \let\`=\LQ % left quote in a string
  \let\{=\LB % left brace in a string
  \let\}=\RB % right brace in a string
  \let\~=\TL % tilde in a string
  \let\ =\SP % space in a string
  \let\_=\UL % underline in a string
  \let\&=\AM % ampersand in a string
  #1}}
\def\#{\hbox{\tt\char`\#}} % parameter sign
\def\${\hbox{\tt\char`\$}} % dollar sign
\def\%{\hbox{\tt\char`\%}} % percent sign
\def\^{\ifmmode\mathchar"222 \else\char`^ \fi} % pointer or hat
% circumflex accents can be obtained from \^^D instead of \^
\def\AT!{@} % at sign for control text

\chardef\AM=`\& % ampersand character in a string
\chardef\BS=`\\ % backslash in a string
\chardef\LB=`\{ % left brace in a string
\def\LQ{{\tt\char'22}} % left quote in a string
\chardef\RB=`\} % right brace in a string
\def\RQ{{\tt\char'23}} % right quote in a string
\def\SP{{\tt\char`\ }} % (visible) space in a string
\chardef\TL=`\~ % tilde in a string
\chardef\UL=`\_ % underline character in a string

\newbox\bak \setbox\bak=\hbox to -1em{} % backspace one em
\newbox\bakk\setbox\bakk=\hbox to -2em{} % backspace two ems

\newcount\ind % current indentation in ems
\def\1{\global\advance\ind by1\hangindent\ind em} % indent one more notch
\def\2{\global\advance\ind by-1} % indent one less notch
\def\3#1{\hfil\penalty#10\hfilneg} % optional break within a statement
\def\4{\copy\bak} % backspace one notch
\def\5{\hfil\penalty-1\hfilneg\kern2.5em\copy\bakk\ignorespaces}% optional break
\def\6{\ifmmode\else\par % forced break
  \hangindent\ind em\noindent\kern\ind em\copy\bakk\ignorespaces\fi}
\def\7{\Y\6} % forced break and a little extra space

\newskip\intersecskip \intersecskip=12pt minus 3pt % space between sections
\newtoks\gtitle % title of current major group
\let\yskip=\smallskip
\def\to{\mathrel{.\,.}} % double dot, used only in math mode
\def\note#1#2.{\Y\noindent{\hangindent2em\baselineskip10pt%
    \eightrm#1~\ifacrohint{\pdfnote#2.}\else#2\fi.\par}}

\newtoks\toksA \newtoks\toksB \newtoks\toksC \newtoks\toksD
\newtoks\toksE \newtoks\toksF \newtoks\usersanitizer
\newcount\countA \countA=0 \newcount\countB \countB=0
\newcount\countNOS \countNOS=0
\newif\iftokprocessed \newif\ifTnum \newif\ifinstr \newif\ifhavenum
{\def\\{\global\let\spacechar= }\\ }

% Here we decide the output format, depending on the TeX engine in use:
\input iftex.sty % TeX engine tests
\ifx\pdf+\pdftrue\fi % for plain TeX in combination with dvipdfm
% Uncomment the following line if you want PDF goodies to be the default
%\ifx\pdf-\else\pdftrue\fi
\ifxetex\pdftrue\fi % XeTeX produces PDF output
\ifpdf \def\pdflinkcolor{0 0 1} \fi % the RGB values for hyperlink color
\newif\ifacrohint \ifpdf\acrohinttrue\fi \ifhint\acrohinttrue\fi
\newif\ifpdflua \ifluatex\pdfluatrue\fi \ifpdftex\pdfluatrue\fi
\ifpdflua % pdfTeX and luaTeX produce PDF output if \pdfoutput>0 (default)
  \def\Black{\pdfliteral{0 g 0 G}}  % use rgb colors for direct PDF output too
  \def\Blue{\pdfliteral{\pdflinkcolor\space rg \pdflinkcolor\space RG}} \fi

\ifluatex % LuaTeX is almost like pdfTeX
\protected\def\pdfliteral   {\pdfextension literal}
\protected\def\pdfstartlink {\pdfextension startlink\space}
\protected\def\pdfendlink   {\pdfextension endlink}
\protected\def\pdfoutline   {\pdfextension outline\space}
\protected\def\pdfdest      {\pdfextension dest\space}
\protected\def\pdfcatalog   {\pdfextension catalog}
\chardef\pdfshellescape % excerpt from 'shellesc.sty'
  \directlua{tex.sprint((status.shell_escape or os.execute()) .. " ")}
\edef\pdfhorigin {\pdfvariable horigin}
\edef\pdfvorigin {\pdfvariable vorigin}
\let\pdfpagewidth=\pagewidth
\let\pdfpageheight=\pageheight
\let\pdfoutput=\outputmode
\fi % end of LuaTeX->pdfTeX redirection macros

\ifacrohint % Common macros for \ifpdf and \ifhint for \pdflink's
\def\pdfnote#1.{\setbox0=\hbox{\toksA={#1.}\toksB={}\maketoks}\the\toksA}
\def\firstsecno#1.{\setbox0=\hbox{\toksA={#1.}\toksB={}%
    \def\makenote{\addtokens\toksB{\the\toksC}\def\makenote{\let\space\empty
      \toksC={}\toksD={}}\makenote}\maketoks}}
\def\addtokens#1#2{\edef\addtoks{\noexpand#1={\the#1#2}}\addtoks}
\def\poptoks#1#2|ENDTOKS|{\let\first=#1\toksD={#1}\toksA={#2}%
  \ifcat\noexpand\first0\countB=`#1\else\countB=0\fi}
\def\maketoks{\expandafter\poptoks\the\toksA|ENDTOKS|%
  \let\next=\maketoks % continue processing by default
  \ifnum\countB>`9 \countB=0 \fi
  \ifnum\countB<`0
    \ifhavenum\makenote\fi
    \ifx\first.\let\next=\maketoksdone % fullstop
    \else\addtokens\toksB{\the\toksD}%
      \ifx\first,\addtokens\toksB{\space}\fi
    \fi
  \else\addtokens\toksC{\the\toksD}\global\havenumtrue
  \fi
  \next
}
\def\makenote{\addtokens\toksB
    {\noexpand\pdflink{\the\toksC}}\toksC={}\global\havenumfalse}
\def\maketoksdone{\edef\st{\global\noexpand\toksA={\the\toksB}}\st}
\fi % End of common pdf/hint macros

\ifpdf % The following are general pdf macros from 'cwebmac.tex 3.70'
\def\thewidth{\the\wd0 \space}
\def\theheight{\the\ht\strutbox\space}
\def\thedepth{\the\dp\strutbox\space}
\ifpdflua
  \ifx\pdfannotlink\undefined\let\pdfannotlink\pdfstartlink\fi% for pdfTeX 0.14
  \def\pdflink#1{\hbox{\pdfannotlink height \theheight depth \thedepth
    attr{/Border [0 0 0]} goto num #1 \Blue #1\Black\pdfendlink}}
\else\def\pdflink#1{\setbox0=\hbox{\special{pdf: bc [ \pdflinkcolor ]}{#1}%
    \special{pdf: ec}}\special{pdf: ann width \thewidth height \theheight
      depth \thedepth << /Type /Annot /Subtype /Link /Border [0 0 0]
      /A << /S /GoTo /D (\romannumeral#1) >> >>}\box0\relax}\fi
\def\sanitizecommand#1#2{\addtokens\usersanitizer
       {\noexpand\dosanitizecommand\noexpand#1{#2}}}
\def\dosanitizecommand#1#2{\ifx\nxt#1\addF{#2}\fi}
\ifhint\else\sanitizecommand\to{..}\fi % a trivial example; requires \addtokens

\catcode`\[=1 \catcode`\]=2 \catcode`\{=12 \catcode`\}=12
  \def\lbchar[{]  \def\rbchar[}]
\catcode`\[=12 \catcode`\]=12 \catcode`\{=1 \catcode`\}=2
\catcode`\~=12 \def\tildechar{~}  \catcode`\~=13
\catcode`\|=0 |catcode`|\=12 |def|bschar{\} |catcode`|\=0 \catcode`\|=12
\def\makeoutlinetoks{\toksF={}\Tnumfalse
  \afterassignment\makeolproctok\let\nxt= }
\def\makeolnexttok{\afterassignment\makeolproctok\let\nxt= }
\def\makeolgobbletok{\afterassignment\makeolnexttok\let\nxt= }
\def\makeolgobbletokk{\afterassignment\makeolgobbletok\let\nxt= }
\def\addF#1{\addtokens\toksF{#1}\tokprocessedtrue}
% now comes a routine to "sanitize" section names, for pdf outlines
\def\makeolproctok{\tokprocessedfalse
  \let\next\makeolnexttok % default
  \ifx\nxt\outlinedone\let\next\outlinedone
  \else\ifx{\nxt \else\ifx}\nxt \Tnumfalse \instrfalse % skip braces
  \else\ifx$\nxt % or a $ sign
  \else\ifx^\nxt \addF^\else\ifx_\nxt \addF_% sanitize ^ and _
  \else\ifx\nxt\spacechar \addF\space
  \else\if\noexpand\nxt\relax % we have a control sequence; is it one we know?
    \ifx\nxt~\addF\space
    \else\ifx\nxt\onespace\addF\space
    \else\the\usersanitizer
      \iftokprocessed\else\makeolproctokctl
      \iftokprocessed\else\makeolproctokmath % if not recognised, skip it
      \fi\fi
    \fi\fi
  \else  % we don't have a control sequence, it's an ordinary char
    \ifx(\nxt \addF{\string\(}\else\ifx)\nxt \addF{\string\)}%
    %\else\ifx[\nxt \addF{\string\[}\else\ifx]\nxt \addF{\string\]}%
    %\else\ifx/\nxt \addF{\string\/}% quote chars special to PDF with backslash
    \else\expandafter\makeolproctokchar\meaning\nxt
    \fi\fi%\fi\f\fi
  \fi\fi\fi\fi\fi \fi\fi\fi
  \next
}
\def\makeolproctokctl{%
  \ifx\nxt\PASCAL\addF{Pascal\space}\let\next\makeolgobbletok % \PASCAL/
  \else\ifx\nxt\UNIX\addF{UNIX}\let\next\makeolgobbletok % \UNIX/
  \else\ifx\nxt\TEX\addF{TeX}\let\next\makeolgobbletok % \TEX/
  \else\ifx\nxt\TeX\addF{TeX}\else\ifx\nxt\LaTeX\addF{LaTeX}%
  \else\ifx\nxt\CPLUSPLUS\addF{C++}\let\next\makeolgobbletok % \CPLUSPLUS/
  \else\ifx\nxt\CEE\addF{C}\else\ifx\nxt\Cee\addF{C}%
  \else\ifx\nxt\PB \let\next\makeolgobbletok \tokprocessedtrue % \PB{...}
  \else\ifx\nxt\9 \expandafter\nxt\relax \let\next\makeolgobbletokk % \9{#1}
  \else\ifx\nxt\.\tokprocessedtrue\instrtrue % \.{...}
      % skip \|
  \else\ifx\nxt\\\ifinstr\addF{\bschar\bschar}\else\tokprocessedtrue\fi
  \else\ifx\nxt\&\ifinstr\addF&\else\tokprocessedtrue\fi
  \else\ifx\nxt\~\ifTnum\addF{0}\else\addF\tildechar\fi % 077->\T{\~77}
  \else\ifx\nxt\_\ifTnum\addF{E}\else\addF_\fi % 0.1E5->\T{0.1\_5}
  \else\ifx\nxt\^\ifTnum\addF{0x}\else\addF^\fi % 0x77 -> \T{\^77}
  \else\ifx\nxt\$\ifTnum\tokprocessedtrue\else\addF$\fi %$% \T{77\$L}
  \else\ifx\nxt\{\addF\lbchar\else\ifx\nxt\}\addF\rbchar
  \else\ifx\nxt\ \addF\space\else\ifx\nxt\#\addF{\string\#}%
  \fi\fi\fi\fi\fi \fi\fi\fi\fi\fi
  \fi\fi\fi\fi\fi \fi\fi\fi\fi\fi
  \fi
}
\def\makeolproctokmath{%
  \ifx\nxt\alpha\addF{alpha}\else\ifx\nxt\beta\addF{beta}%
  \else\ifx\nxt\gamma\addF{gamma}\else\ifx\nxt\delta\addF{delta}%
  \else\ifx\nxt\theta\addF{theta}\else\ifx\nxt\psqrt\addF{root\space}%
  \else\ifx\nxt\H\addF{"}\else\ifx\nxt\O\addF{'}\else\ifx\nxt\v\addF{|}%
  \else\ifx\nxt\prime\addF{'}\else\ifx\nxt\K\addF{:=}\else\ifx\nxt\S\addF{==}%
  \else\ifx\nxt\dots\addF{...\space}\else\ifx\nxt\<\addF{<}%
  \fi\fi\fi\fi\fi \fi\fi\fi\fi\fi
  \fi\fi\fi\fi
}
\def\makeolproctokchar#1 #2 #3{\addF{#3}}
\def\outlinedone{\edef\outlinest{\global\noexpand\toksE={\the\toksF}}%
  \outlinest\let\outlinedone=\relax\outlinedone}
\fi % End of general pdf macros

\ifhint
% The following uses HINT primitives to implement:
% \HINThome to mark the position of the home page
% \HINTlabel to attach a label to the top of a section
% \HINTlink to link a section number to the section label
% \HINTcontents to produce an 'outline' item
% These are the top level HINT macros that are used in this file
% No HINT primitives are used directly.
\def\HINThome{\HINTdest name {HINT.home}%
  \HINToutline goto name {HINT.home} depth 1 {Table of Contents}}
\def\HINTlabel{{\let\*=\empty\HINTdest num \modstar top}}
\def\HINTlink#1{\HINTstartlink goto num #1 {#1}\HINTendlink}
\def\HINTcontents#1#2{\HINToutline goto num #2 depth 1 {#1}}
\let\pdflink=\HINTlink
\fi % End of HINT macros

\def\pdfURL#1#2{\ifpdf \ifpdflua\pdfannotlink height \theheight depth \thedepth
    attr {/Border [0 0 0]} user { /Type /Annot /Subtype /Link /A
      << /S /URI /URI (#2) >> }\Blue #1\Black \pdfendlink
  \else {\setbox0=\hbox{\special{pdf: bc [ \pdflinkcolor ]}{#1}%
    \special{pdf: ec}}\special{pdf: ann width \thewidth height \theheight
      depth \thedepth << /Border [0 0 0] /Type /Annot /Subtype /Link /A
      << /S /URI /URI (#2) >> >>}\box0\relax}\fi
  \else #1 ({\tt#2})\fi}
{\catcode`\~=12 \gdef\TILDE/{~}} % ~ in a URL
{\catcode`\_=12 \gdef\UNDER/{_}} % _ in a URL

\def\lapstar{\rlap{*}}
\def\stsec{\rightskip=0pt % get out of Pascal mode (cf. \P)
  \sfcode`;=1500 \pretolerance 200 \hyphenpenalty 50 \exhyphenpenalty 50
  \ifhint\HINTlabel\fi% Start page before section
  \noindent{\let\*=\lapstar\bf\modstar.\quad}%
  \ifpdf \smash{\raise\baselineskip\hbox to0pt{\let\*=\empty
    \ifpdflua \pdfdest num \modstar fith%
    \else \special{pdf: dest (\romannumeral\modstar)
      [ @thispage /FitH @ypos ]}\fi}}\fi}
\let\startsection=\stsec
\def\defin#1{\global\advance\ind by 2 \1\&{#1 }} % begin `define' or `format'
\def\A{\note{See also section}} % xref for doubly defined section name
\def\As{\note{See also sections}} % xref for multiply defined section name
\def\B{\mathopen{\.{@\{}}} % begin controlled comment
\def\C#1{\ifmmode\gdef\XX{\null$\null}\else\gdef\XX{}\fi %$% Pascal comments
  \XX\hfil\penalty-1\hfilneg\quad$\{\,$#1$\,\}$\XX}
\def\D{\defin{define}} % macro definition
\def\E{\cdot10^} % exponent in floating point constant
\def\ET{ and~} % conjunction between two section numbers
\def\ETs{, and~} % conjunction between the last two of several section numbers
\def\F{\defin{format}} % format definition
\let\G=\ge % greater than or equal sign
\def\H#1{\hbox{\rm\char"7D\tt#1}} % hexadecimal constant
\let\I=\ne % unequal sign
\def\J{\.{@\&}} % TANGLE's join operation
\let\K=\gets % left arrow
\let\L=\le % less than or equal sign
\outer\def\M#1.{\MN#1.\ifon\vfil\penalty-100\vfilneg % beginning of section
  \vskip\intersecskip\startsection\ignorespaces}
\outer\def\N#1.#2.{% beginning of starred section
  \ifpdf{\makeoutlinetoks#2\outlinedone}\fi
  \gtitle={#2}\MN#1.\vfil\eject % define running headline
  \message{*\modno} % progress report
  \def\stripprefix##1>{}\def\gtitletoks{#2}%
  \edef\gtitletoks{\expandafter\stripprefix\meaning\gtitletoks}%
  \edef\next{\write\cont{\Z{\gtitletoks}{\modno}% write to contents file
   {\noexpand\the\pageno}{\the\toksE}}}\next % \Z{title}{sec}{page}{ss}
  \ifpdf \ifpdflua\relax
  \else \special{pdf: outline 0 << /Title (\the\toksE) /Dest
    [ @thispage /FitH @ypos ] >>}\fi\fi
  \ifon\startsection{\bf\ignorespaces#2.\quad}\ignorespaces}
\def\MN#1.{\par % common code for \M, \N
  {\xdef\modstar{#1}\let\*=\empty\xdef\modno{#1}}% remove \* from section name
  \ifx\modno\modstar \onmaybe \else\ontrue \fi
  \mark{{{\tensy x}\modno}{\the\gtitle}}}
% each \mark is {section reference or null}{group title}
\def\O#1{\hbox{\rm\char'23\kern-.2em\it#1\/\kern.05em}} % octal constant
\def\P{\rightskip=0pt plus 100pt minus 10pt % go into Pascal mode
  \sfcode`;=3000
  \pretolerance 10000
  \hyphenpenalty 1000 % so strings can be broken (discretionary \ is inserted)
  \exhyphenpenalty 10000
  \global\ind=2 \1\ \unskip}
\let\R=\lnot % logical not
\let\S=\equiv % equivalence sign
\def\T{\mathclose{\.{@\}}}} % terminate controlled comment
\def\U{\note{This code is used in section}} % xref for use of a section
\def\Us{\note{This code is used in sections}} % xref for uses of a section
\let\V=\lor % logical or
\let\W=\land % logical and
\def\X#1:#2\X{\ifmmode\gdef\XX{\null$\null}\else\gdef\XX{}\fi %$% section name
  \XX$\langle\,$#2{\eightrm\kern.5em
    \ifacrohint{\pdfnote#1.}\else#1\fi}$\,\rangle$\XX}
\def\Y{\par\yskip}
\let\Z=\let % now you can \send the control sequence \Z
\def\){\hbox{\.{@\$}}} % sign for string pool check sum
\def\]{\hbox{\.{@\\}}} % sign for forced line break
\def\=#1{\kern2pt\hbox{\vrule\vtop{\vbox{\hrule
        \hbox{\strut\kern2pt\.{#1}\kern2pt}}
      \hrule}\vrule}\kern2pt} % verbatim string
\let\~=\ignorespaces
\let\*=*

\def\onmaybe{\let\ifon=\maybe} \let\maybe=\iftrue
\newif\ifon \newif\iftitle \newif\ifpagesaved
\def\lheader{\mainfont\the\pageno\eightrm\qquad\grouptitle
  \hfill\title\qquad\mainfont\topsecno} % top line on left-hand pages
\def\rheader{\mainfont\topsecno\eightrm\qquad\title\hfill
  \grouptitle\qquad\mainfont\the\pageno} % top line on right-hand pages
\def\grouptitle{\let\i=I\let\j=J\uppercase\expandafter{\expandafter
                          \taketwo\topmark}}
\def\topsecno{\expandafter\takeone\topmark}
\def\takeone#1#2{#1}
\def\taketwo#1#2{#2}
\def\nullsec{\eightrm\kern-2em} % the \kern-2em cancels \qquad in headers

%\let\page=\pagebody \raggedbottom % cweb 3.2 uses this instead of next line
\def\page{\box255 }\normalbottom % faster, but loses plain TeX footnotes
\def\normaloutput#1#2#3{\ifodd\pageno\hoffset=\pageshift\fi
  \shipout\vbox{
    \vbox to\fullpageheight{
      \iftitle\global\titlefalse
      \else\hbox to\pagewidth{\vbox to10pt{}\ifodd\pageno #3\else#2\fi}\fi
      \vfill#1}} % parameter #1 is the page itself
  \global\advance\pageno by1}

\gtitle={{\tentex WEB} output} % this running head is reset by starred sections
\mark{\noexpand\nullsec{\the\gtitle}}
\def\title{\expandafter\uppercase\expandafter{\jobname}}
\def\topofcontents{\centerline{\titlefont\title}\vskip.7in
  \vfill} % this material will start the table of contents page
\def\startpdf{\ifpdf\def\pagemode{/PageMode /UseOutlines}
  \ifnum\contentspagenumber=0 \advance \pageno by -1
    \def\pagelabels{/PageLabels << /Nums [ 0 << /S/D/St 1 >>
      \the\pageno << /P(Contents) >> ] >>}
  \else \twodigits=\pageno \pageno=\contentspagenumber
    \advance \pageno by 1 \advance \twodigits by -\pageno
    \def\pagelabels{/PageLabels << /Nums [ 0 << /S/D/St \the\pageno >>
      \the\twodigits << /S/D/St \contentspagenumber >> ] >>} \fi
  \ifpdflua\pdfcatalog{\pagemode\space\pagelabels}
  \else \special{pdf: docview << \pagemode\space\pagelabels >>}\fi\fi}
\def\botofcontents{\vfill
  \centerline{\covernote}} % this material will end the table of contents page
\def\covernote{}
\def\contentspagenumber{0} % default page number for table of contents
\newdimen\pagewidth \pagewidth=6.5in % the width of each page
\newdimen\pageheight \pageheight=8.7in % the height of each page
\newdimen\fullpageheight \fullpageheight=9in % page height including headlines
\newdimen\pageshift \pageshift=\hoffset
   % shift righthand pages wrt lefthand ones
\def\magnify#1{\mag=#1\pagewidth=6.5truein\pageheight=8.7truein
  \fullpageheight=9truein\setpage}
\def\setpage{\hsize\pagewidth\vsize\pageheight} % use after changing page size
\def\contentsfile{CONTENTS} % file that gets table of contents info
\def\readcontents{\input \contentsfile}

\newwrite\cont
\output{\setbox0=\page % the first page is garbage
  \openout\cont=\contentsfile
       \write\cont{\catcode `\noexpand\@=11\relax}   % \makeatletter
  \global\output{\normaloutput\page\lheader\rheader}}
\setpage
\vbox to \vsize{} % the first \topmark won't be null

\def\ch{\note{The following sections were changed by the change file:}
  \let\*=\relax}
\newbox\sbox % saved box preceding the index
\newbox\lbox % lefthand column in the index
\def\inx{\par\vskip6pt plus 1fil % we are beginning the index
  \def\page{\box255 } \normalbottom
  \write\cont{} % ensure that the contents file isn't empty
       \write\cont{\catcode `\noexpand\@=12\relax}   % \makeatother
  \closeout\cont % the contents information has been fully gathered
  \output{\ifpagesaved\normaloutput{\box\sbox}\lheader\rheader\fi
    \global\setbox\sbox=\page \global\pagesavedtrue \mark{\topmark}}
  \pagesavedfalse \eject % eject the page-so-far and predecessors
  \setbox\sbox\vbox{\unvbox\sbox} % take it out of its box
  \vsize=\pageheight \advance\vsize by -\ht\sbox % the remaining height
  \hsize=.5\pagewidth \advance\hsize by -10pt
    % column width for the index (20pt between cols)
  \ifhint\else
  \parfillskip 0pt plus .6\hsize % try to avoid almost empty lines
  \fi
  \def\lr{L} % this tells whether the left or right column is next
  \output{\if L\lr\global\setbox\lbox=\page \gdef\lr{R}
    \else\normaloutput{\vbox to\pageheight{\box\sbox\vss
        \hbox to\pagewidth{\box\lbox\hfil\page}}}\lheader\rheader
    \global\vsize\pageheight\gdef\lr{L}\global\pagesavedfalse\fi}
  \message{Index:}
  \parskip 0pt plus .5pt
  \outer\def\:##1, ##2.{\par\hangindent2em\noindent##1:\kern1em
    \scan##2!.} % index entry
  \let\ttentry=\. \def\.##1{\ttentry{##1\kern.2em}} % give \tt a little room
  \def\[##1]{$\underline{\scan##1!}$\scan} % underlined index item
  \ifacrohint\def\digits{\pdflink{\the\countA}\scan}
  \else\def\digits{{\the\countA}\scan}\fi
  \def\scan##1{\begingroup
    \ifx!##1% exit on exclamation point
    \else\ifx,##1,\space\aftergroup\scan % insert ,\space}\scan...
    \else\ifx\[##1\aftergroup##1% insert }\[...
    \else\ifx\*##1\aftergroup\lapstar\aftergroup\scan % insert }\lapstar\scan...
    \else\ifnum`##1>`9##1\aftergroup\scan % insert #1}\scan...
    \else\ifnum`##1<`0##1\aftergroup\scan % insert #1}\scan...
    \else
      \afterassignment\digits \aftergroup\countA
      \aftergroup##1% insert }\countA=#1\digits...
    \fi\fi\fi\fi\fi \fi
    \endgroup}
  \rm \rightskip0pt plus 2.5em \tolerance 10000
  \hyphenpenalty 10000 \parindent0pt}
\def\fin{\par\vfill\eject % this is done when we are ending the index
  \ifpagesaved\null\vfill\eject\fi % output a null index column
  \if L\lr\else\null\vfill\eject\fi % finish the current page
  \ifpdflua \makebookmarks % added in Version 4.5
    \ifnum\pdfshellescape=1 {\let\\=\BS % count the @<named sections@>
      \global\countNOS=\input{|grep -c -e '\\:\\X' \jobname.tex}} \fi\fi
  \parfillskip 0pt plus 1fil
  \def\grouptitle{NAMES OF THE SECTIONS}
  \let\topsecno=\nullsec
  \message{Section names:}
  \output={\normaloutput\page\lheader\rheader}
  \setpage
  \def\note##1##2.{\quad{\eightrm##1~\ifacrohint{\pdfnote##2.}\else{##2}\fi.}}
  \linepenalty=10 % try to conserve lines
  \def\U{\note{Used in section}} % crossref for use of a section
  \def\Us{\note{Used in sections}} % crossref for uses of a section
  \def\:{\par\hangindent 2em}\let\*=*\let\.=\ttentry
  \ifpdf \def\outsecname{Names of the sections} \let\Xpdf\X
  \ifpdflua \pdfdest name {NOS} fith
    \ifnum \countNOS>0 \xdef\modno{\the\countNOS} \fi
    \pdfoutline goto name {NOS} count -\modno {\outsecname}
  \else \special{pdf: outline -1 << /Title (\outsecname)
      /Dest [ @thispage /FitH @ypos ] >>}\fi
  \def\X##1:##2\X{\Xpdf##1:##2\X \firstsecno##1.%
    {\makeoutlinetoks##2\outlinedone}%
    \ifpdflua \pdfoutline goto num \the\toksA \expandafter{\the\toksE}
    \else \special{pdf: outline 0 << /Title (\the\toksE)
        /A << /S /GoTo /D (\romannumeral\the\toksA) >> >>}\fi}
  \fi}
\def\makebookmarks{\def\?##1]{[##1]}%
  \let\Z=\writebookmarkline \readcontents\relax}
\def\expnumber#1{\expandafter\ifx\csname#1\endcsname\relax 0%
  \else \csname#1\endcsname \fi} % Petr Olsak's macros from texinfo.tex
\def\advancenumber#1{\countA=\expnumber{#1}\relax \advance\countA by1
  \expandafter\xdef\csname#1\endcsname{\the\countA}}
\def\writebookmarkline#1#2#3#4{{%
  \let\(=\let \let\)=\let \let\[=\let \let\]=\let \let\/=\let
  \pdfoutline goto num #2{#4}}}
\def\con{\par\vfill\eject % finish the section names
% \ifodd\pageno\else\titletrue\null\vfill\eject\fi % for duplex printers
  \rightskip 0pt \hyphenpenalty 50 \tolerance 200
  \setpage \output={\normaloutput\page\lheader\rheader}
  \startpdf \titletrue % prepare to output the table of contents
  \pageno=\contentspagenumber
  \def\grouptitle{TABLE OF CONTENTS}
  \message{Table of contents:}
  \ifhint\HINThome\fi% Mark the Table of contents as home page
  \topofcontents
  \line{\hfil Section\ifhint\else\hbox to3em{\hss Page}\fi}% No Page in HINT
  \let\Z=\contentsline
  \readcontents\relax % read the contents info
  \botofcontents \end} % print the contents page(s) and terminate
\def\contentsline#1#2#3#4{%
    \line{\ignorespaces#1
      \rm\leaders\hbox to .5em{.\hfil}\hfil
      \ \ifhint
          \HINTlink{#2}% No page numbers in HINT
          \HINTcontents{#1}{#2}%
        \else\ifpdf\pdflink{#2}\else#2\fi
          \hbox to3em{\hss#3}\fi}}
\def\today{\ifcase\month\or
  January\or February\or March\or April\or May\or June\or
  July\or August\or September\or October\or November\or December\fi
  \space\number\day, \number\year}
\newcount\twodigits
\def\hours{\twodigits=\time \divide\twodigits by 60 \printtwodigits
  \multiply\twodigits by-60 \advance\twodigits by\time :\printtwodigits}
\def\gobbleone1{}
\def\printtwodigits{\advance\twodigits100
  \expandafter\gobbleone\number\twodigits
  \advance\twodigits-100 }
\def\datethis{\def\startsection{\leftline{\sc\today\ at \hours}\bigskip
  \let\startsection=\stsec\stsec}}
  % say `\datethis' in limbo, to get your listing timestamped before section 1
\def\datecontentspage{% say this in limbo to get the toc page timestamped
  \def\botofcontents{\vfill
   \centerline{\covernote}
   \bigskip
   \leftline{\sc\today\ at \hours}}} % timestamps the contents page
