On Tue, Oct 27, 2009 at 03:20:57PM +0100, Jean-Marc Lasgouttes wrote:
> I would be curious to see how big the overhaul would be.

It turned out to be not so big. The attached patch fixes the problem for me.

-- 
Enrico
Index: src/mathed/InsetMathGrid.cpp
===================================================================
--- src/mathed/InsetMathGrid.cpp        (revisione 31790)
+++ src/mathed/InsetMathGrid.cpp        (copia locale)
@@ -1276,10 +1276,12 @@ void InsetMathGrid::doDispatch(Cursor & 
                InsetMathGrid grid(1, 1);
                if (!topaste.empty())
                        if ((topaste.size() == 1 && topaste.at(0) < 0x80)
-                           || !mathed_parse_normal(grid, topaste, parseflg)) {
+                           || !mathed_parse_normal(grid, topaste, parseflg,
+                                                   cur.buffer())) {
                                resetGrid(grid);
                                mathed_parse_normal(grid, topaste,
-                                               parseflg | Parse::VERBATIM);
+                                                   parseflg | Parse::VERBATIM,
+                                                   cur.buffer());
                        }
 
                if (grid.nargs() == 1) {
Index: src/mathed/InsetMathHull.cpp
===================================================================
--- src/mathed/InsetMathHull.cpp        (revisione 31790)
+++ src/mathed/InsetMathHull.cpp        (copia locale)
@@ -149,7 +149,7 @@ docstring hullName(HullType type)
 
 static InsetLabel * dummy_pointer = 0;
 
-InsetMathHull::InsetMathHull()
+InsetMathHull::InsetMathHull(Buffer * buf)
        : InsetMathGrid(1, 1), type_(hullNone), nonum_(1, false),
          label_(1, dummy_pointer), preview_(new RenderPreview(this))
 {
@@ -157,15 +157,17 @@ InsetMathHull::InsetMathHull()
        //lyxerr << "sizeof MetricsInfo: " << sizeof(MetricsInfo) << endl;
        //lyxerr << "sizeof InsetMathChar: " << sizeof(InsetMathChar) << endl;
        //lyxerr << "sizeof FontInfo: " << sizeof(FontInfo) << endl;
+       buffer_ = buf;
        initMath();
        setDefaults();
 }
 
 
-InsetMathHull::InsetMathHull(HullType type)
+InsetMathHull::InsetMathHull(HullType type, Buffer * buf)
        : InsetMathGrid(getCols(type), 1), type_(type), nonum_(1, false),
          label_(1, dummy_pointer), preview_(new RenderPreview(this))
 {
+       buffer_ = buf;
        initMath();
        setDefaults();
 }
@@ -197,6 +199,7 @@ InsetMathHull & InsetMathHull::operator=
        InsetMathGrid::operator=(other);
        type_  = other.type_;
        nonum_ = other.nonum_;
+       buffer_ = other.buffer_;
        for (size_t i = 0; i < label_.size(); ++i)
                delete label_[i];
        label_ = other.label_;
@@ -807,7 +810,7 @@ void InsetMathHull::glueall()
        MathData ar;
        for (idx_type i = 0; i < nargs(); ++i)
                ar.append(cell(i));
-       *this = InsetMathHull(hullSimple);
+       *this = InsetMathHull(hullSimple, &buffer());
        cell(0) = ar;
        setDefaults();
 }
@@ -1606,7 +1609,7 @@ bool InsetMathHull::searchForward(Buffer
                laststr = str;
                current = ibegin(nucleus());
                ar.clear();
-               mathed_parse_cell(ar, str);
+               mathed_parse_cell(ar, str, Parse::NORMAL, &buffer());
        } else {
                increment(current);
        }
@@ -1644,7 +1647,7 @@ void InsetMathHull::write(ostream & os) 
 void InsetMathHull::read(Lexer & lex)
 {
        MathAtom at;
-       mathed_parse_normal(at, lex);
+       mathed_parse_normal(at, lex, Parse::NORMAL, &buffer());
        operator=(*at->asHullInset());
 }
 
