[latex3-commits] [git/LaTeX3-latex3-latex2e] gh239: Provide also \New... \Renew... and \Provide... versions of \DeclareCommandCopy (137465ca)

PhelypeOleinik tex.phelype at gmail.com
Fri May 8 02:57:48 CEST 2020


Repository : https://github.com/latex3/latex2e
On branch  : gh239
Link       : https://github.com/latex3/latex2e/commit/137465ca6d29cf73e08104c96f7fe6e2dad2f7aa

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

commit 137465ca6d29cf73e08104c96f7fe6e2dad2f7aa
Author: PhelypeOleinik <tex.phelype at gmail.com>
Date:   Thu May 7 21:57:48 2020 -0300

    Provide also \New... \Renew... and \Provide... versions of \DeclareCommandCopy


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

137465ca6d29cf73e08104c96f7fe6e2dad2f7aa
 base/ltdefns.dtx                                   | 92 +++++++++++++++-------
 base/testfiles/github-0239b.lvt                    | 48 +++++++++++
 base/testfiles/github-0239b.tlg                    | 35 ++++++++
 .../tlb-latexrelease-rollback-003-often.luatex.tlg |  8 +-
 .../tlb-latexrelease-rollback-003-often.tlg        |  8 +-
 .../tlb-latexrelease-rollback-003-often.xetex.tlg  |  8 +-
 base/testfiles/tlb-rollback-004-often.luatex.tlg   |  4 +-
 base/testfiles/tlb-rollback-004-often.tlg          |  4 +-
 base/testfiles/tlb-rollback-004-often.xetex.tlg    |  4 +-
 9 files changed, 164 insertions(+), 47 deletions(-)

diff --git a/base/ltdefns.dtx b/base/ltdefns.dtx
index 8309822b..b7ac222a 100644
--- a/base/ltdefns.dtx
+++ b/base/ltdefns.dtx
@@ -645,7 +645,7 @@
 %  \begin{macro}{\@ifdefinable}
 %  \begin{macro}{\@@ifdefinable}
 %  \begin{macro}{\@rc at ifdefinable}
