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"
23 #include "mathed/MathData.h"
24 #include "mathed/InsetMath.h"
25 #include "mathed/InsetMathHull.h"
27 #include "insets/InsetTabular.h"
29 #include "support/debug.h"
30 #include "support/lassert.h"
31 #include "support/lstrings.h"
36 using namespace lyx::support;
41 DocIterator::DocIterator()
42 : boundary_(false), inset_(0), buffer_(0)
45 // We could be able to get rid of this if only every BufferView were
46 // associated to a buffer on construction.
47 DocIterator::DocIterator(Buffer * buf)
48 : boundary_(false), inset_(0), buffer_(buf)
52 DocIterator::DocIterator(Buffer * buf, Inset * inset)
53 : boundary_(false), inset_(inset), buffer_(buf)
57 DocIterator doc_iterator_begin(const Buffer * buf0, const Inset * inset0)
59 Buffer * buf = const_cast<Buffer *>(buf0);
60 Inset * inset = const_cast<Inset *>(inset0);
61 DocIterator dit(buf, inset ? inset : &buf->inset());
67 DocIterator doc_iterator_end(const Buffer * buf0, const Inset * inset0)
69 Buffer * buf = const_cast<Buffer *>(buf0);
70 Inset * inset = const_cast<Inset *>(inset0);
71 return DocIterator(buf, inset ? inset : &buf->inset());
75 bool DocIterator::inRegexped() const
77 InsetMathHull * i = dynamic_cast<InsetMathHull *>(inset().asInsetMath());
78 return i && i->getType() == hullRegexp;
82 LyXErr & operator<<(LyXErr & os, DocIterator const & it)
89 Inset * DocIterator::nextInset() const
91 LASSERT(!empty(), return 0);
92 if (pos() == lastpos())
94 if (pos() > lastpos()) {
95 LYXERR0("Should not happen, but it does. ");
99 return nextAtom().nucleus();
100 return paragraph().getInset(pos());
104 Inset * DocIterator::prevInset() const
106 LASSERT(!empty(), return 0);
111 // FIXME: this should not happen but it does.
113 // http://bugzilla.lyx.org/show_bug.cgi?id=3189
116 return prevAtom().nucleus();
118 return paragraph().getInset(pos() - 1);
122 Inset * DocIterator::realInset() const
124 LASSERT(inTexted(), /**/);
125 // if we are in a tabular, we need the cell
126 if (inset().lyxCode() == TABULAR_CODE) {
127 InsetTabular & tabular = static_cast<InsetTabular&>(inset());
128 return tabular.cell(idx()).get();
134 MathAtom & DocIterator::prevAtom() const
136 LASSERT(!empty(), /**/);
137 LASSERT(pos() > 0, /**/);
138 return cell()[pos() - 1];
142 MathAtom & DocIterator::nextAtom() const
144 LASSERT(!empty(), /**/);
145 //lyxerr << "lastpos: " << lastpos() << " next atom:\n" << *this << endl;
146 LASSERT(pos() < lastpos(), /**/);
147 return cell()[pos()];
151 Text * DocIterator::text() const
153 LASSERT(!empty(), /**/);
158 Paragraph & DocIterator::paragraph() const
162 LASSERT(inTexted(), /**/);
163 return top().paragraph();
167 Paragraph & DocIterator::innerParagraph() const
169 LASSERT(!empty(), /**/);
170 return innerTextSlice().paragraph();
174 CursorSlice const & DocIterator::innerTextSlice() const
176 LASSERT(!empty(), /**/);
177 // go up until first non-0 text is hit
178 // (innermost text is 0 in mathed)
179 for (int i = depth() - 1; i >= 0; --i)
180 if (slices_[i].text())
183 // This case is in principe not possible. We _must_
185 LASSERT(false, /**/);
186 static CursorSlice dummy;
191 pit_type DocIterator::lastpit() const
193 return inMathed() ? 0 : text()->paragraphs().size() - 1;
197 pos_type DocIterator::lastpos() const
199 return inMathed() ? cell().size() : paragraph().size();
203 DocIterator::idx_type DocIterator::lastidx() const
205 return top().lastidx();
209 size_t DocIterator::nargs() const
211 // assume 1x1 grid for main text
212 return top().nargs();
216 size_t DocIterator::ncols() const
218 // assume 1x1 grid for main text
219 return top().ncols();
223 size_t DocIterator::nrows() const
225 // assume 1x1 grid for main text
226 return top().nrows();
230 DocIterator::row_type DocIterator::row() const
236 DocIterator::col_type DocIterator::col() const
242 MathData & DocIterator::cell() const
244 // LASSERT(inMathed(), /**/);
249 Text * DocIterator::innerText() const
251 LASSERT(!empty(), /**/);
252 return innerTextSlice().text();
256 Inset * DocIterator::innerInsetOfType(int code) const
258 for (int i = depth() - 1; i >= 0; --i)
259 if (slices_[i].inset_->lyxCode() == code)
260 return slices_[i].inset_;
265 // This duplicates code above, but is in the critical path.
266 // So please think twice before adding stuff
267 void DocIterator::forwardPos()
269 // this dog bites his tail
271 push_back(CursorSlice(*inset_));
275 CursorSlice & tip = top();
276 //lyxerr << "XXX\n" << *this << endl;
278 // not at cell/paragraph end?
279 if (tip.pos() != tip.lastpos()) {
280 // move into an inset to the right if possible
283 n = (tip.cell().begin() + tip.pos())->nucleus();
285 n = paragraph().getInset(tip.pos());
286 if (n && n->isActive()) {
287 //lyxerr << "... descend" << endl;
288 push_back(CursorSlice(*n));
293 // jump to the next cell/paragraph if possible
299 // otherwise leave inset and jump over inset as a whole
301 // 'tip' is invalid now...
307 void DocIterator::forwardPosIgnoreCollapsed()
309 Inset * const nextinset = nextInset();
310 // FIXME: the check for asInsetMath() shouldn't be necessary
311 // but math insets do not return a sensible editable() state yet.
312 if (nextinset && !nextinset->asInsetMath()
313 && !nextinset->editable()) {
321 void DocIterator::forwardPar()
325 while (!empty() && (!inTexted() || pos() != 0)) {
327 pos_type const lastp = lastpos();
328 Paragraph const & par = paragraph();
329 pos_type & pos = top().pos();
330 if (par.insetList().empty())
333 while (pos < lastp && !par.isInset(pos))
341 void DocIterator::forwardChar()
344 while (!empty() && pos() == lastpos())
349 void DocIterator::forwardInset()
353 while (!empty() && !nextInset()) {
355 pos_type const lastp = lastpos();
356 Paragraph const & par = paragraph();
357 pos_type & pos = top().pos();
358 while (pos < lastp && !par.isInset(pos))
368 void DocIterator::backwardChar()
371 while (!empty() && pos() == lastpos())
376 void DocIterator::backwardPos()
378 //this dog bites his tail
380 push_back(CursorSlice(*inset_));
381 top().idx() = lastidx();
382 top().pit() = lastpit();
383 top().pos() = lastpos();
387 // at inset beginning?
388 if (top().at_begin()) {
395 // entered another cell/paragraph from the right?
396 if (top().pos() == top().lastpos())
399 // move into an inset to the left if possible
402 n = (top().cell().begin() + top().pos())->nucleus();
404 n = paragraph().getInset(top().pos());
405 if (n && n->isActive()) {
406 push_back(CursorSlice(*n));
407 top().idx() = lastidx();
408 top().pit() = lastpit();
409 top().pos() = lastpos();
414 bool DocIterator::hasPart(DocIterator const & it) const
416 // it can't be a part if it is larger
417 if (it.depth() > depth())
420 // as inset adresses are the 'last' level
421 return &it.top().inset() == &slices_[it.depth() - 1].inset();
425 void DocIterator::updateInsets(Inset * inset)
427 // this function re-creates the cache of inset pointers.
428 //lyxerr << "converting:\n" << *this << endl;
429 DocIterator dit = *this;
430 size_t const n = slices_.size();
432 for (size_t i = 0 ; i < n; ++i) {
433 LASSERT(inset, /**/);
435 top().inset_ = inset;
439 //lyxerr << "converted:\n" << *this << endl;
443 bool DocIterator::fixIfBroken()
448 // Go through the slice stack from the bottom.
449 // Check that all coordinates (idx, pit, pos) are correct and
450 // that the inset is the one which is claimed to be there
451 Inset * inset = &slices_[0].inset();
453 size_t n = slices_.size();
454 for (; i != n; ++i) {
455 CursorSlice & cs = slices_[i];
456 if (&cs.inset() != inset) {
457 // the whole slice is wrong, chop off this as well
459 LYXERR(Debug::DEBUG, "fixIfBroken(): inset changed");
461 } else if (cs.idx() > cs.lastidx()) {
462 cs.idx() = cs.lastidx();
463 cs.pit() = cs.lastpit();
464 cs.pos() = cs.lastpos();
465 LYXERR(Debug::DEBUG, "fixIfBroken(): idx fixed");
467 } else if (cs.pit() > cs.lastpit()) {
468 cs.pit() = cs.lastpit();
469 cs.pos() = cs.lastpos();
470 LYXERR(Debug::DEBUG, "fixIfBroken(): pit fixed");
472 } else if (cs.pos() > cs.lastpos()) {
473 cs.pos() = cs.lastpos();
474 LYXERR(Debug::DEBUG, "fixIfBroken(): pos fixed");
476 } else if (i != n - 1 && cs.pos() != cs.lastpos()) {
477 // get inset which is supposed to be in the next slice
478 if (cs.inset().inMathed())
479 inset = (cs.cell().begin() + cs.pos())->nucleus();
480 else if (Inset * csInset = cs.paragraph().getInset(cs.pos()))
483 // there are slices left, so there must be another inset
489 // Did we make it through the whole slice stack? Otherwise there
490 // was a problem at slice i, and we have to chop off above
492 LYXERR(Debug::DEBUG, "fixIfBroken(): cursor chopped at " << i);
500 int DocIterator::find(MathData const & cell) const
502 for (size_t l = 0; l != slices_.size(); ++l) {
503 if (slices_[l].asInsetMath() && &slices_[l].cell() == &cell)
510 int DocIterator::find(Inset const * inset) const
512 for (size_t l = 0; l != slices_.size(); ++l) {
513 if (&slices_[l].inset() == inset)
520 void DocIterator::cutOff(int above, vector<CursorSlice> & cut)
522 cut = vector<CursorSlice>(slices_.begin() + above + 1, slices_.end());
523 slices_.resize(above + 1);
527 void DocIterator::cutOff(int above)
529 slices_.resize(above + 1);
533 void DocIterator::append(vector<CursorSlice> const & x)
535 slices_.insert(slices_.end(), x.begin(), x.end());
539 void DocIterator::append(DocIterator::idx_type idx, pos_type pos)
541 slices_.push_back(CursorSlice());
547 ostream & operator<<(ostream & os, DocIterator const & dit)
549 for (size_t i = 0, n = dit.depth(); i != n; ++i)
550 os << " " << dit[i] << "\n";
555 ///////////////////////////////////////////////////////
557 StableDocIterator::StableDocIterator(DocIterator const & dit)
559 data_ = dit.internalData();
560 for (size_t i = 0, n = data_.size(); i != n; ++i)
565 DocIterator StableDocIterator::asDocIterator(Buffer * buf) const
567 // this function re-creates the cache of inset pointers
568 //lyxerr << "converting:\n" << *this << endl;
569 Inset * inset = &buf->inset();
570 DocIterator dit = DocIterator(buf, inset);
571 for (size_t i = 0, n = data_.size(); i != n; ++i) {
574 LYXERR0(" Should not happen, but does e.g. after "
575 "C-n C-l C-z S-C-z\n"
576 << " or when a Buffer has been concurrently edited by two views"
577 << '\n' << "dit: " << dit << '\n'
578 << " lastpos: " << dit.lastpos());
582 dit.push_back(data_[i]);
583 dit.top().inset_ = inset;
584 if (dit.fixIfBroken())
587 inset = dit.nextInset();
589 //lyxerr << "convert:\n" << *this << " to:\n" << dit << endl;
594 ostream & operator<<(ostream & os, StableDocIterator const & dit)
596 for (size_t i = 0, n = dit.data_.size(); i != n; ++i)
597 os << " " << dit.data_[i] << "\n";
602 bool operator==(StableDocIterator const & dit1, StableDocIterator const & dit2)
604 return dit1.data_ == dit2.data_;