From 28fe97b5532dd25deff7c1565af7862570eaa017 Mon Sep 17 00:00:00 2001 From: Enrico Forestieri Date: Fri, 31 Oct 2008 15:19:39 +0000 Subject: [PATCH] Avoid creating bad math insets with Ctrl+M git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@27214 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/Text3.cpp | 6 ++-- src/mathed/InsetMathHull.cpp | 5 ++-- src/mathed/InsetMathHull.h | 2 +- src/mathed/MathParser.cpp | 56 +++++++++++++++++++++++++++++++++--- 4 files changed, 58 insertions(+), 11 deletions(-) diff --git a/src/Text3.cpp b/src/Text3.cpp index f6a5bcad6f..b08a743f63 100644 --- a/src/Text3.cpp +++ b/src/Text3.cpp @@ -163,13 +163,11 @@ static void mathDispatch(Cursor & cur, FuncRequest const & cmd, bool display) istringstream is(selstr); Lexer lex; lex.setStream(is); - formula->readQuiet(lex); - if (formula->getType() == hullNone) { + if (!formula->readQuiet(lex)) { // No valid formula, let's try with delims is.str("$" + selstr + "$"); lex.setStream(is); - formula->readQuiet(lex); - if (formula->getType() == hullNone) { + if (!formula->readQuiet(lex)) { // Still not valid, leave it as is valid = false; delete formula; diff --git a/src/mathed/InsetMathHull.cpp b/src/mathed/InsetMathHull.cpp index cc4672587d..cb1d457656 100644 --- a/src/mathed/InsetMathHull.cpp +++ b/src/mathed/InsetMathHull.cpp @@ -1585,11 +1585,12 @@ void InsetMathHull::read(Lexer & lex) } -void InsetMathHull::readQuiet(Lexer & lex) +bool InsetMathHull::readQuiet(Lexer & lex) { MathAtom at; - mathed_parse_normal(at, lex, Parse::QUIET); + bool result = mathed_parse_normal(at, lex, Parse::QUIET); operator=(*at->asHullInset()); + return result; } diff --git a/src/mathed/InsetMathHull.h b/src/mathed/InsetMathHull.h index ce1e9e51dc..239da66b94 100644 --- a/src/mathed/InsetMathHull.h +++ b/src/mathed/InsetMathHull.h @@ -113,7 +113,7 @@ public: /// void read(Lexer & lex); /// - void readQuiet(Lexer & lex); + bool readQuiet(Lexer & lex); /// int plaintext(odocstream &, OutputParams const &) const; /// diff --git a/src/mathed/MathParser.cpp b/src/mathed/MathParser.cpp index 7494a2906c..223fd992ac 100644 --- a/src/mathed/MathParser.cpp +++ b/src/mathed/MathParser.cpp @@ -653,10 +653,10 @@ bool Parser::parse(MathAtom & at) // at.nucleus()->cell(0) = ar; //else // lyxerr << "unusual contents found: " << ar << endl; - return false; - } - at = ar[0]; - return true; + success_ = false; + } else + at = ar[0]; + return success_; } @@ -1125,11 +1125,19 @@ bool Parser::parse1(InsetMathGrid & grid, unsigned flags, } else if (t.cs() == "(") { + if (mode == InsetMath::MATH_MODE) { + error("bad math environment"); + break; + } cell->push_back(MathAtom(new InsetMathHull(hullSimple))); parse2(cell->back(), FLAG_SIMPLE2, InsetMath::MATH_MODE, false); } else if (t.cs() == "[") { + if (mode != InsetMath::UNDECIDED_MODE) { + error("bad math environment"); + break; + } cell->push_back(MathAtom(new InsetMathHull(hullEquation))); parse2(cell->back(), FLAG_EQUATION, InsetMath::MATH_MODE, false); } @@ -1365,32 +1373,56 @@ bool Parser::parse1(InsetMathGrid & grid, unsigned flags, } else if (name == "math") { + if (mode == InsetMath::MATH_MODE) { + error("bad math environment"); + break; + } cell->push_back(MathAtom(new InsetMathHull(hullSimple))); parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, true); } else if (name == "equation" || name == "equation*" || name == "displaymath") { + if (mode != InsetMath::UNDECIDED_MODE) { + error("bad math environment"); + break; + } cell->push_back(MathAtom(new InsetMathHull(hullEquation))); parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, (name == "equation")); } else if (name == "eqnarray" || name == "eqnarray*") { + if (mode != InsetMath::UNDECIDED_MODE) { + error("bad math environment"); + break; + } cell->push_back(MathAtom(new InsetMathHull(hullEqnArray))); parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name)); } else if (name == "align" || name == "align*") { + if (mode != InsetMath::UNDECIDED_MODE) { + error("bad math environment"); + break; + } cell->push_back(MathAtom(new InsetMathHull(hullAlign))); parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name)); } else if (name == "flalign" || name == "flalign*") { + if (mode != InsetMath::UNDECIDED_MODE) { + error("bad math environment"); + break; + } cell->push_back(MathAtom(new InsetMathHull(hullFlAlign))); parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name)); } else if (name == "alignat" || name == "alignat*") { + if (mode != InsetMath::UNDECIDED_MODE) { + error("bad math environment"); + break; + } // ignore this for a while getArg('{', '}'); cell->push_back(MathAtom(new InsetMathHull(hullAlignAt))); @@ -1398,6 +1430,10 @@ bool Parser::parse1(InsetMathGrid & grid, unsigned flags, } else if (name == "xalignat" || name == "xalignat*") { + if (mode != InsetMath::UNDECIDED_MODE) { + error("bad math environment"); + break; + } // ignore this for a while getArg('{', '}'); cell->push_back(MathAtom(new InsetMathHull(hullXAlignAt))); @@ -1405,6 +1441,10 @@ bool Parser::parse1(InsetMathGrid & grid, unsigned flags, } else if (name == "xxalignat") { + if (mode != InsetMath::UNDECIDED_MODE) { + error("bad math environment"); + break; + } // ignore this for a while getArg('{', '}'); cell->push_back(MathAtom(new InsetMathHull(hullXXAlignAt))); @@ -1412,11 +1452,19 @@ bool Parser::parse1(InsetMathGrid & grid, unsigned flags, } else if (name == "multline" || name == "multline*") { + if (mode != InsetMath::UNDECIDED_MODE) { + error("bad math environment"); + break; + } cell->push_back(MathAtom(new InsetMathHull(hullMultline))); parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name)); } else if (name == "gather" || name == "gather*") { + if (mode != InsetMath::UNDECIDED_MODE) { + error("bad math environment"); + break; + } cell->push_back(MathAtom(new InsetMathHull(hullGather))); parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name)); } -- 2.39.2