Debian Codegraph @

texmf-dist/source/latex/l3kernel/l3file.dtx

raw
1 % \iffalse meta-comment
2 %
3 %% File: l3file.dtx
4 %
5 % Copyright (C) 1990-2026 The LaTeX Project
6 %
7 % It may be distributed and/or modified under the conditions of the
8 % LaTeX Project Public License (LPPL), either version 1.3c of this
9 % license or (at your option) any later version. The latest version
10 % of this license is in the file
11 %
12 % https://www.latex-project.org/lppl.txt
13 %
14 % This file is part of the "l3kernel bundle" (The Work in LPPL)
15 % and all files in that bundle must be distributed together.
16 %
17 % -----------------------------------------------------------------------
18 %
19 % The development version of the bundle can be found at
20 %
21 % https://github.com/latex3/latex3
22 %
23 % for those people who are interested.
24 %
25 %<*driver>
26 \documentclass[full,kernel]{l3doc}
27 \begin{document}
28 \DocInput{\jobname.dtx}
29 \PrintIndex
30 \end{document}
31 %</driver>
32 % \fi
33 %
34 % \title{^^A
35 % The \pkg{l3file} module\\ File and I/O operations^^A
36 % }
37 %
38 % \author{^^A
39 % The \LaTeX{} Project\thanks
40 % {^^A
41 % E-mail:
42 % \href{mailto:[email protected]}
43 % {[email protected]}^^A
44 % }^^A
45 % }
46 %
47 % \date{Released 2026-05-15}
48 %
49 % \maketitle
50 %
51 % \begin{documentation}
52 %
53 % This module provides functions for working with external files. Some of these
54 % functions apply to an entire file, and have prefix \cs[no-index]{file_\ldots}, while
55 % others are used to work with files on a line by line basis and have prefix
56 % \cs[no-index]{ior_\ldots} (reading) or \cs[no-index]{iow_\ldots} (writing).
57 %
58 % It is important to remember that when reading external files \TeX{}
59 % attempts to locate them using both the operating system path and entries in the
60 % \TeX{} file database (most \TeX{} systems use such a database). Thus the
61 % \enquote{current path} for \TeX{} is somewhat broader than that for other
62 % programs.
63 %
64 % For functions which expect a \meta{file name} argument, this argument
65 % may contain both literal items and expandable content, which should on
66 % full expansion be the desired file name. Active characters (as
67 % declared in \cs{l_char_active_seq}) are \emph{not} expanded,
68 % allowing the direct use of these in file names. Quote tokens (|"|) are
69 % not permitted in file names as they are reserved for internal use by some
70 % \TeX{} primitives.
71 %
72 % Spaces are trimmed at the beginning and end of the file name:
73 % this reflects the fact that some file systems do not allow or interact
74 % unpredictably with spaces in these positions. When no extension is given,
75 % this will trim spaces from the start of the name only.
76 %
77 % \section{Input--output stream management}
78 %
79 % As \TeX{} engines have a limited number of input and output streams, direct
80 % use of the streams by the programmer is not supported in \LaTeX3. Instead, an
81 % internal pool of streams is maintained, and these are allocated and
82 % deallocated as needed by other modules. As a result, the programmer should
83 % close streams when they are no longer needed, to release them for other
84 % processes.
85 %
86 % Note that I/O operations are global: streams should all be declared
87 % with global names and treated accordingly.
88 %
89 % \begin{function}[added = 2011-09-26, updated = 2011-12-27]
90 % {\ior_new:N, \ior_new:c, \iow_new:N, \iow_new:c}
91 % \begin{syntax}
92 % \cs{ior_new:N} \meta{stream}
93 % \cs{iow_new:N} \meta{stream}
94 % \end{syntax}
95 % Globally reserves the name of the \meta{stream}, either for reading
96 % or for writing as appropriate. The \meta{stream} is not opened until
97 % the appropriate \cs[no-index]{\ldots_open:Nn} function is used. Attempting to
98 % use a \meta{stream} which has not been opened is an error, and the
99 % \meta{stream} will behave as the corresponding \cs[no-index]{c_term_\ldots}.
100 % \end{function}
101 %
102 % \begin{function}[updated = 2012-02-10]{\ior_open:Nn, \ior_open:cn}
103 % \begin{syntax}
104 % \cs{ior_open:Nn} \meta{stream} \Arg{file name}
105 % \end{syntax}
106 % Opens \meta{file name} for reading using \meta{stream} as the
107 % control sequence for file access. If the \meta{stream} was already
108 % open it is closed before the new operation begins. The
109 % \meta{stream} is available for access immediately and will remain
110 % allocated to \meta{file name} until an \cs{ior_close:N} instruction
111 % is given or the \TeX{} run ends.
112 % If the file is not found, an error is raised.
113 % \end{function}
114 %
115 % \begin{function}[added = 2013-01-12, TF]{\ior_open:Nn, \ior_open:cn}
116 % \begin{syntax}
117 % \cs{ior_open:NnTF} \meta{stream} \Arg{file name} \Arg{true code} \Arg{false code}
118 % \end{syntax}
119 % Opens \meta{file name} for reading using \meta{stream} as the
120 % control sequence for file access. If the \meta{stream} was already
121 % open it is closed before the new operation begins. The
122 % \meta{stream} is available for access immediately and will remain
123 % allocated to \meta{file name} until a \cs{ior_close:N} instruction
124 % is given or the \TeX{} run ends. The \meta{true code} is then inserted
125 % into the input stream. If the file is not found, no error is raised and
126 % the \meta{false code} is inserted into the input stream.
127 % \end{function}
128 %
129 % \begin{function}[updated = 2012-02-09]
130 % {\iow_open:Nn, \iow_open:NV, \iow_open:cn, \iow_open:cV}
131 % \begin{syntax}
132 % \cs{iow_open:Nn} \meta{stream} \Arg{file name}
133 % \end{syntax}
134 % Opens \meta{file name} for writing using \meta{stream} as the
135 % control sequence for file access. If the \meta{stream} was already
136 % open it is closed before the new operation begins. The
137 % \meta{stream} is available for access immediately and will remain
138 % allocated to \meta{file name} until a \cs{iow_close:N} instruction
139 % is given or the \TeX{} run ends. Opening a file for writing clears
140 % any existing content in the file (i.e., writing is \emph{not}
141 % additive).
142 % \end{function}
143 %
144 % \begin{function}[added = 2019-05-08]{\ior_shell_open:Nn}
145 % \begin{syntax}
146 % \cs{ior_shell_open:Nn} \meta{stream} \Arg{shell~command}
147 % \end{syntax}
148 % Opens the \emph{pseudo}-file created by the output of the
149 % \meta{shell command} for reading using \meta{stream} as the
150 % control sequence for access. If the \meta{stream} was already
151 % open it is closed before the new operation begins. The
152 % \meta{stream} is available for access immediately and will remain
153 % allocated to \meta{shell command} until a \cs{ior_close:N} instruction
154 % is given or the \TeX{} run ends.
155 % If piped system calls are disabled an error is raised.
156 %
157 % For details of handling of the \meta{shell command}, see
158 % \cs{sys_get_shell:nnNTF}.
159 % \end{function}
160 %
161 % \begin{function}[added = 2023-05-25]{\iow_shell_open:Nn}
162 % \begin{syntax}
163 % \cs{iow_shell_open:Nn} \meta{stream} \Arg{shell~command}
164 % \end{syntax}
165 % Opens the \emph{pseudo}-file created by the output of the
166 % \meta{shell command} for writing using \meta{stream} as the
167 % control sequence for access. If the \meta{stream} was already
168 % open it is closed before the new operation begins. The
169 % \meta{stream} is available for access immediately and will remain
170 % allocated to \meta{shell command} until an \cs{iow_close:N} instruction
171 % is given or the \TeX{} run ends.
172 % If piped system calls are disabled an error is raised.
173 %
174 % For details of handling of the \meta{shell command}, see
175 % \cs{sys_get_shell:nnNTF}.
176 % \end{function}
177 %
178 % \begin{function}[updated = 2012-07-31]
179 % {\ior_close:N, \ior_close:c, \iow_close:N, \iow_close:c}
180 % \begin{syntax}
181 % \cs{ior_close:N} \meta{stream}
182 % \cs{iow_close:N} \meta{stream}
183 % \end{syntax}
184 % Closes the \meta{stream}. Streams should always be closed when
185 % they are finished with as this ensures that they remain available
186 % to other programmers.
187 % \end{function}
188 %
189 % \begin{function}[added = 2021-05-11]
190 % {
191 % \ior_show:N, \ior_show:c, \ior_log:N, \ior_log:c,
192 % \iow_show:N, \iow_show:c, \iow_log:N, \iow_log:c
193 % }
194 % \begin{syntax}
195 % \cs{ior_show:N} \meta{stream}
196 % \cs{ior_log:N} \meta{stream}
197 % \cs{iow_show:N} \meta{stream}
198 % \cs{iow_log:N} \meta{stream}
199 % \end{syntax}
200 % Display (to the terminal or log file) the file name associated to
201 % the (read or write) \meta{stream}.
202 % \end{function}
203 %
204 % \begin{function}[added = 2017-06-27]
205 % {
206 % \ior_show_list:, \ior_log_list:,
207 % \iow_show_list:, \iow_log_list:
208 % }
209 % \begin{syntax}
210 % \cs{ior_show_list:}
211 % \cs{ior_log_list:}
212 % \cs{iow_show_list:}
213 % \cs{iow_log_list:}
214 % \end{syntax}
215 % Display (to the terminal or log file) a list of the file names
216 % associated with each open (read or write) stream. This is intended
217 % for tracking down problems.
218 % \end{function}
219 %
220 % \subsection{Reading from files}
221 %
222 % Reading from files and reading from the terminal are separate processes in
223 % \pkg{expl3}. The functions \cs{ior_get:NN} and \cs{ior_str_get:NN}, and their
224 % branching equivalents, are designed to work with \emph{files}.
225 %
226 % \begin{function}[noTF, added = 2012-06-24, updated = 2019-03-23]{\ior_get:NN}
227 % \begin{syntax}
228 % \cs{ior_get:NN} \meta{stream} \meta{tl~var}
229 % \cs{ior_get:NNTF} \meta{stream} \meta{tl~var} \Arg{true code} \Arg{false code}
230 % \end{syntax}
231 % Function that reads one or more lines (until an equal number of left
232 % and right braces are found) from the file input \meta{stream} and stores
233 % the result locally in the \meta{token list} variable.
234 % The material read from the \meta{stream} is tokenized by \TeX{}
235 % according to the category codes and \tn{endlinechar} in force when
236 % the function is used. Assuming normal settings, any lines which do
237 % not end in a comment character~|%| have the line ending
238 % converted to a space, so for example input
239 % \begin{verbatim}
240 % a b c
241 % \end{verbatim}
242 % results in a token list \verb*|a b c |. Any blank line is
243 % converted to the token \cs{par}. Therefore, blank lines can be
244 % skipped by using a test such as
245 % \begin{verbatim}
246 % \ior_get:NN \g_my_ior \l_tmpa_tl
247 % \tl_set:Nn \l_tmpb_tl { \par }
248 % \tl_if_eq:NNF \l_tmpa_tl \l_tmpb_tl
249 % ...
250 % \end{verbatim}
251 % Also notice that if multiple lines are read to match braces
252 % then the resulting token list can contain \cs{par} tokens.
253 % In the non-branching version, where the \meta{stream} is not open
254 % the \meta{tl var} is set to \cs{q_no_value}.
255 % \begin{texnote}
256 % This protected macro is a wrapper around the \TeX{} primitive
257 % \tn{read}. Regardless of settings, \TeX{} replaces trailing space
258 % and tab characters (character codes 32 and~9) in each line by an
259 % end-of-line character (character code \tn{endlinechar}, omitted if
260 % \tn{endlinechar} is negative or too large) before turning
261 % characters into tokens according to current category codes. With
262 % default settings, spaces appearing at the beginning of lines are
263 % also ignored.
264 % \end{texnote}
265 % \end{function}
266 %
267 % \begin{function}[noTF, added = 2016-12-04, updated = 2019-03-23]
268 % {\ior_str_get:NN}
269 % \begin{syntax}
270 % \cs{ior_str_get:NN} \meta{stream} \meta{tl~var}
271 % \cs{ior_str_get:NNTF} \meta{stream} \meta{tl~var} \Arg{true code} \Arg{false code}
272 % \end{syntax}
273 % Function that reads one line from the file input \meta{stream} and stores
274 % the result locally in the \meta{token list} variable.
275 % The material is read from the \meta{stream} as a series of tokens with
276 % category code $12$ (other), with the exception of space
277 % characters which are given category code $10$ (space).
278 % Multiple whitespace characters are retained by this process. It
279 % always only reads one line and any blank lines in the input
280 % result in the \meta{tl~var} being empty. Unlike
281 % \cs{ior_get:NN}, line ends do not receive any special treatment. Thus
282 % input
283 % \begin{verbatim}
284 % a b c
285 % \end{verbatim}
286 % results in a token list |a b c| with the letters |a|, |b|, and |c|
287 % having category code~12.
288 % In the non-branching version, where the\meta{stream} is not open
289 % the \meta{tl var} is set to \cs{q_no_value}.
290 % \begin{texnote}
291 % This protected macro is a wrapper around the \eTeX{} primitive
292 % \tn{readline}. Regardless of settings, \TeX{} removes trailing
293 % space and tab characters (character codes 32 and~9). However, the
294 % end-line character normally added by this primitive is not
295 % included in the result of \cs{ior_str_get:NN}.
296 % \end{texnote}
297 % \end{function}
298 %
299 % All mappings are done at the current group level, i.e., any
300 % local assignments made by the \meta{function} or \meta{code} discussed
301 % below remain in effect after the loop.
302 %
303 % \begin{function}[added = 2012-02-11]{\ior_map_inline:Nn}
304 % \begin{syntax}
305 % \cs{ior_map_inline:Nn} \meta{stream} \Arg{inline function}
306 % \end{syntax}
307 % Applies the \meta{inline function} to each set of \meta{lines}
308 % obtained by calling \cs{ior_get:NN} until reaching the end of the
309 % file. \TeX{} ignores any trailing new-line marker from the file it
310 % reads. The \meta{inline function} should consist of code which
311 % receives the \meta{line} as |#1|.
312 % \end{function}
313 %
314 % \begin{function}[added = 2012-02-11]{\ior_str_map_inline:Nn}
315 % \begin{syntax}
316 % \cs{ior_str_map_inline:Nn} \meta{stream} \Arg{inline function}
317 % \end{syntax}
318 % Applies the \meta{inline function} to every \meta{line}
319 % in the \meta{stream}. The material is read from the \meta{stream}
320 % as a series of tokens with category code $12$ (other), with the
321 % exception of space characters which are given category code $10$
322 % (space). The \meta{inline function} should consist of code which
323 % receives the \meta{line} as |#1|.
324 % Note that \TeX{} removes trailing space and tab characters
325 % (character codes 32 and 9) from every line upon input. \TeX{} also
326 % ignores any trailing new-line marker from the file it reads.
327 % \end{function}
328 %
329 % \begin{function}[added = 2019-01-13]{\ior_map_variable:NNn}
330 % \begin{syntax}
331 % \cs{ior_map_variable:NNn} \meta{stream} \meta{tl~var} \Arg{code}
332 % \end{syntax}
333 % For each set of \meta{lines} obtained by calling \cs{ior_get:NN}
334 % until reaching the end of the file, stores the \meta{lines} in the
335 % \meta{tl~var} then applies the \meta{code}. The \meta{code} will
336 % usually make use of the \meta{variable}, but this is not enforced.
337 % The assignments to the \meta{variable} are local.
338 % Its value after the loop is the last set of \meta{lines}, or its
339 % original value if the \meta{stream} is empty. \TeX{} ignores
340 % any trailing new-line marker from the file it reads.
341 % This function is typically faster than \cs{ior_map_inline:Nn}.
342 % \end{function}
343 %
344 % \begin{function}[added = 2019-01-13]{\ior_str_map_variable:NNn}
345 % \begin{syntax}
346 % \cs{ior_str_map_variable:NNn} \meta{stream} \meta{variable} \Arg{code}
347 % \end{syntax}
348 % For each \meta{line} in the \meta{stream}, stores the \meta{line} in
349 % the \meta{variable} then applies the \meta{code}. The material is
350 % read from the \meta{stream} as a series of tokens with category code
351 % $12$ (other), with the exception of space characters which are given
352 % category code $10$ (space). The \meta{code} will usually make use
353 % of the \meta{variable}, but this is not enforced. The assignments
354 % to the \meta{variable} are local. Its value after the loop is the
355 % last \meta{line}, or its original value if the \meta{stream} is
356 % empty. Note that \TeX{} removes trailing
357 % space and tab characters (character codes 32 and 9) from every line
358 % upon input. \TeX{} also ignores any trailing new-line marker from
359 % the file it reads.
360 % This function is typically faster than \cs{ior_str_map_inline:Nn}.
361 % \end{function}
362 %
363 % \begin{function}[added = 2012-06-29]{\ior_map_break:}
364 % \begin{syntax}
365 % \cs{ior_map_break:}
366 % \end{syntax}
367 % Used to terminate a \cs[no-index]{ior_map_\ldots} function before all
368 % lines from the \meta{stream} have been processed. This
369 % normally takes place within a conditional statement, for example
370 % \begin{verbatim}
371 % \ior_map_inline:Nn \g_my_ior
372 % {
373 % \str_if_eq:nnTF { #1 } { bingo }
374 % { \ior_map_break: }
375 % {
376 % % Do something useful
377 % }
378 % }
379 % \end{verbatim}
380 % Use outside of a \cs[no-index]{ior_map_\ldots} scenario leads to low
381 % level \TeX{} errors.
382 % \begin{texnote}
383 % When the mapping is broken, additional tokens may be inserted
384 % before further items are taken
385 % from the input stream. This depends on the design of the mapping
386 % function.
387 % \end{texnote}
388 % \end{function}
389 %
390 % \begin{function}[added = 2012-06-29]{\ior_map_break:n}
391 % \begin{syntax}
392 % \cs{ior_map_break:n} \Arg{code}
393 % \end{syntax}
394 % Used to terminate a \cs[no-index]{ior_map_\ldots} function before all
395 % lines in the \meta{stream} have been processed, inserting
396 % the \meta{code} after the mapping has ended. This
397 % normally takes place within a conditional statement, for example
398 % \begin{verbatim}
399 % \ior_map_inline:Nn \g_my_ior
400 % {
401 % \str_if_eq:nnTF { #1 } { bingo }
402 % { \ior_map_break:n { <code> } }
403 % {
404 % % Do something useful
405 % }
406 % }
407 % \end{verbatim}
408 % Use outside of a \cs[no-index]{ior_map_\ldots} scenario leads to low
409 % level \TeX{} errors.
410 % \begin{texnote}
411 % When the mapping is broken, additional tokens may be inserted
412 % before the \meta{code} is
413 % inserted into the input stream.
414 % This depends on the design of the mapping function.
415 % \end{texnote}
416 % \end{function}
417 %
418 % \begin{function}[updated = 2012-02-10, EXP, pTF]{\ior_if_eof:N}
419 % \begin{syntax}
420 % \cs{ior_if_eof_p:N} \meta{stream} \\
421 % \cs{ior_if_eof:NTF} \meta{stream} \Arg{true code} \Arg{false code}
422 % \end{syntax}
423 % Tests if the end of a file \meta{stream} has been reached during a reading
424 % operation. The test also returns a \texttt{true} value if
425 % the \meta{stream} is not open.
426 % \end{function}
427 %
428 % \subsection{Reading from the terminal}
429 %
430 % \begin{function}[added = 2019-03-23]{\ior_get_term:nN, \ior_str_get_term:nN}
431 % \begin{syntax}
432 % \cs{ior_get_term:nN} \Arg{prompt} \meta{tl~var}
433 % \end{syntax}
434 % Function that reads one or more lines (until an equal number of left
435 % and right braces are found) from the terminal and stores
436 % the result locally in the \meta{token list} variable. Tokenization
437 % occurs as described for \cs{ior_get:NN} or \cs{ior_str_get:NN}, respectively.
438 % When the \meta{prompt}
439 % is empty, \TeX{} will wait for input without any other indication:
440 % typically the programmer will have provided a suitable text using
441 % e.g.~\cs{iow_term:n}. Where the \meta{prompt} is given, it will appear
442 % in the terminal followed by an |=|, e.g.
443 % \begin{verbatim}
444 % prompt=
445 % \end{verbatim}
446 % \end{function}
447 %
448 % \subsection{Writing to files}
449 %
450 % \begin{function}[updated = 2012-06-05]
451 % {
452 % \iow_now:Nn, \iow_now:NV, \iow_now:Ne,
453 % \iow_now:cn, \iow_now:cV, \iow_now:ce
454 % }
455 % \begin{syntax}
456 % \cs{iow_now:Nn} \meta{stream} \Arg{tokens}
457 % \end{syntax}
458 % This function writes \meta{tokens} to the specified
459 % \meta{stream} immediately (i.e., the write operation is called
460 % on expansion of \cs{iow_now:Nn}).
461 % \end{function}
462 %
463 % \begin{function}{\iow_log:n, \iow_log:e}
464 % \begin{syntax}
465 % \cs{iow_log:n} \Arg{tokens}
466 % \end{syntax}
467 % This function writes the given \meta{tokens} to the log (transcript)
468 % file immediately: it is a dedicated version of \cs{iow_now:Nn}.
469 % \end{function}
470 %
471 % \begin{function}[added = 2025-05-19]{\iow_show:n, \iow_show:e}
472 % \begin{syntax}
473 % \cs{iow_show:n} \Arg{tokens}
474 % \end{syntax}
475 % This function writes the given \meta{tokens} immediately to the same
476 % output as used by \tn{show} and \tn{showtokens}. At the start of a \TeX{}
477 % run this will be the terminal, but may be redirected to a file if the
478 % primitive \tn{showstream} has been set.
479 % \begin{texnote}
480 % At present, there is no \pkg{expl3} interface to set \tn{showstream}, but
481 % use of the \cs{iow_show:n} function is encouraged in places where direct
482 % writing to an I/O stream is intermixed with \texttt{show} functions.
483 % \end{texnote}
484 % \end{function}
485 %
486 % \begin{function}{\iow_term:n, \iow_term:e}
487 % \begin{syntax}
488 % \cs{iow_term:n} \Arg{tokens}
489 % \end{syntax}
490 % This function writes the given \meta{tokens} to the terminal
491 % file immediately: it is a dedicated version of \cs{iow_now:Nn}.
492 % \end{function}
493 %
494 % \begin{function}
495 % {
496 % \iow_shipout:Nn, \iow_shipout:Ne,
497 % \iow_shipout:cn, \iow_shipout:ce
498 % }
499 % \begin{syntax}
500 % \cs{iow_shipout:Nn} \meta{stream} \Arg{tokens}
501 % \end{syntax}
502 % This function writes \meta{tokens} to the specified
503 % \meta{stream} when the current page is finalized (i.e., at
504 % shipout). The \texttt{e}-type variants expand the \meta{tokens}
505 % at the point where the function is used but \emph{not} when the
506 % resulting tokens are written to the \meta{stream}
507 % (\emph{cf.}~\cs{iow_shipout_e:Nn}).
508 % \begin{texnote}
509 % When using \pkg{expl3} with a format other than \LaTeX{}, new line
510 % characters inserted using \cs{iow_newline:} or using the
511 % line-wrapping code \cs{iow_wrap:nnnN} are not recognized in
512 % the argument of \cs{iow_shipout:Nn}. This may lead to the
513 % insertion of additional unwanted line-breaks.
514 % \end{texnote}
515 % \end{function}
516 %
517 % \begin{function}[updated = 2023-09-17]
518 % {
519 % \iow_shipout_e:Nn, \iow_shipout_e:Ne,
520 % \iow_shipout_e:cn, \iow_shipout_e:ce
521 % }
522 % \begin{syntax}
523 % \cs{iow_shipout_e:Nn} \meta{stream} \Arg{tokens}
524 % \end{syntax}
525 % This function writes \meta{tokens} to the specified
526 % \meta{stream} when the current page is finalized (i.e., at
527 % shipout). The \meta{tokens} are expanded at the time of writing
528 % in addition to any expansion when the function is used. This makes
529 % these functions suitable for including material finalized during
530 % the page building process (such as the page number integer).
531 % \begin{texnote}
532 % This is a wrapper around the \TeX{} primitive \tn{write}.
533 % When using \pkg{expl3} with a format other than \LaTeX{}, new line
534 % characters inserted using \cs{iow_newline:} or using the
535 % line-wrapping code \cs{iow_wrap:nnnN} are not recognized in
536 % the argument of \cs{iow_shipout:Nn}. This may lead to the
537 % insertion of additional unwanted line-breaks.
538 % \end{texnote}
539 % \end{function}
540 %
541 % \begin{function}[EXP]{\iow_char:N}
542 % \begin{syntax}
543 % \cs{iow_char:N} |\|\meta{char}
544 % \end{syntax}
545 % Inserts \meta{char} into the output stream. Useful when trying to
546 % write difficult characters such as |%|, |{|, |}|,
547 % etc.~in messages, for example:
548 % \begin{verbatim}
549 % \iow_now:Ne \g_my_iow { \iow_char:N \{ text \iow_char:N \} }
550 % \end{verbatim}
551 % The function has no effect if writing is taking place without
552 % expansion (\emph{e.g.}~in the second argument of \cs{iow_now:Nn}).
553 % \end{function}
554 %
555 % \begin{function}[EXP]{\iow_newline:}
556 % \begin{syntax}
557 % \cs{iow_newline:}
558 % \end{syntax}
559 % Function to add a new line within the \meta{tokens} written to a
560 % file. The function has no effect if writing is taking place without
561 % expansion (\emph{e.g.}~in the second argument of \cs{iow_now:Nn}).
562 % \begin{texnote}
563 % When using \pkg{expl3} with a format other than \LaTeX{}, the
564 % character inserted by \cs{iow_newline:} is not recognized by
565 % \TeX{}, which may lead to the insertion of additional unwanted
566 % line-breaks. This issue only affects \cs{iow_shipout:Nn},
567 % \cs{iow_shipout_e:Nn} and direct uses of primitive operations.
568 % \end{texnote}
569 % \end{function}
570 %
571 % \subsection{Wrapping lines in output}
572 %
573 % \begin{function}[added = 2012-06-28, updated = 2017-12-04]
574 % {\iow_wrap:nnnN, \iow_wrap:nenN}
575 % \begin{syntax}
576 % \cs{iow_wrap:nnnN} \Arg{text} \Arg{run-on text} \Arg{set up} \meta{function}
577 % \end{syntax}
578 % This function wraps the \meta{text} to a fixed number of
579 % characters per line. At the start of each line which is wrapped,
580 % the \meta{run-on text} is inserted. The line character count
581 % targeted is the value of \cs{l_iow_line_count_int} minus the
582 % number of characters in the \meta{run-on text} for all lines except
583 % the first, for which the target number of characters is simply
584 % \cs{l_iow_line_count_int} since there is no run-on text. The
585 % \meta{text} and \meta{run-on text} are exhaustively expanded by the
586 % function, with the following substitutions:
587 % \begin{itemize}
588 % \item |\\| or \cs{iow_newline:} may be used to force a new line,
589 % \item \verb*|\ | may be used to represent a forced space
590 % (for example after a control sequence),
591 % \item |\#|, |\%|, |\{|, |\}|, |\~| may be used to represent
592 % the corresponding character,
593 % \item \cs{iow_wrap_allow_break:} may be used to allow a line-break
594 % without inserting a space,
595 % \item \cs{iow_indent:n} may be used to indent a part of the
596 % \meta{text} (not the \meta{run-on text}).
597 % \end{itemize}
598 % Additional functions may be added to the wrapping by using the
599 % \meta{set up}, which is executed before the wrapping takes place: this
600 % may include overriding the substitutions listed.
601 %
602 % Any expandable material in the \meta{text} which is not to be expanded
603 % on wrapping should be converted to a string using \cs{token_to_str:N},
604 % \cs{tl_to_str:n}, \cs{tl_to_str:N}, etc.
605 %
606 % The result of the wrapping operation is passed as a braced argument to the
607 % \meta{function}, which is typically a wrapper around a write
608 % operation. The output of \cs{iow_wrap:nnnN} (i.e., the argument
609 % passed to the \meta{function}) consists of characters of category
610 % \enquote{other} (category code~12), with the exception of spaces which
611 % have category \enquote{space} (category code~10). This means that the
612 % output does \emph{not} expand further when written to a file.
613 %
614 % \begin{texnote}
615 % Internally, \cs{iow_wrap:nnnN} carries out an \texttt{e}-type expansion
616 % on the \meta{text} to expand it. This is done in such a way that
617 % \cs{exp_not:N} or \cs{exp_not:n} \emph{could} be used to prevent
618 % expansion of material. However, this is less conceptually clear than
619 % conversion to a string, which is therefore the supported method for
620 % handling expandable material in the \meta{text}.
621 % \end{texnote}
622 % \end{function}
623 %
624 % \begin{function}[added = 2023-04-25]{\iow_wrap_allow_break:}
625 % \begin{syntax}
626 % \cs{iow_wrap_allow_break:}
627 % \end{syntax}
628 % In the first argument of \cs{iow_wrap:nnnN} (for instance in
629 % messages), inserts a break-point that allows a line break. If
630 % no break occurs, this function adds nothing to the output.
631 % \end{function}
632 %
633 % \begin{function}[added = 2011-09-21]{\iow_indent:n}
634 % \begin{syntax}
635 % \cs{iow_indent:n} \Arg{text}
636 % \end{syntax}
637 % In the first argument of \cs{iow_wrap:nnnN} (for instance in messages),
638 % indents \meta{text} by four spaces. This function does not cause
639 % a line break, and only affects lines which start within the scope
640 % of the \meta{text}. In case the indented \meta{text} should appear
641 % on separate lines from the surrounding text, use |\\| to force
642 % line breaks.
643 % \end{function}
644 %
645 % \begin{variable}[added = 2012-06-24]{\l_iow_line_count_int}
646 % The maximum number of characters in a line to be written
647 % by the \cs{iow_wrap:nnnN}
648 % function. This value depends on the \TeX{} system in use: the standard
649 % value is $78$, which is typically correct for unmodified \TeX{} Live
650 % and \hologo{MiKTeX} systems.
651 % \end{variable}
652 %
653 % \subsection{Constant input--output streams, and variables}
654 %
655 % \begin{variable}[added = 2017-12-11]{\g_tmpa_ior, \g_tmpb_ior}
656 % Scratch input stream for global use. These are never used by
657 % the kernel code, and so are safe for use with any \LaTeX3-defined
658 % function. However, they may be overwritten by other non-kernel
659 % code and so should only be used for short-term storage.
660 % \end{variable}
661 %
662 % \begin{variable}{\c_log_iow, \c_term_iow}
663 % Constant output streams for writing to the log and to the terminal
664 % (plus the log), respectively.
665 % \end{variable}
666 %
667 % \begin{variable}[added = 2017-12-11]{\g_tmpa_iow, \g_tmpb_iow}
668 % Scratch output stream for global use. These are never used by
669 % the kernel code, and so are safe for use with any \LaTeX3-defined
670 % function. However, they may be overwritten by other non-kernel
671 % code and so should only be used for short-term storage.
672 % \end{variable}
673 %
674 % \subsection{Primitive conditionals}
675 %
676 % \begin{function}[EXP]{\if_eof:w}
677 % \begin{syntax}
678 % \cs{if_eof:w} \meta{stream}
679 % ~~\meta{true code}
680 % \cs{else:}
681 % ~~\meta{false code}
682 % \cs{fi:}
683 % \end{syntax}
684 % Tests if the \meta{stream} returns \enquote{end of file}, which is true
685 % for non-existent files. The \cs{else:} branch is optional.
686 % \begin{texnote}
687 % This is the \TeX{} primitive \tn{ifeof}.
688 % \end{texnote}
689 % \end{function}
690 %
691 % \section{File operations}
692 %
693 % \subsection{Basic file operations}
694 %
695 % \begin{variable}[added = 2017-06-21]
696 % {
697 % \g_file_curr_dir_str,
698 % \g_file_curr_name_str,
699 % \g_file_curr_ext_str
700 % }
701 % Contain the directory, name and extension of the current file. The
702 % directory is empty if the file was loaded without an explicit
703 % path (i.e., if it is in the \TeX{} search path), and does
704 % \emph{not} end in |/| other than the case that it is exactly equal
705 % to the root directory. The \meta{name} and \meta{ext} parts together
706 % make up the file name, thus the \meta{name} part may be thought of
707 % as the \enquote{job name} for the current file.
708 %
709 % Note that \TeX{} does not provide information on the \meta{dir} and
710 % \meta{ext} part for the main (top level) file and that this file
711 % always has empty \meta{dir} and \meta{ext} components.
712 % Also, the \meta{name} here will be equal to \cs{c_sys_jobname_str},
713 % which may be different from the real file name (if set using
714 % |--jobname|, for example).
715 % \end{variable}
716 %
717 % \begin{variable}[added = 2017-06-18, updated = 2023-06-15]
718 % {\l_file_search_path_seq}
719 % Each entry is the path to a directory which should be searched when
720 % seeking a file. Each path can be relative or absolute, and need
721 % not include the trailing slash. Spaces need not be quoted.
722 %
723 % \begin{texnote}
724 % When working as a package in \LaTeXe{}, \pkg{expl3} will
725 % automatically append the current \tn{input@path} to the
726 % set of values from \cs{l_file_search_path_seq}.
727 % \end{texnote}
728 % \end{variable}
729 %
730 % \begin{function}[EXP, pTF, updated = 2023-09-18]
731 % {\file_if_exist:n, \file_if_exist:V}
732 % \begin{syntax}
733 % \cs{file_if_exist_p:n} \Arg{file name}
734 % \cs{file_if_exist:nTF} \Arg{file name} \Arg{true code} \Arg{false code}
735 % \end{syntax}
736 % Expands the argument of the \meta{file name} to give a string, then
737 % searches for this string using the current \TeX{} search
738 % path and the additional paths controlled by
739 % \cs{l_file_search_path_seq}.
740 %
741 % Since \TeX{} cannot remove files, only write to them, once a file
742 % has been found during a \TeX{} run, it will exist until the end of
743 % the run unless a non-\TeX{} process intervenes. Since file operations
744 % are relatively slow, \pkg{expl3} therefore internally tracks when a
745 % file is seen, and uses this information to avoid multiple filesystem
746 % checks. See \cs{file_forget:n} for how to indicate to \pkg{expl3} that
747 % a file may have been deleted \emph{during} a \TeX{} run, so that its
748 % presence in the filesystem can be reasserted with \cs{file_if_exist:nTF}
749 % and similar commands.
750 % \end{function}
751 %
752 % \begin{function}[added = 2024-12-09]{\file_forget:n}
753 % \begin{syntax}
754 % \cs{file_forget:n} \Arg{file name}
755 % \end{syntax}
756 % Resets the internal tracker for files such that a subsequent use of
757 % \cs{file_if_exist:nTF}, \cs{file_size:n}, etc., for the \meta{file name} will
758 % re-query the filesystem rather than use any cached information. This can
759 % be used whether or not the file has previously been seen. This function
760 % is intended to be used where non-\TeX{} processes may result in file
761 % deletion, for example if \LuaTeX{} is in use, |os.remove()| may be used
762 % to delete a file part-way through a run.
763 % \end{function}
764 %
765 % \subsection{Information about files and file contents}
766 %
767 % Functions in this section return information about files as \pkg{expl3}
768 % \texttt{str} data, \emph{except} that the non-expandable functions set their
769 % return \emph{token list} to \cs{q_no_value} if the file requested is not
770 % found. As such, comparison of file names, hashes, sizes, etc., should use
771 % \cs{str_if_eq:nnTF} rather than \cs{tl_if_eq:nnTF} and so on.
772 %
773 % \begin{function}[rEXP, added = 2019-11-19]
774 % {
775 % \file_hex_dump:n, \file_hex_dump:V,
776 % \file_hex_dump:nnn, \file_hex_dump:Vnn
777 % }
778 % \begin{syntax}
779 % \cs{file_hex_dump:n} \Arg{file name}
780 % \cs{file_hex_dump:nnn} \Arg{file name} \Arg{start index} \Arg{end index}
781 % \end{syntax}
782 % Searches for \meta{file name} using the current \TeX{} search
783 % path and the additional paths controlled by \cs{l_file_search_path_seq}.
784 % It then expands to leave the hexadecimal dump of the file content in the
785 % input stream. The file is read as bytes, which means that in
786 % contrast to most \TeX{} behavior there will be a difference in result
787 % depending on the line endings used in text files. The same file will
788 % produce the same result between different engines: the algorithm used
789 % is the same in all cases. When the file is not found, the result of
790 % expansion is empty. The \Arg{start index} and \Arg{end index} values
791 % work as described for \cs{str_range:nnn}.
792 % \end{function}
793 %
794 % \begin{function}[noTF, added = 2019-11-19]
795 % {
796 % \file_get_hex_dump:nN, \file_get_hex_dump:VN,
797 % \file_get_hex_dump:nnnN, \file_get_hex_dump:VnnN
798 % }
799 % \begin{syntax}
800 % \cs{file_get_hex_dump:nN} \Arg{file name} \meta{tl var}
801 % \cs{file_get_hex_dump:nnnN} \Arg{file name} \Arg{start index} \Arg{end index} \meta{tl var}
802 % \end{syntax}
803 % Sets the \meta{tl var} to the result of applying
804 % \cs{file_hex_dump:n}/\cs{file_hex_dump:nnn} to the \meta{file}.
805 % If the file is not found, the \meta{tl var} will be set to \cs{q_no_value}.
806 % \end{function}
807 %
808 % \begin{function}[rEXP, added = 2019-09-03]
809 % {\file_mdfive_hash:n, \file_mdfive_hash:V}
810 % \begin{syntax}
811 % \cs{file_mdfive_hash:n} \Arg{file name}
812 % \end{syntax}
813 % Searches for \meta{file name} using the current \TeX{} search
814 % path and the additional paths controlled by \cs{l_file_search_path_seq}.
815 % It then expands to leave the MD5 sum generated from the contents of
816 % the file in the input stream. The file is read as bytes, which means that in
817 % contrast to most \TeX{} behavior there will be a difference in result
818 % depending on the line endings used in text files. The same file will
819 % produce the same result between different engines: the algorithm used
820 % is the same in all cases. When the file is not found, the result of
821 % expansion is empty.
822 % \end{function}
823 %
824 % \begin{function}[noTF, added = 2017-07-11, updated = 2019-02-16]
825 % {\file_get_mdfive_hash:nN, \file_get_mdfive_hash:VN}
826 % \begin{syntax}
827 % \cs{file_get_mdfive_hash:nN} \Arg{file name} \meta{tl var}
828 % \end{syntax}
829 % Sets the \meta{tl var} to the result of applying
830 % \cs{file_mdfive_hash:n} to the \meta{file}. If the file is not found,
831 % the \meta{tl var} will be set to \cs{q_no_value}.
832 % \end{function}
833 %
834 % \begin{function}[rEXP, added = 2019-09-03]{\file_size:n, \file_size:V}
835 % \begin{syntax}
836 % \cs{file_size:n} \Arg{file name}
837 % \end{syntax}
838 % Searches for \meta{file name} using the current \TeX{} search
839 % path and the additional paths controlled by \cs{l_file_search_path_seq}.
840 % It then expands to leave the size of the file in bytes in the input stream.
841 % When the file is not found, the result of expansion is empty.
842 % \end{function}
843 %
844 % \begin{function}[noTF, added = 2017-07-09, updated = 2019-02-16]
845 % {\file_get_size:nN, \file_get_size:VN}
846 % \begin{syntax}
847 % \cs{file_get_size:nN} \Arg{file name} \meta{tl var}
848 % \end{syntax}
849 % Sets the \meta{tl var} to the result of applying
850 % \cs{file_size:n} to the \meta{file}. If the file is not found,
851 % the \meta{tl var} will be set to \cs{q_no_value}.
852 % \end{function}
853 %
854 % \begin{function}[rEXP, added = 2019-09-03]
855 % {\file_timestamp:n, \file_timestamp:V}
856 % \begin{syntax}
857 % \cs{file_timestamp:n} \Arg{file name}
858 % \end{syntax}
859 % Searches for \meta{file name} using the current \TeX{} search
860 % path and the additional paths controlled by \cs{l_file_search_path_seq}.
861 % It then expands to leave the modification timestamp of
862 % the file in the input stream. The timestamp is of the form
863 % |D:|\meta{year}\meta{month}\meta{day}\meta{hour}^^A
864 % \meta{minute}\meta{second}\meta{offset}, where the latter may be |Z|
865 % (UTC) or \meta{plus-minus}\meta{hours}|'|\meta{minutes}|'|.
866 % When the file is not found, the result of expansion is empty.
867 % \end{function}
868 %
869 % \begin{function}[noTF, added = 2017-07-09, updated = 2019-02-16]
870 % {\file_get_timestamp:nN, \file_get_timestamp:VN}
871 % \begin{syntax}
872 % \cs{file_get_timestamp:nN} \Arg{file name} \meta{tl var}
873 % \end{syntax}
874 % Sets the \meta{tl var} to the result of applying
875 % \cs{file_timestamp:n} to the \meta{file}. If the file is not found,
876 % the \meta{tl var} will be set to \cs{q_no_value}.
877 % \end{function}
878 %
879 % \begin{function}[added = 2019-05-13, updated = 2019-09-20, pTF, EXP]
880 % {
881 % \file_compare_timestamp:nNn,
882 % \file_compare_timestamp:nNV,
883 % \file_compare_timestamp:VNn,
884 % \file_compare_timestamp:VNV
885 % }
886 % \begin{syntax}
887 % \cs{file_compare_timestamp_p:nNn} \Arg{file-1} \meta{relation} \Arg{file-2}
888 % \cs{file_compare_timestamp:nNnTF} \Arg{file-1} \meta{relation} \Arg{file-2} \Arg{true code} \Arg{false code}
889 % \end{syntax}
890 % Compares the file stamps on the two \meta{files} as indicated by
891 % the \meta{relation}, and inserts either the \meta{true code}
892 % or \meta{false case} as required. A file which is not found
893 % is treated as older than any file which is found. This allows for
894 % example the construct
895 % \begin{verbatim}
896 % \file_compare_timestamp:nNnT { source-file } > { derived-file }
897 % {
898 % % Code to regenerate derived file
899 % }
900 % \end{verbatim}
901 % to work when the derived file is entirely absent. The timestamp
902 % of two absent files is regarded as different.
903 % \end{function}
904 %
905 % \begin{function}[noTF, updated = 2019-02-16]
906 % {\file_get_full_name:nN, \file_get_full_name:VN}
907 % \begin{syntax}
908 % \cs{file_get_full_name:nN} \Arg{file name} \meta{tl var}
909 % \cs{file_get_full_name:nNTF} \Arg{file name} \meta{tl var} \Arg{true code} \Arg{false code}
910 % \end{syntax}
911 % Searches for \meta{file name} in the path as detailed for
912 % \cs{file_if_exist:nTF}, and if found sets the \meta{tl var} the
913 % fully-qualified name of the file, i.e., the path and file name.
914 % This includes an extension |.tex| when the given \meta{file name}
915 % has no extension but the file found has that extension.
916 % In the non-branching version, the \meta{tl var} will be set to
917 % \cs{q_no_value} in the case that the file does not exist.
918 % \end{function}
919 %
920 % \begin{function}[added = 2019-09-03, rEXP]{\file_full_name:n, \file_full_name:V}
921 % \begin{syntax}
922 % \cs{file_full_name:n} \Arg{file name}
923 % \end{syntax}
924 % Searches for \meta{file name} in the path as detailed for
925 % \cs{file_if_exist:nTF}, and if found leaves the
926 % fully-qualified name of the file, i.e., the path and file name,
927 % in the input stream.
928 % This includes an extension |.tex| when the given \meta{file name}
929 % has no extension but the file found has that extension.
930 % If the file is not found on the path, the expansion is empty.
931 % \end{function}
932 %
933 % \begin{function}[added = 2017-06-23, updated = 2020-06-24]
934 % {\file_parse_full_name:nNNN, \file_parse_full_name:VNNN}
935 % \begin{syntax}
936 % \cs{file_parse_full_name:nNNN} \Arg{full name} \meta{dir} \meta{name} \meta{ext}
937 % \end{syntax}
938 % Parses the \meta{full name} and splits it into three parts, each of
939 % which is returned by setting the appropriate local string variable:
940 % \begin{itemize}
941 % \item The \meta{dir}: everything up to the last |/| (path separator)
942 % in the \meta{file path}. As with system \texttt{PATH} variables
943 % and related functions, the \meta{dir} does \emph{not} include the
944 % trailing |/| unless it points to the root directory. If there is no path (only
945 % a file name), \meta{dir} is empty.
946 % \item The \meta{name}: everything after the last |/| up to the last |.|,
947 % where both of those characters are optional. The \meta{name} may
948 % contain multiple |.| characters. It is empty if \meta{full name}
949 % consists only of a directory name.
950 % \item The \meta{ext}: everything after the last |.| (including the dot).
951 % The \meta{ext} is empty if there is no |.| after the last |/|.
952 % \end{itemize}
953 %
954 % Before parsing, the \meta{full name} is expanded until only non-expandable
955 % tokens remain, except that active characters are also not expanded.
956 % Quotes (|"|) are invalid in file names and are discarded from the input.
957 % \end{function}
958 %
959 % \begin{function}[EXP, added = 2020-06-24]
960 % {\file_parse_full_name:n, \file_parse_full_name:V}
961 % \begin{syntax}
962 % \cs{file_parse_full_name:n} \Arg{full name}
963 % \end{syntax}
964 % Parses the \meta{full name} as described for \cs{file_parse_full_name:nNNN},
965 % and leaves \meta{dir}, \meta{name}, and \meta{ext} in the input stream,
966 % each inside a pair of braces.
967 % \end{function}
968 %
969 % \begin{function}[EXP, added = 2020-06-24]
970 % {\file_parse_full_name_apply:nN, \file_parse_full_name_apply:VN}
971 % \begin{syntax}
972 % \cs{file_parse_full_name_apply:nN} \Arg{full name} \meta{function}
973 % \end{syntax}
974 % Parses the \meta{full name} as described for \cs{file_parse_full_name:nNNN},
975 % and passes \meta{dir}, \meta{name}, and \meta{ext} as arguments to \meta{function},
976 % as an \texttt{n}-type argument each, in this order.
977 % \end{function}
978 %
979 % \subsection{Accessing file contents}
980 %
981 % \begin{function}[noTF, added = 2019-01-16, updated = 2019-02-16]
982 % {\file_get:nnN, \file_get:VnN}
983 % \begin{syntax}
984 % \cs{file_get:nnN} \Arg{file name} \Arg{setup} \meta{tl var}
985 % \cs{file_get:nnNTF} \Arg{file name} \Arg{setup} \meta{tl var} \Arg{true code} \Arg{false code}
986 % \end{syntax}
987 % Defines \meta{tl var} to the contents of \meta{file name}.
988 % Category codes may need to be set appropriately via the \meta{setup}
989 % argument.
990 % The non-branching version sets the \meta{tl var} to \cs{q_no_value} if the file is
991 % not found. The branching version runs the \meta{true code} after the
992 % assignment to \meta{tl var} if the file is found, and \meta{false code}
993 % otherwise. The file content will be tokenized using the current
994 % category code régime.
995 % \end{function}
996 %
997 % \begin{function}[updated = 2017-06-26]{\file_input:n, \file_input:V}
998 % \begin{syntax}
999 % \cs{file_input:n} \Arg{file name}
1000 % \end{syntax}
1001 % Searches for \meta{file name} in the path as detailed for
1002 % \cs{file_if_exist:nTF}, and if found reads in the file as
1003 % additional \LaTeX{} source. All files read are recorded
1004 % for information and the file name stack is updated by this
1005 % function. An error is raised if the file is not found.
1006 % \end{function}
1007 %
1008 % \begin{function}[added = 2023-05-18, updated = 2025-05-26, EXP]
1009 % {\file_input_raw:n, \file_input_raw:V}
1010 % \begin{syntax}
1011 % \cs{file_input_raw:n} \Arg{file name}
1012 % \end{syntax}
1013 % Searches for \meta{file name} in the path as detailed for
1014 % \cs{file_if_exist:nTF}, and if found reads in the file as
1015 % additional \TeX{} source. No data concerning the file is
1016 % tracked. If the file is not found, no action is taken.
1017 % \begin{texnote}
1018 % This function \emph{requires} the availability of the \tn{input}
1019 % primitive accepting braces (Lua\TeX{} or other engines from
1020 % \TeX{} Live 2020 onwards.)
1021 %
1022 % This function is intended only for contexts where files must
1023 % be read purely by expansion, for example at the start of a
1024 % table cell in an \tn{halign}.
1025 % \end{texnote}
1026 % \end{function}
1027 %
1028 % \begin{function}[added = 2014-07-02]
1029 % {
1030 % \file_if_exist_input:n,
1031 % \file_if_exist_input:V,
1032 % \file_if_exist_input:nF,
1033 % \file_if_exist_input:VF
1034 % }
1035 % \begin{syntax}
1036 % \cs{file_if_exist_input:n} \Arg{file name}
1037 % \cs{file_if_exist_input:nF} \Arg{file name} \Arg{false code}
1038 % \end{syntax}
1039 % Searches for \meta{file name} using the current \TeX{} search
1040 % path and the additional paths included in \cs{l_file_search_path_seq}.
1041 % If found then
1042 % reads in the file as additional \LaTeX{} source as described for
1043 % \cs{file_input:n}, otherwise inserts the \meta{false code}.
1044 % Note that these functions do not raise
1045 % an error if the file is not found, in contrast to \cs{file_input:n}.
1046 % \end{function}
1047 %
1048 % \begin{function}[added = 2017-07-07]{\file_input_stop:}
1049 % \begin{syntax}
1050 % \cs{file_input_stop:}
1051 % \end{syntax}
1052 % Ends the reading of a file started by \cs{file_input:n} or similar before
1053 % the end of the file is reached. Where the file reading is being terminated
1054 % due to an error, \cs[index = msg_critical:nn]{msg_critical:nn(nn)}
1055 % should be preferred.
1056 % \begin{texnote}
1057 % This function must be used on a line on its own: \TeX{} reads files
1058 % line-by-line and so any additional tokens in the \enquote{current} line
1059 % will still be read.
1060 %
1061 % This is also true if the function is hidden inside another function
1062 % (which will be the normal case), i.e., all tokens on the same line
1063 % in the source file are still processed. Putting it on a line by itself
1064 % in the definition doesn't help as it is the line where it is used that
1065 % counts!
1066 % \end{texnote}
1067 % \end{function}
1068 %
1069 % \begin{function}{\file_show_list:, \file_log_list:}
1070 % \begin{syntax}
1071 % \cs{file_show_list:}
1072 % \cs{file_log_list:}
1073 % \end{syntax}
1074 % These functions list all files loaded by \LaTeXe{} commands that
1075 % populate \tn{@filelist} or by \cs{file_input:n}. While
1076 % \cs{file_show_list:} displays the list in the terminal,
1077 % \cs{file_log_list:} outputs it to the log file only.
1078 % \end{function}
1079 %
1080 % \end{documentation}
1081 %
1082 % \begin{implementation}
1083 %
1084 % \section{\pkg{l3file} implementation}
1085 %
1086 % \TestFiles{m3file001}
1087 %
1088 % \begin{macrocode}
1089 %<*code>
1090 % \end{macrocode}
1091 %
1092 % \subsection{Input operations}
1093 %
1094 % \begin{macrocode}
1095 %<@@=ior>
1096 % \end{macrocode}
1097 %
1098 % \subsubsection{Variables and constants}
1099 %
1100 % \begin{variable}{\l_@@_tmp_tl}
1101 % Used as a short-term scratch variable.
1102 % \begin{macrocode}
1103 \tl_new:N \l_@@_tmp_tl
1104 % \end{macrocode}
1105 % \end{variable}
1106 %
1107 % \begin{variable}{\c_@@_term_ior}
1108 % Reading from the terminal (with a prompt) is done using a positive
1109 % but non-existent stream number. Unlike writing, there is no concept
1110 % of reading from the log.
1111 % \begin{macrocode}
1112 \int_const:Nn \c_@@_term_ior { 16 }
1113 % \end{macrocode}
1114 % \end{variable}
1115 %
1116 % \begin{variable}{\g_@@_streams_seq}
1117 % A list of the currently-available input streams to be used as a
1118 % stack.
1119 % \begin{macrocode}
1120 \seq_new:N \g_@@_streams_seq
1121 % \end{macrocode}
1122 % \end{variable}
1123 %
1124 % \begin{variable}{\l_@@_stream_tl}
1125 % Used to recover the raw stream number from the stack.
1126 % \begin{macrocode}
1127 \tl_new:N \l_@@_stream_tl
1128 % \end{macrocode}
1129 % \end{variable}
1130 %
1131 % \begin{variable}{\g_@@_streams_prop}
1132 % The name of the file attached to each stream is tracked in a property
1133 % list. To get the correct number of reserved streams in package mode the
1134 % underlying mechanism needs to be queried. For \LaTeXe{} and plain \TeX{}
1135 % this data is stored in |\count16|: with the \pkg{etex} package loaded
1136 % we need to subtract $1$ as the register holds the number of the next
1137 % stream to use. In \ConTeXt{}, we need to look at |\count38| but there
1138 % is no subtraction: like the original plain \TeX{}/\LaTeXe{} mechanism
1139 % it holds the value of the \emph{last} stream allocated.
1140 % \begin{macrocode}
1141 \prop_new:N \g_@@_streams_prop
1142 \int_step_inline:nnn
1143 { 0 }
1144 {
1145 \cs_if_exist:NTF \contextversion
1146 { \tex_count:D 38 ~ }
1147 {
1148 \tex_count:D 16 ~ %
1149 \cs_if_exist:NT \loccount { - 1 }
1150 }
1151 }
1152 {
1153 \prop_gput:Nnn \g_@@_streams_prop {#1} { Reserved~by~format }
1154 }
1155 % \end{macrocode}
1156 % \end{variable}
1157 %
1158 % \subsubsection{Stream management}
1159 %
1160 % \begin{macro}{\ior_new:N, \ior_new:c}
1161 % Reserving a new stream is done by defining the name as equal to using the
1162 % terminal.
1163 % \begin{macrocode}
1164 \cs_new_protected:Npn \ior_new:N #1 { \cs_new_eq:NN #1 \c_@@_term_ior }
1165 \cs_generate_variant:Nn \ior_new:N { c }
1166 % \end{macrocode}
1167 % \end{macro}
1168 %
1169 % \begin{variable}{\g_tmpa_ior, \g_tmpb_ior}
1170 % The usual scratch space.
1171 % \begin{macrocode}
1172 \ior_new:N \g_tmpa_ior
1173 \ior_new:N \g_tmpb_ior
1174 % \end{macrocode}
1175 % \end{variable}
1176 %
1177 % \begin{macro}{\ior_open:Nn, \ior_open:cn}
1178 % Use the conditional version, with an error if the file is not found.
1179 % \begin{macrocode}
1180 \cs_new_protected:Npn \ior_open:Nn #1#2
1181 { \ior_open:NnF #1 {#2} { \__kernel_file_missing:n {#2} } }
1182 \cs_generate_variant:Nn \ior_open:Nn { c }
1183 % \end{macrocode}
1184 % \end{macro}
1185 %
1186 % \begin{variable}{\l_@@_file_name_tl}
1187 % Data storage.
1188 % \begin{macrocode}
1189 \tl_new:N \l_@@_file_name_tl
1190 % \end{macrocode}
1191 % \end{variable}
1192 %
1193 % \begin{macro}[TF]{\ior_open:Nn, \ior_open:cn}
1194 % An auxiliary searches for the file in the \TeX{}, \LaTeXe{} and
1195 % \LaTeX3 paths. Then pass the file found to the lower-level function
1196 % which deals with streams. The |full_name| is empty when the file is
1197 % not found.
1198 % \begin{macrocode}
1199 \prg_new_protected_conditional:Npnn \ior_open:Nn #1#2 { T , F , TF }
1200 {
1201 \file_get_full_name:nNTF {#2} \l_@@_file_name_tl
1202 {
1203 \__kernel_ior_open:No #1 \l_@@_file_name_tl
1204 \prg_return_true:
1205 }
1206 { \prg_return_false: }
1207 }
1208 \prg_generate_conditional_variant:Nnn \ior_open:Nn { c } { T , F , TF }
1209 % \end{macrocode}
1210 % \end{macro}
1211 %
1212 % \begin{macro}{\@@_new:N}
1213 % Streams are reserved using \tn{newread} before they
1214 % can be managed by \pkg{ior}. To prevent \pkg{ior} from being
1215 % affected by redefinitions of \tn{newread} (such as done by the
1216 % third-party package \pkg{morewrites}), this macro is saved here
1217 % under a private name. The complicated code ensures that
1218 % \cs{@@_new:N} is not \tn{outer} despite plain \TeX{}'s \tn{newread}
1219 % being \tn{outer}. For \ConTeXt{}, we have to deal with the fact
1220 % that \tn{newread} works like our own: it actually checks before
1221 % altering definition.
1222 % \begin{macrocode}
1223 \exp_args:NNf \cs_new_protected:Npn \@@_new:N
1224 { \exp_args:NNc \exp_after:wN \exp_stop_f: { newread } }
1225 \cs_if_exist:NT \contextversion
1226 {
1227 \cs_new_eq:NN \@@_new_aux:N \@@_new:N
1228 \cs_gset_protected:Npn \@@_new:N #1
1229 {
1230 \cs_undefine:N #1
1231 \@@_new_aux:N #1
1232 }
1233 }
1234 % \end{macrocode}
1235 % \end{macro}
1236 %
1237 % \begin{macro}{\__kernel_ior_open:Nn, \__kernel_ior_open:No}
1238 % \begin{macro}{\@@_open_stream:Nn}
1239 % The stream allocation itself uses the fact that there is a list of all of
1240 % those available. Life gets more complex as it's
1241 % important to keep things in sync. That is done using a two-part approach:
1242 % any streams that have already been taken up by \pkg{ior} but are now free
1243 % are tracked, so we first try those. If that fails, ask plain \TeX{} or \LaTeXe{}
1244 % for a new stream and use that number (after a bit of conversion).
1245 % \begin{macrocode}
1246 \cs_new_protected:Npn \__kernel_ior_open:Nn #1#2
1247 {
1248 \ior_close:N #1
1249 \seq_gpop:NNTF \g_@@_streams_seq \l_@@_stream_tl
1250 { \@@_open_stream:Nn #1 {#2} }
1251 {
1252 \@@_new:N #1
1253 \__kernel_tl_set:Nx \l_@@_stream_tl { \int_eval:n {#1} }
1254 \@@_open_stream:Nn #1 {#2}
1255 }
1256 }
1257 \cs_generate_variant:Nn \__kernel_ior_open:Nn { No }
1258 % \end{macrocode}
1259 % Here, we act defensively in case \LuaTeX{} is in use with an
1260 % extensionless file name.
1261 % \begin{macrocode}
1262 \cs_new_protected:Npe \@@_open_stream:Nn #1#2
1263 {
1264 \tex_global:D \tex_chardef:D #1 = \exp_not:N \l_@@_stream_tl \scan_stop:
1265 \prop_gput:NVn \exp_not:N \g_@@_streams_prop #1 {#2}
1266 \tex_openin:D #1
1267 \sys_if_engine_luatex:TF
1268 { {#2} }
1269 { \exp_not:N \__kernel_file_name_quote:n {#2} \scan_stop: }
1270 }
1271 % \end{macrocode}
1272 % \end{macro}
1273 % \end{macro}
1274 %
1275 % \begin{macro}{\ior_shell_open:Nn}
1276 % \begin{macro}{\@@_shell_open:nN, \@@_shell_open:oN}
1277 % Actually much easier than either the standard open or input versions!
1278 % When calling \cs{__kernel_ior_open:Nn} the file the pipe is added to
1279 % signal a shell command, but the quotes are not added yet---they are
1280 % added later by \cs{__kernel_file_name_quote:n}.
1281 % \begin{macrocode}
1282 \cs_new_protected:Npn \ior_shell_open:Nn #1#2
1283 {
1284 \sys_if_shell:TF
1285 { \@@_shell_open:oN { \tl_to_str:n {#2} } #1 }
1286 { \msg_error:nn { kernel } { pipe-failed } }
1287 }
1288 \cs_new_protected:Npn \@@_shell_open:nN #1#2
1289 {
1290 \tl_if_in:nnTF {#1} { " }
1291 {
1292 \msg_error:nne
1293 { kernel } { quote-in-shell } {#1}
1294 }
1295 { \__kernel_ior_open:Nn #2 { |#1 } }
1296 }
1297 \cs_generate_variant:Nn \@@_shell_open:nN { o }
1298 \msg_new:nnnn { kernel } { pipe-failed }
1299 { Cannot~run~piped~system~commands. }
1300 {
1301 LaTeX~tried~to~call~a~system~process~but~this~was~not~possible.\\
1302 Try~the~"--shell-escape"~(or~"--enable-pipes")~option.
1303 }
1304 % \end{macrocode}
1305 % \end{macro}
1306 % \end{macro}
1307 %
1308 % \begin{macro}{\ior_close:N, \ior_close:c}
1309 % Closing a stream means getting rid of it at the \TeX{} level and
1310 % removing from the various data structures. Unless the name passed
1311 % is an invalid stream number (outside the range $[0,15]$), it can be
1312 % closed. On the other hand, it only gets added to the stack if it
1313 % was not already there, to avoid duplicates building up.
1314 % \begin{macrocode}
1315 \cs_new_protected:Npn \ior_close:N #1
1316 {
1317 \int_compare:nT { -1 < #1 < \c_@@_term_ior }
1318 {
1319 \tex_closein:D #1
1320 \prop_gremove:NV \g_@@_streams_prop #1
1321 \seq_if_in:NVF \g_@@_streams_seq #1
1322 { \seq_gpush:NV \g_@@_streams_seq #1 }
1323 \cs_gset_eq:NN #1 \c_@@_term_ior
1324 }
1325 }
1326 \cs_generate_variant:Nn \ior_close:N { c }
1327 % \end{macrocode}
1328 % \end{macro}
1329 %
1330 % \begin{macro}{\ior_show:N, \ior_log:N, \@@_show:NN}
1331 % Seek the stream in the \cs{g_@@_streams_prop} list, then show the
1332 % stream as open or closed accordingly.
1333 % \begin{macrocode}
1334 \cs_new_protected:Npn \ior_show:N { \@@_show:NN \tl_show:n }
1335 \cs_generate_variant:Nn \ior_show:N { c }
1336 \cs_new_protected:Npn \ior_log:N { \@@_show:NN \tl_log:n }
1337 \cs_generate_variant:Nn \ior_log:N { c }
1338 \cs_new_protected:Npn \@@_show:NN #1#2
1339 {
1340 \__kernel_chk_defined:NT #2
1341 {
1342 \prop_get:NVNTF \g_@@_streams_prop #2 \l_@@_tmp_tl
1343 {
1344 \exp_args:Ne #1
1345 { \token_to_str:N #2 ~ open: ~ \l_@@_tmp_tl }
1346 }
1347 { \exp_args:Ne #1 { \token_to_str:N #2 ~ closed } }
1348 }
1349 }
1350 % \end{macrocode}
1351 % \end{macro}
1352 %
1353 % \begin{macro}{\ior_show_list:, \ior_log_list:}
1354 % \begin{macro}{\@@_list:N}
1355 % Show the property lists, but with some \enquote{pretty printing}.
1356 % See the \pkg{l3msg} module. The first argument of the message is
1357 % |ior| (as opposed to |iow|) and the second is empty if no read
1358 % stream is open and non-empty (the list of streams formatted using
1359 % \cs{msg_show_item_unbraced:nn}) otherwise. The code of the message
1360 % \texttt{show-streams} takes care of translating |ior|/|iow| to
1361 % English.
1362 % \begin{macrocode}
1363 \cs_new_protected:Npn \ior_show_list: { \@@_list:N \msg_show:nneeee }
1364 \cs_new_protected:Npn \ior_log_list: { \@@_list:N \msg_log:nneeee }
1365 \cs_new_protected:Npn \@@_list:N #1
1366 {
1367 #1 { kernel } { show-streams }
1368 { ior }
1369 {
1370 \prop_map_function:NN \g_@@_streams_prop
1371 \msg_show_item_unbraced:nn
1372 }
1373 { } { }
1374 }
1375 % \end{macrocode}
1376 % \end{macro}
1377 % \end{macro}
1378 %
1379 % \subsubsection{Reading input}
1380 %
1381 % \begin{macro}{\if_eof:w}
1382 % The primitive conditional
1383 % \begin{macrocode}
1384 \cs_new_eq:NN \if_eof:w \tex_ifeof:D
1385 % \end{macrocode}
1386 % \end{macro}
1387 %
1388 % \begin{macro}[pTF]{\ior_if_eof:N}
1389 % To test if some particular input stream is exhausted the following
1390 % conditional is provided. The primitive test can only deal with
1391 % numbers in the range $[0,15]$ so we catch outliers (they are
1392 % exhausted).
1393 % \begin{macrocode}
1394 \prg_new_conditional:Npnn \ior_if_eof:N #1 { p , T , F , TF }
1395 {
1396 \if_int_compare:w -1 < #1
1397 \if_int_compare:w #1 < \c_@@_term_ior
1398 \if_eof:w #1
1399 \prg_return_true:
1400 \else:
1401 \prg_return_false:
1402 \fi:
1403 \else:
1404 \prg_return_true:
1405 \fi:
1406 \else:
1407 \prg_return_true:
1408 \fi:
1409 }
1410 % \end{macrocode}
1411 % \end{macro}
1412 %
1413 % \begin{macro}{\ior_get:NN, \@@_get:NN}
1414 % \begin{macro}[TF]{\ior_get:NN}
1415 % And here we read from files.
1416 % \begin{macrocode}
1417 \cs_new_protected:Npn \ior_get:NN #1#2
1418 { \ior_get:NNF #1 #2 { \tl_set:Nn #2 { \q_no_value } } }
1419 \cs_new_protected:Npn \@@_get:NN #1#2
1420 { \tex_read:D #1 to #2 }
1421 \prg_new_protected_conditional:Npnn \ior_get:NN #1#2 { T , F , TF }
1422 {
1423 \ior_if_eof:NTF #1
1424 { \prg_return_false: }
1425 {
1426 \@@_get:NN #1 #2
1427 \prg_return_true:
1428 }
1429 }
1430 % \end{macrocode}
1431 % \end{macro}
1432 % \end{macro}
1433 %
1434 % \begin{macro}{\ior_str_get:NN, \@@_str_get:NN}
1435 % \begin{macro}[TF]{\ior_str_get:NN}
1436 % Reading as strings is a more complicated wrapper, as we wish to
1437 % remove the endline character and restore it afterwards.
1438 % \begin{macrocode}
1439 \cs_new_protected:Npn \ior_str_get:NN #1#2
1440 { \ior_str_get:NNF #1 #2 { \tl_set:Nn #2 { \q_no_value } } }
1441 \cs_new_protected:Npn \@@_str_get:NN #1#2
1442 {
1443 \exp_args:Nno \use:n
1444 {
1445 \int_set:Nn \tex_endlinechar:D { -1 }
1446 \tex_readline:D #1 to #2
1447 \int_set:Nn \tex_endlinechar:D
1448 } { \int_use:N \tex_endlinechar:D }
1449 }
1450 \prg_new_protected_conditional:Npnn \ior_str_get:NN #1#2 { T , F , TF }
1451 {
1452 \ior_if_eof:NTF #1
1453 { \prg_return_false: }
1454 {
1455 \@@_str_get:NN #1 #2
1456 \prg_return_true:
1457 }
1458 }
1459 % \end{macrocode}
1460 % \end{macro}
1461 % \end{macro}
1462 %
1463 % \begin{variable}{\c_@@_term_noprompt_ior}
1464 % For reading without a prompt.
1465 % \begin{macrocode}
1466 \int_const:Nn \c_@@_term_noprompt_ior { -1 }
1467 % \end{macrocode}
1468 % \end{variable}
1469 %
1470 % \begin{macro}{\ior_get_term:nN, \ior_str_get_term:nN}
1471 % \begin{macro}{\@@_get_term:NnN}
1472 % Getting from the terminal is better with pretty-printing.
1473 % \begin{macrocode}
1474 \cs_new_protected:Npn \ior_get_term:nN #1#2
1475 { \@@_get_term:NnN \@@_get:NN {#1} #2 }
1476 \cs_new_protected:Npn \ior_str_get_term:nN #1#2
1477 { \@@_get_term:NnN \@@_str_get:NN {#1} #2 }
1478 \cs_new_protected:Npn \@@_get_term:NnN #1#2#3
1479 {
1480 \group_begin:
1481 \tex_escapechar:D = -1 \scan_stop:
1482 \tl_if_blank:nTF {#2}
1483 { \exp_args:NNc #1 \c_@@_term_noprompt_ior }
1484 { \exp_args:NNc #1 \c_@@_term_ior }
1485 {#2}
1486 \exp_args:NNNv \group_end:
1487 \tl_set:Nn #3 {#2}
1488 }
1489 % \end{macrocode}
1490 % \end{macro}
1491 % \end{macro}
1492 %
1493 % \begin{macro}[EXP]{\ior_map_break:, \ior_map_break:n}
1494 % Usual map breaking functions.
1495 % \begin{macrocode}
1496 \cs_new:Npn \ior_map_break:
1497 { \prg_map_break:Nn \ior_map_break: { } }
1498 \cs_new:Npn \ior_map_break:n
1499 { \prg_map_break:Nn \ior_map_break: }
1500 % \end{macrocode}
1501 % \end{macro}
1502 %
1503 % \begin{macro}{\ior_map_inline:Nn, \ior_str_map_inline:Nn}
1504 % \begin{macro}{\@@_map_inline:NNn}
1505 % \begin{macro}{\@@_map_inline:NNNn}
1506 % \begin{macro}{\@@_map_inline_loop:NNN}
1507 % Mapping over an input stream can be done on either a token or a string
1508 % basis, hence the set up. Within that, there is a check to avoid reading
1509 % past the end of a file, hence the two applications of \cs{ior_if_eof:N}
1510 % and its lower-level analogue \cs{if_eof:w}.
1511 % This mapping cannot be nested with twice the same stream, as the
1512 % stream has only one \enquote{current line}.
1513 % \begin{macrocode}
1514 \cs_new_protected:Npn \ior_map_inline:Nn
1515 { \@@_map_inline:NNn \@@_get:NN }
1516 \cs_new_protected:Npn \ior_str_map_inline:Nn
1517 { \@@_map_inline:NNn \@@_str_get:NN }
1518 \cs_new_protected:Npn \@@_map_inline:NNn
1519 {
1520 \int_gincr:N \g__kernel_prg_map_int
1521 \exp_args:Nc \@@_map_inline:NNNn
1522 { @@_map_ \int_use:N \g__kernel_prg_map_int :n }
1523 }
1524 \cs_new_protected:Npn \@@_map_inline:NNNn #1#2#3#4
1525 {
1526 \cs_gset_protected:Npn #1 ##1 {#4}
1527 \ior_if_eof:NF #3 { \@@_map_inline_loop:NNN #1#2#3 }
1528 \prg_break_point:Nn \ior_map_break:
1529 { \int_gdecr:N \g__kernel_prg_map_int }
1530 }
1531 \cs_new_protected:Npn \@@_map_inline_loop:NNN #1#2#3
1532 {
1533 #2 #3 \l_@@_tmp_tl
1534 \if_eof:w #3
1535 \exp_after:wN \ior_map_break:
1536 \fi:
1537 \exp_args:No #1 \l_@@_tmp_tl
1538 \@@_map_inline_loop:NNN #1#2#3
1539 }
1540 % \end{macrocode}
1541 % \end{macro}
1542 % \end{macro}
1543 % \end{macro}
1544 % \end{macro}
1545 %
1546 % \begin{macro}{\ior_map_variable:NNn, \ior_str_map_variable:NNn}
1547 % \begin{macro}{\@@_map_variable:NNNn}
1548 % \begin{macro}{\@@_map_variable_loop:NNNn}
1549 % Since the \TeX{} primitive (\tn{read} or \tn{readline}) assigns the
1550 % tokens read in the same way as a token list assignment, we simply
1551 % call the appropriate primitive. The end-of-loop is checked using
1552 % the primitive conditional for speed.
1553 % \begin{macrocode}
1554 \cs_new_protected:Npn \ior_map_variable:NNn
1555 { \@@_map_variable:NNNn \ior_get:NN }
1556 \cs_new_protected:Npn \ior_str_map_variable:NNn
1557 { \@@_map_variable:NNNn \ior_str_get:NN }
1558 \cs_new_protected:Npn \@@_map_variable:NNNn #1#2#3#4
1559 {
1560 \ior_if_eof:NF #2 { \@@_map_variable_loop:NNNn #1#2#3 {#4} }
1561 \prg_break_point:Nn \ior_map_break: { }
1562 }
1563 \cs_new_protected:Npn \@@_map_variable_loop:NNNn #1#2#3#4
1564 {
1565 #1 #2 #3
1566 \if_eof:w #2
1567 \exp_after:wN \ior_map_break:
1568 \fi:
1569 #4
1570 \@@_map_variable_loop:NNNn #1#2#3 {#4}
1571 }
1572 % \end{macrocode}
1573 % \end{macro}
1574 % \end{macro}
1575 % \end{macro}
1576 %
1577 % \subsection{Output operations}
1578 %
1579 % \begin{macrocode}
1580 %<@@=iow>
1581 % \end{macrocode}
1582 %
1583 % There is a lot of similarity here to the input operations, at least for
1584 % many of the basics. Thus quite a bit is copied from the earlier material
1585 % with minor alterations.
1586 %
1587 % \subsubsection{Variables and constants}
1588 %
1589 % \begin{variable}{\l_@@_tmp_tl}
1590 % Used as a short-term scratch variable.
1591 % \begin{macrocode}
1592 \tl_new:N \l_@@_tmp_tl
1593 % \end{macrocode}
1594 % \end{variable}
1595 %
1596 % \begin{variable}{\c_log_iow, \c_term_iow}
1597 % Here we allocate two output streams for writing to the transcript
1598 % file only (\cs{c_log_iow}) and to both the terminal and transcript
1599 % file (\cs{c_term_iow}). Recent \LuaTeX{} provide $128$ write
1600 % streams; we also use \cs{c_term_iow} as the first non-allowed write
1601 % stream so its value depends on the engine.
1602 % \begin{macrocode}
1603 \int_const:Nn \c_log_iow { -1 }
1604 \int_const:Nn \c_term_iow
1605 {
1606 \bool_lazy_and:nnTF
1607 { \sys_if_engine_luatex_p: }
1608 { \int_compare_p:nNn \tex_luatexversion:D > { 80 } }
1609 { 128 }
1610 { 16 }
1611 }
1612 % \end{macrocode}
1613 % \end{variable}
1614 %
1615 % \begin{variable}{\g_@@_streams_seq}
1616 % A list of the currently-available output streams to be used as a
1617 % stack.
1618 % \begin{macrocode}
1619 \seq_new:N \g_@@_streams_seq
1620 % \end{macrocode}
1621 % \end{variable}
1622 %
1623 % \begin{variable}{\l_@@_stream_tl}
1624 % Used to recover the raw stream number from the stack.
1625 % \begin{macrocode}
1626 \tl_new:N \l_@@_stream_tl
1627 % \end{macrocode}
1628 % \end{variable}
1629 %
1630 % \begin{variable}{\g_@@_streams_prop}
1631 % As for reads with the appropriate adjustment of the register numbers to
1632 % check on.
1633 % \begin{macrocode}
1634 \prop_new:N \g_@@_streams_prop
1635 \int_step_inline:nnn
1636 { 0 }
1637 {
1638 \cs_if_exist:NTF \contextversion
1639 { \tex_count:D 39 ~ }
1640 {
1641 \tex_count:D 17 ~
1642 \cs_if_exist:NT \loccount { - 1 }
1643 }
1644 }
1645 {
1646 \prop_gput:Nnn \g_@@_streams_prop {#1} { Reserved~by~format }
1647 }
1648 % \end{macrocode}
1649 % \end{variable}
1650 %
1651 % \subsubsection{Internal auxiliaries}
1652 %
1653 % \begin{variable}{\s_@@_mark,\s_@@_stop}
1654 % Internal scan marks.
1655 % \begin{macrocode}
1656 \scan_new:N \s_@@_mark
1657 \scan_new:N \s_@@_stop
1658 % \end{macrocode}
1659 % \end{variable}
1660 %
1661 % \begin{macro}[EXP]{\@@_use_i_delimit_by_s_stop:nw}
1662 % Functions to gobble up to a scan mark.
1663 % \begin{macrocode}
1664 \cs_new:Npn \@@_use_i_delimit_by_s_stop:nw #1 #2 \s_@@_stop {#1}
1665 % \end{macrocode}
1666 % \end{macro}
1667 %
1668 % \begin{variable}{\q_@@_nil}
1669 % Internal quarks.
1670 % \begin{macrocode}
1671 \quark_new:N \q_@@_nil
1672 % \end{macrocode}
1673 % \end{variable}
1674 %
1675 % \subsection{Stream management}
1676 %
1677 % \begin{macro}{\iow_new:N, \iow_new:c}
1678 % Reserving a new stream is done by defining the name as equal to writing
1679 % to the terminal: odd but at least consistent.
1680 % \begin{macrocode}
1681 \cs_new_protected:Npn \iow_new:N #1 { \cs_new_eq:NN #1 \c_term_iow }
1682 \cs_generate_variant:Nn \iow_new:N { c }
1683 % \end{macrocode}
1684 % \end{macro}
1685 %
1686 % \begin{variable}{\g_tmpa_iow, \g_tmpb_iow}
1687 % The usual scratch space.
1688 % \begin{macrocode}
1689 \iow_new:N \g_tmpa_iow
1690 \iow_new:N \g_tmpb_iow
1691 % \end{macrocode}
1692 % \end{variable}
1693 %
1694 % \begin{macro}{\@@_new:N}
1695 % As for read streams, copy \tn{newwrite}, making sure
1696 % that it is not \tn{outer}. For \ConTeXt{}, we have to
1697 % deal with the fact that \tn{newwrite} works like our
1698 % own: it actually checks before altering definition.
1699 % \begin{macrocode}
1700 \exp_args:NNf \cs_new_protected:Npn \@@_new:N
1701 { \exp_args:NNc \exp_after:wN \exp_stop_f: { newwrite } }
1702 \cs_if_exist:NT \contextversion
1703 {
1704 \cs_new_eq:NN \@@_new_aux:N \@@_new:N
1705 \cs_gset_protected:Npn \@@_new:N #1
1706 {
1707 \cs_undefine:N #1
1708 \@@_new_aux:N #1
1709 }
1710 }
1711 % \end{macrocode}
1712 % \end{macro}
1713 %
1714 % \begin{variable}{\l_@@_file_name_tl}
1715 % Data storage.
1716 % \begin{macrocode}
1717 \tl_new:N \l_@@_file_name_tl
1718 % \end{macrocode}
1719 % \end{variable}
1720 %
1721 % \begin{macro}{\iow_open:Nn, \iow_open:NV, \iow_open:cn, \iow_open:cV}
1722 % \begin{macro}{\__kernel_iow_open:Nn, \__kernel_iow_open:No}
1723 % \begin{macro}{\@@_open_stream:Nn, \@@_open_stream:NV}
1724 % The same idea as for reading, but without the path and without the need
1725 % to allow for a conditional version.
1726 % \begin{macrocode}
1727 \cs_new_protected:Npn \iow_open:Nn #1#2
1728 {
1729 \__kernel_tl_set:Nx \l_@@_file_name_tl
1730 { \__kernel_file_name_sanitize:n {#2} }
1731 \__kernel_iow_open:No #1 \l_@@_file_name_tl
1732 }
1733 \cs_generate_variant:Nn \iow_open:Nn { NV , c , cV }
1734 \cs_new_protected:Npn \__kernel_iow_open:Nn #1#2
1735 {
1736 \iow_close:N #1
1737 \seq_gpop:NNTF \g_@@_streams_seq \l_@@_stream_tl
1738 { \@@_open_stream:Nn #1 {#2} }
1739 {
1740 \@@_new:N #1
1741 \__kernel_tl_set:Nx \l_@@_stream_tl { \int_eval:n {#1} }
1742 \@@_open_stream:Nn #1 {#2}
1743 }
1744 }
1745 \cs_generate_variant:Nn \__kernel_iow_open:Nn { No }
1746 \cs_new_protected:Npn \@@_open_stream:Nn #1#2
1747 {
1748 \tex_global:D \tex_chardef:D #1 = \l_@@_stream_tl \scan_stop:
1749 \prop_gput:NVn \g_@@_streams_prop #1 {#2}
1750 \tex_immediate:D \tex_openout:D
1751 #1 \__kernel_file_name_quote:n {#2} \scan_stop:
1752 }
1753 \cs_generate_variant:Nn \@@_open_stream:Nn { NV }
1754 % \end{macrocode}
1755 % \end{macro}
1756 % \end{macro}
1757 % \end{macro}
1758 %
1759 % \begin{macro}{\iow_shell_open:Nn}
1760 % \begin{macro}{\@@_shell_open:nN, \@@_shell_open:oN}
1761 % Very similar to the \texttt{ior} version
1762 % \begin{macrocode}
1763 \cs_new_protected:Npn \iow_shell_open:Nn #1#2
1764 {
1765 \sys_if_shell:TF
1766 { \@@_shell_open:oN { \tl_to_str:n {#2} } #1 }
1767 { \msg_error:nn { kernel } { pipe-failed } }
1768 }
1769 \cs_new_protected:Npn \@@_shell_open:nN #1#2
1770 {
1771 \tl_if_in:nnTF {#1} { " }
1772 {
1773 \msg_error:nne
1774 { kernel } { quote-in-shell } {#1}
1775 }
1776 { \__kernel_iow_open:Nn #2 { |#1 } }
1777 }
1778 \cs_generate_variant:Nn \@@_shell_open:nN { o }
1779 % \end{macrocode}
1780 % \end{macro}
1781 % \end{macro}
1782 %
1783 % \begin{macro}{\iow_close:N, \iow_close:c}
1784 % Closing a stream is not quite the reverse of opening one. First,
1785 % the close operation is easier than the open one, and second as the
1786 % stream is actually a number we can use it directly to show that the
1787 % slot has been freed up.
1788 % \begin{macrocode}
1789 \cs_new_protected:Npn \iow_close:N #1
1790 {
1791 \int_compare:nT { \c_log_iow < #1 < \c_term_iow }
1792 {
1793 \tex_immediate:D \tex_closeout:D #1
1794 \prop_gremove:NV \g_@@_streams_prop #1
1795 \seq_if_in:NVF \g_@@_streams_seq #1
1796 { \seq_gpush:NV \g_@@_streams_seq #1 }
1797 \cs_gset_eq:NN #1 \c_term_iow
1798 }
1799 }
1800 \cs_generate_variant:Nn \iow_close:N { c }
1801 % \end{macrocode}
1802 % \end{macro}
1803 %
1804 % \begin{macro}{\iow_show:N, \iow_log:N, \@@_show:NN}
1805 % Seek the stream in the \cs{g_@@_streams_prop} list, then show the
1806 % stream as open or closed accordingly.
1807 % \begin{macrocode}
1808 \cs_new_protected:Npn \iow_show:N { \@@_show:NN \tl_show:n }
1809 \cs_generate_variant:Nn \iow_show:N { c }
1810 \cs_new_protected:Npn \iow_log:N { \@@_show:NN \tl_log:n }
1811 \cs_generate_variant:Nn \iow_log:N { c }
1812 \cs_new_protected:Npn \@@_show:NN #1#2
1813 {
1814 \__kernel_chk_defined:NT #2
1815 {
1816 \prop_get:NVNTF \g_@@_streams_prop #2 \l_@@_tmp_tl
1817 {
1818 \exp_args:Ne #1
1819 { \token_to_str:N #2 ~ open: ~ \l_@@_tmp_tl }
1820 }
1821 { \exp_args:Ne #1 { \token_to_str:N #2 ~ closed } }
1822 }
1823 }
1824 % \end{macrocode}
1825 % \end{macro}
1826 %
1827 % \begin{macro}{\iow_show_list:, \iow_log_list:}
1828 % \begin{macro}{\@@_list:N}
1829 % Done as for input, but with a copy of the auxiliary so the name is correct.
1830 % \begin{macrocode}
1831 \cs_new_protected:Npn \iow_show_list: { \@@_list:N \msg_show:nneeee }
1832 \cs_new_protected:Npn \iow_log_list: { \@@_list:N \msg_log:nneeee }
1833 \cs_new_protected:Npn \@@_list:N #1
1834 {
1835 #1 { kernel } { show-streams }
1836 { iow }
1837 {
1838 \prop_map_function:NN \g_@@_streams_prop
1839 \msg_show_item_unbraced:nn
1840 }
1841 { } { }
1842 }
1843 % \end{macrocode}
1844 % \end{macro}
1845 % \end{macro}
1846 %
1847 % \subsubsection{Deferred writing}
1848 %
1849 % \begin{macro}
1850 % {
1851 % \iow_shipout_e:Nn, \iow_shipout_e:Ne,
1852 % \iow_shipout_e:cn, \iow_shipout_e:ce
1853 % }
1854 % First the easy part, this is the primitive, which expects its
1855 % argument to be braced.
1856 % \begin{macrocode}
1857 \cs_new_protected:Npn \iow_shipout_e:Nn #1#2
1858 { \tex_write:D #1 {#2} }
1859 \cs_generate_variant:Nn \iow_shipout_e:Nn { Ne , c, ce }
1860 % \end{macrocode}
1861 % \end{macro}
1862 %
1863 % \begin{macro}
1864 % {
1865 % \iow_shipout:Nn, \iow_shipout:Ne,
1866 % \iow_shipout:Nx,
1867 % \iow_shipout:cn, \iow_shipout:ce,
1868 % \iow_shipout:cx
1869 % }
1870 % With \eTeX{} available deferred writing without expansion is easy.
1871 % \begin{macrocode}
1872 \cs_new_protected:Npn \iow_shipout:Nn #1#2
1873 { \tex_write:D #1 { \exp_not:n {#2} } }
1874 \cs_generate_variant:Nn \iow_shipout:Nn { Ne , c, ce }
1875 \cs_generate_variant:Nn \iow_shipout:Nn { Nx , cx }
1876 % \end{macrocode}
1877 % \end{macro}
1878 %
1879 % \subsubsection{Immediate writing}
1880 %
1881 % \begin{macro}{\__kernel_iow_with:Nnn}
1882 % \begin{macro}{\@@_with:nNnn, \@@_with:oNnn}
1883 % If the integer~|#1| is equal to~|#2|, just leave~|#3| in the input
1884 % stream. Otherwise, pass the old value to an auxiliary, which sets
1885 % the integer to the new value, runs the code, and restores the
1886 % integer.
1887 % \begin{macrocode}
1888 \cs_new_protected:Npn \__kernel_iow_with:Nnn #1#2
1889 {
1890 \int_compare:nNnTF {#1} = {#2}
1891 { \use:n }
1892 { \@@_with:oNnn { \int_use:N #1 } #1 {#2} }
1893 }
1894 \cs_new_protected:Npn \@@_with:nNnn #1#2#3#4
1895 {
1896 \int_set:Nn #2 {#3}
1897 #4
1898 \int_set:Nn #2 {#1}
1899 }
1900 \cs_generate_variant:Nn \@@_with:nNnn { o }
1901 % \end{macrocode}
1902 % \end{macro}
1903 % \end{macro}
1904 %
1905 % \begin{macro}
1906 % {
1907 % \iow_now:Nn, \iow_now:NV, \iow_now:Ne,
1908 % \iow_now:Nx,
1909 % \iow_now:cn, \iow_now:cV, \iow_now:ce,
1910 % \iow_now:cx
1911 % }
1912 % This routine writes the second argument onto the output stream without
1913 % expansion. If this stream isn't open, the output goes to the terminal
1914 % instead. If the first argument is no output stream at all, we get an
1915 % internal error. We don't use the expansion done by \tn{write} to
1916 % get the |Nx| variant, because it differs in subtle ways from
1917 % \texttt{x}-expansion, namely, macro parameter characters would not
1918 % need to be doubled. We set the \tn{newlinechar} to~$10$ using
1919 % \cs{__kernel_iow_with:Nnn} to support formats such as plain \TeX{}: otherwise,
1920 % \cs{iow_newline:} would not work. We do not do this for
1921 % \cs{iow_shipout:Nn} or \cs{iow_shipout_x:Nn}, as \TeX{} looks at the
1922 % value of the \tn{newlinechar} at shipout time in those cases.
1923 % \begin{macrocode}
1924 \cs_new_protected:Npn \iow_now:Nn #1#2
1925 {
1926 \__kernel_iow_with:Nnn \tex_newlinechar:D { `\^^J }
1927 { \tex_immediate:D \tex_write:D #1 { \exp_not:n {#2} } }
1928 }
1929 \cs_generate_variant:Nn \iow_now:Nn { NV , Ne , c , cV , ce }
1930 \cs_generate_variant:Nn \iow_now:Nn { Nx , cx }
1931 % \end{macrocode}
1932 % \end{macro}
1933 %
1934 % \begin{macro}{\iow_log:n, \iow_log:e, \iow_log:x}
1935 % \begin{macro}{\iow_show:n, \iow_show:e}
1936 % \begin{macro}{\iow_term:n, \iow_term:e, \iow_term:x}
1937 % Writing to the log, the \tn{showstream}, and the terminal directly are
1938 % relatively easy.
1939 % \begin{macrocode}
1940 \cs_new_protected:Npn \iow_log:n { \iow_now:Nn \c_log_iow }
1941 \cs_new_protected:Npn \iow_log:e { \iow_now:Ne \c_log_iow }
1942 \cs_generate_variant:Nn \iow_log:n { x }
1943 \cs_new_protected:Npn \iow_show:n { \iow_now:Nn \tex_showstream:D }
1944 \cs_new_protected:Npn \iow_show:e { \iow_now:Ne \tex_showstream:D }
1945 \cs_new_protected:Npn \iow_term:n { \iow_now:Nn \c_term_iow }
1946 \cs_new_protected:Npn \iow_term:e { \iow_now:Ne \c_term_iow }
1947 \cs_generate_variant:Nn \iow_term:n { x }
1948 % \end{macrocode}
1949 % \end{macro}
1950 % \end{macro}
1951 % \end{macro}
1952 %
1953 % For the above to work, we have to have a non-negative value for
1954 % \tn{tex_showstream:D}, as \tn{tex_write:D} and
1955 % \tn{tex_show:D}/\tn{tex_showtokens:D} treat negative values differently.
1956 % This is needed for our approach to work at all, so we apply even when
1957 % \LaTeX{} is not in use. That doesn't work for LuaMeta\TeX{}, for the moment
1958 % we simply skip. (For Con\TeX{}t we really need a different approach to
1959 % showing messages anyway.)
1960 % \begin{macrocode}
1961 \cs_if_exist:NT \tex_showstream:D
1962 { \int_gset_eq:NN \tex_showstream:D \c_term_iow }
1963 % \end{macrocode}
1964 %
1965 % \subsubsection{Special characters for writing}
1966 %
1967 % \begin{macro}{\iow_newline:}
1968 % Global variable holding the character that forces a new line when
1969 % something is written to an output stream.
1970 % \begin{macrocode}
1971 \cs_new:Npn \iow_newline: { ^^J }
1972 % \end{macrocode}
1973 % \end{macro}
1974 %
1975 % \begin{macro}{\iow_char:N}
1976 % Function to write any escaped char to an output stream.
1977 % \begin{macrocode}
1978 \cs_new_eq:NN \iow_char:N \cs_to_str:N
1979 % \end{macrocode}
1980 % \end{macro}
1981 %
1982 % \subsubsection{Hard-wrapping lines to a character count}
1983 %
1984 % The code here implements a generic hard-wrapping function. This is
1985 % used by the messaging system, but is designed such that it is
1986 % available for other uses.
1987 %
1988 % \begin{variable}{\l_iow_line_count_int}
1989 % This is the \enquote{raw} number of characters in a line which
1990 % can be written to the terminal.
1991 % The standard value is the line length typically used by
1992 % \TeX{} Live and \hologo{MiKTeX}.
1993 % \begin{macrocode}
1994 \int_new:N \l_iow_line_count_int
1995 \int_set:Nn \l_iow_line_count_int { 78 }
1996 % \end{macrocode}
1997 % \end{variable}
1998 %
1999 % \begin{variable}{\l_@@_newline_tl}
2000 % The token list inserted to produce a new line, with the
2001 % \meta{run-on text}.
2002 % \begin{macrocode}
2003 \tl_new:N \l_@@_newline_tl
2004 % \end{macrocode}
2005 % \end{variable}
2006 %
2007 % \begin{variable}{\l_@@_line_target_int}
2008 % This stores the target line count: the full number of characters
2009 % in a line, minus any part for a leader at the start of each line.
2010 % \begin{macrocode}
2011 \int_new:N \l_@@_line_target_int
2012 % \end{macrocode}
2013 % \end{variable}
2014 %
2015 % \begin{macro}{\@@_set_indent:n}
2016 % \begin{macro}{\@@_unindent:w}
2017 % \begin{variable}{\l_@@_one_indent_tl, \l_@@_one_indent_int}
2018 % The \texttt{one_indent} variables hold one indentation marker and
2019 % its length. The \cs{@@_unindent:w} auxiliary removes one
2020 % indentation. The function \cs{@@_set_indent:n} (that could possibly
2021 % be public) sets the indentation in a consistent way. We set it to
2022 % four spaces by default.
2023 % \begin{macrocode}
2024 \tl_new:N \l_@@_one_indent_tl
2025 \int_new:N \l_@@_one_indent_int
2026 \cs_new:Npn \@@_unindent:w { }
2027 \cs_new_protected:Npn \@@_set_indent:n #1
2028 {
2029 \__kernel_tl_set:Nx \l_@@_one_indent_tl
2030 { \exp_args:No \__kernel_str_to_other_fast:n { \tl_to_str:n {#1} } }
2031 \int_set:Nn \l_@@_one_indent_int
2032 { \str_count:N \l_@@_one_indent_tl }
2033 \exp_last_unbraced:NNo
2034 \cs_set:Npn \@@_unindent:w \l_@@_one_indent_tl { }
2035 }
2036 \exp_args:Ne \@@_set_indent:n { \prg_replicate:nn { 4 } { ~ } }
2037 % \end{macrocode}
2038 % \end{variable}
2039 % \end{macro}
2040 % \end{macro}
2041 %
2042 % \begin{variable}{\l_@@_indent_tl, \l_@@_indent_int}
2043 % The current indentation (some copies of \cs{l_@@_one_indent_tl}) and
2044 % its number of characters.
2045 % \begin{macrocode}
2046 \tl_new:N \l_@@_indent_tl
2047 \int_new:N \l_@@_indent_int
2048 % \end{macrocode}
2049 % \end{variable}
2050 %
2051 % \begin{variable}{\l_@@_line_tl, \l_@@_line_part_tl}
2052 % These hold the current line of text and a partial line to be added
2053 % to it, respectively.
2054 % \begin{macrocode}
2055 \tl_new:N \l_@@_line_tl
2056 \tl_new:N \l_@@_line_part_tl
2057 % \end{macrocode}
2058 % \end{variable}
2059 %
2060 % \begin{variable}{\l_@@_line_break_bool}
2061 % Indicates whether the line was broken precisely at a chunk boundary.
2062 % \begin{macrocode}
2063 \bool_new:N \l_@@_line_break_bool
2064 % \end{macrocode}
2065 % \end{variable}
2066 %
2067 % \begin{variable}{\l_@@_wrap_tl}
2068 % Used for the expansion step before detokenizing, and for the output
2069 % from wrapping text: fully expanded and with lines which are not
2070 % overly long.
2071 % \begin{macrocode}
2072 \tl_new:N \l_@@_wrap_tl
2073 % \end{macrocode}
2074 % \end{variable}
2075 %
2076 % \begin{variable}{\c_@@_wrap_marker_tl}
2077 % \begin{variable}
2078 % {
2079 % \c_@@_wrap_end_marker_tl,
2080 % \c_@@_wrap_newline_marker_tl,
2081 % \c_@@_wrap_allow_break_marker_tl,
2082 % \c_@@_wrap_indent_marker_tl,
2083 % \c_@@_wrap_unindent_marker_tl
2084 % }
2085 % Every special action of the wrapping code is starts with
2086 % the same recognizable string, \cs{c_@@_wrap_marker_tl}.
2087 % Upon seeing that \enquote{word}, the wrapping code reads
2088 % one space-delimited argument to know what operation to
2089 % perform. The setting of \tn{escapechar} here is not
2090 % very important, but makes \cs{c_@@_wrap_marker_tl} look
2091 % marginally nicer.
2092 % \begin{macrocode}
2093 \group_begin:
2094 \int_set:Nn \tex_escapechar:D { -1 }
2095 \tl_const:Ne \c_@@_wrap_marker_tl
2096 { \tl_to_str:n { \^^I \^^O \^^W \^^_ \^^W \^^R \^^A \^^P } }
2097 \group_end:
2098 \tl_map_inline:nn
2099 { { end } { newline } { allow_break } { indent } { unindent } }
2100 {
2101 \tl_const:ce { c_@@_wrap_ #1 _marker_tl }
2102 {
2103 \c_@@_wrap_marker_tl
2104 #1
2105 \c_catcode_other_space_tl
2106 }
2107 }
2108 % \end{macrocode}
2109 % \end{variable}
2110 % \end{variable}
2111 %
2112 % \begin{macro}{\iow_wrap_allow_break:}
2113 % \begin{macro}[EXP]{\@@_wrap_allow_break:}
2114 % \begin{macro}[EXP]{\@@_wrap_allow_break_error:}
2115 % We set \cs{iow_wrap_allow_break:n} to produce an error when outside
2116 % messages. Within wrapped message, it is set to \cs{@@_wrap_allow_break:}
2117 % when valid and otherwise to \cs{@@_wrap_allow_break_error:}. The second
2118 % produces an error expandably.
2119 % \begin{macrocode}
2120 \cs_new_protected:Npn \iow_wrap_allow_break:
2121 {
2122 \msg_error:nnnn { kernel } { iow-indent }
2123 { \iow_wrap:nnnN } { \iow_wrap_allow_break: }
2124 }
2125 \cs_new:Npe \@@_wrap_allow_break: { \c_@@_wrap_allow_break_marker_tl }
2126 \cs_new:Npn \@@_wrap_allow_break_error:
2127 {
2128 \msg_expandable_error:nnnn { kernel } { iow-indent }
2129 { \iow_wrap:nnnN } { \iow_wrap_allow_break: }
2130 }
2131 % \end{macrocode}
2132 % \end{macro}
2133 % \end{macro}
2134 % \end{macro}
2135 %
2136 % \begin{macro}{\iow_indent:n}
2137 % \begin{macro}[EXP]{\@@_indent:n}
2138 % \begin{macro}[EXP]{\@@_indent_error:n}
2139 % We set \cs{iow_indent:n} to produce an error when outside
2140 % messages. Within wrapped message, it is set to \cs{@@_indent:n} when
2141 % valid and otherwise to \cs{@@_indent_error:n}. The first places the
2142 % instruction for increasing the indentation before its argument, and
2143 % the instruction for unindenting afterwards. The second produces an
2144 % error expandably. Note that there are no forced line-break, so
2145 % the indentation only changes when the next line is started.
2146 % \begin{macrocode}
2147 \cs_new_protected:Npn \iow_indent:n #1
2148 {
2149 \msg_error:nnnnn { kernel } { iow-indent }
2150 { \iow_wrap:nnnN } { \iow_indent:n } {#1}
2151 #1
2152 }
2153 \cs_new:Npe \@@_indent:n #1
2154 {
2155 \c_@@_wrap_indent_marker_tl
2156 #1
2157 \c_@@_wrap_unindent_marker_tl
2158 }
2159 \cs_new:Npn \@@_indent_error:n #1
2160 {
2161 \msg_expandable_error:nnnnn { kernel } { iow-indent }
2162 { \iow_wrap:nnnN } { \iow_indent:n } {#1}
2163 #1
2164 }
2165 % \end{macrocode}
2166 % \end{macro}
2167 % \end{macro}
2168 % \end{macro}
2169 %
2170 % \begin{macro}{\iow_wrap:nnnN, \iow_wrap:nenN}
2171 % The main wrapping function works as follows. First give |\\|,
2172 % \verb*|\ | and other formatting commands the correct definition for
2173 % messages and perform the given setup~|#3|. The definition of
2174 % \verb*|\ | uses an \enquote{other} space rather than a normal space,
2175 % because the latter might be absorbed by \TeX{} to end a number or
2176 % other \texttt{f}-type expansions.
2177 % Use \cs{conditionally@traceoff} if defined; it is introduced by the
2178 % \pkg{trace} package and suppresses uninteresting tracing of the
2179 % wrapping code.
2180 % \begin{macrocode}
2181 \cs_new_protected:Npn \iow_wrap:nnnN #1#2#3#4
2182 {
2183 \group_begin:
2184 \cs_if_exist_use:N \conditionally@traceoff
2185 \int_set:Nn \tex_escapechar:D { -1 }
2186 \cs_set:Npe \{ { \token_to_str:N \{ }
2187 \cs_set:Npe \# { \token_to_str:N \# }
2188 \cs_set:Npe \} { \token_to_str:N \} }
2189 \cs_set:Npe \% { \token_to_str:N \% }
2190 \cs_set:Npe \~ { \token_to_str:N \~ }
2191 \int_set:Nn \tex_escapechar:D { 92 }
2192 \cs_set_eq:NN \\ \iow_newline:
2193 \cs_set_eq:NN \ \c_catcode_other_space_tl
2194 \cs_set_eq:NN \iow_wrap_allow_break: \@@_wrap_allow_break:
2195 \cs_set_eq:NN \iow_indent:n \@@_indent:n
2196 #3
2197 % \end{macrocode}
2198 % Then fully-expand the input: in package mode, the expansion uses
2199 % \LaTeXe{}'s \tn{protect} mechanism in the same way as \tn{typeout}.
2200 % In generic mode this setting is useless but harmless. As soon as
2201 % the expansion is done, reset \cs{iow_indent:n} to its error
2202 % definition: it only works in the first argument of
2203 % \cs{iow_wrap:nnnN}.
2204 % \begin{macrocode}
2205 \cs_set_eq:NN \protect \token_to_str:N
2206 \__kernel_tl_set:Nx \l_@@_wrap_tl {#1}
2207 \cs_set_eq:NN \iow_wrap_allow_break: \@@_wrap_allow_break_error:
2208 \cs_set_eq:NN \iow_indent:n \@@_indent_error:n
2209 % \end{macrocode}
2210 % Afterwards, set the newline marker (two assignments to fully expand,
2211 % then convert to a string) and initialize the target count for lines
2212 % (the first line has target count \cs{l_iow_line_count_int} instead).
2213 % \begin{macrocode}
2214 \__kernel_tl_set:Nx \l_@@_newline_tl { \iow_newline: #2 }
2215 \__kernel_tl_set:Nx \l_@@_newline_tl { \tl_to_str:N \l_@@_newline_tl }
2216 \int_set:Nn \l_@@_line_target_int
2217 { \l_iow_line_count_int - \str_count:N \l_@@_newline_tl + 1 }
2218 % \end{macrocode}
2219 % Sanity check.
2220 % \begin{macrocode}
2221 \int_compare:nNnT { \l_@@_line_target_int } < 0
2222 {
2223 \tl_set:Nn \l_@@_newline_tl { \iow_newline: }
2224 \int_set:Nn \l_@@_line_target_int
2225 { \l_iow_line_count_int + 1 }
2226 }
2227 % \end{macrocode}
2228 % There is then a loop over the input, which stores the wrapped
2229 % result in \cs{l_@@_wrap_tl}. After the loop, the resulting text is
2230 % passed on to the function which has been given as a post-processor.
2231 % The \cs{tl_to_str:N} step converts the \enquote{other} spaces back
2232 % to normal spaces. The \texttt{f}-expansion removes a leading space
2233 % from \cs{l_@@_wrap_tl}.
2234 % \begin{macrocode}
2235 \@@_wrap_do:
2236 \exp_args:NNf \group_end:
2237 #4 { \tl_to_str:N \l_@@_wrap_tl }
2238 }
2239 \cs_generate_variant:Nn \iow_wrap:nnnN { ne }
2240 % \end{macrocode}
2241 % \end{macro}
2242 %
2243 % \begin{macro}{\@@_wrap_do:, \@@_wrap_fix_newline:w, \@@_wrap_start:w}
2244 % Escape spaces and change newlines to \cs{c_@@_wrap_newline_marker_tl}.
2245 % Set up a few variables, in particular the initial
2246 % value of \cs{l_@@_wrap_tl}: the space stops the
2247 % \texttt{f}-expansion of the main wrapping function and
2248 % \cs{use_none:n} removes a newline marker inserted by later code.
2249 % The main loop consists of repeatedly calling the \texttt{chunk}
2250 % auxiliary to wrap chunks delimited by (newline or indentation)
2251 % markers.
2252 % \begin{macrocode}
2253 \cs_new_protected:Npn \@@_wrap_do:
2254 {
2255 \__kernel_tl_set:Nx \l_@@_wrap_tl
2256 {
2257 \exp_args:No \__kernel_str_to_other_fast:n \l_@@_wrap_tl
2258 \c_@@_wrap_end_marker_tl
2259 }
2260 \__kernel_tl_set:Nx \l_@@_wrap_tl
2261 {
2262 \exp_after:wN \@@_wrap_fix_newline:w \l_@@_wrap_tl
2263 ^^J \q_@@_nil ^^J \s_@@_stop
2264 }
2265 \exp_after:wN \@@_wrap_start:w \l_@@_wrap_tl
2266 }
2267 \cs_new:Npn \@@_wrap_fix_newline:w #1 ^^J #2 ^^J
2268 {
2269 #1
2270 \if_meaning:w \q_@@_nil #2
2271 \@@_use_i_delimit_by_s_stop:nw
2272 \fi:
2273 \c_@@_wrap_newline_marker_tl
2274 \@@_wrap_fix_newline:w #2 ^^J
2275 }
2276 \cs_new_protected:Npn \@@_wrap_start:w
2277 {
2278 \bool_set_false:N \l_@@_line_break_bool
2279 \tl_clear:N \l_@@_line_tl
2280 \tl_clear:N \l_@@_line_part_tl
2281 \tl_set:Nn \l_@@_wrap_tl { ~ \use_none:n }
2282 \int_zero:N \l_@@_indent_int
2283 \tl_clear:N \l_@@_indent_tl
2284 \@@_wrap_chunk:nw { \l_iow_line_count_int }
2285 }
2286 % \end{macrocode}
2287 % \end{macro}
2288 %
2289 % \begin{macro}{\@@_sep:}
2290 % \begin{macrocode}
2291 \cs_new_eq:NN \@@_sep: \__kernel_int_sep:
2292 % \end{macrocode}
2293 % \end{macro}
2294 %
2295 % \begin{macro}{\@@_wrap_chunk:nw, \@@_wrap_next:nw}
2296 % The \texttt{chunk} and \texttt{next} auxiliaries are defined
2297 % indirectly to obtain the expansions of \cs{c_catcode_other_space_tl}
2298 % and \cs{c_@@_wrap_marker_tl} in their definition. The \texttt{next}
2299 % auxiliary calls a function corresponding to the type of marker (its
2300 % |##2|), which can be \texttt{newline} or \texttt{indent} or
2301 % \texttt{unindent} or \texttt{end}. The first argument of the
2302 % \texttt{chunk} auxiliary is a target number of characters and the
2303 % second is some string to wrap. If the chunk is empty simply call
2304 % \texttt{next}. Otherwise, set up a call to \cs{@@_wrap_line:nw},
2305 % including the indentation if the current line is empty, and
2306 % including a trailing space (|#1|) before the
2307 % \cs{@@_wrap_end_chunk:w} auxiliary.
2308 % \begin{macrocode}
2309 \cs_set_protected:Npn \@@_tmp:w #1#2
2310 {
2311 \cs_new_protected:Npn \@@_wrap_chunk:nw ##1##2 #2
2312 {
2313 \tl_if_empty:nTF {##2}
2314 {
2315 \tl_clear:N \l_@@_line_part_tl
2316 \@@_wrap_next:nw {##1}
2317 }
2318 {
2319 \tl_if_empty:NTF \l_@@_line_tl
2320 {
2321 \@@_wrap_line:nw
2322 { \l_@@_indent_tl }
2323 ##1 - \l_@@_indent_int \@@_sep:
2324 }
2325 { \@@_wrap_line:nw { } ##1 \@@_sep: }
2326 ##2 #1
2327 \@@_wrap_end_chunk:w 7 6 5 4 3 2 1 0 \s_@@_stop
2328 }
2329 }
2330 \cs_new_protected:Npn \@@_wrap_next:nw ##1##2 #1
2331 { \use:c { @@_wrap_##2:n } {##1} }
2332 }
2333 \exp_args:NVV \@@_tmp:w \c_catcode_other_space_tl \c_@@_wrap_marker_tl
2334 % \end{macrocode}
2335 % \end{macro}
2336 %
2337 % \begin{macro}{\@@_wrap_line:nw}
2338 % \begin{macro}[EXP]
2339 % {
2340 % \@@_wrap_line_loop:w,
2341 % \@@_wrap_line_aux:Nw,
2342 % \@@_wrap_line_seven:nnnnnnn,
2343 % \@@_wrap_line_end:NnnnnnnnN,
2344 % \@@_wrap_line_end:nw,
2345 % \@@_wrap_end_chunk:w
2346 % }
2347 % This is followed by \Arg{string} \meta{int expr} \cs{@@_sep:}. It stores the
2348 % \meta{string} and up to \meta{int expr} characters from the current
2349 % chunk into \cs{l_@@_line_part_tl}. Characters are grabbed 8~at a
2350 % time and left in \cs{l_@@_line_part_tl} by the \texttt{line_loop}
2351 % auxiliary. When $k<8$ remain to be found, the \texttt{line_aux}
2352 % auxiliary calls the \texttt{line_end} auxiliary followed by (the
2353 % single digit) $k$, then $7-k$ empty brace groups, then the chunk's
2354 % remaining characters. The \texttt{line_end} auxiliary leaves
2355 % $k$~characters from the chunk in the line part, then ends the
2356 % assignment. Ignore the \cs{use_none:nnnnn} line for now. If the
2357 % next character is a space the line can be broken there:
2358 % store what we found into the result and get the next line.
2359 % Otherwise some work is needed to find a break-point. So far we have
2360 % ignored what happens if the chunk is shorter than the requested
2361 % number of characters: this is dealt with by the \texttt{end_chunk}
2362 % auxiliary, which gets treated like a character by the rest of the
2363 % code. It ends up being called either as one of the arguments
2364 % |#2|--|#9| of the \texttt{line_loop} auxiliary or as one of the
2365 % arguments |#2|--|#8| of the \texttt{line_end} auxiliary. In both
2366 % cases stop the assignment and work out how many characters are still
2367 % needed. Notice that when we have exactly seven arguments to clean up,
2368 % a \cs{exp_stop_f:} has to be inserted to stop the \cs{exp:w}.
2369 % The weird \cs{use_none:nnnnn} ensures that the required
2370 % data is in the right place.
2371 % \begin{macrocode}
2372 \cs_new_protected:Npn \@@_wrap_line:nw #1
2373 {
2374 \tex_edef:D \l_@@_line_part_tl { \if_false: } \fi:
2375 #1
2376 \exp_after:wN \@@_wrap_line_loop:w
2377 \int_value:w \int_eval:w
2378 }
2379 \cs_new:Npn \@@_wrap_line_loop:w #1 \@@_sep: #2#3#4#5#6#7#8#9
2380 {
2381 \if_int_compare:w #1 < 8 \exp_stop_f:
2382 \@@_wrap_line_aux:Nw #1
2383 \fi:
2384 #2 #3 #4 #5 #6 #7 #8 #9
2385 \exp_after:wN \@@_wrap_line_loop:w
2386 \int_value:w \int_eval:w #1 - 8 \@@_sep:
2387 }
2388 \cs_new:Npn \@@_wrap_line_aux:Nw #1#2#3 \exp_after:wN #4 \@@_sep:
2389 {
2390 #2
2391 \exp_after:wN \@@_wrap_line_end:NnnnnnnnN
2392 \exp_after:wN #1
2393 \exp:w \exp_end_continue_f:w
2394 \exp_after:wN \exp_after:wN
2395 \if_case:w #1 \exp_stop_f:
2396 \prg_do_nothing:
2397 \or: \use_none:n
2398 \or: \use_none:nn
2399 \or: \use_none:nnn
2400 \or: \use_none:nnnn
2401 \or: \use_none:nnnnn
2402 \or: \use_none:nnnnnn
2403 \or: \@@_wrap_line_seven:nnnnnnn
2404 \fi:
2405 { } { } { } { } { } { } { } #3
2406 }
2407 \cs_new:Npn \@@_wrap_line_seven:nnnnnnn #1#2#3#4#5#6#7 { \exp_stop_f: }
2408 \cs_new:Npn \@@_wrap_line_end:NnnnnnnnN #1#2#3#4#5#6#7#8#9
2409 {
2410 #2 #3 #4 #5 #6 #7 #8
2411 \use_none:nnnnn \int_eval:w 8 - \@@_sep: #9
2412 \token_if_eq_charcode:NNTF \c_space_token #9
2413 { \@@_wrap_line_end:nw { } }
2414 { \if_false: { \fi: } \@@_wrap_break:w #9 }
2415 }
2416 \cs_new:Npn \@@_wrap_line_end:nw #1
2417 {
2418 \if_false: { \fi: }
2419 \@@_wrap_store_do:n {#1}
2420 \@@_wrap_next_line:w
2421 }
2422 \cs_new:Npn \@@_wrap_end_chunk:w
2423 #1 \int_eval:w #2 - #3 \@@_sep: #4#5 \s_@@_stop
2424 {
2425 \if_false: { \fi: }
2426 \exp_args:Nf \@@_wrap_next:nw { \int_eval:n { #2 - #4 } }
2427 }
2428 % \end{macrocode}
2429 % \end{macro}
2430 % \end{macro}
2431 %
2432 % \begin{macro}[EXP]{\@@_wrap_break:w}
2433 % \begin{macro}[EXP]
2434 % {
2435 % \@@_wrap_break_first:w,
2436 % \@@_wrap_break_none:w,
2437 % \@@_wrap_break_loop:w,
2438 % \@@_wrap_break_end:w,
2439 % }
2440 % Functions here are defined indirectly: \cs{@@_tmp:w} is eventually
2441 % called with an \enquote{other} space as its argument. The goal is
2442 % to remove from \cs{l_@@_line_part_tl} the part after the last space.
2443 % In most cases this is done by repeatedly calling the
2444 % \texttt{break_loop} auxiliary, which leaves \enquote{words}
2445 % (delimited by spaces) until it hits the trailing space: then its
2446 % argument |##3| is |?| \cs{@@_wrap_break_end:w} instead of a single
2447 % token, and that \texttt{break_end} auxiliary leaves in the
2448 % assignment the line until the last space, then calls
2449 % \cs{@@_wrap_line_end:nw} to finish up the line and move on to the
2450 % next. If there is no space in \cs{l_@@_line_part_tl} then the
2451 % \texttt{break_first} auxiliary calls the \texttt{break_none}
2452 % auxiliary. In that case, if the current line is empty, the complete
2453 % word (including |##4|, characters beyond what we had grabbed) is
2454 % added to the line, making it over-long. Otherwise, the word is
2455 % used for the following line (and the last space of the line so far
2456 % is removed because it was inserted due to the presence of a marker).
2457 % \begin{macrocode}
2458 \cs_set_protected:Npn \@@_tmp:w #1
2459 {
2460 \cs_new:Npn \@@_wrap_break:w
2461 {
2462 \tex_edef:D \l_@@_line_part_tl
2463 { \if_false: } \fi:
2464 \exp_after:wN \@@_wrap_break_first:w
2465 \l_@@_line_part_tl
2466 #1
2467 { ? \@@_wrap_break_end:w }
2468 \s_@@_mark
2469 }
2470 \cs_new:Npn \@@_wrap_break_first:w ##1 #1 ##2
2471 {
2472 \use_none:nn ##2 \@@_wrap_break_none:w
2473 \@@_wrap_break_loop:w ##1 #1 ##2
2474 }
2475 \cs_new:Npn \@@_wrap_break_none:w ##1##2 #1 ##3 \s_@@_mark ##4 #1
2476 {
2477 \tl_if_empty:NTF \l_@@_line_tl
2478 { ##2 ##4 \@@_wrap_line_end:nw { } }
2479 { \@@_wrap_line_end:nw { \@@_wrap_trim:N } ##2 ##4 #1 }
2480 }
2481 \cs_new:Npn \@@_wrap_break_loop:w ##1 #1 ##2 #1 ##3
2482 {
2483 \use_none:n ##3
2484 ##1 #1
2485 \@@_wrap_break_loop:w ##2 #1 ##3
2486 }
2487 \cs_new:Npn \@@_wrap_break_end:w ##1 #1 ##2 ##3 #1 ##4 \s_@@_mark
2488 { ##1 \@@_wrap_line_end:nw { } ##3 }
2489 }
2490 \exp_args:NV \@@_tmp:w \c_catcode_other_space_tl
2491 % \end{macrocode}
2492 % \end{macro}
2493 % \end{macro}
2494 %
2495 % \begin{macro}{\@@_wrap_next_line:w}
2496 % The special case where the end of a line coincides with the end of a
2497 % chunk is detected here, to avoid a spurious empty line. Otherwise,
2498 % call \cs{@@_wrap_line:nw} to find characters for the next line
2499 % (remembering to account for the indentation).
2500 % \begin{macrocode}
2501 \cs_new_protected:Npn \@@_wrap_next_line:w #1#2 \s_@@_stop
2502 {
2503 \tl_clear:N \l_@@_line_tl
2504 \token_if_eq_meaning:NNTF #1 \@@_wrap_end_chunk:w
2505 {
2506 \tl_clear:N \l_@@_line_part_tl
2507 \bool_set_true:N \l_@@_line_break_bool
2508 \@@_wrap_next:nw { \l_@@_line_target_int }
2509 }
2510 {
2511 \@@_wrap_line:nw
2512 { \l_@@_indent_tl }
2513 \l_@@_line_target_int - \l_@@_indent_int \@@_sep:
2514 #1 #2 \s_@@_stop
2515 }
2516 }
2517 % \end{macrocode}
2518 % \end{macro}
2519 %
2520 % \begin{macro}{\@@_wrap_allow_break:n}
2521 % This is called after a chunk has been wrapped. The
2522 % \cs{l_@@_line_part_tl} typically ends with a space (except at the
2523 % beginning of a line?), which we remove since the
2524 % \texttt{allow\_break} marker should not insert a space. Then move
2525 % on with the next chunk, making sure to adjust the target number of
2526 % characters for the line in case we did remove a space.
2527 % \begin{macrocode}
2528 \cs_new_protected:Npn \@@_wrap_allow_break:n #1
2529 {
2530 \__kernel_tl_set:Nx \l_@@_line_tl
2531 { \l_@@_line_tl \@@_wrap_trim:N \l_@@_line_part_tl }
2532 \bool_set_false:N \l_@@_line_break_bool
2533 \tl_if_empty:NTF \l_@@_line_part_tl
2534 { \@@_wrap_chunk:nw {#1} }
2535 { \exp_args:Nf \@@_wrap_chunk:nw { \int_eval:n { #1 + 1 } } }
2536 }
2537 % \end{macrocode}
2538 % \end{macro}
2539 %
2540 % \begin{macro}{\@@_wrap_indent:n, \@@_wrap_unindent:n}
2541 % These functions are called after a chunk has been wrapped, when
2542 % encountering \texttt{indent}/\texttt{unindent} markers. Add the
2543 % line part (last line part of the previous chunk) to the line so far
2544 % and reset a boolean denoting the presence of a line-break. Most
2545 % importantly, add or remove one indent from the current indent (both
2546 % the integer and the token list). Finally, continue wrapping.
2547 % \begin{macrocode}
2548 \cs_new_protected:Npn \@@_wrap_indent:n #1
2549 {
2550 \tl_put_right:Ne \l_@@_line_tl { \l_@@_line_part_tl }
2551 \bool_set_false:N \l_@@_line_break_bool
2552 \int_add:Nn \l_@@_indent_int { \l_@@_one_indent_int }
2553 \tl_put_right:No \l_@@_indent_tl { \l_@@_one_indent_tl }
2554 \@@_wrap_chunk:nw {#1}
2555 }
2556 \cs_new_protected:Npn \@@_wrap_unindent:n #1
2557 {
2558 \tl_put_right:Ne \l_@@_line_tl { \l_@@_line_part_tl }
2559 \bool_set_false:N \l_@@_line_break_bool
2560 \int_sub:Nn \l_@@_indent_int { \l_@@_one_indent_int }
2561 \__kernel_tl_set:Nx \l_@@_indent_tl
2562 { \exp_after:wN \@@_unindent:w \l_@@_indent_tl }
2563 \@@_wrap_chunk:nw {#1}
2564 }
2565 % \end{macrocode}
2566 % \end{macro}
2567 %
2568 % \begin{macro}{\@@_wrap_newline:n, \@@_wrap_end:n}
2569 % These functions are called after a chunk has been line-wrapped, when
2570 % encountering a \texttt{newline}/\texttt{end} marker. Unless we just
2571 % took a line-break, store the line part and the line so far into the
2572 % whole \cs{l_@@_wrap_tl}, trimming a trailing space. In the
2573 % \texttt{newline} case look for a new line (of length
2574 % \cs{l_@@_line_target_int}) in a new chunk.
2575 % \begin{macrocode}
2576 \cs_new_protected:Npn \@@_wrap_newline:n #1
2577 {
2578 \bool_if:NF \l_@@_line_break_bool
2579 { \@@_wrap_store_do:n { \@@_wrap_trim:N } }
2580 \bool_set_false:N \l_@@_line_break_bool
2581 \@@_wrap_chunk:nw { \l_@@_line_target_int }
2582 }
2583 \cs_new_protected:Npn \@@_wrap_end:n #1
2584 {
2585 \bool_if:NF \l_@@_line_break_bool
2586 { \@@_wrap_store_do:n { \@@_wrap_trim:N } }
2587 \bool_set_false:N \l_@@_line_break_bool
2588 }
2589 % \end{macrocode}
2590 % \end{macro}
2591 %
2592 % \begin{macro}{\@@_wrap_store_do:n}
2593 % First add the last line part to the line, then append it to
2594 % \cs{l_@@_wrap_tl} with the appropriate new line (with
2595 % \enquote{run-on} text), possibly with its last space removed (|#1|
2596 % is empty or \cs{@@_wrap_trim:N}).
2597 % \begin{macrocode}
2598 \cs_new_protected:Npn \@@_wrap_store_do:n #1
2599 {
2600 \__kernel_tl_set:Nx \l_@@_line_tl
2601 { \l_@@_line_tl \l_@@_line_part_tl }
2602 \__kernel_tl_set:Nx \l_@@_wrap_tl
2603 {
2604 \l_@@_wrap_tl
2605 \l_@@_newline_tl
2606 #1 \l_@@_line_tl
2607 }
2608 \tl_clear:N \l_@@_line_tl
2609 }
2610 % \end{macrocode}
2611 % \end{macro}
2612 %
2613 % \begin{macro}[EXP]{\@@_wrap_trim:N, \@@_wrap_trim:w, \@@_wrap_trim_aux:w}
2614 % Remove one trailing \enquote{other} space from the argument if present.
2615 % \begin{macrocode}
2616 \cs_set_protected:Npn \@@_tmp:w #1
2617 {
2618 \cs_new:Npn \@@_wrap_trim:N ##1
2619 { \exp_after:wN \@@_wrap_trim:w ##1 \s_@@_mark #1 \s_@@_mark \s_@@_stop }
2620 \cs_new:Npn \@@_wrap_trim:w ##1 #1 \s_@@_mark
2621 { \@@_wrap_trim_aux:w ##1 \s_@@_mark }
2622 \cs_new:Npn \@@_wrap_trim_aux:w ##1 \s_@@_mark ##2 \s_@@_stop {##1}
2623 }
2624 \exp_args:NV \@@_tmp:w \c_catcode_other_space_tl
2625 % \end{macrocode}
2626 % \end{macro}
2627 %
2628 % \begin{macrocode}
2629 %<@@=file>
2630 % \end{macrocode}
2631 %
2632 % \subsection{File operations}
2633 %
2634 % \begin{variable}{\l_@@_tmp_tl}
2635 % Used as a short-term scratch variable.
2636 % \begin{macrocode}
2637 \tl_new:N \l_@@_tmp_tl
2638 % \end{macrocode}
2639 % \end{variable}
2640 %
2641 % \begin{variable}
2642 % {
2643 % \g_file_curr_dir_str ,
2644 % \g_file_curr_ext_str ,
2645 % \g_file_curr_name_str
2646 % }
2647 % The name of the current file should be available at all times:
2648 % the name itself is set dynamically.
2649 % \begin{macrocode}
2650 \str_new:N \g_file_curr_dir_str
2651 \str_new:N \g_file_curr_ext_str
2652 \str_new:N \g_file_curr_name_str
2653 % \end{macrocode}
2654 % \end{variable}
2655 %
2656 % \begin{variable}{\g_@@_stack_seq}
2657 % The input list of files is stored as a sequence stack. In package
2658 % mode we can recover the information from the details held by
2659 % \LaTeXe{} (we must be in the preamble and loaded using \tn{usepackage}
2660 % or \tn{RequirePackage}). As \LaTeXe{} doesn't store directory and
2661 % name separately, we stick to the same convention here. In pre-loading,
2662 % \tn{@currnamestack} is empty so is skipped.
2663 % \begin{macrocode}
2664 \seq_new:N \g_@@_stack_seq
2665 \group_begin:
2666 \cs_set_protected:Npn \@@_tmp:w #1#2#3
2667 {
2668 \tl_if_blank:nTF {#1}
2669 {
2670 \cs_set:Npn \@@_tmp:w ##1 " ##2 " ##3 \s_@@_stop
2671 { { } {##2} { } }
2672 \seq_gput_right:Ne \g_@@_stack_seq
2673 {
2674 \exp_after:wN \@@_tmp:w \tex_jobname:D
2675 " \tex_jobname:D " \s_@@_stop
2676 }
2677 }
2678 {
2679 \seq_gput_right:Nn \g_@@_stack_seq { { } {#1} {#2} }
2680 \@@_tmp:w
2681 }
2682 }
2683 \cs_if_exist:NT \@currnamestack
2684 {
2685 \tl_if_empty:NF \@currnamestack
2686 { \exp_after:wN \@@_tmp:w \@currnamestack }
2687 }
2688 \group_end:
2689 % \end{macrocode}
2690 % \end{variable}
2691 %
2692 % \begin{variable}{\g_@@_record_seq}
2693 % The total list of files used is recorded separately from the current
2694 % file stack, as nothing is ever popped from this list. The current
2695 % file name should be included in the file list! We
2696 % will eventually copy the contents of \cs{@filelist}.
2697 % \begin{macrocode}
2698 \seq_new:N \g_@@_record_seq
2699 % \end{macrocode}
2700 % \end{variable}
2701 %
2702 % \begin{variable}{\l_@@_base_name_tl, \l_@@_full_name_tl}
2703 % For storing the basename and full path whilst passing data internally.
2704 % \begin{macrocode}
2705 \tl_new:N \l_@@_base_name_tl
2706 \tl_new:N \l_@@_full_name_tl
2707 % \end{macrocode}
2708 % \end{variable}
2709 %
2710 % \begin{variable}{\l_@@_dir_str, \l_@@_ext_str, \l_@@_name_str}
2711 % Used in parsing a path into parts: in contrast to the above, these are
2712 % never used outside of the current module.
2713 % \begin{macrocode}
2714 \str_new:N \l_@@_dir_str
2715 \str_new:N \l_@@_ext_str
2716 \str_new:N \l_@@_name_str
2717 % \end{macrocode}
2718 % \end{variable}
2719 %
2720 % \begin{variable}{\l_file_search_path_seq}
2721 % The current search path.
2722 % \begin{macrocode}
2723 \seq_new:N \l_file_search_path_seq
2724 % \end{macrocode}
2725 % \end{variable}
2726 %
2727 % \begin{variable}{\l_@@_tmp_seq}
2728 % Scratch space for comma list conversion.
2729 % \begin{macrocode}
2730 \seq_new:N \l_@@_tmp_seq
2731 % \end{macrocode}
2732 % \end{variable}
2733 %
2734 % \subsubsection{Internal auxiliaries}
2735 %
2736 % \begin{variable}{\s_@@_stop}
2737 % Internal scan marks.
2738 % \begin{macrocode}
2739 \scan_new:N \s_@@_stop
2740 % \end{macrocode}
2741 % \end{variable}
2742 %
2743 % \begin{variable}{\q_@@_nil}
2744 % Internal quarks.
2745 % \begin{macrocode}
2746 \quark_new:N \q_@@_nil
2747 % \end{macrocode}
2748 % \end{variable}
2749 %
2750 % \begin{macro}[pTF]{\@@_quark_if_nil:n}
2751 % Branching quark conditional.
2752 % \begin{macrocode}
2753 \__kernel_quark_new_conditional:Nn \@@_quark_if_nil:n { TF }
2754 % \end{macrocode}
2755 % \end{macro}
2756 %
2757 % \begin{variable}{\q_@@_recursion_tail,\q_@@_recursion_stop}
2758 % Internal recursion quarks.
2759 % \begin{macrocode}
2760 \quark_new:N \q_@@_recursion_tail
2761 \quark_new:N \q_@@_recursion_stop
2762 % \end{macrocode}
2763 % \end{variable}
2764 %
2765 % \begin{macro}[EXP]{
2766 % \@@_if_recursion_tail_break:NN,
2767 % \@@_if_recursion_tail_stop_do:Nn
2768 % }
2769 % Functions to query recursion quarks.
2770 % \begin{macrocode}
2771 \__kernel_quark_new_test:N \@@_if_recursion_tail_stop:N
2772 \__kernel_quark_new_test:N \@@_if_recursion_tail_stop_do:nn
2773 % \end{macrocode}
2774 % \end{macro}
2775 %
2776 % \begin{macro}[EXP]{\__kernel_file_name_sanitize:n}
2777 % \begin{macro}[EXP]{
2778 % \@@_name_expand:n,
2779 % \@@_name_expand_cleanup:Nw,
2780 % \@@_name_expand_cleanup:w,
2781 % \@@_name_expand_end:,
2782 % \@@_name_expand_error:Nw,
2783 % \@@_name_expand_error_aux:Nw,
2784 % }
2785 % \begin{macro}[EXP]{
2786 % \@@_name_strip_quotes:n,
2787 % \@@_name_strip_quotes:nnnw,
2788 % \@@_name_strip_quotes:nnn,
2789 % }
2790 % \begin{macro}[EXP]{
2791 % \@@_name_trim_spaces:n,
2792 % \@@_name_trim_spaces:nw,
2793 % \@@_name_trim_spaces_aux:n,
2794 % \@@_name_trim_spaces_aux:w,
2795 % }
2796 % Expanding the file name uses a \tn{csname}-based approach, and
2797 % relies on active characters (for example from UTF-8 characters)
2798 % being properly set up to expand to a expansion-safe version using
2799 % \cs{ifcsname}. This is less conservative than the token-by-token
2800 % approach used before, but it is much faster.
2801 % \begin{macrocode}
2802 \cs_new:Npn \__kernel_file_name_sanitize:n #1
2803 {
2804 \exp_args:Ne \@@_name_trim_spaces:n
2805 {
2806 \exp_args:Ne \@@_name_strip_quotes:n
2807 { \@@_name_expand:n {#1} }
2808 }
2809 }
2810 % \end{macrocode}
2811 %
2812 % We'll use \cs{cs:w} to start expanding the file name, and to avoid
2813 % creating csnames equal to \tn{relax} with \enquote{common} names,
2814 % there's a prefix |__file_name=| to the csname. There's also a guard
2815 % token at the end so we can check if there was an error during the
2816 % process and (try to) clean up gracefully.
2817 % \begin{macrocode}
2818 \cs_new:Npn \@@_name_expand:n #1
2819 {
2820 \exp_after:wN \@@_name_expand_cleanup:Nw
2821 \cs:w @@_name = #1 \cs_end:
2822 \@@_name_expand_end:
2823 }
2824 % \end{macrocode}
2825 % With the csname built, we grab it, and grab the remaining tokens
2826 % delimited by \cs{@@_name_expand_end:}. If there are any remaining
2827 % tokens, something bad happened, so we'll call the error procedure
2828 % \cs{@@_name_expand_error:Nw}.
2829 % If everything went according to plan, then use \cs{token_to_str:N}
2830 % on the csname built, and call \cs{@@_name_expand_cleanup:w} to
2831 % remove the prefix we added a while back.
2832 % \cs{@@_name_expand_cleanup:w} takes a leading argument so we don't
2833 % have to bother about the value of \cs{tex_escapechar:D}.
2834 % \begin{macrocode}
2835 \cs_new:Npn \@@_name_expand_cleanup:Nw #1 #2 \@@_name_expand_end:
2836 {
2837 \tl_if_empty:nF {#2}
2838 { \@@_name_expand_error:Nw #2 \@@_name_expand_end: }
2839 \exp_after:wN \@@_name_expand_cleanup:w \token_to_str:N #1
2840 }
2841 \exp_last_unbraced:NNNNo
2842 \cs_new:Npn \@@_name_expand_cleanup:w #1 \tl_to_str:n { @@_name = } { }
2843 % \end{macrocode}
2844 % In non-error cases \cs{@@_name_expand_end:} should not expand. It
2845 % will only do so in case there is a \cs{csname} too much in the file
2846 % name, so it will throw an error (while expanding), then insert the
2847 % missing \cs{cs_end:} and yet another \cs{@@_name_expand_end:} that
2848 % will be used as a delimiter by \cs{@@_name_expand_cleanup:Nw} (or
2849 % that will expand again if yet another \cs{endcsname} is missing).
2850 % \begin{macrocode}
2851 \cs_new:Npn \@@_name_expand_end:
2852 {
2853 \msg_expandable_error:nn
2854 { kernel } { filename-missing-endcsname }
2855 \cs_end: \@@_name_expand_end:
2856 }
2857 % \end{macrocode}
2858 % Now to the error case. \cs{@@_name_expand_error:Nw} adds an extra
2859 % \cs{cs_end:} so that in case there was an extra \tn{csname} in the
2860 % file name, then \cs{@@_name_expand_error_aux:Nw} throws the error.
2861 % \begin{macrocode}
2862 \cs_new:Npn \@@_name_expand_error:Nw #1 #2 \@@_name_expand_end:
2863 { \@@_name_expand_error_aux:Nw #1 #2 \cs_end: \@@_name_expand_end: }
2864 \cs_new:Npn \@@_name_expand_error_aux:Nw #1 #2 \cs_end: #3
2865 \@@_name_expand_end:
2866 {
2867 \msg_expandable_error:nnff
2868 { kernel } { filename-chars-lost }
2869 { \token_to_str:N #1 } { \exp_stop_f: #2 }
2870 }
2871 % \end{macrocode}
2872 % Quoting file name uses basically the same approach as for
2873 % \texttt{luaquotejobname}: count the |"| tokens and remove them.
2874 % \begin{macrocode}
2875 \cs_new:Npn \@@_name_strip_quotes:n #1
2876 {
2877 \@@_name_strip_quotes:nw { 0 }
2878 #1 " \q_@@_recursion_tail " \q_@@_recursion_stop {#1}
2879 }
2880 \cs_new:Npn \@@_name_strip_quotes:nw #1#2 "
2881 {
2882 \if_meaning:w \q_@@_recursion_tail #2
2883 \@@_name_strip_quotes_end:wnwn
2884 \fi:
2885 #2
2886 \@@_name_strip_quotes:nw { #1 + 1 }
2887 }
2888 \cs_new:Npn \@@_name_strip_quotes_end:wnwn \fi: #1
2889 \@@_name_strip_quotes:nw #2 \q_@@_recursion_stop #3
2890 {
2891 \fi:
2892 \int_if_odd:nT {#2}
2893 {
2894 \msg_expandable_error:nnn
2895 { kernel } { unbalanced-quote-in-filename } {#3}
2896 }
2897 }
2898 % \end{macrocode}
2899 % Spaces need to be trimmed from the start of the name and from the end of
2900 % any extension. However, the name we are passed might not have an extension:
2901 % that means we have to look for one. If there is no extension, we still use
2902 % the standard trimming function but deliberately prevent any spaces being
2903 % removed at the end.
2904 % \begin{macrocode}
2905 \cs_new:Npn \@@_name_trim_spaces:n #1
2906 { \@@_name_trim_spaces:nw {#1} #1 . \q_@@_nil . \s_@@_stop }
2907 \cs_new:Npn \@@_name_trim_spaces:nw #1#2 . #3 . #4 \s_@@_stop
2908 {
2909 \@@_quark_if_nil:nTF {#3}
2910 {
2911 \tl_trim_spaces_apply:nN { #1 \s_@@_stop }
2912 \@@_name_trim_spaces_aux:n
2913 }
2914 { \tl_trim_spaces:n {#1} }
2915 }
2916 \cs_new:Npn \@@_name_trim_spaces_aux:n #1
2917 { \@@_name_trim_spaces_aux:w #1 }
2918 \cs_new:Npn \@@_name_trim_spaces_aux:w #1 \s_@@_stop {#1}
2919 % \end{macrocode}
2920 % \end{macro}
2921 % \end{macro}
2922 % \end{macro}
2923 % \end{macro}
2924 %
2925 % \begin{macro}[EXP]{\__kernel_file_name_quote:n}
2926 % \begin{macro}[EXP]{\@@_name_quote:nw}
2927 % \begin{macrocode}
2928 \cs_new:Npn \__kernel_file_name_quote:n #1
2929 { \@@_name_quote:nw {#1} #1 ~ \q_@@_nil \s_@@_stop }
2930 \cs_new:Npn \@@_name_quote:nw #1 #2 ~ #3 \s_@@_stop
2931 {
2932 \@@_quark_if_nil:nTF {#3}
2933 { #1 }
2934 { "#1" }
2935 }
2936 % \end{macrocode}
2937 % \end{macro}
2938 % \end{macro}
2939 %
2940 % \begin{variable}{\c_@@_marker_tl}
2941 % The same idea as the marker for rescanning token lists: this pair of
2942 % tokens cannot appear in a file that is being input.
2943 % \begin{macrocode}
2944 \tl_const:Ne \c_@@_marker_tl { : \token_to_str:N : }
2945 % \end{macrocode}
2946 % \end{variable}
2947 %
2948 % \begin{macro}[TF]{\file_get:nnN, \file_get:VnN}
2949 % \begin{macro}{\file_get:nnN,\@@_get_aux:nnN,\@@_get_do:Nw}
2950 % The approach here is similar to that for \cs{tl_set_rescan:Nnn}.
2951 % The file contents are grabbed as an argument delimited by
2952 % \cs{c_@@_marker_tl}. A few subtleties: braces in \cs{if_false:}
2953 % \ldots{} \cs{fi:} to deal with possible alignment tabs,
2954 % \tn{tracingnesting} to avoid a warning about a group being closed
2955 % inside the \tn{scantokens}, and \cs{prg_return_true:} is placed
2956 % after the end-of-file marker.
2957 % \begin{macrocode}
2958 \cs_new_protected:Npn \file_get:nnN #1#2#3
2959 {
2960 \file_get:nnNF {#1} {#2} #3
2961 { \tl_set:Nn #3 { \q_no_value } }
2962 }
2963 \cs_generate_variant:Nn \file_get:nnN { V }
2964 \prg_new_protected_conditional:Npnn \file_get:nnN #1#2#3 { T , F , TF }
2965 {
2966 \file_get_full_name:nNTF {#1} \l_@@_full_name_tl
2967 {
2968 \exp_args:NV \@@_get_aux:nnN
2969 \l_@@_full_name_tl
2970 {#2} #3
2971 \prg_return_true:
2972 }
2973 { \prg_return_false: }
2974 }
2975 \prg_generate_conditional_variant:Nnn \file_get:nnN { V } { T , F , TF }
2976 \cs_new_protected:Npe \@@_get_aux:nnN #1#2#3
2977 {
2978 \exp_not:N \if_false: { \exp_not:N \fi:
2979 \group_begin:
2980 \int_set_eq:NN \tex_tracingnesting:D \c_zero_int
2981 \exp_not:N \exp_args:No \tex_everyeof:D
2982 { \exp_not:N \c_@@_marker_tl }
2983 #2 \scan_stop:
2984 \exp_not:N \exp_after:wN \exp_not:N \@@_get_do:Nw
2985 \exp_not:N \exp_after:wN #3
2986 \exp_not:N \exp_after:wN \exp_not:N \prg_do_nothing:
2987 \exp_not:N \tex_input:D
2988 \sys_if_engine_luatex:TF
2989 { {#1} }
2990 { \exp_not:N \__kernel_file_name_quote:n {#1} \scan_stop: }
2991 \exp_not:N \if_false: } \exp_not:N \fi:
2992 }
2993 \exp_args:Nno \use:nn
2994 { \cs_new_protected:Npn \@@_get_do:Nw #1#2 }
2995 { \c_@@_marker_tl }
2996 {
2997 \group_end:
2998 \tl_set:No #1 {#2}
2999 }
3000 % \end{macrocode}
3001 % \end{macro}
3002 % \end{macro}
3003 %
3004 % \begin{macro}{\@@_size:n}
3005 % A copy of the primitive where it's available.
3006 % \begin{macrocode}
3007 \cs_new_eq:NN \@@_size:n \tex_filesize:D
3008 % \end{macrocode}
3009 % \end{macro}
3010 %
3011 % \begin{macro}[EXP]{\file_full_name:n, \@@_full_name:n, \@@_full_name_aux:n}
3012 % \begin{macro}[EXP]{\@@_full_name_auxi:nn, \@@_full_name_auxii:nn}
3013 % \begin{macro}[EXP]{\@@_full_name_aux:Nnn}
3014 % \begin{macro}[EXP]{\@@_full_name_slash:n}
3015 % \begin{macro}[EXP]{\@@_full_name_slash:w}
3016 % \begin{macro}[EXP]{\@@_full_name_aux:nN}
3017 % \begin{macro}[EXP]{\@@_full_name_aux:nnN}
3018 % \begin{macro}[EXP]{\@@_name_cleanup:w}
3019 % \begin{macro}[EXP]{\@@_name_end:}
3020 % \begin{macro}[EXP]{\@@_name_ext_check:nn}
3021 % \begin{macro}[EXP]{\@@_name_ext_check:nnw}
3022 % \begin{macro}[EXP]{\@@_name_ext_check:nnnw}
3023 % \begin{macro}[EXP]{\@@_name_ext_check:nnn}
3024 % \begin{macro}[EXP]{\@@_name_ext_check:nnnn}
3025 % File searching can be carried out if the \tn{pdffilesize} primitive
3026 % or an equivalent is available. That of course means we need to
3027 % arrange for everything else to here to be done by expansion too.
3028 % We start off by sanitizing the name and quoting if required: we
3029 % may need to remove those quotes, so the raw name is passed too.
3030 % \begin{macrocode}
3031 \cs_new:Npn \file_full_name:n #1
3032 {
3033 \exp_args:Ne \@@_full_name:n
3034 { \__kernel_file_name_sanitize:n {#1} }
3035 }
3036 \cs_generate_variant:Nn \file_full_name:n { V }
3037 % \end{macrocode}
3038 % First, we check of the file is just here: no mapping so we do not
3039 % need the break part of the broader auxiliary. We are using the fact
3040 % that the primitive here returns nothing if the file is entirely absent.
3041 % To avoid unnecessary filesystem lookups, the result of \tn{pdffilesize}
3042 % is kept available as an argument.
3043 % For package mode, \tn{input@path} is a token list not a sequence.
3044 % \begin{macrocode}
3045 \cs_new:Npn \@@_full_name:n #1
3046 {
3047 \tl_if_blank:nF {#1}
3048 { \exp_args:Nne \@@_full_name_auxii:nn {#1} { \@@_full_name_aux:n {#1} } }
3049 }
3050 % \end{macrocode}
3051 % To avoid repeated reading of files we need to cache the loading:
3052 % this is important as the code here is used by \emph{all} file checks.
3053 % The same marker is used in the \LaTeXe{} kernel, meaning that we get a
3054 % double-saving with for example \cs{IfFileExists}. As this is all about
3055 % performance, we use the low-level approach for the conditionals. For
3056 % a file already seen, the size is reported as $-1$ so it's distinct from
3057 % any non-cached ones.
3058 % \begin{macrocode}
3059 \cs_new:Npn \@@_full_name_aux:n #1
3060 {
3061 \if_cs_exist:w @@_seen_ \tl_to_str:n {#1} : \cs_end:
3062 -1
3063 \else:
3064 \exp_args:Ne \@@_full_name_auxi:nn { \@@_size:n {#1} } {#1}
3065 \fi:
3066 }
3067 % \end{macrocode}
3068 % We will need the size of files later, and we have to avoid the
3069 % \cs{scan_stop:} causing issues if we are raising the flag. Thus there is
3070 % a slightly odd gobble here.
3071 % \begin{macrocode}
3072 \cs_new:Npn \@@_full_name_auxi:nn #1#2
3073 {
3074 \if:w \scan_stop: #1 \scan_stop:
3075 \else:
3076 \exp_after:wN \use_none:n
3077 \cs:w @@_seen_ \tl_to_str:n {#2} : \cs_end:
3078 #1
3079 \fi:
3080 }
3081 \cs_new:Npn \@@_full_name_auxii:nn #1 #2
3082 {
3083 \tl_if_blank:nTF {#2}
3084 {
3085 \seq_map_tokens:Nn \l_file_search_path_seq
3086 { \@@_full_name_aux:Nnn \seq_map_break:n {#1} }
3087 \cs_if_exist:NT \input@path
3088 {
3089 \tl_map_tokens:Nn \input@path
3090 { \@@_full_name_aux:Nnn \tl_map_break:n {#1} }
3091 }
3092 \@@_name_end:
3093 }
3094 { \@@_ext_check:nn {#1} {#2} }
3095 }
3096 % \end{macrocode}
3097 % Two pars to the auxiliary here so we can avoid doing quoting
3098 % twice in the event we find the right file.
3099 % \begin{macrocode}
3100 \cs_new:Npn \@@_full_name_aux:Nnn #1#2#3
3101 {
3102 \exp_args:Ne \@@_full_name_aux:nN
3103 { \@@_full_name_slash:n {#3} #2 }
3104 #1
3105 }
3106 \cs_new:Npn \@@_full_name_slash:n #1
3107 {
3108 \@@_full_name_slash:nw {#1} #1 \q_nil / \q_nil / \q_nil \q_stop
3109 }
3110 \cs_new:Npn \@@_full_name_slash:nw #1#2 / \q_nil / #3 \q_stop
3111 {
3112 \quark_if_nil:nTF {#3}
3113 { #1 / }
3114 { #2 / }
3115 }
3116 \cs_new:Npn \@@_full_name_aux:nN #1
3117 { \exp_args:Nne \@@_full_name_aux:nnN {#1} { \@@_full_name_aux:n {#1} } }
3118 \cs_new:Npn \@@_full_name_aux:nnN #1 #2 #3
3119 {
3120 \tl_if_blank:nF {#2}
3121 {
3122 #3
3123 {
3124 \@@_ext_check:nn {#1} {#2}
3125 \@@_name_cleanup:w
3126 }
3127 }
3128 }
3129 \cs_new:Npn \@@_name_cleanup:w #1 \@@_name_end: { }
3130 \cs_new:Npn \@@_name_end: { }
3131 % \end{macrocode}
3132 % As \TeX{} automatically adds |.tex| if there is no extension,
3133 % there is a little clean up to do here. First, make sure we are not in the
3134 % directory part, saving that. Then check for an extension.
3135 % \begin{macrocode}
3136 \cs_new:Npn \@@_ext_check:nn #1 #2
3137 { \@@_ext_check:nnw {#2} { / } #1 / \q_@@_nil / \s_@@_stop }
3138 \cs_new:Npn \@@_ext_check:nnw #1 #2 #3 / #4 / #5 \s_@@_stop
3139 {
3140 \@@_quark_if_nil:nTF {#4}
3141 {
3142 \exp_args:No \@@_ext_check:nnnw
3143 { \use_none:n #2 } {#1} {#3} #3 . \q_@@_nil . \s_@@_stop
3144 }
3145 { \@@_ext_check:nnw {#1} { #2 #3 / } #4 / #5 \s_@@_stop }
3146 }
3147 \cs_new:Npe \@@_ext_check:nnnw #1#2#3#4 . #5 . #6 \s_@@_stop
3148 {
3149 \exp_not:N \@@_quark_if_nil:nTF {#5}
3150 {
3151 \exp_not:N \@@_ext_check:nnn
3152 { #1 #3 \tl_to_str:n { .tex } } { #1 #3 } {#2}
3153 }
3154 { #1 #3 }
3155 }
3156 \cs_new:Npn \@@_ext_check:nnn #1
3157 { \exp_args:Nne \@@_ext_check:nnnn {#1} { \@@_full_name_aux:n {#1} } }
3158 \cs_new:Npn \@@_ext_check:nnnn #1#2#3#4
3159 {
3160 \tl_if_blank:nTF {#2}
3161 {#3}
3162 {
3163 \bool_lazy_or:nnTF
3164 { \int_compare_p:nNn {#4} = {#2} }
3165 { \int_compare_p:nNn {#2} = { -1 } }
3166 {#1}
3167 {#3}
3168 }
3169 }
3170 % \end{macrocode}
3171 % \end{macro}
3172 % \end{macro}
3173 % \end{macro}
3174 % \end{macro}
3175 % \end{macro}
3176 % \end{macro}
3177 % \end{macro}
3178 % \end{macro}
3179 % \end{macro}
3180 % \end{macro}
3181 % \end{macro}
3182 % \end{macro}
3183 % \end{macro}
3184 % \end{macro}
3185 %
3186 % \begin{macro}{\file_forget:n}
3187 % Just a wrapper around a csname: we have to do a lookup here to make
3188 % sure any paths are handled.
3189 % \begin{macrocode}
3190 \cs_new_protected:Npn \file_forget:n #1
3191 { \cs_undefine:c { @@_seen_ \file_full_name:n {#1} : } }
3192 % \end{macrocode}
3193 % \end{macro}
3194 %
3195 % \begin{macro}{\file_get_full_name:nN, \file_get_full_name:VN}
3196 % \begin{macro}[TF]{\file_get_full_name:nN, \file_get_full_name:VN}
3197 % \begin{macro}{\@@_get_full_name_search:nN}
3198 % These functions pre-date using \cs{tex_filesize:D} for file searching,
3199 % so are |get| functions with protection. To avoid having different
3200 % search set ups, they are simply wrappers around the code above.
3201 % \begin{macrocode}
3202 \cs_new_protected:Npn \file_get_full_name:nN #1#2
3203 {
3204 \file_get_full_name:nNF {#1} #2
3205 { \tl_set:Nn #2 { \q_no_value } }
3206 }
3207 \cs_generate_variant:Nn \file_get_full_name:nN { V }
3208 \prg_new_protected_conditional:Npnn \file_get_full_name:nN #1#2 { T , F , TF }
3209 {
3210 \__kernel_tl_set:Nx #2
3211 { \file_full_name:n {#1} }
3212 \tl_if_empty:NTF #2
3213 { \prg_return_false: }
3214 { \prg_return_true: }
3215 }
3216 \prg_generate_conditional_variant:Nnn \file_get_full_name:nN
3217 { V } { T , F , TF }
3218 % \end{macrocode}
3219 % \end{macro}
3220 % \end{macro}
3221 % \end{macro}
3222 %
3223 % \begin{variable}{\g_@@_tmp_ior}
3224 % A reserved stream to test for opening a shell.
3225 % \begin{macrocode}
3226 \ior_new:N \g_@@_tmp_ior
3227 % \end{macrocode}
3228 % \end{variable}
3229 %
3230 % \begin{macro}[rEXP]
3231 % {
3232 % \file_mdfive_hash:n, \file_mdfive_hash:V,
3233 % \file_size:n, \file_size:V,
3234 % \file_timestamp:n, \file_timestamp:V
3235 % }
3236 % \begin{macro}[rEXP]{\@@_details:nn, \@@_details_aux:nn}
3237 % \begin{macro}[rEXP]{\@@_mdfive_hash:n}
3238 % Getting file details by expansion is relatively easy if a bit repetitive.
3239 % As the MD5 function has a slightly different syntax from the other commands,
3240 % there is a little cleaning up to do.
3241 % \begin{macrocode}
3242 \cs_new:Npn \file_size:n #1
3243 { \@@_details:nn {#1} { size } }
3244 \cs_generate_variant:Nn \file_size:n { V }
3245 \cs_new:Npn \file_timestamp:n #1
3246 { \@@_details:nn {#1} { moddate } }
3247 \cs_generate_variant:Nn \file_timestamp:n { V }
3248 \cs_new:Npn \@@_details:nn #1#2
3249 {
3250 \exp_args:Ne \@@_details_aux:nn
3251 { \file_full_name:n {#1} } {#2}
3252 }
3253 \cs_new:Npn \@@_details_aux:nn #1#2
3254 {
3255 \tl_if_blank:nF {#1}
3256 { \use:c { tex_file #2 :D } {#1} }
3257 }
3258 \cs_new:Npn \file_mdfive_hash:n #1
3259 { \exp_args:Ne \@@_mdfive_hash:n { \file_full_name:n {#1} } }
3260 \cs_generate_variant:Nn \file_mdfive_hash:n { V }
3261 \cs_new:Npn \@@_mdfive_hash:n #1
3262 { \tex_mdfivesum:D file {#1} }
3263 % \end{macrocode}
3264 % \end{macro}
3265 % \end{macro}
3266 % \end{macro}
3267 %
3268 % \begin{macro}[rEXP]{\file_hex_dump:nnn, \file_hex_dump:Vnn, \@@_hex_dump_auxi:nnn}
3269 % \begin{macro}[rEXP]{\@@_hex_dump_auxii:nnnn, \@@_hex_dump_auxiii:nnnn}
3270 % \begin{macro}[rEXP]{\@@_hex_dump_auxiiv:nnn}
3271 % \begin{macro}[rEXP]{\file_hex_dump:n, \file_hex_dump:V, \@@_hex_dump:n}
3272 % These are separate as they need multiple arguments \emph{or} the
3273 % file size. For \LuaTeX{}, the emulation does not need the file
3274 % size so we save a little on expansion.
3275 % \begin{macrocode}
3276 \cs_new:Npn \file_hex_dump:nnn #1#2#3
3277 {
3278 \exp_args:Neee \@@_hex_dump_auxi:nnn
3279 { \file_full_name:n {#1} }
3280 { \int_eval:n {#2} }
3281 { \int_eval:n {#3} }
3282 }
3283 \cs_generate_variant:Nn \file_hex_dump:nnn { V }
3284 \cs_new:Npn \@@_hex_dump_auxi:nnn #1#2#3
3285 {
3286 \bool_lazy_any:nF
3287 {
3288 { \tl_if_blank_p:n {#1} }
3289 { \int_compare_p:nNn {#2} = 0 }
3290 { \int_compare_p:nNn {#3} = 0 }
3291 }
3292 {
3293 \exp_args:Ne \@@_hex_dump_auxii:nnnn
3294 { \@@_details_aux:nn {#1} { size } }
3295 {#1} {#2} {#3}
3296 }
3297 }
3298 \cs_new:Npn \@@_hex_dump_auxii:nnnn #1#2#3#4
3299 {
3300 \int_compare:nNnTF {#3} > 0
3301 { \@@_hex_dump_auxiii:nnnn {#3} }
3302 {
3303 \exp_args:Ne \@@_hex_dump_auxiii:nnnn
3304 { \int_eval:n { #1 + #3 } }
3305 }
3306 {#1} {#2} {#4}
3307 }
3308 \cs_new:Npn \@@_hex_dump_auxiii:nnnn #1#2#3#4
3309 {
3310 \int_compare:nNnTF {#4} > 0
3311 { \@@_hex_dump_auxiv:nnn {#4} }
3312 {
3313 \exp_args:Ne \@@_hex_dump_auxiv:nnn
3314 { \int_eval:n { #2 + #4 } }
3315 }
3316 {#1} {#3}
3317 }
3318 \cs_new:Npn \@@_hex_dump_auxiv:nnn #1#2#3
3319 {
3320 \tex_filedump:D
3321 offset ~ \int_eval:n { #2 - 1 } ~
3322 length ~ \int_eval:n { #1 - #2 + 1 }
3323 {#3}
3324 }
3325 \cs_new:Npn \file_hex_dump:n #1
3326 { \exp_args:Ne \@@_hex_dump:n { \file_full_name:n {#1} } }
3327 \cs_generate_variant:Nn \file_hex_dump:n { V }
3328 \sys_if_engine_luatex:TF
3329 {
3330 \cs_new:Npn \@@_hex_dump:n #1
3331 {
3332 \tl_if_blank:nF {#1}
3333 { \tex_filedump:D whole {#1} {#1} }
3334 }
3335 }
3336 {
3337 \cs_new:Npn \@@_hex_dump:n #1
3338 {
3339 \tl_if_blank:nF {#1}
3340 { \tex_filedump:D length \tex_filesize:D {#1} {#1} }
3341 }
3342 }
3343 % \end{macrocode}
3344 % \end{macro}
3345 % \end{macro}
3346 % \end{macro}
3347 % \end{macro}
3348 %
3349 % \begin{macro}[noTF]
3350 % {
3351 % \file_get_hex_dump:nN, \file_get_hex_dump:VN,
3352 % \file_get_mdfive_hash:nN, \file_get_mdfive_hash:VN,
3353 % \file_get_size:nN, \file_get_size:VN,
3354 % \file_get_timestamp:nN, \file_get_timestamp:VN
3355 % }
3356 % \begin{macro}{\@@_get_details:nnN}
3357 % Non-expandable wrappers around the above in the case where appropriate
3358 % primitive support exists.
3359 % \begin{macrocode}
3360 \cs_new_protected:Npn \file_get_hex_dump:nN #1#2
3361 { \file_get_hex_dump:nNF {#1} #2 { \tl_set:Nn #2 { \q_no_value } } }
3362 \cs_generate_variant:Nn \file_get_hex_dump:nN { V }
3363 \cs_new_protected:Npn \file_get_mdfive_hash:nN #1#2
3364 { \file_get_mdfive_hash:nNF {#1} #2 { \tl_set:Nn #2 { \q_no_value } } }
3365 \cs_generate_variant:Nn \file_get_mdfive_hash:nN { V }
3366 \cs_new_protected:Npn \file_get_size:nN #1#2
3367 { \file_get_size:nNF {#1} #2 { \tl_set:Nn #2 { \q_no_value } } }
3368 \cs_generate_variant:Nn \file_get_size:nN { V }
3369 \cs_new_protected:Npn \file_get_timestamp:nN #1#2
3370 { \file_get_timestamp:nNF {#1} #2 { \tl_set:Nn #2 { \q_no_value } } }
3371 \cs_generate_variant:Nn \file_get_timestamp:nN { V }
3372 \prg_new_protected_conditional:Npnn \file_get_hex_dump:nN #1#2 { T , F , TF }
3373 { \@@_get_details:nnN {#1} { hex_dump } #2 }
3374 \prg_generate_conditional_variant:Nnn \file_get_hex_dump:nN
3375 { V } { T , F , TF }
3376 \prg_new_protected_conditional:Npnn \file_get_mdfive_hash:nN #1#2 { T , F , TF }
3377 { \@@_get_details:nnN {#1} { mdfive_hash } #2 }
3378 \prg_generate_conditional_variant:Nnn \file_get_mdfive_hash:nN
3379 { V } { T , F , TF }
3380 \prg_new_protected_conditional:Npnn \file_get_size:nN #1#2 { T , F , TF }
3381 { \@@_get_details:nnN {#1} { size } #2 }
3382 \prg_generate_conditional_variant:Nnn \file_get_size:nN
3383 { V } { T , F , TF }
3384 \prg_new_protected_conditional:Npnn \file_get_timestamp:nN #1#2 { T , F , TF }
3385 { \@@_get_details:nnN {#1} { timestamp } #2 }
3386 \prg_generate_conditional_variant:Nnn \file_get_timestamp:nN
3387 { V } { T , F , TF }
3388 \cs_new_protected:Npn \@@_get_details:nnN #1#2#3
3389 {
3390 \__kernel_tl_set:Nx #3
3391 { \use:c { file_ #2 :n } {#1} }
3392 \tl_if_empty:NTF #3
3393 { \prg_return_false: }
3394 { \prg_return_true: }
3395 }
3396 % \end{macrocode}
3397 % \end{macro}
3398 % \end{macro}
3399 %
3400 % \begin{macro}[noTF]{\file_get_hex_dump:nnnN, \file_get_hex_dump:VnnN}
3401 % Custom code due to the additional arguments.
3402 % \begin{macrocode}
3403 \cs_new_protected:Npn \file_get_hex_dump:nnnN #1#2#3#4
3404 {
3405 \file_get_hex_dump:nnnNF {#1} {#2} {#3} #4
3406 { \tl_set:Nn #4 { \q_no_value } }
3407 }
3408 \cs_generate_variant:Nn \file_get_hex_dump:nnnN { V }
3409 \prg_new_protected_conditional:Npnn \file_get_hex_dump:nnnN #1#2#3#4
3410 { T , F , TF }
3411 {
3412 \__kernel_tl_set:Nx #4
3413 { \file_hex_dump:nnn {#1} {#2} {#3} }
3414 \tl_if_empty:NTF #4
3415 { \prg_return_false: }
3416 { \prg_return_true: }
3417 }
3418 \prg_generate_conditional_variant:Nnn \file_get_hex_dump:nnnN
3419 { V } { T , F , TF }
3420 % \end{macrocode}
3421 % \end{macro}
3422 %
3423 % \begin{macro}[EXP]{\@@_str_cmp:nn}
3424 % As we are doing a fixed-length \enquote{big} integer comparison, it
3425 % is easiest to use the low-level behavior of string comparisons.
3426 % \begin{macrocode}
3427 \cs_new_eq:NN \@@_str_cmp:nn \tex_strcmp:D
3428 % \end{macrocode}
3429 % \end{macro}
3430 %
3431 % \begin{macro}[EXP, pTF]
3432 % {
3433 % \file_compare_timestamp:nNn,
3434 % \file_compare_timestamp:nNV,
3435 % \file_compare_timestamp:VNn,
3436 % \file_compare_timestamp:VNV
3437 % }
3438 % \begin{macro}[EXP]{\@@_compare_timestamp:nnN}
3439 % \begin{macro}[EXP]{\@@_timestamp:n}
3440 % Comparison of file date can be done by using the low-level nature of the
3441 % string comparison functions.
3442 % \begin{macrocode}
3443 \prg_new_conditional:Npnn \file_compare_timestamp:nNn #1#2#3
3444 { p , T , F , TF }
3445 {
3446 \exp_args:Nee \@@_compare_timestamp:nnN
3447 { \file_full_name:n {#1} }
3448 { \file_full_name:n {#3} }
3449 #2
3450 }
3451 \prg_generate_conditional_variant:Nnn \file_compare_timestamp:nNn
3452 { nNV , V , VNV } { p , T , F , TF }
3453 \cs_new:Npn \@@_compare_timestamp:nnN #1#2#3
3454 {
3455 \tl_if_blank:nTF {#1}
3456 {
3457 \if_charcode:w #3 <
3458 \prg_return_true:
3459 \else:
3460 \prg_return_false:
3461 \fi:
3462 }
3463 {
3464 \tl_if_blank:nTF {#2}
3465 {
3466 \if_charcode:w #3 >
3467 \prg_return_true:
3468 \else:
3469 \prg_return_false:
3470 \fi:
3471 }
3472 {
3473 \if_int_compare:w
3474 \@@_str_cmp:nn
3475 { \@@_timestamp:n {#1} }
3476 { \@@_timestamp:n {#2} }
3477 #3 \c_zero_int
3478 \prg_return_true:
3479 \else:
3480 \prg_return_false:
3481 \fi:
3482 }
3483 }
3484 }
3485 \cs_new_eq:NN \@@_timestamp:n \tex_filemoddate:D
3486 % \end{macrocode}
3487 % \end{macro}
3488 % \end{macro}
3489 % \end{macro}
3490 %
3491 % \begin{macro}[pTF]{\file_if_exist:n, \file_if_exist:V}
3492 % The test for the existence of a file is a wrapper around the function to
3493 % add a path to a file. If the file was found, the path contains
3494 % something, whereas if the file was not located then the return value
3495 % is empty.
3496 % \begin{macrocode}
3497 \prg_new_conditional:Npnn \file_if_exist:n #1 { p , T , F , TF }
3498 {
3499 \tl_if_blank:eTF { \file_full_name:n {#1} }
3500 { \prg_return_false: }
3501 { \prg_return_true: }
3502 }
3503 \prg_generate_conditional_variant:Nnn \file_if_exist:n { V } { p , T , F , TF }
3504 % \end{macrocode}
3505 % \end{macro}
3506 %
3507 % \begin{macro}
3508 % {
3509 % \file_if_exist_input:n,
3510 % \file_if_exist_input:V,
3511 % \file_if_exist_input:nF,
3512 % \file_if_exist_input:VF
3513 % }
3514 % Input of a file with a test for existence. We do not define the |T|
3515 % or |TF| variants because the most useful place to place the
3516 % \meta{true code} would be inconsistent with other conditionals.
3517 % \begin{macrocode}
3518 \cs_new_protected:Npn \file_if_exist_input:n #1
3519 {
3520 \file_get_full_name:nNT {#1} \l_@@_full_name_tl
3521 { \@@_input:V \l_@@_full_name_tl }
3522 }
3523 \cs_generate_variant:Nn \file_if_exist_input:n { V }
3524 \cs_new_protected:Npn \file_if_exist_input:nF #1#2
3525 {
3526 \file_get_full_name:nNTF {#1} \l_@@_full_name_tl
3527 { \@@_input:V \l_@@_full_name_tl }
3528 {#2}
3529 }
3530 \cs_generate_variant:Nn \file_if_exist_input:nF { V }
3531 % \end{macrocode}
3532 % \end{macro}
3533 %
3534 % \begin{macro}{\file_input_stop:}
3535 % A simple rename.
3536 % \begin{macrocode}
3537 \cs_new_protected:Npn \file_input_stop: { \tex_endinput:D }
3538 % \end{macrocode}
3539 % \end{macro}
3540 %
3541 % \begin{macro}{\__kernel_file_missing:n}
3542 % An error message for a missing file, also used in \cs{ior_open:Nn}.
3543 % \begin{macrocode}
3544 \cs_new_protected:Npn \__kernel_file_missing:n #1
3545 {
3546 \msg_error:nne { kernel } { file-not-found }
3547 { \__kernel_file_name_sanitize:n {#1} }
3548 }
3549 % \end{macrocode}
3550 % \end{macro}
3551 %
3552 % \begin{macro}{\file_input:n, \file_input:V}
3553 % \begin{macro}{\@@_input:n, \@@_input:V}
3554 % \begin{macro}{\@@_input_push:n, \__kernel_file_input_push:n}
3555 % \begin{macro}{\@@_input_pop:, \__kernel_file_input_pop:}
3556 % \begin{macro}{\@@_input_pop:nnn}
3557 % Loading a file is done in a safe way, checking first that the file
3558 % exists and loading only if it does. Push the file name on the
3559 % \cs{g_@@_stack_seq}, and add it to the file list, either
3560 % \cs{g_@@_record_seq}, or \cs{@filelist} in package mode.
3561 % \begin{macrocode}
3562 \cs_new_protected:Npn \file_input:n #1
3563 {
3564 \file_get_full_name:nNTF {#1} \l_@@_full_name_tl
3565 { \@@_input:V \l_@@_full_name_tl }
3566 { \__kernel_file_missing:n {#1} }
3567 }
3568 \cs_generate_variant:Nn \file_input:n { V }
3569 \cs_new_protected:Npe \@@_input:n #1
3570 {
3571 \exp_not:N \clist_if_exist:NTF \exp_not:N \@filelist
3572 { \exp_not:N \@addtofilelist {#1} }
3573 { \seq_gput_right:Nn \exp_not:N \g_@@_record_seq {#1} }
3574 \exp_not:N \@@_input_push:n {#1}
3575 \exp_not:N \tex_input:D
3576 \sys_if_engine_luatex:TF
3577 { {#1} }
3578 { \exp_not:N \__kernel_file_name_quote:n {#1} \scan_stop: }
3579 \exp_not:N \@@_input_pop:
3580 }
3581 \cs_generate_variant:Nn \@@_input:n { V }
3582 % \end{macrocode}
3583 % Keeping a track of the file data is easy enough: we store the separated
3584 % parts so we do not need to parse them twice.
3585 % \begin{macrocode}
3586 \cs_new_protected:Npn \@@_input_push:n #1
3587 {
3588 \seq_gpush:Ne \g_@@_stack_seq
3589 {
3590 { \g_file_curr_dir_str }
3591 { \g_file_curr_name_str }
3592 { \g_file_curr_ext_str }
3593 }
3594 \file_parse_full_name:nNNN {#1}
3595 \l_@@_dir_str \l_@@_name_str \l_@@_ext_str
3596 \str_gset_eq:NN \g_file_curr_dir_str \l_@@_dir_str
3597 \str_gset_eq:NN \g_file_curr_name_str \l_@@_name_str
3598 \str_gset_eq:NN \g_file_curr_ext_str \l_@@_ext_str
3599 }
3600 \cs_new_eq:NN \__kernel_file_input_push:n \@@_input_push:n
3601 \cs_new_protected:Npn \@@_input_pop:
3602 {
3603 \seq_gpop:NN \g_@@_stack_seq \l_@@_tmp_tl
3604 \exp_after:wN \@@_input_pop:nnn \l_@@_tmp_tl
3605 }
3606 \cs_new_eq:NN \__kernel_file_input_pop: \@@_input_pop:
3607 \cs_new_protected:Npn \@@_input_pop:nnn #1#2#3
3608 {
3609 \str_gset:Nn \g_file_curr_dir_str {#1}
3610 \str_gset:Nn \g_file_curr_name_str {#2}
3611 \str_gset:Nn \g_file_curr_ext_str {#3}
3612 }
3613 % \end{macrocode}
3614 % \end{macro}
3615 % \end{macro}
3616 % \end{macro}
3617 % \end{macro}
3618 % \end{macro}
3619 %
3620 % \begin{macro}{\file_input_raw:n, \file_input_raw:V, \@@_input_raw:nn}
3621 % No error checking, no tracking.
3622 % \begin{macrocode}
3623 \cs_new:Npn \file_input_raw:n #1
3624 { \exp_args:Ne \@@_input_raw:nn { \file_full_name:n {#1} } {#1} }
3625 \cs_generate_variant:Nn \file_input_raw:n { V }
3626 \cs_new:Npn \@@_input_raw:nn #1#2
3627 {
3628 \tl_if_blank:nTF {#1}
3629 {
3630 \exp_args:Nnne \msg_expandable_error:nnn
3631 { kernel } { file-not-found }
3632 { \__kernel_file_name_sanitize:n {#2} }
3633 }
3634 { \tex_input:D {#1} }
3635 }
3636 \exp_args_generate:n { nne }
3637 % \end{macrocode}
3638 % \end{macro}
3639 %
3640 % \begin{macro}{\file_parse_full_name:n, \file_parse_full_name:V}
3641 % \begin{macro}{\file_parse_full_name_apply:nN, \file_parse_full_name_apply:VN}
3642 % The main parsing macro \cs{file_parse_full_name_apply:nN} passes the
3643 % file name |#1| through \cs{__kernel_file_name_sanitize:n} so that we
3644 % have a single normalized way to treat files internally.
3645 % \cs{file_parse_full_name:n} uses the former, with
3646 % \cs{prg_do_nothing:} to
3647 % leave each part of the name within a pair of braces.
3648 % \begin{macrocode}
3649 \cs_new:Npn \file_parse_full_name:n #1
3650 {
3651 \file_parse_full_name_apply:nN {#1}
3652 \prg_do_nothing:
3653 }
3654 \cs_generate_variant:Nn \file_parse_full_name:n { V }
3655 \cs_new:Npn \file_parse_full_name_apply:nN #1
3656 {
3657 \exp_args:Ne \@@_parse_full_name_auxi:nN
3658 { \__kernel_file_name_sanitize:n {#1} }
3659 }
3660 \cs_generate_variant:Nn \file_parse_full_name_apply:nN { V }
3661 % \end{macrocode}
3662 %
3663 % \begin{macro}{\@@_parse_full_name_auxi:nN}
3664 % \begin{macro}{\@@_parse_full_name_area:nw}
3665 % \cs{@@_parse_full_name_area:nw} splits the file name into chunks
3666 % separated by |/|, until the last one is reached. The last chunk is
3667 % the file name plus the extension, and everything before that is the
3668 % path. When \cs{@@_parse_full_name_area:nw} is done, it leaves
3669 % the path within braces after the scan mark \cs{s_@@_stop} and
3670 % proceeds parsing the actual file name.
3671 % \begin{macrocode}
3672 \cs_new:Npn \@@_parse_full_name_auxi:nN #1
3673 {
3674 \@@_parse_full_name_area:nw { } #1
3675 / \s_@@_stop
3676 }
3677 \cs_new:Npn \@@_parse_full_name_area:nw #1 #2 / #3 \s_@@_stop
3678 {
3679 \tl_if_empty:nTF {#3}
3680 { \@@_parse_full_name_base:nw { } #2 . \s_@@_stop {#1} }
3681 { \@@_parse_full_name_area:nw { #1 / #2 } #3 \s_@@_stop }
3682 }
3683 % \end{macrocode}
3684 %
3685 % \begin{macro}{\@@_parse_full_name_base:nw}
3686 % \cs{@@_parse_full_name_base:nw} does roughly the same as above, but
3687 % it separates the chunks at each period. However here there's some
3688 % extra complications: In case |#1| is empty, it is assumed that the
3689 % extension is actually empty, and the file name is |#2|. Besides, an
3690 % extra |.| has to be added to |#2| because it is later removed in
3691 % \cs{@@_parse_full_name_tidy:nnnN}. In any case, if there's an
3692 % extension, it is returned with a leading |.|.
3693 % \begin{macrocode}
3694 \cs_new:Npn \@@_parse_full_name_base:nw #1 #2 . #3 \s_@@_stop
3695 {
3696 \tl_if_empty:nTF {#3}
3697 {
3698 \tl_if_empty:nTF {#1}
3699 {
3700 \tl_if_empty:nTF {#2}
3701 { \@@_parse_full_name_tidy:nnnN { } { } }
3702 { \@@_parse_full_name_tidy:nnnN { .#2 } { } }
3703 }
3704 { \@@_parse_full_name_tidy:nnnN {#1} { .#2 } }
3705 }
3706 { \@@_parse_full_name_base:nw { #1 . #2 } #3 \s_@@_stop }
3707 }
3708 % \end{macrocode}
3709 %
3710 % \begin{macro}{\@@_parse_full_name_tidy:nnnN}
3711 % Now we just need to tidy some bits left loose before. The loop
3712 % used in the two macros above start with a leading |/| and |.| in the
3713 % file path an name, so here we need to remove them, except in the
3714 % path, if it is a single |/|, in which case it's left as is. After
3715 % all's done, pass to |#4|.
3716 % \begin{macrocode}
3717 \cs_new:Npn \@@_parse_full_name_tidy:nnnN #1 #2 #3 #4
3718 {
3719 \exp_args:Nee #4
3720 {
3721 \str_if_eq:nnF {#3} { / } { \use_none:n }
3722 #3 \prg_do_nothing:
3723 }
3724 { \use_none:n #1 \prg_do_nothing: }
3725 {#2}
3726 }
3727 % \end{macrocode}
3728 % \end{macro}
3729 % \end{macro}
3730 % \end{macro}
3731 % \end{macro}
3732 % \end{macro}
3733 % \end{macro}
3734 %
3735 % \begin{macro}{\file_parse_full_name:nNNN, \file_parse_full_name:VNNN}
3736 % \begin{macrocode}
3737 \cs_new_protected:Npn \file_parse_full_name:nNNN #1 #2 #3 #4
3738 {
3739 \file_parse_full_name_apply:nN {#1}
3740 \@@_full_name_assign:nnnNNN #2 #3 #4
3741 }
3742 \cs_new_protected:Npn \@@_full_name_assign:nnnNNN #1 #2 #3 #4 #5 #6
3743 {
3744 \str_set:Nn #4 {#1}
3745 \str_set:Nn #5 {#2}
3746 \str_set:Nn #6 {#3}
3747 }
3748 \cs_generate_variant:Nn \file_parse_full_name:nNNN { V }
3749 % \end{macrocode}
3750 % \end{macro}
3751 %
3752 % \begin{macro}{\file_show_list:, \file_log_list:, \@@_list:N}
3753 % \begin{macro}[EXP]{\@@_list_aux:n}
3754 % A function to list all files used to the log, without duplicates.
3755 % In package mode, if \cs{@filelist} is still defined, we need to take
3756 % this list of file names into account (we capture it
3757 % \cs{AtBeginDocument} into \cs{g_@@_record_seq}), turning it to a
3758 % string (this does not affect the commas of this comma list).
3759 % \begin{macrocode}
3760 \cs_new_protected:Npn \file_show_list: { \@@_list:N \msg_show:nneeee }
3761 \cs_new_protected:Npn \file_log_list: { \@@_list:N \msg_log:nneeee }
3762 \cs_new_protected:Npn \@@_list:N #1
3763 {
3764 \seq_clear:N \l_@@_tmp_seq
3765 \clist_if_exist:NT \@filelist
3766 {
3767 \exp_args:NNe \seq_set_from_clist:Nn \l_@@_tmp_seq
3768 { \tl_to_str:N \@filelist }
3769 }
3770 \seq_concat:NNN \l_@@_tmp_seq \l_@@_tmp_seq \g_@@_record_seq
3771 \seq_remove_duplicates:N \l_@@_tmp_seq
3772 #1 { kernel } { file-list }
3773 { \seq_map_function:NN \l_@@_tmp_seq \@@_list_aux:n }
3774 { } { } { }
3775 }
3776 \cs_new:Npn \@@_list_aux:n #1 { \iow_newline: #1 }
3777 % \end{macrocode}
3778 % \end{macro}
3779 % \end{macro}
3780 %
3781 % When used as a package, there is a need to hold onto the standard file
3782 % list as well as the new one here. File names recorded in
3783 % \cs{@filelist} must be turned to strings before being added to
3784 % \cs{g_@@_record_seq}.
3785 % \begin{macrocode}
3786 \cs_if_exist:NT \@filelist
3787 {
3788 \AtBeginDocument
3789 {
3790 \exp_args:NNe \seq_set_from_clist:Nn \l_@@_tmp_seq
3791 { \tl_to_str:N \@filelist }
3792 \seq_gconcat:NNN
3793 \g_@@_record_seq
3794 \g_@@_record_seq
3795 \l_@@_tmp_seq
3796 }
3797 }
3798 % \end{macrocode}
3799 %
3800 % \subsection{GetIdInfo}
3801 %
3802 % \begin{macro}{\GetIdInfo}
3803 % \begin{macro}{\@@_id_info_auxi:w, \@@_id_info_auxii:w, \@@_id_info_auxiii:w}
3804 % As documented in \pkg{expl3.dtx} this function extracts file name
3805 % etc from an \textsc{svn} \texttt{Id} line. This used to be how we
3806 % got version number and so on in all modules, so it had to be defined
3807 % in \pkg{l3bootstrap}. Now it's more convenient to define it after
3808 % we have set up quite a lot of tools, and \pkg{l3file} seems the
3809 % least unreasonable place for it.
3810 %
3811 % The idea here is to extract out the information needed from a standard
3812 % \textsc{svn} \texttt{Id} line, but to avoid a line that would get
3813 % changed when the file is checked in. Hence the fact that none of the
3814 % lines here include both a dollar sign and the \texttt{Id} keyword!
3815 % \begin{macrocode}
3816 \cs_new_protected:Npn \GetIdInfo
3817 {
3818 \tl_clear_new:N \ExplFileDescription
3819 \tl_clear_new:N \ExplFileDate
3820 \tl_clear_new:N \ExplFileName
3821 \tl_clear_new:N \ExplFileExtension
3822 \tl_clear_new:N \ExplFileVersion
3823 \group_begin:
3824 \char_set_catcode_space:n { 32 }
3825 \exp_after:wN
3826 \group_end:
3827 \@@_id_info_auxi:w
3828 }
3829 % \end{macrocode}
3830 % A first check for a completely empty \textsc{svn} field. If that is
3831 % not the case, there is a second case when a file created using
3832 % \texttt{svn cp} but has not been checked in. That leaves a special
3833 % marker \texttt{-1} version, which has no further data. Dealing
3834 % correctly with that is the reason for the space in the line to use
3835 % \cs{@@_id_info_auxii:w}.
3836 % \begin{macrocode}
3837 \cs_new_protected:Npn \@@_id_info_auxi:w $ #1 $ #2
3838 {
3839 \tl_set:Nn \ExplFileDescription {#2}
3840 \str_if_eq:nnTF {#1} { Id }
3841 {
3842 \tl_set:Nn \ExplFileDate { 0000/00/00 }
3843 \tl_set:Nn \ExplFileName { [unknown] }
3844 \tl_set:Nn \ExplFileExtension { [unknown~extension] }
3845 \tl_set:Nn \ExplFileVersion {-1}
3846 }
3847 { \@@_id_info_auxii:w #1 ~ \s_@@_stop }
3848 }
3849 % \end{macrocode}
3850 % Here, |#1| is |Id|, |#2| is the file name, |#3| is the extension,
3851 % |#4| is the version, |#5| is the check in date and |#6| is the check
3852 % in time and user, plus some trailing spaces. If |#4| is the marker
3853 % |-1| value then |#5| and |#6| are empty.
3854 % \begin{macrocode}
3855 \cs_new_protected:Npn \@@_id_info_auxii:w
3856 #1 ~ #2.#3 ~ #4 ~ #5 ~ #6 \s_@@_stop
3857 {
3858 \tl_set:Nn \ExplFileName {#2}
3859 \tl_set:Nn \ExplFileExtension {#3}
3860 \tl_set:Nn \ExplFileVersion {#4}
3861 \str_if_eq:nnTF {#4} {-1}
3862 { \tl_set:Nn \ExplFileDate { 0000/00/00 } }
3863 { \@@_id_info_auxiii:w #5 - 0 - 0 - \s_@@_stop }
3864 }
3865 % \end{macrocode}
3866 % Convert an \textsc{svn}-style date into a \LaTeX{}-style one.
3867 % \begin{macrocode}
3868 \cs_new_protected:Npn \@@_id_info_auxiii:w #1 - #2 - #3 - #4 \s_@@_stop
3869 { \tl_set:Nn \ExplFileDate { #1/#2/#3 } }
3870 % \end{macrocode}
3871 % \end{macro}
3872 % \end{macro}
3873 %
3874 % \subsection{Checking the version of kernel dependencies}
3875 %
3876 % \begin{macro}{\__kernel_dependency_version_check:Nn}
3877 % \begin{macro}{\__kernel_dependency_version_check:nn}
3878 % \begin{macro}{\@@_kernel_dependency_compare:nnn,\@@_parse_version:w}
3879 % This function is responsible for checking if dependencies of the
3880 % \LaTeX3 kernel match the version preloaded in the \LaTeXe{} kernel.
3881 % If versions don't match, the function attempts to tell why by
3882 % searching for a possible stray format file.
3883 %
3884 % The function starts by checking that the kernel date is defined, and
3885 % if not zero is used to force the error route. The kernel date is
3886 % then compared with the argument requested date (usually the
3887 % packaging date of the dependency). If the kernel date is less than
3888 % the required date, it's an error and the loading should abort.
3889 % \begin{macrocode}
3890 \cs_new_protected:Npn \__kernel_dependency_version_check:Nn #1
3891 { \exp_args:NV \__kernel_dependency_version_check:nn #1 }
3892 \cs_new_protected:Npn \__kernel_dependency_version_check:nn #1
3893 {
3894 \cs_if_exist:NTF \c__kernel_expl_date_tl
3895 {
3896 \exp_args:NV \@@_kernel_dependency_compare:nnn
3897 \c__kernel_expl_date_tl {#1}
3898 }
3899 { \@@_kernel_dependency_compare:nnn { 0000-00-00 } {#1} }
3900 }
3901 \cs_new_protected:Npn \@@_kernel_dependency_compare:nnn #1 #2 #3
3902 {
3903 \int_compare:nNnT
3904 { \@@_parse_version:w #1 \s_@@_stop } <
3905 { \@@_parse_version:w #2 \s_@@_stop }
3906 { \@@_mismatched_dependency_error:nn {#2} {#3} }
3907 }
3908 \cs_new:Npn \@@_parse_version:w #1 - #2 - #3 \s_@@_stop {#1#2#3}
3909 % \end{macrocode}
3910 %
3911 % \begin{macro}{\@@_mismatched_dependency_error:nn}
3912 % If the versions differ, then we try to give the user some guidance.
3913 % This function starts by taking the engine name \cs{c_sys_engine_str}
3914 % and replacing |tex| by |latex|, then building a command of the form:
3915 % \begin{texttt}
3916 % kpsewhich --all --engine=\meta{engine} \meta{format}[-dev].fmt
3917 % \end{texttt}
3918 % to query the format files available. A shell is opened and each
3919 % line is read into a sequence.
3920 % \begin{macrocode}
3921 \cs_new_protected:Npn \@@_mismatched_dependency_error:nn #1 #2
3922 {
3923 \exp_args:NNe \ior_shell_open:Nn \g_@@_tmp_ior
3924 {
3925 kpsewhich ~ --all ~
3926 --engine = \c_sys_engine_exec_str
3927 \c_space_tl \c_sys_engine_format_str
3928 \bool_lazy_and:nnT
3929 { \tl_if_exist_p:N \development@branch@name }
3930 { ! \tl_if_empty_p:N \development@branch@name }
3931 { -dev } .fmt
3932 }
3933 \seq_clear:N \l_@@_tmp_seq
3934 \ior_map_inline:Nn \g_@@_tmp_ior
3935 { \seq_put_right:Nn \l_@@_tmp_seq {##1} }
3936 \ior_close:N \g_@@_tmp_ior
3937 \msg_error:nnnn { kernel } { mismatched-support-file }
3938 {#1} {#2}
3939 % \end{macrocode}
3940 % And finish by ending the current file.
3941 % \begin{macrocode}
3942 \tex_endinput:D
3943 }
3944 % \end{macrocode}
3945 %
3946 % Now define the actual error message:
3947 % \begin{macrocode}
3948 \msg_new:nnnn { kernel } { mismatched-support-file }
3949 {
3950 Mismatched~LaTeX~support~files~detected. \\
3951 Loading~'#2'~aborted!
3952 % \end{macrocode}
3953 % \cs{c__kernel_expl_date_tl} may not exist, due to an older format,
3954 % so only print the dates when the sentinel token list exists:
3955 % \begin{macrocode}
3956 \tl_if_exist:NT \c__kernel_expl_date_tl
3957 {
3958 \\ \\
3959 The~L3~programming~layer~in~the~LaTeX~format \\
3960 is~dated~\c__kernel_expl_date_tl,~but~in~your~TeX~
3961 tree~the~files~require \\ at~least~#1.
3962 }
3963 }
3964 {
3965 % \end{macrocode}
3966 % The sequence containing the format files should have exactly one
3967 % item: the format file currently being run. If that's the case, the
3968 % cause of the error is not that, so print a generic help with some
3969 % possible causes. If more than one format file was found, then print
3970 % the list to the user, with appropriate indications of what's in the
3971 % system and what's in the user tree.
3972 % \begin{macrocode}
3973 \int_compare:nNnTF { \seq_count:N \l_@@_tmp_seq } > 1
3974 {
3975 The~cause~seems~to~be~an~old~format~file~in~the~user~tree. \\
3976 LaTeX~found~these~files:
3977 \seq_map_tokens:Nn \l_@@_tmp_seq { \\~-~\use:n } \\
3978 Try~deleting~the~file~in~the~user~tree~then~run~LaTeX~again.
3979 }
3980 {
3981 The~most~likely~causes~are:
3982 \\~-~A~recent~format~generation~failed;
3983 \\~-~A~stray~format~file~in~the~user~tree~which~needs~
3984 to~be~removed~or~rebuilt;
3985 \\~-~You~are~running~a~manually~installed~version~of~#2 \\
3986 \ \ \ which~is~incompatible~with~the~version~in~LaTeX. \\
3987 }
3988 \\
3989 LaTeX~will~abort~loading~the~incompatible~support~files~
3990 but~this~may~lead~to \\ later~errors.~Please~ensure~that~
3991 your~LaTeX~format~is~correctly~regenerated.
3992 }
3993 % \end{macrocode}
3994 % \end{macro}
3995 % \end{macro}
3996 % \end{macro}
3997 % \end{macro}
3998 %
3999 % \subsection{Messages}
4000 %
4001 % \begin{macrocode}
4002 \msg_new:nnnn { kernel } { file-not-found }
4003 { File~'#1'~not~found. }
4004 {
4005 The~requested~file~could~not~be~found~in~the~current~directory,~
4006 in~the~TeX~search~path~or~in~the~LaTeX~search~path.
4007 }
4008 \msg_new:nnn { kernel } { file-list }
4009 {
4010 >~File~List~<
4011 #1 \\
4012 .............
4013 }
4014 \msg_new:nnnn { kernel } { filename-chars-lost }
4015 { #1~invalid~in~file~name.~Lost:~#2. }
4016 {
4017 There~was~an~invalid~token~in~the~file~name~that~caused~
4018 the~characters~following~it~to~be~lost.
4019 }
4020 \msg_new:nnnn { kernel } { filename-missing-endcsname }
4021 { Missing~\iow_char:N\\endcsname~inserted~in~filename. }
4022 {
4023 The~file~name~had~more~\iow_char:N\\csname~commands~than~
4024 \iow_char:N\\endcsname~ones.~LaTeX~will~add~the~missing~
4025 \iow_char:N\\endcsname~and~try~to~continue~as~best~as~it~can.
4026 }
4027 \msg_new:nnnn { kernel } { unbalanced-quote-in-filename }
4028 { Unbalanced~quotes~in~file~name~'#1'. }
4029 {
4030 File~names~must~contain~balanced~numbers~of~quotes~(").
4031 }
4032 \msg_new:nnnn { kernel } { iow-indent }
4033 { Only~#1 allows~#2 }
4034 {
4035 The~command~#2 can~only~be~used~in~messages~
4036 which~will~be~wrapped~using~#1.
4037 \tl_if_empty:nF {#3} { ~ It~was~called~with~argument~'#3'. }
4038 }
4039 % \end{macrocode}
4040 %
4041 % \subsection{Functions delayed from earlier modules}
4042 %
4043 %<@@=sys>
4044 %
4045 % \begin{variable}{\c_sys_platform_str}
4046 % Detecting the platform on \LuaTeX{} is easy: for other engines, we use
4047 % the fact that the two common cases have special null files. It is possible
4048 % to probe further (see package \pkg{ifplatform}), but that requires shell
4049 % escape and seems unlikely to be useful. This is set up here as it requires
4050 % file searching.
4051 % \begin{macrocode}
4052 \sys_if_engine_luatex:TF
4053 {
4054 \str_const:Ne \c_sys_platform_str
4055 { \tex_directlua:D { tex.print(os.type) } }
4056 }
4057 {
4058 \file_if_exist:nTF { nul: }
4059 {
4060 \file_if_exist:nF { /dev/null }
4061 { \str_const:Nn \c_sys_platform_str { windows } }
4062 }
4063 {
4064 \file_if_exist:nT { /dev/null }
4065 { \str_const:Nn \c_sys_platform_str { unix } }
4066 }
4067 }
4068 \cs_if_exist:NF \c_sys_platform_str
4069 { \str_const:Nn \c_sys_platform_str { unknown } }
4070 % \end{macrocode}
4071 % \end{variable}
4072 % \begin{macro}[pTF]{\sys_if_platform_unix:}
4073 % \begin{macro}[pTF]{\sys_if_platform_windows:}
4074 % We can now set up the tests.
4075 % \begin{macrocode}
4076 \clist_map_inline:nn { unix , windows }
4077 {
4078 \@@_const:nn { sys_if_platform_ #1 }
4079 { \str_if_eq_p:Vn \c_sys_platform_str { #1 } }
4080 }
4081 % \end{macrocode}
4082 % \end{macro}
4083 % \end{macro}
4084 %
4085 % \begin{macrocode}
4086 %</code>
4087 % \end{macrocode}
4088 %
4089 % \end{implementation}
4090 %
4091 % \PrintIndex