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
This commit is contained in:
Enrico Forestieri 2008-10-31 15:19:39 +00:00
parent fa4801bdc9
commit 28fe97b553
4 changed files with 58 additions and 11 deletions

View File

@ -163,13 +163,11 @@ static void mathDispatch(Cursor & cur, FuncRequest const & cmd, bool display)
istringstream is(selstr); istringstream is(selstr);
Lexer lex; Lexer lex;
lex.setStream(is); lex.setStream(is);
formula->readQuiet(lex); if (!formula->readQuiet(lex)) {
if (formula->getType() == hullNone) {
// No valid formula, let's try with delims // No valid formula, let's try with delims
is.str("$" + selstr + "$"); is.str("$" + selstr + "$");
lex.setStream(is); lex.setStream(is);
formula->readQuiet(lex); if (!formula->readQuiet(lex)) {
if (formula->getType() == hullNone) {
// Still not valid, leave it as is // Still not valid, leave it as is
valid = false; valid = false;
delete formula; delete formula;

View File

@ -1585,11 +1585,12 @@ void InsetMathHull::read(Lexer & lex)
} }
void InsetMathHull::readQuiet(Lexer & lex) bool InsetMathHull::readQuiet(Lexer & lex)
{ {
MathAtom at; MathAtom at;
mathed_parse_normal(at, lex, Parse::QUIET); bool result = mathed_parse_normal(at, lex, Parse::QUIET);
operator=(*at->asHullInset()); operator=(*at->asHullInset());
return result;
} }

View File

@ -113,7 +113,7 @@ public:
/// ///
void read(Lexer & lex); void read(Lexer & lex);
/// ///
void readQuiet(Lexer & lex); bool readQuiet(Lexer & lex);
/// ///
int plaintext(odocstream &, OutputParams const &) const; int plaintext(odocstream &, OutputParams const &) const;
/// ///

View File

@ -653,10 +653,10 @@ bool Parser::parse(MathAtom & at)
// at.nucleus()->cell(0) = ar; // at.nucleus()->cell(0) = ar;
//else //else
// lyxerr << "unusual contents found: " << ar << endl; // lyxerr << "unusual contents found: " << ar << endl;
return false; success_ = false;
} } else
at = ar[0]; at = ar[0];
return true; return success_;
} }
@ -1125,11 +1125,19 @@ bool Parser::parse1(InsetMathGrid & grid, unsigned flags,
} }
else if (t.cs() == "(") { else if (t.cs() == "(") {
if (mode == InsetMath::MATH_MODE) {
error("bad math environment");
break;
}
cell->push_back(MathAtom(new InsetMathHull(hullSimple))); cell->push_back(MathAtom(new InsetMathHull(hullSimple)));
parse2(cell->back(), FLAG_SIMPLE2, InsetMath::MATH_MODE, false); parse2(cell->back(), FLAG_SIMPLE2, InsetMath::MATH_MODE, false);
} }
else if (t.cs() == "[") { else if (t.cs() == "[") {
if (mode != InsetMath::UNDECIDED_MODE) {
error("bad math environment");
break;
}
cell->push_back(MathAtom(new InsetMathHull(hullEquation))); cell->push_back(MathAtom(new InsetMathHull(hullEquation)));
parse2(cell->back(), FLAG_EQUATION, InsetMath::MATH_MODE, false); parse2(cell->back(), FLAG_EQUATION, InsetMath::MATH_MODE, false);
} }
@ -1365,32 +1373,56 @@ bool Parser::parse1(InsetMathGrid & grid, unsigned flags,
} }
else if (name == "math") { else if (name == "math") {
if (mode == InsetMath::MATH_MODE) {
error("bad math environment");
break;
}
cell->push_back(MathAtom(new InsetMathHull(hullSimple))); cell->push_back(MathAtom(new InsetMathHull(hullSimple)));
parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, true); parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, true);
} }
else if (name == "equation" || name == "equation*" else if (name == "equation" || name == "equation*"
|| name == "displaymath") { || name == "displaymath") {
if (mode != InsetMath::UNDECIDED_MODE) {
error("bad math environment");
break;
}
cell->push_back(MathAtom(new InsetMathHull(hullEquation))); cell->push_back(MathAtom(new InsetMathHull(hullEquation)));
parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, (name == "equation")); parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, (name == "equation"));
} }
else if (name == "eqnarray" || name == "eqnarray*") { else if (name == "eqnarray" || name == "eqnarray*") {
if (mode != InsetMath::UNDECIDED_MODE) {
error("bad math environment");
break;
}
cell->push_back(MathAtom(new InsetMathHull(hullEqnArray))); cell->push_back(MathAtom(new InsetMathHull(hullEqnArray)));
parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name)); parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
} }
else if (name == "align" || name == "align*") { else if (name == "align" || name == "align*") {
if (mode != InsetMath::UNDECIDED_MODE) {
error("bad math environment");
break;
}
cell->push_back(MathAtom(new InsetMathHull(hullAlign))); cell->push_back(MathAtom(new InsetMathHull(hullAlign)));
parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name)); parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
} }
else if (name == "flalign" || name == "flalign*") { else if (name == "flalign" || name == "flalign*") {
if (mode != InsetMath::UNDECIDED_MODE) {
error("bad math environment");
break;
}
cell->push_back(MathAtom(new InsetMathHull(hullFlAlign))); cell->push_back(MathAtom(new InsetMathHull(hullFlAlign)));
parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name)); parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
} }
else if (name == "alignat" || name == "alignat*") { else if (name == "alignat" || name == "alignat*") {
if (mode != InsetMath::UNDECIDED_MODE) {
error("bad math environment");
break;
}
// ignore this for a while // ignore this for a while
getArg('{', '}'); getArg('{', '}');
cell->push_back(MathAtom(new InsetMathHull(hullAlignAt))); cell->push_back(MathAtom(new InsetMathHull(hullAlignAt)));
@ -1398,6 +1430,10 @@ bool Parser::parse1(InsetMathGrid & grid, unsigned flags,
} }
else if (name == "xalignat" || name == "xalignat*") { else if (name == "xalignat" || name == "xalignat*") {
if (mode != InsetMath::UNDECIDED_MODE) {
error("bad math environment");
break;
}
// ignore this for a while // ignore this for a while
getArg('{', '}'); getArg('{', '}');
cell->push_back(MathAtom(new InsetMathHull(hullXAlignAt))); cell->push_back(MathAtom(new InsetMathHull(hullXAlignAt)));
@ -1405,6 +1441,10 @@ bool Parser::parse1(InsetMathGrid & grid, unsigned flags,
} }
else if (name == "xxalignat") { else if (name == "xxalignat") {
if (mode != InsetMath::UNDECIDED_MODE) {
error("bad math environment");
break;
}
// ignore this for a while // ignore this for a while
getArg('{', '}'); getArg('{', '}');
cell->push_back(MathAtom(new InsetMathHull(hullXXAlignAt))); cell->push_back(MathAtom(new InsetMathHull(hullXXAlignAt)));
@ -1412,11 +1452,19 @@ bool Parser::parse1(InsetMathGrid & grid, unsigned flags,
} }
else if (name == "multline" || name == "multline*") { else if (name == "multline" || name == "multline*") {
if (mode != InsetMath::UNDECIDED_MODE) {
error("bad math environment");
break;
}
cell->push_back(MathAtom(new InsetMathHull(hullMultline))); cell->push_back(MathAtom(new InsetMathHull(hullMultline)));
parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name)); parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
} }
else if (name == "gather" || name == "gather*") { else if (name == "gather" || name == "gather*") {
if (mode != InsetMath::UNDECIDED_MODE) {
error("bad math environment");
break;
}
cell->push_back(MathAtom(new InsetMathHull(hullGather))); cell->push_back(MathAtom(new InsetMathHull(hullGather)));
parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name)); parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
} }