2 * \file DocIterator.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
14 #include "DocIterator.h"
18 #include "Paragraph.h"
20 #include "mathed/MathData.h"
21 #include "mathed/InsetMath.h"
23 #include "insets/InsetTabular.h"
25 #include <boost/assert.hpp>
26 #include <boost/current_function.hpp>
34 // We could be able to get rid of this if only every BufferView were
35 // associated to a buffer on construction.
36 DocIterator::DocIterator()
37 : boundary_(false), inset_(0)
41 DocIterator::DocIterator(Inset & inset)
42 : boundary_(false), inset_(&inset)
46 DocIterator doc_iterator_begin(Inset & inset)
48 DocIterator dit(inset);
54 DocIterator doc_iterator_end(Inset & inset)
56 return DocIterator(inset);
60 Inset * DocIterator::nextInset()
62 BOOST_ASSERT(!empty());
63 if (pos() == lastpos())
65 if (pos() > lastpos()) {
66 lyxerr << "Should not happen, but it does. " << endl;
70 return nextAtom().nucleus();
71 return paragraph().isInset(pos()) ? paragraph().getInset(pos()) : 0;
75 Inset * DocIterator::prevInset()
77 BOOST_ASSERT(!empty());
82 // FIXME: this should not happen but it does.
84 // http://bugzilla.lyx.org/show_bug.cgi?id=3189
87 return prevAtom().nucleus();
88 return paragraph().isInset(pos() - 1) ? paragraph().getInset(pos() - 1) : 0;
92 Inset const * DocIterator::prevInset() const
94 BOOST_ASSERT(!empty());
99 // FIXME: this should not happen but it does.
101 // http://bugzilla.lyx.org/show_bug.cgi?id=3189
104 return prevAtom().nucleus();
105 return paragraph().isInset(pos() - 1) ? paragraph().getInset(pos() - 1) : 0;
109 Inset * DocIterator::realInset() const
111 BOOST_ASSERT(inTexted());
112 // if we are in a tabular, we need the cell
113 if (inset().lyxCode() == Inset::TABULAR_CODE) {
114 InsetTabular & tabular = static_cast<InsetTabular&>(inset());
115 return tabular.cell(idx()).get();
121 MathAtom const & DocIterator::prevAtom() const
123 BOOST_ASSERT(!empty());
124 BOOST_ASSERT(pos() > 0);
125 return cell()[pos() - 1];
129 MathAtom & DocIterator::prevAtom()
131 BOOST_ASSERT(!empty());
132 BOOST_ASSERT(pos() > 0);
133 return cell()[pos() - 1];
137 MathAtom const & DocIterator::nextAtom() const
139 BOOST_ASSERT(!empty());
140 //lyxerr << "lastpos: " << lastpos() << " next atom:\n" << *this << endl;
141 BOOST_ASSERT(pos() < lastpos());
142 return cell()[pos()];
146 MathAtom & DocIterator::nextAtom()
148 BOOST_ASSERT(!empty());
149 //lyxerr << "lastpos: " << lastpos() << " next atom:\n" << *this << endl;
150 BOOST_ASSERT(pos() < lastpos());
151 return cell()[pos()];
155 Text * DocIterator::text()
157 BOOST_ASSERT(!empty());
161 Text const * DocIterator::text() const
163 BOOST_ASSERT(!empty());
168 Paragraph & DocIterator::paragraph()
171 lyxerr << *this << endl;
172 BOOST_ASSERT(inTexted());
173 return top().paragraph();
177 Paragraph const & DocIterator::paragraph() const
179 BOOST_ASSERT(inTexted());
180 return top().paragraph();
184 Paragraph const & DocIterator::innerParagraph() const
186 BOOST_ASSERT(!empty());
187 // go up until first non-0 text is hit
188 // (innermost text is 0 in mathed)
189 for (int i = depth() - 1; i >= 0; --i)
190 if (slices_[i].text())
191 return slices_[i].paragraph();
193 // This case is in principe not possible. We _must_
194 // be inside a Paragraph.
200 pit_type DocIterator::lastpit() const
202 return inMathed() ? 0 : text()->paragraphs().size() - 1;
206 pos_type DocIterator::lastpos() const
208 return inMathed() ? cell().size() : paragraph().size();
212 DocIterator::idx_type DocIterator::lastidx() const
214 return top().lastidx();
218 size_t DocIterator::nargs() const
220 // assume 1x1 grid for main text
221 return top().nargs();
225 size_t DocIterator::ncols() const
227 // assume 1x1 grid for main text
228 return top().ncols();
232 size_t DocIterator::nrows() const
234 // assume 1x1 grid for main text
235 return top().nrows();
239 DocIterator::row_type DocIterator::row() const
245 DocIterator::col_type DocIterator::col() const
251 MathData const & DocIterator::cell() const
253 // BOOST_ASSERT(inMathed());
258 MathData & DocIterator::cell()
260 // BOOST_ASSERT(inMathed());
265 Text * DocIterator::innerText()
267 BOOST_ASSERT(!empty());
268 // Go up until first non-0 text is hit
269 // (innermost text is 0 in mathed)
270 for (int i = depth() - 1; i >= 0; --i)
271 if (slices_[i].text())
272 return slices_[i].text();
276 Text const * DocIterator::innerText() const
278 BOOST_ASSERT(!empty());
279 // go up until first non-0 text is hit
280 // (innermost text is 0 in mathed)
281 for (int i = depth() - 1; i >= 0; --i)
282 if (slices_[i].text())
283 return slices_[i].text();
288 Inset * DocIterator::innerInsetOfType(int code) const
290 for (int i = depth() - 1; i >= 0; --i)
291 if (slices_[i].inset_->lyxCode() == code)
292 return slices_[i].inset_;
297 void DocIterator::forwardPos(bool ignorecollapsed)
299 //this dog bites his tail
301 push_back(CursorSlice(*inset_));
305 Inset * const nextinset = nextInset();
306 // jump over collapsables if they are collapsed
307 // FIXME: the check for asInsetMath() shouldn't be necessary
308 // but math insets do not return a sensible editable() state yet.
309 if (ignorecollapsed && nextinset && (!nextinset->asInsetMath()
310 && nextinset->editable() != Inset::HIGHLY_EDITABLE)) {
315 CursorSlice & tip = top();
316 //lyxerr << "XXX\n" << *this << endl;
318 // this is used twice and shows up in the profiler!
319 pos_type const lastp = lastpos();
321 // move into an inset to the right if possible
324 if (tip.pos() != lastp) {
325 // this is impossible for pos() == size()
327 n = (tip.cell().begin() + tip.pos())->nucleus();
329 if (paragraph().isInset(tip.pos()))
330 n = paragraph().getInset(tip.pos());
334 if (n && n->isActive()) {
335 //lyxerr << "... descend" << endl;
336 push_back(CursorSlice(*n));
340 // otherwise move on one position if possible
341 if (tip.pos() < lastp) {
342 //lyxerr << "... next pos" << endl;
346 //lyxerr << "... no next pos" << endl;
348 // otherwise move on one paragraph if possible
349 if (tip.pit() < lastpit()) {
350 //lyxerr << "... next par" << endl;
355 //lyxerr << "... no next pit" << endl;
357 // otherwise try to move on one cell if possible
358 if (tip.idx() < lastidx()) {
359 //lyxerr << "... next idx" << endl;
365 //lyxerr << "... no next idx" << endl;
367 // otherwise leave inset and jump over inset as a whole
369 // 'top' is invalid now...
375 void DocIterator::forwardPosNoDescend()
377 CursorSlice & tip = top();
378 pos_type const lastp = lastpos();
380 // move on one position if possible
381 if (tip.pos() < lastp) {
382 //lyxerr << "... next pos" << endl;
386 //lyxerr << "... no next pos" << endl;
388 // otherwise move on one paragraph if possible
389 if (tip.pit() < lastpit()) {
390 //lyxerr << "... next par" << endl;
395 //lyxerr << "... no next pit" << endl;
397 // otherwise try to move on one cell if possible
398 if (tip.idx() < lastidx()) {
399 //lyxerr << "... next idx" << endl;
405 //lyxerr << "... no next idx" << endl;
407 // otherwise leave inset and jump over inset as a whole
409 // 'top' is invalid now...
415 void DocIterator::forwardPar()
419 while (!empty() && (!inTexted() || pos() != 0)) {
421 pos_type const lastp = lastpos();
422 Paragraph const & par = paragraph();
423 pos_type & pos = top().pos();
424 if (par.insetlist.empty())
427 while (pos < lastp && !par.isInset(pos))
435 void DocIterator::forwardIdx()
437 CursorSlice & tip = top();
439 //prevent endless loops
440 BOOST_ASSERT(tip.idx() < lastidx());
448 void DocIterator::forwardChar()
451 while (!empty() && pos() == lastpos())
456 void DocIterator::forwardInset()
460 while (!empty() && !nextInset()) {
462 pos_type const lastp = lastpos();
463 Paragraph const & par = paragraph();
464 pos_type & pos = top().pos();
465 while (pos < lastp && !par.isInset(pos))
475 void DocIterator::backwardChar()
478 while (!empty() && pos() == lastpos())
483 void DocIterator::backwardPos()
485 //this dog bites his tail
487 push_back(CursorSlice(*inset_));
488 top().idx() = lastidx();
489 top().pit() = lastpit();
490 top().pos() = lastpos();
494 CursorSlice & tip = top();
496 if (tip.pos() != 0) {
498 } else if (tip.pit() != 0) {
500 tip.pos() = lastpos();
502 } else if (tip.idx() != 0) {
504 tip.pit() = lastpit();
505 tip.pos() = lastpos();
512 // move into an inset to the left if possible
516 n = (tip.cell().begin() + tip.pos())->nucleus();
518 if (paragraph().isInset(tip.pos()))
519 n = paragraph().getInset(tip.pos());
522 if (n && n->isActive()) {
523 push_back(CursorSlice(*n));
524 top().idx() = lastidx();
525 top().pit() = lastpit();
526 top().pos() = lastpos();
531 bool DocIterator::hasPart(DocIterator const & it) const
533 // it can't be a part if it is larger
534 if (it.depth() > depth())
537 // as inset adresses are the 'last' level
538 return &it.top().inset() == &slices_[it.depth() - 1].inset();
542 void DocIterator::updateInsets(Inset * inset)
544 // this function re-creates the cache of inset pointers.
545 // code taken in part from StableDocIterator::asDocIterator.
546 //lyxerr << "converting:\n" << *this << endl;
547 DocIterator dit = DocIterator(*inset);
548 size_t const n = slices_.size();
549 for (size_t i = 0 ; i < n; ++i) {
551 dit.push_back(slices_[i]);
552 dit.top().inset_ = inset;
554 inset = dit.nextInset();
556 //lyxerr << "converted:\n" << *this << endl;
561 bool DocIterator::fixIfBroken()
565 for (size_t i = slices_.size() - 1; i != 0; --i)
566 if (!slices_[i].isValid()) {
571 // The top level CursorSlice should always be valid.
572 BOOST_ASSERT(slices_[0].isValid());
574 if (idx() > lastidx()) {
575 lyxerr << "wrong idx " << idx()
576 << ", max is " << lastidx()
577 << " at level " << depth()
578 << ". Trying to correct this." << endl;
579 lyxerr << "old: " << *this << endl;
580 for (size_t i = idx(); i != lastidx(); --i)
587 else if (pit() > lastpit()) {
588 lyxerr << "wrong pit " << pit()
589 << ", max is " << lastpit()
590 << " at level " << depth()
591 << ". Trying to correct this." << endl;
592 lyxerr << "old: " << *this << endl;
597 else if (pos() > lastpos()) {
598 lyxerr << "wrong pos " << pos()
599 << ", max is " << lastpos()
600 << " at level " << depth()
601 << ". Trying to correct this." << endl;
602 lyxerr << "old: " << *this << endl;
607 lyxerr << "new: " << *this << endl;
613 std::ostream & operator<<(std::ostream & os, DocIterator const & dit)
615 for (size_t i = 0, n = dit.depth(); i != n; ++i)
616 os << " " << dit[i] << "\n";
621 bool operator<(DocIterator const & p, DocIterator const & q)
623 size_t depth = std::min(p.depth(), q.depth());
624 for (size_t i = 0 ; i < depth ; ++i) {
628 return p.depth() < q.depth();
632 bool operator>(DocIterator const & p, DocIterator const & q)
638 bool operator<=(DocIterator const & p, DocIterator const & q)
644 ///////////////////////////////////////////////////////
646 StableDocIterator::StableDocIterator(DocIterator const & dit)
648 data_ = dit.internalData();
649 for (size_t i = 0, n = data_.size(); i != n; ++i)
654 DocIterator StableDocIterator::asDocIterator(Inset * inset) const
656 // this function re-creates the cache of inset pointers
657 //lyxerr << "converting:\n" << *this << endl;
658 DocIterator dit = DocIterator(*inset);
659 for (size_t i = 0, n = data_.size(); i != n; ++i) {
662 lyxerr << BOOST_CURRENT_FUNCTION
663 << " Should not happen, but does e.g. after C-n C-l C-z S-C-z\n"
664 << " or when a Buffer has been concurently edited by two views"
665 << '\n' << "dit: " << dit << '\n'
666 << " lastpos: " << dit.lastpos() << endl;
670 dit.push_back(data_[i]);
671 dit.top().inset_ = inset;
672 if (dit.fixIfBroken())
675 inset = dit.nextInset();
677 //lyxerr << "convert:\n" << *this << " to:\n" << dit << endl;
682 std::ostream & operator<<(std::ostream & os, StableDocIterator const & dit)
684 for (size_t i = 0, n = dit.data_.size(); i != n; ++i)
685 os << " " << dit.data_[i] << "\n";
690 bool operator==(StableDocIterator const & dit1, StableDocIterator const & dit2)
692 return dit1.data_ == dit2.data_;