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"
22 #include "mathed/MathData.h"
23 #include "mathed/InsetMath.h"
24 #include "mathed/InsetMathHull.h"
26 #include "insets/InsetTabular.h"
28 #include "support/debug.h"
29 #include "support/lassert.h"
38 DocIterator::DocIterator()
39 : boundary_(false), inset_(0), buffer_(0)
42 // We could be able to get rid of this if only every BufferView were
43 // associated to a buffer on construction.
44 DocIterator::DocIterator(Buffer * buf)
45 : boundary_(false), inset_(0), buffer_(buf)
49 DocIterator::DocIterator(Buffer * buf, Inset * inset)
50 : boundary_(false), inset_(inset), buffer_(buf)
54 DocIterator doc_iterator_begin(const Buffer * buf0, const Inset * inset0)
56 Buffer * buf = const_cast<Buffer *>(buf0);
57 Inset * inset = const_cast<Inset *>(inset0);
58 DocIterator dit(buf, inset ? inset : &buf->inset());
64 DocIterator doc_iterator_end(const Buffer * buf0, const Inset * inset0)
66 Buffer * buf = const_cast<Buffer *>(buf0);
67 Inset * inset = const_cast<Inset *>(inset0);
68 return DocIterator(buf, inset ? inset : &buf->inset());
72 bool DocIterator::inRegexped() const
74 InsetMathHull * i = dynamic_cast<InsetMathHull *>(inset().asInsetMath());
75 return i && i->getType() == hullRegexp;
79 LyXErr & operator<<(LyXErr & os, DocIterator const & it)
86 Inset * DocIterator::nextInset() const
88 LASSERT(!empty(), return 0);
89 if (pos() == lastpos())
91 if (pos() > lastpos()) {
92 LYXERR0("Should not happen, but it does. ");
96 return nextAtom().nucleus();
97 return paragraph().getInset(pos());
101 Inset * DocIterator::prevInset() const
103 LASSERT(!empty(), return 0);
108 // FIXME: this should not happen but it does.
110 // http://bugzilla.lyx.org/show_bug.cgi?id=3189
113 return prevAtom().nucleus();
115 return paragraph().getInset(pos() - 1);
119 Inset * DocIterator::realInset() const
121 LASSERT(inTexted(), /**/);
122 // if we are in a tabular, we need the cell
123 if (inset().lyxCode() == TABULAR_CODE) {
124 InsetTabular & tabular = static_cast<InsetTabular&>(inset());
125 return tabular.cell(idx()).get();
131 MathAtom & DocIterator::prevAtom() const
133 LASSERT(!empty(), /**/);
134 LASSERT(pos() > 0, /**/);
135 return cell()[pos() - 1];
139 MathAtom & DocIterator::nextAtom() const
141 LASSERT(!empty(), /**/);
142 //lyxerr << "lastpos: " << lastpos() << " next atom:\n" << *this << endl;
143 LASSERT(pos() < lastpos(), /**/);
144 return cell()[pos()];
148 Text * DocIterator::text() const
150 LASSERT(!empty(), /**/);
155 Paragraph & DocIterator::paragraph() const
159 LASSERT(inTexted(), /**/);
160 return top().paragraph();
164 Paragraph & DocIterator::innerParagraph() const
166 LASSERT(!empty(), /**/);
167 return innerTextSlice().paragraph();
171 CursorSlice const & DocIterator::innerTextSlice() const
173 LASSERT(!empty(), /**/);
174 // go up until first non-0 text is hit
175 // (innermost text is 0 in mathed)
176 for (int i = depth() - 1; i >= 0; --i)
177 if (slices_[i].text())
180 // This case is in principe not possible. We _must_
182 LASSERT(false, /**/);
183 static CursorSlice dummy;
188 pit_type DocIterator::lastpit() const
190 return inMathed() ? 0 : text()->paragraphs().size() - 1;
194 pos_type DocIterator::lastpos() const
196 return inMathed() ? cell().size() : paragraph().size();
200 DocIterator::idx_type DocIterator::lastidx() const
202 return top().lastidx();
206 size_t DocIterator::nargs() const
208 // assume 1x1 grid for main text
209 return top().nargs();
213 size_t DocIterator::ncols() const
215 // assume 1x1 grid for main text
216 return top().ncols();
220 size_t DocIterator::nrows() const
222 // assume 1x1 grid for main text
223 return top().nrows();
227 DocIterator::row_type DocIterator::row() const
233 DocIterator::col_type DocIterator::col() const
239 MathData & DocIterator::cell() const
241 // LASSERT(inMathed(), /**/);
246 Text * DocIterator::innerText() const
248 LASSERT(!empty(), /**/);
249 // go up until first non-0 text is hit
250 // (innermost text is 0 in mathed)
251 for (int i = depth() - 1; i >= 0; --i)
252 if (slices_[i].text())
253 return slices_[i].text();
258 Inset * DocIterator::innerInsetOfType(int code) const
260 for (int i = depth() - 1; i >= 0; --i)
261 if (slices_[i].inset_->lyxCode() == code)
262 return slices_[i].inset_;
267 // This duplicates code above, but is in the critical path.
268 // So please think twice before adding stuff
269 void DocIterator::forwardPos()
271 // this dog bites his tail
273 push_back(CursorSlice(*inset_));
277 CursorSlice & tip = top();
278 //lyxerr << "XXX\n" << *this << endl;
280 // not at cell/paragraph end?
281 if (tip.pos() != tip.lastpos()) {
282 // move into an inset to the right if possible
285 n = (tip.cell().begin() + tip.pos())->nucleus();
287 n = paragraph().getInset(tip.pos());
288 if (n && n->isActive()) {
289 //lyxerr << "... descend" << endl;
290 push_back(CursorSlice(*n));
295 // jump to the next cell/paragraph if possible
301 // otherwise leave inset and jump over inset as a whole
303 // 'tip' is invalid now...
309 void DocIterator::forwardPosIgnoreCollapsed()
311 Inset * const nextinset = nextInset();
312 // FIXME: the check for asInsetMath() shouldn't be necessary
313 // but math insets do not return a sensible editable() state yet.
314 if (nextinset && !nextinset->asInsetMath()
315 && nextinset->editable() != Inset::HIGHLY_EDITABLE) {
323 void DocIterator::forwardPar()
327 while (!empty() && (!inTexted() || pos() != 0)) {
329 pos_type const lastp = lastpos();
330 Paragraph const & par = paragraph();
331 pos_type & pos = top().pos();
332 if (par.insetList().empty())
335 while (pos < lastp && !par.isInset(pos))
343 void DocIterator::forwardChar()
346 while (!empty() && pos() == lastpos())
351 void DocIterator::forwardInset()
355 while (!empty() && !nextInset()) {
357 pos_type const lastp = lastpos();
358 Paragraph const & par = paragraph();
359 pos_type & pos = top().pos();
360 while (pos < lastp && !par.isInset(pos))
370 void DocIterator::backwardChar()
373 while (!empty() && pos() == lastpos())
378 void DocIterator::backwardPos()
380 //this dog bites his tail
382 push_back(CursorSlice(*inset_));
383 top().idx() = lastidx();
384 top().pit() = lastpit();
385 top().pos() = lastpos();
389 // at inset beginning?
390 if (top().at_begin()) {
397 // entered another cell/paragraph from the right?
398 if (top().pos() == top().lastpos())
401 // move into an inset to the left if possible
404 n = (top().cell().begin() + top().pos())->nucleus();
406 n = paragraph().getInset(top().pos());
407 if (n && n->isActive()) {
408 push_back(CursorSlice(*n));
409 top().idx() = lastidx();
410 top().pit() = lastpit();
411 top().pos() = lastpos();
416 bool DocIterator::hasPart(DocIterator const & it) const
418 // it can't be a part if it is larger
419 if (it.depth() > depth())
422 // as inset adresses are the 'last' level
423 return &it.top().inset() == &slices_[it.depth() - 1].inset();
427 void DocIterator::updateInsets(Inset * inset)
429 // this function re-creates the cache of inset pointers.
430 //lyxerr << "converting:\n" << *this << endl;
431 DocIterator dit = *this;
432 size_t const n = slices_.size();
434 for (size_t i = 0 ; i < n; ++i) {
435 LASSERT(inset, /**/);
437 top().inset_ = inset;
441 //lyxerr << "converted:\n" << *this << endl;
445 bool DocIterator::fixIfBroken()
450 // Go through the slice stack from the bottom.
451 // Check that all coordinates (idx, pit, pos) are correct and
452 // that the inset is the one which is claimed to be there
453 Inset * inset = &slices_[0].inset();
455 size_t n = slices_.size();
456 for (; i != n; ++i) {
457 CursorSlice & cs = slices_[i];
458 if (&cs.inset() != inset) {
459 // the whole slice is wrong, chop off this as well
461 LYXERR(Debug::DEBUG, "fixIfBroken(): inset changed");
463 } else if (cs.idx() > cs.lastidx()) {
464 cs.idx() = cs.lastidx();
465 cs.pit() = cs.lastpit();
466 cs.pos() = cs.lastpos();
467 LYXERR(Debug::DEBUG, "fixIfBroken(): idx fixed");
469 } else if (cs.pit() > cs.lastpit()) {
470 cs.pit() = cs.lastpit();
471 cs.pos() = cs.lastpos();
472 LYXERR(Debug::DEBUG, "fixIfBroken(): pit fixed");
474 } else if (cs.pos() > cs.lastpos()) {
475 cs.pos() = cs.lastpos();
476 LYXERR(Debug::DEBUG, "fixIfBroken(): pos fixed");
478 } else if (i != n - 1 && cs.pos() != cs.lastpos()) {
479 // get inset which is supposed to be in the next slice
480 if (cs.inset().inMathed())
481 inset = (cs.cell().begin() + cs.pos())->nucleus();
482 else if (Inset * csInset = cs.paragraph().getInset(cs.pos()))
485 // there are slices left, so there must be another inset
491 // Did we make it through the whole slice stack? Otherwise there
492 // was a problem at slice i, and we have to chop off above
494 LYXERR(Debug::DEBUG, "fixIfBroken(): cursor chopped at " << i);
502 int DocIterator::find(MathData const & cell) const
504 for (size_t l = 0; l != slices_.size(); ++l) {
505 if (slices_[l].asInsetMath() && &slices_[l].cell() == &cell)
512 int DocIterator::find(Inset const * inset) const
514 for (size_t l = 0; l != slices_.size(); ++l) {
515 if (&slices_[l].inset() == inset)
522 void DocIterator::cutOff(int above, vector<CursorSlice> & cut)
524 cut = vector<CursorSlice>(slices_.begin() + above + 1, slices_.end());
525 slices_.resize(above + 1);
529 void DocIterator::cutOff(int above)
531 slices_.resize(above + 1);
535 void DocIterator::append(vector<CursorSlice> const & x)
537 slices_.insert(slices_.end(), x.begin(), x.end());
541 void DocIterator::append(DocIterator::idx_type idx, pos_type pos)
543 slices_.push_back(CursorSlice());
549 ostream & operator<<(ostream & os, DocIterator const & dit)
551 for (size_t i = 0, n = dit.depth(); i != n; ++i)
552 os << " " << dit[i] << "\n";
557 ///////////////////////////////////////////////////////
559 StableDocIterator::StableDocIterator(DocIterator const & dit)
561 data_ = dit.internalData();
562 for (size_t i = 0, n = data_.size(); i != n; ++i)
567 DocIterator StableDocIterator::asDocIterator(Buffer * buf) const
569 // this function re-creates the cache of inset pointers
570 //lyxerr << "converting:\n" << *this << endl;
571 Inset * inset = &buf->inset();
572 DocIterator dit = DocIterator(buf, inset);
573 for (size_t i = 0, n = data_.size(); i != n; ++i) {
576 LYXERR0(" Should not happen, but does e.g. after "
577 "C-n C-l C-z S-C-z\n"
578 << " or when a Buffer has been concurrently edited by two views"
579 << '\n' << "dit: " << dit << '\n'
580 << " lastpos: " << dit.lastpos());
584 dit.push_back(data_[i]);
585 dit.top().inset_ = inset;
586 if (dit.fixIfBroken())
589 inset = dit.nextInset();
591 //lyxerr << "convert:\n" << *this << " to:\n" << dit << endl;
596 ostream & operator<<(ostream & os, StableDocIterator const & dit)
598 for (size_t i = 0, n = dit.data_.size(); i != n; ++i)
599 os << " " << dit.data_[i] << "\n";
604 bool operator==(StableDocIterator const & dit1, StableDocIterator const & dit2)
606 return dit1.data_ == dit2.data_;