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