[latex3-commits] [git/LaTeX3-latex3-latex2e] make-robust: Change \MakeRobust to behave as \DeclareRobustCommand (b6430ef6)

PhelypeOleinik phelype.oleinik at latex-project.org
Wed Aug 26 00:59:27 CEST 2020


Repository : https://github.com/latex3/latex2e
On branch  : make-robust
Link       : https://github.com/latex3/latex2e/commit/b6430ef6b707fb0df227968d0fae1016fa5fd3e4

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

commit b6430ef6b707fb0df227968d0fae1016fa5fd3e4
Author: PhelypeOleinik <tex.phelype at gmail.com>
Date:   Fri Aug 21 22:20:32 2020 -0300

    Change \MakeRobust to behave as \DeclareRobustCommand
    
    And \kernel at make@fragile to revert correctly


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

b6430ef6b707fb0df227968d0fae1016fa5fd3e4
 base/ltdefns.dtx | 111 ++++++++++++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 102 insertions(+), 9 deletions(-)

diff --git a/base/ltdefns.dtx b/base/ltdefns.dtx
index 64c57e1a..0bd1411f 100644
--- a/base/ltdefns.dtx
+++ b/base/ltdefns.dtx
@@ -1211,9 +1211,11 @@
 %    at all.
 %    \begin{macrocode}
 %</2ekernel>
-%<latexrelease>\IncludeInRelease{2019/10/01}{\MakeRobust}{\MakeRobust}%
+%<latexrelease>\IncludeInRelease{2020/10/01}{\MakeRobust}{\MakeRobust}%
 %<*2ekernel|latexrelease>
 \def\MakeRobust#1{%
+  \count@=\escapechar
+  \escapechar=`\\
   \@ifundefined{\expandafter\@gobble\string#1}{%
     \@latex at error{The control sequence `\string#1' is undefined!%
       \MessageBreak There is nothing here to make robust}%
@@ -1225,12 +1227,15 @@
 % \verb*=\foo =. If it is already defined do nothing, otherwise set
 % \verb*=\foo = equal to \verb*=\foo= and redefine \verb*=\foo= so
 % that it acts like a macro defined with \verb=\DeclareRobustCommand=.
