%%
%% This is file `tensile.sty',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% tensile.dtx  (with options: `package')
%% ----------------------------------------------------------------
%% tensile --- Literate programming.
%% E-mail: taylor@metasyntax.net
%% Released under the LaTeX Project Public License v1.3c or later
%% See http://www.latex-project.org/lppl.txt
%% ----------------------------------------------------------------
%% 
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{tensile}
\RequirePackage{kvoptions}
\SetupKeyvalOptions{family=tnsl,prefix=tnslOpt@}
\DeclareStringOption[8pt]{codeSize}
\DeclareStringOption[10pt]{codeSkip}
\DeclareStringOption[8pt]{unitNameSize}
\DeclareStringOption[10pt]{unitNameSkip}
\DeclareStringOption[1pc]{codeMargin}
\DeclareStringOption[1pc]{tagMargin}
\ProcessKeyvalOptions{tnsl}
{\obeyspaces\AtBeginDocument{\global\let =\ }}

\def\tnslMarginTag#1{\leavevmode\kern-\codemargin\tnsl@marginTag{#1}\kern\codemargin}
\def\tnsl@marginTag#1{\llap{#1\kern\tnsl@tagMarginGlue}}
\newdimen\tnsl@tagMarginGlue
\tnsl@tagMarginGlue=\tnslOpt@tagMargin
\newdimen\codemargin
\codemargin=\tnslOpt@codeMargin
\setbox0=\hbox{\tt m}
\newdimen\codewidth
\codewidth=92\wd0
\advance\codewidth by \codemargin
\newdimen\tnsl@defSpace
\tnsl@defSpace=\codewidth
\advance\tnsl@defSpace by -\textwidth\relax
\advance\tnsl@defSpace by 0\codemargin % why was this in the original?
\newskip\tnsl@commentarySkip
\tnsl@commentarySkip=4pt plus 1pt minus 1pt
\chardef\other=12
\def\tnsl@codeInit{%
  \chardef\\=`\\
  \chardef\{=`\{
  \chardef\}=`\}
  \catcode`\$=\other
  \catcode`\&=\other
  \catcode`\#=\other
  \catcode`\%=\other
  \catcode`\~=\other
  \catcode`\_=\other
  \catcode`\^=\other
  \ttfamily
  \fontsize{\tnslOpt@codeSize}{\tnslOpt@codeSkip}\selectfont
  \obeylines\frenchspacing\obeyspaces
}
\def\tnsl@unitNameInit{%
  \catcode`\$=3
  \catcode`\&=4
  \catcode`\#=6
  \catcode`\%=14
  \catcode`\~=13
  \catcode`\_=8
  \catcode`\^=7
  \catcode`\ =10
  \catcode`\^^M=5
  \let\{\lbrace
  \let\}\rbrace
  \rmfamily\itshape
  \fontsize{\tnslOpt@unitNameSize}{\tnslOpt@unitNameSkip}\selectfont
}
\def\tnslStartUnitName{\begingroup\mbox\bgroup%
  \tnsl@unitNameInit%
  \ensuremath{\langle}}
\def\tnslEndUnitName{\ensuremath{\rangle}\egroup\endgroup}
\def\tnslBeginUnitDef{\leavevmode\kern-\codemargin\tnslStartUnitName}
\def\tnslEndUnitDef{%
  \tnslEndUnitName%
  \ensuremath{\mathord{\equiv}}%
  \nobreak\hfill\nobreak}
\def\tnslEndUnitDefPlus{%
  \tnslEndUnitName%
  \begingroup%
  \fontsize{\tnslOpt@unitNameSize}{\tnslOpt@unitNameSkip}\selectfont
  \ensuremath{\mathord{+}\mathord{\equiv}}%
  \endgroup
  \nobreak\hfill\nobreak}
\newskip\tnsl@codeTopSep
\tnsl@codeTopSep=4pt plus 1pt minus 1pt
\def\tnsl@makeOther#1{\catcode`#1=12 }

\raggedbottom
\def\tnslBeginCode#1{%
  \begingroup % ends in \tnslEndCode
  \topsep \tnsl@codeTopSep
  \@beginparpenalty \@highpenalty
  \@endparpenalty -\@highpenalty
  \trivlist\item[]
    \leftskip\@totalleftmargin
      \advance\leftskip\codemargin
    \rightskip\hsize
      \advance\rightskip -\codewidth
    \linewidth\codewidth
    \parskip\z@
    \parindent\z@
    \parfillskip\@flushglue
  \@@par
  \def\par{\leavevmode\null \@@par \penalty\@highpenalty}
  \@noligs\let\do=\tnsl@makeOther \verbatim@nolig@list \do@noligs\`
  \tnsl@codeInit
}
\def\tnslEndCode{\endtrivlist\endgroup}
\def\tnslBeginDoc#1{\ifvmode\noindent\fi}
\let\tnslEndDoc=\relax
\def\tnsl@docParSkip#1{\vskip0pt plus#1\penalty-200\vskip0pt plus -#1}
\def\tnslDocPar{\tnsl@docParSkip{12pt}}%
\newcommand{\tnsl@prevDef}[1]{%
  \mbox{$\mathord{\triangleleft}\,\mathord{\mbox{\subpageref{#1}}}$}%
}
\newcommand{\tnsl@nextDef}[1]{%
  \mbox{$\mathord{\mbox{\subpageref{#1}}}\,\mathord{\triangleright}$}%
}
\newcommand{\tnsl@prevNextDefs}[2]{%
  \begingroup%
  \rmfamily%
  \fontsize{\tnslOpt@codeSize}{\tnslOpt@codeSkip}\selectfont%
  \ifx\relax#1\else ~~\tnsl@prevDef{#1}\fi%
  \ifx\relax#2\else ~~\tnsl@nextDef{#2}\fi%
  \endgroup}
\newcommand{\tnsl@backRef}[1]{
  \begingroup%
  \rmfamily%
  \upshape%
  \fontsize{\tnslOpt@codeSize}{\tnslOpt@codeSkip}\selectfont%
  (\@pagenumsl{#1})%
  \endgroup}
\def\tnsl@commentary#1{\@@par\penalty\@highpenalty
    \if@firstcommentary
      \vskip\tnsl@commentarySkip\penalty\@highpenalty\@firstcommentaryfalse
    \fi%
    \hspace{-\codemargin}{%
        \rightskip=0pt plus1in
        \interlinepenalty\@highpenalty
        \let\\\relax\footnotesize\rm #1\@@par\penalty\@highpenalty}}

\newif\if@firstcommentary
\@firstcommentarytrue
\def\tnsl@alsoDefined#1{%
  \tnsl@commentary{Definition continues in \@pagesl{#1}.}%
}
\def\tnsl@used#1{%
  \tnsl@commentary{Code is used in \@pagesl{#1}.}%
}
\def\tnsl@notUsed#1{%
  \tnsl@commentary{Root chunk.}%
}
\let\tnslAlsoDefined\tnsl@alsoDefined
\let\tnslUsed\@gobble
\let\tnslNotUsed\@gobble
\let\tnslPrevNextDefs\tnsl@prevNextDefs
\let\tnslBackRef\tnsl@backRef
\newcommand{\tnslBeginDefLine}{\nobreak\hfill\nobreak}
\newcommand{\tnslEndDefLine}{\nobreak\hskip \tnsl@defSpace minus\tnsl@defSpace\nobreak}
\newcount\@tnslFirstPage
\newcount\@tnslLastPage
\newcount\@tnslFirstSubPage
\newcount\@tnslLastSubPage
\newcount\@tmpLastPage
\newcount\@tnslPageCount
\def\tnsl@stripLastTwoDigits#1{
  \divide #1 by 100
  \multiply #1 by 100
}
\def\increment#1{\advance #1 by \@ne}
\def\decrement#1{\advance #1 by \m@ne}
\def\tnsl@firstPage#1#2#3{%
  \@tnslFirstSubPage=#1%
  \@tnslFirstPage=#2%
  \def\@nwloxreftag{#3}%
  \increment\@tnslPageCount%
  \@tnslLastPage=\@tnslFirstPage%
  \increment\@tnslLastPage%
}
\def\tnsl@nextPage#1#2#3{%
  \ifnum\@tnslLastPage=#2
    \increment\@tnslLastPage%
    \increment\@tnslPageCount%
    \@tnslLastSubPage=#1%
    \def\@nwhixreftag{#3}%
  \else
    \ifnum#2<\@tnslFirstPage
      % page comes before tnslFirstPage
      \decrement\@tnslLastPage%
      \ifnum\@tnslLastPage=\@tnslFirstPage
        \edef\@tempa{\noexpand\noexpand\noexpand\\%
          {{\tnsl@chunkTag{\number\@tnslFirstSubPage}{\number\@tnslFirstPage}}%
          {\@nwloxreftag}}}%
      \else
        \@tmpLastPage=\@tnslLastPage
        \decrement\@tmpLastPage
        \ifnum\@tmpLastPage=\@tnslFirstPage%
          % tnslLastPage = tnslFirstPage + 1
          \edef\@tempa{%
            \noexpand\noexpand\noexpand\\%
            {{\tnsl@chunkTag{\number\@tnslFirstSubPage}{\number\@tnslFirstPage}}%
              {\@nwloxreftag}}%
            \noexpand\noexpand\noexpand\\%
            {{\tnsl@chunkTag{\number\@tnslLastSubPage}{\number\@tnslLastPage}}
              {\@nwhixreftag}}}%
        \else%
          \edef\@tempa{\noexpand\noexpand\noexpand\\%
            {{\number\@tnslFirstPage--\number\@tnslLastPage}{}}}%
        \fi%
      \fi%
      \edef\@tempa{\noexpand\tnsl@listAppend\noexpand\tnsl@pages{\@tempa}}%
      \@tempa\tnsl@firstPage{#1}{#2}{#3}%
    \else
      \ifnum#2>\@tnslLastPage
        % page comes after tnslLastPage
        \decrement\@tnslLastPage%
        \ifnum\@tnslLastPage=\@tnslFirstPage
          \edef\@tempa{\noexpand\noexpand\noexpand\\%
            {{\tnsl@chunkTag{\number\@tnslFirstSubPage}{\number\@tnslFirstPage}}%
              {\@nwloxreftag}}}%
        \else
          \@tmpLastPage=\@tnslLastPage
          \decrement\@tmpLastPage
          \ifnum\@tmpLastPage=\@tnslFirstPage
            % tnslLastPage = tnslFirstPage + 1
            \edef\@tempa{%
              \noexpand\noexpand\noexpand\\%
              {{\tnsl@chunkTag{\number\@tnslFirstSubPage}{\number\@tnslFirstPage}}%
                {\@nwloxreftag}}%
              \noexpand\noexpand\noexpand\\%
              {{\tnsl@chunkTag{\number\@tnslLastSubPage}{\number\@tnslLastPage}}
                {\@nwhixreftag}}}%
          \else
            \edef\@tempa{\noexpand\noexpand\noexpand\\%
              {{\number\@tnslFirstPage--\number\@tnslLastPage}{}}}%
          \fi%
        \fi%
        \edef\@tempa{\noexpand\tnsl@listAppend\noexpand\tnsl@pages{\@tempa}}%
        \@tempa\tnsl@firstPage{#1}{#2}{#3}%
      \else%
        \@tnslFirstSubPage=0%
        \@tnslLastSubPage=0%
      \fi%
    \fi%
  \fi%
}

\def\tnsl@firstPageLabel#1{% label
  \@ifundefined{r@#1}{\@warning{Reference `#1' on page \thepage \space undefined}%
                      \tnsl@listAppend\tnsl@pages{\\{\bf ??}}}{%
    \edef\@tempa{\noexpand\tnsl@firstPage\subpagepair{#1}{#1}}\@tempa}}

\def\tnsl@nextPageLabel#1{% label
  \@ifundefined{r@#1}{\@warning{Reference `#1' on page \thepage \space undefined}%
                      \tnsl@listAppend\tnsl@pages{\\{\bf ??}}}{%
    \edef\@tempa{\noexpand\tnsl@nextPage\subpagepair{#1}{#1}}\@tempa}}

\def\@pagesl#1{%  list of labels
  \gdef\tnsl@pages{}%
  \@tnslPageCount=0
  \def\\##1{\tnsl@firstPageLabel{##1}\let\\=\tnsl@nextPageLabel}#1%
  \decrement \@tnslLastPage
  \ifnum\@tnslLastPage=\@tnslFirstPage
    \edef\@tempa{%
      \noexpand\noexpand\noexpand\\%
      {{\tnsl@chunkTag{\number\@tnslFirstSubPage}{\number\@tnslFirstPage}}%
        {\@nwloxreftag}}}%
  \else
    \@tmpLastPage=\@tnslLastPage
    \decrement\@tmpLastPage
    \ifnum\@tmpLastPage=\@tnslFirstPage
      % tnslLastPage = tnslFirstPage + 1
      \edef\@tempa{%
        \noexpand\noexpand\noexpand\\%
        {{\tnsl@chunkTag{\number\@tnslFirstSubPage}{\number\@tnslFirstPage}}%
          {\@nwloxreftag}}%
        \noexpand\noexpand\noexpand\\%
        {{\tnsl@chunkTag{\number\@tnslLastSubPage}{\number\@tnslLastPage}}%
          {\@nwhixreftag}}}%
    \else
      \edef\@tempa{\noexpand\noexpand\noexpand\\%
        {{\number\@tnslFirstPage--\number\@tnslLastPage}{}}}
    \fi
  \fi%
  \edef\@tempa{\noexpand\tnsl@listAppend\noexpand\tnsl@pages{\@tempa}}%
  \@tempa%
  \def\\##1{\@nwhyperpagenum##1}%
  \ifnum\@tnslPageCount=1 chunk \else chunks\fi~\commafy{\tnsl@pages}%
}

\def\@nwhyperpagenum#1#2{\tnsl@hyperLink{#2}{#1}}

\def\@pagenumsl#1{%
  \gdef\tnsl@pages{}%
  \@tnslPageCount=0
  \def\\##1{\tnsl@firstPageLabel{##1}\let\\=\tnsl@nextPageLabel}#1%
  \decrement\@tnslLastPage
  \ifnum\@tnslLastPage=\@tnslFirstPage
    \edef\@tempa{%
      \noexpand\noexpand\noexpand\\%
      {{\tnsl@chunkTag{\number\@tnslFirstSubPage}{\number\@tnslFirstPage}}%
        {\@nwloxreftag}}}%
  \else
    \@tmpLastPage=\@tnslLastPage
    \decrement\@tmpLastPage
    \ifnum\@tmpLastPage=\@tnslFirstPage
      % tnslFollowPage = tnslFirstPage + 1
      \edef\@tempa{%
        \noexpand\noexpand\noexpand\\%
        {{\tnsl@chunkTag{\number\@tnslFirstSubPage}{\number\@tnslFirstPage}}%
          {\@nwloxreftag}}%
        \noexpand\noexpand\noexpand\\%
        {{\tnsl@chunkTag{\number\@tnslLastSubPage}{\number\@tnslLastPage}}
          {\@nwhixreftag}}}%
    \else
      \edef\@tempa{%
        \noexpand\noexpand\noexpand\\%
        {{\number\@tnslFirstPage--\number\@tnslLastPage}{}}}%
    \fi%
  \fi%
  \edef\@tempa{\noexpand\tnsl@listAppend\noexpand\tnsl@pages{\@tempa}}%
  \@tempa%
  \def\\##1{\@nwhyperpagenum##1\let\\=\@nwpagenumslrest}%
  \tnsl@pages%
}

\def\@nwpagenumslrest#1{~\@nwhyperpagenum#1}

\newcommand\tnsl@genericRef[2]{% what to do, name of ref
  \expandafter\tnsl@genericRefPrime\csname r@#2\endcsname#1{#2}}
\newcommand\tnsl@genericRefPrime[3]{% control sequence, what to do, name
  \ifx#1\relax
    \ref{#3}% trigger the standard `undefined ref' mechanisms
  \else
    \expandafter#2#1.\\%
  \fi}
\newcommand{\subpageref}[1]{%
  \begingroup%
  \rmfamily%
  \upshape%
  \tnsl@hyperLink{#1}{\tnsl@genericRef\@subpageref{#1}}%
  \endgroup%
}
\def\@subpageref#1#2#3\\{%
  \@ifundefined{2on#2}{#2}{\tnsl@chunkTag{#1}{#2}}}
\newcommand{\subpagepair}[1]{%  % produces {subpage}{page}
  \@ifundefined{r@#1}%
    {{0}{0}}%
    {\tnsl@genericRef\@subpagepair{#1}}}
\def\@subpagepair#1#2#3\\{%
  \@ifundefined{2on#2}{{0}{#2}}{{#1}{#2}}}
\newcommand{\sublabel}[1]{%
  \leavevmode % needed to make \@bsphack work
  \@bsphack
  \tnsl@hyperTarget{#1}%
  \if@filesw {\let\thepage\relax
   \def\protect{\noexpand\noexpand\noexpand}%
   \edef\@tempa{\write\@auxout{\string
      \newsublabel{#1}{{}{\thepage}}}}%
   \expandafter}\@tempa
   \if@nobreak \ifvmode\nobreak\fi\fi\fi\@esphack}
\newcommand{\newsublabel}[2]{%
  \edef\this@page{\@cdr#2\@nil}%
  \ifx\this@page\last@page\else
    \sub@page=\z@
  \fi
  \edef\last@page{\this@page}
  \increment \sub@page
  \ifnum\sub@page=\tw@
    \global\@namedef{2on\this@page}{}%
  \fi
  \pendingsublabel{#1}%
  \edef\@tempa##1{\noexpand\newlabel{##1}%
    {{\number\sub@page}{\this@page}}}%
  \pending@sublabels
  \def\pending@sublabels{}}

\newcommand{\nextchunklabel}[1]{%
  \tnsl@hyperAnchor{#1}%
  \@bsphack\if@filesw {\let\thepage\relax
      \edef\@tempa{\write\@auxout{\string\pendingsublabel{#1}}}%
      \expandafter}\@tempa
   \if@nobreak \ifvmode\nobreak\fi\fi\fi\@esphack}

\newcommand\pendingsublabel[1]{%
  \def\@tempa{\noexpand\@tempa}%
  \edef\pending@sublabels{\noexpand\@tempa{#1}\pending@sublabels}}
\def\pending@sublabels{}
\def\last@page{\relax}
\newcount\sub@page

\def\tnsl@chunkTag#1#2{#2\ifnum#1=0 \else\@alph{#1}\fi}
\newcount\@commacount
\def\commafy#1{%
  \begingroup
  \tnsl@countListElements{#1}
  \@commacount=\nwix@counter
  \let\@comma@each=\\%
  \ifcase\@commacount\let\\=\@comma@each\or\let\\=\@comma@each\or
    \def\\{\def\\{ and \@comma@each}\@comma@each}\else
    \def\\{\def\\{, %
                  \decrement \@commacount
                  \ifnum\@commacount=1 and~\fi\@comma@each}\@comma@each}\fi
  #1%
  \endgroup%
}
\def\tnsl@listAppend#1#2{% {list}{\marker{element}}
  \begingroup%
  \toks0=\expandafter{#1}%
  \def\@tempa{#2}%
  \toks2=\expandafter{\@tempa}%
  \xdef#1{\the\toks0 \the\toks2 }%
  \endgroup%
}

\def\tnsl@determineUses#1{% {label}
  \def\nwixu{\\}
  \let\nwixd\@gobble
  \@nameuse{nwixl@#1}}

\def\tnsl@determineDefs#1{% {label}
  \def\nwixd{\\}\let\nwixu\@gobble\@nameuse{nwixl@#1}}

\newcount\nwix@counter
\def\tnsl@countListElements#1{%
  \begingroup
  \@tmpCounter=0
  \def\\##1{\increment \@tmpCounter}%
  #1%
  \global\nwix@counter=\@tmpCounter%
  \endgroup%
}

\def\tnsl@countUses#1{\tnsl@countListElements{\tnsl@determineUses{#1}}}
\def\tnsl@countDefs#1{\tnsl@countListElements{\tnsl@determineDefs{#1}}}
\def\nwix@id@defs#1{% index pair
  {{\Tt \@car#1\@nil}%
  \def\\##1{\penalty200\ \subpageref{##1}}\tnsl@determineDefs{\@cdr#1\@nil}}}

\newif\if@nwlongchunks
\@nwlongchunkstrue
\newcommand\@tnsl@hyperLink[2]{\hyperlink{tensile.#1}{#2}}
\newcommand\tnsl@hyperLink{%
  \@ifundefined{hyperlink}
    {\global\let\tnsl@hyperLink\@gobble}
    {\global\let\tnsl@hyperLink\@tnsl@hyperLink}%
  \tnsl@hyperLink
}
\newcommand\@tnsl@hyperTarget[1]{\hypertarget{tensile.#1}{\relax}}
\newcommand\tnsl@hyperTarget{%
  \@ifundefined{hypertarget}
    {\global\let\tnsl@hyperTarget\@gobble}
    {\global\let\tnsl@hyperTarget\@tnsl@hyperTarget}%
  \tnsl@hyperTarget
}
%% 
%% Copyright (C) 2009 by Taylor Venable <taylor@metasyntax.net>
%% 
%% This work may be distributed and/or modified under the
%% conditions of the LaTeX Project Public License (LPPL), either
%% version 1.3c of this license or (at your option) any later
%% version.  The latest version of this license is in the file:
%% 
%% http://www.latex-project.org/lppl.txt
%% 
%% This work is "maintained" (as per LPPL maintenance status) by
%% Taylor Venable.
%% 
%% This work consists of the file  tensile.dtx
%% and the derived files           tensile.ins,
%%                                 tensile.pdf and
%%                                 tensile.sty.
%% 
%% The tensile.sty file is based in part on work by Norman Ramsey
%% called Noweb.  Specifically, this file contains modified code
%% fragments from the original noweb.sty distributed with Noweb.
%% The Noweb COPYRIGHT notice is reproduced in full below.
%% 
%% ================================================================
%% 
%% Noweb is copyright 1989-2000 by Norman Ramsey.
%% All rights reserved.
%% 
%% Noweb is protected by copyright.  It is not public-domain
%% software or shareware, and it is not protected by a ``copyleft''
%% agreement like the one used by the Free Software Foundation.
%% 
%% Noweb is available free for any use in any field of endeavor.
%% You may redistribute noweb in whole or in part provided you
%% acknowledge its source and include this COPYRIGHT file.  You may
%% modify noweb and create derived works, provided you retain this
%% copyright notice, but the result may not be called noweb without
%% my written consent.
%% 
%% You may sell noweb if you wish.  For example, you may sell a
%% CD-ROM including noweb.
%% 
%% You may sell a derived work, provided that all source code for
%% your derived work is available, at no additional charge, to
%% anyone who buys your derived work in any form.  You must give
%% permisson for said source code to be used and modified under the
%% terms of this license.  You must state clearly that your work
%% uses or is based on noweb and that noweb is available free of
%% change.  You must also request that bug reports on your work be
%% reported to you.
%%
%% End of file `tensile.sty'.
