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 // go up until first non-0 text is hit
142 // (innermost text is 0 in mathed)
143 for (int i = depth() - 1; i >= 0; --i)
144 if (slices_[i].text())
145 return slices_[i].paragraph();
147 // This case is in principe not possible. We _must_
148 // be inside a Paragraph.
154 pit_type DocIterator::lastpit() const
156 return inMathed() ? 0 : text()->paragraphs().size() - 1;
160 pos_type DocIterator::lastpos() const
162 return inMathed() ? cell().size() : paragraph().size();
166 DocIterator::idx_type DocIterator::lastidx() const
168 return top().lastidx();
172 size_t DocIterator::nargs() const
174 // assume 1x1 grid for main text
175 return top().nargs();
179 size_t DocIterator::ncols() const
181 // assume 1x1 grid for main text
182 return top().ncols();
186 size_t DocIterator::nrows() const
188 // assume 1x1 grid for main text
189 return top().nrows();
193 DocIterator::row_type DocIterator::row() const
199 DocIterator::col_type DocIterator::col() const
205 MathData & DocIterator::cell() const
207 // BOOST_ASSERT(inMathed());
212 Text * DocIterator::innerText() const
214 BOOST_ASSERT(!empty());
215 // go up until first non-0 text is hit
216 // (innermost text is 0 in mathed)
217 for (int i = depth() - 1; i >= 0; --i)
218 if (slices_[i].text())
219 return slices_[i].text();
224 Inset * DocIterator::innerInsetOfType(int code) const
226 for (int i = depth() - 1; i >= 0; --i)
227 if (slices_[i].inset_->lyxCode() == code)
228 return slices_[i].inset_;
233 void DocIterator::forwardPos(bool ignorecollapsed)
235 //this dog bites his tail
237 push_back(CursorSlice(*inset_));
241 Inset * const nextinset = nextInset();
242 // jump over collapsables if they are collapsed
243 // FIXME: the check for asInsetMath() shouldn't be necessary
244 // but math insets do not return a sensible editable() state yet.
245 if (ignorecollapsed && nextinset && (!nextinset->asInsetMath()
246 && nextinset->editable() != Inset::HIGHLY_EDITABLE)) {
251 CursorSlice & tip = top();
252 //lyxerr << "XXX\n" << *this << endl;
254 // this is used twice and shows up in the profiler!
255 pos_type const lastp = lastpos();
257 // move into an inset to the right if possible
260 if (tip.pos() != lastp) {
261 // this is impossible for pos() == size()
263 n = (tip.cell().begin() + tip.pos())->nucleus();
265 if (paragraph().isInset(tip.pos()))
266 n = paragraph().getInset(tip.pos());
270 if (n && n->isActive()) {
271 //lyxerr << "... descend" << endl;
272 push_back(CursorSlice(*n));
280 // otherwise leave inset and jump over inset as a whole
282 // 'tip' is invalid now...
288 void DocIterator::forwardPar()
292 while (!empty() && (!inTexted() || pos() != 0)) {
294 pos_type const lastp = lastpos();
295 Paragraph const & par = paragraph();
296 pos_type & pos = top().pos();
297 if (par.insetlist.empty())
300 while (pos < lastp && !par.isInset(pos))
308 void DocIterator::forwardChar()
311 while (!empty() && pos() == lastpos())
316 void DocIterator::forwardInset()
320 while (!empty() && !nextInset()) {
322 pos_type const lastp = lastpos();
323 Paragraph const & par = paragraph();
324 pos_type & pos = top().pos();
325 while (pos < lastp && !par.isInset(pos))
335 void DocIterator::backwardChar()
338 while (!empty() && pos() == lastpos())
343 void DocIterator::backwardPos()
345 //this dog bites his tail
347 push_back(CursorSlice(*inset_));
348 top().idx() = lastidx();
349 top().pit() = lastpit();
350 top().pos() = lastpos();
354 if (top().at_begin()) {
361 // move into an inset to the left if possible
365 n = (top().cell().begin() + top().pos())->nucleus();
367 if (paragraph().isInset(top().pos()))
368 n = paragraph().getInset(top().pos());
371 if (n && n->isActive()) {
372 push_back(CursorSlice(*n));
373 top().idx() = lastidx();
374 top().pit() = lastpit();
375 top().pos() = lastpos();
380 bool DocIterator::hasPart(DocIterator const & it) const
382 // it can't be a part if it is larger
383 if (it.depth() > depth())
386 // as inset adresses are the 'last' level
387 return &it.top().inset() == &slices_[it.depth() - 1].inset();
391 void DocIterator::updateInsets(Inset * inset)
393 // this function re-creates the cache of inset pointers.
394 // code taken in part from StableDocIterator::asDocIterator.
395 //lyxerr << "converting:\n" << *this << endl;
396 DocIterator dit = DocIterator(*inset);
397 size_t const n = slices_.size();
398 for (size_t i = 0 ; i < n; ++i) {
400 dit.push_back(slices_[i]);
401 dit.top().inset_ = inset;
403 inset = dit.nextInset();
405 //lyxerr << "converted:\n" << *this << endl;
410 bool DocIterator::fixIfBroken()
412 // Go through the slice stack from the bottom.
413 // Check that all coordinates (idx, pit, pos) are correct and
414 // that the inset is the one which is claimed to be there
415 Inset * inset = &slices_[0].inset();
417 size_t n = slices_.size();
418 for (; i != n; ++i) {
419 CursorSlice & cs = slices_[i];
420 if (&cs.inset() != inset) {
421 // the whole slice is wrong, chop off this as well
423 LYXERR(Debug::DEBUG) << "fixIfBroken(): inset changed" << endl;
425 } else if (cs.idx() > cs.lastidx()) {
426 cs.idx() = cs.lastidx();
427 cs.pit() = cs.lastpit();
428 cs.pos() = cs.lastpos();
429 LYXERR(Debug::DEBUG) << "fixIfBroken(): idx fixed" << endl;
431 } else if (cs.pit() > cs.lastpit()) {
432 cs.pit() = cs.lastpit();
433 cs.pos() = cs.lastpos();
434 LYXERR(Debug::DEBUG) << "fixIfBroken(): pit fixed" << endl;
436 } else if (cs.pos() > cs.lastpos()) {
437 cs.pos() = cs.lastpos();
438 LYXERR(Debug::DEBUG) << "fixIfBroken(): pos fixed" << endl;
440 } else if (i != n - 1 && cs.pos() != cs.lastpos()) {
441 // get inset which is supposed to be in the next slice
442 if (cs.inset().inMathed())
443 inset = (cs.cell().begin() + cs.pos())->nucleus();
444 else if (cs.paragraph().isInset(cs.pos()))
445 inset = cs.paragraph().getInset(cs.pos());
447 // there are slices left, so there must be another inset
453 // Did we make it through the whole slice stack? Otherwise there
454 // was a problem at slice i, and we have to chop off above
456 LYXERR(Debug::DEBUG) << "fixIfBroken(): cursor chopped at " << i << endl;
464 std::ostream & operator<<(std::ostream & os, DocIterator const & dit)
466 for (size_t i = 0, n = dit.depth(); i != n; ++i)
467 os << " " << dit[i] << "\n";
472 bool operator<(DocIterator const & p, DocIterator const & q)
474 size_t depth = std::min(p.depth(), q.depth());
475 for (size_t i = 0 ; i < depth ; ++i) {
479 return p.depth() < q.depth();
483 bool operator>(DocIterator const & p, DocIterator const & q)
489 bool operator<=(DocIterator const & p, DocIterator const & q)
495 ///////////////////////////////////////////////////////
497 StableDocIterator::StableDocIterator(DocIterator const & dit)
499 data_ = dit.internalData();
500 for (size_t i = 0, n = data_.size(); i != n; ++i)
505 DocIterator StableDocIterator::asDocIterator(Inset * inset) const
507 // this function re-creates the cache of inset pointers
508 //lyxerr << "converting:\n" << *this << endl;
509 DocIterator dit = DocIterator(*inset);
510 for (size_t i = 0, n = data_.size(); i != n; ++i) {
513 lyxerr << BOOST_CURRENT_FUNCTION
514 << " Should not happen, but does e.g. after C-n C-l C-z S-C-z\n"
515 << " or when a Buffer has been concurently edited by two views"
516 << '\n' << "dit: " << dit << '\n'
517 << " lastpos: " << dit.lastpos() << endl;
521 dit.push_back(data_[i]);
522 dit.top().inset_ = inset;
523 if (dit.fixIfBroken())
526 inset = dit.nextInset();
528 //lyxerr << "convert:\n" << *this << " to:\n" << dit << endl;
533 std::ostream & operator<<(std::ostream & os, StableDocIterator const & dit)
535 for (size_t i = 0, n = dit.data_.size(); i != n; ++i)
536 os << " " << dit.data_[i] << "\n";
541 bool operator==(StableDocIterator const & dit1, StableDocIterator const & dit2)
543 return dit1.data_ == dit2.data_;