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 DocIterator::DocIterator() : inset_(0)
36 DocIterator doc_iterator_begin(InsetBase & inset)
38 DocIterator dit(inset);
44 DocIterator doc_iterator_end(InsetBase & inset)
46 return DocIterator(inset);
50 DocIterator::DocIterator(InsetBase & inset) : inset_(&inset)
54 InsetBase * DocIterator::nextInset()
56 BOOST_ASSERT(!empty());
57 if (pos() == lastpos())
60 return nextAtom().nucleus();
61 return paragraph().isInset(pos()) ? paragraph().getInset(pos()) : 0;
65 InsetBase * DocIterator::prevInset()
67 BOOST_ASSERT(!empty());
71 return prevAtom().nucleus();
72 return paragraph().isInset(pos() - 1) ? paragraph().getInset(pos() - 1) : 0;
76 InsetBase const * DocIterator::prevInset() const
78 BOOST_ASSERT(!empty());
82 return prevAtom().nucleus();
83 return paragraph().isInset(pos() - 1) ? paragraph().getInset(pos() - 1) : 0;
87 MathAtom const & DocIterator::prevAtom() const
89 BOOST_ASSERT(!empty());
90 BOOST_ASSERT(pos() > 0);
91 return cell()[pos() - 1];
95 MathAtom & DocIterator::prevAtom()
97 BOOST_ASSERT(!empty());
98 BOOST_ASSERT(pos() > 0);
99 return cell()[pos() - 1];
103 MathAtom const & DocIterator::nextAtom() const
105 BOOST_ASSERT(!empty());
106 lyxerr << "lastpos: " << lastpos() << " next atom:\n" << *this << endl;
107 BOOST_ASSERT(pos() < lastpos());
108 return cell()[pos()];
112 MathAtom & DocIterator::nextAtom()
114 BOOST_ASSERT(!empty());
115 lyxerr << "lastpos: " << lastpos() << " next atom:\n" << *this << endl;
116 BOOST_ASSERT(pos() < lastpos());
117 return cell()[pos()];
121 LyXText * DocIterator::text() const
123 BOOST_ASSERT(!empty());
128 Paragraph & DocIterator::paragraph()
130 BOOST_ASSERT(inTexted());
131 return top().paragraph();
135 Paragraph const & DocIterator::paragraph() const
137 BOOST_ASSERT(inTexted());
138 return top().paragraph();
142 Row & DocIterator::textRow()
144 return *paragraph().getRow(pos());
148 Row const & DocIterator::textRow() const
150 return *paragraph().getRow(pos());
154 DocIterator::par_type DocIterator::lastpar() const
156 return inMathed() ? 0 : text()->paragraphs().size() - 1;
160 DocIterator::pos_type DocIterator::lastpos() const
162 return inMathed() ? cell().size() : paragraph().size();
166 DocIterator::row_type DocIterator::crow() const
168 return paragraph().row(pos());
172 DocIterator::row_type DocIterator::lastcrow() const
174 return paragraph().rows.size();
178 DocIterator::idx_type DocIterator::lastidx() const
180 return top().lastidx();
184 size_t DocIterator::nargs() const
186 // assume 1x1 grid for main text
187 return top().nargs();
191 size_t DocIterator::ncols() const
193 // assume 1x1 grid for main text
194 return top().ncols();
198 size_t DocIterator::nrows() const
200 // assume 1x1 grid for main text
201 return top().nrows();
205 DocIterator::row_type DocIterator::row() const
211 DocIterator::col_type DocIterator::col() const
217 MathArray const & DocIterator::cell() const
219 BOOST_ASSERT(inMathed());
224 MathArray & DocIterator::cell()
226 BOOST_ASSERT(inMathed());
231 bool DocIterator::inMathed() const
233 return !empty() && inset().inMathed();
237 bool DocIterator::inTexted() const
239 return !empty() && !inset().inMathed();
243 LyXText * DocIterator::innerText() const
245 BOOST_ASSERT(!empty());
246 // go up until first non-0 text is hit
247 // (innermost text is 0 in mathed)
248 for (int i = size() - 1; i >= 0; --i)
249 if (operator[](i).text())
250 return operator[](i).text();
255 InsetBase * DocIterator::innerInsetOfType(int code) const
257 for (int i = size() - 1; i >= 0; --i)
258 if (operator[](i).inset_->lyxCode() == code)
259 return operator[](i).inset_;
264 void DocIterator::forwardPos()
266 //this dog bites his tail
268 push_back(CursorSlice(*inset_));
272 CursorSlice & top = back();
273 //lyxerr << "XXX\n" << *this << endl;
275 // this is used twice and shows up in the profiler!
276 pos_type const lastp = lastpos();
278 // move into an inset to the right if possible
281 if (top.pos() != lastp) {
282 // this is impossible for pos() == size()
284 n = (top.cell().begin() + top.pos())->nucleus();
286 if (paragraph().isInset(top.pos()))
287 n = paragraph().getInset(top.pos());
291 if (n && n->isActive()) {
292 //lyxerr << "... descend" << endl;
293 push_back(CursorSlice(*n));
297 // otherwise move on one position if possible
298 if (top.pos() < lastp) {
299 //lyxerr << "... next pos" << endl;
303 //lyxerr << "... no next pos" << endl;
305 // otherwise move on one paragraph if possible
306 if (top.par() < lastpar()) {
307 //lyxerr << "... next par" << endl;
312 //lyxerr << "... no next par" << endl;
314 // otherwise try to move on one cell if possible
315 if (top.idx() < lastidx()) {
316 //lyxerr << "... next idx" << endl;
322 //lyxerr << "... no next idx" << endl;
324 // otherwise leave inset and jump over inset as a whole
326 // 'top' is invalid now...
332 void DocIterator::forwardPar()
335 while (!empty() && (!inTexted() || pos() != 0))
340 void DocIterator::forwardChar()
343 while (size() != 0 && pos() == lastpos())
348 void DocIterator::forwardInset()
351 while (size() != 0 && (pos() == lastpos() || nextInset() == 0))
356 void DocIterator::backwardChar()
359 while (size() != 0 && pos() == lastpos())
364 void DocIterator::backwardPos()
366 //this dog bites his tail
368 push_back(CursorSlice(*inset_));
369 back().idx() = lastidx();
370 back().par() = lastpar();
371 back().pos() = lastpos();
375 CursorSlice & top = back();
377 if (top.pos() != 0) {
379 } else if (top.par() != 0) {
381 top.pos() = lastpos();
383 } else if (top.idx() != 0) {
385 top.par() = lastpar();
386 top.pos() = lastpos();
393 // move into an inset to the left if possible
397 n = (top.cell().begin() + top.pos())->nucleus();
399 if (paragraph().isInset(top.pos()))
400 n = paragraph().getInset(top.pos());
403 if (n && n->isActive()) {
404 push_back(CursorSlice(*n));
405 back().idx() = lastidx();
406 back().par() = lastpar();
407 back().pos() = lastpos();
412 std::ostream & operator<<(std::ostream & os, DocIterator const & dit)
414 for (size_t i = 0, n = dit.size(); i != n; ++i)
415 os << " " << dit.operator[](i) << "\n";
421 ///////////////////////////////////////////////////////
423 StableDocIterator::StableDocIterator(const DocIterator & dit)
426 for (size_t i = 0, n = data_.size(); i != n; ++i)
432 StableDocIterator::asDocIterator(InsetBase * inset) const
434 // this function re-creates the cache of inset pointers
435 //lyxerr << "converting:\n" << *this << endl;
436 DocIterator dit = DocIterator(*inset);
437 for (size_t i = 0, n = data_.size(); i != n; ++i) {
438 dit.push_back(data_[i]);
439 dit.back().inset_ = inset;
441 inset = dit.nextInset();
443 //lyxerr << "convert:\n" << *this << " to:\n" << dit << endl;
448 std::ostream & operator<<(std::ostream & os, StableDocIterator const & dit)
450 for (size_t i = 0, n = dit.data_.size(); i != n; ++i)
451 os << " " << dit.data_[i] << "\n";