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"
19 #include "Paragraph.h"
21 #include "mathed/MathData.h"
22 #include "mathed/InsetMath.h"
24 #include "insets/InsetTabular.h"
26 #include <boost/assert.hpp>
27 #include <boost/current_function.hpp>
35 // We could be able to get rid of this if only every BufferView were
36 // associated to a buffer on construction.
37 DocIterator::DocIterator()
38 : boundary_(false), inset_(0)
42 DocIterator::DocIterator(Inset & inset)
43 : boundary_(false), inset_(&inset)
47 DocIterator doc_iterator_begin(Inset & inset)
49 DocIterator dit(inset);
55 DocIterator doc_iterator_end(Inset & inset)
57 return DocIterator(inset);
61 Inset * DocIterator::nextInset()
63 BOOST_ASSERT(!empty());
64 if (pos() == lastpos())
66 if (pos() > lastpos()) {
67 lyxerr << "Should not happen, but it does. " << endl;
71 return nextAtom().nucleus();
72 return paragraph().isInset(pos()) ? paragraph().getInset(pos()) : 0;
76 Inset * DocIterator::prevInset()
78 BOOST_ASSERT(!empty());
83 // FIXME: this should not happen but it does.
85 // http://bugzilla.lyx.org/show_bug.cgi?id=3189
88 return prevAtom().nucleus();
89 return paragraph().isInset(pos() - 1) ? paragraph().getInset(pos() - 1) : 0;
93 Inset const * DocIterator::prevInset() const
95 BOOST_ASSERT(!empty());
100 // FIXME: this should not happen but it does.
102 // http://bugzilla.lyx.org/show_bug.cgi?id=3189
105 return prevAtom().nucleus();
106 return paragraph().isInset(pos() - 1) ? paragraph().getInset(pos() - 1) : 0;
110 Inset * DocIterator::realInset() const
112 BOOST_ASSERT(inTexted());
113 // if we are in a tabular, we need the cell
114 if (inset().lyxCode() == Inset::TABULAR_CODE) {
115 InsetTabular & tabular = static_cast<InsetTabular&>(inset());
116 return tabular.cell(idx()).get();
122 MathAtom const & DocIterator::prevAtom() const
124 BOOST_ASSERT(!empty());
125 BOOST_ASSERT(pos() > 0);
126 return cell()[pos() - 1];
130 MathAtom & DocIterator::prevAtom()
132 BOOST_ASSERT(!empty());
133 BOOST_ASSERT(pos() > 0);
134 return cell()[pos() - 1];
138 MathAtom const & DocIterator::nextAtom() const
140 BOOST_ASSERT(!empty());
141 //lyxerr << "lastpos: " << lastpos() << " next atom:\n" << *this << endl;
142 BOOST_ASSERT(pos() < lastpos());
143 return cell()[pos()];
147 MathAtom & DocIterator::nextAtom()
149 BOOST_ASSERT(!empty());
150 //lyxerr << "lastpos: " << lastpos() << " next atom:\n" << *this << endl;
151 BOOST_ASSERT(pos() < lastpos());
152 return cell()[pos()];
156 Text * DocIterator::text()
158 BOOST_ASSERT(!empty());
162 Text const * DocIterator::text() const
164 BOOST_ASSERT(!empty());
169 Paragraph & DocIterator::paragraph()
172 lyxerr << *this << endl;
173 BOOST_ASSERT(inTexted());
174 return top().paragraph();
178 Paragraph const & DocIterator::paragraph() const
180 BOOST_ASSERT(inTexted());
181 return top().paragraph();
185 Paragraph const & DocIterator::innerParagraph() const
187 BOOST_ASSERT(!empty());
188 // go up until first non-0 text is hit
189 // (innermost text is 0 in mathed)
190 for (int i = depth() - 1; i >= 0; --i)
191 if (slices_[i].text())
192 return slices_[i].paragraph();
194 // This case is in principe not possible. We _must_
195 // be inside a Paragraph.
201 pit_type DocIterator::lastpit() const
203 return inMathed() ? 0 : text()->paragraphs().size() - 1;
207 pos_type DocIterator::lastpos() const
209 return inMathed() ? cell().size() : paragraph().size();
213 DocIterator::idx_type DocIterator::lastidx() const
215 return top().lastidx();
219 size_t DocIterator::nargs() const
221 // assume 1x1 grid for main text
222 return top().nargs();
226 size_t DocIterator::ncols() const
228 // assume 1x1 grid for main text
229 return top().ncols();
233 size_t DocIterator::nrows() const
235 // assume 1x1 grid for main text
236 return top().nrows();
240 DocIterator::row_type DocIterator::row() const
246 DocIterator::col_type DocIterator::col() const
252 MathData const & DocIterator::cell() const
254 // BOOST_ASSERT(inMathed());
259 MathData & DocIterator::cell()
261 // BOOST_ASSERT(inMathed());
266 Text * DocIterator::innerText()
268 BOOST_ASSERT(!empty());
269 // Go up until first non-0 text is hit
270 // (innermost text is 0 in mathed)
271 for (int i = depth() - 1; i >= 0; --i)
272 if (slices_[i].text())
273 return slices_[i].text();
277 Text const * DocIterator::innerText() const
279 BOOST_ASSERT(!empty());
280 // go up until first non-0 text is hit
281 // (innermost text is 0 in mathed)
282 for (int i = depth() - 1; i >= 0; --i)
283 if (slices_[i].text())
284 return slices_[i].text();
289 Inset * DocIterator::innerInsetOfType(int code) const
291 for (int i = depth() - 1; i >= 0; --i)
292 if (slices_[i].inset_->lyxCode() == code)
293 return slices_[i].inset_;
298 void DocIterator::forwardPos(bool ignorecollapsed)
300 //this dog bites his tail
302 push_back(CursorSlice(*inset_));
306 Inset * const nextinset = nextInset();
307 // jump over collapsables if they are collapsed
308 // FIXME: the check for asInsetMath() shouldn't be necessary
309 // but math insets do not return a sensible editable() state yet.
310 if (ignorecollapsed && nextinset && (!nextinset->asInsetMath()
311 && nextinset->editable() != Inset::HIGHLY_EDITABLE)) {
316 CursorSlice & tip = top();
317 //lyxerr << "XXX\n" << *this << endl;
319 // this is used twice and shows up in the profiler!
320 pos_type const lastp = lastpos();
322 // move into an inset to the right if possible
325 if (tip.pos() != lastp) {
326 // this is impossible for pos() == size()
328 n = (tip.cell().begin() + tip.pos())->nucleus();
330 if (paragraph().isInset(tip.pos()))
331 n = paragraph().getInset(tip.pos());
335 if (n && n->isActive()) {
336 //lyxerr << "... descend" << endl;
337 push_back(CursorSlice(*n));
345 // otherwise leave inset and jump over inset as a whole
347 // 'tip' is invalid now...
353 void DocIterator::forwardPar()
357 while (!empty() && (!inTexted() || pos() != 0)) {
359 pos_type const lastp = lastpos();
360 Paragraph const & par = paragraph();
361 pos_type & pos = top().pos();
362 if (par.insetlist.empty())
365 while (pos < lastp && !par.isInset(pos))
373 void DocIterator::forwardIdx()
375 CursorSlice & tip = top();
377 //prevent endless loops
378 BOOST_ASSERT(tip.idx() < tip.nargs());
385 void DocIterator::forwardChar()
388 while (!empty() && pos() == lastpos())
393 void DocIterator::forwardInset()
397 while (!empty() && !nextInset()) {
399 pos_type const lastp = lastpos();
400 Paragraph const & par = paragraph();
401 pos_type & pos = top().pos();
402 while (pos < lastp && !par.isInset(pos))
412 void DocIterator::backwardChar()
415 while (!empty() && pos() == lastpos())
420 void DocIterator::backwardPos()
422 //this dog bites his tail
424 push_back(CursorSlice(*inset_));
425 top().idx() = lastidx();
426 top().pit() = lastpit();
427 top().pos() = lastpos();
431 if (top().at_begin()) {
438 // move into an inset to the left if possible
442 n = (top().cell().begin() + top().pos())->nucleus();
444 if (paragraph().isInset(top().pos()))
445 n = paragraph().getInset(top().pos());
448 if (n && n->isActive()) {
449 push_back(CursorSlice(*n));
450 top().idx() = lastidx();
451 top().pit() = lastpit();
452 top().pos() = lastpos();
457 bool DocIterator::hasPart(DocIterator const & it) const
459 // it can't be a part if it is larger
460 if (it.depth() > depth())
463 // as inset adresses are the 'last' level
464 return &it.top().inset() == &slices_[it.depth() - 1].inset();
468 void DocIterator::updateInsets(Inset * inset)
470 // this function re-creates the cache of inset pointers.
471 // code taken in part from StableDocIterator::asDocIterator.
472 //lyxerr << "converting:\n" << *this << endl;
473 DocIterator dit = DocIterator(*inset);
474 size_t const n = slices_.size();
475 for (size_t i = 0 ; i < n; ++i) {
477 dit.push_back(slices_[i]);
478 dit.top().inset_ = inset;
480 inset = dit.nextInset();
482 //lyxerr << "converted:\n" << *this << endl;
487 bool DocIterator::fixIfBroken()
489 // Go through the slice stack from the bottom.
490 // Check that all coordinates (idx, pit, pos) are correct and
491 // that the inset is the one which is claimed to be there
492 Inset * inset = &slices_[0].inset();
494 size_t n = slices_.size();
495 for (; i != n; ++i) {
496 CursorSlice & cs = slices_[i];
497 if (&cs.inset() != inset) {
498 // the whole slice is wrong, chop off this as well
500 LYXERR(Debug::DEBUG) << "fixIfBroken(): inset changed" << endl;
502 } else if (cs.idx() > cs.lastidx()) {
503 cs.idx() = cs.lastidx();
504 cs.pit() = cs.lastpit();
505 cs.pos() = cs.lastpos();
506 LYXERR(Debug::DEBUG) << "fixIfBroken(): idx fixed" << endl;
508 } else if (cs.pit() > cs.lastpit()) {
509 cs.pit() = cs.lastpit();
510 cs.pos() = cs.lastpos();
511 LYXERR(Debug::DEBUG) << "fixIfBroken(): pit fixed" << endl;
513 } else if (cs.pos() > cs.lastpos()) {
514 cs.pos() = cs.lastpos();
515 LYXERR(Debug::DEBUG) << "fixIfBroken(): pos fixed" << endl;
517 } else if (i != n - 1 && cs.pos() != cs.lastpos()) {
518 // get inset which is supposed to be in the next slice
519 if (cs.inset().inMathed())
520 inset = (cs.cell().begin() + cs.pos())->nucleus();
521 else if (cs.paragraph().isInset(cs.pos()))
522 inset = cs.paragraph().getInset(cs.pos());
524 // there are slices left, so there must be another inset
530 // Did we make it through the whole slice stack? Otherwise there
531 // was a problem at slice i, and we have to chop off above
533 LYXERR(Debug::DEBUG) << "fixIfBroken(): cursor chopped at " << i << endl;
541 std::ostream & operator<<(std::ostream & os, DocIterator const & dit)
543 for (size_t i = 0, n = dit.depth(); i != n; ++i)
544 os << " " << dit[i] << "\n";
549 bool operator<(DocIterator const & p, DocIterator const & q)
551 size_t depth = std::min(p.depth(), q.depth());
552 for (size_t i = 0 ; i < depth ; ++i) {
556 return p.depth() < q.depth();
560 bool operator>(DocIterator const & p, DocIterator const & q)
566 bool operator<=(DocIterator const & p, DocIterator const & q)
572 ///////////////////////////////////////////////////////
574 StableDocIterator::StableDocIterator(DocIterator const & dit)
576 data_ = dit.internalData();
577 for (size_t i = 0, n = data_.size(); i != n; ++i)
582 DocIterator StableDocIterator::asDocIterator(Inset * inset) const
584 // this function re-creates the cache of inset pointers
585 //lyxerr << "converting:\n" << *this << endl;
586 DocIterator dit = DocIterator(*inset);
587 for (size_t i = 0, n = data_.size(); i != n; ++i) {
590 lyxerr << BOOST_CURRENT_FUNCTION
591 << " Should not happen, but does e.g. after C-n C-l C-z S-C-z\n"
592 << " or when a Buffer has been concurently edited by two views"
593 << '\n' << "dit: " << dit << '\n'
594 << " lastpos: " << dit.lastpos() << endl;
598 dit.push_back(data_[i]);
599 dit.top().inset_ = inset;
600 if (dit.fixIfBroken())
603 inset = dit.nextInset();
605 //lyxerr << "convert:\n" << *this << " to:\n" << dit << endl;
610 std::ostream & operator<<(std::ostream & os, StableDocIterator const & dit)
612 for (size_t i = 0, n = dit.data_.size(); i != n; ++i)
613 os << " " << dit.data_[i] << "\n";
618 bool operator==(StableDocIterator const & dit1, StableDocIterator const & dit2)
620 return dit1.data_ == dit2.data_;