#include "DocIterator.h"
#include "debug.h"
-#include "LyXText.h"
+#include "Text.h"
#include "Paragraph.h"
#include "mathed/MathData.h"
{}
-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();
}
-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())
}
-InsetBase * DocIterator::prevInset()
+Inset * DocIterator::prevInset()
{
BOOST_ASSERT(!empty());
if (pos() == 0)
}
-InsetBase const * DocIterator::prevInset() const
+Inset const * DocIterator::prevInset() const
{
BOOST_ASSERT(!empty());
if (pos() == 0)
}
-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();
}
}
-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();
}
+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;
}
-LyXText * DocIterator::innerText()
+Text * DocIterator::innerText()
{
BOOST_ASSERT(!empty());
// Go up until first non-0 text is hit
return 0;
}
-LyXText const * DocIterator::innerText() const
+Text const * DocIterator::innerText() const
{
BOOST_ASSERT(!empty());
// go up until first non-0 text is hit
}
-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)
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;
}
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()
}
+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();
}
// move into an inset to the left if possible
- InsetBase * n = 0;
+ Inset * n = 0;
if (inMathed()) {
n = (tip.cell().begin() + tip.pos())->nucleus();
}
-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.
}
+bool DocIterator::fixIfBroken()
+{
+ bool fixed = false;
+
+ for (size_t i = slices_.size() - 1; i != 0; --i)
+ if (!slices_[i].isValid()) {
+ pop_back();
+ fixed = true;
+ }
+
+ // The top level CursorSlice should always be valid.
+ BOOST_ASSERT(slices_[0].isValid());
+
+ if (idx() > lastidx()) {
+ lyxerr << "wrong idx " << idx()
+ << ", max is " << lastidx()
+ << " at level " << depth()
+ << ". Trying to correct this." << endl;
+ lyxerr << "old: " << *this << endl;
+ for (size_t i = idx(); i != lastidx(); --i)
+ pop_back();
+ idx() = lastidx();
+ pit() = lastpit();
+ pos() = lastpos();
+ fixed = true;
+ }
+ else if (pit() > lastpit()) {
+ lyxerr << "wrong pit " << pit()
+ << ", max is " << lastpit()
+ << " at level " << depth()
+ << ". Trying to correct this." << endl;
+ lyxerr << "old: " << *this << endl;
+ pit() = lastpit();
+ pos() = 0;
+ fixed = true;
+ }
+ else if (pos() > lastpos()) {
+ lyxerr << "wrong pos " << pos()
+ << ", max is " << lastpos()
+ << " at level " << depth()
+ << ". Trying to correct this." << endl;
+ lyxerr << "old: " << *this << endl;
+ pos() = lastpos();
+ fixed = true;
+ }
+ if (fixed) {
+ lyxerr << "new: " << *this << endl;
+ }
+ return fixed;
+}
+
+
std::ostream & operator<<(std::ostream & os, DocIterator const & dit)
{
for (size_t i = 0, n = dit.depth(); i != n; ++i)
}
+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);
+}
+
///////////////////////////////////////////////////////
}
-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;
if (inset == 0) {
// FIXME
lyxerr << BOOST_CURRENT_FUNCTION
- << " Should not happen, but does e.g. after C-n C-l C-z S-C-z"
+ << " Should not happen, but does e.g. after C-n C-l C-z S-C-z\n"
+ << " or when a Buffer has been concurently edited by two views"
<< '\n' << "dit: " << dit << '\n'
<< " lastpos: " << dit.lastpos() << endl;
- //break;
- BOOST_ASSERT(false);
+ dit.fixIfBroken();
+ break;
}
dit.push_back(data_[i]);
dit.top().inset_ = inset;
+ if (dit.fixIfBroken())
+ break;
if (i + 1 != n)
inset = dit.nextInset();
}