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