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