-%    Test is user is allowed to define a command.
+%    Test if user is allowed to define a command.
 %    \begin{macrocode}
 \long\def\@ifdefinable #1#2{%
       \edef\reserved at a{\expandafter\@gobble\string #1}%
@@ -1261,44 +1261,78 @@
 % \end{macro}
 %
 %
-% \begin{macro}{\DeclareCommandCopy}
-% \changes{v1.5h}{2020/05/01}{Added macro (gh/239)}
+% \begin{macro}{\NewCommandCopy,\RenewCommandCopy,
+%               \DeclareCommandCopy,\ProvideCommandCopy}
+% \changes{v1.5h}{2020/05/01}{Added \cs{DeclareCommandCopy} (gh/239)}
 %
 %   With most document level commands being robust now there is more of a
 %   requirement to have a standard way of aliasing (or copying) a command to a
 %   new name, for example to save an original definition before changing a
-%   command.  \cs{DeclareCommandCopy} is analogous to \TeX's \cs{let}, except that
-%   it copes with the different types of robust commands defined by \LaTeX's
-%   mechanisms.
+%   command.  \cs{DeclareCommandCopy} is analogous to \TeX's \cs{let}, except
+%   that it copes with the different types of robust commands defined by
+%   \LaTeX's mechanisms.
 %
 %   The current implementation is an almost exact copy of Heiko Oberdiek's
 %   \cs{LetLtxMacro}, with changed names and some simplifications thanks to
 %   $\epsilon$-\TeX.
 %    \begin{macrocode}
 %</2ekernel>
-%<latexrelease>\IncludeInRelease{2020/10/01}{\DeclareCommandCopy}{\DeclareCommandCopy}%
+%<latexrelease>\IncludeInRelease{2020-10-01}{\DeclareCommandCopy}
+%<latexrelease>{Add \NewCommandCopy, \RenewCommandCopy,
+%<latexrelease> \ProvideCommandCopy, and \DeclareCommandCopy}%
 %<*2ekernel|latexrelease>
 %    \end{macrocode}
-%   \cs{DeclareCommandCopy} just calls \cs{declare at commandcopy} with a \cs{relax}
+%   \cs{DeclareCommandCopy} just calls \cs{declare at command@copy} with \cs{relax},
 %   which serves as a prefix for definitions.  Heiko's \pkg{letltxmacro} defines
 %   a global variant of \cs{LetLtxMacro}, but since none of our definition
 %   commands have a global version, this one won't either, for consistency.
-%    \begin{macrocode}
-\def\DeclareCommandCopy{\declare at commandcopy\relax}
-%    \end{macrocode}
-%   Start by saving the current value of \cs{escapechar} and setting it to $-1$.
-%    \begin{macrocode}
-\def\declare at commandcopy#1#2#3{%
-  \edef\reserved at a{\the\escapechar}%
-  \escapechar=-1 %
+%
+%   \cs{NewCommandCopy} checks if \verb=#1= is already defined, and raises an
+%   error if so, otherwise the definition is carried out.
+%   \cs{RenewCommandCopy} does (almost) the opposite.  If the command is
+%   \emph{not} defined, then an error is raised.  But the definition is carried
+%   out anyhow, so the behaviour is consistent with \cs{renewcommand}.
+%   \cs{ProvideCommandCopy} only carries out the definition if \verb=#1= is not
+%   yet defined, otherwise it does nothing.
+%    \begin{macrocode}
+\def\NewCommandCopy#1{%
+  \declare at commandcopy#1%
+    {\@firstofone}%
+    {\@firstoftwo\@notdefinable}}
+\def\RenewCommandCopy#1{%
+  \declare at commandcopy#1%
+    {\@latex at error{Command \string#1 undefined}\@ehc
+     \@firstofone}%
+    {\@firstofone}}
+\def\ProvideCommandCopy#1{%
+  \declare at commandcopy#1%
+    {\@firstofone}%
+    {\@gobble}}
+\def\DeclareCommandCopy#1{%
+  \declare at commandcopy#1%
+    {\@firstofone}%
+    {\@firstofone}}
+%    \end{macrocode}
+%   Start by saving the current value of \cs{escapechar} and setting it to $-1$,
+%   then check if the command is already defined.  The proper action, if the
+%   command is defined or not is taken by each specific command above.
+%    \begin{macrocode}
+\def\declare at commandcopy#1#2#3#4{%
+  \@tempcnta=\escapechar
+  \escapechar=\m at ne
+  \edef\reserved at a{\string#1}%
+  \@ifundefined\reserved at a{#2}{#3}%
+    {\declare at command@copy\relax#1#4}%
+  \escapechar=\@tempcnta}
 %    \end{macrocode}
 %   First we do some normalisation, to cover the case where \verb=#3= expands to
 %   \verb*=\protect\#3 =.  This is the case when \verb=#3= is a multi-letter
 %   control sequence defined with \cs{DeclareRobustCommand}.  In this case we
 %   copy \verb*=\#3 = into \verb*=\#2 = and then call the inner
-%   \cs{declare at command@copy} with \verb*=\#2 = and \verb*=\#3 = rather than
+%   \cs{declare@@command at copy} with \verb*=\#2 = and \verb*=\#3 = rather than
 %   \verb*=\#2= and \verb*=\#3=.
 %    \begin{macrocode}
+\def\declare at command@copy#1#2#3{%
   \edef\reserved at b{%
     \noexpand\protect
     \expandafter\noexpand\csname\string#3 \endcsname
@@ -1312,26 +1346,21 @@
     #1\expandafter\let
     \csname\string#2 \expandafter\endcsname
     \csname\string#3 \endcsname
-    \expandafter\declare at command@copy
+    \expandafter\declare@@command at copy
       \csname\string#2 \expandafter\endcsname
       \csname\string#3 \endcsname{#1}%
 %    \end{macrocode}
-%   Otherwise just call \cs{declare at command@copy} with \verb*=\#2= and \verb*=\#3=
+%   Otherwise just call \cs{declare@@command at copy} with \verb*=\#2= and \verb*=\#3=
 %   to do the heavy-lifting.
 %    \begin{macrocode}
   \else
-    \declare at command@copy{#2}{#3}{#1}%
-  \fi
-%    \end{macrocode}
-%   Reset \cs{escapechar} when we're done.
-%    \begin{macrocode}
-  \escapechar=\reserved at a\relax
-}
+    \declare@@command at copy{#2}{#3}{#1}%
+  \fi}
 %    \end{macrocode}
 %   Check if the \verb=#2= is actually a macro, otherwise just use \cs{let}.
 %    \begin{macrocode}
-\def\declare at command@copy#1#2#3{%
-  \escapechar=92 %
+\def\declare@@command at copy#1#2#3{%
+  \escapechar=`\\
   \expandafter\declare at copy@chk at parm\meaning#2:->\@nil{%
 %    \end{macrocode}
 %   Now the real fun begins.  We'll redefine (in a group) some of \LaTeX's
@@ -1475,8 +1504,13 @@
 \def\declare at copy@CarThree#1#2#3#4\@nil{#1#2#3}%
 %</2ekernel|latexrelease>
 %<latexrelease>\EndIncludeInRelease
-%<latexrelease>\IncludeInRelease{0000/00/00}{\DeclareCommandCopy}{\DeclareCommandCopy}%
+%<latexrelease>\IncludeInRelease{0000-00-00}{\DeclareCommandCopy}
+%<latexrelease>{Undefine \NewCommandCopy, \RenewCommandCopy,
+%<latexrelease> \ProvideCommandCopy, and \DeclareCommandCopy}%
+%<latexrelease>\let\NewCommandCopy\@undefined
+%<latexrelease>\let\RenewCommandCopy\@undefined
 %<latexrelease>\let\DeclareCommandCopy\@undefined
+%<latexrelease>\let\ProvideCommandCopy\@undefined
 %<latexrelease>\EndIncludeInRelease
 %<*2ekernel>
 %    \end{macrocode}
diff --git a/base/testfiles/github-0239b.lvt b/base/testfiles/github-0239b.lvt
new file mode 100644
index 00000000..4876c9e0
--- /dev/null
+++ b/base/testfiles/github-0239b.lvt
@@ -0,0 +1,48 @@
+
+\documentclass{article}
+\input{test2e}
+
+\showoutput
+
+\begin{document}
+
+\START
+
+\def\test#1#2#3{%
+  \begingroup
+    #1#2#3%
+    \typeout{\string#2 =\meaning#2|}%
+  \endgroup}
+\def\expect#1{\typeout{\detokenize{#1}}}
+
+\def\exists{exists}
+\def\another{another}
+
+% \tracingall
+\expect{\tmpa =macro:->another|}
+\test\NewCommandCopy\tmpa\another
+% \tracingnone
+
+\expect{\exists =macro:->exists| (and an error)}
+\test\NewCommandCopy\exists\another
+
+\expect{\tmpb =macro:->another| (and an error)}
+\test\RenewCommandCopy\tmpb\another
+
+\expect{\exists =macro:->another|}
+\test\RenewCommandCopy\exists\another
+
+\expect{\tmpc =macro:->another|}
+\test\ProvideCommandCopy\tmpc\another
+
+\expect{\exists =macro:->exists|}
+\test\ProvideCommandCopy\exists\another
+
+\expect{\tmpd =macro:->another|}
+\test\DeclareCommandCopy\tmpd\another
+
+\expect{\exists =macro:->another|}
+\test\DeclareCommandCopy\exists\another
+
+\end{document}
+
diff --git a/base/testfiles/github-0239b.tlg b/base/testfiles/github-0239b.tlg
new file mode 100644
index 00000000..4c181417
--- /dev/null
+++ b/base/testfiles/github-0239b.tlg
@@ -0,0 +1,35 @@
+This is a generated file for the LaTeX2e validation system.
+Don't change this file in any respect.
+\tmpa =macro:->another|
+\tmpa =macro:->another|
+\exists =macro:->exists| (and an error)
+! LaTeX Error: Command \exists already defined.
+               Or name \end... illegal, see p.192 of the manual.
+See the LaTeX manual or LaTeX Companion for explanation.
+Type  H <return>  for immediate help.
+ ...                                              
+l. ...\test\NewCommandCopy\exists\another
+Your command was ignored.
+Type  I <command> <return>  to replace it with another command,
+or  <return>  to continue without it.
+\exists =macro:->exists|
+\tmpb =macro:->another| (and an error)
+! LaTeX Error: Command tmpb undefined.
+See the LaTeX manual or LaTeX Companion for explanation.
+Type  H <return>  for immediate help.
+ ...                                              
+l. ...\test\RenewCommandCopy\tmpb\another
+Try typing  <return>  to proceed.
+If that doesn't work, type  X <return>  to quit.
+\tmpb =macro:->another|
+\exists =macro:->another|
+\exists =macro:->another|
+\tmpc =macro:->another|
+\tmpc =macro:->another|
+\exists =macro:->exists|
+\exists =macro:->exists|
+\tmpd =macro:->another|
+\tmpd =macro:->another|
+\exists =macro:->another|
+\exists =macro:->another|
+(github-0239b.aux)
diff --git a/base/testfiles/tlb-latexrelease-rollback-003-often.luatex.tlg b/base/testfiles/tlb-latexrelease-rollback-003-often.luatex.tlg
index df0862a9..62bfd0c8 100644
--- a/base/testfiles/tlb-latexrelease-rollback-003-often.luatex.tlg
+++ b/base/testfiles/tlb-latexrelease-rollback-003-often.luatex.tlg
@@ -13,8 +13,8 @@ Applying: [....-..-..] Final dot for extension on input line ....
 Skipping: [....-..-..] \MakeRobust  on input line ....
 Skipping: [....-..-..] \MakeRobust  on input line ....
 Applying: [....-..-..] \MakeRobust  on input line ....
-Skipping: [....-..-..] \DeclareCommandCopy  on input line ....
-Applying: [....-..-..] \DeclareCommandCopy  on input line ....
+Skipping: [....-..-..] Add \NewCommandCopy , \RenewCommandCopy , \ProvideCommandCopy , and \DeclareCommandCopy  on input line ....
+Applying: [....-..-..] Undefine \NewCommandCopy , \RenewCommandCopy , \ProvideCommandCopy , and \DeclareCommandCopy  on input line ....
 Applying: [....-..-..] Undo robustness on input line ....
 Skipping: [....-..-..] Leave commands undefined in \@ifundefined  on input line ....
 Applying: [....-..-..] Leave commands undefined in \@ifundefined  on input line ....
@@ -324,8 +324,8 @@ Already applied: [....-..-..] Final dot for extension on input line ....
 Applying: [....-..-..] \MakeRobust  on input line ....
 Already applied: [....-..-..] \MakeRobust  on input line ....
 Already applied: [....-..-..] \MakeRobust  on input line ....
-Skipping: [....-..-..] \DeclareCommandCopy  on input line ....
-Applying: [....-..-..] \DeclareCommandCopy  on input line ....
+Skipping: [....-..-..] Add \NewCommandCopy , \RenewCommandCopy , \ProvideCommandCopy , and \DeclareCommandCopy  on input line ....
+Applying: [....-..-..] Undefine \NewCommandCopy , \RenewCommandCopy , \ProvideCommandCopy , and \DeclareCommandCopy  on input line ....
 Applying: [....-..-..] Undo robustness on input line ....
 Applying: [....-..-..] Leave commands undefined in \@ifundefined  on input line ....
 Already applied: [....-..-..] Leave commands undefined in \@ifundefined  on input line ....
diff --git a/base/testfiles/tlb-latexrelease-rollback-003-often.tlg b/base/testfiles/tlb-latexrelease-rollback-003-often.tlg
index f12a33d4..ba5f66ee 100644
--- a/base/testfiles/tlb-latexrelease-rollback-003-often.tlg
+++ b/base/testfiles/tlb-latexrelease-rollback-003-often.tlg
@@ -11,8 +11,8 @@ Applying: [....-..-..] Final dot for extension on input line ....
 Skipping: [....-..-..] \MakeRobust  on input line ....
 Skipping: [....-..-..] \MakeRobust  on input line ....
 Applying: [....-..-..] \MakeRobust  on input line ....
-Skipping: [....-..-..] \DeclareCommandCopy  on input line ....
-Applying: [....-..-..] \DeclareCommandCopy  on input line ....
+Skipping: [....-..-..] Add \NewCommandCopy , \RenewCommandCopy , \ProvideCommandCopy , and \DeclareCommandCopy  on input line ....
+Applying: [....-..-..] Undefine \NewCommandCopy , \RenewCommandCopy , \ProvideCommandCopy , and \DeclareCommandCopy  on input line ....
 Applying: [....-..-..] Undo robustness on input line ....
 Skipping: [....-..-..] Leave commands undefined in \@ifundefined  on input line ....
 Applying: [....-..-..] Leave commands undefined in \@ifundefined  on input line ....
@@ -316,8 +316,8 @@ Already applied: [....-..-..] Final dot for extension on input line ....
 Applying: [....-..-..] \MakeRobust  on input line ....
 Already applied: [....-..-..] \MakeRobust  on input line ....
 Already applied: [....-..-..] \MakeRobust  on input line ....
-Skipping: [....-..-..] \DeclareCommandCopy  on input line ....
-Applying: [....-..-..] \DeclareCommandCopy  on input line ....
+Skipping: [....-..-..] Add \NewCommandCopy , \RenewCommandCopy , \ProvideCommandCopy , and \DeclareCommandCopy  on input line ....
+Applying: [....-..-..] Undefine \NewCommandCopy , \RenewCommandCopy , \ProvideCommandCopy , and \DeclareCommandCopy  on input line ....
 Applying: [....-..-..] Undo robustness on input line ....
 Applying: [....-..-..] Leave commands undefined in \@ifundefined  on input line ....
 Already applied: [....-..-..] Leave commands undefined in \@ifundefined  on input line ....
diff --git a/base/testfiles/tlb-latexrelease-rollback-003-often.xetex.tlg b/base/testfiles/tlb-latexrelease-rollback-003-often.xetex.tlg
index ff8acb7c..27cc2b1e 100644
--- a/base/testfiles/tlb-latexrelease-rollback-003-often.xetex.tlg
+++ b/base/testfiles/tlb-latexrelease-rollback-003-often.xetex.tlg
@@ -11,8 +11,8 @@ Applying: [....-..-..] Final dot for extension on input line ....
 Skipping: [....-..-..] \MakeRobust  on input line ....
 Skipping: [....-..-..] \MakeRobust  on input line ....
 Applying: [....-..-..] \MakeRobust  on input line ....
-Skipping: [....-..-..] \DeclareCommandCopy  on input line ....
-Applying: [....-..-..] \DeclareCommandCopy  on input line ....
+Skipping: [....-..-..] Add \NewCommandCopy , \RenewCommandCopy , \ProvideCommandCopy , and \DeclareCommandCopy  on input line ....
+Applying: [....-..-..] Undefine \NewCommandCopy , \RenewCommandCopy , \ProvideCommandCopy , and \DeclareCommandCopy  on input line ....
 Applying: [....-..-..] Undo robustness on input line ....
 Skipping: [....-..-..] Leave commands undefined in \@ifundefined  on input line ....
 Applying: [....-..-..] Leave commands undefined in \@ifundefined  on input line ....
@@ -325,8 +325,8 @@ Already applied: [....-..-..] Final dot for extension on input line ....
 Applying: [....-..-..] \MakeRobust  on input line ....
 Already applied: [....-..-..] \MakeRobust  on input line ....
 Already applied: [....-..-..] \MakeRobust  on input line ....
-Skipping: [....-..-..] \DeclareCommandCopy  on input line ....
-Applying: [....-..-..] \DeclareCommandCopy  on input line ....
+Skipping: [....-..-..] Add \NewCommandCopy , \RenewCommandCopy , \ProvideCommandCopy , and \DeclareCommandCopy  on input line ....
+Applying: [....-..-..] Undefine \NewCommandCopy , \RenewCommandCopy , \ProvideCommandCopy , and \DeclareCommandCopy  on input line ....
 Applying: [....-..-..] Undo robustness on input line ....
 Applying: [....-..-..] Leave commands undefined in \@ifundefined  on input line ....
 Already applied: [....-..-..] Leave commands undefined in \@ifundefined  on input line ....
diff --git a/base/testfiles/tlb-rollback-004-often.luatex.tlg b/base/testfiles/tlb-rollback-004-often.luatex.tlg
index f8be7e33..053bf6bf 100644
--- a/base/testfiles/tlb-rollback-004-often.luatex.tlg
+++ b/base/testfiles/tlb-rollback-004-often.luatex.tlg
@@ -10,8 +10,8 @@ Already applied: [....-..-..] Final dot for extension on input line ....
 Applying: [....-..-..] \MakeRobust  on input line ....
 Already applied: [....-..-..] \MakeRobust  on input line ....
 Already applied: [....-..-..] \MakeRobust  on input line ....
-Skipping: [....-..-..] \DeclareCommandCopy  on input line ....
-Applying: [....-..-..] \DeclareCommandCopy  on input line ....
+Skipping: [....-..-..] Add \NewCommandCopy , \RenewCommandCopy , \ProvideCommandCopy , and \DeclareCommandCopy  on input line ....
+Applying: [....-..-..] Undefine \NewCommandCopy , \RenewCommandCopy , \ProvideCommandCopy , and \DeclareCommandCopy  on input line ....
 Applying: [....-..-..] Undo robustness on input line ....
 Applying: [....-..-..] Leave commands undefined in \@ifundefined  on input line ....
 Already applied: [....-..-..] Leave commands undefined in \@ifundefined  on input line ....
diff --git a/base/testfiles/tlb-rollback-004-often.tlg b/base/testfiles/tlb-rollback-004-often.tlg
index 790c83cb..79879061 100644
--- a/base/testfiles/tlb-rollback-004-often.tlg
+++ b/base/testfiles/tlb-rollback-004-often.tlg
@@ -8,8 +8,8 @@ Already applied: [....-..-..] Final dot for extension on input line ....
 Applying: [....-..-..] \MakeRobust  on input line ....
 Already applied: [....-..-..] \MakeRobust  on input line ....
 Already applied: [....-..-..] \MakeRobust  on input line ....
-Skipping: [....-..-..] \DeclareCommandCopy  on input line ....
-Applying: [....-..-..] \DeclareCommandCopy  on input line ....
+Skipping: [....-..-..] Add \NewCommandCopy , \RenewCommandCopy , \ProvideCommandCopy , and \DeclareCommandCopy  on input line ....
+Applying: [....-..-..] Undefine \NewCommandCopy , \RenewCommandCopy , \ProvideCommandCopy , and \DeclareCommandCopy  on input line ....
 Applying: [....-..-..] Undo robustness on input line ....
 Applying: [....-..-..] Leave commands undefined in \@ifundefined  on input line ....
 Already applied: [....-..-..] Leave commands undefined in \@ifundefined  on input line ....
diff --git a/base/testfiles/tlb-rollback-004-often.xetex.tlg b/base/testfiles/tlb-rollback-004-often.xetex.tlg
index 0f2c1571..17b880b6 100644
--- a/base/testfiles/tlb-rollback-004-often.xetex.tlg
+++ b/base/testfiles/tlb-rollback-004-often.xetex.tlg
@@ -8,8 +8,8 @@ Already applied: [....-..-..] Final dot for extension on input line ....
 Applying: [....-..-..] \MakeRobust  on input line ....
 Already applied: [....-..-..] \MakeRobust  on input line ....
 Already applied: [....-..-..] \MakeRobust  on input line ....
-Skipping: [....-..-..] \DeclareCommandCopy  on input line ....
-Applying: [....-..-..] \DeclareCommandCopy  on input line ....
+Skipping: [....-..-..] Add \NewCommandCopy , \RenewCommandCopy , \ProvideCommandCopy , and \DeclareCommandCopy  on input line ....
+Applying: [....-..-..] Undefine \NewCommandCopy , \RenewCommandCopy , \ProvideCommandCopy , and \DeclareCommandCopy  on input line ....
 Applying: [....-..-..] Undo robustness on input line ....
 Applying: [....-..-..] Leave commands undefined in \@ifundefined  on input line ....
 Already applied: [....-..-..] Leave commands undefined in \@ifundefined  on input line ....





More information about the latex3-commits mailing list.