[latex3-commits] [git/LaTeX3-latex3-latex2e] apdf161: Unify hook name and label normalisation (3dbfec7b)

PhelypeOleinik phelype.oleinik at latex-project.org
Tue Sep 8 00:27:18 CEST 2020


Repository : https://github.com/latex3/latex2e
On branch  : apdf161
Link       : https://github.com/latex3/latex2e/commit/3dbfec7b3d65a79629778bdfcd9a0d53c1d3cc52

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

commit 3dbfec7b3d65a79629778bdfcd9a0d53c1d3cc52
Author: PhelypeOleinik <phelype.oleinik at latex-project.org>
Date:   Mon Sep 7 19:27:18 2020 -0300

    Unify hook name and label normalisation


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

3dbfec7b3d65a79629778bdfcd9a0d53c1d3cc52
 base/lthooks.dtx                       | 125 +++++++++++++++++++++------------
 base/testfiles-lthooks/lthooks-027.lvt |  15 ++--
 base/testfiles-lthooks/lthooks-027.tlg |  12 ++--
 3 files changed, 97 insertions(+), 55 deletions(-)

diff --git a/base/lthooks.dtx b/base/lthooks.dtx
index 59abfa7e..741fd5f2 100644
--- a/base/lthooks.dtx
+++ b/base/lthooks.dtx
@@ -1687,10 +1687,7 @@
 %    \hook{begindocument}.
 %    \begin{macrocode}
 \cs_new_protected:Npn \hook_new:n #1
