Revert [60ac25a4/lyxgit], [30f16463/lyxgit], and part of [e8f480e7/lyxgit]

As evidenced in #11552, at loading time there is no way to tell
whether braces were added by LyX with certainty. However, after
[503f7db2/lyxgit], LyX does not automatically removes brace insets.
So, in case one faces the problems mentioned in [e8f480e7/lyxgit],
the workaround of adding a brace inset now will not be frustrated.
In essence, after [503f7db2/lyxgit], the real fix for #11552 would
have been reverting the part in [e8f480e7/lyxgit] that was adding
the braces on output.
This commit is contained in:
Enrico Forestieri 2019-04-26 12:21:38 +02:00
parent 209b9c594f
commit e763f90244
5 changed files with 16 additions and 60 deletions

View File

@ -621,9 +621,6 @@ public:
typedef std::set<docstring> UserMacroSet; typedef std::set<docstring> UserMacroSet;
mutable UserMacroSet usermacros; mutable UserMacroSet usermacros;
/// Collect user macro names with optional parameters at loading time
mutable UserMacroSet usermacros_with_opts;
/// Replace the inset contents for insets which InsetCode is equal /// Replace the inset contents for insets which InsetCode is equal
/// to the passed \p inset_code. /// to the passed \p inset_code.
void changeRefsIfUnique(docstring const & from, docstring const & to); void changeRefsIfUnique(docstring const & from, docstring const & to);

View File

@ -1079,6 +1079,13 @@ void InsetMathMacro::write(WriteStream & os) const
// we should be ok to continue even if this fails. // we should be ok to continue even if this fails.
LATTEST(d->macro_); LATTEST(d->macro_);
// Always protect macros in a fragile environment
if (os.fragile())
os << "\\protect";
os << "\\" << name();
bool first = true;
// Optional arguments: // Optional arguments:
// First find last non-empty optional argument // First find last non-empty optional argument
idx_type emptyOptFrom = 0; idx_type emptyOptFrom = 0;
@ -1088,18 +1095,6 @@ void InsetMathMacro::write(WriteStream & os) const
emptyOptFrom = i + 1; emptyOptFrom = i + 1;
} }
// 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_ && os.insideMacroOpt() && emptyOptFrom)
os << '{';
// Always protect macros in a fragile environment
if (os.fragile())
os << "\\protect";
os << "\\" << name();
bool first = true;
// print out optionals // print out optionals
for (i=0; i < cells_.size() && i < emptyOptFrom; ++i) { for (i=0; i < cells_.size() && i < emptyOptFrom; ++i) {
first = false; first = false;
@ -1115,16 +1110,10 @@ void InsetMathMacro::write(WriteStream & os) const
} else if (cell(i).size() && cell(i)[0].nucleus()->asScriptInset()) { } else if (cell(i).size() && cell(i)[0].nucleus()->asScriptInset()) {
braced = cell(i)[0].nucleus()->asScriptInset()->nuc().empty(); 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) if (braced)
os << "[{" << cell(i) << "}]"; os << "[{" << cell(i) << "}]";
else else
os << "[" << cell(i) << "]"; os << "[" << cell(i) << "]";
os.insideMacroOpt(inside_macro);
} }
// skip the tailing empty optionals // skip the tailing empty optionals
@ -1143,10 +1132,8 @@ void InsetMathMacro::write(WriteStream & os) const
first = false; first = false;
} }
// Close the opened brace or add space if there was no argument // add space if there was no argument
if (d->optionals_ && os.insideMacroOpt() && emptyOptFrom) if (first)
os << '}';
else if (first)
os.pendingSpace(true); os.pendingSpace(true);
} }

View File

