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