]> 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 a0f8013fdcb82d729e1f8f7125a14f1851c38c0c..ace2f1228954417531070986fce3b4259629c3b5 100644 (file)
 #include <algorithm>
 #include <cctype>
 
-#include "math_inset.h"
-#include "math_arrayinset.h"
-#include "math_parser.h"
-#include "math_cursor.h"
-#include "math_macro.h"
-#include "math_macroarg.h"
-#include "math_macrotable.h"
-#include "math_root.h"
-#include "support/lstrings.h"
 #include "debug.h"
 #include "LColor.h"
 #include "Painter.h"
-#include "math_matrixinset.h"
-#include "math_grid.h"
-#include "math_spaceinset.h"
-#include "math_funcinset.h"
+#include "mathed/support.h"
+#include "formulabase.h"
+#include "math_cursor.h"
+#include "math_arrayinset.h"
 #include "math_bigopinset.h"
-#include "math_fracinset.h"
+#include "math_symbolinset.h"
 #include "math_decorationinset.h"
-#include "math_dotsinset.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"
+#include "math_macrotable.h"
 #include "math_macrotemplate.h"
+#include "math_matrixinset.h"
+#include "math_rootinset.h"
+#include "math_spaceinset.h"
 #include "math_sqrtinset.h"
+#include "support/lstrings.h"
 #include "math_scriptinset.h"
-#include "mathed/support.h"
-#include "formulabase.h"
-
+#include "math_parser.h"
 
 using std::endl;
 using std::min;
 using std::max;
 using std::isalnum;
 
-#define RECTANGULAR_SELECT 1
 
 namespace {
 
@@ -71,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
                }
        }
 
@@ -92,37 +84,16 @@ struct Selection
                if (i1.idx_ == i2.idx_) {
                        i1.cell().erase(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)
                                i1.cell(indices[i]).erase();
-#else
-                       i1.cell().erase(i1.pos_, i1.cell().size());
-                       for (int i = i1.idx_ + 1; i < i2.idx_; ++i)
-                               i1.cell(i).erase();
-                       i2.cell().erase(0, i2.pos_);
-
-                       int from = i1.cell().size() ? i1.idx_ + 1 : i1.idx_; 
-                       int to   = i2.cell().size() ? i2.idx_ : i2.idx_ + 1; 
-                       i1.par_->idxDeleteRange(from, to);
-#endif
                }
                cursor.cursor() = i1;
        }
 
        void paste(MathCursor & cursor) const
        {
-#ifdef RECTANGULAR_SELECT
-               cursor.cursor().cell().push_back(glue());
-#else
-               unsigned na  = cursor.cursor().par_->nargs();
-               unsigned idx = cursor.cursor().idx_;
-               unsigned end = std::min(idx + data_.size(), na);
-               for (int i = 0; i < end - idx; ++i)
-                       cursor.cursor().cell(idx + i).push_back(data_[i]);
-               for (unsigned i = end - idx; i < data_.size(); ++i)
-                       cursor.cursor().cell(end - 1).push_back(data_[i]);
-#endif
+               cursor.insert(glue());
        }
 
        // glues selection to one cell
@@ -139,31 +110,17 @@ struct Selection
                data_.clear();
        }
 
-       
-
        std::vector<MathArray> data_;
 };
 
-Selection theSelection;
-
 
+Selection theSelection;
 
-bool IsMacro(short tok, int id)
-{
-       return tok != LM_TK_STACK &&
-              tok != LM_TK_FRAC &&
-              tok != LM_TK_SQRT &&
-              tok != LM_TK_DECORATION &&
-              tok != LM_TK_SPACE &&
-              tok != LM_TK_DOTS &&
-              tok != LM_TK_FUNCLIM &&
-              tok != LM_TK_BIGSYM &&
-              !(tok == LM_TK_SYM && id < 255);
-}
 
-ostream & operator<<(ostream & os, MathCursorPos const & p)
+std::ostream & operator<<(std::ostream & os, MathCursorPos const & p)
 {
-       os << "(par: " << p.par_ << " idx: " << p.idx_ << " pos: " << p.pos_ << ")";
+       os << "(par: " << p.par_ << " idx: " << p.idx_
+          << " pos: " << p.pos_ << ")";
        return os;
 }
 
