]> git.lyx.org Git - lyx.git/blob - src/iterators.C
Georg's tex2lyx patch.
[lyx.git] / src / iterators.C
1 /* \file iterators.C
2  * This file is part of LyX, the document processor.
3  * Licence details can be found in the file COPYING.
4  *
5  * \author unknown
6  * \author Lars Gullik Bjønnes
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11
12 #include <config.h>
13
14 #include "iterators.h"
15 #include "paragraph.h"
16 #include "cursor.h"
17 #include "buffer.h"
18 #include "BufferView.h"
19 #include "dispatchresult.h"
20
21 #include "insets/inset.h"
22 #include "insets/updatableinset.h"
23 #include "insets/insettext.h"
24
25 #include <boost/next_prior.hpp>
26
27 using lyx::par_type;
28
29 using boost::next;
30
31 ///
32 /// ParPosition
33 ///
34
35
36 ParPosition::ParPosition(par_type p, ParagraphList const & pl)
37         : pit(p), plist(&pl)
38 {
39         if (p != par_type(pl.size()))
40                 it.reset(const_cast<InsetList&>(pl[p].insetlist).begin());
41 }
42
43
44 bool operator==(ParPosition const & pos1, ParPosition const & pos2)
45 {
46         return pos1.pit == pos2.pit;
47 }
48
49
50 bool operator!=(ParPosition const & pos1, ParPosition const & pos2)
51 {
52         return !(pos1 == pos2);
53 }
54
55
56 ///
57 /// ParIterator
58 ///
59
60 ParIterator::ParIterator(par_type pit, ParagraphList const & pl)
61 {
62         positions_.push_back(ParPosition(pit, pl));
63 }
64
65
66 ParIterator::~ParIterator()
67 {}
68
69
70 ParIterator::ParIterator(ParIterator const & pi)
71         : positions_(pi.positions_)
72 {}
73
74
75 void ParIterator::operator=(ParIterator const & pi)
76 {
77         ParIterator tmp(pi);
78         swap(positions_, tmp.positions_);
79 }
80
81
82 ParIterator & ParIterator::operator++()
83 {
84         while (!positions_.empty()) {
85                 ParPosition & p = positions_.back();
86
87                 // Does the current inset contain more "cells" ?
88                 if (p.index) {
89                         ++(*p.index);
90                         if (LyXText * text = (*p.it)->inset->getText(*p.index)) {
91                                 ParagraphList & plist = text->paragraphs();
92                                 if (!plist.empty()) {
93                                         positions_.push_back(ParPosition(0, plist));
94                                         return *this;
95                                 }
96                         }
97                         ++(*p.it);
98                 } else {
99                         // The following line is needed because the value of
100                         // p.it may be invalid if inset was added/removed to
101                         // the paragraph pointed by the iterator
102                         p.it.reset(const_cast<InsetList&>((*p.plist)[p.pit].insetlist).begin());
103                 }
104
105                 // Try to find the next inset that contains paragraphs
106                 InsetList::iterator end =
107                         const_cast<InsetList&>((*p.plist)[p.pit].insetlist).end();
108                 for (; *p.it != end; ++(*p.it)) {
109                         if (LyXText * text = (*p.it)->inset->getText(0)) {
110                                 ParagraphList & plist = text->paragraphs();
111                                 if (!plist.empty()) {
112                                         p.index.reset(0);
113                                         positions_.push_back(ParPosition(0, plist));
114                                         return *this;
115                                 }
116                         }
117                 }
118
119                 // Try to go to the next paragarph
120                 if (p.pit + 1 != par_type(p.plist->size()) ||
121                     positions_.size() == 1) {
122                         ++p.pit;
123                         p.index.reset();
124                         p.it.reset();
125                         return *this;
126                 }
127
128                 // Drop end and move up in the stack.
129                 positions_.pop_back();
130         }
131         return *this;
132 }
133
134
135 LyXText * ParIterator::text(Buffer & buf) const
136 {
137         //lyxerr << "positions.size: " << positions.size() << std::endl;
138         if (positions_.size() <= 1)
139                 return &buf.text();
140
141         ParPosition const & pos = positions_[positions_.size() - 2];
142         return (*pos.it)->inset->getText(*pos.index);
143 }
144
145
146 InsetBase * ParIterator::inset() const
147 {
148         //lyxerr << "positions.size: " << positions.size() << std::endl;
149         if (positions_.size() <= 1)
150                 return 0;
151
152         ParPosition const & pos = positions_[positions_.size() - 2];
153         return (*pos.it)->inset;
154 }
155
156
157 int ParIterator::index() const
158 {
159         if (positions_.size() <= 1)
160                 return 0;
161
162         return *(positions_[positions_.size() - 2].index);
163 }
164
165
166 Paragraph & ParIterator::operator*() const
167 {
168         return plist()[positions_.back().pit];
169 }
170
171
172 par_type ParIterator::pit() const
173 {
174         return positions_.back().pit;
175 }
176
177
178 Paragraph * ParIterator::operator->() const
179 {
180         return &plist()[positions_.back().pit];
181 }
182
183
184 par_type ParIterator::outerPar() const
185 {
186         return positions_[0].pit;
187 }
188
189
190 size_t ParIterator::size() const
191 {
192         return positions_.size();
193 }
194
195
196 ParagraphList & ParIterator::plist() const
197 {
198         return *const_cast<ParagraphList*>(positions_.back().plist);
199 }
200
201
202 ParIterator::ParIterator(DocumentIterator const & cur)
203 {
204         int const size = cur.size();
205
206         for (int i = 0; i < size; ++i) {
207                 CursorSlice sl = cur[i];
208                 ParPosition pp(sl.par(), sl.text()->paragraphs());
209                 if (i < size - 1) {
210                         Paragraph & par = sl.text()->paragraphs()[sl.par()];
211                         InsetBase * inset = par.getInset(sl.pos());
212                         BOOST_ASSERT(inset);
213                         InsetList::iterator beg = par.insetlist.begin();
214                         InsetList::iterator end = par.insetlist.end();
215                         for ( ; beg != end && beg->inset != inset; ++beg)
216                                 ;
217                         pp.it.reset(beg);
218                         pp.index.reset(sl.idx() - 1);
219                 }
220                 positions_.push_back(pp);
221         }
222 }
223
224
225 bool operator==(ParIterator const & iter1, ParIterator const & iter2)
226 {
227         return iter1.positions() == iter2.positions();
228 }
229
230
231 bool operator!=(ParIterator const & iter1, ParIterator const & iter2)
232 {
233         return !(iter1 == iter2);
234 }
235
236
237 ///
238 /// ParConstIterator
239 ///
240
241
242 ParConstIterator::ParConstIterator(par_type pit, ParagraphList const & pl)
243 {
244         positions_.push_back(ParPosition(pit, pl));
245 }
246
247
248 ParConstIterator::~ParConstIterator()
249 {}
250
251
252 ParConstIterator::ParConstIterator(ParConstIterator const & pi)
253         : positions_(pi.positions_)
254 {}
255
256
257 ParConstIterator & ParConstIterator::operator++()
258 {
259         while (!positions_.empty()) {
260                 ParPosition & p = positions_.back();
261
262                 // Does the current inset contain more "cells" ?
263                 if (p.index) {
264                         ++(*p.index);
265                         if (LyXText * text = (*p.it)->inset->getText(*p.index)) {
266                                 ParagraphList & plist = text->paragraphs();
267                                 if (!plist.empty()) {
268                                         positions_.push_back(ParPosition(0, plist));
269                                         return *this;
270                                 }
271                         }
272                         ++(*p.it);
273                 } else {
274                         // The following line is needed because the value of
275                         // p.it may be invalid if inset was added/removed to
276                         // the paragraph pointed by the iterator
277                         p.it.reset(const_cast<InsetList&>((*p.plist)[p.pit].insetlist).begin());
278                 }
279
280                 // Try to find the next inset that contains paragraphs
281                 InsetList::iterator end =
282                         const_cast<InsetList&>((*p.plist)[p.pit].insetlist).end();
283                 for (; *p.it != end; ++(*p.it)) {
284                         if (LyXText * text = (*p.it)->inset->getText(0)) {
285                                 ParagraphList & plist = text->paragraphs();
286                                 if (!plist.empty()) {
287                                         p.index.reset(0);
288                                         positions_.push_back(ParPosition(0, plist));
289                                         return *this;
290                                 }
291                         }
292                 }
293
294                 // Try to go to the next paragarph
295                 if (p.pit + 1 != par_type(p.plist->size()) ||
296                     positions_.size() == 1) {
297                         ++p.pit;
298                         p.index.reset();
299                         p.it.reset();
300                         return *this;
301                 }
302
303                 // Drop end and move up in the stack.
304                 positions_.pop_back();
305         }
306
307         return *this;
308 }
309
310
311 Paragraph const & ParConstIterator::operator*() const
312 {
313         return plist()[positions_.back().pit];
314 }
315
316
317 par_type ParConstIterator::pit() const
318 {
319         return positions_.back().pit;
320 }
321
322
323 Paragraph const * ParConstIterator::operator->() const
324 {
325         return &plist()[positions_.back().pit];
326 }
327
328
329 ParagraphList const & ParConstIterator::plist() const
330 {
331         return *positions_.back().plist;
332 }
333
334
335 size_t ParConstIterator::size() const
336 {
337         return positions_.size();
338 }
339
340
341 bool operator==(ParConstIterator const & iter1, ParConstIterator const & iter2)
342 {
343         return iter1.positions() == iter2.positions();
344 }
345
346
347 bool operator!=(ParConstIterator const & iter1, ParConstIterator const & iter2)
348 {
349         return !(iter1 == iter2);
350 }