]> git.lyx.org Git - lyx.git/blobdiff - src/cursor.C
avoid to update when navigating
[lyx.git] / src / cursor.C
index 6394ee357437e933062847ced02794548a8fd98f..f563141a2e3ae1228bb560cbb50add3544c7f77b 100644 (file)
@@ -36,8 +36,8 @@
 #include "insets/insettext.h"
 
 #include "mathed/math_data.h"
-#include "mathed/math_support.h"
 #include "mathed/math_inset.h"
+#include "mathed/math_macrotable.h"
 
 #include "support/limited_stack.h"
 #include "support/std_sstream.h"
@@ -58,34 +58,8 @@ using std::min;
 using std::swap;
 
 
-
-// our own cut buffer
-limited_stack<string> theCutBuffer;
-
-
-namespace {
-
-void region(CursorSlice const & i1, CursorSlice const & i2,
-       LCursor::row_type & r1, LCursor::row_type & r2,
-       LCursor::col_type & c1, LCursor::col_type & c2)
-{
-       InsetBase & p = i1.inset();
-       c1 = p.col(i1.idx());
-       c2 = p.col(i2.idx());
-       if (c1 > c2)
-               swap(c1, c2);
-       r1 = p.row(i1.idx());
-       r2 = p.row(i2.idx());
-       if (r1 > r2)
-               swap(r1, r2);
-}
-
-}
-
-
 LCursor::LCursor(BufferView & bv)
-       : DocIterator(), bv_(&bv),
-         anchor_(), cached_y_(0), x_target_(-1),
+       : DocIterator(), bv_(&bv), anchor_(), x_target_(-1),
          selection_(false), mark_(false)
 {}
 
@@ -95,7 +69,6 @@ void LCursor::reset(InsetBase & inset)
        clear();
        push_back(CursorSlice(inset));
        anchor_ = DocIterator(inset);
-       cached_y_ = 0;
        clearTargetX();
        selection_ = false;
        mark_ = false;
@@ -139,6 +112,7 @@ DispatchResult LCursor::dispatch(FuncRequest const & cmd0)
        if (!disp_.dispatched()) {
                lyxerr << "RESTORING OLD CURSOR!" << endl;
                operator=(safe);
+               disp_.dispatched(false);
        }
        return disp_;
 }
@@ -173,10 +147,19 @@ bool LCursor::getStatus(FuncRequest const & cmd, FuncStatus & status)
 
 BufferView & LCursor::bv() const
 {
+       BOOST_ASSERT(bv_);
        return *bv_;
 }
 
 
