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 Text * DocIterator::text()
157 BOOST_ASSERT(!empty());
161 Text 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 Paragraph const & DocIterator::innerParagraph() const
186 BOOST_ASSERT(!empty());
187 // go up until first non-0 text is hit
188 // (innermost text is 0 in mathed)
189 for (int i = depth() - 1; i >= 0; --i)
190 if (slices_[i].text())
191 return slices_[i].paragraph();
193 // This case is in principe not possible. We _must_
194 // we inside a Paragraph.
200 pit_type DocIterator::lastpit() const
202 return inMathed() ? 0 : text()->paragraphs().size() - 1;
206 pos_type DocIterator::lastpos() const
208 return inMathed() ? cell().size() : paragraph().size();
212 DocIterator::idx_type DocIterator::lastidx() const
214 return top().lastidx();
218 size_t DocIterator::nargs() const
220 // assume 1x1 grid for main text
221 return top().nargs();
225 size_t DocIterator::ncols() const
227 // assume 1x1 grid for main text
228 return top().ncols();
232 size_t DocIterator::nrows() const
234 // assume 1x1 grid for main text
235 return top().nrows();
239 DocIterator::row_type DocIterator::row() const
245 DocIterator::col_type DocIterator::col() const
251 MathData const & DocIterator::cell() const
253 // BOOST_ASSERT(inMathed());
258 MathData & DocIterator::cell()
260 // BOOST_ASSERT(inMathed());
265 Text * DocIterator::innerText()
267 BOOST_ASSERT(!empty());
268 // Go up until first non-0 text is hit
269 // (innermost text is 0 in mathed)
270 for (int i = depth() - 1; i >= 0; --i)
271 if (slices_[i].text())
272 return slices_[i].text();
276 Text const * DocIterator::innerText() const
278 BOOST_ASSERT(!empty());
279 // go up until first non-0 text is hit
280 // (innermost text is 0 in mathed)
281 for (int i = depth() - 1; i >= 0; --i)
282 if (slices_[i].text())
283 return slices_[i].text();
288 Inset * DocIterator::innerInsetOfType(int code) const
290 for (int i = depth() - 1; i >= 0; --i)
291 if (slices_[i].inset_->lyxCode() == code)
292 return slices_[i].inset_;
297 void DocIterator::forwardPos(bool ignorecollapsed)
299 //this dog bites his tail
301 push_back(CursorSlice(*inset_));
305 Inset * const nextinset = nextInset();
306 // jump over collapsables if they are collapsed
307 // FIXME: the check for asInsetMath() shouldn't be necessary
308 // but math insets do not return a sensible editable() state yet.
309 if (ignorecollapsed && nextinset && (!nextinset->asInsetMath()
310 && nextinset->editable() != Inset::HIGHLY_EDITABLE)) {
315 CursorSlice & tip = top();
316 //lyxerr << "XXX\n" << *this << endl;
318 // this is used twice and shows up in the profiler!
319 pos_type const lastp = lastpos();
321 // move into an inset to the right if possible
324 if (tip.pos() != lastp) {
325 // this is impossible for pos() == size()
327 n = (tip.cell().begin() + tip.pos())->nucleus();
329 if (paragraph().isInset(tip.pos()))
330 n = paragraph().getInset(tip.pos());
334 if (n && n->isActive()) {
335 //lyxerr << "... descend" << endl;
336 push_back(CursorSlice(*n));
340 // otherwise move on one position if possible
341 if (tip.pos() < lastp) {
342 //lyxerr << "... next pos" << endl;
346 //lyxerr << "... no next pos" << endl;
348 // otherwise move on one paragraph if possible
349 if (tip.pit() < lastpit()) {
350 //lyxerr << "... next par" << endl;
355 //lyxerr << "... no next pit" << endl;
357 // otherwise try to move on one cell if possible
358 if (tip.idx() < lastidx()) {
359 //lyxerr << "... next idx" << endl;
365 //lyxerr << "... no next idx" << endl;
367 // otherwise leave inset and jump over inset as a whole
369 // 'top' is invalid now...
375 void DocIterator::forwardPosNoDescend()
377 CursorSlice & tip = top();
378 pos_type const lastp = lastpos();
380 // move on one position if possible
381 if (tip.pos() < lastp) {
382 //lyxerr << "... next pos" << endl;
386 //lyxerr << "... no next pos" << endl;
388 // otherwise move on one paragraph if possible
389 if (tip.pit() < lastpit()) {
390 //lyxerr << "... next par" << endl;
395 //lyxerr << "... no next pit" << endl;
397 // otherwise try to move on one cell if possible
398 if (tip.idx() < lastidx()) {
399 //lyxerr << "... next idx" << endl;
405 //lyxerr << "... no next idx" << endl;
407 // otherwise leave inset and jump over inset as a whole
409 // 'top' is invalid now...
415 void DocIterator::forwardPar()
419 while (!empty() && (!inTexted() || pos() != 0)) {
421 pos_type const lastp = lastpos();
422 Paragraph const & par = paragraph();
423 pos_type & pos = top().pos();
424 if (par.insetlist.empty())
427 while (pos < lastp && !par.isInset(pos))
435 void DocIterator::forwardChar()
438 while (!empty() && pos() == lastpos())
443 void DocIterator::forwardInset()
447 while (!empty() && !nextInset()) {
449 pos_type const lastp = lastpos();
450 Paragraph const & par = paragraph();
451 pos_type & pos = top().pos();
452 while (pos < lastp && !par.isInset(pos))
462 void DocIterator::backwardChar()
465 while (!empty() && pos() == lastpos())
470 void DocIterator::backwardPos()
472 //this dog bites his tail
474 push_back(CursorSlice(*inset_));
475 top().idx() = lastidx();
476 top().pit() = lastpit();
477 top().pos() = lastpos();
481 CursorSlice & tip = top();
483 if (tip.pos() != 0) {
485 } else if (tip.pit() != 0) {
487 tip.pos() = lastpos();
489 } else if (tip.idx() != 0) {
491 tip.pit() = lastpit();
492 tip.pos() = lastpos();
499 // move into an inset to the left if possible
503 n = (tip.cell().begin() + tip.pos())->nucleus();
505 if (paragraph().isInset(tip.pos()))
506 n = paragraph().getInset(tip.pos());
509 if (n && n->isActive()) {
510 push_back(CursorSlice(*n));
511 top().idx() = lastidx();
512 top().pit() = lastpit();
513 top().pos() = lastpos();
518 bool DocIterator::hasPart(DocIterator const & it) const
520 // it can't be a part if it is larger
521 if (it.depth() > depth())
524 // as inset adresses are the 'last' level
525 return &it.top().inset() == &slices_[it.depth() - 1].inset();
529 void DocIterator::updateInsets(Inset * inset)
531 // this function re-creates the cache of inset pointers.
532 // code taken in part from StableDocIterator::asDocIterator.
533 //lyxerr << "converting:\n" << *this << endl;
534 DocIterator dit = DocIterator(*inset);
535 size_t const n = slices_.size();
536 for (size_t i = 0 ; i < n; ++i) {
538 dit.push_back(slices_[i]);
539 dit.top().inset_ = inset;
541 inset = dit.nextInset();
543 //lyxerr << "converted:\n" << *this << endl;
548 std::ostream & operator<<(std::ostream & os, DocIterator const & dit)
550 for (size_t i = 0, n = dit.depth(); i != n; ++i)
551 os << " " << dit[i] << "\n";
557 ///////////////////////////////////////////////////////
559 StableDocIterator::StableDocIterator(DocIterator const & dit)
561 data_ = dit.internalData();
562 for (size_t i = 0, n = data_.size(); i != n; ++i)
567 DocIterator StableDocIterator::asDocIterator(Inset * inset) const
569 // this function re-creates the cache of inset pointers
570 //lyxerr << "converting:\n" << *this << endl;
571 DocIterator dit = DocIterator(*inset);
572 for (size_t i = 0, n = data_.size(); i != n; ++i) {
575 lyxerr << BOOST_CURRENT_FUNCTION
576 << " Should not happen, but does e.g. after C-n C-l C-z S-C-z"
577 << '\n' << "dit: " << dit << '\n'
578 << " lastpos: " << dit.lastpos() << endl;
582 dit.push_back(data_[i]);
583 dit.top().inset_ = inset;
585 inset = dit.nextInset();
587 //lyxerr << "convert:\n" << *this << " to:\n" << dit << endl;
592 std::ostream & operator<<(std::ostream & os, StableDocIterator const & dit)
594 for (size_t i = 0, n = dit.data_.size(); i != n; ++i)
595 os << " " << dit.data_[i] << "\n";
600 bool operator==(StableDocIterator const & dit1, StableDocIterator const & dit2)
602 return dit1.data_ == dit2.data_;