[latex3-commits] [git/LaTeX3-latex3-latex2e] hook-args: Less code duplication (426a3495)

PhelypeOleinik phelype.oleinik at latex-project.org
Wed Mar 29 02:23:54 CEST 2023


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

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

commit 426a34957f489e6e667cf351bfe78052abaa86fd
Author: PhelypeOleinik <phelype.oleinik at latex-project.org>
Date:   Tue Mar 28 21:23:54 2023 -0300

    Less code duplication


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

426a34957f489e6e667cf351bfe78052abaa86fd
 base/lthooks.dtx | 59 +++++++++++++++++++++++++++++++-------------------------
 1 file changed, 33 insertions(+), 26 deletions(-)

diff --git a/base/lthooks.dtx b/base/lthooks.dtx
index 35b01513..724d3925 100644
--- a/base/lthooks.dtx
+++ b/base/lthooks.dtx
@@ -3465,19 +3465,7 @@
 %    \begin{macrocode}
 \cs_new_protected:Npn \@@_gput_code:nnn #1 #2 #3
   {
-    \@@_if_replacing_args:TF
-      {
-        \tl_if_exist:cTF { c_@@_#1_parameter_tl }
-          { \tl_if_empty:cT { c_@@_#1_parameter_tl } { \use_ii:nn } }
-          { \@@_if_declared:nT {#1} { \use_ii:nn } }
-        \use_none:n
-          {
-            \msg_error:nnnn { hooks } { without-args }
-              {#1} { AddToHook }
-            \@@_replacing_args_false:
-          }
-      }
-      { }
+    \@@_chk_args_allowed:nn {#1} { AddToHook }
 %    \end{macrocode}
 %   Then check if the code should be executed immediately, rather than
 %   stored:
@@ -3500,6 +3488,9 @@
       { \@@_gput_code_store:nnn {#1} {#2} }
           {#3}
   }
+%    \end{macrocode}
+%
+%    \begin{macrocode}
 \cs_new_protected:Npn \@@_gput_code_store:nnn #1 #2 #3
   {
 %    \end{macrocode}
@@ -3652,6 +3643,32 @@
 % \end{macro}
 % \end{macro}
 %
+% \begin{macro}{\@@_chk_args_allowed:nn}
+%   This macro checks if it is possible to add code with references to
+%   a hook's arguments for hook \verb|#1|.  It only does something if
+%   the function being run is \verb|replacing_args|.  This macro will
+%   error if the hook is declared and takes no arguments, then it will
+%   set \cs{@@_replacing_args_false:} so that the macro which called it
+%   will add the code normally.
+%    \begin{macrocode}
+\cs_new_protected:Npn \@@_chk_args_allowed:nn #1 #2
+  {
+    \@@_if_replacing_args:TF
+      {
+        \tl_if_exist:cTF { c_@@_#1_parameter_tl }
+          { \tl_if_empty:cT { c_@@_#1_parameter_tl } { \use_ii:nn } }
+          { \@@_if_declared:nT {#1} { \use_ii:nn } }
+        \use_none:n
+          {
+            \msg_error:nnnn { hooks } { without-args } {#1} {#2}
+            \@@_replacing_args_false:
+          }
+      }
+      { }
+  }
+%    \end{macrocode}
+% \end{macro}
+%
 % \begin{macro}{\@@_gput_undeclared_hook:nnn}
 %   Often it may happen that a package $A$ defines a hook \verb=foo=,
 %   but package $B$, that adds code to that hook, is loaded before $A$.
@@ -5849,22 +5866,12 @@
   }
 %    \end{macrocode}
 %
+%   Here we start by sanity-checking with \cs{@@_chk_args_allowed:nn},
+%   then passing the \verb|_next| macro to \cs{@@_gput_next_do:Nnn}.
 %    \begin{macrocode}
 \cs_new_protected:Npn \@@_gput_next_do:nn #1
   {
-    \@@_if_replacing_args:TF
-      {
-        \tl_if_exist:cTF { c_@@_#1_parameter_tl }
-          { \tl_if_empty:cT { c_@@_#1_parameter_tl } { \use_ii:nn } }
-          { \@@_if_declared:nT {#1} { \use_ii:nn } }
-        \use_none:n
-          {
-            \msg_error:nnnn { hooks } { without-args }
-              {#1} { AddToHookNext }
-            \@@_replacing_args_false:
-          }
-      }
-      { }
+    \@@_chk_args_allowed:nn {#1} { AddToHookNext }
     \exp_args:Nc \@@_gput_next_do:Nnn { @@_next~#1 } {#1}
   }
 %    \end{macrocode}





More information about the latex3-commits mailing list.