From f505227f6e0815379b45d268a9674a485ae55aeb Mon Sep 17 00:00:00 2001 From: Enrico Forestieri Date: Mon, 6 Oct 2008 22:49:30 +0000 Subject: [PATCH] Smash remaining drawbacks for bug 2315 http://bugzilla.lyx.org/show_bug.cgi?id=2315 git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@26792 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/Text3.cpp | 33 ++++++++++++++++++++++++++------- src/mathed/MathParser.cpp | 12 +++++++++++- src/mathed/MathParser.h | 3 +++ 3 files changed, 40 insertions(+), 8 deletions(-) diff --git a/src/Text3.cpp b/src/Text3.cpp index b965198547..e78a4ee689 100644 --- a/src/Text3.cpp +++ b/src/Text3.cpp @@ -67,6 +67,7 @@ #include "mathed/InsetMathHull.h" #include "mathed/MathMacroTemplate.h" +#include "mathed/MathParser.h" #include @@ -127,6 +128,9 @@ static void mathDispatch(Cursor & cur, FuncRequest const & cmd, bool display) // It may happen that sel is empty but there is a selection replaceSelection(cur); + // Is this a valid formula? + bool valid = true; + if (sel.empty()) { #ifdef ENABLE_ASSERTIONS const int old_pos = cur.pos(); @@ -156,20 +160,35 @@ static void mathDispatch(Cursor & cur, FuncRequest const & cmd, bool display) && sel.find(from_ascii("\\def")) == string::npos) { InsetMathHull * formula = new InsetMathHull; - istringstream is(to_utf8(sel)); + string const selstr = to_utf8(sel); + istringstream is(selstr); Lexer lex; lex.setStream(is); + mathed_parser_warn_contents(false); formula->read(lex); - if (formula->getType() == hullNone) - // Don't create pseudo formulas if - // delimiters are left out - formula->mutate(hullSimple); - cur.insert(formula); + if (formula->getType() == hullNone) { + // No valid formula, let's try with delims + is.str("$" + selstr + "$"); + lex.setStream(is); + formula->read(lex); + if (formula->getType() == hullNone) { + // Still not valid, leave it as is + valid = false; + delete formula; + cur.insert(sel); + } else + cur.insert(formula); + } else + cur.insert(formula); + mathed_parser_warn_contents(true); } else { cur.insert(new MathMacroTemplate(sel)); } } - cur.message(from_utf8(N_("Math editor mode"))); + if (valid) + cur.message(from_utf8(N_("Math editor mode"))); + else + cur.message(from_utf8(N_("No valid math formula"))); } diff --git a/src/mathed/MathParser.cpp b/src/mathed/MathParser.cpp index bc398467d0..becc9c89b3 100644 --- a/src/mathed/MathParser.cpp +++ b/src/mathed/MathParser.cpp @@ -79,6 +79,9 @@ namespace lyx { namespace { +bool warn_unusual_contents = true; + + InsetMath::mode_type asMode(InsetMath::mode_type oldmode, docstring const & str) { //lyxerr << "handling mode: '" << str << "'" << endl; @@ -611,7 +614,8 @@ bool Parser::parse(MathAtom & at) MathData ar; parse(ar, false, InsetMath::UNDECIDED_MODE); if (ar.size() != 1 || ar.front()->getType() == hullNone) { - lyxerr << "unusual contents found: " << ar << endl; + if (warn_unusual_contents) + lyxerr << "unusual contents found: " << ar << endl; at = MathAtom(new InsetMathPar(ar)); //if (at->nargs() > 0) // at.nucleus()->cell(0) = ar; @@ -1687,6 +1691,12 @@ void Parser::parse1(InsetMathGrid & grid, unsigned flags, } // anonymous namespace +void mathed_parser_warn_contents(bool warn) +{ + warn_unusual_contents = warn; +} + + void mathed_parse_cell(MathData & ar, docstring const & str) { Parser(str).parse(ar, 0, InsetMath::MATH_MODE); diff --git a/src/mathed/MathParser.h b/src/mathed/MathParser.h index de97e1033d..43b6f92090 100644 --- a/src/mathed/MathParser.h +++ b/src/mathed/MathParser.h @@ -58,6 +58,9 @@ public: /// check whether this is a well-known (La)TeX macro or primitive latexkeys const * in_word_set(docstring const & str); +/// tell the parser whether it should warn about unusual contents +void mathed_parser_warn_contents(bool); + /// parse formula from a string bool mathed_parse_normal(MathAtom &, docstring const &); /// ... the LyX lexxer