]> git.lyx.org Git - lyx.git/blobdiff - src/DocIterator.cpp
* do not lookup the same macro all the time
[lyx.git] / src / DocIterator.cpp
index 6c71e0e5a974fee35319097f0825caf9375a5f81..3ef66b46c705307d2ea416bf93df8d05d5d4df56 100644 (file)
@@ -14,7 +14,7 @@
 #include "DocIterator.h"
 
 #include "debug.h"
-#include "LyXText.h"
+#include "Text.h"
 #include "Paragraph.h"
 
 #include "mathed/MathData.h"
@@ -38,12 +38,12 @@ DocIterator::DocIterator()
 {}
 
 
-DocIterator::DocIterator(InsetBase & inset)
+DocIterator::DocIterator(Inset & inset)
        : boundary_(false), inset_(&inset)
 {}
 
 
-DocIterator doc_iterator_begin(InsetBase & inset)
+DocIterator doc_iterator_begin(Inset & inset)
 {
        DocIterator dit(inset);
        dit.forwardPos();
@@ -51,13 +51,13 @@ DocIterator doc_iterator_begin(InsetBase & inset)
 }
 
 
-DocIterator doc_iterator_end(InsetBase & inset)
+DocIterator doc_iterator_end(Inset & inset)
 {
        return DocIterator(inset);
 }
 
 
-InsetBase * DocIterator::nextInset()
+Inset * DocIterator::nextInset()
 {
        BOOST_ASSERT(!empty());
        if (pos() == lastpos())
@@ -72,7 +72,7 @@ InsetBase * DocIterator::nextInset()
 }
 
 
-InsetBase * DocIterator::prevInset()
+Inset * DocIterator::prevInset()
 {
        BOOST_ASSERT(!empty());
        if (pos() == 0)
@@ -89,7 +89,7 @@ InsetBase * DocIterator::prevInset()
 }
 
 
-InsetBase const * DocIterator::prevInset() const
+Inset const * DocIterator::prevInset() const
 {
        BOOST_ASSERT(!empty());
        if (pos() == 0)
@@ -106,11 +106,11 @@ InsetBase const * DocIterator::prevInset() const
 }
 
 
-InsetBase * DocIterator::realInset() const
+Inset * DocIterator::realInset() const
 {
        BOOST_ASSERT(inTexted());
        // if we are in a tabular, we need the cell
-       if (inset().lyxCode() == InsetBase::TABULAR_CODE) {
+       if (inset().lyxCode() == Inset::TABULAR_CODE) {
                InsetTabular & tabular = static_cast<InsetTabular&>(inset());
                return tabular.cell(idx()).get();
        }
@@ -152,13 +152,13 @@ MathAtom & DocIterator::nextAtom()
 }
 
 
-LyXText * DocIterator::text()
+Text * DocIterator::text()
 {
        BOOST_ASSERT(!empty());
        return top().text();
 }
 
-LyXText const * DocIterator::text() const
+Text const * DocIterator::text() const
 {
        BOOST_ASSERT(!empty());
        return top().text();
@@ -181,6 +181,22 @@ Paragraph const & DocIterator::paragraph() const
 }
 
 
+Paragraph const & DocIterator::innerParagraph() const
+{
+       BOOST_ASSERT(!empty());
+       // go up until first non-0 text is hit
+       // (innermost text is 0 in mathed)
+       for (int i = depth() - 1; i >= 0; --i)
+               if (slices_[i].text())
+                       return slices_[i].paragraph();
+
+       // This case is in principe not possible. We _must_
+       // be inside a Paragraph.
+       BOOST_ASSERT(false);
+       return paragraph();
+}
+
+
 pit_type DocIterator::lastpit() const
 {
        return inMathed() ? 0 : text()->paragraphs().size() - 1;
@@ -246,7 +262,7 @@ MathData & DocIterator::cell()
 }
 
 
-LyXText * DocIterator::innerText()
+Text * DocIterator::innerText()
 {
        BOOST_ASSERT(!empty());
        // Go up until first non-0 text is hit
@@ -257,7 +273,7 @@ LyXText * DocIterator::innerText()
        return 0;
 }
 
-LyXText const * DocIterator::innerText() const
+Text const * DocIterator::innerText() const
 {
        BOOST_ASSERT(!empty());
        // go up until first non-0 text is hit
@@ -269,7 +285,7 @@ LyXText const * DocIterator::innerText() const
 }
 
 
-InsetBase * DocIterator::innerInsetOfType(int code) const
+Inset * DocIterator::innerInsetOfType(int code) const
 {
        for (int i = depth() - 1; i >= 0; --i)
                if (slices_[i].inset_->lyxCode() == code)
@@ -286,12 +302,12 @@ void DocIterator::forwardPos(bool ignorecollapsed)
                return;
        }
 
-       InsetBase * const nextinset = nextInset();
+       Inset * const nextinset = nextInset();
        // jump over collapsables if they are collapsed
        // FIXME: the check for asInsetMath() shouldn't be necessary
        // but math insets do not return a sensible editable() state yet.
        if (ignorecollapsed && nextinset && (!nextinset->asInsetMath()
-           && nextinset->editable() != InsetBase::HIGHLY_EDITABLE)) {
+           && nextinset->editable() != Inset::HIGHLY_EDITABLE)) {
                ++top().pos();
                return;
        }
