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"
17 #include "InsetList.h"
18 #include "Paragraph.h"
21 #include "mathed/MathData.h"
22 #include "mathed/InsetMath.h"
24 #include "insets/InsetTabular.h"
26 #include "support/debug.h"
28 #include <boost/assert.hpp>
37 // We could be able to get rid of this if only every BufferView were
38 // associated to a buffer on construction.
39 DocIterator::DocIterator()
40 : boundary_(false), inset_(0)
44 DocIterator::DocIterator(Inset & inset)
45 : boundary_(false), inset_(&inset)
49 DocIterator doc_iterator_begin(Inset & inset)
51 DocIterator dit(inset);
57 DocIterator doc_iterator_end(Inset & inset)
59 return DocIterator(inset);
63 LyXErr & operator<<(LyXErr & os, DocIterator const & it)
70 Inset * DocIterator::nextInset() const
72 BOOST_ASSERT(!empty());
73 if (pos() == lastpos())
75 if (pos() > lastpos()) {
76 LYXERR0("Should not happen, but it does. ");
80 return nextAtom().nucleus();
81 return paragraph().getInset(pos());
85 Inset * DocIterator::prevInset() const
87 BOOST_ASSERT(!empty());
92 // FIXME: this should not happen but it does.
94 // http://bugzilla.lyx.org/show_bug.cgi?id=3189
97 return prevAtom().nucleus();
99 return paragraph().getInset(pos() - 1);
103 Inset * DocIterator::realInset() const
105 BOOST_ASSERT(inTexted());
106 // if we are in a tabular, we need the cell
107 if (inset().lyxCode() == TABULAR_CODE) {
108 InsetTabular & tabular = static_cast<InsetTabular&>(inset());
109 return tabular.cell(idx()).get();
115 MathAtom & DocIterator::prevAtom() const
117 BOOST_ASSERT(!empty());
118 BOOST_ASSERT(pos() > 0);
119 return cell()[pos() - 1];
123 MathAtom & DocIterator::nextAtom() const
125 BOOST_ASSERT(!empty());
126 //lyxerr << "lastpos: " << lastpos() << " next atom:\n" << *this << endl;
127 BOOST_ASSERT(pos() < lastpos());
128 return cell()[pos()];
132 Text * DocIterator::text() const
134 BOOST_ASSERT(!empty());
139 Paragraph & DocIterator::paragraph() const
143 BOOST_ASSERT(inTexted());
144 return top().paragraph();
148 Paragraph & DocIterator::innerParagraph() const
150 BOOST_ASSERT(!empty());
151 return innerTextSlice().paragraph();
155 CursorSlice const & DocIterator::innerTextSlice() const
157 BOOST_ASSERT(!empty());
158 // go up until first non-0 text is hit
159 // (innermost text is 0 in mathed)
160 for (int i = depth() - 1; i >= 0; --i)
161 if (slices_[i].text())
164 // This case is in principe not possible. We _must_
167 static CursorSlice dummy;
172 pit_type DocIterator::lastpit() const
174 return inMathed() ? 0 : text()->paragraphs().size() - 1;
178 pos_type DocIterator::lastpos() const
180 return inMathed() ? cell().size() : paragraph().size();
184 DocIterator::idx_type DocIterator::lastidx() const
186 return top().lastidx();
190 size_t DocIterator::nargs() const
192 // assume 1x1 grid for main text
193 return top().nargs();
197 size_t DocIterator::ncols() const
199 // assume 1x1 grid for main text
200 return top().ncols();
204 size_t DocIterator::nrows() const
206 // assume 1x1 grid for main text
207 return top().nrows();
211 DocIterator::row_type DocIterator::row() const
217 DocIterator::col_type DocIterator::col() const
223 MathData & DocIterator::cell() const
225 // BOOST_ASSERT(inMathed());
230 Text * DocIterator::innerText() const
232 BOOST_ASSERT(!empty());
233 // go up until first non-0 text is hit
234 // (innermost text is 0 in mathed)
235 for (int i = depth() - 1; i >= 0; --i)
236 if (slices_[i].text())
237 return slices_[i].text();
242 Inset * DocIterator::innerInsetOfType(int code) const
244 for (int i = depth() - 1; i >= 0; --i)
245 if (slices_[i].inset_->lyxCode() == code)
246 return slices_[i].inset_;
251 // This duplicates code above, but is in the critical path.
252 // So please think twice before adding stuff
253 void DocIterator::forwardPos()
255 // this dog bites his tail
257 push_back(CursorSlice(*inset_));
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 n = paragraph().getInset(tip.pos());
278 if (n && n->isActive()) {
279 //lyxerr << "... descend" << endl;
280 push_back(CursorSlice(*n));
288 // otherwise leave inset and jump over inset as a whole
290 // 'tip' is invalid now...
296 void DocIterator::forwardPosIgnoreCollapsed()
298 Inset * const nextinset = nextInset();
299 // FIXME: the check for asInsetMath() shouldn't be necessary
300 // but math insets do not return a sensible editable() state yet.
301 if (nextinset && !nextinset->asInsetMath()
302 && nextinset->editable() != Inset::HIGHLY_EDITABLE) {
310 void DocIterator::forwardPar()
314 while (!empty() && (!inTexted() || pos() != 0)) {
316 pos_type const lastp = lastpos();
317 Paragraph const & par = paragraph();
318 pos_type & pos = top().pos();
319 if (par.insetList().empty())
322 while (pos < lastp && !par.isInset(pos))
330 void DocIterator::forwardChar()
333 while (!empty() && pos() == lastpos())
338 void DocIterator::forwardInset()
342 while (!empty() && !nextInset()) {
344 pos_type const lastp = lastpos();
345 Paragraph const & par = paragraph();
346 pos_type & pos = top().pos();
347 while (pos < lastp && !par.isInset(pos))
357 void DocIterator::backwardChar()
360 while (!empty() && pos() == lastpos())
365 void DocIterator::backwardPos()
367 //this dog bites his tail
369 push_back(CursorSlice(*inset_));
370 top().idx() = lastidx();
371 top().pit() = lastpit();
372 top().pos() = lastpos();
376 if (top().at_begin()) {
383 // move into an inset to the left if possible
387 n = (top().cell().begin() + top().pos())->nucleus();
389 n = paragraph().getInset(top().pos());
391 if (n && n->isActive()) {
392 push_back(CursorSlice(*n));
393 top().idx() = lastidx();
394 top().pit() = lastpit();
395 top().pos() = lastpos();
400 bool DocIterator::hasPart(DocIterator const & it) const
402 // it can't be a part if it is larger
403 if (it.depth() > depth())
406 // as inset adresses are the 'last' level
407 return &it.top().inset() == &slices_[it.depth() - 1].inset();
411 void DocIterator::updateInsets(Inset * inset)
413 // this function re-creates the cache of inset pointers.
414 //lyxerr << "converting:\n" << *this << endl;
415 DocIterator dit = *this;
416 size_t const n = slices_.size();
418 for (size_t i = 0 ; i < n; ++i) {
421 top().inset_ = inset;
425 //lyxerr << "converted:\n" << *this << endl;
429 bool DocIterator::fixIfBroken()
431 // Go through the slice stack from the bottom.
432 // Check that all coordinates (idx, pit, pos) are correct and
433 // that the inset is the one which is claimed to be there
434 Inset * inset = &slices_[0].inset();
436 size_t n = slices_.size();
437 for (; i != n; ++i) {
438 CursorSlice & cs = slices_[i];
439 if (&cs.inset() != inset) {
440 // the whole slice is wrong, chop off this as well
442 LYXERR(Debug::DEBUG, "fixIfBroken(): inset changed");
444 } else if (cs.idx() > cs.lastidx()) {
445 cs.idx() = cs.lastidx();
446 cs.pit() = cs.lastpit();
447 cs.pos() = cs.lastpos();
448 LYXERR(Debug::DEBUG, "fixIfBroken(): idx fixed");
450 } else if (cs.pit() > cs.lastpit()) {
451 cs.pit() = cs.lastpit();
452 cs.pos() = cs.lastpos();
453 LYXERR(Debug::DEBUG, "fixIfBroken(): pit fixed");
455 } else if (cs.pos() > cs.lastpos()) {
456 cs.pos() = cs.lastpos();
457 LYXERR(Debug::DEBUG, "fixIfBroken(): pos fixed");
459 } else if (i != n - 1 && cs.pos() != cs.lastpos()) {
460 // get inset which is supposed to be in the next slice
461 if (cs.inset().inMathed())
462 inset = (cs.cell().begin() + cs.pos())->nucleus();
463 else if (Inset * csInset = cs.paragraph().getInset(cs.pos()))
466 // there are slices left, so there must be another inset
472 // Did we make it through the whole slice stack? Otherwise there
473 // was a problem at slice i, and we have to chop off above
475 LYXERR(Debug::DEBUG, "fixIfBroken(): cursor chopped at " << i);
483 int DocIterator::find(MathData const & cell) const
485 for (size_t l = 0; l != slices_.size(); ++l) {
486 if (slices_[l].asInsetMath() && &slices_[l].cell() == &cell)
493 int DocIterator::find(InsetMath const * inset) const
495 for (size_t l = 0; l != slices_.size(); ++l) {
496 if (slices_[l].asInsetMath() == inset)
503 void DocIterator::cutOff(int above, vector<CursorSlice> & cut)
505 cut = vector<CursorSlice>(slices_.begin() + above + 1, slices_.end());
506 slices_.resize(above + 1);
510 void DocIterator::cutOff(int above)
512 slices_.resize(above + 1);
516 void DocIterator::append(vector<CursorSlice> const & x)
518 slices_.insert(slices_.end(), x.begin(), x.end());
522 void DocIterator::append(DocIterator::idx_type idx, pos_type pos)
524 slices_.push_back(CursorSlice());
530 ostream & operator<<(ostream & os, DocIterator const & dit)
532 for (size_t i = 0, n = dit.depth(); i != n; ++i)
533 os << " " << dit[i] << "\n";
538 ///////////////////////////////////////////////////////
540 StableDocIterator::StableDocIterator(DocIterator const & dit)
542 data_ = dit.internalData();
543 for (size_t i = 0, n = data_.size(); i != n; ++i)
548 DocIterator StableDocIterator::asDocIterator(Inset * inset) const
550 // this function re-creates the cache of inset pointers
551 //lyxerr << "converting:\n" << *this << endl;
552 DocIterator dit = DocIterator(*inset);
553 for (size_t i = 0, n = data_.size(); i != n; ++i) {
556 LYXERR0(" Should not happen, but does e.g. after "
557 "C-n C-l C-z S-C-z\n"
558 << " or when a Buffer has been concurrently edited by two views"
559 << '\n' << "dit: " << dit << '\n'
560 << " lastpos: " << dit.lastpos());
564 dit.push_back(data_[i]);
565 dit.top().inset_ = inset;
566 if (dit.fixIfBroken())
569 inset = dit.nextInset();
571 //lyxerr << "convert:\n" << *this << " to:\n" << dit << endl;
576 ostream & operator<<(ostream & os, StableDocIterator const & dit)
578 for (size_t i = 0, n = dit.data_.size(); i != n; ++i)
579 os << " " << dit.data_[i] << "\n";
584 bool operator==(StableDocIterator const & dit1, StableDocIterator const & dit2)
586 return dit1.data_ == dit2.data_;