[tex-eplain] Improvements for \listing

Oleg Katsitadze olegkat at gmail.com
Tue Apr 18 16:11:03 CEST 2006


I have a bug fix and two feature suggestions for the
\listing macro.  All patches are against eplain.tex from
CVS, which you can get from

  http://tug.org/eplain/src/eplain.tex


============================================================

I've encountered this bug when compiling the file bug.tex
(attached).  Notice that the last line's number is shifted
inside the paragraph.

The problem is in \@removebox, which is triggered by
\obeywhitespace at the end of the enclosing group.

\linenumberedlisting sets \everypar to call
\printlistinglineno, which produces an \llap, which is a
zero-width \hbox.  Since bug.tex sets \parindent = 0pt, the
first test in \@removebox succeeds:

 \ifdim\wd0=\parindent

and later \box2 is correctly detected as not empty; the
problem is that we end up putting back \box2, which discards
the effect of \llap.

Attached patch.bug seems to fix this problem.  It uses
\unhcopy instead of \unhbox to preserve \box0, and then puts
back \box0 instead of the crippled \box2.  As far as I can
tell, this shouldn't break anything.


============================================================

Eplain's \listing macro produces an empty line at the end of
the output when line numbers are enabled.  In a way, this is
correct, but sometimes it is desirable not to display the
empty line at the end.

My suggestion is to provide a `hook' which the user can use
to suppress the last line (see the attached patch.lastline).
It works with line numbering only if \printlistinglineno
contains only boxes, which it does by default and which is
reasonable to require from the user.

So, the user can say:

  \def\setuplistinghook{\linenumberedlisting \nolastlinelisting}

to have the line numbers and to suppress the last empty line.


============================================================

Another suggestion is to provide the ability to typeset some
TeX material inside a file inputed with \listing.  This
seems to be easy to do, see the attached patch.inline.  For
the escape char, I chose the non-printable code 0x02 (^^B),
mainly because of its name ("Start of text").  But if this
is a bad choice for some reason, we can choose any other
char.

\catcodespecials restores the catcodes damaged by
\uncatcodespecials (with plain TeX's \dospecials),
\obeywhitespace and \@setuplisting.

To see how it works, apply the patch and compile the
attached inline.tex (put the attached files example.eps and
source.c in the same directory with inline.tex).


How does all this look?

Best,
Oleg
-------------- next part --------------
A non-text attachment was scrubbed...
Name: bug.tex
Type: text/x-tex
Size: 97 bytes
Desc: not available
Url : http://tug.org/pipermail/tex-eplain/attachments/20060418/52eba879/bug.bin
-------------- next part --------------
--- eplain.tex.orig	2006-04-18 15:30:54.000000000 +0300
+++ eplain.tex	2006-04-18 15:31:12.000000000 +0300
@@ -1,10 +1,10 @@
 %% @texfile{
 %%   author = "Karl Berry, Steven Smith, Oleg Katsitadze, and others",
 %%   version = "3.0.90",
-%%   date = "Tue Apr 18 15:23:31 EEST 2006",
+%%   date = "Tue Apr 18 15:31:12 EEST 2006",
 %%   filename = "eplain.tex",
 %%   email = "bug-eplain at tug.org",
-%%   checksum = "3863   8347 123882",
+%%   checksum = "3863   8356 123926",
 %%   codetable = "ASCII",
 %%   supported = "yes",
 %%   docstring = "This file defines macros that extend and expand on
@@ -648,11 +648,11 @@
   \ifhmode
     \setbox0 = \lastbox
     \ifdim\wd0=\parindent
-      \setbox2 = \hbox{\unhbox0}%
+      \setbox2 = \hbox{\unhcopy0}% Preserve \box0 so we can put it back.
       \ifdim\wd2=0pt
         \ignorespaces
       \else
-        \box2 % Put it back: it wasn't empty.
+        \box0 % Put it back: it wasn't empty.
       \fi
     \else
        \box0 % Put it back: it wasn't the right width.
-------------- next part --------------
--- eplain.tex.orig	2006-04-18 15:30:54.000000000 +0300
+++ eplain.tex	2006-04-18 17:00:24.000000000 +0300
@@ -1,10 +1,10 @@
 %% @texfile{
 %%   author = "Karl Berry, Steven Smith, Oleg Katsitadze, and others",
 %%   version = "3.0.90",
-%%   date = "Tue Apr 18 15:23:31 EEST 2006",
+%%   date = "Tue Apr 18 17:00:24 EEST 2006",
 %%   filename = "eplain.tex",
 %%   email = "bug-eplain at tug.org",
-%%   checksum = "3863   8347 123882",
+%%   checksum = "3872   8365 124101",
 %%   codetable = "ASCII",
 %%   supported = "yes",
 %%   docstring = "This file defines macros that extend and expand on
@@ -1709,6 +1709,15 @@
   \everypar = {\advance\lineno by 1 \printlistinglineno}%
 }%
 \def\printlistinglineno{\llap{[\the\lineno]\quad}}%
+\def\nolastlinelisting{\aftergroup\@removeboxes}%
+\def\@removeboxes{%
+  \setbox0 = \lastbox
+  \ifvoid0
+    \ignorespaces % Ignore spaces after the \obeywhitespace's group.
+  \else
+    \expandafter\@removeboxes
+  \fi
+}%
 {%
   \makeactive\^^L
   \let^^L = \relax
-------------- next part --------------
--- eplain.tex.orig	2006-04-18 15:30:54.000000000 +0300
+++ eplain.tex	2006-04-18 16:20:01.000000000 +0300
@@ -1,10 +1,10 @@
 %% @texfile{
 %%   author = "Karl Berry, Steven Smith, Oleg Katsitadze, and others",
 %%   version = "3.0.90",
-%%   date = "Tue Apr 18 15:23:31 EEST 2006",
+%%   date = "Tue Apr 18 16:20:01 EEST 2006",
 %%   filename = "eplain.tex",
 %%   email = "bug-eplain at tug.org",
-%%   checksum = "3863   8347 123882",
+%%   checksum = "3889   8391 124471",
 %%   codetable = "ASCII",
 %%   supported = "yes",
 %%   docstring = "This file defines macros that extend and expand on
@@ -124,6 +124,29 @@
    \def\do##1{\catcode`##1 = \other}%
    \dospecials
 }%
