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