]> git.lyx.org Git - features.git/blob - src/DocIterator.cpp
some refactoring.
[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().getInset(pos());
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().getInset(pos() - 1);
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                         n = paragraph().getInset(tip.pos());
275         }
276
277         if (n && n->isActive()) {
278                 //lyxerr << "... descend" << endl;
279                 push_back(CursorSlice(*n));
280                 return;
281         }
282
283         if (!tip.at_end()) {
284                 tip.forwardPos();
285                 return;
286         }
287         // otherwise leave inset and jump over inset as a whole
288         pop_back();
289         // 'tip' is invalid now...
290         if (!empty())
291                 ++top().pos();
292 }
293
294
295 void DocIterator::forwardPosIgnoreCollapsed()
296 {
297         Inset * const nextinset = nextInset();
298         // FIXME: the check for asInsetMath() shouldn't be necessary
299         // but math insets do not return a sensible editable() state yet.
300         if (nextinset && !nextinset->asInsetMath()
301             && nextinset->editable() != Inset::HIGHLY_EDITABLE) {
302                 ++top().pos();
303                 return;
304         }
305         forwardPos();
306 }
307
308
309 void DocIterator::forwardPar()
310 {
311         forwardPos();
312
313         while (!empty() && (!inTexted() || pos() != 0)) {
314                 if (inTexted()) {
315                         pos_type const lastp = lastpos();
316                         Paragraph const & par = paragraph();
317                         pos_type & pos = top().pos();
318                         if (par.insetList().empty())
319                                 pos = lastp;
320                         else
321                                 while (pos < lastp && !par.isInset(pos))
322                                         ++pos;
323                 }
324                 forwardPos();
325         }
326 }
327
328
329 void DocIterator::forwardChar()
330 {
331         forwardPos();
332         while (!empty() && pos() == lastpos())
333                 forwardPos();
334 }
335
336
337 void DocIterator::forwardInset()
338 {
339         forwardPos();
340
341         while (!empty() && !nextInset()) {
342                 if (inTexted()) {
343                         pos_type const lastp = lastpos();
344                         Paragraph const & par = paragraph();
345                         pos_type & pos = top().pos();
346                         while (pos < lastp && !par.isInset(pos))
347                                 ++pos;
348                         if (pos < lastp)
349                                 break;
350                 }
351                 forwardPos();
352         }
353 }
354
355
356 void DocIterator::backwardChar()
357 {
358         backwardPos();
359         while (!empty() && pos() == lastpos())
360                 backwardPos();
361 }
362
363
364 void DocIterator::backwardPos()
365 {
366         //this dog bites his tail
367         if (empty()) {
368                 push_back(CursorSlice(*inset_));
369                 top().idx() = lastidx();
370                 top().pit() = lastpit();
371                 top().pos() = lastpos();
372                 return;
373         }
374
375         if (top().at_begin()) {
376                 pop_back();
377                 return;
378         }
379
380         top().backwardPos();
381
382         // move into an inset to the left if possible
383         Inset * n = 0;
384
385         if (inMathed())
386                 n = (top().cell().begin() + top().pos())->nucleus();
387         else
388                 n = paragraph().getInset(top().pos());
389
390         if (n && n->isActive()) {
391                 push_back(CursorSlice(*n));
392                 top().idx() = lastidx();
393                 top().pit() = lastpit();
394                 top().pos() = lastpos();
395         }
396 }
397
398
399 bool DocIterator::hasPart(DocIterator const & it) const
400 {
401         // it can't be a part if it is larger
402         if (it.depth() > depth())
403                 return false;
404
405         // as inset adresses are the 'last' level
406         return &it.top().inset() == &slices_[it.depth() - 1].inset();
407 }
408
409
410 void DocIterator::updateInsets(Inset * inset)
411 {
412         // this function re-creates the cache of inset pointers.
413         //lyxerr << "converting:\n" << *this << endl;
414         DocIterator dit = *this;
415         size_t const n = slices_.size();
416         slices_.resize(0);
417         for (size_t i = 0 ; i < n; ++i) {
418                 BOOST_ASSERT(inset);
419                 push_back(dit[i]);
420                 top().inset_ = inset;
421                 if (i + 1 != n)
422                         inset = nextInset();
423         }
424         //lyxerr << "converted:\n" << *this << endl;
425 }
426
427
428 bool DocIterator::fixIfBroken()
429 {
430         // Go through the slice stack from the bottom. 
431         // Check that all coordinates (idx, pit, pos) are correct and
432         // that the inset is the one which is claimed to be there
433         Inset * inset = &slices_[0].inset();
434         size_t i = 0;
435         size_t n = slices_.size();
436         for (; i != n; ++i) {
437                 CursorSlice & cs = slices_[i];
438                 if (&cs.inset() != inset) {
439                         // the whole slice is wrong, chop off this as well
440                         --i;
441                         LYXERR(Debug::DEBUG, "fixIfBroken(): inset changed");
442                         break;
443                 } else if (cs.idx() > cs.lastidx()) {
444                         cs.idx() = cs.lastidx();
445                         cs.pit() = cs.lastpit();
446                         cs.pos() = cs.lastpos();
447                         LYXERR(Debug::DEBUG, "fixIfBroken(): idx fixed");
448                         break;
449                 } else if (cs.pit() > cs.lastpit()) {
450                         cs.pit() = cs.lastpit();
451                         cs.pos() = cs.lastpos();
452                         LYXERR(Debug::DEBUG, "fixIfBroken(): pit fixed");
453                         break;
454                 } else if (cs.pos() > cs.lastpos()) {
455                         cs.pos() = cs.lastpos();
456                         LYXERR(Debug::DEBUG, "fixIfBroken(): pos fixed");
457                         break;
458                 } else if (i != n - 1 && cs.pos() != cs.lastpos()) {
459                         // get inset which is supposed to be in the next slice
460                         if (cs.inset().inMathed())
461                                 inset = (cs.cell().begin() + cs.pos())->nucleus();
462                         else if (Inset * csInset = cs.paragraph().getInset(cs.pos()))
463                                 inset = csInset;
464                         else {
465                                 // there are slices left, so there must be another inset
466                                 break;
467                         }
468                 }
469         }
470
471         // Did we make it through the whole slice stack? Otherwise there
472         // was a problem at slice i, and we have to chop off above
473         if (i < n) {
474                 LYXERR(Debug::DEBUG, "fixIfBroken(): cursor chopped at " << i);
475                 resize(i + 1);
476                 return true;
477         } else
478                 return false;
479 }
480
481
482 DocIterator::idx_type DocIterator::find(MathData const & cell) const
483 {
484         for (size_t l = 0; l != slices_.size(); ++l) {
485                 if (slices_[l].asInsetMath() && &slices_[l].cell() == &cell)
486                         return l;
487         }
488         return -1;
489 }
490
491
492 DocIterator::idx_type DocIterator::find(InsetMath const * inset) const 
493 {
494         for (size_t l = 0; l != slices_.size(); ++l) {
495                 if (slices_[l].asInsetMath() == inset)
496                         return l;
497         }
498         return -1;
499 }
500
501
502 void DocIterator::cutOff(DocIterator::idx_type above, vector<CursorSlice> & cut)
503 {
504         cut = vector<CursorSlice>(slices_.begin() + above + 1, slices_.end());
505         slices_.resize(above + 1);
506 }
507
508
509 void DocIterator::cutOff(DocIterator::idx_type above) 
510 {
511         slices_.resize(above + 1);
512 }
513
514
515 void DocIterator::append(vector<CursorSlice> const & x) 
516 {
517         slices_.insert(slices_.end(), x.begin(), x.end());
518 }
519
520
521 void DocIterator::append(DocIterator::idx_type idx, pos_type pos) 
522 {
523         slices_.push_back(CursorSlice());
524         top().idx() = idx;
525         top().pos() = pos;
526 }
527
528
529 ostream & operator<<(ostream & os, DocIterator const & dit)
530 {
531         for (size_t i = 0, n = dit.depth(); i != n; ++i)
532                 os << " " << dit[i] << "\n";
533         return os;
534 }
535
536
537 ///////////////////////////////////////////////////////
538
539 StableDocIterator::StableDocIterator(DocIterator const & dit)
540 {
541         data_ = dit.internalData();
542         for (size_t i = 0, n = data_.size(); i != n; ++i)
543                 data_[i].inset_ = 0;
544 }
545
546
547 DocIterator StableDocIterator::asDocIterator(Inset * inset) const
548 {
549         // this function re-creates the cache of inset pointers
550         //lyxerr << "converting:\n" << *this << endl;
551         DocIterator dit = DocIterator(*inset);
552         for (size_t i = 0, n = data_.size(); i != n; ++i) {
553                 if (inset == 0) {
554                         // FIXME
555                         LYXERR0(" Should not happen, but does e.g. after "
556                                 "C-n C-l C-z S-C-z\n"
557                                 << " or when a Buffer has been concurrently edited by two views"
558                                 << '\n' << "dit: " << dit << '\n'
559                                 << " lastpos: " << dit.lastpos());
560                         dit.fixIfBroken();
561                         break;
562                 }
563                 dit.push_back(data_[i]);
564                 dit.top().inset_ = inset;
565                 if (dit.fixIfBroken())
566                         break;
567                 if (i + 1 != n)
568                         inset = dit.nextInset();
569         }
570         //lyxerr << "convert:\n" << *this << " to:\n" << dit << endl;
571         return dit;
572 }
573
574
575 ostream & operator<<(ostream & os, StableDocIterator const & dit)
576 {
577         for (size_t i = 0, n = dit.data_.size(); i != n; ++i)
578                 os << " " << dit.data_[i] << "\n";
579         return os;
580 }
581
582
583 bool operator==(StableDocIterator const & dit1, StableDocIterator const & dit2)
584 {
585         return dit1.data_ == dit2.data_;
586 }
587
588
589 } // namespace lyx