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()
142 lyxerr << *this << endl;
143 BOOST_ASSERT(inTexted());
144 return top().paragraph();
148 Paragraph const & DocIterator::paragraph() const
150 BOOST_ASSERT(inTexted());
151 return top().paragraph();
155 Row & DocIterator::textRow()
157 BOOST_ASSERT(!paragraph().rows().empty());
158 return paragraph().getRow(pos());
162 Row const & DocIterator::textRow() const
164 BOOST_ASSERT(!paragraph().rows().empty());
165 return paragraph().getRow(pos());
169 DocIterator::pit_type DocIterator::lastpit() const
171 return inMathed() ? 0 : text()->paragraphs().size() - 1;
175 DocIterator::pos_type DocIterator::lastpos() const
177 return inMathed() ? cell().size() : paragraph().size();
181 DocIterator::idx_type DocIterator::lastidx() const
183 return top().lastidx();
187 size_t DocIterator::nargs() const
189 // assume 1x1 grid for main text
190 return top().nargs();
194 size_t DocIterator::ncols() const
196 // assume 1x1 grid for main text
197 return top().ncols();
201 size_t DocIterator::nrows() const
203 // assume 1x1 grid for main text
204 return top().nrows();
208 DocIterator::row_type DocIterator::row() const
214 DocIterator::col_type DocIterator::col() const
220 MathArray const & DocIterator::cell() const
222 BOOST_ASSERT(inMathed());
227 MathArray & DocIterator::cell()
229 BOOST_ASSERT(inMathed());
234 bool DocIterator::inMathed() const
236 return !empty() && inset().inMathed();
240 bool DocIterator::inTexted() const
242 return !empty() && !inset().inMathed();
246 LyXText * DocIterator::innerText()
248 BOOST_ASSERT(!empty());
249 // Go up until first non-0 text is hit
250 // (innermost text is 0 in mathed)
251 for (int i = depth() - 1; i >= 0; --i)
252 if (slices_[i].text())
253 return slices_[i].text();
257 LyXText const * DocIterator::innerText() const
259 BOOST_ASSERT(!empty());
260 // go up until first non-0 text is hit
261 // (innermost text is 0 in mathed)
262 for (int i = depth() - 1; i >= 0; --i)
263 if (slices_[i].text())
264 return slices_[i].text();
269 InsetBase * DocIterator::innerInsetOfType(int code) const
271 for (int i = depth() - 1; i >= 0; --i)
272 if (slices_[i].inset_->lyxCode() == code)
273 return slices_[i].inset_;
278 void DocIterator::forwardPos()
280 //this dog bites his tail
282 push_back(CursorSlice(*inset_));
286 CursorSlice & tip = top();
287 //lyxerr << "XXX\n" << *this << endl;
289 // this is used twice and shows up in the profiler!
290 pos_type const lastp = lastpos();
292 // move into an inset to the right if possible
295 if (tip.pos() != lastp) {
296 // this is impossible for pos() == size()
298 n = (tip.cell().begin() + tip.pos())->nucleus();
300 if (paragraph().isInset(tip.pos()))
301 n = paragraph().getInset(tip.pos());
305 if (n && n->isActive()) {
306 //lyxerr << "... descend" << endl;
307 push_back(CursorSlice(*n));
311 // otherwise move on one position if possible
312 if (tip.pos() < lastp) {
313 //lyxerr << "... next pos" << endl;
317 //lyxerr << "... no next pos" << endl;
319 // otherwise move on one paragraph if possible
320 if (tip.pit() < lastpit()) {
321 //lyxerr << "... next par" << endl;
326 //lyxerr << "... no next pit" << endl;
328 // otherwise try to move on one cell if possible
329 if (tip.idx() < lastidx()) {
330 //lyxerr << "... next idx" << endl;
336 //lyxerr << "... no next idx" << endl;
338 // otherwise leave inset and jump over inset as a whole
340 // 'top' is invalid now...
346 void DocIterator::forwardPosNoDescend()
348 CursorSlice & tip = top();
349 pos_type const lastp = lastpos();
351 // move on one position if possible
352 if (tip.pos() < lastp) {
353 //lyxerr << "... next pos" << endl;
357 //lyxerr << "... no next pos" << endl;
359 // otherwise move on one paragraph if possible
360 if (tip.pit() < lastpit()) {
361 //lyxerr << "... next par" << endl;
366 //lyxerr << "... no next pit" << endl;
368 // otherwise try to move on one cell if possible
369 if (tip.idx() < lastidx()) {
370 //lyxerr << "... next idx" << endl;
376 //lyxerr << "... no next idx" << endl;
378 // otherwise we can't move on
382 void DocIterator::forwardPar()
385 while (!empty() && (!inTexted() || pos() != 0))
390 void DocIterator::forwardChar()
393 while (!empty() && pos() == lastpos())
398 void DocIterator::forwardInset()
401 while (!empty() && (pos() == lastpos() || nextInset() == 0))
406 void DocIterator::backwardChar()
409 while (!empty() && pos() == lastpos())
414 void DocIterator::backwardPos()
416 //this dog bites his tail
418 push_back(CursorSlice(*inset_));
419 top().idx() = lastidx();
420 top().pit() = lastpit();
421 top().pos() = lastpos();
425 CursorSlice & tip = top();
427 if (tip.pos() != 0) {
429 } else if (tip.pit() != 0) {
431 tip.pos() = lastpos();
433 } else if (tip.idx() != 0) {
435 tip.pit() = lastpit();
436 tip.pos() = lastpos();
443 // move into an inset to the left if possible
447 n = (tip.cell().begin() + tip.pos())->nucleus();
449 if (paragraph().isInset(tip.pos()))
450 n = paragraph().getInset(tip.pos());
453 if (n && n->isActive()) {
454 push_back(CursorSlice(*n));
455 top().idx() = lastidx();
456 top().pit() = lastpit();
457 top().pos() = lastpos();
462 bool DocIterator::hasPart(DocIterator const & it) const
464 // it can't be a part if it is larger
465 if (it.depth() > depth())
468 // as inset adresses are the 'last' level
469 return &it.top().inset() == &slices_[it.depth() - 1].inset();
473 std::ostream & operator<<(std::ostream & os, DocIterator const & dit)
475 for (size_t i = 0, n = dit.depth(); i != n; ++i)
476 os << " " << dit[i] << "\n";
482 ///////////////////////////////////////////////////////
484 StableDocIterator::StableDocIterator(DocIterator const & dit)
486 data_ = dit.internalData();
487 for (size_t i = 0, n = data_.size(); i != n; ++i)
492 DocIterator StableDocIterator::asDocIterator(InsetBase * inset) const
494 // this function re-creates the cache of inset pointers
495 //lyxerr << "converting:\n" << *this << endl;
496 DocIterator dit = DocIterator(*inset);
497 for (size_t i = 0, n = data_.size(); i != n; ++i) {
500 lyxerr << BOOST_CURRENT_FUNCTION
501 << " Should not happen, but does e.g. after C-n C-l C-z S-C-z"
502 << '\n' << "dit: " << dit << '\n'
503 << " lastpos: " << dit.lastpos() << endl;
507 dit.push_back(data_[i]);
508 dit.top().inset_ = inset;
510 inset = dit.nextInset();
512 //lyxerr << "convert:\n" << *this << " to:\n" << dit << endl;
517 std::ostream & operator<<(std::ostream & os, StableDocIterator const & dit)
519 for (size_t i = 0, n = dit.data_.size(); i != n; ++i)
520 os << " " << dit.data_[i] << "\n";