@@ -171,15 +128,17 @@ ostream & operator<<(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;
@@ -206,36 +165,43 @@ MathInset * MathCursor::parInset(int i) const
        return Cursor_[i].par_;
 }
 
+
 void MathCursor::dump(char const * what) const
 {
        return;
 
-       lyxerr << "MC: " << what
-               << " cursor.pos: " << cursor().pos_
-               << " cursor.idx: " << cursor().idx_
-               << " cursor.par: " << cursor().par_
-               << " sel: " << selection
-               << " data: " << array()
-               << "\n";
+       lyxerr << "MC: " << what << "\n";
+       for (unsigned i = 0; i < Cursor_.size(); ++i)
+               lyxerr << "  i: " << i 
+                       << " pos: " << Cursor_[i].pos_
+                       << " idx: " << Cursor_[i].idx_
+                       << " par: " << Cursor_[i].par_ << "\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) 
@@ -248,11 +214,12 @@ bool MathCursor::openable(MathInset * p, bool sel, bool useupdown) const
 {
        if (!p)
                return false;
-       if (!(p->isActive() || (useupdown && p->isUpDownInset())))
+
+       if (!(p->isActive() || (useupdown && p->isScriptInset())))
                return false;
 
        if (sel) {
-               // we can't move into everything during selection
+               // we can't move into anything new during selection
                if (Cursor_.size() == Anchor_.size())
                        return false;
                if (p != Anchor_[Cursor_.size()].par_)
@@ -261,35 +228,44 @@ bool MathCursor::openable(MathInset * p, bool sel, bool useupdown) const
        return true;
 }
 
-bool MathCursor::plainLeft()
+
+void MathCursor::plainLeft()
+{
+       --cursor().pos_;
+}
+
+
+void MathCursor::plainRight()
 {
-       return array().prev(cursor().pos_);
+       ++cursor().pos_;
 }
 
 
-bool MathCursor::Left(bool sel)
+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();
        if (openable(p, sel, false)) {
-               array().prev(cursor().pos_);
+               plainLeft();
                push(p, false);
                return true;
        } 
-       if (array().prev(cursor().pos_))
+       if (cursor().pos_) {
+               plainLeft();
                return true;
+       }
        if (cursor().par_->idxLeft(cursor().idx_, cursor().pos_))
                return true;
        if (pop())
@@ -298,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();
@@ -319,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;
@@ -379,139 +353,134 @@ 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)) {
-                       array().prev(cursor().pos_);
+               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");
-       if (macro_mode)
-               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");
-       if (macro_mode)
-               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::plainErase()
+{
+       array().erase(cursor().pos_);
 }
 
 
 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;
        }
 
-       if (cursor().pos_ < array().size())
-               array().erase(cursor().pos_);
-
        // 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;
        }
 
-#ifdef WITH_WARNINGS
-#warning pullArg disabled
-#endif
-       //if (cursor().pos_ == 0 && Cursor_.size() >= 1) {
-       //      lyxerr << "Delete: popping...\n";
-       //      pop();
-       //}
+       if (cursor().pos_ < array().size())
+               plainErase();
 
-       dump("Delete 2");
+       dump("erase 2");
 }
 
 
-void MathCursor::DelLine()
+void MathCursor::delLine()
 {
-       MacroModeClose();
+       macroModeClose();
 
-       if (selection) {
-               SelDel();
+       if (selection_) {
+               selDel();
                return;
        }
 
@@ -520,15 +489,15 @@ 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().par_->idxUp(cursor().idx_, cursor().pos_)) {
+               if (cursor().idxDown()) {
                        cursor().pos_ = xarray().x2pos(x);
                        return true;
                }
@@ -543,10 +512,9 @@ bool MathCursor::Up(bool sel)
                int idx, pos;
                if (p->idxFirstUp(idx, pos)) {
                        push(p, true);
-                       cursor().par_ = p;
                        cursor().idx_ = idx;
                        cursor().pos_ = pos;
-                       dump("Up 3");
+                       dump("up 3");
                        return true;
                }
        }
@@ -555,12 +523,11 @@ bool MathCursor::Up(bool sel)
        if (p) {
                int idx, pos;
                if (p->idxLastUp(idx, pos)) {
-                       array().prev(cursor().pos_);
+                       plainLeft();
                        push(p, false);
-                       cursor().par_ = p;
                        cursor().idx_ = idx;
                        cursor().pos_ = pos;
-                       dump("Up 4");
+                       dump("up 4");
                        return true;
                }
        }
@@ -576,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);
@@ -610,7 +577,7 @@ bool MathCursor::Down(bool sel)
        if (p) {
                int idx, pos;
                if (p->idxLastDown(idx, pos)) {
-                       array().prev(cursor().pos_);
+                       plainLeft();
                        push(p, false);
                        cursor().idx_ = idx;
                        cursor().pos_ = pos;
@@ -620,7 +587,7 @@ bool MathCursor::Down(bool sel)
        }
 
        int x = xarray().pos2x(cursor().pos_);
-       if (cursor().par_->idxDown(cursor().idx_, cursor().pos_)) {
+       if (cursor().idxDown()) {
                cursor().pos_ = xarray().x2pos(x);
                return true;
        }
@@ -632,50 +599,54 @@ 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);
+       cursor().par_->userSetSize(size);
 }
 
 
 
