]> git.lyx.org Git - lyx.git/blob - src/DocIterator.cpp
prepare Qt 5.6 builds
[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 DocIterator DocIterator::getInnerText() const
230 {
231         DocIterator texted = *this;
232         while (!texted.inTexted()) 
233                 texted.pop_back();
234         return texted;
235 }
236
237
238 pit_type DocIterator::lastpit() const
239 {
240         return inMathed() ? 0 : text()->paragraphs().size() - 1;
241 }
242
243
244 pos_type DocIterator::lastpos() const
245 {
246         return inMathed() ? cell().size() : paragraph().size();
247 }
248
249
250 DocIterator::idx_type DocIterator::lastidx() const
251 {
252         return top().lastidx();
253 }
254
255
256 size_t DocIterator::nargs() const
257 {
258         // assume 1x1 grid for main text
259         return top().nargs();
260 }
261
262
263 size_t DocIterator::ncols() const
264 {
265         // assume 1x1 grid for main text
266         return top().ncols();
267 }
268
269
270 size_t DocIterator::nrows() const
271 {
272         // assume 1x1 grid for main text
273         return top().nrows();
274 }
275
276
277 DocIterator::row_type DocIterator::row() const
278 {
279         return top().row();
280 }
281
282
283 DocIterator::col_type DocIterator::col() const
284 {
285         return top().col();
286 }
287
288
289 MathData & DocIterator::cell() const
290 {
291 //      LASSERT(inMathed(), /**/);
292         return top().cell();
293 }
294
295
296 Text * DocIterator::innerText() const
297 {
298         LASSERT(!empty(), return 0);
299         return innerTextSlice().text();
300 }
301
302
303 Inset * DocIterator::innerInsetOfType(int code) const
304 {
305         for (int i = depth() - 1; i >= 0; --i)
306                 if (slices_[i].inset_->lyxCode() == code)
307                         return slices_[i].inset_;
308         return 0;
309 }
310
311
312 // This duplicates code above, but is in the critical path.
313 // So please think twice before adding stuff
314 void DocIterator::forwardPos()
315 {
316         // this dog bites his tail
317         if (empty()) {
318                 push_back(CursorSlice(*inset_));
319                 return;
320         }
321
322         CursorSlice & tip = top();
323         //lyxerr << "XXX\n" << *this << endl;
324
325         // not at cell/paragraph end?
326         if (tip.pos() != tip.lastpos()) {
327                 // move into an inset to the right if possible
328                 Inset * n = 0;
329                 if (inMathed())
330                         n = (tip.cell().begin() + tip.pos())->nucleus();
331                 else
332                         n = paragraph().getInset(tip.pos());
333                 if (n && n->isActive()) {
334                         //lyxerr << "... descend" << endl;
335                         push_back(CursorSlice(*n));
336                         return;
337                 }
338         }
339
340         // jump to the next cell/paragraph if possible
341         if (!tip.at_end()) {
342                 tip.forwardPos();
343                 return;
344         }
345
346         // otherwise leave inset and jump over inset as a whole
347         pop_back();
348         // 'tip' is invalid now...
349         if (!empty())
350                 ++top().pos();
351 }
352
353
354 void DocIterator::forwardPosIgnoreCollapsed()
355 {
356         Inset * const nextinset = nextInset();
357         // FIXME: the check for asInsetMath() shouldn't be necessary
358         // but math insets do not return a sensible editable() state yet.
359         if (nextinset && !nextinset->asInsetMath()
360             && !nextinset->editable()) {
361                 ++top().pos();
362                 return;
363         }
364         forwardPos();
365 }
366
367
368 void DocIterator::forwardPar()
369 {
370         forwardPos();
371
372         while (!empty() && (!inTexted() || pos() != 0)) {
373                 if (inTexted()) {
374                         pos_type const lastp = lastpos();
375                         Paragraph const & par = paragraph();
376                         pos_type & pos = top().pos();
377                         if (par.insetList().empty())
378                                 pos = lastp;
379                         else
380                                 while (pos < lastp && !par.isInset(pos))
381                                         ++pos;
382                 }
383                 forwardPos();
384         }
385 }
386
387
388 void DocIterator::forwardChar()
389 {
390         forwardPos();
391         while (!empty() && pos() == lastpos())
392                 forwardPos();
393 }
394
395
396 void DocIterator::forwardInset()
397 {
398         forwardPos();
399
400         while (!empty() && !nextInset()) {
401                 if (inTexted()) {
402                         pos_type const lastp = lastpos();
403                         Paragraph const & par = paragraph();
404                         pos_type & pos = top().pos();
405                         while (pos < lastp && !par.isInset(pos))
406                                 ++pos;
407                         if (pos < lastp)
408                                 break;
409                 }
410                 forwardPos();
411         }
412 }
413
414
415 void DocIterator::backwardChar()
416 {
417         backwardPos();
418         while (!empty() && pos() == lastpos())
419                 backwardPos();
420 }
421
422
423 void DocIterator::backwardPos()
424 {
425         //this dog bites his tail
426         if (empty()) {
427                 push_back(CursorSlice(*inset_));
428                 top().idx() = lastidx();
429                 top().pit() = lastpit();
430                 top().pos() = lastpos();
431                 return;
432         }
433
434         // at inset beginning?
435         if (top().at_begin()) {
436                 pop_back();
437                 return;
438         }
439
440         top().backwardPos();
441
442         // entered another cell/paragraph from the right?
443         if (top().pos() == top().lastpos())
444                 return;
445
446         // move into an inset to the left if possible
447         Inset * n = 0;
448         if (inMathed())
449                 n = (top().cell().begin() + top().pos())->nucleus();
450         else
451                 n = paragraph().getInset(top().pos());
452         if (n && n->isActive()) {
453                 push_back(CursorSlice(*n));
454                 top().idx() = lastidx();
455                 top().pit() = lastpit();
456                 top().pos() = lastpos();
457         }
458 }
459
460
461 #if 0
462 // works, but currently not needed
463 void DocIterator::backwardInset()
464 {
465         backwardPos();
466
467         while (!empty() && !nextInset()) {
468                 if (inTexted()) {
469                         pos_type const lastp = lastpos();
470                         Paragraph const & par = paragraph();
471                         pos_type & pos = top().pos();
472                         while (pos > 0 && (pos == lastp || !par.isInset(pos)))
473                                 --pos;
474                         if (pos > 0)
475                                 break;
476                 }
477                 backwardPos();
478         }
479 }
480 #endif
481
482
483 bool DocIterator::hasPart(DocIterator const & it) const
484 {
485         // it can't be a part if it is larger
486         if (it.depth() > depth())
487                 return false;
488
489         // as inset adresses are the 'last' level
490         return &it.top().inset() == &slices_[it.depth() - 1].inset();
491 }
492
493
494 bool DocIterator::allowSpellCheck() const
495 {
496         /// spell check is disabled if the iterator position
497         /// is inside of an inset which disables the spell checker
498         size_t const n = depth();
499         for (size_t i = 0; i < n; ++i) {
500                 if (!slices_[i].inset_->allowSpellCheck())
501                         return false;
502         }
503         return true;
504 }
505
506
507 void DocIterator::updateInsets(Inset * inset)
508 {
509         // this function re-creates the cache of inset pointers.
510         //lyxerr << "converting:\n" << *this << endl;
511         DocIterator dit = *this;
512         size_t const n = slices_.size();
513         slices_.resize(0);
514         for (size_t i = 0 ; i < n; ++i) {
515                 LBUFERR(inset);
516                 push_back(dit[i]);
517                 top().inset_ = inset;
518                 if (i + 1 != n)
519                         inset = nextInset();
520         }
521         //lyxerr << "converted:\n" << *this << endl;
522 }
523
524
525 bool DocIterator::fixIfBroken()
526 {
527         if (empty())
528                 return false;
529
530         // Go through the slice stack from the bottom. 
531         // Check that all coordinates (idx, pit, pos) are correct and
532         // that the inset is the one which is claimed to be there
533         Inset * inset = &slices_[0].inset();
534         size_t i = 0;
535         size_t n = slices_.size();
536         for (; i != n; ++i) {
537                 CursorSlice & cs = slices_[i];
538                 if (&cs.inset() != inset) {
539                         // the whole slice is wrong, chop off this as well
540                         --i;
541                         LYXERR(Debug::DEBUG, "fixIfBroken(): inset changed");
542                         break;
543                 } else if (cs.idx() > cs.lastidx()) {
544                         cs.idx() = cs.lastidx();
545                         cs.pit() = cs.lastpit();
546                         cs.pos() = cs.lastpos();
547                         LYXERR(Debug::DEBUG, "fixIfBroken(): idx fixed");
548                         break;
549                 } else if (cs.pit() > cs.lastpit()) {
550                         cs.pit() = cs.lastpit();
551                         cs.pos() = cs.lastpos();
552                         LYXERR(Debug::DEBUG, "fixIfBroken(): pit fixed");
553                         break;
554                 } else if (cs.pos() > cs.lastpos()) {
555                         cs.pos() = cs.lastpos();
556                         LYXERR(Debug::DEBUG, "fixIfBroken(): pos fixed");
557                         break;
558                 } else if (i != n - 1 && cs.pos() != cs.lastpos()) {
559                         // get inset which is supposed to be in the next slice
560                         if (cs.inset().inMathed())
561                                 inset = (cs.cell().begin() + cs.pos())->nucleus();
562                         else if (Inset * csInset = cs.paragraph().getInset(cs.pos()))
563                                 inset = csInset;
564                         else {
565                                 // there are slices left, so there must be another inset
566                                 break;
567                         }
568                 }
569         }
570
571         // Did we make it through the whole slice stack? Otherwise there
572         // was a problem at slice i, and we have to chop off above
573         if (i < n) {
574                 LYXERR(Debug::DEBUG, "fixIfBroken(): cursor chopped at " << i);
575                 resize(i + 1);
576                 return true;
577         } else
578                 return false;
579 }
580
581
582 void DocIterator::sanitize()
583 {
584         // keep a copy of the slices
585         vector<CursorSlice> const sl = slices_;
586         slices_.clear();
587         if (buffer_)
588                 inset_ = &buffer_->inset();
589         Inset * inset = inset_;
590         // re-add the slices one by one, and adjust the inset pointer.
591         for (size_t i = 0, n = sl.size(); i != n; ++i) {
592                 if (inset == 0) {
593                         // FIXME
594                         LYXERR0(" Should not happen, but does e.g. after "
595                                 "C-n C-l C-z S-C-z\n"
596                                 << " or when a Buffer has been concurrently edited by two views"
597                                 << '\n' << "dit: " << *this << '\n'
598                                 << " lastpos: " << slices_[i].lastpos());
599                         fixIfBroken();
600                         break;
601                 }
602                 if (!inset->isActive()) {
603                         LYXERR0("Inset found on cursor stack is not active.");
604                         fixIfBroken();
605                         break;
606                 }
607                 push_back(sl[i]);
608                 top().inset_ = inset;
609                 if (fixIfBroken())
610                         break;
611                 if (i + 1 != n)
612                         inset = nextInset();
613         }
614 }
615
616
617 int DocIterator::find(MathData const & cell) const
618 {
619         for (size_t l = 0; l != slices_.size(); ++l) {
620                 if (slices_[l].asInsetMath() && &slices_[l].cell() == &cell)
621                         return l;
622         }
623         return -1;
624 }
625
626
627 int DocIterator::find(Inset const * inset) const 
628 {
629         for (size_t l = 0; l != slices_.size(); ++l) {
630                 if (&slices_[l].inset() == inset)
631                         return l;
632         }
633         return -1;
634 }
635
636
637 void DocIterator::cutOff(int above, vector<CursorSlice> & cut)
638 {
639         cut = vector<CursorSlice>(slices_.begin() + above + 1, slices_.end());
640         slices_.resize(above + 1);
641 }
642
643
644 void DocIterator::cutOff(int above)
645 {
646         slices_.resize(above + 1);
647 }
648
649
650 void DocIterator::append(vector<CursorSlice> const & x) 
651 {
652         slices_.insert(slices_.end(), x.begin(), x.end());
653 }
654
655
656 void DocIterator::append(DocIterator::idx_type idx, pos_type pos) 
657 {
658         slices_.push_back(CursorSlice());
659         top().idx() = idx;
660         top().pos() = pos;
661 }
662
663
664 ostream & operator<<(ostream & os, DocIterator const & dit)
665 {
666         for (size_t i = 0, n = dit.depth(); i != n; ++i)
667                 os << " " << dit[i] << "\n";
668         return os;
669 }
670
671
672 ///////////////////////////////////////////////////////
673
674 StableDocIterator::StableDocIterator(DocIterator const & dit)
675 {
676         data_ = dit.internalData();
677         for (size_t i = 0, n = data_.size(); i != n; ++i)
678                 data_[i].inset_ = 0;
679 }
680
681
682 DocIterator StableDocIterator::asDocIterator(Buffer * buf) const
683 {
684         DocIterator dit(buf);
685         dit.slices_ = data_;
686         dit.sanitize();
687         return dit;
688 }
689
690
691 ostream & operator<<(ostream & os, StableDocIterator const & dit)
692 {
693         for (size_t i = 0, n = dit.data_.size(); i != n; ++i)
694                 os << " " << dit.data_[i] << "\n";
695         return os;
696 }
697
698
699 bool operator==(StableDocIterator const & dit1, StableDocIterator const & dit2)
700 {
701         return dit1.data_ == dit2.data_;
702 }
703
704
705 } // namespace lyx