-
Notifications
You must be signed in to change notification settings - Fork 177
/
l3file.dtx
2459 lines (2459 loc) · 86.6 KB
/
l3file.dtx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
% \iffalse meta-comment
%
%% File: l3file.dtx Copyright (C) 1990-2017 The LaTeX3 Project
%
% It may be distributed and/or modified under the conditions of the
% LaTeX Project Public License (LPPL), either version 1.3c of this
% license or (at your option) any later version. The latest version
% of this license is in the file
%
% https://www.latex-project.org/lppl.txt
%
% This file is part of the "l3kernel bundle" (The Work in LPPL)
% and all files in that bundle must be distributed together.
%
% -----------------------------------------------------------------------
%
% The development version of the bundle can be found at
%
% https://github.com/latex3/latex3
%
% for those people who are interested.
%
%<*driver>
\documentclass[full,kernel]{l3doc}
\begin{document}
\DocInput{\jobname.dtx}
\PrintIndex
\end{document}
%</driver>
% \fi
%
% \title{^^A
% The \pkg{l3file} package\\ File and I/O operations^^A
% }
%
% \author{^^A
% The \LaTeX3 Project\thanks
% {^^A
% E-mail:
% \href{mailto:latex-team@latex-project.org}
% {latex-team@latex-project.org}^^A
% }^^A
% }
%
% \date{Released 2017/11/14}
%
% \maketitle
%
% \begin{documentation}
%
% This module provides functions for working with external files. Some of these
% functions apply to an entire file, and have prefix \cs[no-index]{file_\ldots}, while
% others are used to work with files on a line by line basis and have prefix
% \cs[no-index]{ior_\ldots} (reading) or \cs[no-index]{iow_\ldots} (writing).
%
% It is important to remember that when reading external files \TeX{}
% attempts to locate them using both the operating system path and entries in the
% \TeX{} file database (most \TeX{} systems use such a database). Thus the
% \enquote{current path} for \TeX{} is somewhat broader than that for other
% programs.
%
% For functions which expect a \meta{file name} argument, this argument
% may contain both literal items and expandable content, which should on
% full expansion be the desired file name. Active characters (as
% declared in \cs{l_char_active_seq}) are \emph{not} expanded,
% allowing the direct use of these in file names. File names are quoted
% using |"| tokens if they contain spaces: as a result, |"| tokens are
% \emph{not} permitted in file names.
%
% \section{File operation functions}
%
% \begin{variable}[added = 2017-06-21]
% {
% \g_file_curr_dir_str,
% \g_file_curr_name_str,
% \g_file_curr_ext_str
% }
% Contain the directory, name and extension of the current file. The
% directory is empty if the file was loaded without an explicit
% path (\emph{i.e.}~if it is in the \TeX{} search path), and does
% \emph{not} end in |/| other than the case that it is exactly equal
% to the root directory. The \meta{name} and \meta{ext} parts together
% make up the file name, thus the \meta{name} part may be thought of
% as the \enquote{job name} for the current file. Note that \TeX{} does
% not provide information on the \meta{ext} part for the main (top
% level) file and that this file always has an empty \meta{dir} component.
% Also, the \meta{name} here will be equal to \cs{c_sys_jobname_str},
% which may be different from the real file name (if set using
% |--jobname|, for example).
% \end{variable}
%
% \begin{variable}[added = 2017-06-18]{\l_file_search_path_seq}
% Each entry is the path to a directory which should be searched when
% seeking a file. Each path can be relative or absolute, and should
% not include the trailing slash. The entries are not expanded when
% used so may contain active characters but should not feature any
% variable content. Spaces need not be quoted.
%
% \begin{texnote}
% When working as a package in \LaTeXe{}, \pkg{expl3} will
% automatically append the current \tn{input@path} to the
% set of values from \cs{l_file_search_path_seq}.
% \end{texnote}
% \end{variable}
%
% \begin{function}[TF, updated = 2012-02-10]{\file_if_exist:n}
% \begin{syntax}
% \cs{file_if_exist:nTF} \Arg{file name} \Arg{true code} \Arg{false code}
% \end{syntax}
% Searches for \meta{file name} using the current \TeX{} search
% path and the additional paths controlled by
% \cs{l_file_search_path_seq}.
% \end{function}
%
% \begin{function}[updated = 2017-06-26]
% {\file_get_full_name:nN, \file_get_full_name:VN}
% \begin{syntax}
% \cs{file_get_full_name:nN} \Arg{file name} \meta{str var}
% \end{syntax}
% Searches for \meta{file name} in the path as detailed for
% \cs{file_if_exist:nTF}, and if found sets the \meta{str var} the
% fully-qualified name of the file, \emph{i.e.}~the path and file name.
% This includes an extension |.tex| when the given \meta{file name}
% has no extension but the file found has that extension.
% If the file is not found then the \meta{str var} is empty.
% \end{function}
%
% \begin{function}[added = 2017-06-23, updated = 2017-06-26]
% {\file_parse_full_name:nNNN}
% \begin{syntax}
% \cs{file_parse_full_name:nNNN} \Arg{full name} \meta{dir} \meta{name} \meta{ext}
% \end{syntax}
% Parses the \meta{full name} and splits it into three parts, each of
% which is returned by setting the appropriate local string variable:
% \begin{itemize}
% \item The \meta{dir}: everything up to the last |/| (path separator)
% in the \meta{file path}. As with system \texttt{PATH} variables
% and related functions, the \meta{dir} does \emph{not} include the
% trailing |/| unless it points to the root directory. If there is no path (only
% a file name), \meta{dir} is empty.
% \item The \meta{name}: everything after the last |/| up to the last |.|,
% where both of those characters are optional. The \meta{name} may
% contain multiple |.| characters. It is empty if \meta{full name}
% consists only of a directory name.
% \item The \meta{ext}: everything after the last |.| (including the dot).
% The \meta{ext} is empty if there is no |.| after the last |/|.
% \end{itemize}
% This function does not expand the \meta{full name} before turning it
% to a string. It assume that the \meta{full name} either contains no
% quote (|"|) characters or is surrounded by a pair of quotes.
% \end{function}
%
% \begin{function}[updated = 2017-06-26]{\file_input:n}
% \begin{syntax}
% \cs{file_input:n} \Arg{file name}
% \end{syntax}
% Searches for \meta{file name} in the path as detailed for
% \cs{file_if_exist:nTF}, and if found reads in the file as
% additional \LaTeX{} source. All files read are recorded
% for information and the file name stack is updated by this
% function. An error is raised if the file is not found.
% \end{function}
%
% \begin{function}{\file_show_list:, \file_log_list:}
% \begin{syntax}
% \cs{file_show_list:}
% \cs{file_log_list:}
% \end{syntax}
% These functions list all files loaded by \LaTeXe{} commands that
% populate \tn{@filelist} or by \cs{file_input:n}. While
% \cs{file_show_list:} displays the list in the terminal,
% \cs{file_log_list:} outputs it to the log file only.
% \end{function}
%
% \subsection{Input--output stream management}
%
% As \TeX{} engines have a limited number of input and output streams, direct
% use of the streams by the programmer is not supported in \LaTeX3. Instead, an
% internal pool of streams is maintained, and these are allocated and
% deallocated as needed by other modules. As a result, the programmer should
% close streams when they are no longer needed, to release them for other
% processes.
%
% Note that I/O operations are global: streams should all be declared
% with global names and treated accordingly.
%
% \begin{function}[added = 2011-09-26, updated = 2011-12-27]
% {\ior_new:N, \ior_new:c, \iow_new:N, \iow_new:c}
% \begin{syntax}
% \cs{ior_new:N} \meta{stream}
% \cs{iow_new:N} \meta{stream}
% \end{syntax}
% Globally reserves the name of the \meta{stream}, either for reading
% or for writing as appropriate. The \meta{stream} is not opened until
% the appropriate \cs[no-index]{\ldots_open:Nn} function is used. Attempting to
% use a \meta{stream} which has not been opened is an error, and the
% \meta{stream} will behave as the corresponding \cs[no-index]{c_term_\ldots}.
% \end{function}
%
% \begin{function}[updated = 2012-02-10]{\ior_open:Nn, \ior_open:cn}
% \begin{syntax}
% \cs{ior_open:Nn} \meta{stream} \Arg{file name}
% \end{syntax}
% Opens \meta{file name} for reading using \meta{stream} as the
% control sequence for file access. If the \meta{stream} was already
% open it is closed before the new operation begins. The
% \meta{stream} is available for access immediately and will remain
% allocated to \meta{file name} until a \cs{ior_close:N} instruction
% is given or the \TeX{} run ends.
% If the file is not found, an error is raised.
% \end{function}
%
% \begin{function}[added = 2013-01-12, TF]{\ior_open:Nn, \ior_open:cn}
% \begin{syntax}
% \cs{ior_open:NnTF} \meta{stream} \Arg{file name} \Arg{true code} \Arg{false code}
% \end{syntax}
% Opens \meta{file name} for reading using \meta{stream} as the
% control sequence for file access. If the \meta{stream} was already
% open it is closed before the new operation begins. The
% \meta{stream} is available for access immediately and will remain
% allocated to \meta{file name} until a \cs{ior_close:N} instruction
% is given or the \TeX{} run ends. The \meta{true code} is then inserted
% into the input stream. If the file is not found, no error is raised and
% the \meta{false code} is inserted into the input stream.
% \end{function}
%
% \begin{function}[updated = 2012-02-09]{\iow_open:Nn, \iow_open:cn}
% \begin{syntax}
% \cs{iow_open:Nn} \meta{stream} \Arg{file name}
% \end{syntax}
% Opens \meta{file name} for writing using \meta{stream} as the
% control sequence for file access. If the \meta{stream} was already
% open it is closed before the new operation begins. The
% \meta{stream} is available for access immediately and will remain
% allocated to \meta{file name} until a \cs{iow_close:N} instruction
% is given or the \TeX{} run ends. Opening a file for writing clears
% any existing content in the file (\emph{i.e.}~writing is \emph{not}
% additive).
% \end{function}
%
% \begin{function}[updated = 2012-07-31]
% {\ior_close:N, \ior_close:c, \iow_close:N, \iow_close:c}
% \begin{syntax}
% \cs{ior_close:N} \meta{stream}
% \cs{iow_close:N} \meta{stream}
% \end{syntax}
% Closes the \meta{stream}. Streams should always be closed when
% they are finished with as this ensures that they remain available
% to other programmers.
% \end{function}
%
% \begin{function}[added = 2017-06-27]
% {
% \ior_show_list:, \ior_log_list:,
% \iow_show_list:, \iow_log_list:
% }
% \begin{syntax}
% \cs{ior_show_list:}
% \cs{ior_log_list:}
% \cs{iow_show_list:}
% \cs{iow_log_list:}
% \end{syntax}
% Display (to the terminal or log file) a list of the file names
% associated with each open (read or write) stream. This is intended
% for tracking down problems.
% \end{function}
%
% \subsection{Reading from files}
%
% \begin{function}[added = 2012-06-24]{\ior_get:NN}
% \begin{syntax}
% \cs{ior_get:NN} \meta{stream} \meta{token list variable}
% \end{syntax}
% Function that reads one or more lines (until an equal number of left
% and right braces are found) from the input \meta{stream} and stores
% the result locally in the \meta{token list} variable. If the
% \meta{stream} is not open, input is requested from the terminal.
% The material read from the \meta{stream} is tokenized by \TeX{}
% according to the category codes and \tn{endlinechar} in force when
% the function is used. Assuming normal settings, any lines which do
% not end in a comment character~|%| have the line ending
% converted to a space, so for example input
% \begin{verbatim}
% a b c
% \end{verbatim}
% results in a token list \verb*|a b c |. Any blank line is
% converted to the token \cs{par}. Therefore, blank lines can be
% skipped by using a test such as
% \begin{verbatim}
% \ior_get:NN \l_my_stream \l_tmpa_tl
% \tl_set:Nn \l_tmpb_tl { \par }
% \tl_if_eq:NNF \l_tmpa_tl \l_tmpb_tl
% ...
% \end{verbatim}
% Also notice that if multiple lines are read to match braces
% then the resulting token list can contain \cs{par} tokens.
% \begin{texnote}
% This protected macro is a wrapper around the \TeX{} primitive
% \tn{read}. Regardless of settings, \TeX{} replaces trailing space
% and tab characters (character codes 32 and~9) in each line by an
% end-of-line character (character code \tn{endlinechar}, omitted if
% \tn{endlinechar} is negative or too large) before turning
% characters into tokens according to current category codes. With
% default settings, spaces appearing at the beginning of lines are
% also ignored.
% \end{texnote}
% \end{function}
%
% \begin{function}[added = 2016-12-04]{\ior_str_get:NN}
% \begin{syntax}
% \cs{ior_str_get:NN} \meta{stream} \meta{token list variable}
% \end{syntax}
% Function that reads one line from the input \meta{stream} and stores
% the result locally in the \meta{token list} variable. If the
% \meta{stream} is not open, input is requested from the terminal.
% The material is read from the \meta{stream} as a series of tokens with
% category code $12$ (other), with the exception of space
% characters which are given category code $10$ (space).
% Multiple whitespace characters are retained by this process. It
% always only reads one line and any blank lines in the input
% result in the \meta{token list variable} being empty. Unlike
% \cs{ior_get:NN}, line ends do not receive any special treatment. Thus
% input
% \begin{verbatim}
% a b c
% \end{verbatim}
% results in a token list |a b c| with the letters |a|, |b|, and |c|
% having category code~12.
% \begin{texnote}
% This protected macro is a wrapper around the \eTeX{} primitive
% \tn{readline}. Regardless of settings, \TeX{} removes trailing
% space and tab characters (character codes 32 and~9). However, the
% end-line character normally added by this primitive is not
% included in the result of \cs{ior_str_get:NN}.
% \end{texnote}
% \end{function}
%
% \begin{function}[added = 2012-02-11]{\ior_map_inline:Nn}
% \begin{syntax}
% \cs{ior_map_inline:Nn} \meta{stream} \Arg{inline function}
% \end{syntax}
% Applies the \meta{inline function} to each set of \meta{lines}
% obtained by calling \cs{ior_get:NN} until reaching the end of the
% file. \TeX{} ignores any trailing new-line marker from the file it
% reads. The \meta{inline function} should consist of code which
% receives the \meta{line} as |#1|.
% \end{function}
%
% \begin{function}[added = 2012-02-11]{\ior_str_map_inline:Nn}
% \begin{syntax}
% \cs{ior_str_map_inline:Nn} \meta{stream} \Arg{inline function}
% \end{syntax}
% Applies the \meta{inline function} to every \meta{line}
% in the \meta{stream}. The material is read from the \meta{stream}
% as a series of tokens with category code $12$ (other), with the
% exception of space characters which are given category code $10$
% (space). The \meta{inline function} should consist of code which
% receives the \meta{line} as |#1|.
% Note that \TeX{} removes trailing space and tab characters
% (character codes 32 and 9) from every line upon input. \TeX{} also
% ignores any trailing new-line marker from the file it reads.
% \end{function}
%
% \begin{function}[added = 2012-06-29]{\ior_map_break:}
% \begin{syntax}
% \cs{ior_map_break:}
% \end{syntax}
% Used to terminate a \cs[no-index]{ior_map_\ldots} function before all
% lines from the \meta{stream} have been processed. This
% normally takes place within a conditional statement, for example
% \begin{verbatim}
% \ior_map_inline:Nn \l_my_ior
% {
% \str_if_eq:nnTF { #1 } { bingo }
% { \ior_map_break: }
% {
% % Do something useful
% }
% }
% \end{verbatim}
% Use outside of a \cs[no-index]{ior_map_\ldots} scenario leads to low
% level \TeX{} errors.
% \begin{texnote}
% When the mapping is broken, additional tokens may be inserted
% before further items are taken
% from the input stream. This depends on the design of the mapping
% function.
% \end{texnote}
% \end{function}
%
% \begin{function}[added = 2012-06-29]{\ior_map_break:n}
% \begin{syntax}
% \cs{ior_map_break:n} \Arg{code}
% \end{syntax}
% Used to terminate a \cs[no-index]{ior_map_\ldots} function before all
% lines in the \meta{stream} have been processed, inserting
% the \meta{code} after the mapping has ended. This
% normally takes place within a conditional statement, for example
% \begin{verbatim}
% \ior_map_inline:Nn \l_my_ior
% {
% \str_if_eq:nnTF { #1 } { bingo }
% { \ior_map_break:n { <code> } }
% {
% % Do something useful
% }
% }
% \end{verbatim}
% Use outside of a \cs[no-index]{ior_map_\ldots} scenario leads to low
% level \TeX{} errors.
% \begin{texnote}
% When the mapping is broken, additional tokens may be inserted
% before the \meta{code} is
% inserted into the input stream.
% This depends on the design of the mapping function.
% \end{texnote}
% \end{function}
%
%\begin{function}[updated = 2012-02-10, EXP, pTF]{\ior_if_eof:N}
% \begin{syntax}
% \cs{ior_if_eof_p:N} \meta{stream} \\
% \cs{ior_if_eof:NTF} \meta{stream} \Arg{true code} \Arg{false code}
% \end{syntax}
% Tests if the end of a \meta{stream} has been reached during a reading
% operation. The test also returns a \texttt{true} value if
% the \meta{stream} is not open.
%\end{function}
%
% \section{Writing to files}
%
% \begin{function}[updated = 2012-06-05]{\iow_now:Nn, \iow_now:Nx, \iow_now:cn, \iow_now:cx}
% \begin{syntax}
% \cs{iow_now:Nn} \meta{stream} \Arg{tokens}
% \end{syntax}
% This functions writes \meta{tokens} to the specified
% \meta{stream} immediately (\emph{i.e.}~the write operation is called
% on expansion of \cs{iow_now:Nn}).
% \end{function}
%
% \begin{function}{\iow_log:n, \iow_log:x}
% \begin{syntax}
% \cs{iow_log:n} \Arg{tokens}
% \end{syntax}
% This function writes the given \meta{tokens} to the log (transcript)
% file immediately: it is a dedicated version of \cs{iow_now:Nn}.
% \end{function}
%
% \begin{function}{\iow_term:n, \iow_term:x}
% \begin{syntax}
% \cs{iow_term:n} \Arg{tokens}
% \end{syntax}
% This function writes the given \meta{tokens} to the terminal
% file immediately: it is a dedicated version of \cs{iow_now:Nn}.
% \end{function}
%
% \begin{function}
% {
% \iow_shipout:Nn, \iow_shipout:Nx,
% \iow_shipout:cn, \iow_shipout:cx
% }
% \begin{syntax}
% \cs{iow_shipout:Nn} \meta{stream} \Arg{tokens}
% \end{syntax}
% This functions writes \meta{tokens} to the specified
% \meta{stream} when the current page is finalised (\emph{i.e.}~at
% shipout). The \texttt{x}-type variants expand the \meta{tokens}
% at the point where the function is used but \emph{not} when the
% resulting tokens are written to the \meta{stream}
% (\emph{cf.}~\cs{iow_shipout_x:Nn}).
% \begin{texnote}
% When using \pkg{expl3} with a format other than \LaTeX{}, new line
% characters inserted using \cs{iow_newline:} or using the
% line-wrapping code \cs{iow_wrap:nnnN} are not recognized in
% the argument of \cs{iow_shipout:Nn}. This may lead to the
% insertion of additional unwanted line-breaks.
% \end{texnote}
% \end{function}
%
% \begin{function}[updated = 2012-09-08]
% {
% \iow_shipout_x:Nn, \iow_shipout_x:Nx,
% \iow_shipout_x:cn, \iow_shipout_x:cx
% }
% \begin{syntax}
% \cs{iow_shipout_x:Nn} \meta{stream} \Arg{tokens}
% \end{syntax}
% This functions writes \meta{tokens} to the specified
% \meta{stream} when the current page is finalised (\emph{i.e.}~at
% shipout). The \meta{tokens} are expanded at the time of writing
% in addition to any expansion when the function is used. This makes
% these functions suitable for including material finalised during
% the page building process (such as the page number integer).
% \begin{texnote}
% This is a wrapper around the \TeX{} primitive \tn{write}.
% When using \pkg{expl3} with a format other than \LaTeX{}, new line
% characters inserted using \cs{iow_newline:} or using the
% line-wrapping code \cs{iow_wrap:nnnN} are not recognized in
% the argument of \cs{iow_shipout:Nn}. This may lead to the
% insertion of additional unwanted line-breaks.
% \end{texnote}
% \end{function}
%
% \begin{function}[EXP]{\iow_char:N}
% \begin{syntax}
% \cs{iow_char:N} |\|\meta{char}
% \end{syntax}
% Inserts \meta{char} into the output stream. Useful when trying to
% write difficult characters such as |%|, |{|, |}|,
% \emph{etc.}~in messages, for example:
% \begin{verbatim}
% \iow_now:Nx \g_my_iow { \iow_char:N \{ text \iow_char:N \} }
% \end{verbatim}
% The function has no effect if writing is taking place without
% expansion (\emph{e.g.}~in the second argument of \cs{iow_now:Nn}).
% \end{function}
%
% \begin{function}[EXP]{\iow_newline:}
% \begin{syntax}
% \cs{iow_newline:}
% \end{syntax}
% Function to add a new line within the \meta{tokens} written to a
% file. The function has no effect if writing is taking place without
% expansion (\emph{e.g.}~in the second argument of \cs{iow_now:Nn}).
% \begin{texnote}
% When using \pkg{expl3} with a format other than \LaTeX{}, the
% character inserted by \cs{iow_newline:} is not recognized by
% \TeX{}, which may lead to the insertion of additional unwanted
% line-breaks. This issue only affects \cs{iow_shipout:Nn},
% \cs{iow_shipout_x:Nn} and direct uses of primitive operations.
% \end{texnote}
% \end{function}
%
% \subsection{Wrapping lines in output}
%
% \begin{function}[added = 2012-06-28, updated = 2017-12-04]{\iow_wrap:nnnN}
% \begin{syntax}
% \cs{iow_wrap:nnnN} \Arg{text} \Arg{run-on text} \Arg{set up} \meta{function}
% \end{syntax}
% This function wraps the \meta{text} to a fixed number of
% characters per line. At the start of each line which is wrapped,
% the \meta{run-on text} is inserted. The line character count
% targeted is the value of \cs{l_iow_line_count_int} minus the
% number of characters in the \meta{run-on text} for all lines except
% the first, for which the target number of characters is simply
% \cs{l_iow_line_count_int} since there is no run-on text. The
% \meta{text} and \meta{run-on text} are exhaustively expanded by the
% function, with the following substitutions:
% \begin{itemize}
% \item |\\| or \cs{iow_newline:} may be used to force a new line,
% \item \verb*|\ | may be used to represent a forced space
% (for example after a control sequence),
% \item |\#|, |\%|, |\{|, |\}|, |\~| may be used to represent
% the corresponding character,
% \item \cs{iow_indent:n} may be used to indent a part of the
% \meta{text} (not the \meta{run-on text}).
% \end{itemize}
% Additional functions may be added to the wrapping by using the
% \meta{set up}, which is executed before the wrapping takes place: this
% may include overriding the substitutions listed.
%
% Any expandable material in the \meta{text} which is not to be expanded
% on wrapping should be converted to a string using \cs{token_to_str:N},
% \cs{tl_to_str:n}, \cs{tl_to_str:N}, \emph{etc.}
%
% The result of the wrapping operation is passed as a braced argument to the
% \meta{function}, which is typically a wrapper around a write
% operation. The output of \cs{iow_wrap:nnnN} (\emph{i.e.}~the argument
% passed to the \meta{function}) consists of characters of category
% \enquote{other} (category code~12), with the exception of spaces which
% have category \enquote{space} (category code~10). This means that the
% output does \emph{not} expand further when written to a file.
%
% \begin{texnote}
% Internally, \cs{iow_wrap:nnnN} carries out an \texttt{x}-type expansion
% on the \meta{text} to expand it. This is done in such a way that
% \cs{exp_not:N} or \cs{exp_not:n} \emph{could} be used to prevent
% expansion of material. However, this is less conceptually clear than
% conversion to a string, which is therefore the supported method for
% handling expandable material in the \meta{text}.
% \end{texnote}
% \end{function}
%
% \begin{function}[added = 2011-09-21]{\iow_indent:n}
% \begin{syntax}
% \cs{iow_indent:n} \Arg{text}
% \end{syntax}
% In the first argument of \cs{iow_wrap:nnnN} (for instance in messages),
% indents \meta{text} by four spaces. This function does not cause
% a line break, and only affects lines which start within the scope
% of the \meta{text}. In case the indented \meta{text} should appear
% on separate lines from the surrounding text, use |\\| to force
% line breaks.
% \end{function}
%
% \begin{variable}[added = 2012-06-24]{\l_iow_line_count_int}
% The maximum number of characters in a line to be written
% by the \cs{iow_wrap:nnnN}
% function. This value depends on the \TeX{} system in use: the standard
% value is $78$, which is typically correct for unmodified \TeX{}live
% and MiK\TeX{} systems.
% \end{variable}
%
% \subsection{Constant input--output streams}
%
% \begin{variable}{\c_term_ior}
% Constant input stream for reading from the terminal. Reading from this
% stream using \cs{ior_get:NN} or similar results in a prompt from
% \TeX{} of the form
% \begin{verbatim}
% <tl>=
% \end{verbatim}
% \end{variable}
%
% \begin{variable}{\c_log_iow, \c_term_iow}
% Constant output streams for writing to the log and to the terminal
% (plus the log), respectively.
% \end{variable}
%
% \subsection{Primitive conditionals}
%
% \begin{function}[EXP]{\if_eof:w}
% \begin{syntax}
% \cs{if_eof:w} \meta{stream}
% ~~\meta{true code}
% \cs{else:}
% ~~\meta{false code}
% \cs{fi:}
% \end{syntax}
% Tests if the \meta{stream} returns \enquote{end of file}, which is true
% for non-existent files. The \cs{else:} branch is optional.
% \begin{texnote}
% This is the \TeX{} primitive \tn{ifeof}.
% \end{texnote}
% \end{function}
%
% \subsection{Internal file functions and variables}
%
% \begin{variable}{\g__file_internal_ior}
% Used to test for the existence of files when opening.
% \end{variable}
%
% \begin{variable}{\l__file_base_name_str, \l__file_full_name_str}
% Used to store and transfer the file name (including extension)
% and (partial) file path whilst reading files. (The file base is the
% base name plus any preceding directory name.)
% \end{variable}
%
% \begin{function}[added = 2017-06-25]{\__file_missing:n}
% \begin{syntax}
% \cs{__file_missing:n} \Arg{name}
% \end{syntax}
% Expands the \meta{name} as per \cs{__file_name_sanitize:nN} then
% produces an error message indicating that that file was not found.
% \end{function}
%
% \begin{function}[added = 2017-06-19]{\__file_name_sanitize:nN}
% \begin{syntax}
% \cs{__file_name_sanitize:nN} \Arg{name} \meta{str var}
% \end{syntax}
% Exhaustively-expands the \meta{name} with the exception of any
% category \meta{active} (catcode~$13$) tokens, which are not expanded.
% The list of \meta{active} tokens is taken from \cs{l_char_active_seq}.
% The \meta{str var} is then set to the \meta{sanitized name}.
% \end{function}
%
% \begin{function}[added = 2017-06-19, updated = 2017-06-25]
% {\__file_name_quote:nN}
% \begin{syntax}
% \cs{__file_name_quote:nN} \Arg{name} \meta{str var}
% \end{syntax}
% Expands the \meta{name} (without special-casing active tokens), then
% sets the \meta{str var} to the \meta{name} quoted using |"| at each
% end if required by the presence of spaces in the \meta{name}. Any existing
% |"| tokens is removed and if their number is odd an error is raised.
% \end{function}
%
% \subsection{Internal input--output functions}
%
% \begin{function}[added = 2012-01-23]{\__ior_open:Nn, \__ior_open:No}
% \begin{syntax}
% \cs{__ior_open:Nn} \meta{stream} \Arg{file name}
% \end{syntax}
% This function has identical syntax to the public version. However,
% is does not take precautions against active characters in the
% \meta{file name}, and it does not attempt to add a \meta{path} to
% the \meta{file name}: it is therefore intended to be used by
% higher-level
% functions which have already fully expanded the \meta{file name} and which
% need to perform multiple open or close operations. See for example the
% implementation of \cs{file_get_full_name:nN},
% \end{function}
%
% \begin{function}[added = 2014-08-23]{\__iow_with:Nnn}
% \begin{syntax}
% \cs{__iow_with:Nnn} \meta{integer} \Arg{value} \Arg{code}
% \end{syntax}
% If the \meta{integer} is equal to the \meta{value} then this
% function simply runs the \meta{code}. Otherwise it saves the
% current value of the \meta{integer}, sets it to the \meta{value},
% runs the \meta{code}, and restores the \meta{integer} to its former
% value. This is used to ensure that the \tn{newlinechar} is~$10$
% when writing to a stream, which lets \cs{iow_newline:} work, and
% that \tn{errorcontextlines} is $-1$ when displaying a message.
% \end{function}
%
% \end{documentation}
%
% \begin{implementation}
%
% \section{\pkg{l3file} implementation}
%
% \TestFiles{m3file001}
%
% \begin{macrocode}
%<*initex|package>
% \end{macrocode}
%
% \begin{macrocode}
%<@@=file>
% \end{macrocode}
%
% \subsection{File operations}
%
% \begin{variable}
% {
% \g_file_curr_dir_str ,
% \g_file_curr_ext_str ,
% \g_file_curr_name_str
% }
% The name of the current file should be available at all times.
% For the format the file name needs to be picked up at the start of the
% run. In \LaTeXe{} package mode the current file name is collected from
% \tn{@currname}.
% \begin{macrocode}
\str_new:N \g_file_curr_dir_str
\str_new:N \g_file_curr_ext_str
\str_new:N \g_file_curr_name_str
%<*initex>
\tex_everyjob:D \exp_after:wN
{
\tex_the:D \tex_everyjob:D
\str_gset:Nx \g_file_curr_name_str { \tex_jobname:D }
}
%</initex>
%<*package>
\cs_if_exist:NT \@currname
{ \str_gset_eq:NN \g_file_curr_name_str \@currname }
%</package>
% \end{macrocode}
% \end{variable}
%
% \begin{variable}{\g_@@_stack_seq}
% The input list of files is stored as a sequence stack. In package
% mode we can recover the information from the details held by
% \LaTeXe{} (we must be in the preamble and loaded using \tn{usepackage}
% or \tn{RequirePackage}). As \LaTeXe{} doesn't store directory and
% name separately, we stick to the same convention here.
% \begin{macrocode}
\seq_new:N \g_@@_stack_seq
%<*package>
\group_begin:
\cs_set_protected:Npn \@@_tmp:w #1#2#3
{
\tl_if_blank:nTF {#1}
{
\cs_set:Npn \@@_tmp:w ##1 " ##2 " ##3 \q_stop { { } {##2} { } }
\seq_gput_right:Nx \g_@@_stack_seq
{
\exp_after:wN \@@_tmp:w \tex_jobname:D
" \tex_jobname:D " \q_stop
}
}
{
\seq_gput_right:Nn \g_@@_stack_seq { { } {#1} {#2} }
\@@_tmp:w
}
}
\cs_if_exist:NT \@currnamestack
{ \exp_after:wN \@@_tmp:w \@currnamestack }
\group_end:
%</package>
% \end{macrocode}
% \end{variable}
%
% \begin{variable}{\g_@@_record_seq}
% The total list of files used is recorded separately from the current
% file stack, as nothing is ever popped from this list. The current
% file name should be included in the file list! In format mode, this
% is done at the very start of the \TeX{} run. In package mode we
% will eventually copy the contents of \cs{@filelist}.
% \begin{macrocode}
\seq_new:N \g_@@_record_seq
%<*initex>
\tex_everyjob:D \exp_after:wN
{
\tex_the:D \tex_everyjob:D
\seq_gput_right:NV \g_@@_record_seq \g_file_curr_name_str
}
%</initex>
% \end{macrocode}
% \end{variable}
%
% \begin{variable}{\l_@@_tmp_tl}
% Used as a short-term scratch variable.
% \begin{macrocode}
\tl_new:N \l_@@_tmp_tl
% \end{macrocode}
% \end{variable}
%
% \begin{variable}{\l__file_base_name_str, \l__file_full_name_str}
% For storing the basename and full path whilst passing data internally.
% \begin{macrocode}
\str_new:N \l__file_base_name_str
\str_new:N \l__file_full_name_str
% \end{macrocode}
% \end{variable}
%
% \begin{variable}{\l_@@_dir_str, \l_@@_ext_str, \l_@@_name_str}
% Used in parsing a path into parts: in contrast to the above, these are
% never used outside of the current module.
% \begin{macrocode}
\str_new:N \l_@@_dir_str
\str_new:N \l_@@_ext_str
\str_new:N \l_@@_name_str
% \end{macrocode}
% \end{variable}
%
% \begin{variable}{\l_file_search_path_seq}
% The current search path.
% \begin{macrocode}
\seq_new:N \l_file_search_path_seq
% \end{macrocode}
% \end{variable}
%
% \begin{variable}{\l_@@_tmp_seq}
% Scratch space for comma list conversion in package mode.
% \begin{macrocode}
%<*package>
\seq_new:N \l_@@_tmp_seq
%</package>
% \end{macrocode}
% \end{variable}
%
% \begin{macro}{\__file_name_sanitize:nN}
% \begin{macro}{\__file_name_quote:nN}
% \begin{macro}{\@@_name_sanitize_aux:n}
% For converting a token list to a string where active characters are treated
% as strings from the start. The logic to the quoting normalisation is the
% same as used by \texttt{lualatexquotejobname}: check for balanced |"|, and
% assuming they balance strip all of them out before quoting the entire name
% if it contains spaces.
% \begin{macrocode}
\cs_new_protected:Npn \__file_name_sanitize:nN #1#2
{
\group_begin:
\seq_map_inline:Nn \l_char_active_seq
{
\tl_set:Nx \l_@@_tmp_tl { \iow_char:N ##1 }
\char_set_active_eq:NN ##1 \l_@@_tmp_tl
}
\tl_set:Nx \l_@@_tmp_tl {#1}
\tl_set:Nx \l_@@_tmp_tl
{ \tl_to_str:N \l_@@_tmp_tl }
\exp_args:NNNV \group_end:
\str_set:Nn #2 \l_@@_tmp_tl
}
\cs_new_protected:Npn \__file_name_quote:nN #1#2
{
\str_set:Nx #2 {#1}
\int_if_even:nF
{ 0 \tl_map_function:NN #2 \@@_name_quote_aux:n }
{
\__msg_kernel_error:nnx
{ kernel } { unbalanced-quote-in-filename } {#2}
}
\tl_remove_all:Nn #2 { " }
\tl_if_in:NnT #2 { ~ }
{ \str_set:Nx #2 { " \exp_not:V #2 " } }
}
\cs_new:Npn \@@_name_quote_aux:n #1
{ \token_if_eq_charcode:NNT #1 " { + 1 } }
% \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\file_get_full_name:nN, \file_get_full_name:VN}
% \begin{macro}{\@@_get_full_name_search:nN}
% The way to test if a file exists is to try to open it: if it does
% not exist then \TeX{} reports end-of-file. A search is made
% looking at each potential path in turn (starting from the current
% directory). The first location is of course treated as the correct
% one: this is done by jumping to \cs{__prg_break_point:}. If nothing
% is found, |#2| is returned empty. A special case when there is no
% extension is that once the first location is found we test the
% existence of the file with |.tex| extension in that directory, and
% if it exists we include the |.tex| extension in the result.
% \begin{macrocode}
\cs_new_protected:Npn \file_get_full_name:nN #1#2
{
\__file_name_sanitize:nN {#1} \l__file_base_name_str
\@@_get_full_name_search:nN { } \use:n
\seq_map_inline:Nn \l_file_search_path_seq
{ \@@_get_full_name_search:nN { ##1 / } \seq_map_break:n }
%<*package>
\cs_if_exist:NT \input@path
{
\tl_map_inline:Nn \input@path
{ \@@_get_full_name_search:nN { ##1 } \tl_map_break:n }
}
%</package>
\str_clear:N \l__file_full_name_str
\__prg_break_point:
\str_if_empty:NF \l__file_full_name_str
{
\exp_args:NV \file_parse_full_name:nNNN \l__file_full_name_str
\l_@@_dir_str \l_@@_name_str \l_@@_ext_str
\str_if_empty:NT \l_@@_ext_str
{
\__ior_open:No \g_@@_internal_ior
{ \l__file_full_name_str .tex }
\ior_if_eof:NF \g_@@_internal_ior
{ \str_put_right:Nn \l__file_full_name_str { .tex } }
}
}
\str_set_eq:NN #2 \l__file_full_name_str
\ior_close:N \g_@@_internal_ior
}
\cs_generate_variant:Nn \file_get_full_name:nN { V }
\cs_new_protected:Npn \@@_get_full_name_search:nN #1#2
{
\__file_name_quote:nN
{ \tl_to_str:n {#1} \l__file_base_name_str }
\l__file_full_name_str
\__ior_open:No \g_@@_internal_ior \l__file_full_name_str
\ior_if_eof:NF \g_@@_internal_ior { #2 { \__prg_break: } }
}
% \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}[TF]{\file_if_exist:n}
% The test for the existence of a file is a wrapper around the function to
% add a path to a file. If the file was found, the path contains
% something, whereas if the file was not located then the return value
% is empty.
% \begin{macrocode}
\prg_new_protected_conditional:Npnn \file_if_exist:n #1 { T , F , TF }
{
\file_get_full_name:nN {#1} \l__file_full_name_str
\str_if_empty:NTF \l__file_full_name_str
{ \prg_return_false: }
{ \prg_return_true: }
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\__file_missing:n}
% An error message for a missing file, also used in \cs{ior_open:Nn}.
% \begin{macrocode}
\cs_new_protected:Npn \__file_missing:n #1
{
\__file_name_sanitize:nN {#1} \l__file_base_name_str
\__msg_kernel_error:nnx { kernel } { file-not-found }
{ \l__file_base_name_str }
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\file_input:n}
% \begin{macro}{\@@_input:n, \@@_input:V}
% \begin{macro}{\@@_input_push:n}
% \begin{macro}{\@@_input_pop:}
% \begin{macro}{\@@_input_pop:nnn}
% Loading a file is done in a safe way, checking first that the file
% exists and loading only if it does. Push the file name on the
% \cs{g_@@_stack_seq}, and add it to the file list, either
% \cs{g_@@_record_seq}, or \cs{@filelist} in package mode.
% \begin{macrocode}
\cs_new_protected:Npn \file_input:n #1
{
\file_get_full_name:nN {#1} \l__file_full_name_str
\str_if_empty:NTF \l__file_full_name_str
{ \__file_missing:n {#1} }
{ \@@_input:V \l__file_full_name_str }
}
\cs_new_protected:Npn \@@_input:n #1
{
%<*initex>
\seq_gput_right:Nn \g_@@_record_seq {#1}
%</initex>
%<*package>
\clist_if_exist:NTF \@filelist
{ \@addtofilelist {#1} }
{ \seq_gput_right:Nn \g_@@_record_seq {#1} }
%</package>
\@@_input_push:n {#1}
\tex_input:D #1 \c_space_tl
\@@_input_pop:
}