]> git.lyx.org Git - lyx.git/blob - src/dociterator.C
ws changes mostly
[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()
127 {
128         BOOST_ASSERT(!empty());
129         return top().text();
130 }
131
132 LyXText const * DocIterator::text() const
133 {
134         BOOST_ASSERT(!empty());
135         return top().text();
136 }
137
138
139 Paragraph & DocIterator::paragraph()
140 {
141         BOOST_ASSERT(inTexted());
142         return top().paragraph();
143 }
144
145
146 Paragraph const & DocIterator::paragraph() const
147 {
148         BOOST_ASSERT(inTexted());
149         return top().paragraph();
150 }
151
152
153 Row & DocIterator::textRow()
154 {
155         BOOST_ASSERT(!paragraph().rows().empty());
156         return paragraph().getRow(pos());
157 }
158
159
160 Row const & DocIterator::textRow() const
161 {
162         BOOST_ASSERT(!paragraph().rows().empty());
163         return paragraph().getRow(pos());
164 }
165
166
167 DocIterator::pit_type DocIterator::lastpit() const
168 {
169         return inMathed() ? 0 : text()->paragraphs().size() - 1;
170 }
171
172
173 DocIterator::pos_type DocIterator::lastpos() const
174 {
175         return inMathed() ? cell().size() : paragraph().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()
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 LyXText const * DocIterator::innerText() const
256 {
257         BOOST_ASSERT(!empty());
258         // go up until first non-0 text is hit
259         // (innermost text is 0 in mathed)
260         for (int i = size() - 1; i >= 0; --i)
261                 if (operator[](i).text())
262                         return operator[](i).text();
263         return 0;
264 }
265
266
267 InsetBase * DocIterator::innerInsetOfType(int code) const
268 {
269         for (int i = size() - 1; i >= 0; --i)
270                 if (operator[](i).inset_->lyxCode() == code)
271                         return operator[](i).inset_;
272         return 0;
273 }
274
275
276 void DocIterator::forwardPos()
277 {
278         //this dog bites his tail
279         if (empty()) {
280                 push_back(CursorSlice(*inset_));
281                 return;
282         }
283
284         CursorSlice & top = back();
285         //lyxerr << "XXX\n" << *this << endl;
286
287         // this is used twice and shows up in the profiler!
288         pos_type const lastp = lastpos();
289
290         // move into an inset to the right if possible
291         InsetBase * n = 0;
292
293         if (top.pos() != lastp) {
294                 // this is impossible for pos() == size()
295                 if (inMathed()) {
296                         n = (top.cell().begin() + top.pos())->nucleus();
297                 } else {
298                         if (paragraph().isInset(top.pos()))
299                                 n = paragraph().getInset(top.pos());
300                 }
301         }
302
303         if (n && n->isActive()) {
304                 //lyxerr << "... descend" << endl;
305                 push_back(CursorSlice(*n));
306                 return;
307         }
308
309         // otherwise move on one position if possible
310         if (top.pos() < lastp) {
311                 //lyxerr << "... next pos" << endl;
312                 ++top.pos();
313                 return;
314         }
315         //lyxerr << "... no next pos" << endl;
316
317         // otherwise move on one paragraph if possible
318         if (top.pit() < lastpit()) {
319                 //lyxerr << "... next par" << endl;
320                 ++top.pit();
321                 top.pos() = 0;
322                 return;
323         }
324         //lyxerr << "... no next pit" << endl;
325
326         // otherwise try to move on one cell if possible
327         if (top.idx() < lastidx()) {
328                 //lyxerr << "... next idx" << endl;
329                 ++top.idx();
330                 top.pit() = 0;
331                 top.pos() = 0;
332                 return;
333         }
334         //lyxerr << "... no next idx" << endl;
335
336         // otherwise leave inset and jump over inset as a whole
337         pop_back();
338         // 'top' is invalid now...
339         if (size())
340                 ++back().pos();
341 }
342
343
344 void DocIterator::forwardPar()
345 {
346         forwardPos();
347         while (!empty() && (!inTexted() || pos() != 0))
348                 forwardPos();
349 }
350
351
352 void DocIterator::forwardChar()
353 {
354         forwardPos();
355         while (size() != 0 && pos() == lastpos())
356                 forwardPos();
357 }
358
359
360 void DocIterator::forwardInset()
361 {
362         forwardPos();
363         while (size() != 0 && (pos() == lastpos() || nextInset() == 0))
364                 forwardPos();
365 }
366
367
368 void DocIterator::backwardChar()
369 {
370         backwardPos();
371         while (size() != 0 && pos() == lastpos())
372                 backwardPos();
373 }
374
375
376 void DocIterator::backwardPos()
377 {
378         //this dog bites his tail
379         if (empty()) {
380                 push_back(CursorSlice(*inset_));
381                 back().idx() = lastidx();
382                 back().pit() = lastpit();
383                 back().pos() = lastpos();
384                 return;
385         }
386
387         CursorSlice & top = back();
388
389         if (top.pos() != 0) {
390                 --top.pos();
391         } else if (top.pit() != 0) {
392                 --top.pit();
393                 top.pos() = lastpos();
394                 return;
395         } else if (top.idx() != 0) {
396                 --top.idx();
397                 top.pit() = lastpit();
398                 top.pos() = lastpos();
399                 return;
400         } else {
401                 pop_back();
402                 return;
403         }
404
405         // move into an inset to the left if possible
406         InsetBase * n = 0;
407
408         if (inMathed()) {
409                 n = (top.cell().begin() + top.pos())->nucleus();
410         } else {
411                 if (paragraph().isInset(top.pos()))
412                         n = paragraph().getInset(top.pos());
413         }
414
415         if (n && n->isActive()) {
416                 push_back(CursorSlice(*n));
417                 back().idx() = lastidx();
418                 back().pit() = lastpit();
419                 back().pos() = lastpos();
420         }
421 }
422
423
424 bool DocIterator::hasPart(DocIterator const & it) const
425 {
426         // it can't be a part if it is larger
427         if (it.size() > size())
428                 return false;
429
430         // as inset adresses are the 'last' level
431         return &it.back().inset() == &operator[](it.size() - 1).inset();
432 }
433
434
435 std::ostream & operator<<(std::ostream & os, DocIterator const & dit)
436 {
437         for (size_t i = 0, n = dit.size(); i != n; ++i)
438                 os << " " << dit.operator[](i) << "\n";
439         return os;
440 }
441
442
443
444 ///////////////////////////////////////////////////////
445
446 StableDocIterator::StableDocIterator(const DocIterator & dit)
447 {
448         data_ = dit;
449         for (size_t i = 0, n = data_.size(); i != n; ++i)
450                 data_[i].inset_ = 0;
451 }
452
453
454 DocIterator StableDocIterator::asDocIterator(InsetBase * inset) const
455 {
456         // this function re-creates the cache of inset pointers
457         //lyxerr << "converting:\n" << *this << endl;
458         DocIterator dit = DocIterator(*inset);
459         for (size_t i = 0, n = data_.size(); i != n; ++i) {
460                 if (inset == 0) {
461                         // FIXME
462                         lyxerr << "Should not happen, but does e.g. after C-n C-l C-z S-C-z"
463                                 << endl << "dit: " << dit << endl
464                                 << " lastpos: " << dit.lastpos() << endl;
465                         //break;
466                         BOOST_ASSERT(false);
467                 }
468                 dit.push_back(data_[i]);
469                 dit.back().inset_ = inset;
470                 if (i + 1 != n)
471                         inset = dit.nextInset();
472         }
473         //lyxerr << "convert:\n" << *this << " to:\n" << dit << endl;
474         return dit;
475 }
476
477
478 std::ostream & operator<<(std::ostream & os, StableDocIterator const & dit)
479 {
480         for (size_t i = 0, n = dit.data_.size(); i != n; ++i)
481                 os << " " << dit.data_[i] << "\n";
482         return os;
483 }