[latex3-commits] [git/LaTeX3-latex3-latex2e] gh373: Add \@show at DeclareRobustCommand and \@show at newcommand (dbbd6356)

PhelypeOleinik tex.phelype at gmail.com
Thu Aug 20 06:04:53 CEST 2020


Repository : https://github.com/latex3/latex2e
On branch  : gh373
Link       : https://github.com/latex3/latex2e/commit/dbbd6356400c18360e1727ea609460261e010800

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

commit dbbd6356400c18360e1727ea609460261e010800
Author: PhelypeOleinik <tex.phelype at gmail.com>
Date:   Thu Aug 20 01:04:53 2020 -0300

    Add \@show at DeclareRobustCommand and \@show at newcommand


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

dbbd6356400c18360e1727ea609460261e010800
 base/ltdefns.dtx | 86 ++++++++++++++++++++++++++++++++++++++++++++++++++++----
 base/ltexpl.dtx  | 12 ++++++++
 2 files changed, 93 insertions(+), 5 deletions(-)

diff --git a/base/ltdefns.dtx b/base/ltdefns.dtx
index ab89d8e9..1c7dd3b0 100644
--- a/base/ltdefns.dtx
+++ b/base/ltdefns.dtx
@@ -1579,8 +1579,6 @@
 % \subsubsection{Commands defined with \cs{DeclareRobustCommand}}
 %
 % \begin{macro}{\@if at DeclareRobustCommand}
-% \begin{macro}{\@copy at DeclareRobustCommand}
-% \begin{macro}{\copy at kernel@robust at command}
 % \changes{v1.5j}{2020/05/09}{Added \cs{DeclareCommandCopy} (gh/239)}
 %
 %   Now that we provided a generic way to copy one macro to another, we need to
@@ -1612,7 +1610,10 @@
     \expandafter\@secondoftwo
   \fi}
 %    \end{macrocode}
+% \end{macro}
 %
+% \begin{macro}{\@copy at DeclareRobustCommand}
+% \begin{macro}{\copy at kernel@robust at command}
 %   If a command was defined by \cs{DeclareRobustCommand} (that is,
 %   \cs{@if at DeclareRobustCommand} returns true), then to make a copy of \verb=\cmd=
 %   into \verb=\foo= we define the latter such that it expands to
@@ -1644,17 +1645,48 @@
   \reserved at a}
 \def\copy at kernel@robust at command#1#2{%
   \@if at newcommand#2%
