]> git.lyx.org Git - lyx.git/blob - src/DocIterator.cpp
bdeff6980a3777d7bfa5d9078189301e02e2e787
[lyx.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  *
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 "Text.h"
18 #include "Paragraph.h"
19
20 #include "mathed/MathData.h"
21 #include "mathed/InsetMath.h"
22
23 #include "insets/InsetTabular.h"
24
25 #include <boost/assert.hpp>
26 #include <boost/current_function.hpp>
27
28
29 namespace lyx {
30
31 using std::endl;
32
33
34 // We could be able to get rid of this if only every BufferView were
35 // associated to a buffer on construction.
36 DocIterator::DocIterator()
37         : boundary_(false), inset_(0)
38 {}
39
40
41 DocIterator::DocIterator(Inset & inset)
42         : boundary_(false), inset_(&inset)
43 {}
44
45
46 DocIterator doc_iterator_begin(Inset & inset)
47 {
48         DocIterator dit(inset);
49         dit.forwardPos();
50         return dit;
51 }
52
53
54 DocIterator doc_iterator_end(Inset & inset)
55 {
56         return DocIterator(inset);
57 }
58
59
60 Inset * DocIterator::nextInset()
61 {
62         BOOST_ASSERT(!empty());
63         if (pos() == lastpos())
64                 return 0;
65         if (pos() > lastpos()) {
66                 lyxerr << "Should not happen, but it does. " << endl;
67                 return 0;
68         }
69         if (inMathed())
70                 return nextAtom().nucleus();
71         return paragraph().isInset(pos()) ? paragraph().getInset(pos()) : 0;
72 }
73
74
75 Inset * DocIterator::prevInset()
76 {
77         BOOST_ASSERT(!empty());
78         if (pos() == 0)
79                 return 0;
80         if (inMathed())
81                 if (cell().empty())
82                         // FIXME: this should not happen but it does.
83                         // See bug 3189
84                         // http://bugzilla.lyx.org/show_bug.cgi?id=3189
85                         return 0;
86                 else
87                         return prevAtom().nucleus();
88         return paragraph().isInset(pos() - 1) ? paragraph().getInset(pos() - 1) : 0;
89 }
90
91
92 Inset const * DocIterator::prevInset() const
93 {
94         BOOST_ASSERT(!empty());
95         if (pos() == 0)
96                 return 0;
97         if (inMathed())
98                 if (cell().empty())
99                         // FIXME: this should not happen but it does.
100                         // See bug 3189
101                         // http://bugzilla.lyx.org/show_bug.cgi?id=3189
102                         return 0;
103                 else
104                         return prevAtom().nucleus();
105         return paragraph().isInset(pos() - 1) ? paragraph().getInset(pos() - 1) : 0;
106 }
107
108
109 Inset * DocIterator::realInset() const
110 {
111         BOOST_ASSERT(inTexted());
112         // if we are in a tabular, we need the cell
113         if (inset().lyxCode() == Inset::TABULAR_CODE) {
114                 InsetTabular & tabular = static_cast<InsetTabular&>(inset());
115                 return tabular.cell(idx()).get();
116         }
117         return &inset();
118 }
119
120
121 MathAtom const & DocIterator::prevAtom() const
122 {
123         BOOST_ASSERT(!empty());
124         BOOST_ASSERT(pos() > 0);
125         return cell()[pos() - 1];
126 }
127
128
129 MathAtom & DocIterator::prevAtom()
130 {
131         BOOST_ASSERT(!empty());
132         BOOST_ASSERT(pos() > 0);
133         return cell()[pos() - 1];
134 }
135
136
137 MathAtom const & DocIterator::nextAtom() const
138 {
139         BOOST_ASSERT(!empty());
140         //lyxerr << "lastpos: " << lastpos() << " next atom:\n" << *this << endl;
141         BOOST_ASSERT(pos() < lastpos());
142         return cell()[pos()];
143 }
144
145
146 MathAtom & DocIterator::nextAtom()
147 {
148         BOOST_ASSERT(!empty());
149         //lyxerr << "lastpos: " << lastpos() << " next atom:\n" << *this << endl;
150         BOOST_ASSERT(pos() < lastpos());
151         return cell()[pos()];
152 }
153
154
155 Text * DocIterator::text()
156 {
157         BOOST_ASSERT(!empty());
158         return top().text();
159 }
160
161 Text const * DocIterator::text() const
162 {
163         BOOST_ASSERT(!empty());
164         return top().text();
165 }
166
167
168 Paragraph & DocIterator::paragraph()
169 {
170         if (!inTexted())
171                 lyxerr << *this << endl;
172         BOOST_ASSERT(inTexted());
173         return top().paragraph();
174 }
175
176
177 Paragraph const & DocIterator::paragraph() const
178 {
179         BOOST_ASSERT(inTexted());
180         return top().paragraph();
181 }
182
183
184 Paragraph const & DocIterator::innerParagraph() const
185 {
186         BOOST_ASSERT(!empty());
187         // go up until first non-0 text is hit
188         // (innermost text is 0 in mathed)
189         for (int i = depth() - 1; i >= 0; --i)
190                 if (slices_[i].text())
191                         return slices_[i].paragraph();
192
193         // This case is in principe not possible. We _must_
194         // be inside a Paragraph.
195         BOOST_ASSERT(false);
196         return paragraph();
197 }
198
199
200 pit_type DocIterator::lastpit() const
201 {
202         return inMathed() ? 0 : text()->paragraphs().size() - 1;
203 }
204
205
206 pos_type DocIterator::lastpos() const
207 {
208         return inMathed() ? cell().size() : paragraph().size();
209 }
210
211
212 DocIterator::idx_type DocIterator::lastidx() const
213 {
214         return top().lastidx();
215 }
216
217
218 size_t DocIterator::nargs() const
219 {
220         // assume 1x1 grid for main text
221         return top().nargs();
222 }
223
224
225 size_t DocIterator::ncols() const
226 {
227         // assume 1x1 grid for main text
228         return top().ncols();
229 }
230
231
232 size_t DocIterator::nrows() const
233 {
234         // assume 1x1 grid for main text
235         return top().nrows();
236 }
237
238
239 DocIterator::row_type DocIterator::row() const
240 {
241         return top().row();
242 }
243
244
245 DocIterator::col_type DocIterator::col() const
246 {
247         return top().col();
248 }
249
250
251 MathData const & DocIterator::cell() const
252 {
253 //      BOOST_ASSERT(inMathed());
254         return top().cell();
255 }
256
257
258 MathData & DocIterator::cell()
259 {
260 //      BOOST_ASSERT(inMathed());
261         return top().cell();
262 }
263
264
265 Text * DocIterator::innerText()
266 {
267         BOOST_ASSERT(!empty());
268         // Go up until first non-0 text is hit
269         // (innermost text is 0 in mathed)
270         for (int i = depth() - 1; i >= 0; --i)
271                 if (slices_[i].text())
272                         return slices_[i].text();
273         return 0;
274 }
275
276 Text const * DocIterator::innerText() const
277 {
278         BOOST_ASSERT(!empty());
279         // go up until first non-0 text is hit
280         // (innermost text is 0 in mathed)
281         for (int i = depth() - 1; i >= 0; --i)
282                 if (slices_[i].text())
283                         return slices_[i].text();
284         return 0;
285 }
286
287
288 Inset * DocIterator::innerInsetOfType(int code) const
289 {
290         for (int i = depth() - 1; i >= 0; --i)
291                 if (slices_[i].inset_->lyxCode() == code)
292                         return slices_[i].inset_;
293         return 0;
294 }
295
296
297 void DocIterator::forwardPos(bool ignorecollapsed)
298 {
299         //this dog bites his tail
300         if (empty()) {
301                 push_back(CursorSlice(*inset_));
302                 return;
303         }
304
305         Inset * const nextinset = nextInset();
306         // jump over collapsables if they are collapsed
307         // FIXME: the check for asInsetMath() shouldn't be necessary
308         // but math insets do not return a sensible editable() state yet.
309         if (ignorecollapsed && nextinset && (!nextinset->asInsetMath()
310             && nextinset->editable() != Inset::HIGHLY_EDITABLE)) {
311                 ++top().pos();
312                 return;
313         }
314
315         CursorSlice & tip = top();
316         //lyxerr << "XXX\n" << *this << endl;
317
318         // this is used twice and shows up in the profiler!
319         pos_type const lastp = lastpos();
320
321         // move into an inset to the right if possible
322         Inset * n = 0;
323
324         if (tip.pos() != lastp) {
325                 // this is impossible for pos() == size()
326                 if (inMathed()) {
327                         n = (tip.cell().begin() + tip.pos())->nucleus();
328                 } else {
329                         if (paragraph().isInset(tip.pos()))
330                                 n = paragraph().getInset(tip.pos());
331                 }
332         }
333
334         if (n && n->isActive()) {
335                 //lyxerr << "... descend" << endl;
336                 push_back(CursorSlice(*n));
337                 return;
338         }
339
340         // otherwise move on one position if possible
341         if (tip.pos() < lastp) {
342                 //lyxerr << "... next pos" << endl;
343                 ++tip.pos();
344                 return;
345         }
346         //lyxerr << "... no next pos" << endl;
347
348         // otherwise move on one paragraph if possible
349         if (tip.pit() < lastpit()) {
350                 //lyxerr << "... next par" << endl;
351                 ++tip.pit();
352                 tip.pos() = 0;
353                 return;
354         }
355         //lyxerr << "... no next pit" << endl;
356
357         // otherwise try to move on one cell if possible
358         if (tip.idx() < lastidx()) {
359                 //lyxerr << "... next idx" << endl;
360                 ++tip.idx();
361                 tip.pit() = 0;
362                 tip.pos() = 0;
363                 return;
364         }
365         //lyxerr << "... no next idx" << endl;
366
367         // otherwise leave inset and jump over inset as a whole
368         pop_back();
369         // 'top' is invalid now...
370         if (!empty())
371                 ++top().pos();
372 }
373
374
375 void DocIterator::forwardPosNoDescend()
376 {
377         CursorSlice & tip = top();
378         pos_type const lastp = lastpos();
379
380         //  move on one position if possible
381         if (tip.pos() < lastp) {
382                 //lyxerr << "... next pos" << endl;
383                 ++tip.pos();
384                 return;
385         }
386         //lyxerr << "... no next pos" << endl;
387
388         // otherwise move on one paragraph if possible
389         if (tip.pit() < lastpit()) {
390                 //lyxerr << "... next par" << endl;
391                 ++tip.pit();
392                 tip.pos() = 0;
393                 return;
394         }
395         //lyxerr << "... no next pit" << endl;
396
397         // otherwise try to move on one cell if possible
398         if (tip.idx() < lastidx()) {
399                 //lyxerr << "... next idx" << endl;
400                 ++tip.idx();
401                 tip.pit() = 0;
402                 tip.pos() = 0;
403                 return;
404         }
405         //lyxerr << "... no next idx" << endl;
406
407         // otherwise leave inset and jump over inset as a whole
408         pop_back();
409         // 'top' is invalid now...
410         if (!empty())
411                 ++top().pos();
412 }
413
414
415 void DocIterator::forwardPar()
416 {
417         forwardPos();
418
419         while (!empty() && (!inTexted() || pos() != 0)) {
420                 if (inTexted()) {
421                         pos_type const lastp = lastpos();
422                         Paragraph const & par = paragraph();
423                         pos_type & pos = top().pos();
424                         if (par.insetlist.empty())
425                                 pos = lastp;
426                         else
427                                 while (pos < lastp && !par.isInset(pos))
428                                         ++pos;
429                 }
430                 forwardPos();
431         }
432 }
433
434
435 void DocIterator::forwardIdx()
436 {
437         CursorSlice & tip = top();
438
439         //prevent endless loops
440         BOOST_ASSERT(tip.idx() < lastidx());
441
442         ++tip.idx();
443         tip.pit() = 0;
444         tip.pos() = 0;
445 }
446
447
448 void DocIterator::forwardChar()
449 {
450         forwardPos();
451         while (!empty() && pos() == lastpos())
452                 forwardPos();
453 }
454
455
456 void DocIterator::forwardInset()
457 {
458         forwardPos();
459
460         while (!empty() && !nextInset()) {
461                 if (inTexted()) {
462                         pos_type const lastp = lastpos();
463                         Paragraph const & par = paragraph();
464                         pos_type & pos = top().pos();
465                         while (pos < lastp && !par.isInset(pos))
466                                 ++pos;
467                         if (pos < lastp)
468                                 break;
469                 }
470                 forwardPos();
471         }
472 }
473
474
475 void DocIterator::backwardChar()
476 {
477         backwardPos();
478         while (!empty() && pos() == lastpos())
479                 backwardPos();
480 }
481
482
483 void DocIterator::backwardPos()
484 {
485         //this dog bites his tail
486         if (empty()) {
487                 push_back(CursorSlice(*inset_));
488                 top().idx() = lastidx();
489                 top().pit() = lastpit();
490                 top().pos() = lastpos();
491                 return;
492         }
493
494         CursorSlice & tip = top();
495
496         if (tip.pos() != 0) {
497                 --tip.pos();
498         } else if (tip.pit() != 0) {
499                 --tip.pit();
500                 tip.pos() = lastpos();
501                 return;
502         } else if (tip.idx() != 0) {
503                 --tip.idx();
504                 tip.pit() = lastpit();
505                 tip.pos() = lastpos();
506                 return;
507         } else {
508                 pop_back();
509                 return;
510         }
511
512         // move into an inset to the left if possible
513         Inset * n = 0;
514
515         if (inMathed()) {
516                 n = (tip.cell().begin() + tip.pos())->nucleus();
517         } else {
518                 if (paragraph().isInset(tip.pos()))
519                         n = paragraph().getInset(tip.pos());
520         }
521
522         if (n && n->isActive()) {
523                 push_back(CursorSlice(*n));
524                 top().idx() = lastidx();
525                 top().pit() = lastpit();
526                 top().pos() = lastpos();
527         }
528 }
529
530
531 bool DocIterator::hasPart(DocIterator const & it) const
532 {
533         // it can't be a part if it is larger
534         if (it.depth() > depth())
535                 return false;
536
537         // as inset adresses are the 'last' level
538         return &it.top().inset() == &slices_[it.depth() - 1].inset();
539 }
540
541
542 void DocIterator::updateInsets(Inset * inset)
543 {
544         // this function re-creates the cache of inset pointers.
545         // code taken in part from StableDocIterator::asDocIterator.
546         //lyxerr << "converting:\n" << *this << endl;
547         DocIterator dit = DocIterator(*inset);
548         size_t const n = slices_.size();
549         for (size_t i = 0 ; i < n; ++i) {
550                 BOOST_ASSERT(inset);
551                 dit.push_back(slices_[i]);
552                 dit.top().inset_ = inset;
553                 if (i + 1 != n)
554                         inset = dit.nextInset();
555         }
556         //lyxerr << "converted:\n" << *this << endl;
557         operator=(dit);
558 }
559
560
561 bool DocIterator::fixIfBroken()
562 {
563         bool fixed = false;
564
565         for (size_t i = slices_.size() - 1; i != 0; --i)
566                 if (!slices_[i].isValid()) {
567                         pop_back();
568                         fixed = true;
569                 }
570
571         // The top level CursorSlice should always be valid.
572         BOOST_ASSERT(slices_[0].isValid());
573
574         if (idx() > lastidx()) {
575                 lyxerr << "wrong idx " << idx()
576                         << ", max is " << lastidx()
577                         << " at level " << depth()
578                         << ". Trying to correct this."  << endl;
579                 lyxerr << "old: " << *this << endl;
580                 for (size_t i = idx(); i != lastidx(); --i)
581                         pop_back();
582                 idx() = lastidx();
583                 pit() = lastpit();
584                 pos() = lastpos();
585                 fixed = true;
586         }
587         else if (pit() > lastpit()) {
588                 lyxerr << "wrong pit " << pit()
589                         << ", max is " << lastpit()
590                         << " at level " << depth()
591                         << ". Trying to correct this."  << endl;
592                 lyxerr << "old: " << *this << endl;
593                 pit() = lastpit();
594                 pos() = 0;
595                 fixed = true;
596         }
597         else if (pos() > lastpos()) {
598                 lyxerr << "wrong pos " << pos()
599                         << ", max is " << lastpos()
600                         << " at level " << depth()
601                         << ". Trying to correct this."  << endl;
602                 lyxerr << "old: " << *this << endl;
603                 pos() = lastpos();
604                 fixed = true;
605         }
606         if (fixed) {
607                 lyxerr << "new: " << *this << endl;
608         }
609         return fixed;
610 }
611
612
613 std::ostream & operator<<(std::ostream & os, DocIterator const & dit)
614 {
615         for (size_t i = 0, n = dit.depth(); i != n; ++i)
616                 os << " " << dit[i] << "\n";
617         return os;
618 }
619
620
621 bool operator<(DocIterator const & p, DocIterator const & q)
622 {
623         size_t depth = std::min(p.depth(), q.depth());
624         for (size_t i = 0 ; i < depth ; ++i) {
625                 if (p[i] != q[i])
626                         return p[i] < q[i];
627         }
628         return p.depth() < q.depth();
629 }
630
631
632 bool operator>(DocIterator const & p, DocIterator const & q)
633 {
634         return q < p;
635 }
636
637
638 bool operator<=(DocIterator const & p, DocIterator const & q)
639 {
640         return !(q < p);
641 }
642
643
644 ///////////////////////////////////////////////////////
645
646 StableDocIterator::StableDocIterator(DocIterator const & dit)
647 {
648         data_ = dit.internalData();
649         for (size_t i = 0, n = data_.size(); i != n; ++i)
650                 data_[i].inset_ = 0;
651 }
652
653
654 DocIterator StableDocIterator::asDocIterator(Inset * inset) const
655 {
656         // this function re-creates the cache of inset pointers
657         //lyxerr << "converting:\n" << *this << endl;
658         DocIterator dit = DocIterator(*inset);
659         for (size_t i = 0, n = data_.size(); i != n; ++i) {
660                 if (inset == 0) {
661                         // FIXME
662                         lyxerr << BOOST_CURRENT_FUNCTION
663                                << " Should not happen, but does e.g. after C-n C-l C-z S-C-z\n"
664                                    << " or when a Buffer has been concurently edited by two views"
665                                 << '\n' << "dit: " << dit << '\n'
666                                 << " lastpos: " << dit.lastpos() << endl;
667                         dit.fixIfBroken();
668                         break;
669                 }
670                 dit.push_back(data_[i]);
671                 dit.top().inset_ = inset;
672                 if (dit.fixIfBroken())
673                         break;
674                 if (i + 1 != n)
675                         inset = dit.nextInset();
676         }
677         //lyxerr << "convert:\n" << *this << " to:\n" << dit << endl;
678         return dit;
679 }
680
681
682 std::ostream & operator<<(std::ostream & os, StableDocIterator const & dit)
683 {
684         for (size_t i = 0, n = dit.data_.size(); i != n; ++i)
685                 os << " " << dit.data_[i] << "\n";
686         return os;
687 }
688
689
690 bool operator==(StableDocIterator const & dit1, StableDocIterator const & dit2)
691 {
692         return dit1.data_ == dit2.data_;
693 }
694
695
696 } // namespace lyx