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 LyXText * DocIterator::innerText()
239 BOOST_ASSERT(!empty());
240 // Go up until first non-0 text is hit
241 // (innermost text is 0 in mathed)
242 for (int i = depth() - 1; i >= 0; --i)
243 if (slices_[i].text())
244 return slices_[i].text();
248 LyXText const * DocIterator::innerText() const
250 BOOST_ASSERT(!empty());
251 // go up until first non-0 text is hit
252 // (innermost text is 0 in mathed)
253 for (int i = depth() - 1; i >= 0; --i)
254 if (slices_[i].text())
255 return slices_[i].text();
260 InsetBase * DocIterator::innerInsetOfType(int code) const
262 for (int i = depth() - 1; i >= 0; --i)
263 if (slices_[i].inset_->lyxCode() == code)
264 return slices_[i].inset_;
269 void DocIterator::forwardPos(bool ignorecollapsed)
271 //this dog bites his tail
273 push_back(CursorSlice(*inset_));
277 InsetBase * const nextinset = nextInset();
278 // jump over collapsables if they are collapsed
279 // FIXME: the check for asInsetMath() shouldn't be necessary
280 // but math insets do not return a sensible editable() state yet.
281 if (ignorecollapsed && nextinset && (!nextinset->asInsetMath()
282 && nextinset->editable() != InsetBase::HIGHLY_EDITABLE)) {
287 CursorSlice & tip = top();
288 //lyxerr << "XXX\n" << *this << endl;
290 // this is used twice and shows up in the profiler!
291 pos_type const lastp = lastpos();
293 // move into an inset to the right if possible
296 if (tip.pos() != lastp) {
297 // this is impossible for pos() == size()
299 n = (tip.cell().begin() + tip.pos())->nucleus();
301 if (paragraph().isInset(tip.pos()))
302 n = paragraph().getInset(tip.pos());
306 if (n && n->isActive()) {
307 //lyxerr << "... descend" << endl;
308 push_back(CursorSlice(*n));
312 // otherwise move on one position if possible
313 if (tip.pos() < lastp) {
314 //lyxerr << "... next pos" << endl;
318 //lyxerr << "... no next pos" << endl;
320 // otherwise move on one paragraph if possible
321 if (tip.pit() < lastpit()) {
322 //lyxerr << "... next par" << endl;
327 //lyxerr << "... no next pit" << endl;
329 // otherwise try to move on one cell if possible
330 if (tip.idx() < lastidx()) {
331 //lyxerr << "... next idx" << endl;
337 //lyxerr << "... no next idx" << endl;
339 // otherwise leave inset and jump over inset as a whole
341 // 'top' is invalid now...
347 void DocIterator::forwardPosNoDescend()
349 CursorSlice & tip = top();
350 pos_type const lastp = lastpos();
352 // move on one position if possible
353 if (tip.pos() < lastp) {
354 //lyxerr << "... next pos" << endl;
358 //lyxerr << "... no next pos" << endl;
360 // otherwise move on one paragraph if possible
361 if (tip.pit() < lastpit()) {
362 //lyxerr << "... next par" << endl;
367 //lyxerr << "... no next pit" << endl;
369 // otherwise try to move on one cell if possible
370 if (tip.idx() < lastidx()) {
371 //lyxerr << "... next idx" << endl;
377 //lyxerr << "... no next idx" << endl;
379 // otherwise leave inset and jump over inset as a whole
381 // 'top' is invalid now...
387 void DocIterator::forwardPar()
391 while (!empty() && (!inTexted() || pos() != 0)) {
393 pos_type const lastp = lastpos();
394 Paragraph const & par = paragraph();
395 pos_type & pos = top().pos();
396 if (par.insetlist.empty())
399 while (pos < lastp && !par.isInset(pos))
407 void DocIterator::forwardChar()
410 while (!empty() && pos() == lastpos())
415 void DocIterator::forwardInset()
419 while (!empty() && !nextInset()) {
421 pos_type const lastp = lastpos();
422 Paragraph const & par = paragraph();
423 pos_type & pos = top().pos();
424 while (pos < lastp && !par.isInset(pos))
434 void DocIterator::backwardChar()
437 while (!empty() && pos() == lastpos())
442 void DocIterator::backwardPos()
444 //this dog bites his tail
446 push_back(CursorSlice(*inset_));
447 top().idx() = lastidx();
448 top().pit() = lastpit();
449 top().pos() = lastpos();
453 CursorSlice & tip = top();
455 if (tip.pos() != 0) {
457 } else if (tip.pit() != 0) {
459 tip.pos() = lastpos();
461 } else if (tip.idx() != 0) {
463 tip.pit() = lastpit();
464 tip.pos() = lastpos();
471 // move into an inset to the left if possible
475 n = (tip.cell().begin() + tip.pos())->nucleus();
477 if (paragraph().isInset(tip.pos()))
478 n = paragraph().getInset(tip.pos());
481 if (n && n->isActive()) {
482 push_back(CursorSlice(*n));
483 top().idx() = lastidx();
484 top().pit() = lastpit();
485 top().pos() = lastpos();
490 bool DocIterator::hasPart(DocIterator const & it) const
492 // it can't be a part if it is larger
493 if (it.depth() > depth())
496 // as inset adresses are the 'last' level
497 return &it.top().inset() == &slices_[it.depth() - 1].inset();
501 void DocIterator::updateInsets(InsetBase * inset)
503 // this function re-creates the cache of inset pointers.
504 // code taken in part from StableDocIterator::asDocIterator.
505 //lyxerr << "converting:\n" << *this << endl;
506 DocIterator dit = DocIterator(*inset);
507 size_t const n = slices_.size();
508 for (size_t i = 0 ; i < n; ++i) {
510 dit.push_back(slices_[i]);
511 dit.top().inset_ = inset;
513 inset = dit.nextInset();
515 //lyxerr << "converted:\n" << *this << endl;
520 std::ostream & operator<<(std::ostream & os, DocIterator const & dit)
522 for (size_t i = 0, n = dit.depth(); i != n; ++i)
523 os << " " << dit[i] << "\n";
529 ///////////////////////////////////////////////////////
531 StableDocIterator::StableDocIterator(DocIterator const & dit)
533 data_ = dit.internalData();
534 for (size_t i = 0, n = data_.size(); i != n; ++i)
539 DocIterator StableDocIterator::asDocIterator(InsetBase * inset) const
541 // this function re-creates the cache of inset pointers
542 //lyxerr << "converting:\n" << *this << endl;
543 DocIterator dit = DocIterator(*inset);
544 for (size_t i = 0, n = data_.size(); i != n; ++i) {
547 lyxerr << BOOST_CURRENT_FUNCTION
548 << " Should not happen, but does e.g. after C-n C-l C-z S-C-z"
549 << '\n' << "dit: " << dit << '\n'
550 << " lastpos: " << dit.lastpos() << endl;
554 dit.push_back(data_[i]);
555 dit.top().inset_ = inset;
557 inset = dit.nextInset();
559 //lyxerr << "convert:\n" << *this << " to:\n" << dit << endl;
564 std::ostream & operator<<(std::ostream & os, StableDocIterator const & dit)
566 for (size_t i = 0, n = dit.data_.size(); i != n; ++i)
567 os << " " << dit.data_[i] << "\n";
572 bool operator==(StableDocIterator const & dit1, StableDocIterator const & dit2)
574 return dit1.data_ == dit2.data_;