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/ExceptionMessage.h"
31 #include "support/gettext.h"
32 #include "support/lassert.h"
33 #include "support/lstrings.h"
38 using namespace lyx::support;
43 DocIterator::DocIterator()
44 : boundary_(false), inset_(0), buffer_(0)
48 // We could be able to get rid of this if only every BufferView were
49 // associated to a buffer on construction.
50 DocIterator::DocIterator(Buffer * buf)
51 : boundary_(false), inset_(0), buffer_(buf)
55 DocIterator::DocIterator(Buffer * buf, Inset * inset)
56 : boundary_(false), inset_(inset), buffer_(buf)
60 DocIterator doc_iterator_begin(const Buffer * buf0, const Inset * inset0)
62 Buffer * buf = const_cast<Buffer *>(buf0);
63 Inset * inset = const_cast<Inset *>(inset0);
64 DocIterator dit(buf, inset ? inset : &buf->inset());
70 DocIterator doc_iterator_end(const Buffer * buf0, const Inset * inset0)
72 Buffer * buf = const_cast<Buffer *>(buf0);
73 Inset * inset = const_cast<Inset *>(inset0);
74 return DocIterator(buf, inset ? inset : &buf->inset());
78 DocIterator DocIterator::clone(Buffer * buffer) const
80 LASSERT(buffer->isClone(), return DocIterator());
81 Inset * inset = &buffer->inset();
82 DocIterator dit(buffer);
83 size_t const n = slices_.size();
84 for (size_t i = 0 ; i != n; ++i) {
86 dit.push_back(slices_[i]);
87 dit.top().inset_ = inset;
89 inset = dit.nextInset();
95 bool DocIterator::inRegexped() const
97 InsetMath * im = inset().asInsetMath();
100 InsetMathHull * hull = im->asHullInset();
101 return hull && hull->getType() == hullRegexp;
105 LyXErr & operator<<(LyXErr & os, DocIterator const & it)
112 Inset * DocIterator::nextInset() const
114 LASSERT(!empty(), return 0);
115 if (pos() == lastpos())
117 if (pos() > lastpos()) {
118 LYXERR0("Should not happen, but it does: pos() = "
119 << pos() << ", lastpos() = " << lastpos());
123 return nextAtom().nucleus();
124 return paragraph().getInset(pos());
128 Inset * DocIterator::prevInset() const
130 LASSERT(!empty(), return 0);
135 // FIXME: this should not happen but it does.
137 // http://www.lyx.org/trac/ticket/3189
140 return prevAtom().nucleus();
142 return paragraph().getInset(pos() - 1);
146 Inset * DocIterator::realInset() const
148 LASSERT(inTexted(), return 0);
149 // if we are in a tabular, we need the cell
150 if (inset().lyxCode() == TABULAR_CODE) {
151 InsetTabular * tabular = inset().asInsetTabular();
152 return tabular->cell(idx()).get();
158 MathAtom & DocIterator::prevAtom() const
160 LASSERT(!empty(), /**/);
161 LASSERT(pos() > 0, /**/);
162 return cell()[pos() - 1];
166 MathAtom & DocIterator::nextAtom() const
168 LASSERT(!empty(), /**/);
169 //lyxerr << "lastpos: " << lastpos() << " next atom:\n" << *this << endl;
170 LASSERT(pos() < lastpos(), /**/);
171 return cell()[pos()];
175 Text * DocIterator::text() const
177 LASSERT(!empty(), return 0);
182 Paragraph & DocIterator::paragraph() const
188 return top().paragraph();
192 Paragraph & DocIterator::innerParagraph() const
195 return innerTextSlice().paragraph();
199 FontSpan DocIterator::locateWord(word_location const loc) const
201 FontSpan f = FontSpan();
203 if (!top().text()->empty()) {
205 top().paragraph().locateWord(f.first, f.last, loc);
211 CursorSlice const & DocIterator::innerTextSlice() const
214 // go up until first non-0 text is hit
215 // (innermost text is 0 in mathed)
216 for (int i = depth() - 1; i >= 0; --i)
217 if (slices_[i].text())
220 // This case is in principle not possible. We _must_
224 static const CursorSlice c;
229 DocIterator DocIterator::getInnerText() const
231 DocIterator texted = *this;
232 while (!texted.inTexted())
238 pit_type DocIterator::lastpit() const
240 return inMathed() ? 0 : text()->paragraphs().size() - 1;
244 pos_type DocIterator::lastpos() const
246 return inMathed() ? cell().size() : paragraph().size();
250 DocIterator::idx_type DocIterator::lastidx() const
252 return top().lastidx();
256 size_t DocIterator::nargs() const
258 // assume 1x1 grid for main text
259 return top().nargs();
263 size_t DocIterator::ncols() const
265 // assume 1x1 grid for main text
266 return top().ncols();
270 size_t DocIterator::nrows() const
272 // assume 1x1 grid for main text
273 return top().nrows();
277 DocIterator::row_type DocIterator::row() const
283 DocIterator::col_type DocIterator::col() const
289 MathData & DocIterator::cell() const
291 // LASSERT(inMathed(), /**/);
296 Text * DocIterator::innerText() const
298 LASSERT(!empty(), return 0);
299 return innerTextSlice().text();
303 Inset * DocIterator::innerInsetOfType(int code) const
305 for (int i = depth() - 1; i >= 0; --i)
306 if (slices_[i].inset_->lyxCode() == code)
307 return slices_[i].inset_;
312 // This duplicates code above, but is in the critical path.
313 // So please think twice before adding stuff
314 void DocIterator::forwardPos()
316 // this dog bites his tail
318 push_back(CursorSlice(*inset_));
322 CursorSlice & tip = top();
323 //lyxerr << "XXX\n" << *this << endl;
325 // not at cell/paragraph end?
326 if (tip.pos() != tip.lastpos()) {
327 // move into an inset to the right if possible
330 n = (tip.cell().begin() + tip.pos())->nucleus();
332 n = paragraph().getInset(tip.pos());
333 if (n && n->isActive()) {
334 //lyxerr << "... descend" << endl;
335 push_back(CursorSlice(*n));
340 // jump to the next cell/paragraph if possible
346 // otherwise leave inset and jump over inset as a whole
348 // 'tip' is invalid now...
354 void DocIterator::forwardPosIgnoreCollapsed()
356 Inset * const nextinset = nextInset();
357 // FIXME: the check for asInsetMath() shouldn't be necessary
358 // but math insets do not return a sensible editable() state yet.
359 if (nextinset && !nextinset->asInsetMath()
360 && !nextinset->editable()) {
368 void DocIterator::forwardPar()
372 while (!empty() && (!inTexted() || pos() != 0)) {
374 pos_type const lastp = lastpos();
375 Paragraph const & par = paragraph();
376 pos_type & pos = top().pos();
377 if (par.insetList().empty())
380 while (pos < lastp && !par.isInset(pos))
388 void DocIterator::forwardChar()
391 while (!empty() && pos() == lastpos())
396 void DocIterator::forwardInset()
400 while (!empty() && !nextInset()) {
402 pos_type const lastp = lastpos();
403 Paragraph const & par = paragraph();
404 pos_type & pos = top().pos();
405 while (pos < lastp && !par.isInset(pos))
415 void DocIterator::backwardChar()
418 while (!empty() && pos() == lastpos())
423 void DocIterator::backwardPos()
425 //this dog bites his tail
427 push_back(CursorSlice(*inset_));
428 top().idx() = lastidx();
429 top().pit() = lastpit();
430 top().pos() = lastpos();
434 // at inset beginning?
435 if (top().at_begin()) {
442 // entered another cell/paragraph from the right?
443 if (top().pos() == top().lastpos())
446 // move into an inset to the left if possible
449 n = (top().cell().begin() + top().pos())->nucleus();
451 n = paragraph().getInset(top().pos());
452 if (n && n->isActive()) {
453 push_back(CursorSlice(*n));
454 top().idx() = lastidx();
455 top().pit() = lastpit();
456 top().pos() = lastpos();
462 // works, but currently not needed
463 void DocIterator::backwardInset()
467 while (!empty() && !nextInset()) {
469 pos_type const lastp = lastpos();
470 Paragraph const & par = paragraph();
471 pos_type & pos = top().pos();
472 while (pos > 0 && (pos == lastp || !par.isInset(pos)))
483 bool DocIterator::hasPart(DocIterator const & it) const
485 // it can't be a part if it is larger
486 if (it.depth() > depth())
489 // as inset adresses are the 'last' level
490 return &it.top().inset() == &slices_[it.depth() - 1].inset();
494 bool DocIterator::allowSpellCheck() const
496 /// spell check is disabled if the iterator position
497 /// is inside of an inset which disables the spell checker
498 size_t const n = depth();
499 for (size_t i = 0; i < n; ++i) {
500 if (!slices_[i].inset_->allowSpellCheck())
507 void DocIterator::updateInsets(Inset * inset)
509 // this function re-creates the cache of inset pointers.
510 //lyxerr << "converting:\n" << *this << endl;
511 DocIterator dit = *this;
512 size_t const n = slices_.size();
514 for (size_t i = 0 ; i < n; ++i) {
517 top().inset_ = inset;
521 //lyxerr << "converted:\n" << *this << endl;
525 bool DocIterator::fixIfBroken()
530 // Go through the slice stack from the bottom.
531 // Check that all coordinates (idx, pit, pos) are correct and
532 // that the inset is the one which is claimed to be there
533 Inset * inset = &slices_[0].inset();
535 size_t n = slices_.size();
536 for (; i != n; ++i) {
537 CursorSlice & cs = slices_[i];
538 if (&cs.inset() != inset) {
539 // the whole slice is wrong, chop off this as well
541 LYXERR(Debug::DEBUG, "fixIfBroken(): inset changed");
543 } else if (cs.idx() > cs.lastidx()) {
544 cs.idx() = cs.lastidx();
545 cs.pit() = cs.lastpit();
546 cs.pos() = cs.lastpos();
547 LYXERR(Debug::DEBUG, "fixIfBroken(): idx fixed");
549 } else if (cs.pit() > cs.lastpit()) {
550 cs.pit() = cs.lastpit();
551 cs.pos() = cs.lastpos();
552 LYXERR(Debug::DEBUG, "fixIfBroken(): pit fixed");
554 } else if (cs.pos() > cs.lastpos()) {
555 cs.pos() = cs.lastpos();
556 LYXERR(Debug::DEBUG, "fixIfBroken(): pos fixed");
558 } else if (i != n - 1 && cs.pos() != cs.lastpos()) {
559 // get inset which is supposed to be in the next slice
560 if (cs.inset().inMathed())
561 inset = (cs.cell().begin() + cs.pos())->nucleus();
562 else if (Inset * csInset = cs.paragraph().getInset(cs.pos()))
565 // there are slices left, so there must be another inset
571 // Did we make it through the whole slice stack? Otherwise there
572 // was a problem at slice i, and we have to chop off above
574 LYXERR(Debug::DEBUG, "fixIfBroken(): cursor chopped at " << i);
582 void DocIterator::sanitize()
584 // keep a copy of the slices
585 vector<CursorSlice> const sl = slices_;
588 inset_ = &buffer_->inset();
589 Inset * inset = inset_;
590 // re-add the slices one by one, and adjust the inset pointer.
591 for (size_t i = 0, n = sl.size(); i != n; ++i) {
594 LYXERR0(" Should not happen, but does e.g. after "
595 "C-n C-l C-z S-C-z\n"
596 << " or when a Buffer has been concurrently edited by two views"
597 << '\n' << "dit: " << *this << '\n'
598 << " lastpos: " << slices_[i].lastpos());
602 if (!inset->isActive()) {
603 LYXERR0("Inset found on cursor stack is not active.");
608 top().inset_ = inset;
617 int DocIterator::find(MathData const & cell) const
619 for (size_t l = 0; l != slices_.size(); ++l) {
620 if (slices_[l].asInsetMath() && &slices_[l].cell() == &cell)
627 int DocIterator::find(Inset const * inset) const
629 for (size_t l = 0; l != slices_.size(); ++l) {
630 if (&slices_[l].inset() == inset)
637 void DocIterator::cutOff(int above, vector<CursorSlice> & cut)
639 cut = vector<CursorSlice>(slices_.begin() + above + 1, slices_.end());
640 slices_.resize(above + 1);
644 void DocIterator::cutOff(int above)
646 slices_.resize(above + 1);
650 void DocIterator::append(vector<CursorSlice> const & x)
652 slices_.insert(slices_.end(), x.begin(), x.end());
656 void DocIterator::append(DocIterator::idx_type idx, pos_type pos)
658 slices_.push_back(CursorSlice());
664 ostream & operator<<(ostream & os, DocIterator const & dit)
666 for (size_t i = 0, n = dit.depth(); i != n; ++i)
667 os << " " << dit[i] << "\n";
672 ///////////////////////////////////////////////////////
674 StableDocIterator::StableDocIterator(DocIterator const & dit) :
675 data_(dit.internalData())
677 for (size_t i = 0, n = data_.size(); i != n; ++i)
682 DocIterator StableDocIterator::asDocIterator(Buffer * buf) const
684 DocIterator dit(buf);
691 ostream & operator<<(ostream & os, StableDocIterator const & dit)
693 for (size_t i = 0, n = dit.data_.size(); i != n; ++i)
694 os << " " << dit.data_[i] << "\n";
699 bool operator==(StableDocIterator const & dit1, StableDocIterator const & dit2)
701 return dit1.data_ == dit2.data_;