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();
200 top().paragraph().locateWord(f.first, f.last, loc);
205 CursorSlice const & DocIterator::innerTextSlice() const
207 LASSERT(!empty(), /**/);
208 // go up until first non-0 text is hit
209 // (innermost text is 0 in mathed)
210 for (int i = depth() - 1; i >= 0; --i)
211 if (slices_[i].text())
214 // This case is in principe not possible. We _must_
216 LASSERT(false, /**/);
217 static CursorSlice dummy;
222 pit_type DocIterator::lastpit() const
224 return inMathed() ? 0 : text()->paragraphs().size() - 1;
228 pos_type DocIterator::lastpos() const
230 return inMathed() ? cell().size() : paragraph().size();
234 DocIterator::idx_type DocIterator::lastidx() const
236 return top().lastidx();
240 size_t DocIterator::nargs() const
242 // assume 1x1 grid for main text
243 return top().nargs();
247 size_t DocIterator::ncols() const
249 // assume 1x1 grid for main text
250 return top().ncols();
254 size_t DocIterator::nrows() const
256 // assume 1x1 grid for main text
257 return top().nrows();
261 DocIterator::row_type DocIterator::row() const
267 DocIterator::col_type DocIterator::col() const
273 MathData & DocIterator::cell() const
275 // LASSERT(inMathed(), /**/);
280 Text * DocIterator::innerText() const
282 LASSERT(!empty(), /**/);
283 return innerTextSlice().text();
287 Inset * DocIterator::innerInsetOfType(int code) const
289 for (int i = depth() - 1; i >= 0; --i)
290 if (slices_[i].inset_->lyxCode() == code)
291 return slices_[i].inset_;
296 // This duplicates code above, but is in the critical path.
297 // So please think twice before adding stuff
298 void DocIterator::forwardPos()
300 // this dog bites his tail
302 push_back(CursorSlice(*inset_));
306 CursorSlice & tip = top();
307 //lyxerr << "XXX\n" << *this << endl;
309 // not at cell/paragraph end?
310 if (tip.pos() != tip.lastpos()) {
311 // move into an inset to the right if possible
314 n = (tip.cell().begin() + tip.pos())->nucleus();
316 n = paragraph().getInset(tip.pos());
317 if (n && n->isActive()) {
318 //lyxerr << "... descend" << endl;
319 push_back(CursorSlice(*n));
324 // jump to the next cell/paragraph if possible
330 // otherwise leave inset and jump over inset as a whole
332 // 'tip' is invalid now...
338 void DocIterator::forwardPosIgnoreCollapsed()
340 Inset * const nextinset = nextInset();
341 // FIXME: the check for asInsetMath() shouldn't be necessary
342 // but math insets do not return a sensible editable() state yet.
343 if (nextinset && !nextinset->asInsetMath()
344 && !nextinset->editable()) {
352 void DocIterator::forwardPar()
356 while (!empty() && (!inTexted() || pos() != 0)) {
358 pos_type const lastp = lastpos();
359 Paragraph const & par = paragraph();
360 pos_type & pos = top().pos();
361 if (par.insetList().empty())
364 while (pos < lastp && !par.isInset(pos))
372 void DocIterator::forwardChar()
375 while (!empty() && pos() == lastpos())
380 void DocIterator::forwardInset()
384 while (!empty() && !nextInset()) {
386 pos_type const lastp = lastpos();
387 Paragraph const & par = paragraph();
388 pos_type & pos = top().pos();
389 while (pos < lastp && !par.isInset(pos))
399 void DocIterator::backwardChar()
402 while (!empty() && pos() == lastpos())
407 void DocIterator::backwardPos()
409 //this dog bites his tail
411 push_back(CursorSlice(*inset_));
412 top().idx() = lastidx();
413 top().pit() = lastpit();
414 top().pos() = lastpos();
418 // at inset beginning?
419 if (top().at_begin()) {
426 // entered another cell/paragraph from the right?
427 if (top().pos() == top().lastpos())
430 // move into an inset to the left if possible
433 n = (top().cell().begin() + top().pos())->nucleus();
435 n = paragraph().getInset(top().pos());
436 if (n && n->isActive()) {
437 push_back(CursorSlice(*n));
438 top().idx() = lastidx();
439 top().pit() = lastpit();
440 top().pos() = lastpos();
445 bool DocIterator::hasPart(DocIterator const & it) const
447 // it can't be a part if it is larger
448 if (it.depth() > depth())
451 // as inset adresses are the 'last' level
452 return &it.top().inset() == &slices_[it.depth() - 1].inset();
456 void DocIterator::updateInsets(Inset * inset)
458 // this function re-creates the cache of inset pointers.
459 //lyxerr << "converting:\n" << *this << endl;
460 DocIterator dit = *this;
461 size_t const n = slices_.size();
463 for (size_t i = 0 ; i < n; ++i) {
464 LASSERT(inset, /**/);
466 top().inset_ = inset;
470 //lyxerr << "converted:\n" << *this << endl;
474 bool DocIterator::fixIfBroken()
479 // Go through the slice stack from the bottom.
480 // Check that all coordinates (idx, pit, pos) are correct and
481 // that the inset is the one which is claimed to be there
482 Inset * inset = &slices_[0].inset();
484 size_t n = slices_.size();
485 for (; i != n; ++i) {
486 CursorSlice & cs = slices_[i];
487 if (&cs.inset() != inset) {
488 // the whole slice is wrong, chop off this as well
490 LYXERR(Debug::DEBUG, "fixIfBroken(): inset changed");
492 } else if (cs.idx() > cs.lastidx()) {
493 cs.idx() = cs.lastidx();
494 cs.pit() = cs.lastpit();
495 cs.pos() = cs.lastpos();
496 LYXERR(Debug::DEBUG, "fixIfBroken(): idx fixed");
498 } else if (cs.pit() > cs.lastpit()) {
499 cs.pit() = cs.lastpit();
500 cs.pos() = cs.lastpos();
501 LYXERR(Debug::DEBUG, "fixIfBroken(): pit fixed");
503 } else if (cs.pos() > cs.lastpos()) {
504 cs.pos() = cs.lastpos();
505 LYXERR(Debug::DEBUG, "fixIfBroken(): pos fixed");
507 } else if (i != n - 1 && cs.pos() != cs.lastpos()) {
508 // get inset which is supposed to be in the next slice
509 if (cs.inset().inMathed())
510 inset = (cs.cell().begin() + cs.pos())->nucleus();
511 else if (Inset * csInset = cs.paragraph().getInset(cs.pos()))
514 // there are slices left, so there must be another inset
520 // Did we make it through the whole slice stack? Otherwise there
521 // was a problem at slice i, and we have to chop off above
523 LYXERR(Debug::DEBUG, "fixIfBroken(): cursor chopped at " << i);
531 int DocIterator::find(MathData const & cell) const
533 for (size_t l = 0; l != slices_.size(); ++l) {
534 if (slices_[l].asInsetMath() && &slices_[l].cell() == &cell)
541 int DocIterator::find(Inset const * inset) const
543 for (size_t l = 0; l != slices_.size(); ++l) {
544 if (&slices_[l].inset() == inset)
551 void DocIterator::cutOff(int above, vector<CursorSlice> & cut)
553 cut = vector<CursorSlice>(slices_.begin() + above + 1, slices_.end());
554 slices_.resize(above + 1);
558 void DocIterator::cutOff(int above)
560 slices_.resize(above + 1);
564 void DocIterator::append(vector<CursorSlice> const & x)
566 slices_.insert(slices_.end(), x.begin(), x.end());
570 void DocIterator::append(DocIterator::idx_type idx, pos_type pos)
572 slices_.push_back(CursorSlice());
578 ostream & operator<<(ostream & os, DocIterator const & dit)
580 for (size_t i = 0, n = dit.depth(); i != n; ++i)
581 os << " " << dit[i] << "\n";
586 ///////////////////////////////////////////////////////
588 StableDocIterator::StableDocIterator(DocIterator const & dit)
590 data_ = dit.internalData();
591 for (size_t i = 0, n = data_.size(); i != n; ++i)
596 DocIterator StableDocIterator::asDocIterator(Buffer * buf) const
598 // this function re-creates the cache of inset pointers
599 //lyxerr << "converting:\n" << *this << endl;
600 Inset * inset = &buf->inset();
601 DocIterator dit = DocIterator(buf, inset);
602 for (size_t i = 0, n = data_.size(); i != n; ++i) {
605 LYXERR0(" Should not happen, but does e.g. after "
606 "C-n C-l C-z S-C-z\n"
607 << " or when a Buffer has been concurrently edited by two views"
608 << '\n' << "dit: " << dit << '\n'
609 << " lastpos: " << dit.lastpos());
613 dit.push_back(data_[i]);
614 dit.top().inset_ = inset;
615 if (dit.fixIfBroken())
618 inset = dit.nextInset();
620 //lyxerr << "convert:\n" << *this << " to:\n" << dit << endl;
625 ostream & operator<<(ostream & os, StableDocIterator const & dit)
627 for (size_t i = 0, n = dit.data_.size(); i != n; ++i)
628 os << " " << dit.data_[i] << "\n";
633 bool operator==(StableDocIterator const & dit1, StableDocIterator const & dit2)
635 return dit1.data_ == dit2.data_;