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() const
128 BOOST_ASSERT(!empty());
133 Paragraph & DocIterator::paragraph()
135 BOOST_ASSERT(inTexted());
136 return top().paragraph();
140 Paragraph const & DocIterator::paragraph() const
142 BOOST_ASSERT(inTexted());
143 return top().paragraph();
147 Row & DocIterator::textRow()
149 return paragraph().getRow(pos());
153 Row const & DocIterator::textRow() const
155 return paragraph().getRow(pos());
159 DocIterator::pit_type DocIterator::lastpit() const
161 return inMathed() ? 0 : text()->paragraphs().size() - 1;
165 DocIterator::pos_type DocIterator::lastpos() const
167 return inMathed() ? cell().size() : paragraph().size();
171 DocIterator::idx_type DocIterator::lastidx() const
173 return top().lastidx();
177 size_t DocIterator::nargs() const
179 // assume 1x1 grid for main text
180 return top().nargs();
184 size_t DocIterator::ncols() const
186 // assume 1x1 grid for main text
187 return top().ncols();
191 size_t DocIterator::nrows() const
193 // assume 1x1 grid for main text
194 return top().nrows();
198 DocIterator::row_type DocIterator::row() const
204 DocIterator::col_type DocIterator::col() const
210 MathArray const & DocIterator::cell() const
212 BOOST_ASSERT(inMathed());
217 MathArray & DocIterator::cell()
219 BOOST_ASSERT(inMathed());
224 bool DocIterator::inMathed() const
226 return !empty() && inset().inMathed();
230 bool DocIterator::inTexted() const
232 return !empty() && !inset().inMathed();
236 LyXText * DocIterator::innerText() const
238 BOOST_ASSERT(!empty());
239 // go up until first non-0 text is hit
240 // (innermost text is 0 in mathed)
241 for (int i = size() - 1; i >= 0; --i)
242 if (operator[](i).text())
243 return operator[](i).text();
248 InsetBase * DocIterator::innerInsetOfType(int code) const
250 for (int i = size() - 1; i >= 0; --i)
251 if (operator[](i).inset_->lyxCode() == code)
252 return operator[](i).inset_;
257 void DocIterator::forwardPos()
259 //this dog bites his tail
261 push_back(CursorSlice(*inset_));
265 CursorSlice & top = back();
266 //lyxerr << "XXX\n" << *this << endl;
268 // this is used twice and shows up in the profiler!
269 pos_type const lastp = lastpos();
271 // move into an inset to the right if possible
274 if (top.pos() != lastp) {
275 // this is impossible for pos() == size()
277 n = (top.cell().begin() + top.pos())->nucleus();
279 if (paragraph().isInset(top.pos()))
280 n = paragraph().getInset(top.pos());
284 if (n && n->isActive()) {
285 //lyxerr << "... descend" << endl;
286 push_back(CursorSlice(*n));
290 // otherwise move on one position if possible
291 if (top.pos() < lastp) {
292 //lyxerr << "... next pos" << endl;
296 //lyxerr << "... no next pos" << endl;
298 // otherwise move on one paragraph if possible
299 if (top.pit() < lastpit()) {
300 //lyxerr << "... next par" << endl;
305 //lyxerr << "... no next pit" << endl;
307 // otherwise try to move on one cell if possible
308 if (top.idx() < lastidx()) {
309 //lyxerr << "... next idx" << endl;
315 //lyxerr << "... no next idx" << endl;
317 // otherwise leave inset and jump over inset as a whole
319 // 'top' is invalid now...
325 void DocIterator::forwardPar()
328 while (!empty() && (!inTexted() || pos() != 0))
333 void DocIterator::forwardChar()
336 while (size() != 0 && pos() == lastpos())
341 void DocIterator::forwardInset()
344 while (size() != 0 && (pos() == lastpos() || nextInset() == 0))
349 void DocIterator::backwardChar()
352 while (size() != 0 && pos() == lastpos())
357 void DocIterator::backwardPos()
359 //this dog bites his tail
361 push_back(CursorSlice(*inset_));
362 back().idx() = lastidx();
363 back().pit() = lastpit();
364 back().pos() = lastpos();
368 CursorSlice & top = back();
370 if (top.pos() != 0) {
372 } else if (top.pit() != 0) {
374 top.pos() = lastpos();
376 } else if (top.idx() != 0) {
378 top.pit() = lastpit();
379 top.pos() = lastpos();
386 // move into an inset to the left if possible
390 n = (top.cell().begin() + top.pos())->nucleus();
392 if (paragraph().isInset(top.pos()))
393 n = paragraph().getInset(top.pos());
396 if (n && n->isActive()) {
397 push_back(CursorSlice(*n));
398 back().idx() = lastidx();
399 back().pit() = lastpit();
400 back().pos() = lastpos();
405 bool DocIterator::hasPart(DocIterator const & it) const
407 // it can't be a part if it is larger
408 if (it.size() > size())
411 // as inset adresses are the 'last' level
412 return &it.back().inset() == &operator[](it.size() - 1).inset();
416 std::ostream & operator<<(std::ostream & os, DocIterator const & dit)
418 for (size_t i = 0, n = dit.size(); i != n; ++i)
419 os << " " << dit.operator[](i) << "\n";
425 ///////////////////////////////////////////////////////
427 StableDocIterator::StableDocIterator(const DocIterator & dit)
430 for (size_t i = 0, n = data_.size(); i != n; ++i)
435 DocIterator StableDocIterator::asDocIterator(InsetBase * inset) const
437 // this function re-creates the cache of inset pointers
438 //lyxerr << "converting:\n" << *this << endl;
439 DocIterator dit = DocIterator(*inset);
440 for (size_t i = 0, n = data_.size(); i != n; ++i) {
443 lyxerr << "Should not happen, but does e.g. after C-n C-l C-z S-C-z"
444 << endl << "dit: " << dit << endl
445 << " lastpos: " << dit.lastpos() << endl;
449 dit.push_back(data_[i]);
450 dit.back().inset_ = inset;
452 inset = dit.nextInset();
454 //lyxerr << "convert:\n" << *this << " to:\n" << dit << endl;
459 std::ostream & operator<<(std::ostream & os, StableDocIterator const & dit)
461 for (size_t i = 0, n = dit.data_.size(); i != n; ++i)
462 os << " " << dit.data_[i] << "\n";