+% We use \cs{@kernel at rename@newcommand} to copy \verb*=\foo= over to
+% \verb*=\foo =, including a possible default optional argument.
 %    \begin{macrocode}
   {%
     \@ifundefined{\expandafter\@gobble\string#1\space}%
     {%
-      \global\expandafter\let\csname
-      \expandafter\@gobble\string#1\space\endcsname=#1%
+      \expandafter\@kernel at rename@newcommand
+        \csname\expandafter\@gobble\string#1\space\endcsname
+        #1%
       \edef\reserved at a{\string#1}%
       \def\reserved at b{#1}%
       \edef\reserved at b{\expandafter\strip at prefix\meaning\reserved at b}%
@@ -1242,10 +1247,72 @@
         \csname\expandafter\@gobble\string#1\space\endcsname}%
     }%
     {\@latex at info{The control sequence `\string#1' is already robust}}%
-   }%
+  }%
+  \escapechar=\count@
 }%
+%    \end{macrocode}
+%
+%  \begin{macro}{\@kernel at rename@newcommand}
+%    This macro renames a command, possibly with an optional argument (defined
+%    with \cs{newcommand}) from |#2| to |#1|, by renaming the internal macro
+%    \verb=\\#2= to \verb=\\#1= and defining \verb=\#1= appropriately, then
+%    undefining \verb=\#2= and \verb=\\#2=.  The \cs{afterassignment} trick is
+%    to make both definitions in \cs{@copy at newcommand} global (which are local
+%    by default).
+%
+%    In case the macro was defined with \cs{newcommand} and an optional
+%    argument, to replicate exactly the behaviour of \cs{DeclareRobustCommand}
+%    we have to move also the internal \verb*=\\foo= to \verb*=\\foo =.  In that
+%    case, \verb=#1= will be a parameterless macro (\cs{robust at command@chk at safe}
+%    checks that), and \cs{@if at newcommand} will return true (both defined below
+%    in this file).  If so, we can use \cs{@copy at newcommand} rather than plain
+%    \cs{let} to copy the command over.  \cs{@kernel at rename@newcommand} does
+%    this test and carries out the renaming.
+%    \begin{macrocode}
+\def\@kernel at rename@newcommand#1#2{%
+  \robust at command@chk at safe#2%
+    {\@if at newcommand#2%
+      {\afterassignment\global
+       \global\@copy at newcommand#1#2%
+       \global\let#2\@undefined
+       \global\expandafter\let\csname\string#2\endcsname\@undefined}%
+      {\global\let#1=#2}}%
+    {\global\let#1=#2}}
+%    \end{macrocode}
+%  \end{macro}
+%
+%    \begin{macrocode}
 %</2ekernel|latexrelease>
 %<latexrelease>\EndIncludeInRelease
+%
+%<latexrelease>\IncludeInRelease{2019/10/01}{\MakeRobust}{\MakeRobust}%
+%<latexrelease>\def\MakeRobust#1{%
+%<latexrelease>  \@ifundefined{\expandafter\@gobble\string#1}{%
+%<latexrelease>    \@latex at error{The control sequence `\string#1' is undefined!%
+%<latexrelease>      \MessageBreak There is nothing here to make robust}%
+%<latexrelease>    \@eha
+%<latexrelease>  }%
+%<latexrelease>  {%
+%<latexrelease>    \@ifundefined{\expandafter\@gobble\string#1\space}%
+%<latexrelease>    {%
+%<latexrelease>      \global\expandafter\let\csname
+%<latexrelease>      \expandafter\@gobble\string#1\space\endcsname=#1%
+%<latexrelease>      \edef\reserved at a{\string#1}%
+%<latexrelease>      \def\reserved at b{#1}%
+%<latexrelease>      \edef\reserved at b{\expandafter\strip at prefix\meaning\reserved at b}%
+%<latexrelease>      \xdef#1{%
+%<latexrelease>        \ifx\reserved at a\reserved at b
+%<latexrelease>          \noexpand\x at protect\noexpand#1%
+%<latexrelease>        \fi
+%<latexrelease>        \noexpand\protect\expandafter\noexpand
+%<latexrelease>        \csname\expandafter\@gobble\string#1\space\endcsname}%
+%<latexrelease>    }%
+%<latexrelease>    {\@latex at info{The control sequence `\string#1' is already robust}}%
+%<latexrelease>   }%
+%<latexrelease>}%
+%<latexrelease>\let\@kernel at rename@newcommand\@undefined
+%<latexrelease>\EndIncludeInRelease
+%
 %<latexrelease>\IncludeInRelease{2015/01/01}{\MakeRobust}{\MakeRobust}%
 %<latexrelease>\def\MakeRobust#1{%
 %<latexrelease>  \@ifundefined{\expandafter\@gobble\string#1}{%
@@ -1271,9 +1338,12 @@
 %<latexrelease>    {\@latex at info{The control sequence `\string#1' is already robust}}%
 %<latexrelease>   }%
 %<latexrelease>}%
+%<latexrelease>\let\@kernel at rename@newcommand\@undefined
 %<latexrelease>\EndIncludeInRelease
+%
 %<latexrelease>\IncludeInRelease{0000/00/00}{\MakeRobust}{\MakeRobust}%
 %<latexrelease>\let\MakeRobust\@undefined
+%<latexrelease>\let\@kernel at rename@newcommand\@undefined
 %<latexrelease>\EndIncludeInRelease
 %<*2ekernel>
 %    \end{macrocode}
@@ -1292,7 +1362,7 @@
 %    \begin{macrocode}
 %</2ekernel>
 %<*2ekernel|latexrelease>
-%<latexrelease>\IncludeInRelease{0000/00/00}%
+%<latexrelease>\IncludeInRelease{2020/10/01}%
 %<latexrelease>                 {\kernel at make@fragile}{Undo robustness}%
 \def\kernel at make@fragile#1{%
   \@ifundefined{\expandafter\@gobble\string#1\space}%
@@ -1301,16 +1371,39 @@
 %    \begin{macrocode}
      {}%
 %    \end{macrocode}
-%    Otherwise copy \verb*=\foo = back to \verb=\foo= and then undefine
-%    the payload command.
+%    Otherwise copy \verb*=\foo = back to \verb=\foo=.
+%    Then use \cs{@kernel at rename@newcommand} to check and copy
+%    \verb*=\\foo = back to \verb*=\\foo= in case the command has an optional
+%    argument.  If so, also undefine \verb*=\\foo =, and at the end undefine
+%    \verb*=\foo =.
 %    \begin{macrocode}
      {%
       \global\expandafter\let\expandafter #1\csname
-      \expandafter\@gobble\string#1\space\endcsname
+        \expandafter\@gobble\string#1\space\endcsname
+      \expandafter\@kernel at rename@newcommand
+        \csname\expandafter\@gobble\string#1\expandafter\endcsname
+        \csname\expandafter\@gobble\string#1\space\endcsname
       \global\expandafter\let\csname
-      \expandafter\@gobble\string#1\space\endcsname\@undefined
+        \expandafter\@gobble\string#1\space\endcsname\@undefined
      }%
 }
+%    \end{macrocode}
+%
+%    \begin{macrocode}
+%<latexrelease>\EndIncludeInRelease
+%
+%<latexrelease>\IncludeInRelease{0000/00/00}%
+%<latexrelease>                 {\kernel at make@fragile}{Undo robustness}%
+%<latexrelease>\def\kernel at make@fragile#1{%
+%<latexrelease>  \@ifundefined{\expandafter\@gobble\string#1\space}%
+%<latexrelease>     {}%
+%<latexrelease>     {%
+%<latexrelease>      \global\expandafter\let\expandafter #1\csname
+%<latexrelease>      \expandafter\@gobble\string#1\space\endcsname
+%<latexrelease>      \global\expandafter\let\csname
+%<latexrelease>      \expandafter\@gobble\string#1\space\endcsname\@undefined
+%<latexrelease>     }%
+%<latexrelease>}
 %<latexrelease>\EndIncludeInRelease
 %</2ekernel|latexrelease>
 %<*2ekernel>





More information about the latex3-commits mailing list.