]> git.lyx.org Git - lyx.git/blobdiff - src/cursor.C
Fix #1736
[lyx.git] / src / cursor.C
index 947d70b6103b66157232c970f1ee1b49c218e1c4..e84d6c27ec312f9b1a4f89de63bf345cf5573b76 100644 (file)
@@ -44,6 +44,8 @@
 #include "frontends/LyXView.h"
 
 #include <boost/assert.hpp>
+#include <boost/bind.hpp>
+#include <boost/current_function.hpp>
 
 #include <sstream>
 
@@ -60,8 +62,8 @@ using std::swap;
 
 namespace {
 
-       bool positionable
-               (DocIterator const & cursor, DocIterator const & anchor)
+       bool
+       positionable(DocIterator const & cursor, DocIterator const & anchor)
        {
                // avoid deeper nested insets when selecting
                if (cursor.size() > anchor.size())
@@ -91,12 +93,12 @@ namespace {
                for (int i = 0; ; ++i) {
                        int xo, yo;
                        LCursor cur = c;
-                       cur.setCursor(it, false);
+                       cur.setCursor(it);
                        cur.inset().getCursorPos(cur, xo, yo);
                        double d = (x - xo) * (x - xo) + (y - yo) * (y - yo);
                        // '<=' in order to take the last possible position
                        // this is important for clicking behind \sum in e.g. '\sum_i a'
-                       lyxerr << "i: " << i << " d: " << d << " best: " << best_dist << endl;
+                       lyxerr[Debug::DEBUG] << "i: " << i << " d: " << d << " best: " << best_dist << endl;
                        if (d <= best_dist) {
                                best_dist = d;
                                result = it;
@@ -137,7 +139,7 @@ namespace {
                        if (!cursor.selection() || positionable(it, cursor.anchor_)) {
                                int xo = 0, yo = 0;
                                LCursor cur = cursor;
-                               cur.setCursor(it, false);
+                               cur.setCursor(it);
                                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);
@@ -155,7 +157,7 @@ namespace {
 
                //lyxerr << "best_dist: " << best_dist << " cur:\n" << best_cursor << endl;
                if (best_dist < 1e10)
-                       cursor.setCursor(best_cursor, false);
+                       cursor.setCursor(best_cursor);
                return best_dist < 1e10;
        }
 
@@ -180,11 +182,10 @@ void LCursor::reset(InsetBase & inset)
 }
 
 
-void LCursor::setCursor(DocIterator const & cur, bool sel)
+// this (intentionally) does neither touch anchor nor selection status
+void LCursor::setCursor(DocIterator const & cur)
 {
-       // this (intentionally) does not touch the anchor
        DocIterator::operator=(cur);
-       selection() = sel;
 }
 
 
@@ -197,8 +198,8 @@ void LCursor::dispatch(FuncRequest const & cmd0)
        FuncRequest cmd = cmd0;
        LCursor safe = *this;
 
-       for ( ; size(); pop()) {
-               lyxerr << "LCursor::dispatch: cmd: " << cmd0 << endl << *this << endl;
+       for (; size(); pop()) {
+               lyxerr[Debug::DEBUG] << "LCursor::dispatch: cmd: " << cmd0 << endl << *this << endl;
                BOOST_ASSERT(pos() <= lastpos());
                BOOST_ASSERT(idx() <= lastidx());
                BOOST_ASSERT(par() <= lastpar());
@@ -237,9 +238,21 @@ bool LCursor::getStatus(FuncRequest const & cmd, FuncStatus & status)
        bool res = false;
        for ( ; size(); pop()) {
                //lyxerr << "\nLCursor::getStatus: cmd: " << cmd << endl << *this << endl;
-               BOOST_ASSERT(pos() <= lastpos());
-               BOOST_ASSERT(idx() <= lastidx());
-               BOOST_ASSERT(par() <= lastpar());
+               if (idx() > lastidx()) {
+                       lyxerr << "wrong idx " << idx() << ", max is " << lastidx()
+                               << ". Trying to correct this."  << endl;
+                       idx() = lastidx();
+               }
+               if (par() > lastpar()) {
+                       lyxerr << "wrong par " << par() << ", max is " << lastpar()
+                               << ". Trying to correct this."  << endl;
+                       par() = lastpar();
+               }
+               if (pos() > lastpos()) {
+                       lyxerr << "wrong pos " << pos() << ", max is " << lastpos()
+                               << ". Trying to correct this."  << endl;
+                       pos() = lastpos();
+               }
 
                // The inset's getStatus() will return 'true' if it made
                // a definitive decision on whether it want to handle the
@@ -446,7 +459,8 @@ void LCursor::setSelection()
 
 void LCursor::setSelection(DocIterator const & where, size_t n)
 {
-       setCursor(where, true);
+       setCursor(where);
+       selection() = true;
        anchor_ = where;
        pos() += n;
 }
@@ -611,9 +625,9 @@ void LCursor::plainInsert(MathAtom const & t)
 
 void LCursor::insert(string const & str)
 {
-       //lyxerr << "LCursor::insert str '" << str << "'" << endl;
-       for (string::const_iterator it = str.begin(); it != str.end(); ++it)
-               insert(*it);
+       for_each(str.begin(), str.end(),
+                boost::bind(static_cast<void(LCursor::*)(char)>
+                            (&LCursor::insert), this, _1));
 }
 
 
@@ -770,7 +784,7 @@ bool LCursor::up()
        DocIterator save = *this;
        if (goUpDown(true))
                return true;
-       setCursor(save, false);
+       setCursor(save);
        autocorrect() = false;
        return selection();
 }
@@ -782,7 +796,7 @@ bool LCursor::down()
        DocIterator save = *this;
        if (goUpDown(false))
                return true;
-       setCursor(save, false);
+       setCursor(save);
        autocorrect() = false;
        return selection();
 }
@@ -975,7 +989,7 @@ bool LCursor::goUpDown(bool up)
                if (inset().idxUpDown(*this, up)) {
                        // try to find best position within this inset
                        if (!selection())
-                               setCursor(bruteFind2(*this, xo, yo), false);
+                               setCursor(bruteFind2(*this, xo, yo));
                        return true;
                }
 
@@ -1001,7 +1015,7 @@ bool LCursor::goUpDown(bool up)
 
 void LCursor::handleFont(string const & font)
 {
-       lyxerr << "LCursor::handleFont: " << font << endl;
+       lyxerr[Debug::DEBUG] << BOOST_CURRENT_FUNCTION << ": " << font << endl;
        string safe;
        if (selection()) {
                macroModeClose();
@@ -1150,5 +1164,3 @@ void LCursor::noUpdate()
 {
        disp_.update(false);
 }
-
-