]> git.lyx.org Git - features.git/blob - src/DocIterator.cpp
remove fake constness in DocIterator
[features.git] / src / DocIterator.cpp
1 /**
2  * \file DocIterator.cpp
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  * \author Alfredo Braunstein
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12
13 #include <config.h>
14
15 #include "DocIterator.h"
16
17 #include "debug.h"
18 #include "Text.h"
19 #include "Paragraph.h"
20
21 #include "mathed/MathData.h"
22 #include "mathed/InsetMath.h"
23
24 #include "insets/InsetTabular.h"
25
26 #include <boost/assert.hpp>
27 #include <boost/current_function.hpp>
28
29 using std::endl;
30
31
32 namespace lyx {
33
34
35 // We could be able to get rid of this if only every BufferView were
36 // associated to a buffer on construction.
37 DocIterator::DocIterator()
38         : boundary_(false), inset_(0)
39 {}
40
41
42 DocIterator::DocIterator(Inset & inset)
43         : boundary_(false), inset_(&inset)
44 {}
45
46
47 DocIterator doc_iterator_begin(Inset & inset)
48 {
49         DocIterator dit(inset);
50         dit.forwardPos();
51         return dit;
52 }
53
54
55 DocIterator doc_iterator_end(Inset & inset)
56 {
57         return DocIterator(inset);
58 }
59
60
61 Inset * DocIterator::nextInset() const
62 {
63         BOOST_ASSERT(!empty());
64         if (pos() == lastpos())
65                 return 0;
66         if (pos() > lastpos()) {
67                 lyxerr << "Should not happen, but it does. " << endl;
68                 return 0;
69         }
70         if (inMathed())
71                 return nextAtom().nucleus();
72         return paragraph().isInset(pos()) ? paragraph().getInset(pos()) : 0;
73 }
74
75
76 Inset * DocIterator::prevInset() const
77 {
78         BOOST_ASSERT(!empty());
79         if (pos() == 0)
80                 return 0;
81         if (inMathed())
82                 if (cell().empty())
83                         // FIXME: this should not happen but it does.
84                         // See bug 3189
85                         // http://bugzilla.lyx.org/show_bug.cgi?id=3189
86                         return 0;
87                 else
88                         return prevAtom().nucleus();
89         return paragraph().isInset(pos() - 1) ? paragraph().getInset(pos() - 1) : 0;
90 }
91
92
93 Inset * DocIterator::realInset() const
94 {
95         BOOST_ASSERT(inTexted());
96         // if we are in a tabular, we need the cell
97         if (inset().lyxCode() == Inset::TABULAR_CODE) {
98                 InsetTabular & tabular = static_cast<InsetTabular&>(inset());
99                 return tabular.cell(idx()).get();
100         }
101         return &inset();
102 }
103
104
105 MathAtom & DocIterator::prevAtom() const
106 {
107         BOOST_ASSERT(!empty());
108         BOOST_ASSERT(pos() > 0);
109         return cell()[pos() - 1];
110 }
111
112
113 MathAtom & DocIterator::nextAtom() const
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 Text * DocIterator::text() const
123 {
124         BOOST_ASSERT(!empty());
125         return top().text();
126 }
127
128
129 Paragraph & DocIterator::paragraph() const
130 {
131         if (!inTexted())
132                 lyxerr << *this << endl;
133         BOOST_ASSERT(inTexted());
134         return top().paragraph();
135 }
136
137
138 Paragraph & DocIterator::innerParagraph() const
139 {
140         BOOST_ASSERT(!empty());
141         // go up until first non-0 text is hit
142         // (innermost text is 0 in mathed)
143         for (int i = depth() - 1; i >= 0; --i)
144                 if (slices_[i].text())
145                         return slices_[i].paragraph();
146
147         // This case is in principe not possible. We _must_
148         // be inside a Paragraph.
149         BOOST_ASSERT(false);
150         return paragraph();
151 }
152
153
154 pit_type DocIterator::lastpit() const
155 {
156         return inMathed() ? 0 : text()->paragraphs().size() - 1;
157 }
158
159
160 pos_type DocIterator::lastpos() const
161 {
162         return inMathed() ? cell().size() : paragraph().size();
163 }
164
165
166 DocIterator::idx_type DocIterator::lastidx() const
167 {
168         return top().lastidx();
169 }
170
171
172 size_t DocIterator::nargs() const
173 {
174         // assume 1x1 grid for main text
175         return top().nargs();
176 }
177
178
179 size_t DocIterator::ncols() const
180 {
181         // assume 1x1 grid for main text
182         return top().ncols();
183 }
184
185
186 size_t DocIterator::nrows() const
187 {
188         // assume 1x1 grid for main text
189         return top().nrows();
190 }
191
192
193 DocIterator::row_type DocIterator::row() const
194 {
195         return top().row();
196 }
197
198
199 DocIterator::col_type DocIterator::col() const
200 {
201         return top().col();
202 }
203
204
205 MathData & DocIterator::cell() const
206 {
207 //      BOOST_ASSERT(inMathed());
208         return top().cell();
209 }
210
211
212 Text * DocIterator::innerText() const
213 {
214         BOOST_ASSERT(!empty());
215         // go up until first non-0 text is hit
216         // (innermost text is 0 in mathed)
217         for (int i = depth() - 1; i >= 0; --i)
218                 if (slices_[i].text())
219                         return slices_[i].text();
220         return 0;
221 }
222
223
224 Inset * DocIterator::innerInsetOfType(int code) const
225 {
226         for (int i = depth() - 1; i >= 0; --i)
227                 if (slices_[i].inset_->lyxCode() == code)
228                         return slices_[i].inset_;
229         return 0;
230 }
231
232
233 void DocIterator::forwardPos(bool ignorecollapsed)
234 {
235         //this dog bites his tail
236         if (empty()) {
237                 push_back(CursorSlice(*inset_));
238                 return;
239         }
240
241         Inset * const nextinset = nextInset();
242         // jump over collapsables if they are collapsed
243         // FIXME: the check for asInsetMath() shouldn't be necessary
244         // but math insets do not return a sensible editable() state yet.
245         if (ignorecollapsed && nextinset && (!nextinset->asInsetMath()
246             && nextinset->editable() != Inset::HIGHLY_EDITABLE)) {
247                 ++top().pos();
248                 return;
249         }
250
251         CursorSlice & tip = top();
252         //lyxerr << "XXX\n" << *this << endl;
253
254         // this is used twice and shows up in the profiler!
255         pos_type const lastp = lastpos();
256
257         // move into an inset to the right if possible
258         Inset * n = 0;
259
260         if (tip.pos() != lastp) {
261                 // this is impossible for pos() == size()
262                 if (inMathed()) {
263                         n = (tip.cell().begin() + tip.pos())->nucleus();
264                 } else {
265                         if (paragraph().isInset(tip.pos()))
266                                 n = paragraph().getInset(tip.pos());
267                 }
268         }
269
270         if (n && n->isActive()) {
271                 //lyxerr << "... descend" << endl;
272                 push_back(CursorSlice(*n));
273                 return;
274         }
275
276         if (!tip.at_end()) {
277                 tip.forwardPos();
278                 return;
279         }
280         // otherwise leave inset and jump over inset as a whole
281         pop_back();
282         // 'tip' is invalid now...
283         if (!empty())
284                 ++top().pos();
285 }
286
287
288 void DocIterator::forwardPar()
289 {
290         forwardPos();
291
292         while (!empty() && (!inTexted() || pos() != 0)) {
293                 if (inTexted()) {
294                         pos_type const lastp = lastpos();
295                         Paragraph const & par = paragraph();
296                         pos_type & pos = top().pos();
297                         if (par.insetlist.empty())
298                                 pos = lastp;
299                         else
300                                 while (pos < lastp && !par.isInset(pos))
301                                         ++pos;
302                 }
303                 forwardPos();
304         }
305 }
306
307
308 void DocIterator::forwardIdx()
309 {
310         CursorSlice & tip = top();
311
312         //prevent endless loops
313         BOOST_ASSERT(tip.idx() < tip.nargs());
314         ++tip.idx();
315         tip.pit() = 0;
316         tip.pos() = 0;
317 }
318
319
320 void DocIterator::forwardChar()
321 {
322         forwardPos();
323         while (!empty() && pos() == lastpos())
324                 forwardPos();
325 }
326
327
328 void DocIterator::forwardInset()
329 {
330         forwardPos();
331
332         while (!empty() && !nextInset()) {
333                 if (inTexted()) {
334                         pos_type const lastp = lastpos();
335                         Paragraph const & par = paragraph();
336                         pos_type & pos = top().pos();
337                         while (pos < lastp && !par.isInset(pos))
338                                 ++pos;
339                         if (pos < lastp)
340                                 break;
341                 }
342                 forwardPos();
343         }
344 }
345
346
347 void DocIterator::backwardChar()
348 {
349         backwardPos();
350         while (!empty() && pos() == lastpos())
351                 backwardPos();
352 }
353
354
355 void DocIterator::backwardPos()
356 {
357         //this dog bites his tail
358         if (empty()) {
359                 push_back(CursorSlice(*inset_));
360                 top().idx() = lastidx();
361                 top().pit() = lastpit();
362                 top().pos() = lastpos();
363                 return;
364         }
365
366         if (top().at_begin()) {
367                 pop_back();
368                 return;
369         }
370
371         top().backwardPos();
372
373         // move into an inset to the left if possible
374         Inset * n = 0;
375
376         if (inMathed()) {
377                 n = (top().cell().begin() + top().pos())->nucleus();
378         } else {
379                 if (paragraph().isInset(top().pos()))
380                         n = paragraph().getInset(top().pos());
381         }
382
383         if (n && n->isActive()) {
384                 push_back(CursorSlice(*n));
385                 top().idx() = lastidx();
386                 top().pit() = lastpit();
387                 top().pos() = lastpos();
388         }
389 }
390
391
392 bool DocIterator::hasPart(DocIterator const & it) const
393 {
394         // it can't be a part if it is larger
395         if (it.depth() > depth())
396                 return false;
397
398         // as inset adresses are the 'last' level
399         return &it.top().inset() == &slices_[it.depth() - 1].inset();
400 }
401
402
403 void DocIterator::updateInsets(Inset * inset)
404 {
405         // this function re-creates the cache of inset pointers.
406         // code taken in part from StableDocIterator::asDocIterator.
407         //lyxerr << "converting:\n" << *this << endl;
408         DocIterator dit = DocIterator(*inset);
409         size_t const n = slices_.size();
410         for (size_t i = 0 ; i < n; ++i) {
411                 BOOST_ASSERT(inset);
412                 dit.push_back(slices_[i]);
413                 dit.top().inset_ = inset;
414                 if (i + 1 != n)
415                         inset = dit.nextInset();
416         }
417         //lyxerr << "converted:\n" << *this << endl;
418         operator=(dit);
419 }
420
421
422 bool DocIterator::fixIfBroken()
423 {
424         // Go through the slice stack from the bottom. 
425         // Check that all coordinates (idx, pit, pos) are correct and
426         // that the inset is the one which is claimed to be there
427         Inset * inset = &slices_[0].inset();
428         size_t i = 0;
429         size_t n = slices_.size();
430         for (; i != n; ++i) {
431                 CursorSlice & cs = slices_[i];
432                 if (&cs.inset() != inset) {
433                         // the whole slice is wrong, chop off this as well
434                         --i;
435                         LYXERR(Debug::DEBUG) << "fixIfBroken(): inset changed" << endl;
436                         break;
437                 } else if (cs.idx() > cs.lastidx()) {
438                         cs.idx() = cs.lastidx();
439                         cs.pit() = cs.lastpit();
440                         cs.pos() = cs.lastpos();
441                         LYXERR(Debug::DEBUG) << "fixIfBroken(): idx fixed" << endl;
442                         break;
443                 } else if (cs.pit() > cs.lastpit()) {
444                         cs.pit() = cs.lastpit();
445                         cs.pos() = cs.lastpos();
446                         LYXERR(Debug::DEBUG) << "fixIfBroken(): pit fixed" << endl;
447                         break;
448                 } else if (cs.pos() > cs.lastpos()) {
449                         cs.pos() = cs.lastpos();
450                         LYXERR(Debug::DEBUG) << "fixIfBroken(): pos fixed" << endl;
451                         break;
452                 } else if (i != n - 1 && cs.pos() != cs.lastpos()) {
453                         // get inset which is supposed to be in the next slice
454                         if (cs.inset().inMathed())
455                                 inset = (cs.cell().begin() + cs.pos())->nucleus();
456                         else if (cs.paragraph().isInset(cs.pos()))
457                                 inset = cs.paragraph().getInset(cs.pos());
458                         else {
459                                 // there are slices left, so there must be another inset
460                                 break;
461                         }
462                 }
463         }
464
465         // Did we make it through the whole slice stack? Otherwise there
466         // was a problem at slice i, and we have to chop off above
467         if (i < n) {
468                 LYXERR(Debug::DEBUG) << "fixIfBroken(): cursor chopped at " << i << endl;
469                 resize(i + 1);
470                 return true;
471         } else
472                 return false;
473 }
474
475
476 std::ostream & operator<<(std::ostream & os, DocIterator const & dit)
477 {
478         for (size_t i = 0, n = dit.depth(); i != n; ++i)
479                 os << " " << dit[i] << "\n";
480         return os;
481 }
482
483
484 bool operator<(DocIterator const & p, DocIterator const & q)
485 {
486         size_t depth = std::min(p.depth(), q.depth());
487         for (size_t i = 0 ; i < depth ; ++i) {
488                 if (p[i] != q[i])
489                         return p[i] < q[i];
490         }
491         return p.depth() < q.depth();
492 }
493
494
495 bool operator>(DocIterator const & p, DocIterator const & q)
496 {
497         return q < p;
498 }
499
500
501 bool operator<=(DocIterator const & p, DocIterator const & q)
502 {
503         return !(q < p);
504 }
505
506
507 ///////////////////////////////////////////////////////
508
509 StableDocIterator::StableDocIterator(DocIterator const & dit)
510 {
511         data_ = dit.internalData();
512         for (size_t i = 0, n = data_.size(); i != n; ++i)
513                 data_[i].inset_ = 0;
514 }
515
516
517 DocIterator StableDocIterator::asDocIterator(Inset * inset) const
518 {
519         // this function re-creates the cache of inset pointers
520         //lyxerr << "converting:\n" << *this << endl;
521         DocIterator dit = DocIterator(*inset);
522         for (size_t i = 0, n = data_.size(); i != n; ++i) {
523                 if (inset == 0) {
524                         // FIXME
525                         lyxerr << BOOST_CURRENT_FUNCTION
526                                << " Should not happen, but does e.g. after C-n C-l C-z S-C-z\n"
527                                    << " or when a Buffer has been concurently edited by two views"
528                                 << '\n' << "dit: " << dit << '\n'
529                                 << " lastpos: " << dit.lastpos() << endl;
530                         dit.fixIfBroken();
531                         break;
532                 }
533                 dit.push_back(data_[i]);
534                 dit.top().inset_ = inset;
535                 if (dit.fixIfBroken())
536                         break;
537                 if (i + 1 != n)
538                         inset = dit.nextInset();
539         }
540         //lyxerr << "convert:\n" << *this << " to:\n" << dit << endl;
541         return dit;
542 }
543
544
545 std::ostream & operator<<(std::ostream & os, StableDocIterator const & dit)
546 {
547         for (size_t i = 0, n = dit.data_.size(); i != n; ++i)
548                 os << " " << dit.data_[i] << "\n";
549         return os;
550 }
551
552
553 bool operator==(StableDocIterator const & dit1, StableDocIterator const & dit2)
554 {
555         return dit1.data_ == dit2.data_;
556 }
557
558
559 } // namespace lyx