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.
This commit is contained in:
Enrico Forestieri 2019-04-25 11:11:51 +02:00
parent 072fe84186
commit 30f1646369
3 changed files with 15 additions and 15 deletions

View 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);
}

View 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)

View 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?