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 "support/debug.h"
18 #include "InsetList.h"
19 #include "Paragraph.h"
22 #include "mathed/MathData.h"
23 #include "mathed/InsetMath.h"
25 #include "insets/InsetTabular.h"
27 #include <boost/assert.hpp>
36 // We could be able to get rid of this if only every BufferView were
37 // associated to a buffer on construction.
38 DocIterator::DocIterator()
39 : boundary_(false), inset_(0)
43 DocIterator::DocIterator(Inset & inset)
44 : boundary_(false), inset_(&inset)
48 DocIterator doc_iterator_begin(Inset & inset)
50 DocIterator dit(inset);
56 DocIterator doc_iterator_end(Inset & inset)
58 return DocIterator(inset);
62 LyXErr & operator<<(LyXErr & os, DocIterator const & it)
69 Inset * DocIterator::nextInset() const
71 BOOST_ASSERT(!empty());
72 if (pos() == lastpos())
74 if (pos() > lastpos()) {
75 LYXERR0("Should not happen, but it does. ");
79 return nextAtom().nucleus();
80 return paragraph().isInset(pos()) ? paragraph().getInset(pos()) : 0;
84 Inset * DocIterator::prevInset() const
86 BOOST_ASSERT(!empty());
91 // FIXME: this should not happen but it does.
93 // http://bugzilla.lyx.org/show_bug.cgi?id=3189
96 return prevAtom().nucleus();
98 return paragraph().isInset(pos() - 1) ? paragraph().getInset(pos() - 1) : 0;
102 Inset * DocIterator::realInset() const
104 BOOST_ASSERT(inTexted());
105 // if we are in a tabular, we need the cell
106 if (inset().lyxCode() == TABULAR_CODE) {
107 InsetTabular & tabular = static_cast<InsetTabular&>(inset());
108 return tabular.cell(idx()).get();
114 MathAtom & DocIterator::prevAtom() const
116 BOOST_ASSERT(!empty());
117 BOOST_ASSERT(pos() > 0);
118 return cell()[pos() - 1];
122 MathAtom & DocIterator::nextAtom() const
124 BOOST_ASSERT(!empty());
125 //lyxerr << "lastpos: " << lastpos() << " next atom:\n" << *this << endl;
126 BOOST_ASSERT(pos() < lastpos());
127 return cell()[pos()];
131 Text * DocIterator::text() const
133 BOOST_ASSERT(!empty());
138 Paragraph & DocIterator::paragraph() const
142 BOOST_ASSERT(inTexted());
143 return top().paragraph();
147 Paragraph & DocIterator::innerParagraph() const
149 BOOST_ASSERT(!empty());
150 return innerTextSlice().paragraph();
154 CursorSlice const & DocIterator::innerTextSlice() const
156 BOOST_ASSERT(!empty());
157 // go up until first non-0 text is hit
158 // (innermost text is 0 in mathed)
159 for (int i = depth() - 1; i >= 0; --i)
160 if (slices_[i].text())
163 // This case is in principe not possible. We _must_
166 static CursorSlice dummy;
171 pit_type DocIterator::lastpit() const
173 return inMathed() ? 0 : text()->paragraphs().size() - 1;
177 pos_type DocIterator::lastpos() const
179 return inMathed() ? cell().size() : paragraph().size();
183 DocIterator::idx_type DocIterator::lastidx() const
185 return top().lastidx();
189 size_t DocIterator::nargs() const
191 // assume 1x1 grid for main text
192 return top().nargs();
196 size_t DocIterator::ncols() const
198 // assume 1x1 grid for main text
199 return top().ncols();
203 size_t DocIterator::nrows() const
205 // assume 1x1 grid for main text
206 return top().nrows();
210 DocIterator::row_type DocIterator::row() const
216 DocIterator::col_type DocIterator::col() const
222 MathData & DocIterator::cell() const
224 // BOOST_ASSERT(inMathed());
229 Text * DocIterator::innerText() const
231 BOOST_ASSERT(!empty());
232 // go up until first non-0 text is hit
233 // (innermost text is 0 in mathed)
234 for (int i = depth() - 1; i >= 0; --i)
235 if (slices_[i].text())
236 return slices_[i].text();
241 Inset * DocIterator::innerInsetOfType(int code) const
243 for (int i = depth() - 1; i >= 0; --i)
244 if (slices_[i].inset_->lyxCode() == code)
245 return slices_[i].inset_;
250 void DocIterator::forwardPos(bool ignorecollapsed)
252 //this dog bites his tail
254 push_back(CursorSlice(*inset_));
258 Inset * const nextinset = nextInset();
259 // jump over collapsables if they are collapsed
260 // FIXME: the check for asInsetMath() shouldn't be necessary
261 // but math insets do not return a sensible editable() state yet.
262 if (ignorecollapsed && nextinset && (!nextinset->asInsetMath()
263 && nextinset->editable() != Inset::HIGHLY_EDITABLE)) {
268 CursorSlice & tip = top();
269 //lyxerr << "XXX\n" << *this << endl;
271 // this is used twice and shows up in the profiler!
272 pos_type const lastp = lastpos();
274 // move into an inset to the right if possible
277 if (tip.pos() != lastp) {
278 // this is impossible for pos() == size()
280 n = (tip.cell().begin() + tip.pos())->nucleus();
282 if (paragraph().isInset(tip.pos()))
283 n = paragraph().getInset(tip.pos());
287 if (n && n->isActive()) {
288 //lyxerr << "... descend" << endl;
289 push_back(CursorSlice(*n));
297 // otherwise leave inset and jump over inset as a whole
299 // 'tip' is invalid now...
305 void DocIterator::forwardPar()
309 while (!empty() && (!inTexted() || pos() != 0)) {
311 pos_type const lastp = lastpos();
312 Paragraph const & par = paragraph();
313 pos_type & pos = top().pos();
314 if (par.insetList().empty())
317 while (pos < lastp && !par.isInset(pos))
325 void DocIterator::forwardChar()
328 while (!empty() && pos() == lastpos())
333 void DocIterator::forwardInset()
337 while (!empty() && !nextInset()) {
339 pos_type const lastp = lastpos();
340 Paragraph const & par = paragraph();
341 pos_type & pos = top().pos();
342 while (pos < lastp && !par.isInset(pos))
352 void DocIterator::backwardChar()
355 while (!empty() && pos() == lastpos())
360 void DocIterator::backwardPos()
362 //this dog bites his tail
364 push_back(CursorSlice(*inset_));
365 top().idx() = lastidx();
366 top().pit() = lastpit();
367 top().pos() = lastpos();
371 if (top().at_begin()) {
378 // move into an inset to the left if possible
382 n = (top().cell().begin() + top().pos())->nucleus();
384 if (paragraph().isInset(top().pos()))
385 n = paragraph().getInset(top().pos());
388 if (n && n->isActive()) {
389 push_back(CursorSlice(*n));
390 top().idx() = lastidx();
391 top().pit() = lastpit();
392 top().pos() = lastpos();
397 bool DocIterator::hasPart(DocIterator const & it) const
399 // it can't be a part if it is larger
400 if (it.depth() > depth())
403 // as inset adresses are the 'last' level
404 return &it.top().inset() == &slices_[it.depth() - 1].inset();
408 void DocIterator::updateInsets(Inset * inset)
410 // this function re-creates the cache of inset pointers.
411 //lyxerr << "converting:\n" << *this << endl;
412 DocIterator dit = *this;
413 size_t const n = slices_.size();
415 for (size_t i = 0 ; i < n; ++i) {
418 top().inset_ = inset;
422 //lyxerr << "converted:\n" << *this << endl;
426 bool DocIterator::fixIfBroken()
428 // Go through the slice stack from the bottom.
429 // Check that all coordinates (idx, pit, pos) are correct and
430 // that the inset is the one which is claimed to be there
431 Inset * inset = &slices_[0].inset();
433 size_t n = slices_.size();
434 for (; i != n; ++i) {
435 CursorSlice & cs = slices_[i];
436 if (&cs.inset() != inset) {
437 // the whole slice is wrong, chop off this as well
439 LYXERR(Debug::DEBUG, "fixIfBroken(): inset changed");
441 } else if (cs.idx() > cs.lastidx()) {
442 cs.idx() = cs.lastidx();
443 cs.pit() = cs.lastpit();
444 cs.pos() = cs.lastpos();
445 LYXERR(Debug::DEBUG, "fixIfBroken(): idx fixed");
447 } else if (cs.pit() > cs.lastpit()) {
448 cs.pit() = cs.lastpit();
449 cs.pos() = cs.lastpos();
450 LYXERR(Debug::DEBUG, "fixIfBroken(): pit fixed");
452 } else if (cs.pos() > cs.lastpos()) {
453 cs.pos() = cs.lastpos();
454 LYXERR(Debug::DEBUG, "fixIfBroken(): pos fixed");
456 } else if (i != n - 1 && cs.pos() != cs.lastpos()) {
457 // get inset which is supposed to be in the next slice
458 if (cs.inset().inMathed())
459 inset = (cs.cell().begin() + cs.pos())->nucleus();
460 else if (cs.paragraph().isInset(cs.pos()))
461 inset = cs.paragraph().getInset(cs.pos());
463 // there are slices left, so there must be another inset
469 // Did we make it through the whole slice stack? Otherwise there
470 // was a problem at slice i, and we have to chop off above
472 LYXERR(Debug::DEBUG, "fixIfBroken(): cursor chopped at " << i);
480 DocIterator::idx_type DocIterator::find(MathData const & cell) const
482 for (size_t l = 0; l != slices_.size(); ++l) {
483 if (slices_[l].asInsetMath() && &slices_[l].cell() == &cell)
490 DocIterator::idx_type DocIterator::find(InsetMath const * inset) const
492 for (size_t l = 0; l != slices_.size(); ++l) {
493 if (slices_[l].asInsetMath() == inset)
500 void DocIterator::cutOff(DocIterator::idx_type above, vector<CursorSlice> & cut)
502 cut = vector<CursorSlice>(slices_.begin() + above + 1, slices_.end());
503 slices_.resize(above + 1);
507 void DocIterator::cutOff(DocIterator::idx_type above)
509 slices_.resize(above + 1);
513 void DocIterator::append(vector<CursorSlice> const & x)
515 slices_.insert(slices_.end(), x.begin(), x.end());
519 void DocIterator::append(DocIterator::idx_type idx, pos_type pos)
521 slices_.push_back(CursorSlice());
527 ostream & operator<<(ostream & os, DocIterator const & dit)
529 for (size_t i = 0, n = dit.depth(); i != n; ++i)
530 os << " " << dit[i] << "\n";
535 ///////////////////////////////////////////////////////
537 StableDocIterator::StableDocIterator(DocIterator const & dit)
539 data_ = dit.internalData();
540 for (size_t i = 0, n = data_.size(); i != n; ++i)
545 DocIterator StableDocIterator::asDocIterator(Inset * inset) const
547 // this function re-creates the cache of inset pointers
548 //lyxerr << "converting:\n" << *this << endl;
549 DocIterator dit = DocIterator(*inset);
550 for (size_t i = 0, n = data_.size(); i != n; ++i) {
553 LYXERR0(" Should not happen, but does e.g. after "
554 "C-n C-l C-z S-C-z\n"
555 << " or when a Buffer has been concurrently edited by two views"
556 << '\n' << "dit: " << dit << '\n'
557 << " lastpos: " << dit.lastpos());
561 dit.push_back(data_[i]);
562 dit.top().inset_ = inset;
563 if (dit.fixIfBroken())
566 inset = dit.nextInset();
568 //lyxerr << "convert:\n" << *this << " to:\n" << dit << endl;
573 ostream & operator<<(ostream & os, StableDocIterator const & dit)
575 for (size_t i = 0, n = dit.data_.size(); i != n; ++i)
576 os << " " << dit.data_[i] << "\n";
581 bool operator==(StableDocIterator const & dit1, StableDocIterator const & dit2)
583 return dit1.data_ == dit2.data_;