]> git.lyx.org Git - lyx.git/blobdiff - src/mathed/math_cursor.C
Fix broken left movement (and some other things using prevInset())
[lyx.git] / src / mathed / math_cursor.C
index 961e82681732b4b3e34e298922f82ce633d6bf19..ace2f1228954417531070986fce3b4259629c3b5 100644 (file)
 #include "math_cursor.h"
 #include "math_arrayinset.h"
 #include "math_bigopinset.h"
+#include "math_symbolinset.h"
 #include "math_decorationinset.h"
 #include "math_deliminset.h"
 #include "math_dotsinset.h"
 #include "math_fracinset.h"
 #include "math_funcinset.h"
+#include "math_funcliminset.h"
 #include "math_gridinset.h"
 #include "math_macro.h"
 #include "math_macroarg.h"
@@ -68,16 +70,9 @@ struct Selection
                if (i1.idx_ == i2.idx_)
                        data_.push_back(MathArray(i1.cell(), i1.pos_, i2.pos_));
                else {
-#ifdef RECTANGULAR_SELECT
                        std::vector<int> indices = i1.par_->idxBetween(i1.idx_, i2.idx_);
                        for (unsigned i = 0; i < indices.size(); ++i)
                                data_.push_back(i1.cell(indices[i]));
-#else
-                       data_.push_back(MathArray(i1.cell(), i1.pos_, i1.cell().size()));
-                       for (int i = i1.idx_ + 1; i < i2.idx_; ++i)
-                               data_.push_back(i1.cell(i));
-                       data_.push_back(MathArray(i2.cell(), 0, i2.pos_));
-#endif
                }
        }
 
@@ -133,15 +128,17 @@ std::ostream & operator<<(std::ostream & os, MathCursorPos const & p)
 
 
 MathCursor::MathCursor(InsetFormulaBase * formula)
-       : formula_(formula)
+       : formula_(formula), lastcode_(LM_TC_MIN), imacro_(0), selection_(false)
 {
-       lastcode   = LM_TC_MIN;
-       macro_mode = false;
-       selection  = false;
        first();
 }
 
 
+MathCursor::~MathCursor()
+{
+       delete imacro_;
+}
+
 void MathCursor::push(MathInset * par, bool first)
 {
        MathCursorPos p;
@@ -180,27 +177,31 @@ void MathCursor::dump(char const * what) const
                        << " idx: " << Cursor_[i].idx_
                        << " par: " << Cursor_[i].par_ << "\n";
 
-       //lyxerr        << " sel: " << selection << " data: " << array() << "\n";
+       //lyxerr        << " sel: " << selection_ << " data: " << array() << "\n";
 }
 
 
-void MathCursor::seldump(char const *) const
+void MathCursor::seldump(char const * str) const
 {
        //lyxerr << "SEL: " << str << ": '" << theSelection << "'\n";
        //dump("   Pos");
        return;
 
-       //lyxerr << "\n\n\\n=================vvvvvvvvvvvvv=======================   "
-       //      <<  str << "\ntheSelection: " << theSelection;
-       //for (unsigned int i = 0; i < Cursor_.size(); ++i) 
-       //      lyxerr << Cursor_[i].par_ << "\n'" << Cursor_[i].cell() << "'\n";
+       lyxerr << "\n\n\n=================vvvvvvvvvvvvv=======================   "
+               <<  str << "\ntheSelection: " << selection_
+               << " '" << theSelection.glue() << "'\n";
+       for (unsigned int i = 0; i < Cursor_.size(); ++i) 
+               lyxerr << Cursor_[i].par_ << "\n'" << Cursor_[i].cell() << "'\n";
+       lyxerr << "\n";
+       for (unsigned int i = 0; i < Anchor_.size(); ++i) 
+               lyxerr << Anchor_[i].par_ << "\n'" << Anchor_[i].cell() << "'\n";
        //lyxerr << "\ncursor.pos_: " << cursor().pos_;
        //lyxerr << "\nanchor.pos_: " << anchor().pos_;
-       //lyxerr << "\n===================^^^^^^^^^^^^=====================\n\n\n";
+       lyxerr << "\n===================^^^^^^^^^^^^=====================\n\n\n";
 }
 
 
