Debian Codegraph @

texmf-dist/source/latex/base/ltfilehook.dtx

raw
1 % \iffalse meta-comment
2 %
3 % Copyright (C) 2020-2025
4 % Frank Mittelbach, Phelype Oleinik and The LaTeX Project
5 %
6 % This file is part of the LaTeX base system.
7 % -------------------------------------------
8 %
9 % It may be distributed and/or modified under the
10 % conditions of the LaTeX Project Public License, either version 1.3c
11 % of this license or (at your option) any later version.
12 % The latest version of this license is in
13 % https://www.latex-project.org/lppl.txt
14 % and version 1.3c or later is part of all distributions of LaTeX
15 % version 2008 or later.
16 %
17 % This file has the LPPL maintenance status "maintained".
18 %
19 % The list of all files belonging to the LaTeX base distribution is
20 % given in the file `manifest.txt'. See also `legal.txt' for additional
21 % information.
22 %
23 % The list of derived (unpacked) files belonging to the distribution
24 % and covered by LPPL is defined by the unpacking scripts (with
25 % extension .ins) which are part of the distribution.
26 %
27 % \fi
28 %
29 % \iffalse
30 %
31 %%% From File: ltfilehook.dtx
32 %
33 % \fi
34 %\iffalse
35 %<*driver,structuredlog>
36 %\fi
37 \def\ltfilehookdate{2025/06/20}
38 \def\ltfilehookversion{v1.0o}
39 %\iffalse
40 %</driver,structuredlog>
41 %<*driver>
42 %\fi
43 \expanded{\noexpand\ProvidesFile{ltfilehook.dtx}
44 [\ltfilehookdate\space \ltfilehookversion\space LaTeX Kernel (hooks)]}
45 % \iffalse
46 %
47 \documentclass{l3doc}
48
49 \providecommand\InternalDetectionOff{}
50 \providecommand\InternalDetectionOn{}
51
52
53 \usepackage{structuredlog} % for demonstration
54
55 \EnableCrossrefs
56 \CodelineIndex
57 \begin{document}
58 \DocInput{ltfilehook.dtx}
59 \end{document}
60 %</driver>
61 %
62 % \fi
63 %
64 %
65 % \long\def\fmi#1{\begin{quote}\itshape Todo: #1\end{quote}}
66 %
67 % \providecommand\hook[1]{\texttt{#1}}
68 %
69 %
70 % \title{The \texttt{ltfilehook} documentation\thanks{This code has version
71 % \ltfilehookversion\ dated \ltfilehookdate, \copyright\ \LaTeX\
72 % Project.}}
73 %
74 % \author{Frank Mittelbach, Phelype Oleinik, \LaTeX{} Project Team}
75 %
76 % \maketitle
77 %
78 %
79 %
80 % \tableofcontents
81 %
82 % \section{Introduction}
83 %
84 %
85 %
86 % \subsection{Provided hooks}
87 %
88 % The code offers a number of hooks into which packages (or the
89 % user) can add code to support different use cases.
90 % Many hooks are offered as pairs (i.e., the second hook is
91 % reversed. Also important to know is that these pairs are
92 % properly nested with respect to other pairs of hooks.
93 %
94 % There are hooks that are executed for all files of a certain type
95 % (if they contain code), e.g., for all \enquote{include files} or
96 % all \enquote{packages},
97 % and there are also hooks that are specific to a single file,
98 % e.g., do something after the package \texttt{foo.sty} has been
99 % loaded.
100 %
101 %
102 % \subsection{General hooks for file reading}
103 % \label{sec:general-file-hooks}
104 %
105 % There are four hooks that are called for each file that is read
106 % using document-level commands such as \cs{input}, \cs{include},
107 % \cs{usepackage}, etc. They are not called for files read using
108 % internal low-level methods, such as \cs{@input} or \cs{openin}.
109 %
110 % \begin{variable}{file/before,file/.../before,
111 % file/.../after,file/after,
112 % }
113 % These are:
114 % \begin{description}
115 % \item[\hook{file/before}, \hook{file/\meta{file-name}/before}]
116 %
117 % These hooks are executed in that order just before the file is
118 % loaded for reading. The code of the first hook is used
119 % with every file, while the second is executed only for the
120 % file with matching \meta{file-name} allowing you to specify
121 % code that only applies to one file.
122 %
123 % \item[\hook{file/\meta{file-name}/after}, \hook{file/after}]
124 %
125 % These hooks are executed after the file with name \meta{file-name} has
126 % been fully consumed. The order is swapped (the specific one
127 % comes first) so that the \hook{/before} and \hook{/after}
128 % hooks nest properly, which is important if any of them involve
129 % grouping (e.g., contain environments, for example).
130 % Furthermore both hooks are reversed hooks to support correct
131 % nesting of different packages adding code to both
132 % \hook{/before} and \hook{/after} hooks.
133 %
134 % \end{description}
135 % \end{variable}
136 %
137 %
138 % So the overall sequence of hook processing for any file read
139 % through the user interface commands of \LaTeX{} is:
140 %
141 % \begin{tabbing}
142 % mm\=mm\=mm\=mm\=\kill
143 % \>\cs{UseHook}\texttt{\{\hook{file/before}\}} \\
144 % \>\cs{UseHook}\texttt{\{\hook{file/\meta{file name}/before}\}} \\
145 % \>\> \meta{file contents} \\
146 % \>\cs{UseHook}\texttt{\{\hook{file/\meta{file name}/after}\}} \\
147 % \>\cs{UseHook}\texttt{\{\hook{file/after}\}}
148 % \end{tabbing}
149 %
150 % The file hooks only refer to the file by its name and extension,
151 % so the \meta{file name} should be the file name as it is on the
152 % filesystem with extension (if any) and without paths. Different
153 % from \cs{input} and similar commands, the \texttt{.tex}
154 % extension is not assumed in hook \meta{file name}, so \texttt{.tex}
155 % files must be specified
156 % with their extension to be recognized.
157 % Files within subfolders should also be addressed by their name and
158 % extension only.
159 %
160 % Extensionless files also work, and should then be given without
161 % extension. Note however that \TeX{} prioritizes \texttt{.tex}
162 % files, so if two files \texttt{foo} and \texttt{foo.tex} exist in
163 % the search path, only the latter will be seen.
164 %
165 % When a file is input, the \meta{file name} is available in
166 % \cs{CurrentFile}, which is then used when accessing the
167 % \hook{file/\meta{file name}/before} and
168 % \hook{file/\meta{file name}/after}.
169 %
170 % \begin{variable}{\CurrentFile}
171 % The name of the file about to be read (or just finished) is
172 % available to the hooks through \cs{CurrentFile} (there is no
173 % \texttt{expl3} name for it for now). The file is always provided
174 % with its extension, i.e., how it appears on your hard drive, but
175 % without any specified path to it. For example,
176 % \verb=\input{sample}= and \verb=\input{app/sample.tex}= would
177 % both have \cs{CurrentFile} being \texttt{sample.tex}.
178 % \end{variable}
179 %
180 % \begin{variable}{\CurrentFilePath}
181 % The path to the current file (complement to \cs{CurrentFile}) is
182 % available in \cs{CurrentFilePath} if needed.
183 % The paths returned in \cs{CurrentFilePath} are only user paths,
184 % given through \cs{input@path} (or \pkg{expl3}'s equivalent
185 % \cs{l_file_search_path_seq}) or by directly typing in the path
186 % in the \cs{input} command or equivalent. Files located by
187 % \texttt{kpsewhich} get the path added internally by the \TeX{}
188 % implementation, so at the macro level it looks as if the file were
189 % in the current folder, so the path in \cs{CurrentFilePath} is empty
190 % in these cases (package and class files, mostly).
191 % \end{variable}
192 %
193 % \begin{variable}{\CurrentFileUsed,\CurrentFilePathUsed}
194 % In normal circumstances these are identical to \cs{CurrentFile} and
195 % \cs{CurrentFilePath}. They will differ when a file substitution
196 % has occurred for \cs{CurrentFile}. In that case,
197 % \cs{CurrentFileUsed} and \cs{CurrentFilePathUsed} will hold the
198 % actual file name and path loaded by \LaTeX, while \cs{CurrentFile}
199 % and \cs{CurrentFilePath} will hold the names that were
200 % \emph{asked for}. Unless doing very specific work on the file
201 % being read, \cs{CurrentFile} and \cs{CurrentFilePath} should be
202 % enough.
203 % \end{variable}
204 %
205 % \subsection{Hooks for package and class files}
206 %
207 % Commands to load package and class files (e.g., \cs{usepackage},
208 % \cs{RequirePackage}, \cs{LoadPackageWithOptions}, etc.) offer the
209 % hooks from section~\ref{sec:general-file-hooks} when they are
210 % used to load a package or class file, e.g.,
211 % \hook{file/array.sty/after} would be called after the
212 % \pkg{array} package got loaded. But as packages and classes form as special group
213 % of files, there are some additional hooks available that only
214 % apply when a package or class is loaded.
215 %
216 %
217 % \begin{variable}{
218 % package/before,package/after,
219 % package/.../before,package/.../after,
220 % class/before,class/after,
221 % class/.../before,class/.../after,
222 % }
223 % These are:
224 % \begin{description}
225 % \item[\hook{package/before}, \hook{package/after}]
226 %
227 % These hooks are called for each package being loaded.
228 %
229 % \item[\hook{package/\meta{name}/before},
230 % \hook{package/\meta{name}/after}]
231 %
232 % These hooks are additionally called if the package name is
233 % \meta{name} (without extension).
234 %
235 % \item[\hook{class/before}, \hook{class/after}]
236 %
237 % These hooks are called for each class being loaded.
238 %
239 % \item[\hook{class/\meta{name}/before}, \hook{class/\meta{name}/after}]
240 %
241 % These hooks are additionally called if the class name is
242 % \meta{name} (without extension).
243 %
244 % \end{description}
245 % All \hook{/after} hooks are implemented as reversed hooks.
246 % \end{variable}
247 %
248 % \bigskip
249 %
250 % The overall sequence of execution for \cs{usepackage}
251 % and friends is:
252 % \begin{tabbing}
253 % mm\=mm\=mm\=mm\=\kill
254 % \>\cs{UseHook}\texttt{\{\hook{package/before}\}} \\
255 % \>\cs{UseOneTimeHook}\texttt{\{\hook{package/\meta{package name}/before}\}} \\[5pt]
256 % \>\>\cs{UseHook}\texttt{\{\hook{file/before}\}} \\
257 % \>\>\cs{UseHook}\texttt{\{\hook{file/\meta{package name}.sty/before}\}} \\
258 % \>\>\> \meta{package contents} \\
259 % \>\>\cs{UseHook}\texttt{\{\hook{file/\meta{package name}.sty/after}\}} \\
260 % \>\>\cs{UseHook}\texttt{\{\hook{file/after}\}} \\[5pt]
261 % \>\>\emph{code from \cs{AtEndOfPackage} if
262 % used inside the package} \\[5pt]
263 % \>\cs{UseOneTimeHook}\texttt{\{\hook{package/\meta{package name}/after}\}} \\
264 % \>\cs{UseHook}\texttt{\{\hook{package/after}\}}
265 % \end{tabbing}
266 % and similar for class file loading, except that \hook{package/}
267 % is replaced by \hook{class/} and \cs{AtEndOfPackage} by
268 % \cs{AtEndOfClass}.
269 %
270 % If a package or class is not loaded none of the hooks are executed!
271 %
272 % All class or package hooks involving the name of the class or
273 % package are implemented as
274 % one-time hooks, whereas all other such hooks are normal hooks.
275 % This allows for the following use case
276 %\begin{verbatim}
277 % \AddToHook{package/varioref/after}
278 % { ... apply my customizations if the package gets
279 % loaded (or was loaded already) ... }
280 %\end{verbatim}
281 % without the need to first test if the package is already loaded.
282 %
283 %
284 %
285 %
286 %
287 % \subsection{Hooks for \cs{include} files}
288 %
289 % To manage \cs{include} files, \LaTeX{} issues a \cs{clearpage}
290 % before and after loading such a file. Depending on the use case
291 % one may want to execute code before or after these
292 % \cs{clearpage}s especially for the one that is issued at the end.
293 %
294 % Executing code before the final \cs{clearpage}, means that the
295 % code is processed while the last page of the included material is
296 % still under construction. Executing code after it means that all
297 % floats from inside the include file are placed (which
298 % might have added further pages) and the final page has finished.
299 %
300 % Because of these different scenarios we offer hooks in three
301 % places.\footnote{If you want to execute code before the first
302 % \cs{clearpage} there is no need to use a hook---you can write it
303 % directly in front of the \cs{include}.}
304 % None of the hooks are executed when an \cs{include} file is
305 % bypassed because of an \cs{includeonly} declaration. They are,
306 % however, all executed if \LaTeX{} makes an attempt to load the
307 % \cs{include} file (even if it doesn't exist and all that happens
308 % is \enquote{\texttt{No file \meta{filename}.tex}}).
309 %
310 %
311 % \begin{variable}{include/before,include/.../before,
312 % include/end,include/.../end,
313 % include/after,include/.../after,
314 % }
315 % These are:
316 % \begin{description}
317 %
318 % \item[\hook{include/before}, \hook{include/\meta{name}/before}]
319 %
320 % These hooks are executed (in that order) after the initial
321 % \cs{clearpage} and after \texttt{.aux} file is changed to use
322 % \texttt{\meta{name}.aux}, but before the
323 % \texttt{\meta{name}.tex} file is loaded. In other words they are executed
324 % at the very beginning of the first page of the \cs{include}
325 % file.
326 %
327 %
328 % \item[\hook{include/\meta{name}/end}, \hook{include/end}]
329 %
330 % These hooks are executed (in that order) after \LaTeX{} has
331 % stopped reading from the \cs{include} file, but before it has
332 % issued a \cs{clearpage} to output any deferred floats.
333 %
334 %
335 % \item[\hook{include/\meta{name}/after}, \hook{include/after}]
336 %
337 % These hooks are executed (in that order) after \LaTeX{} has
338 % issued the \cs{clearpage} but before is has switched back
339 % writing to the main \texttt{.aux} file. Thus technically we are
340 % still inside the \cs{include} and if the hooks generate any
341 % further typeset material including anything that writes to the
342 % \texttt{.aux} file, then it would be considered part of the
343 % included material and bypassed if it is not loaded because of
344 % some \cs{includeonly} statement.\footnotemark
345 %
346 % \item[\hook{include/excluded}, \hook{include/\meta{name}/excluded}]
347 %
348 % The above hooks for \cs{include} files are only executed when
349 % the file is loaded (or more exactly the load is attempted). If,
350 % however, the \cs{include} file is explicitly excluded (through
351 % an \cs{includeonly} statement) the above
352 % hooks are bypassed and instead the \hook{include/excluded}
353 % hook followed by the \hook{include/\meta{name}/excluded} hook
354 % are executed. This happens after
355 % \LaTeX{} has loaded the \texttt{.aux} file for this include file,
356 % i.e., after \LaTeX{} has updated its counters to pretend that the file
357 % was seen.
358 %
359 % \end{description}
360 % \end{variable}\footnotetext{For that reason
361 % another \cs{clearpage} is executed after these hooks which
362 % normally does nothing, but starts a new page if further material
363 % got added this way.}
364 %
365 %
366 % All \hook{include} hooks involving the name of the included file are implemented as
367 % one-time hooks (whereas all other such hooks are normal hooks).
368 %
369 % If you want to execute code that is run for every \cs{include}
370 % regardless of whether or not it is excluded, use the
371 % \hook{cmd/include/before} or \hook{cmd/include/after} hooks.
372 %
373 %
374 %
375 % \subsection{High-level interfaces for \LaTeX{}}
376 %
377 % We do not provide any additional wrappers around the hooks (like
378 % \pkg{filehook} or \pkg{scrlfile} do) because we believe that for
379 % package writers the high-level commands from the hook management,
380 % e.g., \cs{AddToHook}, etc.\
381 % are sufficient and in fact easier to work with, given that the hooks
382 % have consistent naming conventions.
383 %
384 %
385 %
386 % \subsection{Kernel, class, and package interfaces for \LaTeX{}}
387 %
388 % \begin{function}{\declare@file@substitution,\undeclare@file@substitution}
389 % \begin{syntax}
390 % \cs{declare@file@substitution} \Arg{file} \Arg{replacement-file}
391 % \cs{undeclare@file@substitution} \Arg{file}
392 % \end{syntax}
393 % If \meta{file} is requested for loading replace it with
394 % \meta{replacement-file}. \cs{CurrentFile} remains pointing to
395 % \meta{file} but \cs{CurrentFileUsed} will show the file actually
396 % loaded.
397 %
398 % The main use case for this declaration is to provide a corrected
399 % version of a package that can't be changed (due to its license)
400 % but no longer functions because of \LaTeX{} kernel changes, for
401 % example, or to provide a version that makes use of new kernel
402 % functionality while the original package remains available for
403 % use with older releases.
404 % As such it is mainly meant for use in the \LaTeX{} kernel but
405 % other use cases are conceivable.
406 %
407 % The \cs{undeclare@file@substitution} declaration undoes a
408 % substitution made earlier.
409 %
410 % \begin{quote}
411 % \em
412 % Please do not misuse this functionality and replace a file with
413 % another unless if really needed and only if the new version is
414 % implementing the same functionality as the original one!
415 % \end{quote}
416 % \end{function}
417 %
418 % \begin{function}{\disable@package@load,\reenable@package@load}
419 % \begin{syntax}
420 % \cs{disable@package@load} \Arg{package} \Arg{alternate-code}
421 % \cs{reenable@package@load} \Arg{package}
422 % \end{syntax}
423 % If \meta{package} is requested, do not load it but instead run
424 % \meta{alternate-code} which could issue a warning, error or any
425 % other code.
426 %
427 % The main use case is for classes that want to restrict the set of
428 % supported packages or contain code that make the use of some
429 % packages impossible. So rather than waiting until the document
430 % breaks they can set up informative messages why certain packages
431 % are not available.
432 %
433 % The function is only implemented for packages not for arbitrary
434 % files and again it should only be applied if there are good
435 % reasons for doing this.\footnote{Just to be sure: \enquote{I don't
436 % like this package by somebody else} is not a good one :-)}
437 % \end{function}
438 %
439 %
440 % \subsection{A sample package for structuring the log output}
441 %
442 % As an application we provide the package \pkg{structuredlog} that
443 % adds lines to the \texttt{.log} when a file is opened and closed
444 % for reading keeping track of nesting level es well.
445 % For example, for the current document it adds the lines
446 %\begin{verbatim}
447 % = (LEVEL 1 START) t1lmr.fd
448 % = (LEVEL 1 STOP) t1lmr.fd
449 % = (LEVEL 1 START) supp-pdf.mkii
450 % = (LEVEL 1 STOP) supp-pdf.mkii
451 % = (LEVEL 1 START) nameref.sty
452 % == (LEVEL 2 START) refcount.sty
453 % == (LEVEL 2 STOP) refcount.sty
454 % == (LEVEL 2 START) gettitlestring.sty
455 % == (LEVEL 2 STOP) gettitlestring.sty
456 % = (LEVEL 1 STOP) nameref.sty
457 % = (LEVEL 1 START) ltfilehook-doc.out
458 % = (LEVEL 1 STOP) ltfilehook-doc.out
459 % = (LEVEL 1 START) ltfilehook-doc.out
460 % = (LEVEL 1 STOP) ltfilehook-doc.out
461 % = (LEVEL 1 START) ltfilehook-doc.hd
462 % = (LEVEL 1 STOP) ltfilehook-doc.hd
463 % = (LEVEL 1 START) ltfilehook.dtx
464 % == (LEVEL 2 START) ot1lmr.fd
465 % == (LEVEL 2 STOP) ot1lmr.fd
466 % == (LEVEL 2 START) omllmm.fd
467 % == (LEVEL 2 STOP) omllmm.fd
468 % == (LEVEL 2 START) omslmsy.fd
469 % == (LEVEL 2 STOP) omslmsy.fd
470 % == (LEVEL 2 START) omxlmex.fd
471 % == (LEVEL 2 STOP) omxlmex.fd
472 % == (LEVEL 2 START) umsa.fd
473 % == (LEVEL 2 STOP) umsa.fd
474 % == (LEVEL 2 START) umsb.fd
475 % == (LEVEL 2 STOP) umsb.fd
476 % == (LEVEL 2 START) ts1lmr.fd
477 % == (LEVEL 2 STOP) ts1lmr.fd
478 % == (LEVEL 2 START) t1lmss.fd
479 % == (LEVEL 2 STOP) t1lmss.fd
480 % = (LEVEL 1 STOP) ltfilehook.dtx
481 %\end{verbatim}
482 % Thus if you inspect an issue in the \texttt{.log} it is easy to
483 % figure out in which file it occurred, simply by searching back for
484 % \texttt{LEVEL} and if it is a \texttt{STOP} then remove 1 from
485 % the level value and search further for \texttt{LEVEL} with that value
486 % which should then be the \texttt{START} level of the file you are in.
487 %
488 % \MaybeStop{\setlength\IndexMin{200pt} \PrintIndex }
489 %
490 %
491 % \section{The Implementation}
492 %
493 % \begin{macrocode}
494 %<*2ekernel>
495 % \end{macrocode}
496 %
497 % \begin{macrocode}
498 %<@@=filehook>
499 % \end{macrocode}
500 %
501 % \changes{v1.0k}{2021/05/24}{Use \cs{msg_...} instead of \cs{__kernel_msg...}}
502 %
503 %
504 % \subsection{Document and package-level commands}
505 %
506 %
507 % \begin{macro}{\CurrentFile,\CurrentFilePath}
508 % \begin{macro}{\CurrentFileUsed,\CurrentFilePathUsed}
509 % User-level macros that hold the current file name and file path.
510 % These are used internally as well because the code takes care to
511 % protect against a possible redefinition of these macros in the
512 % loaded file (it's necessary anyway to make hooks work with nested
513 % \cs{input}). The versions |\...Used| hold the \emph{actual} file
514 % name and path that is loaded by \LaTeX, whereas the other two hold
515 % the name as requested. They will differ in case there's a file
516 % substitution.
517 % \begin{macrocode}
518 %</2ekernel>
519 %<*2ekernel|latexrelease>
520 %<latexrelease>\IncludeInRelease{2020/10/01}%
521 %<latexrelease> {\CurrentFile}{Hook management file}%
522 \ExplSyntaxOn
523 \tl_new:N \CurrentFile
524 \tl_new:N \CurrentFilePath
525 \tl_new:N \CurrentFileUsed
526 \tl_new:N \CurrentFilePathUsed
527 \ExplSyntaxOff
528 %</2ekernel|latexrelease>
529 %<latexrelease>\EndIncludeInRelease
530 % \end{macrocode}
531 %
532 % \begin{macrocode}
533 %<latexrelease>\IncludeInRelease{0000/00/00}%
534 %<latexrelease> {\CurrentFile}{Hook management file}%
535 %<latexrelease>
536 %<latexrelease>\let \CurrentFile \@undefined
537 %<latexrelease>\let \CurrentFilePath \@undefined
538 %<latexrelease>\let \CurrentFileUsed \@undefined
539 %<latexrelease>\let \CurrentFilePathUsed \@undefined
540 %<latexrelease>
541 %<latexrelease>\EndIncludeInRelease
542 %<*2ekernel>
543 % \end{macrocode}
544 % \end{macro}
545 % \end{macro}
546 %
547 %
548 %
549 % \subsection{\pkg{expl3} helpers}
550 %
551 % \begin{macrocode}
552 %</2ekernel>
553 %<*2ekernel|latexrelease>
554 %<latexrelease>\IncludeInRelease{2020/10/01}%
555 %<latexrelease> {\@@_file_parse_full_name:nN}{File helpers}%
556 \ExplSyntaxOn
557 % \end{macrocode}
558 %
559 % \begin{macro}{
560 % \@@_file_parse_full_name:nN,
561 % \@@_full_name:nn,
562 % }
563 % A utility macro to trigger \pkg{expl3}'s file-parsing and lookup,
564 % and return a normalized representation of the file name. If the
565 % queried file doesn't exist, no normalization takes place.
566 % The output of \cs{@@_file_parse_full_name:nN} is passed on to the
567 % |#2|---a 3-argument macro that takes the \meta{path}, \meta{base},
568 % and \meta{ext} parts of the file name.
569 %
570 % \begin{macrocode}
571 \cs_new:Npn \@@_file_parse_full_name:nN #1
572 {
573 \exp_args:Nf \file_parse_full_name_apply:nN
574 {
575 \exp_args:Nf \@@_full_name:nn
576 { \file_full_name:n {#1} } {#1}
577 }
578 }
579 \cs_new:Npn \@@_full_name:nn #1 #2
580 {
581 \tl_if_empty:nTF {#1}
582 { \tl_trim_spaces:n {#2} }
583 { \tl_trim_spaces:n {#1} }
584 }
585 % \end{macrocode}
586 % \end{macro}
587 %
588 % \begin{macro}{
589 % \@@_if_no_extension:nTF,
590 % \@@_drop_extension:N
591 % }
592 % Some actions depend on whether the file extension was explicitly
593 % given, and sometimes the extension has to be removed. The macros
594 % below use \cs{@@_file_parse_full_name:nN} to split up the file name
595 % and either check if \meta{ext} (|#3|) is empty, or discard it.
596 % \begin{macrocode}
597 \cs_new:Npn \@@_if_no_extension:nTF #1
598 {
599 \exp_args:Ne \tl_if_empty:nTF
600 { \file_parse_full_name_apply:nN {#1} \use_iii:nnn }
601 }
602 \cs_new_protected:Npn \@@_drop_extension:N #1
603 {
604 \tl_gset:Nx #1
605 {
606 \exp_args:NV \@@_file_parse_full_name:nN #1
607 \@@_drop_extension_aux:nnn
608 }
609 }
610 \cs_new:Npn \@@_drop_extension_aux:nnn #1 #2 #3
611 { \tl_if_empty:nF {#1} { #1 / } #2 }
612 % \end{macrocode}
613 % \end{macro}
614 %
615 % \begin{macro}{\g_@@_input_file_seq,\l_@@_internal_tl}
616 % \begin{macro}{\@@_file_push:,\@@_file_pop:}
617 % \begin{macro}{\@@_file_pop_assign:nnnn}
618 % Yet another stack, to keep track of \cs{CurrentFile} and
619 % \cs{CurrentFilePath} with nested \cs{input}s. At the beginning of
620 % \cs{InputIfFileExists}, the current value of \cs{CurrentFilePath}
621 % and \cs{CurrentFile} is pushed to \cs{g_@@_input_file_seq}, and
622 % at the end, it is popped and the value reassigned. Some other
623 % places don't use \cs{InputIfFileExists} directly (\cs{include}) or
624 % need \cs{CurrentFile} earlier (\cs{@onefilewithoptions}), so these
625 % are manually used elsewhere as well.
626 % \changes{v1.0h}{2021/03/18}
627 % {Define \cs{g__filehook_input_file_seq} to avoid losing data when
628 % rolling back.}
629 % \changes{v1.0l}{2021/08/27}{Internal message name changes}
630 % \begin{macrocode}
631 \tl_new:N \l_@@_internal_tl
632 \seq_if_exist:NF \g_@@_input_file_seq
633 { \seq_new:N \g_@@_input_file_seq }
634 \cs_new_protected:Npn \@@_file_push:
635 {
636 \seq_gpush:Nx \g_@@_input_file_seq
637 {
638 { \CurrentFilePathUsed } { \CurrentFileUsed }
639 { \CurrentFilePath } { \CurrentFile }
640 }
641 }
642 \cs_new_protected:Npn \@@_file_pop:
643 {
644 \seq_gpop:NNTF \g_@@_input_file_seq \l_@@_internal_tl
645 { \exp_after:wN \@@_file_pop_assign:nnnn \l_@@_internal_tl }
646 {
647 \msg_error:nnn { latex2e } { should-not-happen }
648 { Tried~to~pop~from~an~empty~file~name~stack. }
649 }
650 }
651 \cs_new_protected:Npn \@@_file_pop_assign:nnnn #1 #2 #3 #4
652 {
653 \tl_set:Nn \CurrentFilePathUsed {#1}
654 \tl_set:Nn \CurrentFileUsed {#2}
655 \tl_set:Nn \CurrentFilePath {#3}
656 \tl_set:Nn \CurrentFile {#4}
657 }
658 \ExplSyntaxOff
659 % \end{macrocode}
660 % \end{macro}
661 % \end{macro}
662 % \end{macro}
663 %
664 % \begin{macrocode}
665 %</2ekernel|latexrelease>
666 %<latexrelease>\EndIncludeInRelease
667 % \end{macrocode}
668 %
669 % When rolling forward the following expl3 functions may not be defined.
670 % If we roll back the code does nothing.
671 % \changes{v1.0d}{2020/11/24}{Support for roll forward (gh/434)}
672 % \InternalDetectionOff
673 % \begin{macrocode}
674 %<latexrelease>\IncludeInRelease{2020/10/01}%
675 %<latexrelease> {\file_parse_full_name_apply:nN}{Roll forward help}%
676 %<latexrelease>
677 %<latexrelease>\ExplSyntaxOn
678 %<latexrelease>\cs_if_exist:NF\file_parse_full_name_apply:nN
679 %<latexrelease>{
680 %<latexrelease>\cs_new:Npn \file_parse_full_name_apply:nN #1
681 %<latexrelease> {
682 %<latexrelease> \exp_args:Ne \__file_parse_full_name_auxi:nN
683 %<latexrelease> { \__kernel_file_name_sanitize:n {#1} }
684 %<latexrelease> }
685 %<latexrelease>\cs_new:Npn \__file_parse_full_name_auxi:nN #1
686 %<latexrelease> {
687 %<latexrelease> \__file_parse_full_name_area:nw { } #1
688 %<latexrelease> / \s__file_stop
689 %<latexrelease> }
690 %<latexrelease>\cs_new:Npn \__file_parse_full_name_area:nw #1 #2 / #3 \s__file_stop
691 %<latexrelease> {
692 %<latexrelease> \tl_if_empty:nTF {#3}
693 %<latexrelease> { \__file_parse_full_name_base:nw { } #2 . \s__file_stop {#1} }
694 %<latexrelease> { \__file_parse_full_name_area:nw { #1 / #2 }
695 %<latexrelease> #3 \s__file_stop }
696 %<latexrelease> }
697 %<latexrelease>\cs_new:Npn \__file_parse_full_name_base:nw #1 #2 . #3 \s__file_stop
698 %<latexrelease> {
699 %<latexrelease> \tl_if_empty:nTF {#3}
700 %<latexrelease> {
701 %<latexrelease> \tl_if_empty:nTF {#1}
702 %<latexrelease> {
703 %<latexrelease> \tl_if_empty:nTF {#2}
704 %<latexrelease> { \__file_parse_full_name_tidy:nnnN { } { } }
705 %<latexrelease> { \__file_parse_full_name_tidy:nnnN { .#2 } { } }
706 %<latexrelease> }
707 %<latexrelease> { \__file_parse_full_name_tidy:nnnN {#1} { .#2 } }
708 %<latexrelease> }
709 %<latexrelease> { \__file_parse_full_name_base:nw { #1 . #2 }
710 %<latexrelease> #3 \s__file_stop }
711 %<latexrelease> }
712 %<latexrelease>\cs_new:Npn \__file_parse_full_name_tidy:nnnN #1 #2 #3 #4
713 %<latexrelease> {
714 %<latexrelease> \exp_args:Nee #4
715 %<latexrelease> {
716 %<latexrelease> \str_if_eq:nnF {#3} { / } { \use_none:n }
717 %<latexrelease> #3 \prg_do_nothing:
718 %<latexrelease> }
719 %<latexrelease> { \use_none:n #1 \prg_do_nothing: }
720 %<latexrelease> {#2}
721 %<latexrelease> }
722 %<latexrelease>}
723 %<latexrelease>\ExplSyntaxOff
724 %<latexrelease>
725 %<latexrelease>\EndIncludeInRelease
726 %<*2ekernel>
727 % \end{macrocode}
728 % \InternalDetectionOn
729 %
730 % \begin{macrocode}
731 %<@@=>
732 % \end{macrocode}
733 %
734 % \subsection{Declaring the file-related hooks}
735 %
736 % These hooks have names with three-parts that
737 % start with \hook{file/}, \hook{include/},
738 % \hook{class/} or \hook{package/} and end with \hook{/before} or
739 % \hook{/after} (or \hook{/end} in the case of \hook{include/}).
740 % They are all generic hooks
741 % so will be declared only if code is added to them;
742 % this declaration is done for you automatically and, indeed, they should
743 % not be declared explicitly.
744 %
745 % Those named \hook{.../after} and \hook{include/.../end}
746 % are, when code is added, declared as reversed hooks.
747 %
748 %
749 % \subsection{Patching \LaTeX{}'s \cs{InputIfFileExists} command}
750 %
751 % Most of what we have to do is adding \cs{UseHook} into several
752 % \LaTeXe{} core commands, because of some circular dependencies in the
753 % kernel we do this only now and not in \texttt{ltfiles}.
754 %
755 % \begin{macro}{\InputIfFileExists}
756 % \begin{macro}{\@input@file@exists@with@hooks}
757 % \begin{macro}{\unqu@tefilef@und}
758 % \cs{InputIfFileExists} loads any file if it is available so we
759 % have to add the hooks \hook{file/before} and
760 % \hook{file/after} in the right places. If the file doesn't
761 % exist no hooks should be executed.
762 % \begin{macrocode}
763 %</2ekernel>
764 %<latexrelease>\IncludeInRelease{2020/10/01}%
765 %<latexrelease> {\InputIfFileExists}{Hook management (files)}%
766 %<*2ekernel|latexrelease>
767 % \end{macrocode}
768 %
769 % \begin{macrocode}
770 \let\InputIfFileExists\@undefined
771 \DeclareRobustCommand \InputIfFileExists[2]{%
772 \IfFileExists{#1}%
773 {%
774 \@expl@@@filehook@file@push@@
775 \@filehook@set@CurrentFile
776 % \end{macrocode}
777 % We pre-expand \cs{@filef@und} so that in case another file is
778 % loaded in the true branch of \cs{InputIfFileExists}, these don't
779 % change their value meanwhile. This isn't a worry with
780 % \cs[no-index]{CurrentFile...} because they are kept in a stack.
781 %
782 % \changes{v1.0d}{2020/11/20}
783 % {Move loading to \cs{@input@file@exists@with@hooks} and expand
784 % \cs{@filef@und} to avoid getting the wrong file name in the case of
785 % a substitution.}
786 % \begin{macrocode}
787 \expandafter\@swaptwoargs\expandafter
788 {\expandafter\@input@file@exists@with@hooks
789 \expandafter{\@filef@und}}%
790 {#2}%
791 \@expl@@@filehook@file@pop@@
792 }%
793 }
794 \def\@input@file@exists@with@hooks#1{%
795 % \end{macrocode}
796 % If the file exists then \cs{CurrentFile} holds its name. But we
797 % can't rely on that still being true after the file has been
798 % processed. Thus for using the name in the file hooks we need to
799 % preserve the name and then restore it for the
800 % \hook{file/.../after} hook.
801 %
802 % The hook always refers to the file requested by the user. The hook
803 % is \emph{always} loaded for \cs{CurrentFile} which usually is the
804 % same as \cs{CurrentFileUsed}. In the case of a file replacement,
805 % the \cs{CurrentFileUsed} holds the actual file loaded. In any case
806 % the file names are normalized so that the hooks work on the real
807 % file name, rather than what the user typed in.
808 %
809 % \pkg{expl3}'s \cs{file_full_name:n} normalizes the file
810 % name (to factor out differences in the |.tex| extension), and
811 % then does a file lookup to take into account a possible path from
812 % \cs{l_file_search_path_seq} and \cs{input@path}. However only
813 % the file name and extension are returned so that file hooks can
814 % refer to the file by their name only. The path to the file is
815 % returned in \cs{CurrentFilePath}.
816 % \changes{v1.0e}{2021/01/07}{Restore \cs[no-index]{CurrentFile(Path)(Used)}
817 % after the input (gh/464)}
818 % \begin{macrocode}
819 \edef\reserved@a{%
820 \@expl@@@filehook@file@pop@assign@@nnnn
821 {\CurrentFilePathUsed}%
822 {\CurrentFileUsed}%
823 {\CurrentFilePath}%
824 {\CurrentFile}}%
825 \expandafter\@swaptwoargs\expandafter{\reserved@a}%
826 % \end{macrocode}
827 %
828 % Before adding to the file list we need to make all (letter) characters
829 % catcode~11, because several packages use constructions like
830 % \begin{verbatim}
831 % \filename@parse{<filename>}
832 % \ifx\filename@ext\@clsextension
833 % ...
834 % \fi
835 % \end{verbatim}
836 % and that doesn't work if \cs{filename@ext} is \cs{detokenize}d.
837 % Making \cs{@clsextension} a string doesn't help much because some
838 % packages define their own \cs[no-index]{<prefix>@someextension} with
839 % normal catcodes. This is not entirely correct because packages loaded
840 % (somehow) with catcode~12 alphabetic tokens (say, as the result of
841 % a \cs{string} or \cs{detokenize} command, or from a \TeX{} string like
842 % \cs{jobname}) will have these character tokens incorrectly turned into
843 % letter tokens. This however is rare, so we'll go for the all-letters
844 % approach (grepping the packages in \TeX{} Live didn't bring up any
845 % obvious candidate for breaking with this catcode change).
846 % \begin{macrocode}
847 {\edef\reserved@a{\unqu@tefilef@und#1\@nil}%
848 \@addtofilelist{\string@makeletter\reserved@a}%
849 \UseHook{file/before}%
850 % \end{macrocode}
851 % The current file name is available in \cs{CurrentFile} so we use
852 % that in the specific hook.
853 % \begin{macrocode}
854 \UseHook{file/\CurrentFile/before}%
855 \@@input #1% <- trailing space comes from \@filef@und
856 }%
857 % \end{macrocode}
858 % And here, \cs{CurrentFile} is restored
859 % (by \cs{@expl@@@filehook@file@pop@assign@@nnnn}) so we can use it once more.
860 % \begin{macrocode}
861 \UseHook{file/\CurrentFile/after}%
862 \UseHook{file/after}}
863 \def\unqu@tefilef@und"#1" \@nil{#1}
864 % \end{macrocode}
865 %
866 % \changes{v1.0l}{2021/08/25}{Declare non-generic file hooks}
867 % Now declare the non-generic file hooks used above:
868 % \begin{macrocode}
869 \NewHook{file/before}
870 \NewReversedHook{file/after}
871 %<latexrelease>\EndIncludeInRelease
872 %</2ekernel|latexrelease>
873 % \end{macrocode}
874 %
875 % \changes{v0.9b}
876 % {1993/12/04}{Macro added}
877 % \changes{v0.9p}
878 % {1994/01/18}{New Definition}
879 % \changes{v0.3b}{1994/03/13}
880 % {Use new cmd \cs{@addtofilelist}}
881 % Now define |\InputIfFileExists| to input |#1| if it seems to exist.
882 % Immediately prior to the input, |#2| is executed.
883 % If the file |#1| does not exist, execute `|#3|'.
884 % \changes{v1.0t}{1995/05/25}
885 % {(CAR) added \cs{long}}
886 % \changes{v1.1o}{2019/02/07}{Expand \cs{@filef@und} before executing
887 % second argument (github/109)}
888 % \changes{v1.2b}{2019/08/27}{Make command robust}
889 % \begin{macrocode}
890 %<latexrelease>\IncludeInRelease{2019/10/01}%
891 %<latexrelease> {\InputIfFileExists}{Hook management (files)}%
892 %<latexrelease>
893 %<latexrelease>\DeclareRobustCommand \InputIfFileExists[2]{%
894 %<latexrelease> \IfFileExists{#1}%
895 %<latexrelease> {%
896 %<latexrelease> \expandafter\@swaptwoargs\expandafter
897 %<latexrelease> {\@filef@und}{#2\@addtofilelist{#1}\@@input}}}
898 %<latexrelease>\let\@input@file@exists@with@hooks\@undefined
899 %<latexrelease>\let\unqu@tefilef@und\@undefined
900 %<latexrelease>\EndIncludeInRelease
901 % \end{macrocode}
902 %
903 % \begin{macrocode}
904 %<latexrelease>\IncludeInRelease{0000/00/00}%
905 %<latexrelease> {\InputIfFileExists}{Hook management (files)}%
906 %<latexrelease>\long\def \InputIfFileExists#1#2{%
907 %<latexrelease> \IfFileExists{#1}%
908 %<latexrelease> {#2\@addtofilelist{#1}\@@input \@filef@und}}
909 % \end{macrocode}
910 %
911 % Also undo the internal command as some packages unfortunately test
912 % for their existence instead of using \cs{IfFormatAtLeastTF}.
913 % \changes{v1.0g}{2021/02/08}{Undo the internal for robust
914 % \cs{InputIfFileExists} in rollback (gh/494)}
915 % \begin{macrocode}
916 %<latexrelease>\expandafter\let\csname InputIfFileExists \endcsname\@undefined
917 % \end{macrocode}
918 %
919 % \begin{macrocode}
920 %<latexrelease>\let\@input@file@exists@with@hooks\@undefined
921 %<latexrelease>\let\unqu@tefilef@und\@undefined
922 %<latexrelease>\EndIncludeInRelease
923 %<*2ekernel>
924 % \end{macrocode}
925 % \end{macro}
926 % \end{macro}
927 % \end{macro}
928 %
929 %
930 %
931 %
932 %
933 %
934 % \subsection{Declaring a file substitution}
935 %
936 % \begin{macrocode}
937 %<@@=filehook>
938 % \end{macrocode}
939 %
940 % \begin{macrocode}
941 %</2ekernel>
942 %<*2ekernel|latexrelease>
943 %<latexrelease>\IncludeInRelease{2020/10/01}%
944 %<latexrelease> {\@@_subst_add:nn}{Declaring file substitution}%
945 \ExplSyntaxOn
946 % \end{macrocode}
947 %
948 %
949 % \begin{macro}{\@@_subst_add:nn,\@@_subst_remove:n,
950 % \@@_subst_file_normalize:Nn,\@@_subst_empty_name_chk:NN}
951 % \cs{@@_subst_add:nn} declares a file substitution by
952 % doing a (global) definition of the form
953 % |\def|\cs{@file-subst@\meta{file}}|{|\meta{replacement}|}|.
954 % The file names are properly sanitised, and normalized with the same
955 % treatment done for the file hooks. That is, a file replacement is
956 % declared by using the file name (and extension, if any) only, and
957 % the file path should not be given. If a file name is empty it is
958 % replaced by |.tex| (the empty csname is used to check that).
959 % \begin{macrocode}
960 \cs_new_protected:Npn \@@_subst_add:nn #1 #2
961 {
962 \group_begin:
963 \cs_set:cpx { } { \exp_not:o { \cs:w\cs_end: } }
964 \int_set:Nn \tex_escapechar:D { -1 }
965 \cs_gset:cpx
966 {
967 @file-subst@
968 \@@_subst_file_normalize:Nn \use_ii_iii:nnn {#1}
969 }
970 { \@@_subst_file_normalize:Nn \@@_file_name_compose:nnn
971 {#2} }
972 \group_end:
973 }
974 \cs_new_protected:Npn \@@_subst_remove:n #1
975 {
976 \group_begin:
977 \cs_set:cpx { } { \exp_not:o { \cs:w\cs_end: } }
978 \int_set:Nn \tex_escapechar:D { -1 }
979 \cs_undefine:c
980 {
981 @file-subst@
982 \@@_subst_file_normalize:Nn \use_ii_iii:nnn {#1}
983 }
984 \group_end:
985 }
986 \cs_new:Npn \@@_subst_file_normalize:Nn #1 #2
987 {
988 \exp_after:wN \@@_subst_empty_name_chk:NN
989 \cs:w \exp_after:wN \cs_end:
990 \cs:w \@@_file_parse_full_name:nN {#2} #1 \cs_end:
991 }
992 \cs_new:Npn \@@_subst_empty_name_chk:NN #1 #2
993 { \if_meaning:w #1 #2 .tex \else: \token_to_str:N #2 \fi: }
994 % \end{macrocode}
995 % \end{macro}
996 %
997 % \begin{macro}[int]{\use_ii_iii:nnn}
998 % A variant of \cs[no-index]{use_...} to discard the first of three
999 % arguments.
1000 % \fmi{this should move to \pkg{expl3}}
1001 % \begin{macrocode}
1002 \cs_gset:Npn \use_ii_iii:nnn #1 #2 #3 {#2 #3}
1003 % \end{macrocode}
1004 % \end{macro}
1005 %
1006 %
1007 % \begin{macrocode}
1008 \ExplSyntaxOff
1009 %</2ekernel|latexrelease>
1010 %<latexrelease>\EndIncludeInRelease
1011 %<*2ekernel>
1012 % \end{macrocode}
1013 %
1014 %
1015 %
1016 % \begin{macro}{\declare@file@substitution}
1017 % \begin{macro}{\undeclare@file@substitution}
1018 % For two internals we provide \LaTeXe{} names so that we can use
1019 % them elsewhere in the kernel (and so that they can be used in
1020 % packages if really needed, e.g., \pkg{scrlfile}).
1021 % \begin{macrocode}
1022 %</2ekernel>
1023 %<*2ekernel|latexrelease>
1024 %<latexrelease>\IncludeInRelease{2020/10/01}%
1025 %<latexrelease> {\declare@file@substitution}{File substitution}%
1026 \ExplSyntaxOn
1027 \cs_new_eq:NN \declare@file@substitution \@@_subst_add:nn
1028 \cs_new_eq:NN \undeclare@file@substitution \@@_subst_remove:n
1029 \ExplSyntaxOff
1030 %</2ekernel|latexrelease>
1031 %<latexrelease>\EndIncludeInRelease
1032 % \end{macrocode}
1033 %
1034 % We are not fully rolling back the file substitutions in case a
1035 % rollback encounters a package that contains them, but is itself
1036 % not setup for rollback. So we just bypass them and hope for the
1037 % best.
1038 % \changes{v1.0d}{2020/12/04}{Don't drop file substitution commands on
1039 % rollback}
1040 % \begin{macrocode}
1041 %<latexrelease>\IncludeInRelease{0000/00/00}%
1042 %<latexrelease> {\declare@file@substitution}{File substitution}%
1043 %<latexrelease>
1044 %<latexrelease>\let \declare@file@substitution \@gobbletwo
1045 %<latexrelease>\let \undeclare@file@substitution \@gobble
1046 %<latexrelease>
1047 %<latexrelease>\EndIncludeInRelease
1048 %<*2ekernel>
1049 % \end{macrocode}
1050 % \end{macro}
1051 % \end{macro}
1052 %
1053 %
1054 %
1055 %
1056 % \begin{macrocode}
1057 %<@@=>
1058 % \end{macrocode}
1059 %
1060 % \subsection{Selecting a file (\cs{set@curr@file})}
1061 %
1062 % \begin{macro}{\set@curr@file,\set@curr@file@nosearch}
1063 % \begin{macro}{\@curr@file,\@curr@file@reqd}
1064 % \changes{v1.0f}{2021/01/31}{set \cs{protect} to \cs{string} gh/481}
1065 % Now we hook into \cs{set@curr@file} to resolve a possible file
1066 % substitution, and add \cs{@expl@@@filehook@set@curr@file@@nNN}
1067 % at the end, after \cs{@curr@file} is set.
1068 %
1069 % A file name is built using
1070 % \cs{expandafter}\cs{string}\cs{csname}\meta{filename}\cs{endcsname}
1071 % to avoid expanding utf8 active characters. The \cs{csname} expands
1072 % the normalization machinery and the routine to resolve a file
1073 % substitution, returning a control sequence with the same name as the
1074 % file.
1075 %
1076 % It happens that when \meta{filename} is empty, the generated control
1077 % sequence is \cs{csname\cs{endcsname}}, and doing \cs{string} on
1078 % that results in the file |csnameendcsname.tex|. To guard against
1079 % that we \cs{ifx}-compare the generated control sequence with the
1080 % empty csname. To do so, \cs{csname\cs{endcsname}} has to be
1081 % defined, otherwise it would be equal to \cs{relax} and we would have
1082 % false positives. Here we define \cs{csname\cs{endcsname}} to
1083 % expand to itself to avoid it matching the definition of some other
1084 % control sequence.
1085 % \changes{v1.0i}{2021/04/20}
1086 % {Make \string~ expand to a string (tracks change in l3kernel)}
1087 % \changes{v1.0m}{2022/03/10}
1088 % {Add \cs{set@curr@file@nosearch} for \pkg{graphicx}}
1089 % \begin{macrocode}
1090 %</2ekernel>
1091 %<*2ekernel|latexrelease>
1092 %<latexrelease>\IncludeInRelease{2022/06/01}%
1093 %<latexrelease> {\set@curr@file}{Setting current file name}%
1094 \def\set@curr@file{%
1095 \begingroup
1096 \set@curr@file@aux}
1097 \edef\set@curr@file@nosearch{%
1098 \begingroup
1099 \let\noexpand\input@path\noexpand\@empty
1100 \csname seq_clear:N\endcsname
1101 \expandafter\noexpand\csname l_file_search_path_seq\endcsname
1102 \noexpand\set@curr@file@aux}
1103 \def\set@curr@file@aux#1{%
1104 \escapechar\m@ne
1105 \let\protect\string
1106 \edef~{\string~}%
1107 \expandafter\def\csname\expandafter\endcsname
1108 \expandafter{\csname\endcsname}%
1109 % \end{macrocode}
1110 % Two file names are set here: \cs{@curr@file@reqd} which is the file
1111 % requested by the user, and \cs{@curr@file} which should be the same,
1112 % except when we have a file substitution, in which case it holds the
1113 % actual loaded file. \cs{@curr@file} is resolved first, to check if
1114 % a substitution happens. If it doesn't,
1115 % \cs{@expl@@@filehook@if@file@replaced@@TF} short-cuts and just copies
1116 % \cs{@curr@file}, otherwise the full normalization procedure is
1117 % executed.
1118 %
1119 % At this stage the file name is parsed and normalized, but if the
1120 % input doesn't have an extension, the default |.tex| is \emph{not}
1121 % added to \cs{@curr@file} because for applications other than
1122 % \cs{input} (graphics, for example) the default extension may not
1123 % be |.tex|. First check if the input has an extension, then if the
1124 % input had no extension, call \cs{@expl@@@filehook@drop@extension@@N}. In case
1125 % of a file substitution, \cs{@curr@file} will have an extension.
1126 % \begin{macrocode}
1127 \@expl@@@filehook@if@no@extension@@nTF{#1}%
1128 {\@tempswatrue}{\@tempswafalse}%
1129 \@kernel@make@file@csname\@curr@file
1130 \@expl@@@filehook@resolve@file@subst@@w {#1}%
1131 \@expl@@@filehook@if@file@replaced@@TF
1132 {\@kernel@make@file@csname\@curr@file@reqd
1133 \@expl@@@filehook@normalize@file@name@@w{#1}%
1134 \if@tempswa \@expl@@@filehook@drop@extension@@N\@curr@file@reqd \fi}%
1135 {\if@tempswa \@expl@@@filehook@drop@extension@@N\@curr@file \fi
1136 \global\let\@curr@file@reqd\@curr@file}%
1137 \@expl@@@filehook@clear@replacement@flag@@
1138 \endgroup}
1139 %</2ekernel|latexrelease>
1140 %<latexrelease>\EndIncludeInRelease
1141 % \end{macrocode}
1142 %
1143 % \begin{macrocode}
1144 %<latexrelease>\IncludeInRelease{2021/06/01}%
1145 %<latexrelease> {\set@curr@file}{Setting current file name}%
1146 %<latexrelease>\def\set@curr@file#1{%
1147 %<latexrelease> \begingroup
1148 %<latexrelease> \escapechar\m@ne
1149 %<latexrelease> \let\protect\string
1150 %<latexrelease> \edef~{\string~}%
1151 %<latexrelease> \expandafter\def\csname\expandafter\endcsname
1152 %<latexrelease> \expandafter{\csname\endcsname}%
1153 %<latexrelease> \@expl@@@filehook@if@no@extension@@nTF{#1}%
1154 %<latexrelease> {\@tempswatrue}{\@tempswafalse}%
1155 %<latexrelease> \@kernel@make@file@csname\@curr@file
1156 %<latexrelease> \@expl@@@filehook@resolve@file@subst@@w {#1}%
1157 %<latexrelease> \@expl@@@filehook@if@file@replaced@@TF
1158 %<latexrelease> {\@kernel@make@file@csname\@curr@file@reqd
1159 %<latexrelease> \@expl@@@filehook@normalize@file@name@@w{#1}%
1160 %<latexrelease> \if@tempswa \@expl@@@filehook@drop@extension@@N\@curr@file@reqd \fi}%
1161 %<latexrelease> {\if@tempswa \@expl@@@filehook@drop@extension@@N\@curr@file \fi
1162 %<latexrelease> \global\let\@curr@file@reqd\@curr@file}%
1163 %<latexrelease> \@expl@@@filehook@clear@replacement@flag@@
1164 %<latexrelease> \endgroup}
1165 %<latexrelease>\let\set@curr@file@nosearch\@undefined
1166 %<latexrelease>\EndIncludeInRelease
1167 % \end{macrocode}
1168 %
1169 % \begin{macrocode}
1170 %<latexrelease>\IncludeInRelease{2020/10/01}%
1171 %<latexrelease> {\set@curr@file}{Setting current file name}%
1172 %<latexrelease>\def\set@curr@file#1{%
1173 %<latexrelease> \begingroup
1174 %<latexrelease> \escapechar\m@ne
1175 %<latexrelease> \expandafter\def\csname\expandafter\endcsname
1176 %<latexrelease> \expandafter{\csname\endcsname}%
1177 %<latexrelease> \@expl@@@filehook@if@no@extension@@nTF{#1}%
1178 %<latexrelease> {\@tempswatrue}{\@tempswafalse}%
1179 %<latexrelease> \@kernel@make@file@csname\@curr@file
1180 %<latexrelease> \@expl@@@filehook@resolve@file@subst@@w {#1}%
1181 %<latexrelease> \@expl@@@filehook@if@file@replaced@@TF
1182 %<latexrelease> {\@kernel@make@file@csname\@curr@file@reqd
1183 %<latexrelease> \@expl@@@filehook@normalize@file@name@@w{#1}%
1184 %<latexrelease> \if@tempswa \@expl@@@filehook@drop@extension@@N\@curr@file@reqd \fi}%
1185 %<latexrelease> {\if@tempswa \@expl@@@filehook@drop@extension@@N\@curr@file \fi
1186 %<latexrelease> \global\let\@curr@file@reqd\@curr@file}%
1187 %<latexrelease> \@expl@@@filehook@clear@replacement@flag@@
1188 %<latexrelease> \endgroup}
1189 %<latexrelease>\let\set@curr@file@nosearch\@undefined
1190 %<latexrelease>\EndIncludeInRelease
1191 % \end{macrocode}
1192 %
1193 % \begin{macrocode}
1194 %<latexrelease>\IncludeInRelease{2019/10/01}%
1195 %<latexrelease> {\set@curr@file}{Setting current file name}%
1196 %<latexrelease>\def\set@curr@file#1{%
1197 %<latexrelease> \begingroup
1198 %<latexrelease> \escapechar\m@ne
1199 %<latexrelease> \xdef\@curr@file{%
1200 %<latexrelease> \expandafter\expandafter\expandafter\unquote@name
1201 %<latexrelease> \expandafter\expandafter\expandafter{%
1202 %<latexrelease> \expandafter\string
1203 %<latexrelease> \csname\@firstofone#1\@empty\endcsname}}%
1204 %<latexrelease> \endgroup
1205 %<latexrelease>}
1206 %<latexrelease>\let\set@curr@file@nosearch\@undefined
1207 %<latexrelease>\EndIncludeInRelease
1208 % \end{macrocode}
1209 %
1210 % \begin{macrocode}
1211 %<latexrelease>\IncludeInRelease{0000/00/00}%
1212 %<latexrelease> {\set@curr@file}{Setting current file name}%
1213 %<latexrelease>\let\set@curr@file\@undefined
1214 %<latexrelease>\let\set@curr@file@nosearch\@undefined
1215 %<latexrelease>\EndIncludeInRelease
1216 %<*2ekernel>
1217 % \end{macrocode}
1218 % \end{macro}
1219 % \end{macro}
1220 %
1221 %
1222 %
1223 %
1224 % \begin{macro}{\@filehook@set@CurrentFile}
1225 % \begin{macro}{\@kernel@make@file@csname,\@set@curr@file@aux}
1226 %
1227 % \fmi{This should get internalized using \texttt{@expl@} names}
1228 % \begin{macrocode}
1229 %</2ekernel>
1230 %<*2ekernel|latexrelease>
1231 %<latexrelease>\IncludeInRelease{2020/10/01}%
1232 %<latexrelease> {\@kernel@make@file@csname}{Make file csname}%
1233 % \end{macrocode}
1234 %
1235 % \begin{macrocode}
1236 \def\@kernel@make@file@csname#1#2#3{%
1237 \xdef#1{\expandafter\@set@curr@file@aux
1238 \csname\expandafter#2\@firstofone#3\@nil\endcsname}}
1239 % \end{macrocode}
1240 % This auxiliary compares \cs{\meta{filename}} with
1241 % \cs{csname\cs{endcsname}} to check if the empty |.tex| file was
1242 % requested.
1243 % \changes{v1.0o}{2023/04/02}
1244 % {Make \cs{@set@curr@file@aux} \cs{long} gh/942}
1245 % \begin{macrocode}
1246 \long\def\@set@curr@file@aux#1{%
1247 \expandafter\ifx\csname\endcsname#1%
1248 .tex\else\string#1\fi}
1249 % \end{macrocode}
1250 %
1251 % \begin{sloppypar}
1252 % Then we call \cs{@expl@@@filehook@set@curr@file@@nNN} once for
1253 % \cs{@curr@file} to set \cs[no-index]{CurrentFile(Path)Used} and once for
1254 % \cs{@curr@file@reqd} to set \cs[no-index]{CurrentFile(Path)}.
1255 % Here too the slower route is only used if a substitution happened,
1256 % but here \cs{@expl@@@filehook@if@file@replaced@@TF} can't be used because
1257 % the flag is reset at the \cs{endgroup} above, so we check if
1258 % \cs{@curr@file} and \cs{@curr@file@reqd} differ. This macro is
1259 % issued separate from \cs{set@curr@file} because it changes
1260 % \cs{CurrentFile}, and side-effects would quickly get out of control.
1261 % \end{sloppypar}
1262 % \begin{macrocode}
1263 \def\@filehook@set@CurrentFile{%
1264 \@expl@@@filehook@set@curr@file@@nNN{\@curr@file}%
1265 \CurrentFileUsed\CurrentFilePathUsed
1266 \ifx\@curr@file@reqd\@curr@file
1267 \let\CurrentFile\CurrentFileUsed
1268 \let\CurrentFilePath\CurrentFilePathUsed
1269 \else
1270 \@expl@@@filehook@set@curr@file@@nNN{\@curr@file@reqd}%
1271 \CurrentFile\CurrentFilePath
1272 \fi}
1273 %</2ekernel|latexrelease>
1274 %<latexrelease>\EndIncludeInRelease
1275 %<*2ekernel>
1276 % \end{macrocode}
1277 % \end{macro}
1278 % \end{macro}
1279 %
1280 %
1281 % \begin{macrocode}
1282 %<@@=filehook>
1283 % \end{macrocode}
1284 %
1285 %
1286 % \begin{macro}{\@@_set_curr_file:nNN,
1287 % \@@_set_curr_file_assign:nnnNN}
1288 % When inputting a file, \cs{set@curr@file} does a file lookup
1289 % (in \cs{input@path} and \cs{l_file_search_path_seq}) and returns the
1290 % actual file name (\meta{base} plus \meta{ext}) in
1291 % \cs{CurrentFileUsed}, and in case there's a file substitution, the
1292 % requested file in \cs{CurrentFile} (otherwise both are the same).
1293 % Only the base and extension are returned,
1294 % regardless of the input (both \texttt{path/to/file.tex} and
1295 % \texttt{file.tex} end up as \texttt{file.tex} in \cs{CurrentFile}).
1296 % The path is returned in \cs{CurrentFilePath}, in case it's needed.
1297 % \begin{macrocode}
1298 %</2ekernel>
1299 %<*2ekernel|latexrelease>
1300 %<latexrelease>\IncludeInRelease{2020/10/01}%
1301 %<latexrelease> {\@@_set_curr_file:nNN}{Set curr file}%
1302 \ExplSyntaxOn
1303 \cs_new_protected:Npn \@@_set_curr_file:nNN #1
1304 {
1305 \exp_args:Nf \@@_file_parse_full_name:nN {#1}
1306 \@@_set_curr_file_assign:nnnNN
1307 }
1308 \cs_new_protected:Npn \@@_set_curr_file_assign:nnnNN #1 #2 #3 #4 #5
1309 {
1310 \str_set:Nn #5 {#1}
1311 \str_set:Nn #4 {#2#3}
1312 }
1313 \ExplSyntaxOff
1314 %</2ekernel|latexrelease>
1315 %<latexrelease>\EndIncludeInRelease
1316 %<*2ekernel>
1317 % \end{macrocode}
1318 % \end{macro}
1319 %
1320 %
1321 %
1322 %
1323 % \subsection{Replacing a file and detecting loops}
1324 %
1325 % \begin{macro}{\@@_resolve_file_subst:w}
1326 % \begin{macro}{\@@_normalize_file_name:w}
1327 % \begin{macro}{\@@_file_name_compose:nnn}
1328 % Start by sanitizing the file with \cs{@@_file_parse_full_name:nN}
1329 % then do \cs{@@_file_subst_begin:nnn}\Arg{path}\Arg{name}\Arg{ext}.
1330 % \begin{macrocode}
1331 %</2ekernel>
1332 %<*2ekernel|latexrelease>
1333 %<latexrelease>\IncludeInRelease{2020/10/01}%
1334 %<latexrelease> {\@@_resolve_file_subst:w}{Replace files detect loops}%
1335 \ExplSyntaxOn
1336 \cs_new:Npn \@@_resolve_file_subst:w #1 \@nil
1337 { \@@_file_parse_full_name:nN {#1} \@@_file_subst_begin:nnn }
1338 \cs_new:Npn \@@_normalize_file_name:w #1 \@nil
1339 { \@@_file_parse_full_name:nN {#1} \@@_file_name_compose:nnn }
1340 \cs_new:Npn \@@_file_name_compose:nnn #1 #2 #3
1341 { \tl_if_empty:nF {#1} { #1 / } #2#3 }
1342 % \end{macrocode}
1343 % \end{macro}
1344 % \end{macro}
1345 %
1346 % \begin{macro}{@@_file_replaced}
1347 % \begin{macro}{\@@_if_file_replaced:TF}
1348 % \begin{macro}{\@@_clear_replacement_flag:}
1349 % Since the file replacement is done expandably in a \cs{csname}, use
1350 % a flag to remember if a substitution happened. We use this in
1351 % \cs{set@curr@file} to short-circuit some of it in case no
1352 % substitution happened (by far the most common case, so it's worth
1353 % optimizing). The flag raised during the file substitution algorithm
1354 % must be explicitly cleared after the \cs{@@_if_file_replaced:TF}
1355 % conditional is no longer needed, otherwise further uses of
1356 % \cs{@@_if_file_replaced:TF} will wrongly return true.
1357 % \begin{macrocode}
1358 \flag_new:n { @@_file_replaced }
1359 \cs_new:Npn \@@_if_file_replaced:TF #1 #2
1360 { \flag_if_raised:nTF { @@_file_replaced } {#1} {#2} }
1361 \cs_new_protected:Npn \@@_clear_replacement_flag:
1362 { \flag_clear:n { @@_file_replaced } }
1363 % \end{macrocode}
1364 % \end{macro}
1365 % \end{macro}
1366 % \end{macro}
1367 %
1368 % \begin{macro}{\@@_file_subst_begin:nnn}
1369 % First off, start by checking if the current file ($\meta{name} +
1370 % \meta{ext}$) has a declared substitution. If not, then just put
1371 % that as the name (including a possible \meta{path} in this case):
1372 % this is the default case with no substitutions, so it's the first to
1373 % be checked. The auxiliary \cs{@@_file_subst_tortoise_hare:nn} sees
1374 % that there's no replacement for |#2#3| and does nothing else.
1375 % \begin{macrocode}
1376 \cs_new:Npn \@@_file_subst_begin:nnn #1 #2 #3
1377 {
1378 \@@_file_subst_tortoise_hare:nn { #2#3 } { #2#3 }
1379 { \@@_file_name_compose:nnn {#1} {#2} {#3} }
1380 }
1381 \ExplSyntaxOff
1382 %</2ekernel|latexrelease>
1383 %<latexrelease>\EndIncludeInRelease
1384 %<*2ekernel>
1385 % \end{macrocode}
1386 % \end{macro}
1387 %
1388 %
1389 %
1390 %
1391 % \subsubsection{The Tortoise and Hare algorithm}
1392 %
1393 % \begin{macro}{\@@_file_subst_tortoise_hare:nn}
1394 % \begin{macro}{\@@_file_subst_loop:NN,\@@_file_subst_loop:cc}
1395 % If there is a substitution (\meta{true} in the first
1396 % \cs{cs_if_exist:cTF} below), then first check if there is no
1397 % substitution down the line: this should be the second most common
1398 % case, of one file replaced by another. In that case just leave the
1399 % substitution there and the job is done. If any substitution
1400 % happens, then the \cs{flag @@_file_replaced} is raised
1401 % (conditionally, because checking if a flag is raised is much faster
1402 % than raising it over and over again).
1403 %
1404 % If, however there are more substitutions, then we need to check for
1405 % a possible loop in the substitutions, which would otherwise put
1406 % \TeX{} in an infinite loop if just an exhaustive expansion was used.
1407 %
1408 % To detect a loop, the \emph{Tortoise and Hare} algorithm is used.
1409 % The name of the algorithm is an analogy to Aesop's fable, in which
1410 % the Hare outruns a Tortoise. The two pointers here are the csnames
1411 % which contains each file replacement, both of which start at the
1412 % position zero, which is the file requested. In the inner part of
1413 % the macro below, \cs{@@_file_subst_loop:cc} is called with
1414 % \cs[no-index]{@file-subst@\meta{file}} and
1415 % \cs[no-index]{@file-subst@\cs[no-index]{@file-subst@\meta{file}}};
1416 % that is, the substitution of \meta{file} and the substitution of that
1417 % substitution: the Tortoise walks one step while the Hare walks two.
1418 %
1419 % Within \cs{@@_file_subst_loop:NN} the two substitutions are
1420 % compared, and if they lead to the same file it means that there is
1421 % a loop in the substitutions. If there's no loop,
1422 % \cs{@@_file_subst_tortoise_hare:nn} is called again with the
1423 % Tortoise at position~1 and the hare at~2. Again, the substitutions
1424 % are checked ahead of the Hare pointer to check that it won't run too
1425 % far; in case there is no loop in the declarations, eventually one
1426 % of the \cs{cs_if_exist:cTF} below will go \meta{false} and the
1427 % algorithm will end; otherwise it will run until the Hare reaches
1428 % the same spot as the tortoise and a loop is detected.
1429 % \begin{macrocode}
1430 %</2ekernel>
1431 %<*2ekernel|latexrelease>
1432 %<latexrelease>\IncludeInRelease{2020/10/01}%
1433 %<latexrelease> {\@@_file_subst_tortoise_hare:nn}{Tortoise and Hare}%
1434 \ExplSyntaxOn
1435 \cs_new:Npn \@@_file_subst_tortoise_hare:nn #1 #2 #3
1436 {
1437 \cs_if_exist:cTF { @file-subst@ #2 }
1438 {
1439 \flag_if_raised:nF { @@_file_replaced }
1440 { \flag_raise:n { @@_file_replaced } }
1441 \cs_if_exist:cTF { @file-subst@ \use:c { @file-subst@ #2 } }
1442 {
1443 \@@_file_subst_loop:cc
1444 { @file-subst@ #1 }
1445 { @file-subst@ \use:c { @file-subst@ #2 } }
1446 }
1447 { \use:c { @file-subst@ #2 } }
1448 }
1449 { #3 }
1450 }
1451 % \end{macrocode}
1452 % This is just an auxiliary to check if a loop was found, and continue
1453 % the algorithm otherwise. If a loop is found, the |.tex| file is
1454 % used as fallback and \cs{@@_file_subst_cycle_error:cN} is called to
1455 % report the error.
1456 % \begin{macrocode}
1457 \cs_new:Npn \@@_file_subst_loop:NN #1 #2
1458 {
1459 \token_if_eq_meaning:NNTF #1 #2
1460 {
1461 .tex
1462 \@@_file_subst_cycle_error:cN { @file-subst@ #1 } #1
1463 }
1464 { \@@_file_subst_tortoise_hare:nn {#1} {#2} {#2} }
1465 }
1466 \cs_generate_variant:Nn \@@_file_subst_loop:NN { cc }
1467 % \end{macrocode}
1468 % \end{macro}
1469 % \end{macro}
1470 %
1471 % \begin{macro}{
1472 % \@@_file_subst_cycle_error:NN,
1473 % \@@_file_subst_cycle_error:cN,
1474 % }
1475 % \changes{v1.0l}{2021/08/27}{Use \cs{msg_...} not \cs{__kernel_msg_...}}
1476 % Showing this type of error expandably is tricky, as we have a very
1477 % limited amount of characters to show and a potentially large list.
1478 % As a work around, several errors are printed, each showing one step
1479 % of the loop, until all the error messages combined show the loop.
1480 % \begin{macrocode}
1481 \cs_new:Npn \@@_file_subst_cycle_error:NN #1 #2
1482 {
1483 \msg_expandable_error:nnff { latex2e } { file-cycle }
1484 {#1} { \use:c { @file-subst@ #1 } }
1485 \token_if_eq_meaning:NNF #1 #2
1486 { \@@_file_subst_cycle_error:cN { @file-subst@ #1 } #2 }
1487 }
1488 \cs_generate_variant:Nn \@@_file_subst_cycle_error:NN { c }
1489 % \end{macrocode}
1490 %
1491 % And the error message:
1492 % \begin{macrocode}
1493 \msg_new:nnn { latex2e } { file-cycle }
1494 { File~loop!~#1~replaced~by~#2... }
1495 % \end{macrocode}
1496 % \end{macro}
1497 % \end{macro}
1498 %
1499 % \begin{macrocode}
1500 \ExplSyntaxOff
1501 %</2ekernel|latexrelease>
1502 %<latexrelease>\EndIncludeInRelease
1503 %<*2ekernel>
1504 % \end{macrocode}
1505 %
1506 %
1507 % \begin{macrocode}
1508 %<@@=>
1509 % \end{macrocode}
1510 %
1511 %
1512 % \subsection{Preventing a package from loading}
1513 %
1514 % We support the use case of preventing a package from loading but not
1515 % any other type of files (e.g., classes).
1516 %
1517 % \begin{macro}{\disable@package@load}
1518 % \begin{macro}{\reenable@package@load}
1519 % \begin{macro}{\@disable@packageload@do}
1520 % \cs{disable@package@load} defines
1521 % \cs[no-index]{@pkg-disable@\meta{package}} to expand to some code |#2|
1522 % instead of loading the package.
1523 % \begin{macrocode}
1524 %</2ekernel>
1525 %<*2ekernel|latexrelease>
1526 %<latexrelease>\IncludeInRelease{2020/10/01}%
1527 %<latexrelease> {\disable@package@load}{Disable packages}%
1528 \def\disable@package@load#1#2{%
1529 \global\@namedef{@pkg-disable@#1.\@pkgextension}{#2}}
1530 % \end{macrocode}
1531 %
1532 % \changes{v1.0n}{2022/08/18}{Inhibit checking the loaded version when
1533 % package is load-disabled, and write to the .log (gh/888)}
1534 % Here we check if a control sequence named
1535 % \cs[no-index]{@pkg-disable@\meta{name}.sty} is defined, and if so
1536 % don't use the package loading code |#2|, but use the replacement
1537 % code stored in that control sequence, write something to the log,
1538 % and then prevent \cs{@onefilewithoptions} from sanity-checking the
1539 % requested package date (the \tn{expandafter} here triggers one in
1540 % \cs{@onefilewithoptions} that ends a conditional there, and the
1541 % \tn{@gobbletwo} removes the date checking code from the input
1542 % stream).
1543 % \begin{macrocode}
1544 \def\@disable@packageload@do#1#2{%
1545 \@ifundefined{@pkg-disable@#1}%
1546 {#2}%
1547 {\@nameuse{@pkg-disable@#1}%
1548 \@latex@info{Package '#1' has been disabled.%
1549 \MessageBreak Load request ignored}%
1550 \expandafter\@gobbletwo}}
1551 % \end{macrocode}
1552 %
1553 % \cs{reenable@package@load} undefines
1554 % \cs[no-index]{@pkg-disable@\meta{package}} to reallow loading a package.
1555 % \begin{macrocode}
1556 \def\reenable@package@load#1{%
1557 \global\expandafter\let
1558 \csname @pkg-disable@#1.\@pkgextension \endcsname \@undefined}
1559 % \end{macrocode}
1560 %
1561 %
1562 % \begin{macrocode}
1563 %</2ekernel|latexrelease>
1564 %<latexrelease>\EndIncludeInRelease
1565 %<latexrelease>\IncludeInRelease{0000/00/00}%
1566 %<latexrelease> {\disable@package@load}{Disable packages}%
1567 %<latexrelease>
1568 %<latexrelease>\let\disable@package@load \@undefined
1569 %<latexrelease>\let\@disable@packageload@do\@undefined
1570 %<latexrelease>\let\reenable@package@load \@undefined
1571 %<latexrelease>\EndIncludeInRelease
1572 %<*2ekernel>
1573 % \end{macrocode}
1574 % \end{macro}
1575 % \end{macro}
1576 % \end{macro}
1577 %
1578 %
1579 %
1580 %
1581 %
1582 % \subsection{High-level interfaces for \LaTeX{}}
1583 %
1584 % None so far and the general feeling for now is that the hooks are
1585 % enough. Packages like \pkg{filehook}, etc., may use them to set
1586 % up their interfaces (samples are given below) but for the now the
1587 % kernel will not provide any.
1588 %
1589 %
1590 %
1591 % \subsection{Internal commands needed elsewhere}
1592 %
1593 % Here we set up a few horrible (but consistent) \LaTeXe{} names to
1594 % allow for internal commands to be used outside this module (and
1595 % in parts that still use \LaTeXe{} syntax. We have to unset the
1596 % \texttt{@\/@} since we want double ``at'' sign in place of double
1597 % underscores.
1598 %
1599 % \begin{macrocode}
1600 %<@@=>
1601 % \end{macrocode}
1602 % \InternalDetectionOff
1603 % \begin{macrocode}
1604 %</2ekernel>
1605 %<*2ekernel|latexrelease>
1606 %<latexrelease>\IncludeInRelease{2020/10/01}%
1607 %<latexrelease> {\@expl@@@filehook@if@no@extension@@nTF}{2e tmp interfaces}%
1608 \ExplSyntaxOn
1609 % \end{macrocode}
1610 %
1611 % \begin{macrocode}
1612 \cs_new_eq:NN \@expl@@@filehook@if@no@extension@@nTF
1613 \__filehook_if_no_extension:nTF
1614 % \end{macrocode}
1615 %
1616 % \begin{macrocode}
1617 \cs_new_eq:NN \@expl@@@filehook@set@curr@file@@nNN
1618 \__filehook_set_curr_file:nNN
1619 % \end{macrocode}
1620 %
1621 % \begin{macrocode}
1622 \cs_new_eq:NN \@expl@@@filehook@resolve@file@subst@@w
1623 \__filehook_resolve_file_subst:w
1624 % \end{macrocode}
1625 %
1626 % \begin{macrocode}
1627 \cs_new_eq:NN \@expl@@@filehook@normalize@file@name@@w
1628 \__filehook_normalize_file_name:w
1629 % \end{macrocode}
1630 %
1631 % \begin{macrocode}
1632 \cs_new_eq:NN \@expl@@@filehook@if@file@replaced@@TF
1633 \__filehook_if_file_replaced:TF
1634 % \end{macrocode}
1635 %
1636 % \begin{macrocode}
1637 \cs_new_eq:NN \@expl@@@filehook@clear@replacement@flag@@
1638 \__filehook_clear_replacement_flag:
1639 % \end{macrocode}
1640 %
1641 % \begin{macrocode}
1642 \cs_new_eq:NN \@expl@@@filehook@drop@extension@@N
1643 \__filehook_drop_extension:N
1644 % \end{macrocode}
1645 %
1646 % \begin{macrocode}
1647 \cs_new_eq:NN \@expl@@@filehook@file@push@@
1648 \__filehook_file_push:
1649 % \end{macrocode}
1650 %
1651 % \begin{macrocode}
1652 \cs_new_eq:NN \@expl@@@filehook@file@pop@@
1653 \__filehook_file_pop:
1654 % \end{macrocode}
1655 %
1656 % \begin{macrocode}
1657 \cs_new_eq:NN \@expl@@@filehook@file@pop@assign@@nnnn
1658 \__filehook_file_pop_assign:nnnn
1659 % \end{macrocode}
1660 % \InternalDetectionOn
1661 %
1662 %
1663 % \begin{macrocode}
1664 \ExplSyntaxOff
1665 % \end{macrocode}
1666 %
1667 % This one specifically has to be undefined because it is left over in
1668 % the input stream from \cs{InputIfFileExists} and executed when
1669 % \pkg{latexrelease} is loaded. It cannot be \cs{let} to \cs{@undefined}
1670 % otherwise it would error as well, so it is \cs{let} to \cs{relax} to
1671 % be silently ignored when loading \cs{latexrelease}.
1672 % \changes{v1.0e}{2021/01/07}{Added rollback for this case to avoid
1673 % spurious errors (part of gh/463)}
1674 % \begin{macrocode}
1675 %</2ekernel|latexrelease>
1676 %<latexrelease>\EndIncludeInRelease
1677 %<latexrelease>
1678 %<latexrelease>\IncludeInRelease{0000/00/00}%
1679 %<latexrelease> {\@expl@@@filehook@if@no@extension@@nTF}{2e tmp interfaces}%
1680 %<latexrelease>\let\@expl@@@filehook@file@pop@@\relax
1681 %<latexrelease>\EndIncludeInRelease
1682 %<*2ekernel>
1683 % \end{macrocode}
1684 %
1685 % This ends the kernel code in this file.
1686 % \begin{macrocode}
1687 %</2ekernel>
1688 % \end{macrocode}
1689 %
1690 %
1691 %
1692 % \section{A sample package for structuring the log output}
1693 %
1694 % \begin{macrocode}
1695 %<*structuredlog>
1696 %<@@=filehook>
1697 % \end{macrocode}
1698 %
1699 % \begin{macrocode}
1700 \ProvidesExplPackage
1701 {structuredlog}{\ltfilehookdate}{\ltfilehookversion}
1702 {Structuring the TeX transcript file}
1703 % \end{macrocode}
1704 %
1705 % \begin{macro}{\g_@@_nesting_level_int}
1706 % Stores the current package nesting level.
1707 % \begin{macrocode}
1708 \int_new:N \g_@@_nesting_level_int
1709 % \end{macrocode}
1710 % Initialise the counter with the number of files in the
1711 % \cs{@currnamestack} (the number of items divided by $3$) minus one,
1712 % because this package is skipped when printing to the log.
1713 % \begin{macrocode}
1714 \int_gset:Nn \g_@@_nesting_level_int
1715 { ( \tl_count:N \@currnamestack ) / 3 - 1 }
1716 % \end{macrocode}
1717 % \end{macro}
1718 %
1719 % \begin{macro}{\@@_log_file_record:n}
1720 % This macro is responsible for increasing and decreasing the file
1721 % nesting level, as well as printing to the log. The argument is
1722 % either |STOPTART| or |STOP| and the action it takes on the nesting
1723 % integer depends on that.
1724 % \begin{macrocode}
1725 \cs_new_protected:Npn \@@_log_file_record:n #1
1726 {
1727 \str_if_eq:nnT {#1} {START} { \int_gincr:N \g_@@_nesting_level_int }
1728 \iow_term:x
1729 {
1730 \prg_replicate:nn { \g_@@_nesting_level_int } { = } ~
1731 ( LEVEL ~ \int_use:N \g_@@_nesting_level_int \c_space_tl #1 ) ~
1732 \CurrentFileUsed
1733 % \end{macrocode}
1734 % If there was a file replacement, show that as well:
1735 % \begin{macrocode}
1736 \str_if_eq:NNF \CurrentFileUsed \CurrentFile
1737 { ~ ( \CurrentFile \c_space_tl requested ) }
1738 \iow_newline:
1739 }
1740 \str_if_eq:nnT {#1} {STOP} { \int_gdecr:N \g_@@_nesting_level_int }
1741 }
1742 % \end{macrocode}
1743 %
1744 % Now just hook the macro above in the generic |file/before|\ldots
1745 % \begin{macrocode}
1746 \AddToHook{file/before}{ \@@_log_file_record:n { START } }
1747 % \end{macrocode}
1748 % \ldots and |file/after| hooks.
1749 % We don't want to install the \hook{file/after} hook immediately,
1750 % because that would mean it is the first time executed when the
1751 % package finishes. We therefore put the declaration inside
1752 % \cs{AddToHookNext} so that it gets only installed when we have
1753 % left this package.
1754 % \begin{macrocode}
1755 \AddToHookNext{file/after}
1756 { \AddToHook{file/after}{ \@@_log_file_record:n { STOP } } }
1757 % \end{macrocode}
1758 % \end{macro}
1759 %
1760 % \begin{macrocode}
1761 %<@@=>
1762 %</structuredlog>
1763 % \end{macrocode}
1764 %
1765 %
1766 %
1767 %
1768 %
1769 %
1770 % \section{Package emulations}
1771 %
1772 %
1773 % \subsection{Package \pkg{atveryend} emulation}
1774 %
1775 % With the new hook management and the hooks in \cs{enddocument}
1776 % all of \pkg{atveryend} is taken care of.
1777 % We can make an emulation only here after the substitution
1778 % functionality is available:
1779 % \begin{macrocode}
1780 %<*2ekernel>
1781 \declare@file@substitution{atveryend.sty}{atveryend-ltx.sty}
1782 %</2ekernel>
1783 % \end{macrocode}
1784 %
1785 % Here is the package file we point to:
1786 % \begin{macrocode}
1787 %<*atveryend-ltx>
1788 \ProvidesPackage{atveryend-ltx}
1789 [2020/08/19 v1.0a
1790 Emulation of the original atveryend package^^Jwith kernel methods]
1791 % \end{macrocode}
1792 %
1793 %
1794 % Here are new definitions for its interfaces now pointing to the
1795 % hooks in \cs{enddocument}
1796 % \begin{macrocode}
1797 \newcommand\AfterLastShipout {\AddToHook{enddocument/afterlastpage}}
1798 \newcommand\AtVeryEndDocument {\AddToHook{enddocument/afteraux}}
1799 % \end{macrocode}
1800 % Next one is a bit of a fake, but the result should normally be as
1801 % expected. If not, one needs to add a rule to sort the code chunks
1802 % in \hook{enddocument/info}.
1803 % \begin{macrocode}
1804 \newcommand\AtEndAfterFileList{\AddToHook{enddocument/info}}
1805 % \end{macrocode}
1806 %
1807 % \begin{macrocode}
1808 \newcommand\AtVeryVeryEnd {\AddToHook{enddocument/end}}
1809 % \end{macrocode}
1810 %
1811 % \begin{macro}{\BeforeClearDocument}
1812 % This one is the only one we don't implement or rather don't have
1813 % a dedicated hook in the code.
1814 % \begin{macrocode}
1815 \ExplSyntaxOn
1816 \newcommand\BeforeClearDocument[1]
1817 { \AtEndDocument{#1}
1818 \atveryend@DEPRECATED{BeforeClearDocument \tl_to_str:n{#1}}
1819 }
1820 % \end{macrocode}
1821 %
1822 % \begin{macrocode}
1823 \cs_new:Npn\atveryend@DEPRECATED #1
1824 {\iow_term:x{======~DEPRECATED~USAGE~#1~==========}}
1825 \ExplSyntaxOff
1826 % \end{macrocode}
1827 % \end{macro}
1828 %
1829 %
1830 % \begin{macrocode}
1831 %</atveryend-ltx>
1832 % \end{macrocode}
1833 %
1834 %
1835 %
1836 % \Finale
1837 %
1838 %
1839 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1840 \endinput
1841 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1842 %