[latex3-commits] [git/LaTeX3-latex3-latex2e] hook-args: Add 'with_args' versions (ee732547)
PhelypeOleinik
phelype.oleinik at latex-project.org
Sat Mar 11 03:06:36 CET 2023
Repository : https://github.com/latex3/latex2e
On branch : hook-args
Link : https://github.com/latex3/latex2e/commit/ee73254743701059e63cd0bd7c9623c3af5336c5
>---------------------------------------------------------------
commit ee73254743701059e63cd0bd7c9623c3af5336c5
Author: PhelypeOleinik <phelype.oleinik at latex-project.org>
Date: Sun Dec 11 21:35:57 2022 -0300
Add 'with_args' versions
>---------------------------------------------------------------
ee73254743701059e63cd0bd7c9623c3af5336c5
base/lthooks.dtx | 118 ++++++++++++++++++++++++++++++++++++++-----------------
1 file changed, 83 insertions(+), 35 deletions(-)
diff --git a/base/lthooks.dtx b/base/lthooks.dtx
index 1255c63a..c7bd94b8 100644
--- a/base/lthooks.dtx
+++ b/base/lthooks.dtx
@@ -2367,17 +2367,23 @@
% \subsubsection{Setting hooks up}
%
%
-% \begin{macro}{\hook_new:n,\@@_new:n}
+% \begin{macro}{
+% \hook_new:n,
+% \hook_new_with_args:nn
+% }
+% \begin{macro}{@@_new:nn}
% The \cs{hook_new:n} declaration declares a new hook and expects
% the hook \meta{name} as its argument, e.g.,
% \hook{begindocument}.
% \begin{macrocode}
\cs_new_protected:Npn \hook_new:n #1
- { \@@_normalize_hook_args:Nn \@@_new:n {#1} }
+ { \@@_normalize_hook_args:Nn \@@_new:nn {#1} { 0 } }
+\cs_new_protected:Npn \hook_new_with_args:nn #1 #2
+ { \@@_normalize_hook_args:Nn \@@_new:nn {#1} {#2} }
% \end{macrocode}
%
% \begin{macrocode}
-\cs_new_protected:Npn \@@_new:n #1
+\cs_new_protected:Npn \@@_new:nn #1 #2
{
% \end{macrocode}
% We check if the hook was already \emph{explicitly} declared with
@@ -2389,15 +2395,16 @@
{ \msg_error:nnn { hooks } { exists } {#1} }
{
\tl_new:c { g_@@_#1_declared_tl }
- \@@_make_usable:n {#1}
+ \@@_make_usable:nn {#1} {#2}
}
}
% \end{macrocode}
% \end{macro}
+% \end{macro}
%
%
%
-% \begin{macro}{\@@_make_usable:n}
+% \begin{macro}{\@@_make_usable:nn}
%
% This initializes all hook data structures for the hook but if
% used on its own doesn't mark the hook as declared (as
@@ -2405,7 +2412,7 @@
% not result in an error. This command is internally used by
% \cs{hook_gput_code:n} when adding code to a generic hook.
% \begin{macrocode}
-\cs_new_protected:Npn \@@_make_usable:n #1
+\cs_new_protected:Npn \@@_make_usable:nn #1 #2
{
% \end{macrocode}
% Now we check if the hook's data structure can be safely created
@@ -2414,14 +2421,22 @@
% allocate the necessary data structures for the new hook,
% otherwise just do nothing.
% \begin{macrocode}
- \tl_if_exist:cF { @@~#1 }
+ \cs_if_exist:cF { @@~#1 }
{
\seq_gput_right:Nn \g_@@_all_seq {#1}
% \end{macrocode}
+% This token list stores the parameter text of the hook code
+% (a simple run of parameters):
+% \begin{macrocode}
+ \__kernel_cs_parm_from_arg_count:nnF
+ { \tl_const:cn { c_@@_#1_parameter_tl } }
+ {#2}
+ { \tl_const:cn { c_@@_#1_parameter_tl } { } }
+% \end{macrocode}
% This is only used by the actual code of the current hook, so
% declare it normally:
% \begin{macrocode}
- \tl_new:c { @@~#1 }
+ \@@_code_gset:nn {#1} { }
% \end{macrocode}
% Now ensure that the base data structure for the hook exists:
% \begin{macrocode}
@@ -2457,6 +2472,19 @@
% \end{macrocode}
% \end{macro}
%
+% \begin{macro}{\@@_code_gset:nn}
+% A simple auxiliary that defines the hook code macro
+% \cs[no-index]{@@~\meta{hook}} with the defined number of arguments.
+% \begin{macrocode}
+\cs_new_protected:Npn \@@_code_gset:nn #1 #2
+ {
+ \exp_args:Nv \use_ii_i:nn { c_@@_#1_parameter_tl }
+ { \cs_gset:cpn { @@~#1 } } {#2}
+ }
+\cs_generate_variant:Nn \@@_code_gset:nn { no, nV }
+% \end{macrocode}
+% \end{macro}
+%
%
%
% \begin{macro}{\@@_init_structure:n}
@@ -2487,16 +2515,22 @@
%
%
%
-% \begin{macro}{\hook_new_reversed:n,\@@_new_reversed:n}
+% \begin{macro}{
+% \hook_new_reversed:n,
+% \hook_new_reversed_with_args:nn
+% }
+% \begin{macro}{\@@_new_reversed:nn}
%
% Declare a new hook. The default ordering of code chunks is
% reversed, signaled by setting the token list to a minus sign.
% \begin{macrocode}
\cs_new_protected:Npn \hook_new_reversed:n #1
- { \@@_normalize_hook_args:Nn \@@_new_reversed:n {#1} }
-\cs_new_protected:Npn \@@_new_reversed:n #1
+ { \@@_normalize_hook_args:Nn \@@_new_reversed:nn {#1} { 0 } }
+\cs_new_protected:Npn \hook_new_reversed_with_args:nn #1 #2
+ { \@@_normalize_hook_args:Nn \@@_new_reversed:nn {#1} {#2} }
+\cs_new_protected:Npn \@@_new_reversed:nn #1 #2
{
- \@@_new:n {#1}
+ \@@_new:nn {#1} {#2}
% \end{macrocode}
% If the hook already exists the above will generate an error
% message, so the next line should be executed (but it is --- too
@@ -2506,9 +2540,7 @@
}
% \end{macrocode}
% \end{macro}
-%
-%
-%
+% \end{macro}
%
% \begin{macro}{\hook_new_pair:nn}
% A shorthand for declaring a normal and a (matching) reversed hook in one go.
@@ -2595,7 +2627,7 @@
{
\bool_lazy_and:nnTF
{ \tl_if_exist_p:c { g_@@_#1_declared_tl } }
- { ! \tl_if_exist_p:c { @@~#1 } }
+ { ! \cs_if_exist_p:c { @@~#1 } }
{ \prg_return_true: }
{ \prg_return_false: }
}
@@ -2626,11 +2658,19 @@
%
% \begin{macrocode}
\cs_new_protected:Npn \hook_activate_generic:n #1
- { \@@_normalize_hook_args:Nn \@@_activate_generic:nn {#1} { } }
+ {
+ \@@_normalize_hook_args:Nn \@@_activate_generic:nnn
+ {#1} { } { 0 }
+ }
+\cs_new_protected:Npn \hook_activate_generic_with_args:nn #1 #2
+ {
+ \@@_normalize_hook_args:Nn \@@_activate_generic:nnn
+ {#1} { } {#2}
+ }
% \end{macrocode}
%
% \begin{macrocode}
-\cs_new_protected:Npn \@@_activate_generic:nn #1 #2
+\cs_new_protected:Npn \@@_activate_generic:nnn #1 #2 #3
{
% \end{macrocode}
% If the hook to be activated was disabled we warn (for now --- this
@@ -2646,7 +2686,7 @@
\@@_if_declared:nF {#1}
{
\tl_new:c { g_@@_#1_declared_tl }
- \@@_make_usable:n {#1}
+ \@@_make_usable:nn {#1} {#3}
\tl_gset:cx { g_@@_#1_reversed_tl }
{ \@@_if_generic_reversed:nT {#1} { - } }
% \end{macrocode}
@@ -3196,10 +3236,10 @@
% Declare the hook always even if it can't really be used (error
% message generated elsewhere).
%
-% Here we use \cs{@@_make_usable:n}, so that a \cs{hook_new:n} is still
+% Here we use \cs{@@_make_usable:nn}, so that a \cs{hook_new:n} is still
% possible later.
% \begin{macrocode}
- \@@_make_usable:n {#5}
+ \@@_make_usable:nn {#5} { 0 }
}
\@@_if_generic_reversed:nT {#5}
{ \tl_gset:cn { g_@@_#5_reversed_tl } { - } }
@@ -3856,20 +3896,23 @@
% \begin{macrocode}
\@@_debug:n { \prop_gclear:N \g_@@_used_prop }
\seq_map_inline:Nn \g_@@_all_seq
- {
- \@@_update_hook_code:n {##1}
- }
+ { \@@_update_hook_code:n {##1} }
% \end{macrocode}
% If we are debugging we show results hook by hook for all hooks
% that have data.
% \begin{macrocode}
\@@_debug:n
- { \iow_term:x{^^JAll~ initialized~ (non-empty)~ hooks:}
- \prop_map_inline:Nn \g_@@_used_prop
- { \iow_term:x{^^J~ ##1~ ->~
- \exp_not:v {@@~##1}~ }
- }
- }
+ {
+ \iow_term:x{^^JAll~ initialized~ (non-empty)~ hooks:}
+ \prop_map_inline:Nn \g_@@_used_prop
+ {
+ \iow_term:x
+ {
+ ^^J ~ ##1 ~ -> ~
+ \exp_args:Nc \cs_replacement_spec:N { @@~##1 } ~
+ }
+ }
+ }
% \end{macrocode}
% After all hooks are initialized we change the ``use'' to just
% call the hook code and not initialize it (as it was done in the
@@ -3915,7 +3958,7 @@
{
\prop_if_empty:cTF { g_@@_#1_code_prop }
{
- \@@_tl_gset:co { @@~#1 }
+ \@@_code_gset:no {#1}
{
\cs:w @@_toplevel~#1 \exp_after:wN \cs_end:
\cs:w @@_next~#1 \cs_end:
@@ -4185,6 +4228,8 @@
% \begin{macrocode}
\exp_args:NNo \@@_tl_gput:Nn #1 { \cs:w @@_toplevel~#3 \cs_end: }
\@@_tl_gput_right:No #1 { \cs:w @@_next~#3 \cs_end: }
+ \int_compare:nNnT { \tl_count:c { c_@@_#3_parameter_tl } } > { 0 }
+ { \@@_code_gset:nV {#3} #1 }
}
% \end{macrocode}
%
@@ -4754,7 +4799,7 @@
%<latexrelease> {Standardise~generic~hook~names}
\cs_new_protected:Npn \hook_use:n #1
{
- \tl_if_exist:cT { @@~#1 }
+ \cs_if_exist:cT { @@~#1 }
{
\@@_preamble_hook:n {#1}
\cs:w @@~#1 \cs_end:
@@ -4905,7 +4950,7 @@
{ \@@_tl_gset:cn { g_@@_#1_reversed_tl } { I } }
\cs_new_protected:Npn \@@_use_once_clear:n #1
{
- \@@_tl_gclear:c { @@~#1 }
+ \@@_code_gset:nn {#1} { }
\@@_tl_gclear:c { @@_next~#1 }
\@@_tl_gclear:c { @@_toplevel~#1 }
\prop_gclear_new:c { g_@@_#1_code_prop }
@@ -5002,7 +5047,7 @@
% \begin{macrocode}
\prg_new_conditional:Npnn \@@_if_usable:n #1 { p , T , F , TF }
{
- \tl_if_exist:cTF { @@~#1 }
+ \cs_if_exist:cTF { @@~#1 }
{ \prg_return_true: }
{ \prg_return_false: }
}
@@ -5523,7 +5568,10 @@
\@@_activate_generic_pair:nn
}
\cs_new_protected:Npn \@@_activate_generic_reversed:n #1
- { \@@_normalize_hook_args:Nn \@@_activate_generic:nn {#1} { - } }
+ {
+ \@@_normalize_hook_args:Nn \@@_activate_generic:nnn
+ {#1} { - } { 0 }
+ }
\cs_new_protected:Npn \@@_activate_generic_pair:nn #1#2
{ \hook_activate_generic:n {#1} \@@_activate_generic_reversed:n {#2} }
% \end{macrocode}
More information about the latex3-commits
mailing list.