]> git.lyx.org Git - lyx.git/blob - src/dociterator.C
STLPort compile fix
[lyx.git] / src / dociterator.C
1 /**
2  * \file dociterator.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author André Pönitz
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11
12 #include <config.h>
13
14 #include "dociterator.h"
15
16 #include "debug.h"
17 #include "lyxtext.h"
18 #include "lyxrow.h"
19 #include "paragraph.h"
20
21 #include "mathed/math_data.h"
22 #include "mathed/math_inset.h"
23
24 #include <boost/assert.hpp>
25
26 using std::endl;
27
28
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()
32         : inset_(0)
33 {}
34
35
36 DocIterator::DocIterator(InsetBase & inset)
37         : inset_(&inset)
38 {}
39
40
41 DocIterator doc_iterator_begin(InsetBase & inset)
42 {
43         DocIterator dit(inset);
44         dit.forwardPos();
45         return dit;
46 }
47
48
49 DocIterator doc_iterator_end(InsetBase & inset)
50 {
51         return DocIterator(inset);
52 }
53
54
55 InsetBase * DocIterator::nextInset()
56 {
57         BOOST_ASSERT(!empty());
58         if (pos() == lastpos())
59                 return 0;
60         if (inMathed())
61                 return nextAtom().nucleus();
62         return paragraph().isInset(pos()) ? paragraph().getInset(pos()) : 0;
63 }
64
65
66 InsetBase * DocIterator::prevInset()
67 {
68         BOOST_ASSERT(!empty());
69         if (pos() == 0)
70                 return 0;
71         if (inMathed())
72                 return prevAtom().nucleus();
73         return paragraph().isInset(pos() - 1) ? paragraph().getInset(pos() - 1) : 0;
74 }
75
76
77 InsetBase const * DocIterator::prevInset() const
78 {
79         BOOST_ASSERT(!empty());
80         if (pos() == 0)
81                 return 0;
82         if (inMathed())
83                 return prevAtom().nucleus();
84         return paragraph().isInset(pos() - 1) ? paragraph().getInset(pos() - 1) : 0;
85 }
86
87
88 MathAtom const & DocIterator::prevAtom() const
89 {
90         BOOST_ASSERT(!empty());
91         BOOST_ASSERT(pos() > 0);
92         return cell()[pos() - 1];
93 }
94
95
96 MathAtom & DocIterator::prevAtom()
97 {
98         BOOST_ASSERT(!empty());
99         BOOST_ASSERT(pos() > 0);
100         return cell()[pos() - 1];
101 }
102
103
104 MathAtom const & DocIterator::nextAtom() const
105 {
106         BOOST_ASSERT(!empty());
107         //lyxerr << "lastpos: " << lastpos() << " next atom:\n" << *this << endl;
108         BOOST_ASSERT(pos() < lastpos());
109         return cell()[pos()];
110 }
111
112
113 MathAtom & DocIterator::nextAtom()
114 {
115         BOOST_ASSERT(!empty());
116         //lyxerr << "lastpos: " << lastpos() << " next atom:\n" << *this << endl;
117         BOOST_ASSERT(pos() < lastpos());
118         return cell()[pos()];
119 }
120
121
122 LyXText * DocIterator::text() const
123 {
124         BOOST_ASSERT(!empty());
125         return top().text();
126 }
127
128
129 Paragraph & DocIterator::paragraph()
130 {
131         BOOST_ASSERT(inTexted());
132         return top().paragraph();
133 }
134
135
136 Paragraph const & DocIterator::paragraph() const
137 {
138         BOOST_ASSERT(inTexted());
139         return top().paragraph();
140 }
141
142
143 Row & DocIterator::textRow()
144 {
145         return *paragraph().getRow(pos());
146 }
147
148
149 Row const & DocIterator::textRow() const
150 {
151         return *paragraph().getRow(pos());
152 }
153
154
155 DocIterator::par_type DocIterator::lastpar() const
156 {
157         return inMathed() ? 0 : text()->paragraphs().size() - 1;
158 }
159
160
161 DocIterator::pos_type DocIterator::lastpos() const
162 {
163         return inMathed() ? cell().size() : paragraph().size();
164 }
165
166
167 DocIterator::row_type DocIterator::crow() const
168 {
169         return paragraph().row(pos());
170 }
171
172
173 DocIterator::row_type DocIterator::lastcrow() const
174 {
175         return paragraph().rows.size();
176 }
177
178
179 DocIterator::idx_type DocIterator::lastidx() const
180 {
181         return top().lastidx();
182 }
183
184
185 size_t DocIterator::nargs() const
186 {
187         // assume 1x1 grid for main text
188         return top().nargs();
189 }
190
191
192 size_t DocIterator::ncols() const
193 {
194         // assume 1x1 grid for main text
195         return top().ncols();
196 }
197
198
199 size_t DocIterator::nrows() const
200 {
201         // assume 1x1 grid for main text
202         return top().nrows();
203 }
204
205
206 DocIterator::row_type DocIterator::row() const
207 {
208         return top().row();
209 }
210
211
212 DocIterator::col_type DocIterator::col() const
213 {
214         return top().col();
215 }
216
217
218 MathArray const & DocIterator::cell() const
219 {
220         BOOST_ASSERT(inMathed());
221         return top().cell();
222 }
223
224
225 MathArray & DocIterator::cell()
226 {
227         BOOST_ASSERT(inMathed());
228         return top().cell();
229 }
230
231
232 bool DocIterator::inMathed() const
233 {
234         return !empty() && inset().inMathed();
235 }
236
237
238 bool DocIterator::inTexted() const
239 {
240         return !empty() && !inset().inMathed();
241 }
242
243
244 LyXText * DocIterator::innerText() const
245 {
246         BOOST_ASSERT(!empty());
247         // go up until first non-0 text is hit
248         // (innermost text is 0 in mathed)
249         for (int i = size() - 1; i >= 0; --i)
250                 if (operator[](i).text())
251                         return operator[](i).text();
252         return 0;
253 }
254
255
256 InsetBase * DocIterator::innerInsetOfType(int code) const
257 {
258         for (int i = size() - 1; i >= 0; --i)
259                 if (operator[](i).inset_->lyxCode() == code)
260                         return operator[](i).inset_;
261         return 0;
262 }
263
264
265 void DocIterator::forwardPos()
266 {
267         //this dog bites his tail
268         if (empty()) {
269                 push_back(CursorSlice(*inset_));
270                 return;
271         }
272
273         CursorSlice & top = back();
274         //lyxerr << "XXX\n" << *this << endl;
275
276         // this is used twice and shows up in the profiler!
277         pos_type const lastp = lastpos();
278
279         // move into an inset to the right if possible
280         InsetBase * n = 0;
281
282         if (top.pos() != lastp) {
283                 // this is impossible for pos() == size()
284                 if (inMathed()) {
285                         n = (top.cell().begin() + top.pos())->nucleus();
286                 } else {
287                         if (paragraph().isInset(top.pos()))
288                                 n = paragraph().getInset(top.pos());
289                 }
290         }
291
292         if (n && n->isActive()) {
293                 //lyxerr << "... descend" << endl;
294                 push_back(CursorSlice(*n));
295                 return;
296         }
297
298         // otherwise move on one position if possible
299         if (top.pos() < lastp) {
300                 //lyxerr << "... next pos" << endl;
301                 ++top.pos();
302                 return;
303         }
304         //lyxerr << "... no next pos" << endl;
305
306         // otherwise move on one paragraph if possible
307         if (top.par() < lastpar()) {
308                 //lyxerr << "... next par" << endl;
309                 ++top.par();
310                 top.pos() = 0;
311                 return;
312         }
313         //lyxerr << "... no next par" << endl;
314
315         // otherwise try to move on one cell if possible
316         if (top.idx() < lastidx()) {
317                 //lyxerr << "... next idx" << endl;
318                 ++top.idx();
319                 top.par() = 0;
320                 top.pos() = 0;
321                 return;
322         }
323         //lyxerr << "... no next idx" << endl;
324
325         // otherwise leave inset and jump over inset as a whole
326         pop_back();
327         // 'top' is invalid now...
328         if (size())
329                 ++back().pos();
330 }
331
332
333 void DocIterator::forwardPar()
334 {
335         forwardPos();
336         while (!empty() && (!inTexted() || pos() != 0))
337                 forwardPos();
338 }
339
340
341 void DocIterator::forwardChar()
342 {
343         forwardPos();
344         while (size() != 0 && pos() == lastpos())
345                 forwardPos();
346 }
347
348
349 void DocIterator::forwardInset()
350 {
351         forwardPos();
352         while (size() != 0 && (pos() == lastpos() || nextInset() == 0))
353                 forwardPos();
354 }
355
356
357 void DocIterator::backwardChar()
358 {
359         backwardPos();
360         while (size() != 0 && pos() == lastpos())
361                 backwardPos();
362 }
363
364
365 void DocIterator::backwardPos()
366 {
367         //this dog bites his tail
368         if (empty()) {
369                 push_back(CursorSlice(*inset_));
370                 back().idx() = lastidx();
371                 back().par() = lastpar();
372                 back().pos() = lastpos();
373                 return;
374         }
375
376         CursorSlice & top = back();
377
378         if (top.pos() != 0) {
379                 --top.pos();
380         } else if (top.par() != 0) {
381                 --top.par();
382                 top.pos() = lastpos();
383                 return;
384         } else if (top.idx() != 0) {
385                 --top.idx();
386                 top.par() = lastpar();
387                 top.pos() = lastpos();
388                 return;
389         } else {
390                 pop_back();
391                 return;
392         }
393
394         // move into an inset to the left if possible
395         InsetBase * n = 0;
396
397         if (inMathed()) {
398                 n = (top.cell().begin() + top.pos())->nucleus();
399         } else {
400                 if (paragraph().isInset(top.pos()))
401                         n = paragraph().getInset(top.pos());
402         }
403
404         if (n && n->isActive()) {
405                 push_back(CursorSlice(*n));
406                 back().idx() = lastidx();
407                 back().par() = lastpar();
408                 back().pos() = lastpos();
409         }
410 }
411
412
413 std::ostream & operator<<(std::ostream & os, DocIterator const & dit)
414 {
415         for (size_t i = 0, n = dit.size(); i != n; ++i)
416                 os << " " << dit.operator[](i) << "\n";
417         return os;
418 }
419
420
421
422 ///////////////////////////////////////////////////////
423
424 StableDocIterator::StableDocIterator(const DocIterator & dit)
425 {
426         data_ = dit;
427         for (size_t i = 0, n = data_.size(); i != n; ++i)
428                 data_[i].inset_ = 0;
429 }
430
431
432 DocIterator
433 StableDocIterator::asDocIterator(InsetBase * inset) const
434 {
435         // this function re-creates the cache of inset pointers
436         //lyxerr << "converting:\n" << *this << endl;
437         DocIterator dit = DocIterator(*inset);
438         for (size_t i = 0, n = data_.size(); i != n; ++i) {
439                 dit.push_back(data_[i]);
440                 dit.back().inset_ = inset;
441                 if (i + 1 != n)
442                         inset = dit.nextInset();
443         }
444         //lyxerr << "convert:\n" << *this << " to:\n" << dit << endl;
445         return dit;
446 }
447
448
449 std::ostream & operator<<(std::ostream & os, StableDocIterator const & dit)
450 {
451         for (size_t i = 0, n = dit.data_.size(); i != n; ++i)
452                 os << " " << dit.data_[i] << "\n";
453         return os;
454 }