From 30f164636969bf84c3247c5b63ca9fe56270546f Mon Sep 17 00:00:00 2001 From: Enrico Forestieri Date: Thu, 25 Apr 2019 11:11:51 +0200 Subject: [PATCH] Write braces around macros only when actually needed 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 | 16 ++++++++-------- src/mathed/MathStream.cpp | 2 +- src/mathed/MathStream.h | 12 ++++++------ 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/mathed/InsetMathMacro.cpp b/src/mathed/InsetMathMacro.cpp index 4d20d9302e..296876b764 100644 --- a/src/mathed/InsetMathMacro.cpp +++ b/src/mathed/InsetMathMacro.cpp @@ -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); } diff --git a/src/mathed/MathStream.cpp b/src/mathed/MathStream.cpp index 774e964a8d..dd274e8b8f 100644 --- a/src/mathed/MathStream.cpp +++ b/src/mathed/MathStream.cpp @@ -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) diff --git a/src/mathed/MathStream.h b/src/mathed/MathStream.h index 19da27640a..c70d3b1f39 100644 --- a/src/mathed/MathStream.h +++ b/src/mathed/MathStream.h @@ -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? -- 2.39.2