]> git.lyx.org Git - lyx.git/blobdiff - src/mathed/formula.C
compilation fixes when using lyxstring
[lyx.git] / src / mathed / formula.C
index c16e3a0b58447a23e9cd02dc36e15f5b1f58e40d..48d0b55581e30c0bd417f6a58e98afc618fa8b36 100644 (file)
@@ -14,6 +14,7 @@
 */
 
 #include <config.h>
+#include <fstream>
 
 #include "Lsstream.h"
 
@@ -26,7 +27,6 @@
 #include "math_cursor.h"
 #include "math_parser.h"
 #include "lyx_main.h"
-#include "minibuffer.h"
 #include "BufferView.h"
 #include "lyxtext.h"
 #include "gettext.h"
 #include "debug.h"
 #include "lyx_gui_misc.h"
 #include "support/LOstream.h"
+#include "support/lyxlib.h"
+#include "support/syscall.h"
 #include "LyXView.h"
 #include "Painter.h"
 #include "font.h"
-#include "support/lyxlib.h"
 #include "lyxrc.h"
 #include "math_inset.h"
 #include "math_parinset.h"
@@ -47,7 +48,9 @@
 #include "math_deliminset.h"
 #include "mathed/support.h"
 
+using std::ostringstream;
 using std::ostream;
+using std::ifstream;
 using std::istream;
 using std::pair;
 using std::endl;
@@ -64,27 +67,28 @@ extern char const * latex_mathenv[];
 // this is only used by Whichfont and mathed_init_fonts (Lgb)
 LyXFont * Math_Fonts = 0;
 
-static LyXFont::FONT_SIZE lfont_size = LyXFont::SIZE_NORMAL;
 
-// local global
-static int sel_x;
-static int sel_y;
-static bool sel_flag;
+MathedCursor * mathcursor = 0;
 
-// quite a hack i know. Should be done with return values...
-int number_of_newlines = 0;
 
-static
+namespace {
+
+LyXFont::FONT_SIZE lfont_size = LyXFont::SIZE_NORMAL;
+
+// local global
+int sel_x;
+int sel_y;
+bool sel_flag;
+
 int mathed_write(MathParInset *, std::ostream &, bool fragile,
                 string const & label = string());
 
 void mathed_init_fonts();
 
-static
 void mathedValidate(LaTeXFeatures & features, MathParInset * par);
 
+} // namespaces
 
-MathedCursor * InsetFormula::mathcursor = 0;
 
 
 LyXFont WhichFont(short type, int size)
@@ -169,6 +173,8 @@ LyXFont WhichFont(short type, int size)
 }
 
 
+namespace {
+
 void mathed_init_fonts() //removed 'static' because DEC cxx does not
 //like it (JMarc)
 // Probably because this func is declared as a friend in math_defs.h
@@ -205,6 +211,11 @@ void mathed_init_fonts() //removed 'static' because DEC cxx does not
        MathedInset::defaultWidth(lyxfont::width('I', f));
 }
 
+} // namespace anon
+
+
+// quite a hack i know. Should be done with return values...
+int number_of_newlines = 0;
 
 
 InsetFormula::InsetFormula(bool display)
@@ -229,21 +240,22 @@ InsetFormula::InsetFormula(MathParInset * p)
                new MathParInset(*p);
        //   mathcursor = 0;
 
-       disp_flag_ = (par->GetType()>0);
+       disp_flag_ = par->GetType() > 0;
 }
 
 
 InsetFormula::~InsetFormula()
 {
-       delete par;
+#ifdef WITH_WARNINGS
+#warning leak this for a while...
+#endif
+       //delete par;
 }
 
 
 Inset * InsetFormula::Clone(Buffer const &) const
 {
-       InsetFormula * f = new InsetFormula(par);
-       f->label_ = label_;
-       return f;
+       return new InsetFormula(par);
 }
 
 
@@ -256,9 +268,7 @@ void InsetFormula::Write(Buffer const * buf, ostream & os) const
 
 int InsetFormula::Latex(Buffer const *, ostream & os, bool fragile, bool) const
 {
-       //#warning Alejandro, the number of lines is not returned in this case
-       // This problem will disapear at 0.13.
-       return mathed_write(par, os, fragile, label_);
+       return mathed_write(par, os, fragile, par->label()); 
 }
 
 
