Skip to content

Commit

Permalink
Distinguish protected ltcmds with only m-type args
Browse files Browse the repository at this point in the history
  • Loading branch information
muzimuzhi committed Mar 11, 2023
1 parent a0c7e9b commit 7cb1037
Show file tree
Hide file tree
Showing 5 changed files with 162 additions and 7 deletions.
7 changes: 7 additions & 0 deletions base/changes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ completeness or accuracy and it contains some references to files that
are not part of the distribution.
================================================================================

2023-03-11 Yukai Chou <muzimuzhi@gmail.com>

* ltcmd.dtx (subsubsection{Showing the definition of a command}),
(subsubsection{Copying a command and its internal structure}):
Correctly recognize (non-expandable) document commands with empty or only
m-type arguments (gh/1009)

2023-01-30 Frank Mittelbach <Frank.Mittelbach@latex-project.org>

* ltpara.dtx (subsection{Providing hooks for paragraphs}):
Expand Down
10 changes: 10 additions & 0 deletions base/doc/ltnews37.tex
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,16 @@ \subsection{Improve spacing at top of \env{minipages}}
\githubissue{989}
\subsection{A fix for \cs{NewCommandCopy} and \cs{ShowCommand}}
When copying and showing definitions of (non-expandable) document commands
(aka commands defined by \cs{NewDocumentCommand} and friends) containing empty
or only \texttt{m}-type arguments, these commands were wrongly recognized as
expandable ones.
This is fixed in the present \LaTeX{} release.
%
\githubissue{1009}
\section{Changes to packages in the \pkg{amsmath} category}
Expand Down
32 changes: 25 additions & 7 deletions base/ltcmd.dtx
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@
%%% From File: ltcmd.dtx
%
% \begin{macrocode}
\def\ltcmdversion{v1.1b}
\def\ltcmddate{2022-11-29}
\def\ltcmdversion{v1.1c}
\def\ltcmddate{2023-03-12}
% \end{macrocode}
%
%<*driver>
Expand Down Expand Up @@ -2123,15 +2123,22 @@
% and possibly another auxiliary
% \cs{\meta{cmd}\textvisiblespace\textvisiblespace}, if the command
% has both long and short arguments. Then, its signature also has
% several specific bits that are unique to that command; this is in contrast to
% non-expandable commands, which use a common set of parsing functions.
% several specific bits that are unique to that command; this is in contrast
% to non-expandable commands, which use a common set of parsing functions.
%
% We start by copying the basics, then call
% \cs{@@_copy_expandable_signature:NnNNNNnnn} to parse the signature
% of the command and build up the modified copy in a temporary token list,
% then we call \cs{@@_copy_expandable:NnNNNNnnn} that will copy the
% top-level definition of the command, with the proper internal
% renames.
%
% \changes{v1.1c}{2023/03/12}
% {Distinguish (non-expandable) document commands starting with
% \cs{@@_start_expandable:nNNNNn}}
% There's one variant: a command begins with \cs{@@_start_expandable:nNNNNn}
% may still be un-expandable/protected if it's defined by
% \cs{NewDocumentCommand} and friends, with empty or only m-type arguments.
% \begin{macrocode}
\cs_new_protected:Npn \@@_copy_expandable:nnNN #1 #2 #3 #4
{
Expand All @@ -2140,7 +2147,9 @@
\@@_set_eq_if_exist:cc { #1 ~ \c_space_tl } { #2 ~ \c_space_tl }
\@@_set_eq_if_exist:cc { #1 ~ defaults } { #2 ~ defaults }
\exp_after:wN \@@_copy_expandable_signature:NnNNNNnnn #4 {#1} {#2}
\cs_set_nopar:Npx #3
\token_if_protected_macro:NTF #4
{ \cs_set_protected_nopar:Npx }{ \cs_set_nopar:Npx }
#3
{ \exp_after:wN \@@_copy_expandable:NnNNNNnnn #4 {#1} {#2} }
}
\cs_new:Npn \@@_copy_expandable:NnNNNNnnn #1 #2 #3 #4 #5 #6 #7 #8 #9
Expand Down Expand Up @@ -2418,10 +2427,19 @@
}
\cs_new_protected:Npn \@@_show_expandable:N #1
{ \exp_after:wN \@@_show_expandable:NnNNNNnN #1 #1 }
% \end{macrocode}
% \changes{v1.1c}{2023/03/12}
% {Distinguish (non-expandable) document commands starting with
% \cs{@@_start_expandable:nNNNNn}}
% There's one variant: a command begins with \cs{@@_start_expandable:nNNNNn}
% may still be un-expandable/protected if it's defined by
% \cs{NewDocumentCommand} and friends, with empty or only m-type arguments.
% \begin{macrocode}
\cs_new_protected:Npn \@@_show_expandable:NnNNNNnN #1 #2 #3 #4 #5 #6 #7 #8
{
\@@_show_command_aux:NnNNn \tl_show:x
{ expandable~document~command } #8 #5 {#2}
\exp_args:NNe \@@_show_command_aux:NnNNn \tl_show:x
{ \token_if_protected_macro:NF #8 { expandable~ } document~command }
#8 #5 {#2}
}
% \end{macrocode}
%
Expand Down
44 changes: 44 additions & 0 deletions base/testfiles-ltcmd/github-1009.lvt
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
\input{regression-test}

\START

\TEST{Non-expandable Document Commands with m-type arguments}{
\DeclareDocumentCommand{\foo}{ }{}
\ShowCommand{\foo}
\DeclareCommandCopy{\baz}{\foo}
\ShowCommand{\baz}

\DeclareDocumentCommand{\foo}{ m }{}
\ShowCommand{\foo}
\DeclareCommandCopy{\baz}{\foo}
\ShowCommand{\baz}

\DeclareDocumentCommand{\foo}{ +m }{}
\ShowCommand{\foo}
\DeclareCommandCopy{\baz}{\foo}
\ShowCommand{\baz}

\DeclareDocumentCommand{\foo}{ >{\foo}m }{}
\ShowCommand{\foo}
\DeclareCommandCopy{\baz}{\foo}
\ShowCommand{\baz}
}

\TEST{Expandable Document Commands with m-type arguments}{
\DeclareExpandableDocumentCommand{\foo}{ }{}
\ShowCommand{\foo}
\DeclareCommandCopy{\baz}{\foo}
\ShowCommand{\baz}

\DeclareExpandableDocumentCommand{\foo}{ m }{}
\ShowCommand{\foo}
\DeclareCommandCopy{\baz}{\foo}
\ShowCommand{\baz}

\DeclareExpandableDocumentCommand{\foo}{ +m }{}
\ShowCommand{\foo}
\DeclareCommandCopy{\baz}{\foo}
\ShowCommand{\baz}
}

\END
76 changes: 76 additions & 0 deletions base/testfiles-ltcmd/github-1009.tlg
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
This is a generated file for the LaTeX2e validation system.
Don't change this file in any respect.
============================================================
TEST 1: Non-expandable Document Commands with m-type arguments
============================================================
> \foo=document command:
->.
<recently read> }
l. ...}
> \baz=document command:
->.
<recently read> }
l. ...}
> \foo=document command:
#1:m
->.
<recently read> }
l. ...}
> \baz=document command:
#1:m
->.
<recently read> }
l. ...}
> \foo=document command:
#1:+m
->.
<recently read> }
l. ...}
> \baz=document command:
#1:+m
->.
<recently read> }
l. ...}
> \foo=document command:
#1:>{\foo }m
->.
<recently read> }
l. ...}
> \baz=document command:
#1:>{\foo }m
->.
<recently read> }
l. ...}
============================================================
============================================================
TEST 2: Expandable Document Commands with m-type arguments
============================================================
> \foo=expandable document command:
->.
<recently read> }
l. ...}
> \baz=expandable document command:
->.
<recently read> }
l. ...}
> \foo=expandable document command:
#1:m
->.
<recently read> }
l. ...}
> \baz=expandable document command:
#1:m
->.
<recently read> }
l. ...}
> \foo=expandable document command:
#1:+m
->.
<recently read> }
l. ...}
> \baz=expandable document command:
#1:+m
->.
<recently read> }
l. ...}
============================================================

0 comments on commit 7cb1037

Please sign in to comment.