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 LyXText * DocIterator::text()
157 BOOST_ASSERT(!empty());
161 LyXText 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 pit_type DocIterator::lastpit() const
186 return inMathed() ? 0 : text()->paragraphs().size() - 1;
190 pos_type DocIterator::lastpos() const
192 return inMathed() ? cell().size() : paragraph().size();
196 DocIterator::idx_type DocIterator::lastidx() const
198 return top().lastidx();
202 size_t DocIterator::nargs() const
204 // assume 1x1 grid for main text
205 return top().nargs();
209 size_t DocIterator::ncols() const
211 // assume 1x1 grid for main text
212 return top().ncols();
216 size_t DocIterator::nrows() const
218 // assume 1x1 grid for main text
219 return top().nrows();
223 DocIterator::row_type DocIterator::row() const
229 DocIterator::col_type DocIterator::col() const
235 MathData const & DocIterator::cell() const
237 // BOOST_ASSERT(inMathed());
242 MathData & DocIterator::cell()
244 // BOOST_ASSERT(inMathed());
249 LyXText * DocIterator::innerText()
251 BOOST_ASSERT(!empty());
252 // Go up until first non-0 text is hit
253 // (innermost text is 0 in mathed)
254 for (int i = depth() - 1; i >= 0; --i)
255 if (slices_[i].text())
256 return slices_[i].text();
260 LyXText const * DocIterator::innerText() const
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();
272 Inset * DocIterator::innerInsetOfType(int code) const
274 for (int i = depth() - 1; i >= 0; --i)
275 if (slices_[i].inset_->lyxCode() == code)
276 return slices_[i].inset_;
281 void DocIterator::forwardPos(bool ignorecollapsed)
283 //this dog bites his tail
285 push_back(CursorSlice(*inset_));
289 Inset * const nextinset = nextInset();
290 // jump over collapsables if they are collapsed
291 // FIXME: the check for asInsetMath() shouldn't be necessary
292 // but math insets do not return a sensible editable() state yet.
293 if (ignorecollapsed && nextinset && (!nextinset->asInsetMath()
294 && nextinset->editable() != Inset::HIGHLY_EDITABLE)) {
299 CursorSlice & tip = top();
300 //lyxerr << "XXX\n" << *this << endl;
302 // this is used twice and shows up in the profiler!
303 pos_type const lastp = lastpos();
305 // move into an inset to the right if possible
308 if (tip.pos() != lastp) {
309 // this is impossible for pos() == size()
311 n = (tip.cell().begin() + tip.pos())->nucleus();
313 if (paragraph().isInset(tip.pos()))
314 n = paragraph().getInset(tip.pos());
318 if (n && n->isActive()) {
319 //lyxerr << "... descend" << endl;
320 push_back(CursorSlice(*n));
324 // otherwise move on one position if possible
325 if (tip.pos() < lastp) {
326 //lyxerr << "... next pos" << endl;
330 //lyxerr << "... no next pos" << endl;
332 // otherwise move on one paragraph if possible
333 if (tip.pit() < lastpit()) {
334 //lyxerr << "... next par" << endl;
339 //lyxerr << "... no next pit" << endl;
341 // otherwise try to move on one cell if possible
342 if (tip.idx() < lastidx()) {
343 //lyxerr << "... next idx" << endl;
349 //lyxerr << "... no next idx" << endl;
351 // otherwise leave inset and jump over inset as a whole
353 // 'top' is invalid now...
359 void DocIterator::forwardPosNoDescend()
361 CursorSlice & tip = top();
362 pos_type const lastp = lastpos();
364 // move on one position if possible
365 if (tip.pos() < lastp) {
366 //lyxerr << "... next pos" << endl;
370 //lyxerr << "... no next pos" << endl;
372 // otherwise move on one paragraph if possible
373 if (tip.pit() < lastpit()) {
374 //lyxerr << "... next par" << endl;
379 //lyxerr << "... no next pit" << endl;
381 // otherwise try to move on one cell if possible
382 if (tip.idx() < lastidx()) {
383 //lyxerr << "... next idx" << endl;
389 //lyxerr << "... no next idx" << endl;
391 // otherwise leave inset and jump over inset as a whole
393 // 'top' is invalid now...
399 void DocIterator::forwardPar()
403 while (!empty() && (!inTexted() || pos() != 0)) {
405 pos_type const lastp = lastpos();
406 Paragraph const & par = paragraph();
407 pos_type & pos = top().pos();
408 if (par.insetlist.empty())
411 while (pos < lastp && !par.isInset(pos))
419 void DocIterator::forwardChar()
422 while (!empty() && pos() == lastpos())
427 void DocIterator::forwardInset()
431 while (!empty() && !nextInset()) {
433 pos_type const lastp = lastpos();
434 Paragraph const & par = paragraph();
435 pos_type & pos = top().pos();
436 while (pos < lastp && !par.isInset(pos))
446 void DocIterator::backwardChar()
449 while (!empty() && pos() == lastpos())
454 void DocIterator::backwardPos()
456 //this dog bites his tail
458 push_back(CursorSlice(*inset_));
459 top().idx() = lastidx();
460 top().pit() = lastpit();
461 top().pos() = lastpos();
465 CursorSlice & tip = top();
467 if (tip.pos() != 0) {
469 } else if (tip.pit() != 0) {
471 tip.pos() = lastpos();
473 } else if (tip.idx() != 0) {
475 tip.pit() = lastpit();
476 tip.pos() = lastpos();
483 // move into an inset to the left if possible
487 n = (tip.cell().begin() + tip.pos())->nucleus();
489 if (paragraph().isInset(tip.pos()))
490 n = paragraph().getInset(tip.pos());
493 if (n && n->isActive()) {
494 push_back(CursorSlice(*n));
495 top().idx() = lastidx();
496 top().pit() = lastpit();
497 top().pos() = lastpos();
502 bool DocIterator::hasPart(DocIterator const & it) const
504 // it can't be a part if it is larger
505 if (it.depth() > depth())
508 // as inset adresses are the 'last' level
509 return &it.top().inset() == &slices_[it.depth() - 1].inset();
513 void DocIterator::updateInsets(Inset * inset)
515 // this function re-creates the cache of inset pointers.
516 // code taken in part from StableDocIterator::asDocIterator.
517 //lyxerr << "converting:\n" << *this << endl;
518 DocIterator dit = DocIterator(*inset);
519 size_t const n = slices_.size();
520 for (size_t i = 0 ; i < n; ++i) {
522 dit.push_back(slices_[i]);
523 dit.top().inset_ = inset;
525 inset = dit.nextInset();
527 //lyxerr << "converted:\n" << *this << endl;
532 std::ostream & operator<<(std::ostream & os, DocIterator const & dit)
534 for (size_t i = 0, n = dit.depth(); i != n; ++i)
535 os << " " << dit[i] << "\n";
541 ///////////////////////////////////////////////////////
543 StableDocIterator::StableDocIterator(DocIterator const & dit)
545 data_ = dit.internalData();
546 for (size_t i = 0, n = data_.size(); i != n; ++i)
551 DocIterator StableDocIterator::asDocIterator(Inset * inset) const
553 // this function re-creates the cache of inset pointers
554 //lyxerr << "converting:\n" << *this << endl;
555 DocIterator dit = DocIterator(*inset);
556 for (size_t i = 0, n = data_.size(); i != n; ++i) {
559 lyxerr << BOOST_CURRENT_FUNCTION
560 << " Should not happen, but does e.g. after C-n C-l C-z S-C-z"
561 << '\n' << "dit: " << dit << '\n'
562 << " lastpos: " << dit.lastpos() << endl;
566 dit.push_back(data_[i]);
567 dit.top().inset_ = inset;
569 inset = dit.nextInset();
571 //lyxerr << "convert:\n" << *this << " to:\n" << dit << endl;
576 std::ostream & operator<<(std::ostream & os, StableDocIterator const & dit)
578 for (size_t i = 0, n = dit.data_.size(); i != n; ++i)
579 os << " " << dit.data_[i] << "\n";
584 bool operator==(StableDocIterator const & dit1, StableDocIterator const & dit2)
586 return dit1.data_ == dit2.data_;