@@ -1652,9 +1655,10 @@ void InsetMathHull::read(Lexer & lex)
 bool InsetMathHull::readQuiet(Lexer & lex)
 {
        MathAtom at;
-       bool result = mathed_parse_normal(at, lex, Parse::QUIET);
-       operator=(*at->asHullInset());
-       return result;
+       bool success = mathed_parse_normal(at, lex, Parse::QUIET, &buffer());
+       if (success)
+               operator=(*at->asHullInset());
+       return success;
 }
 
 
Index: src/mathed/InsetMathArray.cpp
===================================================================
--- src/mathed/InsetMathArray.cpp       (revisione 31790)
+++ src/mathed/InsetMathArray.cpp       (copia locale)
@@ -59,7 +59,9 @@ InsetMathArray::InsetMathArray(docstring
                addCol(0);
        for (row_type row = 0; row < dat.size(); ++row)
                for (col_type col = 0; col < dat[0].size(); ++col)
-                       mathed_parse_cell(cell(index(row, col)), 
from_utf8(dat[row][col]));
+                       mathed_parse_cell(cell(index(row, col)),
+                                         from_utf8(dat[row][col]),
+                                         Parse::NORMAL, &buffer());
 }
 
 
Index: src/mathed/MathMacroTemplate.h
===================================================================
--- src/mathed/MathMacroTemplate.h      (revisione 31790)
+++ src/mathed/MathMacroTemplate.h      (copia locale)
@@ -26,15 +26,15 @@ namespace lyx {
 class MathMacroTemplate : public InsetMathNest {
 public:
        ///
-       MathMacroTemplate();
+       MathMacroTemplate(Buffer * buf = 0);
        ///
        MathMacroTemplate(docstring const & name, int nargs, int optional, 
-               MacroType type,
+               MacroType type, Buffer * buf = 0,
                std::vector<MathData> const & optionalValues = 
std::vector<MathData>(),
                MathData const & def = MathData(),
                MathData const & display = MathData());
        ///
-       explicit MathMacroTemplate(const docstring & str);
+       explicit MathMacroTemplate(const docstring & str, Buffer * buf = 0);
        ///
        bool editable() const { return true; }
        ///
Index: src/mathed/InsetMathHull.h
===================================================================
--- src/mathed/InsetMathHull.h  (revisione 31790)
+++ src/mathed/InsetMathHull.h  (copia locale)
@@ -28,9 +28,9 @@ class RenderPreview;
 class InsetMathHull : public InsetMathGrid {
 public:
        ///
-       InsetMathHull();
+       InsetMathHull(Buffer * buf = 0);
        ///
-       explicit InsetMathHull(HullType type);
+       explicit InsetMathHull(HullType type, Buffer * buf = 0);
        ///
        ~InsetMathHull();
        ///
Index: src/mathed/MathMacroTemplate.cpp
===================================================================
--- src/mathed/MathMacroTemplate.cpp    (revisione 31790)
+++ src/mathed/MathMacroTemplate.cpp    (copia locale)
@@ -390,22 +390,24 @@ void InsetNameWrapper::draw(PainterInfo 
 ///////////////////////////////////////////////////////////////////////
 
 
-MathMacroTemplate::MathMacroTemplate()
+MathMacroTemplate::MathMacroTemplate(Buffer * buf)
        : InsetMathNest(3), numargs_(0), argsInLook_(0), optionals_(0),
          type_(MacroTypeNewcommand), lookOutdated_(true)
 {
+       buffer_ = buf;
        initMath();
 }
 
 
 MathMacroTemplate::MathMacroTemplate(docstring const & name, int numargs,
-       int optionals, MacroType type,
+       int optionals, MacroType type, Buffer * buf,
        vector<MathData> const & optionalValues,
        MathData const & def, MathData const & display)
        : InsetMathNest(optionals + 3), numargs_(numargs), argsInLook_(numargs),
          optionals_(optionals), optionalValues_(optionalValues),
          type_(type), lookOutdated_(true)
 {
+       buffer_ = buf;
        initMath();
 
        if (numargs_ > 9)
@@ -423,14 +425,15 @@ MathMacroTemplate::MathMacroTemplate(doc
 }
 
 
-MathMacroTemplate::MathMacroTemplate(docstring const & str)
+MathMacroTemplate::MathMacroTemplate(docstring const & str, Buffer * buf)
        : InsetMathNest(3), numargs_(0), optionals_(0),
        type_(MacroTypeNewcommand), lookOutdated_(true)
 {
+       buffer_ = buf;
        initMath();
 
        MathData ar;
-       mathed_parse_cell(ar, str);
+       mathed_parse_cell(ar, str, Parse::NORMAL, buf);
        if (ar.size() != 1 || !ar[0]->asMacroTemplate()) {
                lyxerr << "Cannot read macro from '" << ar << "'" << endl;
                asArray(from_ascii("invalidmacro"), cell(0));
@@ -1106,7 +1109,7 @@ bool MathMacroTemplate::getStatus(Cursor
 void MathMacroTemplate::read(Lexer & lex)
 {
        MathData ar;
-       mathed_parse_cell(ar, lex.getStream());
+       mathed_parse_cell(ar, lex.getStream(), Parse::NORMAL, &buffer());
        if (ar.size() != 1 || !ar[0]->asMacroTemplate()) {
                lyxerr << "Cannot read macro from '" << ar << "'" << endl;
                lyxerr << "Read: " << to_utf8(asString(ar)) << endl;
Index: src/mathed/MathParser.cpp
===================================================================
--- src/mathed/MathParser.cpp   (revisione 31790)
+++ src/mathed/MathParser.cpp   (copia locale)
@@ -64,6 +64,7 @@ following hack as starting point to writ
 #include "MathMacroArgument.h"
 #include "MathSupport.h"
 
+#include "Buffer.h"
 #include "Encoding.h"
 #include "Lexer.h"
 
@@ -359,12 +360,12 @@ public:
        typedef  Parse::flags parse_mode;
 
        ///
-       Parser(Lexer & lex, parse_mode mode);
+       Parser(Lexer & lex, parse_mode mode, Buffer * buf);
        /// Only use this for reading from .lyx file format, for the reason
        /// see Parser::tokenize(istream &).
-       Parser(istream & is, parse_mode mode);
+       Parser(istream & is, parse_mode mode, Buffer * buf);
        ///
-       Parser(docstring const & str, parse_mode mode);
+       Parser(docstring const & str, parse_mode mode, Buffer * buf);
 
        ///
        bool parse(MathAtom & at);
@@ -432,27 +433,36 @@ private:
        parse_mode mode_;
        ///
        bool success_;
+       ///
+       Buffer * buffer_;
 };
 
 
-Parser::Parser(Lexer & lexer, parse_mode mode)
-       : lineno_(lexer.lineNumber()), pos_(0), mode_(mode), success_(true)
+Parser::Parser(Lexer & lexer, parse_mode mode, Buffer * buf)
+       : lineno_(lexer.lineNumber()), pos_(0), mode_(mode), success_(true),
+         buffer_(buf)
 {
+       if (buf)
+               buf->updateMacros();
        tokenize(lexer.getStream());
        lexer.eatLine();
 }
 
 
-Parser::Parser(istream & is, parse_mode mode)
-       : lineno_(0), pos_(0), mode_(mode), success_(true)
+Parser::Parser(istream & is, parse_mode mode, Buffer * buf)
+       : lineno_(0), pos_(0), mode_(mode), success_(true), buffer_(buf)
 {
+       if (buf)
+               buf->updateMacros();
        tokenize(is);
 }
 
 
-Parser::Parser(docstring const & str, parse_mode mode)
-       : lineno_(0), pos_(0), mode_(mode), success_(true)
+Parser::Parser(docstring const & str, parse_mode mode, Buffer * buf)
+       : lineno_(0), pos_(0), mode_(mode), success_(true), buffer_(buf)
 {
+       if (buf)
+               buf->updateMacros();
        tokenize(str);
 }
 
@@ -825,14 +835,14 @@ bool Parser::parse1(InsetMathGrid & grid
                                Token const & n = getToken();
                                if (n.cat() == catMath) {
                                        // TeX's $$...$$ syntax for displayed 
math
-                                       cell->push_back(MathAtom(new 
InsetMathHull(hullEquation)));
+                                       cell->push_back(MathAtom(new 
InsetMathHull(hullEquation, buffer_)));
                                        parse2(cell->back(), FLAG_SIMPLE, 
InsetMath::MATH_MODE, false);
                                        getToken(); // skip the second '$' token
                                } else {
                                        // simple $...$  stuff
                                        putback();
                                        if (mode == InsetMath::UNDECIDED_MODE) {
-                                               cell->push_back(MathAtom(new 
InsetMathHull(hullSimple)));
+                                               cell->push_back(MathAtom(new 
InsetMathHull(hullSimple, buffer_)));
                                                parse2(cell->back(), 
FLAG_SIMPLE, InsetMath::MATH_MODE, false);
                                        } else {
                                                // Don't create nested math 
hulls (bug #5392)
@@ -1007,8 +1017,9 @@ bool Parser::parse1(InsetMathGrid & grid
                        if (nextToken().cat() == catBegin)
                                parse(display, FLAG_ITEM, InsetMath::MATH_MODE);
                        
-                       cell->push_back(MathAtom(new MathMacroTemplate(name, 
nargs,
-                              0, MacroTypeDef, vector<MathData>(), def, 
display)));
+                       cell->push_back(MathAtom(new MathMacroTemplate(name,
+                               nargs, 0, MacroTypeDef, buffer_,
+                               vector<MathData>(), def, display)));
                }
                
                else if (t.cs() == "newcommand" ||
@@ -1051,9 +1062,9 @@ bool Parser::parse1(InsetMathGrid & grid
                        if (nextToken().cat() == catBegin)
                                parse(display, FLAG_ITEM, InsetMath::MATH_MODE);
                        
-                       cell->push_back(MathAtom(new MathMacroTemplate(name, 
nargs,
-                               optionals, MacroTypeNewcommand, optionalValues, 
def, display)));
-                       
+                       cell->push_back(MathAtom(new MathMacroTemplate(name,
+                               nargs, optionals, MacroTypeNewcommand, buffer_,
+                               optionalValues, def, display)));
                }
                
                else if (t.cs() == "newcommandx" ||
@@ -1169,9 +1180,9 @@ bool Parser::parse1(InsetMathGrid & grid
                        if (nextToken().cat() == catBegin)
                                parse(display, FLAG_ITEM, InsetMath::MATH_MODE);
 
-                       cell->push_back(MathAtom(new MathMacroTemplate(name, 
nargs,
-                               optionals, MacroTypeNewcommandx, 
optionalValues, def, 
-                               display)));
+                       cell->push_back(MathAtom(new MathMacroTemplate(name,
+                               nargs, optionals, MacroTypeNewcommandx, buffer_,
+                               optionalValues, def, display)));
                }
 
                else if (t.cs() == "(") {
@@ -1179,7 +1190,7 @@ bool Parser::parse1(InsetMathGrid & grid
                                error("bad math environment");
                                break;
                        }
-                       cell->push_back(MathAtom(new 
InsetMathHull(hullSimple)));
+                       cell->push_back(MathAtom(new InsetMathHull(hullSimple, 
buffer_)));
                        parse2(cell->back(), FLAG_SIMPLE2, 
InsetMath::MATH_MODE, false);
                }
 
@@ -1188,7 +1199,7 @@ bool Parser::parse1(InsetMathGrid & grid
                                error("bad math environment");
                                break;
                        }
-                       cell->push_back(MathAtom(new 
InsetMathHull(hullEquation)));
+                       cell->push_back(MathAtom(new 
InsetMathHull(hullEquation, buffer_)));
                        parse2(cell->back(), FLAG_EQUATION, 
InsetMath::MATH_MODE, false);
                }
 
@@ -1446,7 +1457,7 @@ bool Parser::parse1(InsetMathGrid & grid
                                        error("bad math environment");
                                        break;
                                }
-                               cell->push_back(MathAtom(new 
InsetMathHull(hullSimple)));
+                               cell->push_back(MathAtom(new 
InsetMathHull(hullSimple, buffer_)));
                                parse2(cell->back(), FLAG_END, 
InsetMath::MATH_MODE, true);
                        }
 
@@ -1456,7 +1467,7 @@ bool Parser::parse1(InsetMathGrid & grid
                                        error("bad math environment");
                                        break;
                                }
-                               cell->push_back(MathAtom(new 
InsetMathHull(hullEquation)));
+                               cell->push_back(MathAtom(new 
InsetMathHull(hullEquation, buffer_)));
                                parse2(cell->back(), FLAG_END, 
InsetMath::MATH_MODE, (name == "equation"));
                        }
 
@@ -1465,7 +1476,7 @@ bool Parser::parse1(InsetMathGrid & grid
                                        error("bad math environment");
                                        break;
                                }
-                               cell->push_back(MathAtom(new 
InsetMathHull(hullEqnArray)));
+                               cell->push_back(MathAtom(new 
InsetMathHull(hullEqnArray, buffer_)));
                                parse2(cell->back(), FLAG_END, 
InsetMath::MATH_MODE, !stared(name));
                        }
 
@@ -1474,7 +1485,7 @@ bool Parser::parse1(InsetMathGrid & grid
                                        error("bad math environment");
                                        break;
                                }
-                               cell->push_back(MathAtom(new 
InsetMathHull(hullAlign)));
+                               cell->push_back(MathAtom(new 
InsetMathHull(hullAlign, buffer_)));
                                parse2(cell->back(), FLAG_END, 
InsetMath::MATH_MODE, !stared(name));
                        }
 
@@ -1483,7 +1494,7 @@ bool Parser::parse1(InsetMathGrid & grid
                                        error("bad math environment");
                                        break;
                                }
-                               cell->push_back(MathAtom(new 
InsetMathHull(hullFlAlign)));
+                               cell->push_back(MathAtom(new 
InsetMathHull(hullFlAlign, buffer_)));
                                parse2(cell->back(), FLAG_END, 
InsetMath::MATH_MODE, !stared(name));
                        }
 
@@ -1494,7 +1505,7 @@ bool Parser::parse1(InsetMathGrid & grid
                                }
                                // ignore this for a while
                                getArg('{', '}');
-                               cell->push_back(MathAtom(new 
InsetMathHull(hullAlignAt)));
+                               cell->push_back(MathAtom(new 
InsetMathHull(hullAlignAt, buffer_)));
                                parse2(cell->back(), FLAG_END, 
InsetMath::MATH_MODE, !stared(name));
                        }
 
@@ -1505,7 +1516,7 @@ bool Parser::parse1(InsetMathGrid & grid
                                }
                                // ignore this for a while
                                getArg('{', '}');
-                               cell->push_back(MathAtom(new 
InsetMathHull(hullXAlignAt)));
+                               cell->push_back(MathAtom(new 
InsetMathHull(hullXAlignAt, buffer_)));
                                parse2(cell->back(), FLAG_END, 
InsetMath::MATH_MODE, !stared(name));
                        }
 
@@ -1516,7 +1527,7 @@ bool Parser::parse1(InsetMathGrid & grid
                                }
                                // ignore this for a while
                                getArg('{', '}');
-                               cell->push_back(MathAtom(new 
InsetMathHull(hullXXAlignAt)));
+                               cell->push_back(MathAtom(new 
InsetMathHull(hullXXAlignAt, buffer_)));
                                parse2(cell->back(), FLAG_END, 
InsetMath::MATH_MODE, !stared(name));
                        }
 
@@ -1525,7 +1536,7 @@ bool Parser::parse1(InsetMathGrid & grid
                                        error("bad math environment");
                                        break;
                                }
-                               cell->push_back(MathAtom(new 
InsetMathHull(hullMultline)));
+                               cell->push_back(MathAtom(new 
InsetMathHull(hullMultline, buffer_)));
                                parse2(cell->back(), FLAG_END, 
InsetMath::MATH_MODE, !stared(name));
                        }
 
@@ -1534,7 +1545,7 @@ bool Parser::parse1(InsetMathGrid & grid
                                        error("bad math environment");
                                        break;
                                }
-                               cell->push_back(MathAtom(new 
InsetMathHull(hullGather)));
+                               cell->push_back(MathAtom(new 
InsetMathHull(hullGather, buffer_)));
                                parse2(cell->back(), FLAG_END, 
InsetMath::MATH_MODE, !stared(name));
                        }
 
@@ -1681,7 +1692,8 @@ bool Parser::parse1(InsetMathGrid & grid
                                // we must not create an InsetMathSpace.
                                cell->push_back(MathAtom(new MathMacro(name)));
                                MathData ar;
-                               mathed_parse_cell(ar, '{' + arg + '}');
+                               mathed_parse_cell(ar, '{' + arg + '}',
+                                               Parse::NORMAL, buffer_);
                                cell->append(ar);
                        }
                }
@@ -1749,8 +1761,10 @@ bool Parser::parse1(InsetMathGrid & grid
                }
 
                else if (t.cs().size()) {
+                       bool const is_user_macro =
+                               buffer_ && buffer_->getMacro(t.cs(), false);
                        latexkeys const * l = in_word_set(t.cs());
-                       if (l) {
+                       if (l && !is_user_macro) {
                                if (l->inset == "big") {
                                        skipSpaces();
                                        docstring const delim = 
getToken().asInput();
@@ -1800,7 +1814,7 @@ bool Parser::parse1(InsetMathGrid & grid
 
                        else {
                                bool is_unicode_symbol = false;
-                               if (mode == InsetMath::TEXT_MODE) {
+                               if (mode == InsetMath::TEXT_MODE && 
!is_user_macro) {
                                        int num_tokens = 0;
                                        docstring cmd = prevToken().asInput();
                                        CatCode cat = nextToken().cat();
@@ -1837,7 +1851,9 @@ bool Parser::parse1(InsetMathGrid & grid
                                        }
                                }
                                if (!is_unicode_symbol) {
-                                       MathAtom at = createInsetMath(t.cs());
+                                       MathAtom at = is_user_macro ?
+                                               MathAtom(new MathMacro(t.cs()))
+                                               : createInsetMath(t.cs());
                                        InsetMath::mode_type m = mode;
                                        //if (m == InsetMath::UNDECIDED_MODE)
                                        //lyxerr << "default creation: m1: " << 
m << endl;
@@ -1875,35 +1891,40 @@ bool Parser::parse1(InsetMathGrid & grid
 } // anonymous namespace
 
 
-bool mathed_parse_cell(MathData & ar, docstring const & str, Parse::flags f)
+bool mathed_parse_cell(MathData & ar, docstring const & str,
+                      Parse::flags f, Buffer * buf)
 {
-       return Parser(str, f).parse(ar, 0, f & Parse::TEXTMODE ?
+       return Parser(str, f, buf).parse(ar, 0, f & Parse::TEXTMODE ?
                                InsetMath::TEXT_MODE : InsetMath::MATH_MODE);
 }
 
 
-bool mathed_parse_cell(MathData & ar, istream & is, Parse::flags f)
+bool mathed_parse_cell(MathData & ar, istream & is,
+                      Parse::flags f, Buffer * buf)
 {
-       return Parser(is, f).parse(ar, 0, f & Parse::TEXTMODE ?
+       return Parser(is, f, buf).parse(ar, 0, f & Parse::TEXTMODE ?
                                InsetMath::TEXT_MODE : InsetMath::MATH_MODE);
 }
 
 
-bool mathed_parse_normal(MathAtom & t, docstring const & str, Parse::flags f)
+bool mathed_parse_normal(MathAtom & t, docstring const & str,
+                        Parse::flags f, Buffer * buf)
 {
-       return Parser(str, f).parse(t);
+       return Parser(str, f, buf).parse(t);
 }
 
 
-bool mathed_parse_normal(MathAtom & t, Lexer & lex, Parse::flags f)
+bool mathed_parse_normal(MathAtom & t, Lexer & lex,
+                        Parse::flags f, Buffer * buf)
 {
-       return Parser(lex, f).parse(t);
+       return Parser(lex, f, buf).parse(t);
 }
 
 
-bool mathed_parse_normal(InsetMathGrid & grid, docstring const & str, 
Parse::flags f)
+bool mathed_parse_normal(InsetMathGrid & grid, docstring const & str,
+                        Parse::flags f, Buffer * buf)
 {
-       return Parser(str, f).parse1(grid, 0, f & Parse::TEXTMODE ?
+       return Parser(str, f, buf).parse1(grid, 0, f & Parse::TEXTMODE ?
                        InsetMath::TEXT_MODE : InsetMath::MATH_MODE, false);
 }
 
Index: src/mathed/InsetMathNest.cpp
===================================================================
--- src/mathed/InsetMathNest.cpp        (revisione 31790)
+++ src/mathed/InsetMathNest.cpp        (copia locale)
@@ -977,7 +977,7 @@ void InsetMathNest::doDispatch(Cursor & 
 #else
                if (currentMode() == Inset::TEXT_MODE) {
                        cur.recordUndoSelection();
-                       cur.niceInsert(MathAtom(new InsetMathHull("simple")));
+                       cur.niceInsert(MathAtom(new InsetMathHull("simple", 
cur.buffer())));
                        cur.message(_("create new math text environment 
($...$)"));
                } else {
                        handleFont(cur, cmd.argument(), "textrm");
@@ -996,7 +996,7 @@ void InsetMathNest::doDispatch(Cursor & 
                cur.macroModeClose();
                docstring const save_selection = grabAndEraseSelection(cur);
                selClearOrDel(cur);
-               cur.plainInsert(MathAtom(new InsetMathHull(hullRegexp)));
+               cur.plainInsert(MathAtom(new InsetMathHull(hullRegexp, 
cur.buffer())));
                cur.posBackward();
                cur.pushBackward(*cur.nextInset());
                cur.niceInsert(save_selection);
Index: src/mathed/MathParser.h
===================================================================
--- src/mathed/MathParser.h     (revisione 31790)
+++ src/mathed/MathParser.h     (copia locale)
@@ -21,6 +21,7 @@
 
 namespace lyx {
 
+class Buffer;
 class MathAtom;
 class MathData;
 class InsetMathGrid;
@@ -61,17 +62,25 @@ public:
 latexkeys const * in_word_set(docstring const & str);
 
 /// parse formula from a string
-bool mathed_parse_normal(MathAtom &, docstring const &, Parse::flags f = 
Parse::NORMAL);
-/// ... the LyX lexxer
-bool mathed_parse_normal(MathAtom &, Lexer &, Parse::flags f = Parse::NORMAL);
+bool mathed_parse_normal(MathAtom &, docstring const &,
+               Parse::flags f = Parse::NORMAL, Buffer * buf = 0);
+
+/// parse formula from the LyX lexxer
+bool mathed_parse_normal(MathAtom &, Lexer &,
+               Parse::flags f = Parse::NORMAL, Buffer * buf = 0);
+
 /// parse formula from a string into a grid
-bool mathed_parse_normal(InsetMathGrid &, docstring const &, Parse::flags f = 
Parse::NORMAL);
+bool mathed_parse_normal(InsetMathGrid &, docstring const &,
+               Parse::flags f = Parse::NORMAL, Buffer * buf = 0);
 
 /// parse a single cell from a string
-bool mathed_parse_cell(MathData & ar, docstring const &, Parse::flags f = 
Parse::NORMAL);
+bool mathed_parse_cell(MathData & ar, docstring const &,
+               Parse::flags f = Parse::NORMAL, Buffer * buf = 0);
+
 /// parse a single cell from a stream. Only use this for reading from .lyx
 /// file format, for the reason see Parser::tokenize(std::istream &).
-bool mathed_parse_cell(MathData & ar, std::istream &, Parse::flags f = 
Parse::NORMAL);
+bool mathed_parse_cell(MathData & ar, std::istream &,
+               Parse::flags f = Parse::NORMAL, Buffer * buf = 0);
 
 void initParser();
 
Index: src/factory.cpp
===================================================================
--- src/factory.cpp     (revisione 31790)
+++ src/factory.cpp     (copia locale)
@@ -531,9 +531,9 @@ Inset * readInset(Lexer & lex, Buffer co
                } else if (tmptok == "External") {
                        inset.reset(new InsetExternal(const_cast<Buffer 
&>(buf)));
                } else if (tmptok == "FormulaMacro") {
-                       inset.reset(new MathMacroTemplate);
+                       inset.reset(new MathMacroTemplate(&const_cast<Buffer 
&>(buf)));
                } else if (tmptok == "Formula") {
-                       inset.reset(new InsetMathHull);
+                       inset.reset(new InsetMathHull(&const_cast<Buffer 
&>(buf)));
                } else if (tmptok == "Graphics") {
                        inset.reset(new InsetGraphics(const_cast<Buffer 
&>(buf)));
                } else if (tmptok == "Note") {
Index: src/Text3.cpp
===================================================================
--- src/Text3.cpp       (revisione 31790)
+++ src/Text3.cpp       (copia locale)
@@ -141,7 +141,7 @@ static void mathDispatch(Cursor & cur, F
 #ifdef ENABLE_ASSERTIONS
                const int old_pos = cur.pos();
 #endif
-               cur.insert(new InsetMathHull(hullSimple));
+               cur.insert(new InsetMathHull(hullSimple, cur.buffer()));
 #ifdef ENABLE_ASSERTIONS
                LASSERT(old_pos == cur.pos(), /**/);
 #endif
@@ -165,7 +165,7 @@ static void mathDispatch(Cursor & cur, F
                                && sel.find(from_ascii("\\newlyxcommand")) == 
string::npos
                                && sel.find(from_ascii("\\def")) == 
string::npos)
                {
-                       InsetMathHull * formula = new InsetMathHull;
+                       InsetMathHull * formula = new 
InsetMathHull(cur.buffer());
                        string const selstr = to_utf8(sel);
                        istringstream is(selstr);
                        Lexer lex;
@@ -184,7 +184,7 @@ static void mathDispatch(Cursor & cur, F
                        } else
                                cur.insert(formula);
                } else {
-                       cur.insert(new MathMacroTemplate(sel));
+                       cur.insert(new MathMacroTemplate(sel, cur.buffer()));
                }
        }
        if (valid)
@@ -207,7 +207,7 @@ void regexpDispatch(Cursor & cur, FuncRe
        // It may happen that sel is empty but there is a selection
        replaceSelection(cur);
 
-       cur.insert(new InsetMathHull(hullRegexp));
+       cur.insert(new InsetMathHull(hullRegexp, cur.buffer()));
        cur.nextInset()->edit(cur, true);
        cur.niceInsert(sel);
 
@@ -1753,7 +1753,7 @@ void Text::dispatch(Cursor & cur, FuncRe
                        MacroType type = MacroTypeNewcommand;
                        if (s2 == "def")
                                type = MacroTypeDef;
-                       MathMacroTemplate * inset = new 
MathMacroTemplate(from_utf8(token(s, ' ', 0)), nargs, false, type);
+                       MathMacroTemplate * inset = new 
MathMacroTemplate(from_utf8(token(s, ' ', 0)), nargs, false, type, 
cur.buffer());
                        inset->setBuffer(bv->buffer());
                        insertInset(cur, inset);
 
@@ -1781,7 +1781,7 @@ void Text::dispatch(Cursor & cur, FuncRe
        case LFUN_MATH_BIGDELIM: {
                cur.recordUndo();
                cap::replaceSelection(cur);
-               cur.insert(new InsetMathHull(hullSimple));
+               cur.insert(new InsetMathHull(hullSimple, cur.buffer()));
                checkAndActivateInset(cur, true);
                LASSERT(cur.inMathed(), /**/);
                cur.dispatch(cmd);
Index: src/Cursor.cpp
===================================================================
--- src/Cursor.cpp      (revisione 31790)
+++ src/Cursor.cpp      (copia locale)
@@ -1440,7 +1440,8 @@ bool Cursor::macroModeClose()
        InsetMathNest * const in = inset().asInsetMath()->asNestInset();
        if (in && in->interpretString(*this, s))
                return true;
-       MathAtom atom = createInsetMath(name);
+       MathAtom atom = buffer()->getMacro(name, *this, false) ?
+               MathAtom(new MathMacro(name)) : createInsetMath(name);
 
        // try to put argument into macro, if we just inserted a macro
        bool macroArg = false;

Reply via email to