]> 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 b0811d3b57044d4e509dec45c42ad7ce0a629ad3..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"
@@ -132,6 +134,11 @@ MathCursor::MathCursor(InsetFormulaBase * formula)
 }
 
 
+MathCursor::~MathCursor()
+{
+       delete imacro_;
+}
+
 void MathCursor::push(MathInset * par, bool first)
 {
        MathCursorPos p;
@@ -174,23 +181,27 @@ void MathCursor::dump(char const * what) const
 }
 
 
-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) 
@@ -203,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;
 
@@ -217,9 +229,15 @@ bool MathCursor::openable(MathInset * p, bool sel, bool useupdown) const
 }
 
 
-bool MathCursor::plainLeft()
+void MathCursor::plainLeft()
+{
+       --cursor().pos_;
+}
+
+
+void MathCursor::plainRight()
 {
-       return array().prev(cursor().pos_);
+       ++cursor().pos_;
 }
 
 
@@ -244,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())
@@ -254,12 +274,6 @@ bool MathCursor::left(bool sel)
 }
 
 
-bool MathCursor::plainRight()
-{
-       return array().next(cursor().pos_);
-}
-
-
 bool MathCursor::right(bool sel)
 {
        dump("Right 1");
@@ -275,28 +289,32 @@ 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);
 }
 
 
@@ -309,7 +327,7 @@ void MathCursor::setPos(int x, int y)
        lastcode_ = LM_TC_MIN;
        first();
 
-       cursor().par_  = formula()->par();
+       cursor().par_  = outerPar();
 
        while (1) {
                cursor().idx_ = -1;
@@ -365,7 +383,7 @@ void MathCursor::end()
        clearLastCode();
        if (!cursor().par_->idxEnd(cursor().idx_, cursor().pos_)) {
                pop();
-               array().next(cursor().pos_);
+               ++cursor().pos_;
        }
        dump("end 2");
 }
@@ -398,7 +416,7 @@ void MathCursor::insert(char c, MathTextCodes t)
        }
 
        array().insert(cursor().pos_, c, t);
-       array().next(cursor().pos_);
+       plainRight();
 }
 
 
@@ -414,7 +432,7 @@ void MathCursor::insert(MathInset * p)
        }
 
        array().insert(cursor().pos_, p);
-       array().next(cursor().pos_);
+       plainRight();
 }
 
 
@@ -441,7 +459,7 @@ void MathCursor::erase()
        }
 
        // 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);
@@ -581,9 +599,9 @@ 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();
@@ -602,7 +620,10 @@ void MathCursor::interpret(string const & s)
        //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();       
                MathScriptInset * p = prevScriptInset();
@@ -659,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_FUNCLIM:
+                               p = new MathFuncLimInset(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_SYM: 
+                               p = new MathSymbolInset(l);
                                break;
-                       }
 
                        case LM_TK_STACK:
                                p = new MathFracInset("stackrel");
@@ -688,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:
@@ -700,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:
@@ -714,15 +730,12 @@ void MathCursor::interpret(string const & s)
        }
 
        if (p) {
-               bool oldsel = selection_;
-               if (oldsel) 
-                       selCut();
+               selCut();
                insert(p);
                if (p->nargs()) {
                        plainLeft();
-                       right();  // do not push for e.g. MathBigopInset
-                       if (oldsel) 
-                               selPaste();
+                       right();  // do not push for e.g. MathSymbolInset
+                       selPaste();
                }
                p->metrics(p->size());
        }
@@ -733,7 +746,9 @@ void MathCursor::macroModeOpen()
 {
        if (!imacro_) {
                imacro_ = new MathFuncInset("");
-               insert(imacro_);
+               array().insert(cursor().pos_, imacro_);
+               ++cursor().pos_;
+               //insert(imacro_);
        } else
                lyxerr << "Math Warning: Already in macro mode" << endl;
 }
@@ -768,6 +783,8 @@ void MathCursor::selCut()
                theSelection.grab(*this);
                theSelection.erase(*this);
                selClear();
+       } else {
+               theSelection.clear();
        }
 }
 
@@ -792,10 +809,12 @@ void MathCursor::selPaste()
 
 void MathCursor::selHandle(bool sel)
 {
-       if (sel && !selection_)
-               selStart();
-       if (!sel && selection_)
-               selClear();
+       if (sel == selection_)
+               return;
+
+       theSelection.clear();
+       Anchor_    = Cursor_;
+       selection_ = sel;
 }
 
 
@@ -805,6 +824,7 @@ void MathCursor::selStart()
        if (selection_)
                return;
 
+       theSelection.clear();
        Anchor_ = Cursor_;
        selection_ = true;
 }
@@ -812,6 +832,7 @@ void MathCursor::selStart()
 
 void MathCursor::selClear()
 {
+       seldump("selClear");
        selection_ = false;
 }
 
@@ -857,8 +878,8 @@ void MathCursor::handleFont(MathTextCodes t)
                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))) { 
+                       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);
                                }
@@ -868,9 +889,13 @@ void MathCursor::handleFont(MathTextCodes t)
 }
 
 
-void MathCursor::handleAccent(string const & name, int code)
+void MathCursor::handleAccent(string const & name)
 {
-       MathDecorationInset * p = new MathDecorationInset(name, code);
+       latexkeys const * l = in_word_set(name);
+       if (!l)
+               return;
+
+       MathDecorationInset * p = new MathDecorationInset(l);
        if (selection_) {
                selCut();
                p->cell(0) = theSelection.glue();
@@ -960,13 +985,12 @@ MathTextCodes MathCursor::getLastCode() const
 }
 
 
-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;
@@ -1000,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";
@@ -1046,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);
 }
 
 
@@ -1063,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;
 }
 
@@ -1071,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;
 }
 
@@ -1099,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();
@@ -1151,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);
@@ -1179,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;
 }
 
@@ -1188,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;
 }
 
@@ -1284,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";
@@ -1314,3 +1324,10 @@ bool MathCursorPos::idxRight()
 {
        return par_->idxRight(idx_, pos_);
 }
+
+
+MathMatrixInset * MathCursor::outerPar() const
+{
+       return
+               static_cast<MathMatrixInset *>(const_cast<MathInset *>(formula_->par()));
+}