Skip to content

Commit

Permalink
Merge code for \hook_provide:n and \hook_provide_reversed:n
Browse files Browse the repository at this point in the history
  • Loading branch information
PhelypeOleinik committed Apr 30, 2021
1 parent 4b57ed9 commit 551e9f7
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 62 deletions.
89 changes: 32 additions & 57 deletions base/lthooks.dtx
Expand Up @@ -2462,7 +2462,9 @@
% \end{macro}
% \end{macro}
%
% \begin{macro}{\hook_provide:n,\@@_provide:n}
% \begin{macro}{\hook_provide:n}
% \begin{macro}{\hook_provide_reversed:n}
% \begin{macro}{\@@_provide:n}
% The \cs{hook_provide:n} declaration declares a new hook and expects
% the hook \meta{name} as its argument, e.g.,
% \hook{begindocument}.
Expand All @@ -2473,70 +2475,41 @@
%
% \begin{macrocode}
\cs_new_protected:Npn \hook_provide:n #1
{ \@@_normalize_hook_args:Nn \@@_provide:n {#1} }
{ \@@_normalize_hook_args:Nn \@@_provide:nn {#1} { } }
\cs_new_protected:Npn \hook_provide_reversed:n #1
{ \@@_normalize_hook_args:Nn \@@_provide:nn {#1} { - } }
% \end{macrocode}
%
% \begin{macrocode}
\cs_new_protected:Npn \@@_provide:n #1
\cs_new_protected:Npn \@@_provide:nn #1 #2
{
% \end{macrocode}
% We check if the hook was already \emph{explicitly} declared with
% \cs{hook_new:n}, and if it already exists we complain, otherwise set
% the \enquote{created} flag for the hook so that it errors next time
% \cs{hook_new:n} is used.
% \begin{macrocode}
\@@_if_disabled:nTF {#1}
{ \__kernel_msg_warning:nnn { hooks } { provide-disabled } {#1} }
{ \@@_if_declared:nTF {#1}
{
\@@_if_reversed:nT {#1}
{ \__kernel_msg_error:nnnnn { hooks } { provide-error }
{#1} {normal} {reversed} }
}
{
\tl_new:c { g_@@_#1_declared_tl }
\@@_make_usable:n {#1}
}
}
{ \__kernel_msg_warning:nnn { hooks } { provide-disabled } {#1} }
{
\@@_if_declared:nTF {#1}
{
\str_if_eq:eeF { \tl_use:c { g_@@_#1_reversed_tl } } {#2}
{ \__kernel_msg_error:nnn { hooks } { provide-error } {#1} }
}
{
\tl_new:c { g_@@_#1_declared_tl }
\@@_make_usable:n {#1}
\tl_gset:cn { g_@@_#1_reversed_tl } {#2}
}
}
}
% \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
%
%
% \begin{macro}{\hook_provide_reversed:n,\@@_provide_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_provide_reversed:n #1
{ \@@_normalize_hook_args:Nn \@@_provide_reversed:n {#1} }
\cs_new_protected:Npn \@@_provide_reversed:n #1
{
\@@_if_disabled:nTF {#1}
{ \__kernel_msg_warning:nnn { hooks } { provide-disabled } {#1} }
{ \@@_if_declared:nTF {#1}
{
\@@_if_reversed:nF {#1}
{ \__kernel_msg_error:nnnnn { hooks } { provide-error }
{#1} {reversed} {normal} }
}
{
\@@_provide:n {#1}
\tl_gset:cn { g_@@_#1_reversed_tl } { - }
}
}
}
% \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\hook_provide_pair:nn}
% \begin{macro}{\hook_provide_pair:nn}
% A shorthand for providing a normal and a (matching) reversed hook in one go.
% \begin{macrocode}
\cs_new_protected:Npn \hook_provide_pair:nn #1#2
{ \hook_provide:n {#1} \hook_provide_reversed:n {#2} }
% \end{macrocode}
% \end{macro}
%
% \begin{macrocode}
%<latexrelease>\EndIncludeInRelease
Expand All @@ -2552,7 +2525,6 @@
%<latexrelease>
%<latexrelease>\EndIncludeInRelease
% \end{macrocode}
% \end{macro}
%
%
% \subsection{Parsing a label}
Expand Down Expand Up @@ -4678,19 +4650,22 @@
% \begin{macrocode}
\__kernel_msg_new:nnn { hooks } { provide-disabled }
{
Can't~ provide~ hook~ #1~ as~ it~ was~ disabled!
Can't~ provide~ hook~ #1~ because~ it~ is~ disabled!
}
% \end{macrocode}
%
% \begin{macrocode}
\__kernel_msg_new:nnnn { hooks } { provide-error }
{
Hook~ #1~ already~ declared~ as~ a~ #3~ hook!
Hook~ #1~ already~ declared~ as~ a~
\@@_if_reversed:nTF {#1} { reversed } { normal }~ hook!
}
{
You~ attempted~ to~ provide~ the~ hook~ #1~ as~ #2~ hook,~ but~ it~
was~ already~ previously~ declared~ as~ a~ #3~ hook.~
A~ redeclaration~ is~ not~ possible.
You~ attempted~ to~ provide~ the~ hook~ #1~ as~ a~
\@@_if_reversed:nTF {#1} { normal } { reversed }~ hook,~ but~ it~
was~ already~ previously~ declared~ as~ a~
\@@_if_reversed:nTF {#1} { reversed } { normal }~ hook.~
A~ redeclaration~ is~ not~ possible.
}
% \end{macrocode}
%
Expand Down
10 changes: 5 additions & 5 deletions base/testfiles-lthooks/lthooks-029.tlg
Expand Up @@ -91,11 +91,11 @@ l. ...\ShowHook{ccc}
<recently read> }
l. ...\ShowHook{ccc}
------------ provide an reversed hook on an existing normal hook --------------
! LaTeX3 Error: Hook aaa already declared as a normal hook!
! LaTeX3 Error: Hook `aaa' already declared as a normal hook!
For immediate help type H <return>.
...
l. ...\ProvideReversedHook{aaa}
You attempted to provide the hook aaa as reversed hook, but it was already
You attempted to provide the hook `aaa' as a reversed hook, but it was already
previously declared as a normal hook. A redeclaration is not possible.
-> The hook 'aaa':
> Code chunks:
Expand All @@ -111,11 +111,11 @@ previously declared as a normal hook. A redeclaration is not possible.
<recently read> }
l. ...\ShowHook{aaa}
------------ provide an normal hook on an existing reversed hook --------------
! LaTeX3 Error: Hook ccc already declared as a reversed hook!
! LaTeX3 Error: Hook `ccc' already declared as a reversed hook!
For immediate help type H <return>.
...
l. ...\ProvideHook{ccc}
You attempted to provide the hook ccc as normal hook, but it was already
You attempted to provide the hook `ccc' as a normal hook, but it was already
previously declared as a reversed hook. A redeclaration is not possible.
-> The hook 'ccc':
> Code chunks:
Expand All @@ -131,7 +131,7 @@ previously declared as a reversed hook. A redeclaration is not possible.
<recently read> }
l. ...\ShowHook{ccc}
------------ provide hook that was disabled --------------
LaTeX3 Warning: Can't provide hook ddd as it was disabled!
LaTeX3 Warning: Can't provide hook `ddd' because it is disabled!
-> The hook 'ddd':
> The hook is not declared.
> The hook is disabled.
Expand Down

0 comments on commit 551e9f7

Please sign in to comment.