From 92019d6a66f62f0f3a2cb2dc3bee9bb55d7ba852 Mon Sep 17 00:00:00 2001 From: Enrico Forestieri Date: Fri, 26 Apr 2019 12:21:38 +0200 Subject: [PATCH] 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. --- src/Buffer.h | 3 --- src/mathed/InsetMathMacro.cpp | 31 +++++++++---------------------- src/mathed/MathParser.cpp | 28 +++------------------------- src/mathed/MathStream.cpp | 8 ++++---- src/mathed/MathStream.h | 6 ------ 5 files changed, 16 insertions(+), 60 deletions(-) diff --git a/src/Buffer.h b/src/Buffer.h index 7fc9e4808c..f1bd1f5e79 100644 --- a/src/Buffer.h +++ b/src/Buffer.h @@ -621,9 +621,6 @@ public: typedef std::set UserMacroSet; 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 /// to the passed \p inset_code. void changeRefsIfUnique(docstring const & from, docstring const & to); diff --git a/src/mathed/InsetMathMacro.cpp b/src/mathed/InsetMathMacro.cpp index 296876b764..4b5242e5fc 100644 --- a/src/mathed/InsetMathMacro.cpp +++ b/src/mathed/InsetMathMacro.cpp @@ -1079,6 +1079,13 @@ void InsetMathMacro::write(WriteStream & os) const // we should be ok to continue even if this fails. LATTEST(d->macro_); + // Always protect macros in a fragile environment + if (os.fragile()) + os << "\\protect"; + + os << "\\" << name(); + bool first = true; + // Optional arguments: // First find last non-empty optional argument idx_type emptyOptFrom = 0; @@ -1088,18 +1095,6 @@ void InsetMathMacro::write(WriteStream & os) const 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 for (i=0; i < cells_.size() && i < emptyOptFrom; ++i) { first = false; @@ -1115,16 +1110,10 @@ 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 @@ -1143,10 +1132,8 @@ void InsetMathMacro::write(WriteStream & os) const first = false; } - // Close the opened brace or add space if there was no argument - if (d->optionals_ && os.insideMacroOpt() && emptyOptFrom) - os << '}'; - else if (first) + // add space if there was no argument + if (first) os.pendingSpace(true); } diff --git a/src/mathed/MathParser.cpp b/src/mathed/MathParser.cpp index 8539ab6619..436f022e25 100644 --- a/src/mathed/MathParser.cpp +++ b/src/mathed/MathParser.cpp @@ -942,27 +942,11 @@ bool Parser::parse1(InsetMathGrid & grid, unsigned flags, cell->push_back(MathAtom(new InsetMathSpace(string(1, t.character()), ""))); else if (t.cat() == catBegin) { - bool const inbraces = nextToken().cat() == catBegin || - (!cell->empty() && cell->back()->asMacro()); MathData ar; parse(ar, FLAG_BRACE_LAST, mode); // do not create a BraceInset if they were written by LyX // this helps to keep the annoyance of "a choose b" to a minimum - InsetMathMacro const * ma = !inbraces && ar.size() ? ar[0]->asMacro() : 0; - 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) + if (ar.size() == 1 && ar[0]->extraBraces()) cell->append(ar); else cell->push_back(MathAtom(new InsetMathBrace(ar))); @@ -1150,11 +1134,8 @@ bool Parser::parse1(InsetMathGrid & grid, unsigned flags, name, nargs, optionals, MacroTypeNewcommand, optionalValues, def, display))); - if (buf && (mode_ & Parse::TRACKMACRO)) { + if (buf && (mode_ & Parse::TRACKMACRO)) buf->usermacros.insert(name); - if (optionals) - buf->usermacros_with_opts.insert(name); - } } else if (t.cs() == "newcommandx" || @@ -1274,11 +1255,8 @@ bool Parser::parse1(InsetMathGrid & grid, unsigned flags, name, nargs, optionals, MacroTypeNewcommandx, optionalValues, def, display))); - if (buf && (mode_ & Parse::TRACKMACRO)) { + if (buf && (mode_ & Parse::TRACKMACRO)) buf->usermacros.insert(name); - if (optionals) - buf->usermacros_with_opts.insert(name); - } } else if (t.cs() == "(") { diff --git a/src/mathed/MathStream.cpp b/src/mathed/MathStream.cpp index dd274e8b8f..9a6c32ca21 100644 --- a/src/mathed/MathStream.cpp +++ b/src/mathed/MathStream.cpp @@ -128,10 +128,10 @@ 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_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) + output_(output), 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 c70d3b1f39..b7e79434af 100644 --- a/src/mathed/MathStream.h +++ b/src/mathed/MathStream.h @@ -81,10 +81,6 @@ 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 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 +120,6 @@ private: int latex_; /// output type (default, source preview, instant preview)? OutputType output_; - /// 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.5