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 InsetMathHull * i = dynamic_cast<InsetMathHull *>(inset().asInsetMath());
95 return i && i->getType() == hullRegexp;
99 LyXErr & operator<<(LyXErr & os, DocIterator const & it)
106 Inset * DocIterator::nextInset() const
108 LASSERT(!empty(), return 0);
109 if (pos() == lastpos())
111 if (pos() > lastpos()) {
112 LYXERR0("Should not happen, but it does: pos() = "
113 << pos() << ", lastpos() = " << lastpos());
117 return nextAtom().nucleus();
118 return paragraph().getInset(pos());
122 Inset * DocIterator::prevInset() const
124 LASSERT(!empty(), return 0);
129 // FIXME: this should not happen but it does.
131 // http://www.lyx.org/trac/ticket/3189
134 return prevAtom().nucleus();
136 return paragraph().getInset(pos() - 1);
140 Inset * DocIterator::realInset() const
142 LASSERT(inTexted(), /**/);
143 // if we are in a tabular, we need the cell
144 if (inset().lyxCode() == TABULAR_CODE) {
145 InsetTabular & tabular = static_cast<InsetTabular&>(inset());
146 return tabular.cell(idx()).get();
152 MathAtom & DocIterator::prevAtom() const
154 LASSERT(!empty(), /**/);
155 LASSERT(pos() > 0, /**/);
156 return cell()[pos() - 1];
160 MathAtom & DocIterator::nextAtom() const
162 LASSERT(!empty(), /**/);
163 //lyxerr << "lastpos: " << lastpos() << " next atom:\n" << *this << endl;
164 LASSERT(pos() < lastpos(), /**/);
165 return cell()[pos()];
169 Text * DocIterator::text() const
171 LASSERT(!empty(), /**/);
176 Paragraph & DocIterator::paragraph() const
180 LASSERT(inTexted(), /**/);
181 return top().paragraph();
185 Paragraph & DocIterator::innerParagraph() const
187 LASSERT(!empty(), /**/);
188 return innerTextSlice().paragraph();
192 CursorSlice const & DocIterator::innerTextSlice() const
194 LASSERT(!empty(), /**/);
195 // go up until first non-0 text is hit
196 // (innermost text is 0 in mathed)
197 for (int i = depth() - 1; i >= 0; --i)
198 if (slices_[i].text())
201 // This case is in principe not possible. We _must_
203 LASSERT(false, /**/);
204 static CursorSlice dummy;
209 pit_type DocIterator::lastpit() const
211 return inMathed() ? 0 : text()->paragraphs().size() - 1;
215 pos_type DocIterator::lastpos() const
217 return inMathed() ? cell().size() : paragraph().size();
221 DocIterator::idx_type DocIterator::lastidx() const
223 return top().lastidx();
227 size_t DocIterator::nargs() const
229 // assume 1x1 grid for main text
230 return top().nargs();
234 size_t DocIterator::ncols() const
236 // assume 1x1 grid for main text
237 return top().ncols();
241 size_t DocIterator::nrows() const
243 // assume 1x1 grid for main text
244 return top().nrows();
248 DocIterator::row_type DocIterator::row() const
254 DocIterator::col_type DocIterator::col() const
260 MathData & DocIterator::cell() const
262 // LASSERT(inMathed(), /**/);
267 Text * DocIterator::innerText() const
269 LASSERT(!empty(), /**/);
270 return innerTextSlice().text();
274 Inset * DocIterator::innerInsetOfType(int code) const
276 for (int i = depth() - 1; i >= 0; --i)
277 if (slices_[i].inset_->lyxCode() == code)
278 return slices_[i].inset_;
283 // This duplicates code above, but is in the critical path.
284 // So please think twice before adding stuff
285 void DocIterator::forwardPos()
287 // this dog bites his tail
289 push_back(CursorSlice(*inset_));
293 CursorSlice & tip = top();
294 //lyxerr << "XXX\n" << *this << endl;
296 // not at cell/paragraph end?
297 if (tip.pos() != tip.lastpos()) {
298 // move into an inset to the right if possible
301 n = (tip.cell().begin() + tip.pos())->nucleus();
303 n = paragraph().getInset(tip.pos());
304 if (n && n->isActive()) {
305 //lyxerr << "... descend" << endl;
306 push_back(CursorSlice(*n));
311 // jump to the next cell/paragraph if possible
317 // otherwise leave inset and jump over inset as a whole
319 // 'tip' is invalid now...
325 void DocIterator::forwardPosIgnoreCollapsed()
327 Inset * const nextinset = nextInset();
328 // FIXME: the check for asInsetMath() shouldn't be necessary
329 // but math insets do not return a sensible editable() state yet.
330 if (nextinset && !nextinset->asInsetMath()
331 && !nextinset->editable()) {
339 void DocIterator::forwardPar()
343 while (!empty() && (!inTexted() || pos() != 0)) {
345 pos_type const lastp = lastpos();
346 Paragraph const & par = paragraph();
347 pos_type & pos = top().pos();
348 if (par.insetList().empty())
351 while (pos < lastp && !par.isInset(pos))
359 void DocIterator::forwardChar()
362 while (!empty() && pos() == lastpos())
367 void DocIterator::forwardInset()
371 while (!empty() && !nextInset()) {
373 pos_type const lastp = lastpos();
374 Paragraph const & par = paragraph();
375 pos_type & pos = top().pos();
376 while (pos < lastp && !par.isInset(pos))
386 void DocIterator::backwardChar()
389 while (!empty() && pos() == lastpos())
394 void DocIterator::backwardPos()
396 //this dog bites his tail
398 push_back(CursorSlice(*inset_));
399 top().idx() = lastidx();
400 top().pit() = lastpit();
401 top().pos() = lastpos();
405 // at inset beginning?
406 if (top().at_begin()) {
413 // entered another cell/paragraph from the right?
414 if (top().pos() == top().lastpos())
417 // move into an inset to the left if possible
420 n = (top().cell().begin() + top().pos())->nucleus();
422 n = paragraph().getInset(top().pos());
423 if (n && n->isActive()) {
424 push_back(CursorSlice(*n));
425 top().idx() = lastidx();
426 top().pit() = lastpit();
427 top().pos() = lastpos();
432 bool DocIterator::hasPart(DocIterator const & it) const
434 // it can't be a part if it is larger
435 if (it.depth() > depth())
438 // as inset adresses are the 'last' level
439 return &it.top().inset() == &slices_[it.depth() - 1].inset();
443 void DocIterator::updateInsets(Inset * inset)
445 // this function re-creates the cache of inset pointers.
446 //lyxerr << "converting:\n" << *this << endl;
447 DocIterator dit = *this;
448 size_t const n = slices_.size();
450 for (size_t i = 0 ; i < n; ++i) {
451 LASSERT(inset, /**/);
453 top().inset_ = inset;
457 //lyxerr << "converted:\n" << *this << endl;
461 bool DocIterator::fixIfBroken()
466 // Go through the slice stack from the bottom.
467 // Check that all coordinates (idx, pit, pos) are correct and
468 // that the inset is the one which is claimed to be there
469 Inset * inset = &slices_[0].inset();
471 size_t n = slices_.size();
472 for (; i != n; ++i) {
473 CursorSlice & cs = slices_[i];
474 if (&cs.inset() != inset) {
475 // the whole slice is wrong, chop off this as well
477 LYXERR(Debug::DEBUG, "fixIfBroken(): inset changed");
479 } else if (cs.idx() > cs.lastidx()) {
480 cs.idx() = cs.lastidx();
481 cs.pit() = cs.lastpit();
482 cs.pos() = cs.lastpos();
483 LYXERR(Debug::DEBUG, "fixIfBroken(): idx fixed");
485 } else if (cs.pit() > cs.lastpit()) {
486 cs.pit() = cs.lastpit();
487 cs.pos() = cs.lastpos();
488 LYXERR(Debug::DEBUG, "fixIfBroken(): pit fixed");
490 } else if (cs.pos() > cs.lastpos()) {
491 cs.pos() = cs.lastpos();
492 LYXERR(Debug::DEBUG, "fixIfBroken(): pos fixed");
494 } else if (i != n - 1 && cs.pos() != cs.lastpos()) {
495 // get inset which is supposed to be in the next slice
496 if (cs.inset().inMathed())
497 inset = (cs.cell().begin() + cs.pos())->nucleus();
498 else if (Inset * csInset = cs.paragraph().getInset(cs.pos()))
501 // there are slices left, so there must be another inset
507 // Did we make it through the whole slice stack? Otherwise there
508 // was a problem at slice i, and we have to chop off above
510 LYXERR(Debug::DEBUG, "fixIfBroken(): cursor chopped at " << i);
518 int DocIterator::find(MathData const & cell) const
520 for (size_t l = 0; l != slices_.size(); ++l) {
521 if (slices_[l].asInsetMath() && &slices_[l].cell() == &cell)
528 int DocIterator::find(Inset const * inset) const
530 for (size_t l = 0; l != slices_.size(); ++l) {
531 if (&slices_[l].inset() == inset)
538 void DocIterator::cutOff(int above, vector<CursorSlice> & cut)
540 cut = vector<CursorSlice>(slices_.begin() + above + 1, slices_.end());
541 slices_.resize(above + 1);
545 void DocIterator::cutOff(int above)
547 slices_.resize(above + 1);
551 void DocIterator::append(vector<CursorSlice> const & x)
553 slices_.insert(slices_.end(), x.begin(), x.end());
557 void DocIterator::append(DocIterator::idx_type idx, pos_type pos)
559 slices_.push_back(CursorSlice());
565 ostream & operator<<(ostream & os, DocIterator const & dit)
567 for (size_t i = 0, n = dit.depth(); i != n; ++i)
568 os << " " << dit[i] << "\n";
573 ///////////////////////////////////////////////////////
575 StableDocIterator::StableDocIterator(DocIterator const & dit)
577 data_ = dit.internalData();
578 for (size_t i = 0, n = data_.size(); i != n; ++i)
583 DocIterator StableDocIterator::asDocIterator(Buffer * buf) const
585 // this function re-creates the cache of inset pointers
586 //lyxerr << "converting:\n" << *this << endl;
587 Inset * inset = &buf->inset();
588 DocIterator dit = DocIterator(buf, inset);
589 for (size_t i = 0, n = data_.size(); i != n; ++i) {
592 LYXERR0(" Should not happen, but does e.g. after "
593 "C-n C-l C-z S-C-z\n"
594 << " or when a Buffer has been concurrently edited by two views"
595 << '\n' << "dit: " << dit << '\n'
596 << " lastpos: " << dit.lastpos());
600 dit.push_back(data_[i]);
601 dit.top().inset_ = inset;
602 if (dit.fixIfBroken())
605 inset = dit.nextInset();
607 //lyxerr << "convert:\n" << *this << " to:\n" << dit << endl;
612 ostream & operator<<(ostream & os, StableDocIterator const & dit)
614 for (size_t i = 0, n = dit.data_.size(); i != n; ++i)
615 os << " " << dit.data_[i] << "\n";
620 bool operator==(StableDocIterator const & dit1, StableDocIterator const & dit2)
622 return dit1.data_ == dit2.data_;