]> git.lyx.org Git - lyx.git/blobdiff - src/cursor.C
Clean-up FileFilterList API.
[lyx.git] / src / cursor.C
index 2a45dbd4da51961455d68779b057749f06998315..ff51abc330aa72e45566889230e65dceeaa9105e 100644 (file)
 #include "frontends/LyXView.h"
 
 #include <boost/assert.hpp>
+#include <boost/bind.hpp>
 #include <boost/current_function.hpp>
 
 #include <sstream>
 
-using lyx::par_type;
+using lyx::pit_type;
 
 using std::string;
 using std::vector;
@@ -61,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())
@@ -92,7 +93,7 @@ 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
@@ -115,7 +116,7 @@ namespace {
                int x, int y, int xlow, int xhigh, int ylow, int yhigh)
        {
                BOOST_ASSERT(!cursor.empty());
-               par_type beg, end;
+               pit_type beg, end;
                CursorSlice bottom = cursor[0];
                LyXText * text = bottom.text();
                BOOST_ASSERT(text);
@@ -127,8 +128,8 @@ namespace {
                //lyxerr << "xlow: " << xlow << " ylow: " << ylow << endl;
                //lyxerr << "xhigh: " << xhigh << " yhigh: " << yhigh << endl;
 
-               it.par() = beg;
-               //et.par() = text->parOffset(end);
+               it.pit() = beg;
+               //et.pit() = text->parOffset(end);
 
                double best_dist = 10e10;
                DocIterator best_cursor = it;
@@ -138,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);
@@ -156,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;
        }
 
@@ -181,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;
 }
 
 
@@ -202,7 +202,7 @@ void LCursor::dispatch(FuncRequest const & cmd0)
                lyxerr[Debug::DEBUG] << "LCursor::dispatch: cmd: " << cmd0 << endl << *this << endl;
                BOOST_ASSERT(pos() <= lastpos());
                BOOST_ASSERT(idx() <= lastidx());
-               BOOST_ASSERT(par() <= lastpar());
+               BOOST_ASSERT(pit() <= lastpit());
 
                // The common case is 'LFUN handled, need update', so make the
                // LFUN handler's life easier by assuming this as default value.
@@ -238,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 (pit() > lastpit()) {
+                       lyxerr << "wrong par " << pit() << ", max is " << lastpit()
+                               << ". Trying to correct this."  << endl;
+                       pit() = lastpit();
+               }
+               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
@@ -440,14 +452,15 @@ void LCursor::setSelection()
 #ifdef WITH_WARNINGS
 #warning doesnt look ok
 #endif
-       if (par() == anchor().par() && pos() == anchor().pos())
+       if (pit() == anchor().pit() && pos() == anchor().pos())
                selection() = false;
 }
 
 
 void LCursor::setSelection(DocIterator const & where, size_t n)
 {
-       setCursor(where, true);
+       setCursor(where);
+       selection() = true;
        anchor_ = where;
        pos() += n;
 }
@@ -612,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));
 }
 
 
@@ -771,7 +784,7 @@ bool LCursor::up()
        DocIterator save = *this;
        if (goUpDown(true))
                return true;
-       setCursor(save, false);
+       setCursor(save);
        autocorrect() = false;
        return selection();
 }
@@ -783,7 +796,7 @@ bool LCursor::down()
        DocIterator save = *this;
        if (goUpDown(false))
                return true;
-       setCursor(save, false);
+       setCursor(save);
        autocorrect() = false;
        return selection();
 }
@@ -976,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;
                }
 
@@ -1057,8 +1070,8 @@ string LCursor::selectionAsString(bool label) const
                ParagraphList & pars = text()->paragraphs();
 
                // should be const ...
-               par_type startpit = selBegin().par();
-               par_type endpit = selEnd().par();
+               pit_type startpit = selBegin().pit();
+               pit_type endpit = selEnd().pit();
                size_t const startpos = selBegin().pos();
                size_t const endpos = selEnd().pos();
 
@@ -1070,7 +1083,7 @@ string LCursor::selectionAsString(bool label) const
                        asString(buffer, startpos, pars[startpit].size(), label) + "\n\n";
 
                // The paragraphs in between (if any)
-               for (par_type pit = startpit + 1; pit != endpit; ++pit) {
+               for (pit_type pit = startpit + 1; pit != endpit; ++pit) {
                        Paragraph & par = pars[pit];
                        result += par.asString(buffer, 0, par.size(), label) + "\n\n";
                }
@@ -1123,8 +1136,8 @@ Encoding const * LCursor::getEncoding() const
                        break;
        CursorSlice const & sl = operator[](s);
        LyXText & text = *sl.text();
-       LyXFont font = text.getPar(sl.par()).getFont(
-               bv().buffer()->params(), sl.pos(), outerFont(sl.par(), text.paragraphs()));
+       LyXFont font = text.getPar(sl.pit()).getFont(
+               bv().buffer()->params(), sl.pos(), outerFont(sl.pit(), text.paragraphs()));
        return font.language()->encoding();
 }