+ //this dog bites his tail
+ if (empty()) {
+ push_back(CursorSlice(*inset_));
+ top().idx() = lastidx();
+ top().pit() = lastpit();
+ top().pos() = lastpos();
+ return;
+ }
+
+ CursorSlice & tip = top();
+
+ if (tip.pos() != 0) {
+ --tip.pos();
+ } else if (tip.pit() != 0) {
+ --tip.pit();
+ tip.pos() = lastpos();
+ return;
+ } else if (tip.idx() != 0) {
+ --tip.idx();
+ tip.pit() = lastpit();
+ tip.pos() = lastpos();
+ return;
+ } else {
+ pop_back();
+ return;
+ }
+
+ // move into an inset to the left if possible
+ InsetBase * n = 0;
+
+ if (inMathed()) {
+ n = (tip.cell().begin() + tip.pos())->nucleus();
+ } else {
+ if (paragraph().isInset(tip.pos()))
+ n = paragraph().getInset(tip.pos());
+ }
+
+ if (n && n->isActive()) {
+ push_back(CursorSlice(*n));
+ top().idx() = lastidx();
+ top().pit() = lastpit();
+ top().pos() = lastpos();
+ }
+}
+
+
+bool DocIterator::hasPart(DocIterator const & it) const
+{
+ // it can't be a part if it is larger
+ if (it.depth() > depth())
+ return false;
+
+ // as inset adresses are the 'last' level
+ return &it.top().inset() == &slices_[it.depth() - 1].inset();
+}
+
+
+void DocIterator::updateInsets(InsetBase * inset)
+{
+ // this function re-creates the cache of inset pointers.
+ // code taken in part from StableDocIterator::asDocIterator.
+ //lyxerr << "converting:\n" << *this << endl;
+ DocIterator dit = DocIterator(*inset);
+ size_t const n = slices_.size();
+ for (size_t i = 0 ; i < n; ++i) {
+ BOOST_ASSERT(inset);
+ dit.push_back(slices_[i]);
+ dit.top().inset_ = inset;
+ if (i + 1 != n)
+ inset = dit.nextInset();
+ }
+ //lyxerr << "converted:\n" << *this << endl;
+ operator=(dit);
+}
+
+
+std::ostream & operator<<(std::ostream & os, DocIterator const & dit)
+{
+ for (size_t i = 0, n = dit.depth(); i != n; ++i)
+ os << " " << dit[i] << "\n";