@@ -300,22 +310,25 @@ void InsetFormula::Read(Buffer const *buffer, LyXLex & lex)
 
        mathed_parser_file(is, lex.GetLineNo());
 
-       // Silly hack to read labels.
-       mathed_label.erase();
-
        MathedArray ar;
-       mathed_parse(ar, 0, &par);
+       mathed_parse(ar, par, 0);
+       if (par->isMatrix()) {
+               //lyxerr << "################## InsetFormula::Read: IsMatrix\n";
+               static_cast<MathMatrixInset*>(par)->setData(ar);
+       }
+       else {
+               par->setData(ar);
+               //lyxerr << "################## InsetFormula::Read: keine Matrix\n";
+       }
+
+       //lyxerr << "InsetFormula::Read: par: " << par << " " << par->GetData() << endl;
+
        par->Metrics();
        disp_flag_ = (par->GetType() > 0);
 
        // Update line number
        lex.setLineNo(mathed_parser_lineno());
 
-       if (!mathed_label.empty()) {
-               label_ = mathed_label;
-               mathed_label.erase();
-       }
-
        // reading of end_inset in the inset!!!
        while (lex.IsOK()) {
                lex.nextToken();
@@ -385,15 +398,13 @@ void InsetFormula::draw(BufferView * bv, LyXFont const & f,
 
                if (is_singlely_numbered(par->GetType())) {
                        string str;
-                       if (!label_.empty())
-                               str = string("(") + label_ + ")";
+                       if (!par->label().empty())
+                               str = string("(") + par->label() + ")";
                        else
                                str = string("(#)");
                        pain.text(int(x + 20), baseline, str, wfont);
                } else {
-                       MathMatrixInset * mt =
-                               static_cast<MathMatrixInset*>(par);
-                       MathedRowSt const * crow = mt->getRowSt();
+                       MathedRowContainer::iterator crow = par->getRowSt().begin();
                        while (crow) {
                                int const y = baseline + crow->getBaseline();
                                if (crow->isNumbered()) {
@@ -404,11 +415,11 @@ void InsetFormula::draw(BufferView * bv, LyXFont const & f,
                                                str = "(#)";
                                        pain.text(int(x + 20), y, str, wfont);
                                }
-                               crow = crow->getNext();
+                               ++crow;
                        }
                }
        }
-       cursor_visible = false;
+       setCursorVisible(false);
 }
 
 
@@ -451,12 +462,12 @@ void InsetFormula::InsetUnlock(BufferView * bv)
 
 
 // Now a symbol can be inserted only if the inset is locked
-void InsetFormula::InsertSymbol(BufferView * bv, string const & s)
+void InsetFormula::InsertSymbol(BufferView *, string const & s)
 {
        if (s.empty() || !mathcursor)
                return;
        mathcursor->Interpret(s);
-       UpdateLocal(bv);
+       // Andre: UpdateLocal(bv);
 }
 
 
@@ -479,21 +490,21 @@ void InsetFormula::ToggleInsetCursor(BufferView * bv)
        //  x -= par->xo;
        y -= par->yo();
        LyXFont font = WhichFont(LM_TC_TEXTRM, LM_ST_TEXT);
-       int asc = lyxfont::maxAscent(font);
-       int desc = lyxfont::maxDescent(font);
+       int const asc = lyxfont::maxAscent(font);
+       int const desc = lyxfont::maxDescent(font);
 
-       if (cursor_visible)
+       if (isCursorVisible())
                bv->hideLockedInsetCursor();
        else
                bv->showLockedInsetCursor(x, y, asc, desc);
 
-       cursor_visible = !cursor_visible;
+       toggleCursorVisible();
 }
 
 
 void InsetFormula::ShowInsetCursor(BufferView * bv, bool)
 {
-       if (!cursor_visible) {
+       if (!isCursorVisible()) {
                if (mathcursor) {
                        int x;
                        int y;
@@ -501,8 +512,8 @@ void InsetFormula::ShowInsetCursor(BufferView * bv, bool)
                        //  x -= par->xo;
                        y -= par->yo();
                        LyXFont font = WhichFont(LM_TC_TEXTRM, LM_ST_TEXT);
-                       int asc = lyxfont::maxAscent(font);
-                       int desc = lyxfont::maxDescent(font);
+                       int const asc = lyxfont::maxAscent(font);
+                       int const desc = lyxfont::maxDescent(font);
                        bv->fitLockedInsetCursor(x, y, asc, desc);
                }
                ToggleInsetCursor(bv);
@@ -512,7 +523,7 @@ void InsetFormula::ShowInsetCursor(BufferView * bv, bool)
 
 void InsetFormula::HideInsetCursor(BufferView * bv)
 {
-       if (cursor_visible)
+       if (isCursorVisible())
                ToggleInsetCursor(bv);
 }
 
@@ -539,9 +550,14 @@ void InsetFormula::display(bool dspf)
                        }
                        par->SetType(LM_OT_MIN);
                        par->SetStyle(LM_ST_TEXT);
+#ifdef WITH_WARNINGS
+#warning Labels
+#endif
+/*
                        if (!label_.empty()) {
                                label_.erase();
                        }
+*/
                }
                disp_flag_ = dspf;
        }
