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)
46 // We could be able to get rid of this if only every BufferView were
47 // associated to a buffer on construction.
48 DocIterator::DocIterator(Buffer * buf)
49 : boundary_(false), inset_(0), buffer_(buf)
53 DocIterator::DocIterator(Buffer * buf, Inset * inset)
54 : boundary_(false), inset_(inset), buffer_(buf)
58 DocIterator doc_iterator_begin(const Buffer * buf0, const Inset * inset0)
60 Buffer * buf = const_cast<Buffer *>(buf0);
61 Inset * inset = const_cast<Inset *>(inset0);
62 DocIterator dit(buf, inset ? inset : &buf->inset());
68 DocIterator doc_iterator_end(const Buffer * buf0, const Inset * inset0)
70 Buffer * buf = const_cast<Buffer *>(buf0);
71 Inset * inset = const_cast<Inset *>(inset0);
72 return DocIterator(buf, inset ? inset : &buf->inset());
76 DocIterator DocIterator::clone(Buffer * buffer) const
78 LASSERT(buffer->isClone(), return DocIterator());
79 Inset * inset = &buffer->inset();
80 DocIterator dit(buffer);
81 size_t const n = slices_.size();
82 for (size_t i = 0 ; i != n; ++i) {
84 dit.push_back(slices_[i]);
85 dit.top().inset_ = inset;
87 inset = dit.nextInset();
93 bool DocIterator::inRegexped() const
95 InsetMath * im = inset().asInsetMath();
98 InsetMathHull * hull = im->asHullInset();
99 return hull && hull->getType() == hullRegexp;
103 LyXErr & operator<<(LyXErr & os, DocIterator const & it)
110 Inset * DocIterator::nextInset() const
112 LASSERT(!empty(), return 0);
113 if (pos() == lastpos())
115 if (pos() > lastpos()) {
116 LYXERR0("Should not happen, but it does: pos() = "
117 << pos() << ", lastpos() = " << lastpos());
121 return nextAtom().nucleus();
122 return paragraph().getInset(pos());
126 Inset * DocIterator::prevInset() const
128 LASSERT(!empty(), return 0);
133 // FIXME: this should not happen but it does.
135 // http://www.lyx.org/trac/ticket/3189
138 return prevAtom().nucleus();
140 return paragraph().getInset(pos() - 1);
144 Inset * DocIterator::realInset() const
146 LASSERT(inTexted(), /**/);
147 // if we are in a tabular, we need the cell
148 if (inset().lyxCode() == TABULAR_CODE) {
149 InsetTabular * tabular = inset().asInsetTabular();
150 return tabular->cell(idx()).get();
156 MathAtom & DocIterator::prevAtom() const
158 LASSERT(!empty(), /**/);
159 LASSERT(pos() > 0, /**/);
160 return cell()[pos() - 1];
164 MathAtom & DocIterator::nextAtom() const
166 LASSERT(!empty(), /**/);
167 //lyxerr << "lastpos: " << lastpos() << " next atom:\n" << *this << endl;
168 LASSERT(pos() < lastpos(), /**/);
169 return cell()[pos()];
173 Text * DocIterator::text() const
175 LASSERT(!empty(), /**/);
180 Paragraph & DocIterator::paragraph() const
184 LASSERT(inTexted(), /**/);
185 return top().paragraph();
189 Paragraph & DocIterator::innerParagraph() const
191 LASSERT(!empty(), /**/);
192 return innerTextSlice().paragraph();
196 FontSpan DocIterator::locateWord(word_location const loc) const
198 FontSpan f = FontSpan();
200 if (!top().text()->empty()) {
202 top().paragraph().locateWord(f.first, f.last, loc);
208 CursorSlice const & DocIterator::innerTextSlice() const
210 LASSERT(!empty(), /**/);
211 // go up until first non-0 text is hit
212 // (innermost text is 0 in mathed)
213 for (int i = depth() - 1; i >= 0; --i)
214 if (slices_[i].text())
217 // This case is in principe not possible. We _must_
219 LASSERT(false, /**/);
220 static CursorSlice dummy;
225 pit_type DocIterator::lastpit() const
227 return inMathed() ? 0 : text()->paragraphs().size() - 1;
231 pos_type DocIterator::lastpos() const
233 return inMathed() ? cell().size() : paragraph().size();
237 DocIterator::idx_type DocIterator::lastidx() const
239 return top().lastidx();
243 size_t DocIterator::nargs() const
245 // assume 1x1 grid for main text
246 return top().nargs();
250 size_t DocIterator::ncols() const
252 // assume 1x1 grid for main text
253 return top().ncols();
257 size_t DocIterator::nrows() const
259 // assume 1x1 grid for main text
260 return top().nrows();
264 DocIterator::row_type DocIterator::row() const
270 DocIterator::col_type DocIterator::col() const
276 MathData & DocIterator::cell() const
278 // LASSERT(inMathed(), /**/);
283 Text * DocIterator::innerText() const
285 LASSERT(!empty(), /**/);
286 return innerTextSlice().text();
290 Inset * DocIterator::innerInsetOfType(int code) const
292 for (int i = depth() - 1; i >= 0; --i)
293 if (slices_[i].inset_->lyxCode() == code)
294 return slices_[i].inset_;
299 // This duplicates code above, but is in the critical path.
300 // So please think twice before adding stuff
301 void DocIterator::forwardPos()
303 // this dog bites his tail
305 push_back(CursorSlice(*inset_));
309 CursorSlice & tip = top();
310 //lyxerr << "XXX\n" << *this << endl;
312 // not at cell/paragraph end?
313 if (tip.pos() != tip.lastpos()) {
314 // move into an inset to the right if possible
317 n = (tip.cell().begin() + tip.pos())->nucleus();
319 n = paragraph().getInset(tip.pos());
320 if (n && n->isActive()) {
321 //lyxerr << "... descend" << endl;
322 push_back(CursorSlice(*n));
327 // jump to the next cell/paragraph if possible
333 // otherwise leave inset and jump over inset as a whole
335 // 'tip' is invalid now...
341 void DocIterator::forwardPosIgnoreCollapsed()
343 Inset * const nextinset = nextInset();
344 // FIXME: the check for asInsetMath() shouldn't be necessary
345 // but math insets do not return a sensible editable() state yet.
346 if (nextinset && !nextinset->asInsetMath()
347 && !nextinset->editable()) {
355 void DocIterator::forwardPar()
359 while (!empty() && (!inTexted() || pos() != 0)) {
361 pos_type const lastp = lastpos();
362 Paragraph const & par = paragraph();
363 pos_type & pos = top().pos();
364 if (par.insetList().empty())
367 while (pos < lastp && !par.isInset(pos))
375 void DocIterator::forwardChar()
378 while (!empty() && pos() == lastpos())
383 void DocIterator::forwardInset()
387 while (!empty() && !nextInset()) {
389 pos_type const lastp = lastpos();
390 Paragraph const & par = paragraph();
391 pos_type & pos = top().pos();
392 while (pos < lastp && !par.isInset(pos))
402 void DocIterator::backwardChar()
405 while (!empty() && pos() == lastpos())
410 void DocIterator::backwardPos()
412 //this dog bites his tail
414 push_back(CursorSlice(*inset_));
415 top().idx() = lastidx();
416 top().pit() = lastpit();
417 top().pos() = lastpos();
421 // at inset beginning?
422 if (top().at_begin()) {
429 // entered another cell/paragraph from the right?
430 if (top().pos() == top().lastpos())
433 // move into an inset to the left if possible
436 n = (top().cell().begin() + top().pos())->nucleus();
438 n = paragraph().getInset(top().pos());
439 if (n && n->isActive()) {
440 push_back(CursorSlice(*n));
441 top().idx() = lastidx();
442 top().pit() = lastpit();
443 top().pos() = lastpos();
448 bool DocIterator::hasPart(DocIterator const & it) const
450 // it can't be a part if it is larger
451 if (it.depth() > depth())
454 // as inset adresses are the 'last' level
455 return &it.top().inset() == &slices_[it.depth() - 1].inset();
459 void DocIterator::updateInsets(Inset * inset)
461 // this function re-creates the cache of inset pointers.
462 //lyxerr << "converting:\n" << *this << endl;
463 DocIterator dit = *this;
464 size_t const n = slices_.size();
466 for (size_t i = 0 ; i < n; ++i) {
467 LASSERT(inset, /**/);
469 top().inset_ = inset;
473 //lyxerr << "converted:\n" << *this << endl;
477 bool DocIterator::fixIfBroken()
482 // Go through the slice stack from the bottom.
483 // Check that all coordinates (idx, pit, pos) are correct and
484 // that the inset is the one which is claimed to be there
485 Inset * inset = &slices_[0].inset();
487 size_t n = slices_.size();
488 for (; i != n; ++i) {
489 CursorSlice & cs = slices_[i];
490 if (&cs.inset() != inset) {
491 // the whole slice is wrong, chop off this as well
493 LYXERR(Debug::DEBUG, "fixIfBroken(): inset changed");
495 } else if (cs.idx() > cs.lastidx()) {
496 cs.idx() = cs.lastidx();
497 cs.pit() = cs.lastpit();
498 cs.pos() = cs.lastpos();
499 LYXERR(Debug::DEBUG, "fixIfBroken(): idx fixed");
501 } else if (cs.pit() > cs.lastpit()) {
502 cs.pit() = cs.lastpit();
503 cs.pos() = cs.lastpos();
504 LYXERR(Debug::DEBUG, "fixIfBroken(): pit fixed");
506 } else if (cs.pos() > cs.lastpos()) {
507 cs.pos() = cs.lastpos();
508 LYXERR(Debug::DEBUG, "fixIfBroken(): pos fixed");
510 } else if (i != n - 1 && cs.pos() != cs.lastpos()) {
511 // get inset which is supposed to be in the next slice
512 if (cs.inset().inMathed())
513 inset = (cs.cell().begin() + cs.pos())->nucleus();
514 else if (Inset * csInset = cs.paragraph().getInset(cs.pos()))
517 // there are slices left, so there must be another inset
523 // Did we make it through the whole slice stack? Otherwise there
524 // was a problem at slice i, and we have to chop off above
526 LYXERR(Debug::DEBUG, "fixIfBroken(): cursor chopped at " << i);
534 void DocIterator::sanitize()
536 // keep a copy of the slices
537 vector<CursorSlice> const sl = slices_;
540 inset_ = &buffer_->inset();
541 Inset * inset = inset_;
542 // re-add the slices one by one, and adjust the inset pointer.
543 for (size_t i = 0, n = sl.size(); i != n; ++i) {
546 LYXERR0(" Should not happen, but does e.g. after "
547 "C-n C-l C-z S-C-z\n"
548 << " or when a Buffer has been concurrently edited by two views"
549 << '\n' << "dit: " << *this << '\n'
550 << " lastpos: " << slices_[i].lastpos());
555 top().inset_ = inset;
564 int DocIterator::find(MathData const & cell) const
566 for (size_t l = 0; l != slices_.size(); ++l) {
567 if (slices_[l].asInsetMath() && &slices_[l].cell() == &cell)
574 int DocIterator::find(Inset const * inset) const
576 for (size_t l = 0; l != slices_.size(); ++l) {
577 if (&slices_[l].inset() == inset)
584 void DocIterator::cutOff(int above, vector<CursorSlice> & cut)
586 cut = vector<CursorSlice>(slices_.begin() + above + 1, slices_.end());
587 slices_.resize(above + 1);
591 void DocIterator::cutOff(int above)
593 slices_.resize(above + 1);
597 void DocIterator::append(vector<CursorSlice> const & x)
599 slices_.insert(slices_.end(), x.begin(), x.end());
603 void DocIterator::append(DocIterator::idx_type idx, pos_type pos)
605 slices_.push_back(CursorSlice());
611 ostream & operator<<(ostream & os, DocIterator const & dit)
613 for (size_t i = 0, n = dit.depth(); i != n; ++i)
614 os << " " << dit[i] << "\n";
619 ///////////////////////////////////////////////////////
621 StableDocIterator::StableDocIterator(DocIterator const & dit)
623 data_ = dit.internalData();
624 for (size_t i = 0, n = data_.size(); i != n; ++i)
629 DocIterator StableDocIterator::asDocIterator(Buffer * buf) const
631 DocIterator dit(buf);
638 ostream & operator<<(ostream & os, StableDocIterator const & dit)
640 for (size_t i = 0, n = dit.data_.size(); i != n; ++i)
641 os << " " << dit.data_[i] << "\n";
646 bool operator==(StableDocIterator const & dit1, StableDocIterator const & dit2)
648 return dit1.data_ == dit2.data_;