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().getInset(pos());
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().getInset(pos() - 1);
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 // This duplicates code above, but is in the critical path.
251 // So please think twice before adding stuff
252 void DocIterator::forwardPos()
254 // this dog bites his tail
256 push_back(CursorSlice(*inset_));
260 CursorSlice & tip = top();
261 //lyxerr << "XXX\n" << *this << endl;
263 // this is used twice and shows up in the profiler!
264 pos_type const lastp = lastpos();
266 // move into an inset to the right if possible
269 if (tip.pos() != lastp) {
270 // this is impossible for pos() == size()
272 n = (tip.cell().begin() + tip.pos())->nucleus();
274 n = paragraph().getInset(tip.pos());
277 if (n && n->isActive()) {
278 //lyxerr << "... descend" << endl;
279 push_back(CursorSlice(*n));
287 // otherwise leave inset and jump over inset as a whole
289 // 'tip' is invalid now...
295 void DocIterator::forwardPosIgnoreCollapsed()
297 Inset * const nextinset = nextInset();
298 // FIXME: the check for asInsetMath() shouldn't be necessary
299 // but math insets do not return a sensible editable() state yet.
300 if (nextinset && !nextinset->asInsetMath()
301 && nextinset->editable() != Inset::HIGHLY_EDITABLE) {
309 void DocIterator::forwardPar()
313 while (!empty() && (!inTexted() || pos() != 0)) {
315 pos_type const lastp = lastpos();
316 Paragraph const & par = paragraph();
317 pos_type & pos = top().pos();
318 if (par.insetList().empty())
321 while (pos < lastp && !par.isInset(pos))
329 void DocIterator::forwardChar()
332 while (!empty() && pos() == lastpos())
337 void DocIterator::forwardInset()
341 while (!empty() && !nextInset()) {
343 pos_type const lastp = lastpos();
344 Paragraph const & par = paragraph();
345 pos_type & pos = top().pos();
346 while (pos < lastp && !par.isInset(pos))
356 void DocIterator::backwardChar()
359 while (!empty() && pos() == lastpos())
364 void DocIterator::backwardPos()
366 //this dog bites his tail
368 push_back(CursorSlice(*inset_));
369 top().idx() = lastidx();
370 top().pit() = lastpit();
371 top().pos() = lastpos();
375 if (top().at_begin()) {
382 // move into an inset to the left if possible
386 n = (top().cell().begin() + top().pos())->nucleus();
388 n = paragraph().getInset(top().pos());
390 if (n && n->isActive()) {
391 push_back(CursorSlice(*n));
392 top().idx() = lastidx();
393 top().pit() = lastpit();
394 top().pos() = lastpos();
399 bool DocIterator::hasPart(DocIterator const & it) const
401 // it can't be a part if it is larger
402 if (it.depth() > depth())
405 // as inset adresses are the 'last' level
406 return &it.top().inset() == &slices_[it.depth() - 1].inset();
410 void DocIterator::updateInsets(Inset * inset)
412 // this function re-creates the cache of inset pointers.
413 //lyxerr << "converting:\n" << *this << endl;
414 DocIterator dit = *this;
415 size_t const n = slices_.size();
417 for (size_t i = 0 ; i < n; ++i) {
420 top().inset_ = inset;
424 //lyxerr << "converted:\n" << *this << endl;
428 bool DocIterator::fixIfBroken()
430 // Go through the slice stack from the bottom.
431 // Check that all coordinates (idx, pit, pos) are correct and
432 // that the inset is the one which is claimed to be there
433 Inset * inset = &slices_[0].inset();
435 size_t n = slices_.size();
436 for (; i != n; ++i) {
437 CursorSlice & cs = slices_[i];
438 if (&cs.inset() != inset) {
439 // the whole slice is wrong, chop off this as well
441 LYXERR(Debug::DEBUG, "fixIfBroken(): inset changed");
443 } else if (cs.idx() > cs.lastidx()) {
444 cs.idx() = cs.lastidx();
445 cs.pit() = cs.lastpit();
446 cs.pos() = cs.lastpos();
447 LYXERR(Debug::DEBUG, "fixIfBroken(): idx fixed");
449 } else if (cs.pit() > cs.lastpit()) {
450 cs.pit() = cs.lastpit();
451 cs.pos() = cs.lastpos();
452 LYXERR(Debug::DEBUG, "fixIfBroken(): pit fixed");
454 } else if (cs.pos() > cs.lastpos()) {
455 cs.pos() = cs.lastpos();
456 LYXERR(Debug::DEBUG, "fixIfBroken(): pos fixed");
458 } else if (i != n - 1 && cs.pos() != cs.lastpos()) {
459 // get inset which is supposed to be in the next slice
460 if (cs.inset().inMathed())
461 inset = (cs.cell().begin() + cs.pos())->nucleus();
462 else if (Inset * csInset = cs.paragraph().getInset(cs.pos()))
465 // there are slices left, so there must be another inset
471 // Did we make it through the whole slice stack? Otherwise there
472 // was a problem at slice i, and we have to chop off above
474 LYXERR(Debug::DEBUG, "fixIfBroken(): cursor chopped at " << i);
482 int DocIterator::find(MathData const & cell) const
484 for (size_t l = 0; l != slices_.size(); ++l) {
485 if (slices_[l].asInsetMath() && &slices_[l].cell() == &cell)
492 int DocIterator::find(InsetMath const * inset) const
494 for (size_t l = 0; l != slices_.size(); ++l) {
495 if (slices_[l].asInsetMath() == inset)
502 void DocIterator::cutOff(int above, vector<CursorSlice> & cut)
504 cut = vector<CursorSlice>(slices_.begin() + above + 1, slices_.end());
505 slices_.resize(above + 1);
509 void DocIterator::cutOff(int above)
511 slices_.resize(above + 1);
515 void DocIterator::append(vector<CursorSlice> const & x)
517 slices_.insert(slices_.end(), x.begin(), x.end());
521 void DocIterator::append(DocIterator::idx_type idx, pos_type pos)
523 slices_.push_back(CursorSlice());
529 ostream & operator<<(ostream & os, DocIterator const & dit)
531 for (size_t i = 0, n = dit.depth(); i != n; ++i)
532 os << " " << dit[i] << "\n";
537 ///////////////////////////////////////////////////////
539 StableDocIterator::StableDocIterator(DocIterator const & dit)
541 data_ = dit.internalData();
542 for (size_t i = 0, n = data_.size(); i != n; ++i)
547 DocIterator StableDocIterator::asDocIterator(Inset * inset) const
549 // this function re-creates the cache of inset pointers
550 //lyxerr << "converting:\n" << *this << endl;
551 DocIterator dit = DocIterator(*inset);
552 for (size_t i = 0, n = data_.size(); i != n; ++i) {
555 LYXERR0(" Should not happen, but does e.g. after "
556 "C-n C-l C-z S-C-z\n"
557 << " or when a Buffer has been concurrently edited by two views"
558 << '\n' << "dit: " << dit << '\n'
559 << " lastpos: " << dit.lastpos());
563 dit.push_back(data_[i]);
564 dit.top().inset_ = inset;
565 if (dit.fixIfBroken())
568 inset = dit.nextInset();
570 //lyxerr << "convert:\n" << *this << " to:\n" << dit << endl;
575 ostream & operator<<(ostream & os, StableDocIterator const & dit)
577 for (size_t i = 0, n = dit.data_.size(); i != n; ++i)
578 os << " " << dit.data_[i] << "\n";
583 bool operator==(StableDocIterator const & dit1, StableDocIterator const & dit2)
585 return dit1.data_ == dit2.data_;