]> git.lyx.org Git - features.git/commitdiff
Smash remaining drawbacks for bug 2315
authorEnrico Forestieri <forenr@lyx.org>
Mon, 6 Oct 2008 22:49:30 +0000 (22:49 +0000)
committerEnrico Forestieri <forenr@lyx.org>
Mon, 6 Oct 2008 22:49:30 +0000 (22:49 +0000)
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
src/mathed/MathParser.cpp
src/mathed/MathParser.h

index b965198547ab6ee79c1eab4db9f9b37b2422ccbb..e78a4ee68984baf6bcdfc7862be9465f92d87649 100644 (file)
@@ -67,6 +67,7 @@
 
 #include "mathed/InsetMathHull.h"
 #include "mathed/MathMacroTemplate.h"
+#include "mathed/MathParser.h"
 
 #include <boost/next_prior.hpp>
 
@@ -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")));
 }
 
 
index bc398467d0a6567cef4c1a43d64a4ab366aa4a7b..becc9c89b377eae32c9b1a82b63f16bac327fd6d 100644 (file)
@@ -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);
index de97e1033db52f9596cd738738edc6d15aa06013..43b6f920909dab900d66e1c87a5501fff29d6459 100644 (file)
@@ -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