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