-void MathCursor::Interpret(string const & s)
+void MathCursor::interpret(string const & s)
 {
-       lyxerr << "Interpret: '" << s << "'  ('" << s.substr(0, 7)  << "' " <<
-in_word_set(s) << " \n";
+       //lyxerr << "interpret: '" << s << "'\n";
+       //lyxerr << "in: " << in_word_set(s) << " \n";
 
-       if (s[0] == '^') {
-               MathUpDownInset * p = nearbyUpDownInset();
-               if (!p) {
-                       p = new MathScriptInset(true, false);
-                       insert(p);
-                       array().prev(cursor().pos_);
-               }
-               push(p, true);
-               p->up(true);
-               cursor().idx_ = 0;
+       if (s.empty())
                return;
-       }
 
-       if (s[0] == '_') {
-               MathUpDownInset * p = nearbyUpDownInset();
+       if (s[0] == '^' || s[0] == '_') {
+               bool const up = (s[0] == '^');
+               selCut();       
+               MathScriptInset * p = prevScriptInset();
                if (!p) {
-                       p = new MathScriptInset(false, true);
+                       MathInset * b = prevInset();
+                       if (b && b->isScriptable()) {
+                               p = new MathScriptInset(up, !up, b->clone());
+                               plainLeft();
+                               erase();
+                       } else {
+                               p = new MathScriptInset(up, !up);
+                       }
                        insert(p);
-                       array().prev(cursor().pos_);
+                       plainLeft();
                }
                push(p, true);
-               p->down(true);
-               cursor().idx_ = 1;
+               if (up)
+                       p->up(true);
+               else
+                       p->down(true);
+               cursor().idx_ = up ? 0 : 1;
+               cursor().pos_ = 0;
+               selPaste();
                return;
        }
 
@@ -709,18 +680,19 @@ in_word_set(s) << " \n";
                        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(l->name, l->id);
-                                       break;
-                               
+                       case LM_TK_BIGSYM:
+                               p = new MathBigopInset(l);
+                               break;
+
+                       case LM_TK_FUNCLIM:
+                               p = new MathFuncLimInset(l);
+                               break;
+
                        case LM_TK_SYM: 
-                               if (l->id < 255)
-                                       insert(l->id, MathIsBOPS(l->id) ? LM_TC_BOPS : LM_TC_SYMB);
-                               else
-                                       p = new MathFuncInset(l->name);
+                               p = new MathSymbolInset(l);
                                break;
 
                        case LM_TK_STACK:
@@ -736,11 +708,7 @@ in_word_set(s) << " \n";
                                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:
@@ -748,7 +716,7 @@ in_word_set(s) << " \n";
                                break;
 
                        case LM_TK_DOTS:
-                               p = new MathDotsInset(l->name, l->id);
+                               p = new MathDotsInset(l);
                                break;
 
                        case LM_TK_MACRO:
@@ -762,126 +730,116 @@ in_word_set(s) << " \n";
        }
 
        if (p) {
-               bool oldsel = selection;
-               if (oldsel) 
-                       SelCut();
+               selCut();
                insert(p);
                if (p->nargs()) {
-                       array().prev(cursor().pos_);
-                       push(p, true);
-                       if (oldsel) 
-                               SelPaste();
+                       plainLeft();
+                       right();  // do not push for e.g. MathSymbolInset
+                       selPaste();
                }
-               p->Metrics(p->size());
+               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)  {
-               macro_mode = false;
-               latexkeys const * l = in_word_set(imacro->name());
-               if (!imacro->name().empty()
-                               && (!l || (l && IsMacro(l->token, l->id)))
-                               && !MathMacroTable::hasTemplate(imacro->name()))
-               {
-                       if (!l) {
-                               //imacro->SetName(macrobf);
-                               // This guarantees that the string will be removed by destructor
-                               imacro->SetType(LM_OT_UNDEF);
-                       } else
-                               imacro->SetName(l->name);
-               } else {
-                       Left();
-                       array().erase(cursor().pos_);
-                       if (l || MathMacroTable::hasTemplate(imacro->name())) 
-                               Interpret(imacro->name());
-                       imacro->SetName(string());
-               }
-               imacro = 0;
+       if (imacro_) {
+               string name = imacro_->name();
+               plainLeft();
+               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;
@@ -899,7 +857,6 @@ void MathCursor::drawSelection(Painter & pain) const
                pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
        } else {
 
-#if RECTANGULAR_SELECT
                std::vector<int> indices = i1.par_->idxBetween(i1.idx_, i2.idx_);
                for (unsigned i = 0; i < indices.size(); ++i) {
                        MathXArray & c = i1.xcell(indices[i]);
@@ -909,79 +866,59 @@ void MathCursor::drawSelection(Painter & pain) const
                        int y2 = c.yo() + c.descent();
                        pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
                }
-#else
-               // leftmost cell
-               MathXArray & c = i1.xcell();
-               int x1 = c.xo() + c.pos2x(i1.pos_);
-               int y1 = c.yo() - c.ascent();
-               int x2 = c.xo() + c.width();
-               int y2 = c.yo() + c.descent();
-               pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
-               // middle cells
-               for (int idx = i1.idx_ + 1; idx < i2.idx_; ++idx) { 
-                       MathXArray & c = i1.xcell(idx);
-                       int x1 = c.xo();
-                       int y1 = c.yo() - c.ascent();
-                       int x2 = c.xo() + c.width();
-                       int y2 = c.yo() + c.descent();
-                       pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
-               }
-               // rightmost cell
-               MathXArray & cr = i2.xcell();
-               x1 = cr.xo();
-               y1 = cr.yo() - cr.ascent();
-               x2 = cr.xo() + cr.pos2x(i2.pos_);
-               y2 = cr.yo() + cr.descent();
-               pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
-#endif
        }
 }
 
 
 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);
        push(p, true);
 }
 
