Debian Codegraph @

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

raw
1 % \iffalse meta-comment
2 %
3 % Copyright (C) 2020-2026
4 % Frank Mittelbach, Phelype Oleinik, 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 %%% From File: lthooks.dtx
31 %
32 %<*driver>
33 % \fi
34 \ProvidesFile{lthooks.dtx}
35 [2026-03-13 v1.1o LaTeX Kernel (hooks)]
36 % \iffalse
37 %
38 \documentclass{l3in2edoc}
39 \GetFileInfo{lthooks.dtx}
40
41 \providecommand\InternalDetectionOff{}
42 \providecommand\InternalDetectionOn{}
43
44 \EnableCrossrefs
45 \CodelineIndex
46 \begin{document}
47 \DocInput{lthooks.dtx}
48 \end{document}
49 %</driver>
50 %
51 % \fi
52 %
53 %
54 % \providecommand\hook[1]{\texttt{#1}}
55 %
56 % \providecommand\fmi[1]{\marginpar{\footnotesize FMi: #1}}
57 % \providecommand\fmiinline[1]{\begin{quote}\itshape\footnotesize FMi: #1\end{quote}}
58 % \providecommand\pho[1]{\marginpar{\footnotesize PhO: #1}}
59 % \providecommand\phoinline[1]{\begin{quote}\itshape\footnotesize PhO: #1\end{quote}}
60 %
61 %
62 %
63 % \title{\LaTeX{}'s hook management\thanks{This module has version
64 % \fileversion\ dated \filedate, \copyright\ \LaTeX\
65 % Project.}}
66 %
67 % \author{Frank Mittelbach\thanks{Code improvements for speed and other goodies by Phelype Oleinik}}
68 %
69 % \maketitle
70 %
71 %
72 % \tableofcontents
73 %
74 % \section{Introduction}
75 %
76 % Hooks are points in the code of commands or environments where it
77 % is possible to add processing code into existing commands. This
78 % can be done by different packages that do not know about each
79 % other, and to allow for hopefully safe processing it is necessary
80 % to sort different chunks of code added by different packages into
81 % a suitable processing order.
82 %
83 % This is done by the packages adding chunks of code (via
84 % \cs{AddToHook}) and labeling their code with some label by
85 % default using the package name as a label.
86 %
87 % At \verb=\begin{document}= all code for a hook is then sorted
88 % according to some rules (given by \cs{DeclareHookRule}) for fast
89 % execution without processing overhead. If the hook code is
90 % modified afterwards (or the rules are changed),
91 % a new version for fast processing is generated.
92 %
93 % Some hooks are used already in the preamble of the document. If
94 % that happens then the hook is prepared for execution (and sorted)
95 % already at that point.
96 %
97 %
98 % \section{Package writer interface}
99 %
100 % The hook management system is offered as a set of CamelCase
101 % commands for traditional \LaTeXe{} packages (and for use in the
102 % document preamble if needed) as well as \texttt{expl3} commands
103 % for modern packages, that use the L3 programming layer of
104 % \LaTeX{}. Behind the scenes, a single set of data structures is
105 % accessed so that packages from both worlds can coexist and access
106 % hooks in other packages.
107 %
108 %
109 %
110 % \subsection{\LaTeXe\ interfaces}
111 %
112 % \subsubsection{Declaring hooks}
113 %
114 % With a few exceptions, hooks have to be declared before they can
115 % be used. The exceptions are the generic hooks for commands and
116 % environments (executed at \cs{begin} and \cs{end}), and the generic
117 % hooks run when loading files (see section~\ref{sec:generic}).
118 %
119 % \begin{function}{\NewHook}
120 % \begin{syntax}
121 % \cs{NewHook} \Arg{hook}
122 % \end{syntax}
123 % Creates a new \meta{hook}.
124 % If this hook is declared within a package it is suggested
125 % that its name is always structured as follows:
126 % \meta{package-name}\texttt{/}\meta{hook-name}. If necessary you
127 % can further subdivide the name by adding more \texttt{/} parts.
128 % If a hook name is already taken, an error is raised and the hook
129 % is not created.
130 %
131 % The \meta{hook} can be specified using the dot-syntax to denote
132 % the current package name. See section~\ref{sec:default-label}.
133 % The string \texttt{??} can't be used as a hook name because it
134 % has a special significance as a placeholder in hook rules.
135 %
136 % \end{function}
137 %
138 % \begin{function}{\NewReversedHook}
139 % \begin{syntax}
140 % \cs{NewReversedHook} \Arg{hook}
141 % \end{syntax}
142 % Like \cs{NewHook} declares a new \meta{hook}.
143 % the difference is that the code chunks for this hook are in
144 % reverse order by default (those added last are executed first).
145 % Any rules for the hook are applied after the default ordering.
146 % See sections~\ref{sec:order} and \ref{sec:reversed-order}
147 % for further details.
148 %
149 % The \meta{hook} can be specified using the dot-syntax to denote
150 % the current package name. See section~\ref{sec:default-label}.
151 % \end{function}
152 %
153 % \begin{function}{\NewMirroredHookPair}
154 % \begin{syntax}
155 % \cs{NewMirroredHookPair} \Arg{hook-1} \Arg{hook-2}
156 % \end{syntax}
157 % A shorthand for
158 % \cs{NewHook}\Arg{hook-1}\cs{NewReversedHook}\Arg{hook-2}.
159 %
160 % The \meta{hook} can be specified using the dot-syntax to denote
161 % the current package name. See section~\ref{sec:default-label}.
162 % \end{function}
163 %
164 %
165 % \begin{function}{\NewHookWithArguments}
166 % \begin{syntax}
167 % \cs{NewHookWithArguments} \Arg{hook} \Arg{number}
168 % \end{syntax}
169 % Creates a new \meta{hook} whose code takes \meta{number} arguments,
170 % and otherwise works exactly like \cs{NewHook}.
171 % Section~\ref{sec:hook-args} explains hooks with arguments.
172 %
173 % The \meta{hook} can be specified using the dot-syntax to denote
174 % the current package name. See section~\ref{sec:default-label}.
175 % \end{function}
176 %
177 % \begin{function}{\NewReversedHookWithArguments}
178 % \begin{syntax}
179 % \cs{NewReversedHookWithArguments} \Arg{hook} \Arg{number}
180 % \end{syntax}
181 % Like \cs{NewReversedHook}, but creates a hook whose code takes
182 % \meta{number} arguments.
183 % Section~\ref{sec:hook-args} explains hooks with arguments.
184 %
185 % The \meta{hook} can be specified using the dot-syntax to denote
186 % the current package name. See section~\ref{sec:default-label}.
187 % \end{function}
188 %
189 % \begin{function}{\NewMirroredHookPairWithArguments}
190 % \begin{syntax}
191 % \cs{NewMirroredHookPairWithArguments} \Arg{hook-1} \Arg{hook-2} \Arg{number}
192 % \end{syntax}
193 % A shorthand for
194 % \cs{NewHookWithArguments}\Arg{hook-1}\Arg{number}\\
195 % \cs{NewReversedHookWithArguments}\Arg{hook-2}\Arg{number}.
196 % Section~\ref{sec:hook-args} explains hooks with arguments.
197 %
198 % The \meta{hook} can be specified using the dot-syntax to denote
199 % the current package name. See section~\ref{sec:default-label}.
200 % \end{function}
201 %
202 %
203 % \subsubsection{Special declarations for generic hooks}
204 %
205 % The declarations here should normally not be used. They are available
206 % to provide support for special use cases mainly involving
207 % generic command hooks.
208 %
209 % \changes{v1.0p}{2021/08/20}{Documentation updates for generic hook commands (gh/638)}
210 %
211 % \begin{function}{\DisableGenericHook}
212 % \begin{syntax}
213 % \cs{DisableGenericHook} \Arg{hook}
214 % \end{syntax}
215 % After this declaration\footnotemark{} the \meta{hook} is no longer
216 % usable: Any further attempt to add code to it will result in an
217 % error and any use, e.g., via \cs{UseHook}, will simply do nothing.
218 %
219 % This is intended to be used with generic command hooks (see
220 % \texttt{ltcmdhooks-doc}) as depending on the definition of the
221 % command such generic hooks may be unusable. If that is known, a
222 % package developer can disable such hooks up front.
223 %
224 % The \meta{hook} can be specified using the dot-syntax to denote
225 % the current package name. See section~\ref{sec:default-label}.
226 % \end{function}\footnotetext{In the 2020/06 release this command was
227 % called \cs{DisableHook}, but that name was misleading as it
228 % shouldn't be used to disable non-generic hooks.}
229 %
230 %
231 % \begin{function}{\ActivateGenericHook}
232 % \begin{syntax}
233 % \cs{ActivateGenericHook} \Arg{hook}
234 % \end{syntax}
235 % This declaration activates a generic hook provided by a package/class
236 % (e.g., one used in code with \cs{UseHook} or
237 % \cs{UseOneTimeHook}) without it being explicitly declared with
238 % \cs{NewHook}).
239 % If the hook is already activated, this command does nothing.
240 %
241 % Note that this command does not undo the effect of \cs{DisableGenericHook}.
242 % See section~\ref{sec:generic-hooks} for a discussion of when this
243 % declaration is appropriate.
244 % \end{function}
245 %
246 %
247 %
248 %
249 %
250 % \subsubsection{Using hooks in code}
251 %
252 % Using a hook that is executing the code that has been associated
253 % with it is only allowed if the hook has been previously declared
254 % with \cs{NewHook}. For performance reason there are no runtime
255 % checks for this and it is the responsibility of the programmer of a
256 % package to ensure that all hooks that are used in a package (with
257 % one of the commands in this section) are declared first.
258 %
259 % \begin{function}{\UseHook}
260 % \begin{syntax}
261 % \cs{UseHook} \Arg{hook}
262 % \end{syntax}
263 % Execute the code stored in the \meta{hook}.
264 %
265 % Before \verb=\begin{document}= the fast execution code for a hook
266 % is not set up, so in order to use a hook there it is explicitly
267 % initialized first. As that involves assignments using a hook at
268 % those times is not 100\% the same as using it after
269 % \verb=\begin{document}=.
270 %
271 % The \meta{hook} \emph{cannot} be specified using the dot-syntax.
272 % A leading |.| is treated literally.
273 % \end{function}
274 %
275 % \begin{function}{\UseHookWithArguments}
276 % \begin{syntax}
277 % \cs{UseHookWithArguments} \Arg{hook} \Arg{number} \Arg{arg_1} \ldots \Arg{arg_n}
278 % \end{syntax}
279 % Execute the code stored in the \meta{hook} and pass the arguments
280 % \Arg{arg_1} through \Arg{arg_n} to the \meta{hook}. Otherwise, it
281 % works exactly like \cs{UseHook}.
282 % The \meta{number} should be the number of arguments declared for
283 % the hook. If the hook is not declared, this command does nothing
284 % and it will remove \meta{number} items from the input.
285 % Section~\ref{sec:hook-args} explains hooks with arguments.
286 %
287 % The \meta{hook} \emph{cannot} be specified using the dot-syntax.
288 % A leading |.| is treated literally.
289 % \end{function}
290 %
291 % \begin{function}{\UseOneTimeHook}
292 % \begin{syntax}
293 % \cs{UseOneTimeHook} \Arg{hook}
294 % \end{syntax}
295 % Some hooks are only used (and can be only used) in one place, for
296 % example, those in \verb=\begin{document}= or
297 % \verb=\end{document}=. From that point onwards, adding to the hook
298 % through a defined \cs[no-index]{\meta{addto-cmd}} command (e.g.,
299 % \cs{AddToHook} or \cs{AtBeginDocument}, etc.\@) would have no
300 % effect (as would the use of such a command inside the hook code
301 % itself). It is therefore customary to redefine
302 % \cs{\meta{addto-cmd}} to simply process its argument, i.e.,
303 % essentially make it behave like \cs{@firstofone}.
304 %
305 % \cs{UseOneTimeHook} does that: it records that the hook has been
306 % consumed and any further attempt to add to it will result in
307 % executing the code to be added immediately.
308 %
309 % Using \cs{UseOneTimeHook} several times with the same
310 % \Arg{hook} means that it only executes the first time it is used.
311 % For example, if it is used in a command that can be called several times
312 % then the hook executes during only the \emph{first} invocation of that
313 % command; this allows its use as an \enquote{initialization hook}.
314 %
315 % Mixing \cs{UseHook} and \cs{UseOneTimeHook} for the same
316 % \Arg{hook} should be avoided, but if this is done then neither will execute
317 % after the first \cs{UseOneTimeHook}.
318 %
319 % The \meta{hook} \emph{cannot} be specified using the dot-syntax.
320 % A leading |.| is treated literally. See
321 % section~\ref{sec:default-label} for details.
322 %
323 % \end{function}
324 %
325 % \begin{function}{\UseOneTimeHookWithArguments}
326 % \begin{syntax}
327 % \cs{UseOneTimeHookWithArguments} \Arg{hook} \Arg{number} \Arg{arg_1} \ldots \Arg{arg_n}
328 % \end{syntax}
329 % Works exactly like \cs{UseOneTimeHook}, but passes arguments
330 % \Arg{arg_1} through \Arg{arg_n} to the \meta{hook}.
331 % The \meta{number} should be the number of arguments declared for
332 % the hook. If the hook is not declared, this command does nothing
333 % and it will remove \meta{number} items from the input.
334 %
335 % It should be noted that after a one-time hook is used, it is no
336 % longer possible to use \cs{AddToHookWithArguments} or similar with
337 % that hook. \cs{AddToHook} continues to work as normal.
338 % Section~\ref{sec:hook-args} explains hooks with arguments.
339 %
340 % The \meta{hook} \emph{cannot} be specified using the dot-syntax.
341 % A leading |.| is treated literally. See
342 % section~\ref{sec:default-label} for details.
343 %
344 % \end{function}
345 %
346 %
347 % \subsubsection{Updating code for hooks}
348 %
349 % In contrast to the commands from the previous section, declarations
350 % such as \cs{AddToHook} or \cs{DeclareHookRule} can be
351 % used even when the hook is not yet declared. The rationale is that
352 % the hook declaration may be in some package that is loaded later, or
353 % perhaps not loaded at all.
354 %
355 % A side effect of this design is that misspellings do not raise an
356 % error but are simply regarded as declarations for hooks with a
357 % different name.
358 %
359 % \begin{function}{\AddToHook}
360 % \begin{syntax}
361 % \cs{AddToHook} \Arg{hook} \oarg{label} \Arg{code}
362 % \end{syntax}
363 % Adds \meta{code} to the \meta{hook} labeled by \meta{label}.
364 % When the optional argument \meta{label} is not provided, the
365 % \meta{default label} is used (see section~\ref{sec:default-label}).
366 % If \cs{AddToHook} is used in a package/class, the
367 % \meta{default label} is the package/class name, otherwise it is
368 % \hook{top-level} (the \hook{top-level} label is treated
369 % differently: see section~\ref{sec:top-level}).
370 %
371 % If there already exists code under the \meta{label} then the new
372 % \meta{code} is appended to the existing one (even if this is a reversed hook).
373 % If you want to replace existing code under the
374 % \meta{label}, first apply \cs{RemoveFromHook}.
375 %
376 % The hook doesn't have to exist for code to be added to
377 % it. However, if it is not declared, then obviously the
378 % added \meta{code} will never be executed. This
379 % allows for hooks to work regardless of package loading order and
380 % enables packages to add to hooks from other packages without
381 % worrying whether they are actually used in the current document.
382 % See section~\ref{sec:querying}.
383 %
384 % The \meta{hook} and \meta{label} can be specified using the
385 % dot-syntax to denote the current package name.
386 % See section~\ref{sec:default-label}.
387 % \end{function}
388 %
389 % \begin{function}{\AddToHookWithArguments}
390 % \begin{syntax}
391 % \cs{AddToHookWithArguments} \Arg{hook} \oarg{label} \Arg{code}
392 % \end{syntax}
393 % Works exactly like \cs{AddToHook}, except that the \meta{code} can
394 % access the arguments passed to the hook using \verb|#1|, \verb|#2|,
395 % \ldots, \verb|#n| (up to the number of arguments declared for the
396 % hook). If the \meta{code} should contain \emph{parameter tokens}
397 % (\verb|#|) that are not supposed to be understood as the arguments
398 % of the hook, such tokens should be doubled. For example, with
399 % \cs{AddToHook} one can write:
400 %\begin{verbatim}
401 % \AddToHook{myhook}{\def\foo#1{Hello, #1!}}
402 %\end{verbatim}
403 % but to achieve the same with \cs{AddToHookWithArguments}, one should
404 % write:
405 %\begin{verbatim}
406 % \AddToHookWithArguments{myhook}{\def\foo##1{Hello, ##1!}}
407 %\end{verbatim}
408 % because in the latter case, \verb|#1| refers to the first argument
409 % of the hook \hook{myhook}.
410 % Section~\ref{sec:hook-args} explains hooks with arguments.
411 %
412 % The \meta{hook} and \meta{label} can be specified using the
413 % dot-syntax to denote the current package name.
414 % See section~\ref{sec:default-label}.
415 % \end{function}
416 %
417 % \begin{function}{\RemoveFromHook}
418 % \begin{syntax}
419 % \cs{RemoveFromHook} \Arg{hook} \oarg{label}
420 % \end{syntax}
421 % Removes any code labeled by \meta{label} from the \meta{hook}.
422 % When the optional argument \meta{label} is not provided, the
423 % \meta{default label} is used (see section~\ref{sec:default-label}).
424 % \end{function}
425 %
426 % If there is no code under the \meta{label} in the \meta{hook},
427 % or if the \meta{hook} does not exist, a warning is issued when
428 % you attempt to \cs{RemoveFromHook}, and the command is ignored.
429 % \cs{RemoveFromHook}\marginpar{\raggedleft\rightskip5pt\itshape
430 % \textbf{Important:}\break The \cs{RemoveFromHook} command should be
431 % only used if one has full control over the code chunk to be removed.
432 % In particular it should not be used to remove code chunks from other
433 % packages! For this the \texttt{voids} relation is provided.}
434 % should be used only when you know exactly what
435 % labels are in a hook. Typically this will be when some code gets added to a hook
436 % by a package, then later this code is removed by that same package.
437 % If you want to prevent the execution of code from another
438 % package, use the |voids| rule instead (see section~\ref{sec:rules}).
439 %
440 % If the optional \meta{label} argument is \texttt{*}, then all code chunks are
441 % removed. This is rather dangerous as it may well drop code from other
442 % packages (that one may not know about); it should therefore not be used
443 % in packages but only in document preambles!
444 %
445 % The \meta{hook} and \meta{label} can be specified using the
446 % dot-syntax to denote the current package name.
447 % See section~\ref{sec:default-label}.
448 %
449 % In contrast to the \texttt {voids} relationship between two labels
450 % in a \cs{DeclareHookRule} this is a destructive operation as the
451 % labeled code is removed from the hook data structure, whereas the
452 % relationship setting can be undone by providing a different
453 % relationship later.
454 %
455 % \medskip
456 %
457 % A useful application for this declaration inside the document body
458 % is when one wants to temporarily add code to hooks and later remove
459 % it again, e.g.,
460 % \changes{v1.1l}{2025/01/03}{Correct example to use \texttt{env/quote/begin}
461 % instead of \texttt{/before} (gh/1599)}
462 %\begin{verbatim}
463 % \AddToHook{env/quote/begin}{\small}
464 % \begin{quote}
465 % A quote set in a smaller typeface
466 % \end{quote}
467 % ...
468 % \RemoveFromHook{env/quote/begin}
469 % ... now back to normal for further quotes
470 %\end{verbatim}
471 % Note that you can't cancel the setting with
472 %\begin{verbatim}
473 % \AddToHook{env/quote/begin}{}
474 %\end{verbatim}
475 % because that only \enquote{adds} a further empty chunk of code to
476 % the hook. Adding \cs{normalsize} would work but that means the hook
477 % then contained \cs{small}\cs{normalsize} which means two font size
478 % changes for no good reason.
479 %
480 % The above is only needed if one wants to typeset several quotes in a
481 % smaller typeface. If the hook is only needed once then
482 % \cs{AddToHookNext} is simpler, because it resets itself after one use.
483 %
484 %
485 % \begin{function}{\AddToHookNext}
486 % \begin{syntax}
487 % \cs{AddToHookNext} \Arg{hook} \Arg{code}
488 % \end{syntax}
489 % Adds \meta{code} to the next invocation of the \meta{hook}.
490 % The code is executed after the normal hook code has finished and
491 % it is executed only once, i.e. it is deleted after it was used.
492 %
493 % Using this declaration is a global operation, i.e., the code is
494 % not lost even if the declaration is used inside a group and the
495 % next invocation of the hook happens after the end of that group.
496 % If the declaration is
497 % used several times before the hook is executed then all code is
498 % executed in the order in which it was declared.\footnotemark
499 %
500 % If this declaration is used with a one-time hook then the code
501 % is only ever used if the declaration comes before the hook’s
502 % invocation. This is because, in contrast
503 % to \cs{AddToHook}, the code in this declaration is not
504 % executed immediately in the case when the invocation of the hook
505 % has already happened---in other words, this code will truly execute
506 % only on the next invocation of the hook (and in the case of a
507 % one-time hook there is no such \enquote{next invocation}).
508 % This gives you a choice: should my code execute
509 % always, or should it execute only at the point where the
510 % one-time hook is used (and not at all if this is impossible)? For
511 % both of these possibilities there are use cases.
512 %
513 % It is possible to nest this declaration using the same hook (or
514 % different hooks): e.g.,
515 % \begin{quote}
516 % \cs{AddToHookNext}\Arg{hook}\verb={=\meta{code-1}^^A
517 % \cs{AddToHookNext}\Arg{hook}\Arg{code-2}\verb=}=
518 % \end{quote}
519 % will execute \meta{code-1} next time the \meta{hook} is used and at
520 % that point puts \meta{code-2} into the \meta{hook} so that it gets
521 % executed on following time the hook is run.
522 %
523 % A hook doesn't have to exist for code to be added to it. This
524 % allows for hooks to work regardless of package loading
525 % order.
526 % See section~\ref{sec:querying}.
527 %
528 % The \meta{hook} can be specified using the dot-syntax to denote
529 % the current package name. See section~\ref{sec:default-label}.
530 % \end{function}\footnotetext{There is
531 % no mechanism to reorder such code chunks (or delete them).}
532 %
533 % \begin{function}{\AddToHookNextWithArguments}
534 % \begin{syntax}
535 % \cs{AddToHookNextWithArguments} \Arg{hook} \Arg{code}
536 % \end{syntax}
537 % Works exactly like \cs{AddToHookNext}, but the \meta{code} can
538 % contain references to the arguments of the \meta{hook} as described
539 % for \cs{AddToHookWithArguments} above.
540 % Section~\ref{sec:hook-args} explains hooks with arguments.
541 %
542 % The \meta{hook} can be specified using the dot-syntax to denote
543 % the current package name. See section~\ref{sec:default-label}.
544 % \end{function}
545 %
546 %
547 %
548 % \begin{function}{\ClearHookNext}
549 % \begin{syntax}
550 % \cs{ClearHookNext} \Arg{hook}
551 % \end{syntax}
552 % Normally \cs{AddToHookNext} is only used when you know precisely
553 % where it will apply and why you want some extra code at that
554 % point. However, there are a few use cases in which such a
555 % declaration needs to be canceled, for example, when
556 % discarding a page with \cs{DiscardShipoutBox} (but even then not
557 % always), and in such situations \cs{ClearHookNext} can be
558 % used.
559 % \end{function}
560 %
561 %
562 %
563 %
564 % \subsubsection{Hook names and default labels}
565 % \label{sec:default-label}
566 %
567 % It is best practice to use \cs{AddToHook} in packages or classes
568 % \emph{without specifying a \meta{label}} because then the package
569 % or class name is automatically used, which is helpful if rules are
570 % needed, and avoids mistyping the \meta{label}.
571 %
572 % Using an explicit \meta{label} is only necessary in very specific
573 % situations, e.g., if you want to add several chunks of code into a
574 % single hook and have them placed in different parts of the hook
575 % (by providing some rules).
576 %
577 % The other case is when you develop a larger package with several
578 % sub-packages. In that case you may want to use the same
579 % \meta{label} throughout the sub-packages in order to avoid
580 % that the labels change if you internally reorganize your code.
581 %
582 % Except for \cs{UseHook}, \cs{UseOneTimeHook} and \cs{IfHookEmptyTF}
583 % (and their \pkg{expl3} interfaces \cs{hook_use:n},
584 % \cs{hook_use_once:n} and \cs{hook_if_empty:nTF}), all \meta{hook}
585 % and \meta{label} arguments are processed in the same way: first,
586 % spaces are trimmed around the argument, then it is fully expanded
587 % until only character tokens remain. If the full expansion of the
588 % \meta{hook} or \meta{label} contains a non-expandable non-character
589 % token, a low-level \TeX{} error is raised (namely, the \meta{hook} is
590 % expanded using \TeX's \cs{csname}\ldots\cs{endcsname}, as such,
591 % Unicode characters are allowed in \meta{hook} and \meta{label}
592 % arguments). The arguments of \cs{UseHook}, \cs{UseOneTimeHook}, and
593 % \cs{IfHookEmptyTF} are
594 % processed much in the same way except that spaces are not trimmed
595 % around the argument, for better performance.
596 %
597 % It is not enforced, but highly recommended that the hooks defined by
598 % a package, and the \meta{labels} used to add code to other hooks
599 % contain the package name to easily identify the source of the code
600 % chunk and to prevent clashes. This should be the standard practice,
601 % so this hook management code provides a shortcut to refer to the
602 % current package in the name of a \meta{hook} and in a \meta{label}.
603 % If the \meta{hook} name or the \meta{label} consist just of a single dot
604 % (|.|), or starts with a dot followed by a slash (|./|) then the dot
605 % denotes the \meta{default label} (usually the current package or class
606 % name---see~\cs{SetDefaultHookLabel}).
607 % A \enquote{|.|} or \enquote{|./|} anywhere else in a \meta{hook} or in
608 % \meta{label} is treated literally and is not replaced.
609 %
610 % For example,
611 % inside the package \texttt{mypackage.sty}, the default label is
612 % \texttt{mypackage}, so the instructions:
613 % \begin{verbatim}
614 % \NewHook {./hook}
615 % \AddToHook {./hook}[.]{code} % Same as \AddToHook{./hook}{code}
616 % \AddToHook {./hook}[./sub]{code}
617 % \DeclareHookRule{begindocument}{.}{before}{babel}
618 % \AddToHook {file/foo.tex/after}{code}
619 % \end{verbatim}
620 % are equivalent to:
621 % \begin{verbatim}
622 % \NewHook {mypackage/hook}
623 % \AddToHook {mypackage/hook}[mypackage]{code}
624 % \AddToHook {mypackage/hook}[mypackage/sub]{code}
625 % \DeclareHookRule{begindocument}{mypackage}{before}{babel}
626 % \AddToHook {file/foo.tex/after}{code} % unchanged
627 % \end{verbatim}
628 %
629 % The \meta{default label} is automatically set equal to the name of the
630 % current package or class at the time the package is loaded. If the
631 % hook command is used outside of a package, or the current file wasn't
632 % loaded with \cs{usepackage} or \cs{documentclass}, then the
633 % \texttt{top-level} is used as the \meta{default label}. This may have
634 % exceptions---see \cs{PushDefaultHookLabel}.
635 %
636 % This syntax is available in all \meta{label} arguments and most
637 % \meta{hook} arguments, both in the \LaTeXe{} interface, and the \LaTeX3
638 % interface described in section~\ref{sec:l3hook-interface}.
639 %
640 % Note,\marginpar{\raggedleft\rightskip5pt\itshape \textbf{Important:}\break The dot-syntax
641 % is \textbf{not} available with
642 % \cs{UseHook} and some other commands that are typically used within code!}
643 % however, that the replacement of |.| by the \meta{default label}
644 % takes place when the hook command is executed, so actions that are
645 % somehow executed after the package ends will have the wrong
646 % \meta{default label} if the dot-syntax is used. For that reason,
647 % this syntax is not available in \cs{UseHook} (and \cs{hook_use:n})
648 % because the hook is most of the time used outside of the package file
649 % in which it was defined. This syntax is also not available in the hook
650 % conditionals \cs{IfHookEmptyTF} (and \cs{hook_if_empty:nTF}), because these
651 % conditionals are used in some performance-critical parts of the hook
652 % management code, and because they are usually used to refer to other
653 % package's hooks, so the dot-syntax doesn't make much sense.
654 %
655 % In some cases, for example in large packages, one may want to separate
656 % the code in logical parts, but still use the main package name as the
657 % \meta{label}, then the \meta{default label} can be set using
658 % \cs{PushDefaultHookLabel}\verb={...}=\,\ldots\cs{PopDefaultHookLabel}
659 % or \cs{SetDefaultHookLabel}\verb={...}=.
660 %
661 % \begin{function}{\PushDefaultHookLabel,\PopDefaultHookLabel}
662 % \begin{syntax}
663 % \cs{PushDefaultHookLabel} \Arg{default label}
664 % \quad \meta{code}
665 % \cs{PopDefaultHookLabel}
666 % \end{syntax}
667 % \cs{PushDefaultHookLabel} sets the current \meta{default label} to
668 % be used in \meta{label} arguments, or when replacing a leading
669 % ``|.|'' (see above). \cs{PopDefaultHookLabel} reverts the
670 % \meta{default label} to its previous value.
671 %
672 % Inside a package or class, the \meta{default label} is equal to the
673 % package or class name, unless explicitly changed. Everywhere else,
674 % the \meta{default label} is |top-level| (see
675 % section~\ref{sec:top-level}) unless explicitly changed.
676 %
677 % The effect of \cs{PushDefaultHookLabel} holds until the next
678 % \cs{PopDefaultHookLabel}. \cs{usepackage} (and \cs{RequirePackage}
679 % and \cs{documentclass}) internally use
680 % \begin{quote}
681 % \cs{PushDefaultHookLabel}\Arg{package name} \\
682 % \null \quad \meta{package code} \\
683 % \cs{PopDefaultHookLabel}
684 % \end{quote}
685 % to set the \meta{default label} for the package or class file.
686 % Inside the \meta{package code} the \meta{default label} can also be
687 % changed with \cs{SetDefaultHookLabel}. \cs{input} and other
688 % file input-related commands from the \LaTeX{} kernel do not use
689 % \cs{PushDefaultHookLabel}, so code within files loaded by these
690 % commands does \emph{not} get a dedicated \meta{label}! (that is, the
691 % \meta{default label} is the current active one when the file was
692 % loaded.)
693 %
694 % Packages that provide their own package-like interfaces
695 % (Ti\textit{k}Z's \cs{usetikzlibrary}, for example) can use
696 % \cs{PushDefaultHookLabel} and \cs{PopDefaultHookLabel} to set
697 % dedicated labels and to emulate \cs{usepackage}-like hook behavior
698 % within those contexts.
699 %
700 % The |top-level| label is treated differently, and is reserved to the
701 % user document, so it is not allowed to change the
702 % \meta{default label} to |top-level|.
703 % \end{function}
704 %
705 % \begin{function}{\SetDefaultHookLabel}
706 % \begin{syntax}
707 % \cs{SetDefaultHookLabel} \Arg{default label}
708 % \end{syntax}
709 % Similarly to \cs{PushDefaultHookLabel},
710 % sets the current \meta{default label} to
711 % be used in \meta{label} arguments, or when replacing a leading
712 % ``|.|''. The effect holds until the label is changed again or until
713 % the next \cs{PopDefaultHookLabel}. The difference between
714 % \cs{PushDefaultHookLabel} and \cs{SetDefaultHookLabel} is that the
715 % latter does not save the current \meta{default label}.
716 %
717 % This command is useful when a large package is composed of several
718 % smaller packages, but all should have the same \meta{label}, so
719 % \cs{SetDefaultHookLabel} can be used at the beginning of each
720 % package file to set the correct label.
721 %
722 % \cs{SetDefaultHookLabel} is not allowed in the main document, where
723 % the \meta{default label} is |top-level| and there is no
724 % \cs{PopDefaultHookLabel} to end its effect.
725 % It is also not allowed to change the \meta{default label} to
726 % |top-level|.
727 % \end{function}
728 %
729 % \subsubsection{The \texttt{top-level} label}
730 % \label{sec:top-level}
731 %
732 % The |top-level| label, assigned to code added from the main document,
733 % is different from other labels. Code added to hooks (usually
734 % \cs{AtBeginDocument}) in the preamble is almost always to change
735 % something defined by a package, so it should go at the very end of the
736 % hook.
737 %
738 % Therefore, code added in the |top-level| is always executed at the end
739 % of the hook, regardless of where it was declared. If the hook is
740 % reversed (see \cs{NewReversedHook}), the |top-level| chunk is executed
741 % at the very beginning instead.
742 %
743 % Rules regarding |top-level| have no effect: if a user wants to have a
744 % specific set of rules for a code chunk, they should use a different
745 % label to said code chunk, and provide a rule for that label instead.
746 %
747 % The |top-level| label is exclusive for the user, so trying to add code
748 % with that label from a package results in an error.
749 %
750 % \subsubsection{Defining relations between hook code}
751 % \label{sec:rules}
752 %
753 % The default assumption is that code added to hooks by different
754 % packages are independent and the order in which they are executed is
755 % irrelevant. While this is true in many cases it is obviously false
756 % in others.
757 %
758 % Before the hook management system was introduced
759 % packages had to take elaborate precautions to determine whether some other
760 % package had also been loaded (before or after) and then to find some ways to
761 % alter its behavior accordingly. In addition is was often the user's
762 % responsibility to load packages in the right order so that alterations
763 % made by packages were done in thsat same order; and in some cases even
764 % altering the loading order wouldn't resolve the conflicts.
765 %
766 % With the new hook management system it is now possible to define
767 % rules (i.e., relationships) between code chunks added by different
768 % packages and to specify explicitly the order in which they should be
769 % processed.
770 %
771 % The rules can be declared for hooks before the hook has been
772 % declared with \cs{NewHook} and they are allowed to refer to code
773 % labels that do not yet exist, e.g., because a package defining the
774 % code chunk with that label has not yet been loaded.
775 % When the hook code is finally sorted for fast execution, all rules that apply are acted on
776 % and the others are ignored.
777 %
778 % This offers the flexibility needed to handle complicated
779 % relationships between code from different packages and to set this up
780 % beforehand in a way that is independent of whether or not the packages are actually loaded in a
781 % specific document. The downside of this is that misspellings of
782 % hook names or code labels will not raise any error, instead the rule
783 % will simply never apply!
784 %
785 % \begin{function}{\DeclareHookRule}
786 % \begin{syntax}
787 % \cs{DeclareHookRule} \Arg{hook} \Arg{label1} \Arg{relation} \Arg{label2}
788 % \end{syntax}
789 % Defines a relation between \meta{label1} and \meta{label2} for a
790 % given \meta{hook}. If \meta{hook} is \texttt{??} this defines a default
791 % relation for all hooks that use the two labels, i.e., that have
792 % chunks of code labeled with \meta{label1} and \meta{label2}.
793 %
794 % Currently, the supported relations are the following:
795 % \begin{itemize}
796 %
797 % \item[\texttt{before} or \texttt{\string<}]
798 %
799 % Code for \meta{label1} comes before code for \meta{label2}.
800 %
801 % \item[\texttt{after} or \texttt{\string>}]
802 % Code for \meta{label1} comes after code for \meta{label2}.
803 %
804 % \item[\texttt{incompatible-warning}]
805 %
806 % Only code for either \meta{label1} or \meta{label2} can appear
807 % for that hook (a way to say that two packages---or parts of
808 % them---are incompatible). A warning is raised if both labels
809 % appear in the same hook.
810 %
811 % \item[\texttt{incompatible-error}]
812 %
813 % Like \texttt{incompatible-error} but instead of a warning a
814 % \LaTeX{} error is raised, and the code for both labels are
815 % dropped from that hook until the conflict is resolved.
816 %
817 % \item[\texttt{voids}]
818 %
819 % Code for \meta{label1} overwrites code for \meta{label2}. More
820 % precisely, code for \meta{label2} is dropped for that
821 % hook. This can be used, for example if one package is a
822 % superset in functionality of another one and therefore wants to
823 % undo code in some hook and replace it with its own version.
824 %
825 % \item[\texttt{unrelated}]
826 %
827 % The order of code for \meta{label1} and \meta{label2} is
828 % irrelevant. This rule is there to undo an incorrect rule
829 % specified earlier.
830 %
831 % \end{itemize}
832 % There can only be a single relation between two labels for a
833 % given hook, i.e., a later \cs{DeclareHookRule} overwrites any
834 % previous declaration. In all cases rules specific to a given
835 % hook take precedence over default rules that use \texttt{??} as
836 % the \meta{hook}.
837 %
838 % If a default rule is applied, it is done before reversing the
839 % label order in a reversed hook, e.g., \texttt{before} in a
840 % default rule effectively becomes \texttt{after} in such a hook.
841 % In contrast, a rule for a specific hook is always applied to the
842 % state after any reversal (i.e., the state you see when using
843 % \cs{ShowHook} on that hook).
844 %
845 % The \meta{hook} and \meta{label} can be specified using the
846 % dot-syntax to denote the current package name.
847 % See section~\ref{sec:default-label}.
848 %
849 % \end{function}
850 %
851 %
852 % \begin{function}{\ClearHookRule}
853 % \begin{syntax}
854 % \cs{ClearHookRule} \Arg{hook} \Arg{label1} \Arg{label2}
855 % \end{syntax}
856 % Syntactic sugar for saying that \meta{label1} and \meta{label2}
857 % are unrelated for the given \meta{hook}.
858 % \end{function}
859 %
860 %
861 %
862 % \begin{function}{\DeclareDefaultHookRule}
863 % \begin{syntax}
864 % \cs{DeclareDefaultHookRule} \Arg{label1} \Arg{relation} \Arg{label2}
865 % \end{syntax}
866 % This sets up a relation between \meta{label1} and \meta{label2}
867 % for all hooks unless overwritten by a specific rule for a hook.
868 % Useful for cases where one package has a specific relation to
869 % some other package, e.g., is \texttt{incompatible} or always
870 % needs a special ordering \texttt{before} or \texttt{after}.
871 % (Technically it is just a shorthand for using \cs{DeclareHookRule}
872 % with \texttt{??} as the hook name.)
873 %
874 % If such a rule is applied to a reversed
875 % hook it behaves as if the rule is reversed (e.g.,
876 % \texttt{after} becomes \texttt{before})
877 % because those rules are applied first and then the order is reversed.
878 % The rationale is that in hook pairs (in which the ordering in one
879 % is reversed) default rules have to be reversed too in nearly all
880 % scenarios. If this is not the case, a default rule can't be used
881 % or has to be overwritten with an explicit \cs{DeclareHookRule}
882 % for that specific hook.
883 %
884 % Declaring default rules is only supported in the document
885 % preamble.\footnotemark{}
886 %
887 % The \meta{label} can be specified using the dot-syntax to denote
888 % the current package name. See section~\ref{sec:default-label}.
889 % \end{function}\footnotetext{Trying to do so, e.g., via
890 % \cs{DeclareHookRule} with \texttt{??} has bad side-effects and
891 % is not supported (though not explicitly caught for performance
892 % reasons).}
893 %
894 %
895 %
896 % \subsubsection{Querying hooks}
897 % \label{sec:querying}
898 %
899 % Simpler data types, like token lists, have three possible states; they
900 % can:
901 % \begin{itemize}
902 % \item exist and be empty;
903 % \item exist and be non-empty; and
904 % \item not exist (in which case emptiness doesn't apply);
905 % \end{itemize}
906 % Hooks are a bit more complicated:
907 % a hook may exist or not, and independently it may or may not be empty.
908 % This means that even a hook that doesn't exist may be non-empty and
909 % it can also be disabled.
910 %
911 % This seemingly strange state may happen when, for example, package~$A$
912 % defines hook \hook{A/foo}, and package $B$ adds some code to that
913 % hook. However, a document may load package $B$ before package $A$, or
914 % may not load package $A$ at all. In both cases some code is added to
915 % hook \hook{A/foo} without that hook being defined yet, thus that
916 % hook is said to be non-empty, whereas it doesn't exist. Therefore,
917 % querying the existence of a hook doesn't imply its emptiness, neither
918 % does the other way around.
919 %
920 % Given that code or rules can be added to a hook even if it doesn't
921 % physically exist yet, means that a querying its existence has no
922 % real use case (in contrast to other variables that can only be
923 % update if they have already been declared). For that reason only the
924 % test for emptiness has a public interface.
925 %
926 % A hook is said to be empty when no code was added to it, either to
927 % its permanent code pool, or to its ``next'' token list. The hook
928 % doesn't need to be declared to have code added to its code pool.
929 % A hook is said to exist when it was declared with \cs{NewHook} or
930 % some variant thereof. Generic hooks such as \hook{file} and \hook{env} hooks are
931 % automatically declared when code is added to them.
932 %
933 % \begin{function}[EXP]{\IfHookEmptyTF,\IfHookEmptyT,\IfHookEmptyF}
934 % \begin{syntax}
935 % \cs{IfHookEmptyTF} \Arg{hook} \Arg{true code} \Arg{false code}
936 % \end{syntax}
937 % Tests if the \meta{hook} is empty (\emph{i.e.}, no code was added to
938 % it using either \cs{AddToHook} or \cs{AddToHookNext}) or such code
939 % was removed again (via \cs{RemoveFromHook}), and
940 % branches to either \meta{true code} or \meta{false code} depending
941 % on the result.
942 %
943 % The \meta{hook} \emph{cannot} be specified using the dot-syntax.
944 % A leading |.| is treated literally.
945 % \end{function}
946 %
947 %
948 %
949 % \subsubsection{Displaying hook code}\label{sec:display-hooks}
950 %
951 % If one has to adjust the code execution in a hook using a hook
952 % rule it is helpful to get some information about the code
953 % associated with a hook, its current order and the existing rules.
954 %
955 % \begin{function}{\ShowHook,\LogHook}
956 % \begin{syntax}
957 % \cs{ShowHook} \Arg{hook}
958 % \cs{LogHook} \Arg{hook}
959 % \end{syntax}
960 % Displays information about the \meta{hook} such as
961 % \begin{itemize}
962 % \item
963 % the code chunks (and their labels) added to it,
964 % \item
965 % any rules set up to order them,
966 % \item
967 % the computed order in which the chunks are executed,
968 % \item
969 % any code executed on the next invocation only.
970 % \end{itemize}
971 % \end{function}
972 %
973 % \cs{LogHook} prints the information to the |.log| file, and
974 % \cs{ShowHook} prints them to the terminal/command window and starts
975 % \TeX's prompt (only in \cs{errorstopmode}) to wait for user action.
976 %
977 % The \meta{hook} can be specified using the dot-syntax to denote
978 % the current package name. See section~\ref{sec:default-label}.
979 %
980 %^^A % Code for the listing below:
981 %^^A \NewHook{example-hook}
982 %^^A \AddToHook{example-hook}{[code from 'top-level']}
983 %^^A \AddToHook{example-hook}[foo]{[code from package 'foo']}
984 %^^A \AddToHook{example-hook}[bar]{[from package 'bar']}
985 %^^A \AddToHook{example-hook}[baz]{[package 'baz' is here]}
986 %^^A \AddToHookNext{example-hook}{[one-time code]}
987 %^^A \DeclareHookRule{example-hook}{baz}{before}{foo}
988 %^^A \DeclareDefaultHookRule{bar}{after}{baz}
989 %^^A \ShowHook{example-hook}
990 %
991 % \def\theFancyVerbLine{\textcolor[gray]{0.5}{%^^A
992 % \sffamily\tiny\arabic{FancyVerbLine}}}
993 %
994 % \bigskip
995 % Suppose a hook \texttt{example-hook} whose output of
996 % \cs{ShowHook}|{example-hook}| is:
997 % \begin{verbatim}[numbers=left]
998 % -> The hook 'example-hook':
999 % > Code chunks:
1000 % > foo -> [code from package 'foo']
1001 % > bar -> [from package 'bar']
1002 % > baz -> [package 'baz' is here]
1003 % > Document-level (top-level) code (executed last):
1004 % > -> [code from 'top-level']
1005 % > Extra code for next invocation:
1006 % > -> [one-time code]
1007 % > Rules:
1008 % > foo|baz with relation >
1009 % > baz|bar with default relation <
1010 % > Execution order (after applying rules):
1011 % > baz, foo, bar.
1012 % \end{verbatim}
1013 %
1014 % In the listing above, lines~3 to~5 show the three code chunks added
1015 % to the hook and their respective labels in the format
1016 % \begin{quote}
1017 % \quad \meta{label}\verb| -> |\meta{code}
1018 % \end{quote}
1019 %
1020 % Line~7 shows the code chunk added by the user in the main document
1021 % (labeled |top-level|) in the format
1022 % \begin{quote}
1023 % \quad\verb|Document-level (top-level) code (executed |%^^A
1024 % \meta{first\texttt{\string|}last}\verb|):|\\
1025 % \quad\verb| -> |\meta{\texttt{top-level} code}
1026 % \end{quote}
1027 % This code will be either the first or last code executed by the hook
1028 % (|last| if the hook is normal, |first| if it is reversed). This
1029 % chunk is not affected by rules and does not take part in sorting.
1030 %
1031 % Line~9 shows the code chunk for the next execution of the hook in
1032 % the format
1033 % \begin{quote}
1034 % \quad \verb|-> |\meta{next-code}
1035 % \end{quote}
1036 % This code will be used and disappear at the next
1037 % \verb|\UseHook{example-hook}|, in contrast to the chunks mentioned
1038 % earlier, which can only be removed from that hook by doing
1039 % \verb|\RemoveFromHook{|\meta{label}|}[example-hook]|.
1040 %
1041 % Lines~11 and~12 show the rules declared that affect this hook in the
1042 % format
1043 % \begin{quote}
1044 % \quad \meta{label-1}\verb+|+\meta{label-2}| with |%^^A
1045 % \meta{\texttt{default}?}| relation |\meta{relation}
1046 % \end{quote}
1047 % which means that the \meta{relation} applies to \meta{label-1} and
1048 % \meta{label-2}, in that order, as detailed in \cs{DeclareHookRule}.
1049 % If the relation is \texttt{default} it means that this rule applies
1050 % to \meta{label-1} and \meta{label-2} in \emph{all} hooks, (unless
1051 % overridden by a non-default relation).
1052 %
1053 % Finally, line~14 lists the labels in the hook after sorting;
1054 % that is, in the order they will be executed when the hook is used.
1055 %
1056 %
1057 % \subsubsection{Debugging hook code}
1058 %
1059 % \begin{function}{\DebugHooksOn,\DebugHooksOff}
1060 % \begin{syntax}
1061 % \cs{DebugHooksOn} \ldots\ \cs{DebugHooksOff}
1062 % \end{syntax}
1063 % Turn the debugging of hook code on or off. This displays most changes
1064 % made to the hook data structures. The output is rather coarse and
1065 % not really intended for normal use, but it can be helpful in case hooks do
1066 % not work as expected. See also \ref{sec:display-hooks} for
1067 % commands to inspect individual hooks.
1068 % \end{function}
1069 %
1070 %
1071 % \subsection{L3 programming layer (\texttt{expl3}) interfaces}
1072 % \label{sec:l3hook-interface}
1073 %
1074 %
1075 % This is a quick summary of the \LaTeX3 programming interfaces for
1076 % use with packages written in \texttt{expl3}. In contrast to the
1077 % \LaTeXe{} interfaces they always use mandatory arguments only, e.g.,
1078 % you always have to specify the \meta{label} for a code chunk. We
1079 % therefore suggest to use the declarations discussed in the previous
1080 % section even in \texttt{expl3} packages, but the choice is yours.
1081 %
1082 %
1083 % \begin{function}{
1084 % \hook_new:n,
1085 % \hook_new_reversed:n,
1086 % \hook_new_pair:nn
1087 % }
1088 % \begin{syntax}
1089 % \cs{hook_new:n} \Arg{hook}
1090 % \cs{hook_new_reversed:n} \Arg{hook}
1091 % \cs{hook_new_pair:nn} \Arg{hook-1} \Arg{hook-2}
1092 % \end{syntax}
1093 % Creates a new \meta{hook} with normal or reverse ordering of code
1094 % chunks. \cs{hook_new_pair:nn} creates a pair of such hooks with
1095 % \Arg{hook-2} being a reversed hook.
1096 % If a hook name is already taken, an error is raised and the hook
1097 % is not created.
1098 %
1099 % The \meta{hook} can be specified using the dot-syntax to denote
1100 % the current package name. See section~\ref{sec:default-label}.
1101 % \end{function}
1102 %
1103 % \begin{function}{
1104 % \hook_new_with_args:nn,
1105 % \hook_new_reversed_with_args:nn,
1106 % \hook_new_pair_with_args:nnn
1107 % }
1108 % \begin{syntax}
1109 % \cs{hook_new_with_args:nn} \Arg{hook} \Arg{number}
1110 % \cs{hook_new_reversed_with_args:nn} \Arg{hook} \Arg{number}
1111 % \cs{hook_new_pair_with_args:nnn} \Arg{hook-1} \Arg{hook-2} \Arg{number}
1112 % \end{syntax}
1113 % Creates a new \meta{hook} with normal or reverse ordering of code
1114 % chunks, that takes \meta{number} arguments from the input stream
1115 % when it is used. \cs{hook_new_pair_with_args:nn} creates a pair of
1116 % such hooks with \Arg{hook-2} being a reversed hook.
1117 % If a hook name is already taken, an error is raised and the hook
1118 % is not created.
1119 %
1120 % The \meta{hook} can be specified using the dot-syntax to denote
1121 % the current package name. See section~\ref{sec:default-label}.
1122 % \end{function}
1123 %
1124 %
1125 %
1126 % \begin{function}{\hook_disable_generic:n}
1127 % \begin{syntax}
1128 % \cs{hook_disable_generic:n} \Arg{hook}
1129 % \end{syntax}
1130 % Marks \Arg{hook} as disabled. Any further attempt to add code to
1131 % it or declare it, will result in an error and any call to
1132 % \cs{hook_use:n} will simply do nothing.
1133 %
1134 % This declaration is intended for use with generic hooks that are
1135 % known not to work (see \texttt{ltcmdhooks-doc}) if they receive
1136 % code.
1137 %
1138 % The \meta{hook} can be specified using the dot-syntax to denote
1139 % the current package name. See section~\ref{sec:default-label}.
1140 % \end{function}
1141 %
1142 % \begin{function}{\hook_activate_generic:n}
1143 % \begin{syntax}
1144 % \cs{hook_activate_generic:n} \Arg{hook}
1145 % \end{syntax}
1146 % This is like \cs{hook_new:n} but it does nothing if the hook was previously
1147 % declared with \cs{hook_new:n}. This declaration should be used
1148 % only in special situations, e.g., when a command from another package
1149 % needs to be altered and it is not clear whether a
1150 % generic \hook{cmd} hook (for that command) has been previously
1151 % explicitly declared.
1152 %
1153 % Normally \cs{hook_new:n} should be used instead of this.
1154 % \end{function}
1155 %
1156 %
1157 %
1158 %
1159 % \begin{function}{\hook_use:n, \hook_use:nnw}
1160 % \begin{syntax}
1161 % \cs{hook_use:n} \Arg{hook}
1162 % \cs{hook_use:nnw} \Arg{hook} \Arg{number} \Arg{arg_1} \ldots \Arg{arg_n}
1163 % \end{syntax}
1164 % Executes the \Arg{hook} code followed (if set up) by the code for next
1165 % invocation only, then empties that next invocation code.
1166 % \cs{hook_use:nnw} should be used for hooks declared with arguments,
1167 % and should be followed by as many brace groups as the declared
1168 % number of arguments.
1169 % The \meta{number} should be the number of arguments declared for
1170 % the hook. If the hook is not declared, this command does nothing
1171 % and it will remove \meta{number} items from the input.
1172 %
1173 % The \meta{hook} \emph{cannot} be specified using the dot-syntax.
1174 % A leading |.| is treated literally.
1175 % \end{function}
1176 %
1177 % \begin{function}{\hook_use_once:n, \hook_use_once:nnw}
1178 % \begin{syntax}
1179 % \cs{hook_use_once:n} \Arg{hook}
1180 % \cs{hook_use_once:nnw} \Arg{hook} \Arg{number} \Arg{arg_1} \ldots \Arg{arg_n}
1181 % \end{syntax}
1182 % Changes the \Arg{hook} status so that from now on any addition to
1183 % the hook code is executed immediately. Then execute any
1184 % \Arg{hook} code already set up.
1185 % \cs{hook_use_once:nnw} should be used for hooks declared with arguments,
1186 % and should be followed by as many brace groups as the declared
1187 % number of arguments.
1188 % The \meta{number} should be the number of arguments declared for
1189 % the hook. If the hook is not declared, this command does nothing
1190 % and it will remove \meta{number} items from the input.
1191 %
1192 % The \meta{hook} \emph{cannot} be specified using the dot-syntax.
1193 % A leading |.| is treated literally.
1194 % \end{function}
1195 %
1196 % \begin{function}{
1197 % \hook_gput_code:nnn, \hook_gput_code_with_args:nnn
1198 % }
1199 % \begin{syntax}
1200 % \cs{hook_gput_code:nnn} \Arg{hook} \Arg{label} \Arg{code}
1201 % \cs{hook_gput_code_with_args:nnn} \Arg{hook} \Arg{label} \Arg{code}
1202 % \end{syntax}
1203 % Adds a chunk of \meta{code} to the \meta{hook} labeled
1204 % \meta{label}. If the label already exists the \meta{code} is
1205 % appended to the already existing code.
1206 %
1207 % If \cs{hook_gput_code_with_args:nnn} is used, the \meta{code}
1208 % can access the arguments passed to \cs{hook_use:nnw}
1209 % (or~\cs{hook_use_once:nnw}) with \verb|#1|, \verb|#2|, \ldots,
1210 % \verb|#n| (up to the number of arguments declared for the hook).
1211 % In that case, if an actual parameter token should be added to the
1212 % code, it should be doubled.
1213 %
1214 % If code is added to an external \meta{hook} (of the kernel or
1215 % another package) then the convention is to use the package name
1216 % as the \meta{label} not some internal module name or some other
1217 % arbitrary string.
1218 %
1219 % The \meta{hook} and \meta{label} can be specified using the
1220 % dot-syntax to denote the current package name.
1221 % See section~\ref{sec:default-label}.
1222 % \end{function}
1223 %
1224 % \begin{function}{
1225 % \hook_gput_next_code:nn, \hook_gput_next_code_with_args:nn
1226 % }
1227 % \begin{syntax}
1228 % \cs{hook_gput_next_code:nn} \Arg{hook} \Arg{code}
1229 % \cs{hook_gput_next_code_with_args:nn} \Arg{hook} \Arg{code}
1230 % \end{syntax}
1231 % Adds a chunk of \meta{code} for use only in the next invocation of the
1232 % \meta{hook}. Once used it is gone.
1233 %
1234 % If \cs{hook_gput_next_code_with_args:nn} is used, the \meta{code}
1235 % can access the arguments passed to \cs{hook_use:nnw}
1236 % (or~\cs{hook_use_once:nnw}) with \verb|#1|, \verb|#2|, \ldots,
1237 % \verb|#n| (up to the number of arguments declared for the hook).
1238 % In that case, if an actual parameter token should be added to the
1239 % code, it should be doubled.
1240 %
1241 % \end{function}
1242 % This is simpler than \cs{hook_gput_code:nnn}, the code is simply
1243 % appended to the hook in the order of declaration at the very end,
1244 % i.e., after all standard code for the hook got executed.
1245 % Thus if one needs to undo what the standard does one has to do
1246 % that as part of \meta{code}.
1247 %
1248 % The \meta{hook} can be specified using the dot-syntax to denote
1249 % the current package name. See section~\ref{sec:default-label}.
1250 %
1251 %
1252 % \begin{function}{\hook_gclear_next_code:n}
1253 % \begin{syntax}
1254 % \cs{hook_gclear_next_code:n} \Arg{hook}
1255 % \end{syntax}
1256 % Undo any earlier \cs{hook_gput_next_code:nn}.
1257 % \end{function}
1258 %
1259 %
1260 %
1261 % \begin{function}{\hook_gremove_code:nn}
1262 % \begin{syntax}
1263 % \cs{hook_gremove_code:nn} \Arg{hook} \Arg{label}
1264 % \end{syntax}
1265 % Removes any code for \meta{hook} labeled \meta{label}.
1266 %
1267 % If there is no code under the \meta{label} in the \meta{hook},
1268 % or if the \meta{hook} does not exist, a warning is issued when
1269 % you attempt to use \cs{hook_gremove_code:nn}, and the command is ignored.
1270 %
1271 % If the second argument is \texttt{*}, then all code chunks are
1272 % removed. This is rather dangerous as it drops code from other
1273 % packages one may not know about, so think twice before using
1274 % that!
1275 %
1276 % The \meta{hook} and \meta{label} can be specified using the
1277 % dot-syntax to denote the current package name.
1278 % See section~\ref{sec:default-label}.
1279 % \end{function}
1280 %
1281 %
1282 % \begin{function}{\hook_gset_rule:nnnn}
1283 % \begin{syntax}
1284 % \cs{hook_gset_rule:nnnn} \Arg{hook} \Arg{label1} \Arg{relation} \Arg{label2}
1285 % \end{syntax}
1286 % Relate \meta{label1} with \meta{label2} when used in \meta{hook}.
1287 % See \cs{DeclareHookRule} for the allowed \meta{relation}s.
1288 % If \meta{hook} is \texttt{??} a default rule is specified.
1289 %
1290 % The \meta{hook} and \meta{label} can be specified using the
1291 % dot-syntax to denote the current package name.
1292 % See section~\ref{sec:default-label}.
1293 % The dot-syntax is parsed in both \meta{label} arguments, but it
1294 % usually makes sense to be used in only one of them.
1295 % \end{function}
1296 %
1297 % \begin{function}[pTF]{\hook_if_empty:n}
1298 % \begin{syntax}
1299 % \cs{hook_if_empty:nTF} \Arg{hook} \Arg{true code} \Arg{false code}
1300 % \end{syntax}
1301 % Tests if the \meta{hook} is empty (\emph{i.e.}, no code was added to
1302 % it using either \cs{AddToHook} or \cs{AddToHookNext}), and
1303 % branches to either \meta{true code} or \meta{false code} depending
1304 % on the result.
1305 %
1306 % The \meta{hook} \emph{cannot} be specified using the dot-syntax.
1307 % A leading |.| is treated literally.
1308 % \end{function}
1309 %
1310 %
1311 % \begin{function}{\hook_show:n,\hook_log:n}
1312 % \begin{syntax}
1313 % \cs{hook_show:n} \Arg{hook}
1314 % \cs{hook_log:n} \Arg{hook}
1315 % \end{syntax}
1316 % Displays information about the \meta{hook} such as
1317 % \begin{itemize}
1318 % \item
1319 % the code chunks (and their labels) added to it,
1320 % \item
1321 % any rules set up to order them,
1322 % \item
1323 % the computed order in which the chunks are executed,
1324 % \item
1325 % any code executed on the next invocation only.
1326 % \end{itemize}
1327 %
1328 % \cs{hook_log:n} prints the information to the |.log| file, and
1329 % \cs{hook_show:n} prints them to the terminal/command window and starts
1330 % \TeX's prompt (only if \cs{errorstopmode}) to wait for user action.
1331 %
1332 % The \meta{hook} can be specified using the dot-syntax to denote
1333 % the current package name. See section~\ref{sec:default-label}.
1334 % \end{function}
1335 %
1336 % \begin{function}{\hook_debug_on:,\hook_debug_off:}
1337 % \begin{syntax}
1338 % \cs{hook_debug_on:}
1339 % \end{syntax}
1340 % Turns the debugging of hook code on or off. This displays changes
1341 % to the hook data.
1342 % \end{function}
1343 %
1344 %
1345 %
1346 % \subsection{On the order of hook code execution} \label{sec:order}
1347 %
1348 % Chunks of code for a \meta{hook} under different labels are supposed
1349 % to be independent if there are no special rules set up that
1350 % define a relation between the chunks. This means that you can't
1351 % make assumptions about the order of execution!
1352 %
1353 % Suppose you have the following declarations:
1354 %\begin{verbatim}
1355 % \NewHook{myhook}
1356 % \AddToHook{myhook}[packageA]{\typeout{A}}
1357 % \AddToHook{myhook}[packageB]{\typeout{B}}
1358 % \AddToHook{myhook}[packageC]{\typeout{C}}
1359 %\end{verbatim}
1360 % then executing the hook with \cs{UseHook} will produce the
1361 % typeout \texttt{A} \texttt{B} \texttt{C} in that order. In other
1362 % words, the execution order is computed to be \texttt{packageA},
1363 % \texttt{packageB}, \texttt{packageC} which you can verify with
1364 % \cs{ShowHook}\texttt{\{myhook\}}:
1365 %\begin{verbatim}
1366 % -> The hook 'myhook':
1367 % > Code chunks:
1368 % > packageA -> \typeout {A}
1369 % > packageB -> \typeout {B}
1370 % > packageC -> \typeout {C}
1371 % > Document-level (top-level) code (executed last):
1372 % > ---
1373 % > Extra code for next invocation:
1374 % > ---
1375 % > Rules:
1376 % > ---
1377 % > Execution order:
1378 % > packageA, packageB, packageC.
1379 %\end{verbatim}
1380 % The reason is that the code chunks are internally saved in a property list
1381 % and the initial order of such a property list is the order in
1382 % which key-value pairs got added. However, that is only true if
1383 % nothing other than adding happens!
1384 %
1385 % Suppose, for example, you want to replace the code chunk for
1386 % \texttt{packageA}, e.g.,
1387 %\begin{verbatim}
1388 % \RemoveFromHook{myhook}[packageA]
1389 % \AddToHook{myhook}[packageA]{\typeout{A alt}}
1390 %\end{verbatim}
1391 % then your order becomes \texttt{packageB},
1392 % \texttt{packageC}, \texttt{packageA} because the label got removed
1393 % from the property list and then re-added (at its end).
1394 %
1395 % While that may not be too surprising, the execution order is
1396 % also sometimes altered if you add a redundant rule, e.g. if you specify
1397 %\begin{verbatim}
1398 % \DeclareHookRule{myhook}{packageA}{before}{packageB}
1399 %\end{verbatim}
1400 % instead of the previous lines we get
1401 %\begin{verbatim}
1402 % -> The hook 'myhook':
1403 % > Code chunks:
1404 % > packageA -> \typeout {A}
1405 % > packageB -> \typeout {B}
1406 % > packageC -> \typeout {C}
1407 % > Document-level (top-level) code (executed last):
1408 % > ---
1409 % > Extra code for next invocation:
1410 % > ---
1411 % > Rules:
1412 % > packageB|packageA with relation >
1413 % > Execution order (after applying rules):
1414 % > packageA, packageC, packageB.
1415 %\end{verbatim}
1416 % As you can see the code chunks are still in the same order, but
1417 % in the execution order for the labels \texttt{packageB} and
1418 % \texttt{packageC} have
1419 % swapped places.
1420 % The reason is that, with the rule there are two orders that
1421 % satisfy it, and the algorithm for sorting happened to pick a
1422 % different one compared to the case without rules (where it
1423 % doesn't run at all as there is nothing to resolve).
1424 % Incidentally, if we had instead specified the redundant rule
1425 %\begin{verbatim}
1426 % \DeclareHookRule{myhook}{packageB}{before}{packageC}
1427 %\end{verbatim}
1428 % the execution order would not have changed.
1429 %
1430 % In summary: it is not possible to rely on the order of execution
1431 % unless there are rules that partially or fully define the order
1432 % (in which you can rely on them being fulfilled).
1433 %
1434 %
1435 % \subsection{The use of \enquote{reversed} hooks} \label{sec:reversed-order}
1436 %
1437 % You may have wondered why you can declare a \enquote{reversed} hook
1438 % with \cs{NewReversedHook} and what that does exactly.
1439 %
1440 % In short: the execution order of a reversed hook (without any
1441 % rules!) is exactly reversed to the order you would have gotten for
1442 % a hook declared with \cs{NewHook}.
1443 %
1444 % This is helpful if you have a pair of hooks where you expect to see
1445 % code added that involves grouping, e.g., starting an environment
1446 % in the first and closing that environment in the second hook.
1447 % To give a somewhat contrived example\footnote{There are simpler
1448 % ways to achieve the same effect.}, suppose there is a package
1449 % adding the following:
1450 %\begin{verbatim}
1451 % \AddToHook{env/quote/before}[package-1]{\begin{itshape}}
1452 % \AddToHook{env/quote/after} [package-1]{\end{itshape}}
1453 %\end{verbatim}
1454 % As a result, all quotes will be in italics.
1455 % Now suppose further that another |package-too| makes the quotes
1456 % also in blue and therefore adds:
1457 %\begin{verbatim}
1458 % \usepackage{color}
1459 % \AddToHook{env/quote/before}[package-too]{\begin{color}{blue}}
1460 % \AddToHook{env/quote/after} [package-too]{\end{color}}
1461 %\end{verbatim}
1462 % Now if the \hook{env/quote/after} hook would be a normal hook we
1463 % would get the same execution order in both hooks, namely:
1464 %\begin{verbatim}
1465 % package-1, package-too
1466 %\end{verbatim}
1467 % (or vice versa) and as a result, would get:
1468 %\begin{verbatim}
1469 % \begin{itshape}\begin{color}{blue} ...
1470 % \end{itshape}\end{color}
1471 %\end{verbatim}
1472 % and an error message saying that \verb=\begin{color}= was ended by
1473 % \verb=\end{itshape}=.
1474 % With \hook{env/quote/after} declared as a reversed hook the
1475 % execution order is reversed and so all environments are closed in
1476 % the correct sequence and \cs{ShowHook} would give us the
1477 % following output:
1478 %\begin{verbatim}
1479 % -> The hook 'env/quote/after':
1480 % > Code chunks:
1481 % > package-1 -> \end {itshape}
1482 % > package-too -> \end {color}
1483 % > Document-level (top-level) code (executed first):
1484 % > ---
1485 % > Extra code for next invocation:
1486 % > ---
1487 % > Rules:
1488 % > ---
1489 % > Execution order (after reversal):
1490 % > package-too, package-1.
1491 %\end{verbatim}
1492 %
1493 % If there is a matching default rule (done with
1494 % \cs{DeclareDefaultHookRule} or with \texttt{??} for the hook
1495 % name) then this default rule is applied before the reversal so that the order
1496 % in the reversed hook mirrors the one in the normal hook. However,
1497 % all rules specific to a hook happen always after the reversal of the
1498 % execution order, so if you alter the order you will probably have
1499 % to alter it in both hooks, not just in one, but that depends on
1500 % the use case.
1501 %
1502 %
1503 %
1504 %
1505 % \subsection{Difference between \enquote{normal} and
1506 % \enquote{one-time} hooks}
1507 % \label{sec:onetime-hooks}
1508 %
1509 % When executing a hook a developer has the choice of using
1510 % either \cs{UseHook} or \cs{UseOneTimeHook} (or their \pkg{expl3}
1511 % equivalents \cs{hook_use:n} and \cs{hook_use_once:n}).
1512 % This choice affects how \cs{AddToHook} is handled after the hook
1513 % has been executed for the first time.
1514 %
1515 % With normal hooks adding code via \cs{AddToHook} means that the
1516 % code chunk is added to the hook data structure and then used each time
1517 % \cs{UseHook} is called.
1518 %
1519 % With one-time hooks it this is handled slightly differently:
1520 % After \cs{UseOneTimeHook} has been called, any further attempts to
1521 % add code to the hook via \cs{AddToHook} will simply execute the
1522 % \meta{code} immediately.
1523 %
1524 % This has some consequences one needs to be aware of:
1525 % \begin{itemize}
1526 % \item
1527 %
1528 % If \meta{code} is added to a normal hook after the hook was
1529 % executed and it is never executed again for one or the other
1530 % reason, then this new \meta{code} will never be executed.
1531 %
1532 % \item
1533 %
1534 % In contrast if that happens with a one-time hook the \meta{code} is
1535 % executed immediately.
1536 %
1537 % \end{itemize}
1538 % In particular this means that construct such as
1539 %\begin{quote}
1540 % \cs{AddToHook}\verb={myhook}=\\
1541 % \phantom{\cs{AddToHook}}\verb={= \meta{code-1}
1542 % \cs{AddToHook}\verb={myhook}=\Arg{code-2}
1543 % \meta{code-3} \verb=}=
1544 %\end{quote}
1545 % works for one-time hooks\footnote{This is sometimes used with
1546 % \cs{AtBeginDocument} which is why it is supported.} (all three
1547 % code chunks are executed one after another), but it makes little
1548 % sense with a normal hook, because with a normal hook the first time
1549 % \verb=\UseHook{myhook}= is executed it would
1550 % \begin{itemize}
1551 % \item
1552 % execute \meta{code-1},
1553 % \item
1554 % then execute \verb=\AddToHook{myhook}{code-2}= which adds the
1555 % code chunk \meta{code-2} to the hook for use on the next invocation,
1556 % \item
1557 % and finally execute \meta{code-3}.
1558 % \end{itemize}
1559 % The second time \cs{UseHook} is called it would execute the
1560 % above and in addition \meta{code-2} as that was added as a code
1561 % chunk to the hook in the meantime. So each time the hook is used
1562 % another copy of \meta{code-2} is added and so that code chunk
1563 % is executed $\meta{\# of invocations} -1$ times.
1564 %
1565 %
1566 %
1567 % \subsection{Generic hooks provided by packages}
1568 % \label{sec:generic-hooks}
1569 %
1570 % \changes{v1.0p}{2021/08/20}{Section on generic hooks added (gh/638)}
1571 %
1572 % The hook management system also implements a category of hooks
1573 % that are called \enquote{Generic Hooks}. Normally a hook has to
1574 % be explicitly declared before it can be used in code. This
1575 % ensures that different packages are not using the same hook name
1576 % for unrelated purposes---something that would result in absolute
1577 % chaos. However, there are a number of \enquote{standard} hooks
1578 % where it is unreasonable to declare them beforehand, e.g, each
1579 % and every command has (in theory) an associated \texttt{before}
1580 % and \texttt{after} hook. In such cases, i.e., for command,
1581 % environment or file hooks, they can be used simply by adding code
1582 % to them with \cs{AddToHook}. For more specialized generic hooks,
1583 % e.g., those provided by \pkg{babel}, you have to additionally
1584 % enable them with \cs{ActivateGenericHook} as explained below.
1585 %
1586 % The generic hooks provided by \LaTeX{} are those for
1587 % \hook{cmd},
1588 % \hook{env},
1589 % \hook{file},
1590 % \hook{include},
1591 % \hook{package}, and
1592 % \hook{class},
1593 % and all these are available out of the box: you only have to
1594 % use \cs{AddToHook} to
1595 % add code to them, but you don't have to add \cs{UseHook} or
1596 % \cs{UseOneTimeHook} to your code, because this is already done for
1597 % you (or, in the case of \hook{cmd} hooks, the command’s code is
1598 % patched at \verb=\begin{document}=, if necessary).
1599 %
1600 % However, if you want to provide further generic hooks in your own
1601 % code, the situation is slightly different. To do this you should
1602 % use \cs{UseHook} or \cs{UseOneTimeHook}, but
1603 % \emph{without declaring the hook} with \cs{NewHook}. As
1604 % mentioned earlier, a call to \cs{UseHook} with an undeclared hook
1605 % name does nothing. So as an additional setup step, you need to
1606 % explicitly activate your generic hook. Note that a generic hook
1607 % produced in this way is always a normal hook.
1608 %
1609 % For a truly generic hook, with a variable part in the hook name,
1610 % such upfront activation would be difficult or impossible, because
1611 % you typically do not know what kind of variable parts may come up
1612 % in real documents.
1613 %
1614 % For example, \pkg{babel} provides hooks such as
1615 % \hook{babel/\meta{language}/afterextras}. However, language
1616 % support in \pkg{babel} is often done through external language
1617 % packages. Thus doing the activation for all languages inside the
1618 % core \pkg{babel} code is not a viable approach. Instead it needs
1619 % to be done by each language package (or by the user who wants to
1620 % use a particular hook).
1621 %
1622 % Because the hooks are not declared with \cs{NewHook} their names
1623 % should be carefully chosen to ensure that they are (likely to be)
1624 % unique. Best practice is to include the package or
1625 % command name, as was done in the \pkg{babel} example above.
1626 %
1627 % Generic hooks defined in this way are always normal hooks (i.e.,
1628 % you can't implement reversed hooks this way). This is a
1629 % deliberate limitation, because it speeds up the processing
1630 % considerably.
1631 %
1632 %
1633 % \subsection{Hooks with arguments}
1634 % \label{sec:hook-args}
1635 %
1636 % Sometimes it is necessary to pass contextual information to a hook,
1637 % and, for one reason or another, it is not feasible to store such
1638 % information in macros. To serve this purpose, hooks can be
1639 % declared with arguments, so that the programmer can pass along the
1640 % data necessary for the code in the hook to function properly.
1641 %
1642 % A hook with arguments works mostly like a regular hook, and most
1643 % commands that work for regular hooks, also work for hooks that take
1644 % arguments. The differences are when the hook is declared
1645 % (\cs{NewHookWithArguments} is used instead of \cs{NewHook}), then
1646 % code can be added with both \cs{AddToHook} and
1647 % \cs{AddToHookWithArguments}, and when the hook is used
1648 % (\cs{UseHookWithArguments} instead of \cs{UseHook}).
1649 %
1650 % \medskip
1651 %
1652 % A hook with arguments must be declared as such (before it is first
1653 % used, as all regular hooks) using
1654 % \cs{NewHookWithArguments}\Arg{hook}\Arg{number}. All code added to
1655 % that hook can then use \verb|#1| to access the first argument,
1656 % \verb|#2| to access the second, and so forth up to the number of
1657 % arguments declared. However, it is still possible to add code with
1658 % references to the arguments of a hook that was not yet declared
1659 % (we will discuss that later). At their core, hooks are macros, so
1660 % \TeX's limit of 9~arguments applies, and a low-level \TeX{} error
1661 % is raised if you try to reference an argument number that doesn't
1662 % exist.
1663 %
1664 % \medskip
1665 %
1666 % To use a hook with arguments, just write
1667 % \cs{UseHookWithArguments}\Arg{hook}\Arg{number} followed by a
1668 % braced list of the arguments. For example, if the hook \hook{test}
1669 % takes three arguments, write:
1670 %\begin{verbatim}
1671 % \UseHookWithArguments{test}{3}{arg-1}{arg-2}{arg-3}
1672 %\end{verbatim}
1673 % then, in the \meta{code} of the hook, all instances of \verb|#1|
1674 % will be replaced by \verb|arg-1|, \verb|#2| by \verb|arg-2| and so
1675 % on. If, at the point of usage, the programmer provides more
1676 % arguments than the hook is declared to take, the excess arguments
1677 % are simply ignored by the hook. Behavior is
1678 % unpredictable\footnote
1679 % {The hook \emph{will} take the declared number of arguments, and
1680 % what will happen depends on what was grabbed, and what the hook
1681 % code does with its arguments.}
1682 % if too few arguments are provided. If the hook isn't declared,
1683 % \meta{number} arguments are removed from the input stream.
1684 %
1685 % \medskip
1686 %
1687 % Adding code to a hook with arguments can be done with
1688 % \cs{AddToHookWithArguments} as well as with the regular
1689 % \cs{AddToHook}, to achieve different outcomes. The main difference
1690 % when it comes to adding code to a hook, in this case, is firstly
1691 % the possibility of accessing a hook's arguments, of course, and
1692 % second, how parameter tokens (\verb|#|$_6$) are treated.
1693 %
1694 % Using \cs{AddToHook} in a hook that takes arguments will work as it
1695 % does for all other hooks. This allows a package developer to add
1696 % arguments to a hook that otherwise had none without having to worry
1697 % about compatibility. This means that, for example:
1698 %\begin{verbatim}
1699 % \AddToHook{test}{\def\foo#1{Hello, #1!}}
1700 %\end{verbatim}
1701 % will define the same macro \cs[no-index]{foo} regardless if the
1702 % hook \hook{test} takes arguments or not.
1703 %
1704 % Using \cs{AddToHookWithArguments} allows the \meta{code} added to
1705 % access the arguments of the hook with \verb|#1|, \verb|#2|, and so
1706 % forth, up to the number of the arguments declared in the hook.
1707 % This means that if one wants to add a \verb|#|$_6$ to the
1708 % \meta{code} that token must be doubled in the input. The same
1709 % definition from above, using \cs{AddToHookWithArguments}, needs to
1710 % be rewritten:
1711 %\begin{verbatim}
1712 % \AddToHookWithArguments{test}{\def\foo##1{Hello, ##1!}}
1713 %\end{verbatim}
1714 %
1715 % Extending the above example to use the hook arguments, we could
1716 % rewrite something like (now from declaration to usage, to get the
1717 % whole picture):
1718 %\begin{verbatim}
1719 % \NewHookWithArguments{test}{1}
1720 % \AddToHookWithArguments{test}{%
1721 % \typeout{Defining foo with "#1"}
1722 % \def\foo##1{Hello, ##1! Some text after: #1}%
1723 % }
1724 % \UseHook{test}{Howdy!}
1725 % \ShowCommand\foo
1726 %\end{verbatim}
1727 % Running the code above prints in the terminal:
1728 %\begin{verbatim}
1729 % Defining foo with "Howdy!"
1730 % > \foo=macro:
1731 % #1->Hello, #1! Some text after: Howdy!.
1732 %\end{verbatim}
1733 % Note how \verb|##1| in the call to \cs{AddToHookWithArguments}
1734 % became \verb|#1|, and the \verb|#1| was replaced by the argument
1735 % passed to the hook. Should the hook be used again, with a
1736 % different argument, the definition would naturally change.
1737 %
1738 % \bigskip
1739 %
1740 % It is possible to add code referencing a hook's arguments before
1741 % such hook is declared and the number of hooks is fixed. However,
1742 % if some code is added to the hook, that references more arguments
1743 % than will be declared for the hook, there will be a low-level
1744 % \TeX{} error about an \enquote{Illegal parameter number} at the
1745 % time the hook is declared, which will be hard to track down because
1746 % at that point \TeX{} can't know whence the offending code came
1747 % from. Thus it is important that package writers explicitly
1748 % document how many arguments (if any) each hook can take, so users
1749 % of those packages know how many arguments can be referenced, and
1750 % equally important, what each argument means.
1751 %
1752 % \subsection{Private \LaTeX{} kernel hooks}
1753 %
1754 % There are a few places where it is absolutely essential for
1755 % \LaTeX{} to function correctly that code is executed in a precisely
1756 % defined order. Even that could have been implemented with the
1757 % hook management (by adding various rules to ensure the
1758 % appropriate ordering with respect to other code added by
1759 % packages). However, this makes every document unnecessary
1760 % slow, because there has to be sorting even though the result is
1761 % predetermined. Furthermore it forces package writers to
1762 % unnecessarily add such rules if they add further code to the hook
1763 % (or break \LaTeX{}).
1764 %
1765 % For that reason such code is not using the hook management, but
1766 % instead private kernel commands directly before or after a public
1767 % hook with the following naming
1768 % convention: \cs{@kernel@before@\meta{hook}} or
1769 % \cs{@kernel@after@\meta{hook}}. For example, in
1770 % \cs{enddocument} you find
1771 %\begin{verbatim}
1772 % \UseHook{enddocument}%
1773 % \@kernel@after@enddocument
1774 %\end{verbatim}
1775 % which means first the user/package-accessible \hook{enddocument}
1776 % hook is executed and then the internal kernel hook. As their name
1777 % indicates these kernel commands should not be altered by third-party
1778 % packages, so please refrain from that in the interest of
1779 % stability and instead use the public hook next to it.\footnote{As
1780 % with everything in \TeX{} there is not enforcement of this rule,
1781 % and by looking at the code it is easy to find out how the kernel
1782 % adds to them. The main reason of this section is therefore to say
1783 % \enquote{please don't do that, this is unconfigurable code!}}
1784 %
1785 %
1786 %
1787 % \subsection{Legacy \LaTeXe{} interfaces}
1788 %
1789 % \newcommand\onetimetext{This is a one-time hook, so after it
1790 % is executed, all further
1791 % attempts to add code to it will execute such code immediately
1792 % (see section~\ref{sec:onetime-hooks}).}
1793 %
1794 % \LaTeXe{} offered a small number of hooks together with commands to
1795 % add to them. They are listed here and are retained for backwards
1796 % compatibility.
1797 %
1798 % With the new hook management, several additional hooks have been added
1799 % to \LaTeX\ and more will follow. See the next section for what
1800 % is already available.
1801 %
1802 %
1803 % \begin{function}{\AtBeginDocument}
1804 % \begin{syntax}
1805 % \cs{AtBeginDocument} \oarg{label} \Arg{code}
1806 % \end{syntax}
1807 % If used without the optional argument \meta{label}, it works essentially
1808 % like before, i.e., it is adding \meta{code} to the hook
1809 % \hook{begindocument}
1810 % (which is executed inside \verb=\begin{document}=).
1811 % However, all code added this way is labeled with the label
1812 % \hook{top-level} (see section~\ref{sec:top-level})
1813 % if done outside of a package or class or with the
1814 % package/class name if called inside such a file
1815 % (see section~\ref{sec:default-label}).
1816 %
1817 % This way one can add code to the hook using
1818 % \cs{AddToHook} or \cs{AtBeginDocument} using a different label
1819 % and explicitly order the code chunks as necessary, e.g., run some
1820 % code before or after another package's code. When using the
1821 % optional argument the call is equivalent to running
1822 % \cs{AddToHook} \texttt{\{begindocument\}} \oarg{label}
1823 % \Arg{code}.
1824 %
1825 % \cs{AtBeginDocument} is a wrapper around the \hook{begindocument}
1826 % hook (see section~\ref{sec:begindocument-hooks}), which is a
1827 % one-time hook. As such, after the \hook{begindocument} hook is
1828 % executed at \verb=\begin{document}= any attempt to add \meta{code}
1829 % to this hook with \cs{AtBeginDocument} or with \cs{AddToHook} will
1830 % cause that \meta{code} to execute immediately instead.
1831 % See section~\ref{sec:onetime-hooks} for more on one-time hooks.
1832 %
1833 % For important packages with known order requirement we may over
1834 % time add rules to the kernel (or to those packages) so that they
1835 % work regardless of the loading-order in the document.
1836 % \end{function}
1837 %
1838 % \begin{function}{\AtEndDocument}
1839 % \begin{syntax}
1840 % \cs{AtEndDocument} \oarg{label} \Arg{code}
1841 % \end{syntax}
1842 % Like \cs{AtBeginDocument} but for the \hook{enddocument} hook.
1843 % \end{function}
1844 %
1845 % \bigskip
1846 %
1847 % The few hooks that existed previously in \LaTeXe{} used internally
1848 % commands such as \cs{@begindocumenthook} and packages sometimes
1849 % augmented them directly rather than working through
1850 % \cs{AtBeginDocument}. For that reason there is currently support
1851 % for this, that is, if the system detects that such an internal
1852 % legacy hook command contains code it adds it to the new hook
1853 % system under the label \texttt{legacy} so that it doesn't get
1854 % lost.
1855 %
1856 % However, over time the remaining cases of direct usage need
1857 % updating because in one of the future release of \LaTeX{} we will
1858 % turn this legacy support off, as it does unnecessary slow down
1859 % the processing.
1860 %
1861 %
1862 % \section{\LaTeXe{} commands and environments augmented by hooks}
1863 %
1864 % In this section we describe the standard hooks that are now
1865 % offered by \LaTeX{}, or give pointers to other documents in which
1866 % they are described. This section will grow over time (and
1867 % perhaps eventually move to usrguide3).
1868 %
1869 % \subsection{Generic hooks}
1870 % \label{sec:generic}
1871 %
1872 % As stated earlier, with the exception of generic hooks, all hooks must
1873 % be declared with \cs{NewHook} before they can be used.
1874 % All generic hooks have names of the form
1875 % \enquote{\meta{type}/\meta{name}/\meta{position}}, where \meta{type}
1876 % is from the predefined list shown below, and \meta{name} is the variable
1877 % part whose meaning will depend on the \meta{type}. The last component,
1878 % \meta{position}, has more complex possibilities:
1879 % it can always be |before| or |after|; for |env| hooks, it can also be |begin|
1880 % or |end|; and for |include| hooks it can also be |end|. Each specific
1881 % hook is documented below, or in \texttt{ltcmdhooks-doc.pdf} or
1882 % \texttt{ltfilehook-doc.pdf}.
1883 %
1884 % The generic hooks provided by \LaTeX{} belong to one of the six types:
1885 % \begin{description}
1886 % \item[env] Hooks executed before and after environments --
1887 % \meta{name} is the name of the environment, and available values
1888 % for \meta{position} are |before|, |begin|, |end|, and |after|;
1889 % \item[cmd] Hooks added to and executed before and after commands --
1890 % \meta{name} is the name of the command, and available values
1891 % for \meta{position} are |before| and |after|;
1892 % \item[file] Hooks executed before and after reading a file --
1893 % \meta{name} is the name of the file (with extension), and
1894 % available values for \meta{position} are |before| and |after|;
1895 % \item[package] Hooks executed before and after loading packages --
1896 % \meta{name} is the name of the package, and available values for
1897 % \meta{position} are |before| and |after|;
1898 % \item[class] Hooks executed before and after loading classes --
1899 % \meta{name} is the name of the class, and available values for
1900 % \meta{position} are |before| and |after|;
1901 % \item[include] Hooks executed before and after \cs{include}d files --
1902 % \meta{name} is the name of the included file (without the |.tex|
1903 % extension), and available values for \meta{position} are |before|,
1904 % |end|, and |after|.
1905 % \end{description}
1906 %
1907 % Each of the hooks above are detailed in the following sections and
1908 % in linked documentation.
1909 % ^^A^^A^^A \pho{Wouldn't it be better to document all hooks here?}
1910 %
1911 % \subsubsection{Generic hooks for all environments}
1912 %
1913 % Every environment \meta{env} has now four associated hooks coming
1914 % with it:
1915 % \begin{description}
1916 % \item[\hook{env/\meta{env}/before}]
1917 %
1918 % This hook is executed as part of \cs{begin} as the very first
1919 % action, in particular prior to starting the environment group.
1920 % Its scope is therefore not restricted by the environment.
1921 %
1922 % \item[\hook{env/\meta{env}/begin}]
1923 %
1924 % This hook is executed as part of \cs{begin} directly in front
1925 % of the code specific to the environment start (e.g.,
1926 % the third argument of \cs{NewDocumentEnvironment} and
1927 % the second argument of \cs{newenvironment}).
1928 % Its scope is the environment body.
1929 %
1930 % \item[\hook{env/\meta{env}/end}]
1931 %
1932 % This hook is executed as part of \cs{end} directly in front of the
1933 % code specific to the end of the environment (e.g.,
1934 % the forth argument of \cs{NewDocumentEnvironment} and
1935 % the third argument of \cs{newenvironment}).
1936 %
1937 % \item[\hook{env/\meta{env}/after}]
1938 %
1939 % This hook is executed as part of \cs{end} after the
1940 % code specific to the environment end and after the environment
1941 % group has ended.
1942 % Its scope is therefore not restricted by the environment.
1943 %
1944 % The hook is implemented as a reversed hook so if two packages
1945 % add code to \hook{env/\meta{env}/before} and to
1946 % \hook{env/\meta{env}/after} they can add surrounding
1947 % environments and the order of closing them happens in the
1948 % right sequence.
1949 %
1950 % \end{description}
1951 %
1952 % Given that these generic hook names involve \texttt{/} as part of
1953 % their name they would not work if one tries to define an
1954 % environment using a name that involves a
1955 % \texttt{/}.\footnote{Officially, \LaTeX{} names for environments
1956 % should only consist of a sequence of letters, numbers, and the
1957 % character \texttt{*}, i.e., this is not a new restriction.}
1958 %
1959 % Generic environment hooks are never one-time hooks even with
1960 % environments that are supposed to appear only once in a
1961 % document.\footnote{Thus if one adds code to such hooks after the
1962 % environment has been processed, it will only be executed if the
1963 % environment appears again and if that doesn't happen the code
1964 % will never get executed.} In contrast to other hooks there is
1965 % also no need to declare them using \cs{NewHook}.
1966 %
1967 % The hooks are only executed if \cs{begin}\Arg{env} and
1968 % \cs{end}\Arg{env} is used. If the environment code is executed
1969 % via low-level calls to \cs[no-index]{\meta{env}} and \cs[no-index]{end\meta{env}}
1970 % (e.g., to avoid the environment grouping) they are not
1971 % available. If you want them available in code using this method,
1972 % you would need to add them yourself, i.e., write something like
1973 %\begin{verbatim}
1974 % \UseHook{env/quote/before}\quote
1975 % ...
1976 % \endquote\UseHook{env/quote/after}
1977 %\end{verbatim}
1978 % to add the outer hooks, etc.
1979 %
1980 %
1981 % Largely for compatibility with existing packages, the following
1982 % four commands are also available to set the environment hooks; but for
1983 % new packages we recommend directly using the hook names and
1984 % \cs{AddToHook}.
1985 %
1986 %
1987 % \begin{function}{\BeforeBeginEnvironment}
1988 % \begin{syntax}
1989 % \cs{BeforeBeginEnvironment} \oarg{label} \Arg{env} \Arg{code}
1990 % \end{syntax}
1991 % This declaration adds to the \hook{env/\meta{env}/before} hook
1992 % using the \meta{label}. If \meta{label} is not given, the
1993 % \meta{default label} is used (see section~\ref{sec:default-label}).
1994 % \end{function}
1995 %
1996 % \begin{function}{\AtBeginEnvironment}
1997 % \begin{syntax}
1998 % \cs{AtBeginEnvironment} \oarg{label} \Arg{env} \Arg{code}
1999 % \end{syntax}
2000 % This is like \cs{BeforeBeginEnvironment} but it adds to the \hook{env/\meta{env}/begin} hook.
2001 % \end{function}
2002 %
2003 % \begin{function}{\AtEndEnvironment}
2004 % \begin{syntax}
2005 % \cs{AtEndEnvironment} \oarg{label} \Arg{env} \Arg{code}
2006 % \end{syntax}
2007 % This is like \cs{BeforeBeginEnvironment} but it adds to the \hook{env/\meta{env}/end} hook.
2008 % \end{function}
2009 %
2010 % \begin{function}{\AfterEndEnvironment}
2011 % \begin{syntax}
2012 % \cs{AfterEndEnvironment} \oarg{label} \Arg{env} \Arg{code}
2013 % \end{syntax}
2014 % This is like \cs{BeforeBeginEnvironment} but it adds to the \hook{env/\meta{env}/after} hook.
2015 % \end{function}
2016 %
2017 %
2018 % \subsubsection{Generic hooks for commands}
2019 %
2020 % Similar to environments there are now (at least in theory) two
2021 % generic hooks available for any \LaTeX{} command. These are
2022 % \begin{description}
2023 % \item[\hook{cmd/\meta{name}/before}]
2024 %
2025 % This hook is executed at the very start of the command
2026 % execution.
2027 %
2028 % \item[\hook{cmd/\meta{name}/after}]
2029 % This hook is executed at the very end of the command body. It is
2030 % implemented as a reversed hook.
2031 % \end{description}
2032 % In practice there are restrictions and especially the
2033 % \hook{after} hook works only with a subset of commands. Details
2034 % about these restrictions are documented in
2035 % \texttt{ltcmdhooks-doc.pdf} or with code in
2036 % \texttt{ltcmdhooks-code.pdf}.
2037 %
2038 %
2039 %
2040 %
2041 % \subsubsection{Generic hooks provided by file loading operations}
2042 %
2043 % There are several hooks added to \LaTeX{}'s process of loading
2044 % file via its high-level interfaces such as \cs{input},
2045 % \cs{include}, \cs{usepackage}, \cs{RequirePackage}, etc. These
2046 % are documented in \texttt{ltfilehook-doc.pdf} or with code in
2047 % \texttt{ltfilehook-code.pdf}.
2048 %
2049 %
2050 %
2051 % \subsection{Hooks provided by \cs{begin}\texttt{\{document\}}}
2052 % \label{sec:begindocument-hooks}
2053 %
2054 % Until 2020 \cs{begin}\texttt{\{document\}} offered exactly one
2055 % hook that one could add to using
2056 % \cs{AtBeginDocument}. Experiences over the years have shown that
2057 % this single hook in one place was not enough and as part of
2058 % adding the general hook management system a number of additional
2059 % hooks have been added at this point. The places for these hooks have
2060 % been chosen to provide the same support as offered by external
2061 % packages, such as \pkg{etoolbox} and others that augmented
2062 % \cs{document} to gain better control.
2063 %
2064 % Supported are now the following hooks (all of them one-time hooks):
2065 % \begin{description}
2066 %
2067 %
2068 % \item[\hook{begindocument/before}]
2069 %
2070 % This hook is executed at the very start of \cs{document}, one can
2071 % think of it as a hook for code at the end of the preamble
2072 % section and this is how it is used by \pkg{etoolbox}'s
2073 % \cs{AtEndPreamble}.
2074 %
2075 % \onetimetext
2076 %
2077 % \item[\hook{begindocument}]
2078 %
2079 % This hook is added to by using \cs{AddToHook}\texttt{\{begindocument\}}
2080 % or by using \cs{AtBeginDocument} and it is
2081 % executed after the \texttt{.aux} file has been read and most
2082 % initialization are done, so they can be altered and inspected by
2083 % the hook code. It is followed by a small number of further
2084 % initializations that shouldn't be altered and are therefore
2085 % coming later.
2086 %
2087 % The hook should not be used to add material for typesetting as
2088 % we are still in \LaTeX's initialization phase and not in the
2089 % document body. If such material needs to be added to the document
2090 % body use the next hook instead.
2091 %
2092 % \onetimetext
2093 %
2094 % \item[\hook{begindocument/end}]
2095 %
2096 % This hook is executed at the end of the \cs{document} code in
2097 % other words at the beginning of the document body. The only
2098 % command that follows it is \cs{ignorespaces}.
2099 %
2100 % \onetimetext
2101 %
2102 % \end{description}
2103 % The generic hooks executed by \cs{begin} also exist, i.e.,
2104 % \hook{env/document/before} and \hook{env/document/begin}, but
2105 % with this special environment it is better use the dedicated
2106 % one-time hooks above.
2107 %
2108 %
2109 %
2110 %
2111 % \subsection{Hooks provided by \cs{end}\texttt{\{document\}}}
2112 %
2113 % \LaTeXe{} has always provided \cs{AtEndDocument} to add code to the
2114 % \verb=\end{document}=, just in front of the code that
2115 % is normally executed there. While this was a big improvement over
2116 % the situation in \LaTeX\,2.09, it was not flexible enough for a
2117 % number of use cases and so packages, such as \pkg{etoolbox},
2118 % \pkg{atveryend} and others patched \cs{enddocument} to add
2119 % additional points where code could be hooked into.
2120 %
2121 % Patching using packages is always problematical as leads to
2122 % conflicts (code availability, ordering of patches, incompatible
2123 % patches, etc.). For this reason a number of additional hooks
2124 % have been added to the \cs{enddocument} code to allow packages
2125 % to add code in various places in a controlled way without the
2126 % need for overwriting or patching the core code.
2127 %
2128 % Supported are now the following hooks (all of them one-time hooks):
2129 % \begin{description}
2130 %
2131 % \item[\hook{enddocument}]
2132 %
2133 % The hook associated with \cs{AtEndDocument}. It is immediately
2134 % called at the beginning of \cs{enddocument}.
2135 %
2136 % When this hook is executed there may be still unprocessed
2137 % material (e.g., floats on the deferlist) and the hook may add
2138 % further material to be typeset. After it, \cs{clearpage} is
2139 % called to ensure that all such material gets typeset. If there
2140 % is nothing waiting the \cs{clearpage} has no effect.
2141 %
2142 % \onetimetext
2143 %
2144 % \item[\hook{enddocument/afterlastpage}]
2145 %
2146 % As the name indicates this hook should not receive code that
2147 % generates material for further pages. It is the right place to
2148 % do some final housekeeping and possibly write out some
2149 % information to the \texttt{.aux} file (which is still open at
2150 % this point to receive data). Just before this hook \cs{write} is redefined
2151 % and is now always \cs{immediate}\cs{write}. This means that writing e.g. a label
2152 % or something to the \texttt{.toc} works despite the fact that no more pages are output.
2153 % It is also the correct place to
2154 % set up any testing code to be run when the \texttt{.aux} file
2155 % is re-read in the next step.
2156 %
2157 %
2158 % After this hook has been executed the \texttt{.aux} file is
2159 % closed for writing and then read back in to do some tests
2160 % (e.g., looking for missing references or duplicated labels, etc.).
2161 %
2162 % \onetimetext
2163 %
2164 % \item[\hook{enddocument/afteraux}]
2165 %
2166 % At this point, the \texttt{.aux} file has been reprocessed and so
2167 % this is a possible place for final checks and display of
2168 % information to the user. However, for the latter you might
2169 % prefer the next hook, so that your information is displayed after the
2170 % (possibly longish) list of files if that got requested via \cs{listfiles}.
2171 %
2172 % \onetimetext
2173 %
2174 % \item[\hook{enddocument/info}]
2175 %
2176 % This hook is meant to receive code that write final information
2177 % messages to the terminal. It follows immediately after the
2178 % previous hook (so both could have been combined, but then
2179 % packages adding further code would always need to also supply
2180 % an explicit rule to specify where it should go.
2181 %
2182 % This hook already contains some code added by the kernel (under
2183 % the labels \texttt{kernel/filelist} and
2184 % \texttt{kernel/warnings}), namely the list of files when
2185 % \cs{listfiles} has been used and the warnings for duplicate
2186 % labels, missing references, font substitutions etc.
2187 %
2188 % \onetimetext
2189 %
2190 % \item[\hook{enddocument/end}]
2191 %
2192 % Finally, this hook is executed just in front of the final call
2193 % to \cs{@{}@end}.
2194 %
2195 % \onetimetext % is it even possible to add code after this one?
2196 %
2197 % \end{description}
2198 %
2199 %
2200 % There is also the hook \hook{shipout/lastpage}. This hook is
2201 % executed as part of the last \cs{shipout} in the document to
2202 % allow package to add final \cs{special}'s to that page. Where
2203 % this hook is executed in relation to those from the above list
2204 % can vary from document to document. Furthermore to determine correctly
2205 % which of the \cs{shipout}s is the last one, \LaTeX{} needs to be run
2206 % several times, so initially it might get executed on the wrong
2207 % page. See section~\ref{sec:shipout} for where to find the details.
2208 %
2209 %
2210 % It is in also possible to use the generic \hook{env/document/end}
2211 % hook which is executed by \cs{end}, i.e., just in front of the
2212 % first hook above. Note however that the other generic \cs{end}
2213 % environment hook, i.e., \hook{env/document/after} will never get
2214 % executed, because by that time \LaTeX{} has finished the document
2215 % processing.
2216 %
2217 %
2218 %
2219 %
2220 % \subsection{Hooks provided by \cs{shipout} operations}
2221 % \label{sec:shipout}
2222 %
2223 % There are several hooks and mechanisms added to \LaTeX{}'s
2224 % process of generating pages. These are documented in
2225 % \texttt{ltshipout-doc.pdf} or with code in
2226 % \texttt{ltshipout-code.pdf}.
2227 %
2228 %
2229 % \subsection{Hooks provided for paragraphs}
2230 % \label{sec:para}
2231 %
2232 % The paragraph processing has been augmented to include a number of
2233 % internal and public hooks. These are documented in
2234 % \texttt{ltpara-doc.pdf} or with code in
2235 % \texttt{ltpara-code.pdf}.
2236 %
2237 %
2238 %
2239 % \subsection{Hooks provided in NFSS commands}
2240 %
2241 % In languages that need to support for more than one script in
2242 % parallel (and thus several sets of fonts, e.g., supporting both Latin and
2243 % Japanese fonts), NFSS font commands such as \cs{sffamily} need
2244 % to switch both the Latin family to ``Sans Serif'' and in addition
2245 % alter a second set of fonts.
2246 %
2247 % To support this, several NFSS commands have hooks to which
2248 % such support can be added.
2249 % \begin{description}
2250 %
2251 % \item[\hook{rmfamily}]
2252 %
2253 % After \cs{rmfamily} has done its initial checks and prepared a
2254 % font series update, this hook is executed before \cs{selectfont}.
2255 %
2256 % \item[\hook{sffamily}]
2257 %
2258 % This is like the \hook{rmfamily} hook, but for the \cs{sffamily} command.
2259 %
2260 % \item[\hook{ttfamily}]
2261 %
2262 % This is like the \hook{rmfamily} hook, but for the \cs{ttfamily} command.
2263 %
2264 % \item[\hook{normalfont}]
2265 %
2266 % The \cs{normalfont} command resets the font encoding, family, series
2267 % and shape to their document defaults. It then executes this
2268 % hook and finally calls \cs{selectfont}.
2269 %
2270 % \item[\hook{expand@font@defaults}]
2271 %
2272 % The internal \cs{expand@font@defaults} command expands and
2273 % saves the current defaults for the metafamilies (rm/sf/tt) and
2274 % the metaseries (bf/md). If the NFSS machinery has been
2275 % augmented, e.g., for Chinese or Japanese fonts, then further
2276 % defaults may need to be set at this point. This can be done in
2277 % this hook which is executed at the end of this macro.
2278 %
2279 % \item[\hook{bfseries/defaults}, \hook{bfseries}]
2280 %
2281 % If the \cs{bfdefault} was explicitly changed by the user, its
2282 % new value is used to set the bf series defaults for the
2283 % metafamilies (rm/sf/tt) when \cs{bfseries} is called. The
2284 % \hook{bfseries/defaults} hook allows further adjustments to be made
2285 % in this case. This hook is only executed if such a change is
2286 % detected. In contrast, the \hook{bfseries} hook is always
2287 % executed just before \cs{selectfont} is called to change to the
2288 % new series.
2289 %
2290 %
2291 % \item[\hook{mdseries/defaults}, \hook{mdseries}]
2292 %
2293 % These two hooks are like the previous ones but they are in the
2294 % \cs{mdseries} command.
2295 %
2296 % \item[\hook{selectfont}]
2297 %
2298 % This hook is executed inside \cs{selectfont}, after the current
2299 % values for \textit{encoding}, \textit{family}, \textit{series},
2300 % \textit{shape}, and \textit{size} are evaluated and the new font
2301 % is selected (and if necessary loaded). After the hook has
2302 % executed, NFSS will still do any updates necessary for a new
2303 % \textit{size} (such as changing the size of \cs{strut}) and any
2304 % updates necessary to a change in \textit{encoding}.
2305 %
2306 % This hook is intended for use cases where, in parallel to a
2307 % change in the main font, some other fonts need to be altered
2308 % (e.g., in CJK processing where you may need to deal with several
2309 % different alphabets).
2310 %
2311 % \end{description}
2312 %
2313 %
2314 %
2315 % \subsection{Hook provided by the mark mechanism}
2316 %
2317 % See \texttt{ltmarks-doc.pdf} for details.
2318 % \begin{description}
2319 %
2320 % \item[\hook{insertmark}]
2321 %
2322 % This hook allows for a special setup while \cs{InsertMark}
2323 % inserts a mark. It is executed in group so local changes only
2324 % apply to the mark being inserted.
2325 %
2326 % \end{description}
2327 %
2328 % \MaybeStop{\setlength\IndexMin{200pt} \PrintIndex }
2329 %
2330 %
2331 % \section{The Implementation}
2332 %
2333 %
2334 % \begin{macrocode}
2335 %<@@=hook>
2336 % \end{macrocode}
2337 %
2338 % \changes{v1.0i}{2021/03/18}{Use \cs{NewModuleRelease}.}
2339 % \changes{v1.0n}{2021/05/24}{Use \cs{msg_...} instead of \cs{__kernel_msg...}}
2340 % \changes{v1.1o}{2026-03-12}{Switch from \texttt{x}- to \texttt{e}-type
2341 % expansion in expl3 code}
2342 %
2343 % \begin{macrocode}
2344 %<*2ekernel|latexrelease>
2345 \ExplSyntaxOn
2346 %<latexrelease>\NewModuleRelease{2020/10/01}{lthooks}
2347 %<latexrelease> {The~hook~management~system}
2348 % \end{macrocode}
2349 %
2350 % \subsection{Debugging}
2351 %
2352 % \begin{macro}{\g_@@_debug_bool}
2353 % Holds the current debugging state.
2354 % \begin{macrocode}
2355 \bool_new:N \g_@@_debug_bool
2356 % \end{macrocode}
2357 % \end{macro}
2358 %
2359 % \begin{macro}{\hook_debug_on:,\hook_debug_off:}
2360 % \begin{macro}{\@@_debug:n}
2361 % \begin{macro}{\@@_debug_gset:}
2362 % Turns debugging on and off by redefining \cs{@@_debug:n}.
2363 % \begin{macrocode}
2364 \cs_new_eq:NN \@@_debug:n \use_none:n
2365 \cs_new_protected:Npn \hook_debug_on:
2366 {
2367 \bool_gset_true:N \g_@@_debug_bool
2368 \@@_debug_gset:
2369 }
2370 \cs_new_protected:Npn \hook_debug_off:
2371 {
2372 \bool_gset_false:N \g_@@_debug_bool
2373 \@@_debug_gset:
2374 }
2375 \cs_new_protected:Npn \@@_debug_gset:
2376 {
2377 \cs_gset_protected:Npe \@@_debug:n ##1
2378 { \bool_if:NT \g_@@_debug_bool {##1} }
2379 }
2380 % \end{macrocode}
2381 % \end{macro}
2382 % \end{macro}
2383 % \end{macro}
2384 %
2385 %
2386 %
2387 % \subsection{Borrowing from internals of other kernel modules}
2388 %
2389 %
2390 % \begin{macro}[EXP]{\@@_str_compare:nn}
2391 % Private copy of \cs{__str_if_eq:nn}
2392 % \InternalDetectionOff
2393 % \begin{macrocode}
2394 \cs_new_eq:NN \@@_str_compare:nn \__str_if_eq:nn
2395 % \end{macrocode}
2396 % \InternalDetectionOn
2397 % \end{macro}
2398 %
2399 % \subsection{Declarations}
2400 %
2401 % \begin{macro}{\l_@@_tmpa_bool}
2402 % Scratch boolean used throughout the package.
2403 % \begin{macrocode}
2404 \bool_new:N \l_@@_tmpa_bool
2405 % \end{macrocode}
2406 % \end{macro}
2407 %
2408 % \begin{macro}{\l_@@_return_tl,\l_@@_tmpa_tl,\l_@@_tmpb_tl}
2409 % Scratch variables used throughout the package.
2410 % \begin{macrocode}
2411 \tl_new:N \l_@@_return_tl
2412 \tl_new:N \l_@@_tmpa_tl
2413 \tl_new:N \l_@@_tmpb_tl
2414 % \end{macrocode}
2415 % \end{macro}
2416 %
2417 % \begin{macro}{\g_@@_all_seq}
2418 % In a few places we need a list of all hook names ever defined so
2419 % we keep track if them in this sequence.
2420 % \begin{macrocode}
2421 \seq_new:N \g_@@_all_seq
2422 % \end{macrocode}
2423 % \end{macro}
2424 %
2425 % \begin{macro}{\l_@@_cur_hook_tl}
2426 % Stores the name of the hook currently being sorted.
2427 % \begin{macrocode}
2428 \tl_new:N \l_@@_cur_hook_tl
2429 % \end{macrocode}
2430 % \end{macro}
2431 %
2432 % \begin{macro}{\l_@@_work_prop}
2433 % A property list holding a copy of the
2434 % \cs[no-index]{g_@@_\meta{hook}_code_prop} of the hook being sorted
2435 % to work on, so that changes don't act destructively on the hook data
2436 % structure.
2437 % \begin{macrocode}
2438 \prop_new:N \l_@@_work_prop
2439 % \end{macrocode}
2440 % \end{macro}
2441 %
2442 % \begin{macro}{\g_@@_used_prop}
2443 % All hooks that receive code (for use in debugging display).
2444 % \begin{macrocode}
2445 \prop_new:N \g_@@_used_prop
2446 % \end{macrocode}
2447 % \end{macro}
2448 %
2449 % \begin{macro}{\g_@@_hook_curr_name_tl,\g_@@_name_stack_seq}
2450 % Default label used for hook commands, and a stack to keep track of
2451 % packages within packages.
2452 % \begin{macrocode}
2453 \tl_new:N \g_@@_hook_curr_name_tl
2454 \seq_new:N \g_@@_name_stack_seq
2455 % \end{macrocode}
2456 % \end{macro}
2457 %
2458 % \begin{macro}{\@@_tmp:w}
2459 % Temporary macro for generic usage.
2460 % \begin{macrocode}
2461 \cs_new_eq:NN \@@_tmp:w ?
2462 % \end{macrocode}
2463 % \end{macro}
2464 %
2465 % \begin{macro}{\c_@@_empty_tl}
2466 % \begin{macro}{\c_@@_nine_parameters_tl}
2467 % An empty token list, and one containing nine parameters.
2468 % \changes{v1.1a}{2023/04/06}
2469 % {Add auxiliary token lists (hook-args).}
2470 % \begin{macrocode}
2471 \tl_const:Nn \c_@@_empty_tl { }
2472 \tl_const:Nn \c_@@_nine_parameters_tl { #1#2#3#4#5#6#7#8#9 }
2473 % \end{macrocode}
2474 % \end{macro}
2475 % \end{macro}
2476 %
2477 % \begin{macro}[int]{
2478 % \str_count:e
2479 % }
2480 % \changes{v1.1o}{2026-03-13}{Tidy up variants}
2481 % Some variants of \pkg{expl3} functions.
2482 % \fmiinline{should probably be moved to expl3}
2483 % \begin{macrocode}
2484 \cs_generate_variant:Nn \str_count:n { e }
2485 % \end{macrocode}
2486 % \end{macro}
2487 %
2488 % \begin{macro}{\s_@@_mark}
2489 % Scan mark used for delimited arguments.
2490 % \begin{macrocode}
2491 \scan_new:N \s_@@_mark
2492 % \end{macrocode}
2493 % \end{macro}
2494 %
2495 % \begin{macro}{
2496 % \@@_use_none_delimit_by_s_mark:w,
2497 % \@@_use_i_delimit_by_s_mark:nw
2498 % }
2499 % Removes tokens until the next \cs{s_@@_mark}.
2500 % \changes{v1.1a}{2023/04/06}
2501 % {Use standard naming scheme (hook-args).}
2502 % \begin{macrocode}
2503 \cs_new:Npn \@@_use_none_delimit_by_s_mark:w #1 \s_@@_mark { }
2504 \cs_new:Npn \@@_use_i_delimit_by_s_mark:nw #1 #2 \s_@@_mark {#1}
2505 % \end{macrocode}
2506 % \end{macro}
2507 %
2508 % \begin{macro}{\@@_tl_set:cn}
2509 % Private copies of a few \pkg{expl3} functions. \pkg{l3debug} will
2510 % only add debugging to the public names, not to these copies, so we
2511 % don't have to use \cs{debug_suspend:} and \cs{debug_resume:}
2512 % everywhere.
2513 %
2514 % Functions like \cs{@@_tl_set:Nn} have to be redefined, rather than
2515 % copied because in \pkg{expl3} they use
2516 % \cs[no-index]{__kernel_tl_(g)set:Nx}, which is also patched by
2517 % \pkg{l3debug}.
2518 % \changes{v1.0h}{2021/01/07}{Manually define some \pkg{l3tl} commands
2519 % to work around \pkg{expl3} changes}
2520 % \changes{v1.1a}{2023/04/06}
2521 % {Clean-up unused variants (hook-args).}
2522 % \begin{macrocode}
2523 \cs_new_protected:Npn \@@_tl_set:cn #1#2
2524 { \cs_set_nopar:cpe {#1} { \__kernel_exp_not:w {#2} } }
2525 % \end{macrocode}
2526 % \end{macro}
2527 %
2528 % \begin{macro}{\@@_tl_gset:Nn,\@@_tl_gset:Ne,
2529 % \@@_tl_gset:cn,\@@_tl_gset:co,\@@_tl_gset:ce}
2530 % Same as above.
2531 % \begin{macrocode}
2532 \cs_new_protected:Npn \@@_tl_gset:Nn #1#2
2533 { \cs_gset_nopar:Npe #1 { \__kernel_exp_not:w {#2} } }
2534 \cs_new_protected:Npn \@@_tl_gset:Ne #1#2
2535 { \cs_gset_nopar:Npe #1 {#2} }
2536 \cs_generate_variant:Nn \@@_tl_gset:Nn { c, co }
2537 \cs_generate_variant:Nn \@@_tl_gset:Ne { c }
2538 % \end{macrocode}
2539 % \end{macro}
2540 %
2541 % \begin{macro}{
2542 % \@@_tl_gput_right:Nn,
2543 % \@@_tl_gput_right:Ne,
2544 % \@@_tl_gput_right:cn,
2545 % }
2546 % Same as above.
2547 % \begin{macrocode}
2548 \cs_new_protected:Npn \@@_tl_gput_right:Nn #1#2
2549 { \@@_tl_gset:Ne #1 { \__kernel_exp_not:w \exp_after:wN { #1 #2 } } }
2550 \cs_generate_variant:Nn \@@_tl_gput_right:Nn { Ne, cn }
2551 % \end{macrocode}
2552 % \end{macro}
2553 %
2554 % \begin{macro}{\@@_tl_gput_left:Nn}
2555 % Same as above.
2556 % \begin{macrocode}
2557 \cs_new_protected:Npn \@@_tl_gput_left:Nn #1#2
2558 {
2559 \@@_tl_gset:Ne #1
2560 { \__kernel_exp_not:w {#2} \__kernel_exp_not:w \exp_after:wN {#1} }
2561 }
2562 % \end{macrocode}
2563 % \end{macro}
2564 %
2565 % \begin{macro}{\@@_tl_gset_eq:NN}
2566 % Same as above.
2567 % \begin{macrocode}
2568 \cs_new_eq:NN \@@_tl_gset_eq:NN \tl_gset_eq:NN
2569 % \end{macrocode}
2570 % \end{macro}
2571 %
2572 % \begin{macro}{\@@_tl_gclear:N,\@@_tl_gclear:c}
2573 % Same as above.
2574 % \begin{macrocode}
2575 \cs_new_protected:Npn \@@_tl_gclear:N #1
2576 { \@@_tl_gset_eq:NN #1 \c_empty_tl }
2577 \cs_generate_variant:Nn \@@_tl_gclear:N { c }
2578 % \end{macrocode}
2579 % \end{macro}
2580 %
2581 %
2582 % \subsection{Providing new hooks}
2583 %
2584 % \subsubsection{The data structures of a hook}
2585 %
2586 %^^A \DecribeMacro is not set up to use @@ convention so __hook is used explicitly
2587 %
2588 % \DescribeMacro{\g__hook_\meta{hook}_code_prop}
2589 % \DescribeMacro{\__hook\textvisiblespace\meta{hook}}
2590 % \DescribeMacro{\g__hook_\meta{hook}_reversed_tl}
2591 % \DescribeMacro{\g__hook_\meta{hook}_declared_tl}
2592 % \DescribeMacro{\g__hook_\meta{hook}_parameter_tl}
2593 % \DescribeMacro{\__hook_next\textvisiblespace\meta{hook}}
2594 % \DescribeMacro{\__hook_toplevel\textvisiblespace\meta{hook}}
2595 % Hooks have a name (called \meta{hook} in the description below)
2596 % and for each hook we have to
2597 % provide a number of data structures. These are
2598 % \begin{description}
2599 % \item[\cs{g_@@_\meta{hook}_code_prop}] A property list holding the code
2600 % for the hook in separate chunks. The keys are by default the
2601 % package names that add code to the hook, but it is possible
2602 % for packages to define other keys.
2603 %
2604 % \item[{\cs[no-index]{g_@@_\meta{hook}_rule_\meta{label1}\string|\meta{label2}_tl}}]
2605 % A token list holding the relation between \meta{label1} and
2606 % \meta{label2} in the \meta{hook}. The \meta{labels} are lexically
2607 % (reverse) sorted to ensure that two labels always point to the same
2608 % token list. For global rules, the \meta{hook} name is \texttt{??}.
2609 %
2610 % \item[\cs{@@\textvisiblespace\meta{hook}}] The code that is actually executed
2611 % when the hook is called in the document is stored in this token
2612 % list. It is constructed from the code chunks applying the
2613 % information.
2614 % This token list is named like that so that in case of an error
2615 % inside the hook, the reported token list in the error is shorter,
2616 % and to make it simpler to normalize hook names in
2617 % \cs{@@_make_name:n}.
2618 %
2619 % \item[\cs{g_@@_\meta{hook}_reversed_tl}] Some hooks are
2620 % \enquote{reversed}. This token list stores a |-| for such hook
2621 % so that it can be identified. The |-| character is used because
2622 % $\meta{reversed}1$ is $+1$ for normal hooks and $-1$ for reversed
2623 % ones.
2624 %
2625 % \item[{\cs[no-index]{g_@@_\meta{hook}_declared_tl}}] This token
2626 % list serves as a marker for the hook being officially declared. Its
2627 % existence is tested to raise an error in case another declaration
2628 % is attempted.
2629 %
2630 % \item[{\cs[no-index]{c_@@_\meta{hook}_parameter_tl}}] This token
2631 % list stores the parameter text for a declared hook (its existence
2632 % almost completely intersects the token list above), which is used
2633 % for managing hooks with arguments.
2634 %
2635 % \item[\cs{@@_toplevel\textvisiblespace\meta{hook}}] This token list stores the code
2636 % inserted in the hook from the user's document, in the |top-level|
2637 % label. This label is special, and doesn't participate in sorting.
2638 % Instead, all code is appended to it and executed after (or before,
2639 % if the hook is reversed) the normal
2640 % hook code, but before the |next| code chunk.
2641 %
2642 % \item[\cs{@@_next\textvisiblespace\meta{hook}}] Finally there is
2643 % extra code (normally empty) that is used on the next invocation
2644 % of the hook (and then deleted). This can be used to define some
2645 % special behavior for a single occasion from within the document.
2646 % This token list follows the same naming scheme than the main
2647 % \cs{@@\textvisiblespace\meta{hook}} token list. It is called
2648 % \cs{@@_next\textvisiblespace\meta{hook}} rather than
2649 % \cs[no-index]{@@\textvisiblespace next_\meta{hook}} because
2650 % otherwise a hook whose name is |next_|\meta{hook} would clash
2651 % with the next code-token list of the hook called \meta{hook}.
2652 %
2653 % \end{description}
2654 %
2655 %
2656 % \subsubsection{On the existence of hooks}
2657 % \label{sec:existence}
2658 %
2659 % A hook may be in different states of existence. Here we give an
2660 % overview of the internal commands to set up hooks and explain how the
2661 % different states are distinguished. The actual implementation
2662 % then follows in subsequent sections.
2663 %
2664 % One problem we have to solve is that we need to be able to add
2665 % code to hooks (e.g., with \cs{AddToHook}) even if that code has
2666 % not yet been declared. For example, one package needs to write
2667 % into a hook of another package, but that package may not get
2668 % loaded, or is loaded only later. Another problem is that most hooks,
2669 % but not the generic hooks, require a declaration.
2670 %
2671 % We therefore distinguish the following states for a hook, which
2672 % are managed by four different tests: structure existence
2673 % (\cs{@@_if_structure_exist:nTF}), creation
2674 % (\cs{@@_if_usable:nTF}), declaration (\cs{@@_if_declared:nTF})
2675 % and disabled or not (\cs{@@_if_disabled:nTF})
2676 % \begin{description}
2677 % \setlist[itemize]{leftmargin=5cm,format=\cs}
2678 % \item[not existing]
2679 %
2680 % Nothing is known about the hook so far. This state can be
2681 % detected with \cs{@@_if_structure_exist:nTF}
2682 % (which uses the false branch).
2683 %
2684 % In this state the hook can be declared, disabled, rules can be
2685 % defined or code could be added to it, but it is not possible
2686 % to use the hook (with \cs{UseHook}).
2687
2688 % \item[basic data structure set up]
2689 %
2690 % A hook is this state when its basic data structure has been
2691 % set up (using \cs{@@_init_structure:n}). The data structure setup happens
2692 % automatically when commands such as \cs{AddToHook} are used
2693 % and the hook is at that point in state \enquote{not existing}.
2694 %
2695 % In this state the four tests give the following results:
2696 % \begin{itemize}
2697 % \item [@@_if_structure_exist:nTF] returns |true|.
2698 % \item [@@_if_usable:nTF] returns |false|.
2699 % \item [@@_if_declared:nTF] returns |false|.
2700 % \item [@@_if_disabled:nTF] returns |false|.
2701 % \end{itemize}
2702 %
2703 % The allowed actions are the same as in the \enquote{not
2704 % existing} state.
2705 %
2706 % \item[declared]
2707 %
2708 % A hook is in this state it is not disabled and was explicitly declared (e.g.,
2709 % with \cs{NewHook}). In this case the four tests give the
2710 % following results:
2711 % \begin{itemize}
2712 % \item [@@_if_structure_exist:nTF] returns |true|.
2713 % \item [@@_if_usable:nTF] returns |true|.
2714 % \item [@@_if_declared:nTF] returns |true|.
2715 % \item [@@_if_disabled:nTF] returns |false|.
2716 % \end{itemize}
2717 %
2718 % \item[usable]
2719 %
2720 % A hook is in this state if it is not disabled, was not
2721 % explicitly declared but nevertheless is allowed to be used
2722 % (with \cs{UseHook} or \cs{hook_use:n}). This state is only
2723 % possible for generic hooks as they do not need to be
2724 % declared. Therefore such hooks move directly from state
2725 % \enquote{not existing} to \enquote{usable} the moment a
2726 % declaration such as \cs{AddToHook} wants to add to the hook
2727 % data structure. In this state the tests give the following
2728 % results:
2729 % \begin{itemize}
2730 % \item [@@_if_structure_exist:nTF] returns |true|.
2731 % \item [@@_if_usable:nTF] returns |true|.
2732 % \item [@@_if_declared:nTF] returns |false|.
2733 % \item [@@_if_disabled:nTF] returns |false|.
2734 % \end{itemize}
2735 %
2736 %
2737 % \item[disabled]
2738 %
2739 % A generic hook in any state is moved to this state when
2740 % \cs{DisableGenericHook} is used. This changes the tests to give the
2741 % following results:
2742 % \begin{itemize}
2743 % \item [@@_if_structure_exist:nTF] \emph{unchanged}.
2744 % \item [@@_if_usable:nTF] returns |false|.
2745 % \item [@@_if_declared:nTF] returns |true|.
2746 % \item [@@_if_disabled:nTF] returns |true|.
2747 % \end{itemize}
2748 % The structure test is unchanged (if the hook was unknown before it is
2749 % |false|, otherwise |true|). The usable test returns |false| so that
2750 % any \cs{UseHook} will bypass the hook from now on. The
2751 % declared test returns true so that any further \cs{NewHook}
2752 % generates an error and the disabled test returns true so that
2753 % \cs{AddToHook} can return an error.
2754 % \fmiinline{maybe it should do this only after begin document?}
2755 %
2756 % \end{description}
2757 %
2758 %
2759 %
2760 %
2761 % \subsubsection{Setting hooks up}
2762 %
2763 %
2764 % \begin{macro}{
2765 % \hook_new:n,
2766 % \hook_new_with_args:nn
2767 % }
2768 % \begin{macro}{\@@_new:nn}
2769 % The \cs{hook_new:n} declaration declares a new hook and expects
2770 % the hook \meta{name} as its argument, e.g.,
2771 % \hook{begindocument}.
2772 % \changes{v1.1a}{2023/04/06}
2773 % {Add \cs{hook_new_with_args:nn} (hook-args).}
2774 % \begin{macrocode}
2775 %<latexrelease>\IncludeInRelease{2023/06/01}{\hook_new_with_args:nn}
2776 %<latexrelease> {Hooks~with~args}
2777 \cs_new_protected:Npn \hook_new:n #1
2778 { \@@_normalize_hook_args:Nn \@@_new:nn {#1} { 0 } }
2779 \cs_new_protected:Npn \hook_new_with_args:nn #1 #2
2780 { \@@_normalize_hook_args:Nn \@@_new:nn {#1} {#2} }
2781 % \end{macrocode}
2782 %
2783 % \begin{macrocode}
2784 \cs_new_protected:Npn \@@_new:nn #1 #2
2785 {
2786 % \end{macrocode}
2787 % We check if the hook was already \emph{explicitly} declared with
2788 % \cs{hook_new:n}, and if it already exists we complain, otherwise set
2789 % the \enquote{created} flag for the hook so that it errors next time
2790 % \cs{hook_new:n} is used.
2791 % \changes{v1.1d}{2023/05/21}
2792 % {Changes to allow support arguments in cmd hooks (cmd-args).}
2793 % \begin{macrocode}
2794 \@@_if_declared:nTF {#1}
2795 { \msg_error:nnn { hooks } { exists } {#1} }
2796 {
2797 \tl_new:c { g_@@_#1_declared_tl }
2798 \cs_undefine:c { @@~#1 }
2799 \cs_undefine:c { c_@@_#1_parameter_tl }
2800 \@@_make_usable:nn {#1} {#2}
2801 % \end{macrocode}
2802 % In case there is already code in a hook, but it's undeclared, run
2803 % \cs{@@_update_hook_code:n} to make it ready to be executed (see test
2804 % \texttt{lthooks-034}).
2805 % \changes{v1.1a}{2023/04/06}
2806 % {Update hook code after declaring.}
2807 % \begin{macrocode}
2808 \@@_update_hook_code:n {#1}
2809 }
2810 }
2811 %<latexrelease>\EndIncludeInRelease
2812 % \end{macrocode}
2813 % \end{macro}
2814 %
2815 % \begin{macrocode}
2816 %<latexrelease>\IncludeInRelease{2020/10/01}{\hook_new_with_args:nn}
2817 %<latexrelease> {Hooks~with~args}
2818 %<latexrelease>\cs_gset_protected:Npn \hook_new:n #1
2819 %<latexrelease> { \@@_normalize_hook_args:Nn \@@_new:n {#1} }
2820 %<latexrelease>\cs_undefine:N \@@_new:nn
2821 %<latexrelease>\cs_gset_protected:Npn \@@_new:n #1
2822 %<latexrelease> {
2823 %<latexrelease> \@@_if_declared:nTF {#1}
2824 %<latexrelease> { \msg_error:nnn { hooks } { exists } {#1} }
2825 %<latexrelease> {
2826 %<latexrelease> \tl_new:c { g_@@_#1_declared_tl }
2827 %<latexrelease> \@@_make_usable:n {#1}
2828 %<latexrelease> }
2829 %<latexrelease> }
2830 %<latexrelease>\cs_gset_protected:Npn \hook_new_with_args:nn #1 { }
2831 %<latexrelease>\EndIncludeInRelease
2832 % \end{macrocode}
2833 % \end{macro}
2834 %
2835 %
2836 %
2837 % \begin{macro}{\@@_make_usable:nn}
2838 %
2839 % This initializes all hook data structures for the hook but if
2840 % used on its own doesn't mark the hook as declared (as
2841 % \cs{hook_new:n} does, so a later \cs{hook_new:n} on that hook will
2842 % not result in an error. This command is internally used by
2843 % \cs{hook_gput_code:nnn} when adding code to a generic hook.
2844 % \changes{v1.1a}{2023/04/06}
2845 % {Changes to add hook arguments (hook-args).}
2846 % \begin{macrocode}
2847 %<latexrelease>\IncludeInRelease{2023/06/01}{\@@_make_usable:nn}
2848 %<latexrelease> {Hooks~with~args}
2849 \cs_new_protected:Npn \@@_make_usable:nn #1 #2
2850 {
2851 % \end{macrocode}
2852 % Now we check if the hook's data structure can be safely created
2853 % without \pkg{expl3} raising errors, then
2854 % we add the hook name to the list of all hooks and
2855 % allocate the necessary data structures for the new hook,
2856 % otherwise just do nothing.
2857 % \begin{macrocode}
2858 \@@_if_usable:nF {#1}
2859 {
2860 \seq_gput_right:Nn \g_@@_all_seq {#1}
2861 % \end{macrocode}
2862 % Here we'll define the \cs[no-index]{c_@@_\meta{hook}_parameter_tl}
2863 % to hold a run of parameters up to the number of arguments of the
2864 % hook (\verb|#2|).
2865 % \begin{macrocode}
2866 \__kernel_cs_parm_from_arg_count:nnF
2867 { \tl_const:cn { c_@@_#1_parameter_tl } } {#2}
2868 {
2869 \msg_error:nnnn { hooks } { too-many-args } {#1} {#2}
2870 \tl_const:ce { c_@@_#1_parameter_tl }
2871 { \exp_not:V \c_@@_nine_parameters_tl }
2872 }
2873 % \end{macrocode}
2874 % After that, use \cs{@@_normalise_cs_args:nn} to correct the number
2875 % of parameters of the macros
2876 % \cs[no-index]{@@_toplevel\textvisiblespace\meta{hook}} and
2877 % \cs[no-index]{@@_next\textvisiblespace\meta{hook}}. We need to be
2878 % able to add
2879 % code with arguments to a hook without prior knowledge of the number
2880 % of arguments of that hook, so \pkg{lthooks} assumes~9 until the
2881 % hook is properly declared and the number of arguments is known.
2882 % \cs{@@_normalise_cs_args:nn} does the normalization by using the
2883 % \cs[no-index]{c_@@_\meta{hook}_parameter_tl} defined just above.
2884 % \begin{macrocode}
2885 \@@_normalise_cs_args:nn { _toplevel } {#1}
2886 \@@_normalise_cs_args:nn { _next } {#1}
2887 % \end{macrocode}
2888 % This is only used by the actual code of the current hook, so
2889 % declare it normally:
2890 % \begin{macrocode}
2891 \@@_code_gset:nn {#1} { }
2892 % \end{macrocode}
2893 % Now ensure that the base data structure for the hook exists:
2894 % \begin{macrocode}
2895 \@@_init_structure:n {#1}
2896 % \end{macrocode}
2897 % The call to \cs{@@_normalise_code_pool:n} will correct any improper
2898 % reference to arguments that don't exist in the hook, raising a
2899 % low-level \TeX{} error and doubling the offending parameter tokens.
2900 % It has to be done after \cs{@@_init_structure:n} because it
2901 % operates on \cs[no-index]{g_@@_\meta{hook}_code_prop}.
2902 % \begin{macrocode}
2903 \@@_normalise_code_pool:n {#1}
2904 % \end{macrocode}
2905 % The \cs{g_@@_\meta{hook}_labels_clist} holds the sorted list of
2906 % labels (once it got sorted). This is used only for debugging.
2907 % These are defined conditionally, in case \cs{@@_make_usable:nn}
2908 % is being used to redefine a hook.
2909 % \changes{v1.1d}{2023/05/21}
2910 % {Changes to allow support arguments in cmd hooks (cmd-args).}
2911 % \begin{macrocode}
2912 \clist_if_exist:cF { g_@@_#1_labels_clist }
2913 {
2914 \clist_new:c { g_@@_#1_labels_clist }
2915 % \end{macrocode}
2916 % Some hooks should reverse the default order of code chunks. To
2917 % signal this we have a token list which is empty for normal hooks
2918 % and contains a \verb=-= for reversed hooks.
2919 % \begin{macrocode}
2920 \tl_new:c { g_@@_#1_reversed_tl }
2921 }
2922 % \end{macrocode}
2923 % The above is all in L3 convention, but we also provide an
2924 % interface to legacy \LaTeXe{} hooks of the form \cs{@...hook},
2925 % e.g., \cs{@begindocumenthook}.
2926 % there have been a few of them and they have been added to
2927 % using \cs{g@addto@macro}. If there exists such a macro matching
2928 % the name of the new hook, i.e.,
2929 % \verb+\@+\meta{hook-name}\texttt{hook} and it is not empty then
2930 % we add its contents as a code chunk under the label \texttt{legacy}.
2931 % \begin{quote}
2932 % \textbf{Warning: this support will vanish in future releases!}
2933 % \end{quote}
2934 %
2935 % \begin{macrocode}
2936 \@@_include_legacy_code_chunk:n {#1}
2937 }
2938 }
2939 %<latexrelease>\EndIncludeInRelease
2940 % \end{macrocode}
2941 %
2942 % \begin{macrocode}
2943 %<latexrelease>\IncludeInRelease{2020/10/01}{\@@_make_usable:nn}
2944 %<latexrelease> {Hooks~with~args}
2945 %<latexrelease>\cs_undefine:N \@@_make_usable:nn
2946 %<latexrelease>\cs_gset_protected:Npn \@@_make_usable:n #1
2947 %<latexrelease> {
2948 %<latexrelease> \tl_if_exist:cF { @@~#1 }
2949 %<latexrelease> {
2950 %<latexrelease> \seq_gput_right:Nn \g_@@_all_seq {#1}
2951 %<latexrelease> \tl_new:c { @@~#1 }
2952 %<latexrelease> \@@_init_structure:n {#1}
2953 %<latexrelease> \clist_new:c { g_@@_#1_labels_clist }
2954 %<latexrelease> \tl_new:c { g_@@_#1_reversed_tl }
2955 %<latexrelease> \@@_include_legacy_code_chunk:n {#1}
2956 %<latexrelease> }
2957 %<latexrelease> }
2958 %<latexrelease>\EndIncludeInRelease
2959 % \end{macrocode}
2960 % \end{macro}
2961 %
2962 %
2963 % \begin{macro}{\@@_init_structure:n}
2964 % This function declares the basic data structures for a hook
2965 % without explicit declaring the hook itself. This is needed to
2966 % allow adding to undeclared hooks. Here it is unnecessary to
2967 % check whether all variables exist, since all three are declared
2968 % at the same time (either all of them exist, or none).
2969 %
2970 % It creates the hook code pool
2971 % (\cs[no-index]{g_@@_\meta{hook}_code_prop}) and the |top-level|
2972 % and |next| token lists. A hook is initialized with
2973 % \cs{@@_init_structure:n} the first time anything is added to it.
2974 % Initializing a hook just with \cs{@@_init_structure:n} will not
2975 % make it usable with \cs{hook_use:n}.
2976 % \changes{v1.1a}{2023/04/06}
2977 % {Changes to add hook arguments (hook-args).}
2978 % \begin{macrocode}
2979 %<latexrelease>\IncludeInRelease{2023/06/01}{\@@_init_structure:n}
2980 %<latexrelease> {Hooks~with~args}
2981 \cs_new_protected:Npn \@@_init_structure:n #1
2982 {
2983 \@@_if_structure_exist:nF {#1}
2984 {
2985 \prop_new:c { g_@@_#1_code_prop }
2986 \@@_toplevel_gset:nn {#1} { }
2987 \@@_next_gset:nn {#1} { }
2988 }
2989 }
2990 %<latexrelease>\EndIncludeInRelease
2991 % \end{macrocode}
2992 %
2993 % \begin{macrocode}
2994 %<latexrelease>\IncludeInRelease{2020/10/01}{\@@_init_structure:n}
2995 %<latexrelease> {Hooks~with~args}
2996 %<latexrelease>\cs_gset_protected:Npn \@@_init_structure:n #1
2997 %<latexrelease> {
2998 %<latexrelease> \@@_if_structure_exist:nF {#1}
2999 %<latexrelease> {
3000 %<latexrelease> \prop_new:c { g_@@_#1_code_prop }
3001 %<latexrelease> \tl_new:c { @@_toplevel~#1 }
3002 %<latexrelease> \tl_new:c { @@_next~#1 }
3003 %<latexrelease> }
3004 %<latexrelease> }
3005 %<latexrelease>\EndIncludeInRelease
3006 % \end{macrocode}
3007 % \end{macro}
3008 %
3009 % \begin{macro}{
3010 % \hook_new_reversed:n,
3011 % \hook_new_reversed_with_args:nn
3012 % }
3013 % \begin{macro}{\@@_new_reversed:nn}
3014 %
3015 % Declare a new hook. The default ordering of code chunks is
3016 % reversed, signaled by setting the token list to a minus sign.
3017 % \changes{v1.1a}{2023/04/06}
3018 % {Add \cs{hook_new_reversed_with_args:nn} (hook-args).}
3019 % \begin{macrocode}
3020 %<latexrelease>\IncludeInRelease{2023/06/01}{\hook_new_reversed_with_args:nn}
3021 %<latexrelease> {Hooks~with~args}
3022 \cs_new_protected:Npn \hook_new_reversed:n #1
3023 { \@@_normalize_hook_args:Nn \@@_new_reversed:nn {#1} { 0 } }
3024 \cs_new_protected:Npn \hook_new_reversed_with_args:nn #1 #2
3025 { \@@_normalize_hook_args:Nn \@@_new_reversed:nn {#1} {#2} }
3026 \cs_new_protected:Npn \@@_new_reversed:nn #1 #2
3027 {
3028 \@@_if_declared:nTF {#1}
3029 { \msg_error:nnn { hooks } { exists } {#1} }
3030 {
3031 \@@_new:nn {#1} {#2}
3032 \tl_gset:cn { g_@@_#1_reversed_tl } { - }
3033 }
3034 }
3035 %<latexrelease>\EndIncludeInRelease
3036 % \end{macrocode}
3037 %
3038 % \begin{macrocode}
3039 %<latexrelease>\IncludeInRelease{2020/10/01}{\hook_new_reversed_with_args:nn}
3040 %<latexrelease> {Hooks~with~args}
3041 %<latexrelease>\cs_gset_protected:Npn \hook_new_reversed:n #1
3042 %<latexrelease> { \@@_normalize_hook_args:Nn \@@_new_reversed:n {#1} }
3043 %<latexrelease>\cs_undefine:N \@@_new_reversed:nn
3044 %<latexrelease>\cs_gset_protected:Npn \@@_new_reversed:n #1
3045 %<latexrelease> {
3046 %<latexrelease> \@@_new:n {#1}
3047 %<latexrelease> \tl_gset:cn { g_@@_#1_reversed_tl } { - }
3048 %<latexrelease> }
3049 %<latexrelease>\cs_undefine:N \@@_new_reversed:nn
3050 %<latexrelease>\cs_gset_protected:Npn \hook_new_reversed_with_args:nn #1 #2 { }
3051 %<latexrelease>\EndIncludeInRelease
3052 % \end{macrocode}
3053 % \end{macro}
3054 % \end{macro}
3055 %
3056 % \begin{macro}{\hook_new_pair:nn,\hook_new_pair_with_args:nnn}
3057 % A shorthand for declaring a normal and a (matching) reversed hook in one go.
3058 % \changes{v1.1a}{2023/04/06}
3059 % {Add \cs{hook_new_pair_with_args:nnn} (hook-args).}
3060 % \begin{macrocode}
3061 %<latexrelease>\IncludeInRelease{2023/06/01}{\hook_new_pair_with_args:nnn}
3062 %<latexrelease> {Hooks~with~args}
3063 \cs_new_protected:Npn \hook_new_pair:nn #1#2
3064 { \@@_normalize_hook_args:Nnn \@@_new_pair:nnn {#1} {#2} { 0 } }
3065 \cs_new_protected:Npn \hook_new_pair_with_args:nnn #1#2#3
3066 { \@@_normalize_hook_args:Nnn \@@_new_pair:nnn {#1} {#2} {#3} }
3067 \cs_new_protected:Npn \@@_new_pair:nnn #1 #2 #3
3068 {
3069 \@@_if_declared:nTF {#1}
3070 { \msg_error:nnn { hooks } { exists } {#1} }
3071 {
3072 \@@_if_declared:nTF {#2}
3073 { \msg_error:nnn { hooks } { exists } {#2} }
3074 {
3075 \@@_new:nn {#1} {#3}
3076 \@@_new_reversed:nn {#2} {#3}
3077 }
3078 }
3079 }
3080 %<latexrelease>\EndIncludeInRelease
3081 % \end{macrocode}
3082 %
3083 % \begin{macrocode}
3084 %<latexrelease>\IncludeInRelease{2020/10/01}{\hook_new_pair_with_args:nnn}
3085 %<latexrelease> {Hooks~with~args}
3086 %<latexrelease>\cs_gset_protected:Npn \hook_new_pair:nn #1#2
3087 %<latexrelease> {
3088 %<latexrelease> \hook_new:n {#1}
3089 %<latexrelease> \hook_new_reversed:n {#2}
3090 %<latexrelease> }
3091 %<latexrelease>\cs_gset_protected:Npn \hook_new_pair_with_args:nnn #1#2#3
3092 %<latexrelease> { }
3093 %<latexrelease>\EndIncludeInRelease
3094 % \end{macrocode}
3095 % \end{macro}
3096 %
3097 %
3098 % \begin{macro}{\@@_include_legacy_code_chunk:n}
3099 % The \LaTeX{} legacy concept for hooks uses with hooks the
3100 % following naming scheme in the code: \cs{@...hook}.
3101 %
3102 % If this macro is not empty we add it under the label
3103 % \texttt{legacy} to the current hook and then empty it globally.
3104 % This way packages or classes directly manipulating commands such
3105 % as \cs{@begindocumenthook} still get their hook data added.
3106 % \begin{quote}
3107 % \textbf{Warning: this support will vanish in future releases!}
3108 % \end{quote}
3109 % \begin{macrocode}
3110 %<latexrelease>\IncludeInRelease{2023/06/01}{\@@_include_legacy_code_chunk:n}
3111 %<latexrelease> {Hooks~with~args}
3112 \cs_new_protected:Npn \@@_include_legacy_code_chunk:n #1
3113 {
3114 % \end{macrocode}
3115 % If the macro doesn't exist (which is the usual case) then nothing
3116 % needs to be done.
3117 % \begin{macrocode}
3118 \tl_if_exist:cT { @#1hook }
3119 {
3120 % \end{macrocode}
3121 % Of course if the legacy hook exists but is empty, there is no need
3122 % to add anything under \texttt{legacy} the legacy label.
3123 % \begin{macrocode}
3124 \tl_if_empty:cF { @#1hook }
3125 {
3126 % \end{macrocode}
3127 % Here we set \cs{@@_replacing_args_false:} because no legacy code
3128 % will reference hook arguments.
3129 % \changes{v1.1b}{2023/04/16}
3130 % {\cs{__hook_replacing_args_false:} in
3131 % \cs{__hook_include_legacy_code_chunk:n}}
3132 % \begin{macrocode}
3133 \@@_replacing_args_false:
3134 \use:e
3135 {
3136 \@@_hook_gput_code_do:nnn {#1} { legacy }
3137 { \exp_not:v { @#1hook } }
3138 }
3139 \@@_replacing_args_reset:
3140 % \end{macrocode}
3141 % Once added to the hook, we need to clear it otherwise it might
3142 % get added again later if the hook data gets updated.
3143 % \begin{macrocode}
3144 \@@_tl_gclear:c { @#1hook }
3145 }
3146 }
3147 }
3148 %<latexrelease>\EndIncludeInRelease
3149 %<latexrelease>\IncludeInRelease{2020/10/01}{\@@_include_legacy_code_chunk:n}
3150 %<latexrelease> {Hooks~with~args}
3151 %<latexrelease>\cs_gset_protected:Npn \@@_include_legacy_code_chunk:n #1
3152 %<latexrelease> {
3153 %<latexrelease> \tl_if_exist:cT { @#1hook }
3154 %<latexrelease> {
3155 %<latexrelease> \tl_if_empty:cF { @#1hook }
3156 %<latexrelease> {
3157 %<latexrelease> \exp_args:Nnnv \@@_hook_gput_code_do:nnn
3158 %<latexrelease> {#1} { legacy } { @#1hook }
3159 %<latexrelease> \@@_tl_gclear:c { @#1hook }
3160 %<latexrelease> }
3161 %<latexrelease> }
3162 %<latexrelease> }
3163 %<latexrelease>\EndIncludeInRelease
3164 % \end{macrocode}
3165 % \end{macro}
3166 %
3167 %
3168 %
3169 % \subsubsection{Disabling and providing hooks}
3170 %
3171 % \changes{v1.0p}{2021/08/20}{Renames of generic hook commands (gh/638)}
3172 %
3173 % \begin{macro}{\hook_disable_generic:n}
3174 % \begin{macro}{\@@_disable:n}
3175 % \begin{macro}[pTF]{\@@_if_disabled:n}
3176 %
3177 % Disables a hook by creating its
3178 % \cs[no-index]{g_@@_\meta{hook}_declared_tl} so that the hook
3179 % errors when used with \cs{hook_new:n}, then it undefines
3180 % \cs{@@\textvisiblespace\meta{hook}} so that it may not be executed.
3181 %
3182 % This does not clear any code that may be already stored in the
3183 % hook's structure, but doesn't allow adding more code.
3184 % \cs{@@_if_disabled:nTF} uses that specific combination to check
3185 % if the hook is disabled.
3186 %
3187 % \begin{macrocode}
3188 %<latexrelease>\IncludeInRelease{2021/06/01}{\hook_disable_generic:n}
3189 %<latexrelease> {Disable~hooks}
3190 % \end{macrocode}
3191 %
3192 % \begin{macrocode}
3193 \cs_new_protected:Npn \hook_disable_generic:n #1
3194 { \@@_normalize_hook_args:Nn \@@_disable:n {#1} }
3195 \cs_new_protected:Npn \@@_disable:n #1
3196 {
3197 \tl_gclear_new:c { g_@@_#1_declared_tl }
3198 \cs_undefine:c { @@~#1 }
3199 }
3200 \prg_new_conditional:Npnn \@@_if_disabled:n #1 { p, T, F, TF }
3201 {
3202 \bool_lazy_and:nnTF
3203 { \tl_if_exist_p:c { g_@@_#1_declared_tl } }
3204 { ! \cs_if_exist_p:c { @@~#1 } }
3205 { \prg_return_true: }
3206 { \prg_return_false: }
3207 }
3208 %<latexrelease>\EndIncludeInRelease
3209 % \end{macrocode}
3210 %
3211 % \begin{macrocode}
3212 %<latexrelease>\IncludeInRelease{2020/10/01}{\hook_disable_generic:n}
3213 %<latexrelease> {Disable~hooks}
3214 %<latexrelease>
3215 %<latexrelease>\cs_new_protected:Npn \hook_disable_generic:n #1 {}
3216 %<latexrelease>
3217 %<latexrelease>\EndIncludeInRelease
3218 % \end{macrocode}
3219 % \end{macro}
3220 % \end{macro}
3221 % \end{macro}
3222 %
3223 % \begin{macro}{\hook_activate_generic:n}
3224 % \begin{macro}{\@@_activate_generic:n}
3225 % The \cs{hook_activate_generic:n} declaration declares a new hook if it
3226 % wasn't declared already, in which case it only checks that the
3227 % already existing hook is not a reversed hook.
3228 % \changes{v1.1a}{2023/04/06}
3229 % {Changes to add hook arguments (hook-args).}
3230 % \begin{macrocode}
3231 %<latexrelease>\IncludeInRelease{2023/06/01}{\hook_activate_generic:n}
3232 %<latexrelease> {Providing~hooks}
3233 % \end{macrocode}
3234 %
3235 % \begin{macrocode}
3236 \cs_new_protected:Npn \hook_activate_generic:n #1
3237 { \@@_normalize_hook_args:Nn \@@_activate_generic:nn {#1} { } }
3238 % \end{macrocode}
3239 %
3240 % \begin{macrocode}
3241 \cs_new_protected:Npn \@@_activate_generic:nn #1 #2
3242 {
3243 % \end{macrocode}
3244 % If the hook to be activated was disabled we warn (for now --- this
3245 % may change).
3246 % \begin{macrocode}
3247 \@@_if_disabled:nTF {#1}
3248 { \msg_warning:nnn { hooks } { activate-disabled } {#1} }
3249 % \end{macrocode}
3250 % Otherwise we check if the hook is not declared, and if it isn't,
3251 % figure out if it's reversed or not, then declare it accordingly.
3252 % \begin{macrocode}
3253 {
3254 \@@_if_declared:nF {#1}
3255 {
3256 \tl_new:c { g_@@_#1_declared_tl }
3257 \@@_make_usable:nn {#1} { 0 }
3258 \tl_gset:ce { g_@@_#1_reversed_tl }
3259 { \@@_if_generic_reversed:nT {#1} { - } }
3260 % \end{macrocode}
3261 % Reflect that we have activated the generic hook and set its
3262 % execution code.
3263 % \changes{v1.0v}{2022/06/15}{Ensure that a newly activated generic hook
3264 % gets its execution code set}
3265 % \begin{macrocode}
3266 \@@_update_hook_code:n {#1}
3267 }
3268 }
3269 }
3270 % \end{macrocode}
3271 %
3272 % \begin{macrocode}
3273 %<latexrelease>\EndIncludeInRelease
3274 % \end{macrocode}
3275 %
3276 % \begin{macrocode}
3277 %<latexrelease>\IncludeInRelease{2021/06/01}{\hook_activate_generic:n}
3278 %<latexrelease> {Providing~hooks}
3279 %<latexrelease>\cs_gset_protected:Npn \@@_activate_generic:nn #1 #2
3280 %<latexrelease> {
3281 %<latexrelease> \@@_if_disabled:nTF {#1}
3282 %<latexrelease> { \msg_warning:nnn { hooks } { activate-disabled } {#1} }
3283 %<latexrelease> {
3284 %<latexrelease> \@@_if_declared:nF {#1}
3285 %<latexrelease> {
3286 %<latexrelease> \tl_new:c { g_@@_#1_declared_tl }
3287 %<latexrelease> \@@_make_usable:n {#1}
3288 %<latexrelease> \tl_gset:cx { g_@@_#1_reversed_tl }
3289 %<latexrelease> { \@@_if_generic_reversed:nT {#1} { - } }
3290 %<latexrelease> \@@_update_hook_code:n {#1}
3291 %<latexrelease> }
3292 %<latexrelease> }
3293 %<latexrelease> }
3294 %<latexrelease>\EndIncludeInRelease
3295 % \end{macrocode}
3296 %
3297 % \begin{macrocode}
3298 %<latexrelease>\IncludeInRelease{2020/10/01}{\hook_activate_generic:n}
3299 %<latexrelease> {Providing~hooks}
3300 %<latexrelease>\cs_gset_protected:Npn \hook_activate_generic:n #1 { }
3301 %<latexrelease>\EndIncludeInRelease
3302 % \end{macrocode}
3303 % \end{macro}
3304 % \end{macro}
3305 %
3306 %
3307 % \subsection{Parsing a label}
3308 %
3309 % \begin{macro}[EXP]{\@@_parse_label_default:nN}
3310 % This macro checks if a label was given (not \cs{c_novalue_tl}), and
3311 % if so, tries to parse the label looking for a leading \verb|.| to
3312 % replace by \cs{@@_currname_or_default:}.
3313 % |#2| is a boolean representing if |#1| is a label name.
3314 % \changes{v1.1i}{2024/08/09}
3315 % {Distinguish between empty label and empty hook (gh/1423)}
3316 % \begin{macrocode}
3317 \cs_new:Npn \@@_parse_label_default:nN #1#2
3318 {
3319 \tl_if_novalue:nTF {#1}
3320 { \@@_currname_or_default: }
3321 { \tl_trim_spaces_apply:nN {#1} \@@_parse_dot_label:nN #2 }
3322 }
3323 % \end{macrocode}
3324 % \end{macro}
3325 %
3326 % \begin{macro}[EXP]{\@@_parse_dot_label:nN}
3327 % \changes{v1.1i}{2024/08/09}
3328 % {Distinguish between empty label and empty hook (gh/1423)}
3329 % \begin{macro}[EXP]{
3330 % \@@_parse_dot_label:w,
3331 % \@@_parse_dot_label_cleanup:w,
3332 % \@@_parse_dot_label_aux:w
3333 % }
3334 % Start by checking if the label is empty, which raises an error, and
3335 % uses the fallback value. If not,
3336 % split the label at a \verb|./|, if any, and check if no tokens are
3337 % before the \verb|./|, or if the only character is a \verb|.|.
3338 % If these requirements are fulfilled, the leading
3339 % \verb|.| is replaced with \cs{@@_currname_or_default:}. Otherwise
3340 % the label is returned unchanged.
3341 % |#2| is a boolean representing if |#1| is a label name.
3342 % \begin{macrocode}
3343 \cs_new:Npn \@@_parse_dot_label:nN #1#2
3344 {
3345 \tl_if_empty:nTF {#1}
3346 {
3347 \bool_if:NTF #2
3348 { \msg_expandable_error:nn { hooks } { empty-label } }
3349 { \msg_expandable_error:nn { hooks } { empty-hook } }
3350 \@@_currname_or_default:
3351 }
3352 {
3353 \str_if_eq:nnTF {#1} { . }
3354 { \@@_currname_or_default: }
3355 { \@@_parse_dot_label:w #1 ./ \s_@@_mark }
3356 }
3357 }
3358 \cs_new:Npn \@@_parse_dot_label:w #1 ./ #2 \s_@@_mark
3359 {
3360 \tl_if_empty:nTF {#1}
3361 { \@@_parse_dot_label_aux:w #2 \s_@@_mark }
3362 {
3363 \tl_if_empty:nTF {#2}
3364 { \@@_make_name:n {#1} }
3365 { \@@_parse_dot_label_cleanup:w #1 ./ #2 \s_@@_mark }
3366 }
3367 }
3368 \cs_new:Npn \@@_parse_dot_label_cleanup:w #1 ./ \s_@@_mark {#1}
3369 \cs_new:Npn \@@_parse_dot_label_aux:w #1 ./ \s_@@_mark
3370 { \@@_currname_or_default: / \@@_make_name:n {#1} }
3371 % \end{macrocode}
3372 % \end{macro}
3373 % \end{macro}
3374 %
3375 % \begin{macro}[EXP]{\@@_currname_or_default:}
3376 % This uses \cs{g_@@_hook_curr_name_tl} if it is set, otherwise it tries
3377 % \cs{@currname}. If neither is set, it raises an error and uses the
3378 % fallback value \verb|label-missing|.
3379 % \begin{macrocode}
3380 \cs_new:Npn \@@_currname_or_default:
3381 {
3382 \tl_if_empty:NTF \g_@@_hook_curr_name_tl
3383 {
3384 \tl_if_empty:NTF \@currname
3385 {
3386 \msg_expandable_error:nnn { latex2e } { should-not-happen }
3387 { Empty~default~label. }
3388 \@@_make_name:n { label-missing }
3389 }
3390 { \@currname }
3391 }
3392 { \g_@@_hook_curr_name_tl }
3393 }
3394 % \end{macrocode}
3395 % \end{macro}
3396 %
3397 % \begin{macro}[EXP]{\@@_make_name:n,\@@_make_name:w}
3398 % This provides a standard sanitization of a hook's name.
3399 % It uses \cs{cs:w} to build a control sequence out of the hook name,
3400 % then uses \cs{cs_to_str:N} to get the string representation of that,
3401 % without the escape character. \cs{cs:w}-based expansion is used
3402 % instead of |e|-based because Unicode characters don't behave well
3403 % inside \cs{expanded}. The macro adds the \cs[no-index]{@@\textvisiblespace} prefix to the
3404 % hook name to reuse the hook's code token list to build the csname
3405 % and avoid leaving \enquote{public} control sequences defined
3406 % (as~\cs[no-index]{relax}) in TeX's memory.
3407 % \begin{macrocode}
3408 \cs_new:Npn \@@_make_name:n #1
3409 {
3410 \exp_after:wN \exp_after:wN \exp_after:wN \@@_make_name:w
3411 \exp_after:wN \token_to_str:N \cs:w @@~ #1 \cs_end:
3412 }
3413 \exp_last_unbraced:NNNNo
3414 \cs_new:Npn \@@_make_name:w #1 \tl_to_str:n { @@~ } { }
3415 % \end{macrocode}
3416 % \end{macro}
3417 %
3418 % \begin{macro}{\@@_normalize_hook_args:Nn}
3419 % \begin{macro}{\@@_normalize_hook_args:Nnn}
3420 % \begin{macro}{\@@_normalize_hook_rule_args:Nnnnn}
3421 % \begin{macro}{\@@_normalize_hook_args_aux:Nn}
3422 % This is the standard route for normalizing hook and label arguments. The main
3423 % macro does the entire operation within a group so that csnames made
3424 % by \cs{@@_make_name:n} are wiped off before continuing. This means
3425 % that this function cannot be used for \cs{hook_use:n}!
3426 % \changes{v1.1i}{2024/08/09}
3427 % {Distinguish between empty label and empty hook (gh/1423)}
3428 % \begin{macrocode}
3429 \cs_new_protected:Npn \@@_normalize_hook_args_aux:Nn #1 #2
3430 {
3431 \group_begin:
3432 \use:e
3433 {
3434 \group_end:
3435 \exp_not:N #1 #2
3436 }
3437 }
3438 \cs_new_protected:Npn \@@_normalize_hook_args:Nn #1 #2
3439 {
3440 \@@_normalize_hook_args_aux:Nn #1
3441 { { \@@_parse_label_default:nN {#2} \c_false_bool } }
3442 }
3443 \cs_new_protected:Npn \@@_normalize_hook_args:Nnn #1 #2 #3
3444 {
3445 \@@_normalize_hook_args_aux:Nn #1
3446 {
3447 { \@@_parse_label_default:nN {#2} \c_false_bool }
3448 { \@@_parse_label_default:nN {#3} \c_true_bool }
3449 }
3450 }
3451 \cs_new_protected:Npn \@@_normalize_hook_rule_args:Nnnnn #1 #2 #3 #4 #5
3452 {
3453 \@@_normalize_hook_args_aux:Nn #1
3454 {
3455 { \@@_parse_label_default:nN {#2} \c_false_bool }
3456 { \@@_parse_label_default:nN {#3} \c_true_bool }
3457 { \tl_trim_spaces:n {#4} }
3458 { \@@_parse_label_default:nN {#5} \c_true_bool }
3459 }
3460 }
3461 % \end{macrocode}
3462 % \end{macro}
3463 % \end{macro}
3464 % \end{macro}
3465 % \end{macro}
3466 %
3467 % \begin{macro}{\@@_curr_name_push:n,\@@_curr_name_push_aux:n}
3468 % \changes{v1.1o}{2026/03/10}{Support dot notation (gh/1162)}
3469 % \begin{macro}{\@@_curr_name_pop:}
3470 % \begin{macro}{\@@_end_document_label_check:}
3471 % The token list \cs{g_@@_hook_curr_name_tl} stores the name of the
3472 % current package/file to be used as the default label in hooks.
3473 % Providing a consistent interface is tricky because packages can
3474 % be loaded within packages, and some packages may not use
3475 % \cs{SetDefaultHookLabel} to change the default label (in which
3476 % case \cs{@currname} is used).
3477 %
3478 % To pull that one off, we keep a stack that contains the default
3479 % label for each level of input. The bottom of the stack contains the
3480 % default label for the |top-level| (this stack should never go
3481 % empty). If we're building the format, set the default label to be
3482 % |top-level|:
3483 % \begin{macrocode}
3484 \tl_gset:Nn \g_@@_hook_curr_name_tl { top-level }
3485 % \end{macrocode}
3486 %
3487 % Then, in case we're in \pkg{latexrelease} we push something on
3488 % the stack to support roll forward. But in some rare cases,
3489 % \pkg{latexrelease} may be loaded inside another package (notably
3490 % \pkg{platexrelease}), so we'll first push the |top-level| entry:
3491 % \changes{v1.0i}{2021/03/18}
3492 % {Only add \texttt{top-level} if not already there.}
3493 % \begin{macrocode}
3494 %<latexrelease>\seq_if_empty:NT \g_@@_name_stack_seq
3495 %<latexrelease> { \seq_gput_right:Nn \g_@@_name_stack_seq { top-level } }
3496 % \end{macrocode}
3497 % then we dissect the \cs{@currnamestack}, adding \cs{@currname} to
3498 % the stack:
3499 % \changes{v1.0f}{2020/11/24}{Support for roll forward (gh/434)}
3500 % \begin{macrocode}
3501 %<latexrelease>\cs_set_protected:Npn \@@_tmp:w #1 #2 #3
3502 %<latexrelease> {
3503 %<latexrelease> \quark_if_recursion_tail_stop:n {#1}
3504 %<latexrelease> \seq_gput_right:Nn \g_@@_name_stack_seq {#1}
3505 %<latexrelease> \@@_tmp:w
3506 %<latexrelease> }
3507 %<latexrelease>\exp_after:wN \@@_tmp:w \@currnamestack
3508 %<latexrelease> \q_recursion_tail \q_recursion_tail
3509 %<latexrelease> \q_recursion_tail \q_recursion_stop
3510 % \end{macrocode}
3511 % and finally set the default label to be the \cs{@currname}:
3512 % \changes{v1.0i}{2021/03/18}
3513 % {Remove the (empty) \enquote{top-level} from \cs{@currnamestack}.}
3514 % \begin{macrocode}
3515 %<latexrelease>\tl_gset:Nx \g_@@_hook_curr_name_tl { \@currname }
3516 %<latexrelease>\seq_gpop_right:NN \g_@@_name_stack_seq \l_@@_tmpa_tl
3517 % \end{macrocode}
3518 %
3519 % Two commands keep track of the stack: when a file is input,
3520 % \cs{@@_curr_name_push:n} pushes the current default label onto the
3521 % stack and sets the new default label (all in one go):
3522 % \begin{macrocode}
3523 \cs_new_protected:Npn \@@_curr_name_push:n #1
3524 {
3525 \exp_args:Ne \@@_curr_name_push_aux:n
3526 {
3527 \tl_if_blank:nF {#1}
3528 { \@@_parse_dot_label:nN {#1} \c_false_bool }
3529 }
3530 }
3531 \cs_new_protected:Npn \@@_curr_name_push_aux:n #1
3532 {
3533 \tl_if_blank:nTF {#1}
3534 { \msg_error:nn { hooks } { no-default-label } }
3535 {
3536 \str_if_eq:nnTF {#1} { top-level }
3537 {
3538 \msg_error:nnnnn { hooks } { set-top-level }
3539 { to } { PushDefaultHookLabel } {#1}
3540 }
3541 {
3542 \seq_gpush:NV \g_@@_name_stack_seq \g_@@_hook_curr_name_tl
3543 \tl_gset:Nn \g_@@_hook_curr_name_tl {#1}
3544 }
3545 }
3546 }
3547 % \end{macrocode}
3548 % and when an input is over, the topmost item of the stack is popped,
3549 % since that label will not be used again, and \cs{g_@@_hook_curr_name_tl}
3550 % is updated to equal the now topmost item of the stack:
3551 % \begin{macrocode}
3552 \cs_new_protected:Npn \@@_curr_name_pop:
3553 {
3554 \seq_gpop:NNTF \g_@@_name_stack_seq \l_@@_return_tl
3555 { \tl_gset_eq:NN \g_@@_hook_curr_name_tl \l_@@_return_tl }
3556 { \msg_error:nn { hooks } { extra-pop-label } }
3557 }
3558 % \end{macrocode}
3559 %
3560 % At the end of the document we want to check if there was no
3561 % \cs{@@_curr_name_push:n} without a matching \cs{@@_curr_name_pop:}
3562 % (not a critical error, but it might indicate that something else is
3563 % not quite right):
3564 % \begin{macrocode}
3565 \tl_gput_right:Nn \@kernel@after@enddocument@afterlastpage
3566 { \@@_end_document_label_check: }
3567 \cs_new_protected:Npn \@@_end_document_label_check:
3568 {
3569 \seq_gpop:NNT \g_@@_name_stack_seq \l_@@_return_tl
3570 {
3571 \msg_error:nne { hooks } { missing-pop-label }
3572 { \g_@@_hook_curr_name_tl }
3573 \tl_gset_eq:NN \g_@@_hook_curr_name_tl \l_@@_return_tl
3574 \@@_end_document_label_check:
3575 }
3576 }
3577 % \end{macrocode}
3578 %
3579 % The token list \cs{g_@@_hook_curr_name_tl} is but a mirror of the
3580 % top of the stack.
3581 %
3582 % \begin{macro}{\@@_set_default_hook_label:n,\@@_set_default_label:n}
3583 % Now define a wrapper that replaces the top of the stack with the
3584 % argument, and updates \cs{g_@@_hook_curr_name_tl} accordingly.
3585 % \begin{macrocode}
3586 \cs_new_protected:Npn \@@_set_default_hook_label:n #1
3587 {
3588 \seq_if_empty:NTF \g_@@_name_stack_seq
3589 {
3590 \msg_error:nnnnn { hooks } { set-top-level }
3591 { for } { SetDefaultHookLabel } {#1}
3592 }
3593 {
3594 \exp_args:Ne \@@_set_default_label:n
3595 {
3596 \tl_if_blank:nF {#1}
3597 { \@@_parse_dot_label:nN {#1} \c_false_bool }
3598 }
3599 }
3600 }
3601 \cs_new_protected:Npn \@@_set_default_label:n #1
3602 {
3603 \str_if_eq:nnTF {#1} { top-level }
3604 {
3605 \msg_error:nnnnn { hooks } { set-top-level }
3606 { to } { SetDefaultHookLabel } {#1}
3607 }
3608 { \tl_gset:Nn \g_@@_hook_curr_name_tl {#1} }
3609 }
3610 % \end{macrocode}
3611 % \end{macro}
3612 % \end{macro}
3613 % \end{macro}
3614 % \end{macro}
3615 %
3616 % \subsection{Adding or removing hook code}
3617 %
3618 % \begin{macro}{\hook_gput_code:nnn,\hook_gput_code_with_args:nnn}
3619 % \begin{macro}{
3620 % \@@_gput_code:nnn,
3621 % \@@_gput_code_store:nnn,
3622 % \@@_hook_gput_code_do:nnn,
3623 % \@@_prop_gput_labeled_cleanup:nnn,
3624 % \@@_prop_gput_labeled_do:Nnnn
3625 % }
3626 % \begin{macro}[EXP]{\@@_hash_check:nTF}
3627 % \changes{v1.1n}{2025/09/24}{New function}
3628 % \begin{macro}[EXP]{\@@_hash_check_aux:w}
3629 % \changes{v1.1n}{2025/09/24}{New function}
3630 %
3631 % With \cs{hook_gput_code:nnn}\Arg{hook}\Arg{label}\Arg{code} a
3632 % chunk of \meta{code} is added to an existing \meta{hook} labeled
3633 % with \meta{label}.
3634 % \changes{v1.0o}{2021/07/22}{Do not queue removals (gh/625)}
3635 % \changes{v1.1a}{2023/04/06}
3636 % {Add \cs{hook_gput_code_with_args:nnn} (hook-args).}
3637 % \begin{macrocode}
3638 %<latexrelease>\IncludeInRelease{2023/06/01}{\hook_gput_code:nnn}
3639 %<latexrelease> {Hooks~with~args}
3640 \cs_new_protected:Npn \hook_gput_code:nnn #1 #2 #3
3641 {
3642 \@@_replacing_args_false:
3643 \@@_normalize_hook_args:Nnn \@@_gput_code:nnn {#1} {#2} {#3}
3644 \@@_replacing_args_reset:
3645 }
3646 \cs_new_protected:Npn \hook_gput_code_with_args:nnn #1 #2 #3
3647 {
3648 \@@_replacing_args_true:
3649 \@@_normalize_hook_args:Nnn \@@_gput_code:nnn {#1} {#2} {#3}
3650 \@@_replacing_args_reset:
3651 }
3652 % \end{macrocode}
3653 %
3654 % If \cs{AddToHookWithArguments} was used, do some sanity checking,
3655 % and if it's not possible to use arguments at this point, fall back
3656 % to regular \cs{AddToHook} by using \cs{@@_replacing_args_false:}.
3657 % \changes{v1.1a}{2023/04/06}
3658 % {Changes to add hook arguments (hook-args).}
3659 % \begin{macrocode}
3660 \cs_new_protected:Npn \@@_gput_code:nnn #1 #2 #3
3661 {
3662 \@@_chk_args_allowed:nn {#1} { AddToHook }
3663 % \end{macrocode}
3664 % Then check if the code should be executed immediately, rather than
3665 % stored:
3666 % \changes{v1.0r}{2021/09/06}{Use dedicated conditional (gh/606)}
3667 % \begin{macrocode}
3668 \@@_if_execute_immediately:nTF {#1}
3669 {
3670 % \end{macrocode}
3671 % \cs{AddToHookWithArguments} can't be used on one-time hooks (that
3672 % were already used).
3673 % \begin{macrocode}
3674 \@@_if_replacing_args:TF
3675 {
3676 \msg_error:nnnn { hooks } { one-time-args }
3677 {#1} { AddToHook }
3678 }
3679 { }
3680 \use:n
3681 }
3682 { \@@_gput_code_store:nnn {#1} {#2} }
3683 {#3}
3684 }
3685 % \end{macrocode}
3686 %
3687 % \begin{macrocode}
3688 \cs_new_protected:Npn \@@_gput_code_store:nnn #1 #2 #3
3689 {
3690 % \end{macrocode}
3691 % Then check if the hook is usable.
3692 % \begin{macrocode}
3693 \@@_if_usable:nTF {#1}
3694 % \end{macrocode}
3695 % If so we simply add (or append) the new code to the property list
3696 % holding different chunks for the hook. At \verb=\begin{document}=
3697 % this is then sorted into a token list for fast execution.
3698 % \begin{macrocode}
3699 {
3700 \@@_hook_gput_code_do:nnn {#1} {#2} {#3}
3701 % \end{macrocode}
3702 % However, if there is an update within the document we need to alter
3703 % this execution code which is done by
3704 % \cs{@@_update_hook_code:n}. In the preamble this does nothing.
3705 % \begin{macrocode}
3706 \@@_update_hook_code:n {#1}
3707 }
3708 % \end{macrocode}
3709 %
3710 % If the hook is not usable, before giving up, check if it's not
3711 % disabled and otherwise try to declare it as a generic hook, if its
3712 % name matches one of the valid patterns.
3713 % \begin{macrocode}
3714 {
3715 \@@_if_disabled:nTF {#1}
3716 { \msg_error:nnn { hooks } { hook-disabled } {#1} }
3717 { \@@_try_declaring_generic_hook:nnn {#1} {#2} {#3} }
3718 }
3719 }
3720 % \end{macrocode}
3721 %
3722 % This macro will unconditionally add a chunk of code to the given hook.
3723 % \changes{v1.1a}{2023/04/06}
3724 % {Changes to add hook arguments (hook-args).}
3725 % \begin{macrocode}
3726 \cs_new_protected:Npn \@@_hook_gput_code_do:nnn #1 #2 #3
3727 {
3728 % \end{macrocode}
3729 % However, first some debugging info if debugging is enabled:
3730 % \changes{v1.1l}{2024/11/26}{Adjust debugging message (gh/1459)}
3731 % \begin{macrocode}
3732 \@@_debug:n{\iow_term:e{[lthooks]~ Add~ to~
3733 \@@_if_usable:nF {#1} { undeclared~ }
3734 hook~ '#1'~ (#2) \on@line
3735 ^^J [lthooks] \@spaces
3736 <-~ \tl_to_str:n{#3}} }
3737 % \end{macrocode}
3738 % Then try to get the code chunk labeled \verb=#2= from the hook.
3739 % If there's code already there, then append \verb=#3= to that,
3740 % otherwise just put \verb=#3=. If the current label is |top-level|,
3741 % the code is added to a dedicated token list
3742 % \cs{@@_toplevel\textvisiblespace\meta{hook}} that goes at the end of the
3743 % hook (or at the beginning, for a reversed hook), just before
3744 % \cs[no-index]{@@_next\textvisiblespace\meta{hook}}.
3745 % \begin{macrocode}
3746 \str_if_eq:nnTF {#2} { top-level }
3747 {
3748 \str_if_eq:eeTF { top-level } { \@@_currname_or_default: }
3749 {
3750 % \end{macrocode}
3751 % If the hook's basic structure does not exist, we need to declare it
3752 % with \cs{@@_init_structure:n}.
3753 % \begin{macrocode}
3754 \@@_init_structure:n {#1}
3755 % \end{macrocode}
3756 % Then append to the \verb|_toplevel| container for the hook.
3757 % \begin{macrocode}
3758 \@@_cs_gput_right:nnn { _toplevel } {#1} {#3}
3759 }
3760 { \msg_error:nnn { hooks } { misused-top-level } {#1} }
3761 }
3762 {
3763 % \end{macrocode}
3764 % \changes{v1.1n}{2025/09/24}{Only double hashes when needed}
3765 % When adding to the code pool, we have to double hashes if
3766 % \cs{AddToHook} was used (\verb|replacing_args| is false), so that
3767 % later it is turned into a single parameter token, rather than a
3768 % parameter to the hook macro. We skip this step if there are
3769 % no hashes at all in the argument: the token-by-token approach
3770 % otherwise becomes a major performance issue when the contents of
3771 % the hook are long.
3772 % \begin{macrocode}
3773 \exp_args:Ne \@@_prop_gput_labeled_cleanup:nnn
3774 {
3775 \@@_if_replacing_args:TF
3776 { \exp_not:n }
3777 {
3778 \@@_hash_check:nTF {#3}
3779 { \exp_not:n }
3780 { \@@_double_hashes:n }
3781 }
3782 {#3}
3783 }
3784 {#1} {#2}
3785 }
3786 }
3787 \cs_new:Npe \@@_hash_check:nTF #1
3788 {
3789 \exp_not:N \exp_after:wN \exp_not:N \@@_hash_check_aux:w
3790 \exp_not:N \tl_to_str:n {#1} \c_hash_str \c_hash_str
3791 \exp_not:N \q_stop
3792 }
3793 \use:e
3794 {
3795 \cs_new:Npn \exp_not:N \@@_hash_check_aux:w
3796 #1 \c_hash_str #2 \c_hash_str #3 \exp_not:N \q_stop
3797 }
3798 { \tl_if_empty:nTF {#3} }
3799 % \end{macrocode}
3800 %
3801 % Adds code to a hook's code pool.
3802 % \changes{v1.1a}{2023/04/06}
3803 % {Macro added (hook-args).}
3804 % \begin{macrocode}
3805 \cs_new_protected:Npn \@@_prop_gput_labeled_cleanup:nnn #1 #2 #3
3806 {
3807 \tl_set:Nn \l_@@_return_tl {#1}
3808 \@@_if_replacing_args:TF
3809 {
3810 \@@_if_usable:nT {#2}
3811 {
3812 \@@_set_normalise_fn:nn {#2}
3813 { Invalid~code~added~\msg_line_context: }
3814 \@@_normalise_fn:nn {#3} {#1}
3815 \prop_get:NnN \l_@@_work_prop {#3} \l_@@_return_tl
3816 }
3817 }
3818 { }
3819 \exp_args:NcV \@@_prop_gput_labeled_do:Nnn
3820 { g_@@_#2_code_prop } \l_@@_return_tl {#3}
3821 }
3822 \cs_new_protected:Npn \@@_prop_gput_labeled_do:Nnn #1 #2 #3
3823 {
3824 \prop_get:NnNTF #1 {#3} \l_@@_return_tl
3825 { \prop_gput:Nno #1 {#3} { \l_@@_return_tl #2 } }
3826 { \prop_gput:Nnn #1 {#3} {#2} }
3827 }
3828 % \end{macrocode}
3829 %
3830 % \begin{macrocode}
3831 %<latexrelease>\EndIncludeInRelease
3832 % \end{macrocode}
3833 %
3834 % \begin{macrocode}
3835 %<latexrelease>\IncludeInRelease{2020/10/01}{\hook_gput_code:nnn}
3836 %<latexrelease> {Providing~hooks}
3837 %<latexrelease>\cs_gset_protected:Npn \hook_gput_code:nnn #1 #2
3838 %<latexrelease> { \@@_normalize_hook_args:Nnn
3839 %<latexrelease> \@@_gput_code:nnn {#1} {#2} }
3840 %<latexrelease>\cs_gset_protected:Npn \@@_gput_code:nnn #1 #2 #3
3841 %<latexrelease> {
3842 %<latexrelease> \@@_if_execute_immediately:nTF {#1}
3843 %<latexrelease> {#3}
3844 %<latexrelease> {
3845 %<latexrelease> \@@_if_usable:nTF {#1}
3846 %<latexrelease> {
3847 %<latexrelease> \@@_hook_gput_code_do:nnn {#1} {#2} {#3}
3848 %<latexrelease> \@@_update_hook_code:n {#1}
3849 %<latexrelease> }
3850 %<latexrelease> {
3851 %<latexrelease> \@@_if_disabled:nTF {#1}
3852 %<latexrelease> { \msg_error:nnn { hooks } { hook-disabled } {#1} }
3853 %<latexrelease> { \@@_try_declaring_generic_hook:nnn
3854 %<latexrelease> {#1} {#2} {#3} }
3855 %<latexrelease> }
3856 %<latexrelease> }
3857 %<latexrelease> }
3858 %<latexrelease>\cs_gset_protected:Npn \@@_hook_gput_code_do:nnn #1 #2 #3
3859 %<latexrelease> {
3860 %<latexrelease> \@@_debug:n{\iow_term:x{****~ Add~ to~
3861 %<latexrelease> \@@_if_usable:nF {#1} { undeclared~ }
3862 %<latexrelease> hook~ #1~ (#2)
3863 %<latexrelease> \on@line\space <-~ \tl_to_str:n{#3}} }
3864 %<latexrelease> \str_if_eq:nnTF {#2} { top-level }
3865 %<latexrelease> {
3866 %<latexrelease> \str_if_eq:eeTF { top-level }
3867 %<latexrelease> { \@@_currname_or_default: }
3868 %<latexrelease> {
3869 %<latexrelease> \@@_init_structure:n {#1}
3870 %<latexrelease> \@@_tl_gput_right:cn { @@_toplevel~#1 } {#3}
3871 %<latexrelease> }
3872 %<latexrelease> { \msg_error:nnn { hooks } { misused-top-level } {#1} }
3873 %<latexrelease> }
3874 %<latexrelease> {
3875 %<latexrelease> \prop_get:cnNTF
3876 %<latexrelease> { g_@@_#1_code_prop } {#2} \l_@@_return_tl
3877 %<latexrelease> {
3878 %<latexrelease> \prop_gput:cno { g_@@_#1_code_prop } {#2}
3879 %<latexrelease> { \l_@@_return_tl #3 }
3880 %<latexrelease> }
3881 %<latexrelease> { \prop_gput:cnn { g_@@_#1_code_prop } {#2} {#3} }
3882 %<latexrelease> }
3883 %<latexrelease> }
3884 %<latexrelease>\cs_gset_protected:Npn \hook_gput_code_with_args:nnn #1#2#3 { }
3885 %<latexrelease>\EndIncludeInRelease
3886 % \end{macrocode}
3887 % \end{macro}
3888 % \end{macro}
3889 % \end{macro}
3890 % \end{macro}
3891 %
3892 % \begin{macro}{\@@_chk_args_allowed:nn}
3893 % This macro checks if it is possible to add code with references to
3894 % a hook's arguments for hook \verb|#1|. It only does something if
3895 % the function being run is \verb|replacing_args|. This macro will
3896 % error if the hook is declared and takes no arguments, then it will
3897 % set \cs{@@_replacing_args_false:} so that the macro which called it
3898 % will add the code normally.
3899 % \changes{v1.1a}{2023/04/06}
3900 % {Macro added (hook-args).}
3901 % \begin{macrocode}
3902 %<latexrelease>\IncludeInRelease{2023/06/01}{\@@_chk_args_allowed:nn}
3903 %<latexrelease> {Hooks~with~args}
3904 \cs_new_protected:Npn \@@_chk_args_allowed:nn #1 #2
3905 {
3906 \@@_if_replacing_args:TF
3907 {
3908 \@@_if_declared:nT {#1}
3909 { \tl_if_empty:cT { c_@@_#1_parameter_tl } { \use_ii:nn } }
3910 \use_none:n
3911 {
3912 \msg_error:nnnn { hooks } { without-args } {#1} {#2}
3913 \@@_replacing_args_false:
3914 }
3915 }
3916 { }
3917 }
3918 %<latexrelease>\EndIncludeInRelease
3919 %<latexrelease>\IncludeInRelease{2020/10/01}{\@@_chk_args_allowed:nn}
3920 %<latexrelease> {Hooks~with~args}
3921 %<latexrelease>\cs_undefine:N \@@_chk_args_allowed:nn
3922 %<latexrelease>\EndIncludeInRelease
3923 % \end{macrocode}
3924 % \end{macro}
3925 %
3926 % \begin{macro}{\@@_gput_undeclared_hook:nnn}
3927 % Often it may happen that a package $A$ defines a hook \verb=foo=,
3928 % but package $B$, that adds code to that hook, is loaded before $A$.
3929 % In such case we need to add code to the hook before it is declared.
3930 % An implicitly declared hook doesn't have arguments (in principle),
3931 % so use \cs{c_false_bool} here.
3932 % \begin{macrocode}
3933 \cs_new_protected:Npn \@@_gput_undeclared_hook:nnn #1 #2 #3
3934 {
3935 \@@_init_structure:n {#1}
3936 \@@_hook_gput_code_do:nnn {#1} {#2} {#3}
3937 }
3938 % \end{macrocode}
3939 % \end{macro}
3940 %
3941 % \begin{macro}{\@@_try_declaring_generic_hook:nnn,
3942 % \@@_try_declaring_generic_next_hook:nn}
3943 %
3944 % These entry-level macros just pass the arguments along to the
3945 % common \cs{@@_try_declaring_generic_hook:nNNnn} with the right
3946 % functions to execute when some action is to be taken.
3947 %
3948 % The wrapper \cs{@@_try_declaring_generic_hook:nnn} then defers
3949 % \cs{hook_gput_code:nnn} if the generic hook was declared, or to
3950 % \cs{@@_gput_undeclared_hook:nnn} otherwise (the hook was tested for
3951 % existence before, so at this point if it isn't generic, it doesn't
3952 % exist).
3953 %
3954 % The wrapper \cs{@@_try_declaring_generic_next_hook:nn} for
3955 % next-execution hooks does the same: it defers the code to
3956 % \cs{hook_gput_next_code:nn} if the generic hook was declared, or
3957 % to \cs{@@_gput_next_do:nn} otherwise.
3958 % \changes{v1.0p}{2021/08/25}{Standardize generic hook names (gh/648)}
3959 % \changes{v1.1d}{2023/05/21}
3960 % {Changes to allow support arguments in cmd hooks (cmd-args).}
3961 % \begin{macrocode}
3962 %<latexrelease>\IncludeInRelease{2023/06/01}
3963 %<latexrelease> {\@@_try_declaring_generic_hook:nnn}
3964 %<latexrelease> {Hooks~with~args}
3965 \cs_new_protected:Npn \@@_try_declaring_generic_hook:nnn #1
3966 {
3967 \@@_try_declaring_generic_hook:wnTF #1 / / / \scan_stop: {#1}
3968 \@@_gput_code:nnn
3969 \@@_gput_undeclared_hook:nnn
3970 {#1}
3971 }
3972 \cs_new_protected:Npn \@@_try_declaring_generic_next_hook:nn #1
3973 {
3974 \@@_try_declaring_generic_hook:wnTF #1 / / / \scan_stop: {#1}
3975 \@@_gput_next_code:nn
3976 \@@_gput_next_do:nn
3977 {#1}
3978 }
3979 %<latexrelease>\EndIncludeInRelease
3980 %<latexrelease>\IncludeInRelease{2021/11/15}
3981 %<latexrelease> {\@@_try_declaring_generic_hook:nnn}
3982 %<latexrelease> {Standardize~generic~hook~names}
3983 %<latexrelease>\cs_gset_protected:Npn \@@_try_declaring_generic_hook:nnn #1
3984 %<latexrelease> {
3985 %<latexrelease> \@@_try_declaring_generic_hook:wnTF #1 / / / \scan_stop:
3986 %<latexrelease> {#1}
3987 %<latexrelease> \hook_gput_code:nnn
3988 %<latexrelease> \@@_gput_undeclared_hook:nnn
3989 %<latexrelease> {#1}
3990 %<latexrelease> }
3991 %<latexrelease>\cs_gset_protected:Npn
3992 %<latexrelease> \@@_try_declaring_generic_next_hook:nn #1
3993 %<latexrelease> {
3994 %<latexrelease> \@@_try_declaring_generic_hook:wnTF #1 / / / \scan_stop:
3995 %<latexrelease> {#1}
3996 %<latexrelease> \hook_gput_next_code:nn
3997 %<latexrelease> \@@_gput_next_do:nn
3998 %<latexrelease> {#1}
3999 %<latexrelease> }
4000 %<latexrelease>\EndIncludeInRelease
4001 %<latexrelease>\IncludeInRelease{2020/10/01}
4002 %<latexrelease> {\@@_try_declaring_generic_hook:nnn}
4003 %<latexrelease> {Standardize~generic~hook~names}
4004 %<latexrelease>\cs_new_protected:Npn
4005 %<latexrelease> \@@_try_declaring_generic_hook:nnn #1
4006 %<latexrelease> {
4007 %<latexrelease> \@@_try_declaring_generic_hook:nNNnn {#1}
4008 %<latexrelease> \hook_gput_code:nnn \@@_gput_undeclared_hook:nnn
4009 %<latexrelease> }
4010 %<latexrelease>\cs_new_protected:Npn
4011 %<latexrelease> \@@_try_declaring_generic_next_hook:nn #1
4012 %<latexrelease> {
4013 %<latexrelease> \@@_try_declaring_generic_hook:nNNnn {#1}
4014 %<latexrelease> \hook_gput_next_code:nn \@@_gput_next_do:nn
4015 %<latexrelease> }
4016 % \end{macrocode}
4017 % \end{macro}
4018 %
4019 % \begin{macro}{\@@_try_declaring_generic_hook:nNNnn,
4020 % \@@_try_declaring_generic_hook_split:nNNnn}
4021 %
4022 % \cs{@@_try_declaring_generic_hook:nNNnn}
4023 % now splits the hook name
4024 % at the first \texttt{/} (if any) and first checks if it is a
4025 % file-specific hook (they require some normalization) using
4026 % \cs{@@_if_file_hook:wTF}. If not then check it is one of a
4027 % predefined set for generic names. We also split off the second
4028 % component to see if we have to make a reversed hook. In either case
4029 % the function returns \meta{true} for a generic hook and \meta{false}
4030 % in other cases.
4031 %
4032 % \changes{v1.0s}{2021/09/28}
4033 % {Correct usage of older \cs{__hook_if_file_hook:wTF} (gh/675)}
4034 % \changes{v1.1h}{2024/01/24}
4035 % {Correct usage of older \cs{__hook_if_file_hook:wTF} (gh/1243)}
4036 % \begin{macrocode}
4037 %<latexrelease>\cs_new_protected:Npn \@@_try_declaring_generic_hook:nNNnn #1
4038 %<latexrelease> {
4039 %<latexrelease> \@@_if_file_hook:wTF #1 / / \s_@@_mark
4040 %<latexrelease> {
4041 %<latexrelease> \exp_args:Ne
4042 %<latexrelease> \@@_try_declaring_generic_hook_split:nNNnn
4043 %<latexrelease> { \exp_args:Ne \@@_file_hook_normalize:n {#1} }
4044 %<latexrelease> }
4045 %<latexrelease> { \@@_try_declaring_generic_hook_split:nNNnn {#1} }
4046 %<latexrelease> }
4047 % \end{macrocode}
4048 %
4049 % \begin{macrocode}
4050 %<latexrelease>\cs_new_protected:Npn
4051 %<latexrelease> \@@_try_declaring_generic_hook_split:nNNnn #1 #2 #3
4052 %<latexrelease> {
4053 %<latexrelease> \@@_try_declaring_generic_hook:wnTF #1 / / / \scan_stop:
4054 %<latexrelease> {#1}
4055 %<latexrelease> { #2 }
4056 %<latexrelease> { #3 } {#1}
4057 %<latexrelease> }
4058 %<latexrelease>\EndIncludeInRelease
4059 % \end{macrocode}
4060 % \end{macro}
4061 %
4062 % \begin{macro}[TF]{\@@_try_declaring_generic_hook:wn}
4063 % \changes{v1.1a}{2023/04/06}
4064 % {Changes to add hook arguments (hook-args).}
4065 % \begin{macrocode}
4066 %<latexrelease>\IncludeInRelease{2023/06/01}
4067 %<latexrelease> {\@@_try_declaring_generic_hook:wn}
4068 %<latexrelease> {Hooks~with~args}
4069 \prg_new_protected_conditional:Npnn
4070 \@@_try_declaring_generic_hook:wn
4071 #1 / #2 / #3 / #4 \scan_stop: #5 { TF }
4072 {
4073 \@@_if_generic:nTF {#5}
4074 {
4075 \@@_if_usable:nF {#5}
4076 {
4077 % \end{macrocode}
4078 % If the hook doesn't exist yet we check if it is a \texttt{cmd}
4079 % hook and if so we attempt patching the command in addition to
4080 % declaring the hook.
4081 %
4082 % For some commands this will not be possible, in which case
4083 % \cs{@@_patch_cmd_or_delay:Nnn} (defined in \texttt{ltcmdhooks})
4084 % will generate an appropriate error message.
4085 % \changes{v1.1d}{2023/05/21}
4086 % {Changes to allow support arguments in cmd hooks (cmd-args).}
4087 % \begin{macrocode}
4088 \str_if_eq:nnT {#1} { cmd }
4089 {
4090 \@@_try_put_cmd_hook:n {#5}
4091 \@@_make_usable:nn {#5} { 9 }
4092 \use_none:nnn
4093 }
4094 % \end{macrocode}
4095 %
4096 % Declare the hook always even if it can't really be used (error
4097 % message generated elsewhere).
4098 %
4099 % Here we use \cs{@@_make_usable:nn}, so that a \cs{hook_new:n} is
4100 % still possible later. Generic hooks (except \hook{cmd} hooks) take
4101 % no arguments, so use zero as the second argument.
4102 % \begin{macrocode}
4103 \@@_make_usable:nn {#5} { 0 }
4104 }
4105 \@@_if_generic_reversed:nT {#5}
4106 { \tl_gset:cn { g_@@_#5_reversed_tl } { - } }
4107 \prg_return_true:
4108 }
4109 {
4110 % \end{macrocode}
4111 %
4112 % Generic hooks are all named \meta{type}/\meta{name}/\meta{place},
4113 % where \meta{type} and \meta{place} are predefined
4114 % (\cs{c_@@_generic_\meta{type}/./\meta{place}_tl}), and \meta{name}
4115 % is the variable component. Older releases had some hooks with the
4116 % \meta{name} in the third part, so the code below supports that
4117 % syntax for a while, with a warning.
4118 %
4119 % The \cs{exp_after:wN} |...| \cs{exp:w} trick is there to remove the
4120 % conditional structure inserted by
4121 % \cs{@@_try_declaring_generic_hook:wnTF} and thus allow access to the
4122 % tokens that follow it, as is needed to keep things going.
4123 %
4124 % When the deprecation cycle ends, the lines below should all be
4125 % replaced by \cs{prg_return_false:}.
4126 % \begin{macrocode}
4127 \@@_if_deprecated_generic:nTF {#5}
4128 {
4129 \@@_deprecated_generic_warn:n {#5}
4130 \exp_after:wN \@@_declare_deprecated_generic:NNn
4131 \exp:w % \exp_end:
4132 }
4133 { \prg_return_false: }
4134 }
4135 }
4136 % \end{macrocode}
4137 %
4138 % \begin{macro}{
4139 % \@@_deprecated_generic_warn:Nn,
4140 % \@@_deprecated_generic_warn:Nw,
4141 % }
4142 % \cs{@@_deprecated_generic_warn:n} will issue a deprecation warning
4143 % for a given hook, and mark that hook such that the warning will not
4144 % be issued again (multiple warnings can be issued, but only once per
4145 % hook).
4146 % \begin{macrocode}
4147 \cs_new_protected:Npn \@@_deprecated_generic_warn:n #1
4148 { \@@_deprecated_generic_warn:w #1 \s_@@_mark }
4149 \cs_new_protected:Npn \@@_deprecated_generic_warn:w
4150 #1 / #2 / #3 \s_@@_mark
4151 {
4152 \if_cs_exist:w @@~#1/#2/#3 \cs_end: \else:
4153 \msg_warning:nnnnn { hooks } { generic-deprecated } {#1} {#2} {#3}
4154 \fi:
4155 \cs_gset_eq:cN { @@~#1/#2/#3 } \scan_stop:
4156 }
4157 % \end{macrocode}
4158 % \end{macro}
4159 %
4160 % \begin{macro}{
4161 % \@@_do_deprecated_generic:Nn,
4162 % \@@_do_deprecated_generic:Nw,
4163 % \@@_declare_deprecated_generic:NNw,
4164 % \@@_declare_deprecated_generic:NNw,
4165 % }
4166 % Now that the user has been told about the deprecation, we proceed by
4167 % swapping \meta{name} and \meta{place} and adding the code to the
4168 % correct hook.
4169 % \begin{macrocode}
4170 \cs_new_protected:Npn \@@_do_deprecated_generic:Nn #1 #2
4171 { \@@_do_deprecated_generic:Nw #1 #2 \s_@@_mark }
4172 \cs_new_protected:Npn \@@_do_deprecated_generic:Nw #1
4173 #2 / #3 / #4 \s_@@_mark
4174 { #1 { #2 / #4 / #3 } }
4175 \cs_new_protected:Npn \@@_declare_deprecated_generic:NNn #1 #2 #3
4176 { \@@_declare_deprecated_generic:NNw #1 #2 #3 \s_@@_mark }
4177 \cs_new_protected:Npn \@@_declare_deprecated_generic:NNw #1 #2
4178 #3 / #4 / #5 \s_@@_mark
4179 {
4180 \@@_try_declaring_generic_hook:wnTF #3 / #5 / #4 / \scan_stop:
4181 { #3 / #5 / #4 }
4182 #1 #2 { #3 / #5 / #4 }
4183 }
4184 %<latexrelease>\EndIncludeInRelease
4185 % \end{macrocode}
4186 % \end{macro}
4187 %
4188 % \begin{macrocode}
4189 %<latexrelease>\IncludeInRelease{2021/11/15}
4190 %<latexrelease> {\@@_try_declaring_generic_hook:wn}
4191 %<latexrelease> {Standardize~generic~hook~names}
4192 %<latexrelease>\prg_new_protected_conditional:Npnn
4193 %<latexrelease> \@@_try_declaring_generic_hook:wn
4194 %<latexrelease> #1 / #2 / #3 / #4 \scan_stop: #5 { TF }
4195 %<latexrelease> {
4196 %<latexrelease> \@@_if_generic:nTF {#5}
4197 %<latexrelease> {
4198 %<latexrelease> \@@_if_usable:nF {#5}
4199 %<latexrelease> {
4200 %<latexrelease> \str_if_eq:nnT {#1} { cmd }
4201 %<latexrelease> { \@@_try_put_cmd_hook:n {#5} }
4202 %<latexrelease> \@@_make_usable:n {#5}
4203 %<latexrelease> }
4204 %<latexrelease> \@@_if_generic_reversed:nT {#5}
4205 %<latexrelease> { \tl_gset:cn { g_@@_#5_reversed_tl } { - } }
4206 %<latexrelease> \prg_return_true:
4207 %<latexrelease> }
4208 %<latexrelease> {
4209 %<latexrelease> \@@_if_deprecated_generic:nTF {#5}
4210 %<latexrelease> {
4211 %<latexrelease> \@@_deprecated_generic_warn:n {#5}
4212 %<latexrelease> \exp_after:wN \@@_declare_deprecated_generic:NNn
4213 %<latexrelease> \exp:w % \exp_end:
4214 %<latexrelease> }
4215 %<latexrelease> { \prg_return_false: }
4216 %<latexrelease> }
4217 %<latexrelease> }
4218 %<latexrelease>\EndIncludeInRelease
4219 % \end{macrocode}
4220 %
4221 % \begin{macrocode}
4222 %<latexrelease>\IncludeInRelease{2021/06/01}
4223 %<latexrelease> {\@@_try_declaring_generic_hook:wn}
4224 %<latexrelease> {Support~cmd~hooks}
4225 %<latexrelease>\prg_new_protected_conditional:Npnn
4226 %<latexrelease> \@@_try_declaring_generic_hook:wn
4227 %<latexrelease> #1 / #2 / #3 / #4 \scan_stop: #5 { TF }
4228 %<latexrelease> {
4229 %<latexrelease> \tl_if_empty:nTF {#2}
4230 %<latexrelease> { \prg_return_false: }
4231 %<latexrelease> {
4232 %<latexrelease> \prop_if_in:NnTF \c_@@_generics_prop {#1}
4233 %<latexrelease> {
4234 %<latexrelease> \@@_if_usable:nF {#5}
4235 %<latexrelease> {
4236 %<latexrelease> \str_if_eq:nnT {#1} { cmd }
4237 %<latexrelease> { \@@_try_put_cmd_hook:n {#5} }
4238 %<latexrelease> \@@_make_usable:n {#5}
4239 %<latexrelease> }
4240 %<latexrelease> \prop_if_in:NnTF
4241 %<latexrelease> \c_@@_generics_reversed_ii_prop {#2}
4242 %<latexrelease> { \tl_gset:cn { g_@@_#5_reversed_tl } { - } }
4243 %<latexrelease> {
4244 %<latexrelease> \prop_if_in:NnT
4245 %<latexrelease> \c_@@_generics_reversed_iii_prop {#3}
4246 %<latexrelease> { \tl_gset:cn { g_@@_#5_reversed_tl } { - } }
4247 %<latexrelease> }
4248 %<latexrelease> \prg_return_true:
4249 %<latexrelease> }
4250 %<latexrelease> { \prg_return_false: }
4251 %<latexrelease> }
4252 %<latexrelease> }
4253 %<latexrelease>\EndIncludeInRelease
4254 % \end{macrocode}
4255 %
4256 % \begin{macrocode}
4257 %<latexrelease>\IncludeInRelease{2020/10/01}
4258 %<latexrelease> {\@@_try_declaring_generic_hook:wn}
4259 %<latexrelease> {Support~cmd~hooks}
4260 %<latexrelease>\prg_new_protected_conditional:Npnn
4261 %<latexrelease> \@@_try_declaring_generic_hook:wn
4262 %<latexrelease> #1 / #2 / #3 / #4 \scan_stop: #5 { TF }
4263 %<latexrelease> {
4264 %<latexrelease> \tl_if_empty:nTF {#2}
4265 %<latexrelease> { \prg_return_false: }
4266 %<latexrelease> {
4267 %<latexrelease> \prop_if_in:NnTF \c_@@_generics_prop {#1}
4268 %<latexrelease> {
4269 %<latexrelease> \@@_if_declared:nF {#5} { \hook_new:n {#5} }
4270 %<latexrelease> \prop_if_in:NnTF
4271 %<latexrelease> \c_@@_generics_reversed_ii_prop {#2}
4272 %<latexrelease> { \tl_gset:cn { g_@@_#5_reversed_tl } { - } }
4273 %<latexrelease> {
4274 %<latexrelease> \prop_if_in:NnT
4275 %<latexrelease> \c_@@_generics_reversed_iii_prop {#3}
4276 %<latexrelease> { \tl_gset:cn { g_@@_#5_reversed_tl } { - } }
4277 %<latexrelease> }
4278 %<latexrelease> \prg_return_true:
4279 %<latexrelease> }
4280 %<latexrelease> { \prg_return_false: }
4281 %<latexrelease> }
4282 %<latexrelease> }
4283 %<latexrelease>\EndIncludeInRelease
4284 % \end{macrocode}
4285 % \end{macro}
4286 %
4287 %
4288 % \begin{macro}[pTF]{\@@_if_file_hook:w}
4289 % \cs{@@_if_file_hook:wTF} checks if the argument is a valid
4290 % file-specific hook (not, for example, |file/before|, but
4291 % |file/foo.tex/before|). If it is a file-specific hook, then it
4292 % executes the \meta{true} branch, otherwise \meta{false}.
4293 % \begin{macrocode}
4294 %<latexrelease>\IncludeInRelease{2021/11/15}{\@@_if_file_hook:w}
4295 %<latexrelease> {Standardize~generic~hook~names}
4296 %<latexrelease>\EndIncludeInRelease
4297 %<latexrelease>\IncludeInRelease{2020/10/01}{\@@_if_file_hook:w}
4298 %<latexrelease> {Standardize~generic~hook~names}
4299 %<latexrelease>\prg_new_conditional:Npnn \@@_if_file_hook:w
4300 %<latexrelease> #1 / #2 / #3 \s_@@_mark { TF }
4301 %<latexrelease> {
4302 %<latexrelease> \str_if_eq:nnTF {#1} { file }
4303 %<latexrelease> {
4304 %<latexrelease> \bool_lazy_or:nnTF
4305 %<latexrelease> { \tl_if_empty_p:n {#3} }
4306 %<latexrelease> { \str_if_eq_p:nn {#3} { / } }
4307 %<latexrelease> { \prg_return_false: }
4308 %<latexrelease> {
4309 %<latexrelease> \prop_if_in:NnTF \c_@@_generics_file_prop {#2}
4310 %<latexrelease> { \prg_return_true: }
4311 %<latexrelease> { \prg_return_false: }
4312 %<latexrelease> }
4313 %<latexrelease> }
4314 %<latexrelease> { \prg_return_false: }
4315 %<latexrelease> }
4316 %<latexrelease>\EndIncludeInRelease
4317 % \end{macrocode}
4318 % \end{macro}
4319 %
4320 % \begin{macro}[EXP]{\@@_file_hook_normalize:n}
4321 % \begin{macro}[EXP]{\@@_strip_double_slash:n,\@@_strip_double_slash:w}
4322 %
4323 % \begin{macrocode}
4324 %<latexrelease>\IncludeInRelease{2021/11/15}{\@@_file_hook_normalize:n}
4325 %<latexrelease> {Standardize~generic~hook~names}
4326 %<latexrelease>\EndIncludeInRelease
4327 % \end{macrocode}
4328 %
4329 % When a file-specific hook is found, before being declared it is
4330 % lightly normalized by \cs{@@_file_hook_normalize:n}. The current
4331 % implementation just replaces two consecutive slashes (|//|) by a
4332 % single one, to cope with simple cases where the user did something
4333 % like \verb|\def\input@path{{./mypath/}}|, in which case a hook would
4334 % have to be \verb|\AddToHook{file/./mypath//file.tex/after}|.
4335 % \begin{macrocode}
4336 %<latexrelease>\IncludeInRelease{2020/10/01}{\@@_file_hook_normalize:n}
4337 %<latexrelease> {Standardize~generic~hook~names}
4338 %<latexrelease>\cs_new:Npn \@@_file_hook_normalize:n #1
4339 %<latexrelease> { \@@_strip_double_slash:n {#1} }
4340 %<latexrelease>\cs_new:Npn \@@_strip_double_slash:n #1
4341 %<latexrelease> { \@@_strip_double_slash:w #1 // \s_@@_mark }
4342 % \end{macrocode}
4343 % This function is always called after testing if the argument is a
4344 % file hook with \cs{@@_if_file_hook:wTF}, so we can assume it has
4345 % three parts (it is either \verb|file/.../before| or
4346 % \verb|file/.../after|), so we use \verb|#1/#2/#3 //| instead of just
4347 % \verb|#1 //| to prevent losing a slash if the file name is empty.
4348 % \changes{v1.0h}{2021/01/07}{Assume hook name has at least three
4349 % nonempty parts (gh/464)}
4350 % \begin{macrocode}
4351 %<latexrelease>\cs_new:Npn \@@_strip_double_slash:w #1/#2/#3//#4\s_@@_mark
4352 %<latexrelease> {
4353 %<latexrelease> \tl_if_empty:nTF {#4}
4354 %<latexrelease> { #1/#2/#3 }
4355 %<latexrelease> { \@@_strip_double_slash:w #1/#2/#3 /#4\s_@@_mark }
4356 %<latexrelease> }
4357 %<latexrelease>\EndIncludeInRelease
4358 % \end{macrocode}
4359 % \end{macro}
4360 % \end{macro}
4361 %
4362 % \begin{macro}{
4363 % \c_@@_generic_cmd/./before_tl,\c_@@_generic_cmd/./after_tl,
4364 % \c_@@_generic_env/./before_tl,\c_@@_generic_env/./after_tl,
4365 % \c_@@_generic_file/./before_tl,\c_@@_generic_file/./after_tl,
4366 % \c_@@_generic_package/./before_tl,\c_@@_generic_package/./after_tl,
4367 % \c_@@_generic_class/./before_tl,\c_@@_generic_class/./after_tl,
4368 % \c_@@_generic_include/./before_tl,\c_@@_generic_include/./after_tl,
4369 % \c_@@_generic_env/./begin_tl,\c_@@_generic_env/./end_tl,
4370 % \c_@@_generic_include/./end_tl
4371 % }
4372 % Token lists defining the possible generic hooks. We don't provide
4373 % any user interface to this as this is meant to be static.
4374 % \begin{description}
4375 % \item[\texttt{cmd}]
4376 % The generic hooks used for commands.
4377 % \item[\texttt{env}]
4378 % The generic hooks used in \cs{begin} and \cs{end}.
4379 % \item[\texttt{file}, \texttt{package}, \texttt{class}, \texttt{include}]
4380 % The generic hooks used when loading a file
4381 % \end{description}
4382 % \begin{macrocode}
4383 %<latexrelease>\IncludeInRelease{2021/11/15}{\c_@@_generics_prop}
4384 %<latexrelease> {Standardize~generic~hook~names}
4385 \clist_map_inline:nn { cmd , env , file , package , class , include }
4386 {
4387 \tl_const:cn { c_@@_generic_#1/./before_tl } { + }
4388 \tl_const:cn { c_@@_generic_#1/./after_tl } { - }
4389 }
4390 \tl_const:cn { c_@@_generic_env/./begin_tl } { + }
4391 \tl_const:cn { c_@@_generic_env/./end_tl } { + }
4392 % \end{macrocode}
4393 %
4394 % \changes{v1.0t}{2022/04/01}{Support generic \texttt{include/.../excluded} hooks}
4395 % \begin{macrocode}
4396 \tl_const:cn { c_@@_generic_include/./end_tl } { - }
4397 \tl_const:cn { c_@@_generic_include/./excluded_tl } { + }
4398 % \end{macrocode}
4399 %
4400 % Deprecated generic hooks:
4401 % \begin{macrocode}
4402 \clist_map_inline:nn { file , package , class , include }
4403 {
4404 \tl_const:cn { c_@@_deprecated_#1/./before_tl } { }
4405 \tl_const:cn { c_@@_deprecated_#1/./after_tl } { }
4406 }
4407 \tl_const:cn { c_@@_deprecated_include/./end_tl } { }
4408 %<latexrelease>\EndIncludeInRelease
4409 % \end{macrocode}
4410 %
4411 % \begin{macrocode}
4412 %<latexrelease>\IncludeInRelease{2020/10/01}{\c_@@_generics_prop}
4413 %<latexrelease> {Standardize~generic~hook~names}
4414 %<latexrelease>\prop_const_from_keyval:Nn \c_@@_generics_prop
4415 %<latexrelease> {cmd=,env=,file=,package=,class=,include=}
4416 %<latexrelease>\EndIncludeInRelease
4417 % \end{macrocode}
4418 % \end{macro}
4419 %
4420 % \begin{macro}{\c_@@_generics_reversed_ii_prop,
4421 % \c_@@_generics_reversed_iii_prop,
4422 % \c_@@_generics_file_prop}
4423 % The following generic hooks are supposed to use reverse ordering
4424 % (the |ii| and |iii| names are kept for the deprecation cycle):
4425 % \begin{macrocode}
4426 %<latexrelease>\IncludeInRelease{2021/11/15}{\c_@@_generics_reversed_ii_prop}
4427 %<latexrelease> {Standardize~generic~hook~names}
4428 %<latexrelease>\EndIncludeInRelease
4429 % \end{macrocode}
4430 %
4431 % \begin{macrocode}
4432 %<latexrelease>\IncludeInRelease{2020/10/01}{\c_@@_generics_reversed_ii_prop}
4433 %<latexrelease> {Standardize~generic~hook~names}
4434 %<latexrelease>\prop_const_from_keyval:Nn
4435 %<latexrelease> \c_@@_generics_reversed_ii_prop {after=,end=}
4436 %<latexrelease>\prop_const_from_keyval:Nn
4437 %<latexrelease> \c_@@_generics_reversed_iii_prop {after=}
4438 %<latexrelease>\prop_const_from_keyval:Nn
4439 %<latexrelease> \c_@@_generics_file_prop {before=,after=}
4440 %<latexrelease>\EndIncludeInRelease
4441 % \end{macrocode}
4442 % \end{macro}
4443 %
4444 %
4445 % \begin{macro}{
4446 % \c_@@_parameter_cmd/./before_tl,\c_@@_parameter_cmd/./after_tl,
4447 % }
4448 % Token lists defining the number of arguments for a given type of
4449 % generic hook.
4450 % \changes{v1.1d}{2023/05/21}
4451 % {Token lists added (cmd-args).}
4452 % \begin{macrocode}
4453 %<latexrelease>\IncludeInRelease{2023/06/01}{\c_@@_parameter_cmd/./before_tl}
4454 %<latexrelease> {Hooks~with~args}
4455 % \end{macrocode}
4456 %
4457 % \hook{cmd} hooks are declared with 9 arguments because they have a
4458 % variable number of arguments (depending on the command they are
4459 % attached to), so we use the maximum here.
4460 % \begin{macrocode}
4461 \tl_const:cn { c_@@_parameter_cmd/./before_tl } { #1#2#3#4#5#6#7#8#9 }
4462 \tl_const:cn { c_@@_parameter_cmd/./after_tl } { #1#2#3#4#5#6#7#8#9 }
4463 % \end{macrocode}
4464 %
4465 % \begin{macrocode}
4466 %<latexrelease>\EndIncludeInRelease
4467 %<latexrelease>\IncludeInRelease{2020/10/01}{\c_@@_parameter_cmd/./before_tl}
4468 %<latexrelease> {Hooks~with~args}
4469 %<latexrelease>\EndIncludeInRelease
4470 % \end{macrocode}
4471 % \end{macro}
4472 %
4473 % \begin{macro}{\hook_gremove_code:nn}
4474 % \begin{macro}{\@@_gremove_code:nn}
4475 %
4476 % With \cs{hook_gremove_code:nn}\Arg{hook}\Arg{label} any code
4477 % for \meta{hook} stored under \meta{label} is removed.
4478 % \changes{v1.0o}{2021/07/22}{Do not queue removals (gh/625)}
4479 % \changes{v1.1a}{2023/04/06}
4480 % {Changes to add hook arguments (hook-args).}
4481 % \begin{macrocode}
4482 %<latexrelease>\IncludeInRelease{2023/06/01}{\hook_gremove_code:nn}
4483 %<latexrelease> {Hooks~with~args}
4484 \cs_new_protected:Npn \hook_gremove_code:nn #1 #2
4485 { \@@_normalize_hook_args:Nnn \@@_gremove_code:nn {#1} {#2} }
4486 \cs_new_protected:Npn \@@_gremove_code:nn #1 #2
4487 {
4488 % \end{macrocode}
4489 % First check that the hook code pool exists. \cs{@@_if_usable:nTF}
4490 % isn't used here because it should be possible to remove code from a
4491 % hook before its defined (see section~\ref{sec:querying}).
4492 % \begin{macrocode}
4493 \@@_if_structure_exist:nTF {#1}
4494 {
4495 % \end{macrocode}
4496 % Then remove the chunk and run \cs{@@_update_hook_code:n} so
4497 % that the execution token list reflects the change if we are after
4498 % \verb=\begin{document}=.
4499 %
4500 % If all code is to be removed, clear the code pool
4501 % \cs{g_@@_\meta{hook}_code_prop}, the top-level code
4502 % \cs{@@_toplevel\textvisiblespace\meta{hook}}, and the next-execution code
4503 % \cs{@@_next\textvisiblespace\meta{hook}}.
4504 % \begin{macrocode}
4505 \str_if_eq:nnTF {#2} {*}
4506 {
4507 \prop_gclear:c { g_@@_#1_code_prop }
4508 \@@_toplevel_gset:nn {#1} { }
4509 \@@_next_gset:nn {#1} { }
4510 }
4511 {
4512 % \end{macrocode}
4513 % If the label is |top-level| then clear the token list, as all code
4514 % there is under the same label.
4515 % \begin{macrocode}
4516 \str_if_eq:nnTF {#2} { top-level }
4517 { \@@_toplevel_gset:nn {#1} { } }
4518 {
4519 \prop_gpop:cnNF { g_@@_#1_code_prop }
4520 {#2} \l_@@_return_tl
4521 { \msg_warning:nnnn { hooks } { cannot-remove } {#1} {#2} }
4522 }
4523 }
4524 % \end{macrocode}
4525 % Finally update the code, if the hook exists.
4526 % \begin{macrocode}
4527 \@@_if_usable:nT {#1}
4528 { \@@_update_hook_code:n {#1} }
4529 }
4530 % \end{macrocode}
4531 %
4532 % If the code pool for this hook doesn't exist, show a warning:
4533 % \begin{macrocode}
4534 {
4535 \@@_if_deprecated_generic:nTF {#1}
4536 {
4537 \@@_deprecated_generic_warn:n {#1}
4538 \@@_do_deprecated_generic:Nn
4539 \@@_gremove_code:nn {#1} {#2}
4540 }
4541 { \msg_warning:nnnn { hooks } { cannot-remove } {#1} {#2} }
4542 }
4543 }
4544 %<latexrelease>\EndIncludeInRelease
4545 % \end{macrocode}
4546 %
4547 % \begin{macrocode}
4548 %<latexrelease>\IncludeInRelease{2020/10/01}{\hook_gremove_code:nn}
4549 %<latexrelease> {Hooks~with~args}
4550 %<latexrelease>\cs_new_protected:Npn \@@_gremove_code:nn #1 #2
4551 %<latexrelease> {
4552 %<latexrelease> \@@_if_structure_exist:nTF {#1}
4553 %<latexrelease> {
4554 %<latexrelease> \str_if_eq:nnTF {#2} {*}
4555 %<latexrelease> {
4556 %<latexrelease> \prop_gclear:c { g_@@_#1_code_prop }
4557 %<latexrelease> \@@_tl_gclear:c { @@_toplevel~#1 }
4558 %<latexrelease> \@@_tl_gclear:c { @@_next~#1 }
4559 %<latexrelease> }
4560 %<latexrelease> {
4561 %<latexrelease> \str_if_eq:nnTF {#2} { top-level }
4562 %<latexrelease> { \@@_tl_gclear:c { @@_toplevel~#1 } }
4563 %<latexrelease> {
4564 %<latexrelease> \prop_gpop:cnNF { g_@@_#1_code_prop }
4565 %<latexrelease> {#2} \l_@@_return_tl
4566 %<latexrelease> { \msg_warning:nnnn { hooks } { cannot-remove }
4567 %<latexrelease> {#1} {#2} }
4568 %<latexrelease> }
4569 %<latexrelease> }
4570 %<latexrelease> \@@_if_usable:nT {#1}
4571 %<latexrelease> { \@@_update_hook_code:n {#1} }
4572 %<latexrelease> }
4573 %<latexrelease> {
4574 %<latexrelease> \@@_if_deprecated_generic:nTF {#1}
4575 %<latexrelease> {
4576 %<latexrelease> \@@_deprecated_generic_warn:n {#1}
4577 %<latexrelease> \@@_do_deprecated_generic:Nn
4578 %<latexrelease> \@@_gremove_code:nn {#1} {#2}
4579 %<latexrelease> }
4580 %<latexrelease> { \msg_warning:nnnn { hooks } { cannot-remove }
4581 %<latexrelease> {#1} {#2} }
4582 %<latexrelease> }
4583 %<latexrelease> }
4584 %<latexrelease>\EndIncludeInRelease
4585 % \end{macrocode}
4586 % \end{macro}
4587 % \end{macro}
4588 %
4589 % \begin{macro}{\@@_cs_gput_right:nnn}
4590 % \begin{macro}{\@@_cs_gput_right_fast:nnn,\@@_cs_gput_right_slow:nnn}
4591 % \begin{macro}{\@@_code_gset_auxi:nnnn,\@@_code_gset_auxi:eeen}
4592 % This macro is used to append code to the \verb|toplevel| and
4593 % \verb|next| token lists, treating them correctly depending on their
4594 % number of arguments, and depending on whether the code being added should
4595 % have parameter tokens understood as parameters, or doubled to be
4596 % stored as parameter tokens.
4597 % \changes{v1.1a}{2023/04/06}
4598 % {Macro added (hook-args).}
4599 % \changes{v1.1e}{2023/06/06}
4600 % {Short-circuit when the hook is declared without args (gh1078).}
4601 % \begin{macrocode}
4602 %<latexrelease>\IncludeInRelease{2023/06/01}{\@@_cs_gput_right:nnn}
4603 %<latexrelease> {Hooks~with~args}
4604 % \end{macrocode}
4605 %
4606 % Check if the current hook is declared and takes no arguments. In
4607 % this case, we short-circuit and use the simpler and much faster
4608 % approach that doesn't require hash-doubling.
4609 % \begin{macrocode}
4610 \cs_new_protected:Npn \@@_cs_gput_right:nnn #1 #2
4611 {
4612 \if:w T
4613 \@@_if_declared:nF {#2} { F }
4614 \tl_if_empty:cF { c_@@_#2_parameter_tl } { F }
4615 T
4616 \exp_after:wN \@@_cs_gput_right_fast:nnn
4617 \else:
4618 \exp_after:wN \@@_cs_gput_right_slow:nnn
4619 \fi:
4620 {#1} {#2}
4621 }
4622 \cs_new_protected:Npn \@@_cs_gput_right_fast:nnn #1 #2 #3
4623 { \cs_gset:cpe { @@#1~#2 }
4624 { \exp_not:v { @@#1~#2 } \exp_not:n {#3} } }
4625 \cs_new_protected:Npn \@@_cs_gput_right_slow:nnn #1 #2 #3
4626 {
4627 % \end{macrocode}
4628 % The auxiliary \cs{@@_code_gset_auxi:eeen} just does the assignment
4629 % at the end. Its first argument is the parameter text of the macro,
4630 % which is chosen here depending if
4631 % \cs[no-index]{c_@@_\meta{hook}_parameter_tl} exists, if the hook is
4632 % declared, and if it's a generic hook.
4633 % \begin{macrocode}
4634 \cs_if_exist:cF { @@#1~#2 }
4635 { \@@_code_gset_aux:nnn {#1} {#2} { } }
4636 \@@_code_gset_auxi:eeen
4637 {
4638 \@@_if_declared:nTF {#2}
4639 { \tl_use:c { c_@@_#2_parameter_tl } }
4640 {
4641 \@@_if_generic:nTF {#2}
4642 { \@@_generic_parameter:n {#2} }
4643 { \c_@@_nine_parameters_tl }
4644 }
4645 }
4646 % \end{macrocode}
4647 % Here we take the existing code in the macro, expand it with as many
4648 % arguments as it takes, then double the hashes so the code can be
4649 % reused. \pho{Maybe can be improved. The case of adding to an empty
4650 % cs can be optimized by quickly checking \cs{cs_replacement_spec}.}
4651 % \begin{macrocode}
4652 {
4653 \exp_args:NNo \exp_args:No \@@_double_hashes:n
4654 {
4655 \cs:w @@#1~#2 \exp_last_unbraced:Ne \cs_end:
4656 { \@@_braced_cs_parameter:n { @@#1~#2 } }
4657 }
4658 }
4659 % \end{macrocode}
4660 % Now the new code: if we are replacing arguments, then hashes are
4661 % left untouched, otherwise they are doubled.
4662 % \begin{macrocode}
4663 {
4664 \@@_if_replacing_args:TF
4665 { \exp_not:n }
4666 { \@@_double_hashes:n }
4667 {#3}
4668 }
4669 % \end{macrocode}
4670 % And finally, the csname which we'll define with all the above.
4671 % \begin{macrocode}
4672 { @@#1~#2 }
4673 }
4674 % \end{macrocode}
4675 %
4676 % And as promised, the auxiliary that does the definition.
4677 % \begin{macrocode}
4678 \cs_new_protected:Npn \@@_code_gset_auxi:nnnn #1 #2 #3 #4
4679 { \cs_gset:cpn {#4} #1 { #2 #3 } }
4680 \cs_generate_variant:Nn \@@_code_gset_auxi:nnnn { eeen }
4681 % \end{macrocode}
4682 %
4683 % \begin{macrocode}
4684 %<latexrelease>\EndIncludeInRelease
4685 %<latexrelease>\IncludeInRelease{2020/10/01}{\@@_cs_gput_right:nnn}
4686 %<latexrelease> {Hooks~with~args}
4687 %<latexrelease>\cs_undefine:N \@@_cs_gput_right:nnn
4688 %<latexrelease>\cs_undefine:N \@@_cs_gput_right_fast:nnn
4689 %<latexrelease>\cs_undefine:N \@@_cs_gput_right_slow:nnn
4690 %<latexrelease>\cs_undefine:N \@@_code_gset_auxi:nnnn
4691 %<latexrelease>\EndIncludeInRelease
4692 % \end{macrocode}
4693 % \end{macro}
4694 % \end{macro}
4695 % \end{macro}
4696 %
4697 % \begin{macro}{
4698 % \@@_code_gset:nn,\@@_code_gset:ne,
4699 % \@@_toplevel_gset:nn,
4700 % \@@_next_gset:nn,
4701 % \@@_code_gset_aux:nnn
4702 % }
4703 % These macros define
4704 % \cs[no-index]{@@\meta{type}\textvisiblespace\meta{hook}} (with
4705 % \meta{type} being \verb|_next|, \verb|_toplevel|, or empty) with the
4706 % given code and the parameters stored in
4707 % \cs[no-index]{c_@@_\meta{hook}_parameter_tl} (or none, if that
4708 % doesn't exist).
4709 % \changes{v1.1a}{2023/04/06}
4710 % {Macro added (hook-args).}
4711 % \begin{macrocode}
4712 %<latexrelease>\IncludeInRelease{2023/06/01}{\@@_code_gset:nn}
4713 %<latexrelease> {Hooks~with~args}
4714 \cs_new_protected:Npn \@@_code_gset:nn
4715 { \@@_code_gset_aux:nnn { } }
4716 \cs_new_protected:Npn \@@_toplevel_gset:nn
4717 { \@@_code_gset_aux:nnn { _toplevel } }
4718 \cs_new_protected:Npn \@@_next_gset:nn
4719 { \@@_code_gset_aux:nnn { _next } }
4720 \cs_new_protected:Npn \@@_code_gset_aux:nnn #1 #2 #3
4721 {
4722 \cs_gset:cpn { @@#1~#2 \exp_last_unbraced:Ne }
4723 { \@@_parameter:n {#2} }
4724 {#3}
4725 }
4726 \cs_generate_variant:Nn \@@_code_gset:nn { ne }
4727 % \end{macrocode}
4728 %
4729 % \begin{macrocode}
4730 %<latexrelease>\EndIncludeInRelease
4731 %<latexrelease>\IncludeInRelease{2020/10/01}{\@@_code_gset:nn}
4732 %<latexrelease> {Hooks~with~args}
4733 %<latexrelease>\cs_undefine:N \@@_code_gset:nn
4734 %<latexrelease>\cs_undefine:N \@@_toplevel_gset:nn
4735 %<latexrelease>\cs_undefine:N \@@_next_gset:nn
4736 %<latexrelease>\cs_undefine:N \@@_code_gset_aux:nnn
4737 %<latexrelease>\EndIncludeInRelease
4738 % \end{macrocode}
4739 % \end{macro}
4740 %
4741 % \begin{macro}{\@@_normalise_cs_args:nn}
4742 % This macro normalizes the parameters of the macros
4743 % \cs[no-index]{@@\meta{type}\textvisiblespace\meta{hook}} to take the
4744 % right number of arguments after a hook is declared. At this point
4745 % we know \cs[no-index]{c_@@_\meta{hook}_parameter_tl} exists, so use
4746 % that to count the arguments and use that as \meta{parameter text}
4747 % for the newly (re)defined macro.
4748 % \changes{v1.1a}{2023/04/06}
4749 % {Macro added (hook-args).}
4750 % \begin{macrocode}
4751 %<latexrelease>\IncludeInRelease{2023/06/01}{\@@_normalise_cs_args:nn}
4752 %<latexrelease> {Hooks~with~args}
4753 \cs_new_protected:Npn \@@_normalise_cs_args:nn #1 #2
4754 {
4755 \cs_if_exist:cT { @@#1~#2 }
4756 {
4757 \@@_code_gset_auxi:eeen
4758 { \tl_use:c { c_@@_#2_parameter_tl } }
4759 {
4760 \exp_args:NNo \exp_args:No \@@_double_hashes:n
4761 {
4762 \cs:w @@#1~#2 \exp_last_unbraced:Ne \cs_end:
4763 { \@@_braced_cs_parameter:n { @@#1~#2 } }
4764 }
4765 }
4766 { }
4767 { @@#1~#2 }
4768 }
4769 }
4770 %<latexrelease>\EndIncludeInRelease
4771 % \end{macrocode}
4772 %
4773 % \begin{macrocode}
4774 %<latexrelease>\IncludeInRelease{2020/10/01}{\@@_normalise_cs_args:nn}
4775 %<latexrelease> {Hooks~with~args}
4776 %<latexrelease>\cs_undefine:N \@@_normalise_cs_args:nn
4777 %<latexrelease>\EndIncludeInRelease
4778 % \end{macrocode}
4779 % \end{macro}
4780 %
4781 % \begin{macro}{\@@_normalise_code_pool:n}
4782 % \begin{macro}{\@@_set_normalise_fn:nn}
4783 % This one's a bit of a hack. It takes a hook, and iterates over its
4784 % code pool (\cs[no-index]{g_@@_\meta{hook}_code_prop}), redefining
4785 % each code label to use only valid arguments. This is used when, for
4786 % example, a code is added referencing arguments \verb|#1| and
4787 % \verb|#2|, but the hook has only \verb|#1|. In this example, every
4788 % reference to \verb|#2| is changed to \verb|##2|. This is done
4789 % because otherwise \TeX{} will throw a low-level error every time
4790 % some change happens to the hook (code is added, a rule is set, etc),
4791 % which can get quite repetitive for no good reason.
4792 % \changes{v1.1a}{2023/04/06}
4793 % {Macro added (hook-args).}
4794 % \begin{macrocode}
4795 %<latexrelease>\IncludeInRelease{2023/06/01}{\@@_normalise_code_pool:n}
4796 %<latexrelease> {Hooks~with~args}
4797 \cs_new_protected:Npn \@@_normalise_code_pool:n #1
4798 {
4799 % \end{macrocode}
4800 % First, call \cs{@@_set_normalise_fn:nn} with the hook name to set
4801 % everything up, then we'll loop over the
4802 % hook's code pool applying the normalization above. After that's
4803 % done, copy the temporary property list back to the hook's.
4804 % \begin{macrocode}
4805 \@@_set_normalise_fn:nn {#1} { Offending~label:~'##1' }
4806 \prop_clear:N \l_@@_work_prop
4807 \prop_map_function:cN { g_@@_#1_code_prop } \@@_normalise_fn:nn
4808 \prop_gset_eq:cN { g_@@_#1_code_prop } \l_@@_work_prop
4809 }
4810 % \end{macrocode}
4811 %
4812 % The sole purpose of this function is to define
4813 % \cs{@@_normalise_fn:nn}, which will then do the correcting of the
4814 % code being added to the hook.
4815 % \begin{macrocode}
4816 \cs_new_protected:Npn \@@_set_normalise_fn:nn #1 #2
4817 {
4818 % \end{macrocode}
4819 % To start, we define two auxiliary token lists.
4820 % \cs[no-index]{l_@@_tmpb_tl} contains:
4821 %\begin{verbatim}
4822 % {\c__hook_hashes_tl 1}
4823 % {\c__hook_hashes_tl 2}
4824 % ...
4825 % {\c__hook_hashes_tl 9}
4826 %\end{verbatim}
4827 % \begin{macrocode}
4828 \cs_set:Npn \@@_tmp:w ##1##2##3##4##5##6##7##8##9 { }
4829 \tl_set:Ne \l_@@_tmpb_tl
4830 { \@@_braced_cs_parameter:n { @@_tmp:w } }
4831 \group_begin:
4832 \@@_tl_set:cn { c_@@_hash_tl } { \exp_not:N \c_@@_hashes_tl }
4833 \use:e
4834 {
4835 \group_end:
4836 \tl_set:Nn \exp_not:N \l_@@_tmpb_tl { \l_@@_tmpb_tl }
4837 }
4838 % \end{macrocode}
4839 % And \cs[no-index]{l_@@_tmpa_tl} contains:
4840 %\begin{verbatim}
4841 % {\c__hook_hash_tl 1}
4842 % {\c__hook_hash_tl 2}
4843 % ...
4844 % {\c__hook_hash_tl <n>}
4845 %\end{verbatim}
4846 % with \meta{n} being the number of arguments declared for the hook.
4847 % \begin{macrocode}
4848 \exp_last_unbraced:NNf
4849 \cs_set:Npn \@@_tmp:w { \@@_parameter:n {#1} } { }
4850 \tl_set:Ne \l_@@_tmpa_tl
4851 { \@@_braced_cs_parameter:n { @@_tmp:w } }
4852 % \end{macrocode}
4853 % Now this function does the fun part. It is meant to be used with
4854 % \cs{prop_map_function:NN}, taking a label name in \verb|##1| and the
4855 % code stored in that label in \verb|##2|.
4856 % \begin{macrocode}
4857 \cs_gset_protected:Npe \@@_normalise_fn:nn ##1 ##2
4858 {
4859 % \end{macrocode}
4860 % Here we'll define two auxiliary macros: the first one throws an
4861 % error when it detects an invalid argument reference. It piggybacks
4862 % on \TeX's low-level \enquote{Illegal parameter number} error, but it
4863 % defines a weirdly-named control sequence so that the error comes out
4864 % nicely formatted. For example, if the label \enquote{badpkg} adds
4865 % some code that references argument \verb|#3| in the hook
4866 % \enquote{foo}, which takes only two arguments, the error will be:
4867 %\begin{verbatim}
4868 % ! Illegal parameter number in definition of hook 'foo'.
4869 % (hooks) Offending label: 'badpkg'.
4870 % <to be read again>
4871 % 3
4872 %\end{verbatim}
4873 % At the point of this definition, the error is raised if the code
4874 % happens to reference an invalid argument. If it was possible to
4875 % detect that this definition raised no error, the next step would be
4876 % unnecessary. We'll do all this in a group so this weird definition
4877 % doesn't leak out, and set \cs{tex_escapechar:D} to $-1$ so this hack
4878 % shows up extra nice in the case of an error.
4879 % \begin{macrocode}
4880 \group_begin:
4881 \int_set:Nn \tex_escapechar:D { -1 }
4882 \cs_set:cpn
4883 {
4884 hook~'#1'. ^^J
4885 (hooks) \prg_replicate:nn { 13 } { ~ }
4886 #2 % more message text
4887 }
4888 \exp_not:v { c_@@_#1_parameter_tl }
4889 {##2}
4890 \group_end:
4891 % \end{macrocode}
4892 % This next macro, with a much less fabulous name, takes always nine
4893 % arguments, and it just transfers the code \verb|##2| under the label
4894 % \verb|##1| to the temporary property list. The first \meta{n}
4895 % arguments are taken from \cs[no-index]{l_@@_tmpa_tl}, and the other
4896 % $9-\meta{n}$ taken from \cs[no-index]{l_@@_tmpb_tl} (which contains
4897 % twice as many \verb|#| tokens as the former). Then,
4898 % \cs{@@_double_hashes:n} is used to double non-argument hashes, and
4899 % expand the \cs{c_@@_hash_tl} and \cs{c_@@_hashes_tl} to the actual
4900 % parameter tokens.
4901 % \begin{macrocode}
4902 \cs_set:Npn \exp_not:N \@@_tmp:w
4903 \exp_not:V \c_@@_nine_parameters_tl
4904 {
4905 \prop_put:Nne \exp_not:N \l_@@_work_prop
4906 {##1} { \exp_not:N \@@_double_hashes:n {##2} }
4907 }
4908 % \end{macrocode}
4909 % This next macro, with a much less fabulous name, takes always nine
4910 % arguments, and it just transfers the code \verb|##2| under the label
4911 % \verb|##1| to the temporary property list. The first \meta{n}
4912 % arguments are taken from \cs[no-index]{l_@@_tmpa_tl}, and the other
4913 % $9-\meta{n}$ taken from \cs[no-index]{l_@@_tmpb_tl} (which contains
4914 % twice as many \verb|#| tokens as the former). Then,
4915 % \cs{@@_double_hashes:n} is used to double non-argument hashes, and
4916 % expand the \cs{c_@@_hash_tl} and \cs{c_@@_hashes_tl} to the actual
4917 % parameter tokens.
4918 % \begin{macrocode}
4919 \exp_not:N \@@_tmp:w
4920 \exp_not:V \l_@@_tmpa_tl
4921 \exp_args:No \exp_not:o
4922 { \exp_after:wN \@@_tmp:w \l_@@_tmpb_tl }
4923 }
4924 }
4925 \cs_new_eq:NN \@@_normalise_fn:nn ?
4926 %<latexrelease>\EndIncludeInRelease
4927 % \end{macrocode}
4928 %
4929 % \begin{macrocode}
4930 %<latexrelease>\IncludeInRelease{2020/10/01}{\@@_normalise_code_pool:n}
4931 %<latexrelease> {Hooks~with~args}
4932 %<latexrelease>\cs_undefine:N \@@_normalise_code_pool:n
4933 %<latexrelease>\EndIncludeInRelease
4934 % \end{macrocode}
4935 % \end{macro}
4936 %
4937 % \begin{macro}[pTF]{\@@_cs_if_empty:c}
4938 % Check if the expansion of a control sequence is empty by looking at
4939 % its replacement text.
4940 % \changes{v1.1a}{2023/04/06}
4941 % {Macro added (hook-args).}
4942 % \begin{macrocode}
4943 %<latexrelease>\IncludeInRelease{2023/06/01}{\@@_cs_if_empty:c}
4944 %<latexrelease> {Hooks~with~args}
4945 \prg_new_conditional:Npnn \@@_cs_if_empty:c #1 { p, T, F, TF }
4946 {
4947 \if:w \scan_stop: \@@_replacement_spec:c {#1} \scan_stop:
4948 \prg_return_true:
4949 \else:
4950 \prg_return_false:
4951 \fi:
4952 }
4953 \cs_new:Npn \@@_replacement_spec:c #1
4954 {
4955 \exp_args:Nc \token_if_macro:NT {#1}
4956 { \cs_replacement_spec:c {#1} }
4957 }
4958 %<latexrelease>\EndIncludeInRelease
4959 % \end{macrocode}
4960 %
4961 % \begin{macrocode}
4962 %<latexrelease>\IncludeInRelease{2020/10/01}{\@@_cs_if_empty:c}
4963 %<latexrelease> {Hooks~with~args}
4964 %<latexrelease>\cs_undefine:N \@@_cs_if_empty:c
4965 %<latexrelease>\EndIncludeInRelease
4966 % \end{macrocode}
4967 % \end{macro}
4968 % \end{macro}
4969 %
4970 % \begin{macro}{\@@_braced_cs_parameter:n}
4971 % \begin{macro}{\@@_braced_hidden_loop:w}
4972 % \begin{macro}{\@@_cs_parameter_count:N}
4973 % \begin{macro}{\@@_cs_parameter_count:w,\@@_cs_end:w}
4974 % Looks at the \meta{parameter text} of a control sequence, and
4975 % returns a run of \enquote{hidden} braced parameters for that macro.
4976 % This works as long as the macros take a simple run of zero to nine
4977 % arguments. The parameters are \enquote{hidden} because the
4978 % parameter tokens are returned inside \cs{c_@@_hash_tl} instead of
4979 % explicitly, so that \cs{@@_double_hashes:n} won't touch these.
4980 % \changes{v1.1a}{2023/04/06}
4981 % {Macro added (hook-args).}
4982 % \begin{macrocode}
4983 %<latexrelease>\IncludeInRelease{2023/06/01}{\@@_braced_cs_parameter:n}
4984 %<latexrelease> {Hooks~with~args}
4985 \cs_new:Npn \@@_braced_cs_parameter:n #1
4986 {
4987 \exp_last_unbraced:Ne \@@_braced_hidden_loop:w
4988 { \exp_args:Nc \@@_cs_parameter_count:N {#1} } ? \s_@@_mark
4989 }
4990 \cs_new:Npn \@@_braced_hidden_loop:w #1
4991 {
4992 \if:w ? #1
4993 \@@_use_i_delimit_by_s_mark:nw
4994 \fi:
4995 { \exp_not:N \c_@@_hash_tl #1 }
4996 \@@_braced_hidden_loop:w
4997 }
4998 \cs_new:Npn \@@_cs_parameter_count:N #1
4999 {
5000 \exp_last_unbraced:Nf \@@_cs_parameter_count:w
5001 { \token_if_macro:NT #1 { \cs_parameter_spec:N #1 } }
5002 ? \@@_cs_end:w ? \@@_cs_end:w ? \@@_cs_end:w
5003 ? \@@_cs_end:w ? \@@_cs_end:w ? \@@_cs_end:w
5004 ? \@@_cs_end:w ? \@@_cs_end:w ? \@@_cs_end:w
5005 \s_@@_mark
5006 }
5007 \cs_new:Npn \@@_cs_parameter_count:w #1#2 #3#4 #5#6 #7#8
5008 { #2 #4 #6 #8 \@@_cs_parameter_count:w }
5009 \cs_new:Npn \@@_cs_end:w #1 \s_@@_mark { }
5010 %<latexrelease>\EndIncludeInRelease
5011 % \end{macrocode}
5012 %
5013 % This function can't be undefined when rolling back because it's used
5014 % at the end of this module to adequate the hook data structures to
5015 % previous versions.
5016 % \begin{macrocode}
5017 %<latexrelease>\IncludeInRelease{2020/10/01}{\@@_braced_cs_parameter:n}
5018 %<latexrelease> {Hooks~with~args}
5019 %<latexrelease>\EndIncludeInRelease
5020 % \end{macrocode}
5021 % \end{macro}
5022 % \end{macro}
5023 % \end{macro}
5024 % \end{macro}
5025 %
5026 % \begin{macro}{\@@_braced_parameter:n}
5027 % \begin{macro}{\@@_braced_real_loop:w}
5028 % This one is used in simpler cases, where no special handling of
5029 % hashes is required. This is used only inside
5030 % \cs{@@_initialize_hook_code:n}, so it assumes
5031 % \cs[no-index]{c_@@_\meta{hook}_parameter_tl} is defined, but should
5032 % work otherwise.
5033 % \changes{v1.1a}{2023/04/06}
5034 % {Macro added (hook-args).}
5035 % \begin{macrocode}
5036 %<latexrelease>\IncludeInRelease{2023/06/01}{\@@_braced_parameter:n}
5037 %<latexrelease> {Hooks~with~args}
5038 \cs_new:Npn \@@_braced_parameter:n #1
5039 {
5040 \if_case:w
5041 \int_eval:n
5042 { \exp_args:Nv \str_count:n { c_@@_#1_parameter_tl } / 3 }
5043 \exp_stop_f:
5044 \or: {##1}
5045 \or: {##1} {##2}
5046 \or: {##1} {##2} {##3}
5047 \or: {##1} {##2} {##3} {##4}
5048 \or: {##1} {##2} {##3} {##4} {##5}
5049 \or: {##1} {##2} {##3} {##4} {##5} {##6}
5050 \or: {##1} {##2} {##3} {##4} {##5} {##6} {##7}
5051 \or: {##1} {##2} {##3} {##4} {##5} {##6} {##7} {##8}
5052 \or: {##1} {##2} {##3} {##4} {##5} {##6} {##7} {##8} {##9}
5053 \else:
5054 \msg_expandable_error:nnn { latex2e } { should-not-happen }
5055 { Invalid~parameter~spec. }
5056 \fi:
5057 }
5058 %<latexrelease>\EndIncludeInRelease
5059 % \end{macrocode}
5060 %
5061 % \begin{macrocode}
5062 %<latexrelease>\IncludeInRelease{2020/10/01}{\@@_braced_parameter:n}
5063 %<latexrelease> {Hooks~with~args}
5064 %<latexrelease>\cs_undefine:N \@@_braced_parameter:n
5065 %<latexrelease>\EndIncludeInRelease
5066 % \end{macrocode}
5067 % \end{macro}
5068 % \end{macro}
5069 %
5070 % \begin{macro}{\@@_parameter:n}
5071 % This is just a shortcut to \verb|e|- or \verb|f|-expand to the
5072 % \meta{parameter text} of the hook.
5073 % \changes{v1.1a}{2023/04/06}
5074 % {Macro added (hook-args).}
5075 % \begin{macrocode}
5076 %<latexrelease>\IncludeInRelease{2023/06/01}{\@@_parameter:n}
5077 %<latexrelease> {Hooks~with~args}
5078 \cs_new:Npn \@@_parameter:n #1
5079 {
5080 \cs:w c_@@_
5081 \tl_if_exist:cTF { c_@@_#1_parameter_tl }
5082 { #1_parameter } { empty }
5083 _tl \cs_end:
5084 }
5085 \cs_new:Npn \@@_generic_parameter:n #1
5086 { \@@_generic_parameter:w #1 / / / \s_@@_mark }
5087 \cs_new:Npn \@@_generic_parameter:w #1 / #2 / #3 / #4 \s_@@_mark
5088 {
5089 \cs_if_exist_use:cF { c_@@_parameter_#1/./#3_tl }
5090 { \c_@@_empty_tl }
5091 }
5092 %<latexrelease>\EndIncludeInRelease
5093 % \end{macrocode}
5094 %
5095 % \begin{macrocode}
5096 %<latexrelease>\IncludeInRelease{2020/10/01}{\@@_parameter:n}
5097 %<latexrelease> {Hooks~with~args}
5098 %<latexrelease>\cs_undefine:N \@@_parameter:n
5099 %<latexrelease>\cs_undefine:N \@@_generic_parameter:n
5100 %<latexrelease>\EndIncludeInRelease
5101 % \end{macrocode}
5102 % \end{macro}
5103 %
5104 %
5105 % \subsection{Setting rules for hooks code}
5106 %
5107 % \begin{macro}{
5108 % \g_@@_??_code_prop,
5109 % \@@~??,
5110 % \g_@@_??_reversed_tl,
5111 % \c_@@_??_parameter_tl,
5112 % }
5113 %
5114 % Initially these variables simply used an empty ``label'' name (not
5115 % two question marks). This was a bit unfortunate, because then
5116 % \texttt{l3doc} complains about \verb=__= in the middle of a
5117 % command name when trying to typeset the documentation. However
5118 % using a ``normal'' name such as \texttt{default} has the
5119 % disadvantage of that being not really distinguishable from a real
5120 % hook name. I now have settled for \texttt{??} which needs some
5121 % gymnastics to get it into the csname, but since this is used a
5122 % lot, the code should be fast, so this is not done with \texttt{c}
5123 % expansion in the code later on.
5124 %
5125 % \cs{@@\textvisiblespace??} isn't used, but it has to be defined to
5126 % trick the code into thinking that \texttt{??} is actually a hook.
5127 % \begin{macrocode}
5128 \prop_new:c { g_@@_??_code_prop }
5129 \prop_new:c { @@~?? }
5130 % \end{macrocode}
5131 %
5132 % Default rules are always given in normal ordering (never in
5133 % reversed ordering). If such a rule is applied to a reversed
5134 % hook it behaves as if the rule is reversed (e.g.,
5135 % \texttt{after} becomes \texttt{before})
5136 % because those rules are applied first and then the order is reversed.
5137 % \begin{macrocode}
5138 \tl_new:c { g_@@_??_reversed_tl }
5139 % \end{macrocode}
5140 %
5141 % The parameter text for the \enquote{default} hook is empty.
5142 % \changes{v1.1a}{2023/04/06}
5143 % {Token list added (hook-args).}
5144 % \begin{macrocode}
5145 %<latexrelease>\IncludeInRelease{2023/06/01}{\c_@@_??_parameter_tl}
5146 %<latexrelease> {Hooks~with~args}
5147 \tl_const:cn { c_@@_??_parameter_tl } { }
5148 %<latexrelease>\EndIncludeInRelease
5149 %<latexrelease>\IncludeInRelease{2020/10/01}{\c_@@_??_parameter_tl}
5150 %<latexrelease> {Hooks~with~args}
5151 %<latexrelease>\cs_undefine:c { c_@@_??_parameter_tl }
5152 %<latexrelease>\EndIncludeInRelease
5153 % \end{macrocode}
5154 % \end{macro}
5155 %
5156 % \begin{macro}{\hook_gset_rule:nnnn}
5157 % \begin{macro}{\@@_gset_rule:nnnn}
5158 % With
5159 % \cs{hook_gset_rule:nnnn}\Arg{hook}\Arg{label1}\Arg{relation}\Arg{label2}
5160 % a relation is defined between the two code labels for the given
5161 % \meta{hook}. The special hook \texttt{??} stands for \emph{any}
5162 % hook, which sets a default rule (to be used if no other relation
5163 % between the two hooks exist).
5164 % \begin{macrocode}
5165 \cs_new_protected:Npn \hook_gset_rule:nnnn #1#2#3#4
5166 {
5167 \@@_normalize_hook_rule_args:Nnnnn \@@_gset_rule:nnnn
5168 {#1} {#2} {#3} {#4}
5169 }
5170 % \end{macrocode}
5171 %
5172 % \begin{macrocode}
5173 %<latexrelease>\IncludeInRelease{2022/06/01}{\@@_gset_rule:nnnn}
5174 %<latexrelease> {Refuse~setting~rule~for~one-time~hooks}
5175 % \end{macrocode}
5176 %
5177 % \begin{macrocode}
5178 \cs_new_protected:Npn \@@_gset_rule:nnnn #1#2#3#4
5179 {
5180 \@@_if_deprecated_generic:nT {#1}
5181 {
5182 \@@_deprecated_generic_warn:n {#1}
5183 \@@_do_deprecated_generic:Nn \@@_gset_rule:nnnn {#1}
5184 {#2} {#3} {#4}
5185 \@@_use_none_delimit_by_s_mark:w
5186 }
5187 \@@_if_execute_immediately:nT {#1}
5188 {
5189 \msg_error:nnnnnn { hooks } { rule-too-late }
5190 {#1} {#2} {#3} {#4}
5191 \@@_use_none_delimit_by_s_mark:w
5192 }
5193 % \end{macrocode}
5194 % First we ensure the basic data structure of the hook exists:
5195 % \begin{macrocode}
5196 \@@_init_structure:n {#1}
5197 % \end{macrocode}
5198 % Then we clear any previous relationship between both labels.
5199 % \begin{macrocode}
5200 \@@_rule_gclear:nnn {#1} {#2} {#4}
5201 % \end{macrocode}
5202 % Then we call the function to handle the given rule. Throw an error if the
5203 % rule is invalid.
5204 % \begin{macrocode}
5205 \cs_if_exist_use:cTF { @@_rule_#3_gset:nnn }
5206 {
5207 {#1} {#2} {#4}
5208 \@@_update_hook_code:n {#1}
5209 }
5210 {
5211 \msg_error:nnnnnn { hooks } { unknown-rule }
5212 {#1} {#2} {#3} {#4}
5213 }
5214 \s_@@_mark
5215 }
5216 % \end{macrocode}
5217 %
5218 % \begin{macrocode}
5219 %<latexrelease>\EndIncludeInRelease
5220 %<latexrelease>\IncludeInRelease{2020/10/01}{\@@_gset_rule:nnnn}
5221 %<latexrelease> {Refuse~setting~rule~for~one-time~hooks}
5222 %<latexrelease>\cs_new_protected:Npn \@@_gset_rule:nnnn #1#2#3#4
5223 %<latexrelease> {
5224 %<latexrelease> \@@_if_deprecated_generic:nT {#1}
5225 %<latexrelease> {
5226 %<latexrelease> \@@_deprecated_generic_warn:n {#1}
5227 %<latexrelease> \@@_do_deprecated_generic:Nn \@@_gset_rule:nnnn
5228 %<latexrelease> {#1} {#2} {#3} {#4}
5229 %<latexrelease> \exp_after:wN \use_none:nnnnnnnnn \use_none:n
5230 %<latexrelease> }
5231 %<latexrelease> \@@_init_structure:n {#1}
5232 %<latexrelease> \@@_rule_gclear:nnn {#1} {#2} {#4}
5233 %<latexrelease> \cs_if_exist_use:cTF { @@_rule_#3_gset:nnn }
5234 %<latexrelease> {
5235 %<latexrelease> {#1} {#2} {#4}
5236 %<latexrelease> \@@_update_hook_code:n {#1}
5237 %<latexrelease> }
5238 %<latexrelease> {
5239 %<latexrelease> \msg_error:nnnnnn { hooks } { unknown-rule }
5240 %<latexrelease> {#1} {#2} {#3} {#4}
5241 %<latexrelease> }
5242 %<latexrelease> }
5243 %<latexrelease>\EndIncludeInRelease
5244 % \end{macrocode}
5245 % \end{macro}
5246 % \end{macro}
5247 %
5248 % \begin{macro}{\@@_rule_before_gset:nnn, \@@_rule_after_gset:nnn,
5249 % \@@_rule_<_gset:nnn, \@@_rule_>_gset:nnn}
5250 % Then we add the new rule. We need to normalize the rules here to
5251 % allow for faster processing later. Given a pair of labels
5252 % $l_A$ and $l_B$, the rule $l_A>l_B$ is the same as $l_B<l_A$
5253 % only presented differently. But by normalizing the
5254 % forms of the rule to a single representation, say, $l_B<l_A$, reduces
5255 % the time spent looking for the rules later considerably.
5256 %
5257 % Here we do that normalization by using \cs[no-index]{(pdf)strcmp} to
5258 % lexically sort labels $l_A$ and $l_B$ to a fixed order. This order
5259 % is then enforced every time these two labels are used together.
5260 %
5261 % Here we use \cs{@@_label_pair:nn}~\Arg{hook}~\Arg{l_A}~\Arg{l_B}
5262 % to build a string \texttt{$l_B$\string|$l_A$} with a fixed order, and
5263 % use \cs{@@_label_ordered:nnTF} to apply the correct rule to the pair
5264 % of labels, depending if it was sorted or not.
5265 % \begin{macrocode}
5266 \cs_new_protected:Npn \@@_rule_before_gset:nnn #1#2#3
5267 {
5268 \@@_tl_gset:ce
5269 { g_@@_#1_rule_ \@@_label_pair:nn {#2} {#3} _tl }
5270 { \@@_label_ordered:nnTF {#2} {#3} { < } { > } }
5271 }
5272 \cs_new_eq:cN { @@_rule_<_gset:nnn } \@@_rule_before_gset:nnn
5273 % \end{macrocode}
5274 %
5275 % \begin{macrocode}
5276 \cs_new_protected:Npn \@@_rule_after_gset:nnn #1#2#3
5277 {
5278 \@@_tl_gset:ce
5279 { g_@@_#1_rule_ \@@_label_pair:nn {#3} {#2} _tl }
5280 { \@@_label_ordered:nnTF {#3} {#2} { < } { > } }
5281 }
5282 \cs_new_eq:cN { @@_rule_>_gset:nnn } \@@_rule_after_gset:nnn
5283 % \end{macrocode}
5284 % \end{macro}
5285 %
5286 % \begin{macro}{\@@_rule_voids_gset:nnn}
5287 % This rule removes (clears, actually) the code from label |#3| if
5288 % label |#2| is in the hook |#1|.
5289 % \begin{macrocode}
5290 \cs_new_protected:Npn \@@_rule_voids_gset:nnn #1#2#3
5291 {
5292 \@@_tl_gset:ce
5293 { g_@@_#1_rule_ \@@_label_pair:nn {#2} {#3} _tl }
5294 { \@@_label_ordered:nnTF {#2} {#3} { -> } { <- } }
5295 }
5296 % \end{macrocode}
5297 % \end{macro}
5298 %
5299 % \begin{macro}{
5300 % \@@_rule_incompatible-error_gset:nnn,
5301 % \@@_rule_incompatible-warning_gset:nnn,
5302 % }
5303 % These relations make an error/warning if labels |#2| and |#3| appear
5304 % together in hook |#1|.
5305 % \begin{macrocode}
5306 \cs_new_protected:cpn { @@_rule_incompatible-error_gset:nnn } #1#2#3
5307 { \@@_tl_gset:cn
5308 { g_@@_#1_rule_ \@@_label_pair:nn {#2} {#3} _tl }
5309 { xE }
5310 }
5311 \cs_new_protected:cpn { @@_rule_incompatible-warning_gset:nnn } #1#2#3
5312 { \@@_tl_gset:cn
5313 { g_@@_#1_rule_ \@@_label_pair:nn {#2} {#3} _tl }
5314 { xW }
5315 }
5316 % \end{macrocode}
5317 % \end{macro}
5318 %
5319 % \begin{macro}{\@@_rule_unrelated_gset:nnn, \@@_rule_gclear:nnn}
5320 % Undo a setting. \cs{@@_rule_unrelated_gset:nnn} doesn't need to do anything,
5321 % since we use \cs{@@_rule_gclear:nnn} before setting any rule.
5322 % \begin{macrocode}
5323 \cs_new_protected:Npn \@@_rule_unrelated_gset:nnn #1#2#3 { }
5324 \cs_new_protected:Npn \@@_rule_gclear:nnn #1#2#3
5325 { \cs_undefine:c { g_@@_#1_rule_ \@@_label_pair:nn {#2} {#3} _tl } }
5326 % \end{macrocode}
5327 % \end{macro}
5328 %
5329 % \begin{macro}[EXP]{\@@_label_pair:nn}
5330 % Ensure that the lexically greater label comes first.
5331 % \begin{macrocode}
5332 \cs_new:Npn \@@_label_pair:nn #1#2
5333 {
5334 \if_case:w \@@_str_compare:nn {#1} {#2} \exp_stop_f:
5335 #1 | #1 % 0
5336 \or: #1 | #2 % +1
5337 \else: #2 | #1 % -1
5338 \fi:
5339 }
5340 % \end{macrocode}
5341 % \end{macro}
5342 %
5343 % \begin{macro}[pTF]{\@@_label_ordered:nn}
5344 % Check that labels |#1| and |#2| are in the correct order (as
5345 % returned by \cs{@@_label_pair:nn}) and if so return true, else
5346 % return false.
5347 % \begin{macrocode}
5348 \prg_new_conditional:Npnn \@@_label_ordered:nn #1#2 { TF }
5349 {
5350 \if_int_compare:w \@@_str_compare:nn {#1} {#2} > 0 \exp_stop_f:
5351 \prg_return_true:
5352 \else:
5353 \prg_return_false:
5354 \fi:
5355 }
5356 % \end{macrocode}
5357 % \end{macro}
5358 %
5359 % \begin{macro}[EXP]{\@@_if_label_case:nnnnn}
5360 % To avoid doing the string comparison twice in \cs{@@_initialize_single:NNn}
5361 % (once with \cs{str_if_eq:nn} and again with \cs{@@_label_ordered:nn}),
5362 % we use a three-way branching macro that will compare |#1| and |#2|
5363 % and expand to \cs{use_i:nnn} if they are equal, \cs{use_ii:nn} if
5364 % |#1| is lexically greater, and \cs{use_iii:nn} otherwise.
5365 % \begin{macrocode}
5366 \cs_new:Npn \@@_if_label_case:nnnnn #1#2
5367 {
5368 \cs:w use_
5369 \if_case:w \@@_str_compare:nn {#1} {#2}
5370 i \or: ii \else: iii \fi: :nnn
5371 \cs_end:
5372 }
5373 % \end{macrocode}
5374 % \end{macro}
5375 %
5376 % \begin{macro}{\@@_update_hook_code:n}
5377 % Before \verb=\begin{document}= this does nothing, in the body it
5378 % reinitializes the hook code using the altered data.
5379 % \begin{macrocode}
5380 \cs_new_eq:NN \@@_update_hook_code:n \use_none:n
5381 % \end{macrocode}
5382 % \end{macro}
5383 %
5384 % \begin{macro}{\@@_initialize_all:}
5385 % Initialize all known hooks (at \verb=\begin{document}=), i.e.,
5386 % update the fast execution token lists to hold the necessary code
5387 % in the right order.
5388 % \changes{v1.1a}{2023/04/06}
5389 % {Changes to add hook arguments (hook-args).}
5390 % \begin{macrocode}
5391 %<latexrelease>\IncludeInRelease{2023/06/01}{\@@_initialize_all:}
5392 %<latexrelease> {Hooks~with~args}
5393 \cs_new_protected:Npn \@@_initialize_all:
5394 {
5395 % \end{macrocode}
5396 % First we change \cs{@@_update_hook_code:n} which so far was a
5397 % no-op to now initialize one hook. This way any later updates to
5398 % the hook will run that code and also update the execution token
5399 % list.
5400 % \begin{macrocode}
5401 \cs_gset_eq:NN \@@_update_hook_code:n \@@_initialize_hook_code:n
5402 % \end{macrocode}
5403 % Now we loop over all hooks that have been defined and update each
5404 % of them. Here we have to determine if the hook has arguments so
5405 % that auxiliaries know what to do with hashes. We look at
5406 % \cs[no-index]{c_@@_\meta{hook}_parameter_tl}, if it has any
5407 % parameters, and set \verb|replacing_args| accordingly.
5408 % \begin{macrocode}
5409 \@@_debug:n { \prop_gclear:N \g_@@_used_prop }
5410 \seq_map_inline:Nn \g_@@_all_seq
5411 {
5412 \tl_if_empty:cTF { c_@@_##1_parameter_tl }
5413 { \@@_replacing_args_false: }
5414 { \@@_replacing_args_true: }
5415 \@@_update_hook_code:n {##1}
5416 \@@_replacing_args_reset:
5417 }
5418 % \end{macrocode}
5419 % If we are debugging we show results hook by hook for all hooks
5420 % that have data.
5421 % \changes{v1.1l}{2024/11/26}{Adjust debugging message (gh/1459)}
5422 % \begin{macrocode}
5423 \@@_debug:n
5424 {
5425 \iow_term:e { ^^J[lthooks]~ All~initialized~(non-empty)~hooks: }
5426 \prop_map_inline:Nn \g_@@_used_prop
5427 {
5428 \iow_term:e
5429 { ^^J ~ ##1 ~ -> ~ \cs_replacement_spec:c { @@~##1 } ~ }
5430 }
5431 }
5432 % \end{macrocode}
5433 % After all hooks are initialized we change the ``use'' to just
5434 % call the hook code and not initialize it (as this was already done in the
5435 % preamble.
5436 % \begin{macrocode}
5437 \@@_post_initialization_defs:
5438 }
5439 % \end{macrocode}
5440 %
5441 % \begin{macrocode}
5442 %<latexrelease>\EndIncludeInRelease
5443 %<latexrelease>\IncludeInRelease{2020/10/01}{\@@_initialize_all:}
5444 %<latexrelease> {Hooks~with~args}
5445 %<latexrelease>\cs_gset_protected:Npn \@@_initialize_all:
5446 %<latexrelease> {
5447 %<latexrelease> \cs_gset_eq:NN \@@_update_hook_code:n
5448 %<latexrelease> \@@_initialize_hook_code:n
5449 %<latexrelease> \@@_debug:n { \prop_gclear:N \g_@@_used_prop }
5450 %<latexrelease> \seq_map_inline:Nn \g_@@_all_seq
5451 %<latexrelease> { \@@_update_hook_code:n {##1} }
5452 %<latexrelease> \@@_debug:n
5453 %<latexrelease> {
5454 %<latexrelease> \iow_term:x{^^JAll~ initialized~ (non-empty)~ hooks:}
5455 %<latexrelease> \prop_map_inline:Nn \g_@@_used_prop
5456 %<latexrelease> {
5457 %<latexrelease> \iow_term:x
5458 %<latexrelease> { ^^J ~ ##1 ~ -> ~
5459 %<latexrelease> \cs_replacement_spec:c { @@~##1 } ~ }
5460 %<latexrelease> }
5461 %<latexrelease> }
5462 %<latexrelease> \cs_gset_eq:NN \hook_use:n \@@_use_initialized:n
5463 %<latexrelease> \cs_gset_eq:NN \@@_preamble_hook:n \use_none:n
5464 %<latexrelease> }
5465 %<@@=>
5466 %<latexrelease>\cs_gset_eq:NN \@expl@@@initialize@all@@
5467 %<latexrelease> \__hook_initialize_all:
5468 %<@@=hook>
5469 %<latexrelease>\EndIncludeInRelease
5470 % \end{macrocode}
5471 % \end{macro}
5472 %
5473 %
5474 % \begin{macro}{\@@_initialize_hook_code:n}
5475 % Initializing or reinitializing the fast execution hook code. In
5476 % the preamble this is selectively done in case a hook gets used
5477 % and at \verb=\begin{document}= this is done for all hooks and
5478 % afterwards only if the hook code changes.
5479 % \changes{v1.1a}{2023/04/06}
5480 % {Changes to add hook arguments (hook-args).}
5481 % \changes{v1.1l}{2024/11/26}{Adjust debugging message (gh/1459)}
5482 % \begin{macrocode}
5483 %<latexrelease>\IncludeInRelease{2023/06/01}{\@@_initialize_hook_code:n}
5484 %<latexrelease> {Hooks~with~args}
5485 \cs_new_protected:Npn \@@_initialize_hook_code:n #1
5486 {
5487 \@@_debug:n
5488 { \iow_term:e { ^^J[lthooks]~ Update~code~for~hook~'#1' \on@line :^^J } }
5489 % \end{macrocode}
5490 % This does the sorting and the updates.
5491 % First thing we do is to check if a legacy hook macro exists and
5492 % if so we add it to the hook under the label \texttt{legacy}. This
5493 % might make the hook non-empty so we have to do this before
5494 % the then following test.
5495 % \begin{macrocode}
5496 \@@_include_legacy_code_chunk:n {#1}
5497 % \end{macrocode}
5498 % If there aren't any code
5499 % chunks for the current hook, there is no point in even starting
5500 % the sorting routine so we make a quick test for that and in that
5501 % case just update \cs{@@\textvisiblespace\meta{hook}} to hold the |top-level| and
5502 % |next| code chunks. If there are code chunks we call
5503 % \cs{@@_initialize_single:NNn} and pass to it ready made csnames
5504 % as they are needed several times inside. This way we save a bit
5505 % on processing time if we do that up front.
5506 % \changes{v1.0u}{2022/05/17}{Refuse sorting one-time hooks (gh/818).}
5507 % \begin{macrocode}
5508 \@@_if_usable:nT {#1}
5509 {
5510 \prop_if_empty:cTF { g_@@_#1_code_prop }
5511 {
5512 \@@_code_gset:ne {#1}
5513 {
5514 % \end{macrocode}
5515 % The hook may take arguments, so we add a run of braced parameters
5516 % after the \verb|_next| and \verb|_toplevel| macros, so that the
5517 % arguments passed to the hook are forwarded to them.
5518 % \begin{macrocode}
5519 \exp_not:c { @@_toplevel~#1 }
5520 \@@_braced_parameter:n {#1}
5521 \exp_not:c { @@_next~#1 }
5522 \@@_braced_parameter:n {#1}
5523 }
5524 }
5525 {
5526 % \end{macrocode}
5527 % By default the algorithm sorts the code chunks and then saves the
5528 % result in a token list for fast execution; this is done by adding the code chunks
5529 % one after another, using \cs{tl_gput_right:NV}. When we sort code for
5530 % a reversed hook, all we have to do is to add the code chunks in
5531 % the opposite order into the token list. So all we have to do
5532 % in preparation is to change two definitions that are used later on.
5533 % \begin{macrocode}
5534 \@@_if_reversed:nTF {#1}
5535 { \cs_set_eq:NN \@@_tl_gput:Nn \@@_tl_gput_left:Nn
5536 \cs_set_eq:NN \@@_clist_gput:NV \clist_gput_left:NV }
5537 { \cs_set_eq:NN \@@_tl_gput:Nn \@@_tl_gput_right:Nn
5538 \cs_set_eq:NN \@@_clist_gput:NV \clist_gput_right:NV }
5539 % \end{macrocode}
5540 %
5541 % When sorting, some relations (namely \verb|voids|) need to
5542 % act destructively on the code property lists to remove code that
5543 % shouldn't appear in the sorted hook token list, so we make a copy
5544 % of the code property list that we can safely work on without
5545 % changing the main one.
5546 % \begin{macrocode}
5547 \prop_set_eq:Nc \l_@@_work_prop { g_@@_#1_code_prop }
5548 \@@_initialize_single:ccn
5549 { @@~#1 } { g_@@_#1_labels_clist } {#1}
5550 % \end{macrocode}
5551 % For debug display we want to keep track of those hooks that
5552 % actually got code added to them, so we record that in plist. We
5553 % use a plist to ensure that we record each hook name only once,
5554 % i.e., we are only interested in storing the keys and the value is
5555 % arbitrary.
5556 % \begin{macrocode}
5557 \@@_debug:n
5558 { \exp_args:NNe \prop_gput:Nnn \g_@@_used_prop {#1} { } }
5559 }
5560 }
5561 }
5562 %<latexrelease>\EndIncludeInRelease
5563 % \end{macrocode}
5564 %
5565 % \begin{macrocode}
5566 %<latexrelease>\IncludeInRelease{2020/10/01}{\@@_initialize_hook_code:n}
5567 %<latexrelease> {Hooks~with~args}
5568 %<latexrelease>\cs_gset_protected:Npn \@@_initialize_hook_code:n #1
5569 %<latexrelease> {
5570 %<latexrelease> \@@_debug:n
5571 %<latexrelease> { \iow_term:x { ^^J Update~code~for~hook~'#1'
5572 %<latexrelease> \on@line :^^J } }
5573 %<latexrelease> \@@_include_legacy_code_chunk:n {#1}
5574 %<latexrelease> \@@_if_usable:nT {#1}
5575 %<latexrelease> {
5576 %<latexrelease> \prop_if_empty:cTF { g_@@_#1_code_prop }
5577 %<latexrelease> {
5578 %<latexrelease> \@@_tl_gset:co { @@~#1 }
5579 %<latexrelease> {
5580 %<latexrelease> \cs:w @@_toplevel~#1 \exp_after:wN \cs_end:
5581 %<latexrelease> \cs:w @@_next~#1 \cs_end:
5582 %<latexrelease> }
5583 %<latexrelease> }
5584 %<latexrelease> {
5585 %<latexrelease> \@@_if_reversed:nTF {#1}
5586 %<latexrelease> { \cs_set_eq:NN \@@_tl_gput:Nn
5587 %<latexrelease> \@@_tl_gput_left:Nn
5588 %<latexrelease> \cs_set_eq:NN \@@_clist_gput:NV
5589 %<latexrelease> \clist_gput_left:NV }
5590 %<latexrelease> { \cs_set_eq:NN \@@_tl_gput:Nn
5591 %<latexrelease> \@@_tl_gput_right:Nn
5592 %<latexrelease> \cs_set_eq:NN \@@_clist_gput:NV
5593 %<latexrelease> \clist_gput_right:NV }
5594 %<latexrelease> \prop_set_eq:Nc \l_@@_work_prop
5595 %<latexrelease> { g_@@_#1_code_prop }
5596 %<latexrelease> \@@_initialize_single:ccn
5597 %<latexrelease> { @@~#1 } { g_@@_#1_labels_clist } {#1}
5598 %<latexrelease> \@@_debug:n
5599 %<latexrelease> { \exp_args:NNx \prop_gput:Nnn \g_@@_used_prop
5600 %<latexrelease> {#1} { } }
5601 %<latexrelease> }
5602 %<latexrelease> }
5603 %<latexrelease> }
5604 %<latexrelease>\EndIncludeInRelease
5605 % \end{macrocode}
5606 % \end{macro}
5607 %
5608 %
5609 % \begin{macro}[EXP]{\@@_tl_csname:n,\@@_seq_csname:n}
5610 % It is faster to pass a single token and expand it when necessary
5611 % than to pass a bunch of character tokens around.
5612 % \fmiinline{note to myself: verify}
5613 % \begin{macrocode}
5614 \cs_new:Npn \@@_tl_csname:n #1 { l_@@_label_#1_tl }
5615 \cs_new:Npn \@@_seq_csname:n #1 { l_@@_label_#1_seq }
5616 % \end{macrocode}
5617 % \end{macro}
5618 %
5619 %
5620 % \begin{macro}{\l_@@_labels_seq,\l_@@_labels_int,\l_@@_front_tl,
5621 % \l_@@_rear_tl,\l_@@_label_0_tl}
5622 %
5623 % For the sorting I am basically implementing Knuth's algorithm for
5624 % topological sorting as given in TAOCP volume 1 pages 263--266.
5625 % For this algorithm we need a number of local variables:
5626 % \begin{itemize}
5627 % \item
5628 % List of labels used in the current hook to label code chunks:
5629 % \begin{macrocode}
5630 \seq_new:N \l_@@_labels_seq
5631 % \end{macrocode}
5632 % \item
5633 % Number of labels used in the current hook. In Knuth's algorithm
5634 % this is called $N$:
5635 % \begin{macrocode}
5636 \int_new:N \l_@@_labels_int
5637 % \end{macrocode}
5638 % \item
5639 % The sorted code list to be build is managed using two pointers
5640 % one to the front of the queue and one to the rear. We model this
5641 % using token list pointers. Knuth calls them $F$ and $R$:
5642 % \begin{macrocode}
5643 \tl_new:N \l_@@_front_tl
5644 \tl_new:N \l_@@_rear_tl
5645 % \end{macrocode}
5646 % \item
5647 % The data for the start of the queue is kept in this token list,
5648 % it corresponds to what Don calls \texttt{QLINK[0]} but since we
5649 % aren't manipulating individual words in memory it is slightly
5650 % differently done:
5651 % \begin{macrocode}
5652 \tl_new:c { \@@_tl_csname:n { 0 } }
5653 % \end{macrocode}
5654 %
5655 % \end{itemize}
5656 % \end{macro}
5657 %
5658 %
5659 % \begin{macro}{\@@_initialize_single:NNn,\@@_initialize_single:ccn}
5660 %
5661 % \cs{@@_initialize_single:NNn} implements the sorting of the code
5662 % chunks for a hook and saves the result in the token list for fast
5663 % execution (\verb=#4=). The arguments are \meta{hook-code-plist},
5664 % \meta{hook-code-tl}, \meta{hook-top-level-code-tl},
5665 % \meta{hook-next-code-tl},
5666 % \meta{hook-ordered-labels-clist} and \meta{hook-name} (the latter
5667 % is only used for debugging---the \meta{hook-rule-plist} is accessed
5668 % using the \meta{hook-name}).
5669 %
5670 % The additional complexity compared to Don's algorithm is that we
5671 % do not use simple positive integers but have arbitrary
5672 % alphanumeric labels. As usual Don's data structures are chosen in
5673 % a way that one can omit a lot of tests and I have mimicked that as
5674 % far as possible. The result is a restriction I do not test for at
5675 % the moment: a label can't be equal to the number 0!
5676 % \fmiinline{Needs checking for, just in case ... maybe}
5677 %
5678 % ^^A #1 <- \@@~#1
5679 % ^^A #2 <- \g_@@_#1_labels_clist
5680 % ^^A #3 <- #1
5681 %
5682 % \changes{v1.1a}{2023/04/06}
5683 % {Changes to add hook arguments (hook-args).}
5684 % \begin{macrocode}
5685 %<latexrelease>\IncludeInRelease{2023/06/01}{\@@_initialize_single:NNn}
5686 %<latexrelease> {Hooks~with~args}
5687 \cs_new_protected:Npn \@@_initialize_single:NNn #1#2#3
5688 {
5689 % \end{macrocode}
5690 % Step T1: Initialize the data structure \ldots
5691 % \begin{macrocode}
5692 \seq_clear:N \l_@@_labels_seq
5693 \int_zero:N \l_@@_labels_int
5694 % \end{macrocode}
5695 %
5696 % Store the name of the hook:
5697 % \begin{macrocode}
5698 \tl_set:Nn \l_@@_cur_hook_tl {#3}
5699 % \end{macrocode}
5700 %
5701 % We loop over the property list holding the code and record all
5702 % the labels listed there. Only the rules for those labels are of interest
5703 % to us. While we are at it we count them (which gives us the $N$
5704 % in Knuth's algorithm). The prefix |label_| is added to the variables
5705 % to ensure that labels named |front|, |rear|, |labels|, or |return|
5706 % don't interact with our code.
5707 % \begin{macrocode}
5708 \prop_map_inline:Nn \l_@@_work_prop
5709 {
5710 \int_incr:N \l_@@_labels_int
5711 \seq_put_right:Nn \l_@@_labels_seq {##1}
5712 \@@_tl_set:cn { \@@_tl_csname:n {##1} } { 0 }
5713 \seq_clear_new:c { \@@_seq_csname:n {##1} }
5714 }
5715 % \end{macrocode}
5716 % Steps T2 and T3: Here we sort the relevant rules into the data structure\ldots
5717 %
5718 % This loop constitutes a square matrix of the labels in
5719 % \cs{l_@@_work_prop} in the
5720 % vertical and the horizontal directions. However, since the rule
5721 % $l_A\meta{rel}l_B$ is the same as $l_B\meta{rel}^{-1}l_A$ we can cut
5722 % the loop short at the diagonal of the matrix (\emph{i.e.}, when
5723 % both labels are equal), saving a good amount of time. The way the
5724 % rules were set up (see the implementation of \cs{@@_rule_before_gset:nnn}
5725 % above) ensures that we have no rule in the ignored side of the
5726 % matrix, and all rules are seen. The rules are applied in
5727 % \cs{@@_apply_label_pair:nnn}, which takes the properly-ordered pair
5728 % of labels as argument.
5729 % \begin{macrocode}
5730 \prop_map_inline:Nn \l_@@_work_prop
5731 {
5732 \prop_map_inline:Nn \l_@@_work_prop
5733 {
5734 \@@_if_label_case:nnnnn {##1} {####1}
5735 { \prop_map_break: }
5736 { \@@_apply_label_pair:nnn {##1} {####1} }
5737 { \@@_apply_label_pair:nnn {####1} {##1} }
5738 {#3}
5739 }
5740 }
5741 % \end{macrocode}
5742 % Now take a breath, and look at the data structures that have
5743 % been set up:
5744 % \begin{macrocode}
5745 \@@_debug:n { \@@_debug_label_data:N \l_@@_work_prop }
5746 % \end{macrocode}
5747 %
5748 %
5749 % Step T4:
5750 % \begin{macrocode}
5751 \tl_set:Nn \l_@@_rear_tl { 0 }
5752 \tl_set:cn { \@@_tl_csname:n { 0 } } { 0 }
5753 \seq_map_inline:Nn \l_@@_labels_seq
5754 {
5755 \int_compare:nNnT { \cs:w \@@_tl_csname:n {##1} \cs_end: } = 0
5756 {
5757 \tl_set:cn { \@@_tl_csname:n { \l_@@_rear_tl } }{##1}
5758 \tl_set:Nn \l_@@_rear_tl {##1}
5759 }
5760 }
5761 \tl_set_eq:Nc \l_@@_front_tl { \@@_tl_csname:n { 0 } }
5762 % \end{macrocode}
5763 %
5764 % \begin{macrocode}
5765 \@@_tl_gclear:N #1
5766 \clist_gclear:N #2
5767 % \end{macrocode}
5768 %
5769 % The whole loop gets combined in steps T5--T7:
5770 % \begin{macrocode}
5771 \bool_while_do:nn { ! \str_if_eq_p:Vn \l_@@_front_tl { 0 } }
5772 {
5773 % \end{macrocode}
5774 % This part is step T5:
5775 % \begin{macrocode}
5776 \int_decr:N \l_@@_labels_int
5777 \prop_get:NVN \l_@@_work_prop \l_@@_front_tl \l_@@_return_tl
5778 \exp_args:NNV \@@_tl_gput:Nn #1 \l_@@_return_tl
5779 % \end{macrocode}
5780 %
5781 % \changes{v1.1l}{2024/11/26}{Adjust debugging message (gh/1459)}
5782 % \begin{macrocode}
5783 \@@_clist_gput:NV #2 \l_@@_front_tl
5784 \@@_debug:n{ \iow_term:e{[lthooks]~ Handled~ code~ for~ \l_@@_front_tl} }
5785 % \end{macrocode}
5786 %
5787 % This is step T6, except that we don't use a pointer $P$ to move
5788 % through the successors, but instead use \verb=##1= of the mapping
5789 % function.
5790 % \begin{macrocode}
5791 \seq_map_inline:cn { \@@_seq_csname:n { \l_@@_front_tl } }
5792 {
5793 \tl_set:ce { \@@_tl_csname:n {##1} }
5794 { \int_eval:n
5795 { \cs:w \@@_tl_csname:n {##1} \cs_end: - 1 }
5796 }
5797 \int_compare:nNnT
5798 { \cs:w \@@_tl_csname:n {##1} \cs_end: } = 0
5799 {
5800 \tl_set:cn
5801 { \@@_tl_csname:n { \l_@@_rear_tl } } {##1}
5802 \tl_set:Nn \l_@@_rear_tl {##1}
5803 }
5804 }
5805 % \end{macrocode}
5806 % and here is step T7:
5807 % \begin{macrocode}
5808 \tl_set_eq:Nc \l_@@_front_tl
5809 { \@@_tl_csname:n { \l_@@_front_tl } }
5810 % \end{macrocode}
5811 %
5812 % This is step T8: If we haven't moved the code for all labels
5813 % (i.e., if \cs{l_@@_labels_int} is still greater than zero) we
5814 % have a loop and our partial order can't be flattened out.
5815 % \begin{macrocode}
5816 }
5817 \int_compare:nNnF \l_@@_labels_int = 0
5818 {
5819 \iow_term:e{====================}
5820 \iow_term:e{Error:~ label~ rules~ are~ incompatible:}
5821 % \end{macrocode}
5822 %
5823 % This is not really the information one needs in the error case
5824 % but it will do for now \ldots \fmiinline{improve output on a rainy day}
5825 % \begin{macrocode}
5826 \@@_debug_label_data:N \l_@@_work_prop
5827 \iow_term:e{====================}
5828 }
5829 % \end{macrocode}
5830 % After we have added all hook code to \verb=#1=, we finish it off
5831 % by adding extra code for the |top-level| (\verb=#2=) and for one
5832 % time execution (\verb=#3=). These should normally be empty. The
5833 % |top-level| code is added with \cs{@@_tl_gput:Nn} as that might
5834 % change for a reversed hook (then |top-level| is the very first code
5835 % chunk added). The |next| code is always added last (to the right).
5836 % The hook may take arguments, so we add a run of braced parameters
5837 % after the \verb|_next| and \verb|_toplevel| macros, so that the
5838 % arguments passed to the hook are forwarded to them.
5839 % \begin{macrocode}
5840 \exp_args:NNe \@@_tl_gput:Nn #1
5841 { \exp_not:c { @@_toplevel~#3 } \@@_braced_parameter:n {#3} }
5842 \@@_tl_gput_right:Ne #1
5843 { \exp_not:c { @@_next~#3 } \@@_braced_parameter:n {#3} }
5844 \use:e
5845 {
5846 \cs_gset:cpn { @@~#3 } \use:c { c_@@_#3_parameter_tl }
5847 { \exp_not:V #1 }
5848 }
5849 }
5850 % \end{macrocode}
5851 %
5852 % \begin{macrocode}
5853 \cs_generate_variant:Nn \@@_initialize_single:NNn { cc }
5854 %<latexrelease>\EndIncludeInRelease
5855 % \end{macrocode}
5856 %
5857 % \begin{macrocode}
5858 %<latexrelease>\IncludeInRelease{2020/10/01}{\@@_initialize_single:NNn}
5859 %<latexrelease> {Hooks~with~args}
5860 %<latexrelease>\cs_new_protected:Npn \@@_initialize_single:NNn #1#2#3
5861 %<latexrelease> {
5862 %<latexrelease> \seq_clear:N \l_@@_labels_seq
5863 %<latexrelease> \int_zero:N \l_@@_labels_int
5864 %<latexrelease> \tl_set:Nn \l_@@_cur_hook_tl {#3}
5865 %<latexrelease> \prop_map_inline:Nn \l_@@_work_prop
5866 %<latexrelease> {
5867 %<latexrelease> \int_incr:N \l_@@_labels_int
5868 %<latexrelease> \seq_put_right:Nn \l_@@_labels_seq {##1}
5869 %<latexrelease> \@@_tl_set:cn { \@@_tl_csname:n {##1} } { 0 }
5870 %<latexrelease> \seq_clear_new:c { \@@_seq_csname:n {##1} }
5871 %<latexrelease> }
5872 %<latexrelease> \prop_map_inline:Nn \l_@@_work_prop
5873 %<latexrelease> {
5874 %<latexrelease> \prop_map_inline:Nn \l_@@_work_prop
5875 %<latexrelease> {
5876 %<latexrelease> \@@_if_label_case:nnnnn {##1} {####1}
5877 %<latexrelease> { \prop_map_break: }
5878 %<latexrelease> { \@@_apply_label_pair:nnn {##1} {####1} }
5879 %<latexrelease> { \@@_apply_label_pair:nnn {####1} {##1} }
5880 %<latexrelease> {#3}
5881 %<latexrelease> }
5882 %<latexrelease> }
5883 %<latexrelease> \@@_debug:n
5884 %<latexrelease> { \@@_debug_label_data:N \l_@@_work_prop }
5885 %<latexrelease> \tl_set:Nn \l_@@_rear_tl { 0 }
5886 %<latexrelease> \tl_set:cn { \@@_tl_csname:n { 0 } } { 0 }
5887 %<latexrelease> \seq_map_inline:Nn \l_@@_labels_seq
5888 %<latexrelease> {
5889 %<latexrelease> \int_compare:nNnT
5890 %<latexrelease> { \cs:w \@@_tl_csname:n {##1} \cs_end: } = 0
5891 %<latexrelease> {
5892 %<latexrelease> \tl_set:cn { \@@_tl_csname:n
5893 %<latexrelease> { \l_@@_rear_tl } } {##1}
5894 %<latexrelease> \tl_set:Nn \l_@@_rear_tl {##1}
5895 %<latexrelease> }
5896 %<latexrelease> }
5897 %<latexrelease> \tl_set_eq:Nc \l_@@_front_tl { \@@_tl_csname:n { 0 } }
5898 %<latexrelease> \@@_tl_gclear:N #1
5899 %<latexrelease> \clist_gclear:N #2
5900 %<latexrelease> \bool_while_do:nn
5901 %<latexrelease> { ! \str_if_eq_p:Vn \l_@@_front_tl { 0 } }
5902 %<latexrelease> {
5903 %<latexrelease> \int_decr:N \l_@@_labels_int
5904 %<latexrelease> \prop_get:NVN \l_@@_work_prop
5905 %<latexrelease> \l_@@_front_tl \l_@@_return_tl
5906 %<latexrelease> \exp_args:NNV \@@_tl_gput:Nn #1 \l_@@_return_tl
5907 %<latexrelease> \@@_clist_gput:NV #2 \l_@@_front_tl
5908 %<latexrelease> \@@_debug:n{ \iow_term:x
5909 %<latexrelease> {Handled~ code~ for~ \l_@@_front_tl} }
5910 %<latexrelease> \seq_map_inline:cn
5911 %<latexrelease> { \@@_seq_csname:n { \l_@@_front_tl } }
5912 %<latexrelease> {
5913 %<latexrelease> \tl_set:cx { \@@_tl_csname:n {##1} }
5914 %<latexrelease> { \int_eval:n
5915 %<latexrelease> { \cs:w \@@_tl_csname:n {##1} \cs_end: - 1 }
5916 %<latexrelease> }
5917 %<latexrelease> \int_compare:nNnT
5918 %<latexrelease> { \cs:w \@@_tl_csname:n {##1} \cs_end: } = 0
5919 %<latexrelease> {
5920 %<latexrelease> \tl_set:cn { \@@_tl_csname:n
5921 %<latexrelease> { \l_@@_rear_tl } } {##1}
5922 %<latexrelease> \tl_set:Nn \l_@@_rear_tl {##1}
5923 %<latexrelease> }
5924 %<latexrelease> }
5925 %<latexrelease> \tl_set_eq:Nc \l_@@_front_tl
5926 %<latexrelease> { \@@_tl_csname:n { \l_@@_front_tl } }
5927 %<latexrelease> }
5928 %<latexrelease> \int_compare:nNnF \l_@@_labels_int = 0
5929 %<latexrelease> {
5930 %<latexrelease> \iow_term:x{====================}
5931 %<latexrelease> \iow_term:x{Error:~ label~ rules~ are~ incompatible:}
5932 %<latexrelease> \@@_debug_label_data:N \l_@@_work_prop
5933 %<latexrelease> \iow_term:x{====================}
5934 %<latexrelease> }
5935 %<latexrelease> \exp_args:NNo \@@_tl_gput:Nn #1
5936 %<latexrelease> { \cs:w @@_toplevel~#3 \cs_end: }
5937 %<latexrelease> \@@_tl_gput_right:No #1 { \cs:w @@_next~#3 \cs_end: }
5938 %<latexrelease> }
5939 %<latexrelease>\cs_generate_variant:Nn \@@_tl_gput_right:Nn { No }
5940 %<latexrelease>\EndIncludeInRelease
5941 % \end{macrocode}
5942 % \end{macro}
5943 %
5944 %
5945 %
5946 % \begin{macro}{\@@_tl_gput:Nn,\@@_clist_gput:NV}
5947 % These append either on the right (normal hook) or on the left
5948 % (reversed hook). This is setup up in
5949 % \cs{@@_initialize_hook_code:n}, elsewhere their behavior is undefined.
5950 % \begin{macrocode}
5951 \cs_new:Npn \@@_tl_gput:Nn { \ERROR }
5952 \cs_new:Npn \@@_clist_gput:NV { \ERROR }
5953 % \end{macrocode}
5954 % \end{macro}
5955 %
5956 %
5957 %
5958 % \begin{macro}{\@@_apply_label_pair:nnn,\@@_label_if_exist_apply:nnnF}
5959 %
5960 % This is the payload of steps T2 and T3 executed in the loop described
5961 % above. This macro assumes |#1| and |#2| are ordered, which means that
5962 % any rule pertaining the pair |#1| and |#2| is
5963 % \cs{g_@@_\meta{hook}_rule_\#1\string|\#2_tl}, and not
5964 % \cs{g_@@_\meta{hook}_rule_\#2\string|\#1_tl}. This also saves a great deal
5965 % of time since we only need to check the order of the labels once.
5966 %
5967 % The arguments here are \meta{label1}, \meta{label2}, \meta{hook}, and
5968 % \meta{hook-code-plist}. We are about to apply the next rule and
5969 % enter it into the data structure. \cs{@@_apply_label_pair:nnn} will
5970 % just call \cs{@@_label_if_exist_apply:nnnF} for the \meta{hook}, and
5971 % if no rule is found, also try the \meta{hook} name \texttt{??}
5972 % denoting a default hook rule.
5973 %
5974 % \cs{@@_label_if_exist_apply:nnnF} will check if the rule exists for
5975 % the given hook, and if so call \cs{@@_apply_rule:nnn}.
5976 % \begin{macrocode}
5977 \cs_new_protected:Npn \@@_apply_label_pair:nnn #1#2#3
5978 {
5979 % \end{macrocode}
5980 % Extra complication: as we use default rules and local hook specific
5981 % rules we first have to check if there is a local rule and if that
5982 % exist use it. Otherwise check if there is a default rule and use
5983 % that.
5984 % \begin{macrocode}
5985 \@@_label_if_exist_apply:nnnF {#1} {#2} {#3}
5986 {
5987 % \end{macrocode}
5988 % If there is no hook-specific rule we check for a default one and
5989 % use that if it exists.
5990 % \begin{macrocode}
5991 \@@_label_if_exist_apply:nnnF {#1} {#2} { ?? } { }
5992 }
5993 }
5994 \cs_new_protected:Npn \@@_label_if_exist_apply:nnnF #1#2#3
5995 {
5996 \if_cs_exist:w g_@@_ #3 _rule_ #1 | #2 _tl \cs_end:
5997 % \end{macrocode}
5998 % What to do precisely depends on the type of rule we have
5999 % encountered. If it is a \texttt{before} rule it will be handled by the
6000 % algorithm but other types need to be managed differently. All
6001 % this is done in \cs{@@_apply_rule:nnnN}.
6002 % \begin{macrocode}
6003 \@@_apply_rule:nnn {#1} {#2} {#3}
6004 \exp_after:wN \use_none:n
6005 \else:
6006 \use:nn
6007 \fi:
6008 }
6009 % \end{macrocode}
6010 % \end{macro}
6011 %
6012 %
6013 %
6014 %
6015 % \begin{macro}{\@@_apply_rule:nnn}
6016 % This is the code executed in steps T2 and T3 while looping through
6017 % the matrix This is part of step T3. We are about to apply the next
6018 % rule and enter it into the data structure. The arguments are
6019 % \meta{label1}, \meta{label2}, \meta{hook-name}, and \meta{hook-code-plist}.
6020 % \begin{macrocode}
6021 \cs_new_protected:Npn \@@_apply_rule:nnn #1#2#3
6022 {
6023 \cs:w @@_apply_
6024 \cs:w g_@@_#3_reversed_tl \cs_end: rule_
6025 \cs:w g_@@_ #3 _rule_ #1 | #2 _tl \cs_end: :nnn \cs_end:
6026 {#1} {#2} {#3}
6027 }
6028 % \end{macrocode}
6029 % \end{macro}
6030 %
6031 % \begin{macro}{\@@_apply_rule_<:nnn,\@@_apply_rule_>:nnn}
6032 % The most common cases are \texttt{\string<} and \texttt{\string>} so we handle
6033 % that first. They are relations $\prec$ and $\succ$ in TAOCP, and
6034 % they dictate sorting.
6035 % \begin{macrocode}
6036 \cs_new_protected:cpn { @@_apply_rule_<:nnn } #1#2#3
6037 {
6038 \@@_debug:n { \@@_msg_pair_found:nnn {#1} {#2} {#3} }
6039 \tl_set:ce { \@@_tl_csname:n {#2} }
6040 { \int_eval:n{ \cs:w \@@_tl_csname:n {#2} \cs_end: + 1 } }
6041 \seq_put_right:cn{ \@@_seq_csname:n {#1} }{#2}
6042 }
6043 \cs_new_protected:cpn { @@_apply_rule_>:nnn } #1#2#3
6044 {
6045 \@@_debug:n { \@@_msg_pair_found:nnn {#1} {#2} {#3} }
6046 \tl_set:ce { \@@_tl_csname:n {#1} }
6047 { \int_eval:n{ \cs:w \@@_tl_csname:n {#1} \cs_end: + 1 } }
6048 \seq_put_right:cn{ \@@_seq_csname:n {#2} }{#1}
6049 }
6050 % \end{macrocode}
6051 % \end{macro}
6052 %
6053 % \begin{macro}{\@@_apply_rule_xE:nnn,\@@_apply_rule_xW:nnn}
6054 % These relations make two labels incompatible within a hook.
6055 % |xE| makes raises an error if the labels are found in the same
6056 % hook, and |xW| makes it a warning.
6057 % \begin{macrocode}
6058 \cs_new_protected:cpn { @@_apply_rule_xE:nnn } #1#2#3
6059 {
6060 \@@_debug:n { \@@_msg_pair_found:nnn {#1} {#2} {#3} }
6061 \msg_error:nnnnnn { hooks } { labels-incompatible }
6062 {#1} {#2} {#3} { 1 }
6063 \use:c { @@_apply_rule_->:nnn } {#1} {#2} {#3}
6064 \use:c { @@_apply_rule_<-:nnn } {#1} {#2} {#3}
6065 }
6066 \cs_new_protected:cpn { @@_apply_rule_xW:nnn } #1#2#3
6067 {
6068 \@@_debug:n { \@@_msg_pair_found:nnn {#1} {#2} {#3} }
6069 \msg_warning:nnnnnn { hooks } { labels-incompatible }
6070 {#1} {#2} {#3} { 0 }
6071 }
6072 % \end{macrocode}
6073 % \end{macro}
6074 %
6075 % \begin{macro}{\@@_apply_rule_->:nnn,\@@_apply_rule_<-:nnn}
6076 % If we see \texttt{\detokenize{->}} we have to drop code for label
6077 % \verb=#3= and carry on. We could do a little better and drop
6078 % everything for that label since it doesn't matter where we put
6079 % such empty code. However that would complicate the algorithm a
6080 % lot with little gain.\footnote{This also has the advantage that
6081 % the result of the sorting doesn't change, as it might otherwise do
6082 % (for unrelated chunks) if we aren't careful.} So we still
6083 % unnecessarily try to sort it in and depending on the rules that
6084 % might result in a loop that is otherwise resolved. If that turns
6085 % out to be a real issue, we can improve the code.
6086 %
6087 % Here the code is removed from \cs{l_@@_cur_hook_tl} rather than
6088 % \verb=#3= because the latter may be \texttt{??}, and the default
6089 % hook doesn't store any code. Removing it instead from \cs{l_@@_cur_hook_tl}
6090 % makes the default rules \verb=->= and \verb=<-= work properly.
6091 % \begin{macrocode}
6092 \cs_new_protected:cpn { @@_apply_rule_->:nnn } #1#2#3
6093 {
6094 \@@_debug:n
6095 {
6096 \@@_msg_pair_found:nnn {#1} {#2} {#3}
6097 \iow_term:e{--->~ Drop~ '#2'~ code~ from~
6098 \iow_char:N \\ g_@@_ \l_@@_cur_hook_tl _code_prop ~
6099 because~ of~ '#1' }
6100 }
6101 \prop_put:Nnn \l_@@_work_prop {#2} { }
6102 }
6103 \cs_new_protected:cpn { @@_apply_rule_<-:nnn } #1#2#3
6104 {
6105 \@@_debug:n
6106 {
6107 \@@_msg_pair_found:nnn {#1} {#2} {#3}
6108 \iow_term:e{--->~ Drop~ '#1'~ code~ from~
6109 \iow_char:N \\ g_@@_ \l_@@_cur_hook_tl _code_prop ~
6110 because~ of~ '#2' }
6111 }
6112 \prop_put:Nnn \l_@@_work_prop {#1} { }
6113 }
6114 % \end{macrocode}
6115 % \end{macro}
6116 %
6117 % \begin{macro}{
6118 % \@@_apply_-rule_<:nnn,
6119 % \@@_apply_-rule_>:nnn,
6120 % \@@_apply_-rule_<-:nnn,
6121 % \@@_apply_-rule_->:nnn,
6122 % \@@_apply_-rule_xW:nnn,
6123 % \@@_apply_-rule_xE:nnn,
6124 % }
6125 % Reversed rules.
6126 % \begin{macrocode}
6127 \cs_new_eq:cc { @@_apply_-rule_<:nnn } { @@_apply_rule_>:nnn }
6128 \cs_new_eq:cc { @@_apply_-rule_>:nnn } { @@_apply_rule_<:nnn }
6129 \cs_new_eq:cc { @@_apply_-rule_<-:nnn } { @@_apply_rule_<-:nnn }
6130 \cs_new_eq:cc { @@_apply_-rule_->:nnn } { @@_apply_rule_->:nnn }
6131 \cs_new_eq:cc { @@_apply_-rule_xE:nnn } { @@_apply_rule_xE:nnn }
6132 \cs_new_eq:cc { @@_apply_-rule_xW:nnn } { @@_apply_rule_xW:nnn }
6133 % \end{macrocode}
6134 % \end{macro}
6135 %
6136 %
6137 % \begin{macro}{\@@_msg_pair_found:nnn}
6138 % A macro to avoid moving this many tokens around.
6139 % \begin{macrocode}
6140 \cs_new_protected:Npn \@@_msg_pair_found:nnn #1#2#3
6141 {
6142 \iow_term:e{~ \str_if_eq:nnTF {#3} {??} {default} {~normal} ~
6143 rule~ \@@_label_pair:nn {#1} {#2}:~
6144 \use:c { g_@@_#3_rule_ \@@_label_pair:nn {#1} {#2} _tl } ~
6145 found}
6146 }
6147 % \end{macrocode}
6148 % \end{macro}
6149 %
6150 %
6151 % \begin{macro}{\@@_debug_label_data:N}
6152 %
6153 % \begin{macrocode}
6154 \cs_new_protected:Npn \@@_debug_label_data:N #1 {
6155 \iow_term:e{Code~ labels~ for~ sorting:}
6156 \iow_term:e{~ \seq_use:Nnnn\l_@@_labels_seq {~and~}{,~}{~and~} }
6157 \iow_term:e{^^J Data~ structure~ for~ label~ rules:}
6158 \prop_map_inline:Nn #1
6159 {
6160 \iow_term:e{~ ##1~ =~ \tl_use:c{ \@@_tl_csname:n {##1} }~ ->~
6161 \seq_use:cnnn{ \@@_seq_csname:n {##1} }{~->~}{~->~}{~->~}
6162 }
6163 }
6164 \iow_term:e{}
6165 }
6166 % \end{macrocode}
6167 % \end{macro}
6168 %
6169 %
6170 %
6171 % \begin{macro}{\hook_show:n,\hook_log:n}
6172 % \changes{v1.1m}{2025/06/27}{Support \cs{showstream} (gh/1062)}
6173 % \begin{macro}{\@@_log_line:e,\@@_log_line_indent:e}
6174 % \begin{macro}{\@@_log:nN}
6175 % This writes out information about the hook given in its argument
6176 % onto the \texttt{.log} file and the terminal, if \cs{show_hook:n} is
6177 % used. Internally both share the same structure, except that at the
6178 % end, \cs{hook_show:n} triggers \TeX's prompt.
6179 % \begin{macrocode}
6180 \cs_new_protected:Npn \hook_log:n #1
6181 {
6182 \cs_set_eq:NN \@@_log_cmd:e \iow_log:e
6183 \@@_normalize_hook_args:Nn \@@_log:nN {#1} \tl_log:e
6184 }
6185 \cs_if_exist:NTF \iow_show:e
6186 {
6187 \cs_new_protected:Npn \hook_show:n #1
6188 {
6189 \cs_set_eq:NN \@@_log_cmd:e \iow_show:e
6190 \@@_normalize_hook_args:Nn \@@_log:nN {#1} \tl_show:e
6191 }
6192 }
6193 {
6194 \cs_new_protected:Npn \hook_show:n #1
6195 {
6196 \cs_set_eq:NN \@@_log_cmd:e \iow_term:e
6197 \@@_normalize_hook_args:Nn \@@_log:nN {#1} \tl_show:e
6198 }
6199 }
6200 \cs_new_protected:Npn \@@_log_line:e #1
6201 { \@@_log_cmd:e { >~#1 } }
6202 \cs_new_protected:Npn \@@_log_line_indent:e #1
6203 { \@@_log_cmd:e { >~\@spaces #1 } }
6204 % \end{macrocode}
6205 %
6206 % \changes{v1.1a}{2023/04/06}
6207 % {Changes to add hook arguments (hook-args)}
6208 % \changes{v1.1g}{2024/01/03}
6209 % {Fix expansion of \cs{__hook__print_args:nn} argument (gh/1221)}
6210 % \begin{macrocode}
6211 %<latexrelease>\IncludeInRelease{2023/06/01}{\@@_log:nN}
6212 %<latexrelease> {Hooks~with~args}
6213 \cs_new_protected:Npn \@@_log:nN #1 #2
6214 {
6215 \@@_if_deprecated_generic:nT {#1}
6216 {
6217 \@@_deprecated_generic_warn:n {#1}
6218 \@@_do_deprecated_generic:Nn \@@_log:nN {#1} #2
6219 \exp_after:wN \use_none:nnnnnnnnn \use_none:nnnnn
6220 }
6221 \@@_preamble_hook:n {#1}
6222 \@@_log_cmd:e
6223 {
6224 ^^J ->~The~
6225 \@@_if_generic:nT {#1} { generic~ }
6226 hook~'#1'
6227 \@@_if_disabled:nF {#1}
6228 {
6229 \exp_args:Nne \@@_print_args:nn {#1}
6230 {
6231 \int_eval:n
6232 { \str_count:e { \@@_parameter:n {#1} } / 3 }
6233 }
6234 }
6235 :
6236 }
6237 % \end{macrocode}
6238 %
6239 % \changes{v1.1k}{2024/10/29}
6240 % {Skip mapping over undeclared \cs{g__hook_\meta{hook}_code_prop} (gh/1513)}
6241 % \begin{macrocode}
6242 \@@_if_usable:nF {#1}
6243 { \@@_log_line:e { The~hook~is~not~declared. } }
6244 \@@_if_disabled:nT {#1}
6245 { \@@_log_line:e { The~hook~is~disabled. } }
6246 \hook_if_empty:nTF {#1}
6247 { #2 { The~hook~is~empty } }
6248 {
6249 \@@_log_line:e { Code~chunks: }
6250 \bool_lazy_or:nnTF
6251 { ! \prop_if_exist_p:c { g_@@_#1_code_prop } }
6252 { \prop_if_empty_p:c { g_@@_#1_code_prop } }
6253 { \@@_log_line_indent:e { --- } }
6254 {
6255 \prop_map_inline:cn { g_@@_#1_code_prop }
6256 {
6257 \exp_after:wN \cs_set:Npn \exp_after:wN \@@_tmp:w
6258 \c_@@_nine_parameters_tl {##2}
6259 \@@_log_line_indent:e
6260 { ##1~->~\cs_replacement_spec:N \@@_tmp:w }
6261 }
6262 }
6263 % \end{macrocode}
6264 %
6265 % If there is code in the |top-level| token list, print it:
6266 % \begin{macrocode}
6267 \@@_log_line:e
6268 {
6269 Document-level~(top-level)~code
6270 \@@_if_usable:nT {#1}
6271 { ~(executed~\@@_if_reversed:nTF {#1} {first} {last} ) } :
6272 }
6273 \@@_log_line_indent:e
6274 {
6275 \@@_cs_if_empty:cTF { @@_toplevel~#1 }
6276 { --- }
6277 { -> ~ \cs_replacement_spec:c { @@_toplevel~#1 } }
6278 }
6279 % \end{macrocode}
6280 %
6281 % \begin{macrocode}
6282 \@@_log_line:e { Extra~code~for~next~invocation: }
6283 \@@_log_line_indent:e
6284 {
6285 \@@_cs_if_empty:cTF { @@_next~#1 }
6286 { --- }
6287 % \end{macrocode}
6288 %
6289 % If the token list is not empty we want to display it but without
6290 % the first tokens (the code to clear itself) so we call a helper
6291 % command to get rid of them.
6292 % \begin{macrocode}
6293 {
6294 -> ~ \exp_last_unbraced:Nf \@@_log_next_code:w
6295 { \cs_replacement_spec:c { @@_next~#1 } }
6296 }
6297 }
6298 % \end{macrocode}
6299 %
6300 % Loop through the rules in a hook and for every rule found, print it.
6301 % If no rule is there, print |---|. The boolean \cs{l_@@_tmpa_bool}
6302 % here indicates if the hook has no rules.
6303 % \begin{macrocode}
6304 \@@_log_line:e { Rules: }
6305 \bool_set_true:N \l_@@_tmpa_bool
6306 \@@_list_rules:nn {#1}
6307 {
6308 \bool_set_false:N \l_@@_tmpa_bool
6309 \@@_log_line_indent:e
6310 {
6311 ##2~ with~
6312 \str_if_eq:nnT {##3} {??} { default~ }
6313 relation~ ##1
6314 }
6315 }
6316 \bool_if:NT \l_@@_tmpa_bool
6317 { \@@_log_line_indent:e { --- } }
6318 % \end{macrocode}
6319 %
6320 % When the hook is declared (that is, the sorting algorithm is applied
6321 % to that hook) and not empty
6322 % \begin{macrocode}
6323 \bool_lazy_and:nnTF
6324 { \@@_if_usable_p:n {#1} }
6325 { ! \hook_if_empty_p:n {#1} }
6326 {
6327 \@@_log_line:e
6328 {
6329 Execution~order
6330 \bool_if:NTF \l_@@_tmpa_bool
6331 { \@@_if_reversed:nT {#1} { ~(after~reversal) } }
6332 { ~(after~
6333 \@@_if_reversed:nT {#1} { reversal~and~ }
6334 applying~rules)
6335 } :
6336 }
6337 #2 % \tl_show:n
6338 {
6339 \@spaces
6340 \clist_if_empty:cTF { g_@@_#1_labels_clist }
6341 { --- }
6342 { \clist_use:cn { g_@@_#1_labels_clist } { ,~ } }
6343 }
6344 }
6345 {
6346 \@@_log_line:e { Execution~order: }
6347 #2
6348 {
6349 \@spaces Not~set~because~the~hook~ \@@_if_usable:nTF {#1}
6350 { code~pool~is~empty }
6351 { is~\@@_if_disabled:nTF {#1} {disabled} {undeclared} }
6352 }
6353 }
6354 }
6355 }
6356 %<latexrelease>\EndIncludeInRelease
6357 %
6358 %<latexrelease>\IncludeInRelease{2020/10/01}{\@@_log:nN}
6359 %<latexrelease> {Hooks~with~args}
6360 %<latexrelease>\cs_new_protected:Npn \@@_log:nN #1 #2
6361 %<latexrelease> {
6362 %<latexrelease> \@@_if_deprecated_generic:nT {#1}
6363 %<latexrelease> {
6364 %<latexrelease> \@@_deprecated_generic_warn:n {#1}
6365 %<latexrelease> \@@_do_deprecated_generic:Nn \@@_log:nN {#1} #2
6366 %<latexrelease> \exp_after:wN \use_none:nnnnnnnnn \use_none:nnnnn
6367 %<latexrelease> }
6368 %<latexrelease> \@@_preamble_hook:n {#1}
6369 %<latexrelease> \@@_log_cmd:e
6370 %<latexrelease> { ^^J ->~The~ \@@_if_generic:nT
6371 %<latexrelease> {#1} { generic~ } hook~'#1': }
6372 %<latexrelease> \@@_if_usable:nF {#1}
6373 %<latexrelease> { \@@_log_line:e { The~hook~is~not~declared. } }
6374 %<latexrelease> \@@_if_disabled:nT {#1}
6375 %<latexrelease> { \@@_log_line:e { The~hook~is~disabled. } }
6376 %<latexrelease> \hook_if_empty:nTF {#1}
6377 %<latexrelease> { #2 { The~hook~is~empty } }
6378 %<latexrelease> {
6379 %<latexrelease> \@@_log_line:e { Code~chunks: }
6380 %<latexrelease> \prop_if_empty:cTF { g_@@_#1_code_prop }
6381 %<latexrelease> { \@@_log_line_indent:e { --- } }
6382 %<latexrelease> {
6383 %<latexrelease> \prop_map_inline:cn { g_@@_#1_code_prop }
6384 %<latexrelease> { \@@_log_line_indent:e
6385 %<latexrelease> { ##1~->~\tl_to_str:n {##2} } }
6386 %<latexrelease> }
6387 %<latexrelease> \@@_log_line:e
6388 %<latexrelease> {
6389 %<latexrelease> Document-level~(top-level)~code
6390 %<latexrelease> \@@_if_usable:nT {#1}
6391 %<latexrelease> { ~(executed~
6392 %<latexrelease> \@@_if_reversed:nTF {#1} {first} {last} ) } :
6393 %<latexrelease> }
6394 %<latexrelease> \@@_log_line_indent:e
6395 %<latexrelease> {
6396 %<latexrelease> \tl_if_empty:cTF { @@_toplevel~#1 }
6397 %<latexrelease> { --- }
6398 %<latexrelease> { -> ~ \exp_args:Nv \tl_to_str:n
6399 %<latexrelease> { @@_toplevel~#1 } }
6400 %<latexrelease> }
6401 %<latexrelease> \@@_log_line:e { Extra~code~for~next~invocation: }
6402 %<latexrelease> \@@_log_line_indent:e
6403 %<latexrelease> {
6404 %<latexrelease> \tl_if_empty:cTF { @@_next~#1 }
6405 %<latexrelease> { --- }
6406 %<latexrelease> { ->~ \exp_args:Nv \@@_log_next_code:n
6407 %<latexrelease> { @@_next~#1 } }
6408 %<latexrelease> }
6409 %<latexrelease> \@@_log_line:e { Rules: }
6410 %<latexrelease> \bool_set_true:N \l_@@_tmpa_bool
6411 %<latexrelease> \@@_list_rules:nn {#1}
6412 %<latexrelease> {
6413 %<latexrelease> \bool_set_false:N \l_@@_tmpa_bool
6414 %<latexrelease> \@@_log_line_indent:e
6415 %<latexrelease> {
6416 %<latexrelease> ##2~ with~
6417 %<latexrelease> \str_if_eq:nnT {##3} {??} { default~ }
6418 %<latexrelease> relation~ ##1
6419 %<latexrelease> }
6420 %<latexrelease> }
6421 %<latexrelease> \bool_if:NT \l_@@_tmpa_bool
6422 %<latexrelease> { \@@_log_line_indent:e { --- } }
6423 %<latexrelease> \bool_lazy_and:nnTF
6424 %<latexrelease> { \@@_if_usable_p:n {#1} }
6425 %<latexrelease> { ! \hook_if_empty_p:n {#1} }
6426 %<latexrelease> {
6427 %<latexrelease> \@@_log_line:e
6428 %<latexrelease> {
6429 %<latexrelease> Execution~order
6430 %<latexrelease> \bool_if:NTF \l_@@_tmpa_bool
6431 %<latexrelease> { \@@_if_reversed:nT
6432 %<latexrelease> {#1}{ ~(after~reversal) } }
6433 %<latexrelease> { ~(after~
6434 %<latexrelease> \@@_if_reversed:nT {#1} { reversal~and~ }
6435 %<latexrelease> applying~rules)
6436 %<latexrelease> } :
6437 %<latexrelease> }
6438 %<latexrelease> #2 % \tl_show:n
6439 %<latexrelease> {
6440 %<latexrelease> \@spaces
6441 %<latexrelease> \clist_if_empty:cTF { g_@@_#1_labels_clist }
6442 %<latexrelease> { --- }
6443 %<latexrelease> { \clist_use:cn
6444 %<latexrelease> { g_@@_#1_labels_clist } { ,~ } }
6445 %<latexrelease> }
6446 %<latexrelease> }
6447 %<latexrelease> {
6448 %<latexrelease> \@@_log_line:e { Execution~order: }
6449 %<latexrelease> #2
6450 %<latexrelease> {
6451 %<latexrelease> \@spaces Not~set~because~the~hook~
6452 %<latexrelease> \@@_if_usable:nTF {#1}
6453 %<latexrelease> { code~pool~is~empty }
6454 %<latexrelease> { is~\@@_if_disabled:nTF
6455 %<latexrelease> {#1} {disabled} {undeclared} }
6456 %<latexrelease> }
6457 %<latexrelease> }
6458 %<latexrelease> }
6459 %<latexrelease> }
6460 %<latexrelease>\EndIncludeInRelease
6461 % \end{macrocode}
6462 %
6463 % \begin{macro}{\@@_log_next_code:n}
6464 % To display the code for next invocation only (i.e., from
6465 % \cs{AddToHookNext} we have to remove the string
6466 % \cs{@@_clear_next:n}\Arg{hook}, so the simplest is to use a macro
6467 % delimited by a \verb|}|$_12$.
6468 % \changes{v1.1a}{2023/04/06}
6469 % {Changes to add hook arguments (hook-args).}
6470 % \begin{macrocode}
6471 %<latexrelease>\IncludeInRelease{2023/06/01}{\@@_log_next_code:n}
6472 %<latexrelease> {Hooks~with~args}
6473 \exp_last_unbraced:NNNNo
6474 \cs_new:Npn \@@_log_next_code:w #1 \c_right_brace_str { }
6475 %<latexrelease>\EndIncludeInRelease
6476 %<latexrelease>\IncludeInRelease{2020/10/01}{\@@_log_next_code:n}
6477 %<latexrelease> {Hooks~with~args}
6478 %<latexrelease>\cs_gset:Npn \@@_log_next_code:n #1
6479 %<latexrelease> { \exp_args:No \tl_to_str:n { \use_none:nn #1 } }
6480 %<latexrelease>\EndIncludeInRelease
6481 % \end{macrocode}
6482 % \end{macro}
6483 %
6484 % \begin{macro}{\@@_print_args:n}
6485 % Pretty-prints the number of arguments of a hook.
6486 % \begin{macrocode}
6487 \cs_new:Npn \@@_print_args:nn #1 #2
6488 {
6489 \int_compare:nNnT {#2} > { 0 }
6490 {
6491 \@@_if_declared:nT {#1} { \use_none:nnn }
6492 \@@_if_cmd_hook:nT {#1}
6493 { \use_i:nnn { ~ (unknown ~ } }
6494 \use:n { ~ (#2 ~ }
6495 argument \int_compare:nNnT {#2} > { 1 } { s } )
6496 }
6497 }
6498 % \end{macrocode}
6499 % \end{macro}
6500 %
6501 % \end{macro}
6502 % \end{macro}
6503 % \end{macro}
6504 %
6505 % \begin{macro}{\@@_list_rules:nn}
6506 % \begin{macro}{\@@_list_one_rule:nnn,\@@_list_if_rule_exists:nnnF}
6507 % This macro takes a \meta{hook} and an \meta{inline function} and
6508 % loops through each pair of \meta{labels} in the \meta{hook}, and if
6509 % there is a relation between this pair of \meta{labels}, the
6510 % \meta{inline function} is executed with |#1|${}={}$\meta{relation},
6511 % |#2|${}={}$\meta{label_1}\verb=|=\meta{label_2},
6512 % and |#3|${}={}$\meta{hook} (the latter may be the argument |#1| to
6513 % \cs{@@_list_rules:nn}, or \texttt{??} if it is a default rule).
6514 % \changes{v1.1k}{2024/10/29}
6515 % {Skip mapping over undeclared \cs{g__hook_\meta{hook}_code_prop} (gh/1513)}
6516 % \begin{macrocode}
6517 \cs_new_protected:Npn \@@_list_rules:nn #1 #2
6518 {
6519 \prop_if_exist:cT { g_@@_#1_code_prop }
6520 {
6521 \cs_set_protected:Npn \@@_tmp:w ##1 ##2 ##3 {#2}
6522 \prop_map_inline:cn { g_@@_#1_code_prop }
6523 {
6524 \prop_map_inline:cn { g_@@_#1_code_prop }
6525 {
6526 \@@_if_label_case:nnnnn {##1} {####1}
6527 { \prop_map_break: }
6528 { \@@_list_one_rule:nnn {##1} {####1} }
6529 { \@@_list_one_rule:nnn {####1} {##1} }
6530 {#1}
6531 }
6532 }
6533 }
6534 }
6535 % \end{macrocode}
6536 %
6537 % These two are quite similar to \cs{@@_apply_label_pair:nnn} and
6538 % \cs{@@_label_if_exist_apply:nnnF}, respectively, but rather than
6539 % applying the rule, they pass it to the \meta{inline function}.
6540 % \begin{macrocode}
6541 \cs_new_protected:Npn \@@_list_one_rule:nnn #1#2#3
6542 {
6543 \@@_list_if_rule_exists:nnnF {#1} {#2} {#3}
6544 { \@@_list_if_rule_exists:nnnF {#1} {#2} { ?? } { } }
6545 }
6546 \cs_new_protected:Npn \@@_list_if_rule_exists:nnnF #1#2#3
6547 {
6548 \if_cs_exist:w g_@@_ #3 _rule_ #1 | #2 _tl \cs_end:
6549 \exp_args:Nv \@@_tmp:w
6550 { g_@@_ #3 _rule_ #1 | #2 _tl } { #1 | #2 } {#3}
6551 \exp_after:wN \use_none:nn
6552 \fi:
6553 \use:n
6554 }
6555 % \end{macrocode}
6556 % \end{macro}
6557 % \end{macro}
6558 %
6559 % \begin{macro}{\@@_debug_print_rules:n}
6560 % A shorthand for debugging that prints similar to \cs{prop_show:N}.
6561 % \begin{macrocode}
6562 \cs_new_protected:Npn \@@_debug_print_rules:n #1
6563 {
6564 \iow_term:n { The~hook~#1~contains~the~rules: }
6565 \cs_set_protected:Npn \@@_tmp:w ##1
6566 {
6567 \@@_list_rules:nn {#1}
6568 {
6569 \iow_term:e
6570 {
6571 > ##1 {####2} ##1 => ##1 {####1}
6572 \str_if_eq:nnT {####3} {??} { ~(default) }
6573 }
6574 }
6575 }
6576 \exp_args:No \@@_tmp:w { \use:nn { ~ } { ~ } }
6577 }
6578 % \end{macrocode}
6579 % \end{macro}
6580 %
6581 %
6582 %
6583 %
6584 % \subsection{Specifying code for next invocation}
6585 %
6586 % \begin{macro}{\hook_gput_next_code:nn}
6587 % \changes{v1.1a}{2023/04/06}
6588 % {Add \cs{hook_gput_next_code_with_args:nn} (hook-args).}
6589 % \begin{macrocode}
6590 %<latexrelease>\IncludeInRelease{2023/06/01}{\hook_gput_next_code:nn}
6591 %<latexrelease> {Hooks~with~args}
6592 \cs_new_protected:Npn \hook_gput_next_code:nn #1 #2
6593 {
6594 \@@_replacing_args_false:
6595 \@@_normalize_hook_args:Nn \@@_gput_next_code:nn {#1} {#2}
6596 % \end{macrocode}
6597 %
6598 % \changes{v1.1l}{2024/11/26}{Add debugging message (gh/1459)}
6599 % \begin{macrocode}
6600 \@@_debug:n{\iow_term:e{[lthooks]~ Add~ to~
6601 \@@_if_usable:nF {#1} { undeclared~ }
6602 hook~ '#1'~ ( next~ invocation~ only )
6603 \on@line
6604 ^^J [lthooks] \@spaces
6605 <-~ \tl_to_str:n{#2}
6606 }
6607 }
6608 % \end{macrocode}
6609 %
6610 % \begin{macrocode}
6611 \@@_replacing_args_reset:
6612 }
6613 \cs_new_protected:Npn \hook_gput_next_code_with_args:nn #1 #2
6614 {
6615 \@@_replacing_args_true:
6616 \@@_normalize_hook_args:Nn \@@_gput_next_code:nn {#1} {#2}
6617 % \end{macrocode}
6618 %
6619 % \changes{v1.1l}{2024/11/26}{Add debugging message (gh/1459)}
6620 % \begin{macrocode}
6621 \@@_debug:n{\iow_term:e{[lthooks]~ Add~ to~
6622 \@@_if_usable:nF {#1} { undeclared~ }
6623 hook~ '#1'~ ( next~ invocation~ only )
6624 \on@line
6625 ^^J [lthooks] \@spaces
6626 <-~ \tl_to_str:n{#2}
6627 }
6628 }
6629 % \end{macrocode}
6630 %
6631 % \begin{macrocode}
6632 \@@_replacing_args_reset:
6633 }
6634 %<latexrelease>\EndIncludeInRelease
6635 %<latexrelease>\IncludeInRelease{2020/10/01}{\hook_gput_next_code:nn}
6636 %<latexrelease> {Hooks~with~args}
6637 %<latexrelease>\cs_gset_protected:Npn \hook_gput_next_code:nn #1
6638 %<latexrelease> { \@@_normalize_hook_args:Nn
6639 %<latexrelease> \@@_gput_next_code:nn {#1} }
6640 %<latexrelease>\cs_gset_protected:Npn \hook_gput_next_code_with_args:nn #1 #2 { }
6641 %<latexrelease>\EndIncludeInRelease
6642 % \end{macrocode}
6643 % \end{macro}
6644 %
6645 % \begin{macro}{\@@_gput_next_code:nn}
6646 % \begin{macrocode}
6647 \cs_new_protected:Npn \@@_gput_next_code:nn #1 #2
6648 {
6649 \@@_if_disabled:nTF {#1}
6650 { \msg_error:nnn { hooks } { hook-disabled } {#1} }
6651 {
6652 \@@_if_structure_exist:nTF {#1}
6653 { \@@_gput_next_do:nn }
6654 { \@@_try_declaring_generic_next_hook:nn }
6655 {#1} {#2}
6656 }
6657 }
6658 % \end{macrocode}
6659 % \end{macro}
6660 %
6661 % \begin{macro}{\@@_gput_next_do:nn}
6662 % Start by sanity-checking with \cs{@@_chk_args_allowed:nn}.
6663 % Then check if the ``next code'' token list is empty: if so we need
6664 % to add a \cs{tl_gclear:c} to clear it, so the code lasts for one
6665 % usage only. The token list is cleared early so that nested usages
6666 % don't get lost. \cs{tl_gclear:c} is used instead of
6667 % \cs{tl_gclear:N} in case the hook is used in an expansion-only
6668 % context, so the token list doesn't expand before \cs{tl_gclear:N}:
6669 % that would make an infinite loop. Also in case the main code token
6670 % list is empty, the hook code has to be updated to add the next
6671 % execution token list.
6672 % \changes{v1.1a}{2023/04/06}
6673 % {Changes to add hook arguments (hook-args).}
6674 % \changes{v1.1c}{2023/04/19}
6675 % {Initialize hook structure when adding 'next' code (gh/1052).}
6676 % \begin{macrocode}
6677 %<latexrelease>\IncludeInRelease{2023/06/01}{\@@_gput_next_do:nn}
6678 %<latexrelease> {Hooks~with~args}
6679 \cs_new_protected:Npn \@@_gput_next_do:nn #1
6680 {
6681 \@@_init_structure:n {#1}
6682 \@@_chk_args_allowed:nn {#1} { AddToHookNext }
6683 \@@_cs_if_empty:cT { @@~#1 }
6684 { \@@_update_hook_code:n {#1} }
6685 \@@_cs_if_empty:cT { @@_next~#1 }
6686 { \@@_next_gset:nn {#1} { \@@_clear_next:n {#1} } }
6687 \@@_cs_gput_right:nnn { _next } {#1}
6688 }
6689 %<latexrelease>\EndIncludeInRelease
6690 %<latexrelease>\IncludeInRelease{2020/10/01}{\@@_gput_next_do:nn}
6691 %<latexrelease> {Hooks~with~args}
6692 %<latexrelease>\cs_gset_protected:Npn \@@_gput_next_do:nn #1
6693 %<latexrelease> {
6694 %<latexrelease> \exp_args:Nc \@@_gput_next_do:Nnn
6695 %<latexrelease> { @@_next~#1 } {#1}
6696 %<latexrelease> }
6697 %<latexrelease>\cs_gset_protected:Npn \@@_gput_next_do:Nnn #1 #2
6698 %<latexrelease> {
6699 %<latexrelease> \tl_if_empty:cT { @@~#2 }
6700 %<latexrelease> { \@@_update_hook_code:n {#2} }
6701 %<latexrelease> \tl_if_empty:NT #1
6702 %<latexrelease> { \@@_tl_gset:Nn #1 { \@@_clear_next:n {#2} } }
6703 %<latexrelease> \@@_tl_gput_right:Nn #1
6704 %<latexrelease> }
6705 %<latexrelease>\EndIncludeInRelease
6706 % \end{macrocode}
6707 % \end{macro}
6708 %
6709 % \begin{macro}{\hook_gclear_next_code:n}
6710 % Discard anything set up for next invocation of the hook.
6711 % \changes{v1.0o}{2021/07/27}{Macro made public}
6712 % \begin{macrocode}
6713 \cs_new_protected:Npn \hook_gclear_next_code:n #1
6714 { \@@_normalize_hook_args:Nn \@@_clear_next:n {#1} }
6715 % \end{macrocode}
6716 % \end{macro}
6717 %
6718 % \begin{macro}{\@@_clear_next:n}
6719 % \changes{v1.1a}{2023/04/06}
6720 % {Changes to add hook arguments (hook-args).}
6721 % \begin{macrocode}
6722 %<latexrelease>\IncludeInRelease{2023/06/01}{\@@_clear_next:n}
6723 %<latexrelease> {Hooks~with~args}
6724 \cs_new_protected:Npn \@@_clear_next:n #1
6725 { \@@_next_gset:nn {#1} { } }
6726 %<latexrelease>\EndIncludeInRelease
6727 %<latexrelease>\IncludeInRelease{2020/10/01}{\@@_clear_next:n}
6728 %<latexrelease> {Hooks~with~args}
6729 %<latexrelease>\cs_gset_protected:Npn \@@_clear_next:n #1
6730 %<latexrelease> { \cs_gset_eq:cN { @@_next~#1 } \c_empty_tl }
6731 %<latexrelease>\EndIncludeInRelease
6732 % \end{macrocode}
6733 % \end{macro}
6734 %
6735 %
6736 % \subsection{Using the hook}
6737 %
6738 % \begin{macro}{\hook_use:n}
6739 % \begin{macro}[EXP]{\@@_use_initialized:n}
6740 % \begin{macro}{\@@_preamble_hook:n}
6741 % \cs{hook_use:n} as defined here is used in the preamble, where
6742 % hooks aren't initialized by default. \cs{@@_use_initialized:n} is
6743 % also defined, which is the non-\tn{protected} version for use within
6744 % the document. Their definition is identical, except for the
6745 % \cs{@@_preamble_hook:n} (which wouldn't hurt in the expandable
6746 % version, but it would be an unnecessary extra expansion).
6747 %
6748 % \cs{@@_use_initialized:n} holds the expandable definition while in
6749 % the preamble. \cs{@@_preamble_hook:n} initializes the hook in the
6750 % preamble, and is redefined to \cs{use_none:n} at |\begin{document}|.
6751 %
6752 % Both versions do the same thing internally: they check that the hook exists as
6753 % given, and if so they use it as quickly as possible.
6754 %
6755 % At |\begin{document}|, all hooks are initialized, and any change in
6756 % them causes an update, so \cs{hook_use:n} can be made expandable.
6757 % This one is better not protected so that it can expand into nothing
6758 % if containing no code. Also important in case of generic hooks that
6759 % we do not generate a \cs[no-index]{relax} as a side effect of
6760 % checking for a csname. In contrast to the \TeX{} low-level
6761 % \verb=\csname ...\endcsname= construct \cs{tl_if_exist:c} is
6762 % careful to avoid this.
6763 % \begin{macrocode}
6764 %<latexrelease>\IncludeInRelease{2023/06/01}{\hook_use:n}
6765 %<latexrelease> {Hooks~with~args}
6766 \cs_new_protected:Npn \hook_use:n #1
6767 {
6768 \@@_preamble_hook:n {#1}
6769 \@@_use_initialized:n {#1}
6770 }
6771 \cs_new:Npn \@@_use_initialized:n #1
6772 {
6773 \if_cs_exist:w @@~#1 \cs_end:
6774 \cs:w @@~#1 \use_i:nn
6775 \fi:
6776 \use_none:n
6777 \cs_end:
6778 }
6779 \cs_new_protected:Npn \@@_preamble_hook:n #1
6780 {
6781 \if_cs_exist:w @@~#1 \cs_end:
6782 \@@_initialize_hook_code:n {#1}
6783 \fi:
6784 }
6785 %<latexrelease>\EndIncludeInRelease
6786 % \end{macrocode}
6787 %
6788 % \begin{macrocode}
6789 %<latexrelease>\IncludeInRelease{2021/11/15}{\hook_use:n}
6790 %<latexrelease> {Standardize~generic~hook~names}
6791 %<latexrelease>\cs_new_protected:Npn \hook_use:n #1
6792 %<latexrelease> {
6793 %<latexrelease> \tl_if_exist:cT { @@~#1 }
6794 %<latexrelease> {
6795 %<latexrelease> \@@_preamble_hook:n {#1}
6796 %<latexrelease> \cs:w @@~#1 \cs_end:
6797 %<latexrelease> }
6798 %<latexrelease> }
6799 %<latexrelease>\cs_new:Npn \@@_use_initialized:n #1
6800 %<latexrelease> {
6801 %<latexrelease> \if_cs_exist:w @@~#1 \cs_end:
6802 %<latexrelease> \cs:w @@~#1 \exp_after:wN \cs_end:
6803 %<latexrelease> \fi:
6804 %<latexrelease> }
6805 %<latexrelease>\cs_new_protected:Npn \@@_preamble_hook:n #1
6806 %<latexrelease> { \@@_initialize_hook_code:n {#1} }
6807 %<latexrelease>\cs_new:Npn \hook_use:nnw #1 { }
6808 %<latexrelease>\EndIncludeInRelease
6809 % \end{macrocode}
6810 %
6811 % \begin{macrocode}
6812 %<latexrelease>\IncludeInRelease{2020/10/01}{\hook_use:n}
6813 %<latexrelease> {Standardize~generic~hook~names}
6814 %<latexrelease>\cs_new_protected:Npn \hook_use:n #1
6815 %<latexrelease> {
6816 %<latexrelease> \tl_if_exist:cTF { @@~#1 }
6817 %<latexrelease> {
6818 %<latexrelease> \@@_preamble_hook:n {#1}
6819 %<latexrelease> \cs:w @@~#1 \cs_end:
6820 %<latexrelease> }
6821 %<latexrelease> { \@@_use:wn #1 / \s_@@_mark {#1} }
6822 %<latexrelease> }
6823 %<latexrelease>\cs_new:Npn \@@_use_initialized:n #1
6824 %<latexrelease> {
6825 %<latexrelease> \if_cs_exist:w @@~#1 \cs_end:
6826 %<latexrelease> \else:
6827 %<latexrelease> \@@_use_undefined:w
6828 %<latexrelease> \fi:
6829 %<latexrelease> \cs:w @@~#1 \@@_use_end:
6830 %<latexrelease> }
6831 %<latexrelease>\cs_new:Npn \@@_use_undefined:w
6832 %<latexrelease> #1 #2 @@~#3 \@@_use_end:
6833 %<latexrelease> {
6834 %<latexrelease> #1 % fi
6835 %<latexrelease> \@@_use:wn #3 / \s_@@_mark {#3}
6836 %<latexrelease> }
6837 %<latexrelease>\cs_new_protected:Npn \@@_preamble_hook:n #1
6838 %<latexrelease> { \@@_initialize_hook_code:n {#1} }
6839 %<latexrelease>\cs_new_eq:NN \@@_use_end: \cs_end:
6840 %<latexrelease>\cs_new:Npn \hook_use:nnw #1 { }
6841 %<latexrelease>\EndIncludeInRelease
6842 % \end{macrocode}
6843 % \end{macro}
6844 % \end{macro}
6845 % \end{macro}
6846 %
6847 %
6848 % \begin{macro}{\hook_use:nnw}
6849 % \begin{macro}[EXP]{\@@_use_initialized:nnw}
6850 % \changes{v1.1a}{2023/04/06}
6851 % {Add \cs{hook_use:nnw} (hook-args).}
6852 % \begin{macrocode}
6853 %<latexrelease>\IncludeInRelease{2023/06/01}{\hook_use:nnw}
6854 %<latexrelease> {Hooks~with~args}
6855 \cs_new_protected:Npn \hook_use:nnw #1
6856 {
6857 \@@_preamble_hook:n {#1}
6858 \@@_use_initialized:nnw {#1}
6859 }
6860 \cs_new:Npn \@@_use_initialized:nnw #1 #2
6861 {
6862 \cs:w
6863 \if_cs_exist:w @@~#1 \cs_end:
6864 @@~#1
6865 \else:
6866 use_none: \prg_replicate:nn {#2} { n }
6867 \fi:
6868 \cs_end:
6869 }
6870 %<latexrelease>\EndIncludeInRelease
6871 %<latexrelease>\IncludeInRelease{2020/10/01}{\hook_use:nnw}
6872 %<latexrelease> {Hooks~with~args}
6873 %<latexrelease>\cs_gset:Npn \hook_use:nnw #1 #2
6874 %<latexrelease> { \use:c { use_none: \prg_replicate:nn {#2} { n } } }
6875 %<latexrelease>\EndIncludeInRelease
6876 % \end{macrocode}
6877 % \end{macro}
6878 % \end{macro}
6879 %
6880 %
6881 % \begin{macro}{\@@_post_initialization_defs:}
6882 % \changes{v1.1a}{2023/04/06}
6883 % {Macro added (hook-args).}
6884 % \begin{macrocode}
6885 %<latexrelease>\IncludeInRelease{2023/06/01}{\@@_post_initialization_defs:}
6886 %<latexrelease> {Hooks~with~args}
6887 \cs_new_protected:Npn \@@_post_initialization_defs:
6888 {
6889 \cs_gset_eq:NN \hook_use:n \@@_use_initialized:n
6890 \cs_gset_eq:NN \hook_use:nnw \@@_use_initialized:nnw
6891 \cs_gset_eq:NN \@@_preamble_hook:n \use_none:n
6892 \cs_gset_eq:NN \@@_post_initialization_defs: \prg_do_nothing:
6893 }
6894 %<latexrelease>\EndIncludeInRelease
6895 %<latexrelease>\IncludeInRelease{2020/10/01}{\@@_post_initialization_defs:}
6896 %<latexrelease> {Hooks~with~args}
6897 %<latexrelease>\cs_undefine:N \@@_post_initialization_defs:
6898 %<latexrelease>\EndIncludeInRelease
6899 % \end{macrocode}
6900 % \end{macro}
6901 %
6902 %
6903 % \begin{macro}[EXP]{\@@_use:wn}
6904 % \begin{macro}{\@@_try_file_hook:n,\@@_if_usable_use:n}
6905 % \cs{@@_use:wn} does a quick check to test if the current hook is a
6906 % file hook: those need a special treatment. If it is not, the hook
6907 % does not exist. If it is, then \cs{@@_try_file_hook:n} is called,
6908 % and checks that the current hook is a file-specific hook using
6909 % \cs{@@_if_file_hook:wTF}. If it's not, then it's a generic |file/|
6910 % hook and is used if it exist.
6911 %
6912 % If it is a file-specific hook, it passes through the same
6913 % normalization as during declaration, and then it is used if defined.
6914 % \cs{@@_if_usable_use:n} checks if the hook exist, and calls
6915 % \cs{@@_preamble_hook:n} if so, then uses the hook.
6916 % \begin{macrocode}
6917 %<latexrelease>\IncludeInRelease{2021/11/15}{\@@_use:wn}
6918 %<latexrelease> {Standardize~generic~hook~names}
6919 %<latexrelease>\EndIncludeInRelease
6920 %<latexrelease>\IncludeInRelease{2020/10/01}{\@@_use:wn}
6921 %<latexrelease> {Standardize~generic~hook~names}
6922 %<latexrelease>\cs_new:Npn \@@_use:wn #1 / #2 \s_@@_mark #3
6923 %<latexrelease> {
6924 %<latexrelease> \str_if_eq:nnTF {#1} { file }
6925 %<latexrelease> { \@@_try_file_hook:n {#3} }
6926 %<latexrelease> { } % Hook doesn't exist
6927 %<latexrelease> }
6928 % \end{macrocode}
6929 %
6930 % \changes{v1.0s}{2021/09/28}
6931 % {Correct usage of older \cs{__hook_if_file_hook:wTF} (gh/675)}
6932 % \changes{v1.1h}{2024/01/24}
6933 % {Correct usage of older \cs{__hook_if_file_hook:wTF} (gh/1243)}
6934 % \begin{macrocode}
6935 %<latexrelease>\cs_new_protected:Npn \@@_try_file_hook:n #1
6936 %<latexrelease> {
6937 %<latexrelease> \@@_if_file_hook:wTF #1 / / \s_@@_mark
6938 %<latexrelease> {
6939 %<latexrelease> \exp_args:Ne \@@_if_usable_use:n
6940 %<latexrelease> { \exp_args:Ne \@@_file_hook_normalize:n {#1} }
6941 %<latexrelease> }
6942 %<latexrelease> { \@@_if_usable_use:n {#1} }
6943 %<latexrelease> % file/ generic hook (e.g. file/before)
6944 %<latexrelease> }
6945 % \end{macrocode}
6946 %
6947 % \begin{macrocode}
6948 %<latexrelease>\cs_new_protected:Npn \@@_if_usable_use:n #1
6949 %<latexrelease> {
6950 %<latexrelease> \tl_if_exist:cT { @@~#1 }
6951 %<latexrelease> {
6952 %<latexrelease> \@@_preamble_hook:n {#1}
6953 %<latexrelease> \cs:w @@~#1 \cs_end:
6954 %<latexrelease> }
6955 %<latexrelease> }
6956 %<latexrelease>\EndIncludeInRelease
6957 % \end{macrocode}
6958 % \end{macro}
6959 % \end{macro}
6960 %
6961 % \begin{macro}{\hook_use_once:n,\hook_use_once:nnw}
6962 % For hooks that can and should be used only once we have a special
6963 % use command that further inhibits the hook from getting more code
6964 % added to it. This has the effect that any
6965 % further code added to the hook is executed immediately rather
6966 % than stored in the hook.
6967 %
6968 % The code needs some gymnastics to prevent space trimming from the
6969 % hook name, since \cs{hook_use:n} and \cs{hook_use_once:n} are
6970 % documented to not trim spaces.
6971 %
6972 % \changes{v1.0r}{2021/09/06}{Clean up after \cs{UseOneTimeHook} (gh/606)}
6973 % \changes{v1.1a}{2023/04/06}
6974 % {Add \cs{hook_use_once:nnw} (hook-args).}
6975 % \begin{macrocode}
6976 %<latexrelease>\IncludeInRelease{2023/06/01}{\hook_use_once:nnw}
6977 %<latexrelease> {Hooks~with~args}
6978 \cs_new_protected:Npn \hook_use_once:n #1
6979 {
6980 \@@_if_execute_immediately:nF {#1}
6981 { \@@_normalize_hook_args:Nn \@@_use_once:nn
6982 { \use:n {#1} } { 0 } }
6983 }
6984 \cs_new_protected:Npn \hook_use_once:nnw #1 #2
6985 {
6986 \@@_if_execute_immediately:nF {#1}
6987 { \@@_normalize_hook_args:Nn \@@_use_once:nn
6988 { \use:n {#1} } {#2} }
6989 }
6990 %<latexrelease>\EndIncludeInRelease
6991 % \end{macrocode}
6992 % \end{macro}
6993 %
6994 % \begin{macrocode}
6995 %<latexrelease>\IncludeInRelease{2020/10/01}{\hook_use_once:nnw}
6996 %<latexrelease> {Hooks~with~args}
6997 %<latexrelease>\cs_gset_protected:Npn \hook_use_once:n #1
6998 %<latexrelease> {
6999 %<latexrelease> \@@_if_execute_immediately:nF {#1}
7000 %<latexrelease> { \@@_normalize_hook_args:Nn \@@_use_once:n
7001 %<latexrelease> { \use:n {#1} } }
7002 %<latexrelease> }
7003 %<latexrelease>\cs_gset:Npn \hook_use_once:nnw #1 #2
7004 %<latexrelease> { \use:c { use_none: \prg_replicate:nn {#2} { n } } }
7005 %<latexrelease>\EndIncludeInRelease
7006 % \end{macrocode}
7007 %
7008 % \begin{macro}{\@@_use_once:nn}
7009 % \changes{v1.1a}{2023/04/06}
7010 % {Changes to add hook arguments (hook-args).}
7011 % \begin{macrocode}
7012 %<latexrelease>\IncludeInRelease{2023/06/01}{\@@_use_once:nn}
7013 %<latexrelease> {Hooks~with~args}
7014 \cs_new_protected:Npn \@@_use_once:nn #1 #2
7015 {
7016 \@@_preamble_hook:n {#1}
7017 \@@_use_once_set:n {#1}
7018 % \end{macrocode}
7019 % When a hook has arguments, the call to \cs{@@_use_initialized:n},
7020 % should be the very last thing to happen, otherwise the arguments
7021 % grabbed will be wrong. So, to clean up after the hook we need to
7022 % cheat a bit and sneak the cleanup code at the end of the hook,
7023 % along with the next execution code.
7024 % \begin{macrocode}
7025 \@@_replacing_args_false:
7026 \@@_cs_gput_right:nnn { _next } {#1}
7027 { \@@_use_once_clear:n {#1} }
7028 \@@_replacing_args_reset:
7029 \@@_if_usable:nTF {#1}
7030 { \@@_use_initialized:n {#1} }
7031 {
7032 \int_compare:nNnT {#2} > { 0 }
7033 { \use:c { use_none: \prg_replicate:nn {#2} { n } } }
7034 }
7035 }
7036 %<latexrelease>\EndIncludeInRelease
7037 %
7038 %<latexrelease>\IncludeInRelease{2020/10/01}{\@@_use_once:nn}
7039 %<latexrelease> {Hooks~with~args}
7040 %<latexrelease>\cs_gset_protected:Npn \@@_use_once:n #1
7041 %<latexrelease> {
7042 %<latexrelease> \@@_preamble_hook:n {#1}
7043 %<latexrelease> \@@_use_once_set:n {#1}
7044 %<latexrelease> \@@_use_initialized:n {#1}
7045 %<latexrelease> \@@_use_once_clear:n {#1}
7046 %<latexrelease> }
7047 %<latexrelease>\cs_undefine:N \@@_use_once:nn
7048 %<latexrelease>\EndIncludeInRelease
7049 % \end{macrocode}
7050 % \end{macro}
7051 %
7052 % \begin{macro}{\@@_use_once_set:n}
7053 % \begin{macro}{\@@_use_once_clear:n}
7054 % \cs{@@_use_once_set:n} is used before the actual hook code is
7055 % executed so that any usage of \cs{AddToHook} inside the hook causes
7056 % the code to execute immediately. Setting
7057 % \cs[no-index]{g_@@_\meta{hook}_reversed_tl} to |I| prevents further
7058 % code from being added to the hook. \cs{@@_use_once_clear:n} then
7059 % clears the hook so that any further call to \cs{hook_use:n} or
7060 % \cs{hook_use_once:n} will expand to nothing.
7061 % \changes{v1.0r}{2021/09/06}{Clean up after \cs{UseOneTimeHook} (gh/606)}
7062 % \changes{v1.0u}{2022/05/13}{Check if prop exists to avoid l3debug error}
7063 % \changes{v1.1a}{2023/04/06}
7064 % {Changes to add hook arguments (hook-args).}
7065 % \begin{macrocode}
7066 %<latexrelease>\IncludeInRelease{2023/06/01}{\@@_use_once_clear:n}
7067 %<latexrelease> {Hooks~with~args}
7068 \cs_new_protected:Npn \@@_use_once_set:n #1
7069 { \@@_tl_gset:cn { g_@@_#1_reversed_tl } { I } }
7070 \cs_new_protected:Npn \@@_use_once_clear:n #1
7071 {
7072 \@@_code_gset:nn {#1} { }
7073 \@@_next_gset:nn {#1} { }
7074 \@@_toplevel_gset:nn {#1} { }
7075 \prop_gclear_new:c { g_@@_#1_code_prop }
7076 }
7077 %<latexrelease>\EndIncludeInRelease
7078 % \end{macrocode}
7079 %
7080 % \begin{macrocode}
7081 %<latexrelease>\IncludeInRelease{2020/10/01}{\@@_use_once_clear:n}
7082 %<latexrelease> {Hooks~with~args}
7083 %<latexrelease>\cs_new_protected:Npn \@@_use_once_clear:n #1
7084 %<latexrelease> {
7085 %<latexrelease> \@@_tl_gclear:c { @@~#1 }
7086 %<latexrelease> \@@_tl_gclear:c { @@_next~#1 }
7087 %<latexrelease> \@@_tl_gclear:c { @@_toplevel~#1 }
7088 %<latexrelease> \prop_gclear_new:c { g_@@_#1_code_prop }
7089 %<latexrelease> }
7090 %<latexrelease>\EndIncludeInRelease
7091 % \end{macrocode}
7092 % \end{macro}
7093 % \end{macro}
7094 %
7095 % \begin{macro}[pTF]{\@@_if_execute_immediately:n}
7096 % To check whether the code being added should be executed immediately
7097 % (that is, if the hook is a one-time hook), we check if
7098 % \cs[no-index]{g_@@_\meta{hook}_reversed_tl} is |I|. The gymnastics
7099 % around \cs{if:w} is there to allow the |reversed| token list to be
7100 % empty.
7101 % \changes{v1.0r}{2021/09/06}{Macro added (gh/606)}
7102 % \changes{v1.1a}{2023/04/06}
7103 % {Changes to add hook arguments (hook-args).}
7104 % \begin{macrocode}
7105 \prg_new_conditional:Npnn \@@_if_execute_immediately:n #1 { T, F, TF }
7106 {
7107 \exp_after:wN \@@_use_none_delimit_by_s_mark:w
7108 \if:w I
7109 \if_cs_exist:w g_@@_#1_reversed_tl \cs_end:
7110 \cs:w g_@@_#1_reversed_tl \exp_after:wN \cs_end:
7111 \fi:
7112 X
7113 \s_@@_mark \prg_return_true:
7114 \else:
7115 \s_@@_mark \prg_return_false:
7116 \fi:
7117 }
7118 % \end{macrocode}
7119 % \end{macro}
7120 %
7121 % \subsection{Querying a hook}
7122 %
7123 % Simpler data types, like token lists, have three possible states; they
7124 % can exist and be empty, exist and be non-empty, and they may not
7125 % exist, in which case emptiness doesn't apply (though
7126 % \cs{tl_if_empty:N} returns false in this case).
7127 %
7128 % Hooks are a bit more complicated: they have several other states as
7129 % discussed in \ref{sec:existence}.
7130 % A hook may exist or not, and either way it may or may not be empty
7131 % (even a hook that doesn't exist may be non-empty) or may be disabled.
7132 %
7133 % A hook is said to be empty when no code was added to it, either to
7134 % its permanent code pool, or to its ``next'' token list. The hook
7135 % doesn't need to be declared to have code added to its code pool
7136 % (it may happen that a package $A$ defines a hook \hook{foo}, but
7137 % it's loaded after package $B$, which adds some code to that hook.
7138 % In this case it is important that the code added by package $B$ is
7139 % remembered until package $A$ is loaded).
7140 %
7141 % All other states can only be queried with internal tests as the
7142 % different states are irrelevant for package code.
7143 %
7144 % \begin{macro}[pTF]{\hook_if_empty:n}
7145 % Test if a hook is empty (that is, no code was added to that hook).
7146 % A \meta{hook} being empty means that all three of its
7147 % \cs{g_@@_\meta{hook}_code_prop}, its
7148 % \cs{@@_toplevel\textvisiblespace\meta{hook}} and its
7149 % \cs{@@_next\textvisiblespace\meta{hook}} are empty.
7150 % \changes{v1.1a}{2023/04/06}
7151 % {Changes to add hook arguments (hook-args).}
7152 % \changes{v1.1c}{2023/04/19}
7153 % {Simpler and faster version (gh/1052).}
7154 % \begin{macrocode}
7155 %<latexrelease>\IncludeInRelease{2023/06/01}{\hook_if_empty:n}
7156 %<latexrelease> {Hooks~with~args}
7157 \prg_new_conditional:Npnn \hook_if_empty:n #1 { p , T , F , TF }
7158 {
7159 \if:w
7160 T
7161 \prop_if_exist:cT { g_@@_#1_code_prop }
7162 { \prop_if_empty:cF { g_@@_#1_code_prop } { F } }
7163 \@@_cs_if_empty:cF { @@_toplevel~#1 } { F }
7164 \@@_cs_if_empty:cF { @@_next~#1 } { F }
7165 T
7166 \prg_return_true:
7167 \else:
7168 \prg_return_false:
7169 \fi:
7170 }
7171 %<latexrelease>\EndIncludeInRelease
7172 % \end{macrocode}
7173 %
7174 % \begin{macrocode}
7175 %<latexrelease>\IncludeInRelease{2020/10/01}{\hook_if_empty:n}
7176 %<latexrelease> {Hooks~with~args}
7177 %<latexrelease>\prg_new_conditional:Npnn \hook_if_empty:n #1 { p , T , F , TF }
7178 %<latexrelease> {
7179 %<latexrelease> \@@_if_structure_exist:nTF {#1}
7180 %<latexrelease> {
7181 %<latexrelease> \bool_lazy_and:nnTF
7182 %<latexrelease> { \prop_if_empty_p:c { g_@@_#1_code_prop } }
7183 %<latexrelease> {
7184 %<latexrelease> \bool_lazy_and_p:nn
7185 %<latexrelease> { \tl_if_empty_p:c { @@_toplevel~#1 } }
7186 %<latexrelease> { \tl_if_empty_p:c { @@_next~#1 } }
7187 %<latexrelease> }
7188 %<latexrelease> { \prg_return_true: }
7189 %<latexrelease> { \prg_return_false: }
7190 %<latexrelease> }
7191 %<latexrelease> { \prg_return_true: }
7192 %<latexrelease> }
7193 %<latexrelease>\EndIncludeInRelease
7194 % \end{macrocode}
7195 % \end{macro}
7196 %
7197 %
7198 % \begin{macro}[pTF]{\@@_if_usable:n}
7199 % A hook is usable if the
7200 % token list that stores the sorted code for that hook,
7201 % \cs[no-index]{@@\textvisiblespace\meta{hook}}, exists. The property
7202 % list \cs[no-index]{g_@@_\meta{hook}_code_prop} cannot be used here
7203 % because often it is necessary to add code to a hook without knowing
7204 % if such hook was already declared, or even if it will ever be
7205 % (for example, in case the package that defines it isn't loaded).
7206 % \begin{macrocode}
7207 \prg_new_conditional:Npnn \@@_if_usable:n #1 { p , T , F , TF }
7208 {
7209 \cs_if_exist:cTF { @@~#1 }
7210 { \prg_return_true: }
7211 { \prg_return_false: }
7212 }
7213 % \end{macrocode}
7214 % \end{macro}
7215 %
7216 % \begin{macro}[pTF]{\@@_if_structure_exist:n}
7217 %
7218 % An internal check if the hook has already its basic internal
7219 % structure set up with
7220 % \cs{@@_init_structure:n}. This means that the hook was already used somehow
7221 % (a code chunk or rule was added to it), but it still wasn't declared
7222 % with \cs{hook_new:n}.
7223 % \begin{macrocode}
7224 \prg_new_conditional:Npnn \@@_if_structure_exist:n #1 { p , T , F , TF }
7225 {
7226 \prop_if_exist:cTF { g_@@_#1_code_prop }
7227 { \prg_return_true: }
7228 { \prg_return_false: }
7229 }
7230 % \end{macrocode}
7231 % \end{macro}
7232 %
7233 %
7234 % \begin{macro}[pTF]{\@@_if_declared:n}
7235 %
7236 % Internal test to check if the hook was officially declared with
7237 % \cs{hook_new:n} or a variant.
7238 % \begin{macrocode}
7239 \prg_new_conditional:Npnn \@@_if_declared:n #1 { p, T, F, TF }
7240 {
7241 \tl_if_exist:cTF { g_@@_#1_declared_tl }
7242 { \prg_return_true: }
7243 { \prg_return_false: }
7244 }
7245 % \end{macrocode}
7246 % \end{macro}
7247 %
7248 % \begin{macro}[pTF]{\@@_if_reversed:n}
7249 % An internal conditional that checks if a hook is reversed.
7250 % \begin{macrocode}
7251 \prg_new_conditional:Npnn \@@_if_reversed:n #1 { p , T , F , TF }
7252 {
7253 \exp_after:wN \@@_use_none_delimit_by_s_mark:w
7254 \if:w - \cs:w g_@@_#1_reversed_tl \cs_end:
7255 \s_@@_mark \prg_return_true:
7256 \else:
7257 \s_@@_mark \prg_return_false:
7258 \fi:
7259 }
7260 % \end{macrocode}
7261 % \end{macro}
7262 %
7263 % \begin{macro}[pTF]{\@@_if_generic:n}
7264 % \begin{macro}[pTF]{\@@_if_deprecated_generic:n}
7265 % An internal conditional that checks if a name belongs to a generic
7266 % hook. The deprecated version needs to check if |#3| is empty to
7267 % avoid returning true on \hook{file/before}, for example.
7268 % \begin{macrocode}
7269 \prg_new_conditional:Npnn \@@_if_generic:n #1 { T, TF }
7270 { \@@_if_generic:w #1 / / / \s_@@_mark }
7271 \cs_new:Npn \@@_if_generic:w #1 / #2 / #3 / #4 \s_@@_mark
7272 {
7273 \cs_if_exist:cTF { c_@@_generic_#1/./#3_tl }
7274 { \prg_return_true: }
7275 { \prg_return_false: }
7276 }
7277 \prg_new_conditional:Npnn \@@_if_deprecated_generic:n #1 { T, TF }
7278 { \@@_if_deprecated_generic:w #1 / / / \s_@@_mark }
7279 \cs_new:Npn \@@_if_deprecated_generic:w #1 / #2 / #3 / #4 \s_@@_mark
7280 {
7281 \cs_if_exist:cTF { c_@@_deprecated_#1/./#2_tl }
7282 {
7283 \tl_if_empty:nTF {#3}
7284 { \prg_return_false: }
7285 { \prg_return_true: }
7286 }
7287 { \prg_return_false: }
7288 }
7289 % \end{macrocode}
7290 % \end{macro}
7291 % \end{macro}
7292 %
7293 % \begin{macro}[pTF]{\@@_if_cmd_hook:n}
7294 % \begin{macro}[pTF]{\@@_if_cmd_hook:w}
7295 % An internal conditional that checks if a given hook is a valid
7296 % generic \hook{cmd} hook.
7297 % \changes{v1.1d}{2023/05/21}
7298 % {Changes to allow support arguments in cmd hooks (cmd-args).}
7299 % \begin{macrocode}
7300 %<latexrelease>\IncludeInRelease{2023/06/01}{\@@_if_cmd_hook:n}
7301 %<latexrelease> {Hooks~with~args}
7302 \prg_new_conditional:Npnn \@@_if_cmd_hook:n #1 { T }
7303 { \@@_if_cmd_hook:w #1 / / / \s_@@_mark }
7304 \cs_new:Npn \@@_if_cmd_hook:w #1 / #2 / #3 / #4 \s_@@_mark
7305 {
7306 \if:w Y
7307 \str_if_eq:nnF {#1} { cmd } { N }
7308 \tl_if_exist:cF { c_@@_generic_#1/./#3_tl } { N }
7309 Y
7310 \prg_return_true:
7311 \else:
7312 \prg_return_false:
7313 \fi:
7314 }
7315 %<latexrelease>\EndIncludeInRelease
7316 %<latexrelease>\IncludeInRelease{2020/10/01}{\@@_if_cmd_hook:n}
7317 %<latexrelease> {Hooks~with~args}
7318 %<latexrelease>\cs_undefine:N \@@_if_cmd_hook:nT
7319 %<latexrelease>\EndIncludeInRelease
7320 % \end{macrocode}
7321 % \end{macro}
7322 % \end{macro}
7323 %
7324 % \begin{macro}[pTF]{\@@_if_generic_reversed:n}
7325 % An internal conditional that checks if a name belongs to a generic
7326 % reversed hook.
7327 % \begin{macrocode}
7328 \prg_new_conditional:Npnn \@@_if_generic_reversed:n #1 { T }
7329 { \@@_if_generic_reversed:w #1 / / / \scan_stop: }
7330 \cs_new:Npn \@@_if_generic_reversed:w #1 / #2 / #3 / #4 \scan_stop:
7331 {
7332 \if_charcode:w - \cs:w c_@@_generic_#1/./#3_tl \cs_end:
7333 \prg_return_true:
7334 \else:
7335 \prg_return_false:
7336 \fi:
7337 }
7338 % \end{macrocode}
7339 % \end{macro}
7340 %
7341 % \begin{macro}[EXP]{\@@_if_replacing_args:TF}
7342 % \begin{macro}[EXP]{\@@_misused_if_replacing_args:nn}
7343 % \begin{macro}{\@@_replacing_args_true:}
7344 % \begin{macro}{\@@_replacing_args_false:}
7345 % \begin{macro}{\@@_replacing_args_reset:}
7346 % \begin{macro}{\g_@@_replacing_stack_seq}
7347 % An internal conditional that checks if the code being added to the
7348 % hook contains arguments.
7349 % \changes{v1.1a}{2023/04/06}
7350 % {Macro added (hook-args).}
7351 % \begin{macrocode}
7352 \seq_new:N \g_@@_replacing_stack_seq
7353 \cs_new:Npn \@@_misused_if_replacing_args:nn #1 #2
7354 {
7355 \msg_expandable_error:nnn { latex2e } { should-not-happen }
7356 { Misused~\@@_if_replacing_args:. }
7357 }
7358 \cs_new:Npn \@@_if_replacing_args:TF
7359 { \@@_misused_if_replacing_args:nn }
7360 \cs_new_protected:Npn \@@_replacing_args_true:
7361 {
7362 \seq_gpush:No \g_@@_replacing_stack_seq
7363 { \@@_if_replacing_args:TF }
7364 \cs_set:Npn \@@_if_replacing_args:TF { \use_i:nn }
7365 }
7366 \cs_new_protected:Npn \@@_replacing_args_false:
7367 {
7368 \seq_gpush:No \g_@@_replacing_stack_seq
7369 { \@@_if_replacing_args:TF }
7370 \cs_set:Npn \@@_if_replacing_args:TF { \use_ii:nn }
7371 }
7372 \cs_new_protected:Npn \@@_replacing_args_reset:
7373 {
7374 \seq_gpop:NN \g_@@_replacing_stack_seq \l_@@_return_tl
7375 \cs_gset_eq:NN \@@_if_replacing_args:TF \l_@@_return_tl
7376 }
7377 % \end{macrocode}
7378 % \end{macro}
7379 % \end{macro}
7380 % \end{macro}
7381 % \end{macro}
7382 % \end{macro}
7383 % \end{macro}
7384 %
7385 % \subsection{Messages}
7386 %
7387 % Hook errors are LaTeX kernel errors:
7388 % \begin{macrocode}
7389 \prop_gput:Nnn \g_msg_module_type_prop { hooks } { LaTeX }
7390 % \end{macrocode}
7391 % \changes{v1.0q}{2021/08/27}{Internal message name changes}
7392 % And so are kernel errors (this should move elsewhere eventually).
7393 % \begin{macrocode}
7394 \prop_gput:Nnn \g_msg_module_type_prop { latex2e } { LaTeX }
7395 \prop_gput:Nnn \g_msg_module_name_prop { latex2e } { kernel }
7396 % \end{macrocode}
7397 %
7398 % \begin{macrocode}
7399 \msg_new:nnnn { hooks } { labels-incompatible }
7400 {
7401 Labels~'#1'~and~'#2'~are~incompatible
7402 \str_if_eq:nnF {#3} {??} { ~in~hook~'#3' } .~
7403 \int_compare:nNnTF {#4} = { 1 }
7404 { The~ code~ for~ both~ labels~ will~ be~ dropped. }
7405 { You~ may~ see~ errors~ later. }
7406 }
7407 { LaTeX~found~two~incompatible~labels~in~the~same~hook.~
7408 This~indicates~an~incompatibility~between~packages. }
7409 % \end{macrocode}
7410 %
7411 % \begin{macrocode}
7412 \msg_new:nnnn { hooks } { exists }
7413 { Hook~'#1'~ has~ already~ been~ declared. }
7414 { There~ already~ exists~ a~ hook~ declaration~ with~ this~
7415 name.\\
7416 Please~ use~ a~ different~ name~ for~ your~ hook.}
7417 % \end{macrocode}
7418 %
7419 % \begin{macrocode}
7420 %<latexrelease>\IncludeInRelease{2023/06/01}{too-many-args}
7421 %<latexrelease> {Hooks~with~args}
7422 % \end{macrocode}
7423 %
7424 % \changes{v1.1a}{2023/04/06}
7425 % {Messages 'too-many-args', 'without-args' and 'one-time-args' added (hook-args).}
7426 % \begin{macrocode}
7427 \msg_new:nnnn { hooks } { too-many-args }
7428 { Too~many~arguments~for~hook~'#1'. }
7429 {
7430 You~tried~to~declare~a~hook~with~#2~arguments,~but~a~
7431 hook~can~only~have~up~to~nine.~LaTeX~will~define~this~
7432 hook~with~nine~arguments.
7433 }
7434 % \end{macrocode}
7435 %
7436 % \begin{macrocode}
7437 \msg_new:nnnn { hooks } { without-args }
7438 { Hook~'#1'~has~no~arguments. }
7439 {
7440 You~tried~to~use~\iow_char:N\\#2WithArguments~
7441 on~a~hook~that~takes~no~arguments.\\
7442 Check~the~usage~of~the~hook~or~use~\iow_char:N\\#2~instead.\\
7443 \\
7444 LaTeX~will~use~\iow_char:N\\#2.
7445 }
7446 % \end{macrocode}
7447 %
7448 % \begin{macrocode}
7449 \msg_new:nnnn { hooks } { one-time-args }
7450 { You~can't~have~arguments~in~used~one-time~hook~'#1'. }
7451 {
7452 You~tried~to~use~\iow_char:N\\#2WithArguments~
7453 on~a~one-time~hook~that~has~already~been~used.~
7454 You~have~to~add~the~code~before~the~hook~is~used,~
7455 or~add~the~code~without~arguments~using~\iow_char:N\\#2~instead.\\
7456 \\
7457 LaTeX~will~use~\iow_char:N\\#2.
7458 }
7459 % \end{macrocode}
7460 %
7461 % \begin{macrocode}
7462 %<latexrelease>\EndIncludeInRelease
7463 %<latexrelease>\IncludeInRelease{2020/10/01}{too-many-args}
7464 %<latexrelease> {Hooks~with~args}
7465 %<latexrelease>\EndIncludeInRelease
7466 % \end{macrocode}
7467 %
7468 %
7469 % \begin{macrocode}
7470 \msg_new:nnnn { hooks } { hook-disabled }
7471 { Cannot~add~code~to~disabled~hook~'#1'. }
7472 {
7473 The~hook~'#1'~you~tried~to~add~code~to~was~previously~disabled~
7474 with~\iow_char:N\\hook_disable_generic:n~or~
7475 \iow_char:N\\DisableGenericHook,~so~
7476 it~cannot~have~code~added~to~it.
7477 }
7478 % \end{macrocode}
7479 %
7480 % \begin{macrocode}
7481 \msg_new:nnn { hooks } { empty-label }
7482 {
7483 Empty~code~label~\msg_line_context:.~
7484 Using~'\@@_currname_or_default:'~instead.
7485 }
7486 % \end{macrocode}
7487 %
7488 % \changes{v1.1i}{2024/08/09}{New message ``empty-hook'' (gh/1423).}
7489 % \begin{macrocode}
7490 \msg_new:nnn { hooks } { empty-hook }
7491 {
7492 Empty~hook~name~\msg_line_context:.
7493 }
7494 % \end{macrocode}
7495 %
7496 % \begin{macrocode}
7497 \msg_new:nnn { hooks } { no-default-label }
7498 {
7499 Missing~(empty)~default~label~\msg_line_context:. \\
7500 This~command~was~ignored.
7501 }
7502 % \end{macrocode}
7503 %
7504 % \begin{macrocode}
7505 \msg_new:nnnn { hooks } { unknown-rule }
7506 {
7507 Unknown~ relationship~ '#3'~
7508 between~ labels~ '#2'~ and~ '#4'~
7509 \str_if_eq:nnF {#1} {??} { ~in~hook~'#1' }. ~
7510 Perhaps~ a~ misspelling?
7511 }
7512 {
7513 The~ relation~ used~ not~ known~ to~ the~ system.~ Allowed~ values~ are~
7514 'before'~ or~ '<',~
7515 'after'~ or~ '>',~
7516 'incompatible-warning',~
7517 'incompatible-error',~
7518 'voids'~ or~
7519 'unrelated'.
7520 }
7521 % \end{macrocode}
7522 %
7523 % \begin{macrocode}
7524 \msg_new:nnnn { hooks } { rule-too-late }
7525 {
7526 Sorting~rule~for~'#1'~hook~applied~too~late.\\
7527 Try~setting~this~rule~earlier.
7528 }
7529 {
7530 You~tried~to~set~the~ordering~of~hook~'#1'~using\\
7531 \ \ \iow_char:N\\DeclareHookRule{#1}{#2}{#3}{#4}\\
7532 but~hook~'#1'~was~already~used~as~a~one-time~hook,~
7533 thus~sorting~is\\
7534 no~longer~possible.~Declare~the~rule~
7535 before~the~hook~is~used.
7536 }
7537 % \end{macrocode}
7538 %
7539 % \begin{macrocode}
7540 \msg_new:nnnn { hooks } { misused-top-level }
7541 {
7542 Illegal~use~of~\iow_char:N \\AddToHook{#1}[top-level]{...}.\\
7543 'top-level'~is~reserved~for~the~user's~document.
7544 }
7545 {
7546 The~'top-level'~label~is~meant~for~user~code~only,~and~should~only~
7547 be~used~(sparingly)~in~the~main~document.~Use~the~default~label~
7548 '\@@_currname_or_default:'~for~this~\@cls@pkg,~or~another~
7549 suitable~label.
7550 }
7551 % \end{macrocode}
7552 %
7553 % \begin{macrocode}
7554 \msg_new:nnn { hooks } { set-top-level }
7555 {
7556 You~cannot~change~the~default~label~#1~'top-level'.~Illegal \\
7557 \use:nn { ~ } { ~ } \iow_char:N \\#2{#3} \\
7558 \msg_line_context:.
7559 }
7560 % \end{macrocode}
7561 %
7562 % \begin{macrocode}
7563 \msg_new:nnn { hooks } { extra-pop-label }
7564 {
7565 Extra~\iow_char:N \\PopDefaultHookLabel. \\
7566 This~command~will~be~ignored.
7567 }
7568 \msg_new:nnn { hooks } { missing-pop-label }
7569 {
7570 Missing~\iow_char:N \\PopDefaultHookLabel. \\
7571 The~label~'#1'~was~pushed~but~never~popped.~Something~is~wrong.
7572 }
7573 % \end{macrocode}
7574 %
7575 % \begin{macrocode}
7576 \msg_new:nnn { latex2e } { should-not-happen }
7577 {
7578 This~should~not~happen.~#1 \\
7579 Please~report~at~https://github.com/latex3/latex2e.
7580 }
7581 % \end{macrocode}
7582 %
7583 %
7584 %
7585 % \begin{macrocode}
7586 \msg_new:nnn { hooks } { activate-disabled }
7587 {
7588 Cannot~ activate~ hook~ '#1'~ because~ it~ is~ disabled!
7589 }
7590 % \end{macrocode}
7591 %
7592 % \begin{macrocode}
7593 \msg_new:nnn { hooks } { cannot-remove }
7594 {
7595 Cannot~remove~chunk~'#2'~from~hook~'#1'~because~
7596 \@@_if_structure_exist:nTF {#1}
7597 { it~does~not~exist~in~that~hook. }
7598 { the~hook~does~not~exist. }
7599 }
7600 % \end{macrocode}
7601 %
7602 % \begin{macrocode}
7603 \msg_new:nnn { hooks } { generic-deprecated }
7604 {
7605 Generic~hook~'#1/#2/#3'~is~deprecated. \\
7606 Use~hook~'#1/#3/#2'~instead.
7607 }
7608 % \end{macrocode}
7609 %
7610 %
7611 % \subsection{\LaTeXe{} package interface commands}
7612 %
7613 %
7614 %
7615 % \begin{macro}{\NewHook,\NewReversedHook,\NewMirroredHookPair}
7616 % Declaring new hooks \ldots
7617 % \begin{macrocode}
7618 \NewDocumentCommand \NewHook { m }
7619 { \hook_new:n {#1} }
7620 \NewDocumentCommand \NewReversedHook { m }
7621 { \hook_new_reversed:n {#1} }
7622 \NewDocumentCommand \NewMirroredHookPair { mm }
7623 { \hook_new_pair:nn {#1}{#2} }
7624 % \end{macrocode}
7625 % \end{macro}
7626 %
7627 %
7628 %
7629 % \begin{macro}{
7630 % \NewHookWithArguments,
7631 % \NewReversedHookWithArguments,
7632 % \NewMirroredHookPairWithArguments
7633 % }
7634 % Declaring new hooks with arguments\ldots
7635 % \changes{v1.1a}{2023/04/06}
7636 % {Add \cs{NewHookWithArguments} (hook-args).}
7637 % \begin{macrocode}
7638 %<latexrelease>\IncludeInRelease{2023/06/01}{\NewHookWithArguments}
7639 %<latexrelease> {Hooks~with~args}
7640 \NewDocumentCommand \NewHookWithArguments { mm }
7641 { \hook_new_with_args:nn {#1} {#2} }
7642 \NewDocumentCommand \NewReversedHookWithArguments { mm }
7643 { \hook_new_reversed_with_args:nn {#1} {#2} }
7644 \NewDocumentCommand \NewMirroredHookPairWithArguments { mmm }
7645 { \hook_new_pair_with_args:nnn {#1} {#2} {#3} }
7646 %<latexrelease>\EndIncludeInRelease
7647 %<latexrelease>\IncludeInRelease{2020/10/01}{\NewHookWithArguments}
7648 %<latexrelease> {Hooks~with~args}
7649 %<latexrelease>\cs_new_protected:Npn \NewHookWithArguments #1 #2 { }
7650 %<latexrelease>\cs_new_protected:Npn \NewReversedHookWithArguments #1 #2 { }
7651 %<latexrelease>\cs_new_protected:Npn \NewMirroredHookPairWithArguments #1 #2 #3{}
7652 %<latexrelease>\EndIncludeInRelease
7653 % \end{macrocode}
7654 % \end{macro}
7655 %
7656 %
7657 %
7658 % \begin{macrocode}
7659 %<latexrelease>\IncludeInRelease{2021/06/01}{\ActivateGenericHook}
7660 %<latexrelease> {Providing~hooks}
7661 % \end{macrocode}
7662 %
7663 % \begin{macro}{\ActivateGenericHook}
7664 % Providing new hooks \ldots
7665 % \changes{v1.0m}{2021/04/29}{Add \cs{ProvideHook} etc.}
7666 % \changes{v1.0o}{2021/08/02}{Change name}
7667 % \begin{macrocode}
7668 \NewDocumentCommand \ActivateGenericHook { m }
7669 { \hook_activate_generic:n {#1} }
7670 % \end{macrocode}
7671 % \end{macro}
7672 %
7673 % \begin{macro}{\DisableGenericHook}
7674 % Disabling a generic hook.
7675 % \changes{v1.0o}{2021/08/02}{Change name}
7676 % \begin{macrocode}
7677 \NewDocumentCommand \DisableGenericHook { m }
7678 { \hook_disable_generic:n {#1} }
7679 % \end{macrocode}
7680 % \end{macro}
7681 %
7682 % \begin{macrocode}
7683 %<latexrelease>\EndIncludeInRelease
7684 % \end{macrocode}
7685 %
7686 % \begin{macrocode}
7687 %<latexrelease>\IncludeInRelease{2020/10/01}{\ActivateGenericHook}
7688 %<latexrelease> {Providing~hooks}
7689 %<latexrelease>\def \ActivateGenericHook #1 { }
7690 %<latexrelease>\def \DisableGenericHook #1 { }
7691 %<latexrelease>\EndIncludeInRelease
7692 % \end{macrocode}
7693 %
7694 %
7695 % \begin{macro}{\AddToHook,\AddToHookWithArguments}
7696 % \changes{v1.1a}{2023/04/06}
7697 % {Add \cs{AddToHookWithArguments} (hook-args).}
7698 % \begin{macrocode}
7699 %<latexrelease>\IncludeInRelease{2023/06/01}{\AddToHookWithArguments}
7700 %<latexrelease> {Hooks~with~args}
7701 \NewDocumentCommand \AddToHook { m o +m }
7702 { \hook_gput_code:nnn {#1} {#2} {#3} }
7703 \NewDocumentCommand \AddToHookWithArguments { m o +m }
7704 { \hook_gput_code_with_args:nnn {#1} {#2} {#3} }
7705 %<latexrelease>\EndIncludeInRelease
7706 %<latexrelease>\IncludeInRelease{2020/10/01}{\AddToHookWithArguments}
7707 %<latexrelease> {Hooks~with~args}
7708 %<latexrelease>\cs_new_protected:Npn \AddToHookWithArguments #1 #2 #3 { }
7709 %<latexrelease>\EndIncludeInRelease
7710 % \end{macrocode}
7711 % \end{macro}
7712 %
7713 % \begin{macro}{\AddToHookNext,\AddToHookNextWithArguments}
7714 % \changes{v1.1a}{2023/04/06}
7715 % {Add \cs{AddToHookNextWithArguments} (hook-args).}
7716 % \begin{macrocode}
7717 %<latexrelease>\IncludeInRelease{2023/06/01}{\AddToHookNextWithArguments}
7718 %<latexrelease> {Hooks~with~args}
7719 \NewDocumentCommand \AddToHookNext { m +m }
7720 { \hook_gput_next_code:nn {#1} {#2} }
7721 \NewDocumentCommand \AddToHookNextWithArguments { m +m }
7722 { \hook_gput_next_code_with_args:nn {#1} {#2} }
7723 %<latexrelease>\EndIncludeInRelease
7724 %<latexrelease>\IncludeInRelease{2020/10/01}{\AddToHookNextWithArguments}
7725 %<latexrelease> {Hooks~with~args}
7726 %<latexrelease>\cs_new_protected:Npn \AddToHookNextWithArguments #1 #2 { }
7727 %<latexrelease>\EndIncludeInRelease
7728 % \end{macrocode}
7729 % \end{macro}
7730 %
7731 % \begin{macro}{\ClearHookNext}
7732 % \changes{v1.0o}{2021/07/27}{Macro added}
7733 % \begin{macrocode}
7734 \NewDocumentCommand \ClearHookNext { m }
7735 { \hook_gclear_next_code:n {#1} }
7736 % \end{macrocode}
7737 % \end{macro}
7738 %
7739 %
7740 % \begin{macro}{\RemoveFromHook}
7741 % \begin{macrocode}
7742 \NewDocumentCommand \RemoveFromHook { m o }
7743 { \hook_gremove_code:nn {#1} {#2} }
7744 % \end{macrocode}
7745 % \end{macro}
7746 %
7747 % \begin{macro}{\SetDefaultHookLabel}
7748 % \begin{macro}{\PushDefaultHookLabel}
7749 % \begin{macro}{\PopDefaultHookLabel}
7750 % Now define a wrapper that replaces the top of the stack with the
7751 % argument, and updates \cs{g_@@_hook_curr_name_tl} accordingly.
7752 % \begin{macrocode}
7753 \NewDocumentCommand \SetDefaultHookLabel { m }
7754 { \@@_set_default_hook_label:n {#1} }
7755 % \end{macrocode}
7756 %
7757 % The label is only automatically updated with \cs{@onefilewithoptions}
7758 % (\cs{usepackage} and \cs{documentclass}), but some packages, like
7759 % Ti\emph{k}Z, define package-like interfaces, like
7760 % \cs{usetikzlibrary} that are wrappers around \cs{input}, so they
7761 % inherit the default label currently in force (usually |top-level|,
7762 % but it may change if loaded in another package). To provide a
7763 % package-like behavior also for hooks in these files, we provide
7764 % high-level access to the default label stack.
7765 % \begin{macrocode}
7766 \NewDocumentCommand \PushDefaultHookLabel { m }
7767 { \@@_curr_name_push:n {#1} }
7768 \NewDocumentCommand \PopDefaultHookLabel { }
7769 { \@@_curr_name_pop: }
7770 % \end{macrocode}
7771 %
7772 % The current label stack holds the labels for all files but the
7773 % current one (more or less like \cs{@currnamestack}), and the current
7774 % label token list, \cs{g_@@_hook_curr_name_tl}, holds the label for
7775 % the current file. However \cs{@pushfilename} happens before
7776 % \cs{@currname} is set, so we need to look ahead to get the
7777 % \cs{@currname} for the label. \pkg{expl3} also requires the current
7778 % file in \cs{@pushfilename}, so here we abuse
7779 % \cs{@expl@push@filename@aux@@@@} to do \cs{@@_curr_name_push:n}.
7780 % \begin{macrocode}
7781 \cs_gset_protected:Npn \@expl@push@filename@aux@@@@ #1#2#3
7782 {
7783 \@@_curr_name_push:n {#3}
7784 \str_gset:Ne \g_file_curr_name_str {#3}
7785 #1 #2 {#3}
7786 }
7787 % \end{macrocode}
7788 % \end{macro}
7789 % \end{macro}
7790 % \end{macro}
7791 %
7792 %
7793 %
7794 %
7795 % \begin{macro}{
7796 % \UseHook,
7797 % \UseOneTimeHook,
7798 % \UseHookWithArguments,
7799 % \UseOneTimeHookWithArguments,
7800 % }
7801 % Avoid the overhead of \pkg{xparse} and its protection that we
7802 % don't want here (since the hook should vanish without trace if empty)!
7803 % \changes{v1.1a}{2023/04/06}
7804 % {Add \cs{UseHookWithArguments} (hook-args).}
7805 % \begin{macrocode}
7806 %<latexrelease>\IncludeInRelease{2023/06/01}{\UseHookWithArguments}
7807 %<latexrelease> {Hooks~with~args}
7808 \cs_new:Npn \UseHook { \hook_use:n }
7809 \cs_new:Npn \UseOneTimeHook { \hook_use_once:n }
7810 \cs_new:Npn \UseHookWithArguments { \hook_use:nnw }
7811 \cs_new:Npn \UseOneTimeHookWithArguments { \hook_use_once:nnw }
7812 %<latexrelease>\EndIncludeInRelease
7813 %<latexrelease>\IncludeInRelease{2020/10/01}{\UseHookWithArguments}
7814 %<latexrelease> {Hooks~with~args}
7815 %<latexrelease>\cs_new:Npn \UseHookWithArguments #1 #2 { }
7816 %<latexrelease>\cs_new:Npn \UseOneTimeHookWithArguments #1 #2 { }
7817 %<latexrelease>\EndIncludeInRelease
7818 % \end{macrocode}
7819 % \end{macro}
7820 %
7821 %
7822 %
7823 % \begin{macro}{\ShowHook,\LogHook}
7824 % \begin{macrocode}
7825 \cs_new_protected:Npn \ShowHook { \hook_show:n }
7826 \cs_new_protected:Npn \LogHook { \hook_log:n }
7827 % \end{macrocode}
7828 % \end{macro}
7829 %
7830 % \begin{macro}{\DebugHooksOn,\DebugHooksOff}
7831 %
7832 % \begin{macrocode}
7833 \cs_new_protected:Npn \DebugHooksOn { \hook_debug_on: }
7834 \cs_new_protected:Npn \DebugHooksOff { \hook_debug_off: }
7835 % \end{macrocode}
7836 % \end{macro}
7837 %
7838 %
7839 %
7840 % \begin{macro}{\DeclareHookRule}
7841 %
7842 % \begin{macrocode}
7843 \NewDocumentCommand \DeclareHookRule { m m m m }
7844 { \hook_gset_rule:nnnn {#1}{#2}{#3}{#4} }
7845 % \end{macrocode}
7846 % \end{macro}
7847 %
7848 % \begin{macro}{\DeclareDefaultHookRule}
7849 % This declaration is only supported before \verb=\begin{document}=.
7850 % \begin{macrocode}
7851 \NewDocumentCommand \DeclareDefaultHookRule { m m m }
7852 { \hook_gset_rule:nnnn {??}{#1}{#2}{#3} }
7853 \@onlypreamble\DeclareDefaultHookRule
7854 % \end{macrocode}
7855 % \end{macro}
7856 %
7857 % \begin{macro}{\ClearHookRule}
7858 % A special setup rule that removes an existing relation.
7859 % Basically \cs{@@_rule_gclear:nnn} plus fixing the property list for debugging.
7860 % \fmiinline{Needs perhaps an L3 interface, or maybe it should get dropped?}
7861 % \begin{macrocode}
7862 \NewDocumentCommand \ClearHookRule { m m m }
7863 { \hook_gset_rule:nnnn {#1}{#2}{unrelated}{#3} }
7864 % \end{macrocode}
7865 % \end{macro}
7866 %
7867 %
7868 % \begin{macro}[EXP]{\IfHookEmptyTF,\IfHookEmptyT,\IfHookEmptyF}
7869 % \changes{v1.1j}{2024/10/21}{Define \cs{IfHookEmptyT}, \cs{IfHookEmptyF}}
7870 % Here we avoid the overhead of \pkg{xparse}, since \cs{IfHookEmptyTF}
7871 % is used in \cs{end} (that is, every \LaTeX{} environment). As a
7872 % further optimization, use \cs{let} rather than \cs{def} to avoid one
7873 % expansion step.
7874 % \begin{macrocode}
7875 \cs_new_eq:NN \IfHookEmptyTF \hook_if_empty:nTF
7876 \cs_new_eq:NN \IfHookEmptyT \hook_if_empty:nT
7877 \cs_new_eq:NN \IfHookEmptyF \hook_if_empty:nF
7878 % \end{macrocode}
7879 % \end{macro}
7880 %
7881 % \begin{macro}[EXP,int]{\IfHookExistsTF}
7882 % Marked for removal and no longer documented in the doc section!
7883 % \phoinline{\cs{IfHookExistsTF} is used in \texttt{jlreq.cls},
7884 % \texttt{pxatbegshi.sty}, \texttt{pxeverysel.sty},
7885 % \texttt{pxeveryshi.sty}, so the public name may be an alias of the
7886 % internal conditional for a while. Regardless, those packages' use for
7887 % \cs{IfHookExistsTF} is not really correct and can be changed.}
7888 % \begin{macrocode}
7889 \cs_new_eq:NN \IfHookExistsTF \@@_if_usable:nTF
7890 % \end{macrocode}
7891 % \end{macro}
7892 %
7893 %
7894 %
7895 %
7896 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
7897 %
7898 % \subsection{Deprecated that needs cleanup at some point}
7899 %
7900 % \changes{v1.0p}{2021/08/20}{Added deprecation warnings for
7901 % old generic hook commands (gh/638)}
7902 %
7903 % \begin{macro}[int]{
7904 % \hook_disable:n,
7905 % \hook_provide:n,
7906 % \hook_provide_reversed:n,
7907 % \hook_provide_pair:nn,
7908 % \@@_activate_generic_reversed:n,
7909 % \@@_activate_generic_pair:nn,
7910 % }
7911 % Deprecated.
7912 % \begin{macrocode}
7913 \cs_new_protected:Npn \hook_disable:n
7914 {
7915 \@@_deprecated_warn:nn
7916 { hook_disable:n }
7917 { hook_disable_generic:n }
7918 \hook_disable_generic:n
7919 }
7920 \cs_new_protected:Npn \hook_provide:n
7921 {
7922 \@@_deprecated_warn:nn
7923 { hook_provide:n }
7924 { hook_activate_generic:n }
7925 \hook_activate_generic:n
7926 }
7927 \cs_new_protected:Npn \hook_provide_reversed:n
7928 {
7929 \@@_deprecated_warn:nn
7930 { hook_provide_reversed:n }
7931 { hook_activate_generic:n }
7932 \@@_activate_generic_reversed:n
7933 }
7934 \cs_new_protected:Npn \hook_provide_pair:nn
7935 {
7936 \@@_deprecated_warn:nn
7937 { hook_provide_pair:nn }
7938 { hook_activate_generic:n }
7939 \@@_activate_generic_pair:nn
7940 }
7941 \cs_new_protected:Npn \@@_activate_generic_reversed:n #1
7942 { \@@_normalize_hook_args:Nn \@@_activate_generic:nn {#1} { - } }
7943 \cs_new_protected:Npn \@@_activate_generic_pair:nn #1#2
7944 { \hook_activate_generic:n {#1} \@@_activate_generic_reversed:n {#2} }
7945 % \end{macrocode}
7946 % \end{macro}
7947 %
7948 % \begin{macro}[int]{
7949 % \DisableHook,
7950 % \ProvideHook,
7951 % \ProvideReversedHook,
7952 % \ProvideMirroredHookPair,
7953 % }
7954 % Deprecated.
7955 % \begin{macrocode}
7956 \cs_new_protected:Npn \DisableHook
7957 {
7958 \@@_deprecated_warn:nn
7959 { DisableHook }
7960 { DisableGenericHook }
7961 \hook_disable_generic:n
7962 }
7963 \cs_new_protected:Npn \ProvideHook
7964 {
7965 \@@_deprecated_warn:nn
7966 { ProvideHook }
7967 { ActivateGenericHook }
7968 \hook_activate_generic:n
7969 }
7970 \cs_new_protected:Npn \ProvideReversedHook
7971 {
7972 \@@_deprecated_warn:nn
7973 { ProvideReversedHook }
7974 { ActivateGenericHook }
7975 \@@_activate_generic_reversed:n
7976 }
7977 \cs_new_protected:Npn \ProvideMirroredHookPair
7978 {
7979 \@@_deprecated_warn:nn
7980 { ProvideMirroredHookPair }
7981 { ActivateGenericHook }
7982 \@@_activate_generic_pair:nn
7983 }
7984 % \end{macrocode}
7985 % \end{macro}
7986 %
7987 % \begin{macro}{\@@_deprecated_warn:nn}
7988 % Warns about a deprecation, telling what should be used instead.
7989 % \begin{macrocode}
7990 \cs_new_protected:Npn \@@_deprecated_warn:nn #1 #2
7991 { \msg_warning:nnnn { hooks } { deprecated } {#1} {#2} }
7992 \msg_new:nnn { hooks } { deprecated }
7993 {
7994 Command~\iow_char:N\\#1~is~deprecated~and~will~be~removed~in~a~
7995 future~release. \\ \\
7996 Use~\iow_char:N\\#2~instead.
7997 }
7998 % \end{macrocode}
7999 % \end{macro}
8000 %
8001 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
8002 %
8003 %
8004 % \subsection{Internal commands needed elsewhere}
8005 %
8006 % Here we set up a few horrible (but consistent) \LaTeXe{} names to
8007 % allow for internal commands to be used outside this module. We
8008 % have to unset the \texttt{@\/@} since we want double ``at'' sign
8009 % in place of double underscores.
8010 %
8011 % \begin{macrocode}
8012 %<@@=>
8013 % \end{macrocode}
8014 %
8015 % \begin{macro}[int]{
8016 % \@expl@@@initialize@all@@,
8017 % \@expl@@@hook@curr@name@pop@@
8018 % }
8019 %
8020 % \InternalDetectionOff
8021 % \begin{macrocode}
8022 \cs_new_eq:NN \@expl@@@initialize@all@@
8023 \__hook_initialize_all:
8024 % \end{macrocode}
8025 %
8026 % \begin{macrocode}
8027 \cs_new_eq:NN \@expl@@@hook@curr@name@pop@@
8028 \__hook_curr_name_pop:
8029 % \end{macrocode}
8030 % \InternalDetectionOn
8031 % \end{macro}
8032 %
8033 % Rolling back here doesn't undefine the interface commands as they
8034 % may be used in packages without rollback functionality. So we
8035 % just make them do nothing which may or may not work depending on
8036 % the code usage.
8037 % \changes{v1.0d}{2020/10/04}{Definition \cs{AddToHookNext} was supposed
8038 % to be for \cs{AddToHook} vice versa (gh/401)}
8039 % \begin{macrocode}
8040 %
8041 %<latexrelease>\IncludeInRelease{0000/00/00}{lthooks}
8042 %<latexrelease> {The~hook~management}%
8043 %<latexrelease>
8044 %<latexrelease>\def \NewHook#1{}
8045 %<latexrelease>\def \NewReversedHook#1{}
8046 %<latexrelease>\def \NewMirroredHookPair#1#2{}
8047 %<latexrelease>
8048 %<latexrelease>\def \DisableGenericHook #1{}
8049 %<latexrelease>
8050 %<latexrelease>\long\def\AddToHookNext#1#2{}
8051 %<latexrelease>
8052 %<latexrelease>\def\AddToHook#1{\@gobble@AddToHook@args}
8053 %<latexrelease>\providecommand\@gobble@AddToHook@args[2][]{}
8054 %<latexrelease>
8055 %<latexrelease>\def\RemoveFromHook#1{\@gobble@RemoveFromHook@arg}
8056 %<latexrelease>\providecommand\@gobble@RemoveFromHook@arg[1][]{}
8057 %<latexrelease>
8058 %<latexrelease>\def \UseHook #1{}
8059 %<latexrelease>\def \UseOneTimeHook #1{}
8060 %<latexrelease>\def \ShowHook #1{}
8061 %<latexrelease>\let \DebugHooksOn \@empty
8062 %<latexrelease>\let \DebugHooksOff\@empty
8063 %<latexrelease>
8064 %<latexrelease>\def \DeclareHookRule #1#2#3#4{}
8065 %<latexrelease>\def \DeclareDefaultHookRule #1#2#3{}
8066 %<latexrelease>\def \ClearHookRule #1#2#3{}
8067 % \end{macrocode}
8068 % If the hook management is not provided we make the test for existence
8069 % false and the test for empty true in the hope that this is most
8070 % of the time reasonable. If not a package would need to guard
8071 % against running in an old kernel.
8072 % \begin{macrocode}
8073 %<latexrelease>\long\def \IfHookExistsTF #1#2#3{#3}
8074 %<latexrelease>\long\def \IfHookEmptyTF #1#2#3{#2}
8075 %<latexrelease>
8076 %<latexrelease>\EndModuleRelease
8077 % \end{macrocode}
8078 %
8079 % \begin{macrocode}
8080 %<@@=hook>
8081 % \end{macrocode}
8082 %
8083 % \changes{v1.1a}{2023/04/06}
8084 % {Add dedicated rollback code to revert data structures (hook-args).}
8085 % \begin{macrocode}
8086 %<latexrelease>\cs:w @@_rollback_tidying: \cs_end:
8087 %<latexrelease>\bool_lazy_and:nnT
8088 %<latexrelease> { \int_compare_p:nNn { \sourceLaTeXdate } > { 20230600 } }
8089 %<latexrelease> { \int_compare_p:nNn { \requestedLaTeXdate } < { 20230601 } }
8090 %<latexrelease> {
8091 %<latexrelease> \cs_gset_protected:Npn \@@_rollback_tidying:
8092 %<latexrelease> {
8093 %<latexrelease> \@latex@error { Rollback~code~executed~twice }
8094 %<latexrelease> {
8095 %<latexrelease> Something~went~wrong~(unless~this~was~
8096 %<latexrelease> done~on~purpose~in~a~testing~environment).
8097 %<latexrelease> }
8098 %<latexrelease> \use_none:nnnn
8099 %<latexrelease> }
8100 %<latexrelease> \cs_set:Npn \@@_tmp:w #1 #2
8101 %<latexrelease> {
8102 %<latexrelease> \@@_tl_gset:ce { @@#1~#2 }
8103 %<latexrelease> {
8104 %<latexrelease> \exp_args:No \exp_not:o
8105 %<latexrelease> {
8106 %<latexrelease> \cs:w @@#1~#2 \exp_last_unbraced:Ne \cs_end:
8107 %<latexrelease> { \@@_braced_cs_parameter:n
8108 %<latexrelease> { @@#1~#2 } }
8109 %<latexrelease> }
8110 %<latexrelease> }
8111 %<latexrelease> }
8112 %<latexrelease> \seq_map_inline:Nn \g_@@_all_seq
8113 %<latexrelease> {
8114 %<latexrelease> \exp_after:wN \cs_gset_nopar:Npn
8115 %<latexrelease> \cs:w g_@@_#1_code_prop \exp_args:NNo \exp_args:No
8116 %<latexrelease> \cs_end: { \cs:w g_@@_#1_code_prop \cs_end: }
8117 %<latexrelease> \@@_tmp:w { _toplevel } {#1}
8118 %<latexrelease> \@@_tmp:w { _next } {#1}
8119 %<latexrelease> }
8120 %<latexrelease> }
8121 \ExplSyntaxOff
8122 %</2ekernel|latexrelease>
8123 % \end{macrocode}
8124 %
8125 % \begin{macrocode}
8126 %<@@=>
8127 % \end{macrocode}
8128 %
8129 % \Finale
8130 %
8131 %
8132 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
8133 \endinput
8134
8135 ^^A Needed for emacs
8136 ^^A
8137 ^^A Local Variables:
8138 ^^A mode: latex
8139 ^^A coding: utf-8-unix
8140 ^^A End: