eplain[65] trunk: allow reading toc file after writing

commits+karl at tug.org commits+karl at tug.org
Wed Apr 17 18:37:52 CEST 2024


Revision: 65
          https://tug.org/svn/eplain?view=revision&revision=65
Author:   karl
Date:     2024-04-17 18:37:52 +0200 (Wed, 17 Apr 2024)
Log Message:
-----------
allow reading toc file after writing

Modified Paths:
--------------
    trunk/doc/userdef.texi
    trunk/xeplain.tex

Added Paths:
-----------
    trunk/test/tocwriteread.tex

Modified: trunk/doc/userdef.texi
===================================================================
--- trunk/doc/userdef.texi	2023-10-30 17:44:15 UTC (rev 64)
+++ trunk/doc/userdef.texi	2024-04-17 16:37:52 UTC (rev 65)
@@ -988,17 +988,22 @@
 for anything but preliminary proofs.
 
 Each of @code{\writetocentry}, @code{\writenumberedtocentry} and
- at code{\writenumberedtocline} opens
-the @t{.toc} file for writing, thereby deleting the information from the
-previous run.  You should therefore arrange that @code{\readtocfile} be
-called @emph{before} the first call to a @code{\writetoc at dots{}} macro.
+ at code{\writenumberedtocline} opens the @t{.toc} file for writing,
+thereby deleting the information from the previous run.  You should
+therefore arrange that @code{\readtocfile} be called before the first
+call to a @code{\writetoc at dots{}} macro, or after the last call.  You
+can't arbitrarily mix reading and writing.
+
+ at cindex short contents
+ at cindex contents, short
 @code{\readtocfile} does not itself delete the information
 from the @t{.toc} file, so that you can call it several times,
 e.g., to create both a short
 @cindex table of contents, short
-and normal table of contents.  (To produce this in particular, define
- at code{\tocsectionentry} to produce nothing while you are reading
- at t{.toc} file for a short table of contents (@pxref{Macro arguments}).)
+and normal table of contents.  (To produce this particular case,
+define @code{\tocsectionentry} to produce nothing while you are
+reading @t{.toc} file for a short table of contents (@pxref{Macro
+arguments}).)
 
 On the other hand, if you don't want to rewrite the @t{.toc} file at
 all, perhaps because you are only running @TeX{} on part of your

Added: trunk/test/tocwriteread.tex
===================================================================
--- trunk/test/tocwriteread.tex	                        (rev 0)
+++ trunk/test/tocwriteread.tex	2024-04-17 16:37:52 UTC (rev 65)
@@ -0,0 +1,75 @@
+% $Id$
+% From: Andy Bradford, 15apr24. Public domain.
+% The second and later toc's were empty, because we did not close the
+% toc before we read it.
+
+\input eplain
+\definecontentsfile{tocI}
+\definecontentsfile{tocII}
+\definecontentsfile{tocIII}
+%\let\tocIchapterentry=\tocchapterentry
+%\let\tocIIchapterentry=\tocchapterentry
+%\let\tocIIIchapterentry=\tocchapterentry
+\def\tocIchapterentry{\message{INPUT TOCCHAPTERI}\tocchapterentry}
+\def\tocIIchapterentry{\message{INPUT TOCCHAPTERII}\tocchapterentry}
+\def\tocIIIchapterentry{\message{INPUT TOCCHAPTERIII}\tocchapterentry}
+\def\blankpage{\vfill\eject\vbox{}\vfill\eject}
+\def\contents{
+\blankpage
+\leftline{\bf VOLUME I}
+\medskip
+\readtocIfile
+\bigskip
+\leftline{\bf VOLUME II}
+\medskip
+\readtocIIfile
+\bigskip
+\leftline{\bf VOLUME III}
+\medskip
+\readtocIIIfile
+}
+
+\contents
+\blankpage
+\writetocIentry{chapter}{One}
+\blankpage
+\writetocIentry{chapter}{Two}
+\blankpage
+\writetocIentry{chapter}{Three}
+
+\contents
+\blankpage
+\writetocIIentry{chapter}{Four}
+\blankpage
+\writetocIIentry{chapter}{Five}
+\blankpage
+\writetocIIentry{chapter}{Six}
+
+\contents
+\blankpage
+\writetocIIIentry{chapter}{Seven}
+\blankpage
+\writetocIIIentry{chapter}{Eight}
+
+\contents
+\blankpage
+
+\bye
+
+% (from karl) can be cut down to just writing and reading one toc:
+\input eplain
+\definecontentsfile{tocI}
+\def\tocIchapterentry{\message{[TOC1entry] }\tocchapterentry}
+\def\blankpage{\vfill\eject\vbox{}\vfill\eject}
+\def\contents{\vfill\eject
+\leftline{\bf VOLUME I}
+\readtocIfile
+}
+
+(first) \contents
+\blankpage
+\writetocIentry{chapter}{One}
+
+(second) \contents
+
+\bye


Property changes on: trunk/test/tocwriteread.tex
___________________________________________________________________
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:keywords
## -0,0 +1 ##
+Date Author Id Revision
\ No newline at end of property
Modified: trunk/xeplain.tex
===================================================================
--- trunk/xeplain.tex	2023-10-30 17:44:15 UTC (rev 64)
+++ trunk/xeplain.tex	2024-04-17 16:37:52 UTC (rev 65)
@@ -1578,12 +1578,19 @@
 }%
 % 
 % The entries are read in when the user invokes \readtocfile (which
-% should be before the first \writetocentry).  We do not open the .toc
-% file to allow reading it in twice to make a short contents or some
-% such.  This should not cause any problems because each of
-% \writecontentsentry and \writenumberedcontentsentry open the .toc
-% file for writing (i.e., empty it) before trying to write to it.
+% should be before the first \writetocentry).  We want
+% to for reading it in twice to make a short contents or such. 
+% 
+% Each of \writecontentsentry and \writenumberedcontentsentry open the
+% .toc file for writing (i.e., empty it) before trying to write to it. 
+% That does mean we have to close it before reading, in case the reads
+% and writes are intermixed (release 3.14).
+% 
 \def\readcontentsfile#1{%
+   % Close the toc file before reading, in case it's been written to.
+   \immediate\closeout \csname #1file\endcsname
+   \ece\global{@#1fileopenedfalse}% have to reopen if writing again.
+   %
    \edef\temp{%
      \noexpand\testfileexistence[\csname #1filebasename\endcsname]{#1}%
    }\temp



More information about the tex-eplain-commits mailing list.