+
 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);
+       plainLeft();
        push(p, true);
 }
 
 
-void MathCursor::GetPos(int & x, int & y)
+void MathCursor::getPos(int & x, int & y)
 {
        x = xarray().xo() + xarray().pos2x(cursor().pos_);
        y = xarray().yo();
@@ -990,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); 
 }
 
 
@@ -1018,57 +955,59 @@ 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;
 }
 
-void MathCursor::pullArg()
+
+void MathCursor::pullArg(bool goright)
 {
        // pullArg
+       dump("pullarg");
        MathArray a = array();
-       if (!Left())
-               return;
-       normalize();
-       array().erase(cursor().pos_);
-       array().insert(cursor().pos_, a);
+       if (pop()) {
+               plainErase();
+               array().insert(cursor().pos_, a);
+               if (goright) 
+                       cursor().pos_ += a.size();
+       }
 }
 
 
@@ -1085,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";
@@ -1110,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_);
 }
 
 
@@ -1121,19 +1060,20 @@ string MathCursorPos::readString()
        string s;
        int code = nextCode();
        for ( ; OK() && nextCode() == code; Next()) 
-               s += GetChar();
+               s += getChar();
 
        return s;
 }
 */
 
+
 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);
 }
 
 
@@ -1144,16 +1084,19 @@ MathInset * MathCursor::nextInset() const
 }
 
 
