]> git.lyx.org Git - lyx.git/blobdiff - src/cursor.C
avoid to update when navigating
[lyx.git] / src / cursor.C
index 8ca2bd906c45fb6530d69bad0b77d5bb7e78234d..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,31 +58,6 @@ 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_(), x_target_(-1),
          selection_(false), mark_(false)
@@ -268,7 +243,7 @@ void LCursor::getPos(int & x, int & y) const
        x = 0;
        y = 0;
        if (!empty())
-               inset().getCursorPos(back(), x, y);
+               inset().getCursorPos(*this, x, y);
 }
 
 
@@ -351,7 +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;
 }
@@ -406,145 +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::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()) {
-               if (!sel)
-                       noUpdate();
+       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) {
@@ -679,7 +528,7 @@ void LCursor::insert(char c)
        //lyxerr << "LCursor::insert char '" << c << "'" << endl;
        BOOST_ASSERT(!empty());
        if (inMathed()) {
-               selClearOrDel();
+               lyx::cap::selClearOrDel(*this);
                insert(new MathCharInset(c));
        } else {
                text()->insertChar(*this, c);
@@ -691,7 +540,7 @@ 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;
 }
@@ -720,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()) {
@@ -737,7 +586,7 @@ void LCursor::insert(MathArray const & ar)
 {
        macroModeClose();
        if (selection())
-               eraseSelection();
+               lyx::cap::eraseSelection(*this);
        cell().insert(pos(), ar);
        pos() += ar.size();
 }
@@ -748,7 +597,7 @@ bool LCursor::backspace()
        autocorrect() = false;
 
        if (selection()) {
-               selDel();
+               lyx::cap::selDel(*this);
                return true;
        }
 
@@ -788,7 +637,7 @@ bool LCursor::erase()
                return true;
 
        if (selection()) {
-               selDel();
+               lyx::cap::selDel(*this);
                return true;
        }
 
@@ -881,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());
@@ -1077,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);
@@ -1110,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
@@ -1133,7 +984,7 @@ void LCursor::handleFont(string const & font)
        string safe;
        if (selection()) {
                macroModeClose();
-               safe = grabAndEraseSelection();
+               safe = lyx::cap::grabAndEraseSelection(*this);
        }
 
        if (lastpos() != 0) {