+Buffer & LCursor::buffer() const
+{
+       BOOST_ASSERT(bv_);
+       BOOST_ASSERT(bv_->buffer());
+       return *bv_->buffer();
+}
+
+
 void LCursor::pop()
 {
        BOOST_ASSERT(size() >= 1);
@@ -203,7 +186,7 @@ bool LCursor::popLeft()
 {
        BOOST_ASSERT(!empty());
        //lyxerr << "Leaving inset to the left" << endl;
-       inset().notifyCursorLeaves(idx());
+       inset().notifyCursorLeaves(*this);
        if (depth() == 1)
                return false;
        pop();
@@ -215,7 +198,7 @@ bool LCursor::popRight()
 {
        BOOST_ASSERT(!empty());
        //lyxerr << "Leaving inset to the right" << endl;
-       inset().notifyCursorLeaves(idx());
+       inset().notifyCursorLeaves(*this);
        if (depth() == 1)
                return false;
        pop();
@@ -236,15 +219,6 @@ int LCursor::currentMode()
 }
 
 
-void LCursor::updatePos()
-{
-       BOOST_ASSERT(!empty());
-       if (size() > 1)
-               cached_y_ = bv().top_y() + back().inset().yo();
-               //cached_y_ = back().inset().yo();
-}
-
-
 void LCursor::getDim(int & asc, int & des) const
 {
        if (inMathed()) {
@@ -269,17 +243,7 @@ void LCursor::getPos(int & x, int & y) const
        x = 0;
        y = 0;
        if (!empty())
-               inset().getCursorPos(back(), x, y);
-       // getCursorPos gives _screen_ coordinates. We need to add
-       // top_y to get document coordinates. This is hidden in cached_y_.
-       //y += cached_y_ - inset().yo();
-       // The rest is non-obvious. The reason we have to have these
-       // extra computation is that the getCursorPos() calls rely
-       // on the inset's own knowledge of its screen position.
-       // If we scroll up or down in a big enough increment,
-       // inset->draw() is not called: this doesn't update
-       // inset.yo_, so getCursor() returns an old value.
-       // Ugly as you like.
+               inset().getCursorPos(*this, x, y);
 }
 
 
@@ -314,21 +278,19 @@ bool LCursor::posRight()
 }
 
 
-CursorSlice & LCursor::anchor()
+CursorSlice LCursor::anchor() const
 {
-       BOOST_ASSERT(!anchor_.empty());
-       return anchor_.back();
-}
-
-
-CursorSlice const & LCursor::anchor() const
-{
-       BOOST_ASSERT(!anchor_.empty());
-       return anchor_.back();
+       BOOST_ASSERT(anchor_.size() >= size());
+       CursorSlice normal = anchor_[size() - 1];
+       if (size() < anchor_.size() && back() <= normal) {
+               // anchor is behind cursor -> move anchor behind the inset
+               ++normal.pos();
+       }
+       return normal;
 }
 
 
-CursorSlice const & LCursor::selBegin() const
+CursorSlice LCursor::selBegin() const
 {
        if (!selection())
                return back();
@@ -336,7 +298,7 @@ CursorSlice const & LCursor::selBegin() const
 }
 
 
-CursorSlice const & LCursor::selEnd() const
+CursorSlice LCursor::selEnd() const
 {
        if (!selection())
                return back();
@@ -364,6 +326,9 @@ void LCursor::setSelection()
 {
        selection() = true;
        // a selection with no contents is not a selection
+#ifdef WITH_WARNINGS
+#warning doesnt look ok
+#endif
        if (par() == anchor().par() && pos() == anchor().pos())
                selection() = false;
 }
@@ -418,148 +383,17 @@ void LCursor::info(std::ostream & os) const
 }
 
 
-string LCursor::grabSelection()
-{
-       if (!selection())
-               return string();
-
-       CursorSlice i1 = selBegin();
-       CursorSlice i2 = selEnd();
-
-       if (i1.idx() == i2.idx()) {
-               if (i1.inset().asMathInset()) {
-                       MathArray::const_iterator it = i1.cell().begin();
-                       return asString(MathArray(it + i1.pos(), it + i2.pos()));
-               } else {
-                       return "unknown selection 1";
-               }
-       }
-
-       row_type r1, r2;
-       col_type c1, c2;
-       region(i1, i2, r1, r2, c1, c2);
-
-       string data;
-       if (i1.inset().asMathInset()) {
-               for (row_type row = r1; row <= r2; ++row) {
-                       if (row > r1)
-                               data += "\\\\";
-                       for (col_type col = c1; col <= c2; ++col) {
-                               if (col > c1)
-                                       data += '&';
-                               data += asString(i1.asMathInset()->cell(i1.asMathInset()->index(row, col)));
-                       }
-               }
-       } else {
-               data = "unknown selection 2";
-       }
-       return data;
-}
-
-
-void LCursor::eraseSelection()
-{
-       //lyxerr << "LCursor::eraseSelection" << endl;
-       CursorSlice const & i1 = selBegin();
-       CursorSlice const & i2 = selEnd();
-#ifdef WITH_WARNINGS
-#warning FIXME
-#endif
-       if (i1.inset().asMathInset()) {
-               if (i1.idx() == i2.idx()) {
-                       i1.cell().erase(i1.pos(), i2.pos());
-               } else {
-                       MathInset * p = i1.asMathInset();
-                       row_type r1, r2;
-                       col_type c1, c2;
-                       region(i1, i2, r1, r2, c1, c2);
-                       for (row_type row = r1; row <= r2; ++row)
-                               for (col_type col = c1; col <= c2; ++col)
-                                       p->cell(p->index(row, col)).clear();
-               }
-               back() = i1;
-       } else {
-               lyxerr << "can't erase this selection 1" << endl;
-       }
-       //lyxerr << "LCursor::eraseSelection end" << endl;
-}
-
-
-string LCursor::grabAndEraseSelection()
-{
-       if (!selection())
-               return string();
-       string res = grabSelection();
-       eraseSelection();
-       selection() = false;
-       return res;
-}
-
-
-void LCursor::selClear()
-{
-       resetAnchor();
-       clearSelection();
-}
-
-
-void LCursor::selCopy()
-{
-       if (selection()) {
-               theCutBuffer.push(grabSelection());
-               selection() = false;
-       } else {
-               //theCutBuffer.erase();
-       }
-}
-
-
-void LCursor::selCut()
-{
-       theCutBuffer.push(grabAndEraseSelection());
-}
-
-
-void LCursor::selDel()
-{
-       //lyxerr << "LCursor::selDel" << endl;
-       if (selection()) {
-               eraseSelection();
-               selection() = false;
-       }
-}
-
-
-void LCursor::selPaste(size_t n)
-{
-       selClearOrDel();
-       if (n < theCutBuffer.size())
-               paste(theCutBuffer[n]);
-       //grabSelection();
-       selection() = false;
-}
-
-
 void LCursor::selHandle(bool sel)
 {
        //lyxerr << "LCursor::selHandle" << endl;
        if (sel == selection())
                return;
+
        resetAnchor();
        selection() = sel;
 }
 
 
-void LCursor::selClearOrDel()
-{
-       //lyxerr << "LCursor::selClearOrDel" << endl;
-       if (lyxrc.auto_region_delete)
-               selDel();
-       else
-               selection() = false;
-}
-
-
 std::ostream & operator<<(std::ostream & os, LCursor const & cur)
 {
        for (size_t i = 0, n = cur.size(); i != n; ++i) {
@@ -593,7 +427,6 @@ std::ostream & operator<<(std::ostream & os, LCursor const & cur)
 #include "mathed/math_factory.h"
 #include "mathed/math_gridinset.h"
 #include "mathed/math_macroarg.h"
-#include "mathed/math_macrotemplate.h"
 #include "mathed/math_mathmlstream.h"
 #include "mathed/math_scriptinset.h"
 #include "mathed/math_support.h"
@@ -665,7 +498,7 @@ void LCursor::plainErase()
 
 void LCursor::markInsert()
 {
-       cell().insert(pos(), MathAtom(new MathCharInset(0)));
+       insert(char(0));
 }
 
 
@@ -684,7 +517,7 @@ void LCursor::plainInsert(MathAtom const & t)
 
 void LCursor::insert(string const & str)
 {
-       lyxerr << "LCursor::insert str '" << str << "'" << endl;
+       //lyxerr << "LCursor::insert str '" << str << "'" << endl;
        for (string::const_iterator it = str.begin(); it != str.end(); ++it)
                insert(*it);
 }
@@ -695,8 +528,8 @@ void LCursor::insert(char c)
        //lyxerr << "LCursor::insert char '" << c << "'" << endl;
        BOOST_ASSERT(!empty());
        if (inMathed()) {
-               selClearOrDel();
-               plainInsert(MathAtom(new MathCharInset(c)));
+               lyx::cap::selClearOrDel(*this);
+               insert(new MathCharInset(c));
        } else {
                text()->insertChar(*this, c);
        }
@@ -707,8 +540,9 @@ void LCursor::insert(MathAtom const & t)
 {
        //lyxerr << "LCursor::insert MathAtom: " << endl;
        macroModeClose();
-       selClearOrDel();
+       lyx::cap::selClearOrDel(*this);
        plainInsert(t);
+       lyxerr << "LCursor::insert MathAtom: cur:\n" << *this << endl;
 }
 
 
@@ -735,7 +569,7 @@ void LCursor::niceInsert(string const & t)
 void LCursor::niceInsert(MathAtom const & t)
 {
        macroModeClose();
-       string safe = grabAndEraseSelection();
+       string safe = lyx::cap::grabAndEraseSelection(*this);
        plainInsert(t);
        // enter the new inset and move the contents of the selection if possible
        if (t->isActive()) {
@@ -752,7 +586,7 @@ void LCursor::insert(MathArray const & ar)
 {
        macroModeClose();
        if (selection())
-               eraseSelection();
+               lyx::cap::eraseSelection(*this);
        cell().insert(pos(), ar);
        pos() += ar.size();
 }
@@ -763,7 +597,7 @@ bool LCursor::backspace()
        autocorrect() = false;
 
        if (selection()) {
-               selDel();
+               lyx::cap::selDel(*this);
                return true;
        }
 
@@ -785,6 +619,7 @@ bool LCursor::backspace()
        if (pos() != 0 && prevAtom()->nargs() > 0) {
                // let's require two backspaces for 'big stuff' and
                // highlight on the first
+               resetAnchor();
                selection() = true;
                --pos();
        } else {
@@ -802,7 +637,7 @@ bool LCursor::erase()
                return true;
 
        if (selection()) {
-               selDel();
+               lyx::cap::selDel(*this);
                return true;
        }
 
@@ -823,7 +658,9 @@ bool LCursor::erase()
                return true;
        }
 
+       // 'clever' UI hack: only erase large items if previously slected
        if (pos() != lastpos() && inset().nargs() > 0) {
+               resetAnchor();
                selection() = true;
                ++pos();
        } else {
@@ -893,7 +730,7 @@ void LCursor::handleNest(MathAtom const & a, int c)
 {
        //lyxerr << "LCursor::handleNest: " << c << endl;
        MathAtom t = a;
-       asArray(grabAndEraseSelection(), t.nucleus()->cell(c));
+       asArray(lyx::cap::grabAndEraseSelection(*this), t.nucleus()->cell(c));
        insert(t);
        posLeft();
        pushLeft(*nextInset());
@@ -911,18 +748,6 @@ int LCursor::targetX() const
 }
 
 
-void LCursor::adjust(pos_type from, int diff)
-{
-       if (pos() > from)
-               pos() += diff;
-       if (anchor().pos() > from)
-               anchor().pos() += diff;
-       // just to be on the safe side
-       // theoretically unecessary
-       normalize();
-}
-
-
 bool LCursor::inMacroMode() const
 {
        if (!pos() != 0)
@@ -938,28 +763,6 @@ MathUnknownInset * LCursor::activeMacro()
 }
 
 
-bool LCursor::inMacroArgMode() const
-{
-       return pos() > 0 && prevAtom()->getChar() == '#';
-}
-
-
-MathGridInset * LCursor::enclosingGrid(idx_type & idx) const
-{
-       for (MathInset::difference_type i = depth() - 1; i >= 0; --i) {
-               MathInset * m = operator[](i).inset().asMathInset();
-               if (!m)
-                       return 0;
-               MathGridInset * p = m->asGridInset();
-               if (p) {
-                       idx = operator[](i).idx();
-                       return p;
-               }
-       }
-       return 0;
-}
-
-
 void LCursor::pullArg()
 {
 #ifdef WITH_WARNINGS
@@ -1011,22 +814,6 @@ void LCursor::normalize()
 }
 
 
-char LCursor::valign()
-{
-       idx_type idx;
-       MathGridInset * p = enclosingGrid(idx);
-       return p ? p->valign() : '\0';
-}
-
-
-char LCursor::halign()
-{
-       idx_type idx;
-       MathGridInset * p = enclosingGrid(idx);
-       return p ? p->halign(idx % p->ncols()) : '\0';
-}
-
-
 bool LCursor::goUpDown(bool up)
 {
        // Be warned: The 'logic' implemented in this function is highly
@@ -1139,7 +926,8 @@ bool LCursor::bruteFind(int x, int y, int xlow, int xhigh, int ylow, int yhigh)
                // avoid invalid nesting when selecting
                if (!selection() || positionable(it, anchor_)) {
                        int xo = 0, yo = 0;
-                       CursorSlice & cur = it.back();
+                       LCursor cur = *this;
+                       cur.setCursor(it, false);
                        cur.inset().getCursorPos(cur, xo, yo);
                        if (xlow <= xo && xo <= xhigh && ylow <= yo && yo <= yhigh) {
                                double d = (x - xo) * (x - xo) + (y - yo) * (y - yo);
@@ -1172,7 +960,8 @@ void LCursor::bruteFind2(int x, int y)
        et.back().pos() = et.back().asMathInset()->cell(et.back().idx()).size();
        for (int i = 0; ; ++i) {
                int xo, yo;
-               CursorSlice & cur = it.back();
+               LCursor cur = *this;
+               cur.setCursor(it, false);
                cur.inset().getCursorPos(cur, xo, yo);
                double d = (x - xo) * (x - xo) + (y - yo) * (y - yo);
                // '<=' in order to take the last possible position
@@ -1189,32 +978,13 @@ void LCursor::bruteFind2(int x, int y)
 }
 
 
-CursorSlice LCursor::normalAnchor()
-{
-       if (anchor_.size() < depth()) {
-               resetAnchor();
-               lyxerr << "unusual Anchor size" << endl;
-       }
-       //lyx::BOOST_ASSERT(Anchor_.size() >= cursor.depth());
-       // use Anchor on the same level as Cursor
-       CursorSlice normal = anchor_[size() - 1];
-#if 0
-       if (depth() < anchor_.size() && !(normal < xx())) {
-               // anchor is behind cursor -> move anchor behind the inset
-               ++normal.pos_;
-       }
-#endif
-       return normal;
-}
-
-
 void LCursor::handleFont(string const & font)
 {
        lyxerr << "LCursor::handleFont: " << font << endl;
        string safe;
        if (selection()) {
                macroModeClose();
-               safe = grabAndEraseSelection();
+               safe = lyx::cap::grabAndEraseSelection(*this);
        }
 
        if (lastpos() != 0) {