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