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/MathData.h"
22 #include "mathed/InsetMath.h"
24 #include "insets/insettabular.h"
26 #include <boost/assert.hpp>
27 #include <boost/current_function.hpp>
32 // We could be able to get rid of this if only every BufferView were
33 // associated to a buffer on construction.
34 DocIterator::DocIterator()
35 : boundary_(false), inset_(0)
39 DocIterator::DocIterator(InsetBase & inset)
40 : boundary_(false), inset_(&inset)
44 DocIterator doc_iterator_begin(InsetBase & inset)
46 DocIterator dit(inset);
52 DocIterator doc_iterator_end(InsetBase & inset)
54 return DocIterator(inset);
58 InsetBase * DocIterator::nextInset()
60 BOOST_ASSERT(!empty());
61 if (pos() == lastpos())
63 if (pos() > lastpos()) {
64 lyxerr << "Should not happen, but it does. " << endl;
68 return nextAtom().nucleus();
69 return paragraph().isInset(pos()) ? paragraph().getInset(pos()) : 0;
73 InsetBase * DocIterator::prevInset()
75 BOOST_ASSERT(!empty());
79 return prevAtom().nucleus();
80 return paragraph().isInset(pos() - 1) ? paragraph().getInset(pos() - 1) : 0;
84 InsetBase const * DocIterator::prevInset() const
86 BOOST_ASSERT(!empty());
90 return prevAtom().nucleus();
91 return paragraph().isInset(pos() - 1) ? paragraph().getInset(pos() - 1) : 0;
95 InsetBase * DocIterator::realInset() const
97 BOOST_ASSERT(inTexted());
98 // if we are in a tabular, we need the cell
99 if (inset().lyxCode() == InsetBase::TABULAR_CODE) {
100 InsetTabular & tabular = static_cast<InsetTabular&>(inset());
101 return tabular.cell(idx()).get();
107 MathAtom const & DocIterator::prevAtom() const
109 BOOST_ASSERT(!empty());
110 BOOST_ASSERT(pos() > 0);
111 return cell()[pos() - 1];
115 MathAtom & DocIterator::prevAtom()
117 BOOST_ASSERT(!empty());
118 BOOST_ASSERT(pos() > 0);
119 return cell()[pos() - 1];
123 MathAtom const & DocIterator::nextAtom() const
125 BOOST_ASSERT(!empty());
126 //lyxerr << "lastpos: " << lastpos() << " next atom:\n" << *this << endl;
127 BOOST_ASSERT(pos() < lastpos());
128 return cell()[pos()];
132 MathAtom & DocIterator::nextAtom()
134 BOOST_ASSERT(!empty());
135 //lyxerr << "lastpos: " << lastpos() << " next atom:\n" << *this << endl;
136 BOOST_ASSERT(pos() < lastpos());
137 return cell()[pos()];
141 LyXText * DocIterator::text()
143 BOOST_ASSERT(!empty());
147 LyXText const * DocIterator::text() const
149 BOOST_ASSERT(!empty());
154 Paragraph & DocIterator::paragraph()
157 lyxerr << *this << endl;
158 BOOST_ASSERT(inTexted());
159 return top().paragraph();
163 Paragraph const & DocIterator::paragraph() const
165 BOOST_ASSERT(inTexted());
166 return top().paragraph();
170 Row & DocIterator::textRow()
172 BOOST_ASSERT(!paragraph().rows().empty());
173 return paragraph().getRow(pos(), boundary_);
177 Row const & DocIterator::textRow() const
179 BOOST_ASSERT(!paragraph().rows().empty());
180 return paragraph().getRow(pos(), boundary_);
184 DocIterator::pit_type DocIterator::lastpit() const
186 return inMathed() ? 0 : text()->paragraphs().size() - 1;
190 DocIterator::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 MathArray const & DocIterator::cell() const
237 BOOST_ASSERT(inMathed());
242 MathArray & DocIterator::cell()
244 BOOST_ASSERT(inMathed());
249 bool DocIterator::inMathed() const
251 return !empty() && inset().inMathed();
255 bool DocIterator::inTexted() const
257 return !empty() && !inset().inMathed();
261 LyXText * DocIterator::innerText()
263 BOOST_ASSERT(!empty());
264 // Go up until first non-0 text is hit
265 // (innermost text is 0 in mathed)
266 for (int i = depth() - 1; i >= 0; --i)
267 if (slices_[i].text())
268 return slices_[i].text();
272 LyXText const * DocIterator::innerText() const
274 BOOST_ASSERT(!empty());
275 // go up until first non-0 text is hit
276 // (innermost text is 0 in mathed)
277 for (int i = depth() - 1; i >= 0; --i)
278 if (slices_[i].text())
279 return slices_[i].text();
284 InsetBase * DocIterator::innerInsetOfType(int code) const
286 for (int i = depth() - 1; i >= 0; --i)
287 if (slices_[i].inset_->lyxCode() == code)
288 return slices_[i].inset_;
293 void DocIterator::forwardPos(bool ignorecollapsed)
295 //this dog bites his tail
297 push_back(CursorSlice(*inset_));
301 // jump over collapsables if they are collapsed
302 // FIXME: the check for asInsetMath() shouldn't be necessary
303 // but math insets do not return a sensible editable() state yet.
304 if (ignorecollapsed && nextInset() && (!nextInset()->asInsetMath()
305 && nextInset()->editable() != InsetBase::HIGHLY_EDITABLE)) {
310 CursorSlice & tip = top();
311 //lyxerr << "XXX\n" << *this << endl;
313 // this is used twice and shows up in the profiler!
314 pos_type const lastp = lastpos();
316 // move into an inset to the right if possible
319 if (tip.pos() != lastp) {
320 // this is impossible for pos() == size()
322 n = (tip.cell().begin() + tip.pos())->nucleus();
324 if (paragraph().isInset(tip.pos()))
325 n = paragraph().getInset(tip.pos());
329 if (n && n->isActive()) {
330 //lyxerr << "... descend" << endl;
331 push_back(CursorSlice(*n));
335 // otherwise move on one position if possible
336 if (tip.pos() < lastp) {
337 //lyxerr << "... next pos" << endl;
341 //lyxerr << "... no next pos" << endl;
343 // otherwise move on one paragraph if possible
344 if (tip.pit() < lastpit()) {
345 //lyxerr << "... next par" << endl;
350 //lyxerr << "... no next pit" << endl;
352 // otherwise try to move on one cell if possible
353 if (tip.idx() < lastidx()) {
354 //lyxerr << "... next idx" << endl;
360 //lyxerr << "... no next idx" << endl;
362 // otherwise leave inset and jump over inset as a whole
364 // 'top' is invalid now...
370 void DocIterator::forwardPosNoDescend()
372 CursorSlice & tip = top();
373 pos_type const lastp = lastpos();
375 // move on one position if possible
376 if (tip.pos() < lastp) {
377 //lyxerr << "... next pos" << endl;
381 //lyxerr << "... no next pos" << endl;
383 // otherwise move on one paragraph if possible
384 if (tip.pit() < lastpit()) {
385 //lyxerr << "... next par" << endl;
390 //lyxerr << "... no next pit" << endl;
392 // otherwise try to move on one cell if possible
393 if (tip.idx() < lastidx()) {
394 //lyxerr << "... next idx" << endl;
400 //lyxerr << "... no next idx" << endl;
402 // otherwise leave inset and jump over inset as a whole
404 // 'top' is invalid now...
410 void DocIterator::forwardPar()
415 DocIterator cmp(*this);
418 while (!empty() && (!inTexted() || pos() != 0)) {
420 pos_type const lastp = lastpos();
421 Paragraph const & par = paragraph();
422 pos_type & pos = top().pos();
423 while (pos < lastp && !par.isInset(pos))
430 while (!cmp.empty() && (!cmp.inTexted() || cmp.pos() != 0))
432 BOOST_ASSERT(cmp == *this);
437 void DocIterator::forwardChar()
440 while (!empty() && pos() == lastpos())
445 void DocIterator::forwardInset()
448 while (!empty() && (pos() == lastpos() || nextInset() == 0))
453 void DocIterator::backwardChar()
456 while (!empty() && pos() == lastpos())
461 void DocIterator::backwardPos()
463 //this dog bites his tail
465 push_back(CursorSlice(*inset_));
466 top().idx() = lastidx();
467 top().pit() = lastpit();
468 top().pos() = lastpos();
472 CursorSlice & tip = top();
474 if (tip.pos() != 0) {
476 } else if (tip.pit() != 0) {
478 tip.pos() = lastpos();
480 } else if (tip.idx() != 0) {
482 tip.pit() = lastpit();
483 tip.pos() = lastpos();
490 // move into an inset to the left if possible
494 n = (tip.cell().begin() + tip.pos())->nucleus();
496 if (paragraph().isInset(tip.pos()))
497 n = paragraph().getInset(tip.pos());
500 if (n && n->isActive()) {
501 push_back(CursorSlice(*n));
502 top().idx() = lastidx();
503 top().pit() = lastpit();
504 top().pos() = lastpos();
509 bool DocIterator::hasPart(DocIterator const & it) const
511 // it can't be a part if it is larger
512 if (it.depth() > depth())
515 // as inset adresses are the 'last' level
516 return &it.top().inset() == &slices_[it.depth() - 1].inset();
520 void DocIterator::updateInsets(InsetBase * inset)
522 // this function re-creates the cache of inset pointers.
523 // code taken in part from StableDocIterator::asDocIterator.
524 //lyxerr << "converting:\n" << *this << endl;
525 DocIterator dit = DocIterator(*inset);
526 size_t const n = slices_.size();
527 for (size_t i = 0 ; i < n; ++i) {
529 dit.push_back(slices_[i]);
530 dit.top().inset_ = inset;
532 inset = dit.nextInset();
534 //lyxerr << "converted:\n" << *this << endl;
539 std::ostream & operator<<(std::ostream & os, DocIterator const & dit)
541 for (size_t i = 0, n = dit.depth(); i != n; ++i)
542 os << " " << dit[i] << "\n";
548 ///////////////////////////////////////////////////////
550 StableDocIterator::StableDocIterator(DocIterator const & dit)
552 data_ = dit.internalData();
553 for (size_t i = 0, n = data_.size(); i != n; ++i)
558 DocIterator StableDocIterator::asDocIterator(InsetBase * inset) const
560 // this function re-creates the cache of inset pointers
561 //lyxerr << "converting:\n" << *this << endl;
562 DocIterator dit = DocIterator(*inset);
563 for (size_t i = 0, n = data_.size(); i != n; ++i) {
566 lyxerr << BOOST_CURRENT_FUNCTION
567 << " Should not happen, but does e.g. after C-n C-l C-z S-C-z"
568 << '\n' << "dit: " << dit << '\n'
569 << " lastpos: " << dit.lastpos() << endl;
573 dit.push_back(data_[i]);
574 dit.top().inset_ = inset;
576 inset = dit.nextInset();
578 //lyxerr << "convert:\n" << *this << " to:\n" << dit << endl;
583 std::ostream & operator<<(std::ostream & os, StableDocIterator const & dit)
585 for (size_t i = 0, n = dit.data_.size(); i != n; ++i)
586 os << " " << dit.data_[i] << "\n";
591 bool operator==(StableDocIterator const & dit1, StableDocIterator const & dit2)
593 return dit1.data_ == dit2.data_;