#include "mathed/InsetMathHull.h"
#include "mathed/MathMacroTemplate.h"
+#include "mathed/MathParser.h"
#include <boost/next_prior.hpp>
// 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();
&& 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")));
}
namespace {
+bool warn_unusual_contents = true;
+
+
InsetMath::mode_type asMode(InsetMath::mode_type oldmode, docstring const & str)
{
//lyxerr << "handling mode: '" << str << "'" << endl;
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;
} // 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);
/// 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