2 * \file DocIterator.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
7 * \author Alfredo Braunstein
9 * Full author contact details are available in file CREDITS.
15 #include "DocIterator.h"
19 #include "Paragraph.h"
21 #include "mathed/MathData.h"
22 #include "mathed/InsetMath.h"
24 #include "insets/InsetTabular.h"
26 #include <boost/assert.hpp>
27 #include <boost/current_function.hpp>
35 // We could be able to get rid of this if only every BufferView were
36 // associated to a buffer on construction.
37 DocIterator::DocIterator()
38 : boundary_(false), inset_(0)
42 DocIterator::DocIterator(Inset & inset)
43 : boundary_(false), inset_(&inset)
47 DocIterator doc_iterator_begin(Inset & inset)
49 DocIterator dit(inset);
55 DocIterator doc_iterator_end(Inset & inset)
57 return DocIterator(inset);
61 Inset * DocIterator::nextInset() const
63 BOOST_ASSERT(!empty());
64 if (pos() == lastpos())
66 if (pos() > lastpos()) {
67 lyxerr << "Should not happen, but it does. " << endl;
71 return nextAtom().nucleus();
72 return paragraph().isInset(pos()) ? paragraph().getInset(pos()) : 0;
76 Inset * DocIterator::prevInset() const
78 BOOST_ASSERT(!empty());
83 // FIXME: this should not happen but it does.
85 // http://bugzilla.lyx.org/show_bug.cgi?id=3189
88 return prevAtom().nucleus();
90 return paragraph().isInset(pos() - 1) ? paragraph().getInset(pos() - 1) : 0;
94 Inset * DocIterator::realInset() const
96 BOOST_ASSERT(inTexted());
97 // if we are in a tabular, we need the cell
98 if (inset().lyxCode() == TABULAR_CODE) {
99 InsetTabular & tabular = static_cast<InsetTabular&>(inset());
100 return tabular.cell(idx()).get();
106 MathAtom & DocIterator::prevAtom() const
108 BOOST_ASSERT(!empty());
109 BOOST_ASSERT(pos() > 0);
110 return cell()[pos() - 1];
114 MathAtom & DocIterator::nextAtom() const
116 BOOST_ASSERT(!empty());
117 //lyxerr << "lastpos: " << lastpos() << " next atom:\n" << *this << endl;
118 BOOST_ASSERT(pos() < lastpos());
119 return cell()[pos()];
123 Text * DocIterator::text() const
125 BOOST_ASSERT(!empty());
130 Paragraph & DocIterator::paragraph() const
133 lyxerr << *this << endl;
134 BOOST_ASSERT(inTexted());
135 return top().paragraph();
139 Paragraph & DocIterator::innerParagraph() const
141 BOOST_ASSERT(!empty());
142 return innerTextSlice().paragraph();
146 CursorSlice const & DocIterator::innerTextSlice() const
148 BOOST_ASSERT(!empty());
149 // go up until first non-0 text is hit
150 // (innermost text is 0 in mathed)
151 for (int i = depth() - 1; i >= 0; --i)
152 if (slices_[i].text())
155 // This case is in principe not possible. We _must_
158 static CursorSlice dummy;
163 pit_type DocIterator::lastpit() const
165 return inMathed() ? 0 : text()->paragraphs().size() - 1;
169 pos_type DocIterator::lastpos() const
171 return inMathed() ? cell().size() : paragraph().size();
175 DocIterator::idx_type DocIterator::lastidx() const
177 return top().lastidx();
181 size_t DocIterator::nargs() const
183 // assume 1x1 grid for main text
184 return top().nargs();
188 size_t DocIterator::ncols() const
190 // assume 1x1 grid for main text
191 return top().ncols();
195 size_t DocIterator::nrows() const
197 // assume 1x1 grid for main text
198 return top().nrows();
202 DocIterator::row_type DocIterator::row() const
208 DocIterator::col_type DocIterator::col() const
214 MathData & DocIterator::cell() const
216 // BOOST_ASSERT(inMathed());
221 Text * DocIterator::innerText() const
223 BOOST_ASSERT(!empty());
224 // go up until first non-0 text is hit
225 // (innermost text is 0 in mathed)
226 for (int i = depth() - 1; i >= 0; --i)
227 if (slices_[i].text())
228 return slices_[i].text();
233 Inset * DocIterator::innerInsetOfType(int code) const
235 for (int i = depth() - 1; i >= 0; --i)
236 if (slices_[i].inset_->lyxCode() == code)
237 return slices_[i].inset_;
242 void DocIterator::forwardPos(bool ignorecollapsed)
244 //this dog bites his tail
246 push_back(CursorSlice(*inset_));
250 Inset * const nextinset = nextInset();
251 // jump over collapsables if they are collapsed
252 // FIXME: the check for asInsetMath() shouldn't be necessary
253 // but math insets do not return a sensible editable() state yet.
254 if (ignorecollapsed && nextinset && (!nextinset->asInsetMath()
255 && nextinset->editable() != Inset::HIGHLY_EDITABLE)) {
260 CursorSlice & tip = top();
261 //lyxerr << "XXX\n" << *this << endl;
263 // this is used twice and shows up in the profiler!
264 pos_type const lastp = lastpos();
266 // move into an inset to the right if possible
269 if (tip.pos() != lastp) {
270 // this is impossible for pos() == size()
272 n = (tip.cell().begin() + tip.pos())->nucleus();
274 if (paragraph().isInset(tip.pos()))
275 n = paragraph().getInset(tip.pos());
279 if (n && n->isActive()) {
280 //lyxerr << "... descend" << endl;
281 push_back(CursorSlice(*n));
289 // otherwise leave inset and jump over inset as a whole
291 // 'tip' is invalid now...
297 void DocIterator::forwardPar()
301 while (!empty() && (!inTexted() || pos() != 0)) {
303 pos_type const lastp = lastpos();
304 Paragraph const & par = paragraph();
305 pos_type & pos = top().pos();
306 if (par.insetlist.empty())
309 while (pos < lastp && !par.isInset(pos))
317 void DocIterator::forwardChar()
320 while (!empty() && pos() == lastpos())
325 void DocIterator::forwardInset()
329 while (!empty() && !nextInset()) {
331 pos_type const lastp = lastpos();
332 Paragraph const & par = paragraph();
333 pos_type & pos = top().pos();
334 while (pos < lastp && !par.isInset(pos))
344 void DocIterator::backwardChar()
347 while (!empty() && pos() == lastpos())
352 void DocIterator::backwardPos()
354 //this dog bites his tail
356 push_back(CursorSlice(*inset_));
357 top().idx() = lastidx();
358 top().pit() = lastpit();
359 top().pos() = lastpos();
363 if (top().at_begin()) {
370 // move into an inset to the left if possible
374 n = (top().cell().begin() + top().pos())->nucleus();
376 if (paragraph().isInset(top().pos()))
377 n = paragraph().getInset(top().pos());
380 if (n && n->isActive()) {
381 push_back(CursorSlice(*n));
382 top().idx() = lastidx();
383 top().pit() = lastpit();
384 top().pos() = lastpos();
389 bool DocIterator::hasPart(DocIterator const & it) const
391 // it can't be a part if it is larger
392 if (it.depth() > depth())
395 // as inset adresses are the 'last' level
396 return &it.top().inset() == &slices_[it.depth() - 1].inset();
400 void DocIterator::updateInsets(Inset * inset)
402 // this function re-creates the cache of inset pointers.
403 // code taken in part from StableDocIterator::asDocIterator.
404 //lyxerr << "converting:\n" << *this << endl;
405 DocIterator dit = DocIterator(*inset);
406 size_t const n = slices_.size();
407 for (size_t i = 0 ; i < n; ++i) {
409 dit.push_back(slices_[i]);
410 dit.top().inset_ = inset;
412 inset = dit.nextInset();
414 //lyxerr << "converted:\n" << *this << endl;
419 bool DocIterator::fixIfBroken()
421 // Go through the slice stack from the bottom.
422 // Check that all coordinates (idx, pit, pos) are correct and
423 // that the inset is the one which is claimed to be there
424 Inset * inset = &slices_[0].inset();
426 size_t n = slices_.size();
427 for (; i != n; ++i) {
428 CursorSlice & cs = slices_[i];
429 if (&cs.inset() != inset) {
430 // the whole slice is wrong, chop off this as well
432 LYXERR(Debug::DEBUG) << "fixIfBroken(): inset changed" << endl;
434 } else if (cs.idx() > cs.lastidx()) {
435 cs.idx() = cs.lastidx();
436 cs.pit() = cs.lastpit();
437 cs.pos() = cs.lastpos();
438 LYXERR(Debug::DEBUG) << "fixIfBroken(): idx fixed" << endl;
440 } else if (cs.pit() > cs.lastpit()) {
441 cs.pit() = cs.lastpit();
442 cs.pos() = cs.lastpos();
443 LYXERR(Debug::DEBUG) << "fixIfBroken(): pit fixed" << endl;
445 } else if (cs.pos() > cs.lastpos()) {
446 cs.pos() = cs.lastpos();
447 LYXERR(Debug::DEBUG) << "fixIfBroken(): pos fixed" << endl;
449 } else if (i != n - 1 && cs.pos() != cs.lastpos()) {
450 // get inset which is supposed to be in the next slice
451 if (cs.inset().inMathed())
452 inset = (cs.cell().begin() + cs.pos())->nucleus();
453 else if (cs.paragraph().isInset(cs.pos()))
454 inset = cs.paragraph().getInset(cs.pos());
456 // there are slices left, so there must be another inset
462 // Did we make it through the whole slice stack? Otherwise there
463 // was a problem at slice i, and we have to chop off above
465 LYXERR(Debug::DEBUG) << "fixIfBroken(): cursor chopped at " << i << endl;
473 std::ostream & operator<<(std::ostream & os, DocIterator const & dit)
475 for (size_t i = 0, n = dit.depth(); i != n; ++i)
476 os << " " << dit[i] << "\n";
481 bool operator<(DocIterator const & p, DocIterator const & q)
483 size_t depth = std::min(p.depth(), q.depth());
484 for (size_t i = 0 ; i < depth ; ++i) {
488 return p.depth() < q.depth();
492 bool operator>(DocIterator const & p, DocIterator const & q)
498 bool operator<=(DocIterator const & p, DocIterator const & q)
504 ///////////////////////////////////////////////////////
506 StableDocIterator::StableDocIterator(DocIterator const & dit)
508 data_ = dit.internalData();
509 for (size_t i = 0, n = data_.size(); i != n; ++i)
514 DocIterator StableDocIterator::asDocIterator(Inset * inset) const
516 // this function re-creates the cache of inset pointers
517 //lyxerr << "converting:\n" << *this << endl;
518 DocIterator dit = DocIterator(*inset);
519 for (size_t i = 0, n = data_.size(); i != n; ++i) {
522 lyxerr << BOOST_CURRENT_FUNCTION
523 << " Should not happen, but does e.g. after C-n C-l C-z S-C-z\n"
524 << " or when a Buffer has been concurently edited by two views"
525 << '\n' << "dit: " << dit << '\n'
526 << " lastpos: " << dit.lastpos() << endl;
530 dit.push_back(data_[i]);
531 dit.top().inset_ = inset;
532 if (dit.fixIfBroken())
535 inset = dit.nextInset();
537 //lyxerr << "convert:\n" << *this << " to:\n" << dit << endl;
542 std::ostream & operator<<(std::ostream & os, StableDocIterator const & dit)
544 for (size_t i = 0, n = dit.data_.size(); i != n; ++i)
545 os << " " << dit.data_[i] << "\n";
550 bool operator==(StableDocIterator const & dit1, StableDocIterator const & dit2)
552 return dit1.data_ == dit2.data_;