-bool MathCursor::isInside(MathInset * p) const
+bool MathCursor::isInside(MathInset const * p) const
 {
        for (unsigned i = 0; i < Cursor_.size(); ++i) 
                if (parInset(i) == p) 
@@ -213,6 +214,7 @@ bool MathCursor::openable(MathInset * p, bool sel, bool useupdown) const
 {
        if (!p)
                return false;
+
        if (!(p->isActive() || (useupdown && p->isScriptInset())))
                return false;
 
@@ -227,25 +229,31 @@ bool MathCursor::openable(MathInset * p, bool sel, bool useupdown) const
 }
 
 
-bool MathCursor::plainLeft()
+void MathCursor::plainLeft()
 {
-       return array().prev(cursor().pos_);
+       --cursor().pos_;
 }
 
 
-bool MathCursor::Left(bool sel)
+void MathCursor::plainRight()
+{
+       ++cursor().pos_;
+}
+
+
+bool MathCursor::left(bool sel)
 {
        dump("Left 1");
-       if (macro_mode) {
+       if (imacro_) {
                // was MacroModeBack()
-               if (!imacro->name().empty()) {
-                       imacro->setName(imacro->name().substr(0, imacro->name().length()-1));
-                       imacro->metrics(imacro->size());
+               if (!imacro_->name().empty()) {
+                       imacro_->setName(imacro_->name().substr(0, imacro_->name().length()-1));
+                       imacro_->metrics(imacro_->size());
                } else
-                       MacroModeClose();
+                       macroModeClose();
                return true;
        }
-       SelHandle(sel);
+       selHandle(sel);
        clearLastCode();
 
        MathInset * p = prevInset();
@@ -254,8 +262,10 @@ bool MathCursor::Left(bool sel)
                push(p, false);
                return true;
        } 
-       if (plainLeft())
+       if (cursor().pos_) {
+               plainLeft();
                return true;
+       }
        if (cursor().par_->idxLeft(cursor().idx_, cursor().pos_))
                return true;
        if (pop())
@@ -264,20 +274,14 @@ bool MathCursor::Left(bool sel)
 }
 
 
-bool MathCursor::plainRight()
-{
-       return array().next(cursor().pos_);
-}
-
-
-bool MathCursor::Right(bool sel)
+bool MathCursor::right(bool sel)
 {
        dump("Right 1");
-       if (macro_mode) {
-               MacroModeClose();
+       if (imacro_) {
+               macroModeClose();
                return true;
        }
-       SelHandle(sel);
+       selHandle(sel);
        clearLastCode();
 
        MathInset * p = nextInset();
@@ -285,41 +289,45 @@ bool MathCursor::Right(bool sel)
                push(p, true);
                return true;
        }
-       if (array().next(cursor().pos_))
+       if (cursor().pos_ != array().size()) {
+               plainRight();
                return true;
-       if (cursor().par_->idxRight(cursor().idx_, cursor().pos_))
+       }
+       if (cursor().par_->idxRight(cursor().idx_, cursor().pos_)) {
                return true;
-       if (!pop())
-               return false;
-       array().next(cursor().pos_);
-       return true;
+       }
+       if (pop()) {
+               plainRight();
+               return true;
+       }
+       return false;
 }
 
 
 void MathCursor::first()
 {
        Cursor_.clear();
-       push(formula_->par(), true);
+       push(outerPar(), true);
 }
 
 
 void MathCursor::last()
 {
        Cursor_.clear();
-       push(formula_->par(), false);
+       push(outerPar(), false);
 }
 
 
-void MathCursor::SetPos(int x, int y)
+void MathCursor::setPos(int x, int y)
 {
-       dump("SetPos 1");
-       //lyxerr << "MathCursor::SetPos x: " << x << " y: " << y << "\n";
+       dump("setPos 1");
+       //lyxerr << "MathCursor::setPos x: " << x << " y: " << y << "\n";
 
-       MacroModeClose();
-       lastcode = LM_TC_MIN;
+       macroModeClose();
+       lastcode_ = LM_TC_MIN;
        first();
 
-       cursor().par_  = formula()->par();
+       cursor().par_  = outerPar();
 
        while (1) {
                cursor().idx_ = -1;
@@ -345,43 +353,43 @@ void MathCursor::SetPos(int x, int y)
                //      << cursor().pos_  << "\n";
                MathInset * n = nextInset();
                MathInset * p = prevInset();
-               if (openable(n, selection, true) && n->covers(x, y))
+               if (openable(n, selection_, true) && n->covers(x, y))
                        push(n, true);
-               else if (openable(p, selection, true) && p->covers(x, y)) {
+               else if (openable(p, selection_, true) && p->covers(x, y)) {
                        plainLeft();
                        push(p, false);
                } else 
                        break;
        }
-       dump("SetPos 2");
+       dump("setPos 2");
 }
 
 
-void MathCursor::Home()
+void MathCursor::home()
 {
-       dump("Home 1");
-       MacroModeClose();
+       dump("home 1");
+       macroModeClose();
        clearLastCode();
        if (!cursor().par_->idxHome(cursor().idx_, cursor().pos_)) 
                pop();
-       dump("Home 2");
+       dump("home 2");
 }
 
 
-void MathCursor::End()
+void MathCursor::end()
 {
-       dump("End 1");
-       MacroModeClose();
+       dump("end 1");
+       macroModeClose();
        clearLastCode();
        if (!cursor().par_->idxEnd(cursor().idx_, cursor().pos_)) {
                pop();
-               array().next(cursor().pos_);
+               ++cursor().pos_;
        }
-       dump("End 2");
+       dump("end 2");
 }
 
 
-void MathCursor::erase()
+void MathCursor::plainErase()
 {
        array().erase(cursor().pos_);
 }
@@ -390,91 +398,89 @@ void MathCursor::erase()
 void MathCursor::insert(char c, MathTextCodes t)
 {
        //lyxerr << "inserting '" << c << "'\n";
-       if (selection)
-               SelDel();
+       if (selection_)
+               selDel();
 
-       if (t == LM_TC_MIN)
-               t = lastcode;
+       if (t != LM_TC_MIN)
+               lastcode_ = t;
 
-       if (macro_mode && !(MathIsAlphaFont(t) || t == LM_TC_MIN))
-               MacroModeClose();
+       if (imacro_ && !(MathIsAlphaFont(t) || t == LM_TC_MIN))
+               macroModeClose();
 
-       if (macro_mode) {
+       if (imacro_) {
                if (MathIsAlphaFont(t) || t == LM_TC_MIN) {
                        // was MacroModeinsert(c);
-                       imacro->setName(imacro->name() + static_cast<char>(c));
+                       imacro_->setName(imacro_->name() + c);
                        return;
                }
        }
 
        array().insert(cursor().pos_, c, t);
-       array().next(cursor().pos_);
-
-       lastcode = t;
+       plainRight();
 }
 
 
 void MathCursor::insert(MathInset * p)
 {
-       MacroModeClose();
+       macroModeClose();
 
-       if (selection) {
+       if (selection_) {
                if (p->nargs())
-                       SelCut();
+                       selCut();
                else
-                       SelDel();
+                       selDel();
        }
 
        array().insert(cursor().pos_, p);
-       array().next(cursor().pos_);
+       plainRight();
 }
 
 
 void MathCursor::insert(MathArray const & ar)
 {
-       MacroModeClose();
-       if (selection)
-               SelCut();
+       macroModeClose();
+       if (selection_)
+               selCut();
 
        array().insert(cursor().pos_, ar);
        cursor().pos_ += ar.size();
 }
 
 
-void MathCursor::Delete()
+void MathCursor::erase()
 {
-       dump("Delete 1");
-       if (macro_mode)
+       dump("erase 1");
+       if (imacro_)
                return;
 
-       if (selection) {
-               SelDel();
+       if (selection_) {
+               selDel();
                return;
        }
 
        // delete empty cells if necessary
-       if (cursor().pos_ == 0 && array().size() == 0) {
+       if (cursor().pos_ == 0 && array().empty()) {
                bool popit;
                bool removeit;
                cursor().par_->idxDelete(cursor().idx_, popit, removeit);
                if (popit && pop() && removeit)
-                       Delete();
+                       plainErase();
                return;
        }
 
        if (cursor().pos_ < array().size())
-               array().erase(cursor().pos_);
+               plainErase();
 
-       dump("Delete 2");
+       dump("erase 2");
 }
 
 
-void MathCursor::DelLine()
+void MathCursor::delLine()
 {
-       MacroModeClose();
+       macroModeClose();
 
-       if (selection) {
-               SelDel();
+       if (selection_) {
+               selDel();
                return;
        }
 
@@ -483,13 +489,13 @@ void MathCursor::DelLine()
 }
 
 
-bool MathCursor::Up(bool sel)
+bool MathCursor::up(bool sel)
 {
-       dump("Up 1");
-       MacroModeClose();
-       SelHandle(sel);
+       dump("up 1");
+       macroModeClose();
+       selHandle(sel);
 
-       if (selection) {
+       if (selection_) {
                int x = xarray().pos2x(cursor().pos_);
                if (cursor().idxDown()) {
                        cursor().pos_ = xarray().x2pos(x);
@@ -508,7 +514,7 @@ bool MathCursor::Up(bool sel)
                        push(p, true);
                        cursor().idx_ = idx;
                        cursor().pos_ = pos;
-                       dump("Up 3");
+                       dump("up 3");
                        return true;
                }
        }
@@ -521,7 +527,7 @@ bool MathCursor::Up(bool sel)
                        push(p, false);
                        cursor().idx_ = idx;
                        cursor().pos_ = pos;
-                       dump("Up 4");
+                       dump("up 4");
                        return true;
                }
        }
@@ -537,13 +543,13 @@ bool MathCursor::Up(bool sel)
 }
 
 
-bool MathCursor::Down(bool sel)
+bool MathCursor::down(bool sel)
 {
-       dump("Down 1");
-       MacroModeClose();
-       SelHandle(sel);
+       dump("down 1");
+       macroModeClose();
+       selHandle(sel);
 
-       if (selection) {
+       if (selection_) {
                int x = xarray().pos2x(cursor().pos_);
                if (cursor().idxDown()) {
                        cursor().pos_ = xarray().x2pos(x);
@@ -593,30 +599,33 @@ bool MathCursor::Down(bool sel)
 
 bool MathCursor::toggleLimits()
 {
-       if (!prevIsInset())
+       MathScriptInset * p = prevScriptInset();
+       if (!p)
                return false;
-       MathInset * p = prevInset();
        int old = p->limits();
        p->limits(old < 0 ? 1 : -1);
        return old != p->limits();
 }
 
 
-void MathCursor::SetSize(MathStyles size)
+void MathCursor::setSize(MathStyles size)
 {
        cursor().par_->userSetSize(size);
 }
 
 
 
-void MathCursor::Interpret(string const & s)
+void MathCursor::interpret(string const & s)
 {
-       //lyxerr << "Interpret: '" << s << "'\n";
+       //lyxerr << "interpret: '" << s << "'\n";
        //lyxerr << "in: " << in_word_set(s) << " \n";
 
-       if (s.size() && (s[0] == '^' || s[0] == '_')) {
+       if (s.empty())
+               return;
+
+       if (s[0] == '^' || s[0] == '_') {
                bool const up = (s[0] == '^');
-               SelCut();       
+               selCut();       
                MathScriptInset * p = prevScriptInset();
                if (!p) {
                        MathInset * b = prevInset();
@@ -637,7 +646,7 @@ void MathCursor::Interpret(string const & s)
                        p->down(true);
                cursor().idx_ = up ? 0 : 1;
                cursor().pos_ = 0;
-               SelPaste();
+               selPaste();
                return;
        }
 
@@ -671,21 +680,20 @@ void MathCursor::Interpret(string const & s)
                        p = pp;
                }
                else
-                       p = new MathFuncInset(s, LM_OT_UNDEF);
+                       p = new MathFuncInset(s);
        } else {
                switch (l->token) {
-                       case LM_TK_BIGSYM: 
-                               p = new MathBigopInset(s, l->id);
+                       case LM_TK_BIGSYM:
+                               p = new MathBigopInset(l);
                                break;
-                               
-                       case LM_TK_SYM: {
-                               MathTextCodes code = static_cast<MathTextCodes>(l->id);
-                               if (code < 255)
-                                       insert(l->id, MathIsBOPS(code) ? LM_TC_BOPS : LM_TC_SYMB);
-                               else
-                                       p = new MathFuncInset(l->name);
+
+                       case LM_TK_FUNCLIM:
+                               p = new MathFuncLimInset(l);
+                               break;
+
+                       case LM_TK_SYM: 
+                               p = new MathSymbolInset(l);
                                break;
-                       }
 
                        case LM_TK_STACK:
                                p = new MathFracInset("stackrel");
@@ -700,11 +708,7 @@ void MathCursor::Interpret(string const & s)
                                break;
 
                        case LM_TK_DECORATION:
-                               p = new MathDecorationInset(l->name, l->id);
-                               break;
-
-                       case  LM_TK_FUNCLIM:
-                               p = new MathFuncInset(l->name, LM_OT_FUNCLIM);
+                               p = new MathDecorationInset(l);
                                break;
 
                        case LM_TK_SPACE:
@@ -712,7 +716,7 @@ void MathCursor::Interpret(string const & s)
                                break;
 
                        case LM_TK_DOTS:
-                               p = new MathDotsInset(l->name, l->id);
+                               p = new MathDotsInset(l);
                                break;
 
                        case LM_TK_MACRO:
@@ -726,115 +730,116 @@ void MathCursor::Interpret(string const & s)
        }
 
        if (p) {
-               bool oldsel = selection;
-               if (oldsel) 
-                       SelCut();
+               selCut();
                insert(p);
                if (p->nargs()) {
                        plainLeft();
-                       //push(p, true);
-                       Right();  // do not push for e.g. MathBigopInset
-                       if (oldsel) 
-                               SelPaste();
+                       right();  // do not push for e.g. MathSymbolInset
+                       selPaste();
                }
                p->metrics(p->size());
        }
 }
 
 
-void MathCursor::MacroModeOpen()
+void MathCursor::macroModeOpen()
 {
-       if (!macro_mode) {
-               imacro = new MathFuncInset("");
-               insert(imacro);
-               macro_mode = true;
+       if (!imacro_) {
+               imacro_ = new MathFuncInset("");
+               array().insert(cursor().pos_, imacro_);
+               ++cursor().pos_;
+               //insert(imacro_);
        } else
                lyxerr << "Math Warning: Already in macro mode" << endl;
 }
 
 
-void MathCursor::MacroModeClose()
+void MathCursor::macroModeClose()
 {
-       if (macro_mode) {
-               string name = imacro->name();
+       if (imacro_) {
+               string name = imacro_->name();
                plainLeft();
-               erase();
-               delete imacro;
-               macro_mode = false;
-               imacro = 0;
-               Interpret(name);
+               plainErase();
+               imacro_ = 0;
+               interpret(name);
        }
 }
 
 
-void MathCursor::SelCopy()
+void MathCursor::selCopy()
 {
-       seldump("SelCopy");
-       if (selection) {
+       seldump("selCopy");
+       if (selection_) {
                theSelection.grab(*this);
-               SelClear();
+               selClear();
        }
 }
 
 
-void MathCursor::SelCut()
+void MathCursor::selCut()
 {
-       seldump("SelCut");
-       if (selection) {
+       seldump("selCut");
+       if (selection_) {
                theSelection.grab(*this);
                theSelection.erase(*this);
-               SelClear();
+               selClear();
+       } else {
+               theSelection.clear();
        }
 }
 
 
-void MathCursor::SelDel()
+void MathCursor::selDel()
 {
-       seldump("SelDel");
-       if (selection) {
+       seldump("selDel");
+       if (selection_) {
                theSelection.erase(*this);
-               SelClear();
+               selClear();
        }
 }
 
 
-void MathCursor::SelPaste()
+void MathCursor::selPaste()
 {
-       seldump("SelPaste");
+       seldump("selPaste");
        theSelection.paste(*this);
-       SelClear();
+       selClear();
 }
 
 
-void MathCursor::SelHandle(bool sel)
+void MathCursor::selHandle(bool sel)
 {
-       if (sel && !selection)
-               SelStart();
-       if (!sel && selection)
-               SelClear();
+       if (sel == selection_)
+               return;
+
+       theSelection.clear();
+       Anchor_    = Cursor_;
+       selection_ = sel;
 }
 
 
-void MathCursor::SelStart()
+void MathCursor::selStart()
 {
-       seldump("SelStart");
-       if (selection)
+       seldump("selStart");
+       if (selection_)
                return;
 
+       theSelection.clear();
        Anchor_ = Cursor_;
-       selection = true;
+       selection_ = true;
 }
 
 
-void MathCursor::SelClear()
+void MathCursor::selClear()
 {
-       selection = false;
+       seldump("selClear");
+       selection_ = false;
 }
 
 
 void MathCursor::drawSelection(Painter & pain) const
 {
-       if (!selection)
+       if (!selection_)
                return;
 
        MathCursorPos i1;
@@ -867,29 +872,32 @@ void MathCursor::drawSelection(Painter & pain) const
 
 void MathCursor::handleFont(MathTextCodes t)
 {
-       if (selection) {
+       if (selection_) {
                MathCursorPos i1;
                MathCursorPos i2;
                getSelection(i1, i2); 
                if (i1.idx_ == i2.idx_) {
                        MathArray & ar = i1.cell();
-                       for (int pos = i1.pos_; pos != i2.pos_; ar.next(pos))
-                               if (!ar.isInset(pos) && isalnum(ar.GetChar(pos))) { 
-                                       MathTextCodes c = ar.GetCode(pos) == t ? LM_TC_VAR : t;
+                       for (int pos = i1.pos_; pos != i2.pos_; ++pos)
+                               if (isalnum(ar.getChar(pos))) { 
+                                       MathTextCodes c = ar.getCode(pos) == t ? LM_TC_VAR : t;
                                        ar.setCode(pos, c);
                                }
                }
-       } else {
-               lastcode = (lastcode == t) ? LM_TC_VAR : t;
-       }
+       } else 
+               lastcode_ = (lastcode_ == t) ? LM_TC_VAR : t;
 }
 
 
-void MathCursor::handleAccent(string const & name, int code)
+void MathCursor::handleAccent(string const & name)
 {
-       MathDecorationInset * p = new MathDecorationInset(name, code);
-       if (selection) {
-               SelCut();
+       latexkeys const * l = in_word_set(name);
+       if (!l)
+               return;
+
+       MathDecorationInset * p = new MathDecorationInset(l);
+       if (selection_) {
+               selCut();
                p->cell(0) = theSelection.glue();
        }
        insert(p);
@@ -900,8 +908,8 @@ void MathCursor::handleAccent(string const & name, int code)
 void MathCursor::handleDelim(int l, int r)
 {
        MathDelimInset * p = new MathDelimInset(l, r);
-       if (selection) {
-               SelCut();
+       if (selection_) {
+               selCut();
                p->cell(0) = theSelection.glue();
        }
        insert(p);
@@ -910,7 +918,7 @@ void MathCursor::handleDelim(int l, int r)
 }
 
 
-void MathCursor::GetPos(int & x, int & y)
+void MathCursor::getPos(int & x, int & y)
 {
        x = xarray().xo() + xarray().pos2x(cursor().pos_);
        y = xarray().yo();
@@ -919,13 +927,13 @@ void MathCursor::GetPos(int & x, int & y)
 
 MathTextCodes MathCursor::nextCode() const
 {
-       return array().GetCode(cursor().pos_); 
+       return array().getCode(cursor().pos_); 
 }
 
 
 MathTextCodes MathCursor::prevCode() const
 {
-       return array().GetCode(cursor().pos_ - 1); 
+       return array().getCode(cursor().pos_ - 1); 
 }
 
 
@@ -947,43 +955,42 @@ int MathCursor::pos() const
 }
 
 
-bool MathCursor::InMacroMode() const
+bool MathCursor::inMacroMode() const
 {
-       return macro_mode;
+       return imacro_;
 }
 
 
-bool MathCursor::Selection() const
+bool MathCursor::selection() const
 {
-       return selection;
+       return selection_;
 }
 
 
 void MathCursor::clearLastCode()
 {
-       lastcode = LM_TC_MIN;
+       lastcode_ = LM_TC_MIN;
 }
 
 
 void MathCursor::setLastCode(MathTextCodes t)
 {
-       lastcode = t;
+       lastcode_ = t;
 }
 
 
 MathTextCodes MathCursor::getLastCode() const
 {
-       return lastcode;
+       return lastcode_;
 }
 
 
-MathInset * MathCursor::enclosing(MathInsetTypes t, int & idx) const
+MathArrayInset * MathCursor::enclosingArray(int & idx) const
 {
        for (int i = Cursor_.size() - 1; i >= 0; --i) {
-               //lyxerr << "checking level " << i << "\n";
-               if (Cursor_[i].par_->getType() == t) {
+               if (Cursor_[i].par_->isArray()) {
                        idx = Cursor_[i].idx_;
-                       return Cursor_[i].par_;
+                       return static_cast<MathArrayInset *>(Cursor_[i].par_);
                }
        }
        return 0;
@@ -996,7 +1003,7 @@ void MathCursor::pullArg(bool goright)
        dump("pullarg");
        MathArray a = array();
        if (pop()) {
-               array().erase(cursor().pos_);
+               plainErase();
                array().insert(cursor().pos_, a);
                if (goright) 
                        cursor().pos_ += a.size();
@@ -1017,10 +1024,10 @@ void MathCursor::normalize() const
 #endif
        MathCursor * it = const_cast<MathCursor *>(this);
 
-       if (cursor().idx_ < 0 || cursor().idx_ > cursor().par_->nargs())
+       if (cursor().idx_ < 0 || cursor().idx_ > cursor().par_->nargs() - 1)
                lyxerr << "this should not really happen - 1\n";
        it->cursor().idx_    = max(cursor().idx_, 0);
-       it->cursor().idx_    = min(cursor().idx_, cursor().par_->nargs());
+       it->cursor().idx_    = min(cursor().idx_, cursor().par_->nargs() - 1);
 
        if (cursor().pos_ < 0 || cursor().pos_ > array().size())
                lyxerr << "this should not really happen - 2\n";
@@ -1042,9 +1049,9 @@ int MathCursor::row() const
 
 
 /*
-char MathCursorPos::GetChar() const
+char MathCursorPos::getChar() const
 {
-       return array().GetChar(cursor().pos_);
+       return array().getChar(cursor().pos_);
 }
 
 
@@ -1053,7 +1060,7 @@ string MathCursorPos::readString()
        string s;
        int code = nextCode();
        for ( ; OK() && nextCode() == code; Next()) 
-               s += GetChar();
+               s += getChar();
 
        return s;
 }
@@ -1063,10 +1070,10 @@ string MathCursorPos::readString()
 MathInset * MathCursor::prevInset() const
 {
        normalize();
-       int c = cursor().pos_;
-       if (!array().prev(c))
+       int pos = cursor().pos_;
+       if (!pos)
                return 0;
-       return array().nextInset(c);
+       return array().nextInset(pos - 1);
 }
 
 
@@ -1080,7 +1087,7 @@ MathInset * MathCursor::nextInset() const
 MathScriptInset * MathCursor::prevScriptInset() const
 {
        normalize();
-       MathInset * p = array().prevInset(cursor().pos_);
+       MathInset * p = prevInset();
        return (p && p->isScriptInset()) ? static_cast<MathScriptInset *>(p) : 0;
 }
 
@@ -1088,7 +1095,7 @@ MathScriptInset * MathCursor::prevScriptInset() const
 MathSpaceInset * MathCursor::prevSpaceInset() const
 {
        normalize();
-       MathInset * p = array().prevInset(cursor().pos_);
+       MathInset * p = prevInset();
        return (p && p->isSpaceInset()) ? static_cast<MathSpaceInset *>(p) : 0;
 }
 
@@ -1116,18 +1123,6 @@ MathXArray & MathCursor::xarray() const
 }
 
 
-bool MathCursor::nextIsInset() const
-{
-       return cursor().pos_ < array().size() && MathIsInset(nextCode());
-}
-
-
-bool MathCursor::prevIsInset() const
-{
-       return cursor().pos_ > 0 && MathIsInset(prevCode());
-}
-
-
 int MathCursor::xpos() const 
 {
        normalize();
@@ -1168,7 +1163,7 @@ void MathCursor::splitCell()
 
 void MathCursor::breakLine()
 {
-       MathMatrixInset * p = static_cast<MathMatrixInset *>(formula()->par());
+       MathMatrixInset * p = outerPar();
        if (p->getType() == LM_OT_SIMPLE || p->getType() == LM_OT_EQUATION) {
                p->mutate(LM_OT_EQNARRAY);
                p->addRow(0);
@@ -1196,8 +1191,7 @@ void MathCursor::breakLine()
 char MathCursor::valign() const
 {
        int idx;
-       MathGridInset * p =
-               static_cast<MathGridInset *>(enclosing(LM_OT_MATRIX, idx));
+       MathArrayInset * p = enclosingArray(idx);
        return p ? p->valign() : 0;
 }
 
@@ -1205,8 +1199,7 @@ char MathCursor::valign() const
 char MathCursor::halign() const
 {
        int idx;
-       MathGridInset * p =
-               static_cast<MathGridInset *>(enclosing(LM_OT_MATRIX, idx));
+       MathArrayInset * p = enclosingArray(idx);
        return p ? p->halign(idx % p->ncols()) : 0;
 }
 
@@ -1301,7 +1294,7 @@ MathCursorPos MathCursor::normalAnchor() const
        MathCursorPos normal = Anchor_[Cursor_.size() - 1];
        if (Cursor_.size() < Anchor_.size() && !(normal < cursor())) {
                // anchor is behind cursor -> move anchor behind the inset
-               normal.cell().next(normal.pos_);
+               ++normal.pos_;
        }
        //lyxerr << "normalizing: from " << Anchor_[Anchor_.size() - 1] << " to "
        //      << normal << "\n";
@@ -1331,3 +1324,10 @@ bool MathCursorPos::idxRight()
 {
        return par_->idxRight(idx_, pos_);
 }
+
+
+MathMatrixInset * MathCursor::outerPar() const
+{
+       return
+               static_cast<MathMatrixInset *>(const_cast<MathInset *>(formula_->par()));
+}