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"
18 #include "InsetList.h"
19 #include "Paragraph.h"
22 #include "mathed/MathData.h"
23 #include "mathed/InsetMath.h"
25 #include "insets/InsetTabular.h"
27 #include <boost/assert.hpp>
28 #include <boost/current_function.hpp>
36 // We could be able to get rid of this if only every BufferView were
37 // associated to a buffer on construction.
38 DocIterator::DocIterator()
39 : boundary_(false), inset_(0)
43 DocIterator::DocIterator(Inset & inset)
44 : boundary_(false), inset_(&inset)
48 DocIterator doc_iterator_begin(Inset & inset)
50 DocIterator dit(inset);
56 DocIterator doc_iterator_end(Inset & inset)
58 return DocIterator(inset);
62 Inset * DocIterator::nextInset() const
64 BOOST_ASSERT(!empty());
65 if (pos() == lastpos())
67 if (pos() > lastpos()) {
68 lyxerr << "Should not happen, but it does. " << endl;
72 return nextAtom().nucleus();
73 return paragraph().isInset(pos()) ? paragraph().getInset(pos()) : 0;
77 Inset * DocIterator::prevInset() const
79 BOOST_ASSERT(!empty());
84 // FIXME: this should not happen but it does.
86 // http://bugzilla.lyx.org/show_bug.cgi?id=3189
89 return prevAtom().nucleus();
91 return paragraph().isInset(pos() - 1) ? paragraph().getInset(pos() - 1) : 0;
95 Inset * DocIterator::realInset() const
97 BOOST_ASSERT(inTexted());
98 // if we are in a tabular, we need the cell
99 if (inset().lyxCode() == TABULAR_CODE) {
100 InsetTabular & tabular = static_cast<InsetTabular&>(inset());
101 return tabular.cell(idx()).get();
107 MathAtom & DocIterator::prevAtom() const
109 BOOST_ASSERT(!empty());
110 BOOST_ASSERT(pos() > 0);
111 return cell()[pos() - 1];
115 MathAtom & DocIterator::nextAtom() const
117 BOOST_ASSERT(!empty());
118 //lyxerr << "lastpos: " << lastpos() << " next atom:\n" << *this << endl;
119 BOOST_ASSERT(pos() < lastpos());
120 return cell()[pos()];
124 Text * DocIterator::text() const
126 BOOST_ASSERT(!empty());
131 Paragraph & DocIterator::paragraph() const
134 lyxerr << *this << endl;
135 BOOST_ASSERT(inTexted());
136 return top().paragraph();
140 Paragraph & DocIterator::innerParagraph() const
142 BOOST_ASSERT(!empty());
143 return innerTextSlice().paragraph();
147 CursorSlice const & DocIterator::innerTextSlice() const
149 BOOST_ASSERT(!empty());
150 // go up until first non-0 text is hit
151 // (innermost text is 0 in mathed)
152 for (int i = depth() - 1; i >= 0; --i)
153 if (slices_[i].text())
156 // This case is in principe not possible. We _must_
159 static CursorSlice dummy;
164 pit_type DocIterator::lastpit() const
166 return inMathed() ? 0 : text()->paragraphs().size() - 1;
170 pos_type DocIterator::lastpos() const
172 return inMathed() ? cell().size() : paragraph().size();
176 DocIterator::idx_type DocIterator::lastidx() const
178 return top().lastidx();
182 size_t DocIterator::nargs() const
184 // assume 1x1 grid for main text
185 return top().nargs();
189 size_t DocIterator::ncols() const
191 // assume 1x1 grid for main text
192 return top().ncols();
196 size_t DocIterator::nrows() const
198 // assume 1x1 grid for main text
199 return top().nrows();
203 DocIterator::row_type DocIterator::row() const
209 DocIterator::col_type DocIterator::col() const
215 MathData & DocIterator::cell() const
217 // BOOST_ASSERT(inMathed());
222 Text * DocIterator::innerText() const
224 BOOST_ASSERT(!empty());
225 // go up until first non-0 text is hit
226 // (innermost text is 0 in mathed)
227 for (int i = depth() - 1; i >= 0; --i)
228 if (slices_[i].text())
229 return slices_[i].text();
234 Inset * DocIterator::innerInsetOfType(int code) const
236 for (int i = depth() - 1; i >= 0; --i)
237 if (slices_[i].inset_->lyxCode() == code)
238 return slices_[i].inset_;
243 void DocIterator::forwardPos(bool ignorecollapsed)
245 //this dog bites his tail
247 push_back(CursorSlice(*inset_));
251 Inset * const nextinset = nextInset();
252 // jump over collapsables if they are collapsed
253 // FIXME: the check for asInsetMath() shouldn't be necessary
254 // but math insets do not return a sensible editable() state yet.
255 if (ignorecollapsed && nextinset && (!nextinset->asInsetMath()
256 && nextinset->editable() != Inset::HIGHLY_EDITABLE)) {
261 CursorSlice & tip = top();
262 //lyxerr << "XXX\n" << *this << endl;
264 // this is used twice and shows up in the profiler!
265 pos_type const lastp = lastpos();
267 // move into an inset to the right if possible
270 if (tip.pos() != lastp) {
271 // this is impossible for pos() == size()
273 n = (tip.cell().begin() + tip.pos())->nucleus();
275 if (paragraph().isInset(tip.pos()))
276 n = paragraph().getInset(tip.pos());
280 if (n && n->isActive()) {
281 //lyxerr << "... descend" << endl;
282 push_back(CursorSlice(*n));
290 // otherwise leave inset and jump over inset as a whole
292 // 'tip' is invalid now...
298 void DocIterator::forwardPar()
302 while (!empty() && (!inTexted() || pos() != 0)) {
304 pos_type const lastp = lastpos();
305 Paragraph const & par = paragraph();
306 pos_type & pos = top().pos();
307 if (par.insetList().empty())
310 while (pos < lastp && !par.isInset(pos))
318 void DocIterator::forwardChar()
321 while (!empty() && pos() == lastpos())
326 void DocIterator::forwardInset()
330 while (!empty() && !nextInset()) {
332 pos_type const lastp = lastpos();
333 Paragraph const & par = paragraph();
334 pos_type & pos = top().pos();
335 while (pos < lastp && !par.isInset(pos))
345 void DocIterator::backwardChar()
348 while (!empty() && pos() == lastpos())
353 void DocIterator::backwardPos()
355 //this dog bites his tail
357 push_back(CursorSlice(*inset_));
358 top().idx() = lastidx();
359 top().pit() = lastpit();
360 top().pos() = lastpos();
364 if (top().at_begin()) {
371 // move into an inset to the left if possible
375 n = (top().cell().begin() + top().pos())->nucleus();
377 if (paragraph().isInset(top().pos()))
378 n = paragraph().getInset(top().pos());
381 if (n && n->isActive()) {
382 push_back(CursorSlice(*n));
383 top().idx() = lastidx();
384 top().pit() = lastpit();
385 top().pos() = lastpos();
390 bool DocIterator::hasPart(DocIterator const & it) const
392 // it can't be a part if it is larger
393 if (it.depth() > depth())
396 // as inset adresses are the 'last' level
397 return &it.top().inset() == &slices_[it.depth() - 1].inset();
401 void DocIterator::updateInsets(Inset * inset)
403 // this function re-creates the cache of inset pointers.
404 //lyxerr << "converting:\n" << *this << endl;
405 DocIterator dit = *this;
406 size_t const n = slices_.size();
408 for (size_t i = 0 ; i < n; ++i) {
411 top().inset_ = inset;
415 //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");
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");
440 } else if (cs.pit() > cs.lastpit()) {
441 cs.pit() = cs.lastpit();
442 cs.pos() = cs.lastpos();
443 LYXERR(Debug::DEBUG, "fixIfBroken(): pit fixed");
445 } else if (cs.pos() > cs.lastpos()) {
446 cs.pos() = cs.lastpos();
447 LYXERR(Debug::DEBUG, "fixIfBroken(): pos fixed");
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);
473 DocIterator::idx_type DocIterator::find(MathData const & cell) const
475 for (size_t l = 0; l != slices_.size(); ++l) {
476 if (slices_[l].asInsetMath() && &slices_[l].cell() == &cell)
483 DocIterator::idx_type DocIterator::find(InsetMath const * inset) const
485 for (size_t l = 0; l != slices_.size(); ++l) {
486 if (slices_[l].asInsetMath() == inset)
493 void DocIterator::cutOff(DocIterator::idx_type above, std::vector<CursorSlice> & cut)
495 cut = std::vector<CursorSlice>(slices_.begin() + above + 1, slices_.end());
496 slices_.resize(above + 1);
500 void DocIterator::cutOff(DocIterator::idx_type above)
502 slices_.resize(above + 1);
506 void DocIterator::append(std::vector<CursorSlice> const & x)
508 slices_.insert(slices_.end(), x.begin(), x.end());
512 void DocIterator::append(DocIterator::idx_type idx, pos_type pos)
514 slices_.push_back(CursorSlice());
520 std::ostream & operator<<(std::ostream & os, DocIterator const & dit)
522 for (size_t i = 0, n = dit.depth(); i != n; ++i)
523 os << " " << dit[i] << "\n";
528 bool operator<(DocIterator const & p, DocIterator const & q)
530 size_t depth = std::min(p.depth(), q.depth());
531 for (size_t i = 0 ; i < depth ; ++i) {
535 return p.depth() < q.depth();
539 bool operator>(DocIterator const & p, DocIterator const & q)
545 bool operator<=(DocIterator const & p, DocIterator const & q)
551 ///////////////////////////////////////////////////////
553 StableDocIterator::StableDocIterator(DocIterator const & dit)
555 data_ = dit.internalData();
556 for (size_t i = 0, n = data_.size(); i != n; ++i)
561 DocIterator StableDocIterator::asDocIterator(Inset * inset) const
563 // this function re-creates the cache of inset pointers
564 //lyxerr << "converting:\n" << *this << endl;
565 DocIterator dit = DocIterator(*inset);
566 for (size_t i = 0, n = data_.size(); i != n; ++i) {
569 lyxerr << BOOST_CURRENT_FUNCTION
570 << " Should not happen, but does e.g. after C-n C-l C-z S-C-z\n"
571 << " or when a Buffer has been concurently edited by two views"
572 << '\n' << "dit: " << dit << '\n'
573 << " lastpos: " << dit.lastpos() << endl;
577 dit.push_back(data_[i]);
578 dit.top().inset_ = inset;
579 if (dit.fixIfBroken())
582 inset = dit.nextInset();
584 //lyxerr << "convert:\n" << *this << " to:\n" << dit << endl;
589 std::ostream & operator<<(std::ostream & os, StableDocIterator const & dit)
591 for (size_t i = 0, n = dit.data_.size(); i != n; ++i)
592 os << " " << dit.data_[i] << "\n";
597 bool operator==(StableDocIterator const & dit1, StableDocIterator const & dit2)
599 return dit1.data_ == dit2.data_;