]> git.lyx.org Git - features.git/commitdiff
Write braces around macros only when actually needed
authorEnrico Forestieri <forenr@lyx.org>
Thu, 25 Apr 2019 09:11:51 +0000 (11:11 +0200)
committerJean-Marc Lasgouttes <lasgouttes@lyx.org>
Thu, 18 Jun 2020 13:48:27 +0000 (15:48 +0200)
When a macro with optionals appeared inside the optional argument of
another one, the onscreen display and latex output were wrong. This
issue was addressed at [e8f480e7/lyxgit] by enclosing in braces macros
with optional arguments. However, this was done even when the macro
with optionals was in a non-optional argument of another macro.
This commit limits the bracing to the cases where it is really needed
and allows to address some particular issues evidenced in #11552.

src/mathed/InsetMathMacro.cpp
src/mathed/MathStream.cpp
src/mathed/MathStream.h

index 4d20d9302e8b4416090d4666822a3a134ff2e127..296876b764ce038742db4c8da28eaacf7f56595a 100644 (file)
@@ -1079,10 +1079,6 @@ void InsetMathMacro::write(WriteStream & os) const
        // we should be ok to continue even if this fails.
        LATTEST(d->macro_);
 
-       // We may already be in the argument of a macro
-       bool const inside_macro = os.insideMacro();
-       os.insideMacro(true);
-
        // Optional arguments:
        // First find last non-empty optional argument
        idx_type emptyOptFrom = 0;
@@ -1094,7 +1090,7 @@ void InsetMathMacro::write(WriteStream & os) const
 
        // Enclose in braces to avoid latex errors with xargs if we have
        // optional arguments and are in the optional argument of a macro
-       if (d->optionals_ && inside_macro && emptyOptFrom)
+       if (d->optionals_ && os.insideMacroOpt() && emptyOptFrom)
                os << '{';
 
        // Always protect macros in a fragile environment
@@ -1119,10 +1115,16 @@ void InsetMathMacro::write(WriteStream & os) const
                } else if (cell(i).size() && cell(i)[0].nucleus()->asScriptInset()) {
                        braced = cell(i)[0].nucleus()->asScriptInset()->nuc().empty();
                }
+               // We may already be in the optional argument of a macro
+               bool const inside_macro = os.insideMacroOpt();
+               os.insideMacroOpt(true);
+
                if (braced)
                        os << "[{" << cell(i) << "}]";
                else
                        os << "[" << cell(i) << "]";
+
+               os.insideMacroOpt(inside_macro);
        }
 
        // skip the tailing empty optionals
@@ -1142,12 +1144,10 @@ void InsetMathMacro::write(WriteStream & os) const
        }
 
        // Close the opened brace or add space if there was no argument
-       if (d->optionals_ && inside_macro && emptyOptFrom)
+       if (d->optionals_ && os.insideMacroOpt() && emptyOptFrom)
                os << '}';
        else if (first)
                os.pendingSpace(true);
-
-       os.insideMacro(inside_macro);
 }
 
 
index 774e964a8dfb94caa968a1e7f23193aa12f9c4be..dd274e8b8f1ff66874eeb393c636e4dd8dcbbb9a 100644 (file)
@@ -128,7 +128,7 @@ WriteStream & operator<<(WriteStream & ws, docstring const & s)
 WriteStream::WriteStream(otexrowstream & os, bool fragile, bool latex,
                                                 OutputType output, Encoding const * encoding)
        : os_(os), fragile_(fragile), firstitem_(false), latex_(latex),
-         output_(output), insidemacro_(false), pendingspace_(false),
+         output_(output), insidemacro_opt_(false), pendingspace_(false),
          pendingbrace_(false), textmode_(false), locked_(0), ascii_(0),
          canbreakline_(true), mathsout_(false), ulemcmd_(NONE), line_(0),
          encoding_(encoding), row_entry_(TexRow::row_none)
index 19da27640afd9e820c70d2f72c14d423a8ebfff1..c70d3b1f39c5c95339367b22b508daac6164409e 100644 (file)
@@ -81,10 +81,10 @@ public:
        void ulemCmd(UlemCmdType ulemcmd) { ulemcmd_ = ulemcmd; }
        /// tell which ulem command type we are inside
        UlemCmdType ulemCmd() const { return ulemcmd_; }
-       /// record whether we are in the argument of a math macro
-       void insideMacro(bool insidemacro) { insidemacro_ = insidemacro; }
-       /// tell whether we are in the argument of a math macro
-       bool insideMacro() const { return insidemacro_; }
+       /// record whether we are in the optional argument of a math macro
+       void insideMacroOpt(bool inopt) { insidemacro_opt_ = inopt; }
+       /// tell whether we are in the optional argument of a math macro
+       bool insideMacroOpt() const { return insidemacro_opt_; }
        /// writes space if next thing is isalpha()
        void pendingSpace(bool how);
        /// writes space if next thing is isalpha()
@@ -124,8 +124,8 @@ private:
        int latex_;
        /// output type (default, source preview, instant preview)?
        OutputType output_;
-       /// are we in the argument of a math macro?
-       bool insidemacro_;
+       /// are we in the optional argument of a math macro?
+       bool insidemacro_opt_;
        /// do we have a space pending?
        bool pendingspace_;
        /// do we have a brace pending?