+\begingroup
+\makeactive\^^L % Make sure ^^L is active.
+\let^^L\relax   % Make sure ^^L is not outer.
+\gdef\catcodespecials{%
+  \catcode`\ =10
+  \catcode`\\=0
+  \catcode`\{=1
+  \catcode`\}=2
+  \catcode`\$=3
+  \catcode`\&=4
+  \catcode`\#=6
+  \catcode`\^=7
+  \catcode`\^^K=7
+  \catcode`\_=8
+  \catcode`\^^A=8
+  \catcode`\%=14
+  \catcode`\~=\active \def~{\penalty\@M \ }%
+  \catcode`\^^M=5
+  \catcode`\`=\other
+  \catcode`\^^I=10
+  \catcode`\^^L=\active \outer\def^^L{\endgraf}%
+}%
+\endgroup
 {%
    \makeactive\^^M %
    \long\gdef\letreturn#1{\let^^M = #1}%
@@ -1711,6 +1734,7 @@
 \def\printlistinglineno{\llap{[\the\lineno]\quad}}%
 {%
   \makeactive\^^L
+  \makeactive\^^B
   \let^^L = \relax
   \gdef\@setuplisting{%
      \uncatcodespecials
@@ -1718,7 +1742,9 @@
      \makeactive\`
      \makeactive\^^I
      \makeactive\^^L
+     \makeactive\^^B
      \def^^L{\vfill\break}%
+     \def^^B{\begingroup\let^^B\endgroup\catcodespecials\rm}%
      \parskip = 0pt
      \listingfont
   }%
-------------- next part --------------
A non-text attachment was scrubbed...
Name: inline.tex
Type: text/x-tex
Size: 137 bytes
Desc: not available
Url : http://tug.org/pipermail/tex-eplain/attachments/20060418/52eba879/inline.bin
-------------- next part --------------
A non-text attachment was scrubbed...
Name: source.c
Type: text/x-csrc
Size: 453 bytes
Desc: not available
Url : http://tug.org/pipermail/tex-eplain/attachments/20060418/52eba879/source.bin
-------------- next part --------------
A non-text attachment was scrubbed...
Name: example.eps
Type: application/postscript
Size: 7894 bytes
Desc: not available
Url : http://tug.org/pipermail/tex-eplain/attachments/20060418/52eba879/example.eps


More information about the tex-eplain mailing list