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