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>
25 #include <boost/current_function.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()
37 DocIterator::DocIterator(InsetBase & inset)
42 DocIterator doc_iterator_begin(InsetBase & inset)
44 DocIterator dit(inset);
50 DocIterator doc_iterator_end(InsetBase & inset)
52 return DocIterator(inset);
56 InsetBase * DocIterator::nextInset()
58 BOOST_ASSERT(!empty());
59 if (pos() == lastpos())
61 if (pos() > lastpos()) {
62 lyxerr << "Should not happen, but it does. " << endl;
66 return nextAtom().nucleus();
67 return paragraph().isInset(pos()) ? paragraph().getInset(pos()) : 0;
71 InsetBase * DocIterator::prevInset()
73 BOOST_ASSERT(!empty());
77 return prevAtom().nucleus();
78 return paragraph().isInset(pos() - 1) ? paragraph().getInset(pos() - 1) : 0;
82 InsetBase const * DocIterator::prevInset() const
84 BOOST_ASSERT(!empty());
88 return prevAtom().nucleus();
89 return paragraph().isInset(pos() - 1) ? paragraph().getInset(pos() - 1) : 0;
93 MathAtom const & DocIterator::prevAtom() const
95 BOOST_ASSERT(!empty());
96 BOOST_ASSERT(pos() > 0);
97 return cell()[pos() - 1];
101 MathAtom & DocIterator::prevAtom()
103 BOOST_ASSERT(!empty());
104 BOOST_ASSERT(pos() > 0);
105 return cell()[pos() - 1];
109 MathAtom const & DocIterator::nextAtom() const
111 BOOST_ASSERT(!empty());
112 //lyxerr << "lastpos: " << lastpos() << " next atom:\n" << *this << endl;
113 BOOST_ASSERT(pos() < lastpos());
114 return cell()[pos()];
118 MathAtom & DocIterator::nextAtom()
120 BOOST_ASSERT(!empty());
121 //lyxerr << "lastpos: " << lastpos() << " next atom:\n" << *this << endl;
122 BOOST_ASSERT(pos() < lastpos());
123 return cell()[pos()];
127 LyXText * DocIterator::text()
129 BOOST_ASSERT(!empty());
133 LyXText const * DocIterator::text() const
135 BOOST_ASSERT(!empty());
140 Paragraph & DocIterator::paragraph()
143 lyxerr << *this << endl;
144 BOOST_ASSERT(inTexted());
145 return top().paragraph();
149 Paragraph const & DocIterator::paragraph() const
151 BOOST_ASSERT(inTexted());
152 return top().paragraph();
156 Row & DocIterator::textRow()
158 BOOST_ASSERT(!paragraph().rows().empty());
159 return paragraph().getRow(pos());
163 Row const & DocIterator::textRow() const
165 BOOST_ASSERT(!paragraph().rows().empty());
166 return paragraph().getRow(pos());
170 DocIterator::pit_type DocIterator::lastpit() const
172 return inMathed() ? 0 : text()->paragraphs().size() - 1;
176 DocIterator::pos_type DocIterator::lastpos() const
178 return inMathed() ? cell().size() : paragraph().size();
182 DocIterator::idx_type DocIterator::lastidx() const
184 return top().lastidx();
188 size_t DocIterator::nargs() const
190 // assume 1x1 grid for main text
191 return top().nargs();
195 size_t DocIterator::ncols() const
197 // assume 1x1 grid for main text
198 return top().ncols();
202 size_t DocIterator::nrows() const
204 // assume 1x1 grid for main text
205 return top().nrows();
209 DocIterator::row_type DocIterator::row() const
215 DocIterator::col_type DocIterator::col() const
221 MathArray const & DocIterator::cell() const
223 BOOST_ASSERT(inMathed());
228 MathArray & DocIterator::cell()
230 BOOST_ASSERT(inMathed());
235 bool DocIterator::inMathed() const
237 return !empty() && inset().inMathed();
241 bool DocIterator::inTexted() const
243 return !empty() && !inset().inMathed();
247 LyXText * DocIterator::innerText()
249 BOOST_ASSERT(!empty());
250 // Go up until first non-0 text is hit
251 // (innermost text is 0 in mathed)
252 for (int i = depth() - 1; i >= 0; --i)
253 if (slices_[i].text())
254 return slices_[i].text();
258 LyXText const * DocIterator::innerText() const
260 BOOST_ASSERT(!empty());
261 // go up until first non-0 text is hit
262 // (innermost text is 0 in mathed)
263 for (int i = depth() - 1; i >= 0; --i)
264 if (slices_[i].text())
265 return slices_[i].text();
270 InsetBase * DocIterator::innerInsetOfType(int code) const
272 for (int i = depth() - 1; i >= 0; --i)
273 if (slices_[i].inset_->lyxCode() == code)
274 return slices_[i].inset_;
279 void DocIterator::forwardPos()
281 //this dog bites his tail
283 push_back(CursorSlice(*inset_));
287 CursorSlice & tip = top();
288 //lyxerr << "XXX\n" << *this << endl;
290 // this is used twice and shows up in the profiler!
291 pos_type const lastp = lastpos();
293 // move into an inset to the right if possible
296 if (tip.pos() != lastp) {
297 // this is impossible for pos() == size()
299 n = (tip.cell().begin() + tip.pos())->nucleus();
301 if (paragraph().isInset(tip.pos()))
302 n = paragraph().getInset(tip.pos());
306 if (n && n->isActive()) {
307 //lyxerr << "... descend" << endl;
308 push_back(CursorSlice(*n));
312 // otherwise move on one position if possible
313 if (tip.pos() < lastp) {
314 //lyxerr << "... next pos" << endl;
318 //lyxerr << "... no next pos" << endl;
320 // otherwise move on one paragraph if possible
321 if (tip.pit() < lastpit()) {
322 //lyxerr << "... next par" << endl;
327 //lyxerr << "... no next pit" << endl;
329 // otherwise try to move on one cell if possible
330 if (tip.idx() < lastidx()) {
331 //lyxerr << "... next idx" << endl;
337 //lyxerr << "... no next idx" << endl;
339 // otherwise leave inset and jump over inset as a whole
341 // 'top' is invalid now...
347 void DocIterator::forwardPosNoDescend()
349 CursorSlice & tip = top();
350 pos_type const lastp = lastpos();
352 // move on one position if possible
353 if (tip.pos() < lastp) {
354 //lyxerr << "... next pos" << endl;
358 //lyxerr << "... no next pos" << endl;
360 // otherwise move on one paragraph if possible
361 if (tip.pit() < lastpit()) {
362 //lyxerr << "... next par" << endl;
367 //lyxerr << "... no next pit" << endl;
369 // otherwise try to move on one cell if possible
370 if (tip.idx() < lastidx()) {
371 //lyxerr << "... next idx" << endl;
377 //lyxerr << "... no next idx" << endl;
379 // otherwise we can't move on
383 void DocIterator::forwardPar()
388 DocIterator cmp(*this);
391 while (!empty() && (!inTexted() || pos() != 0)) {
393 pos_type const lastp = lastpos();
394 Paragraph const & par = paragraph();
395 pos_type & pos = top().pos();
396 while (pos < lastp && !par.isInset(pos))
403 while (!cmp.empty() && (!cmp.inTexted() || cmp.pos() != 0))
405 BOOST_ASSERT(cmp == *this);
410 void DocIterator::forwardChar()
413 while (!empty() && pos() == lastpos())
418 void DocIterator::forwardInset()
421 while (!empty() && (pos() == lastpos() || nextInset() == 0))
426 void DocIterator::backwardChar()
429 while (!empty() && pos() == lastpos())
434 void DocIterator::backwardPos()
436 //this dog bites his tail
438 push_back(CursorSlice(*inset_));
439 top().idx() = lastidx();
440 top().pit() = lastpit();
441 top().pos() = lastpos();
445 CursorSlice & tip = top();
447 if (tip.pos() != 0) {
449 } else if (tip.pit() != 0) {
451 tip.pos() = lastpos();
453 } else if (tip.idx() != 0) {
455 tip.pit() = lastpit();
456 tip.pos() = lastpos();
463 // move into an inset to the left if possible
467 n = (tip.cell().begin() + tip.pos())->nucleus();
469 if (paragraph().isInset(tip.pos()))
470 n = paragraph().getInset(tip.pos());
473 if (n && n->isActive()) {
474 push_back(CursorSlice(*n));
475 top().idx() = lastidx();
476 top().pit() = lastpit();
477 top().pos() = lastpos();
482 bool DocIterator::hasPart(DocIterator const & it) const
484 // it can't be a part if it is larger
485 if (it.depth() > depth())
488 // as inset adresses are the 'last' level
489 return &it.top().inset() == &slices_[it.depth() - 1].inset();
493 std::ostream & operator<<(std::ostream & os, DocIterator const & dit)
495 for (size_t i = 0, n = dit.depth(); i != n; ++i)
496 os << " " << dit[i] << "\n";
502 ///////////////////////////////////////////////////////
504 StableDocIterator::StableDocIterator(DocIterator const & dit)
506 data_ = dit.internalData();
507 for (size_t i = 0, n = data_.size(); i != n; ++i)
512 DocIterator StableDocIterator::asDocIterator(InsetBase * inset) const
514 // this function re-creates the cache of inset pointers
515 //lyxerr << "converting:\n" << *this << endl;
516 DocIterator dit = DocIterator(*inset);
517 for (size_t i = 0, n = data_.size(); i != n; ++i) {
520 lyxerr << BOOST_CURRENT_FUNCTION
521 << " Should not happen, but does e.g. after C-n C-l C-z S-C-z"
522 << '\n' << "dit: " << dit << '\n'
523 << " lastpos: " << dit.lastpos() << endl;
527 dit.push_back(data_[i]);
528 dit.top().inset_ = inset;
530 inset = dit.nextInset();
532 //lyxerr << "convert:\n" << *this << " to:\n" << dit << endl;
537 std::ostream & operator<<(std::ostream & os, StableDocIterator const & dit)
539 for (size_t i = 0, n = dit.data_.size(); i != n; ++i)
540 os << " " << dit.data_[i] << "\n";
545 bool operator==(StableDocIterator const & dit1, StableDocIterator const & dit2)
547 return dit1.data_ == dit2.data_;