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 CursorSlice const & DocIterator::innerTextSlice() const
197 LASSERT(!empty(), /**/);
198 // go up until first non-0 text is hit
199 // (innermost text is 0 in mathed)
200 for (int i = depth() - 1; i >= 0; --i)
201 if (slices_[i].text())
204 // This case is in principe not possible. We _must_
206 LASSERT(false, /**/);
207 static CursorSlice dummy;
212 pit_type DocIterator::lastpit() const
214 return inMathed() ? 0 : text()->paragraphs().size() - 1;
218 pos_type DocIterator::lastpos() const
220 return inMathed() ? cell().size() : paragraph().size();
224 DocIterator::idx_type DocIterator::lastidx() const
226 return top().lastidx();
230 size_t DocIterator::nargs() const
232 // assume 1x1 grid for main text
233 return top().nargs();
237 size_t DocIterator::ncols() const
239 // assume 1x1 grid for main text
240 return top().ncols();
244 size_t DocIterator::nrows() const
246 // assume 1x1 grid for main text
247 return top().nrows();
251 DocIterator::row_type DocIterator::row() const
257 DocIterator::col_type DocIterator::col() const
263 MathData & DocIterator::cell() const
265 // LASSERT(inMathed(), /**/);
270 Text * DocIterator::innerText() const
272 LASSERT(!empty(), /**/);
273 return innerTextSlice().text();
277 Inset * DocIterator::innerInsetOfType(int code) const
279 for (int i = depth() - 1; i >= 0; --i)
280 if (slices_[i].inset_->lyxCode() == code)
281 return slices_[i].inset_;
286 // This duplicates code above, but is in the critical path.
287 // So please think twice before adding stuff
288 void DocIterator::forwardPos()
290 // this dog bites his tail
292 push_back(CursorSlice(*inset_));
296 CursorSlice & tip = top();
297 //lyxerr << "XXX\n" << *this << endl;
299 // not at cell/paragraph end?
300 if (tip.pos() != tip.lastpos()) {
301 // move into an inset to the right if possible
304 n = (tip.cell().begin() + tip.pos())->nucleus();
306 n = paragraph().getInset(tip.pos());
307 if (n && n->isActive()) {
308 //lyxerr << "... descend" << endl;
309 push_back(CursorSlice(*n));
314 // jump to the next cell/paragraph if possible
320 // otherwise leave inset and jump over inset as a whole
322 // 'tip' is invalid now...
328 void DocIterator::forwardPosIgnoreCollapsed()
330 Inset * const nextinset = nextInset();
331 // FIXME: the check for asInsetMath() shouldn't be necessary
332 // but math insets do not return a sensible editable() state yet.
333 if (nextinset && !nextinset->asInsetMath()
334 && !nextinset->editable()) {
342 void DocIterator::forwardPar()
346 while (!empty() && (!inTexted() || pos() != 0)) {
348 pos_type const lastp = lastpos();
349 Paragraph const & par = paragraph();
350 pos_type & pos = top().pos();
351 if (par.insetList().empty())
354 while (pos < lastp && !par.isInset(pos))
362 void DocIterator::forwardChar()
365 while (!empty() && pos() == lastpos())
370 void DocIterator::forwardInset()
374 while (!empty() && !nextInset()) {
376 pos_type const lastp = lastpos();
377 Paragraph const & par = paragraph();
378 pos_type & pos = top().pos();
379 while (pos < lastp && !par.isInset(pos))
389 void DocIterator::backwardChar()
392 while (!empty() && pos() == lastpos())
397 void DocIterator::backwardPos()
399 //this dog bites his tail
401 push_back(CursorSlice(*inset_));
402 top().idx() = lastidx();
403 top().pit() = lastpit();
404 top().pos() = lastpos();
408 // at inset beginning?
409 if (top().at_begin()) {
416 // entered another cell/paragraph from the right?
417 if (top().pos() == top().lastpos())
420 // move into an inset to the left if possible
423 n = (top().cell().begin() + top().pos())->nucleus();
425 n = paragraph().getInset(top().pos());
426 if (n && n->isActive()) {
427 push_back(CursorSlice(*n));
428 top().idx() = lastidx();
429 top().pit() = lastpit();
430 top().pos() = lastpos();
435 bool DocIterator::hasPart(DocIterator const & it) const
437 // it can't be a part if it is larger
438 if (it.depth() > depth())
441 // as inset adresses are the 'last' level
442 return &it.top().inset() == &slices_[it.depth() - 1].inset();
446 void DocIterator::updateInsets(Inset * inset)
448 // this function re-creates the cache of inset pointers.
449 //lyxerr << "converting:\n" << *this << endl;
450 DocIterator dit = *this;
451 size_t const n = slices_.size();
453 for (size_t i = 0 ; i < n; ++i) {
454 LASSERT(inset, /**/);
456 top().inset_ = inset;
460 //lyxerr << "converted:\n" << *this << endl;
464 bool DocIterator::fixIfBroken()
469 // Go through the slice stack from the bottom.
470 // Check that all coordinates (idx, pit, pos) are correct and
471 // that the inset is the one which is claimed to be there
472 Inset * inset = &slices_[0].inset();
474 size_t n = slices_.size();
475 for (; i != n; ++i) {
476 CursorSlice & cs = slices_[i];
477 if (&cs.inset() != inset) {
478 // the whole slice is wrong, chop off this as well
480 LYXERR(Debug::DEBUG, "fixIfBroken(): inset changed");
482 } else if (cs.idx() > cs.lastidx()) {
483 cs.idx() = cs.lastidx();
484 cs.pit() = cs.lastpit();
485 cs.pos() = cs.lastpos();
486 LYXERR(Debug::DEBUG, "fixIfBroken(): idx fixed");
488 } else if (cs.pit() > cs.lastpit()) {
489 cs.pit() = cs.lastpit();
490 cs.pos() = cs.lastpos();
491 LYXERR(Debug::DEBUG, "fixIfBroken(): pit fixed");
493 } else if (cs.pos() > cs.lastpos()) {
494 cs.pos() = cs.lastpos();
495 LYXERR(Debug::DEBUG, "fixIfBroken(): pos fixed");
497 } else if (i != n - 1 && cs.pos() != cs.lastpos()) {
498 // get inset which is supposed to be in the next slice
499 if (cs.inset().inMathed())
500 inset = (cs.cell().begin() + cs.pos())->nucleus();
501 else if (Inset * csInset = cs.paragraph().getInset(cs.pos()))
504 // there are slices left, so there must be another inset
510 // Did we make it through the whole slice stack? Otherwise there
511 // was a problem at slice i, and we have to chop off above
513 LYXERR(Debug::DEBUG, "fixIfBroken(): cursor chopped at " << i);
521 int DocIterator::find(MathData const & cell) const
523 for (size_t l = 0; l != slices_.size(); ++l) {
524 if (slices_[l].asInsetMath() && &slices_[l].cell() == &cell)
531 int DocIterator::find(Inset const * inset) const
533 for (size_t l = 0; l != slices_.size(); ++l) {
534 if (&slices_[l].inset() == inset)
541 void DocIterator::cutOff(int above, vector<CursorSlice> & cut)
543 cut = vector<CursorSlice>(slices_.begin() + above + 1, slices_.end());
544 slices_.resize(above + 1);
548 void DocIterator::cutOff(int above)
550 slices_.resize(above + 1);
554 void DocIterator::append(vector<CursorSlice> const & x)
556 slices_.insert(slices_.end(), x.begin(), x.end());
560 void DocIterator::append(DocIterator::idx_type idx, pos_type pos)
562 slices_.push_back(CursorSlice());
568 ostream & operator<<(ostream & os, DocIterator const & dit)
570 for (size_t i = 0, n = dit.depth(); i != n; ++i)
571 os << " " << dit[i] << "\n";
576 ///////////////////////////////////////////////////////
578 StableDocIterator::StableDocIterator(DocIterator const & dit)
580 data_ = dit.internalData();
581 for (size_t i = 0, n = data_.size(); i != n; ++i)
586 DocIterator StableDocIterator::asDocIterator(Buffer * buf) const
588 // this function re-creates the cache of inset pointers
589 //lyxerr << "converting:\n" << *this << endl;
590 Inset * inset = &buf->inset();
591 DocIterator dit = DocIterator(buf, inset);
592 for (size_t i = 0, n = data_.size(); i != n; ++i) {
595 LYXERR0(" Should not happen, but does e.g. after "
596 "C-n C-l C-z S-C-z\n"
597 << " or when a Buffer has been concurrently edited by two views"
598 << '\n' << "dit: " << dit << '\n'
599 << " lastpos: " << dit.lastpos());
603 dit.push_back(data_[i]);
604 dit.top().inset_ = inset;
605 if (dit.fixIfBroken())
608 inset = dit.nextInset();
610 //lyxerr << "convert:\n" << *this << " to:\n" << dit << endl;
615 ostream & operator<<(ostream & os, StableDocIterator const & dit)
617 for (size_t i = 0, n = dit.data_.size(); i != n; ++i)
618 os << " " << dit.data_[i] << "\n";
623 bool operator==(StableDocIterator const & dit1, StableDocIterator const & dit2)
625 return dit1.data_ == dit2.data_;