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