]> git.lyx.org Git - lyx.git/blobdiff - src/CursorSlice.cpp
Embedding: saving inzip name to .lyx file so that embedded files can always be found...
[lyx.git] / src / CursorSlice.cpp
index 47b518cbfd2e72c3eaf0ff2953f33ca1d31ca8d3..af6d97894793e0e853e803d17f9aca8930d71a53 100644 (file)
 
 #include "CursorSlice.h"
 
-#include "debug.h"
 #include "Text.h"
 #include "Paragraph.h"
 
+#include "support/debug.h"
+
 #include "insets/Inset.h"
 
 #include "mathed/InsetMath.h"
@@ -26,8 +27,9 @@
 
 #include <boost/assert.hpp>
 
-using std::endl;
+#include <ostream>
 
+using namespace std;
 
 namespace lyx {
 
@@ -50,13 +52,7 @@ MathData & CursorSlice::cell() const
 }
 
 
-Paragraph & CursorSlice::paragraph()
-{
-       return text()->getPar(pit_);
-}
-
-
-Paragraph const & CursorSlice::paragraph() const
+Paragraph & CursorSlice::paragraph() const
 {
        return text()->getPar(pit_);
 }
@@ -65,7 +61,8 @@ Paragraph const & CursorSlice::paragraph() const
 pos_type CursorSlice::lastpos() const
 {
        BOOST_ASSERT(inset_);
-       return inset_->asInsetMath() ? cell().size() : paragraph().size();
+       return inset_->asInsetMath() ? cell().size() 
+               : (text()->empty() ? 0 : paragraph().size());
 }
 
 
@@ -91,6 +88,80 @@ CursorSlice::col_type CursorSlice::col() const
 }
 
 
+void CursorSlice::forwardPos()
+{
+       //  move on one position if possible
+       if (pos() < lastpos()) {
+               //lyxerr << "... next pos" << endl;
+               ++pos();
+               return;
+       }
+
+       // otherwise move on one paragraph if possible
+       if (pit() < lastpit()) {
+               //lyxerr << "... next par" << endl;
+               ++pit();
+               pos() = 0;
+               return;
+       }
+
+       // otherwise move on one cell
+       //lyxerr << "... next idx" << endl;
+
+       BOOST_ASSERT(idx() < nargs());
+
+       ++idx();
+       pit() = 0;
+       pos() = 0;
+}
+
+
+void CursorSlice::forwardIdx()
+{
+       BOOST_ASSERT(idx() < nargs());
+
+       ++idx();
+       pit() = 0;
+       pos() = 0;
+}
+
+
+void CursorSlice::backwardPos()
+{
+       if (pos() != 0) {
+               --pos();
+               return;
+       }
+
+       if (pit() != 0) {
+               --pit();
+               pos() = lastpos();
+               return;
+       }
+
+       if (idx() != 0) {
+               --idx();
+               pit() = lastpit();
+               pos() = lastpos();
+               return;
+       }
+
+       BOOST_ASSERT(false);
+}
+
+
+bool CursorSlice::at_end() const 
+{
+       return idx() == lastidx() && pit() == lastpit() && pos() == lastpos();
+}
+
+
+bool CursorSlice::at_begin() const
+{
+       return idx() == 0 && pit() == 0 && pos() == 0;
+}
+
+
 bool operator==(CursorSlice const & p, CursorSlice const & q)
 {
        return &p.inset() == &q.inset()
@@ -112,8 +183,8 @@ bool operator!=(CursorSlice const & p, CursorSlice const & q)
 bool operator<(CursorSlice const & p, CursorSlice const & q)
 {
        if (&p.inset() != &q.inset()) {
-               lyxerr << "can't compare cursor and anchor in different insets\n"
-                      << "p: " << p << '\n' << "q: " << q << endl;
+               LYXERR0("can't compare cursor and anchor in different insets\n"
+                      << "p: " << p << '\n' << "q: " << q);
                BOOST_ASSERT(false);
        }
        if (p.idx() != q.idx())
@@ -136,10 +207,10 @@ bool operator<=(CursorSlice const & p, CursorSlice const & q)
 }
 
 
-std::ostream & operator<<(std::ostream & os, CursorSlice const & item)
+ostream & operator<<(ostream & os, CursorSlice const & item)
 {
        return os
-          << "inset: " << &item.inset()
+          << "inset: " << (void *)&item.inset()
 //        << " text: " << item.text()
           << " idx: " << item.idx()
           << " par: " << item.pit()