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