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"
17 #include "InsetList.h"
18 #include "Paragraph.h"
21 #include "mathed/MathData.h"
22 #include "mathed/InsetMath.h"
23 #include "mathed/InsetMathHull.h"
25 #include "insets/InsetTabular.h"
27 #include "support/debug.h"
29 #include "support/lassert.h"
38 // We could be able to get rid of this if only every BufferView were
39 // associated to a buffer on construction.
40 DocIterator::DocIterator()
41 : boundary_(false), inset_(0)
45 DocIterator::DocIterator(Inset & inset)
46 : boundary_(false), inset_(&inset)
50 DocIterator doc_iterator_begin(Inset & inset)
52 DocIterator dit(inset);
58 DocIterator doc_iterator_end(Inset & inset)
60 return DocIterator(inset);
64 bool DocIterator::inRegexped() const
66 InsetMathHull * i = dynamic_cast<InsetMathHull *>(inset().asInsetMath());
67 return i && i->getType() == hullRegexp;
71 LyXErr & operator<<(LyXErr & os, DocIterator const & it)
78 Inset * DocIterator::nextInset() const
80 LASSERT(!empty(), /**/);
81 if (pos() == lastpos())
83 if (pos() > lastpos()) {
84 LYXERR0("Should not happen, but it does. ");
88 return nextAtom().nucleus();
89 return paragraph().getInset(pos());
93 Inset * DocIterator::prevInset() const
95 LASSERT(!empty(), /**/);
100 // FIXME: this should not happen but it does.
102 // http://bugzilla.lyx.org/show_bug.cgi?id=3189
105 return prevAtom().nucleus();
107 return paragraph().getInset(pos() - 1);
111 Inset * DocIterator::realInset() const
113 LASSERT(inTexted(), /**/);
114 // if we are in a tabular, we need the cell
115 if (inset().lyxCode() == TABULAR_CODE) {
116 InsetTabular & tabular = static_cast<InsetTabular&>(inset());
117 return tabular.cell(idx()).get();
123 MathAtom & DocIterator::prevAtom() const
125 LASSERT(!empty(), /**/);
126 LASSERT(pos() > 0, /**/);
127 return cell()[pos() - 1];
131 MathAtom & DocIterator::nextAtom() const
133 LASSERT(!empty(), /**/);
134 //lyxerr << "lastpos: " << lastpos() << " next atom:\n" << *this << endl;
135 LASSERT(pos() < lastpos(), /**/);
136 return cell()[pos()];
140 Text * DocIterator::text() const
142 LASSERT(!empty(), /**/);
147 Paragraph & DocIterator::paragraph() const
151 LASSERT(inTexted(), /**/);
152 return top().paragraph();
156 Paragraph & DocIterator::innerParagraph() const
158 LASSERT(!empty(), /**/);
159 return innerTextSlice().paragraph();
163 CursorSlice const & DocIterator::innerTextSlice() const
165 LASSERT(!empty(), /**/);
166 // go up until first non-0 text is hit
167 // (innermost text is 0 in mathed)
168 for (int i = depth() - 1; i >= 0; --i)
169 if (slices_[i].text())
172 // This case is in principe not possible. We _must_
174 LASSERT(false, /**/);
175 static CursorSlice dummy;
180 pit_type DocIterator::lastpit() const
182 return inMathed() ? 0 : text()->paragraphs().size() - 1;
186 pos_type DocIterator::lastpos() const
188 return inMathed() ? cell().size() : paragraph().size();
192 DocIterator::idx_type DocIterator::lastidx() const
194 return top().lastidx();
198 size_t DocIterator::nargs() const
200 // assume 1x1 grid for main text
201 return top().nargs();
205 size_t DocIterator::ncols() const
207 // assume 1x1 grid for main text
208 return top().ncols();
212 size_t DocIterator::nrows() const
214 // assume 1x1 grid for main text
215 return top().nrows();
219 DocIterator::row_type DocIterator::row() const
225 DocIterator::col_type DocIterator::col() const
231 MathData & DocIterator::cell() const
233 // LASSERT(inMathed(), /**/);
238 Text * DocIterator::innerText() const
240 LASSERT(!empty(), /**/);
241 // go up until first non-0 text is hit
242 // (innermost text is 0 in mathed)
243 for (int i = depth() - 1; i >= 0; --i)
244 if (slices_[i].text())
245 return slices_[i].text();
250 Inset * DocIterator::innerInsetOfType(int code) const
252 for (int i = depth() - 1; i >= 0; --i)
253 if (slices_[i].inset_->lyxCode() == code)
254 return slices_[i].inset_;
259 // This duplicates code above, but is in the critical path.
260 // So please think twice before adding stuff
261 void DocIterator::forwardPos()
263 // this dog bites his tail
265 push_back(CursorSlice(*inset_));
269 CursorSlice & tip = top();
270 //lyxerr << "XXX\n" << *this << endl;
272 // not at cell/paragraph end?
273 if (tip.pos() != tip.lastpos()) {
274 // move into an inset to the right if possible
277 n = (tip.cell().begin() + tip.pos())->nucleus();
279 n = paragraph().getInset(tip.pos());
280 if (n && n->isActive()) {
281 //lyxerr << "... descend" << endl;
282 push_back(CursorSlice(*n));
287 // jump to the next cell/paragraph if possible
293 // otherwise leave inset and jump over inset as a whole
295 // 'tip' is invalid now...
301 void DocIterator::forwardPosIgnoreCollapsed()
303 Inset * const nextinset = nextInset();
304 // FIXME: the check for asInsetMath() shouldn't be necessary
305 // but math insets do not return a sensible editable() state yet.
306 if (nextinset && !nextinset->asInsetMath()
307 && nextinset->editable() != Inset::HIGHLY_EDITABLE) {
315 void DocIterator::forwardPar()
319 while (!empty() && (!inTexted() || pos() != 0)) {
321 pos_type const lastp = lastpos();
322 Paragraph const & par = paragraph();
323 pos_type & pos = top().pos();
324 if (par.insetList().empty())
327 while (pos < lastp && !par.isInset(pos))
335 void DocIterator::forwardChar()
338 while (!empty() && pos() == lastpos())
343 void DocIterator::forwardInset()
347 while (!empty() && !nextInset()) {
349 pos_type const lastp = lastpos();
350 Paragraph const & par = paragraph();
351 pos_type & pos = top().pos();
352 while (pos < lastp && !par.isInset(pos))
362 void DocIterator::backwardChar()
365 while (!empty() && pos() == lastpos())
370 void DocIterator::backwardPos()
372 //this dog bites his tail
374 push_back(CursorSlice(*inset_));
375 top().idx() = lastidx();
376 top().pit() = lastpit();
377 top().pos() = lastpos();
381 // at inset beginning?
382 if (top().at_begin()) {
389 // entered another cell/paragraph from the right?
390 if (top().pos() == top().lastpos())
393 // move into an inset to the left if possible
396 n = (top().cell().begin() + top().pos())->nucleus();
398 n = paragraph().getInset(top().pos());
399 if (n && n->isActive()) {
400 push_back(CursorSlice(*n));
401 top().idx() = lastidx();
402 top().pit() = lastpit();
403 top().pos() = lastpos();
408 bool DocIterator::hasPart(DocIterator const & it) const
410 // it can't be a part if it is larger
411 if (it.depth() > depth())
414 // as inset adresses are the 'last' level
415 return &it.top().inset() == &slices_[it.depth() - 1].inset();
419 void DocIterator::updateInsets(Inset * inset)
421 // this function re-creates the cache of inset pointers.
422 //lyxerr << "converting:\n" << *this << endl;
423 DocIterator dit = *this;
424 size_t const n = slices_.size();
426 for (size_t i = 0 ; i < n; ++i) {
427 LASSERT(inset, /**/);
429 top().inset_ = inset;
433 //lyxerr << "converted:\n" << *this << endl;
437 bool DocIterator::fixIfBroken()
442 // Go through the slice stack from the bottom.
443 // Check that all coordinates (idx, pit, pos) are correct and
444 // that the inset is the one which is claimed to be there
445 Inset * inset = &slices_[0].inset();
447 size_t n = slices_.size();
448 for (; i != n; ++i) {
449 CursorSlice & cs = slices_[i];
450 if (&cs.inset() != inset) {
451 // the whole slice is wrong, chop off this as well
453 LYXERR(Debug::DEBUG, "fixIfBroken(): inset changed");
455 } else if (cs.idx() > cs.lastidx()) {
456 cs.idx() = cs.lastidx();
457 cs.pit() = cs.lastpit();
458 cs.pos() = cs.lastpos();
459 LYXERR(Debug::DEBUG, "fixIfBroken(): idx fixed");
461 } else if (cs.pit() > cs.lastpit()) {
462 cs.pit() = cs.lastpit();
463 cs.pos() = cs.lastpos();
464 LYXERR(Debug::DEBUG, "fixIfBroken(): pit fixed");
466 } else if (cs.pos() > cs.lastpos()) {
467 cs.pos() = cs.lastpos();
468 LYXERR(Debug::DEBUG, "fixIfBroken(): pos fixed");
470 } else if (i != n - 1 && cs.pos() != cs.lastpos()) {
471 // get inset which is supposed to be in the next slice
472 if (cs.inset().inMathed())
473 inset = (cs.cell().begin() + cs.pos())->nucleus();
474 else if (Inset * csInset = cs.paragraph().getInset(cs.pos()))
477 // there are slices left, so there must be another inset
483 // Did we make it through the whole slice stack? Otherwise there
484 // was a problem at slice i, and we have to chop off above
486 LYXERR(Debug::DEBUG, "fixIfBroken(): cursor chopped at " << i);
494 int DocIterator::find(MathData const & cell) const
496 for (size_t l = 0; l != slices_.size(); ++l) {
497 if (slices_[l].asInsetMath() && &slices_[l].cell() == &cell)
504 int DocIterator::find(Inset const * inset) const
506 for (size_t l = 0; l != slices_.size(); ++l) {
507 if (&slices_[l].inset() == inset)
514 void DocIterator::cutOff(int above, vector<CursorSlice> & cut)
516 cut = vector<CursorSlice>(slices_.begin() + above + 1, slices_.end());
517 slices_.resize(above + 1);
521 void DocIterator::cutOff(int above)
523 slices_.resize(above + 1);
527 void DocIterator::append(vector<CursorSlice> const & x)
529 slices_.insert(slices_.end(), x.begin(), x.end());
533 void DocIterator::append(DocIterator::idx_type idx, pos_type pos)
535 slices_.push_back(CursorSlice());
541 ostream & operator<<(ostream & os, DocIterator const & dit)
543 for (size_t i = 0, n = dit.depth(); i != n; ++i)
544 os << " " << dit[i] << "\n";
549 ///////////////////////////////////////////////////////
551 StableDocIterator::StableDocIterator(DocIterator const & dit)
553 data_ = dit.internalData();
554 for (size_t i = 0, n = data_.size(); i != n; ++i)
559 DocIterator StableDocIterator::asDocIterator(Inset * inset) const
561 // this function re-creates the cache of inset pointers
562 //lyxerr << "converting:\n" << *this << endl;
563 DocIterator dit = DocIterator(*inset);
564 for (size_t i = 0, n = data_.size(); i != n; ++i) {
567 LYXERR0(" Should not happen, but does e.g. after "
568 "C-n C-l C-z S-C-z\n"
569 << " or when a Buffer has been concurrently edited by two views"
570 << '\n' << "dit: " << dit << '\n'
571 << " lastpos: " << dit.lastpos());
575 dit.push_back(data_[i]);
576 dit.top().inset_ = inset;
577 if (dit.fixIfBroken())
580 inset = dit.nextInset();
582 //lyxerr << "convert:\n" << *this << " to:\n" << dit << endl;
587 ostream & operator<<(ostream & os, StableDocIterator const & dit)
589 for (size_t i = 0, n = dit.data_.size(); i != n; ++i)
590 os << " " << dit.data_[i] << "\n";
595 bool operator==(StableDocIterator const & dit1, StableDocIterator const & dit2)
597 return dit1.data_ == dit2.data_;