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/convert.h"
30 #include "support/debug.h"
31 #include "support/ExceptionMessage.h"
32 #include "support/gettext.h"
33 #include "support/lassert.h"
34 #include "support/lstrings.h"
39 using namespace lyx::support;
44 DocIterator::DocIterator()
45 : boundary_(false), inset_(0), buffer_(0)
49 // We could be able to get rid of this if only every BufferView were
50 // associated to a buffer on construction.
51 DocIterator::DocIterator(Buffer * buf)
52 : boundary_(false), inset_(0), buffer_(buf)
56 DocIterator::DocIterator(Buffer * buf, Inset * inset)
57 : boundary_(false), inset_(inset), buffer_(buf)
61 DocIterator doc_iterator_begin(const Buffer * buf0, const Inset * inset0)
63 Buffer * buf = const_cast<Buffer *>(buf0);
64 Inset * inset = const_cast<Inset *>(inset0);
65 DocIterator dit(buf, inset ? inset : &buf->inset());
71 DocIterator doc_iterator_end(const Buffer * buf0, const Inset * inset0)
73 Buffer * buf = const_cast<Buffer *>(buf0);
74 Inset * inset = const_cast<Inset *>(inset0);
75 return DocIterator(buf, inset ? inset : &buf->inset());
79 DocIterator DocIterator::clone(Buffer * buffer) const
81 LASSERT(buffer->isClone(), return DocIterator());
82 Inset * inset = &buffer->inset();
83 DocIterator dit(buffer);
84 size_t const n = slices_.size();
85 for (size_t i = 0 ; i != n; ++i) {
87 dit.push_back(slices_[i]);
88 dit.top().inset_ = inset;
90 inset = dit.nextInset();
96 bool DocIterator::inRegexped() const
98 InsetMath * im = inset().asInsetMath();
101 InsetMathHull * hull = im->asHullInset();
102 return hull && hull->getType() == hullRegexp;
106 LyXErr & operator<<(LyXErr & os, DocIterator const & it)
113 Inset * DocIterator::nextInset() const
115 LASSERT(!empty(), return 0);
116 if (pos() == lastpos())
118 if (pos() > lastpos()) {
119 LYXERR0("Should not happen, but it does: pos() = "
120 << pos() << ", lastpos() = " << lastpos());
124 return nextAtom().nucleus();
125 return paragraph().getInset(pos());
129 Inset * DocIterator::prevInset() const
131 LASSERT(!empty(), return 0);
136 // FIXME: this should not happen but it does.
138 // http://www.lyx.org/trac/ticket/3189
141 return prevAtom().nucleus();
143 return paragraph().getInset(pos() - 1);
147 Inset * DocIterator::realInset() const
149 LASSERT(inTexted(), return 0);
150 // if we are in a tabular, we need the cell
151 if (inset().lyxCode() == TABULAR_CODE) {
152 InsetTabular * tabular = inset().asInsetTabular();
153 return tabular->cell(idx()).get();
159 MathAtom & DocIterator::prevAtom() const
161 LASSERT(!empty(), /**/);
162 LASSERT(pos() > 0, /**/);
163 return cell()[pos() - 1];
167 MathAtom & DocIterator::nextAtom() const
169 LASSERT(!empty(), /**/);
170 //lyxerr << "lastpos: " << lastpos() << " next atom:\n" << *this << endl;
171 LASSERT(pos() < lastpos(), /**/);
172 return cell()[pos()];
176 Text * DocIterator::text() const
178 LASSERT(!empty(), return 0);
183 Paragraph & DocIterator::paragraph() const
189 return top().paragraph();
193 Paragraph & DocIterator::innerParagraph() const
196 return innerTextSlice().paragraph();
200 FontSpan DocIterator::locateWord(word_location const loc) const
202 FontSpan f = FontSpan();
204 if (!top().text()->empty()) {
206 top().paragraph().locateWord(f.first, f.last, loc);
212 CursorSlice const & DocIterator::innerTextSlice() const
215 // go up until first non-0 text is hit
216 // (innermost text is 0 in mathed)
217 for (int i = depth() - 1; i >= 0; --i)
218 if (slices_[i].text())
221 // This case is in principle not possible. We _must_
225 static const CursorSlice c;
230 docstring DocIterator::paragraphGotoArgument() const
232 CursorSlice const & s = innerTextSlice();
233 return convert<docstring>(s.paragraph().id()) + ' ' +
234 convert<docstring>(s.pos());
238 DocIterator DocIterator::getInnerText() const
240 DocIterator texted = *this;
241 while (!texted.inTexted())
247 pit_type DocIterator::lastpit() const
249 return inMathed() ? 0 : text()->paragraphs().size() - 1;
253 pos_type DocIterator::lastpos() const
255 return inMathed() ? cell().size() : paragraph().size();
259 DocIterator::idx_type DocIterator::lastidx() const
261 return top().lastidx();
265 size_t DocIterator::nargs() const
267 // assume 1x1 grid for main text
268 return top().nargs();
272 size_t DocIterator::ncols() const
274 // assume 1x1 grid for main text
275 return top().ncols();
279 size_t DocIterator::nrows() const
281 // assume 1x1 grid for main text
282 return top().nrows();
286 DocIterator::row_type DocIterator::row() const
292 DocIterator::col_type DocIterator::col() const
298 MathData & DocIterator::cell() const
300 // LASSERT(inMathed(), /**/);
305 Text * DocIterator::innerText() const
307 LASSERT(!empty(), return 0);
308 return innerTextSlice().text();
312 Inset * DocIterator::innerInsetOfType(int code) const
314 for (int i = depth() - 1; i >= 0; --i)
315 if (slices_[i].inset_->lyxCode() == code)
316 return slices_[i].inset_;
321 // This duplicates code above, but is in the critical path.
322 // So please think twice before adding stuff
323 void DocIterator::forwardPos()
325 // this dog bites his tail
327 push_back(CursorSlice(*inset_));
331 CursorSlice & tip = top();
332 //lyxerr << "XXX\n" << *this << endl;
334 // not at cell/paragraph end?
335 if (tip.pos() != tip.lastpos()) {
336 // move into an inset to the right if possible
339 n = (tip.cell().begin() + tip.pos())->nucleus();
341 n = paragraph().getInset(tip.pos());
342 if (n && n->isActive()) {
343 //lyxerr << "... descend" << endl;
344 push_back(CursorSlice(*n));
349 // jump to the next cell/paragraph if possible
355 // otherwise leave inset and jump over inset as a whole
357 // 'tip' is invalid now...
363 void DocIterator::forwardPosIgnoreCollapsed()
365 Inset * const nextinset = nextInset();
366 // FIXME: the check for asInsetMath() shouldn't be necessary
367 // but math insets do not return a sensible editable() state yet.
368 if (nextinset && !nextinset->asInsetMath()
369 && !nextinset->editable()) {
377 void DocIterator::forwardPar()
381 while (!empty() && (!inTexted() || pos() != 0)) {
383 pos_type const lastp = lastpos();
384 Paragraph const & par = paragraph();
385 pos_type & pos = top().pos();
386 if (par.insetList().empty())
389 while (pos < lastp && !par.isInset(pos))
397 void DocIterator::forwardChar()
400 while (!empty() && pos() == lastpos())
405 void DocIterator::forwardInset()
409 while (!empty() && !nextInset()) {
411 pos_type const lastp = lastpos();
412 Paragraph const & par = paragraph();
413 pos_type & pos = top().pos();
414 while (pos < lastp && !par.isInset(pos))
424 void DocIterator::backwardChar()
427 while (!empty() && pos() == lastpos())
432 void DocIterator::backwardPos()
434 //this dog bites his tail
436 push_back(CursorSlice(*inset_));
437 top().idx() = lastidx();
438 top().pit() = lastpit();
439 top().pos() = lastpos();
443 // at inset beginning?
444 if (top().at_begin()) {
451 // entered another cell/paragraph from the right?
452 if (top().pos() == top().lastpos())
455 // move into an inset to the left if possible
458 n = (top().cell().begin() + top().pos())->nucleus();
460 n = paragraph().getInset(top().pos());
461 if (n && n->isActive()) {
462 push_back(CursorSlice(*n));
463 top().idx() = lastidx();
464 top().pit() = lastpit();
465 top().pos() = lastpos();
471 // works, but currently not needed
472 void DocIterator::backwardInset()
476 while (!empty() && !nextInset()) {
478 pos_type const lastp = lastpos();
479 Paragraph const & par = paragraph();
480 pos_type & pos = top().pos();
481 while (pos > 0 && (pos == lastp || !par.isInset(pos)))
492 bool DocIterator::hasPart(DocIterator const & it) const
494 // it can't be a part if it is larger
495 if (it.depth() > depth())
498 // as inset adresses are the 'last' level
499 return &it.top().inset() == &slices_[it.depth() - 1].inset();
503 bool DocIterator::allowSpellCheck() const
505 /// spell check is disabled if the iterator position
506 /// is inside of an inset which disables the spell checker
507 size_t const n = depth();
508 for (size_t i = 0; i < n; ++i) {
509 if (!slices_[i].inset_->allowSpellCheck())
516 void DocIterator::updateInsets(Inset * inset)
518 // this function re-creates the cache of inset pointers.
519 //lyxerr << "converting:\n" << *this << endl;
520 DocIterator dit = *this;
521 size_t const n = slices_.size();
523 for (size_t i = 0 ; i < n; ++i) {
526 top().inset_ = inset;
530 //lyxerr << "converted:\n" << *this << endl;
534 bool DocIterator::fixIfBroken()
539 // Go through the slice stack from the bottom.
540 // Check that all coordinates (idx, pit, pos) are correct and
541 // that the inset is the one which is claimed to be there
542 Inset * inset = &slices_[0].inset();
544 size_t n = slices_.size();
545 for (; i != n; ++i) {
546 CursorSlice & cs = slices_[i];
547 if (&cs.inset() != inset) {
548 // the whole slice is wrong, chop off this as well
550 LYXERR(Debug::DEBUG, "fixIfBroken(): inset changed");
552 } else if (cs.idx() > cs.lastidx()) {
553 cs.idx() = cs.lastidx();
554 cs.pit() = cs.lastpit();
555 cs.pos() = cs.lastpos();
556 LYXERR(Debug::DEBUG, "fixIfBroken(): idx fixed");
558 } else if (cs.pit() > cs.lastpit()) {
559 cs.pit() = cs.lastpit();
560 cs.pos() = cs.lastpos();
561 LYXERR(Debug::DEBUG, "fixIfBroken(): pit fixed");
563 } else if (cs.pos() > cs.lastpos()) {
564 cs.pos() = cs.lastpos();
565 LYXERR(Debug::DEBUG, "fixIfBroken(): pos fixed");
567 } else if (i != n - 1 && cs.pos() != cs.lastpos()) {
568 // get inset which is supposed to be in the next slice
569 if (cs.inset().inMathed())
570 inset = (cs.cell().begin() + cs.pos())->nucleus();
571 else if (Inset * csInset = cs.paragraph().getInset(cs.pos()))
574 // there are slices left, so there must be another inset
580 // Did we make it through the whole slice stack? Otherwise there
581 // was a problem at slice i, and we have to chop off above
583 LYXERR(Debug::DEBUG, "fixIfBroken(): cursor chopped at " << i);
591 void DocIterator::sanitize()
593 // keep a copy of the slices
594 vector<CursorSlice> const sl = slices_;
597 inset_ = &buffer_->inset();
598 Inset * inset = inset_;
599 // re-add the slices one by one, and adjust the inset pointer.
600 for (size_t i = 0, n = sl.size(); i != n; ++i) {
603 LYXERR0(" Should not happen, but does e.g. after "
604 "C-n C-l C-z S-C-z\n"
605 << " or when a Buffer has been concurrently edited by two views"
606 << '\n' << "dit: " << *this << '\n'
607 << " lastpos: " << slices_[i].lastpos());
611 if (!inset->isActive()) {
612 LYXERR0("Inset found on cursor stack is not active.");
617 top().inset_ = inset;
626 int DocIterator::find(MathData const & cell) const
628 for (size_t l = 0; l != slices_.size(); ++l) {
629 if (slices_[l].asInsetMath() && &slices_[l].cell() == &cell)
636 int DocIterator::find(Inset const * inset) const
638 for (size_t l = 0; l != slices_.size(); ++l) {
639 if (&slices_[l].inset() == inset)
646 void DocIterator::cutOff(int above, vector<CursorSlice> & cut)
648 cut = vector<CursorSlice>(slices_.begin() + above + 1, slices_.end());
649 slices_.resize(above + 1);
653 void DocIterator::cutOff(int above)
655 slices_.resize(above + 1);
659 void DocIterator::append(vector<CursorSlice> const & x)
661 slices_.insert(slices_.end(), x.begin(), x.end());
665 void DocIterator::append(DocIterator::idx_type idx, pos_type pos)
667 slices_.push_back(CursorSlice());
673 ostream & operator<<(ostream & os, DocIterator const & dit)
675 for (size_t i = 0, n = dit.depth(); i != n; ++i)
676 os << " " << dit[i] << "\n";
681 ///////////////////////////////////////////////////////
683 StableDocIterator::StableDocIterator(DocIterator const & dit) :
684 data_(dit.internalData())
686 for (size_t i = 0, n = data_.size(); i != n; ++i)
691 DocIterator StableDocIterator::asDocIterator(Buffer * buf) const
693 DocIterator dit(buf);
700 ostream & operator<<(ostream & os, StableDocIterator const & dit)
702 for (size_t i = 0, n = dit.data_.size(); i != n; ++i)
703 os << " " << dit.data_[i] << "\n";
708 bool operator==(StableDocIterator const & dit1, StableDocIterator const & dit2)
710 return dit1.data_ == dit2.data_;