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)
45 // We could be able to get rid of this if only every BufferView were
46 // associated to a buffer on construction.
47 DocIterator::DocIterator(Buffer * buf)
48 : boundary_(false), inset_(0), buffer_(buf)
52 DocIterator::DocIterator(Buffer * buf, Inset * inset)
53 : boundary_(false), inset_(inset), buffer_(buf)
57 DocIterator doc_iterator_begin(const Buffer * buf0, const Inset * inset0)
59 Buffer * buf = const_cast<Buffer *>(buf0);
60 Inset * inset = const_cast<Inset *>(inset0);
61 DocIterator dit(buf, inset ? inset : &buf->inset());
67 DocIterator doc_iterator_end(const Buffer * buf0, const Inset * inset0)
69 Buffer * buf = const_cast<Buffer *>(buf0);
70 Inset * inset = const_cast<Inset *>(inset0);
71 return DocIterator(buf, inset ? inset : &buf->inset());
75 DocIterator DocIterator::clone(Buffer * buffer) const
77 LASSERT(buffer->isClone(), return DocIterator());
78 Inset * inset = &buffer->inset();
79 DocIterator dit(buffer);
80 size_t const n = slices_.size();
81 for (size_t i = 0 ; i != n; ++i) {
83 dit.push_back(slices_[i]);
84 dit.top().inset_ = inset;
86 inset = dit.nextInset();
92 bool DocIterator::inRegexped() const
94 InsetMath * im = inset().asInsetMath();
97 InsetMathHull * hull = im->asHullInset();
98 return hull && hull->getType() == hullRegexp;
102 LyXErr & operator<<(LyXErr & os, DocIterator const & it)
109 Inset * DocIterator::nextInset() const
111 LASSERT(!empty(), return 0);
112 if (pos() == lastpos())
114 if (pos() > lastpos()) {
115 LYXERR0("Should not happen, but it does: pos() = "
116 << pos() << ", lastpos() = " << lastpos());
120 return nextAtom().nucleus();
121 return paragraph().getInset(pos());
125 Inset * DocIterator::prevInset() const
127 LASSERT(!empty(), return 0);
132 // FIXME: this should not happen but it does.
134 // http://www.lyx.org/trac/ticket/3189
137 return prevAtom().nucleus();
139 return paragraph().getInset(pos() - 1);
143 Inset * DocIterator::realInset() const
145 LASSERT(inTexted(), /**/);
146 // if we are in a tabular, we need the cell
147 if (inset().lyxCode() == TABULAR_CODE) {
148 InsetTabular * tabular = inset().asInsetTabular();
149 return tabular->cell(idx()).get();
155 MathAtom & DocIterator::prevAtom() const
157 LASSERT(!empty(), /**/);
158 LASSERT(pos() > 0, /**/);
159 return cell()[pos() - 1];
163 MathAtom & DocIterator::nextAtom() const
165 LASSERT(!empty(), /**/);
166 //lyxerr << "lastpos: " << lastpos() << " next atom:\n" << *this << endl;
167 LASSERT(pos() < lastpos(), /**/);
168 return cell()[pos()];
172 Text * DocIterator::text() const
174 LASSERT(!empty(), /**/);
179 Paragraph & DocIterator::paragraph() const
183 LASSERT(inTexted(), /**/);
184 return top().paragraph();
188 Paragraph & DocIterator::innerParagraph() const
190 LASSERT(!empty(), /**/);
191 return innerTextSlice().paragraph();
195 FontSpan DocIterator::locateWord(word_location const loc) const
197 FontSpan f = FontSpan();
199 if (!top().text()->empty()) {
201 top().paragraph().locateWord(f.first, f.last, loc);
207 CursorSlice const & DocIterator::innerTextSlice() const
209 LASSERT(!empty(), /**/);
210 // go up until first non-0 text is hit
211 // (innermost text is 0 in mathed)
212 for (int i = depth() - 1; i >= 0; --i)
213 if (slices_[i].text())
216 // This case is in principe not possible. We _must_
218 LASSERT(false, /**/);
219 static CursorSlice dummy;
224 pit_type DocIterator::lastpit() const
226 return inMathed() ? 0 : text()->paragraphs().size() - 1;
230 pos_type DocIterator::lastpos() const
232 return inMathed() ? cell().size() : paragraph().size();
236 DocIterator::idx_type DocIterator::lastidx() const
238 return top().lastidx();
242 size_t DocIterator::nargs() const
244 // assume 1x1 grid for main text
245 return top().nargs();
249 size_t DocIterator::ncols() const
251 // assume 1x1 grid for main text
252 return top().ncols();
256 size_t DocIterator::nrows() const
258 // assume 1x1 grid for main text
259 return top().nrows();
263 DocIterator::row_type DocIterator::row() const
269 DocIterator::col_type DocIterator::col() const
275 MathData & DocIterator::cell() const
277 // LASSERT(inMathed(), /**/);
282 Text * DocIterator::innerText() const
284 LASSERT(!empty(), /**/);
285 return innerTextSlice().text();
289 Inset * DocIterator::innerInsetOfType(int code) const
291 for (int i = depth() - 1; i >= 0; --i)
292 if (slices_[i].inset_->lyxCode() == code)
293 return slices_[i].inset_;
298 // This duplicates code above, but is in the critical path.
299 // So please think twice before adding stuff
300 void DocIterator::forwardPos()
302 // this dog bites his tail
304 push_back(CursorSlice(*inset_));
308 CursorSlice & tip = top();
309 //lyxerr << "XXX\n" << *this << endl;
311 // not at cell/paragraph end?
312 if (tip.pos() != tip.lastpos()) {
313 // move into an inset to the right if possible
316 n = (tip.cell().begin() + tip.pos())->nucleus();
318 n = paragraph().getInset(tip.pos());
319 if (n && n->isActive()) {
320 //lyxerr << "... descend" << endl;
321 push_back(CursorSlice(*n));
326 // jump to the next cell/paragraph if possible
332 // otherwise leave inset and jump over inset as a whole
334 // 'tip' is invalid now...
340 void DocIterator::forwardPosIgnoreCollapsed()
342 Inset * const nextinset = nextInset();
343 // FIXME: the check for asInsetMath() shouldn't be necessary
344 // but math insets do not return a sensible editable() state yet.
345 if (nextinset && !nextinset->asInsetMath()
346 && !nextinset->editable()) {
354 void DocIterator::forwardPar()
358 while (!empty() && (!inTexted() || pos() != 0)) {
360 pos_type const lastp = lastpos();
361 Paragraph const & par = paragraph();
362 pos_type & pos = top().pos();
363 if (par.insetList().empty())
366 while (pos < lastp && !par.isInset(pos))
374 void DocIterator::forwardChar()
377 while (!empty() && pos() == lastpos())
382 void DocIterator::forwardInset()
386 while (!empty() && !nextInset()) {
388 pos_type const lastp = lastpos();
389 Paragraph const & par = paragraph();
390 pos_type & pos = top().pos();
391 while (pos < lastp && !par.isInset(pos))
401 void DocIterator::backwardChar()
404 while (!empty() && pos() == lastpos())
409 void DocIterator::backwardPos()
411 //this dog bites his tail
413 push_back(CursorSlice(*inset_));
414 top().idx() = lastidx();
415 top().pit() = lastpit();
416 top().pos() = lastpos();
420 // at inset beginning?
421 if (top().at_begin()) {
428 // entered another cell/paragraph from the right?
429 if (top().pos() == top().lastpos())
432 // move into an inset to the left if possible
435 n = (top().cell().begin() + top().pos())->nucleus();
437 n = paragraph().getInset(top().pos());
438 if (n && n->isActive()) {
439 push_back(CursorSlice(*n));
440 top().idx() = lastidx();
441 top().pit() = lastpit();
442 top().pos() = lastpos();
447 bool DocIterator::hasPart(DocIterator const & it) const
449 // it can't be a part if it is larger
450 if (it.depth() > depth())
453 // as inset adresses are the 'last' level
454 return &it.top().inset() == &slices_[it.depth() - 1].inset();
458 void DocIterator::updateInsets(Inset * inset)
460 // this function re-creates the cache of inset pointers.
461 //lyxerr << "converting:\n" << *this << endl;
462 DocIterator dit = *this;
463 size_t const n = slices_.size();
465 for (size_t i = 0 ; i < n; ++i) {
466 LASSERT(inset, /**/);
468 top().inset_ = inset;
472 //lyxerr << "converted:\n" << *this << endl;
476 bool DocIterator::fixIfBroken()
481 // Go through the slice stack from the bottom.
482 // Check that all coordinates (idx, pit, pos) are correct and
483 // that the inset is the one which is claimed to be there
484 Inset * inset = &slices_[0].inset();
486 size_t n = slices_.size();
487 for (; i != n; ++i) {
488 CursorSlice & cs = slices_[i];
489 if (&cs.inset() != inset) {
490 // the whole slice is wrong, chop off this as well
492 LYXERR(Debug::DEBUG, "fixIfBroken(): inset changed");
494 } else if (cs.idx() > cs.lastidx()) {
495 cs.idx() = cs.lastidx();
496 cs.pit() = cs.lastpit();
497 cs.pos() = cs.lastpos();
498 LYXERR(Debug::DEBUG, "fixIfBroken(): idx fixed");
500 } else if (cs.pit() > cs.lastpit()) {
501 cs.pit() = cs.lastpit();
502 cs.pos() = cs.lastpos();
503 LYXERR(Debug::DEBUG, "fixIfBroken(): pit fixed");
505 } else if (cs.pos() > cs.lastpos()) {
506 cs.pos() = cs.lastpos();
507 LYXERR(Debug::DEBUG, "fixIfBroken(): pos fixed");
509 } else if (i != n - 1 && cs.pos() != cs.lastpos()) {
510 // get inset which is supposed to be in the next slice
511 if (cs.inset().inMathed())
512 inset = (cs.cell().begin() + cs.pos())->nucleus();
513 else if (Inset * csInset = cs.paragraph().getInset(cs.pos()))
516 // there are slices left, so there must be another inset
522 // Did we make it through the whole slice stack? Otherwise there
523 // was a problem at slice i, and we have to chop off above
525 LYXERR(Debug::DEBUG, "fixIfBroken(): cursor chopped at " << i);
533 int DocIterator::find(MathData const & cell) const
535 for (size_t l = 0; l != slices_.size(); ++l) {
536 if (slices_[l].asInsetMath() && &slices_[l].cell() == &cell)
543 int DocIterator::find(Inset const * inset) const
545 for (size_t l = 0; l != slices_.size(); ++l) {
546 if (&slices_[l].inset() == inset)
553 void DocIterator::cutOff(int above, vector<CursorSlice> & cut)
555 cut = vector<CursorSlice>(slices_.begin() + above + 1, slices_.end());
556 slices_.resize(above + 1);
560 void DocIterator::cutOff(int above)
562 slices_.resize(above + 1);
566 void DocIterator::append(vector<CursorSlice> const & x)
568 slices_.insert(slices_.end(), x.begin(), x.end());
572 void DocIterator::append(DocIterator::idx_type idx, pos_type pos)
574 slices_.push_back(CursorSlice());
580 ostream & operator<<(ostream & os, DocIterator const & dit)
582 for (size_t i = 0, n = dit.depth(); i != n; ++i)
583 os << " " << dit[i] << "\n";
588 ///////////////////////////////////////////////////////
590 StableDocIterator::StableDocIterator(DocIterator const & dit)
592 data_ = dit.internalData();
593 for (size_t i = 0, n = data_.size(); i != n; ++i)
598 DocIterator StableDocIterator::asDocIterator(Buffer * buf) const
600 // this function re-creates the cache of inset pointers
601 //lyxerr << "converting:\n" << *this << endl;
602 Inset * inset = &buf->inset();
603 DocIterator dit = DocIterator(buf, inset);
604 for (size_t i = 0, n = data_.size(); i != n; ++i) {
607 LYXERR0(" Should not happen, but does e.g. after "
608 "C-n C-l C-z S-C-z\n"
609 << " or when a Buffer has been concurrently edited by two views"
610 << '\n' << "dit: " << dit << '\n'
611 << " lastpos: " << dit.lastpos());
615 dit.push_back(data_[i]);
616 dit.top().inset_ = inset;
617 if (dit.fixIfBroken())
620 inset = dit.nextInset();
622 //lyxerr << "convert:\n" << *this << " to:\n" << dit << endl;
627 ostream & operator<<(ostream & os, StableDocIterator const & dit)
629 for (size_t i = 0, n = dit.data_.size(); i != n; ++i)
630 os << " " << dit.data_[i] << "\n";
635 bool operator==(StableDocIterator const & dit1, StableDocIterator const & dit2)
637 return dit1.data_ == dit2.data_;