% $Id: pretzel-noweb-style.nw,v 1.3 1998/02/24 15:18:13 gaertner Exp $
%----------------------------------------------------------------------

%
% XXX
%
% [noweb]
% (history at end)



\documentclass{article}
%
%\usepackage{german}
\usepackage{rcs}
\usepackage{noweb}
%\usepackage{pretzel-noweb} % don't use myself
%
\begin{document}



\title{{\tt pretzel-noweb.sty} \\
        Interfacing Pretzel and {\tt noweb} in \LaTeX}
\author{Felix G\"artner}
\RCSdate $Date: 1998/02/24 15:18:13 $

\maketitle



\section{Introduction}

This file contains some \TeX{} and \LaTeX{} macros that support
prettyprinting in Norman Ramsey's {\tt noweb}. It provides definitions
for the macros that a Pretzel prettyprinting filter for \LaTeX{}
emits.

The definitions in this file are based on the information in the {\tt
support.nw} file from the {\tt noweb} distribution that explains the
macros used in {\tt noweb}. Also, a similar file from Lee Wittenberg that
belong to his Pretzel prettyprinter for Java has been very helpful.



\section{What Pretzel needs}

The macros that are output by the [[Latex_cweb_output]] class are
basicly the following:

<<macros output by Pretzel>>=
\pretzelindent
\pretzeloutdent
\pretzelopt#1                % opt with #1 times 10 penalty
\pretzelbackup
\pretzelbreakspace
\pretzelforce#1              % parm below is to eat {} generated by Pretzel
\pretzelbigforce
\pretzelnoindent
@
The interface between the {\tt noweb} prettyprinting filter and the
{\tt noweb} intermediate stream representation emits [[\begin{ppcode}]]
and [[\end{ppcode}]] lines around the prettyprinted text. This is
usefull to define specialities of the code environment. Also one macro
from the \texttt{noweb.sty} is redefined to make \texttt{CWEB} like
prettyprinting possible.

In situations where prettyprinting is not wished or where the
prettyprinter simply failed to do his task well, we want to try and
restore the old \texttt{noweb}-style verbatim environment by issuing
the [[\pretzelnwcodeimitation]] macro before outputting the code.

\section{The Code}

This is an overview over the file.

<<*>>=
% pretzel-noweb.sty  -- interfacing Pretzel and noweb in LateX
% (c) 1997 by Felix Gaertner. Look at pretzel-noweb-style.nw if
% you want to make changes.
%
<<include other styles>>
<<redefine {\tt noweb} macros>>
<<fake {\tt noweb} code environment>>
<<counting indentation>>
<<the [[ppcode]] environment>>
<<the formatting macros>>
<<specials>>
@
I don't know if it's a good idea to include the {\tt noweb} style here or
leave this to the user. I'll leave it for now and will think about it
later.

<<include other styles>>=
%\input noweb.sty
@
In any case, we'll need to redefine the [[\@begincode]] macro of
{\tt noweb}. The {\tt noweave} processor will bracket code parts with
[[\nwbegincode]] and [[\nwendcode]]. The [[\nwbegincode]] macro sets
separations and penalties and then calls [[\@begincode]], which
captures the common setup code also used in the [[webcode]]
environment that the style offers. Within [[\@begincode]] the
[[verbatim]] kind of code environment ist setup and we have to alter
this, since the Pretzel macros do all of the work by themselves.

This redefinition of [[\@begincode]] bases on Lee Wittenberg's
implementation. It differs from the original version in three things:

\begin{itemize}
\item It doesn't setup [[\obeylines]] and so leaves line breaks to
  \TeX{} and the format commands introduced by Pretzel.
\item It doesn't call [[\setupcode]] and so preserves the usual
  category codes of characters like {\tt\$} and {\tt\_}. We have to
  make sure that the Pretzel prettyprinter handles them well
  ourself (which is especially tricky in identifiers, see below).
\item The last line states [[\let\eatline=\relax]]. I don't know why.
\end{itemize}

<<redefine {\tt noweb} macros>>=
\def\@begincode{%
        \trivlist \item[]%
        \leftskip\@totalleftmargin \advance\leftskip\codemargin
        \rightskip\hsize \advance\rightskip -\codehsize
        \parskip\z@ \parindent\z@ \parfillskip\@flushglue
        \linewidth\codehsize
        \nowebsize
        \let\maybehbox\mbox
%       \let\eatline=\relax
}
@

Sometimes we would like to have the ``real'' version of
[[\@begincode]] still available because prettyprinting is not wished
or cannot be done. The [[\pretzelnwcodeimitation]] macro can be issued
in such cases. It contains all the \TeX{} code that was left away in
the above redefinition and thus turns everything into a state as if
nothing has been changed.

<<fake {\tt noweb} code environment>>=
\def\pretzelnwcodeimitation{
  \@@par
  \def\par{\leavevmode\null \@@par \penalty\nwcodepenalty}%
  \obeylines
  \@noligs   \ifx\verbatim@nolig@list\undefined\else
               \let\do=\nw@makeother \verbatim@nolig@list \do@noligs\`
             \fi
  \setupcode \frenchspacing \@vobeyspaces
}
@

A new counter keeps track of the indentation.

<<counting indentation>>=
\newcount\ind
@

<<the [[ppcode]] environment>>=
\newenvironment{ppcode}{%
        \sfcode`;=3000                % a `;' acts like a period
        \pretolerance=10000 %
        \hyphenpenalty=10000 %
        \exhyphenpenalty=10000 %
        \global\ind=2 % This fakeout is important for opt to work correctly
        \par
        \global\hangindent\ind em
}{%
%        \mathout
}
@
Finally here are the actual formatting macros. They go back to the
original Knuthian versions as in {\tt cwebmac.sty}.

<<the formatting macros>>=
\newbox\bak\setbox\bak=\hbox to -1em{} % backspace 1 em
\newbox\bakk\setbox\bakk=\hbox to -2em{} % backspace 2 ems

\def\pretzelindent{\global\advance\ind by1\hangindent\ind em}%
\def\pretzeloutdent{\global\advance\ind by-1}%
\def\pretzelopt#1{\hfil\penalty#10\hfilneg}%
\def\pretzelbackup{\copy\bak}%
\def\pretzelbreakspace{\hfil\penalty-1\hfilneg
        \kern2.5em\copy\bakk\ignorespaces}%
% parm below is to eat {} generated by Pretzel
\def\pretzelforce#1{\mathout\par\hangindent\ind em\noindent
        \kern\ind em\copy\bakk#1\ignorespaces}%
\def\pretzelbigforce{\mathout\par\smallskip\pretzelforce}%
\def\pretzelnoindent{\hskip-\ind em\hskip 2em}%
@
We'll change the category code of [[^^K]] (which is what?) so that be
can bracket strings with it using [[\verb*]]. I'll better use
the explicit value 12 instead of [[\other]], because it sometimes
messes up \LaTeX.

<<specials>>=
\catcode`\^^K=12    % 12=other, for \verb* with strings
@
We'll also have a macro that handles underscores in identifiers
correctly.

<<specials>>=
\def\pretzelid#1{\begingroup\catcode`\_=12%
  #1\endgroup}
@
These two macros allow better handling of math mode switching. Thanks
to Lee Wittenberg for the idea.

<<specials>>=
\def\mathin{\ifmmode\else\begin{math}\fi{}}
\def\mathout{\ifmmode\end{math}\fi{}}
@

\end{document}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

$Log: pretzel-noweb-style.nw,v $
Revision 1.3  1998/02/24 15:18:13  gaertner
Added macro to fake original noweb output.

Revision 1.2  1998/02/23 12:52:11  gaertner
Cleaned up documentation.

Revision 1.1  1997/07/09 08:35:09  gaertner
Initial revision

