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