]> git.lyx.org Git - features.git/blob - src/DocIterator.cpp
Properly search for relevant TocItem. Now works fine with sections inside insets.
[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  *
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::forwardChar()
436 {
437         forwardPos();
438         while (!empty() && pos() == lastpos())
439                 forwardPos();
440 }
441
442
443 void DocIterator::forwardInset()
444 {
445         forwardPos();
446
447         while (!empty() && !nextInset()) {
448                 if (inTexted()) {
449                         pos_type const lastp = lastpos();
450                         Paragraph const & par = paragraph();
451                         pos_type & pos = top().pos();
452                         while (pos < lastp && !par.isInset(pos))
453                                 ++pos;
454                         if (pos < lastp)
455                                 break;
456                 }
457                 forwardPos();
458         }
459 }
460
461
462 void DocIterator::backwardChar()
463 {
464         backwardPos();
465         while (!empty() && pos() == lastpos())
466                 backwardPos();
467 }
468
469
470 void DocIterator::backwardPos()
471 {
472         //this dog bites his tail
473         if (empty()) {
474                 push_back(CursorSlice(*inset_));
475                 top().idx() = lastidx();
476                 top().pit() = lastpit();
477                 top().pos() = lastpos();
478                 return;
479         }
480
481         CursorSlice & tip = top();
482
483         if (tip.pos() != 0) {
484                 --tip.pos();
485         } else if (tip.pit() != 0) {
486                 --tip.pit();
487                 tip.pos() = lastpos();
488                 return;
489         } else if (tip.idx() != 0) {
490                 --tip.idx();
491                 tip.pit() = lastpit();
492                 tip.pos() = lastpos();
493                 return;
494         } else {
495                 pop_back();
496                 return;
497         }
498
499         // move into an inset to the left if possible
500         Inset * n = 0;
501
502         if (inMathed()) {
503                 n = (tip.cell().begin() + tip.pos())->nucleus();
504         } else {
505                 if (paragraph().isInset(tip.pos()))
506                         n = paragraph().getInset(tip.pos());
507         }
508
509         if (n && n->isActive()) {
510                 push_back(CursorSlice(*n));
511                 top().idx() = lastidx();
512                 top().pit() = lastpit();
513                 top().pos() = lastpos();
514         }
515 }
516
517
518 bool DocIterator::hasPart(DocIterator const & it) const
519 {
520         // it can't be a part if it is larger
521         if (it.depth() > depth())
522                 return false;
523
524         // as inset adresses are the 'last' level
525         return &it.top().inset() == &slices_[it.depth() - 1].inset();
526 }
527
528
529 void DocIterator::updateInsets(Inset * inset)
530 {
531         // this function re-creates the cache of inset pointers.
532         // code taken in part from StableDocIterator::asDocIterator.
533         //lyxerr << "converting:\n" << *this << endl;
534         DocIterator dit = DocIterator(*inset);
535         size_t const n = slices_.size();
536         for (size_t i = 0 ; i < n; ++i) {
537                 BOOST_ASSERT(inset);
538                 dit.push_back(slices_[i]);
539                 dit.top().inset_ = inset;
540                 if (i + 1 != n)
541                         inset = dit.nextInset();
542         }
543         //lyxerr << "converted:\n" << *this << endl;
544         operator=(dit);
545 }
546
547
548 std::ostream & operator<<(std::ostream & os, DocIterator const & dit)
549 {
550         for (size_t i = 0, n = dit.depth(); i != n; ++i)
551                 os << " " << dit[i] << "\n";
552         return os;
553 }
554
555
556 bool operator<(DocIterator const & p, DocIterator const & q)
557 {
558         size_t depth = std::min(p.depth(), q.depth());
559         for (size_t i = 0 ; i < depth ; ++i) {
560                 if (p[i] != q[i])
561                         return p[i] < q[i];
562         }
563         return p.depth() < q.depth();
564 }
565
566
567 bool operator>(DocIterator const & p, DocIterator const & q)
568 {
569         return q < p;
570 }
571
572
573 bool operator<=(DocIterator const & p, DocIterator const & q)
574 {
575         return !(q < p);
576 }
577
578
579 ///////////////////////////////////////////////////////
580
581 StableDocIterator::StableDocIterator(DocIterator const & dit)
582 {
583         data_ = dit.internalData();
584         for (size_t i = 0, n = data_.size(); i != n; ++i)
585                 data_[i].inset_ = 0;
586 }
587
588
589 DocIterator StableDocIterator::asDocIterator(Inset * inset) const
590 {
591         // this function re-creates the cache of inset pointers
592         //lyxerr << "converting:\n" << *this << endl;
593         DocIterator dit = DocIterator(*inset);
594         for (size_t i = 0, n = data_.size(); i != n; ++i) {
595                 if (inset == 0) {
596                         // FIXME
597                         lyxerr << BOOST_CURRENT_FUNCTION
598                                << " Should not happen, but does e.g. after C-n C-l C-z S-C-z"
599                                 << '\n' << "dit: " << dit << '\n'
600                                 << " lastpos: " << dit.lastpos() << endl;
601                         //break;
602                         BOOST_ASSERT(false);
603                 }
604                 dit.push_back(data_[i]);
605                 dit.top().inset_ = inset;
606                 if (i + 1 != n)
607                         inset = dit.nextInset();
608         }
609         //lyxerr << "convert:\n" << *this << " to:\n" << dit << endl;
610         return dit;
611 }
612
613
614 std::ostream & operator<<(std::ostream & os, StableDocIterator const & dit)
615 {
616         for (size_t i = 0, n = dit.data_.size(); i != n; ++i)
617                 os << " " << dit.data_[i] << "\n";
618         return os;
619 }
620
621
622 bool operator==(StableDocIterator const & dit1, StableDocIterator const & dit2)
623 {
624         return dit1.data_ == dit2.data_;
625 }
626
627
628 } // namespace lyx