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/math_data.h"
22 #include "mathed/math_inset.h"
24 #include <boost/assert.hpp>
29 // We could be able to get rid of this if only every BufferView were
30 // associated to a buffer on construction.
31 DocIterator::DocIterator()
36 DocIterator::DocIterator(InsetBase & inset)
41 DocIterator doc_iterator_begin(InsetBase & inset)
43 DocIterator dit(inset);
49 DocIterator doc_iterator_end(InsetBase & inset)
51 return DocIterator(inset);
55 InsetBase * DocIterator::nextInset()
57 BOOST_ASSERT(!empty());
58 if (pos() == lastpos())
60 if (pos() > lastpos()) {
61 lyxerr << "Should not happen, but it does. " << endl;
65 return nextAtom().nucleus();
66 return paragraph().isInset(pos()) ? paragraph().getInset(pos()) : 0;
70 InsetBase * DocIterator::prevInset()
72 BOOST_ASSERT(!empty());
76 return prevAtom().nucleus();
77 return paragraph().isInset(pos() - 1) ? paragraph().getInset(pos() - 1) : 0;
81 InsetBase const * DocIterator::prevInset() const
83 BOOST_ASSERT(!empty());
87 return prevAtom().nucleus();
88 return paragraph().isInset(pos() - 1) ? paragraph().getInset(pos() - 1) : 0;
92 MathAtom const & DocIterator::prevAtom() const
94 BOOST_ASSERT(!empty());
95 BOOST_ASSERT(pos() > 0);
96 return cell()[pos() - 1];
100 MathAtom & DocIterator::prevAtom()
102 BOOST_ASSERT(!empty());
103 BOOST_ASSERT(pos() > 0);
104 return cell()[pos() - 1];
108 MathAtom const & DocIterator::nextAtom() const
110 BOOST_ASSERT(!empty());
111 //lyxerr << "lastpos: " << lastpos() << " next atom:\n" << *this << endl;
112 BOOST_ASSERT(pos() < lastpos());
113 return cell()[pos()];
117 MathAtom & DocIterator::nextAtom()
119 BOOST_ASSERT(!empty());
120 //lyxerr << "lastpos: " << lastpos() << " next atom:\n" << *this << endl;
121 BOOST_ASSERT(pos() < lastpos());
122 return cell()[pos()];
126 LyXText * DocIterator::text()
128 BOOST_ASSERT(!empty());
132 LyXText const * DocIterator::text() const
134 BOOST_ASSERT(!empty());
139 Paragraph & DocIterator::paragraph()
142 lyxerr << *this << endl;
143 BOOST_ASSERT(inTexted());
144 return top().paragraph();
148 Paragraph const & DocIterator::paragraph() const
150 BOOST_ASSERT(inTexted());
151 return top().paragraph();
155 Row & DocIterator::textRow()
157 BOOST_ASSERT(!paragraph().rows().empty());
158 return paragraph().getRow(pos());
162 Row const & DocIterator::textRow() const
164 BOOST_ASSERT(!paragraph().rows().empty());
165 return paragraph().getRow(pos());
169 DocIterator::pit_type DocIterator::lastpit() const
171 return inMathed() ? 0 : text()->paragraphs().size() - 1;
175 DocIterator::pos_type DocIterator::lastpos() const
177 return inMathed() ? cell().size() : paragraph().size();
181 DocIterator::idx_type DocIterator::lastidx() const
183 return top().lastidx();
187 size_t DocIterator::nargs() const
189 // assume 1x1 grid for main text
190 return top().nargs();
194 size_t DocIterator::ncols() const
196 // assume 1x1 grid for main text
197 return top().ncols();
201 size_t DocIterator::nrows() const
203 // assume 1x1 grid for main text
204 return top().nrows();
208 DocIterator::row_type DocIterator::row() const
214 DocIterator::col_type DocIterator::col() const
220 MathArray const & DocIterator::cell() const
222 BOOST_ASSERT(inMathed());
227 MathArray & DocIterator::cell()
229 BOOST_ASSERT(inMathed());
234 bool DocIterator::inMathed() const
236 return !empty() && inset().inMathed();
240 bool DocIterator::inTexted() const
242 return !empty() && !inset().inMathed();
246 LyXText * DocIterator::innerText()
248 BOOST_ASSERT(!empty());
249 // Go up until first non-0 text is hit
250 // (innermost text is 0 in mathed)
251 for (int i = depth() - 1; i >= 0; --i)
252 if (slices_[i].text())
253 return slices_[i].text();
257 LyXText const * DocIterator::innerText() const
259 BOOST_ASSERT(!empty());
260 // go up until first non-0 text is hit
261 // (innermost text is 0 in mathed)
262 for (int i = depth() - 1; i >= 0; --i)
263 if (slices_[i].text())
264 return slices_[i].text();
269 InsetBase * DocIterator::innerInsetOfType(int code) const
271 for (int i = depth() - 1; i >= 0; --i)
272 if (slices_[i].inset_->lyxCode() == code)
273 return slices_[i].inset_;
278 void DocIterator::forwardPos()
280 //this dog bites his tail
282 push_back(CursorSlice(*inset_));
286 CursorSlice & tip = top();
287 //lyxerr << "XXX\n" << *this << endl;
289 // this is used twice and shows up in the profiler!
290 pos_type const lastp = lastpos();
292 // move into an inset to the right if possible
295 if (tip.pos() != lastp) {
296 // this is impossible for pos() == size()
298 n = (tip.cell().begin() + tip.pos())->nucleus();
300 if (paragraph().isInset(tip.pos()))
301 n = paragraph().getInset(tip.pos());
305 if (n && n->isActive()) {
306 //lyxerr << "... descend" << endl;
307 push_back(CursorSlice(*n));
311 // otherwise move on one position if possible
312 if (tip.pos() < lastp) {
313 //lyxerr << "... next pos" << endl;
317 //lyxerr << "... no next pos" << endl;
319 // otherwise move on one paragraph if possible
320 if (tip.pit() < lastpit()) {
321 //lyxerr << "... next par" << endl;
326 //lyxerr << "... no next pit" << endl;
328 // otherwise try to move on one cell if possible
329 if (tip.idx() < lastidx()) {
330 //lyxerr << "... next idx" << endl;
336 //lyxerr << "... no next idx" << endl;
338 // otherwise leave inset and jump over inset as a whole
340 // 'top' is invalid now...
346 void DocIterator::forwardPosNoDescend()
348 CursorSlice & tip = top();
349 pos_type const lastp = lastpos();
351 // move on one position if possible
352 if (tip.pos() < lastp) {
353 //lyxerr << "... next pos" << endl;
357 //lyxerr << "... no next pos" << endl;
359 // otherwise move on one paragraph if possible
360 if (tip.pit() < lastpit()) {
361 //lyxerr << "... next par" << endl;
366 //lyxerr << "... no next pit" << endl;
368 // otherwise try to move on one cell if possible
369 if (tip.idx() < lastidx()) {
370 //lyxerr << "... next idx" << endl;
376 //lyxerr << "... no next idx" << endl;
378 // otherwise we can't move on
382 void DocIterator::forwardPar()
387 DocIterator cmp(*this);
390 while (!empty() && (!inTexted() || pos() != 0)) {
392 pos_type const lastp = lastpos();
393 Paragraph const & par = paragraph();
394 pos_type & pos = top().pos();
395 while (pos < lastp && !par.isInset(pos))
402 while (!cmp.empty() && (!cmp.inTexted() || cmp.pos() != 0))
404 BOOST_ASSERT(cmp == *this);
409 void DocIterator::forwardChar()
412 while (!empty() && pos() == lastpos())
417 void DocIterator::forwardInset()
420 while (!empty() && (pos() == lastpos() || nextInset() == 0))
425 void DocIterator::backwardChar()
428 while (!empty() && pos() == lastpos())
433 void DocIterator::backwardPos()
435 //this dog bites his tail
437 push_back(CursorSlice(*inset_));
438 top().idx() = lastidx();
439 top().pit() = lastpit();
440 top().pos() = lastpos();
444 CursorSlice & tip = top();
446 if (tip.pos() != 0) {
448 } else if (tip.pit() != 0) {
450 tip.pos() = lastpos();
452 } else if (tip.idx() != 0) {
454 tip.pit() = lastpit();
455 tip.pos() = lastpos();
462 // move into an inset to the left if possible
466 n = (tip.cell().begin() + tip.pos())->nucleus();
468 if (paragraph().isInset(tip.pos()))
469 n = paragraph().getInset(tip.pos());
472 if (n && n->isActive()) {
473 push_back(CursorSlice(*n));
474 top().idx() = lastidx();
475 top().pit() = lastpit();
476 top().pos() = lastpos();
481 bool DocIterator::hasPart(DocIterator const & it) const
483 // it can't be a part if it is larger
484 if (it.depth() > depth())
487 // as inset adresses are the 'last' level
488 return &it.top().inset() == &slices_[it.depth() - 1].inset();
492 std::ostream & operator<<(std::ostream & os, DocIterator const & dit)
494 for (size_t i = 0, n = dit.depth(); i != n; ++i)
495 os << " " << dit[i] << "\n";
501 ///////////////////////////////////////////////////////
503 StableDocIterator::StableDocIterator(DocIterator const & dit)
505 data_ = dit.internalData();
506 for (size_t i = 0, n = data_.size(); i != n; ++i)
511 DocIterator StableDocIterator::asDocIterator(InsetBase * inset) const
513 // this function re-creates the cache of inset pointers
514 //lyxerr << "converting:\n" << *this << endl;
515 DocIterator dit = DocIterator(*inset);
516 for (size_t i = 0, n = data_.size(); i != n; ++i) {
519 lyxerr << BOOST_CURRENT_FUNCTION
520 << " Should not happen, but does e.g. after C-n C-l C-z S-C-z"
521 << '\n' << "dit: " << dit << '\n'
522 << " lastpos: " << dit.lastpos() << endl;
526 dit.push_back(data_[i]);
527 dit.top().inset_ = inset;
529 inset = dit.nextInset();
531 //lyxerr << "convert:\n" << *this << " to:\n" << dit << endl;
536 std::ostream & operator<<(std::ostream & os, StableDocIterator const & dit)
538 for (size_t i = 0, n = dit.data_.size(); i != n; ++i)
539 os << " " << dit.data_[i] << "\n";