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"
25 #include "insets/InsetTabular.h"
27 #include <boost/assert.hpp>
28 #include <boost/current_function.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 Inset * DocIterator::nextInset() const
64 BOOST_ASSERT(!empty());
65 if (pos() == lastpos())
67 if (pos() > lastpos()) {
68 lyxerr << "Should not happen, but it does. " << endl;
72 return nextAtom().nucleus();
73 return paragraph().isInset(pos()) ? paragraph().getInset(pos()) : 0;
77 Inset * DocIterator::prevInset() const
79 BOOST_ASSERT(!empty());
84 // FIXME: this should not happen but it does.
86 // http://bugzilla.lyx.org/show_bug.cgi?id=3189
89 return prevAtom().nucleus();
91 return paragraph().isInset(pos() - 1) ? paragraph().getInset(pos() - 1) : 0;
95 Inset * DocIterator::realInset() const
97 BOOST_ASSERT(inTexted());
98 // if we are in a tabular, we need the cell
99 if (inset().lyxCode() == TABULAR_CODE) {
100 InsetTabular & tabular = static_cast<InsetTabular&>(inset());
101 return tabular.cell(idx()).get();
107 MathAtom & DocIterator::prevAtom() const
109 BOOST_ASSERT(!empty());
110 BOOST_ASSERT(pos() > 0);
111 return cell()[pos() - 1];
115 MathAtom & DocIterator::nextAtom() const
117 BOOST_ASSERT(!empty());
118 //lyxerr << "lastpos: " << lastpos() << " next atom:\n" << *this << endl;
119 BOOST_ASSERT(pos() < lastpos());
120 return cell()[pos()];
124 Text * DocIterator::text() const
126 BOOST_ASSERT(!empty());
131 Paragraph & DocIterator::paragraph() const
134 lyxerr << *this << endl;
135 BOOST_ASSERT(inTexted());
136 return top().paragraph();
140 Paragraph & DocIterator::innerParagraph() const
142 BOOST_ASSERT(!empty());
143 return innerTextSlice().paragraph();
147 CursorSlice const & DocIterator::innerTextSlice() const
149 BOOST_ASSERT(!empty());
150 // go up until first non-0 text is hit
151 // (innermost text is 0 in mathed)
152 for (int i = depth() - 1; i >= 0; --i)
153 if (slices_[i].text())
156 // This case is in principe not possible. We _must_
159 static CursorSlice dummy;
164 pit_type DocIterator::lastpit() const
166 return inMathed() ? 0 : text()->paragraphs().size() - 1;
170 pos_type DocIterator::lastpos() const
172 return inMathed() ? cell().size() : paragraph().size();
176 DocIterator::idx_type DocIterator::lastidx() const
178 return top().lastidx();
182 size_t DocIterator::nargs() const
184 // assume 1x1 grid for main text
185 return top().nargs();
189 size_t DocIterator::ncols() const
191 // assume 1x1 grid for main text
192 return top().ncols();
196 size_t DocIterator::nrows() const
198 // assume 1x1 grid for main text
199 return top().nrows();
203 DocIterator::row_type DocIterator::row() const
209 DocIterator::col_type DocIterator::col() const
215 MathData & DocIterator::cell() const
217 // BOOST_ASSERT(inMathed());
222 Text * DocIterator::innerText() const
224 BOOST_ASSERT(!empty());
225 // go up until first non-0 text is hit
226 // (innermost text is 0 in mathed)
227 for (int i = depth() - 1; i >= 0; --i)
228 if (slices_[i].text())
229 return slices_[i].text();
234 Inset * DocIterator::innerInsetOfType(int code) const
236 for (int i = depth() - 1; i >= 0; --i)
237 if (slices_[i].inset_->lyxCode() == code)
238 return slices_[i].inset_;
243 void DocIterator::forwardPos(bool ignorecollapsed)
245 //this dog bites his tail
247 push_back(CursorSlice(*inset_));
251 Inset * const nextinset = nextInset();
252 // jump over collapsables if they are collapsed
253 // FIXME: the check for asInsetMath() shouldn't be necessary
254 // but math insets do not return a sensible editable() state yet.
255 if (ignorecollapsed && nextinset && (!nextinset->asInsetMath()
256 && nextinset->editable() != Inset::HIGHLY_EDITABLE)) {
261 CursorSlice & tip = top();
262 //lyxerr << "XXX\n" << *this << endl;
264 // this is used twice and shows up in the profiler!
265 pos_type const lastp = lastpos();
267 // move into an inset to the right if possible
270 if (tip.pos() != lastp) {
271 // this is impossible for pos() == size()
273 n = (tip.cell().begin() + tip.pos())->nucleus();
275 if (paragraph().isInset(tip.pos()))
276 n = paragraph().getInset(tip.pos());
280 if (n && n->isActive()) {
281 //lyxerr << "... descend" << endl;
282 push_back(CursorSlice(*n));
290 // otherwise leave inset and jump over inset as a whole
292 // 'tip' is invalid now...
298 void DocIterator::forwardPar()
302 while (!empty() && (!inTexted() || pos() != 0)) {
304 pos_type const lastp = lastpos();
305 Paragraph const & par = paragraph();
306 pos_type & pos = top().pos();
307 if (par.insetList().empty())
310 while (pos < lastp && !par.isInset(pos))
318 void DocIterator::forwardChar()
321 while (!empty() && pos() == lastpos())
326 void DocIterator::forwardInset()
330 while (!empty() && !nextInset()) {
332 pos_type const lastp = lastpos();
333 Paragraph const & par = paragraph();
334 pos_type & pos = top().pos();
335 while (pos < lastp && !par.isInset(pos))
345 void DocIterator::backwardChar()
348 while (!empty() && pos() == lastpos())
353 void DocIterator::backwardPos()
355 //this dog bites his tail
357 push_back(CursorSlice(*inset_));
358 top().idx() = lastidx();
359 top().pit() = lastpit();
360 top().pos() = lastpos();
364 if (top().at_begin()) {
371 // move into an inset to the left if possible
375 n = (top().cell().begin() + top().pos())->nucleus();
377 if (paragraph().isInset(top().pos()))
378 n = paragraph().getInset(top().pos());
381 if (n && n->isActive()) {
382 push_back(CursorSlice(*n));
383 top().idx() = lastidx();
384 top().pit() = lastpit();
385 top().pos() = lastpos();
390 bool DocIterator::hasPart(DocIterator const & it) const
392 // it can't be a part if it is larger
393 if (it.depth() > depth())
396 // as inset adresses are the 'last' level
397 return &it.top().inset() == &slices_[it.depth() - 1].inset();
401 void DocIterator::updateInsets(Inset * inset)
403 // this function re-creates the cache of inset pointers.
404 // code taken in part from StableDocIterator::asDocIterator.
405 //lyxerr << "converting:\n" << *this << endl;
406 DocIterator dit = DocIterator(*inset);
407 size_t const n = slices_.size();
408 for (size_t i = 0 ; i < n; ++i) {
410 dit.push_back(slices_[i]);
411 dit.top().inset_ = inset;
413 inset = dit.nextInset();
415 //lyxerr << "converted:\n" << *this << endl;
420 bool DocIterator::fixIfBroken()
422 // Go through the slice stack from the bottom.
423 // Check that all coordinates (idx, pit, pos) are correct and
424 // that the inset is the one which is claimed to be there
425 Inset * inset = &slices_[0].inset();
427 size_t n = slices_.size();
428 for (; i != n; ++i) {
429 CursorSlice & cs = slices_[i];
430 if (&cs.inset() != inset) {
431 // the whole slice is wrong, chop off this as well
433 LYXERR(Debug::DEBUG) << "fixIfBroken(): inset changed" << endl;
435 } else if (cs.idx() > cs.lastidx()) {
436 cs.idx() = cs.lastidx();
437 cs.pit() = cs.lastpit();
438 cs.pos() = cs.lastpos();
439 LYXERR(Debug::DEBUG) << "fixIfBroken(): idx fixed" << endl;
441 } else if (cs.pit() > cs.lastpit()) {
442 cs.pit() = cs.lastpit();
443 cs.pos() = cs.lastpos();
444 LYXERR(Debug::DEBUG) << "fixIfBroken(): pit fixed" << endl;
446 } else if (cs.pos() > cs.lastpos()) {
447 cs.pos() = cs.lastpos();
448 LYXERR(Debug::DEBUG) << "fixIfBroken(): pos fixed" << endl;
450 } else if (i != n - 1 && cs.pos() != cs.lastpos()) {
451 // get inset which is supposed to be in the next slice
452 if (cs.inset().inMathed())
453 inset = (cs.cell().begin() + cs.pos())->nucleus();
454 else if (cs.paragraph().isInset(cs.pos()))
455 inset = cs.paragraph().getInset(cs.pos());
457 // there are slices left, so there must be another inset
463 // Did we make it through the whole slice stack? Otherwise there
464 // was a problem at slice i, and we have to chop off above
466 LYXERR(Debug::DEBUG) << "fixIfBroken(): cursor chopped at " << i << endl;
474 std::ostream & operator<<(std::ostream & os, DocIterator const & dit)
476 for (size_t i = 0, n = dit.depth(); i != n; ++i)
477 os << " " << dit[i] << "\n";
482 bool operator<(DocIterator const & p, DocIterator const & q)
484 size_t depth = std::min(p.depth(), q.depth());
485 for (size_t i = 0 ; i < depth ; ++i) {
489 return p.depth() < q.depth();
493 bool operator>(DocIterator const & p, DocIterator const & q)
499 bool operator<=(DocIterator const & p, DocIterator const & q)
505 ///////////////////////////////////////////////////////
507 StableDocIterator::StableDocIterator(DocIterator const & dit)
509 data_ = dit.internalData();
510 for (size_t i = 0, n = data_.size(); i != n; ++i)
515 DocIterator StableDocIterator::asDocIterator(Inset * inset) const
517 // this function re-creates the cache of inset pointers
518 //lyxerr << "converting:\n" << *this << endl;
519 DocIterator dit = DocIterator(*inset);
520 for (size_t i = 0, n = data_.size(); i != n; ++i) {
523 lyxerr << BOOST_CURRENT_FUNCTION
524 << " Should not happen, but does e.g. after C-n C-l C-z S-C-z\n"
525 << " or when a Buffer has been concurently edited by two views"
526 << '\n' << "dit: " << dit << '\n'
527 << " lastpos: " << dit.lastpos() << endl;
531 dit.push_back(data_[i]);
532 dit.top().inset_ = inset;
533 if (dit.fixIfBroken())
536 inset = dit.nextInset();
538 //lyxerr << "convert:\n" << *this << " to:\n" << dit << endl;
543 std::ostream & operator<<(std::ostream & os, StableDocIterator const & dit)
545 for (size_t i = 0, n = dit.data_.size(); i != n; ++i)
546 os << " " << dit.data_[i] << "\n";
551 bool operator==(StableDocIterator const & dit1, StableDocIterator const & dit2)
553 return dit1.data_ == dit2.data_;