-    {\@copy at newcommand#1#2}%
-    {\declare at commandcopy@let#1#2}}
+    {\@copy at newcommand}%
+    {\declare at commandcopy@let}%
+    #1#2}
 %    \end{macrocode}
 % \end{macro}
 % \end{macro}
+%
+% \begin{macro}{\@show at DeclareRobustCommand}
+%   Showing the command is pretty simple.  This command prints the top-level
+%   expansion as \TeX's \cs{show} would, but with |robust macro:| rather than
+%   just |macro:|, then a blank line and then \cs{show} the inner command.
+%   For a macro defined with, say, |\DeclareRobustCommand\foo[1]{bar}|, it will
+%   print:
+% \begin{verbatim}
+%   > \foo=robust macro:
+%   ->\protect \foo  .
+%   
+%   > \foo =\long macro:
+%   #1->bar.
+% \end{verbatim}
+%   If the inner command is defined with an optional argument, then
+%   \cs{@show at newcommand} is also used.
+%
+%   The value of \cs{escapechar} is deliberately not enforced, so
+%   \cs{ShowCommand} behaves more like \cs{show}.
+%    \begin{macrocode}
+\def\@show at DeclareRobustCommand#1{%
+  \typeout{> \string#1=robust macro:}%
+  \typeout{->\@expl at cs@replacement at spec@@N#1.^^J}%
+  \expandafter\show at kernel@robust at command
+    \csname\@expl at cs@to at str@@N#1 \endcsname}
+\def\show at kernel@robust at command#1{%
+  \@if at newcommand#1%
+    {\@show at newcommand}%
+    {\show}%
+  #1}
+%    \end{macrocode}
 % \end{macro}
 %
 % \subsubsection{Commands defined with \cs{newcommand} (with optional argument)}
 %
 % \begin{macro}{\@if at newcommand}
-% \begin{macro}{\@copy at newcommand}
 %   A command \verb=\cmd= (or \verb*=\cmd =, if it was defined with
 %   \cs{DeclareRobustCommand}) with an optional argument will expand to
 %   \verb*=\@protected at testopt\cmd\\cmd{<opt>}=.  To check that we look at the
@@ -1675,7 +1707,9 @@
     \expandafter\@secondoftwo
   \fi}
 %    \end{macrocode}
+% \end{macro}
 %
+% \begin{macro}{\@copy at newcommand}
 %   Then, if a command \verb=\cmd= takes an optional argument, we copy it to
 %   \verb=\foo= by defining the latter to expand to
 %   \verb=\@protected at testopt\foo\\foo{<opt>}=.
@@ -1691,6 +1725,48 @@
       \csname\@backslashchar\@expl at cs@to at str@@N#2\endcsname}
 %    \end{macrocode}
 % \end{macro}
+%
+% \begin{macro}{\@show at newcommand}
+% \begin{macro}{\@show at newcommand@aux}
+%   A command being \cs{show}n here is guaranteed to have an optional argument.
+%   Start by showing the top-level expansion of the command (using \cs{typeout}
+%   to avoid TeX asking for interaction and extra context lines), then call
+%   \cs{@show at newcommand@aux} with the internal command, which contains the
+%   actual definition, and with the expansion of the command to extract the
+%   default value of the optional argument.
+%    \begin{macrocode}
+\def\@show at newcommand#1{%
+  \typeout{> \string#1=robust macro:}%
+  \typeout{->\@expl at cs@replacement at spec@@N#1.^^J}%
+  \expandafter\@show at newcommand@aux
+    \csname\@backslashchar\@expl at cs@to at str@@N#1\expandafter\endcsname
+    \expandafter{#1}}
+%    \end{macrocode}
+%
+%   For a macro defined with, say, |\newcommand\foo[1][opt]{bar}|, it will
+%   print:
+% \begin{verbatim}
+%   > \foo=robust macro:
+%   ->\@protected at testopt \foo \\foo {opt}.
+%   
+%   > \\foo=\long macro:
+%   > default #1=opt.
+%   [#1]->bar.
+% \end{verbatim}
+%   If the command was defined with \cs{DeclareRobustCommand}, then another pair
+%   of lines show the top-level expansion \verb*|\protect \foo  |.
+%
+%   The extra gymnastics with \cs{showtokens} ensures that \cs{showtokens}
+%   itself, and the internals of this macro aren't showed in the context lines.
+%    \begin{macrocode}
+\def\@show at newcommand@aux#1#2{%
+  \typeout{> \string#1=\@expl at cs@prefix at spec@@N#1macro:}%
+  \edef\reserved at a{%
+    default \string##1=\expandafter\detokenize\@gobblethree#2.^^J%
+    \@expl at cs@argument at spec@@N#1->\@expl at cs@replacement at spec@@N#1}%
+  \showtokens\expandafter\expandafter\expandafter{\expandafter\reserved at a}}
+%    \end{macrocode}
+% \end{macro}
 % \end{macro}
 %
 %   Now the rollback code.
diff --git a/base/ltexpl.dtx b/base/ltexpl.dtx
index b78ede6a..40711635 100644
--- a/base/ltexpl.dtx
+++ b/base/ltexpl.dtx
@@ -262,6 +262,15 @@
 \cs_gset_eq:NN \@expl at str@if at eq@@nnTF \str_if_eq:nnTF
 %    \end{macrocode}
 %
+% \changes{v1.2e}{2020/08/19}
+%         {Add \cs{\@expl at cs@\meta{thing}@spec@@N}
+%          for \cs{ShowCommand} (gh/373)}
+%    \begin{macrocode}
+\cs_gset_eq:NN \@expl at cs@prefix at spec@@N \cs_prefix_spec:N
+\cs_gset_eq:NN \@expl at cs@argument at spec@@N \cs_argument_spec:N
+\cs_gset_eq:NN \@expl at cs@replacement at spec@@N \cs_replacement_spec:N
+%    \end{macrocode}
+%
 %    \begin{macrocode}
 %<latexrelease>\EndIncludeInRelease
 %<latexrelease>\IncludeInRelease{0000/00/00}{expl3~2020-10-01}%
@@ -269,6 +278,9 @@
 %<latexrelease>\cs_undefine:N \@expl at tl@trim at spaces@apply@@nN
 %<latexrelease>\cs_undefine:N \@expl at cs@to at str@@N
 %<latexrelease>\cs_undefine:N \@expl at str@if at eq@@nnTF
+%<latexrelease>\cs_undefine:N \@expl at cs@prefix at spec@@N
+%<latexrelease>\cs_undefine:N \@expl at cs@argument at spec@@N
+%<latexrelease>\cs_undefine:N \@expl at cs@replacement at spec@@N
 %<latexrelease>\EndIncludeInRelease
 %</2ekernel|latexrelease>
 %    \end{macrocode}





More information about the latex3-commits mailing list.