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