home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / pp / pp-6.0 / doc / manual / texinputs / tgrind.sty < prev    next >
Encoding:
Text File  |  1989-12-06  |  5.9 KB  |  204 lines

  1. % LaTeX tgrind environment
  2.  
  3. % Based on Van Jacobson's ``tgrindmac'', a macro package for TeX grinding
  4. % Our job here is to simplify it quite a bit and make it conform to LaTeX
  5.  
  6. \newif\ifcomment\newif\ifstring
  7. \commentfalse\stringfalse
  8. \newcount\linecount\newcount\linenext
  9. \newbox\linesofar
  10. \newdimen\TBwid
  11. \newdimen\ts
  12. \newbox\tbox
  13.  
  14. \def\tgrind{%
  15. \commentfalse\stringfalse
  16. % Stuff we ignore for right now.
  17. \let\Head=\@gobble
  18. \def\File##1,##2,##3{}
  19. \let\Proc=\@gobble
  20. \let\ProcCont=\@gobble
  21. %
  22. % Each formfeed in the input is replaced by a "\NewPage" macro.  If
  23. % you really want a page break here, define this as "\vfill\eject".
  24. \def\NewPage{\filbreak\bigskip}
  25. %
  26. % Each line of the program text is enclosed by a "\L{...}".  We turn
  27. % each line into an hbox of size hsize.  If we saw a procedure name somewhere
  28. % in the line (i.e., "procbox" is not null), we right justify "procbox"
  29. % on the line.  Every 10 lines we output a small, right justified line number.
  30. \def\L##1{\par\hbox to\hsize{\CF\strut\global\advance\linecount by1
  31. ##1\hss\linebox}}
  32. %
  33. \linecount=0
  34. \linenext=9
  35. \def\linebox{\ifnum\linecount>\linenext\global\advance\linenext by10
  36. \hbox{\sevrm\the\linecount}\fi}
  37. %
  38. % The following weirdness is to deal with tabs.  "Pieces" of a line
  39. % between tabs are output as "\LB{...}".  E.g., a line with a tab at
  40. % column 16 would be output as "\LB{xxx}\Tab{16}\LB{yyy}".  (Actually, to
  41. % reduce the number of characters in the .tex file the \Tab macro
  42. % supplies the 2nd & subsequent \LB's.) We accumulate the LB stuff in an
  43. % hbox.  When we see a Tab, we grab this hbox (using "\lastbox") and turn
  44. % it into a box that extends to the tab position.  We stash this box in
  45. % "\linesofar" & use "\everyhbox" to get \linesofar concatenated onto the
  46. % front of the next piece of the line.  (There must be a better way of
  47. % doing tabs [cf., the Plain.tex tab macros] but I'm not not enough of a
  48. % TeX wizard to come up with it.  Suggestions would be appreciated.)
  49. \def\LB{\CF\hbox}
  50. \setbox\linesofar=\null
  51. \everyhbox={\box\linesofar}
  52. \def\Tab##1{\setbox\tbox=\lastbox\TBwid=1\wd\tbox\advance\TBwid by 1\ts
  53. \ifdim\TBwid>##1\ts
  54. \setbox\linesofar=\hbox{\box\tbox\space}\else
  55. \setbox\linesofar=\hbox to ##1\ts{\box\tbox\hfil}\fi\LB}
  56. %
  57. % A normal space is too thin for code listings.  We make spaces & tabs
  58. % be in "\ts" units (which are the width of a "0" in the current font).
  59. \setbox\tbox=\hbox{0} \ts=1\wd\tbox \setbox\tbox=\hbox{\hskip 1\ts}
  60. \def\space{\hskip 1\ts\relax}
  61. %
  62. % Font changing stuff for keywords, comments & strings.  We put keywords
  63. % in boldface, comments in text-italic & strings in typewriter.  Since
  64. % we're usually changing the font inside of a \LB macro, we remember the
  65. % current font in \CF & stick a \CF at the start of each new box.
  66. % Also, the characters " and ' behave differently in comments than in
  67. % code, and others behave differently in strings than in code.
  68. \let\CF=\rm
  69. \def\K##1{{\bf ##1}}    % Keyword
  70. \def\C{\it\global\let\CF=\it\global\commenttrue\relax}    % Comment Start
  71. \def\CE{\rm\global\let\CF=\rm\global\commentfalse\relax}% Comment End
  72. \def\S{\tt\global\let\CF=\tt\global\stringtrue\relax}    % String Start
  73. \def\SE{\rm\global\let\CF=\rm\global\stringfalse\relax}    % String End
  74. %
  75. % Special characters.
  76. \def\{{\ifmmode\lbrace\else\ifstring{\char'173}\else$\lbrace$\fi\fi}
  77. \def\}{\ifmmode\rbrace\else\ifstring{\char'175}\else$\rbrace$\fi\fi}
  78. \def\!{\ifmmode\backslash\else\ifstring{\char'134}\else$\backslash$\fi\fi}
  79. \def\|{\ifmmode|\else\ifstring{\char'174}\else$|$\fi\fi}
  80. \def\<{\ifmmode<\else\ifstring<\else$<$\fi\fi}
  81. \def\>{\ifmmode>\else\ifstring>\else$>$\fi\fi}
  82. \def\/{\ifmmode/\else\ifstring/\else$/$\fi\fi}
  83. \def\-{\ifmmode-\else\ifstring-\else$-$\fi\fi}
  84. \def\_{\ifstring{\char'137}\else\underbar{\ }\fi}
  85. \def\&{{\char'046}}
  86. \def\#{{\char'043}}
  87. \def\%{{\char'045}}
  88. \def\~{{\char'176}}
  89. \def\"{\ifcomment''\else{\tt\char'042}\fi}
  90. \def\'{\ifcomment'\else{\tt\char'047}\fi}
  91. \def\^{{\char'136}}
  92. \def\${{\rm\char'044}}
  93. %
  94. \raggedright\obeyspaces%\let =\space%
  95. }
  96.  
  97. % \tagrind*[FLOAT]{FILE}{CAPTION}{LABEL}
  98. %    *        optional
  99. %    FLOAT    float options
  100. %    FILE    reads LaTeXgrind input in file FILE
  101. %    CAPTION    for list of figures
  102. %    LABEL    for \ref and \pageref
  103. \def\tagrind{\@ifstar{\@stagrind}{\@tagrind}}
  104.  
  105. \def\@tagrind{\@ifnextchar[{\@@tagrind}{\@@tagrind[t]}}
  106. \def\@stagrind{\@ifnextchar[{\@@stagrind}{\@@stagrind[t]}}
  107.  
  108. \def\@@tagrind[#1]#2#3#4{%
  109.     \begin{figure}[#1]
  110.     \hrule
  111.     \vskip .5\baselineskip
  112.     \begin{minipage}\columnwidth\small
  113.         \begin{tgrind}
  114.         \input #2\relax
  115.         \end{tgrind}
  116.     \end{minipage}
  117.     \vskip .5\baselineskip plus .5\baselineskip
  118.     \begingroup
  119.         \setbox\z@=\hbox{#4}%
  120.         \ifdim\wd\z@>\z@
  121.         \caption{#3}%
  122.         \label{#4}%
  123.         \else
  124.         \captcont{#3}%
  125.         \fi
  126.     \endgroup
  127.     \vskip 2pt
  128.     \hrule
  129.     \end{figure}
  130. }
  131.  
  132. \def\@@stagrind[#1]#2#3#4{%
  133.     \begin{figure*}[#1]
  134.     \hrule
  135.     \vskip .5\baselineskip
  136.     \begin{minipage}\textwidth\small
  137.         \begin{tgrind}
  138.         \input #2\relax
  139.         \end{tgrind}
  140.     \end{minipage}
  141.     \vskip .5\baselineskip plus .5\baselineskip
  142.     \begingroup
  143.         \setbox\z@=\hbox{#4}%
  144.         \ifdim\wd\z@>\z@
  145.         \caption{#3}%
  146.         \label{#4}%
  147.         \else
  148.         \captcont{#3}%
  149.         \fi
  150.     \endgroup
  151.     \vskip 2pt
  152.     \hrule
  153.     \end{figure*}
  154. }
  155.  
  156.  
  157. \def\tgrindfile#1{%
  158.     \par\addvspace{0.1in}
  159.     \hrule
  160.     \vskip .5\baselineskip
  161.     \begin{footnotesize}
  162.     \begin{tgrind}
  163.         \input #1\relax
  164.     \end{tgrind}
  165.     \end{footnotesize}
  166.     \vskip .5\baselineskip
  167.     \hrule
  168.     \addvspace{0.1in}
  169. }
  170.  
  171.  
  172. % \tagrindfile{FILE}{CAPTION}{LABEL}
  173.  
  174. \def\tagrindfile#1#2#3{%
  175.     \par\addvspace{0.1in}
  176.     \begingroup
  177.     \hrule
  178.     \vskip .5\baselineskip
  179.     \def\@captype{figure}
  180.     \begingroup
  181.         \setbox\z@=\hbox{#3}%
  182.         \ifdim\wd\z@>\z@
  183.         \refstepcounter{\@captype}
  184.         \label{#3}%
  185.         \fi
  186.     \endgroup
  187.     \begin{tgrind}\footnotesize
  188.         \input #1\relax
  189.     \end{tgrind}
  190.     \vskip .5\baselineskip plus .5\baselineskip
  191.     \begingroup
  192.         \setbox\z@=\hbox{#3}%
  193.         \ifdim\wd\z@>\z@
  194.         \@dblarg{\@caption\@captype}{#2}%
  195.         \else
  196.         \captcont{#2}%
  197.         \fi
  198.     \endgroup
  199.     \vskip 2pt
  200.     \hrule
  201.     \endgroup
  202.     \addvspace{0.1in}
  203. }
  204.