@@ -303,7 +319,7 @@ void DocIterator::forwardPos(bool ignorecollapsed)
        pos_type const lastp = lastpos();
 
        // move into an inset to the right if possible
-       InsetBase * n = 0;
+       Inset * n = 0;
 
        if (tip.pos() != lastp) {
                // this is impossible for pos() == size()
@@ -416,6 +432,19 @@ void DocIterator::forwardPar()
 }
 
 
+void DocIterator::forwardIdx()
+{
+       CursorSlice & tip = top();
+
+       //prevent endless loops
+       BOOST_ASSERT(tip.idx() < lastidx());
+
+       ++tip.idx();
+       tip.pit() = 0;
+       tip.pos() = 0;
+}
+
+
 void DocIterator::forwardChar()
 {
        forwardPos();
@@ -481,7 +510,7 @@ void DocIterator::backwardPos()
        }
 
        // move into an inset to the left if possible
-       InsetBase * n = 0;
+       Inset * n = 0;
 
        if (inMathed()) {
                n = (tip.cell().begin() + tip.pos())->nucleus();
@@ -510,7 +539,7 @@ bool DocIterator::hasPart(DocIterator const & it) const
 }
 
 
-void DocIterator::updateInsets(InsetBase * inset)
+void DocIterator::updateInsets(Inset * inset)
 {
        // this function re-creates the cache of inset pointers.
        // code taken in part from StableDocIterator::asDocIterator.
@@ -537,6 +566,28 @@ std::ostream & operator<<(std::ostream & os, DocIterator const & dit)
 }
 
 
+bool operator<(DocIterator const & p, DocIterator const & q)
+{
+       size_t depth = std::min(p.depth(), q.depth());
+       for (size_t i = 0 ; i < depth ; ++i) {
+               if (p[i] != q[i])
+                       return p[i] < q[i];
+       }
+       return p.depth() < q.depth();
+}
+
+
+bool operator>(DocIterator const & p, DocIterator const & q)
+{
+       return q < p;
+}
+
+
+bool operator<=(DocIterator const & p, DocIterator const & q)
+{
+       return !(q < p);
+}
+
 
 ///////////////////////////////////////////////////////
 
@@ -548,7 +599,7 @@ StableDocIterator::StableDocIterator(DocIterator const & dit)
 }
 
 
-DocIterator StableDocIterator::asDocIterator(InsetBase * inset) const
+DocIterator StableDocIterator::asDocIterator(Inset * inset) const
 {
        // this function re-creates the cache of inset pointers
        //lyxerr << "converting:\n" << *this << endl;