-MathUpDownInset * MathCursor::nearbyUpDownInset() const
+MathScriptInset * MathCursor::prevScriptInset() const
 {
        normalize();
-       MathInset * p = array().prevInset(cursor().pos_);
-       if (p && p->isUpDownInset())
-               return static_cast<MathUpDownInset *>(p);
-       p = array().nextInset(cursor().pos_);
-       if (p && p->isUpDownInset())
-               return static_cast<MathUpDownInset *>(p);
-       return 0;
+       MathInset * p = prevInset();
+       return (p && p->isScriptInset()) ? static_cast<MathScriptInset *>(p) : 0;
+}
+
+
+MathSpaceInset * MathCursor::prevSpaceInset() const
+{
+       normalize();
+       MathInset * p = prevInset();
+       return (p && p->isSpaceInset()) ? static_cast<MathSpaceInset *>(p) : 0;
 }
 
 
@@ -1180,39 +1123,31 @@ 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();
        return xarray().pos2x(cursor().pos_);
 }
 
+
 void MathCursor::gotoX(int x)
 {
        cursor().pos_ = xarray().x2pos(x);      
 }
 
+
 void MathCursor::idxNext()
 {
        cursor().par_->idxNext(cursor().idx_, cursor().pos_);
 }
 
+
 void MathCursor::idxPrev()
 {
        cursor().par_->idxPrev(cursor().idx_, cursor().pos_);
 }
 
+
 void MathCursor::splitCell()
 {
        if (cursor().idx_ == cursor().par_->nargs() - 1) 
@@ -1225,10 +1160,11 @@ void MathCursor::splitCell()
        array().insert(0, ar);
 }
 
+
 void MathCursor::breakLine()
 {
-       MathMatrixInset * p = static_cast<MathMatrixInset *>(formula()->par());
-       if (p->GetType() == LM_OT_SIMPLE || p->GetType() == LM_OT_EQUATION) {
+       MathMatrixInset * p = outerPar();
+       if (p->getType() == LM_OT_SIMPLE || p->getType() == LM_OT_EQUATION) {
                p->mutate(LM_OT_EQNARRAY);
                p->addRow(0);
                cursor().idx_ = p->nrows();
@@ -1251,19 +1187,19 @@ 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;
 }
 
+
 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;
 }
 
@@ -1311,20 +1247,21 @@ MathCursorPos const & MathCursor::cursor() const
 ////////////////////////////////////////////////////////////////////////
 
 
-bool MathCursorPos::operator==(const MathCursorPos & it) const
+bool operator==(MathCursorPos const & ti, MathCursorPos const & it)
 {
-       return par_ == it.par_ && idx_ == it.idx_ && pos_ == it.pos_;
+       return ti.par_ == it.par_ && ti.idx_ == it.idx_ && ti.pos_ == it.pos_;
 }
 
-bool MathCursorPos::operator<(const MathCursorPos & it) const
+
+bool operator<(MathCursorPos const & ti, MathCursorPos const & it)
 {
-       if (par_ != it.par_) {
+       if (ti.par_ != it.par_) {
                lyxerr << "can't compare cursor and anchor in different insets\n";
                return true;
        }
-       if (idx_ != it.idx_)
-               return idx_ < it.idx_;
-       return pos_ < it.pos_;
+       if (ti.idx_ != it.idx_)
+               return ti.idx_ < it.idx_;
+       return ti.pos_ < it.pos_;
 }
 
 
@@ -1344,40 +1281,53 @@ MathXArray & MathCursorPos::xcell(int idx) const
        return par_->xcell(idx);
 }
 
+
 MathXArray & MathCursorPos::xcell() const
 {
        return par_->xcell(idx_);
 }
 
+
 MathCursorPos MathCursor::normalAnchor() const
 {
        // use Anchor on the same level as Cursor
        MathCursorPos normal = Anchor_[Cursor_.size() - 1];
-       if (Cursor_.size() < Anchor_.size() && !(cursor() > normal)) {
+       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";
        return normal;
 }
 
+
 bool MathCursorPos::idxUp()
 {
        return par_->idxUp(idx_, pos_);
 }
 
+
 bool MathCursorPos::idxDown()
 {
        return par_->idxDown(idx_, pos_);
 }
 
+
 bool MathCursorPos::idxLeft()
 {
        return par_->idxLeft(idx_, pos_);
 }
 
+
 bool MathCursorPos::idxRight()
 {
        return par_->idxRight(idx_, pos_);
 }
+
+
+MathMatrixInset * MathCursor::outerPar() const
+{
+       return
+               static_cast<MathMatrixInset *>(const_cast<MathInset *>(formula_->par()));
+}