[latex3-commits] [git/LaTeX3-latex3-latex2e] hook-args: Define a boolean instead of passing one as argument (fe39aca9)

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/fe39aca9d5594eeaa4fc4bf0e029dc5989245695

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

commit fe39aca9d5594eeaa4fc4bf0e029dc5989245695
Author: PhelypeOleinik <phelype.oleinik at latex-project.org>
Date:   Mon Mar 6 00:11:23 2023 -0300

    Define a boolean instead of passing one as argument


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

fe39aca9d5594eeaa4fc4bf0e029dc5989245695
 base/lthooks.dtx | 161 ++++++++++++++++++++++++++++++++++++-------------------
 1 file changed, 105 insertions(+), 56 deletions(-)

diff --git a/base/lthooks.dtx b/base/lthooks.dtx
index 39f29739..5aaf9681 100644
--- a/base/lthooks.dtx
+++ b/base/lthooks.dtx
@@ -2557,28 +2557,28 @@
       }
       { \@@_tl_gset:cn { @@#2~#3 } {#4} }
   }
-\cs_new_protected:Npn \@@_cs_gput_right:Nnnn #1 #2 #3 #4
+\cs_new_protected:Npn \@@_cs_gput_right:nnn #1 #2 #3
   {
-    \tl_if_exist:cTF { c_@@_#3_parameter_tl }
+    \tl_if_exist:cTF { c_@@_#2_parameter_tl }
       {
         \@@_code_gset_auxi:veen
-          { c_@@_#3_parameter_tl }
+          { c_@@_#2_parameter_tl }
           {
             \exp_args:NNo \exp_args:No \@@_double_hashes:n
               {
-                \cs:w @@#2~#3 \exp_last_unbraced:Ne \cs_end:
-                  { \@@_braced_parameter:n {#3} }
+                \cs:w @@#1~#2 \exp_last_unbraced:Ne \cs_end:
+                  { \@@_braced_parameter:n {#2} }
               }
           }
           {
             \bool_if:NTF #1
               { \exp_not:n }
               { \@@_double_hashes:n }
-                {#4}
+                {#3}
           }
-          { @@#2~#3 }
+          { @@#1~#2 }
       }
-      { \@@_tl_gput_right:cn { @@#2~#3 } {#4} }
+      { \@@_tl_gput_right:cn { @@#1~#2 } {#3} }
   }
 \cs_new_protected:Npn \@@_code_gset_auxi:nnnn #1 #2 #3 #4
   { \cs_gset:cpn {#4} #1 { #2 #3 } }
@@ -2786,12 +2786,12 @@
           {
 %    \end{macrocode}
 %    Use \cs{c_false_bool} as the third argument to
-%    \cs{@@_hook_gput_code_do:nnNn} because there is no argument
+%    \cs{@@_hook_gput_code_do:nnn} because there is no argument
 %    replacement in legacy hooks.
 %    \begin{macrocode}
             \use:e
               {
-                \@@_hook_gput_code_do:nnNn {#1} { legacy } \c_false_bool
+                \@@_hook_gput_code_do:nnn {#1} { legacy }
                   { \exp_not:v { @#1hook } }
               }
 %    \end{macrocode}
@@ -3257,7 +3257,7 @@
 % \subsection{Adding or removing hook code}
 %
 % \begin{macro}{\hook_gput_code:nnn}
-% \begin{macro}{\@@_gput_code:nnNn,\@@_hook_gput_code_do:nnNn}
+% \begin{macro}{\@@_gput_code:nnn,\@@_hook_gput_code_do:nnn}
 %
 %    With \cs{hook_gput_code:nnn}\Arg{hook}\Arg{label}\Arg{code} a
 %    chunk of \meta{code} is added to an existing \meta{hook} labeled
@@ -3266,34 +3266,38 @@
 %    \begin{macrocode}
 %<latexrelease>\IncludeInRelease{2023/06/01}%
 %<latexrelease>                 {\hook_gput_code_with_args:nnn}{Hooks~with~args}
-\cs_new_protected:Npn \hook_gput_code:nnn #1 #2
+\cs_new_protected:Npn \hook_gput_code:nnn #1 #2 #3
   {
-    \@@_normalize_hook_args:Nnn \@@_gput_code:nnNn
-      {#1} {#2} \c_false_bool
+    \@@_replacing_args_false:
+    \@@_normalize_hook_args:Nnn \@@_gput_code:nnn {#1} {#2} {#3}
+    \@@_replacing_args_unset:
   }
-\cs_new_protected:Npn \hook_gput_code_with_args:nnn #1 #2
+\cs_new_protected:Npn \hook_gput_code_with_args:nnn #1 #2 #3
   {
-    \@@_normalize_hook_args:Nnn \@@_gput_code:nnNn
-      {#1} {#2} \c_true_bool
+    \@@_replacing_args_true:
+    \@@_normalize_hook_args:Nnn \@@_gput_code:nnn {#1} {#2} {#3}
+    \@@_replacing_args_unset:
   }
 %    \end{macrocode}
 %
 %    \begin{macrocode}
-\cs_new_protected:Npn \@@_gput_code:nnNn #1 #2 #3
+\cs_new_protected:Npn \@@_gput_code:nnn #1 #2
   {
-    \bool_if:NTF #3
+    \@@_if_replacing_args:TF
       {
-        \tl_if_exist:cF { c_@@_#1_parameter_tl }
+        \bool_lazy_and:nnT
+            { \@@_if_usable_p:n {#1} }
+            { ! \tl_if_exist_p:c { c_@@_#1_parameter_tl } }
           {
             \msg_error:nnnn { hooks } { without-args }
               {#1} { AddToHook }
           }
-        \@@_gput_code_normal:nnNn
+        \@@_gput_code_normal:nnn
       }
-      { \@@_gput_code_normal:nnNn }
-        {#1} {#2} #3
+      { \@@_gput_code_normal:nnn }
+        {#1} {#2}
   }
-\cs_new_protected:Npn \@@_gput_code_normal:nnNn #1 #2 #3
+\cs_new_protected:Npn \@@_gput_code_normal:nnn #1 #2
   {
 %    \end{macrocode}
 %   First check if the code should be executed immediately, rather than
@@ -3305,9 +3309,9 @@
         \msg_error:nnnn { hooks } { one-time-args } {#1} { AddToHook }
         \use:n
       }
-      { \@@_gput_code_store:nnNn {#1} {#2} #3 }
+      { \@@_gput_code_store:nnn {#1} {#2} }
   }
-\cs_new_protected:Npn \@@_gput_code_store:nnNn #1 #2 #3 #4
+\cs_new_protected:Npn \@@_gput_code_store:nnn #1 #2 #3
   {
 %    \end{macrocode}
 %    Then check if the hook is usable.
@@ -3319,7 +3323,7 @@
 %    this is then sorted into a token list for fast execution.
 %    \begin{macrocode}
       {
-        \@@_hook_gput_code_do:nnNn {#1} {#2} #3 {#4}
+        \@@_hook_gput_code_do:nnn {#1} {#2} {#3}
 %    \end{macrocode}
 %    However, if there is an update within the document we need to alter
 %    this execution code which is done by
@@ -3336,14 +3340,14 @@
       {
         \@@_if_disabled:nTF {#1}
           { \msg_error:nnn { hooks } { hook-disabled } {#1} }
-          { \@@_try_declaring_generic_hook:nnn {#1} {#2} {#4} }
+          { \@@_try_declaring_generic_hook:nnn {#1} {#2} {#3} }
       }
   }
 %    \end{macrocode}
 %
 %    This macro will unconditionally add a chunk of code to the given hook.
 %    \begin{macrocode}
-\cs_new_protected:Npn \@@_hook_gput_code_do:nnNn #1 #2 #3 #4
+\cs_new_protected:Npn \@@_hook_gput_code_do:nnn #1 #2 #3
   {
 %    \end{macrocode}
 %    However, first some debugging info if debugging is enabled:
@@ -3351,11 +3355,11 @@
     \@@_debug:n{\iow_term:x{****~ Add~ to~
                       \@@_if_usable:nF {#1} { undeclared~ }
                       hook~ #1~ (#2)
-                      \on at line\space <-~ \tl_to_str:n{#4}} }
+                      \on at line\space <-~ \tl_to_str:n{#3}} }
 %    \end{macrocode}
 %    Then try to get the code chunk labeled \verb=#2= from the hook.
-%    If there's code already there, then append \verb=#4= to that,
-%    otherwise just put \verb=#4=.  If the current label is |top-level|,
+%    If there's code already there, then append \verb=#3= to that,
+%    otherwise just put \verb=#3=.  If the current label is |top-level|,
 %    the code is added to a dedicated token list
 %    \cs{@@_toplevel\textvisiblespace\meta{hook}} that goes at the end of the
 %    hook (or at the beginning, for a reversed hook), just before
@@ -3370,7 +3374,7 @@
 %    with \cs{@@_init_structure:n}.
 %    \begin{macrocode}
             \@@_init_structure:n {#1}
-            \@@_cs_gput_right:Nnnn #3 { _toplevel } {#1} {#4}
+            \@@_cs_gput_right:nnn { _toplevel } {#1} {#3}
           }
           { \msg_error:nnn { hooks } { misused-top-level } {#1} }
       }
@@ -3379,11 +3383,11 @@
           { g_@@_#1_code_prop }
           {
             \bool_lazy_or:nnTF
-                { \bool_if_p:N #3 }
+                { \@@_if_replacing_args_p: }
                 { ! \tl_if_exist_p:c { c_@@_#1_parameter_tl } }
               { \exp_not:n }
               { \@@_double_hashes:n }
-                {#4}
+                {#3}
           }
           {#2}
       }
@@ -3464,7 +3468,7 @@
 \cs_new_protected:Npn \@@_gput_undeclared_hook:nnn #1 #2 #3
   {
     \@@_init_structure:n {#1}
-    \@@_hook_gput_code_do:nnNn {#1} {#2} \c_false_bool {#3}
+    \@@_hook_gput_code_do:nnn {#1} {#2} {#3}
   }
 %<latexrelease>\EndIncludeInRelease
 %    \end{macrocode}
@@ -5373,49 +5377,53 @@
 %
 % \begin{macro}{\hook_gput_next_code:nn}
 %    \begin{macrocode}
-\cs_new_protected:Npn \hook_gput_next_code:nn #1
+\cs_new_protected:Npn \hook_gput_next_code:nn #1 #2
   {
-    \@@_normalize_hook_args:Nn \@@_gput_next_code:nNn
-      {#1} \c_false_bool
+    \@@_replacing_args_false:
+    \@@_normalize_hook_args:Nn \@@_gput_next_code:nn {#1} {#2}
+    \@@_replacing_args_unset:
   }
-\cs_new_protected:Npn \hook_gput_next_code_with_args:nn #1
+\cs_new_protected:Npn \hook_gput_next_code_with_args:nn #1 #2
   {
-    \@@_normalize_hook_args:Nn \@@_gput_next_code:nNn
-      {#1} \c_true_bool
+    \@@_replacing_args_true:
+    \@@_normalize_hook_args:Nn \@@_gput_next_code:nn {#1} {#2}
+    \@@_replacing_args_unset:
   }
 %    \end{macrocode}
 %  \end{macro}
 %
 % \begin{macro}{\@@_gput_next_code:nNn,
 %               \@@_gput_next_do:nn,
-%               \@@_gput_next_do:NnNn}
+%               \@@_gput_next_do:Nnn}
 %    \begin{macrocode}
-\cs_new_protected:Npn \@@_gput_next_code:nNn #1 #2 #3
+\cs_new_protected:Npn \@@_gput_next_code:nn #1 #2
   {
     \@@_if_disabled:nTF {#1}
       { \msg_error:nnn { hooks } { hook-disabled } {#1} }
       {
         \@@_if_structure_exist:nTF {#1}
-          { \@@_gput_next_choose:Nnn #2 {#1} {#3} }
-          { \@@_try_declaring_generic_next_hook:nn {#1} {#3} }
+          { \@@_gput_next_choose:nn }
+          { \@@_try_declaring_generic_next_hook:nn }
+              {#1} {#2}
       }
   }
 %    \end{macrocode}
 %
 %    \begin{macrocode}
 \cs_new_protected:Npn \@@_gput_next_do:nn #1
-  { \@@_gput_next_choose:Nnn \c_false_bool }
-\cs_new_protected:Npn \@@_gput_next_choose:Nnn #1 #2
+  { \@@_gput_next_choose:nn }
+\cs_new_protected:Npn \@@_gput_next_choose:nn #1
   {
-    \bool_if:NT #1
+    \@@_if_replacing_args:TF
       {
-        \tl_if_exist:cF { c_@@_#2_parameter_tl }
+        \tl_if_exist:cF { c_@@_#1_parameter_tl }
           {
             \msg_error:nnnn { hooks } { without-args }
-              {#2} { AddToHookNext }
+              {#1} { AddToHookNext }
           }
       }
-    \exp_args:Nc \@@_gput_next_do:NnNn { @@_next~#2 } {#2} #1
+      { }
+    \exp_args:Nc \@@_gput_next_do:Nnn { @@_next~#1 } {#1}
   }
 %    \end{macrocode}
 %   First check if the ``next code'' token list is empty:  if so we need
@@ -5428,13 +5436,13 @@
 %   list is empty, the hook code has to be updated to add the next
 %   execution token list.
 %    \begin{macrocode}
-\cs_new_protected:Npn \@@_gput_next_do:NnNn #1 #2 #3
+\cs_new_protected:Npn \@@_gput_next_do:Nnn #1 #2
   {
     \exp_args:Ne \str_if_empty:nT { \cs_replacement_spec:c { @@~#2 } }
       { \@@_update_hook_code:n {#2} }
     \tl_if_empty:NT #1
       { \@@_tl_gset:Nn #1 { \@@_clear_next:n {#2} } }
-    \@@_cs_gput_right:Nnnn #3 { _next } {#2}
+    \@@_cs_gput_right:nnn { _next } {#2}
   }
 %    \end{macrocode}
 %  \end{macro}
@@ -5686,8 +5694,9 @@
 %   cheat a bit and sneak the cleanup code at the end of the hook,
 %   along with the next execution code.
 %    \begin{macrocode}
-    \@@_cs_gput_right:Nnnn \c_false_bool { _next }
-      {#1} { \@@_use_once_clear:n {#1} }
+    \@@_replacing_args_false:
+    \@@_cs_gput_right:nnn { _next } {#1} { \@@_use_once_clear:n {#1} }
+    \@@_replacing_args_unset:
     \@@_use_initialized:n {#1}
   }
 %<latexrelease>\EndIncludeInRelease
@@ -5955,6 +5964,46 @@
 %    \end{macrocode}
 %  \end{macro}
 %
+% \begin{macro}[EXP]{\@@_if_replacing_args:TF}
+% \begin{macro}[EXP]{\@@_misused_replacing_args:nn}
+% \begin{macro}{\@@_replacing_args_true:}
+% \begin{macro}{\@@_replacing_args_false:}
+% \begin{macro}{\@@_replacing_args_unset:}
+%   An internal conditional that checks if the code being added to the
+%   hook contains arguments.
+%    \begin{macrocode}
+\cs_new:Npn \@@_misused_if_replacing_args:nn #1 #2
+  {
+    \msg_expandable_error:nnn { latex2e } { should-not-happen }
+      { Misused~\@@_if_replacing_args:. }
+  }
+\cs_new_eq:NN \@@_if_replacing_args:TF \@@_misused_if_replacing_args:nn
+\cs_new:Npn \@@_if_replacing_args_p:
+  { \@@_misused_if_replacing_args:nn ?? \c_false_bool }
+\cs_new_protected:Npn \@@_replacing_args_true:
+  {
+    \cs_set_eq:NN \@@_if_replacing_args:TF \use_i:nn
+    \cs_set_eq:NN \@@_if_replacing_args_p: \c_true_bool
+  }
+\cs_new_protected:Npn \@@_replacing_args_false:
+  {
+    \cs_set_eq:NN \@@_if_replacing_args:TF \use_ii:nn
+    \cs_set_eq:NN \@@_if_replacing_args_p: \c_false_bool
+  }
+\cs_new_protected:Npn \@@_replacing_args_unset:
+  {
+    \cs_set_eq:NN \@@_if_replacing_args:TF
+      \@@_misused_if_replacing_args:nn
+    \cs_set:Npn \@@_if_replacing_args_p:
+      { \@@_misused_if_replacing_args:nn ?? \c_false_bool }
+  }
+%    \end{macrocode}
+%  \end{macro}
+%  \end{macro}
+%  \end{macro}
+%  \end{macro}
+%  \end{macro}
+%
 %  \subsection{Messages}
 %
 %    Hook errors are LaTeX kernel errors:





More information about the latex3-commits mailing list.