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"
19 #include "paragraph.h"
21 #include "mathed/math_data.h"
22 #include "mathed/math_inset.h"
23 #include "insets/insettabular.h"
25 #include <boost/assert.hpp>
26 #include <boost/current_function.hpp>
31 // We could be able to get rid of this if only every BufferView were
32 // associated to a buffer on construction.
33 DocIterator::DocIterator()
34 : boundary_(false), inset_(0)
38 DocIterator::DocIterator(InsetBase & inset)
39 : boundary_(false), inset_(&inset)
43 DocIterator doc_iterator_begin(InsetBase & inset)
45 DocIterator dit(inset);
51 DocIterator doc_iterator_end(InsetBase & inset)
53 return DocIterator(inset);
57 InsetBase * DocIterator::nextInset()
59 BOOST_ASSERT(!empty());
60 if (pos() == lastpos())
62 if (pos() > lastpos()) {
63 lyxerr << "Should not happen, but it does. " << endl;
67 return nextAtom().nucleus();
68 return paragraph().isInset(pos()) ? paragraph().getInset(pos()) : 0;
72 InsetBase * DocIterator::prevInset()
74 BOOST_ASSERT(!empty());
78 return prevAtom().nucleus();
79 return paragraph().isInset(pos() - 1) ? paragraph().getInset(pos() - 1) : 0;
83 InsetBase const * DocIterator::prevInset() const
85 BOOST_ASSERT(!empty());
89 return prevAtom().nucleus();
90 return paragraph().isInset(pos() - 1) ? paragraph().getInset(pos() - 1) : 0;
94 InsetBase * DocIterator::realInset() const
96 BOOST_ASSERT(inTexted());
97 // if we are in a tabular, we need the cell
98 if (inset().lyxCode() == InsetBase::TABULAR_CODE) {
99 InsetTabular & tabular = static_cast<InsetTabular&>(inset());
100 return tabular.cell(idx()).get();
106 MathAtom const & DocIterator::prevAtom() const
108 BOOST_ASSERT(!empty());
109 BOOST_ASSERT(pos() > 0);
110 return cell()[pos() - 1];
114 MathAtom & DocIterator::prevAtom()
116 BOOST_ASSERT(!empty());
117 BOOST_ASSERT(pos() > 0);
118 return cell()[pos() - 1];
122 MathAtom const & DocIterator::nextAtom() const
124 BOOST_ASSERT(!empty());
125 //lyxerr << "lastpos: " << lastpos() << " next atom:\n" << *this << endl;
126 BOOST_ASSERT(pos() < lastpos());
127 return cell()[pos()];
131 MathAtom & DocIterator::nextAtom()
133 BOOST_ASSERT(!empty());
134 //lyxerr << "lastpos: " << lastpos() << " next atom:\n" << *this << endl;
135 BOOST_ASSERT(pos() < lastpos());
136 return cell()[pos()];
140 LyXText * DocIterator::text()
142 BOOST_ASSERT(!empty());
146 LyXText const * DocIterator::text() const
148 BOOST_ASSERT(!empty());
153 Paragraph & DocIterator::paragraph()
156 lyxerr << *this << endl;
157 BOOST_ASSERT(inTexted());
158 return top().paragraph();
162 Paragraph const & DocIterator::paragraph() const
164 BOOST_ASSERT(inTexted());
165 return top().paragraph();
169 Row & DocIterator::textRow()
171 BOOST_ASSERT(!paragraph().rows().empty());
172 return paragraph().getRow(pos(), boundary_);
176 Row const & DocIterator::textRow() const
178 BOOST_ASSERT(!paragraph().rows().empty());
179 return paragraph().getRow(pos(), boundary_);
183 DocIterator::pit_type DocIterator::lastpit() const
185 return inMathed() ? 0 : text()->paragraphs().size() - 1;
189 DocIterator::pos_type DocIterator::lastpos() const
191 return inMathed() ? cell().size() : paragraph().size();
195 DocIterator::idx_type DocIterator::lastidx() const
197 return top().lastidx();
201 size_t DocIterator::nargs() const
203 // assume 1x1 grid for main text
204 return top().nargs();
208 size_t DocIterator::ncols() const
210 // assume 1x1 grid for main text
211 return top().ncols();
215 size_t DocIterator::nrows() const
217 // assume 1x1 grid for main text
218 return top().nrows();
222 DocIterator::row_type DocIterator::row() const
228 DocIterator::col_type DocIterator::col() const
234 MathArray const & DocIterator::cell() const
236 BOOST_ASSERT(inMathed());
241 MathArray & DocIterator::cell()
243 BOOST_ASSERT(inMathed());
248 bool DocIterator::inMathed() const
250 return !empty() && inset().inMathed();
254 bool DocIterator::inTexted() const
256 return !empty() && !inset().inMathed();
260 LyXText * DocIterator::innerText()
262 BOOST_ASSERT(!empty());
263 // Go up until first non-0 text is hit
264 // (innermost text is 0 in mathed)
265 for (int i = depth() - 1; i >= 0; --i)
266 if (slices_[i].text())
267 return slices_[i].text();
271 LyXText const * DocIterator::innerText() const
273 BOOST_ASSERT(!empty());
274 // go up until first non-0 text is hit
275 // (innermost text is 0 in mathed)
276 for (int i = depth() - 1; i >= 0; --i)
277 if (slices_[i].text())
278 return slices_[i].text();
283 InsetBase * DocIterator::innerInsetOfType(int code) const
285 for (int i = depth() - 1; i >= 0; --i)
286 if (slices_[i].inset_->lyxCode() == code)
287 return slices_[i].inset_;
292 void DocIterator::forwardPos(bool ignorecollapsed)
294 //this dog bites his tail
296 push_back(CursorSlice(*inset_));
300 // jump over collapsables if they are collapsed
301 // FIXME: the check for asMathInset() shouldn't be necessary
302 // but math insets do not return a sensible editable() state yet.
303 if (ignorecollapsed && nextInset() && (!nextInset()->asMathInset()
304 && nextInset()->editable() != InsetBase::HIGHLY_EDITABLE)) {
309 CursorSlice & tip = top();
310 //lyxerr << "XXX\n" << *this << endl;
312 // this is used twice and shows up in the profiler!
313 pos_type const lastp = lastpos();
315 // move into an inset to the right if possible
318 if (tip.pos() != lastp) {
319 // this is impossible for pos() == size()
321 n = (tip.cell().begin() + tip.pos())->nucleus();
323 if (paragraph().isInset(tip.pos()))
324 n = paragraph().getInset(tip.pos());
328 if (n && n->isActive()) {
329 //lyxerr << "... descend" << endl;
330 push_back(CursorSlice(*n));
334 // otherwise move on one position if possible
335 if (tip.pos() < lastp) {
336 //lyxerr << "... next pos" << endl;
340 //lyxerr << "... no next pos" << endl;
342 // otherwise move on one paragraph if possible
343 if (tip.pit() < lastpit()) {
344 //lyxerr << "... next par" << endl;
349 //lyxerr << "... no next pit" << endl;
351 // otherwise try to move on one cell if possible
352 if (tip.idx() < lastidx()) {
353 //lyxerr << "... next idx" << endl;
359 //lyxerr << "... no next idx" << endl;
361 // otherwise leave inset and jump over inset as a whole
363 // 'top' is invalid now...
369 void DocIterator::forwardPosNoDescend()
371 CursorSlice & tip = top();
372 pos_type const lastp = lastpos();
374 // move on one position if possible
375 if (tip.pos() < lastp) {
376 //lyxerr << "... next pos" << endl;
380 //lyxerr << "... no next pos" << endl;
382 // otherwise move on one paragraph if possible
383 if (tip.pit() < lastpit()) {
384 //lyxerr << "... next par" << endl;
389 //lyxerr << "... no next pit" << endl;
391 // otherwise try to move on one cell if possible
392 if (tip.idx() < lastidx()) {
393 //lyxerr << "... next idx" << endl;
399 //lyxerr << "... no next idx" << endl;
401 // otherwise we can't move on
405 void DocIterator::forwardPar()
410 DocIterator cmp(*this);
413 while (!empty() && (!inTexted() || pos() != 0)) {
415 pos_type const lastp = lastpos();
416 Paragraph const & par = paragraph();
417 pos_type & pos = top().pos();
418 while (pos < lastp && !par.isInset(pos))
425 while (!cmp.empty() && (!cmp.inTexted() || cmp.pos() != 0))
427 BOOST_ASSERT(cmp == *this);
432 void DocIterator::forwardChar()
435 while (!empty() && pos() == lastpos())
440 void DocIterator::forwardInset()
443 while (!empty() && (pos() == lastpos() || nextInset() == 0))
448 void DocIterator::backwardChar()
451 while (!empty() && pos() == lastpos())
456 void DocIterator::backwardPos()
458 //this dog bites his tail
460 push_back(CursorSlice(*inset_));
461 top().idx() = lastidx();
462 top().pit() = lastpit();
463 top().pos() = lastpos();
467 CursorSlice & tip = top();
469 if (tip.pos() != 0) {
471 } else if (tip.pit() != 0) {
473 tip.pos() = lastpos();
475 } else if (tip.idx() != 0) {
477 tip.pit() = lastpit();
478 tip.pos() = lastpos();
485 // move into an inset to the left if possible
489 n = (tip.cell().begin() + tip.pos())->nucleus();
491 if (paragraph().isInset(tip.pos()))
492 n = paragraph().getInset(tip.pos());
495 if (n && n->isActive()) {
496 push_back(CursorSlice(*n));
497 top().idx() = lastidx();
498 top().pit() = lastpit();
499 top().pos() = lastpos();
504 bool DocIterator::hasPart(DocIterator const & it) const
506 // it can't be a part if it is larger
507 if (it.depth() > depth())
510 // as inset adresses are the 'last' level
511 return &it.top().inset() == &slices_[it.depth() - 1].inset();
515 void DocIterator::updateInsets(InsetBase * inset)
517 // this function re-creates the cache of inset pointers.
518 // code taken in part from StableDocIterator::asDocIterator.
519 //lyxerr << "converting:\n" << *this << endl;
520 DocIterator dit = DocIterator(*inset);
521 size_t const n = slices_.size();
522 for (size_t i = 0 ; i < n; ++i) {
524 dit.push_back(slices_[i]);
525 dit.top().inset_ = inset;
527 inset = dit.nextInset();
529 //lyxerr << "converted:\n" << *this << endl;
534 std::ostream & operator<<(std::ostream & os, DocIterator const & dit)
536 for (size_t i = 0, n = dit.depth(); i != n; ++i)
537 os << " " << dit[i] << "\n";
543 ///////////////////////////////////////////////////////
545 StableDocIterator::StableDocIterator(DocIterator const & dit)
547 data_ = dit.internalData();
548 for (size_t i = 0, n = data_.size(); i != n; ++i)
553 DocIterator StableDocIterator::asDocIterator(InsetBase * inset) const
555 // this function re-creates the cache of inset pointers
556 //lyxerr << "converting:\n" << *this << endl;
557 DocIterator dit = DocIterator(*inset);
558 for (size_t i = 0, n = data_.size(); i != n; ++i) {
561 lyxerr << BOOST_CURRENT_FUNCTION
562 << " Should not happen, but does e.g. after C-n C-l C-z S-C-z"
563 << '\n' << "dit: " << dit << '\n'
564 << " lastpos: " << dit.lastpos() << endl;
568 dit.push_back(data_[i]);
569 dit.top().inset_ = inset;
571 inset = dit.nextInset();
573 //lyxerr << "convert:\n" << *this << " to:\n" << dit << endl;
578 std::ostream & operator<<(std::ostream & os, StableDocIterator const & dit)
580 for (size_t i = 0, n = dit.data_.size(); i != n; ++i)
581 os << " " << dit.data_[i] << "\n";
586 bool operator==(StableDocIterator const & dit1, StableDocIterator const & dit2)
588 return dit1.data_ == dit2.data_;