-  {
-    \exp_args:Nx \@@_new:n
-      { \@@_parse_label_default:nn {#1} { top-level } }
-  }
+  { \@@_normalize_hook_args:Nn \@@_new:n {#1} }
 \cs_new_protected:Npn \@@_new:n #1 {
 %    \end{macrocode}
 %    We check for one of the internal data structures and if it
@@ -1765,29 +1762,31 @@
 %
 %
 %
-%  \begin{macro}{\hook_new_reversed:n}
+%  \begin{macro}{\hook_new_reversed:n,\@@_new_reversed:n}
 %
 %    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 {
-  \hook_new:n {#1}
+\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
+  {
+    \@@_new:n {#1}
 %    \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
 %    bad).
 %    \begin{macrocode}
-  \tl_gset:cn { g_@@_#1_reversed_tl } { - }
-}
+    \tl_gset:cn { g_@@_#1_reversed_tl } { - }
+  }
 %    \end{macrocode}
 %  \end{macro}
 %
 %  \begin{macro}{\hook_new_pair:nn}
 %    A shorthand for declaring a normal and a (matching) reversed hook in one go.
 %    \begin{macrocode}
-\cs_new_protected:Npn \hook_new_pair:nn #1#2 {
-  \hook_new:n {#1}  \hook_new_reversed:n {#2}
-}
+\cs_new_protected:Npn \hook_new_pair:nn #1#2
+  { \hook_new:n {#1} \hook_new_reversed:n {#2} }
 %    \end{macrocode}
 %  \end{macro}
 %
@@ -1843,19 +1842,24 @@
 %
 % \subsection{Parsing a label}
 %
-% \begin{macro}[EXP]{\@@_parse_label_default:nn,\@@_parse_label_default:Vn}
+% \begin{macro}[EXP]{\@@_parse_label_default:n}
 %   This macro checks if a label was given (not \cs{c_novalue_tl}), and
 %   if so, tries to parse the label looking for a leading \verb|.| to
 %   replace for \cs{@currname}.  Otherwise \cs{@@_currname_or_default:n}
 %   is used to pick \cs{@currname} or the fallback value.
+%
+%   The default |top-level| is hard-coded here.  It once was an
+%   argument, but it's no longer needed. \pho{can't remember why}
 %    \begin{macrocode}
-\cs_new:Npn \@@_parse_label_default:nn #1 #2
+\cs_new:Npn \@@_parse_label_default:n #1
   {
     \tl_if_novalue:nTF {#1}
-      { \@@_currname_or_default:n {#2} }
-      { \tl_trim_spaces_apply:nN {#1} \@@_parse_dot_label:nn {#2} }
+      { \@@_currname_or_default:n { top-level } }
+      {
+        \tl_trim_spaces_apply:nN {#1}
+          \@@_parse_dot_label:nn { top-level }
+      }
   }
-\cs_generate_variant:Nn \@@_parse_label_default:nn { V }
 %    \end{macrocode}
 % \end{macro}
 %
@@ -1942,6 +1946,53 @@
 %    \end{macrocode}
 % \end{macro}
 %
+% \begin{macro}{\@@_normalize_hook_args:Nn}
+% \begin{macro}{\@@_normalize_hook_args:Nnn}
+% \begin{macro}{\@@_normalize_hook_rule_args:Nnnnn}
+% \begin{macro}{\@@_normalize_hook_args_aux:Nn}
+%   Standard route for normalising hook and label arguments.  The main
+%   macro does the entire operation within a group so that csnames made
+%   by \cs{@@_make_name:n} are wiped off before continuing.  This means
+%   that this function cannot be used for \cs{hook_use:n}!
+%    \begin{macrocode}
+\cs_new_protected:Npn \@@_normalize_hook_args_aux:Nn #1 #2
+  {
+    \group_begin:
+    \use:e
+      {
+        \group_end:
+        \exp_not:N #1 #2
+      }
+  }
+\cs_new_protected:Npn \@@_normalize_hook_args:Nn #1 #2
+  {
+    \@@_normalize_hook_args_aux:Nn #1
+      { { \@@_parse_label_default:n {#2} } }
+  }
+\cs_new_protected:Npn \@@_normalize_hook_args:Nnn #1 #2 #3
+  {
+    \@@_normalize_hook_args_aux:Nn #1
+      {
+        { \@@_parse_label_default:n {#2} }
+        { \@@_parse_label_default:n {#3} }
+      }
+  }
+\cs_new_protected:Npn \@@_normalize_hook_rule_args:Nnnnn #1 #2 #3 #4 #5
+  {
+    \@@_normalize_hook_args_aux:Nn #1
+      {
+        { \@@_parse_label_default:n {#2} }
+        { \@@_parse_label_default:n {#3} }
+        { \tl_trim_spaces:n {#4} }
+        { \@@_parse_label_default:n {#5} }
+      }
+  }
+%    \end{macrocode}
+% \end{macro}
+% \end{macro}
+% \end{macro}
+% \end{macro}
+%
 %
 % \begin{macro}{\hook_gput_code:nnn}
 % \begin{macro}{\@@_gput_code:nnn,\@@_gput_code:nxv,\@@_hook_gput_code_do:nnn}
@@ -1951,11 +2002,7 @@
 %    with \meta{label}.
 %    \begin{macrocode}
 \cs_new_protected:Npn \hook_gput_code:nnn #1 #2
-  {
-    \exp_args:Nxx \@@_gput_code:nnn
-      { \@@_parse_label_default:nn {#1} { top-level } }
-      { \@@_parse_label_default:nn {#2} { top-level } }
-  }
+  { \@@_normalize_hook_args:Nnn \@@_gput_code:nnn {#1} {#2} }
 \cs_new_protected:Npn \@@_gput_code:nnn #1 #2 #3
   {
 %    \end{macrocode}
@@ -2233,11 +2280,7 @@
 %    for \meta{hook} stored under \meta{label} is removed.
 %    \begin{macrocode}
 \cs_new_protected:Npn \hook_gremove_code:nn #1 #2
-  {
-    \exp_args:Nxx \@@_gremove_code:nn
-      { \@@_parse_label_default:nn {#1} { top-level } }
-      { \@@_parse_label_default:nn {#2} { top-level } }
-  }
+  { \@@_normalize_hook_args:Nnn \@@_gremove_code:nn {#1} {#2} }
 \cs_new_protected:Npn \@@_gremove_code:nn #1 #2
   {
 %    \end{macrocode}
@@ -2397,14 +2440,8 @@
 %    \begin{macrocode}
 \cs_new_protected:Npn \hook_gset_rule:nnnn #1#2#3#4
   {
-    \use:x
-      {
-        \@@_gset_rule:nnnn
-          { \@@_parse_label_default:nn {#1} { top-level } }
-          { \@@_parse_label_default:nn {#2} { top-level } }
-          {#3}
-          { \@@_parse_label_default:nn {#4} { top-level } }
-      }
+    \@@_normalize_hook_rule_args:Nnnnn \@@_gset_rule:nnnn
+      {#1} {#2} {#3} {#4}
   }
 %    \end{macrocode}
 %    
@@ -3143,10 +3180,7 @@
 %    onto the terminal and the \texttt{.log} file.
 %    \begin{macrocode}
 \cs_new_protected:Npn \hook_log:n #1
-  {
-    \exp_args:Nx \@@_log:n
-      { \@@_parse_label_default:nn {#1} { top-level } }
-  }
+  { \@@_normalize_hook_args:Nn \@@_log:n {#1} }
 \cs_new_protected:Npn \@@_log:n #1
   {
     \@@_preamble_hook:n {#1}
@@ -3309,10 +3343,7 @@
 %    
 %    \begin{macrocode}
 \cs_new_protected:Npn \hook_gput_next_code:nn #1
-  {
-    \exp_args:Nx \@@_gput_next_code:nn
-      { \@@_parse_label_default:nn {#1} { top-level } }
-  }
+  { \@@_normalize_hook_args:Nn \@@_gput_next_code:nn {#1} }
 \cs_new_protected:Npn \@@_gput_next_code:nn #1 #2
   {
     \@@_declare:n {#1}
@@ -3440,7 +3471,7 @@
 % \end{macro}
 % \end{macro}
 %
-%  \begin{macro}{\hook_use_once:n}
+%  \begin{macro}{\hook_use_once:n,\@@_use_once:n}
 %    For hooks that can and should be used only once we have a special
 %    use command that remembers the hook name in
 %    \cs{g_@@_execute_immediately_prop}. This has the effect that any
@@ -3448,6 +3479,8 @@
 %    than stored in the hook.
 %    \begin{macrocode}
 \cs_new_protected:Npn \hook_use_once:n #1
+  { \@@_normalize_hook_args:Nn \@@_use_once:n {#1} }
+\cs_new_protected:Npn \@@_use_once:n #1
   {
     \tl_if_exist:cT { g_@@_#1_code_tl }
       {
@@ -3713,8 +3746,8 @@
 %    Avoid the overhead of \pkg{xparse} and its protection that we
 %    don't want here (since the hook should vanish without trace if empty)!
 %    \begin{macrocode}
-\newcommand \UseHook        { \hook_use:n }
-\newcommand \UseOneTimeHook { \hook_use_once:n }
+\cs_new:Npn \UseHook        { \hook_use:n }
+\cs_new:Npn \UseOneTimeHook { \hook_use_once:n }
 %    \end{macrocode}
 %  \end{macro}
 %
diff --git a/base/testfiles-lthooks/lthooks-027.lvt b/base/testfiles-lthooks/lthooks-027.lvt
index fb643d59..2e2122cb 100644
--- a/base/testfiles-lthooks/lthooks-027.lvt
+++ b/base/testfiles-lthooks/lthooks-027.lvt
@@ -16,8 +16,9 @@
   {
     \hook_new:n { A }
     \hook_gput_code:nnn { A } { . } { \message{aaa} }
+    \hook_gset_rule:nnnn { A } { . } { after } { x }
     \hook_use_once:n { A }
-    \hook_gput_code:nnn { A } { . } { \message{bbb} }
+    \hook_gput_code:nnn { A } { x } { \message{bbb} }
     \hook_log:n { A }
   }
 \ExplSyntaxOff
@@ -26,8 +27,9 @@
   {
     \NewHook { -{hook}- }
     \AddToHook { -{hook}- } {\message{aaa}}
+    \DeclareHookRule { A } { . } { after } { x }
     \UseOneTimeHook { -{hook}- }
-    \AddToHook { -{hook}- } {\message{bbb}}
+    \AddToHook { -{hook}- } [ x ] {\message{bbb}}
     \ShowHook { -{hook}- }
   }
 
@@ -41,8 +43,9 @@
   {
     \NewHook { grüße }
     \AddToHook { grüße } {\message{aaa}}
+    \DeclareHookRule { A } { . } { after } { x }
     \UseOneTimeHook { grüße }
-    \AddToHook { grüße } {\message{bbb}}
+    \AddToHook { grüße } [ x ] {\message{bbb}}
     \ShowHook { grüße }
   }
 
@@ -52,8 +55,9 @@
   {
     \NewHook { \hookname }
     \AddToHook { \hookname } {\message{aaa}}
+    \DeclareHookRule { A } { . } { after } { x }
     \UseOneTimeHook { \hookname }
-    \AddToHook { \hookname } {\message{bbb}}
+    \AddToHook { \hookname } [ x ] {\message{bbb}}
     \ShowHook { \hookname }
   }
 
@@ -63,8 +67,9 @@
   {
     \NewHook { \hookname }
     \AddToHook { \hookname } {\message{aaa}}
+    \DeclareHookRule { A } { . } { after } { x }
     \UseOneTimeHook { \hookname }
-    \AddToHook { \hookname } {\message{bbb}}
+    \AddToHook { \hookname } [ x ] {\message{bbb}}
     \ShowHook { \hookname }
   }
 
diff --git a/base/testfiles-lthooks/lthooks-027.tlg b/base/testfiles-lthooks/lthooks-027.tlg
index d6215b53..dbb4cfb2 100644
--- a/base/testfiles-lthooks/lthooks-027.tlg
+++ b/base/testfiles-lthooks/lthooks-027.tlg
@@ -17,9 +17,10 @@ The hook 'A':
 ============================================================
 TEST 2:  Braces in hook names (expect 'aaa bbb'): 
 ============================================================
+aaa bbb
 The hook '-{hook}-':
  Code chunks:
-    top-level -> \message {aaa}\message {bbb}
+    top-level -> \message {aaa}
  Extra code next invocation:
     ---
  Rules:
@@ -38,9 +39,10 @@ File: after
 ============================================================
 TEST 4:  Unicode hook names (expect 'aaa bbb'): 
 ============================================================
+aaa bbb
 The hook 'gr^^c3^^bc^^c3^^9fe':
  Code chunks:
-    top-level -> \message {aaa}\message {bbb}
+    top-level -> \message {aaa}
  Extra code next invocation:
     ---
  Rules:
@@ -51,9 +53,10 @@ The hook 'gr^^c3^^bc^^c3^^9fe':
 ============================================================
 TEST 5:  Macros in hook names (expect 'aaa bbb'): 
 ============================================================
+aaa bbb
 The hook '^^e9^^89^^a4':
  Code chunks:
-    top-level -> \message {aaa}\message {bbb}
+    top-level -> \message {aaa}
  Extra code next invocation:
     ---
  Rules:
@@ -64,9 +67,10 @@ The hook '^^e9^^89^^a4':
 ============================================================
 TEST 6:  Spaces aren't trimmed if hidden in a macro (expect 'aaa bbb'): 
 ============================================================
+aaa bbb
 The hook ' <-with spaces-> ':
  Code chunks:
-    top-level -> \message {aaa}\message {bbb}
+    top-level -> \message {aaa}
  Extra code next invocation:
     ---
  Rules:





More information about the latex3-commits mailing list.