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())
61 return nextAtom().nucleus();
62 return paragraph().isInset(pos()) ? paragraph().getInset(pos()) : 0;
66 InsetBase * DocIterator::prevInset()
68 BOOST_ASSERT(!empty());
72 return prevAtom().nucleus();
73 return paragraph().isInset(pos() - 1) ? paragraph().getInset(pos() - 1) : 0;
77 InsetBase const * DocIterator::prevInset() const
79 BOOST_ASSERT(!empty());
83 return prevAtom().nucleus();
84 return paragraph().isInset(pos() - 1) ? paragraph().getInset(pos() - 1) : 0;
88 MathAtom const & DocIterator::prevAtom() const
90 BOOST_ASSERT(!empty());
91 BOOST_ASSERT(pos() > 0);
92 return cell()[pos() - 1];
96 MathAtom & DocIterator::prevAtom()
98 BOOST_ASSERT(!empty());
99 BOOST_ASSERT(pos() > 0);
100 return cell()[pos() - 1];
104 MathAtom const & DocIterator::nextAtom() const
106 BOOST_ASSERT(!empty());
107 //lyxerr << "lastpos: " << lastpos() << " next atom:\n" << *this << endl;
108 BOOST_ASSERT(pos() < lastpos());
109 return cell()[pos()];
113 MathAtom & DocIterator::nextAtom()
115 BOOST_ASSERT(!empty());
116 //lyxerr << "lastpos: " << lastpos() << " next atom:\n" << *this << endl;
117 BOOST_ASSERT(pos() < lastpos());
118 return cell()[pos()];
122 LyXText * DocIterator::text() const
124 BOOST_ASSERT(!empty());
129 Paragraph & DocIterator::paragraph()
131 BOOST_ASSERT(inTexted());
132 return top().paragraph();
136 Paragraph const & DocIterator::paragraph() const
138 BOOST_ASSERT(inTexted());
139 return top().paragraph();
143 Row & DocIterator::textRow()
145 return *paragraph().getRow(pos());
149 Row const & DocIterator::textRow() const
151 return *paragraph().getRow(pos());
155 DocIterator::par_type DocIterator::lastpar() const
157 return inMathed() ? 0 : text()->paragraphs().size() - 1;
161 DocIterator::pos_type DocIterator::lastpos() const
163 return inMathed() ? cell().size() : paragraph().size();
167 DocIterator::row_type DocIterator::crow() const
169 return paragraph().row(pos());
173 DocIterator::row_type DocIterator::lastcrow() const
175 return paragraph().rows.size();
179 DocIterator::idx_type DocIterator::lastidx() const
181 return top().lastidx();
185 size_t DocIterator::nargs() const
187 // assume 1x1 grid for main text
188 return top().nargs();
192 size_t DocIterator::ncols() const
194 // assume 1x1 grid for main text
195 return top().ncols();
199 size_t DocIterator::nrows() const
201 // assume 1x1 grid for main text
202 return top().nrows();
206 DocIterator::row_type DocIterator::row() const
212 DocIterator::col_type DocIterator::col() const
218 MathArray const & DocIterator::cell() const
220 BOOST_ASSERT(inMathed());
225 MathArray & DocIterator::cell()
227 BOOST_ASSERT(inMathed());
232 bool DocIterator::inMathed() const
234 return !empty() && inset().inMathed();
238 bool DocIterator::inTexted() const
240 return !empty() && !inset().inMathed();
244 LyXText * DocIterator::innerText() const
246 BOOST_ASSERT(!empty());
247 // go up until first non-0 text is hit
248 // (innermost text is 0 in mathed)
249 for (int i = size() - 1; i >= 0; --i)
250 if (operator[](i).text())
251 return operator[](i).text();
256 InsetBase * DocIterator::innerInsetOfType(int code) const
258 for (int i = size() - 1; i >= 0; --i)
259 if (operator[](i).inset_->lyxCode() == code)
260 return operator[](i).inset_;
265 void DocIterator::forwardPos()
267 //this dog bites his tail
269 push_back(CursorSlice(*inset_));
273 CursorSlice & top = back();
274 //lyxerr << "XXX\n" << *this << endl;
276 // this is used twice and shows up in the profiler!
277 pos_type const lastp = lastpos();
279 // move into an inset to the right if possible
282 if (top.pos() != lastp) {
283 // this is impossible for pos() == size()
285 n = (top.cell().begin() + top.pos())->nucleus();
287 if (paragraph().isInset(top.pos()))
288 n = paragraph().getInset(top.pos());
292 if (n && n->isActive()) {
293 //lyxerr << "... descend" << endl;
294 push_back(CursorSlice(*n));
298 // otherwise move on one position if possible
299 if (top.pos() < lastp) {
300 //lyxerr << "... next pos" << endl;
304 //lyxerr << "... no next pos" << endl;
306 // otherwise move on one paragraph if possible
307 if (top.par() < lastpar()) {
308 //lyxerr << "... next par" << endl;
313 //lyxerr << "... no next par" << endl;
315 // otherwise try to move on one cell if possible
316 if (top.idx() < lastidx()) {
317 //lyxerr << "... next idx" << endl;
323 //lyxerr << "... no next idx" << endl;
325 // otherwise leave inset and jump over inset as a whole
327 // 'top' is invalid now...
333 void DocIterator::forwardPar()
336 while (!empty() && (!inTexted() || pos() != 0))
341 void DocIterator::forwardChar()
344 while (size() != 0 && pos() == lastpos())
349 void DocIterator::forwardInset()
352 while (size() != 0 && (pos() == lastpos() || nextInset() == 0))
357 void DocIterator::backwardChar()
360 while (size() != 0 && pos() == lastpos())
365 void DocIterator::backwardPos()
367 //this dog bites his tail
369 push_back(CursorSlice(*inset_));
370 back().idx() = lastidx();
371 back().par() = lastpar();
372 back().pos() = lastpos();
376 CursorSlice & top = back();
378 if (top.pos() != 0) {
380 } else if (top.par() != 0) {
382 top.pos() = lastpos();
384 } else if (top.idx() != 0) {
386 top.par() = lastpar();
387 top.pos() = lastpos();
394 // move into an inset to the left if possible
398 n = (top.cell().begin() + top.pos())->nucleus();
400 if (paragraph().isInset(top.pos()))
401 n = paragraph().getInset(top.pos());
404 if (n && n->isActive()) {
405 push_back(CursorSlice(*n));
406 back().idx() = lastidx();
407 back().par() = lastpar();
408 back().pos() = lastpos();
413 std::ostream & operator<<(std::ostream & os, DocIterator const & dit)
415 for (size_t i = 0, n = dit.size(); i != n; ++i)
416 os << " " << dit.operator[](i) << "\n";
422 ///////////////////////////////////////////////////////
424 StableDocIterator::StableDocIterator(const DocIterator & dit)
427 for (size_t i = 0, n = data_.size(); i != n; ++i)
433 StableDocIterator::asDocIterator(InsetBase * inset) const
435 // this function re-creates the cache of inset pointers
436 //lyxerr << "converting:\n" << *this << endl;
437 DocIterator dit = DocIterator(*inset);
438 for (size_t i = 0, n = data_.size(); i != n; ++i) {
439 dit.push_back(data_[i]);
440 dit.back().inset_ = inset;
442 inset = dit.nextInset();
444 //lyxerr << "convert:\n" << *this << " to:\n" << dit << endl;
449 std::ostream & operator<<(std::ostream & os, StableDocIterator const & dit)
451 for (size_t i = 0, n = dit.data_.size(); i != n; ++i)
452 os << " " << dit.data_[i] << "\n";