commit fece5d4f376393bd7bcf15393d8f04420053e7c1
Author: Enrico Forestieri <for...@lyx.org>
Date:   Thu Jan 7 00:57:36 2021 +0100

    Correctly terminate a user math macro in certain circumstances
    
    If a macro has only optionals and none is specified and a [ immediately
    follows, we have to terminate the macro with {}, otherwise what follows
    is taken to be an optional argument.
    
    Fixes #11665.
---
 src/mathed/InsetMathMacro.cpp |    5 ++++-
 src/mathed/MathStream.cpp     |   16 ++++++++++++++--
 src/mathed/MathStream.h       |    8 +++++++-
 3 files changed, 25 insertions(+), 4 deletions(-)

diff --git a/src/mathed/InsetMathMacro.cpp b/src/mathed/InsetMathMacro.cpp
index a214600..d2c35c7 100644
--- a/src/mathed/InsetMathMacro.cpp
+++ b/src/mathed/InsetMathMacro.cpp
@@ -1214,8 +1214,11 @@ void InsetMathMacro::write(TeXMathStream & os) const
        }
 
        // add space if there was no argument
-       if (first)
+       // or add braces if we have optionals but none are present and [ follows
+       if (first) {
                os.pendingSpace(true);
+               os.useBraces(d->optionals_ > 0);
+       }
 
        // write \(no)limits modifiers if relevant
        writeLimits(os);
diff --git a/src/mathed/MathStream.cpp b/src/mathed/MathStream.cpp
index 51ac984..6f4da39 100644
--- a/src/mathed/MathStream.cpp
+++ b/src/mathed/MathStream.cpp
@@ -106,6 +106,8 @@ TeXMathStream & operator<<(TeXMathStream & ws, docstring 
const & s)
        } else if (ws.pendingSpace()) {
                if (isAlphaASCII(s[first]))
                        ws.os() << ' ';
+               else if (s[first] == '[' && ws.useBraces())
+                       ws.os() << "{}";
                else if (s[first] == ' ' && ws.textMode())
                        ws.os() << '\\';
                ws.pendingSpace(false);
@@ -148,9 +150,17 @@ void TeXMathStream::addlines(unsigned int n)
 }
 
 
-void TeXMathStream::pendingSpace(bool how)
+void TeXMathStream::pendingSpace(bool space)
 {
-       pendingspace_ = how;
+       pendingspace_ = space;
+       if (!space)
+               usebraces_ = false;
+}
+
+
+void TeXMathStream::useBraces(bool braces)
+{
+       usebraces_ = braces;
 }
 
 
@@ -226,6 +236,8 @@ TeXMathStream & operator<<(TeXMathStream & ws, char c)
        } else if (ws.pendingSpace()) {
                if (isAlphaASCII(c))
                        ws.os() << ' ';
+               else if (c == '[' && ws.useBraces())
+                       ws.os() << "{}";
                else if (c == ' ' && ws.textMode())
                        ws.os() << '\\';
                ws.pendingSpace(false);
diff --git a/src/mathed/MathStream.h b/src/mathed/MathStream.h
index aa1f71a..153e7df 100644
--- a/src/mathed/MathStream.h
+++ b/src/mathed/MathStream.h
@@ -80,9 +80,13 @@ public:
        /// tell which ulem command type we are inside
        UlemCmdType ulemCmd() const { return ulemcmd_; }
        /// writes space if next thing is isalpha()
-       void pendingSpace(bool how);
+       void pendingSpace(bool space);
        /// writes space if next thing is isalpha()
        bool pendingSpace() const { return pendingspace_; }
+       /// write braces if a space is pending and next char is [
+       void useBraces(bool braces);
+       /// write braces if a space is pending and next char is [
+       bool useBraces() const { return usebraces_; }
        /// tell whether to write the closing brace of \ensuremath
        void pendingBrace(bool brace);
        /// tell whether to write the closing brace of \ensuremath
@@ -124,6 +128,8 @@ private:
        OutputType output_ = wsDefault;
        /// do we have a space pending?
        bool pendingspace_ = false;
+       /// do we have to write braces when a space is pending and [ follows?
+       bool usebraces_ = false;
        /// do we have a brace pending?
        bool pendingbrace_ = false;
        /// are we in text mode when producing latex code?
-- 
lyx-cvs mailing list
lyx-cvs@lists.lyx.org
http://lists.lyx.org/mailman/listinfo/lyx-cvs

Reply via email to