[latex3-commits] [git/LaTeX3-latex3-latex2e] lthooks2: Clear the next_code token list once and before the code (8b97aee2)
PhelypeOleinik
tex.phelype at gmail.com
Sat Aug 22 22:46:13 CEST 2020
Repository : https://github.com/latex3/latex2e
On branch : lthooks2
Link : https://github.com/latex3/latex2e/commit/8b97aee24e20c3aa577401c9b7e7428f9ee9147a
>---------------------------------------------------------------
commit 8b97aee24e20c3aa577401c9b7e7428f9ee9147a
Author: PhelypeOleinik <tex.phelype at gmail.com>
Date: Sat Aug 22 17:46:13 2020 -0300
Clear the next_code token list once and before the code
>---------------------------------------------------------------
8b97aee24e20c3aa577401c9b7e7428f9ee9147a
base/lthooks.dtx | 25 +++++++++++++++++++-----
base/testfiles-lthooks/lthooks-009.lvt | 17 +++++++++++++++++
base/testfiles-lthooks/lthooks-009.tlg | 35 ++++++++++++++++++++++++++++++++++
base/testfiles-lthooks/lthooks-010.tlg | 6 +++++-
4 files changed, 77 insertions(+), 6 deletions(-)
diff --git a/base/lthooks.dtx b/base/lthooks.dtx
index eca2c763..54aede99 100644
--- a/base/lthooks.dtx
+++ b/base/lthooks.dtx
@@ -3120,7 +3120,7 @@
\iow_term:x{~Extra~ code~ next~ invocation:}
\iow_term:x{\@spaces
\tl_if_empty:cTF { g_@@_#1_next_code_tl }
- {---} {->~ \exp_args:NNc \exp_args:No \@@_log_next_code:n {g_@@_#1_next_code_tl} } }
+ {---} {->~ \exp_args:Nv \@@_log_next_code:n {g_@@_#1_next_code_tl} } }
% \end{macrocode}
%
% Loop through the rules in a hook and for every rule found, print it.
@@ -3168,7 +3168,7 @@
%
% \begin{macrocode}
\cs_new:Npn \@@_log_next_code:n #1 {
- \exp_args:No \tl_to_str:n{\use_none:nn #1}
+ \exp_args:No \tl_to_str:n {\use_none:nn #1}
}
% \end{macrocode}
% \end{macro}
@@ -3259,10 +3259,25 @@
{ \@@_gput_next_do:nn {#1} {#2} }
{ \@@_try_declaring_generic_next_hook:nn {#1} {#2} }
}
-\cs_new_protected:Npn \@@_gput_next_do:nn #1 #2
+\cs_new_protected:Npn \@@_gput_next_do:nn #1
{
- \tl_gput_right:cn { g_@@_#1_next_code_tl }
- { #2 \tl_gclear:c { g_@@_#1_next_code_tl } }
+ \exp_args:Nc \@@_gput_next_do:Nnn
+ { g_@@_#1_next_code_tl } {#1}
+ }
+% \end{macrocode}
+% First check if the ``next code'' token list is empty: if so we need
+% to add a \cs{tl_gclear:c} to clear it, so the code lasts for one
+% usage only. The token list is cleared early so that nested usages
+% don't get lost. \cs{tl_gclear:c} is used instead of
+% \cs{tl_gclear:N} in case the hook is used in an expansion-only
+% context, so the token list doesn't expand before \cs{tl_gclear:N}:
+% that would make an infinite loop.
+% \begin{macrocode}
+\cs_new_protected:Npn \@@_gput_next_do:Nnn #1 #2
+ {
+ \tl_if_empty:NT #1
+ { \tl_gset:Nn #1 { \tl_gclear:c { g_@@_#2_next_code_tl } } }
+ \tl_gput_right:Nn #1
}
% \end{macrocode}
% \end{macro}
diff --git a/base/testfiles-lthooks/lthooks-009.lvt b/base/testfiles-lthooks/lthooks-009.lvt
index a6bd1f1a..d232eec6 100644
--- a/base/testfiles-lthooks/lthooks-009.lvt
+++ b/base/testfiles-lthooks/lthooks-009.lvt
@@ -38,4 +38,21 @@
\typeout{Nothing?}
\textsf{E} % nothing
+\def\hello{Hello, }
+
+\NewHook{hook}
+\AddToHookNext{hook}{\hello}
+\AddToHookNext{hook}{\AddToHookNext{hook}{nested}}
+\AddToHookNext{hook}{world!}
+\ShowHook{hook}
+
+\typeout{Use 'hook': \UseHook{hook}}
+\ShowHook{hook}
+
+\typeout{and again: \UseHook{hook}}
+\ShowHook{hook}
+
+\typeout{once more: \UseHook{hook}}
+\ShowHook{hook}
+
\end{document}
diff --git a/base/testfiles-lthooks/lthooks-009.tlg b/base/testfiles-lthooks/lthooks-009.tlg
index 8da7a10e..060beb9a 100644
--- a/base/testfiles-lthooks/lthooks-009.tlg
+++ b/base/testfiles-lthooks/lthooks-009.tlg
@@ -68,6 +68,41 @@ Std sffamily code
Update code for hook 'sffamily' on input line ...:
only this code
Nothing?
+The hook 'hook':
+ Code chunks:
+ ---
+ Extra code next invocation:
+ -> \hello \AddToHookNext {hook}{nested}world!
+ Rules:
+ ---
+ Execution order:
+Use 'hook':
+The hook 'hook':
+ Code chunks:
+ ---
+ Extra code next invocation:
+ -> \hello \AddToHookNext {hook}{nested}world!
+ Rules:
+ ---
+ Execution order:
+and again:
+The hook 'hook':
+ Code chunks:
+ ---
+ Extra code next invocation:
+ -> \hello \AddToHookNext {hook}{nested}world!
+ Rules:
+ ---
+ Execution order:
+once more:
+The hook 'hook':
+ Code chunks:
+ ---
+ Extra code next invocation:
+ -> \hello \AddToHookNext {hook}{nested}world!
+ Rules:
+ ---
+ Execution order:
Absolute page = 1 (target: 1)
[1
] (lthooks-009.aux)
diff --git a/base/testfiles-lthooks/lthooks-010.tlg b/base/testfiles-lthooks/lthooks-010.tlg
index 5ab2af2a..050d969b 100644
--- a/base/testfiles-lthooks/lthooks-010.tlg
+++ b/base/testfiles-lthooks/lthooks-010.tlg
@@ -13,7 +13,7 @@ The hook 'sffamily':
foo -> foo code
bar -> bar code
Extra code next invocation:
- -> first occasion only\tl_gclear:c {g__hook_sffamily_next_code_tl}
+ -> first occasion only $\frac 12$
Rules:
foo|bar with relation >
Execution order (after applying rules):
@@ -33,6 +33,10 @@ LaTeX Font Info: Checking defaults for OMX/cmex/m/n on input line ....
LaTeX Font Info: ... okay on input line ....
LaTeX Font Info: Checking defaults for U/cmr/m/n on input line ....
LaTeX Font Info: ... okay on input line ....
+LaTeX Font Info: External font `cmex10' loaded for size
+(Font) <7> on input line ....
+LaTeX Font Info: External font `cmex10' loaded for size
+(Font) <5> on input line ....
The hook 'sffamily':
Code chunks:
legacy -> Std sffamily code
More information about the latex3-commits
mailing list.