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() const
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() const
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 * DocIterator::realInset() const
95 BOOST_ASSERT(inTexted());
96 // if we are in a tabular, we need the cell
97 if (inset().lyxCode() == Inset::TABULAR_CODE) {
98 InsetTabular & tabular = static_cast<InsetTabular&>(inset());
99 return tabular.cell(idx()).get();
105 MathAtom & DocIterator::prevAtom() const
107 BOOST_ASSERT(!empty());
108 BOOST_ASSERT(pos() > 0);
109 return cell()[pos() - 1];
113 MathAtom & DocIterator::nextAtom() const
115 BOOST_ASSERT(!empty());
116 //lyxerr << "lastpos: " << lastpos() << " next atom:\n" << *this << endl;
117 BOOST_ASSERT(pos() < lastpos());
118 return cell()[pos()];
122 Text * DocIterator::text() const
124 BOOST_ASSERT(!empty());
129 Paragraph & DocIterator::paragraph() const
132 lyxerr << *this << endl;
133 BOOST_ASSERT(inTexted());
134 return top().paragraph();
138 Paragraph & DocIterator::innerParagraph() const
140 BOOST_ASSERT(!empty());
141 return innerTextSlice().paragraph();
145 CursorSlice const & DocIterator::innerTextSlice() const
147 BOOST_ASSERT(!empty());
148 // go up until first non-0 text is hit
149 // (innermost text is 0 in mathed)
150 for (int i = depth() - 1; i >= 0; --i)
151 if (slices_[i].text())
154 // This case is in principe not possible. We _must_
157 static CursorSlice dummy;
162 pit_type DocIterator::lastpit() const
164 return inMathed() ? 0 : text()->paragraphs().size() - 1;
168 pos_type DocIterator::lastpos() const
170 return inMathed() ? cell().size() : paragraph().size();
174 DocIterator::idx_type DocIterator::lastidx() const
176 return top().lastidx();
180 size_t DocIterator::nargs() const
182 // assume 1x1 grid for main text
183 return top().nargs();
187 size_t DocIterator::ncols() const
189 // assume 1x1 grid for main text
190 return top().ncols();
194 size_t DocIterator::nrows() const
196 // assume 1x1 grid for main text
197 return top().nrows();
201 DocIterator::row_type DocIterator::row() const
207 DocIterator::col_type DocIterator::col() const
213 MathData & DocIterator::cell() const
215 // BOOST_ASSERT(inMathed());
220 Text * DocIterator::innerText() const
222 BOOST_ASSERT(!empty());
223 // go up until first non-0 text is hit
224 // (innermost text is 0 in mathed)
225 for (int i = depth() - 1; i >= 0; --i)
226 if (slices_[i].text())
227 return slices_[i].text();
232 Inset * DocIterator::innerInsetOfType(int code) const
234 for (int i = depth() - 1; i >= 0; --i)
235 if (slices_[i].inset_->lyxCode() == code)
236 return slices_[i].inset_;
241 void DocIterator::forwardPos(bool ignorecollapsed)
243 //this dog bites his tail
245 push_back(CursorSlice(*inset_));
249 Inset * const nextinset = nextInset();
250 // jump over collapsables if they are collapsed
251 // FIXME: the check for asInsetMath() shouldn't be necessary
252 // but math insets do not return a sensible editable() state yet.
253 if (ignorecollapsed && nextinset && (!nextinset->asInsetMath()
254 && nextinset->editable() != Inset::HIGHLY_EDITABLE)) {
259 CursorSlice & tip = top();
260 //lyxerr << "XXX\n" << *this << endl;
262 // this is used twice and shows up in the profiler!
263 pos_type const lastp = lastpos();
265 // move into an inset to the right if possible
268 if (tip.pos() != lastp) {
269 // this is impossible for pos() == size()
271 n = (tip.cell().begin() + tip.pos())->nucleus();
273 if (paragraph().isInset(tip.pos()))
274 n = paragraph().getInset(tip.pos());
278 if (n && n->isActive()) {
279 //lyxerr << "... descend" << endl;
280 push_back(CursorSlice(*n));
288 // otherwise leave inset and jump over inset as a whole
290 // 'tip' is invalid now...
296 void DocIterator::forwardPar()
300 while (!empty() && (!inTexted() || pos() != 0)) {
302 pos_type const lastp = lastpos();
303 Paragraph const & par = paragraph();
304 pos_type & pos = top().pos();
305 if (par.insetlist.empty())
308 while (pos < lastp && !par.isInset(pos))
316 void DocIterator::forwardChar()
319 while (!empty() && pos() == lastpos())
324 void DocIterator::forwardInset()
328 while (!empty() && !nextInset()) {
330 pos_type const lastp = lastpos();
331 Paragraph const & par = paragraph();
332 pos_type & pos = top().pos();
333 while (pos < lastp && !par.isInset(pos))
343 void DocIterator::backwardChar()
346 while (!empty() && pos() == lastpos())
351 void DocIterator::backwardPos()
353 //this dog bites his tail
355 push_back(CursorSlice(*inset_));
356 top().idx() = lastidx();
357 top().pit() = lastpit();
358 top().pos() = lastpos();
362 if (top().at_begin()) {
369 // move into an inset to the left if possible
373 n = (top().cell().begin() + top().pos())->nucleus();
375 if (paragraph().isInset(top().pos()))
376 n = paragraph().getInset(top().pos());
379 if (n && n->isActive()) {
380 push_back(CursorSlice(*n));
381 top().idx() = lastidx();
382 top().pit() = lastpit();
383 top().pos() = lastpos();
388 bool DocIterator::hasPart(DocIterator const & it) const
390 // it can't be a part if it is larger
391 if (it.depth() > depth())
394 // as inset adresses are the 'last' level
395 return &it.top().inset() == &slices_[it.depth() - 1].inset();
399 void DocIterator::updateInsets(Inset * inset)
401 // this function re-creates the cache of inset pointers.
402 // code taken in part from StableDocIterator::asDocIterator.
403 //lyxerr << "converting:\n" << *this << endl;
404 DocIterator dit = DocIterator(*inset);
405 size_t const n = slices_.size();
406 for (size_t i = 0 ; i < n; ++i) {
408 dit.push_back(slices_[i]);
409 dit.top().inset_ = inset;
411 inset = dit.nextInset();
413 //lyxerr << "converted:\n" << *this << endl;
418 bool DocIterator::fixIfBroken()
420 // Go through the slice stack from the bottom.
421 // Check that all coordinates (idx, pit, pos) are correct and
422 // that the inset is the one which is claimed to be there
423 Inset * inset = &slices_[0].inset();
425 size_t n = slices_.size();
426 for (; i != n; ++i) {
427 CursorSlice & cs = slices_[i];
428 if (&cs.inset() != inset) {
429 // the whole slice is wrong, chop off this as well
431 LYXERR(Debug::DEBUG) << "fixIfBroken(): inset changed" << endl;
433 } else if (cs.idx() > cs.lastidx()) {
434 cs.idx() = cs.lastidx();
435 cs.pit() = cs.lastpit();
436 cs.pos() = cs.lastpos();
437 LYXERR(Debug::DEBUG) << "fixIfBroken(): idx fixed" << endl;
439 } else if (cs.pit() > cs.lastpit()) {
440 cs.pit() = cs.lastpit();
441 cs.pos() = cs.lastpos();
442 LYXERR(Debug::DEBUG) << "fixIfBroken(): pit fixed" << endl;
444 } else if (cs.pos() > cs.lastpos()) {
445 cs.pos() = cs.lastpos();
446 LYXERR(Debug::DEBUG) << "fixIfBroken(): pos fixed" << endl;
448 } else if (i != n - 1 && cs.pos() != cs.lastpos()) {
449 // get inset which is supposed to be in the next slice
450 if (cs.inset().inMathed())
451 inset = (cs.cell().begin() + cs.pos())->nucleus();
452 else if (cs.paragraph().isInset(cs.pos()))
453 inset = cs.paragraph().getInset(cs.pos());
455 // there are slices left, so there must be another inset
461 // Did we make it through the whole slice stack? Otherwise there
462 // was a problem at slice i, and we have to chop off above
464 LYXERR(Debug::DEBUG) << "fixIfBroken(): cursor chopped at " << i << endl;
472 std::ostream & operator<<(std::ostream & os, DocIterator const & dit)
474 for (size_t i = 0, n = dit.depth(); i != n; ++i)
475 os << " " << dit[i] << "\n";
480 bool operator<(DocIterator const & p, DocIterator const & q)
482 size_t depth = std::min(p.depth(), q.depth());
483 for (size_t i = 0 ; i < depth ; ++i) {
487 return p.depth() < q.depth();
491 bool operator>(DocIterator const & p, DocIterator const & q)
497 bool operator<=(DocIterator const & p, DocIterator const & q)
503 ///////////////////////////////////////////////////////
505 StableDocIterator::StableDocIterator(DocIterator const & dit)
507 data_ = dit.internalData();
508 for (size_t i = 0, n = data_.size(); i != n; ++i)
513 DocIterator StableDocIterator::asDocIterator(Inset * inset) const
515 // this function re-creates the cache of inset pointers
516 //lyxerr << "converting:\n" << *this << endl;
517 DocIterator dit = DocIterator(*inset);
518 for (size_t i = 0, n = data_.size(); i != n; ++i) {
521 lyxerr << BOOST_CURRENT_FUNCTION
522 << " Should not happen, but does e.g. after C-n C-l C-z S-C-z\n"
523 << " or when a Buffer has been concurently edited by two views"
524 << '\n' << "dit: " << dit << '\n'
525 << " lastpos: " << dit.lastpos() << endl;
529 dit.push_back(data_[i]);
530 dit.top().inset_ = inset;
531 if (dit.fixIfBroken())
534 inset = dit.nextInset();
536 //lyxerr << "convert:\n" << *this << " to:\n" << dit << endl;
541 std::ostream & operator<<(std::ostream & os, StableDocIterator const & dit)
543 for (size_t i = 0, n = dit.data_.size(); i != n; ++i)
544 os << " " << dit.data_[i] << "\n";
549 bool operator==(StableDocIterator const & dit1, StableDocIterator const & dit2)
551 return dit1.data_ == dit2.data_;