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::par_type DocIterator::lastpar() 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::row_type DocIterator::crow() const
173 return paragraph().row(pos());
177 DocIterator::row_type DocIterator::lastcrow() const
179 return paragraph().rows.size();
183 DocIterator::idx_type DocIterator::lastidx() const
185 return top().lastidx();
189 size_t DocIterator::nargs() const
191 // assume 1x1 grid for main text
192 return top().nargs();
196 size_t DocIterator::ncols() const
198 // assume 1x1 grid for main text
199 return top().ncols();
203 size_t DocIterator::nrows() const
205 // assume 1x1 grid for main text
206 return top().nrows();
210 DocIterator::row_type DocIterator::row() const
216 DocIterator::col_type DocIterator::col() const
222 MathArray const & DocIterator::cell() const
224 BOOST_ASSERT(inMathed());
229 MathArray & DocIterator::cell()
231 BOOST_ASSERT(inMathed());
236 bool DocIterator::inMathed() const
238 return !empty() && inset().inMathed();
242 bool DocIterator::inTexted() const
244 return !empty() && !inset().inMathed();
248 LyXText * DocIterator::innerText() const
250 BOOST_ASSERT(!empty());
251 // go up until first non-0 text is hit
252 // (innermost text is 0 in mathed)
253 for (int i = size() - 1; i >= 0; --i)
254 if (operator[](i).text())
255 return operator[](i).text();
260 InsetBase * DocIterator::innerInsetOfType(int code) const
262 for (int i = size() - 1; i >= 0; --i)
263 if (operator[](i).inset_->lyxCode() == code)
264 return operator[](i).inset_;
269 void DocIterator::forwardPos()
271 //this dog bites his tail
273 push_back(CursorSlice(*inset_));
277 CursorSlice & top = back();
278 //lyxerr << "XXX\n" << *this << endl;
280 // this is used twice and shows up in the profiler!
281 pos_type const lastp = lastpos();
283 // move into an inset to the right if possible
286 if (top.pos() != lastp) {
287 // this is impossible for pos() == size()
289 n = (top.cell().begin() + top.pos())->nucleus();
291 if (paragraph().isInset(top.pos()))
292 n = paragraph().getInset(top.pos());
296 if (n && n->isActive()) {
297 //lyxerr << "... descend" << endl;
298 push_back(CursorSlice(*n));
302 // otherwise move on one position if possible
303 if (top.pos() < lastp) {
304 //lyxerr << "... next pos" << endl;
308 //lyxerr << "... no next pos" << endl;
310 // otherwise move on one paragraph if possible
311 if (top.par() < lastpar()) {
312 //lyxerr << "... next par" << endl;
317 //lyxerr << "... no next par" << endl;
319 // otherwise try to move on one cell if possible
320 if (top.idx() < lastidx()) {
321 //lyxerr << "... next idx" << endl;
327 //lyxerr << "... no next idx" << endl;
329 // otherwise leave inset and jump over inset as a whole
331 // 'top' is invalid now...
337 void DocIterator::forwardPar()
340 while (!empty() && (!inTexted() || pos() != 0))
345 void DocIterator::forwardChar()
348 while (size() != 0 && pos() == lastpos())
353 void DocIterator::forwardInset()
356 while (size() != 0 && (pos() == lastpos() || nextInset() == 0))
361 void DocIterator::backwardChar()
364 while (size() != 0 && pos() == lastpos())
369 void DocIterator::backwardPos()
371 //this dog bites his tail
373 push_back(CursorSlice(*inset_));
374 back().idx() = lastidx();
375 back().par() = lastpar();
376 back().pos() = lastpos();
380 CursorSlice & top = back();
382 if (top.pos() != 0) {
384 } else if (top.par() != 0) {
386 top.pos() = lastpos();
388 } else if (top.idx() != 0) {
390 top.par() = lastpar();
391 top.pos() = lastpos();
398 // move into an inset to the left if possible
402 n = (top.cell().begin() + top.pos())->nucleus();
404 if (paragraph().isInset(top.pos()))
405 n = paragraph().getInset(top.pos());
408 if (n && n->isActive()) {
409 push_back(CursorSlice(*n));
410 back().idx() = lastidx();
411 back().par() = lastpar();
412 back().pos() = lastpos();
417 bool DocIterator::hasPart(DocIterator const & it) const
419 // it can't be a part if it is larger
420 if (it.size() > size())
423 // as inset adresses are the 'last' level
424 return &it.back().inset() == &operator[](it.size() - 1).inset();
428 std::ostream & operator<<(std::ostream & os, DocIterator const & dit)
430 for (size_t i = 0, n = dit.size(); i != n; ++i)
431 os << " " << dit.operator[](i) << "\n";
437 ///////////////////////////////////////////////////////
439 StableDocIterator::StableDocIterator(const DocIterator & dit)
442 for (size_t i = 0, n = data_.size(); i != n; ++i)
447 DocIterator StableDocIterator::asDocIterator(InsetBase * inset) const
449 // this function re-creates the cache of inset pointers
450 //lyxerr << "converting:\n" << *this << endl;
451 DocIterator dit = DocIterator(*inset);
452 for (size_t i = 0, n = data_.size(); i != n; ++i) {
455 lyxerr << "Should not happen, but does e.g. after C-n C-l C-z S-C-z"
456 << endl << "dit: " << dit << endl
457 << " lastpos: " << dit.lastpos() << endl;
460 dit.push_back(data_[i]);
461 dit.back().inset_ = inset;
463 inset = dit.nextInset();
465 //lyxerr << "convert:\n" << *this << " to:\n" << dit << endl;
470 std::ostream & operator<<(std::ostream & os, StableDocIterator const & dit)
472 for (size_t i = 0, n = dit.data_.size(); i != n; ++i)
473 os << " " << dit.data_[i] << "\n";