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