| 1 |
% \iffalse meta-comment |
| 2 |
% |
| 3 |
%% File: morewrites.dtx Copyright (C) 2011-2024 Bruno Le Floch |
| 4 |
%% |
| 5 |
%% It may be distributed and/or modified under the conditions of the |
| 6 |
%% LaTeX Project Public License (LPPL), either version 1.3c of this |
| 7 |
%% license or (at your option) any later version. The latest version |
| 8 |
%% of this license is in the file |
| 9 |
%% |
| 10 |
%% http://www.latex-project.org/lppl.txt |
| 11 |
%% |
| 12 |
%% This work has the LPPL maintenance status 'maintained' |
| 13 |
%% and the current maintainer is Bruno Le Floch. |
| 14 |
%% ----------------------------------------------------------------------- |
| 15 |
% |
| 16 |
%<*driver> |
| 17 |
\RequirePackage{morewrites} |
| 18 |
\ExplSyntaxOn |
| 19 |
\prg_replicate:nn { 300 } { \newwrite \foo } |
| 20 |
\ExplSyntaxOff |
| 21 |
\documentclass[full]{l3doc} |
| 22 |
\begin{document} |
| 23 |
\DocInput{\jobname.dtx} |
| 24 |
\end{document} |
| 25 |
%</driver> |
| 26 |
% \fi |
| 27 |
% |
| 28 |
% \title{The \textsf{morewrites} package: \\ |
| 29 |
% Always room for a new \tn{write}} |
| 30 |
% \author{Bruno Le Floch} |
| 31 |
% \date{2024/02/02} |
| 32 |
% |
| 33 |
% \maketitle |
| 34 |
% \tableofcontents |
| 35 |
% |
| 36 |
% \begin{documentation} |
| 37 |
% |
| 38 |
% \section{\pkg{morewrites} documentation} |
| 39 |
% |
| 40 |
% This \LaTeX{} package is a solution for the error |
| 41 |
% \enquote{no room for a new \tn{write}}, |
| 42 |
% which occurs when a document reserves too many streams to write data |
| 43 |
% to various auxiliary files. |
| 44 |
% It is in principle possible to rewrite other packages so that they |
| 45 |
% are less greedy on resources, but that is often unpractical for the |
| 46 |
% end-user. Instead, \pkg{morewrites} hooks at the lowest level (\TeX{} |
| 47 |
% primitives). |
| 48 |
% |
| 49 |
% Simply add the line |\usepackage{morewrites}| near the beginning of |
| 50 |
% your \LaTeX{} file's preamble: the \enquote{no room for a new |
| 51 |
% \tn{write}} error should vanish. If it does not, please |
| 52 |
% contact me so that I can correct the problem. This can be done by |
| 53 |
% posting a question on the \url{tex.stackexchange.com} question and |
| 54 |
% answers website, logging an issue on GitHub |
| 55 |
% (\url{https://github.com/blefloch/latex-morewrites}), or emailing me a |
| 56 |
% minimal file showing the problem. |
| 57 |
% |
| 58 |
% Notes. |
| 59 |
% \begin{itemize} |
| 60 |
% \item This package loads the \pkg{expl3} package, hence the |
| 61 |
% \pkg{l3kernel} bundle needs to be up to date. |
| 62 |
% \item This package uses an auxiliary file, \meta{job~name}|.mw|, |
| 63 |
% which can safely be deleted. The package only overwrites this |
| 64 |
% auxiliary file if it is empty, and otherwise uses a modified |
| 65 |
% file name, obtained by adding an integer to the name (previously |
| 66 |
% it was obtained by appending additional copies of |.mw| to the |
| 67 |
% name). Such files can be safely deleted. Be careful though to |
| 68 |
% not delete a Maple worksheet by accident when cleaning up your |
| 69 |
% files. |
| 70 |
% \item \LuaTeX{} allows $128$ \tn{write} streams, so this package does |
| 71 |
% nothing (with a warning) when used with \LuaTeX{}. |
| 72 |
% \end{itemize} |
| 73 |
% |
| 74 |
% \subsection{Commands defined or altered by \pkg{morewrites}} |
| 75 |
% |
| 76 |
% \begin{function}[added = 2014-07-26]{\morewritessetup} |
| 77 |
% \begin{syntax} |
| 78 |
% \cs{morewritessetup} \Arg{key--value list} |
| 79 |
% \end{syntax} |
| 80 |
% Sets the options described by the \meta{key--value list}. |
| 81 |
% \end{function} |
| 82 |
% |
| 83 |
% \begin{function}[added = 2017-04-10]{allocate} |
| 84 |
% \begin{syntax} |
| 85 |
% \cs{morewritessetup} |{| |allocate| |=| \meta{integer} |}| |
| 86 |
% \end{syntax} |
| 87 |
% Sets to (at least) \meta{integer} the number of \tn{write} streams |
| 88 |
% allocated to the inner workings of \pkg{morewrites}. By default |
| 89 |
% this is zero but increasing this value to 10 (or so) may speed up |
| 90 |
% \pkg{morewrites}. |
| 91 |
% \end{function} |
| 92 |
% |
| 93 |
% \begin{function}[added = 2014-07-26, updated = 2024-01-05]{file} |
| 94 |
% \begin{syntax} |
| 95 |
% \cs{morewritessetup} |{| |file| |=| \meta{file name} |}| |
| 96 |
% \end{syntax} |
| 97 |
% Sets (globally) the name of the file which will be used by internal |
| 98 |
% processes of \pkg{morewrites}. The file name is \tn{jobname}|.mw| |
| 99 |
% by default (technically, \cs{c_sys_jobname_str}|.mw|). Contrarily |
| 100 |
% to earlier versions of \pkg{morewrites} non-empty files will not be |
| 101 |
% overwritten; this design choice may lead to unwanted |.mw| files |
| 102 |
% remaining. |
| 103 |
% \end{function} |
| 104 |
% |
| 105 |
% \begin{function}[added = 2024-01-05]{verbose} |
| 106 |
% \begin{syntax} |
| 107 |
% \cs{morewritessetup} |{| |verbose| |}| |
| 108 |
% \end{syntax} |
| 109 |
% This boolean option (\texttt{false} by default) makes the package |
| 110 |
% write to the terminal all of the operations that it performs. This |
| 111 |
% can render \pkg{morewrites} useful for debugging some file-writing |
| 112 |
% operations. |
| 113 |
% \end{function} |
| 114 |
% |
| 115 |
% \begin{function}[updated = 2015-08-01]{\newwrite} |
| 116 |
% This macro is redefined by \pkg{morewrites}. Since \pkg{morewrites} |
| 117 |
% allows more than~$16$ write streams, it removes the corresponding |
| 118 |
% restrictions in \tn{newwrite}. |
| 119 |
% \begin{texnote} |
| 120 |
% The revised \tn{newwrite} allocate stream numbers starting at |
| 121 |
% $129$. This might break some code that expects stream numbers to |
| 122 |
% be less than $16$. |
| 123 |
% \end{texnote} |
| 124 |
% \end{function} |
| 125 |
% |
| 126 |
% \begin{function}[updated = 2015-08-01]{\immediate} |
| 127 |
% This primitive is altered by \pkg{morewrites}, to detect a following |
| 128 |
% \tn{write} or \tn{openout} or \tn{closeout} and perform the |
| 129 |
% appropriate action. |
| 130 |
% \end{function} |
| 131 |
% |
| 132 |
% \begin{function}[updated = 2017-04-20]{\openout, \write, \closeout} |
| 133 |
% These three primitives are altered by \pkg{morewrites} so that they |
| 134 |
% accept stream numbers outside the normal range $[0,15]$ and |
| 135 |
% open/write/close files as appropriate. |
| 136 |
% \begin{texnote} |
| 137 |
% System calls using \tn{write}|18| are detected and forwarded to the engine. |
| 138 |
% \end{texnote} |
| 139 |
% \end{function} |
| 140 |
% |
| 141 |
% \begin{function}{\shipout} |
| 142 |
% This primitive is altered by \pkg{morewrites} to ensure that delayed |
| 143 |
% \tn{openout}, \tn{write} and \tn{closeout} commands are performed at |
| 144 |
% \tn{shipout} time, and in the correct order. |
| 145 |
% \end{function} |
| 146 |
% |
| 147 |
% \subsection{Known deficiencies and open questions} |
| 148 |
% |
| 149 |
% See the bug tracker \url{https://github.com/blefloch/latex-morewrites/issues/} |
| 150 |
% for a list of issues with \pkg{morewrites}. |
| 151 |
% |
| 152 |
% The package code is not good \pkg{expl3} code. \emph{Do not take this |
| 153 |
% package as an example of how to code with \pkg{expl3}; go and see |
| 154 |
% Joseph Wright's \pkg{siunitx} instead.} It uses |
| 155 |
% \cs[no-index]{\ldots{}:D} primitives directly (the |:D| stands for |
| 156 |
% \enquote{do not use}). This is unavoidable in order to hook into the |
| 157 |
% primitives \tn{immediate}, \tn{write}, \emph{etc.\@} and to keep a |
| 158 |
% very strong control on what every command does. |
| 159 |
% |
| 160 |
% \end{documentation} |
| 161 |
% |
| 162 |
% \begin{implementation} |
| 163 |
% |
| 164 |
% \section{\pkg{morewrites} implementation} |
| 165 |
% |
| 166 |
%<*package> |
| 167 |
% \begin{macrocode} |
| 168 |
\RequirePackage {primargs} [2024/01/05] |
| 169 |
\ProvidesExplPackage |
| 170 |
{morewrites} {2024/02/02} {} {Always room for a new write} |
| 171 |
% \end{macrocode} |
| 172 |
% |
| 173 |
% Quit early under \LuaTeX{}. |
| 174 |
% \begin{macrocode} |
| 175 |
\sys_if_engine_luatex:T |
| 176 |
{ |
| 177 |
\cs_new_protected:Npn \morewritessetup #1 { } |
| 178 |
\msg_new:nnn { morewrites } { luatex } |
| 179 |
{ The~morewrites~package~is~unnecessary~in~LuaTeX. } |
| 180 |
\msg_warning:nn { morewrites } { luatex } |
| 181 |
\tex_endinput:D |
| 182 |
}% |
| 183 |
% \end{macrocode} |
| 184 |
% |
| 185 |
% \begin{macrocode} |
| 186 |
%<@@=morewrites> |
| 187 |
% \end{macrocode} |
| 188 |
% |
| 189 |
% \subsection{Overview of relevant \TeX{} facts} |
| 190 |
% |
| 191 |
% The aim of the \pkg{morewrites} package is to lift \TeX{}'s |
| 192 |
% restriction of only having $16$ files open for writing at the same |
| 193 |
% time. This requires patching the primitives \tn{immediate}, |
| 194 |
% \tn{openout}, \tn{write}, \tn{closeout}, and \tn{shipout}, and the |
| 195 |
% macro \tn{newwrite} present in plain \TeX{} and \LaTeXe{}. |
| 196 |
% |
| 197 |
% Note that doing the same for \tn{read} streams is impossible due to |
| 198 |
% the \tn{ifeof} primitive: that primitive cannot be replaced by a macro |
| 199 |
% without breaking nesting of conditionals. |
| 200 |
% |
| 201 |
% The \pkg{morewrites} package should be loaded as early as possible, so |
| 202 |
% that any package loaded later uses the redefined macros instead of the |
| 203 |
% primitives. However, the format (plain \TeX{} or \LaTeXe{}) and the |
| 204 |
% \pkg{expl3} programming language are always loaded before |
| 205 |
% \pkg{morewrites}, and their interaction must be carefully monitored. |
| 206 |
% |
| 207 |
% Henceforth, ``\TeX{} stream'' will refer to stream numbers in the |
| 208 |
% range $[0,15]$ provided to \TeX{}'s write primitives, while |
| 209 |
% ``user stream'' will denote stream numbers in $[0,15]\cup[129,\infty)$ |
| 210 |
% manipulated by the redefined \tn{openout}, \tn{write}, \tn{closeout}, |
| 211 |
% and \tn{newwrite}. A user stream in $[0,15]$ (reserved by \LaTeXe{} |
| 212 |
% or allocated by \pkg{expl3}) is mapped to the same \TeX{} stream |
| 213 |
% number, while a user stream in $[129,\infty)$ is mapped to a \TeX{} |
| 214 |
% stream according to the property list (with integer keys and values) |
| 215 |
% \cs{l_@@_write_prop}. Stream numbers $16$, |
| 216 |
% $17$ and $18$ are unused because \tn{write}16 is often used to write |
| 217 |
% to the terminal, and \tn{write}18 sends its argument to a shell. |
| 218 |
% The stream number $128$ is also often used like $16$ to avoid |
| 219 |
% distinguishing \LuaTeX{}. Rather than special-casing it we skip |
| 220 |
% directly to larger stream numbers. |
| 221 |
% |
| 222 |
% The primitives \tn{openout}, \tn{write}, and \tn{closeout} expect to |
| 223 |
% be followed by an \meta{integer}, normally in the range $[0,15]$, then |
| 224 |
% some further arguments. |
| 225 |
% \begin{quote} |
| 226 |
% \tn{openout} \meta{integer} \meta{equals} \meta{file name} \\ |
| 227 |
% \tn{write} \meta{integer} \meta{filler} \meta{general text} \\ |
| 228 |
% \tn{closeout} \meta{integer} |
| 229 |
% \end{quote} |
| 230 |
% All of the primitives above perform full expansion of all tokens when |
| 231 |
% looking for their operands. |
| 232 |
% \begin{itemize} |
| 233 |
% \item \meta{integer} denotes an integer in any form that \TeX{} |
| 234 |
% accepts as the right-hand side of a primitive integer assignment of |
| 235 |
% the form \tn{count}|0=|\meta{integer}; |
| 236 |
% \item \meta{equals} is an arbitrary (optional) number of explicit or |
| 237 |
% implicit space characters, an optional explicit equal sign of |
| 238 |
% category other, and further (optional) explicit or implicit space |
| 239 |
% characters; |
| 240 |
% \item \meta{file name} is an arbitrary sequence of explicit or implicit |
| 241 |
% characters with arbitrary category codes (except active characters, |
| 242 |
% which are expanded before reaching \TeX{}'s mouth), ending either |
| 243 |
% with a space character (character code $32$, arbitrary non-active |
| 244 |
% category code, explicit or implicit), which is removed, or with a |
| 245 |
% non-expandable token, with some care needed for the case of a |
| 246 |
% \tn{notexpanded:} expandable token; |
| 247 |
% \item \meta{filler} is an arbitrary combination of tokens whose |
| 248 |
% meaning is \tn{relax} or whose category code is $10$; |
| 249 |
% \item \meta{general text} is formed of braced tokens, starting with an |
| 250 |
% explicit or implicit begin-group character, and ending with the |
| 251 |
% matching explicit end-group character (both with any character |
| 252 |
% code), with an equal number of explicit begin-group and end-group |
| 253 |
% characters in between: this is precisely the right-hand side of an |
| 254 |
% assignment of the form \tn{toks}|0=|\meta{general text}. |
| 255 |
% \end{itemize} |
| 256 |
% |
| 257 |
% The \pkg{morewrites} package redefines these three control sequences |
| 258 |
% to expect a user stream number rather than a \TeX{} stream number as |
| 259 |
% the \meta{integer}, then map such a user stream to a \TeX{} stream to |
| 260 |
% call the primitive with the appropriate argument. The primitive |
| 261 |
% \tn{immediate} must also be redefined to detect \tn{openout}, |
| 262 |
% \tn{write}, and \tn{closeout} and make them immediate, while still |
| 263 |
% working with other primitives that can be made immediate. Finally, |
| 264 |
% \tn{newwrite} must be patched to allocate stream numbers beyond $15$. |
| 265 |
% |
| 266 |
% A few comments on the behaviour of primitives concerning the |
| 267 |
% \meta{integer} (\TeX{} stream). The \tn{openout} primitive trigger |
| 268 |
% errors if the \meta{integer} is not in $[0,15]$. The primitive |
| 269 |
% \tn{write} outputs to the log if the \meta{integer} is negative, and |
| 270 |
% to the terminal if the \TeX{} stream is closed or greater than $15$, |
| 271 |
% with the exception of \tn{write}|18| which runs code in a shell. The |
| 272 |
% \tn{closeout} primitive triggers an error if the \meta{integer} is not |
| 273 |
% in $[0,15]$ and silently do nothing if the \TeX{} stream is not open, |
| 274 |
% with the exception of \tn{closeout}|18| which causes a segfault at |
| 275 |
% least in some versions. |
| 276 |
% |
| 277 |
% By default, \tn{openout}, \tn{write} and \tn{closeout} are recorded in |
| 278 |
% a whatsit node in the current list, and will be performed when the box |
| 279 |
% containing the whatsit node is sent to the final \texttt{pdf}, |
| 280 |
% \emph{i.e.}, at \enquote{shipout} time. In particular, the |
| 281 |
% \meta{general text} for the \tn{write} primitive is expanded at |
| 282 |
% shipout time. This behaviour may be modified by putting |
| 283 |
% \tn{immediate} before any of these three primitives to force \TeX{} to |
| 284 |
% perform the action immediately instead of recording it in a whatsit |
| 285 |
% node. |
| 286 |
% |
| 287 |
% Since the \tn{openout}, \tn{write}, and \tn{closeout} primitives |
| 288 |
% operate at \tn{shipout} time, we will have to hook into this primitive |
| 289 |
% too. It expects to be followed by a box specification, for instance |
| 290 |
% \tn{box}\meta{integer} or \tn{hbox}\Arg{material to typeset}. |
| 291 |
% |
| 292 |
% Finally, the \tn{newwrite} macro expects one token as its argument, |
| 293 |
% and defines this token (with \tn{chardef}) to be an integer |
| 294 |
% corresponding to the first available (\TeX{}) write stream. This must |
| 295 |
% be extended to allocate higher (user) streams. |
| 296 |
% |
| 297 |
% \subsection{Preliminaries} |
| 298 |
% |
| 299 |
% \subsubsection{Copying some commands} |
| 300 |
% |
| 301 |
% \begin{macro} |
| 302 |
% { |
| 303 |
% \@@_tex_immediate:w , |
| 304 |
% \@@_tex_openout:w , |
| 305 |
% \@@_tex_write:w , |
| 306 |
% \@@_tex_closeout:w , |
| 307 |
% } |
| 308 |
% Aliases for the write-related primitives, to avoid |
| 309 |
% having |:D| throughout the code. |
| 310 |
% \begin{macrocode} |
| 311 |
\cs_new_eq:NN \@@_tex_immediate:w \tex_immediate:D |
| 312 |
\cs_new_eq:NN \@@_tex_openout:w \tex_openout:D |
| 313 |
\cs_new_eq:NN \@@_tex_write:w \tex_write:D |
| 314 |
\cs_new_eq:NN \@@_tex_closeout:w \tex_closeout:D |
| 315 |
% \end{macrocode} |
| 316 |
% \end{macro} |
| 317 |
% |
| 318 |
% \begin{macro}{\@@_tex_newwrite:N} |
| 319 |
% Copy \tn{newwrite} but making sure that it is |
| 320 |
% not \tn{outer}. This copy will not be affected by redefinitions |
| 321 |
% of \tn{newwrite} later on. |
| 322 |
% \begin{macrocode} |
| 323 |
\exp_args:NNf \cs_new_protected:Npn \@@_tex_newwrite:N |
| 324 |
{ \exp_args:NNc \exp_after:wN \exp_stop_f: { newwrite } } |
| 325 |
% \end{macrocode} |
| 326 |
% \end{macro} |
| 327 |
% |
| 328 |
% \subsubsection{Variants} |
| 329 |
% |
| 330 |
% We need these variants. |
| 331 |
% \begin{macrocode} |
| 332 |
\cs_generate_variant:Nn \prop_gpop:NnNT { NV } |
| 333 |
\cs_generate_variant:Nn \prop_gput:Nnn { NVx } |
| 334 |
\cs_generate_variant:Nn \tl_gput_right:Nn { Nv } |
| 335 |
% \end{macrocode} |
| 336 |
% |
| 337 |
% \subsubsection{Variables} |
| 338 |
% |
| 339 |
% \begin{variable}{\l_@@_internal_tl, \l_@@_internal_seq} |
| 340 |
% Used for temporary scratch purposes. |
| 341 |
% \begin{macrocode} |
| 342 |
\tl_new:N \l_@@_internal_tl |
| 343 |
\seq_new:N \l_@@_internal_seq |
| 344 |
% \end{macrocode} |
| 345 |
% \end{variable} |
| 346 |
% |
| 347 |
% \begin{variable}{\@@_tmp:w} |
| 348 |
% Used for temporary definitions. |
| 349 |
% \begin{macrocode} |
| 350 |
\cs_new_eq:NN \@@_tmp:w ? |
| 351 |
% \end{macrocode} |
| 352 |
% \end{variable} |
| 353 |
% |
| 354 |
% \begin{variable}{\l_@@_verbose_bool} |
| 355 |
% When this boolean is set true, \pkg{morewrites} will print to the terminal all of the operations that it does. |
| 356 |
% \begin{macrocode} |
| 357 |
\bool_new:N \l_@@_verbose_bool |
| 358 |
% \end{macrocode} |
| 359 |
% \end{variable} |
| 360 |
% |
| 361 |
% \begin{variable}{\g_@@_later_int} |
| 362 |
% The integer \cs{g_@@_later_int} labels the various |
| 363 |
% non-immediate operations in the order in which they appear in the |
| 364 |
% source. We can never reuse a number because there is no way to know |
| 365 |
% if a whatsit was recorded in a box register, which could be reused |
| 366 |
% in a shipped-out box: |
| 367 |
% \begin{quote} |
| 368 |
% \cs{vbox_set:Nn} \cs{l_my_box} \\ |
| 369 |
% ~~|{| \cs{iow_shipout_x:Nn} \cs{c_term_iow} \Arg{text} |}| |
| 370 |
% \tn{shipout} \tn{copy} \cs{l_my_box} |
| 371 |
% \tn{shipout} \tn{copy} \cs{l_my_box} |
| 372 |
% \end{quote} |
| 373 |
% will print \meta{text} to the terminal twice. |
| 374 |
% \begin{macrocode} |
| 375 |
\int_new:N \g_@@_later_int |
| 376 |
% \end{macrocode} |
| 377 |
% \end{variable} |
| 378 |
% |
| 379 |
% ^^A todo: populate \g_@@_write_seq because we don't use \newwrite; this should be done in \morewritessetup |
| 380 |
% \begin{variable}{\g_@@_write_seq} |
| 381 |
% Keep track of \TeX{} stream numbers managed by \pkg{morewrites} that |
| 382 |
% are currently not in use as user streams. |
| 383 |
% \begin{macrocode} |
| 384 |
\seq_new:N \g_@@_write_seq |
| 385 |
% \end{macrocode} |
| 386 |
% \end{variable} |
| 387 |
% |
| 388 |
% \begin{variable}{\g_@@_write_prop} |
| 389 |
% Map user streams to \TeX{} streams. |
| 390 |
% \begin{macrocode} |
| 391 |
\prop_new:N \g_@@_write_prop |
| 392 |
% \end{macrocode} |
| 393 |
% \end{variable} |
| 394 |
% |
| 395 |
% \begin{variable}{\g_@@_write_file_prop} |
| 396 |
% Map user streams with no associated \TeX{} streams to file names. |
| 397 |
% \begin{macrocode} |
| 398 |
\prop_new:N \g_@@_write_file_prop |
| 399 |
% \end{macrocode} |
| 400 |
% \end{variable} |
| 401 |
% |
| 402 |
% \begin{variable}{\l_@@_code_tl} |
| 403 |
% Stores the code to run after finding a user stream, in |
| 404 |
% \cs{@@_get_user:n}. |
| 405 |
% \begin{macrocode} |
| 406 |
\tl_new:N \l_@@_code_tl |
| 407 |
% \end{macrocode} |
| 408 |
% \end{variable} |
| 409 |
% |
| 410 |
% \begin{variable}{\l_@@_user_int, \l_@@_tstr_tl} |
| 411 |
% The user stream number following redefined primitives is stored in |
| 412 |
% \cs{l_@@_user_int} (see \cs{@@_get_user:N}). The corresponding |
| 413 |
% \TeX{} stream number is eventually stored in \cs{l_@@_tstr_tl} (a |
| 414 |
% token list). |
| 415 |
% \begin{macrocode} |
| 416 |
\int_new:N \l_@@_user_int |
| 417 |
\tl_new:N \l_@@_tstr_tl |
| 418 |
% \end{macrocode} |
| 419 |
% \end{variable} |
| 420 |
% |
| 421 |
% \begin{variable}{\l_@@_tstr_token} |
| 422 |
% This token is given as an argument to \cs{@@_tex_newwrite:N}. |
| 423 |
% \begin{macrocode} |
| 424 |
\cs_new_eq:NN \l_@@_tstr_token ? |
| 425 |
% \end{macrocode} |
| 426 |
% \end{variable} |
| 427 |
% |
| 428 |
% \begin{macro}{\s_@@} |
| 429 |
% A recognizable version of \cs{scan_stop:}. This is inspired |
| 430 |
% by\footnote{Historically, this might have happened the other way |
| 431 |
% around, since the author of this package is also on the \LaTeX3 |
| 432 |
% Team.} scan marks (see the \pkg{l3quark} module of \LaTeX3), but |
| 433 |
% \cs{scan_new:N} is not used directly, since it is has been made |
| 434 |
% available in \LaTeX3 too recently. |
| 435 |
% \begin{macrocode} |
| 436 |
\cs_new_eq:NN \s_@@ \scan_stop: |
| 437 |
% \end{macrocode} |
| 438 |
% \end{macro} |
| 439 |
% |
| 440 |
% \begin{variable}{\g_@@_iow, \g_@@_ior} |
| 441 |
% The expansion that \tn{write} performs is impossible to emulate (in \XeTeX{} at least) with |
| 442 |
% anything else than \tn{write}. We will write on the stream |
| 443 |
% \cs{g_@@_iow} to the file \cs{g_@@_tmp_file_tl} and read back from |
| 444 |
% it in the stream \cs{g_@@_ior} for things to work properly. |
| 445 |
% Unfortunately, this means that the file is repeatedly opened and |
| 446 |
% closed, leaving a trace of that in the log. |
| 447 |
% \begin{macrocode} |
| 448 |
\newwrite \g_@@_iow |
| 449 |
\newread \g_@@_ior |
| 450 |
% \end{macrocode} |
| 451 |
% \end{variable} |
| 452 |
% |
| 453 |
% \begin{variable}{\g_@@_tmp_file_tl, \g_@@_tmp_file_bool} |
| 454 |
% Temporary file used to do the correct expansion for each \tn{write}. |
| 455 |
% Boolean indicating whether we have already checked that the file can |
| 456 |
% be used by \pkg{morewrites}: before using a file, the |
| 457 |
% \pkg{morewrites} package now checks it is empty, so as to avoid |
| 458 |
% clobbering user data. |
| 459 |
% \begin{macrocode} |
| 460 |
\tl_new:N \g_@@_tmp_file_tl |
| 461 |
\bool_new:N \g_@@_tmp_file_bool |
| 462 |
\bool_gset_false:N \g_@@_tmp_file_bool |
| 463 |
% \end{macrocode} |
| 464 |
% \end{variable} |
| 465 |
% |
| 466 |
% \begin{variable}{\g_@@_group_level_int} |
| 467 |
% The group level when \tn{shipout} is called: this is used to |
| 468 |
% distinguish between explicit boxes and box registers. |
| 469 |
% \begin{macrocode} |
| 470 |
\int_new:N \g_@@_group_level_int |
| 471 |
% \end{macrocode} |
| 472 |
% \end{variable} |
| 473 |
% |
| 474 |
% \begin{variable}{\g_@@_shipout_box} |
| 475 |
% The page to be shipped out. |
| 476 |
% \begin{macrocode} |
| 477 |
\box_new:N \g_@@_shipout_box |
| 478 |
% \end{macrocode} |
| 479 |
% \end{variable} |
| 480 |
% |
| 481 |
% \subsubsection{Verbosity} |
| 482 |
% |
| 483 |
% \begin{macro}{\@@_verbose:n} |
| 484 |
% Messages to put in the terminal if the \texttt{verbose} option is |
| 485 |
% selected. |
| 486 |
% \begin{macrocode} |
| 487 |
\cs_new_protected:Npn \@@_verbose:n #1 |
| 488 |
{ \bool_if:NT \l_@@_verbose_bool { \iow_term:e { morewrites:~#1 } } } |
| 489 |
% \end{macrocode} |
| 490 |
% \end{macro} |
| 491 |
% |
| 492 |
% \subsubsection{Helpers for auxiliary file} |
| 493 |
% |
| 494 |
% \begin{macro}{\@@_set_file:n} |
| 495 |
% Sets \cs{g_@@_tmp_file_tl} to the given value (initially |
| 496 |
% \cs{c_sys_jobname_str}|.mw|). We do not yet expand, delaying that |
| 497 |
% to the time where we start opening/closing the file, in case |#1| |
| 498 |
% contains something that has not yet been fixed. Mark that the file |
| 499 |
% has not been checked. |
| 500 |
% \begin{macrocode} |
| 501 |
\cs_new_protected:Npn \@@_set_file:n #1 |
| 502 |
{ |
| 503 |
\bool_gset_false:N \g_@@_tmp_file_bool |
| 504 |
\tl_gset:Nn \g_@@_tmp_file_tl {#1} |
| 505 |
} |
| 506 |
% \end{macrocode} |
| 507 |
% \end{macro} |
| 508 |
% |
| 509 |
% \begin{macro}{\@@_empty_file:n} |
| 510 |
% Empties a file by opening it and closing it |
| 511 |
% right away. This is used when performing \tn{immediate} |
| 512 |
% \tn{openout}. It is also used to ensure the file used by |
| 513 |
% \pkg{morewrites} is left empty. We do this every time the auxiliary |
| 514 |
% file is used, in case that run ends with an error mid-document. |
| 515 |
% \begin{macrocode} |
| 516 |
\cs_new_protected:Npn \@@_empty_file:n #1 |
| 517 |
{ |
| 518 |
\@@_tex_immediate:w \@@_tex_openout:w |
| 519 |
\g_@@_iow = {#1} \scan_stop: |
| 520 |
\@@_tex_immediate:w \@@_tex_closeout:w |
| 521 |
\g_@@_iow |
| 522 |
} |
| 523 |
% \end{macrocode} |
| 524 |
% \end{macro} |
| 525 |
% |
| 526 |
% \begin{macro}[TF]{\@@_if_file_trivial:n} |
| 527 |
% True if the file does not exist, or if it is empty. |
| 528 |
% Only the \texttt{TF} variant is defined. We set |
| 529 |
% \cs{@@_tmp:w} to \cs{prg_return_true:} or \cs{prg_return_false:} |
| 530 |
% within the group and use it after cleaning up. The first |
| 531 |
% \texttt{eof} test is \texttt{true} if the file does not exist. |
| 532 |
% Then we read one line, the second \texttt{eof} test is |
| 533 |
% \texttt{true} if the file was empty (it is \texttt{false} if |
| 534 |
% the file contained anything, even a single space). |
| 535 |
% \begin{macrocode} |
| 536 |
\prg_new_conditional:Npnn \@@_if_file_trivial:n #1 { TF } |
| 537 |
{ |
| 538 |
\group_begin: |
| 539 |
\tex_openin:D \g_@@_ior = {#1} |
| 540 |
\if_eof:w \g_@@_ior |
| 541 |
\cs_gset_eq:NN \@@_tmp:w \prg_return_true: |
| 542 |
\else: |
| 543 |
\int_set:Nn \tex_endlinechar:D { -1 } |
| 544 |
\tex_readline:D \g_@@_ior to \l_@@_internal_tl |
| 545 |
\if_eof:w \g_@@_ior |
| 546 |
\cs_gset_eq:NN \@@_tmp:w \prg_return_true: |
| 547 |
\else: |
| 548 |
\cs_gset_eq:NN \@@_tmp:w \prg_return_false: |
| 549 |
\fi: |
| 550 |
\fi: |
| 551 |
\tex_closein:D \g_@@_ior |
| 552 |
\group_end: |
| 553 |
\@@_tmp:w |
| 554 |
} |
| 555 |
% \end{macrocode} |
| 556 |
% \end{macro} |
| 557 |
% |
| 558 |
% \begin{macro}{\@@_chk_file:} |
| 559 |
% Expand the file name in \cs{g_@@_tmp_file_tl} once and for all. |
| 560 |
% Check that the file does not exist or is |
| 561 |
% blank. If not, try another file name obtained as follows: if it |
| 562 |
% ends with |.mw| pick up any number that lies just before |.mw| and |
| 563 |
% increment it, and otherwise just add |.mw| at the end of the file. |
| 564 |
% This avoids clobbering files that the user would not want to lose. |
| 565 |
% \begin{macrocode} |
| 566 |
\cs_new_protected:Npn \@@_chk_file: |
| 567 |
{ |
| 568 |
\tl_gset:Ne \g_@@_tmp_file_tl |
| 569 |
{ \tl_to_str:e { \g_@@_tmp_file_tl } } |
| 570 |
\@@_if_file_trivial:nTF { \g_@@_tmp_file_tl } |
| 571 |
{ \bool_gset_true:N \g_@@_tmp_file_bool } |
| 572 |
{ |
| 573 |
\@@_chk_file_aux: |
| 574 |
\msg_warning:nnxx { morewrites } { file-exists } |
| 575 |
{ \g_@@_tmp_file_tl } { \l_@@_internal_tl } |
| 576 |
\tl_gset_eq:NN \g_@@_tmp_file_tl \l_@@_internal_tl |
| 577 |
\@@_chk_file: |
| 578 |
} |
| 579 |
} |
| 580 |
\cs_new_protected:Npn \@@_chk_file_aux: |
| 581 |
{ |
| 582 |
\regex_extract_once:nVNTF |
| 583 |
{ \A (\D*) (\d*) .mw \Z } \g_@@_tmp_file_tl \l_@@_internal_seq |
| 584 |
{ |
| 585 |
\tl_set:Ne \l_@@_internal_tl |
| 586 |
{ |
| 587 |
\seq_item:Nn \l_@@_internal_seq { 2 } |
| 588 |
\int_eval:n { \seq_item:Nn \l_@@_internal_seq { 3 } + 1 } |
| 589 |
.mw |
| 590 |
} |
| 591 |
} |
| 592 |
{ \tl_set:Ne \l_@@_internal_tl { \g_@@_tmp_file_tl .mw } } |
| 593 |
} |
| 594 |
\msg_new:nnnn { morewrites } { file-exists } |
| 595 |
{ File~'#1'~exists,~using~'#2'~instead. } |
| 596 |
{ |
| 597 |
The~file~`#1'~exists~and~was~not~created~by~this~version~of~the~ |
| 598 |
`morewrites'~package.~Please~move~or~delete~that~file,~or~provide~ |
| 599 |
another~file~name~by~adding |
| 600 |
\\ \\ |
| 601 |
\iow_indent:n { \iow_char:N\\morewritessetup~{~file~=~other-name~} } |
| 602 |
\\ \\ |
| 603 |
to~your~source~file.~In~the~meantime,~the~file~`#2'~will~be~used. |
| 604 |
} |
| 605 |
% \end{macrocode} |
| 606 |
% \end{macro} |
| 607 |
% |
| 608 |
% \subsubsection{Parsing and other helpers} |
| 609 |
% |
| 610 |
% \begin{macro}{\@@_equals_file:N} |
| 611 |
% Most of the parsing for primitive arguments is done using |
| 612 |
% \pkg{primargs}, except for one case we care about: after its |
| 613 |
% \meta{number} argument, the \tn{openout} primitive expects an |
| 614 |
% \meta{equals} (optional spaces and |=|) and a \meta{file name}. |
| 615 |
% \begin{macrocode} |
| 616 |
\cs_new_protected:Npn \@@_equals_file:N #1 |
| 617 |
{ |
| 618 |
\group_begin: |
| 619 |
\tex_aftergroup:D \primargs_get_input_file_name:N |
| 620 |
\tex_aftergroup:D #1 |
| 621 |
\primargs_remove_equals:N \group_end: |
| 622 |
} |
| 623 |
% \end{macrocode} |
| 624 |
% \end{macro} |
| 625 |
% |
| 626 |
% \begin{macro}{\@@_get_user:n} |
| 627 |
% \pkg{primargs} commands only take \texttt{N}-type arguments, but we |
| 628 |
% often need to find an integer, save it in \cs{l_@@_user_int}, and |
| 629 |
% run some code |#1|. This is analogous to \cs{primargs_get_number:N}. |
| 630 |
% \begin{macrocode} |
| 631 |
\cs_new_protected:Npn \@@_get_user:n #1 |
| 632 |
{ |
| 633 |
\tl_set:Nn \l_@@_code_tl {#1} |
| 634 |
\tex_afterassignment:D \l_@@_code_tl |
| 635 |
\l_@@_user_int = |
| 636 |
} |
| 637 |
% \end{macrocode} |
| 638 |
% \end{macro} |
| 639 |
% |
| 640 |
% \begin{macro}{\@@_user_to_tstr:NTF} |
| 641 |
% The goal is to go from a user stream \cs{l_@@_user_int} to a \TeX{} |
| 642 |
% stream \cs{l_@@_tstr_tl} (it defaults to the user stream). Streams |
| 643 |
% less than $129$ are not managed by \pkg{morewrites}: actual \TeX{} |
| 644 |
% streams in $[0,15]$; negative for writing to \texttt{log}; $16$, |
| 645 |
% $17$, $128$ for writing to terminal; $18$ for shell escape. Larger stream |
| 646 |
% numbers are looked up in the property list |#1|, namely |
| 647 |
% \cs{g_@@_write_prop}. If present, use the corresponding value as |
| 648 |
% the \TeX{} stream, otherwise run the \texttt{false} branch. |
| 649 |
% \begin{macrocode} |
| 650 |
\cs_new_protected:Npn \@@_user_to_tstr:NTF #1 |
| 651 |
{ |
| 652 |
\tl_set:NV \l_@@_tstr_tl \l_@@_user_int |
| 653 |
\int_compare:nNnTF { \l_@@_user_int } < { 129 } |
| 654 |
{ \use_i:nn } |
| 655 |
{ \prop_get:NVNTF #1 \l_@@_user_int \l_@@_tstr_tl } |
| 656 |
} |
| 657 |
% \end{macrocode} |
| 658 |
% \end{macro} |
| 659 |
% |
| 660 |
% \begin{variable}{\l_@@_collect_next_int} |
| 661 |
% \begin{macro} |
| 662 |
% { |
| 663 |
% \@@_collect:x, \@@_collect_aux:Nn, |
| 664 |
% \@@_collect_aux:cf, \@@_collect_gput_right:N, \@@_collect_gput_right:c |
| 665 |
% } |
| 666 |
% When encountering very large \tn{write} statements we may need to |
| 667 |
% collect many lines. This can easily become an $O(n^2)$ task, and |
| 668 |
% here we make sure that it remains around $O(n\log n)$, with a large |
| 669 |
% constant unfortunately. Each of the token lists |
| 670 |
% \cs[no-index]{l_@@_$k$_tl} is empty or contains $2^k$ lines. As |
| 671 |
% lines accumulate, they move to token lists with larger values |
| 672 |
% of~$k$, and eventually all are combined. The integer |
| 673 |
% \cs{l_@@_collect_next_int} is (one plus) the maximal $k$ among |
| 674 |
% non-empty token lists. |
| 675 |
% \begin{macrocode} |
| 676 |
\int_new:N \l_@@_collect_next_int |
| 677 |
\cs_new_protected:Npn \@@_collect:x #1 |
| 678 |
{ |
| 679 |
\tl_set:Nx \l_@@_internal_tl {#1} |
| 680 |
\@@_collect_aux:cf { l_@@_0_tl } { 1 } |
| 681 |
} |
| 682 |
\cs_new_protected:Npn \@@_collect_aux:Nn #1#2 |
| 683 |
{ |
| 684 |
\int_compare:nNnT {#2} > \l_@@_collect_next_int |
| 685 |
{ |
| 686 |
\tl_clear_new:N #1 |
| 687 |
\int_set:Nn \l_@@_collect_next_int {#2} |
| 688 |
} |
| 689 |
\tl_if_empty:NTF #1 |
| 690 |
{ \tl_set_eq:NN #1 \l_@@_internal_tl } |
| 691 |
{ |
| 692 |
\tl_put_left:No \l_@@_internal_tl {#1} |
| 693 |
\tl_clear:N #1 |
| 694 |
\@@_collect_aux:cf { l_@@_#2_tl } |
| 695 |
{ \int_eval:n { #2 + 1 } } |
| 696 |
} |
| 697 |
} |
| 698 |
\cs_generate_variant:Nn \@@_collect_aux:Nn { cf } |
| 699 |
\cs_new_protected:Npn \@@_collect_gput_right:N #1 |
| 700 |
{ |
| 701 |
\int_compare:nNnF \l_@@_collect_next_int = 0 |
| 702 |
{ |
| 703 |
\int_decr:N \l_@@_collect_next_int |
| 704 |
\tl_gput_right:Nv #1 |
| 705 |
{ |
| 706 |
l_@@_ |
| 707 |
\int_use:N \l_@@_collect_next_int |
| 708 |
_tl |
| 709 |
} |
| 710 |
\@@_collect_gput_right:N #1 |
| 711 |
} |
| 712 |
} |
| 713 |
\cs_generate_variant:Nn \@@_collect_gput_right:N { c } |
| 714 |
% \end{macrocode} |
| 715 |
% \end{macro} |
| 716 |
% \end{variable} |
| 717 |
% |
| 718 |
% \begin{macro}[EXP]{\@@_user_tl_name:n} |
| 719 |
% The name of a global token list variable holding the text of a given |
| 720 |
% user stream. |
| 721 |
% \begin{macrocode} |
| 722 |
\cs_new:Npn \@@_user_tl_name:n #1 |
| 723 |
{ g_@@_iow_ \int_eval:n {#1} _tl } |
| 724 |
% \end{macrocode} |
| 725 |
% \end{macro} |
| 726 |
% |
| 727 |
% \subsection{Writing} |
| 728 |
% |
| 729 |
% We can hold on to material while a file is being written and only |
| 730 |
% write it in one go once the file closes, to avoid using a stream |
| 731 |
% throughout. |
| 732 |
% |
| 733 |
% At any given time, each user stream may point to an open \TeX{} |
| 734 |
% stream, given in \cs{g_@@_write_prop}, or may point to a token list |
| 735 |
% that will eventually be written to a file whose file name is stored in |
| 736 |
% \cs{g_@@_write_file_prop}, or may be closed. |
| 737 |
% |
| 738 |
% When a user stream points to a token list rather than a \TeX{} stream, |
| 739 |
% any material to be written must be written to our temporary file and |
| 740 |
% read back in to apply the same expansion as \tn{write} does. |
| 741 |
% |
| 742 |
% Another difficulty is that users may mix immediate and non-immediate |
| 743 |
% operations. The biggest difficulty comes from the possibility of |
| 744 |
% copying boxes containing delayed actions. If we ever produced a |
| 745 |
% whatsit \tn{write}\meta{number}\Arg{text} then the \TeX{} stream |
| 746 |
% \meta{number} would have to be reserved forever, as as copies of the |
| 747 |
% box containing this delayed actions may be shipped out at any later |
| 748 |
% point in the document. |
| 749 |
% |
| 750 |
% Each delayed action is thus saved in a separate numbered token list |
| 751 |
% and \tn{write}\cs{g_@@_iow}\Arg{number} is inserted instead of the |
| 752 |
% delayed action. At each \tn{shipout}, the stream \cs{g_@@_iow} is |
| 753 |
% opened, to catch the \meta{number} of each action that should be |
| 754 |
% performed at this \tn{shipout}. |
| 755 |
% |
| 756 |
% \subsubsection{Redefining \tn{immediate}} |
| 757 |
% |
| 758 |
% To accomodate the \tn{immediate} primitive, our versions of |
| 759 |
% \tn{openout}, \tn{write} and \tn{closeout} will take the form |
| 760 |
% \begin{quote} |
| 761 |
% \cs{s_@@} \cs{use_i:nn} |
| 762 |
% \quad \Arg{code for delayed action} \\ |
| 763 |
% \quad \Arg{code for immediate action} \\ |
| 764 |
% \meta{further code} |
| 765 |
% \end{quote} |
| 766 |
% The leading \cs{s_@@} allows the redefined \tn{immediate} to detect |
| 767 |
% these redefined primitives, and to run the \meta{code for immediate |
| 768 |
% action} instead of the \meta{code for delayed action} which is run by |
| 769 |
% default. In both cases, any \meta{further code} is run. |
| 770 |
% |
| 771 |
% \begin{macro}[updated = 2012-12-05]{\@@_immediate:w} |
| 772 |
% \begin{macro}{\@@_immediate_auxii:, \@@_immediate_auxiii:N} |
| 773 |
% \TeX{}'s \tn{immediate} primitive raises a flag which is cancelled |
| 774 |
% after \TeX{} sees a non-expandable token. We use |
| 775 |
% \cs{primargs_read_x_token:N} to find the next non-expandable token |
| 776 |
% then test for \tn{openout}, \tn{write}, and \tn{closeout}. More |
| 777 |
% precisely we test for the marker \cs{s_@@} and run the appropriate |
| 778 |
% code as described above. Otherwise we call the primitive, for cases |
| 779 |
% where the next token is \tn{pdfobj} or similar. In contrived |
| 780 |
% situations involving nonsensical uses of \tn{noexpand} after |
| 781 |
% \tn{immediate}, this code does not perfectly match how \TeX{} |
| 782 |
% expands. |
| 783 |
% \begin{macrocode} |
| 784 |
\cs_new_protected:Npn \@@_immediate:w |
| 785 |
{ \primargs_read_x_token:N \@@_immediate_auxii: } |
| 786 |
\cs_new_protected:Npn \@@_immediate_auxii: |
| 787 |
{ |
| 788 |
\token_if_eq_meaning:NNTF \g_primargs_token \s_@@ |
| 789 |
{ \@@_immediate_auxiii:N } |
| 790 |
{ |
| 791 |
\@@_verbose:n |
| 792 |
{ \tl_to_str:n { \immediate } \token_to_meaning:N \g_primargs_token } |
| 793 |
\@@_tex_immediate:w |
| 794 |
} |
| 795 |
} |
| 796 |
\cs_new_protected:Npn \@@_immediate_auxiii:N #1 |
| 797 |
{ \str_if_eq:nnTF { #1 } { \s_@@ } { \use_iii:nnn } { #1 } } |
| 798 |
% \end{macrocode} |
| 799 |
% \end{macro} |
| 800 |
% \end{macro} |
| 801 |
% |
| 802 |
% \subsubsection{Immediate actions} |
| 803 |
% |
| 804 |
% The \tn{openout}, \tn{write}, and \tn{closeout} primitive can be |
| 805 |
% either delayed or immediate. In all cases they begin by looking for a |
| 806 |
% user stream. In this subsubsection we implement the immediate versions |
| 807 |
% only. |
| 808 |
% |
| 809 |
% \begin{macro}{\@@_closeout:w, \@@_closeout_now:, \@@_closeout_now_silent:, \@@_closeout_now:nn} |
| 810 |
% In the immediate case \cs{@@_closeout_now:}, there are three cases. |
| 811 |
% The stream may point to a \TeX{} stream, in which case it is closed, |
| 812 |
% removed from \cs{g_@@_write_prop}, and put back in the list of |
| 813 |
% usable streams. The stream may point to a token list, in which case |
| 814 |
% that token list should be written to the appropriate file. The |
| 815 |
% stream may be closed, in which case nothing happens. |
| 816 |
% The auxiliary \cs{@@_closeout_now:nn} writes the material collected |
| 817 |
% so far for a given user stream |#1| to the file |#2|. This uses the |
| 818 |
% \TeX{} stream \cs{g_@@_iow}. The token list consists of multiple |
| 819 |
% \tn{immediate} \tn{write} \cs{g_@@_iow} \Arg{text} statements |
| 820 |
% because that is the only safe way to obtain new lines. We do not |
| 821 |
% remove the stream/file pair from \cs{g_@@_write_file_prop}. |
| 822 |
% \begin{macrocode} |
| 823 |
\cs_new_protected:Npn \@@_closeout:w |
| 824 |
{ |
| 825 |
\s_@@ |
| 826 |
\use_i:nn |
| 827 |
{ \@@_get_user:n { \@@_closeout_later: } } |
| 828 |
{ \@@_get_user:n { \@@_closeout_now: } } |
| 829 |
} |
| 830 |
\cs_new_protected:Npn \@@_closeout_now: |
| 831 |
{ |
| 832 |
\@@_verbose:n { \tl_to_str:n { \immediate \closeout } \int_use:N \l_@@_user_int } |
| 833 |
\@@_closeout_now_silent: |
| 834 |
} |
| 835 |
\cs_new_protected:Npn \@@_closeout_now_silent: |
| 836 |
{ |
| 837 |
\@@_user_to_tstr:NTF \g_@@_write_prop |
| 838 |
{ |
| 839 |
\@@_tex_immediate:w \@@_tex_closeout:w \l_@@_tstr_tl \exp_stop_f: |
| 840 |
\int_compare:nNnF { \l_@@_tstr_tl } = { \l_@@_user_int } |
| 841 |
{ |
| 842 |
\prop_gremove:NV \g_@@_write_prop \l_@@_user_int |
| 843 |
\seq_gput_left:NV \g_@@_write_seq \l_@@_tstr_tl |
| 844 |
} |
| 845 |
} |
| 846 |
{ |
| 847 |
\prop_gpop:NVNT \g_@@_write_file_prop \l_@@_user_int \l_@@_internal_tl |
| 848 |
{ \@@_closeout_now:nn { \l_@@_user_int } { \l_@@_internal_tl } } |
| 849 |
} |
| 850 |
} |
| 851 |
\cs_new_protected:Npn \@@_closeout_now:nn #1#2 |
| 852 |
{ |
| 853 |
\@@_tex_immediate:w \@@_tex_openout:w \g_@@_iow = {#2} |
| 854 |
\group_begin: |
| 855 |
\int_set:Nn \tex_newlinechar:D { -1 } |
| 856 |
\tl_use:c { \@@_user_tl_name:n {#1} } |
| 857 |
\tl_gclear:c { \@@_user_tl_name:n {#1} } |
| 858 |
\group_end: |
| 859 |
\@@_tex_immediate:w \@@_tex_closeout:w \g_@@_iow |
| 860 |
} |
| 861 |
% \end{macrocode} |
| 862 |
% \end{macro} |
| 863 |
% |
| 864 |
% \begin{macro}{\@@_openout:w, \@@_openout_now:n, \@@_openout_now_silent:n} |
| 865 |
% In the immediate case find a file name, then allocate a \TeX{} |
| 866 |
% stream if possible, and otherwise point the user stream to a token |
| 867 |
% list. In all cases, close the stream to avoid losing any material |
| 868 |
% that \TeX{} would have written, and empty the file by opening and |
| 869 |
% closing it (actually that's done automatically by the primitive). |
| 870 |
% \begin{macrocode} |
| 871 |
\cs_new_protected:Npn \@@_openout:w |
| 872 |
{ |
| 873 |
\s_@@ |
| 874 |
\use_i:nn |
| 875 |
{ \@@_get_user:n { \@@_openout_later:w } } |
| 876 |
{ \@@_get_user:n { \@@_equals_file:N \@@_openout_now:n } } |
| 877 |
} |
| 878 |
\cs_new_protected:Npn \@@_openout_now:n #1 |
| 879 |
{ |
| 880 |
\@@_verbose:n |
| 881 |
{ |
| 882 |
\tl_to_str:n { \immediate\openout } |
| 883 |
\int_use:N \l_@@_user_int |
| 884 |
\c_space_tl = ~ {#1} |
| 885 |
} |
| 886 |
\@@_openout_now_silent:n {#1} |
| 887 |
} |
| 888 |
\cs_new_protected:Npn \@@_openout_now_silent:n #1 |
| 889 |
{ |
| 890 |
\@@_closeout_now_silent: |
| 891 |
\int_compare:nNnTF { \l_@@_user_int } < { 129 } |
| 892 |
{ |
| 893 |
\@@_tex_immediate:w \@@_tex_openout:w \l_@@_user_int |
| 894 |
= { \tl_to_str:n {#1} } |
| 895 |
} |
| 896 |
{ |
| 897 |
\seq_gpop:NNTF \g_@@_write_seq \l_@@_tstr_tl |
| 898 |
{ |
| 899 |
\prop_gput:NVV \g_@@_write_prop \l_@@_user_int \l_@@_tstr_tl |
| 900 |
\@@_tex_immediate:w \@@_tex_openout:w \l_@@_tstr_tl \exp_stop_f: |
| 901 |
= { \tl_to_str:n {#1} } |
| 902 |
} |
| 903 |
{ |
| 904 |
\@@_empty_file:n {#1} |
| 905 |
\prop_gput:NVx \g_@@_write_file_prop \l_@@_user_int |
| 906 |
{ \tl_to_str:n {#1} } |
| 907 |
\tl_gclear_new:c { \@@_user_tl_name:n { \l_@@_user_int } } |
| 908 |
} |
| 909 |
} |
| 910 |
} |
| 911 |
\sys_if_engine_xetex:T |
| 912 |
{ |
| 913 |
\cs_new_eq:NN \@@_openout_now_silent_aux:n \@@_openout_now_silent:n |
| 914 |
\cs_gset_protected:Npn \@@_openout_now_silent:n #1 |
| 915 |
{ |
| 916 |
\tl_set:Nn \l_@@_internal_tl {#1} |
| 917 |
\tl_remove_all:Nn \l_@@_internal_tl { " } % { " } |
| 918 |
\exp_args:No \@@_openout_now_silent_aux:n \l_@@_internal_tl |
| 919 |
} |
| 920 |
} |
| 921 |
% \end{macrocode} |
| 922 |
% \end{macro} |
| 923 |
% |
| 924 |
% \begin{macro}{\@@_write:w, \@@_write_now:w, \@@_write_now:n} |
| 925 |
% In the immediate case we use \cs{@@_write_now_open:n} if the stream |
| 926 |
% points to a token list, and otherwise use the primitive, with the |
| 927 |
% dummy stream $16$ if closed (the text is then written to the |
| 928 |
% terminal). |
| 929 |
% \begin{macrocode} |
| 930 |
\cs_new_protected:Npn \@@_write:w |
| 931 |
{ |
| 932 |
\s_@@ |
| 933 |
\use_i:nn |
| 934 |
{ \@@_get_user:n { \@@_write_later:w } } |
| 935 |
{ \@@_get_user:n { \@@_write_now:w } } |
| 936 |
} |
| 937 |
\cs_new_protected:Npn \@@_write_now:w |
| 938 |
{ |
| 939 |
\@@_user_to_tstr:NTF \g_@@_write_prop |
| 940 |
{ |
| 941 |
\int_compare:nNnT \l_@@_user_int = { 18 } { \use_iii:nnn } |
| 942 |
\int_compare:nT { -1 < \l_@@_user_int < 16 } |
| 943 |
{ |
| 944 |
\@@_verbose:n |
| 945 |
{ |
| 946 |
\tl_to_str:n { \immediate \write } |
| 947 |
\int_use:N \l_@@_user_int |
| 948 |
} |
| 949 |
} |
| 950 |
\@@_tex_immediate:w \@@_tex_write:w \l_@@_tstr_tl \exp_stop_f: |
| 951 |
} |
| 952 |
{ \primargs_get_general_text:N \@@_write_now:n } |
| 953 |
} |
| 954 |
\cs_new_protected:Npn \@@_write_now:n #1 |
| 955 |
{ |
| 956 |
\prop_get:NVNTF \g_@@_write_file_prop \l_@@_user_int \l_@@_internal_tl |
| 957 |
{ |
| 958 |
\@@_verbose:n |
| 959 |
{ |
| 960 |
\tl_to_str:n { \immediate \write } |
| 961 |
\int_use:N \l_@@_user_int |
| 962 |
\tl_to_str:n { ~ {#1} } |
| 963 |
} |
| 964 |
\@@_write_now_open:n {#1} |
| 965 |
} |
| 966 |
{ |
| 967 |
\@@_verbose:n |
| 968 |
{ |
| 969 |
\tl_to_str:n { \immediate \write } |
| 970 |
\int_use:N \l_@@_user_int |
| 971 |
\tl_to_str:n { ~ (closed) ~ {#1} } |
| 972 |
} |
| 973 |
\@@_tex_immediate:w \@@_tex_write:w 16 {#1} |
| 974 |
} |
| 975 |
} |
| 976 |
% \end{macrocode} |
| 977 |
% \end{macro} |
| 978 |
% |
| 979 |
% \begin{macro}{\@@_write_now_open:n} |
| 980 |
% \begin{macro}{\@@_write_now_loop:} |
| 981 |
% Only \tn{write} itself can emulate how \tn{write} expands tokens, |
| 982 |
% because |#| don't have to be doubled, and because the |
| 983 |
% \tn{newlinechar} has to be changed to new lines. Hence, we start by |
| 984 |
% writing |#1| to a file (after making sure we are allowed to alter |
| 985 |
% it), yielding some lines. The lines are then read one at a time |
| 986 |
% using \eTeX{}'s \tn{readline} with \tn{endlinechar} set to $-1$ to |
| 987 |
% avoid spurious characters. Each line becomes a \tn{immediate} |
| 988 |
% \tn{write} statement added to a token list whose name is constructed |
| 989 |
% using \cs{@@_user_tl_name:n}. This token list will be called when |
| 990 |
% it is time to actually write to the file. At that time, |
| 991 |
% \tn{newlinechar} will be $-1$, so that writing each line will |
| 992 |
% produce no extra line. |
| 993 |
% \begin{macrocode} |
| 994 |
\cs_new_protected:Npn \@@_write_now_open:n #1 |
| 995 |
{ |
| 996 |
\bool_if:NF \g_@@_tmp_file_bool { \@@_chk_file: } |
| 997 |
\@@_tex_immediate:w \@@_tex_openout:w |
| 998 |
\g_@@_iow = { \g_@@_tmp_file_tl } |
| 999 |
\@@_tex_immediate:w \@@_tex_write:w |
| 1000 |
\g_@@_iow {#1} |
| 1001 |
\@@_tex_immediate:w \@@_tex_closeout:w |
| 1002 |
\g_@@_iow |
| 1003 |
\group_begin: |
| 1004 |
\int_set:Nn \tex_endlinechar:D { -1 } |
| 1005 |
\tex_openin:D \g_@@_ior = { \g_@@_tmp_file_tl } |
| 1006 |
\@@_write_now_loop: |
| 1007 |
\tex_closein:D \g_@@_ior |
| 1008 |
\@@_collect_gput_right:c |
| 1009 |
{ \@@_user_tl_name:n { \l_@@_user_int } } |
| 1010 |
\group_end: |
| 1011 |
\@@_empty_file:n { \g_@@_tmp_file_tl } |
| 1012 |
} |
| 1013 |
\cs_new_protected:Npn \@@_write_now_loop: |
| 1014 |
{ |
| 1015 |
\tex_readline:D \g_@@_ior to \l_@@_internal_tl |
| 1016 |
\ior_if_eof:NF \g_@@_ior |
| 1017 |
{ |
| 1018 |
\@@_collect:x |
| 1019 |
{ |
| 1020 |
\@@_tex_immediate:w \@@_tex_write:w |
| 1021 |
\g_@@_iow { \l_@@_internal_tl } |
| 1022 |
} |
| 1023 |
\@@_write_now_loop: |
| 1024 |
} |
| 1025 |
} |
| 1026 |
% \end{macrocode} |
| 1027 |
% \end{macro} |
| 1028 |
% \end{macro} |
| 1029 |
% |
| 1030 |
% |
| 1031 |
% \subsubsection{Delayed actions} |
| 1032 |
% |
| 1033 |
% \begin{macro}{\@@_later:n, \@@_later_do:n} |
| 1034 |
% Store the action to be done at shipout in a token list, and |
| 1035 |
% non-immediately write the label \cs{g_@@_later_int} of the |
| 1036 |
% output operation to the temporary file. |
| 1037 |
% \begin{macrocode} |
| 1038 |
\cs_new_protected:Npn \@@_later:n #1 |
| 1039 |
{ |
| 1040 |
\int_gincr:N \g_@@_later_int |
| 1041 |
\tl_const:cx |
| 1042 |
{ |
| 1043 |
c_@@_later_ |
| 1044 |
\int_use:N \g_@@_later_int |
| 1045 |
_tl |
| 1046 |
} |
| 1047 |
{ |
| 1048 |
\int_set:Nn \exp_not:N \l_@@_user_int |
| 1049 |
{ \exp_not:V \l_@@_user_int } |
| 1050 |
\exp_not:n {#1} |
| 1051 |
} |
| 1052 |
\exp_args:NNx \@@_tex_write:w \g_@@_iow |
| 1053 |
{ `( \int_use:N \g_@@_later_int ) } |
| 1054 |
} |
| 1055 |
\cs_new_protected:Npn \@@_later_do:n #1 |
| 1056 |
{ \tl_use:c { c_@@_later_ \int_eval:n {#1} _tl } } |
| 1057 |
% \end{macrocode} |
| 1058 |
% \end{macro} |
| 1059 |
% |
| 1060 |
% \begin{macro}{\@@_closeout_later:} |
| 1061 |
% If the user stream is a \TeX{} stream, use the primitive, otherwise |
| 1062 |
% save \cs{@@_closeout_now_silent:} for later. |
| 1063 |
% \begin{macrocode} |
| 1064 |
\cs_new_protected:Npn \@@_closeout_later: |
| 1065 |
{ |
| 1066 |
\@@_verbose:n { \tl_to_str:n { \closeout (later) ~ } \int_use:N \l_@@_user_int } |
| 1067 |
\int_compare:nNnTF \l_@@_user_int < { 129 } |
| 1068 |
{ \@@_tex_closeout:w \l_@@_user_int } |
| 1069 |
{ \@@_later:n { \@@_closeout_now_silent: } } |
| 1070 |
} |
| 1071 |
% \end{macrocode} |
| 1072 |
% \end{macro} |
| 1073 |
% |
| 1074 |
% \begin{macro}{\@@_openout_later:w, \@@_openout_later:n} |
| 1075 |
% If the user stream is a \TeX{} stream use the primitive, otherwise |
| 1076 |
% find a file name and call \cs{@@_openout_now_silent:n} later. |
| 1077 |
% \begin{macrocode} |
| 1078 |
\cs_new_protected:Npn \@@_openout_later:w |
| 1079 |
{ |
| 1080 |
\int_compare:nNnTF \l_@@_user_int < { 129 } |
| 1081 |
{ |
| 1082 |
\@@_verbose:n { \tl_to_str:n { \openout (later) ~ } \int_use:N \l_@@_user_int } |
| 1083 |
\@@_tex_openout:w \l_@@_user_int |
| 1084 |
} |
| 1085 |
{ \@@_equals_file:N \@@_openout_later:n } |
| 1086 |
} |
| 1087 |
\cs_new_protected:Npn \@@_openout_later:n #1 |
| 1088 |
{ |
| 1089 |
\@@_verbose:n |
| 1090 |
{ |
| 1091 |
\tl_to_str:n { \openout (later)~ } |
| 1092 |
\int_use:N \l_@@_user_int |
| 1093 |
\c_space_tl = ~ {#1} |
| 1094 |
} |
| 1095 |
\@@_later:n { \@@_openout_now_silent:n {#1} } |
| 1096 |
} |
| 1097 |
% \end{macrocode} |
| 1098 |
% \end{macro} |
| 1099 |
% |
| 1100 |
% \begin{macro}{\@@_write_later:w, \@@_write_later:n, \@@_write_later_aux:n} |
| 1101 |
% For \TeX{} streams use the primitive, otherwise find a general text |
| 1102 |
% and save it for later; the auxiliary is very similar to |
| 1103 |
% \cs{@@_write_now:w}. |
| 1104 |
% \begin{macrocode} |
| 1105 |
\cs_new_protected:Npn \@@_write_later:w |
| 1106 |
{ |
| 1107 |
\int_compare:nNnTF \l_@@_user_int < { 129 } |
| 1108 |
{ |
| 1109 |
\@@_verbose:n { \tl_to_str:n { \write (later)~ } \int_use:N \l_@@_user_int } |
| 1110 |
\@@_tex_write:w \l_@@_user_int |
| 1111 |
} |
| 1112 |
{ \primargs_get_general_text:N \@@_write_later:n } |
| 1113 |
} |
| 1114 |
\cs_new_protected:Npn \@@_write_later:n #1 |
| 1115 |
{ |
| 1116 |
\@@_verbose:n |
| 1117 |
{ |
| 1118 |
\tl_to_str:n { \write (later)~ } |
| 1119 |
\int_use:N \l_@@_user_int |
| 1120 |
\tl_to_str:n { ~ {#1} } |
| 1121 |
} |
| 1122 |
\@@_later:n { \@@_write_later_aux:n {#1} } |
| 1123 |
} |
| 1124 |
\cs_new_protected:Npn \@@_write_later_aux:n |
| 1125 |
{ |
| 1126 |
\@@_user_to_tstr:NTF \g_@@_write_prop |
| 1127 |
{ \@@_tex_immediate:w \@@_tex_write:w \l_@@_tstr_tl \exp_stop_f: } |
| 1128 |
{ |
| 1129 |
\prop_get:NVNTF \g_@@_write_file_prop \l_@@_user_int \l_@@_internal_tl |
| 1130 |
{ \@@_write_now_open:n } |
| 1131 |
{ \@@_tex_immediate:w \@@_tex_write:w 16 \exp_stop_f: } |
| 1132 |
} |
| 1133 |
} |
| 1134 |
% \end{macrocode} |
| 1135 |
% \end{macro} |
| 1136 |
% |
| 1137 |
% \subsubsection{Shipout business} |
| 1138 |
% |
| 1139 |
% In this section, we hook into the \tn{shipout} primitive, and redefine |
| 1140 |
% it to first build a box with the material to ship out, then perform |
| 1141 |
% \begin{quote} |
| 1142 |
% \cs{@@_before_shipout:} \\ |
| 1143 |
% \meta{primitive shipout} \meta{collected box} \\ |
| 1144 |
% \cs{@@_after_shipout:} |
| 1145 |
% \end{quote} |
| 1146 |
% |
| 1147 |
% Each delayed output operation has been replaced by \tn{write} |
| 1148 |
% \cs{g_@@_iow} |{`(|\meta{operation number}|)}|. The delimiters we |
| 1149 |
% chose to put around numbers must be at least two distinct characters |
| 1150 |
% on the left (then \cs{tex_newlinechar:D} cannot be equal to the |
| 1151 |
% delimiter), and at least one non-digit character on the right. |
| 1152 |
% |
| 1153 |
% \begin{macro}{\@@_before_shipout:} |
| 1154 |
% Immediately before the shipout, we must open the writing stream |
| 1155 |
% \cs{g_@@_iow} (after making sure we are allowed to alter the |
| 1156 |
% auxiliary file). |
| 1157 |
% \begin{macrocode} |
| 1158 |
\cs_new_protected:Npn \@@_before_shipout: |
| 1159 |
{ |
| 1160 |
\bool_if:NF \g_@@_tmp_file_bool { \@@_chk_file: } |
| 1161 |
\@@_tex_immediate:w \@@_tex_openout:w |
| 1162 |
\g_@@_iow = { \g_@@_tmp_file_tl } |
| 1163 |
} |
| 1164 |
% \end{macrocode} |
| 1165 |
% \end{macro} |
| 1166 |
% |
| 1167 |
% \begin{macro}{\@@_after_shipout:} |
| 1168 |
% \begin{macro}[rEXP]{\@@_after_shipout_loop:ww} |
| 1169 |
% Immediately after all the \tn{write}s are performed, close the file, |
| 1170 |
% then read the file with \tn{endlinechar} set to |
| 1171 |
% \tn{newlinechar}\footnote{Note that the \tn{newlinechar} used by |
| 1172 |
% \tn{write}s at \tn{shipout} time are those in effect when the page |
| 1173 |
% is shipped out, \emph{i.e.}, just after the closing brace of the |
| 1174 |
% \tn{shipout} construction, which is exactly where we have added |
| 1175 |
% this hook.} to get exactly the original characters that have been |
| 1176 |
% written, possibly with extra characters between |`(|\ldots{}|)| |
| 1177 |
% groups. The file is then read with all the appropriate category |
| 1178 |
% codes set up (no other character can appear in the file). The |
| 1179 |
% looping auxiliary \cs{@@_after_shipout_loop:ww} extracts the |
| 1180 |
% \meta{operation} numbers from the file, and makes a token list out |
| 1181 |
% of those. This token list is then used in a mapping function to |
| 1182 |
% perform the appropriate \tn{write} operations. Note that those |
| 1183 |
% operations may reuse the file, so we have to fully parse the file |
| 1184 |
% before moving on. |
| 1185 |
% \begin{macrocode} |
| 1186 |
\cs_new_protected:Npn \@@_after_shipout: |
| 1187 |
{ |
| 1188 |
\@@_tex_immediate:w \@@_tex_closeout:w |
| 1189 |
\g_@@_iow |
| 1190 |
\group_begin: |
| 1191 |
\int_set_eq:NN \tex_endlinechar:D \tex_newlinechar:D |
| 1192 |
\char_set_catcode_other:n { \tex_endlinechar:D } |
| 1193 |
\tl_map_inline:nn { `(0123456789) } |
| 1194 |
{ \char_set_catcode_other:n {`##1} } |
| 1195 |
\tex_everyeof:D { `() \exp_not:N } |
| 1196 |
\tl_set:Nx \l_@@_internal_tl |
| 1197 |
{ |
| 1198 |
\exp_after:wN \@@_after_shipout_loop:ww |
| 1199 |
\tex_input:D { \g_@@_tmp_file_tl } |
| 1200 |
} |
| 1201 |
\@@_empty_file:n { \g_@@_tmp_file_tl } |
| 1202 |
\exp_args:NNo |
| 1203 |
\group_end: |
| 1204 |
\tl_map_function:nN { \l_@@_internal_tl } \@@_later_do:n |
| 1205 |
} |
| 1206 |
\cs_new:Npn \@@_after_shipout_loop:ww #1 `( #2 ) |
| 1207 |
{ |
| 1208 |
\tl_if_empty:nF {#2} |
| 1209 |
{ |
| 1210 |
{#2} |
| 1211 |
\@@_after_shipout_loop:ww |
| 1212 |
} |
| 1213 |
} |
| 1214 |
% \end{macrocode} |
| 1215 |
% \end{macro} |
| 1216 |
% \end{macro} |
| 1217 |
% |
| 1218 |
% \begin{macro}{\@@_shipout:w} |
| 1219 |
% \begin{macro}{\@@_shipout_i:, \@@_shipout_ii:} |
| 1220 |
% Grab the shipped out box using \tn{setbox} and regain control using |
| 1221 |
% \tn{afterassignment}. There are two cases: either the box is given |
| 1222 |
% as \tn{box} or \tn{copy} followed by a number, in which case |
| 1223 |
% \cs{@@_shipout_i:} is inserted afterwards at the same group level, |
| 1224 |
% or the box is given as \tn{hbox} (or \tn{vtop} and so on) and an |
| 1225 |
% additional \tn{aftergroup} is needed to reach a point where we can |
| 1226 |
% use the box saved in \cs{g_@@_shipout_box}. |
| 1227 |
% \begin{macrocode} |
| 1228 |
\cs_new_protected:Npn \@@_shipout:w |
| 1229 |
{ |
| 1230 |
\int_gset_eq:NN \g_@@_group_level_int \tex_currentgrouplevel:D |
| 1231 |
\tex_afterassignment:D \@@_shipout_i: |
| 1232 |
\tex_global:D \tex_setbox:D \g_@@_shipout_box |
| 1233 |
} |
| 1234 |
\cs_new_protected:Npn \@@_shipout_i: |
| 1235 |
{ |
| 1236 |
\int_compare:nNnTF { \g_@@_group_level_int } |
| 1237 |
= { \tex_currentgrouplevel:D } |
| 1238 |
{ \@@_shipout_ii: } |
| 1239 |
{ \tex_aftergroup:D \@@_shipout_ii: } |
| 1240 |
} |
| 1241 |
\cs_new_protected:Npn \@@_shipout_ii: |
| 1242 |
{ |
| 1243 |
\@@_before_shipout: |
| 1244 |
\@@_tex_shipout:w \tex_box:D \g_@@_shipout_box |
| 1245 |
\@@_after_shipout: |
| 1246 |
} |
| 1247 |
% \end{macrocode} |
| 1248 |
% \end{macro} |
| 1249 |
% \end{macro} |
| 1250 |
% |
| 1251 |
% \begin{macro}{\shipout, \@@_tex_shipout:w} |
| 1252 |
% The task is now to locate the shipout primitive, which may have been |
| 1253 |
% renamed and hooked into by many different packages loaded before |
| 1254 |
% \pkg{morewrites}. Any of those control sequences which are equal to |
| 1255 |
% the primitive are redefined to do \cs{@@_shipout:w} instead. If the |
| 1256 |
% primitive is not located at all, the fallback is to hook into the |
| 1257 |
% control sequence \tn{shipout}. |
| 1258 |
% \begin{macrocode} |
| 1259 |
\cs_gset_protected:Npn \@@_tmp:w #1 |
| 1260 |
{ |
| 1261 |
\cs_if_exist:NF \@@_tex_shipout:w |
| 1262 |
{ \cs_new_eq:NN \@@_tex_shipout:w #1 } |
| 1263 |
\cs_gset_eq:NN #1 \@@_shipout:w |
| 1264 |
} |
| 1265 |
\tl_map_inline:nn |
| 1266 |
{ |
| 1267 |
\xyrealshipout@ |
| 1268 |
\org@shipout |
| 1269 |
\PDFSYNCship@ut@ld |
| 1270 |
\CROP@shipout |
| 1271 |
\@soORI |
| 1272 |
\tex_shipout:D |
| 1273 |
\zwpl@Hship |
| 1274 |
\o@shipout@TP |
| 1275 |
\LL@shipout |
| 1276 |
\Shipout |
| 1277 |
\GXTorg@shipout |
| 1278 |
\AtBegShi@OrgShipout |
| 1279 |
\AtBeginShipoutOriginalShipout |
| 1280 |
\minidocument@orig@shipout |
| 1281 |
\shipout |
| 1282 |
} |
| 1283 |
{ |
| 1284 |
\str_if_eq:eeT |
| 1285 |
{ \cs_meaning:N #1 } |
| 1286 |
{ \token_to_str:N \shipout } |
| 1287 |
{ \@@_tmp:w #1 } |
| 1288 |
} |
| 1289 |
\cs_if_exist:NF \@@_tex_shipout:w |
| 1290 |
{ |
| 1291 |
\cs_new_eq:NN \@@_tex_shipout:w \shipout |
| 1292 |
\cs_gset_eq:NN \shipout \@@_shipout:w |
| 1293 |
} |
| 1294 |
% \end{macrocode} |
| 1295 |
% \end{macro} |
| 1296 |
% |
| 1297 |
% \subsubsection{Hook at the very end} |
| 1298 |
% |
| 1299 |
% \begin{macro}{\@@_close_all:} |
| 1300 |
% At the end of the document, close all the files. |
| 1301 |
% \begin{macrocode} |
| 1302 |
\cs_new_protected:Npn \@@_close_all: |
| 1303 |
{ |
| 1304 |
\prop_map_inline:Nn \g_@@_write_prop |
| 1305 |
{ |
| 1306 |
\@@_verbose:n { \tl_to_str:n { \immediate \closeout } ##1 ~ (at~end) } |
| 1307 |
\@@_tex_immediate:w \@@_tex_closeout:w ##2 \scan_stop: |
| 1308 |
} |
| 1309 |
\prop_gclear:N \g_@@_write_prop |
| 1310 |
\prop_map_function:NN \g_@@_write_file_prop |
| 1311 |
\@@_closeout_now:nn |
| 1312 |
\prop_gclear:N \g_@@_write_file_prop |
| 1313 |
} |
| 1314 |
% \end{macrocode} |
| 1315 |
% \end{macro} |
| 1316 |
% |
| 1317 |
% \begin{macro}{\@@_close_all_at_end:nw} |
| 1318 |
% At the end of the run, we try very hard to put some material at the |
| 1319 |
% \tn{@@end}, just in case some other very late code writes to files |
| 1320 |
% that are not yet closed. This is tried at most $5$~times, to avoid |
| 1321 |
% infinite loops in case two packages compete for that last place. |
| 1322 |
% The four |@| become two after \pkg{l3docstrip}. |
| 1323 |
% \begin{macrocode} |
| 1324 |
\cs_new_protected:Npn \@@_close_all_at_end:nw #1#2 \@@@@end |
| 1325 |
{ |
| 1326 |
\int_compare:nNnTF {#1} > 0 |
| 1327 |
{ #2 \@@_close_all_at_end:nw { #1 - 1 } } |
| 1328 |
{ \@@_close_all: #2 } |
| 1329 |
\@@@@end |
| 1330 |
} |
| 1331 |
\AtEndDocument { \@@_close_all_at_end:nw { 5 } } |
| 1332 |
% \end{macrocode} |
| 1333 |
% \end{macro} |
| 1334 |
% |
| 1335 |
% \subsection{Redefining commands} |
| 1336 |
% |
| 1337 |
% \subsubsection{Modified \tn{newwrite}} |
| 1338 |
% |
| 1339 |
% \begin{variable}{\g_@@_alloc_write_int} |
| 1340 |
% Counter to allocate user streams. We used to initialize it to $18$ |
| 1341 |
% so that the first user stream allocated by \pkg{morewrites} was |
| 1342 |
% $19$. Indeed, $18$ is reserved for shell commands and packages may |
| 1343 |
% expect $16$ or $17$ to write to the terminal. This is now changed |
| 1344 |
% to start allocation at $129$, since some packages that do not want |
| 1345 |
% to distinguish \LuaTeX{} from other engines simply use $128$ as a |
| 1346 |
% never-open stream. |
| 1347 |
% \begin{macrocode} |
| 1348 |
\int_new:N \g_@@_alloc_write_int |
| 1349 |
\int_gset:Nn \g_@@_alloc_write_int { 128 } |
| 1350 |
% \end{macrocode} |
| 1351 |
% \end{variable} |
| 1352 |
% |
| 1353 |
% \begin{macro}{\@@_newwrite:N} |
| 1354 |
% Reimplementation of \tn{newwrite} but protected and using a counter |
| 1355 |
% \cs{g_@@_alloc_write_int} instead of what \TeX{}/\LaTeXe{} use. |
| 1356 |
% \begin{macrocode} |
| 1357 |
\cs_new_protected:Npn \@@_newwrite:N #1 |
| 1358 |
{ |
| 1359 |
\int_gincr:N \g_@@_alloc_write_int |
| 1360 |
\int_set_eq:NN \allocationnumber \g_@@_alloc_write_int |
| 1361 |
\cs_undefine:N #1 |
| 1362 |
\int_const:Nn #1 { \allocationnumber } |
| 1363 |
\wlog |
| 1364 |
{ |
| 1365 |
\token_to_str:N #1 |
| 1366 |
= \token_to_str:N \write \int_use:N \allocationnumber |
| 1367 |
} |
| 1368 |
} |
| 1369 |
% \end{macrocode} |
| 1370 |
% \end{macro} |
| 1371 |
% |
| 1372 |
% \begin{macro}{\@@_allocate:n} |
| 1373 |
% Raise to |#1| the number of \tn{write} streams allocated to \pkg{morewrites}. |
| 1374 |
% \begin{macrocode} |
| 1375 |
\cs_new_protected:Npn \@@_allocate:n #1 |
| 1376 |
{ |
| 1377 |
\prg_replicate:nn |
| 1378 |
{ |
| 1379 |
\int_max:nn { 0 } |
| 1380 |
{ |
| 1381 |
(#1) - \seq_count:N \g_@@_write_seq |
| 1382 |
- \prop_count:N \g_@@_write_prop |
| 1383 |
} |
| 1384 |
} |
| 1385 |
{ |
| 1386 |
\@@_tex_newwrite:N \l_@@_tstr_token |
| 1387 |
\seq_gput_right:NV \g_@@_write_seq \l_@@_tstr_token |
| 1388 |
} |
| 1389 |
} |
| 1390 |
% \end{macrocode} |
| 1391 |
% \end{macro} |
| 1392 |
% |
| 1393 |
% \subsection{User commands and keys} |
| 1394 |
% |
| 1395 |
% \begin{macro}[added = 2014-07-26]{\morewritessetup} |
| 1396 |
% Set whatever keys the user passes to \cs{morewritessetup}. |
| 1397 |
% \begin{macrocode} |
| 1398 |
\cs_new_protected:Npn \morewritessetup #1 |
| 1399 |
{ \keys_set:nn { @@ } {#1} } |
| 1400 |
% \end{macrocode} |
| 1401 |
% \end{macro} |
| 1402 |
% |
| 1403 |
% \begin{macro}[added = 2014-07-26]{file} |
| 1404 |
% Because of our use of |.initial:n|, this code must appear after |
| 1405 |
% \cs{@@_set_file:n} is defined. |
| 1406 |
% \begin{macrocode} |
| 1407 |
\keys_define:nn { @@ } |
| 1408 |
{ |
| 1409 |
allocate .code:n = \@@_allocate:n {#1} , |
| 1410 |
file .code:n = \@@_set_file:n {#1} , |
| 1411 |
file .initial:n = \c_sys_jobname_str .mw , |
| 1412 |
verbose .bool_set:N = \l_@@_verbose_bool |
| 1413 |
} |
| 1414 |
% \end{macrocode} |
| 1415 |
% \end{macro} |
| 1416 |
% |
| 1417 |
% \begin{macro}[updated = 2015-08-01] |
| 1418 |
% {\immediate, \openout, \write, \closeout, \newwrite} |
| 1419 |
% \begin{macrocode} |
| 1420 |
\cs_gset_eq:NN \immediate \@@_immediate:w |
| 1421 |
\cs_gset_eq:NN \openout \@@_openout:w |
| 1422 |
\cs_gset_eq:NN \write \@@_write:w |
| 1423 |
\cs_gset_eq:NN \closeout \@@_closeout:w |
| 1424 |
\cs_gset_eq:NN \newwrite \@@_newwrite:N |
| 1425 |
% \end{macrocode} |
| 1426 |
% \end{macro} |
| 1427 |
% |
| 1428 |
%</package> |
| 1429 |
% |
| 1430 |
% \end{implementation} |
| 1431 |
% |
| 1432 |
% \clearpage |
| 1433 |
% \PrintIndex |