[latex3-commits] [git/LaTeX3-latex3-latex2e] hook-args: Allow arguments to a hook that is not yet declared (c50f426f)

PhelypeOleinik phelype.oleinik at latex-project.org
Mon Mar 6 04:12:45 CET 2023


Repository : https://github.com/latex3/latex2e
On branch  : hook-args
Link       : https://github.com/latex3/latex2e/commit/c50f426f0839b1e71db32b4832fbfdb95adc0b3f

>---------------------------------------------------------------

commit c50f426f0839b1e71db32b4832fbfdb95adc0b3f
Author: PhelypeOleinik <phelype.oleinik at latex-project.org>
Date:   Mon Mar 6 00:12:45 2023 -0300

    Allow arguments to a hook that is not yet declared


>---------------------------------------------------------------

c50f426f0839b1e71db32b4832fbfdb95adc0b3f
 base/lthooks.dtx                       | 83 +++++++++++++++++++++++++++-------
 base/testfiles-lthooks/lthooks-033.lvt | 13 +++++-
 base/testfiles-lthooks/lthooks-033.tlg |  8 +++-
 3 files changed, 85 insertions(+), 19 deletions(-)

diff --git a/base/lthooks.dtx b/base/lthooks.dtx
index 3a56ed5e..c9eea049 100644
--- a/base/lthooks.dtx
+++ b/base/lthooks.dtx
@@ -2095,6 +2095,13 @@
 %    \end{macrocode}
 % \end{macro}
 %
