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>
35 // We could be able to get rid of this if only every BufferView were
36 // associated to a buffer on construction.
37 DocIterator::DocIterator()
38 : boundary_(false), inset_(0)
42 DocIterator::DocIterator(InsetBase & inset)
43 : boundary_(false), inset_(&inset)
47 DocIterator doc_iterator_begin(InsetBase & inset)
49 DocIterator dit(inset);
55 DocIterator doc_iterator_end(InsetBase & inset)
57 return DocIterator(inset);
61 InsetBase * DocIterator::nextInset()
63 BOOST_ASSERT(!empty());
64 if (pos() == lastpos())
66 if (pos() > lastpos()) {
67 lyxerr << "Should not happen, but it does. " << endl;
71 return nextAtom().nucleus();
72 return paragraph().isInset(pos()) ? paragraph().getInset(pos()) : 0;
76 InsetBase * DocIterator::prevInset()
78 BOOST_ASSERT(!empty());
82 return prevAtom().nucleus();
83 return paragraph().isInset(pos() - 1) ? paragraph().getInset(pos() - 1) : 0;
87 InsetBase const * DocIterator::prevInset() const
89 BOOST_ASSERT(!empty());
93 return prevAtom().nucleus();
94 return paragraph().isInset(pos() - 1) ? paragraph().getInset(pos() - 1) : 0;
98 InsetBase * DocIterator::realInset() const
100 BOOST_ASSERT(inTexted());
101 // if we are in a tabular, we need the cell
102 if (inset().lyxCode() == InsetBase::TABULAR_CODE) {
103 InsetTabular & tabular = static_cast<InsetTabular&>(inset());
104 return tabular.cell(idx()).get();
110 MathAtom const & DocIterator::prevAtom() const
112 BOOST_ASSERT(!empty());
113 BOOST_ASSERT(pos() > 0);
114 return cell()[pos() - 1];
118 MathAtom & DocIterator::prevAtom()
120 BOOST_ASSERT(!empty());
121 BOOST_ASSERT(pos() > 0);
122 return cell()[pos() - 1];
126 MathAtom const & DocIterator::nextAtom() const
128 BOOST_ASSERT(!empty());
129 //lyxerr << "lastpos: " << lastpos() << " next atom:\n" << *this << endl;
130 BOOST_ASSERT(pos() < lastpos());
131 return cell()[pos()];
135 MathAtom & DocIterator::nextAtom()
137 BOOST_ASSERT(!empty());
138 //lyxerr << "lastpos: " << lastpos() << " next atom:\n" << *this << endl;
139 BOOST_ASSERT(pos() < lastpos());
140 return cell()[pos()];
144 LyXText * DocIterator::text()
146 BOOST_ASSERT(!empty());
150 LyXText const * DocIterator::text() const
152 BOOST_ASSERT(!empty());
157 Paragraph & DocIterator::paragraph()
160 lyxerr << *this << endl;
161 BOOST_ASSERT(inTexted());
162 return top().paragraph();
166 Paragraph const & DocIterator::paragraph() const
168 BOOST_ASSERT(inTexted());
169 return top().paragraph();
173 Row & DocIterator::textRow()
175 BOOST_ASSERT(!paragraph().rows().empty());
176 return paragraph().getRow(pos(), boundary_);
180 Row const & DocIterator::textRow() const
182 BOOST_ASSERT(!paragraph().rows().empty());
183 return paragraph().getRow(pos(), boundary_);
187 pit_type DocIterator::lastpit() const
189 return inMathed() ? 0 : text()->paragraphs().size() - 1;
193 pos_type DocIterator::lastpos() const
195 return inMathed() ? cell().size() : paragraph().size();
199 DocIterator::idx_type DocIterator::lastidx() const
201 return top().lastidx();
205 size_t DocIterator::nargs() const
207 // assume 1x1 grid for main text
208 return top().nargs();
212 size_t DocIterator::ncols() const
214 // assume 1x1 grid for main text
215 return top().ncols();
219 size_t DocIterator::nrows() const
221 // assume 1x1 grid for main text
222 return top().nrows();
226 DocIterator::row_type DocIterator::row() const
232 DocIterator::col_type DocIterator::col() const
238 MathArray const & DocIterator::cell() const
240 BOOST_ASSERT(inMathed());
245 MathArray & DocIterator::cell()
247 BOOST_ASSERT(inMathed());
252 bool DocIterator::inMathed() const
254 return !empty() && inset().inMathed();
258 bool DocIterator::inTexted() const
260 return !empty() && !inset().inMathed();
264 LyXText * DocIterator::innerText()
266 BOOST_ASSERT(!empty());
267 // Go up until first non-0 text is hit
268 // (innermost text is 0 in mathed)
269 for (int i = depth() - 1; i >= 0; --i)
270 if (slices_[i].text())
271 return slices_[i].text();
275 LyXText const * DocIterator::innerText() const
277 BOOST_ASSERT(!empty());
278 // go up until first non-0 text is hit
279 // (innermost text is 0 in mathed)
280 for (int i = depth() - 1; i >= 0; --i)
281 if (slices_[i].text())
282 return slices_[i].text();
287 InsetBase * DocIterator::innerInsetOfType(int code) const
289 for (int i = depth() - 1; i >= 0; --i)
290 if (slices_[i].inset_->lyxCode() == code)
291 return slices_[i].inset_;
296 void DocIterator::forwardPos(bool ignorecollapsed)
298 //this dog bites his tail
300 push_back(CursorSlice(*inset_));
304 // jump over collapsables if they are collapsed
305 // FIXME: the check for asInsetMath() shouldn't be necessary
306 // but math insets do not return a sensible editable() state yet.
307 if (ignorecollapsed && nextInset() && (!nextInset()->asInsetMath()
308 && nextInset()->editable() != InsetBase::HIGHLY_EDITABLE)) {
313 CursorSlice & tip = top();
314 //lyxerr << "XXX\n" << *this << endl;
316 // this is used twice and shows up in the profiler!
317 pos_type const lastp = lastpos();
319 // move into an inset to the right if possible
322 if (tip.pos() != lastp) {
323 // this is impossible for pos() == size()
325 n = (tip.cell().begin() + tip.pos())->nucleus();
327 if (paragraph().isInset(tip.pos()))
328 n = paragraph().getInset(tip.pos());
332 if (n && n->isActive()) {
333 //lyxerr << "... descend" << endl;
334 push_back(CursorSlice(*n));
338 // otherwise move on one position if possible
339 if (tip.pos() < lastp) {
340 //lyxerr << "... next pos" << endl;
344 //lyxerr << "... no next pos" << endl;
346 // otherwise move on one paragraph if possible
347 if (tip.pit() < lastpit()) {
348 //lyxerr << "... next par" << endl;
353 //lyxerr << "... no next pit" << endl;
355 // otherwise try to move on one cell if possible
356 if (tip.idx() < lastidx()) {
357 //lyxerr << "... next idx" << endl;
363 //lyxerr << "... no next idx" << endl;
365 // otherwise leave inset and jump over inset as a whole
367 // 'top' is invalid now...
373 void DocIterator::forwardPosNoDescend()
375 CursorSlice & tip = top();
376 pos_type const lastp = lastpos();
378 // move on one position if possible
379 if (tip.pos() < lastp) {
380 //lyxerr << "... next pos" << endl;
384 //lyxerr << "... no next pos" << endl;
386 // otherwise move on one paragraph if possible
387 if (tip.pit() < lastpit()) {
388 //lyxerr << "... next par" << endl;
393 //lyxerr << "... no next pit" << endl;
395 // otherwise try to move on one cell if possible
396 if (tip.idx() < lastidx()) {
397 //lyxerr << "... next idx" << endl;
403 //lyxerr << "... no next idx" << endl;
405 // otherwise leave inset and jump over inset as a whole
407 // 'top' is invalid now...
413 void DocIterator::forwardPar()
417 while (!empty() && (!inTexted() || pos() != 0)) {
419 pos_type const lastp = lastpos();
420 Paragraph const & par = paragraph();
421 pos_type & pos = top().pos();
422 while (pos < lastp && !par.isInset(pos))
430 void DocIterator::forwardChar()
433 while (!empty() && pos() == lastpos())
438 void DocIterator::forwardInset()
442 while (!empty() && !nextInset()) {
444 pos_type const lastp = lastpos();
445 Paragraph const & par = paragraph();
446 pos_type & pos = top().pos();
447 while (pos < lastp && !par.isInset(pos))
457 void DocIterator::backwardChar()
460 while (!empty() && pos() == lastpos())
465 void DocIterator::backwardPos()
467 //this dog bites his tail
469 push_back(CursorSlice(*inset_));
470 top().idx() = lastidx();
471 top().pit() = lastpit();
472 top().pos() = lastpos();
476 CursorSlice & tip = top();
478 if (tip.pos() != 0) {
480 } else if (tip.pit() != 0) {
482 tip.pos() = lastpos();
484 } else if (tip.idx() != 0) {
486 tip.pit() = lastpit();
487 tip.pos() = lastpos();
494 // move into an inset to the left if possible
498 n = (tip.cell().begin() + tip.pos())->nucleus();
500 if (paragraph().isInset(tip.pos()))
501 n = paragraph().getInset(tip.pos());
504 if (n && n->isActive()) {
505 push_back(CursorSlice(*n));
506 top().idx() = lastidx();
507 top().pit() = lastpit();
508 top().pos() = lastpos();
513 bool DocIterator::hasPart(DocIterator const & it) const
515 // it can't be a part if it is larger
516 if (it.depth() > depth())
519 // as inset adresses are the 'last' level
520 return &it.top().inset() == &slices_[it.depth() - 1].inset();
524 void DocIterator::updateInsets(InsetBase * inset)
526 // this function re-creates the cache of inset pointers.
527 // code taken in part from StableDocIterator::asDocIterator.
528 //lyxerr << "converting:\n" << *this << endl;
529 DocIterator dit = DocIterator(*inset);
530 size_t const n = slices_.size();
531 for (size_t i = 0 ; i < n; ++i) {
533 dit.push_back(slices_[i]);
534 dit.top().inset_ = inset;
536 inset = dit.nextInset();
538 //lyxerr << "converted:\n" << *this << endl;
543 std::ostream & operator<<(std::ostream & os, DocIterator const & dit)
545 for (size_t i = 0, n = dit.depth(); i != n; ++i)
546 os << " " << dit[i] << "\n";
552 ///////////////////////////////////////////////////////
554 StableDocIterator::StableDocIterator(DocIterator const & dit)
556 data_ = dit.internalData();
557 for (size_t i = 0, n = data_.size(); i != n; ++i)
562 DocIterator StableDocIterator::asDocIterator(InsetBase * inset) const
564 // this function re-creates the cache of inset pointers
565 //lyxerr << "converting:\n" << *this << endl;
566 DocIterator dit = DocIterator(*inset);
567 for (size_t i = 0, n = data_.size(); i != n; ++i) {
570 lyxerr << BOOST_CURRENT_FUNCTION
571 << " Should not happen, but does e.g. after C-n C-l C-z S-C-z"
572 << '\n' << "dit: " << dit << '\n'
573 << " lastpos: " << dit.lastpos() << endl;
577 dit.push_back(data_[i]);
578 dit.top().inset_ = inset;
580 inset = dit.nextInset();
582 //lyxerr << "convert:\n" << *this << " to:\n" << dit << endl;
587 std::ostream & operator<<(std::ostream & os, StableDocIterator const & dit)
589 for (size_t i = 0, n = dit.data_.size(); i != n; ++i)
590 os << " " << dit.data_[i] << "\n";
595 bool operator==(StableDocIterator const & dit1, StableDocIterator const & dit2)
597 return dit1.data_ == dit2.data_;