]> git.lyx.org Git - lyx.git/blob - src/DocIterator.cpp
Use the much faster forOutliner also to get the tooltip text.
[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 #if 0
453 // works, but currently not needed
454 void DocIterator::backwardInset()
455 {
456         backwardPos();
457
458         while (!empty() && !nextInset()) {
459                 if (inTexted()) {
460                         pos_type const lastp = lastpos();
461                         Paragraph const & par = paragraph();
462                         pos_type & pos = top().pos();
463                         while (pos > 0 && (pos == lastp || !par.isInset(pos)))
464                                 --pos;
465                         if (pos > 0)
466                                 break;
467                 }
468                 backwardPos();
469         }
470 }
471 #endif
472
473
474 bool DocIterator::hasPart(DocIterator const & it) const
475 {
476         // it can't be a part if it is larger
477         if (it.depth() > depth())
478                 return false;
479
480         // as inset adresses are the 'last' level
481         return &it.top().inset() == &slices_[it.depth() - 1].inset();
482 }
483
484
485 bool DocIterator::allowSpellCheck() const
486 {
487         /// spell check is disabled if the iterator position
488         /// is inside of an inset which disables the spell checker
489         size_t const n = depth();
490         for (size_t i = 0; i < n; ++i) {
491                 if (!slices_[i].inset_->allowSpellCheck())
492                         return false;
493         }
494         return true;
495 }
496
497
498 void DocIterator::updateInsets(Inset * inset)
499 {
500         // this function re-creates the cache of inset pointers.
501         //lyxerr << "converting:\n" << *this << endl;
502         DocIterator dit = *this;
503         size_t const n = slices_.size();
504         slices_.resize(0);
505         for (size_t i = 0 ; i < n; ++i) {
506                 LBUFERR(inset);
507                 push_back(dit[i]);
508                 top().inset_ = inset;
509                 if (i + 1 != n)
510                         inset = nextInset();
511         }
512         //lyxerr << "converted:\n" << *this << endl;
513 }
514
515
516 bool DocIterator::fixIfBroken()
517 {
518         if (empty())
519                 return false;
520
521         // Go through the slice stack from the bottom. 
522         // Check that all coordinates (idx, pit, pos) are correct and
523         // that the inset is the one which is claimed to be there
524         Inset * inset = &slices_[0].inset();
525         size_t i = 0;
526         size_t n = slices_.size();
527         for (; i != n; ++i) {
528                 CursorSlice & cs = slices_[i];
529                 if (&cs.inset() != inset) {
530                         // the whole slice is wrong, chop off this as well
531                         --i;
532                         LYXERR(Debug::DEBUG, "fixIfBroken(): inset changed");
533                         break;
534                 } else if (cs.idx() > cs.lastidx()) {
535                         cs.idx() = cs.lastidx();
536                         cs.pit() = cs.lastpit();
537                         cs.pos() = cs.lastpos();
538                         LYXERR(Debug::DEBUG, "fixIfBroken(): idx fixed");
539                         break;
540                 } else if (cs.pit() > cs.lastpit()) {
541                         cs.pit() = cs.lastpit();
542                         cs.pos() = cs.lastpos();
543                         LYXERR(Debug::DEBUG, "fixIfBroken(): pit fixed");
544                         break;
545                 } else if (cs.pos() > cs.lastpos()) {
546                         cs.pos() = cs.lastpos();
547                         LYXERR(Debug::DEBUG, "fixIfBroken(): pos fixed");
548                         break;
549                 } else if (i != n - 1 && cs.pos() != cs.lastpos()) {
550                         // get inset which is supposed to be in the next slice
551                         if (cs.inset().inMathed())
552                                 inset = (cs.cell().begin() + cs.pos())->nucleus();
553                         else if (Inset * csInset = cs.paragraph().getInset(cs.pos()))
554                                 inset = csInset;
555                         else {
556                                 // there are slices left, so there must be another inset
557                                 break;
558                         }
559                 }
560         }
561
562         // Did we make it through the whole slice stack? Otherwise there
563         // was a problem at slice i, and we have to chop off above
564         if (i < n) {
565                 LYXERR(Debug::DEBUG, "fixIfBroken(): cursor chopped at " << i);
566                 resize(i + 1);
567                 return true;
568         } else
569                 return false;
570 }
571
572
573 void DocIterator::sanitize()
574 {
575         // keep a copy of the slices
576         vector<CursorSlice> const sl = slices_;
577         slices_.clear();
578         if (buffer_)
579                 inset_ = &buffer_->inset();
580         Inset * inset = inset_;
581         // re-add the slices one by one, and adjust the inset pointer.
582         for (size_t i = 0, n = sl.size(); i != n; ++i) {
583                 if (inset == 0) {
584                         // FIXME
585                         LYXERR0(" Should not happen, but does e.g. after "
586                                 "C-n C-l C-z S-C-z\n"
587                                 << " or when a Buffer has been concurrently edited by two views"
588                                 << '\n' << "dit: " << *this << '\n'
589                                 << " lastpos: " << slices_[i].lastpos());
590                         fixIfBroken();
591                         break;
592                 }
593                 if (!inset->isActive()) {
594                         LYXERR0("Inset found on cursor stack is not active.");
595                         fixIfBroken();
596                         break;
597                 }
598                 push_back(sl[i]);
599                 top().inset_ = inset;
600                 if (fixIfBroken())
601                         break;
602                 if (i + 1 != n)
603                         inset = nextInset();
604         }
605 }
606
607
608 int DocIterator::find(MathData const & cell) const
609 {
610         for (size_t l = 0; l != slices_.size(); ++l) {
611                 if (slices_[l].asInsetMath() && &slices_[l].cell() == &cell)
612                         return l;
613         }
614         return -1;
615 }
616
617
618 int DocIterator::find(Inset const * inset) const 
619 {
620         for (size_t l = 0; l != slices_.size(); ++l) {
621                 if (&slices_[l].inset() == inset)
622                         return l;
623         }
624         return -1;
625 }
626
627
628 void DocIterator::cutOff(int above, vector<CursorSlice> & cut)
629 {
630         cut = vector<CursorSlice>(slices_.begin() + above + 1, slices_.end());
631         slices_.resize(above + 1);
632 }
633
634
635 void DocIterator::cutOff(int above)
636 {
637         slices_.resize(above + 1);
638 }
639
640
641 void DocIterator::append(vector<CursorSlice> const & x) 
642 {
643         slices_.insert(slices_.end(), x.begin(), x.end());
644 }
645
646
647 void DocIterator::append(DocIterator::idx_type idx, pos_type pos) 
648 {
649         slices_.push_back(CursorSlice());
650         top().idx() = idx;
651         top().pos() = pos;
652 }
653
654
655 ostream & operator<<(ostream & os, DocIterator const & dit)
656 {
657         for (size_t i = 0, n = dit.depth(); i != n; ++i)
658                 os << " " << dit[i] << "\n";
659         return os;
660 }
661
662
663 ///////////////////////////////////////////////////////
664
665 StableDocIterator::StableDocIterator(DocIterator const & dit)
666 {
667         data_ = dit.internalData();
668         for (size_t i = 0, n = data_.size(); i != n; ++i)
669                 data_[i].inset_ = 0;
670 }
671
672
673 DocIterator StableDocIterator::asDocIterator(Buffer * buf) const
674 {
675         DocIterator dit(buf);
676         dit.slices_ = data_;
677         dit.sanitize();
678         return dit;
679 }
680
681
682 ostream & operator<<(ostream & os, StableDocIterator const & dit)
683 {
684         for (size_t i = 0, n = dit.data_.size(); i != n; ++i)
685                 os << " " << dit.data_[i] << "\n";
686         return os;
687 }
688
689
690 bool operator==(StableDocIterator const & dit1, StableDocIterator const & dit2)
691 {
692         return dit1.data_ == dit2.data_;
693 }
694
695
696 } // namespace lyx