+% \begin{macro}{\c_@@_nine_parameters_tl}
+%   A token list that contains nine parameters.
+%    \begin{macrocode}
+\tl_const:Nn \c_@@_nine_parameters_tl { #1#2#3#4#5#6#7#8#9 }
+%    \end{macrocode}
+% \end{macro}
+%
 % \begin{macro}[int]{\tl_gremove_once:Nx,\tl_show:x,\tl_log:x}
 %   Some variants of \pkg{expl3} functions.
 %   \fmiinline{should probably be moved to expl3}
@@ -2455,7 +2462,13 @@
           {
             \__kernel_cs_parm_from_arg_count:nnF
               { \tl_const:cn { c_@@_#1_parameter_tl } } {#2}
-              { \msg_error:nnnn { hooks } { too-many-args } {#1} {#2} }
+              {
+                \msg_error:nnnn { hooks } { too-many-args } {#1} {#2}
+                \tl_const:cx { c_@@_#1_parameter_tl }
+                  { \exp_not:V \c_@@_nine_parameters_tl }
+              }
+            \@@_normalise_cs_args:nn { _toplevel } {#1}
+            \@@_normalise_cs_args:nn { _next } {#1}
           }
 %    \end{macrocode}
 %    This is only used by the actual code of the current hook, so
@@ -2559,26 +2572,47 @@
   }
 \cs_new_protected:Npn \@@_cs_gput_right:nnn #1 #2 #3
   {
-    \tl_if_exist:cTF { c_@@_#2_parameter_tl }
+    % \tl_if_exist:cTF { c_@@_#2_parameter_tl }
+    %   {
+    \@@_code_gset_auxi:veen
       {
-        \@@_code_gset_auxi:veen
-          { c_@@_#2_parameter_tl }
+        c_@@_
+        \tl_if_exist:cTF { c_@@_#2_parameter_tl }
+          { #2_parameter } { nine_parameters }
+        _tl
+      }
+      {
+        \exp_args:NNo \exp_args:No \@@_double_hashes:n
           {
-            \exp_args:NNo \exp_args:No \@@_double_hashes:n
-              {
-                \cs:w @@#1~#2 \exp_last_unbraced:Ne \cs_end:
-                  { \@@_braced_parameter:n {#2} }
-              }
+            \cs:w @@#1~#2 \exp_last_unbraced:Ne \cs_end:
+              { \@@_braced_parameter:n {#2} }
           }
+      }
+      {
+        \@@_if_replacing_args:TF
+          { \exp_not:n }
+          { \@@_double_hashes:n }
+            {#3}
+      }
+      { @@#1~#2 }
+      % }
+      % { \@@_tl_gput_right:cn { @@#1~#2 } {#3} }
+  }
+\cs_new_protected:Npn \@@_normalise_cs_args:nn #1 #2
+  {
+    \@@_code_gset_auxi:veen
+      { c_@@_#2_parameter_tl }
+      {
+        \int_compare:nNnT { \@@_cs_parameter_count:c { @@#1~#2 } } = { 9 }
           {
-            \bool_if:NTF #1
-              { \exp_not:n }
-              { \@@_double_hashes:n }
-                {#3}
+            \exp_args:No \exp_not:o
+              {
+                \cs:w @@#1~#2 \cs_end:
+                  {##1} {##2} {##3} {##4} {##5} {##6} {##7} {##8} {##9}
+              }
           }
-          { @@#1~#2 }
       }
-      { \@@_tl_gput_right:cn { @@#1~#2 } {#3} }
+      { } { @@#1~#2 }
   }
 \cs_new_protected:Npn \@@_code_gset_auxi:nnnn #1 #2 #3 #4
   { \cs_gset:cpn {#4} #1 { #2 #3 } }
@@ -2603,7 +2637,22 @@
 %    \end{macrocode}
 % \end{macro}
 %
-% \begin{macro}{\@@_cs_gput_right:Nnnn}
+% \begin{macro}[pTF]{\@@_cs_parameter_count:c}
+%   Counts the number of parameters in a control sequence by counting
+%   the length of \cs{cs_parameter_spec:N} and dividing by two.
+%    \begin{macrocode}
+\cs_new:Npn \@@_cs_parameter_count:c #1
+  {
+    \int_eval:n
+      {
+        \exp_args:Nf \str_count:n
+          { \exp_args:Nc \cs_parameter_spec:N {#1} } / 2
+      }
+  }
+%    \end{macrocode}
+% \end{macro}
+%
+% \begin{macro}{\@@_cs_gput_right:nnn}
 % \begin{macro}{\@@_braced_parameter:n,\@@_braced_loop:w}
 %   Check if the expansion of a control sequence is empty by looking at
 %   its replacement text.
@@ -6049,7 +6098,7 @@
   {
     You~tried~to~declare~a~hook~with~#2~arguments,~but~a~
     hook~can~only~have~up~to~nine.~LaTeX~will~define~this~
-    hook~without~arguments.
+    hook~with~nine~arguments.
   }
 %    \end{macrocode}
 %
diff --git a/base/testfiles-lthooks/lthooks-033.lvt b/base/testfiles-lthooks/lthooks-033.lvt
index 7552ed0f..80d78e03 100644
--- a/base/testfiles-lthooks/lthooks-033.lvt
+++ b/base/testfiles-lthooks/lthooks-033.lvt
@@ -8,6 +8,7 @@
 
 \documentclass{minimal}
 \usepackage{trace}
+
 \begin{document}
 
 \START
@@ -118,7 +119,17 @@
     \NewHookWithArguments{use-once}{2}
     \AddToHookWithArguments{use-once}{\typeout{once(#1,#2)}}
     \UseOneTimeHookWithArguments{use-once}{foo}{bar}
-    \AddToHookWithArguments{use-once}{\typeout{once(#1,#2)}}
+    \AddToHookWithArguments{use-once}{\typeout{twice(#1,#2)}}
+  }
+
+%
+
+\TEST{Add with arguments before hook is declared}
+  {
+    \AddToHookWithArguments{not-declared}{\typeout{args(#1,#2)}}
+    \NewHookWithArguments{not-declared}{2}
+    \AddToHookWithArguments{not-declared}{\typeout{more-args(#1,#2)}}
+    \UseHookWithArguments{not-declared}{foo}{bar}
   }
 
 \END
diff --git a/base/testfiles-lthooks/lthooks-033.tlg b/base/testfiles-lthooks/lthooks-033.tlg
index 079454a1..f938f0dd 100644
--- a/base/testfiles-lthooks/lthooks-033.tlg
+++ b/base/testfiles-lthooks/lthooks-033.tlg
@@ -224,5 +224,11 @@ You tried to use \AddToHookWithArguments on a one-time hook that has already
 been used. You have to add the code before the hook is used, or add the code
 without arguments using \AddToHook instead.
 LaTeX will use \AddToHook.
-once(##1,##2)
+twice(##1,##2)
+============================================================
+============================================================
+TEST 6: Add with arguments before hook is declared
+============================================================
+args(foo,bar)
+more-args(foo,bar)
 ============================================================





More information about the latex3-commits mailing list.