]> 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 abb95af028edde36630e78cb246cbfb5644abe9f..ace2f1228954417531070986fce3b4259629c3b5 100644 (file)
@@ -181,19 +181,23 @@ 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";
 }
 
 
@@ -210,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;
 
@@ -230,6 +235,12 @@ void MathCursor::plainLeft()
 }
 
 
+void MathCursor::plainRight()
+{
+       ++cursor().pos_;
+}
+
+
 bool MathCursor::left(bool sel)
 {
        dump("Left 1");
@@ -263,12 +274,6 @@ bool MathCursor::left(bool sel)
 }
 
 
-void MathCursor::plainRight()
-{
-       ++cursor().pos_;
-}
-
-
 bool MathCursor::right(bool sel)
 {
        dump("Right 1");
@@ -284,14 +289,18 @@ 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;
 }
 
 
@@ -407,7 +416,7 @@ void MathCursor::insert(char c, MathTextCodes t)
        }
 
        array().insert(cursor().pos_, c, t);
-       ++cursor().pos_;
+       plainRight();
 }
 
 
@@ -423,7 +432,7 @@ void MathCursor::insert(MathInset * p)
        }
 
        array().insert(cursor().pos_, p);
-       ++cursor().pos_;
+       plainRight();
 }
 
 
@@ -450,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);
@@ -674,7 +683,7 @@ void MathCursor::interpret(string const & s)
                        p = new MathFuncInset(s);
        } else {
                switch (l->token) {
-                       case LM_TK_BIGSYM: 
+                       case LM_TK_BIGSYM:
                                p = new MathBigopInset(l);
                                break;
 
@@ -721,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. MathSymbolInset
-                       if (oldsel) 
-                               selPaste();
+                       selPaste();
                }
                p->metrics(p->size());
        }
@@ -777,6 +783,8 @@ void MathCursor::selCut()
                theSelection.grab(*this);
                theSelection.erase(*this);
                selClear();
+       } else {
+               theSelection.clear();
        }
 }
 
@@ -801,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;
 }
 
 
@@ -814,6 +824,7 @@ void MathCursor::selStart()
        if (selection_)
                return;
 
+       theSelection.clear();
        Anchor_ = Cursor_;
        selection_ = true;
 }
@@ -821,6 +832,7 @@ void MathCursor::selStart()
 
 void MathCursor::selClear()
 {
+       seldump("selClear");
        selection_ = false;
 }
 
@@ -1058,10 +1070,10 @@ string MathCursorPos::readString()
 MathInset * MathCursor::prevInset() const
 {
        normalize();
-       int c = cursor().pos_;
-       if (!c)
+       int pos = cursor().pos_;
+       if (!pos)
                return 0;
-       return array().nextInset(c);
+       return array().nextInset(pos - 1);
 }
 
 
@@ -1282,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";