@ -942,27 +942,11 @@ bool Parser::parse1(InsetMathGrid & grid, unsigned flags,
cell->push_back(MathAtom(new InsetMathSpace(string(1, t.character()), ""))); cell->push_back(MathAtom(new InsetMathSpace(string(1, t.character()), "")));
else if (t.cat() == catBegin) { else if (t.cat() == catBegin) {
bool const inbraces = nextToken().cat() == catBegin ||
(!cell->empty() && cell->back()->asMacro());
MathData ar; MathData ar;
parse(ar, FLAG_BRACE_LAST, mode); parse(ar, FLAG_BRACE_LAST, mode);
// do not create a BraceInset if they were written by LyX // do not create a BraceInset if they were written by LyX
// this helps to keep the annoyance of "a choose b" to a minimum // this helps to keep the annoyance of "a choose b" to a minimum
InsetMathMacro const * ma = !inbraces && ar.size() ? ar[0]->asMacro() : 0; if (ar.size() == 1 && ar[0]->extraBraces())
InsetMathChar const * mc = ma && ar.size() > 1 ? ar[1]->asCharInset(): 0;
bool braced = mc && mc->getChar() == '[';
// If this is a macro, it may have optional
// arguments, even if only defaults are used.
// In this case, there is no following '['.
if (!inbraces && !braced && ma && buf) {
if (mode_ & Parse::TRACKMACRO)
braced = buf->usermacros_with_opts.count(ma->name());
else {
MacroData const * md = buf->getMacro(ma->name(), false);
braced = md && md->optionals();
}
}
if ((ar.size() == 1 && ar[0]->extraBraces()) || braced)
cell->append(ar); cell->append(ar);
else else
cell->push_back(MathAtom(new InsetMathBrace(ar))); cell->push_back(MathAtom(new InsetMathBrace(ar)));
@ -1150,11 +1134,8 @@ bool Parser::parse1(InsetMathGrid & grid, unsigned flags,
name, nargs, optionals, MacroTypeNewcommand, name, nargs, optionals, MacroTypeNewcommand,
optionalValues, def, display))); optionalValues, def, display)));
if (buf && (mode_ & Parse::TRACKMACRO)) { if (buf && (mode_ & Parse::TRACKMACRO))
buf->usermacros.insert(name); buf->usermacros.insert(name);
if (optionals)
buf->usermacros_with_opts.insert(name);
}
} }
else if (t.cs() == "newcommandx" || else if (t.cs() == "newcommandx" ||
@ -1274,11 +1255,8 @@ bool Parser::parse1(InsetMathGrid & grid, unsigned flags,
name, nargs, optionals, MacroTypeNewcommandx, name, nargs, optionals, MacroTypeNewcommandx,
optionalValues, def, display))); optionalValues, def, display)));
if (buf && (mode_ & Parse::TRACKMACRO)) { if (buf && (mode_ & Parse::TRACKMACRO))
buf->usermacros.insert(name); buf->usermacros.insert(name);
if (optionals)
buf->usermacros_with_opts.insert(name);
}
} }
else if (t.cs() == "(") { else if (t.cs() == "(") {

View File

@ -128,10 +128,10 @@ WriteStream & operator<<(WriteStream & ws, docstring const & s)
WriteStream::WriteStream(otexrowstream & os, bool fragile, bool latex, WriteStream::WriteStream(otexrowstream & os, bool fragile, bool latex,
OutputType output, Encoding const * encoding) OutputType output, Encoding const * encoding)
: os_(os), fragile_(fragile), firstitem_(false), latex_(latex), : os_(os), fragile_(fragile), firstitem_(false), latex_(latex),
output_(output), insidemacro_opt_(false), pendingspace_(false), output_(output), pendingspace_(false), pendingbrace_(false),
pendingbrace_(false), textmode_(false), locked_(0), ascii_(0), textmode_(false), locked_(0), ascii_(0), canbreakline_(true),
canbreakline_(true), mathsout_(false), ulemcmd_(NONE), line_(0), mathsout_(false), ulemcmd_(NONE), line_(0), encoding_(encoding),
encoding_(encoding), row_entry_(TexRow::row_none) row_entry_(TexRow::row_none)
{} {}

View File

@ -81,10 +81,6 @@ public:
void ulemCmd(UlemCmdType ulemcmd) { ulemcmd_ = ulemcmd; } void ulemCmd(UlemCmdType ulemcmd) { ulemcmd_ = ulemcmd; }
/// tell which ulem command type we are inside /// tell which ulem command type we are inside
UlemCmdType ulemCmd() const { return ulemcmd_; } UlemCmdType ulemCmd() const { return ulemcmd_; }
/// 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() /// writes space if next thing is isalpha()
void pendingSpace(bool how); void pendingSpace(bool how);
/// writes space if next thing is isalpha() /// writes space if next thing is isalpha()
@ -124,8 +120,6 @@ private:
int latex_; int latex_;
/// output type (default, source preview, instant preview)? /// output type (default, source preview, instant preview)?
OutputType output_; OutputType output_;
/// are we in the optional argument of a math macro?
bool insidemacro_opt_;
/// do we have a space pending? /// do we have a space pending?
bool pendingspace_; bool pendingspace_;
/// do we have a brace pending? /// do we have a brace pending?