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