Debian Codegraph @

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

raw
1 % \iffalse meta-comment
2 %
3 % Copyright (C) 2020-2026
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{2026-03-12}
38 \def\ltfilehookversion{v1.0p}
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{l3in2edoc}
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{\LaTeX{}'s hook management for files\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 % \changes{v1.0p}{2026-03-12}{Switch from \texttt{x}- to \texttt{e}-type
503 % expansion in expl3 code}
504 %
505 % \subsection{Document and package-level commands}
506 %
507 %
508 % \begin{macro}{\CurrentFile,\CurrentFilePath}
509 % \begin{macro}{\CurrentFileUsed,\CurrentFilePathUsed}
510 % User-level macros that hold the current file name and file path.
511 % These are used internally as well because the code takes care to
512 % protect against a possible redefinition of these macros in the
513 % loaded file (it's necessary anyway to make hooks work with nested
514 % \cs{input}). The versions |\...Used| hold the \emph{actual} file
515 % name and path that is loaded by \LaTeX, whereas the other two hold
516 % the name as requested. They will differ in case there's a file
517 % substitution.
518 % \begin{macrocode}
519 %</2ekernel>
520 %<*2ekernel|latexrelease>
521 %<latexrelease>\IncludeInRelease{2020/10/01}%
522 %<latexrelease> {\CurrentFile}{Hook management file}%
523 \ExplSyntaxOn
524 \tl_new:N \CurrentFile
525 \tl_new:N \CurrentFilePath
526 \tl_new:N \CurrentFileUsed
527 \tl_new:N \CurrentFilePathUsed
528 \ExplSyntaxOff
529 %</2ekernel|latexrelease>
530 %<latexrelease>\EndIncludeInRelease
531 % \end{macrocode}
532 %
533 % \begin{macrocode}
534 %<latexrelease>\IncludeInRelease{0000/00/00}%
535 %<latexrelease> {\CurrentFile}{Hook management file}%
536 %<latexrelease>
537 %<latexrelease>\let \CurrentFile \@undefined
538 %<latexrelease>\let \CurrentFilePath \@undefined
539 %<latexrelease>\let \CurrentFileUsed \@undefined
540 %<latexrelease>\let \CurrentFilePathUsed \@undefined
541 %<latexrelease>
542 %<latexrelease>\EndIncludeInRelease
543 %<*2ekernel>
544 % \end{macrocode}
545 % \end{macro}
546 % \end{macro}
547 %
548 %
549 %
550 % \subsection{\pkg{expl3} helpers}
551 %
552 % \begin{macrocode}
553 %</2ekernel>
554 %<*2ekernel|latexrelease>
555 %<latexrelease>\IncludeInRelease{2020/10/01}%
556 %<latexrelease> {\@@_file_parse_full_name:nN}{File helpers}%
557 \ExplSyntaxOn
558 % \end{macrocode}
559 %
560 % \begin{macro}{
561 % \@@_file_parse_full_name:nN,
562 % \@@_full_name:nn,
563 % }
564 % A utility macro to trigger \pkg{expl3}'s file-parsing and lookup,
565 % and return a normalized representation of the file name. If the
566 % queried file doesn't exist, no normalization takes place.
567 % The output of \cs{@@_file_parse_full_name:nN} is passed on to the
568 % |#2|---a 3-argument macro that takes the \meta{path}, \meta{base},
569 % and \meta{ext} parts of the file name.
570 %
571 % \begin{macrocode}
572 \cs_new:Npn \@@_file_parse_full_name:nN #1
573 {
574 \exp_args:Nf \file_parse_full_name_apply:nN
575 {
576 \exp_args:Nf \@@_full_name:nn
577 { \file_full_name:n {#1} } {#1}
578 }
579 }
580 \cs_new:Npn \@@_full_name:nn #1 #2
581 {
582 \tl_if_empty:nTF {#1}
583 { \tl_trim_spaces:n {#2} }
584 { \tl_trim_spaces:n {#1} }
585 }
586 % \end{macrocode}
587 % \end{macro}
588 %
589 % \begin{macro}{
590 % \@@_if_no_extension:nTF,
591 % \@@_drop_extension:N
592 % }
593 % Some actions depend on whether the file extension was explicitly
594 % given, and sometimes the extension has to be removed. The macros
595 % below use \cs{@@_file_parse_full_name:nN} to split up the file name
596 % and either check if \meta{ext} (|#3|) is empty, or discard it.
597 % \begin{macrocode}
598 \cs_new:Npn \@@_if_no_extension:nTF #1
599 {
600 \exp_args:Ne \tl_if_empty:nTF
601 { \file_parse_full_name_apply:nN {#1} \use_iii:nnn }
602 }
603 \cs_new_protected:Npn \@@_drop_extension:N #1
604 {
605 \tl_gset:Ne #1
606 {
607 \exp_args:NV \@@_file_parse_full_name:nN #1
608 \@@_drop_extension_aux:nnn
609 }
610 }
611 \cs_new:Npn \@@_drop_extension_aux:nnn #1 #2 #3
612 { \tl_if_empty:nF {#1} { #1 / } #2 }
613 % \end{macrocode}
614 % \end{macro}
615 %
616 % \begin{macro}{\g_@@_input_file_seq,\l_@@_internal_tl}
617 % \begin{macro}{\@@_file_push:,\@@_file_pop:}
618 % \begin{macro}{\@@_file_pop_assign:nnnn}
619 % Yet another stack, to keep track of \cs{CurrentFile} and
620 % \cs{CurrentFilePath} with nested \cs{input}s. At the beginning of
621 % \cs{InputIfFileExists}, the current value of \cs{CurrentFilePath}
622 % and \cs{CurrentFile} is pushed to \cs{g_@@_input_file_seq}, and
623 % at the end, it is popped and the value reassigned. Some other
624 % places don't use \cs{InputIfFileExists} directly (\cs{include}) or
625 % need \cs{CurrentFile} earlier (\cs{@onefilewithoptions}), so these
626 % are manually used elsewhere as well.
627 % \changes{v1.0h}{2021/03/18}
628 % {Define \cs{g__filehook_input_file_seq} to avoid losing data when
629 % rolling back.}
630 % \changes{v1.0l}{2021/08/27}{Internal message name changes}
631 % \begin{macrocode}
632 \tl_new:N \l_@@_internal_tl
633 \seq_if_exist:NF \g_@@_input_file_seq
634 { \seq_new:N \g_@@_input_file_seq }
635 \cs_new_protected:Npn \@@_file_push:
636 {
637 \seq_gpush:Ne \g_@@_input_file_seq
638 {
639 { \CurrentFilePathUsed } { \CurrentFileUsed }
640 { \CurrentFilePath } { \CurrentFile }
641 }
642 }
643 \cs_new_protected:Npn \@@_file_pop:
644 {
645 \seq_gpop:NNTF \g_@@_input_file_seq \l_@@_internal_tl
646 { \exp_after:wN \@@_file_pop_assign:nnnn \l_@@_internal_tl }
647 {
648 \msg_error:nnn { latex2e } { should-not-happen }
649 { Tried~to~pop~from~an~empty~file~name~stack. }
650 }
651 }
652 \cs_new_protected:Npn \@@_file_pop_assign:nnnn #1 #2 #3 #4
653 {
654 \tl_set:Nn \CurrentFilePathUsed {#1}
655 \tl_set:Nn \CurrentFileUsed {#2}
656 \tl_set:Nn \CurrentFilePath {#3}
657 \tl_set:Nn \CurrentFile {#4}
658 }
659 \ExplSyntaxOff
660 % \end{macrocode}
661 % \end{macro}
662 % \end{macro}
663 % \end{macro}
664 %
665 % \begin{macrocode}
666 %</2ekernel|latexrelease>
667 %<latexrelease>\EndIncludeInRelease
668 % \end{macrocode}
669 %
670 % When rolling forward the following expl3 functions may not be defined.
671 % If we roll back the code does nothing.
672 % \changes{v1.0d}{2020/11/24}{Support for roll forward (gh/434)}
673 % \InternalDetectionOff
674 % \begin{macrocode}
675 %<latexrelease>\IncludeInRelease{2020/10/01}%
676 %<latexrelease> {\file_parse_full_name_apply:nN}{Roll forward help}%
677 %<latexrelease>
678 %<latexrelease>\ExplSyntaxOn
679 %<latexrelease>\cs_if_exist:NF\file_parse_full_name_apply:nN
680 %<latexrelease>{
681 %<latexrelease>\cs_new:Npn \file_parse_full_name_apply:nN #1
682 %<latexrelease> {
683 %<latexrelease> \exp_args:Ne \__file_parse_full_name_auxi:nN
684 %<latexrelease> { \__kernel_file_name_sanitize:n {#1} }
685 %<latexrelease> }
686 %<latexrelease>\cs_new:Npn \__file_parse_full_name_auxi:nN #1
687 %<latexrelease> {
688 %<latexrelease> \__file_parse_full_name_area:nw { } #1
689 %<latexrelease> / \s__file_stop
690 %<latexrelease> }
691 %<latexrelease>\cs_new:Npn \__file_parse_full_name_area:nw #1 #2 / #3 \s__file_stop
692 %<latexrelease> {
693 %<latexrelease> \tl_if_empty:nTF {#3}
694 %<latexrelease> { \__file_parse_full_name_base:nw { } #2 . \s__file_stop {#1} }
695 %<latexrelease> { \__file_parse_full_name_area:nw { #1 / #2 }
696 %<latexrelease> #3 \s__file_stop }
697 %<latexrelease> }
698 %<latexrelease>\cs_new:Npn \__file_parse_full_name_base:nw #1 #2 . #3 \s__file_stop
699 %<latexrelease> {
700 %<latexrelease> \tl_if_empty:nTF {#3}
701 %<latexrelease> {
702 %<latexrelease> \tl_if_empty:nTF {#1}
703 %<latexrelease> {
704 %<latexrelease> \tl_if_empty:nTF {#2}
705 %<latexrelease> { \__file_parse_full_name_tidy:nnnN { } { } }
706 %<latexrelease> { \__file_parse_full_name_tidy:nnnN { .#2 } { } }
707 %<latexrelease> }
708 %<latexrelease> { \__file_parse_full_name_tidy:nnnN {#1} { .#2 } }
709 %<latexrelease> }
710 %<latexrelease> { \__file_parse_full_name_base:nw { #1 . #2 }
711 %<latexrelease> #3 \s__file_stop }
712 %<latexrelease> }
713 %<latexrelease>\cs_new:Npn \__file_parse_full_name_tidy:nnnN #1 #2 #3 #4
714 %<latexrelease> {
715 %<latexrelease> \exp_args:Nee #4
716 %<latexrelease> {
717 %<latexrelease> \str_if_eq:nnF {#3} { / } { \use_none:n }
718 %<latexrelease> #3 \prg_do_nothing:
719 %<latexrelease> }
720 %<latexrelease> { \use_none:n #1 \prg_do_nothing: }
721 %<latexrelease> {#2}
722 %<latexrelease> }
723 %<latexrelease>}
724 %<latexrelease>\ExplSyntaxOff
725 %<latexrelease>
726 %<latexrelease>\EndIncludeInRelease
727 %<*2ekernel>
728 % \end{macrocode}
729 % \InternalDetectionOn
730 %
731 % \begin{macrocode}
732 %<@@=>
733 % \end{macrocode}
734 %
735 % \subsection{Declaring the file-related hooks}
736 %
737 % These hooks have names with three-parts that
738 % start with \hook{file/}, \hook{include/},
739 % \hook{class/} or \hook{package/} and end with \hook{/before} or
740 % \hook{/after} (or \hook{/end} in the case of \hook{include/}).
741 % They are all generic hooks
742 % so will be declared only if code is added to them;
743 % this declaration is done for you automatically and, indeed, they should
744 % not be declared explicitly.
745 %
746 % Those named \hook{.../after} and \hook{include/.../end}
747 % are, when code is added, declared as reversed hooks.
748 %
749 %
750 % \subsection{Patching \LaTeX{}'s \cs{InputIfFileExists} command}
751 %
752 % Most of what we have to do is adding \cs{UseHook} into several
753 % \LaTeXe{} core commands, because of some circular dependencies in the
754 % kernel we do this only now and not in \texttt{ltfiles}.
755 %
756 % \begin{macro}[no-user-doc]{\InputIfFileExists}
757 % \begin{macro}[int]{\@input@file@exists@with@hooks}
758 % \begin{macro}[int]{\unqu@tefilef@und}
759 % \cs{InputIfFileExists} loads any file if it is available so we
760 % have to add the hooks \hook{file/before} and
761 % \hook{file/after} in the right places. If the file doesn't
762 % exist no hooks should be executed.
763 % \begin{macrocode}
764 %</2ekernel>
765 %<latexrelease>\IncludeInRelease{2020/10/01}%
766 %<latexrelease> {\InputIfFileExists}{Hook management (files)}%
767 %<*2ekernel|latexrelease>
768 % \end{macrocode}
769 %
770 % \begin{macrocode}
771 \let\InputIfFileExists\@undefined
772 \DeclareRobustCommand \InputIfFileExists[2]{%
773 \IfFileExists{#1}%
774 {%
775 \@expl@@@filehook@file@push@@
776 \@filehook@set@CurrentFile
777 % \end{macrocode}
778 % We pre-expand \cs{@filef@und} so that in case another file is
779 % loaded in the true branch of \cs{InputIfFileExists}, these don't
780 % change their value meanwhile. This isn't a worry with
781 % \cs[no-index]{CurrentFile...} because they are kept in a stack.
782 %
783 % \changes{v1.0d}{2020/11/20}
784 % {Move loading to \cs{@input@file@exists@with@hooks} and expand
785 % \cs{@filef@und} to avoid getting the wrong file name in the case of
786 % a substitution.}
787 % \begin{macrocode}
788 \expandafter\@swaptwoargs\expandafter
789 {\expandafter\@input@file@exists@with@hooks
790 \expandafter{\@filef@und}}%
791 {#2}%
792 \@expl@@@filehook@file@pop@@
793 }%
794 }
795 \def\@input@file@exists@with@hooks#1{%
796 % \end{macrocode}
797 % If the file exists then \cs{CurrentFile} holds its name. But we
798 % can't rely on that still being true after the file has been
799 % processed. Thus for using the name in the file hooks we need to
800 % preserve the name and then restore it for the
801 % \hook{file/.../after} hook.
802 %
803 % The hook always refers to the file requested by the user. The hook
804 % is \emph{always} loaded for \cs{CurrentFile} which usually is the
805 % same as \cs{CurrentFileUsed}. In the case of a file replacement,
806 % the \cs{CurrentFileUsed} holds the actual file loaded. In any case
807 % the file names are normalized so that the hooks work on the real
808 % file name, rather than what the user typed in.
809 %
810 % \pkg{expl3}'s \cs{file_full_name:n} normalizes the file
811 % name (to factor out differences in the |.tex| extension), and
812 % then does a file lookup to take into account a possible path from
813 % \cs{l_file_search_path_seq} and \cs{input@path}. However only
814 % the file name and extension are returned so that file hooks can
815 % refer to the file by their name only. The path to the file is
816 % returned in \cs{CurrentFilePath}.
817 % \changes{v1.0e}{2021/01/07}{Restore \cs[no-index]{CurrentFile(Path)(Used)}
818 % after the input (gh/464)}
819 % \begin{macrocode}
820 \edef\reserved@a{%
821 \@expl@@@filehook@file@pop@assign@@nnnn
822 {\CurrentFilePathUsed}%
823 {\CurrentFileUsed}%
824 {\CurrentFilePath}%
825 {\CurrentFile}}%
826 \expandafter\@swaptwoargs\expandafter{\reserved@a}%
827 % \end{macrocode}
828 %
829 % Before adding to the file list we need to make all (letter) characters
830 % catcode~11, because several packages use constructions like
831 % \begin{verbatim}
832 % \filename@parse{<filename>}
833 % \ifx\filename@ext\@clsextension
834 % ...
835 % \fi
836 % \end{verbatim}
837 % and that doesn't work if \cs{filename@ext} is \cs{detokenize}d.
838 % Making \cs{@clsextension} a string doesn't help much because some
839 % packages define their own \cs[no-index]{<prefix>@someextension} with
840 % normal catcodes. This is not entirely correct because packages loaded
841 % (somehow) with catcode~12 alphabetic tokens (say, as the result of
842 % a \cs{string} or \cs{detokenize} command, or from a \TeX{} string like
843 % \cs{jobname}) will have these character tokens incorrectly turned into
844 % letter tokens. This however is rare, so we'll go for the all-letters
845 % approach (grepping the packages in \TeX{} Live didn't bring up any
846 % obvious candidate for breaking with this catcode change).
847 % \begin{macrocode}
848 {\edef\reserved@a{\unqu@tefilef@und#1\@nil}%
849 \@addtofilelist{\string@makeletter\reserved@a}%
850 \UseHook{file/before}%
851 % \end{macrocode}
852 % The current file name is available in \cs{CurrentFile} so we use
853 % that in the specific hook.
854 % \begin{macrocode}
855 \UseHook{file/\CurrentFile/before}%
856 \@@input #1% <- trailing space comes from \@filef@und
857 }%
858 % \end{macrocode}
859 % And here, \cs{CurrentFile} is restored
860 % (by \cs{@expl@@@filehook@file@pop@assign@@nnnn}) so we can use it once more.
861 % \begin{macrocode}
862 \UseHook{file/\CurrentFile/after}%
863 \UseHook{file/after}}
864 \def\unqu@tefilef@und"#1" \@nil{#1}
865 % \end{macrocode}
866 %
867 % \changes{v1.0l}{2021/08/25}{Declare non-generic file hooks}
868 % Now declare the non-generic file hooks used above:
869 % \begin{macrocode}
870 \NewHook{file/before}
871 \NewReversedHook{file/after}
872 %<latexrelease>\EndIncludeInRelease
873 %</2ekernel|latexrelease>
874 % \end{macrocode}
875 %
876 % \changes{v0.9b}
877 % {1993/12/04}{Macro added}
878 % \changes{v0.9p}
879 % {1994/01/18}{New Definition}
880 % \changes{v0.3b}{1994/03/13}
881 % {Use new cmd \cs{@addtofilelist}}
882 % Now define |\InputIfFileExists| to input |#1| if it seems to exist.
883 % Immediately prior to the input, |#2| is executed.
884 % If the file |#1| does not exist, execute `|#3|'.
885 % \changes{v1.0t}{1995/05/25}
886 % {(CAR) added \cs{long}}
887 % \changes{v1.1o}{2019/02/07}{Expand \cs{@filef@und} before executing
888 % second argument (github/109)}
889 % \changes{v1.2b}{2019/08/27}{Make command robust}
890 % \begin{macrocode}
891 %<latexrelease>\IncludeInRelease{2019/10/01}%
892 %<latexrelease> {\InputIfFileExists}{Hook management (files)}%
893 %<latexrelease>
894 %<latexrelease>\DeclareRobustCommand \InputIfFileExists[2]{%
895 %<latexrelease> \IfFileExists{#1}%
896 %<latexrelease> {%
897 %<latexrelease> \expandafter\@swaptwoargs\expandafter
898 %<latexrelease> {\@filef@und}{#2\@addtofilelist{#1}\@@input}}}
899 %<latexrelease>\let\@input@file@exists@with@hooks\@undefined
900 %<latexrelease>\let\unqu@tefilef@und\@undefined
901 %<latexrelease>\EndIncludeInRelease
902 % \end{macrocode}
903 %
904 % \begin{macrocode}
905 %<latexrelease>\IncludeInRelease{0000/00/00}%
906 %<latexrelease> {\InputIfFileExists}{Hook management (files)}%
907 %<latexrelease>\long\def \InputIfFileExists#1#2{%
908 %<latexrelease> \IfFileExists{#1}%
909 %<latexrelease> {#2\@addtofilelist{#1}\@@input \@filef@und}}
910 % \end{macrocode}
911 %
912 % Also undo the internal command as some packages unfortunately test
913 % for their existence instead of using \cs{IfFormatAtLeastTF}.
914 % \changes{v1.0g}{2021/02/08}{Undo the internal for robust
915 % \cs{InputIfFileExists} in rollback (gh/494)}
916 % \begin{macrocode}
917 %<latexrelease>\expandafter\let\csname InputIfFileExists \endcsname\@undefined
918 % \end{macrocode}
919 %
920 % \begin{macrocode}
921 %<latexrelease>\let\@input@file@exists@with@hooks\@undefined
922 %<latexrelease>\let\unqu@tefilef@und\@undefined
923 %<latexrelease>\EndIncludeInRelease
924 %<*2ekernel>
925 % \end{macrocode}
926 % \end{macro}
927 % \end{macro}
928 % \end{macro}
929 %
930 %
931 %
932 %
933 %
934 %
935 % \subsection{Declaring a file substitution}
936 %
937 % \begin{macrocode}
938 %<@@=filehook>
939 % \end{macrocode}
940 %
941 % \begin{macrocode}
942 %</2ekernel>
943 %<*2ekernel|latexrelease>
944 %<latexrelease>\IncludeInRelease{2020/10/01}%
945 %<latexrelease> {\@@_subst_add:nn}{Declaring file substitution}%
946 \ExplSyntaxOn
947 % \end{macrocode}
948 %
949 %
950 % \begin{macro}{\@@_subst_add:nn,\@@_subst_remove:n,
951 % \@@_subst_file_normalize:Nn,\@@_subst_empty_name_chk:NN}
952 % \cs{@@_subst_add:nn} declares a file substitution by
953 % doing a (global) definition of the form
954 % |\def|\cs{@file-subst@\meta{file}}|{|\meta{replacement}|}|.
955 % The file names are properly sanitised, and normalized with the same
956 % treatment done for the file hooks. That is, a file replacement is
957 % declared by using the file name (and extension, if any) only, and
958 % the file path should not be given. If a file name is empty it is
959 % replaced by |.tex| (the empty csname is used to check that).
960 % \begin{macrocode}
961 \cs_new_protected:Npn \@@_subst_add:nn #1 #2
962 {
963 \group_begin:
964 \cs_set:cpe { } { \exp_not:o { \cs:w\cs_end: } }
965 \int_set:Nn \tex_escapechar:D { -1 }
966 \cs_gset:cpe
967 {
968 @file-subst@
969 \@@_subst_file_normalize:Nn \use_ii_iii:nnn {#1}
970 }
971 { \@@_subst_file_normalize:Nn \@@_file_name_compose:nnn
972 {#2} }
973 \group_end:
974 }
975 \cs_new_protected:Npn \@@_subst_remove:n #1
976 {
977 \group_begin:
978 \cs_set:cpe { } { \exp_not:o { \cs:w\cs_end: } }
979 \int_set:Nn \tex_escapechar:D { -1 }
980 \cs_undefine:c
981 {
982 @file-subst@
983 \@@_subst_file_normalize:Nn \use_ii_iii:nnn {#1}
984 }
985 \group_end:
986 }
987 \cs_new:Npn \@@_subst_file_normalize:Nn #1 #2
988 {
989 \exp_after:wN \@@_subst_empty_name_chk:NN
990 \cs:w \exp_after:wN \cs_end:
991 \cs:w \@@_file_parse_full_name:nN {#2} #1 \cs_end:
992 }
993 \cs_new:Npn \@@_subst_empty_name_chk:NN #1 #2
994 { \if_meaning:w #1 #2 .tex \else: \token_to_str:N #2 \fi: }
995 % \end{macrocode}
996 % \end{macro}
997 %
998 % \begin{macro}[int]{\use_ii_iii:nnn}
999 % A variant of \cs[no-index]{use_...} to discard the first of three
1000 % arguments.
1001 % \fmi{this should move to \pkg{expl3}}
1002 % \begin{macrocode}
1003 \cs_gset:Npn \use_ii_iii:nnn #1 #2 #3 {#2 #3}
1004 % \end{macrocode}
1005 % \end{macro}
1006 %
1007 %
1008 % \begin{macrocode}
1009 \ExplSyntaxOff
1010 %</2ekernel|latexrelease>
1011 %<latexrelease>\EndIncludeInRelease
1012 %<*2ekernel>
1013 % \end{macrocode}
1014 %
1015 %
1016 %
1017 % \begin{macro}{\declare@file@substitution}
1018 % \begin{macro}{\undeclare@file@substitution}
1019 % For two internals we provide \LaTeXe{} names so that we can use
1020 % them elsewhere in the kernel (and so that they can be used in
1021 % packages if really needed, e.g., \pkg{scrlfile}).
1022 % \begin{macrocode}
1023 %</2ekernel>
1024 %<*2ekernel|latexrelease>
1025 %<latexrelease>\IncludeInRelease{2020/10/01}%
1026 %<latexrelease> {\declare@file@substitution}{File substitution}%
1027 \ExplSyntaxOn
1028 \cs_new_eq:NN \declare@file@substitution \@@_subst_add:nn
1029 \cs_new_eq:NN \undeclare@file@substitution \@@_subst_remove:n
1030 \ExplSyntaxOff
1031 %</2ekernel|latexrelease>
1032 %<latexrelease>\EndIncludeInRelease
1033 % \end{macrocode}
1034 %
1035 % We are not fully rolling back the file substitutions in case a
1036 % rollback encounters a package that contains them, but is itself
1037 % not setup for rollback. So we just bypass them and hope for the
1038 % best.
1039 % \changes{v1.0d}{2020/12/04}{Don't drop file substitution commands on
1040 % rollback}
1041 % \begin{macrocode}
1042 %<latexrelease>\IncludeInRelease{0000/00/00}%
1043 %<latexrelease> {\declare@file@substitution}{File substitution}%
1044 %<latexrelease>
1045 %<latexrelease>\let \declare@file@substitution \@gobbletwo
1046 %<latexrelease>\let \undeclare@file@substitution \@gobble
1047 %<latexrelease>
1048 %<latexrelease>\EndIncludeInRelease
1049 %<*2ekernel>
1050 % \end{macrocode}
1051 % \end{macro}
1052 % \end{macro}
1053 %
1054 %
1055 %
1056 %
1057 % \begin{macrocode}
1058 %<@@=>
1059 % \end{macrocode}
1060 %
1061 % \subsection{Selecting a file (\cs{set@curr@file})}
1062 %
1063 % \begin{macro}[no-user-doc]{\set@curr@file,\set@curr@file@nosearch}
1064 % \begin{macro}[int]{\@curr@file,\@curr@file@reqd}
1065 % \changes{v1.0f}{2021/01/31}{set \cs{protect} to \cs{string} gh/481}
1066 % Now we hook into \cs{set@curr@file} to resolve a possible file
1067 % substitution, and add \cs{@expl@@@filehook@set@curr@file@@nNN}
1068 % at the end, after \cs{@curr@file} is set.
1069 %
1070 % A file name is built using
1071 % \cs{expandafter}\cs{string}\cs{csname}\meta{filename}\cs{endcsname}
1072 % to avoid expanding utf8 active characters. The \cs{csname} expands
1073 % the normalization machinery and the routine to resolve a file
1074 % substitution, returning a control sequence with the same name as the
1075 % file.
1076 %
1077 % It happens that when \meta{filename} is empty, the generated control
1078 % sequence is \cs{csname\cs{endcsname}}, and doing \cs{string} on
1079 % that results in the file |csnameendcsname.tex|. To guard against
1080 % that we \cs{ifx}-compare the generated control sequence with the
1081 % empty csname. To do so, \cs{csname\cs{endcsname}} has to be
1082 % defined, otherwise it would be equal to \cs{relax} and we would have
1083 % false positives. Here we define \cs{csname\cs{endcsname}} to
1084 % expand to itself to avoid it matching the definition of some other
1085 % control sequence.
1086 % \changes{v1.0i}{2021/04/20}
1087 % {Make \string~ expand to a string (tracks change in l3kernel)}
1088 % \changes{v1.0m}{2022/03/10}
1089 % {Add \cs{set@curr@file@nosearch} for \pkg{graphicx}}
1090 % \begin{macrocode}
1091 %</2ekernel>
1092 %<*2ekernel|latexrelease>
1093 %<latexrelease>\IncludeInRelease{2022/06/01}%
1094 %<latexrelease> {\set@curr@file}{Setting current file name}%
1095 \def\set@curr@file{%
1096 \begingroup
1097 \set@curr@file@aux}
1098 \edef\set@curr@file@nosearch{%
1099 \begingroup
1100 \let\noexpand\input@path\noexpand\@empty
1101 \csname seq_clear:N\endcsname
1102 \expandafter\noexpand\csname l_file_search_path_seq\endcsname
1103 \noexpand\set@curr@file@aux}
1104 \def\set@curr@file@aux#1{%
1105 \escapechar\m@ne
1106 \let\protect\string
1107 \edef~{\string~}%
1108 \expandafter\def\csname\expandafter\endcsname
1109 \expandafter{\csname\endcsname}%
1110 % \end{macrocode}
1111 % Two file names are set here: \cs{@curr@file@reqd} which is the file
1112 % requested by the user, and \cs{@curr@file} which should be the same,
1113 % except when we have a file substitution, in which case it holds the
1114 % actual loaded file. \cs{@curr@file} is resolved first, to check if
1115 % a substitution happens. If it doesn't,
1116 % \cs{@expl@@@filehook@if@file@replaced@@TF} short-cuts and just copies
1117 % \cs{@curr@file}, otherwise the full normalization procedure is
1118 % executed.
1119 %
1120 % At this stage the file name is parsed and normalized, but if the
1121 % input doesn't have an extension, the default |.tex| is \emph{not}
1122 % added to \cs{@curr@file} because for applications other than
1123 % \cs{input} (graphics, for example) the default extension may not
1124 % be |.tex|. First check if the input has an extension, then if the
1125 % input had no extension, call \cs{@expl@@@filehook@drop@extension@@N}. In case
1126 % of a file substitution, \cs{@curr@file} will have an extension.
1127 % \begin{macrocode}
1128 \@expl@@@filehook@if@no@extension@@nTF{#1}%
1129 {\@tempswatrue}{\@tempswafalse}%
1130 \@kernel@make@file@csname\@curr@file
1131 \@expl@@@filehook@resolve@file@subst@@w {#1}%
1132 \@expl@@@filehook@if@file@replaced@@TF
1133 {\@kernel@make@file@csname\@curr@file@reqd
1134 \@expl@@@filehook@normalize@file@name@@w{#1}%
1135 \if@tempswa \@expl@@@filehook@drop@extension@@N\@curr@file@reqd \fi}%
1136 {\if@tempswa \@expl@@@filehook@drop@extension@@N\@curr@file \fi
1137 \global\let\@curr@file@reqd\@curr@file}%
1138 \@expl@@@filehook@clear@replacement@flag@@
1139 \endgroup}
1140 %</2ekernel|latexrelease>
1141 %<latexrelease>\EndIncludeInRelease
1142 % \end{macrocode}
1143 %
1144 % \begin{macrocode}
1145 %<latexrelease>\IncludeInRelease{2021/06/01}%
1146 %<latexrelease> {\set@curr@file}{Setting current file name}%
1147 %<latexrelease>\def\set@curr@file#1{%
1148 %<latexrelease> \begingroup
1149 %<latexrelease> \escapechar\m@ne
1150 %<latexrelease> \let\protect\string
1151 %<latexrelease> \edef~{\string~}%
1152 %<latexrelease> \expandafter\def\csname\expandafter\endcsname
1153 %<latexrelease> \expandafter{\csname\endcsname}%
1154 %<latexrelease> \@expl@@@filehook@if@no@extension@@nTF{#1}%
1155 %<latexrelease> {\@tempswatrue}{\@tempswafalse}%
1156 %<latexrelease> \@kernel@make@file@csname\@curr@file
1157 %<latexrelease> \@expl@@@filehook@resolve@file@subst@@w {#1}%
1158 %<latexrelease> \@expl@@@filehook@if@file@replaced@@TF
1159 %<latexrelease> {\@kernel@make@file@csname\@curr@file@reqd
1160 %<latexrelease> \@expl@@@filehook@normalize@file@name@@w{#1}%
1161 %<latexrelease> \if@tempswa \@expl@@@filehook@drop@extension@@N\@curr@file@reqd \fi}%
1162 %<latexrelease> {\if@tempswa \@expl@@@filehook@drop@extension@@N\@curr@file \fi
1163 %<latexrelease> \global\let\@curr@file@reqd\@curr@file}%
1164 %<latexrelease> \@expl@@@filehook@clear@replacement@flag@@
1165 %<latexrelease> \endgroup}
1166 %<latexrelease>\let\set@curr@file@nosearch\@undefined
1167 %<latexrelease>\EndIncludeInRelease
1168 % \end{macrocode}
1169 %
1170 % \begin{macrocode}
1171 %<latexrelease>\IncludeInRelease{2020/10/01}%
1172 %<latexrelease> {\set@curr@file}{Setting current file name}%
1173 %<latexrelease>\def\set@curr@file#1{%
1174 %<latexrelease> \begingroup
1175 %<latexrelease> \escapechar\m@ne
1176 %<latexrelease> \expandafter\def\csname\expandafter\endcsname
1177 %<latexrelease> \expandafter{\csname\endcsname}%
1178 %<latexrelease> \@expl@@@filehook@if@no@extension@@nTF{#1}%
1179 %<latexrelease> {\@tempswatrue}{\@tempswafalse}%
1180 %<latexrelease> \@kernel@make@file@csname\@curr@file
1181 %<latexrelease> \@expl@@@filehook@resolve@file@subst@@w {#1}%
1182 %<latexrelease> \@expl@@@filehook@if@file@replaced@@TF
1183 %<latexrelease> {\@kernel@make@file@csname\@curr@file@reqd
1184 %<latexrelease> \@expl@@@filehook@normalize@file@name@@w{#1}%
1185 %<latexrelease> \if@tempswa \@expl@@@filehook@drop@extension@@N\@curr@file@reqd \fi}%
1186 %<latexrelease> {\if@tempswa \@expl@@@filehook@drop@extension@@N\@curr@file \fi
1187 %<latexrelease> \global\let\@curr@file@reqd\@curr@file}%
1188 %<latexrelease> \@expl@@@filehook@clear@replacement@flag@@
1189 %<latexrelease> \endgroup}
1190 %<latexrelease>\let\set@curr@file@nosearch\@undefined
1191 %<latexrelease>\EndIncludeInRelease
1192 % \end{macrocode}
1193 %
1194 % \begin{macrocode}
1195 %<latexrelease>\IncludeInRelease{2019/10/01}%
1196 %<latexrelease> {\set@curr@file}{Setting current file name}%
1197 %<latexrelease>\def\set@curr@file#1{%
1198 %<latexrelease> \begingroup
1199 %<latexrelease> \escapechar\m@ne
1200 %<latexrelease> \xdef\@curr@file{%
1201 %<latexrelease> \expandafter\expandafter\expandafter\unquote@name
1202 %<latexrelease> \expandafter\expandafter\expandafter{%
1203 %<latexrelease> \expandafter\string
1204 %<latexrelease> \csname\@firstofone#1\@empty\endcsname}}%
1205 %<latexrelease> \endgroup
1206 %<latexrelease>}
1207 %<latexrelease>\let\set@curr@file@nosearch\@undefined
1208 %<latexrelease>\EndIncludeInRelease
1209 % \end{macrocode}
1210 %
1211 % \begin{macrocode}
1212 %<latexrelease>\IncludeInRelease{0000/00/00}%
1213 %<latexrelease> {\set@curr@file}{Setting current file name}%
1214 %<latexrelease>\let\set@curr@file\@undefined
1215 %<latexrelease>\let\set@curr@file@nosearch\@undefined
1216 %<latexrelease>\EndIncludeInRelease
1217 %<*2ekernel>
1218 % \end{macrocode}
1219 % \end{macro}
1220 % \end{macro}
1221 %
1222 %
1223 %
1224 %
1225 % \begin{macro}[int]{\@filehook@set@CurrentFile}
1226 % \begin{macro}[int]{\@kernel@make@file@csname,\@set@curr@file@aux}
1227 %
1228 % \fmi{This should get internalized using \texttt{@expl@} names}
1229 % \begin{macrocode}
1230 %</2ekernel>
1231 %<*2ekernel|latexrelease>
1232 %<latexrelease>\IncludeInRelease{2020/10/01}%
1233 %<latexrelease> {\@kernel@make@file@csname}{Make file csname}%
1234 % \end{macrocode}
1235 %
1236 % \begin{macrocode}
1237 \def\@kernel@make@file@csname#1#2#3{%
1238 \xdef#1{\expandafter\@set@curr@file@aux
1239 \csname\expandafter#2\@firstofone#3\@nil\endcsname}}
1240 % \end{macrocode}
1241 % This auxiliary compares \cs{\meta{filename}} with
1242 % \cs{csname\cs{endcsname}} to check if the empty |.tex| file was
1243 % requested.
1244 % \changes{v1.0o}{2023/04/02}
1245 % {Make \cs{@set@curr@file@aux} \cs{long} gh/942}
1246 % \begin{macrocode}
1247 \long\def\@set@curr@file@aux#1{%
1248 \expandafter\ifx\csname\endcsname#1%
1249 .tex\else\string#1\fi}
1250 % \end{macrocode}
1251 %
1252 % \begin{sloppypar}
1253 % Then we call \cs{@expl@@@filehook@set@curr@file@@nNN} once for
1254 % \cs{@curr@file} to set \cs[no-index]{CurrentFile(Path)Used} and once for
1255 % \cs{@curr@file@reqd} to set \cs[no-index]{CurrentFile(Path)}.
1256 % Here too the slower route is only used if a substitution happened,
1257 % but here \cs{@expl@@@filehook@if@file@replaced@@TF} can't be used because
1258 % the flag is reset at the \cs{endgroup} above, so we check if
1259 % \cs{@curr@file} and \cs{@curr@file@reqd} differ. This macro is
1260 % issued separate from \cs{set@curr@file} because it changes
1261 % \cs{CurrentFile}, and side-effects would quickly get out of control.
1262 % \end{sloppypar}
1263 % \begin{macrocode}
1264 \def\@filehook@set@CurrentFile{%
1265 \@expl@@@filehook@set@curr@file@@nNN{\@curr@file}%
1266 \CurrentFileUsed\CurrentFilePathUsed
1267 \ifx\@curr@file@reqd\@curr@file
1268 \let\CurrentFile\CurrentFileUsed
1269 \let\CurrentFilePath\CurrentFilePathUsed
1270 \else
1271 \@expl@@@filehook@set@curr@file@@nNN{\@curr@file@reqd}%
1272 \CurrentFile\CurrentFilePath
1273 \fi}
1274 %</2ekernel|latexrelease>
1275 %<latexrelease>\EndIncludeInRelease
1276 %<*2ekernel>
1277 % \end{macrocode}
1278 % \end{macro}
1279 % \end{macro}
1280 %
1281 % \begin{macrocode}
1282 %<@@=filehook>
1283 % \end{macrocode}
1284 %
1285 % \begin{macro}{\@@_set_curr_file:nNN}
1286 % \begin{macro}{\@@_set_curr_file_assign:nnnNN}
1287 % When inputting a file, \cs{set@curr@file} does a file lookup
1288 % (in \cs{input@path} and \cs{l_file_search_path_seq}) and returns the
1289 % actual file name (\meta{base} plus \meta{ext}) in
1290 % \cs{CurrentFileUsed}, and in case there's a file substitution, the
1291 % requested file in \cs{CurrentFile} (otherwise both are the same).
1292 % Only the base and extension are returned,
1293 % regardless of the input (both \texttt{path/to/file.tex} and
1294 % \texttt{file.tex} end up as \texttt{file.tex} in \cs{CurrentFile}).
1295 % The path is returned in \cs{CurrentFilePath}, in case it's needed.
1296 % \begin{macrocode}
1297 %</2ekernel>
1298 %<*2ekernel|latexrelease>
1299 %<latexrelease>\IncludeInRelease{2020/10/01}%
1300 %<latexrelease> {\@@_set_curr_file:nNN}{Set curr file}%
1301 \ExplSyntaxOn
1302 \cs_new_protected:Npn \@@_set_curr_file:nNN #1
1303 {
1304 \exp_args:Nf \@@_file_parse_full_name:nN {#1}
1305 \@@_set_curr_file_assign:nnnNN
1306 }
1307 \cs_new_protected:Npn \@@_set_curr_file_assign:nnnNN #1 #2 #3 #4 #5
1308 {
1309 \str_set:Nn #5 {#1}
1310 \str_set:Nn #4 {#2#3}
1311 }
1312 \ExplSyntaxOff
1313 %</2ekernel|latexrelease>
1314 %<latexrelease>\EndIncludeInRelease
1315 %<*2ekernel>
1316 % \end{macrocode}
1317 % \end{macro}
1318 % \end{macro}
1319 %
1320 %
1321 %
1322 % \subsection{Replacing a file and detecting loops}
1323 %
1324 % \begin{macro}{\@@_resolve_file_subst:w}
1325 % \begin{macro}{\@@_normalize_file_name:w}
1326 % \begin{macro}{\@@_file_name_compose:nnn}
1327 % Start by sanitizing the file with \cs{@@_file_parse_full_name:nN}
1328 % then do \cs{@@_file_subst_begin:nnn}\Arg{path}\Arg{name}\Arg{ext}.
1329 % \begin{macrocode}
1330 %</2ekernel>
1331 %<*2ekernel|latexrelease>
1332 %<latexrelease>\IncludeInRelease{2020/10/01}%
1333 %<latexrelease> {\@@_resolve_file_subst:w}{Replace files detect loops}%
1334 \ExplSyntaxOn
1335 \cs_new:Npn \@@_resolve_file_subst:w #1 \@nil
1336 { \@@_file_parse_full_name:nN {#1} \@@_file_subst_begin:nnn }
1337 \cs_new:Npn \@@_normalize_file_name:w #1 \@nil
1338 { \@@_file_parse_full_name:nN {#1} \@@_file_name_compose:nnn }
1339 \cs_new:Npn \@@_file_name_compose:nnn #1 #2 #3
1340 { \tl_if_empty:nF {#1} { #1 / } #2#3 }
1341 % \end{macrocode}
1342 % \end{macro}
1343 % \end{macro}
1344 %
1345 % \begin{macro}{@@_file_replaced}
1346 % \begin{macro}{\@@_if_file_replaced:TF}
1347 % \begin{macro}{\@@_clear_replacement_flag:}
1348 % Since the file replacement is done expandably in a \cs{csname}, use
1349 % a flag to remember if a substitution happened. We use this in
1350 % \cs{set@curr@file} to short-circuit some of it in case no
1351 % substitution happened (by far the most common case, so it's worth
1352 % optimizing). The flag raised during the file substitution algorithm
1353 % must be explicitly cleared after the \cs{@@_if_file_replaced:TF}
1354 % conditional is no longer needed, otherwise further uses of
1355 % \cs{@@_if_file_replaced:TF} will wrongly return true.
1356 % \begin{macrocode}
1357 \flag_new:n { @@_file_replaced }
1358 \cs_new:Npn \@@_if_file_replaced:TF #1 #2
1359 { \flag_if_raised:nTF { @@_file_replaced } {#1} {#2} }
1360 \cs_new_protected:Npn \@@_clear_replacement_flag:
1361 { \flag_clear:n { @@_file_replaced } }
1362 % \end{macrocode}
1363 % \end{macro}
1364 % \end{macro}
1365 % \end{macro}
1366 %
1367 % \begin{macro}{\@@_file_subst_begin:nnn}
1368 % First off, start by checking if the current file ($\meta{name} +
1369 % \meta{ext}$) has a declared substitution. If not, then just put
1370 % that as the name (including a possible \meta{path} in this case):
1371 % this is the default case with no substitutions, so it's the first to
1372 % be checked. The auxiliary \cs{@@_file_subst_tortoise_hare:nn} sees
1373 % that there's no replacement for |#2#3| and does nothing else.
1374 % \begin{macrocode}
1375 \cs_new:Npn \@@_file_subst_begin:nnn #1 #2 #3
1376 {
1377 \@@_file_subst_tortoise_hare:nn { #2#3 } { #2#3 }
1378 { \@@_file_name_compose:nnn {#1} {#2} {#3} }
1379 }
1380 \ExplSyntaxOff
1381 %</2ekernel|latexrelease>
1382 %<latexrelease>\EndIncludeInRelease
1383 %<*2ekernel>
1384 % \end{macrocode}
1385 % \end{macro}
1386 %
1387 %
1388 %
1389 %
1390 % \subsubsection{The Tortoise and Hare algorithm}
1391 %
1392 % \begin{macro}{\@@_file_subst_tortoise_hare:nn}
1393 % \begin{macro}{\@@_file_subst_loop:NN,\@@_file_subst_loop:cc}
1394 % If there is a substitution (\meta{true} in the first
1395 % \cs{cs_if_exist:cTF} below), then first check if there is no
1396 % substitution down the line: this should be the second most common
1397 % case, of one file replaced by another. In that case just leave the
1398 % substitution there and the job is done. If any substitution
1399 % happens, then the \cs{flag @@_file_replaced} is raised
1400 % (conditionally, because checking if a flag is raised is much faster
1401 % than raising it over and over again).
1402 %
1403 % If, however there are more substitutions, then we need to check for
1404 % a possible loop in the substitutions, which would otherwise put
1405 % \TeX{} in an infinite loop if just an exhaustive expansion was used.
1406 %
1407 % To detect a loop, the \emph{Tortoise and Hare} algorithm is used.
1408 % The name of the algorithm is an analogy to Aesop's fable, in which
1409 % the Hare outruns a Tortoise. The two pointers here are the csnames
1410 % which contains each file replacement, both of which start at the
1411 % position zero, which is the file requested. In the inner part of
1412 % the macro below, \cs{@@_file_subst_loop:cc} is called with
1413 % \cs[no-index]{@file-subst@\meta{file}} and
1414 % \cs[no-index]{@file-subst@\cs[no-index]{@file-subst@\meta{file}}};
1415 % that is, the substitution of \meta{file} and the substitution of that
1416 % substitution: the Tortoise walks one step while the Hare walks two.
1417 %
1418 % Within \cs{@@_file_subst_loop:NN} the two substitutions are
1419 % compared, and if they lead to the same file it means that there is
1420 % a loop in the substitutions. If there's no loop,
1421 % \cs{@@_file_subst_tortoise_hare:nn} is called again with the
1422 % Tortoise at position~1 and the hare at~2. Again, the substitutions
1423 % are checked ahead of the Hare pointer to check that it won't run too
1424 % far; in case there is no loop in the declarations, eventually one
1425 % of the \cs{cs_if_exist:cTF} below will go \meta{false} and the
1426 % algorithm will end; otherwise it will run until the Hare reaches
1427 % the same spot as the tortoise and a loop is detected.
1428 % \begin{macrocode}
1429 %</2ekernel>
1430 %<*2ekernel|latexrelease>
1431 %<latexrelease>\IncludeInRelease{2020/10/01}%
1432 %<latexrelease> {\@@_file_subst_tortoise_hare:nn}{Tortoise and Hare}%
1433 \ExplSyntaxOn
1434 \cs_new:Npn \@@_file_subst_tortoise_hare:nn #1 #2 #3
1435 {
1436 \cs_if_exist:cTF { @file-subst@ #2 }
1437 {
1438 \flag_if_raised:nF { @@_file_replaced }
1439 { \flag_raise:n { @@_file_replaced } }
1440 \cs_if_exist:cTF { @file-subst@ \use:c { @file-subst@ #2 } }
1441 {
1442 \@@_file_subst_loop:cc
1443 { @file-subst@ #1 }
1444 { @file-subst@ \use:c { @file-subst@ #2 } }
1445 }
1446 { \use:c { @file-subst@ #2 } }
1447 }
1448 { #3 }
1449 }
1450 % \end{macrocode}
1451 % This is just an auxiliary to check if a loop was found, and continue
1452 % the algorithm otherwise. If a loop is found, the |.tex| file is
1453 % used as fallback and \cs{@@_file_subst_cycle_error:cN} is called to
1454 % report the error.
1455 % \begin{macrocode}
1456 \cs_new:Npn \@@_file_subst_loop:NN #1 #2
1457 {
1458 \token_if_eq_meaning:NNTF #1 #2
1459 {
1460 .tex
1461 \@@_file_subst_cycle_error:cN { @file-subst@ #1 } #1
1462 }
1463 { \@@_file_subst_tortoise_hare:nn {#1} {#2} {#2} }
1464 }
1465 \cs_generate_variant:Nn \@@_file_subst_loop:NN { cc }
1466 % \end{macrocode}
1467 % \end{macro}
1468 % \end{macro}
1469 %
1470 % \begin{macro}{
1471 % \@@_file_subst_cycle_error:NN,
1472 % \@@_file_subst_cycle_error:cN,
1473 % }
1474 % \changes{v1.0l}{2021/08/27}{Use \cs{msg_...} not \cs{__kernel_msg_...}}
1475 % Showing this type of error expandably is tricky, as we have a very
1476 % limited amount of characters to show and a potentially large list.
1477 % As a work around, several errors are printed, each showing one step
1478 % of the loop, until all the error messages combined show the loop.
1479 % \begin{macrocode}
1480 \cs_new:Npn \@@_file_subst_cycle_error:NN #1 #2
1481 {
1482 \msg_expandable_error:nnff { latex2e } { file-cycle }
1483 {#1} { \use:c { @file-subst@ #1 } }
1484 \token_if_eq_meaning:NNF #1 #2
1485 { \@@_file_subst_cycle_error:cN { @file-subst@ #1 } #2 }
1486 }
1487 \cs_generate_variant:Nn \@@_file_subst_cycle_error:NN { c }
1488 % \end{macrocode}
1489 %
1490 % And the error message:
1491 % \begin{macrocode}
1492 \msg_new:nnn { latex2e } { file-cycle }
1493 { File~loop!~#1~replaced~by~#2... }
1494 % \end{macrocode}
1495 % \end{macro}
1496 % \end{macro}
1497 %
1498 % \begin{macrocode}
1499 \ExplSyntaxOff
1500 %</2ekernel|latexrelease>
1501 %<latexrelease>\EndIncludeInRelease
1502 %<*2ekernel>
1503 % \end{macrocode}
1504 %
1505 %
1506 % \begin{macrocode}
1507 %<@@=>
1508 % \end{macrocode}
1509 %
1510 %
1511 % \subsection{Preventing a package from loading}
1512 %
1513 % We support the use case of preventing a package from loading but not
1514 % any other type of files (e.g., classes).
1515 %
1516 % \begin{macro}{\disable@package@load}
1517 % \begin{macro}{\reenable@package@load}
1518 % \begin{macro}{\@disable@packageload@do}
1519 % \cs{disable@package@load} defines
1520 % \cs[no-index]{@pkg-disable@\meta{package}} to expand to some code |#2|
1521 % instead of loading the package.
1522 % \begin{macrocode}
1523 %</2ekernel>
1524 %<*2ekernel|latexrelease>
1525 %<latexrelease>\IncludeInRelease{2020/10/01}%
1526 %<latexrelease> {\disable@package@load}{Disable packages}%
1527 \def\disable@package@load#1#2{%
1528 \global\@namedef{@pkg-disable@#1.\@pkgextension}{#2}}
1529 % \end{macrocode}
1530 %
1531 % \changes{v1.0n}{2022/08/18}{Inhibit checking the loaded version when
1532 % package is load-disabled, and write to the .log (gh/888)}
1533 % Here we check if a control sequence named
1534 % \cs[no-index]{@pkg-disable@\meta{name}.sty} is defined, and if so
1535 % don't use the package loading code |#2|, but use the replacement
1536 % code stored in that control sequence, write something to the log,
1537 % and then prevent \cs{@onefilewithoptions} from sanity-checking the
1538 % requested package date (the \tn{expandafter} here triggers one in
1539 % \cs{@onefilewithoptions} that ends a conditional there, and the
1540 % \tn{@gobbletwo} removes the date checking code from the input
1541 % stream).
1542 % \begin{macrocode}
1543 \def\@disable@packageload@do#1#2{%
1544 \@ifundefined{@pkg-disable@#1}%
1545 {#2}%
1546 {\@nameuse{@pkg-disable@#1}%
1547 \@latex@info{Package '#1' has been disabled.%
1548 \MessageBreak Load request ignored}%
1549 \expandafter\@gobbletwo}}
1550 % \end{macrocode}
1551 %
1552 % \cs{reenable@package@load} undefines
1553 % \cs[no-index]{@pkg-disable@\meta{package}} to reallow loading a package.
1554 % \begin{macrocode}
1555 \def\reenable@package@load#1{%
1556 \global\expandafter\let
1557 \csname @pkg-disable@#1.\@pkgextension \endcsname \@undefined}
1558 % \end{macrocode}
1559 %
1560 %
1561 % \begin{macrocode}
1562 %</2ekernel|latexrelease>
1563 %<latexrelease>\EndIncludeInRelease
1564 %<latexrelease>\IncludeInRelease{0000/00/00}%
1565 %<latexrelease> {\disable@package@load}{Disable packages}%
1566 %<latexrelease>
1567 %<latexrelease>\let\disable@package@load \@undefined
1568 %<latexrelease>\let\@disable@packageload@do\@undefined
1569 %<latexrelease>\let\reenable@package@load \@undefined
1570 %<latexrelease>\EndIncludeInRelease
1571 %<*2ekernel>
1572 % \end{macrocode}
1573 % \end{macro}
1574 % \end{macro}
1575 % \end{macro}
1576 %
1577 %
1578 %
1579 %
1580 %
1581 % \subsection{High-level interfaces for \LaTeX{}}
1582 %
1583 % None so far and the general feeling for now is that the hooks are
1584 % enough. Packages like \pkg{filehook}, etc., may use them to set
1585 % up their interfaces (samples are given below) but for the now the
1586 % kernel will not provide any.
1587 %
1588 %
1589 %
1590 % \subsection{Internal commands needed elsewhere}
1591 %
1592 % Here we set up a few horrible (but consistent) \LaTeXe{} names to
1593 % allow for internal commands to be used outside this module (and
1594 % in parts that still use \LaTeXe{} syntax. We have to unset the
1595 % \texttt{@\/@} since we want double ``at'' sign in place of double
1596 % underscores.
1597 %
1598 % \begin{macrocode}
1599 %<@@=>
1600 % \end{macrocode}
1601 % \InternalDetectionOff
1602 % \begin{macrocode}
1603 %</2ekernel>
1604 %<*2ekernel|latexrelease>
1605 %<latexrelease>\IncludeInRelease{2020/10/01}%
1606 %<latexrelease> {\@expl@@@filehook@if@no@extension@@nTF}{2e tmp interfaces}%
1607 \ExplSyntaxOn
1608 % \end{macrocode}
1609 %
1610 % \begin{macrocode}
1611 \cs_new_eq:NN \@expl@@@filehook@if@no@extension@@nTF
1612 \__filehook_if_no_extension:nTF
1613 % \end{macrocode}
1614 %
1615 % \begin{macrocode}
1616 \cs_new_eq:NN \@expl@@@filehook@set@curr@file@@nNN
1617 \__filehook_set_curr_file:nNN
1618 % \end{macrocode}
1619 %
1620 % \begin{macrocode}
1621 \cs_new_eq:NN \@expl@@@filehook@resolve@file@subst@@w
1622 \__filehook_resolve_file_subst:w
1623 % \end{macrocode}
1624 %
1625 % \begin{macrocode}
1626 \cs_new_eq:NN \@expl@@@filehook@normalize@file@name@@w
1627 \__filehook_normalize_file_name:w
1628 % \end{macrocode}
1629 %
1630 % \begin{macrocode}
1631 \cs_new_eq:NN \@expl@@@filehook@if@file@replaced@@TF
1632 \__filehook_if_file_replaced:TF
1633 % \end{macrocode}
1634 %
1635 % \begin{macrocode}
1636 \cs_new_eq:NN \@expl@@@filehook@clear@replacement@flag@@
1637 \__filehook_clear_replacement_flag:
1638 % \end{macrocode}
1639 %
1640 % \begin{macrocode}
1641 \cs_new_eq:NN \@expl@@@filehook@drop@extension@@N
1642 \__filehook_drop_extension:N
1643 % \end{macrocode}
1644 %
1645 % \begin{macrocode}
1646 \cs_new_eq:NN \@expl@@@filehook@file@push@@
1647 \__filehook_file_push:
1648 % \end{macrocode}
1649 %
1650 % \begin{macrocode}
1651 \cs_new_eq:NN \@expl@@@filehook@file@pop@@
1652 \__filehook_file_pop:
1653 % \end{macrocode}
1654 %
1655 % \begin{macrocode}
1656 \cs_new_eq:NN \@expl@@@filehook@file@pop@assign@@nnnn
1657 \__filehook_file_pop_assign:nnnn
1658 % \end{macrocode}
1659 % \InternalDetectionOn
1660 %
1661 %
1662 % \begin{macrocode}
1663 \ExplSyntaxOff
1664 % \end{macrocode}
1665 %
1666 % This one specifically has to be undefined because it is left over in
1667 % the input stream from \cs{InputIfFileExists} and executed when
1668 % \pkg{latexrelease} is loaded. It cannot be \cs{let} to \cs{@undefined}
1669 % otherwise it would error as well, so it is \cs{let} to \cs{relax} to
1670 % be silently ignored when loading \cs{latexrelease}.
1671 % \changes{v1.0e}{2021/01/07}{Added rollback for this case to avoid
1672 % spurious errors (part of gh/463)}
1673 % \begin{macrocode}
1674 %</2ekernel|latexrelease>
1675 %<latexrelease>\EndIncludeInRelease
1676 %<latexrelease>
1677 %<latexrelease>\IncludeInRelease{0000/00/00}%
1678 %<latexrelease> {\@expl@@@filehook@if@no@extension@@nTF}{2e tmp interfaces}%
1679 %<latexrelease>\let\@expl@@@filehook@file@pop@@\relax
1680 %<latexrelease>\EndIncludeInRelease
1681 %<*2ekernel>
1682 % \end{macrocode}
1683 %
1684 % This ends the kernel code in this file.
1685 % \begin{macrocode}
1686 %</2ekernel>
1687 % \end{macrocode}
1688 %
1689 %
1690 %
1691 % \section{A sample package for structuring the log output}
1692 %
1693 % \begin{macrocode}
1694 %<*structuredlog>
1695 %<@@=filehook>
1696 % \end{macrocode}
1697 %
1698 % \begin{macrocode}
1699 \ProvidesExplPackage
1700 {structuredlog}{\ltfilehookdate}{\ltfilehookversion}
1701 {Structuring the TeX transcript file}
1702 % \end{macrocode}
1703 %
1704 % \begin{macro}{\g_@@_nesting_level_int}
1705 % Stores the current package nesting level.
1706 % \begin{macrocode}
1707 \int_new:N \g_@@_nesting_level_int
1708 % \end{macrocode}
1709 % Initialise the counter with the number of files in the
1710 % \cs{@currnamestack} (the number of items divided by $3$) minus one,
1711 % because this package is skipped when printing to the log.
1712 % \begin{macrocode}
1713 \int_gset:Nn \g_@@_nesting_level_int
1714 { ( \tl_count:N \@currnamestack ) / 3 - 1 }
1715 % \end{macrocode}
1716 % \end{macro}
1717 %
1718 % \begin{macro}{\@@_log_file_record:n}
1719 % This macro is responsible for increasing and decreasing the file
1720 % nesting level, as well as printing to the log. The argument is
1721 % either |STOPTART| or |STOP| and the action it takes on the nesting
1722 % integer depends on that.
1723 % \begin{macrocode}
1724 \cs_new_protected:Npn \@@_log_file_record:n #1
1725 {
1726 \str_if_eq:nnT {#1} {START} { \int_gincr:N \g_@@_nesting_level_int }
1727 \iow_term:e
1728 {
1729 \prg_replicate:nn { \g_@@_nesting_level_int } { = } ~
1730 ( LEVEL ~ \int_use:N \g_@@_nesting_level_int \c_space_tl #1 ) ~
1731 \CurrentFileUsed
1732 % \end{macrocode}
1733 % If there was a file replacement, show that as well:
1734 % \begin{macrocode}
1735 \str_if_eq:NNF \CurrentFileUsed \CurrentFile
1736 { ~ ( \CurrentFile \c_space_tl requested ) }
1737 \iow_newline:
1738 }
1739 \str_if_eq:nnT {#1} {STOP} { \int_gdecr:N \g_@@_nesting_level_int }
1740 }
1741 % \end{macrocode}
1742 %
1743 % Now just hook the macro above in the generic |file/before|\ldots
1744 % \begin{macrocode}
1745 \AddToHook{file/before}{ \@@_log_file_record:n { START } }
1746 % \end{macrocode}
1747 % \ldots and |file/after| hooks.
1748 % We don't want to install the \hook{file/after} hook immediately,
1749 % because that would mean it is the first time executed when the
1750 % package finishes. We therefore put the declaration inside
1751 % \cs{AddToHookNext} so that it gets only installed when we have
1752 % left this package.
1753 % \begin{macrocode}
1754 \AddToHookNext{file/after}
1755 { \AddToHook{file/after}{ \@@_log_file_record:n { STOP } } }
1756 % \end{macrocode}
1757 % \end{macro}
1758 %
1759 % \begin{macrocode}
1760 %<@@=>
1761 %</structuredlog>
1762 % \end{macrocode}
1763 %
1764 %
1765 %
1766 %
1767 %
1768 %
1769 % \section{Package emulations}
1770 %
1771 %
1772 % \subsection{Package \pkg{atveryend} emulation}
1773 %
1774 % With the new hook management and the hooks in \cs{enddocument}
1775 % all of \pkg{atveryend} is taken care of.
1776 % We can make an emulation only here after the substitution
1777 % functionality is available:
1778 % \begin{macrocode}
1779 %<*2ekernel>
1780 \declare@file@substitution{atveryend.sty}{atveryend-ltx.sty}
1781 %</2ekernel>
1782 % \end{macrocode}
1783 %
1784 % Here is the package file we point to:
1785 % \begin{macrocode}
1786 %<*atveryend-ltx>
1787 \ProvidesPackage{atveryend-ltx}
1788 [2020/08/19 v1.0a
1789 Emulation of the original atveryend package^^Jwith kernel methods]
1790 % \end{macrocode}
1791 %
1792 %
1793 % Here are new definitions for its interfaces now pointing to the
1794 % hooks in \cs{enddocument}
1795 % \begin{macrocode}
1796 \newcommand\AfterLastShipout {\AddToHook{enddocument/afterlastpage}}
1797 \newcommand\AtVeryEndDocument {\AddToHook{enddocument/afteraux}}
1798 % \end{macrocode}
1799 % Next one is a bit of a fake, but the result should normally be as
1800 % expected. If not, one needs to add a rule to sort the code chunks
1801 % in \hook{enddocument/info}.
1802 % \begin{macrocode}
1803 \newcommand\AtEndAfterFileList{\AddToHook{enddocument/info}}
1804 % \end{macrocode}
1805 %
1806 % \begin{macrocode}
1807 \newcommand\AtVeryVeryEnd {\AddToHook{enddocument/end}}
1808 % \end{macrocode}
1809 %
1810 % \begin{macro}[no-user-doc]{\BeforeClearDocument}
1811 % This one is the only one we don't implement or rather don't have
1812 % a dedicated hook in the code.
1813 % \begin{macrocode}
1814 \ExplSyntaxOn
1815 \newcommand\BeforeClearDocument[1]
1816 { \AtEndDocument{#1}
1817 \atveryend@DEPRECATED{BeforeClearDocument \tl_to_str:n{#1}}
1818 }
1819 % \end{macrocode}
1820 %
1821 % \begin{macrocode}
1822 \cs_new:Npn\atveryend@DEPRECATED #1
1823 {\iow_term:e{======~DEPRECATED~USAGE~#1~==========}}
1824 \ExplSyntaxOff
1825 % \end{macrocode}
1826 % \end{macro}
1827 %
1828 %
1829 % \begin{macrocode}
1830 %</atveryend-ltx>
1831 % \end{macrocode}
1832 %
1833 %
1834 %
1835 % \Finale
1836 %
1837 %
1838 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1839 \endinput
1840 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1841 %