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