Debian Codegraph @

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

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