lgrind.sty 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. %%
  2. %% This is file `lgrind.sty',
  3. %% generated with the docstrip utility.
  4. %%
  5. %% The original source files were:
  6. %%
  7. %% lgrind.dtx (with options: `package')
  8. %%
  9. %% LGrind is used to format source code of different programming
  10. %% languages for LaTeX.
  11. %%
  12. %% LGrind is a major adaptation of Jerry Leichter's tgrind for LaTeX,
  13. %% which was a notable improvement upon Van Jacobsen's tgrind for
  14. %% plain TeX, which was adapted from vgrind, a troff prettyprinter.
  15. %%
  16. %% Author: Michael Piefel, piefel@cs.tu-berlin.de
  17. %% Based on Van Jacobson's ``tgrindmac'', a macro package for TeX.
  18. %% Modified, 1987 by Jerry Leichter. Put '@' in all internal names.
  19. %% Modified, 1991 by George Reilly. Changed name from tgrind to lgrind.
  20. %% Modified, 1995 by Michael Piefel. Made it work with \LaTeXe.
  21. %% -1999 Hundreds of bells and whistles. No changelog here.
  22. \NeedsTeXFormat{LaTeX2e}[1996/06/01]
  23. \ProvidesPackage{lgrind}
  24. [1999/05/28 v3.6 LGrind environment and supporting stuff]
  25. %%stopzone % VIM syncing
  26. \newcount\lc@unt
  27. \newcount\ln@xt
  28. \newcount\LGnuminterval
  29. \LGnuminterval=10
  30. \DeclareOption{nolineno}{\LGnuminterval=50000}
  31. \DeclareOption{lineno5}{\LGnuminterval=5}
  32. \DeclareOption{lineno1}{\LGnuminterval=1}
  33. \newif\ifLGleftnum
  34. \DeclareOption{leftno}{\LGleftnumtrue}
  35. \newskip\LGindent
  36. \LGindent=1.0\parindent
  37. \DeclareOption{noindent}{\LGindent=0pt}
  38. \newif\ifLGnorules
  39. \DeclareOption{norules}{\LGnorulestrue}
  40. \newlength{\LGsloppy}
  41. \setlength{\LGsloppy}{7.2pt}
  42. \DeclareOption{fussy}{\LGsloppy=0pt}
  43. \newcommand{\DefaultProc}{\@gobble}
  44. \newcommand{\DefaultProcCont}{\@gobble}
  45. \DeclareOption{procnames}{
  46. \renewcommand{\DefaultProc}[1]{\renewcommand{\Procname}{#1}%
  47. \global\setbox\procbox=\hbox{\PNsize #1}}
  48. \renewcommand{\DefaultProcCont}[1]{\renewcommand\Procname{#1}
  49. \global\setbox\procbox=\hbox{\PNsize\dots #1}}}
  50. \newbox\procbox
  51. \newcommand{\Procname}{}
  52. \newif\ifLGnoprocindex
  53. \DeclareOption{noprocindex}{\LGnoprocindextrue}
  54. \ProcessOptions
  55. %\def\BGfont{\sffamily}
  56. \def\BGfont{\ttfamily\bfseries}
  57. \def\CMfont{\rmfamily\itshape}
  58. \def\NOfont{\sffamily}
  59. %\def\NOfont{\ttfamily}
  60. %\def\KWfont{\rmfamily\bfseries}
  61. \def\KWfont{\rmfamily\itshape\bfseries}
  62. %\def\KWfont{\ttfamily}
  63. %\def\STfont{\ttfamily}
  64. \def\STfont{\rmfamily\itshape\bfseries}
  65. %\def\TTfont{\ttfamily\upshape}
  66. \def\TTfont{\rmfamily\itshape\bfseries}
  67. %\def\VRfont{\ttfamily}
  68. \def\VRfont{\rmfamily\itshape\bfseries}
  69. \def\PNsize{\BGfont\small}
  70. \def\LGsize{\small}
  71. \def\LGfsize{\footnotesize}
  72. \newif\ifLGinline
  73. \newif\ifLGd@fault
  74. \def\LGbegin{\ifLGinline$\hbox\else$$\vbox\fi\bgroup\LGd@faulttrue}
  75. \def\LGend{\ifLGd@fault\egroup\ifLGinline$\else$$\fi\LGd@faultfalse\fi}
  76. %%stopzone % VIM syncing
  77. \newif\ifc@mment
  78. \newif\ifstr@ng
  79. \newif\ifright@
  80. \newbox\ls@far
  81. \newbox\tb@x
  82. \newdimen\TBw@d
  83. \newdimen\@ts
  84. {\catcode`\_=\active \gdef\@setunder{\let_=\sp@ce}}
  85. \newcommand{\lgrindhead}{}
  86. \newcommand{\lgrindfilename}{}\newcommand{\lgrindfilesize}{}
  87. \newcommand{\lgrindmodyear}{}\newcommand{\lgrindmodmonth}{}
  88. \newcommand{\lgrindmodday}{}\newcommand{\lgrindmodtime}{}
  89. \newenvironment{lgrind}[1][1]{%
  90. \def\Line##1{\L{\LB{##1}}}%
  91. \newcommand{\Head}[1]{\gdef\lgrindhead{##1}}%
  92. \newcommand{\File}[6]{\gdef\lgrindfilename{##1}\message{(LGround: ##1)}%
  93. \gdef\lgrindmodyear{##2}\gdef\lgrindmodmonth{##3}%
  94. \gdef\lgrindmodday{##4}\gdef\lgrindmodtime{##5}%
  95. \gdef\lgrindfilesize{##6}}%
  96. \let\Proc=\DefaultProc%
  97. \let\ProcCont=\DefaultProcCont%
  98. \ifLGnoprocindex%
  99. \let\index\@gobble%
  100. \fi%
  101. \hfuzz=\LGsloppy
  102. \def\NewPage{\filbreak\bigskip}%
  103. \ifLGinline
  104. \def\L##1{\setbox\ls@far\null{\CF\strut##1}\ignorespaces}%
  105. \else
  106. \let\r@ghtlno\relax\let\l@ftlno\relax
  107. \ifnum\LGnuminterval>\z@
  108. \ifLGleftnum
  109. \def\l@ftlno{\ifnum\lc@unt>\ln@xt
  110. \global\advance\ln@xt by\LGnuminterval
  111. \llap{{\normalfont\scriptsize\the\lc@unt\quad}}\fi}
  112. \def\r@ghtlno{\rlap{\enspace\box\procbox}}%
  113. \else
  114. \def\r@ghtlno{\ifnum\lc@unt>\ln@xt
  115. \global\advance\ln@xt by\LGnuminterval
  116. \rlap{{\normalfont\scriptsize\enspace\the\lc@unt%
  117. \enspace\box\procbox}}
  118. \else\rlap{\enspace\box\procbox}\fi}%
  119. \fi
  120. \fi
  121. \def\L##1{\@@par\setbox\ls@far=\null\strut
  122. \global\advance\lc@unt by1%
  123. \hbox to \linewidth{\hskip\LGindent\l@ftlno ##1\egroup%
  124. \hfil\r@ghtlno}%
  125. \ignorespaces}%
  126. \fi
  127. \lc@unt=#1\advance\lc@unt by-1%
  128. \ln@xt=\LGnuminterval\advance\ln@xt by-1%
  129. \loop\ifnum\lc@unt>\ln@xt\advance\ln@xt by\LGnuminterval\repeat%
  130. \def\LB{\hbox\bgroup\bgroup\box\ls@far\CF\let\next=}%
  131. \def\Tab##1{\egroup\setbox\tb@x=\lastbox\TBw@d=\wd\tb@x%
  132. \advance\TBw@d by 1\@ts\ifdim\TBw@d>##1\@ts
  133. \setbox\ls@far=\hbox{\box\ls@far \box\tb@x \sp@ce}\else
  134. \setbox\ls@far=\hbox to ##1\@ts{\box\ls@far \box\tb@x \hfil}\fi\LB}%
  135. \ifLGinline\def\sp@ce{{\hskip .3333em}}%
  136. \else \setbox\tb@x=\hbox{\texttt{0}}%
  137. \@ts=0.8\wd\tb@x \def\sp@ce{{\hskip 1\@ts}}\fi
  138. \catcode`\_=\active \@setunder
  139. \def\CF{\ifc@mment\CMfont\else\ifstr@ng\STfont\fi\fi}
  140. \def\N##1{{\NOfont ##1}\global\futurelet\next\ic@r}%
  141. \def\K##1{{\KWfont ##1}\global\futurelet\next\ic@r}%
  142. \def\V##1{{\VRfont ##1}\global\futurelet\next\ic@r}%
  143. \def\ic@r{\let\@tempa\/\ifx.\next\let\@tempa\relax%
  144. \else\ifx,\next\let\@tempa\relax\fi\fi\@tempa}%
  145. \def\C{\egroup\bgroup\CMfont \global\c@mmenttrue \global\right@false}%
  146. \def\CE{\egroup\bgroup \global\c@mmentfalse}%
  147. \def\S{\egroup\bgroup\STfont \global\str@ngtrue}%
  148. \def\SE{\egroup\bgroup \global\str@ngfalse}%
  149. \def\,{\relax \ifmmode\mskip\thinmuskip \else\thinspace \fi}%
  150. \def\!{\relax \ifmmode\mskip-\thinmuskip \else\negthinspace \fi}%
  151. %%stopzone % VIM syncing
  152. \def\CH##1##2##3{\relax\ifmmode ##1\relax
  153. \else\ifstr@ng ##2\relax\else$##3$\fi\fi }%
  154. \def\|{\CH|||}% not necessary for T1
  155. \def\<{\CH<<<}% dto.
  156. \def\>{\CH>>>}% dto.
  157. \def\-{\CH---}% minus sign nicer than hyphen
  158. \def\_{\ifstr@ng {\char'137}\else
  159. \leavevmode \kern.06em \vbox{\hrule width.35em}%
  160. \ifdim\fontdimen\@ne\font=\z@ \kern.06em \fi\fi }%
  161. \def\#{{\STfont\char'043}}%
  162. \def\2{\CH\backslash {\char'134}\backslash }% % \
  163. \def\3{\ifc@mment\ifright@ ''\global\right@false%
  164. \else``\global\right@true \fi
  165. \else{\texttt{\char'042}}\fi}% % "
  166. \def\5{{\texttt{\char'136}}}% % ^
  167. \parindent\z@\parskip\z@ plus 1pt%
  168. \bgroup\BGfont
  169. }
  170. {\egroup\@@par} % end of environment lgrind
  171. \def\lgrinde{\ifLGinline\else\LGsize\fi\begin{lgrind}}
  172. \def\endlgrinde{\end{lgrind}}
  173. \def\lagrind{\@ifstar{\@slagrind}{\@lagrind}}
  174. \def\@lagrind{\@ifnextchar[{\@@lagrind}{\@@lagrind[t]}}
  175. \def\@slagrind{\@ifnextchar[{\@@slagrind}{\@@slagrind[t]}}
  176. \def\@@lagrind[#1]#2#3#4{%
  177. \begin{figure}[#1]
  178. \ifLGnorules\else\hrule\fi
  179. \vskip .5\baselineskip
  180. \begin{minipage}\columnwidth\LGsize\LGindent\z@
  181. \begin{lgrind}
  182. \input #2\relax
  183. \end{lgrind}
  184. \end{minipage}
  185. \vskip .5\baselineskip plus .5\baselineskip
  186. \ifLGnorules\else\hrule\fi\vskip .5\baselineskip
  187. \begingroup
  188. \setbox\z@=\hbox{#4}%
  189. \ifdim\wd\z@>\z@
  190. \caption{#3}%
  191. \label{#4}%
  192. \else
  193. \captcont{#3}%
  194. \fi
  195. \endgroup
  196. \vskip 2pt
  197. \end{figure}
  198. }
  199. \def\@@slagrind[#1]#2#3#4{%
  200. \begin{figure*}[#1]
  201. \ifLGnorules\else\hrule\fi
  202. \vskip .5\baselineskip
  203. \begin{minipage}\linewidth\LGsize\LGindent\z@
  204. \begin{lgrind}
  205. \input #2\relax
  206. \end{lgrind}
  207. \end{minipage}
  208. \vskip .5\baselineskip plus .5\baselineskip
  209. \ifLGnorules\else\hrule\fi\vskip .5\baselineskip
  210. \begingroup
  211. \setbox\z@=\hbox{#4}%
  212. \ifdim\wd\z@>\z@
  213. \caption{#3}%
  214. \label{#4}%
  215. \else
  216. \captcont{#3}%
  217. \fi
  218. \endgroup
  219. \vskip 2pt
  220. \end{figure*}
  221. }
  222. \def\lgrindfile#1{%
  223. \par\addvspace{0.1in}
  224. \ifLGnorules\else\hrule\fi
  225. \vskip .5\baselineskip
  226. \begingroup\LGfsize\LGindent\z@
  227. \begin{lgrind}
  228. \input #1\relax
  229. \end{lgrind}
  230. \endgroup
  231. \vskip .5\baselineskip
  232. \ifLGnorules\else\hrule\fi
  233. \addvspace{0.1in}
  234. }
  235. \endinput
  236. %%
  237. %% End of file `lgrind.sty'.