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 pit_type DocIterator::lastpit() const
231 return inMathed() ? 0 : text()->paragraphs().size() - 1;
235 pos_type DocIterator::lastpos() const
237 return inMathed() ? cell().size() : paragraph().size();
241 DocIterator::idx_type DocIterator::lastidx() const
243 return top().lastidx();
247 size_t DocIterator::nargs() const
249 // assume 1x1 grid for main text
250 return top().nargs();
254 size_t DocIterator::ncols() const
256 // assume 1x1 grid for main text
257 return top().ncols();
261 size_t DocIterator::nrows() const
263 // assume 1x1 grid for main text
264 return top().nrows();
268 DocIterator::row_type DocIterator::row() const
274 DocIterator::col_type DocIterator::col() const
280 MathData & DocIterator::cell() const
282 // LASSERT(inMathed(), /**/);
287 Text * DocIterator::innerText() const
289 LASSERT(!empty(), return 0);
290 return innerTextSlice().text();
294 Inset * DocIterator::innerInsetOfType(int code) const
296 for (int i = depth() - 1; i >= 0; --i)
297 if (slices_[i].inset_->lyxCode() == code)
298 return slices_[i].inset_;
303 // This duplicates code above, but is in the critical path.
304 // So please think twice before adding stuff
305 void DocIterator::forwardPos()
307 // this dog bites his tail
309 push_back(CursorSlice(*inset_));
313 CursorSlice & tip = top();
314 //lyxerr << "XXX\n" << *this << endl;
316 // not at cell/paragraph end?
317 if (tip.pos() != tip.lastpos()) {
318 // move into an inset to the right if possible
321 n = (tip.cell().begin() + tip.pos())->nucleus();
323 n = paragraph().getInset(tip.pos());
324 if (n && n->isActive()) {
325 //lyxerr << "... descend" << endl;
326 push_back(CursorSlice(*n));
331 // jump to the next cell/paragraph if possible
337 // otherwise leave inset and jump over inset as a whole
339 // 'tip' is invalid now...
345 void DocIterator::forwardPosIgnoreCollapsed()
347 Inset * const nextinset = nextInset();
348 // FIXME: the check for asInsetMath() shouldn't be necessary
349 // but math insets do not return a sensible editable() state yet.
350 if (nextinset && !nextinset->asInsetMath()
351 && !nextinset->editable()) {
359 void DocIterator::forwardPar()
363 while (!empty() && (!inTexted() || pos() != 0)) {
365 pos_type const lastp = lastpos();
366 Paragraph const & par = paragraph();
367 pos_type & pos = top().pos();
368 if (par.insetList().empty())
371 while (pos < lastp && !par.isInset(pos))
379 void DocIterator::forwardChar()
382 while (!empty() && pos() == lastpos())
387 void DocIterator::forwardInset()
391 while (!empty() && !nextInset()) {
393 pos_type const lastp = lastpos();
394 Paragraph const & par = paragraph();
395 pos_type & pos = top().pos();
396 while (pos < lastp && !par.isInset(pos))
406 void DocIterator::backwardChar()
409 while (!empty() && pos() == lastpos())
414 void DocIterator::backwardPos()
416 //this dog bites his tail
418 push_back(CursorSlice(*inset_));
419 top().idx() = lastidx();
420 top().pit() = lastpit();
421 top().pos() = lastpos();
425 // at inset beginning?
426 if (top().at_begin()) {
433 // entered another cell/paragraph from the right?
434 if (top().pos() == top().lastpos())
437 // move into an inset to the left if possible
440 n = (top().cell().begin() + top().pos())->nucleus();
442 n = paragraph().getInset(top().pos());
443 if (n && n->isActive()) {
444 push_back(CursorSlice(*n));
445 top().idx() = lastidx();
446 top().pit() = lastpit();
447 top().pos() = lastpos();
453 // works, but currently not needed
454 void DocIterator::backwardInset()
458 while (!empty() && !nextInset()) {
460 pos_type const lastp = lastpos();
461 Paragraph const & par = paragraph();
462 pos_type & pos = top().pos();
463 while (pos > 0 && (pos == lastp || !par.isInset(pos)))
474 bool DocIterator::hasPart(DocIterator const & it) const
476 // it can't be a part if it is larger
477 if (it.depth() > depth())
480 // as inset adresses are the 'last' level
481 return &it.top().inset() == &slices_[it.depth() - 1].inset();
485 bool DocIterator::allowSpellCheck() const
487 /// spell check is disabled if the iterator position
488 /// is inside of an inset which disables the spell checker
489 size_t const n = depth();
490 for (size_t i = 0; i < n; ++i) {
491 if (!slices_[i].inset_->allowSpellCheck())
498 void DocIterator::updateInsets(Inset * inset)
500 // this function re-creates the cache of inset pointers.
501 //lyxerr << "converting:\n" << *this << endl;
502 DocIterator dit = *this;
503 size_t const n = slices_.size();
505 for (size_t i = 0 ; i < n; ++i) {
508 top().inset_ = inset;
512 //lyxerr << "converted:\n" << *this << endl;
516 bool DocIterator::fixIfBroken()
521 // Go through the slice stack from the bottom.
522 // Check that all coordinates (idx, pit, pos) are correct and
523 // that the inset is the one which is claimed to be there
524 Inset * inset = &slices_[0].inset();
526 size_t n = slices_.size();
527 for (; i != n; ++i) {
528 CursorSlice & cs = slices_[i];
529 if (&cs.inset() != inset) {
530 // the whole slice is wrong, chop off this as well
532 LYXERR(Debug::DEBUG, "fixIfBroken(): inset changed");
534 } else if (cs.idx() > cs.lastidx()) {
535 cs.idx() = cs.lastidx();
536 cs.pit() = cs.lastpit();
537 cs.pos() = cs.lastpos();
538 LYXERR(Debug::DEBUG, "fixIfBroken(): idx fixed");
540 } else if (cs.pit() > cs.lastpit()) {
541 cs.pit() = cs.lastpit();
542 cs.pos() = cs.lastpos();
543 LYXERR(Debug::DEBUG, "fixIfBroken(): pit fixed");
545 } else if (cs.pos() > cs.lastpos()) {
546 cs.pos() = cs.lastpos();
547 LYXERR(Debug::DEBUG, "fixIfBroken(): pos fixed");
549 } else if (i != n - 1 && cs.pos() != cs.lastpos()) {
550 // get inset which is supposed to be in the next slice
551 if (cs.inset().inMathed())
552 inset = (cs.cell().begin() + cs.pos())->nucleus();
553 else if (Inset * csInset = cs.paragraph().getInset(cs.pos()))
556 // there are slices left, so there must be another inset
562 // Did we make it through the whole slice stack? Otherwise there
563 // was a problem at slice i, and we have to chop off above
565 LYXERR(Debug::DEBUG, "fixIfBroken(): cursor chopped at " << i);
573 void DocIterator::sanitize()
575 // keep a copy of the slices
576 vector<CursorSlice> const sl = slices_;
579 inset_ = &buffer_->inset();
580 Inset * inset = inset_;
581 // re-add the slices one by one, and adjust the inset pointer.
582 for (size_t i = 0, n = sl.size(); i != n; ++i) {
585 LYXERR0(" Should not happen, but does e.g. after "
586 "C-n C-l C-z S-C-z\n"
587 << " or when a Buffer has been concurrently edited by two views"
588 << '\n' << "dit: " << *this << '\n'
589 << " lastpos: " << slices_[i].lastpos());
593 if (!inset->isActive()) {
594 LYXERR0("Inset found on cursor stack is not active.");
599 top().inset_ = inset;
608 int DocIterator::find(MathData const & cell) const
610 for (size_t l = 0; l != slices_.size(); ++l) {
611 if (slices_[l].asInsetMath() && &slices_[l].cell() == &cell)
618 int DocIterator::find(Inset const * inset) const
620 for (size_t l = 0; l != slices_.size(); ++l) {
621 if (&slices_[l].inset() == inset)
628 void DocIterator::cutOff(int above, vector<CursorSlice> & cut)
630 cut = vector<CursorSlice>(slices_.begin() + above + 1, slices_.end());
631 slices_.resize(above + 1);
635 void DocIterator::cutOff(int above)
637 slices_.resize(above + 1);
641 void DocIterator::append(vector<CursorSlice> const & x)
643 slices_.insert(slices_.end(), x.begin(), x.end());
647 void DocIterator::append(DocIterator::idx_type idx, pos_type pos)
649 slices_.push_back(CursorSlice());
655 ostream & operator<<(ostream & os, DocIterator const & dit)
657 for (size_t i = 0, n = dit.depth(); i != n; ++i)
658 os << " " << dit[i] << "\n";
663 ///////////////////////////////////////////////////////
665 StableDocIterator::StableDocIterator(DocIterator const & dit)
667 data_ = dit.internalData();
668 for (size_t i = 0, n = data_.size(); i != n; ++i)
673 DocIterator StableDocIterator::asDocIterator(Buffer * buf) const
675 DocIterator dit(buf);
682 ostream & operator<<(ostream & os, StableDocIterator const & dit)
684 for (size_t i = 0, n = dit.data_.size(); i != n; ++i)
685 os << " " << dit.data_[i] << "\n";
690 bool operator==(StableDocIterator const & dit1, StableDocIterator const & dit2)
692 return dit1.data_ == dit2.data_;