Skip to content

Commit

Permalink
Gh1399 Trim spaces from envname (#1401)
Browse files Browse the repository at this point in the history
* Trim spaces from envname first (#1399)

* Extend ltcmd001 tests

* Add rollback

* Add ltnews entry

* Correct fun. names in {macro} markup
  • Loading branch information
muzimuzhi authored Jul 6, 2024
1 parent 7c96b6b commit 7e2f1a1
Show file tree
Hide file tree
Showing 42 changed files with 325 additions and 29 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 @@ to completeness or accuracy and it contains some references to files that are
not part of the distribution.
================================================================================

2024-07-06 Yukai Chou <[email protected]>

* ltcmd.dtx (subsection{User functions}):
Trim spaces for envname before existence check (gh/1399)
(subsection{Declaring commands and environments}):
Simplify \__cmd_declare_env:nnnn, use space-trimmed envname directly

2024-06-23 Yukai Chou <[email protected]>

* ltpara.dtx
Expand Down
18 changes: 18 additions & 0 deletions base/doc/ltnews40.tex
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,24 @@ \subsection{Fix wrong file type in a rollback warning}
%
\githubissue{870}

\subsection*{Fix existence check of document environments}
\cs{NewDocumentEnvironment} and friends define (or redefine) a document
environment using the space-trimmed \meta{envname}, but the existence
check for \meta{envname} was done without space trimming. Thus when the
user-specified \meta{envname} consists of leading and/or trailing
space(s), it may lead to erroneously silent environment declaration.
For example, in
\begin{verbatim}
\NewDocumentEnvironment{myenv}{}{begin}{end}
\NewDocumentEnvironment{ myenv }{}{begin}{end}
\end{verbatim}
the first line defines a new environment \env{myenv} but the second
line would check existence for \env{ myenv } (which is not yet defined),
then redefine \env{myenv} environment without raising any errors.
This has now been corrected.
%
\githubissue{1399}

\subsection{\pkg{doc}:\ \cs{PrintDescribeMacro} in preamble}

In \pkg{doc} version 2 it was possible alter the definition of
Expand Down
152 changes: 123 additions & 29 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.2e}
\def\ltcmddate{2024-04-17}
\def\ltcmdversion{v1.2f}
\def\ltcmddate{2024-07-06}
% \end{macrocode}
%
%<*driver>
Expand Down Expand Up @@ -620,31 +620,51 @@
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@@_declare_env:nnnn}
% \begin{macro}{\@@_declare_env:nnnn, \@@_declare_env:ennn}
% \changes{v1.2f}{2024/07/06}{Use space-trimmed envname directly}
% \begin{macro}{\@@_declare_env_internal:nnnn}
% \begin{macro}{\@@_set_environment_end:n}
% The lead-off to creating an environment is much the same as that for
% creating a command: issue the appropriate message, store the argument
% specification then hand off to an internal function.
% \begin{macrocode}
%<latexrelease>\IncludeInRelease{2024/11/01}{\@@_declare_env:nnnn}%
%<latexrelease> {Use~space-trimmed~envname~directly}
\cs_new_protected:Npn \@@_declare_env:nnnn #1#2
{
\str_set:Nx \l_@@_environment_str {#1}
\str_set:Nx \l_@@_environment_str
{ \tl_trim_spaces:o { \l_@@_environment_str } }
\cs_if_exist:cTF { \l_@@_environment_str }
{
\msg_info:nnxx { cmd } { redefine-env }
{ \l_@@_environment_str } { \tl_to_str:n {#2} }
}
{
\msg_info:nnxx { cmd } { define-env }
{ \l_@@_environment_str } { \tl_to_str:n {#2} }
}
\str_set:Nn \l_@@_environment_str {#1}
\cs_if_exist:cTF { #1 }
{ \msg_info:nnnn { cmd } { redefine-env } { #1 } { #2 } }
{ \msg_info:nnnn { cmd } { define-env } { #1 } { #2 } }
\bool_set_false:N \l_@@_expandable_bool
\bool_set_true:N \l_@@_environment_bool
\exp_args:NV \@@_declare_env_internal:nnnn
\l_@@_environment_str {#2}
\@@_declare_env_internal:nnnn {#1} {#2}
}
\cs_generate_variant:Nn \@@_declare_env:nnnn { e }
%<latexrelease>\EndIncludeInRelease
%<latexrelease>\IncludeInRelease{2024/06/01}{\@@_declare_env:nnnn}%
%<latexrelease> {Use~space-trimmed~envname~directly}
%<latexrelease>\cs_new_protected:Npn \@@_declare_env:nnnn #1#2
%<latexrelease> {
%<latexrelease> \str_set:Nx \l_@@_environment_str {#1}
%<latexrelease> \str_set:Nx \l_@@_environment_str
%<latexrelease> { \tl_trim_spaces:o { \l_@@_environment_str } }
%<latexrelease> \cs_if_exist:cTF { \l_@@_environment_str }
%<latexrelease> {
%<latexrelease> \msg_info:nnxx { cmd } { redefine-env }
%<latexrelease> { \l_@@_environment_str } { \tl_to_str:n {#2} }
%<latexrelease> }
%<latexrelease> {
%<latexrelease> \msg_info:nnxx { cmd } { define-env }
%<latexrelease> { \l_@@_environment_str } { \tl_to_str:n {#2} }
%<latexrelease> }
%<latexrelease> \bool_set_false:N \l_@@_expandable_bool
%<latexrelease> \bool_set_true:N \l_@@_environment_bool
%<latexrelease> \exp_args:NV \@@_declare_env_internal:nnnn
%<latexrelease> \l_@@_environment_str {#2}
%<latexrelease> }
%<latexrelease>
%<latexrelease>\EndIncludeInRelease
% \end{macrocode}
% Creating a document environment requires a few more steps than creating
% a single command. In order to pass the arguments of the command to the
Expand Down Expand Up @@ -685,6 +705,7 @@
% \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \subsection{Structure of \pkg{xparse} commands}
%
Expand Down Expand Up @@ -5022,36 +5043,109 @@
%
% \begin{macro}{\NewDocumentEnvironment}
% \changes{v1.0h}{2021/08/27}{Check for end-of-environment command}
% \changes{v1.0h}{2024/07/06}{Trim spaces from envname first}
% \begin{macro}{\RenewDocumentEnvironment}
% \begin{macro}{\ProvideDocumentEnvironment}
% \begin{macro}{\DeclareDocumentEnvironment}
% Very similar for environments.
% \begin{macrocode}
% \begin{macro}{\@@_new_env:nnnn, \@@_renew_env:nnnn, \@@_provide_env:nnnn}
% \begin{macro}{\@@_new_env:ennn, \@@_renew_env:ennn, \@@_provide_env:ennn}
% Very similar for environments. Trim spaces from user-specified
% \meta{envname}, do existence check then hand off to
% \cs{@@_declare_env:nnnn}.
% \begin{macrocode}
%<latexrelease>\IncludeInRelease{2024/11/01}{\NewDocumentEnvironment}%
%<latexrelease> {Trim~spaces~from~envname~first}
\cs_new_protected:Npn \NewDocumentEnvironment #1#2#3#4
{
\@@_new_env:ennn { \tl_trim_spaces:e {#1} } {#2} {#3} {#4}
}
\cs_new_protected:Npn \RenewDocumentEnvironment #1#2#3#4
{
\@@_renew_env:ennn { \tl_trim_spaces:e {#1} } {#2} {#3} {#4}
}
\cs_new_protected:Npn \ProvideDocumentEnvironment #1#2#3#4
{
\@@_provide_env:ennn { \tl_trim_spaces:e {#1} } {#2} {#3} {#4}
}
\cs_new_protected:Npn \DeclareDocumentEnvironment #1#2#3#4
{
\@@_declare_env:ennn { \tl_trim_spaces:e {#1} } {#2} {#3} {#4}
}
% \end{macrocode}
% Each of \cs[no-index]{@@_(new|renew|provide)_env:nnnn} is curried.
% \begin{macrocode}
\cs_new_protected:Npn \@@_new_env:nnnn #1
{
\cs_if_exist:cTF {#1}
{ \msg_error:nnx { cmd } { env-already-defined } {#1} }
{
\msg_error:nnx { cmd } { env-already-defined } {#1}
\use_none:nnn
}
{
\cs_if_exist:cTF { end #1 }
{ \msg_error:nnx { cmd } { env-end-already-defined } {#1} }
{ \@@_declare_env:nnnn {#1} {#2} {#3} {#4} }
{
\msg_error:nnx { cmd } { env-end-already-defined } {#1}
\use_none:nnn
}
{ \@@_declare_env:nnnn {#1} }
}
}
\cs_new_protected:Npn \RenewDocumentEnvironment #1#2#3#4
\cs_new_protected:Npn \@@_renew_env:nnnn #1
{
\cs_if_exist:cTF {#1}
{ \@@_declare_env:nnnn {#1} {#2} {#3} {#4} }
{ \msg_error:nnx { cmd } { env-undefined } {#1} }
{ \@@_declare_env:nnnn {#1} }
{
\msg_error:nnx { cmd } { env-undefined } {#1}
\use_none:nnn
}
}
\cs_new_protected:Npn \ProvideDocumentEnvironment #1#2#3#4
{ \cs_if_exist:cF {#1} { \@@_declare_env:nnnn {#1} {#2} {#3} {#4} } }
\cs_new_protected:Npn \DeclareDocumentEnvironment #1#2#3#4
{ \@@_declare_env:nnnn {#1} {#2} {#3} {#4} }
\cs_new_protected:Npn \@@_provide_env:nnnn #1
{
\cs_if_exist:cTF {#1}
{ \use_none:nnn }
{ \@@_declare_env:nnnn {#1} }
}
\cs_generate_variant:Nn \@@_new_env:nnnn { e }
\cs_generate_variant:Nn \@@_renew_env:nnnn { e }
\cs_generate_variant:Nn \@@_provide_env:nnnn { e }
%<latexrelease>\EndIncludeInRelease
%<latexrelease>\IncludeInRelease{2024/06/01}{\NewDocumentEnvironment}%
%<latexrelease> {Trim~spaces~from~envname~first}
%<latexrelease>\cs_new_protected:Npn \NewDocumentEnvironment #1#2#3#4
%<latexrelease> {
%<latexrelease> \cs_if_exist:cTF {#1}
%<latexrelease> { \msg_error:nnx { cmd } { env-already-defined } {#1} }
%<latexrelease> {
%<latexrelease> \cs_if_exist:cTF { end #1 }
%<latexrelease> { \msg_error:nnx { cmd } { env-end-already-defined } {#1} }
%<latexrelease> { \@@_declare_env:nnnn {#1} {#2} {#3} {#4} }
%<latexrelease> }
%<latexrelease> }
%<latexrelease>\cs_new_protected:Npn \RenewDocumentEnvironment #1#2#3#4
%<latexrelease> {
%<latexrelease> \cs_if_exist:cTF {#1}
%<latexrelease> { \@@_declare_env:nnnn {#1} {#2} {#3} {#4} }
%<latexrelease> { \msg_error:nnx { cmd } { env-undefined } {#1} }
%<latexrelease> }
%<latexrelease>\cs_new_protected:Npn \ProvideDocumentEnvironment #1#2#3#4
%<latexrelease> { \cs_if_exist:cF {#1} { \@@_declare_env:nnnn {#1} {#2} {#3} {#4} } }
%<latexrelease>\cs_new_protected:Npn \DeclareDocumentEnvironment #1#2#3#4
%<latexrelease> { \@@_declare_env:nnnn {#1} {#2} {#3} {#4} }
%<latexrelease>\cs_undefine:N \@@_new_env:nnnn
%<latexrelease>\cs_undefine:N \@@_new_env:ennn
%<latexrelease>\cs_undefine:N \@@_renew_env:nnnn
%<latexrelease>\cs_undefine:N \@@_renew_env:ennn
%<latexrelease>\cs_undefine:N \@@_provide_env:nnnn
%<latexrelease>\cs_undefine:N \@@_provide_env:ennn
%<latexrelease>
%<latexrelease>\EndIncludeInRelease
% \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\NewExpandableDocumentCommand}
% \begin{macro}{\RenewExpandableDocumentCommand}
Expand Down
3 changes: 3 additions & 0 deletions base/testfiles-ltcmd/ltcmd001.lvt
Original file line number Diff line number Diff line change
Expand Up @@ -222,15 +222,18 @@
\DeclareDocumentEnvironment { foo } { } { } { }
\TIMO
\NewDocumentEnvironment { foo } { } { } { }
\NewDocumentEnvironment { ~foo } { } { } { } % see gh1399
\OMIT
\cs_undefine:N \environment_begin_foo:w
\cs_undefine:N \foo
\TIMO
\RenewDocumentEnvironment { foo } { } { } { }
\RenewDocumentEnvironment { foo~ } { } { } { }
\OMIT
\DeclareDocumentEnvironment { foo } { } { First } { First }
\TIMO
\ProvideDocumentEnvironment { foo } { } { Second } { Second }
\ProvideDocumentEnvironment { ~foo~ } { } { Second } { Second }
\xparse_show_env:n { foo }
}

Expand Down
14 changes: 14 additions & 0 deletions base/testfiles-ltcmd/ltcmd001.tlg
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,20 @@ l. ...}
You have used \NewDocumentEnvironment with an environment that already has a
definition.
The existing definition of 'foo' will not be altered.
! LaTeX cmd Error: Environment 'foo' already defined.
For immediate help type H <return>.
...
l. ...}
You have used \NewDocumentEnvironment with an environment that already has a
definition.
The existing definition of 'foo' will not be altered.
! LaTeX cmd Error: Environment 'foo' undefined.
For immediate help type H <return>.
...
l. ...}
You have used \RenewDocumentEnvironment with an environment that was never
defined.
LaTeX will ignore this entire definition.
! LaTeX cmd Error: Environment 'foo' undefined.
For immediate help type H <return>.
...
Expand Down
4 changes: 4 additions & 0 deletions base/testfiles-lthooks/lthooks-rollback-args.tlg
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ Already applied: [....-..-..] Make various commands robust on input line ....
BEGIN module: ltcmd (....-..-..) on input line ....
Document command parser.
Skipping module ltcmd on input line ....
Skipping: [....-..-..] Use space-trimmed envname directly on input line ....
Skipping: [....-..-..] Use space-trimmed envname directly on input line ....
Skipping: [....-..-..] Support \NewCommandCopy in ltcmd on input line ....
Applying: [....-..-..] Support \NewCommandCopy in ltcmd on input line ....
Skipping: [....-..-..] Distinguish non-expandable document commands on input line ....
Expand All @@ -116,6 +118,8 @@ Skipping: [....-..-..] Support \ShowCommand in ltcmd on input line ....
Applying: [....-..-..] Support \ShowCommand in ltcmd on input line ....
Skipping: [....-..-..] Endlines as \obeyedline on input line ....
Applying: [....-..-..] Endlines as \obeyedline on input line ....
Skipping: [....-..-..] Trim spaces from envname first on input line ....
Skipping: [....-..-..] Trim spaces from envname first on input line ....
Skipping: [....-..-..] Testing for empty or blank on input line ....
Skipping: [....-..-..] Testing for empty or blank on input line ....
Skipping: [....-..-..] Document command parser on input line ....
Expand Down
4 changes: 4 additions & 0 deletions base/testfiles/github-0479-often.luatex.tlg
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ Already applied: [....-..-..] Make various commands robust on input line ....
BEGIN module: ltcmd (....-..-..) on input line ....
Document command parser.
Skipping module ltcmd on input line ....
Skipping: [....-..-..] Use space-trimmed envname directly on input line ....
Skipping: [....-..-..] Use space-trimmed envname directly on input line ....
Skipping: [....-..-..] Support \NewCommandCopy in ltcmd on input line ....
Applying: [....-..-..] Support \NewCommandCopy in ltcmd on input line ....
Skipping: [....-..-..] Distinguish non-expandable document commands on input line ....
Expand All @@ -126,6 +128,8 @@ Skipping: [....-..-..] Support \ShowCommand in ltcmd on input line ....
Applying: [....-..-..] Support \ShowCommand in ltcmd on input line ....
Skipping: [....-..-..] Endlines as \obeyedline on input line ....
Applying: [....-..-..] Endlines as \obeyedline on input line ....
Skipping: [....-..-..] Trim spaces from envname first on input line ....
Skipping: [....-..-..] Trim spaces from envname first on input line ....
Skipping: [....-..-..] Testing for empty or blank on input line ....
Skipping: [....-..-..] Testing for empty or blank on input line ....
Skipping: [....-..-..] Document command parser on input line ....
Expand Down
4 changes: 4 additions & 0 deletions base/testfiles/github-0479-often.tlg
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ Already applied: [....-..-..] Make various commands robust on input line ....
BEGIN module: ltcmd (....-..-..) on input line ....
Document command parser.
Skipping module ltcmd on input line ....
Skipping: [....-..-..] Use space-trimmed envname directly on input line ....
Skipping: [....-..-..] Use space-trimmed envname directly on input line ....
Skipping: [....-..-..] Support \NewCommandCopy in ltcmd on input line ....
Applying: [....-..-..] Support \NewCommandCopy in ltcmd on input line ....
Skipping: [....-..-..] Distinguish non-expandable document commands on input line ....
Expand All @@ -116,6 +118,8 @@ Skipping: [....-..-..] Support \ShowCommand in ltcmd on input line ....
Applying: [....-..-..] Support \ShowCommand in ltcmd on input line ....
Skipping: [....-..-..] Endlines as \obeyedline on input line ....
Applying: [....-..-..] Endlines as \obeyedline on input line ....
Skipping: [....-..-..] Trim spaces from envname first on input line ....
Skipping: [....-..-..] Trim spaces from envname first on input line ....
Skipping: [....-..-..] Testing for empty or blank on input line ....
Skipping: [....-..-..] Testing for empty or blank on input line ....
Skipping: [....-..-..] Document command parser on input line ....
Expand Down
4 changes: 4 additions & 0 deletions base/testfiles/github-0479-often.xetex.tlg
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ Already applied: [....-..-..] Make various commands robust on input line ....
BEGIN module: ltcmd (....-..-..) on input line ....
Document command parser.
Skipping module ltcmd on input line ....
Skipping: [....-..-..] Use space-trimmed envname directly on input line ....
Skipping: [....-..-..] Use space-trimmed envname directly on input line ....
Skipping: [....-..-..] Support \NewCommandCopy in ltcmd on input line ....
Applying: [....-..-..] Support \NewCommandCopy in ltcmd on input line ....
Skipping: [....-..-..] Distinguish non-expandable document commands on input line ....
Expand All @@ -116,6 +118,8 @@ Skipping: [....-..-..] Support \ShowCommand in ltcmd on input line ....
Applying: [....-..-..] Support \ShowCommand in ltcmd on input line ....
Skipping: [....-..-..] Endlines as \obeyedline on input line ....
Applying: [....-..-..] Endlines as \obeyedline on input line ....
Skipping: [....-..-..] Trim spaces from envname first on input line ....
Skipping: [....-..-..] Trim spaces from envname first on input line ....
Skipping: [....-..-..] Testing for empty or blank on input line ....
Skipping: [....-..-..] Testing for empty or blank on input line ....
Skipping: [....-..-..] Document command parser on input line ....
Expand Down
8 changes: 8 additions & 0 deletions base/testfiles/tlb-latexrelease-rollback-003-often.luatex.tlg
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ Applying: [....-..-..] Make various commands robust on input line ....
BEGIN module: ltcmd (....-..-..) on input line ....
Document command parser.
Skipping module ltcmd on input line ....
Skipping: [....-..-..] Use space-trimmed envname directly on input line ....
Skipping: [....-..-..] Use space-trimmed envname directly on input line ....
Skipping: [....-..-..] Support \NewCommandCopy in ltcmd on input line ....
Skipping: [....-..-..] Support \NewCommandCopy in ltcmd on input line ....
Skipping: [....-..-..] Distinguish non-expandable document commands on input line ....
Expand All @@ -108,6 +110,8 @@ Skipping: [....-..-..] Support \ShowCommand in ltcmd on input line ....
Skipping: [....-..-..] Support \ShowCommand in ltcmd on input line ....
Skipping: [....-..-..] Endlines as \obeyedline on input line ....
Skipping: [....-..-..] Endlines as \obeyedline on input line ....
Skipping: [....-..-..] Trim spaces from envname first on input line ....
Skipping: [....-..-..] Trim spaces from envname first on input line ....
Skipping: [....-..-..] Testing for empty or blank on input line ....
Skipping: [....-..-..] Testing for empty or blank on input line ....
Applying: [....-..-..] Document command parser on input line ....
Expand Down Expand Up @@ -885,6 +889,8 @@ Already applied: [....-..-..] Make various commands robust on input line ....
BEGIN module: ltcmd (....-..-..) on input line ....
Document command parser.
Skipping module ltcmd on input line ....
Skipping: [....-..-..] Use space-trimmed envname directly on input line ....
Skipping: [....-..-..] Use space-trimmed envname directly on input line ....
Skipping: [....-..-..] Support \NewCommandCopy in ltcmd on input line ....
Skipping: [....-..-..] Support \NewCommandCopy in ltcmd on input line ....
Skipping: [....-..-..] Distinguish non-expandable document commands on input line ....
Expand All @@ -901,6 +907,8 @@ Skipping: [....-..-..] Support \ShowCommand in ltcmd on input line ....
Skipping: [....-..-..] Support \ShowCommand in ltcmd on input line ....
Skipping: [....-..-..] Endlines as \obeyedline on input line ....
Skipping: [....-..-..] Endlines as \obeyedline on input line ....
Skipping: [....-..-..] Trim spaces from envname first on input line ....
Skipping: [....-..-..] Trim spaces from envname first on input line ....
Skipping: [....-..-..] Testing for empty or blank on input line ....
Skipping: [....-..-..] Testing for empty or blank on input line ....
Applying: [....-..-..] Document command parser on input line ....
Expand Down
Loading

0 comments on commit 7e2f1a1

Please sign in to comment.