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 DocIterator DocIterator::clone(Buffer * buffer) const
77 LASSERT(buffer->isClone(), return DocIterator());
78 Inset * inset = &buffer->inset();
79 DocIterator dit = *this;
82 size_t const n = slices_.size();
83 for (size_t i = 0 ; i != n; ++i) {
85 dit.top().inset_ = inset;
87 inset = dit.nextInset();
93 bool DocIterator::inRegexped() const
95 InsetMathHull * i = dynamic_cast<InsetMathHull *>(inset().asInsetMath());
96 return i && i->getType() == hullRegexp;
100 LyXErr & operator<<(LyXErr & os, DocIterator const & it)
107 Inset * DocIterator::nextInset() const
109 LASSERT(!empty(), return 0);
110 if (pos() == lastpos())
112 if (pos() > lastpos()) {
113 LYXERR0("Should not happen, but it does: pos() = "
114 << pos() << ", lastpos() = " << lastpos());
118 return nextAtom().nucleus();
119 return paragraph().getInset(pos());
123 Inset * DocIterator::prevInset() const
125 LASSERT(!empty(), return 0);
130 // FIXME: this should not happen but it does.
132 // http://www.lyx.org/trac/ticket/3189
135 return prevAtom().nucleus();
137 return paragraph().getInset(pos() - 1);
141 Inset * DocIterator::realInset() const
143 LASSERT(inTexted(), /**/);
144 // if we are in a tabular, we need the cell
145 if (inset().lyxCode() == TABULAR_CODE) {
146 InsetTabular & tabular = static_cast<InsetTabular&>(inset());
147 return tabular.cell(idx()).get();
153 MathAtom & DocIterator::prevAtom() const
155 LASSERT(!empty(), /**/);
156 LASSERT(pos() > 0, /**/);
157 return cell()[pos() - 1];
161 MathAtom & DocIterator::nextAtom() const
163 LASSERT(!empty(), /**/);
164 //lyxerr << "lastpos: " << lastpos() << " next atom:\n" << *this << endl;
165 LASSERT(pos() < lastpos(), /**/);
166 return cell()[pos()];
170 Text * DocIterator::text() const
172 LASSERT(!empty(), /**/);
177 Paragraph & DocIterator::paragraph() const
181 LASSERT(inTexted(), /**/);
182 return top().paragraph();
186 Paragraph & DocIterator::innerParagraph() const
188 LASSERT(!empty(), /**/);
189 return innerTextSlice().paragraph();
193 CursorSlice const & DocIterator::innerTextSlice() const
195 LASSERT(!empty(), /**/);
196 // go up until first non-0 text is hit
197 // (innermost text is 0 in mathed)
198 for (int i = depth() - 1; i >= 0; --i)
199 if (slices_[i].text())
202 // This case is in principe not possible. We _must_
204 LASSERT(false, /**/);
205 static CursorSlice dummy;
210 pit_type DocIterator::lastpit() const
212 return inMathed() ? 0 : text()->paragraphs().size() - 1;
216 pos_type DocIterator::lastpos() const
218 return inMathed() ? cell().size() : paragraph().size();
222 DocIterator::idx_type DocIterator::lastidx() const
224 return top().lastidx();
228 size_t DocIterator::nargs() const
230 // assume 1x1 grid for main text
231 return top().nargs();
235 size_t DocIterator::ncols() const
237 // assume 1x1 grid for main text
238 return top().ncols();
242 size_t DocIterator::nrows() const
244 // assume 1x1 grid for main text
245 return top().nrows();
249 DocIterator::row_type DocIterator::row() const
255 DocIterator::col_type DocIterator::col() const
261 MathData & DocIterator::cell() const
263 // LASSERT(inMathed(), /**/);
268 Text * DocIterator::innerText() const
270 LASSERT(!empty(), /**/);
271 return innerTextSlice().text();
275 Inset * DocIterator::innerInsetOfType(int code) const
277 for (int i = depth() - 1; i >= 0; --i)
278 if (slices_[i].inset_->lyxCode() == code)
279 return slices_[i].inset_;
284 // This duplicates code above, but is in the critical path.
285 // So please think twice before adding stuff
286 void DocIterator::forwardPos()
288 // this dog bites his tail
290 push_back(CursorSlice(*inset_));
294 CursorSlice & tip = top();
295 //lyxerr << "XXX\n" << *this << endl;
297 // not at cell/paragraph end?
298 if (tip.pos() != tip.lastpos()) {
299 // move into an inset to the right if possible
302 n = (tip.cell().begin() + tip.pos())->nucleus();
304 n = paragraph().getInset(tip.pos());
305 if (n && n->isActive()) {
306 //lyxerr << "... descend" << endl;
307 push_back(CursorSlice(*n));
312 // jump to the next cell/paragraph if possible
318 // otherwise leave inset and jump over inset as a whole
320 // 'tip' is invalid now...
326 void DocIterator::forwardPosIgnoreCollapsed()
328 Inset * const nextinset = nextInset();
329 // FIXME: the check for asInsetMath() shouldn't be necessary
330 // but math insets do not return a sensible editable() state yet.
331 if (nextinset && !nextinset->asInsetMath()
332 && !nextinset->editable()) {
340 void DocIterator::forwardPar()
344 while (!empty() && (!inTexted() || pos() != 0)) {
346 pos_type const lastp = lastpos();
347 Paragraph const & par = paragraph();
348 pos_type & pos = top().pos();
349 if (par.insetList().empty())
352 while (pos < lastp && !par.isInset(pos))
360 void DocIterator::forwardChar()
363 while (!empty() && pos() == lastpos())
368 void DocIterator::forwardInset()
372 while (!empty() && !nextInset()) {
374 pos_type const lastp = lastpos();
375 Paragraph const & par = paragraph();
376 pos_type & pos = top().pos();
377 while (pos < lastp && !par.isInset(pos))
387 void DocIterator::backwardChar()
390 while (!empty() && pos() == lastpos())
395 void DocIterator::backwardPos()
397 //this dog bites his tail
399 push_back(CursorSlice(*inset_));
400 top().idx() = lastidx();
401 top().pit() = lastpit();
402 top().pos() = lastpos();
406 // at inset beginning?
407 if (top().at_begin()) {
414 // entered another cell/paragraph from the right?
415 if (top().pos() == top().lastpos())
418 // move into an inset to the left if possible
421 n = (top().cell().begin() + top().pos())->nucleus();
423 n = paragraph().getInset(top().pos());
424 if (n && n->isActive()) {
425 push_back(CursorSlice(*n));
426 top().idx() = lastidx();
427 top().pit() = lastpit();
428 top().pos() = lastpos();
433 bool DocIterator::hasPart(DocIterator const & it) const
435 // it can't be a part if it is larger
436 if (it.depth() > depth())
439 // as inset adresses are the 'last' level
440 return &it.top().inset() == &slices_[it.depth() - 1].inset();
444 void DocIterator::updateInsets(Inset * inset)
446 // this function re-creates the cache of inset pointers.
447 //lyxerr << "converting:\n" << *this << endl;
448 DocIterator dit = *this;
449 size_t const n = slices_.size();
451 for (size_t i = 0 ; i < n; ++i) {
452 LASSERT(inset, /**/);
454 top().inset_ = inset;
458 //lyxerr << "converted:\n" << *this << endl;
462 bool DocIterator::fixIfBroken()
467 // Go through the slice stack from the bottom.
468 // Check that all coordinates (idx, pit, pos) are correct and
469 // that the inset is the one which is claimed to be there
470 Inset * inset = &slices_[0].inset();
472 size_t n = slices_.size();
473 for (; i != n; ++i) {
474 CursorSlice & cs = slices_[i];
475 if (&cs.inset() != inset) {
476 // the whole slice is wrong, chop off this as well
478 LYXERR(Debug::DEBUG, "fixIfBroken(): inset changed");
480 } else if (cs.idx() > cs.lastidx()) {
481 cs.idx() = cs.lastidx();
482 cs.pit() = cs.lastpit();
483 cs.pos() = cs.lastpos();
484 LYXERR(Debug::DEBUG, "fixIfBroken(): idx fixed");
486 } else if (cs.pit() > cs.lastpit()) {
487 cs.pit() = cs.lastpit();
488 cs.pos() = cs.lastpos();
489 LYXERR(Debug::DEBUG, "fixIfBroken(): pit fixed");
491 } else if (cs.pos() > cs.lastpos()) {
492 cs.pos() = cs.lastpos();
493 LYXERR(Debug::DEBUG, "fixIfBroken(): pos fixed");
495 } else if (i != n - 1 && cs.pos() != cs.lastpos()) {
496 // get inset which is supposed to be in the next slice
497 if (cs.inset().inMathed())
498 inset = (cs.cell().begin() + cs.pos())->nucleus();
499 else if (Inset * csInset = cs.paragraph().getInset(cs.pos()))
502 // there are slices left, so there must be another inset
508 // Did we make it through the whole slice stack? Otherwise there
509 // was a problem at slice i, and we have to chop off above
511 LYXERR(Debug::DEBUG, "fixIfBroken(): cursor chopped at " << i);
519 int DocIterator::find(MathData const & cell) const
521 for (size_t l = 0; l != slices_.size(); ++l) {
522 if (slices_[l].asInsetMath() && &slices_[l].cell() == &cell)
529 int DocIterator::find(Inset const * inset) const
531 for (size_t l = 0; l != slices_.size(); ++l) {
532 if (&slices_[l].inset() == inset)
539 void DocIterator::cutOff(int above, vector<CursorSlice> & cut)
541 cut = vector<CursorSlice>(slices_.begin() + above + 1, slices_.end());
542 slices_.resize(above + 1);
546 void DocIterator::cutOff(int above)
548 slices_.resize(above + 1);
552 void DocIterator::append(vector<CursorSlice> const & x)
554 slices_.insert(slices_.end(), x.begin(), x.end());
558 void DocIterator::append(DocIterator::idx_type idx, pos_type pos)
560 slices_.push_back(CursorSlice());
566 ostream & operator<<(ostream & os, DocIterator const & dit)
568 for (size_t i = 0, n = dit.depth(); i != n; ++i)
569 os << " " << dit[i] << "\n";
574 ///////////////////////////////////////////////////////
576 StableDocIterator::StableDocIterator(DocIterator const & dit)
578 data_ = dit.internalData();
579 for (size_t i = 0, n = data_.size(); i != n; ++i)
584 DocIterator StableDocIterator::asDocIterator(Buffer * buf) const
586 // this function re-creates the cache of inset pointers
587 //lyxerr << "converting:\n" << *this << endl;
588 Inset * inset = &buf->inset();
589 DocIterator dit = DocIterator(buf, inset);
590 for (size_t i = 0, n = data_.size(); i != n; ++i) {
593 LYXERR0(" Should not happen, but does e.g. after "
594 "C-n C-l C-z S-C-z\n"
595 << " or when a Buffer has been concurrently edited by two views"
596 << '\n' << "dit: " << dit << '\n'
597 << " lastpos: " << dit.lastpos());
601 dit.push_back(data_[i]);
602 dit.top().inset_ = inset;
603 if (dit.fixIfBroken())
606 inset = dit.nextInset();
608 //lyxerr << "convert:\n" << *this << " to:\n" << dit << endl;
613 ostream & operator<<(ostream & os, StableDocIterator const & dit)
615 for (size_t i = 0, n = dit.data_.size(); i != n; ++i)
616 os << " " << dit.data_[i] << "\n";
621 bool operator==(StableDocIterator const & dit1, StableDocIterator const & dit2)
623 return dit1.data_ == dit2.data_;