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 "support/lassert.h"
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 LASSERT(!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 LASSERT(!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 LASSERT(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 LASSERT(!empty(), /**/);
118 LASSERT(pos() > 0, /**/);
119 return cell()[pos() - 1];
123 MathAtom & DocIterator::nextAtom() const
125 LASSERT(!empty(), /**/);
126 //lyxerr << "lastpos: " << lastpos() << " next atom:\n" << *this << endl;
127 LASSERT(pos() < lastpos(), /**/);
128 return cell()[pos()];
132 Text * DocIterator::text() const
134 LASSERT(!empty(), /**/);
139 Paragraph & DocIterator::paragraph() const
143 LASSERT(inTexted(), /**/);
144 return top().paragraph();
148 Paragraph & DocIterator::innerParagraph() const
150 LASSERT(!empty(), /**/);
151 return innerTextSlice().paragraph();
155 CursorSlice const & DocIterator::innerTextSlice() const
157 LASSERT(!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_
166 LASSERT(false, /**/);
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 // LASSERT(inMathed(), /**/);
230 Text * DocIterator::innerText() const
232 LASSERT(!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 // not at cell/paragraph end?
265 if (tip.pos() != tip.lastpos()) {
266 // move into an inset to the right if possible
269 n = (tip.cell().begin() + tip.pos())->nucleus();
271 n = paragraph().getInset(tip.pos());
272 if (n && n->isActive()) {
273 //lyxerr << "... descend" << endl;
274 push_back(CursorSlice(*n));
279 // jump to the next cell/paragraph if possible
285 // otherwise leave inset and jump over inset as a whole
287 // 'tip' is invalid now...
293 void DocIterator::forwardPosIgnoreCollapsed()
295 Inset * const nextinset = nextInset();
296 // FIXME: the check for asInsetMath() shouldn't be necessary
297 // but math insets do not return a sensible editable() state yet.
298 if (nextinset && !nextinset->asInsetMath()
299 && nextinset->editable() != Inset::HIGHLY_EDITABLE) {
307 void DocIterator::forwardPar()
311 while (!empty() && (!inTexted() || pos() != 0)) {
313 pos_type const lastp = lastpos();
314 Paragraph const & par = paragraph();
315 pos_type & pos = top().pos();
316 if (par.insetList().empty())
319 while (pos < lastp && !par.isInset(pos))
327 void DocIterator::forwardChar()
330 while (!empty() && pos() == lastpos())
335 void DocIterator::forwardInset()
339 while (!empty() && !nextInset()) {
341 pos_type const lastp = lastpos();
342 Paragraph const & par = paragraph();
343 pos_type & pos = top().pos();
344 while (pos < lastp && !par.isInset(pos))
354 void DocIterator::backwardChar()
357 while (!empty() && pos() == lastpos())
362 void DocIterator::backwardPos()
364 //this dog bites his tail
366 push_back(CursorSlice(*inset_));
367 top().idx() = lastidx();
368 top().pit() = lastpit();
369 top().pos() = lastpos();
373 // at inset beginning?
374 if (top().at_begin()) {
381 // entered another cell/paragraph from the right?
382 if (top().pos() == top().lastpos())
385 // move into an inset to the left if possible
388 n = (top().cell().begin() + top().pos())->nucleus();
390 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) {
419 LASSERT(inset, /**/);
421 top().inset_ = inset;
425 //lyxerr << "converted:\n" << *this << endl;
429 bool DocIterator::fixIfBroken()
434 // Go through the slice stack from the bottom.
435 // Check that all coordinates (idx, pit, pos) are correct and
436 // that the inset is the one which is claimed to be there
437 Inset * inset = &slices_[0].inset();
439 size_t n = slices_.size();
440 for (; i != n; ++i) {
441 CursorSlice & cs = slices_[i];
442 if (&cs.inset() != inset) {
443 // the whole slice is wrong, chop off this as well
445 LYXERR(Debug::DEBUG, "fixIfBroken(): inset changed");
447 } else if (cs.idx() > cs.lastidx()) {
448 cs.idx() = cs.lastidx();
449 cs.pit() = cs.lastpit();
450 cs.pos() = cs.lastpos();
451 LYXERR(Debug::DEBUG, "fixIfBroken(): idx fixed");
453 } else if (cs.pit() > cs.lastpit()) {
454 cs.pit() = cs.lastpit();
455 cs.pos() = cs.lastpos();
456 LYXERR(Debug::DEBUG, "fixIfBroken(): pit fixed");
458 } else if (cs.pos() > cs.lastpos()) {
459 cs.pos() = cs.lastpos();
460 LYXERR(Debug::DEBUG, "fixIfBroken(): pos fixed");
462 } else if (i != n - 1 && cs.pos() != cs.lastpos()) {
463 // get inset which is supposed to be in the next slice
464 if (cs.inset().inMathed())
465 inset = (cs.cell().begin() + cs.pos())->nucleus();
466 else if (Inset * csInset = cs.paragraph().getInset(cs.pos()))
469 // there are slices left, so there must be another inset
475 // Did we make it through the whole slice stack? Otherwise there
476 // was a problem at slice i, and we have to chop off above
478 LYXERR(Debug::DEBUG, "fixIfBroken(): cursor chopped at " << i);
486 int DocIterator::find(MathData const & cell) const
488 for (size_t l = 0; l != slices_.size(); ++l) {
489 if (slices_[l].asInsetMath() && &slices_[l].cell() == &cell)
496 int DocIterator::find(Inset const * inset) const
498 for (size_t l = 0; l != slices_.size(); ++l) {
499 if (&slices_[l].inset() == inset)
506 void DocIterator::cutOff(int above, vector<CursorSlice> & cut)
508 cut = vector<CursorSlice>(slices_.begin() + above + 1, slices_.end());
509 slices_.resize(above + 1);
513 void DocIterator::cutOff(int above)
515 slices_.resize(above + 1);
519 void DocIterator::append(vector<CursorSlice> const & x)
521 slices_.insert(slices_.end(), x.begin(), x.end());
525 void DocIterator::append(DocIterator::idx_type idx, pos_type pos)
527 slices_.push_back(CursorSlice());
533 ostream & operator<<(ostream & os, DocIterator const & dit)
535 for (size_t i = 0, n = dit.depth(); i != n; ++i)
536 os << " " << dit[i] << "\n";
541 ///////////////////////////////////////////////////////
543 StableDocIterator::StableDocIterator(DocIterator const & dit)
545 data_ = dit.internalData();
546 for (size_t i = 0, n = data_.size(); i != n; ++i)
551 DocIterator StableDocIterator::asDocIterator(Inset * inset) const
553 // this function re-creates the cache of inset pointers
554 //lyxerr << "converting:\n" << *this << endl;
555 DocIterator dit = DocIterator(*inset);
556 for (size_t i = 0, n = data_.size(); i != n; ++i) {
559 LYXERR0(" Should not happen, but does e.g. after "
560 "C-n C-l C-z S-C-z\n"
561 << " or when a Buffer has been concurrently edited by two views"
562 << '\n' << "dit: " << dit << '\n'
563 << " lastpos: " << dit.lastpos());
567 dit.push_back(data_[i]);
568 dit.top().inset_ = inset;
569 if (dit.fixIfBroken())
572 inset = dit.nextInset();
574 //lyxerr << "convert:\n" << *this << " to:\n" << dit << endl;
579 ostream & operator<<(ostream & os, StableDocIterator const & dit)
581 for (size_t i = 0, n = dit.data_.size(); i != n; ++i)
582 os << " " << dit.data_[i] << "\n";
587 bool operator==(StableDocIterator const & dit1, StableDocIterator const & dit2)
589 return dit1.data_ == dit2.data_;