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 bool DocIterator::posBackward()
330 bool DocIterator::posForward()
332 if (pos() == lastpos())
339 // This duplicates code above, but is in the critical path.
340 // So please think twice before adding stuff
341 void DocIterator::forwardPos()
343 // this dog bites his tail
345 push_back(CursorSlice(*inset_));
349 CursorSlice & tip = top();
350 //lyxerr << "XXX\n" << *this << endl;
352 // not at cell/paragraph end?
353 if (tip.pos() != tip.lastpos()) {
354 // move into an inset to the right if possible
357 n = (tip.cell().begin() + tip.pos())->nucleus();
359 n = paragraph().getInset(tip.pos());
360 if (n && n->isActive()) {
361 //lyxerr << "... descend" << endl;
362 push_back(CursorSlice(*n));
367 // jump to the next cell/paragraph if possible
373 // otherwise leave inset and jump over inset as a whole
375 // 'tip' is invalid now...
381 void DocIterator::forwardPosIgnoreCollapsed()
383 Inset * const nextinset = nextInset();
384 // FIXME: the check for asInsetMath() shouldn't be necessary
385 // but math insets do not return a sensible editable() state yet.
386 if (nextinset && !nextinset->asInsetMath()
387 && !nextinset->editable()) {
395 void DocIterator::forwardPar()
399 while (!empty() && (!inTexted() || pos() != 0)) {
401 pos_type const lastp = lastpos();
402 Paragraph const & par = paragraph();
403 pos_type & pos = top().pos();
404 if (par.insetList().empty())
407 while (pos < lastp && !par.isInset(pos))
415 void DocIterator::forwardChar()
418 while (!empty() && pos() == lastpos())
423 void DocIterator::forwardInset()
427 while (!empty() && !nextInset()) {
429 pos_type const lastp = lastpos();
430 Paragraph const & par = paragraph();
431 pos_type & pos = top().pos();
432 while (pos < lastp && !par.isInset(pos))
442 void DocIterator::backwardChar()
445 while (!empty() && pos() == lastpos())
450 void DocIterator::backwardPos()
452 //this dog bites his tail
454 push_back(CursorSlice(*inset_));
455 top().idx() = lastidx();
456 top().pit() = lastpit();
457 top().pos() = lastpos();
461 // at inset beginning?
462 if (top().at_begin()) {
469 // entered another cell/paragraph from the right?
470 if (top().pos() == top().lastpos())
473 // move into an inset to the left if possible
476 n = (top().cell().begin() + top().pos())->nucleus();
478 n = paragraph().getInset(top().pos());
479 if (n && n->isActive()) {
480 push_back(CursorSlice(*n));
481 top().idx() = lastidx();
482 top().pit() = lastpit();
483 top().pos() = lastpos();
489 // works, but currently not needed
490 void DocIterator::backwardInset()
494 while (!empty() && !nextInset()) {
496 pos_type const lastp = lastpos();
497 Paragraph const & par = paragraph();
498 pos_type & pos = top().pos();
499 while (pos > 0 && (pos == lastp || !par.isInset(pos)))
510 bool DocIterator::hasPart(DocIterator const & it) const
512 // it can't be a part if it is larger
513 if (it.depth() > depth())
516 // as inset adresses are the 'last' level
517 return &it.top().inset() == &slices_[it.depth() - 1].inset();
521 bool DocIterator::allowSpellCheck() const
523 /// spell check is disabled if the iterator position
524 /// is inside of an inset which disables the spell checker
525 size_t const n = depth();
526 for (size_t i = 0; i < n; ++i) {
527 if (!slices_[i].inset_->allowSpellCheck())
534 void DocIterator::updateInsets(Inset * inset)
536 // this function re-creates the cache of inset pointers.
537 //lyxerr << "converting:\n" << *this << endl;
538 DocIterator dit = *this;
539 size_t const n = slices_.size();
541 for (size_t i = 0 ; i < n; ++i) {
544 top().inset_ = inset;
548 //lyxerr << "converted:\n" << *this << endl;
552 bool DocIterator::fixIfBroken()
557 // Go through the slice stack from the bottom.
558 // Check that all coordinates (idx, pit, pos) are correct and
559 // that the inset is the one which is claimed to be there
560 Inset * inset = &slices_[0].inset();
562 size_t n = slices_.size();
563 for (; i != n; ++i) {
564 CursorSlice & cs = slices_[i];
565 if (&cs.inset() != inset || ! cs.inset().isActive()) {
566 // the whole slice is wrong, chop off this as well
568 LYXERR(Debug::DEBUG, "fixIfBroken(): inset changed");
570 } else if (cs.idx() > cs.lastidx()) {
571 cs.idx() = cs.lastidx();
572 cs.pit() = cs.lastpit();
573 cs.pos() = cs.lastpos();
574 LYXERR(Debug::DEBUG, "fixIfBroken(): idx fixed");
576 } else if (cs.pit() > cs.lastpit()) {
577 cs.pit() = cs.lastpit();
578 cs.pos() = cs.lastpos();
579 LYXERR(Debug::DEBUG, "fixIfBroken(): pit fixed");
581 } else if (cs.pos() > cs.lastpos()) {
582 cs.pos() = cs.lastpos();
583 LYXERR(Debug::DEBUG, "fixIfBroken(): pos fixed");
585 } else if (i != n - 1 && cs.pos() != cs.lastpos()) {
586 // get inset which is supposed to be in the next slice
587 if (cs.inset().inMathed())
588 inset = (cs.cell().begin() + cs.pos())->nucleus();
589 else if (Inset * csInset = cs.paragraph().getInset(cs.pos()))
592 // there are slices left, so there must be another inset
598 // Did we make it through the whole slice stack? Otherwise there
599 // was a problem at slice i, and we have to chop off above
601 LYXERR(Debug::DEBUG, "fixIfBroken(): cursor chopped at " << i);
609 void DocIterator::sanitize()
611 // keep a copy of the slices
612 vector<CursorSlice> const sl = slices_;
615 inset_ = &buffer_->inset();
616 Inset * inset = inset_;
617 // re-add the slices one by one, and adjust the inset pointer.
618 for (size_t i = 0, n = sl.size(); i != n; ++i) {
621 LYXERR0(" Should not happen, but does e.g. after "
622 "C-n C-l C-z S-C-z\n"
623 << " or when a Buffer has been concurrently edited by two views"
624 << '\n' << "dit: " << *this << '\n'
625 << " lastpos: " << slices_[i].lastpos());
629 if (!inset->isActive()) {
630 LYXERR0("Inset found on cursor stack is not active.");
635 top().inset_ = inset;
644 int DocIterator::find(MathData const & cell) const
646 for (size_t l = 0; l != slices_.size(); ++l) {
647 if (slices_[l].asInsetMath() && &slices_[l].cell() == &cell)
654 int DocIterator::find(Inset const * inset) const
656 for (size_t l = 0; l != slices_.size(); ++l) {
657 if (&slices_[l].inset() == inset)
664 void DocIterator::cutOff(int above, vector<CursorSlice> & cut)
666 cut = vector<CursorSlice>(slices_.begin() + above + 1, slices_.end());
667 slices_.resize(above + 1);
671 void DocIterator::cutOff(int above)
673 slices_.resize(above + 1);
677 void DocIterator::append(vector<CursorSlice> const & x)
679 slices_.insert(slices_.end(), x.begin(), x.end());
683 void DocIterator::append(DocIterator::idx_type idx, pos_type pos)
685 slices_.push_back(CursorSlice());
691 ostream & operator<<(ostream & os, DocIterator const & dit)
693 for (size_t i = 0, n = dit.depth(); i != n; ++i)
694 os << " " << dit[i] << "\n";
699 ///////////////////////////////////////////////////////
701 StableDocIterator::StableDocIterator(DocIterator const & dit) :
702 data_(dit.internalData())
704 for (size_t i = 0, n = data_.size(); i != n; ++i)
709 DocIterator StableDocIterator::asDocIterator(Buffer * buf) const
711 DocIterator dit(buf);
718 ostream & operator<<(ostream & os, StableDocIterator const & dit)
720 for (size_t i = 0, n = dit.data_.size(); i != n; ++i)
721 os << " " << dit.data_[i] << "\n";
726 bool operator==(StableDocIterator const & dit1, StableDocIterator const & dit2)
728 return dit1.data_ == dit2.data_;