olsak / OpTeX

OpTeX - LuaTeX format with extended Plain TeX macros
http://petr.olsak.net/optex/
35 stars 14 forks source link

`%%` comments in printdoc #50

Closed robertbachmann closed 3 years ago

robertbachmann commented 3 years ago

It would be nice if we can use %% in addition to _doc/_cod for literate .opm module programming.

However currently I can not get it to work.

Example files:

% doc.tex
\load[doc]

\sec Try printdoc
{\catcode`\%=12 \_ea}\commentchars %%
\printdoc code

\sec Try verbinput
\verbinput (-) code.tex

\bye
\_codedecl{\foo}{...}

    \_doc
    Here is a note. Lorem ipsum.
    \_cod

    \def\dummya{}

%% Here is another note.
%% Lorem ipsum.

    \def\dummyb{}

\_endcode
\_endinput

Result is:

image

olsak commented 3 years ago

I registered this problem. But now, I am not able to find the core. I tried to copy \_printcomment definition from verbatim.opm to testing file doc.tex and I did a slight modification in it:

\_def\_printcomments{\_ttskip
   {\_reloading \_rm \_everypar={}%
    \_noindent XXX\_vcomments YYY\_ignorespaces \_scantextokens\_ea{\_vcomments}ZZZ\_par}%
   \_ttskip
}

Now, when I run your test then I see XXX YYY ZZZ in both tests, the first expansion of \_vcomments in both tests but the result of \_scantextokens is missing in the first test. I don't know why. printdoc

olsak commented 3 years ago

The problem was in "waiting \endinput". It can be illustrated by following code

\def\a{\endinput \scantokens{ABC}}
\a
\end

The \endinput primitive sets only a flag that if a next line will be opened, then current file is closed. \endinput does not do this work immediatelly. It means that following code in the macro \a is processed with "waiting \endinput". If this code includes \scantokens then the \endiput is applied to this internal stream (before ABC in our example). The result is, that our example skips ABC and follows the reading of the file by \end command. Empty output is here and TeX is finished at \end and it does not start reading from terminal (which is more expected behavior in this case, but not realized). Maybe, this is a bug in eTeX (i.e. in pdfTeX, LuaTeX etc. too). But \endinput with context of following \scantokens is not well documented, so we don't know if this is bug or feature.