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(InsetBase & inset)
42 : boundary_(false), inset_(&inset)
46 DocIterator doc_iterator_begin(InsetBase & inset)
48 DocIterator dit(inset);
54 DocIterator doc_iterator_end(InsetBase & inset)
56 return DocIterator(inset);
60 InsetBase * 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 InsetBase * DocIterator::prevInset()
77 BOOST_ASSERT(!empty());
81 return prevAtom().nucleus();
82 return paragraph().isInset(pos() - 1) ? paragraph().getInset(pos() - 1) : 0;
86 InsetBase const * DocIterator::prevInset() const
88 BOOST_ASSERT(!empty());
92 return prevAtom().nucleus();
93 return paragraph().isInset(pos() - 1) ? paragraph().getInset(pos() - 1) : 0;
97 InsetBase * DocIterator::realInset() const
99 BOOST_ASSERT(inTexted());
100 // if we are in a tabular, we need the cell
101 if (inset().lyxCode() == InsetBase::TABULAR_CODE) {
102 InsetTabular & tabular = static_cast<InsetTabular&>(inset());
103 return tabular.cell(idx()).get();
109 MathAtom const & DocIterator::prevAtom() const
111 BOOST_ASSERT(!empty());
112 BOOST_ASSERT(pos() > 0);
113 return cell()[pos() - 1];
117 MathAtom & DocIterator::prevAtom()
119 BOOST_ASSERT(!empty());
120 BOOST_ASSERT(pos() > 0);
121 return cell()[pos() - 1];
125 MathAtom const & DocIterator::nextAtom() const
127 BOOST_ASSERT(!empty());
128 //lyxerr << "lastpos: " << lastpos() << " next atom:\n" << *this << endl;
129 BOOST_ASSERT(pos() < lastpos());
130 return cell()[pos()];
134 MathAtom & DocIterator::nextAtom()
136 BOOST_ASSERT(!empty());
137 //lyxerr << "lastpos: " << lastpos() << " next atom:\n" << *this << endl;
138 BOOST_ASSERT(pos() < lastpos());
139 return cell()[pos()];
143 LyXText * DocIterator::text()
145 BOOST_ASSERT(!empty());
149 LyXText const * DocIterator::text() const
151 BOOST_ASSERT(!empty());
156 Paragraph & DocIterator::paragraph()
159 lyxerr << *this << endl;
160 BOOST_ASSERT(inTexted());
161 return top().paragraph();
165 Paragraph const & DocIterator::paragraph() const
167 BOOST_ASSERT(inTexted());
168 return top().paragraph();
172 pit_type DocIterator::lastpit() const
174 return inMathed() ? 0 : text()->paragraphs().size() - 1;
178 pos_type DocIterator::lastpos() const
180 return inMathed() ? cell().size() : paragraph().size();
184 DocIterator::idx_type DocIterator::lastidx() const
186 return top().lastidx();
190 size_t DocIterator::nargs() const
192 // assume 1x1 grid for main text
193 return top().nargs();
197 size_t DocIterator::ncols() const
199 // assume 1x1 grid for main text
200 return top().ncols();
204 size_t DocIterator::nrows() const
206 // assume 1x1 grid for main text
207 return top().nrows();
211 DocIterator::row_type DocIterator::row() const
217 DocIterator::col_type DocIterator::col() const
223 MathArray const & DocIterator::cell() const
225 BOOST_ASSERT(inMathed());
230 MathArray & DocIterator::cell()
232 BOOST_ASSERT(inMathed());
237 bool DocIterator::inMathed() const
239 return !empty() && inset().inMathed();
243 bool DocIterator::inTexted() const
245 return !empty() && !inset().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 InsetBase * 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 // jump over collapsables if they are collapsed
290 // FIXME: the check for asInsetMath() shouldn't be necessary
291 // but math insets do not return a sensible editable() state yet.
292 if (ignorecollapsed && nextInset() && (!nextInset()->asInsetMath()
293 && nextInset()->editable() != InsetBase::HIGHLY_EDITABLE)) {
298 CursorSlice & tip = top();
299 //lyxerr << "XXX\n" << *this << endl;
301 // this is used twice and shows up in the profiler!
302 pos_type const lastp = lastpos();
304 // move into an inset to the right if possible
307 if (tip.pos() != lastp) {
308 // this is impossible for pos() == size()
310 n = (tip.cell().begin() + tip.pos())->nucleus();
312 if (paragraph().isInset(tip.pos()))
313 n = paragraph().getInset(tip.pos());
317 if (n && n->isActive()) {
318 //lyxerr << "... descend" << endl;
319 push_back(CursorSlice(*n));
323 // otherwise move on one position if possible
324 if (tip.pos() < lastp) {
325 //lyxerr << "... next pos" << endl;
329 //lyxerr << "... no next pos" << endl;
331 // otherwise move on one paragraph if possible
332 if (tip.pit() < lastpit()) {
333 //lyxerr << "... next par" << endl;
338 //lyxerr << "... no next pit" << endl;
340 // otherwise try to move on one cell if possible
341 if (tip.idx() < lastidx()) {
342 //lyxerr << "... next idx" << endl;
348 //lyxerr << "... no next idx" << endl;
350 // otherwise leave inset and jump over inset as a whole
352 // 'top' is invalid now...
358 void DocIterator::forwardPosNoDescend()
360 CursorSlice & tip = top();
361 pos_type const lastp = lastpos();
363 // move on one position if possible
364 if (tip.pos() < lastp) {
365 //lyxerr << "... next pos" << endl;
369 //lyxerr << "... no next pos" << endl;
371 // otherwise move on one paragraph if possible
372 if (tip.pit() < lastpit()) {
373 //lyxerr << "... next par" << endl;
378 //lyxerr << "... no next pit" << endl;
380 // otherwise try to move on one cell if possible
381 if (tip.idx() < lastidx()) {
382 //lyxerr << "... next idx" << endl;
388 //lyxerr << "... no next idx" << endl;
390 // otherwise leave inset and jump over inset as a whole
392 // 'top' is invalid now...
398 void DocIterator::forwardPar()
402 while (!empty() && (!inTexted() || pos() != 0)) {
404 pos_type const lastp = lastpos();
405 Paragraph const & par = paragraph();
406 pos_type & pos = top().pos();
407 while (pos < lastp && !par.isInset(pos))
415 void DocIterator::forwardChar()
418 while (!empty() && pos() == lastpos())
423 void DocIterator::forwardInset()
427 while (!empty() && !nextInset()) {
429 pos_type const lastp = lastpos();
430 Paragraph const & par = paragraph();
431 pos_type & pos = top().pos();
432 while (pos < lastp && !par.isInset(pos))
442 void DocIterator::backwardChar()
445 while (!empty() && pos() == lastpos())
450 void DocIterator::backwardPos()
452 //this dog bites his tail
454 push_back(CursorSlice(*inset_));
455 top().idx() = lastidx();
456 top().pit() = lastpit();
457 top().pos() = lastpos();
461 CursorSlice & tip = top();
463 if (tip.pos() != 0) {
465 } else if (tip.pit() != 0) {
467 tip.pos() = lastpos();
469 } else if (tip.idx() != 0) {
471 tip.pit() = lastpit();
472 tip.pos() = lastpos();
479 // move into an inset to the left if possible
483 n = (tip.cell().begin() + tip.pos())->nucleus();
485 if (paragraph().isInset(tip.pos()))
486 n = paragraph().getInset(tip.pos());
489 if (n && n->isActive()) {
490 push_back(CursorSlice(*n));
491 top().idx() = lastidx();
492 top().pit() = lastpit();
493 top().pos() = lastpos();
498 bool DocIterator::hasPart(DocIterator const & it) const
500 // it can't be a part if it is larger
501 if (it.depth() > depth())
504 // as inset adresses are the 'last' level
505 return &it.top().inset() == &slices_[it.depth() - 1].inset();
509 void DocIterator::updateInsets(InsetBase * inset)
511 // this function re-creates the cache of inset pointers.
512 // code taken in part from StableDocIterator::asDocIterator.
513 //lyxerr << "converting:\n" << *this << endl;
514 DocIterator dit = DocIterator(*inset);
515 size_t const n = slices_.size();
516 for (size_t i = 0 ; i < n; ++i) {
518 dit.push_back(slices_[i]);
519 dit.top().inset_ = inset;
521 inset = dit.nextInset();
523 //lyxerr << "converted:\n" << *this << endl;
528 std::ostream & operator<<(std::ostream & os, DocIterator const & dit)
530 for (size_t i = 0, n = dit.depth(); i != n; ++i)
531 os << " " << dit[i] << "\n";
537 ///////////////////////////////////////////////////////
539 StableDocIterator::StableDocIterator(DocIterator const & dit)
541 data_ = dit.internalData();
542 for (size_t i = 0, n = data_.size(); i != n; ++i)
547 DocIterator StableDocIterator::asDocIterator(InsetBase * inset) const
549 // this function re-creates the cache of inset pointers
550 //lyxerr << "converting:\n" << *this << endl;
551 DocIterator dit = DocIterator(*inset);
552 for (size_t i = 0, n = data_.size(); i != n; ++i) {
555 lyxerr << BOOST_CURRENT_FUNCTION
556 << " Should not happen, but does e.g. after C-n C-l C-z S-C-z"
557 << '\n' << "dit: " << dit << '\n'
558 << " lastpos: " << dit.lastpos() << endl;
562 dit.push_back(data_[i]);
563 dit.top().inset_ = inset;
565 inset = dit.nextInset();
567 //lyxerr << "convert:\n" << *this << " to:\n" << dit << endl;
572 std::ostream & operator<<(std::ostream & os, StableDocIterator const & dit)
574 for (size_t i = 0, n = dit.data_.size(); i != n; ++i)
575 os << " " << dit.data_[i] << "\n";
580 bool operator==(StableDocIterator const & dit1, StableDocIterator const & dit2)
582 return dit1.data_ == dit2.data_;