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()
141 BOOST_ASSERT(inTexted());
142 return top().paragraph();
146 Paragraph const & DocIterator::paragraph() const
148 BOOST_ASSERT(inTexted());
149 return top().paragraph();
153 Row & DocIterator::textRow()
155 BOOST_ASSERT(!paragraph().rows().empty());
156 return paragraph().getRow(pos());
160 Row const & DocIterator::textRow() const
162 BOOST_ASSERT(!paragraph().rows().empty());
163 return paragraph().getRow(pos());
167 DocIterator::pit_type DocIterator::lastpit() const
169 return inMathed() ? 0 : text()->paragraphs().size() - 1;
173 DocIterator::pos_type DocIterator::lastpos() const
175 return inMathed() ? cell().size() : paragraph().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()
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();
255 LyXText const * DocIterator::innerText() const
257 BOOST_ASSERT(!empty());
258 // go up until first non-0 text is hit
259 // (innermost text is 0 in mathed)
260 for (int i = size() - 1; i >= 0; --i)
261 if (operator[](i).text())
262 return operator[](i).text();
267 InsetBase * DocIterator::innerInsetOfType(int code) const
269 for (int i = size() - 1; i >= 0; --i)
270 if (operator[](i).inset_->lyxCode() == code)
271 return operator[](i).inset_;
276 void DocIterator::forwardPos()
278 //this dog bites his tail
280 push_back(CursorSlice(*inset_));
284 CursorSlice & top = back();
285 //lyxerr << "XXX\n" << *this << endl;
287 // this is used twice and shows up in the profiler!
288 pos_type const lastp = lastpos();
290 // move into an inset to the right if possible
293 if (top.pos() != lastp) {
294 // this is impossible for pos() == size()
296 n = (top.cell().begin() + top.pos())->nucleus();
298 if (paragraph().isInset(top.pos()))
299 n = paragraph().getInset(top.pos());
303 if (n && n->isActive()) {
304 //lyxerr << "... descend" << endl;
305 push_back(CursorSlice(*n));
309 // otherwise move on one position if possible
310 if (top.pos() < lastp) {
311 //lyxerr << "... next pos" << endl;
315 //lyxerr << "... no next pos" << endl;
317 // otherwise move on one paragraph if possible
318 if (top.pit() < lastpit()) {
319 //lyxerr << "... next par" << endl;
324 //lyxerr << "... no next pit" << endl;
326 // otherwise try to move on one cell if possible
327 if (top.idx() < lastidx()) {
328 //lyxerr << "... next idx" << endl;
334 //lyxerr << "... no next idx" << endl;
336 // otherwise leave inset and jump over inset as a whole
338 // 'top' is invalid now...
344 void DocIterator::forwardPar()
347 while (!empty() && (!inTexted() || pos() != 0))
352 void DocIterator::forwardChar()
355 while (size() != 0 && pos() == lastpos())
360 void DocIterator::forwardInset()
363 while (size() != 0 && (pos() == lastpos() || nextInset() == 0))
368 void DocIterator::backwardChar()
371 while (size() != 0 && pos() == lastpos())
376 void DocIterator::backwardPos()
378 //this dog bites his tail
380 push_back(CursorSlice(*inset_));
381 back().idx() = lastidx();
382 back().pit() = lastpit();
383 back().pos() = lastpos();
387 CursorSlice & top = back();
389 if (top.pos() != 0) {
391 } else if (top.pit() != 0) {
393 top.pos() = lastpos();
395 } else if (top.idx() != 0) {
397 top.pit() = lastpit();
398 top.pos() = lastpos();
405 // move into an inset to the left if possible
409 n = (top.cell().begin() + top.pos())->nucleus();
411 if (paragraph().isInset(top.pos()))
412 n = paragraph().getInset(top.pos());
415 if (n && n->isActive()) {
416 push_back(CursorSlice(*n));
417 back().idx() = lastidx();
418 back().pit() = lastpit();
419 back().pos() = lastpos();
424 bool DocIterator::hasPart(DocIterator const & it) const
426 // it can't be a part if it is larger
427 if (it.size() > size())
430 // as inset adresses are the 'last' level
431 return &it.back().inset() == &operator[](it.size() - 1).inset();
435 std::ostream & operator<<(std::ostream & os, DocIterator const & dit)
437 for (size_t i = 0, n = dit.size(); i != n; ++i)
438 os << " " << dit.operator[](i) << "\n";
444 ///////////////////////////////////////////////////////
446 StableDocIterator::StableDocIterator(const DocIterator & dit)
449 for (size_t i = 0, n = data_.size(); i != n; ++i)
454 DocIterator StableDocIterator::asDocIterator(InsetBase * inset) const
456 // this function re-creates the cache of inset pointers
457 //lyxerr << "converting:\n" << *this << endl;
458 DocIterator dit = DocIterator(*inset);
459 for (size_t i = 0, n = data_.size(); i != n; ++i) {
462 lyxerr << "Should not happen, but does e.g. after C-n C-l C-z S-C-z"
463 << endl << "dit: " << dit << endl
464 << " lastpos: " << dit.lastpos() << endl;
468 dit.push_back(data_[i]);
469 dit.back().inset_ = inset;
471 inset = dit.nextInset();
473 //lyxerr << "convert:\n" << *this << " to:\n" << dit << endl;
478 std::ostream & operator<<(std::ostream & os, StableDocIterator const & dit)
480 for (size_t i = 0, n = dit.data_.size(); i != n; ++i)
481 os << " " << dit.data_[i] << "\n";