home *** CD-ROM | disk | FTP | other *** search
/ Atari FTP / ATARI_FTP_0693.zip / ATARI_FTP_0693 / Tex / lgrind.zoo / lgrind.doc < prev    next >
Text File  |  1992-09-28  |  9KB  |  268 lines

  1. %
  2. %% LaTeX lgrind environment
  3. %
  4. % $Id: lgrind.doc,v 1.2 91/10/01 00:34:36 gvr Exp $
  5. %
  6. % Based on Van Jacobson's ``tgrindmac'', a macro package for TeX grinding
  7. % Our job here is to simplify it quite a bit and make it conform to LaTeX
  8. %
  9. % Modified, 8-Oct-87 by Jerry Leichter to work with various changes made
  10. % while adapting to VMS.  Made all the internal variables contain an "@".
  11. %
  12. % Modified, 17-Jan-87 by Jerry Leichter.  Many changes to correspond to
  13. % modifications in tgrind.exe.
  14. %
  15. % Modified, 6-Sep-91 by George Reilly.  Changed the name from tgrind to lgrind.
  16. % Minor changes to get rid of superfluous spaces.
  17.  
  18. \newif\ifc@mment            %True when setting a comment
  19. \newif\ifstr@ng                %True when setting a string constant
  20. \newif\ifright@                %In comments, \" => '' (else ``)
  21. \newif\ifLGd@fault            %True after default \LGbegin
  22. \newcount\lc@unt            %Line counter
  23. \newcount\ln@xt                %Next line to get numbered
  24. \newbox\ls@far                %Stores lines so far for tabbing
  25. \newdimen\TBw@d                %Tabwidth when tabbing
  26. \newbox\tb@x                %Tab positioning
  27. \newdimen\@ts                %Width of listing space
  28. %
  29. % _ marks a point where the pre-processor wants a fixed-width space (of
  30. % width \@ts).
  31. {\catcode`\_=\active \gdef\@setunder{\let_=\sp@ce}}
  32.  
  33. \def\BGfont{\sf}            %"Background" font
  34. \def\CMfont{\rm}            %Comment font
  35. \def\KWfont{\bf}            %Keyword font
  36. \def\STfont{\tt}            %String font
  37. \def\VRfont{\it}            %Variable name font
  38. \def\LGsize{\small}            %Size to use in displayed code
  39. \def\LGfsize{\footnotesize}        %Size to use in \lgrindfile
  40. \newif\ifLGinline            %True for in-line code
  41. \newif\ifLGleftnum            %Put line numbers on the left
  42. \newcount\LGnuminterval            %Line numbering interval
  43. \LGnuminterval=10
  44. \newskip\LGindent            %Indentation for displayed lines
  45. \LGindent=1.6667\parindent
  46. %
  47. % Default commands to open and close a code example.  \LGend is a no-op
  48. % unless \LGbegin was executed, so the user can provide explicit open code
  49. % on the %[ or %( line without providing any special code on the matching
  50. % %] or %) line.
  51. %
  52. \def\LGbegin{\ifLGinline$\hbox\else$$\vbox\fi\bgroup\LGd@faulttrue}
  53. \def\LGend{\ifLGd@fault\egroup\ifLGinline$\else$$\fi\LGd@faultfalse\fi}
  54.  
  55. %
  56. % Warning:  \lgrind may be executed in horizontal mode; it's very important
  57. % that there be no random spaces in it.  That's why so many of the lines
  58. % end in "%".
  59. \def\lgrind{%
  60. \def\Line##1{\L{\LB{##1}}}%        %For use with %=
  61. %
  62. % Stuff we ignore --- meant for stand-alone listings.
  63. %
  64. \let\Head=\@gobble            %Header information (-h)
  65. \def\File##1,##2,##3{}%            %File specification info
  66. \let\Proc=\@gobble            %Marks procedure def beginning
  67. \let\ProcCont=\@gobble            %Marks end of nested procedure def
  68. %
  69. % Each formfeed in the input is replaced by a "\NewPage" macro.  If
  70. % you really want a page break here, define this as "\vfill\eject".
  71. %
  72. \def\NewPage{\filbreak\bigskip}%
  73. %
  74. % Each line of displayed program text is enclosed by a "\L{...}".  We turn
  75. % each line into an hbox.  Every \LGnuminterval lines we output a small line
  76. % number in past the right edge.
  77. %
  78. \ifLGinline
  79.  \def\L##1{\setbox\ls@far\null\CF\strut##1\ignorespaces}%
  80. \else
  81.  \let\r@ghtlno\relax\let\l@ftlno\relax
  82.  \ifnum\LGnuminterval>\z@
  83.   \ifLGleftnum
  84.    \def\l@ftlno{\ifnum\lc@unt>\ln@xt\global\advance\ln@xt by\LGnuminterval
  85.      \llap{{\scriptsize\the\lc@unt\quad}}\fi}%
  86.   \else
  87.   \def\r@ghtlno{\ifnum\lc@unt>\ln@xt\global\advance\ln@xt by\LGnuminterval
  88.      \rlap{{\scriptsize\enspace\the\lc@unt}}\fi}%
  89.   \fi
  90.  \fi
  91.  \def\L##1{\@@par\setbox\ls@far=\null\CF\strut
  92.   \global\advance\lc@unt by1%
  93.   \hbox to\hsize{\hskip\LGindent\l@ftlno ##1\hfil\r@ghtlno}\ignorespaces}%
  94. \fi
  95. %
  96. \lc@unt=0\ln@xt=\LGnuminterval\advance\ln@xt by-1%
  97. %
  98. % The following weirdness is to deal with tabs.  "Pieces" of a line
  99. % between tabs are output as "\LB{...}".  E.g., a line with a tab at
  100. % column 16 would be output as "\LB{xxx}\Tab{16}\LB{yyy}".  (Actually, to
  101. % reduce the number of characters in the .tex file the \Tab macro
  102. % supplies the 2nd & subsequent \LB's.) We accumulate the LB stuff in an
  103. % hbox.  When we see a Tab, we grab this hbox (using "\lastb@x") and turn
  104. % it into a box that extends to the tab position.  We stash this box in
  105. % "\ls@far" & stick it on in front of the next piece of the line.  (There must
  106. % be a better way of doing tabs [cf., the Plain.tex tab macros] but I'm not
  107. % not enough of a TeX wizard to come up with it.  Suggestions would be
  108. % appreciated.)
  109. \def\LB{\CF\hbox\bgroup\box\ls@far\let\next=}%
  110. \def\Tab##1{\setbox\tb@x=\lastbox\TBw@d=\wd\tb@x\advance\TBw@d by 1\@ts
  111.  \ifdim\TBw@d>##1\@ts
  112.   \setbox\ls@far=\hbox{\box\ls@far \box\tb@x \sp@ce}\else
  113.   \setbox\ls@far=\hbox to ##1\@ts{\box\ls@far \box\tb@x \hfil}\fi\LB}%
  114. %
  115. %
  116. % A normal space is too thin for code listings.  We make spaces & tabs
  117. % be in "\@ts" units, which for displays are the width of a "0" in the tt
  118. % font.  For inline stuff, on the other hand, we prefer a somewhat smaller
  119. % space - actually, the same size as normal inter-word spaces - to help make the included stuff look like a unit.
  120. %
  121. % An _ expands to one of these spaces.
  122. \ifLGinline\def\sp@ce{\hskip .3333em}%
  123. \else \setbox\tb@x=\hbox{{\tt 0}}\@ts=\wd\tb@x \def\sp@ce{\hskip 1\@ts}\fi
  124. \catcode`\_=\active \@setunder
  125. %
  126. % Font changing stuff for keywords, comments & strings.  We put keywords
  127. % in boldface, comments in text-italic & strings in typewriter.  Since
  128. % we're usually changing the font inside of a \LB macro, we remember the
  129. % current font in \CF & stick a \CF at the start of each new box.
  130. % Also, the characters " and ' behave differently in comments than in
  131. % code, and others behave differently in strings than in code.
  132. \let\CF=\BGfont
  133. \def\K##1{{\KWfont ##1}\global\futurelet\next\ic@r}%        %Keyword
  134. \def\V##1{{\VRfont ##1}\global\futurelet\next\ic@r}%        %Variable
  135. \def\ic@r{\let\@tempa\/\ifx.\next\let\@tempa\relax%        %Optional \/
  136.  \else\ifx,\next\let\@tempa\relax\fi\fi\@tempa}%
  137. %
  138. % Comment Start, End
  139. %
  140. \def\C{\CMfont \global\let\CF\CMfont \global\c@mmenttrue \global\right@false}%
  141. \def\CE{\BGfont \global\let\CF\BGfont \global\c@mmentfalse}%
  142. %
  143. % String Start, End
  144. %
  145. \def\S{\STfont \global\let\CF\STfont \global\str@ngtrue}%
  146. \def\SE{\BGfont \global\let\CF\BGfont \global\str@ngfalse}%
  147. %
  148. % We need positive and negative thinspaces in both text and math modes, so
  149. % we re-define \, and \! here.  The definition for \, isn't really needed for
  150. % LaTex, but we try to be more complete.  Note that in LaTeX terms, the new
  151. % definition isn't robust, like the old - but nothing we produce here is
  152. % likely to be robust - or NEED to be! - anyway!
  153. \def\,{\relax \ifmmode\mskip\thinmuskip \else\thinspace \fi}%
  154. \def\!{\relax \ifmmode\mskip-\thinmuskip \else\negthinspace \fi}%
  155. %
  156. % Special characters.
  157. % \CH choses its first option alone in math mode; its second option in
  158. % a string; and its third option, enclosed in $'s, otherwise.  (At the
  159. % moment, nothing is ever set in math mode, but you never know....)
  160. %
  161. \def\CH##1##2##3{\relax\ifmmode ##1\relax
  162. \else\ifstr@ng ##2\relax\else$##3$\fi\fi }%
  163. \def\{{\CH\lbrace {\char'173}\lbrace }%
  164. \def\}{\CH\rbrace {\char'175}\rbrace }%
  165. \def\1{\CH///}%                        %/
  166. \def\2{\CH\backslash {\char'134}\backslash }%        %\
  167. \def\|{\CH|{\char'174}|}%
  168. \def\<{\CH<<<}%
  169. \def\>{\CH>>>}%
  170. \def\*{\CH***}\relax                    %\relax for DOCSTY
  171. \def\-{\CH---}%
  172. \def\_{\ifstr@ng {\char'137}\else
  173.   \leavevmode \kern.06em \vbox{\hrule width.35em}%
  174.   \ifdim\fontdimen\@ne\font=\z@ \kern.06em \fi\fi }%
  175. \def\&{{\sf \char'046}}%
  176. \def\#{{\STfont \char'043}}%
  177. \def\%{{\char'045}}%
  178. \def\~{{\char'176}}%
  179. \def\"{\ifc@mment\ifright@ ''\global\right@false \else``\global\right@true \fi
  180.    \else{\tt \char'042}\fi}%
  181. \def\'{\ifc@mment'\else {\tt \char'015}\fi}%
  182. \def\^{{\tt \char'136}}%
  183. \def\${{\ifmmode\sl\else\ifdim\fontdimen\@ne\font>\z@\sl\fi\fi
  184.   \char'044}}%                        %No $ in \it, use \sl
  185. \parindent\z@\parskip\z@ plus 1pt\hsize\linewidth%
  186. }
  187. \def\endlgrind{\@@par}
  188. %
  189. % The following are generated as part of opening and closing included
  190. % code sequences.
  191. %
  192. \def\lgrinde{\ifLGinline\else\LGsize\fi\begin{lgrind}}
  193. \def\endlgrinde{\end{lgrind}}
  194.  
  195. % \lagrind*[FLOAT]{FILE}{CAPTION}{LABEL}
  196. %    *        optional
  197. %    FLOAT    float options
  198. %    FILE    reads LaTeXgrind input in file FILE
  199. %    CAPTION    for list of figures
  200. %    LABEL    for \ref and \pageref
  201. \def\lagrind{\@ifstar{\@slagrind}{\@lagrind}}
  202.  
  203. \def\@lagrind{\@ifnextchar[{\@@lagrind}{\@@lagrind[t]}}
  204. \def\@slagrind{\@ifnextchar[{\@@slagrind}{\@@slagrind[t]}}
  205.  
  206. \def\@@lagrind[#1]#2#3#4{%
  207.     \begin{figure}[#1]
  208.     \hrule
  209.     \vskip .5\baselineskip
  210.     \begin{minipage}\columnwidth\LGsize\LGindent\z@
  211.         \begin{lgrind}
  212.         \input #2\relax
  213.         \end{lgrind}
  214.     \end{minipage}
  215.     \vskip .5\baselineskip plus .5\baselineskip
  216.     \begingroup
  217.         \setbox\z@=\hbox{#4}%
  218.         \ifdim\wd\z@>\z@
  219.         \caption{#3}%
  220.         \label{#4}%
  221.         \else
  222.         \captcont{#3}%
  223.         \fi
  224.     \endgroup
  225.     \vskip 2pt
  226.     \hrule
  227.     \end{figure}
  228. }
  229.  
  230. \def\@@slagrind[#1]#2#3#4{%
  231.     \begin{figure*}[#1]
  232.     \hrule
  233.     \vskip .5\baselineskip
  234.     \begin{minipage}\textwidth\LGsize\LGindent\z@
  235.         \begin{lgrind}
  236.         \input #2\relax
  237.         \end{lgrind}
  238.     \end{minipage}
  239.     \vskip .5\baselineskip plus .5\baselineskip
  240.     \begingroup
  241.         \setbox\z@=\hbox{#4}%
  242.         \ifdim\wd\z@>\z@
  243.         \caption{#3}%
  244.         \label{#4}%
  245.         \else
  246.         \captcont{#3}%
  247.         \fi
  248.     \endgroup
  249.     \vskip 2pt
  250.     \hrule
  251.     \end{figure*}
  252. }
  253.  
  254.  
  255. \def\lgrindfile#1{%
  256.     \par\addvspace{0.1in}
  257.     \hrule
  258.     \vskip .5\baselineskip
  259.     \begingroup\LGfsize\LGindent\z@
  260.     \begin{lgrind}
  261.         \input #1\relax
  262.     \end{lgrind}
  263.     \endgroup
  264.     \vskip .5\baselineskip
  265.     \hrule
  266.     \addvspace{0.1in}
  267. }
  268.