The following minimal file overruns the available output streams
(version 2006.08.08 and context live):
\starttext
\dorecurse{16}{\startstaticMPfigure{\recurselevel}
fill fullcircle scaled 1cm withcolor red;
\stopstaticMPfigure}
\stoptext
At the end are the last few lines of the log. Here are the last few
lines of the last few lines:
! Bad number (16).
\openMPgraphicfile ...immediate \openout \MPwrite
\MPgraphicfile .mp\relax \...
After experiments and digging in the TeXbook I'm pretty sure the cause
is as follows:
The macro \openMPgraphicfile (supp-mps.tex) constructs the name of a new
output stream and allocates a new write to it:
\donetrue [EMAIL PROTECTED]@EA\csname
newwrite\endcsname\csname\@@MPG\@@MPG\MPgraphicfile\endcsname
Then \MPwrite is set to this stream number:
[EMAIL PROTECTED]@EA\MPwrite\csname\@@MPG\@@MPG\MPgraphicfile\endcsname
And then it's opened:
\immediate\openout\MPwrite\MPgraphicfile.mp\relax
which is where TeX complains about the bad number (16).
The number gets that high because each static figure goes into a
separate file, and each file requires a \newwrite. I thought it would
not run out of streams because the allocation is all enclosed in
\bgroup..\egroup (in \startstaticMPgraphic) and the \egroup would pop
the write-stream allocation stack. But no such luck. The workhorse
\alloc (p.347 of the TeXBook) uses a \global assignment to the control
sequence.
Here's a possible solution that works here but is a hack and may make
unwarranted assumptions about control-sequence names. It require a new
if to say whether we are processing a static MP figure, and then using a
dedicated output stream for those writes.
cut here
\unprotect
\newif\ifinstaticMPfigure
\newwrite\MPstaticwrite
\long\def\startstaticMPgraphic#1#2\stopstaticMPgraphic
{\bgroup
\instaticMPfiguretrue % value is restored by the \egroup
\setMPrandomseedfalse
\def\MPgraphicfile{#1}% no \jobname here
\let\allocateMPslot \gobbleoneargument
\let\deallocateMPslot\gobbleoneargument
\let\runMPgraphic\gobbleoneargument
\runMPgraphicstrue
\enableincludeMPgraphics
\currentMPgraphic\plusone % hack, else no close
\startMPgraphic#2\stopMPgraphic
\executeMPgraphicfile
\egroup}
\def\openMPgraphicfile#1#2% #1=alwaysopen #2=message
{%\doifundefinedelse{\@@MPG\@@MPG\MPgraphicfile}
% {\donetrue
% [EMAIL PROTECTED]@@MPG\@@MPG\MPgraphicfile\endcsname}%
% {\ifcase#1\donefalse\else\donetrue\fi}%
[EMAIL PROTECTED]@@MPG\@@MPG\MPgraphicfile\endcsname\relax
%\donetrue [EMAIL PROTECTED]@@MPG\@@MPG\MPgraphicfile\endcsname
% for the sake of plain usage
\donetrue
\ifinstaticMPfigure
\let\MPwrite\MPstaticwrite
\else
[EMAIL PROTECTED]@EA\csname
newwrite\endcsname\csname\@@MPG\@@MPG\MPgraphicfile\endcsname
\fi
\doglobal\addtocommalist\MPgraphicfile\MPgraphicfiles
\else
\ifcase#1\relax\donefalse\else\donetrue\fi
\fi
\ifinstaticMPfigure
\else
[EMAIL PROTECTED]@EA\MPwrite\csname\@@MPG\@@MPG\MPgraphicfile\endcsname
\fi
\ifdone
\immediate\openout\MPwrite\MPgraphicfile.mp\relax
\ifx\MPinputtranslation\empty\else
\immediate\write\MPwrite{\MPinputtranslation}%
\immediate\write\MPwrite{verbatimtex \MPinputtranslation etex ;}%
\fi
\immediate\write\MPwrite{\letterpercent\space #2 graphics of job
\jobname}%
\writeMPgraph
\setMPrandomseed
\fi}
\protect
\starttext
\dorecurse{16}{\startstaticMPfigure{\recurselevel}
fill fullcircle scaled 1cm withcolor red;
\stopstaticMPfigure}
\stoptext
cut here
As a diff:
cut here
--- a/supp-mps.tex 2006-07-22 11:26:47.0 -0400
+++ b/supp-mps.tex 2006-09-24 15:32:19.0 -0400
@@ -315,7 +315,9 @@
%D setting the next boolean to true.
\newwrite\MPwrite
+\newwrite\MPstaticwrite
+\newif\ifinstaticMPfigure
\newif\iflongMPlines \longMPlinestrue % we now have \obeyMPlines
\ifx \overlaywidth \undefined \def \overlaywidth {4cm} \fi
@@ -720,12 +722,20 @@
[EMAIL PROTECTED]@@MPG\@@MPG\MPgraphicfile\endcsname\relax
%\donetrue [EMAIL PROTECTED]@@MPG\@@MPG\MPgraphicfile\endcsname
% for the sake of plain usage
- \donetrue [EMAIL PROTECTED]@EA\csname
newwrite\endcsname\csname\@@MPG\@@MPG\MPgraphicfile\endcsname
+ \donetrue
+ \ifinstaticMPfigure
+ \let\MPwrite\MPstaticwrite
+ \else
+ [EMAIL PROTECTED]@EA\csname
newwrite\endcsname\csname\@@MPG\@@MPG\MPgraphicfile\endcsname
+ \fi
\doglobal\addtocommalist\MPgraphicfile\MPgraphicfiles
\else
\ifcase#1\relax\donefalse\else\donetrue\fi
\fi
- [EMAIL PROTECTED]@EA\MPwrite\csname\@@MPG\@@MPG\MPgraphicfile\endcsname
+ \ifinstaticMPfigure
+ \else
+ [EMAIL