@@ -550,21 +566,10 @@ void InsetFormula::display(bool dspf)
 
 vector<string> const InsetFormula::getLabelList() const
 {
-       //#warning This is dirty, I know. Ill clean it at 0.11
-       // Correction, the only way to clean this is with a new kernel: 0.13.
-
        vector<string> label_list;
-
-       if (is_multi_numbered(par->GetType())) {
-               MathMatrixInset * mt = static_cast<MathMatrixInset*>(par);
-               MathedRowSt const * crow = mt->getRowSt();
-               while (crow) {
-                       if (!crow->getLabel().empty())
-                               label_list.push_back(crow->getLabel());
-                       crow = crow->getNext();
-               }
-       } else if (!label_.empty())
-               label_list.push_back(label_);
+       MathedRowContainer::iterator crow = par->getRowSt().begin();
+       for ( ; crow; ++crow)
+               label_list.push_back(crow->getLabel());
 
        return label_list;
 }
@@ -661,7 +666,14 @@ UpdatableInset::RESULT
 InsetFormula::LocalDispatch(BufferView * bv, kb_action action,
                            string const & arg)
 {
+       //lyxerr << "InsetFormula::LocalDispatch: act: " << action
+       //      << " arg: '" << arg << "' cursor: " << mathcursor << "\n";
        //   extern char *dispatch_result;
+
+       if (!mathcursor) {
+               return UNDISPATCHED;
+       }
+
        MathedTextCodes varcode = LM_TC_MIN;
        bool was_macro = mathcursor->InMacroMode();
        bool sel = false;
@@ -685,6 +697,7 @@ InsetFormula::LocalDispatch(BufferView * bv, kb_action action,
                result = DISPATCH_RESULT(mathcursor->Right(sel));
                if (!sel && (result == DISPATCHED))
                        result = DISPATCHED_NOUPDATE;
+               UpdateLocal(bv);
                break;
 
 
@@ -695,6 +708,7 @@ InsetFormula::LocalDispatch(BufferView * bv, kb_action action,
                result = DISPATCH_RESULT(mathcursor->Left(sel));
                if (!sel && (result == DISPATCHED))
                        result = DISPATCHED_NOUPDATE;
+               UpdateLocal(bv);
                break;
 
 
@@ -705,6 +719,7 @@ InsetFormula::LocalDispatch(BufferView * bv, kb_action action,
                result = DISPATCH_RESULT(mathcursor->Up(sel));
                if (!sel && (result == DISPATCHED))
                        result = DISPATCHED_NOUPDATE;
+               UpdateLocal(bv);
                break;
 
 
@@ -715,6 +730,7 @@ InsetFormula::LocalDispatch(BufferView * bv, kb_action action,
                result = DISPATCH_RESULT(mathcursor->Down(sel));
                if (!sel && (result == DISPATCHED))
                        result = DISPATCHED_NOUPDATE;
+               UpdateLocal(bv);
                break;
 
 
@@ -731,6 +747,7 @@ InsetFormula::LocalDispatch(BufferView * bv, kb_action action,
        case LFUN_DELETE_LINE_FORWARD:
                bv->lockedInsetStoreUndo(Undo::DELETE);
                mathcursor->DelLine();
+               // Andre:
                UpdateLocal(bv);
                break;
 
@@ -739,11 +756,17 @@ InsetFormula::LocalDispatch(BufferView * bv, kb_action action,
                bv->lockedInsetStoreUndo(Undo::INSERT);
                byte c = arg.empty() ? '1' : arg[0];
                mathcursor->Insert(c, LM_TC_CR);
-               if (!label_.empty()) {
+#ifdef WITH_WARNINGS
+#warning Labels
+#endif
+/*
+               if (!par->label().empty()) {
                        mathcursor->setLabel(label_);
                        label_.erase();
                }
+*/
                par = mathcursor->GetPar();
+               // Andre:
                UpdateLocal(bv);
        }
        break;
@@ -757,6 +780,7 @@ InsetFormula::LocalDispatch(BufferView * bv, kb_action action,
        case LFUN_TABINSERT:
                bv->lockedInsetStoreUndo(Undo::INSERT);
                mathcursor->Insert('T', LM_TC_TAB);
+               // Andre:
                UpdateLocal(bv);
                break;
 
@@ -800,12 +824,14 @@ InsetFormula::LocalDispatch(BufferView * bv, kb_action action,
                        mathcursor->MacroModeClose();
                bv->lockedInsetStoreUndo(Undo::INSERT);
                mathcursor->SelPaste();
+               // Andre:
                UpdateLocal(bv);
                break;
 
        case LFUN_CUT:
                bv->lockedInsetStoreUndo(Undo::DELETE);
                mathcursor->SelCut();
+               // Andre:
                UpdateLocal(bv);
                break;
 
@@ -836,7 +862,7 @@ InsetFormula::LocalDispatch(BufferView * bv, kb_action action,
        case LFUN_GREEK:
                if (!greek_kb_flag) {
                        greek_kb_flag = 1;
-                       bv->owner()->getMiniBuffer()->Set(_("Math greek mode on"));
+                       bv->owner()->message(_("Math greek mode on"));
                } else
                        greek_kb_flag = 0;
                break;
@@ -845,9 +871,9 @@ InsetFormula::LocalDispatch(BufferView * bv, kb_action action,
        case LFUN_GREEK_TOGGLE:
                greek_kb_flag = (greek_kb_flag) ? 0 : 2;
                if (greek_kb_flag)
-                       bv->owner()->getMiniBuffer()->Set(_("Math greek keyboard on"));
+                       bv->owner()->message(_("Math greek keyboard on"));
                else
-                       bv->owner()->getMiniBuffer()->Set(_("Math greek keyboard off"));
+                       bv->owner()->message(_("Math greek keyboard off"));
                break;
 
                //  Math fonts
@@ -861,7 +887,7 @@ InsetFormula::LocalDispatch(BufferView * bv, kb_action action,
        case LFUN_TEX:
                // varcode = LM_TC_TEX;
                mathcursor->setLastCode(LM_TC_TEX);
-               bv->owner()->getMiniBuffer()->Set(_("TeX mode"));
+               bv->owner()->message(_("TeX mode"));
                break;
 
        case LFUN_MATH_NUMBER:
@@ -870,31 +896,39 @@ InsetFormula::LocalDispatch(BufferView * bv, kb_action action,
                        short type = par->GetType();
                        if (is_numbered(type)) {
                                --type;
+#ifdef WITH_WARNINGS
+#warning Labels
+#endif
+/*
                                if (!label_.empty()) {
                                        label_.erase();
                                }
-                               bv->owner()->getMiniBuffer()->Set(_("No number"));
+*/
+                               bv->owner()->message(_("No number"));
                        } else {
                                ++type;
-                               bv->owner()->getMiniBuffer()->Set(_("Number"));
+                               bv->owner()->message(_("Number"));
                        }
                        par->SetType(type);
+                       // Andre:
                        UpdateLocal(bv);
                }
                break;
 
        case LFUN_MATH_NONUMBER:
                if (is_multi_numbered(par->GetType())) {
-                               //         MathMatrixInset *mt = (MathMatrixInset*)par;
                                //BUG
-                               //         mt->SetNumbered(!mt->IsNumbered());
+                               //         par->SetNumbered(!par->IsNumbered());
 
+#ifdef WITH_WARNINGS
 #warning This is a terrible hack! We should find a better solution.
+#endif
                        while (mathcursor->getLabel() == MathedXIter::error_label) {
                                if (LocalDispatch(bv, LFUN_LEFT, string()) == FINISHED)
                                        return DISPATCHED;
                        }
                        mathcursor->setNumbered();
+                       // Andre:
                        UpdateLocal(bv);
                }
                break;
@@ -902,14 +936,16 @@ InsetFormula::LocalDispatch(BufferView * bv, kb_action action,
        case LFUN_MATH_LIMITS:
                bv->lockedInsetStoreUndo(Undo::INSERT);
                if (mathcursor->Limits())
+                       // Andre:
                        UpdateLocal(bv);
                // fall through!
 
        case LFUN_MATH_SIZE:
                if (!arg.empty()) {
                        latexkeys const * l = in_word_set(arg);
-                       int const sz = (l) ? l->id: -1;
+                       int const sz = l ? l->id : -1;
                        mathcursor->SetSize(sz);
+                       // Andre:
                        UpdateLocal(bv);
                        break;
                }
@@ -918,6 +954,7 @@ InsetFormula::LocalDispatch(BufferView * bv, kb_action action,
        case LFUN_INSERT_MATH:
                bv->lockedInsetStoreUndo(Undo::INSERT);
                InsertSymbol(bv, arg);
+               UpdateLocal(bv);
                break;
 
 
@@ -940,11 +977,12 @@ InsetFormula::LocalDispatch(BufferView * bv, kb_action action,
                        n = 1;
                }
 
-               MathMatrixInset * p = new MathMatrixInset(m, n);
-               if (mathcursor && p) {
+               if (mathcursor) {
+                       MathMatrixInset * p = new MathMatrixInset(m, n);
                        if (k > 2 && int(strlen(s)) > m)
                                p->SetAlign(s[0], &s[1]);
                        mathcursor->insertInset(p, LM_TC_ACTIVE_INSET);
+                       // Andre:
                        UpdateLocal(bv);
                }
                break;
@@ -974,7 +1012,7 @@ InsetFormula::LocalDispatch(BufferView * bv, kb_action action,
                                ilf = lyx::atoi(lf);
                        else
                                if (lf[1]) {
-                                       l = in_word_set(lf, strlen(lf));
+                                       l = in_word_set(lf);
                                        // Long words will cause l == 0; so check.
                                        if (l)
                                                ilf = l->id;
@@ -986,7 +1024,7 @@ InsetFormula::LocalDispatch(BufferView * bv, kb_action action,
                                        irg = lyx::atoi(rg);
                                else
                                        if (rg[1]) {
-                                               l = in_word_set(rg, strlen(rg));
+                                               l = in_word_set(rg);
                                                if (l)
                                                        irg = l->id;
                                        } else if (vdelim.find(rg[0]) != string::npos)
@@ -996,6 +1034,7 @@ InsetFormula::LocalDispatch(BufferView * bv, kb_action action,
 
                MathDelimInset * p = new MathDelimInset(ilf, irg);
                mathcursor->insertInset(p, LM_TC_ACTIVE_INSET);
+               // Andre:
                UpdateLocal(bv);
                break;
        }
@@ -1005,6 +1044,7 @@ InsetFormula::LocalDispatch(BufferView * bv, kb_action action,
                sp = new MathSpaceInset(1);
                mathcursor->insertInset(sp, LM_TC_INSET);
                space_on = true;
+               // Andre:
                UpdateLocal(bv);
                break;
 
@@ -1015,9 +1055,11 @@ InsetFormula::LocalDispatch(BufferView * bv, kb_action action,
                        break;
 
                string old_label = is_multiline(par->GetType())
-                       ? mathcursor->getLabel() : label_;
+                       ? mathcursor->getLabel() : par->label();
 
+#ifdef WITH_WARNINGS
 #warning This is a terrible hack! We should find a better solution.
+#endif
                // This is needed because in some positions
                // mathcursor->cursor->crow is equal to 0, and therefore
                // the label cannot be inserted.
@@ -1031,9 +1073,11 @@ InsetFormula::LocalDispatch(BufferView * bv, kb_action action,
 
                string new_label = arg;
                if (new_label.empty()) {
-                       string default_label = (lyxrc.label_init_length >= 0) ? "eq:" : "";
-                       pair<bool, string> res = old_label.empty()
-                               ? askForText(_("Enter new label to insert:"), default_label)
+                       string const default_label =
+                               (lyxrc.label_init_length >= 0) ? "eq:" : "";
+                       pair<bool, string> const res = old_label.empty()
+                               ? askForText(_("Enter new label to insert:"),
+                                            default_label)
                                : askForText(_("Enter label:"), old_label);
                        if (!res.first)
                                break;
@@ -1049,13 +1093,18 @@ InsetFormula::LocalDispatch(BufferView * bv, kb_action action,
                if (!new_label.empty() && bv->ChangeRefsIfUnique(old_label, new_label))
                        bv->redraw();
 
+#ifdef WITH_WARNINGS
+#warning Labels
+#endif
+/*
                if (is_multi_numbered(par->GetType())) {
                        mathcursor->setLabel(new_label);
-                       // MathMatrixInset *mt = (MathMatrixInset*)par;
-                       // mt->SetLabel(new_label);
+                       // par->SetLabel(new_label);
                } else
                        label_ = new_label;
+*/
 
+               // Andre:
                UpdateLocal(bv);
                break;
        }
@@ -1063,13 +1112,14 @@ InsetFormula::LocalDispatch(BufferView * bv, kb_action action,
        case LFUN_MATH_DISPLAY:
                bv->lockedInsetStoreUndo(Undo::EDIT);
                display(!disp_flag_);
+               // Andre:
                UpdateLocal(bv);
                break;
 
                // Invalid actions under math mode
        case LFUN_MATH_MODE:
                if (mathcursor->getLastCode()!= LM_TC_TEXTRM) {
-                       bv->owner()->getMiniBuffer()->Set(_("math text mode"));
+                       bv->owner()->message(_("math text mode"));
                        varcode = LM_TC_TEXTRM;
                } else {
                        varcode = LM_TC_VAR;
@@ -1078,16 +1128,24 @@ InsetFormula::LocalDispatch(BufferView * bv, kb_action action,
                break;
 
        case LFUN_UNDO:
-               bv->owner()->getMiniBuffer()->Set(_("Invalid action in math mode!"));
+               bv->owner()->message(_("Invalid action in math mode!"));
                break;
 
-               //------- dummy actions
-       case LFUN_EXEC_COMMAND:
-               bv->owner()->getMiniBuffer()->PrepareForCommand();
+       case LFUN_MATH_EXTERN:
+               HandleExtern(arg, bv);
                break;
 
+               //------- dummy actions
+#ifdef WITH_WARNINGS
+#warning Is this needed here? Shouldnt the main dispatch handle this? (Lgb)
+#endif
+               //case LFUN_EXEC_COMMAND:
+               //bv->owner()->getMiniBuffer()->PrepareForCommand();
+               //break;
+
        default:
-               if ((action == -1  || action == LFUN_SELFINSERT) && !arg.empty())  {
+               if ((action == -1  || action == LFUN_SELFINSERT)
+                   && !arg.empty())  {
                        unsigned char c = arg[0];
                        bv->lockedInsetStoreUndo(Undo::INSERT);
 
@@ -1192,9 +1250,14 @@ InsetFormula::LocalDispatch(BufferView * bv, kb_action action,
                        } else if (c == '\\') {
                                if (was_macro)
                                        mathcursor->MacroModeClose();
-                               bv->owner()->getMiniBuffer()->Set(_("TeX mode"));
+                               // This line nukes the mathcursor. Why?
+#ifdef WITH_WARNINGS
+#warning Still? (Lgb)
+#endif
+                               bv->owner()->message(_("TeX mode"));
                                mathcursor->setLastCode(LM_TC_TEX);
                        }
+                       // Andre:
                        UpdateLocal(bv);
                } else if (action == LFUN_MATH_PANEL) {
                        result = UNDISPATCHED;
@@ -1204,29 +1267,32 @@ InsetFormula::LocalDispatch(BufferView * bv, kb_action action,
                }
        }
        
-       if (was_macro != mathcursor->InMacroMode()
+       //UpdateLocal(bv);
+       
+       // Andre:
+       if ((mathcursor && was_macro != mathcursor->InMacroMode())
            && action >= 0
            && action != LFUN_BACKSPACE) 
-               UpdateLocal(bv);
+               UpdateLocal(bv);
 
        if (sp && !space_on)
                sp = 0;
 
-       if (mathcursor->Selection() || was_selection)
+       if (mathcursor && (mathcursor->Selection() || was_selection))
                ToggleInsetSelection(bv);
 
-       if ((result == DISPATCHED) || (result == DISPATCHED_NOUPDATE) ||
-           (result == UNDISPATCHED))
+       if (result == DISPATCHED || result == DISPATCHED_NOUPDATE ||
+           result == UNDISPATCHED)
                ShowInsetCursor(bv);
        else
                bv->unlockInset(this);
 
-       return result;
+       return result;  // original version
 }
 
 
+namespace {
 
-static
 void mathedValidate(LaTeXFeatures & features, MathParInset * par)
 {
        MathedIter it(&par->GetData());
@@ -1256,7 +1322,6 @@ void mathedValidate(LaTeXFeatures & features, MathParInset * par)
 }
 
 
-static
 int mathed_write(MathParInset * p, ostream & os,
                 bool fragile, string const & label)
 {
@@ -1308,3 +1373,64 @@ int mathed_write(MathParInset * p, ostream & os,
        }
        return number_of_newlines;
 }
+
+} // namespace anon
+
+
+/* FIXME: math-greek-toggle seems to work OK, but math-greek doesn't turn
+ * on greek mode */
+bool math_insert_greek(BufferView * bv, char c)
+{
+       if (bv->available() &&
+           (('A' <= c && c <= 'Z') ||
+            ('a'<= c && c<= 'z')))   {
+               string tmp;
+               tmp = c;
+               if (!bv->theLockingInset() || bv->theLockingInset()->IsTextInset()) {
+                       int greek_kb_flag_save = greek_kb_flag;
+                       InsetFormula * new_inset = new InsetFormula();
+                       bv->beforeChange(bv->text);
+                       if (!bv->insertInset(new_inset)) {
+                               delete new_inset;
+                               return false;
+                       }
+//      Update(1);//BUG
+                       new_inset->Edit(bv, 0, 0, 0);
+                       new_inset->LocalDispatch(bv, LFUN_SELFINSERT, tmp);
+                       if (greek_kb_flag_save < 2) {
+                               bv->unlockInset(new_inset); // bv->theLockingInset());
+                               bv->text->CursorRight(bv, true);
+                       }
+               } else
+                       if (bv->theLockingInset()->LyxCode() == Inset::MATH_CODE ||
+                           bv->theLockingInset()->LyxCode() == Inset::MATHMACRO_CODE)
+                               static_cast<InsetFormula*>(bv->theLockingInset())->LocalDispatch(bv, LFUN_SELFINSERT, tmp);
+                       else
+                               lyxerr << "Math error: attempt to write on a wrong "
+                                       "class of inset." << endl;
+               return true;
+       }
+       return false;
+}
+
+void InsetFormula::HandleExtern(const string & arg, BufferView * bv)
+{
+       //string outfile = lyx::tempName("maple.out");
+       string outfile = "/tmp/lyx2" + arg + ".out";
+       ostringstream os;
+       par->WriteNormal(os); 
+       string code = os.str().c_str();
+       string script = "lyx2" + arg + " '" + code + "' " + outfile;
+       lyxerr << "calling: " << script << endl;
+       Systemcalls cmd(Systemcalls::System, script, 0);
+
+       ifstream is(outfile.c_str());
+       MathedArray ar;
+       mathed_parser_file(is, 0);
+       mathed_parse(ar, par, 0);
+       par->array = ar;
+
+       UpdateLocal(bv);
+}
+
+