2 * This file is part of LyX, the document processor.
3 * Licence details can be found in the file COPYING.
5 * \author Alfredo Braunstein
7 * Full author contact details are available in file CREDITS.
13 #include "PosIterator.h"
16 #include "BufferView.h"
17 #include "cursor_slice.h"
18 #include "iterators.h"
20 #include "paragraph.h"
22 #include "insets/insettext.h"
23 #include "insets/updatableinset.h"
24 #include "insets/inset.h"
26 #include <boost/next_prior.hpp>
31 PosIterator & PosIterator::operator++()
33 BOOST_ASSERT(!stack_.empty());
35 PosIteratorItem & p = stack_.back();
37 if (p.pos < p.pit->size()) {
38 if (InsetOld * inset = p.pit->getInset(p.pos)) {
39 if (LyXText * text = inset->getText(p.index)) {
40 ParagraphList & pl = text->paragraphs();
42 stack_.push_back(PosIteratorItem(&pl, pl.begin(), 0));
53 if (p.pit != p.pl->end() || stack_.size() == 1)
62 PosIterator & PosIterator::operator--()
64 BOOST_ASSERT(!stack_.empty());
66 // try to go one position backwards: if on the start of the
67 // ParagraphList, pops an item
68 PosIteratorItem & p = stack_.back();
71 InsetOld * inset = p.pit->getInset(p.pos);
73 p.index = inset->numParagraphs();
75 if (p.pit == p.pl->begin()) {
76 if (stack_.size() == 1)
79 --stack_.back().index;
82 p.pos = p.pit->size();
85 // try to push an item if there is some left unexplored
86 PosIteratorItem & q = stack_.back();
87 if (q.pos < q.pit->size()) {
88 InsetOld * inset = q.pit->getInset(q.pos);
89 if (inset && q.index > 0) {
90 LyXText * text = inset->getText(q.index - 1);
92 ParagraphList & pl = text->paragraphs();
93 stack_.push_back(PosIteratorItem(&pl, prior(pl.end()), pl.back().size()));
100 bool operator!=(PosIterator const & lhs, PosIterator const & rhs)
102 return !(lhs == rhs);
106 bool operator==(PosIterator const & lhs, PosIterator const & rhs)
109 PosIteratorItem const & li = lhs.stack_.back();
110 PosIteratorItem const & ri = rhs.stack_.back();
112 return (li.pl == ri.pl && li.pit == ri.pit &&
113 (li.pit == li.pl->end() || li.pos == ri.pos));
117 bool PosIterator::at_end() const
119 return pos() == pit()->size();
123 PosIterator::PosIterator(ParagraphList * pl, ParagraphList::iterator pit,
126 stack_.push_back(PosIteratorItem(pl, pit, pos));
130 PosIterator::PosIterator(BufferView & bv)
132 LyXText * text = bv.getLyXText();
133 lyx::pos_type pos = bv.cursor().pos();
134 ParagraphList::iterator pit = text->cursorPar();
136 ParIterator par = bv.buffer()->par_iterator_begin();
137 ParIterator end = bv.buffer()->par_iterator_end();
138 for ( ; par != end; ++par) {
139 if (par.pit() == pit)
143 operator=(par.asPosIterator(pos));
147 InsetOld * PosIterator::inset() const
149 if (stack_.size() == 1)
151 PosIteratorItem const & pi = stack_[stack_.size() - 2];
152 return pi.pit->getInset(pi.pos);
156 int distance(PosIterator const & cur, PosIterator const & end)
160 for (; p != end; ++p, ++count)
166 void advance(PosIterator & cur, int howmuch)
168 for (int i = 0; i < howmuch; ++i)
170 for (int i = 0; i > howmuch; --i)