3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
7 * \author Lars Gullik Bjønnes
9 * Full author contact details are available in file CREDITS.
15 #include "dociterator.h"
20 #include "paragraph.h"
22 #include "mathed/math_data.h"
23 #include "mathed/math_inset.h"
25 #include <boost/assert.hpp>
30 //we could be able to get rid of this if only every BufferView were
31 //associated to a buffer on construction
32 DocumentIterator::DocumentIterator() : inset_(0)
36 DocumentIterator doc_iterator_begin(InsetBase & inset)
38 DocumentIterator dit(inset);
44 DocumentIterator doc_iterator_end(InsetBase & inset)
46 return DocumentIterator(inset);
50 DocumentIterator::DocumentIterator(InsetBase & inset) : inset_(&inset)
54 InsetBase * DocumentIterator::nextInset()
56 BOOST_ASSERT(!empty());
57 if (pos() == lastpos())
60 return nextAtom().nucleus();
61 return paragraph().isInset(pos()) ? paragraph().getInset(pos()) : 0;
65 InsetBase * DocumentIterator::prevInset()
67 BOOST_ASSERT(!empty());
71 return prevAtom().nucleus();
72 return paragraph().isInset(pos() - 1) ? paragraph().getInset(pos() - 1) : 0;
76 InsetBase const * DocumentIterator::prevInset() const
78 BOOST_ASSERT(!empty());
82 return prevAtom().nucleus();
83 return paragraph().isInset(pos() - 1) ? paragraph().getInset(pos() - 1) : 0;
87 MathAtom const & DocumentIterator::prevAtom() const
89 BOOST_ASSERT(!empty());
90 BOOST_ASSERT(pos() > 0);
91 return cell()[pos() - 1];
95 MathAtom & DocumentIterator::prevAtom()
97 BOOST_ASSERT(!empty());
98 BOOST_ASSERT(pos() > 0);
99 return cell()[pos() - 1];
103 MathAtom const & DocumentIterator::nextAtom() const
105 BOOST_ASSERT(!empty());
106 BOOST_ASSERT(pos() < lastpos());
107 return cell()[pos()];
111 MathAtom & DocumentIterator::nextAtom()
113 BOOST_ASSERT(!empty());
114 BOOST_ASSERT(pos() < lastpos());
115 return cell()[pos()];
119 LyXText * DocumentIterator::text() const
121 BOOST_ASSERT(!empty());
126 Paragraph & DocumentIterator::paragraph()
128 BOOST_ASSERT(inTexted());
129 return top().paragraph();
133 Paragraph const & DocumentIterator::paragraph() const
135 BOOST_ASSERT(inTexted());
136 return top().paragraph();
140 Row & DocumentIterator::textRow()
142 return *paragraph().getRow(pos());
146 Row const & DocumentIterator::textRow() const
148 return *paragraph().getRow(pos());
152 DocumentIterator::par_type DocumentIterator::lastpar() const
154 return inMathed() ? 0 : text()->paragraphs().size() - 1;
158 DocumentIterator::pos_type DocumentIterator::lastpos() const
160 return inMathed() ? cell().size() : paragraph().size();
164 DocumentIterator::row_type DocumentIterator::crow() const
166 return paragraph().row(pos());
170 DocumentIterator::row_type DocumentIterator::lastcrow() const
172 return paragraph().rows.size();
176 DocumentIterator::idx_type DocumentIterator::lastidx() const
178 return top().lastidx();
182 size_t DocumentIterator::nargs() const
184 // assume 1x1 grid for main text
185 return top().nargs();
189 size_t DocumentIterator::ncols() const
191 // assume 1x1 grid for main text
192 return top().ncols();
196 size_t DocumentIterator::nrows() const
198 // assume 1x1 grid for main text
199 return top().nrows();
203 DocumentIterator::row_type DocumentIterator::row() const
209 DocumentIterator::col_type DocumentIterator::col() const
215 MathArray const & DocumentIterator::cell() const
217 BOOST_ASSERT(inMathed());
222 MathArray & DocumentIterator::cell()
224 BOOST_ASSERT(inMathed());
229 bool DocumentIterator::inMathed() const
231 return !empty() && inset().inMathed();
235 bool DocumentIterator::inTexted() const
237 return !empty() && !inset().inMathed();
241 LyXText * DocumentIterator::innerText() const
243 BOOST_ASSERT(!empty());
244 // go up until first non-0 text is hit
245 // (innermost text is 0 in mathed)
246 for (int i = size() - 1; i >= 0; --i)
247 if (operator[](i).text())
248 return operator[](i).text();
253 InsetBase * DocumentIterator::innerInsetOfType(int code) const
255 for (int i = size() - 1; i >= 0; --i)
256 if (operator[](i).inset_->lyxCode() == code)
257 return operator[](i).inset_;
262 void DocumentIterator::forwardPos()
264 //this dog bites his tail
266 push_back(CursorSlice(*inset_));
270 CursorSlice & top = back();
271 //lyxerr << "XXX\n" << *this << endl;
273 // this is used twice and shows up in the profiler!
274 pos_type const lastp = lastpos();
276 // move into an inset to the right if possible
279 if (top.pos() != lastp) {
280 // this is impossible for pos() == size()
282 n = (top.cell().begin() + top.pos())->nucleus();
284 if (paragraph().isInset(top.pos()))
285 n = paragraph().getInset(top.pos());
289 if (n && n->isActive()) {
290 //lyxerr << "... descend" << endl;
291 push_back(CursorSlice(*n));
295 // otherwise move on one position if possible
296 if (top.pos() < lastp) {
297 //lyxerr << "... next pos" << endl;
301 //lyxerr << "... no next pos" << endl;
303 // otherwise move on one paragraph if possible
304 if (top.par() < lastpar()) {
305 //lyxerr << "... next par" << endl;
310 //lyxerr << "... no next par" << endl;
312 // otherwise try to move on one cell if possible
313 if (top.idx() < lastidx()) {
314 //lyxerr << "... next idx" << endl;
320 //lyxerr << "... no next idx" << endl;
322 // otherwise leave inset and jump over inset as a whole
324 // 'top' is invalid now...
330 void DocumentIterator::forwardPar()
333 while (!empty() && (!inTexted() || pos() != 0))
338 void DocumentIterator::forwardChar()
341 while (size() != 0 && pos() == lastpos())
346 void DocumentIterator::forwardInset()
349 while (size() != 0 && (pos() == lastpos() || nextInset() == 0))
354 void DocumentIterator::backwardChar()
357 while (size() != 0 && pos() == lastpos())
362 void DocumentIterator::backwardPos()
364 //this dog bites his tail
366 push_back(CursorSlice(*inset_));
367 back().idx() = lastidx();
368 back().par() = lastpar();
369 back().pos() = lastpos();
373 CursorSlice & top = back();
375 if (top.pos() != 0) {
377 } else if (top.par() != 0) {
379 top.pos() = lastpos();
381 } else if (top.idx() != 0) {
383 top.par() = lastpar();
384 top.pos() = lastpos();
391 // move into an inset to the left if possible
395 n = (top.cell().begin() + top.pos())->nucleus();
397 if (paragraph().isInset(top.pos()))
398 n = paragraph().getInset(top.pos());
401 if (n && n->isActive()) {
402 push_back(CursorSlice(*n));
403 back().idx() = lastidx();
404 back().par() = lastpar();
405 back().pos() = lastpos();
410 std::ostream & operator<<(std::ostream & os, DocumentIterator const & dit)
412 for (size_t i = 0, n = dit.size(); i != n; ++i)
413 os << " " << dit.operator[](i) << "\n";
419 ///////////////////////////////////////////////////////
421 StableDocumentIterator::StableDocumentIterator(const DocumentIterator & dit)
424 for (size_t i = 0, n = data_.size(); i != n; ++i)
430 StableDocumentIterator::asDocumentIterator(InsetBase * inset) const
432 // this function re-creates the cache of inset pointers
433 //lyxerr << "converting:\n" << *this << endl;
434 DocumentIterator dit = DocumentIterator(*inset);
435 for (size_t i = 0, n = data_.size(); i != n; ++i) {
436 dit.push_back(data_[i]);
437 dit.back().inset_ = inset;
439 inset = dit.nextInset();
441 //lyxerr << "convert:\n" << *this << " to:\n" << dit << endl;
446 std::ostream & operator<<(std::ostream & os, StableDocumentIterator const & dit)
448 for (size_t i = 0, n = dit.data_.size(); i != n; ++i)
449 os << " " << dit.data_[i] << "\n";