]> git.lyx.org Git - lyx.git/blob - src/DocIterator.cpp
Fix layout bug. Pasting text into a cell tried to set Standard layout, because
[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 "support/lassert.h"
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         LASSERT(!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         LASSERT(!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         LASSERT(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         LASSERT(!empty(), /**/);
118         LASSERT(pos() > 0, /**/);
119         return cell()[pos() - 1];
120 }
121
122
123 MathAtom & DocIterator::nextAtom() const
124 {
125         LASSERT(!empty(), /**/);
126         //lyxerr << "lastpos: " << lastpos() << " next atom:\n" << *this << endl;
127         LASSERT(pos() < lastpos(), /**/);
128         return cell()[pos()];
129 }
130
131
132 Text * DocIterator::text() const
133 {
134         LASSERT(!empty(), /**/);
135         return top().text();
136 }
137
138
139 Paragraph & DocIterator::paragraph() const
140 {
141         if (!inTexted())
142                 LYXERR0(*this);
143         LASSERT(inTexted(), /**/);
144         return top().paragraph();
145 }
146
147
148 Paragraph & DocIterator::innerParagraph() const
149 {
150         LASSERT(!empty(), /**/);
151         return innerTextSlice().paragraph();
152 }
153
154
155 CursorSlice const & DocIterator::innerTextSlice() const
156 {
157         LASSERT(!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         LASSERT(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 //      LASSERT(inMathed(), /**/);
226         return top().cell();
227 }
228
229
230 Text * DocIterator::innerText() const
231 {
232         LASSERT(!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         // not at cell/paragraph end?
265         if (tip.pos() != tip.lastpos()) {
266                 // move into an inset to the right if possible
267                 Inset * n = 0;
268                 if (inMathed())
269                         n = (tip.cell().begin() + tip.pos())->nucleus();
270                 else
271                         n = paragraph().getInset(tip.pos());
272                 if (n && n->isActive()) {
273                         //lyxerr << "... descend" << endl;
274                         push_back(CursorSlice(*n));
275                         return;
276                 }
277         }
278
279         // jump to the next cell/paragraph if possible
280         if (!tip.at_end()) {
281                 tip.forwardPos();
282                 return;
283         }
284
285         // otherwise leave inset and jump over inset as a whole
286         pop_back();
287         // 'tip' is invalid now...
288         if (!empty())
289                 ++top().pos();
290 }
291
292
293 void DocIterator::forwardPosIgnoreCollapsed()
294 {
295         Inset * const nextinset = nextInset();
296         // FIXME: the check for asInsetMath() shouldn't be necessary
297         // but math insets do not return a sensible editable() state yet.
298         if (nextinset && !nextinset->asInsetMath()
299             && nextinset->editable() != Inset::HIGHLY_EDITABLE) {
300                 ++top().pos();
301                 return;
302         }
303         forwardPos();
304 }
305
306
307 void DocIterator::forwardPar()
308 {
309         forwardPos();
310
311         while (!empty() && (!inTexted() || pos() != 0)) {
312                 if (inTexted()) {
313                         pos_type const lastp = lastpos();
314                         Paragraph const & par = paragraph();
315                         pos_type & pos = top().pos();
316                         if (par.insetList().empty())
317                                 pos = lastp;
318                         else
319                                 while (pos < lastp && !par.isInset(pos))
320                                         ++pos;
321                 }
322                 forwardPos();
323         }
324 }
325
326
327 void DocIterator::forwardChar()
328 {
329         forwardPos();
330         while (!empty() && pos() == lastpos())
331                 forwardPos();
332 }
333
334
335 void DocIterator::forwardInset()
336 {
337         forwardPos();
338
339         while (!empty() && !nextInset()) {
340                 if (inTexted()) {
341                         pos_type const lastp = lastpos();
342                         Paragraph const & par = paragraph();
343                         pos_type & pos = top().pos();
344                         while (pos < lastp && !par.isInset(pos))
345                                 ++pos;
346                         if (pos < lastp)
347                                 break;
348                 }
349                 forwardPos();
350         }
351 }
352
353
354 void DocIterator::backwardChar()
355 {
356         backwardPos();
357         while (!empty() && pos() == lastpos())
358                 backwardPos();
359 }
360
361
362 void DocIterator::backwardPos()
363 {
364         //this dog bites his tail
365         if (empty()) {
366                 push_back(CursorSlice(*inset_));
367                 top().idx() = lastidx();
368                 top().pit() = lastpit();
369                 top().pos() = lastpos();
370                 return;
371         }
372
373         // at inset beginning?
374         if (top().at_begin()) {
375                 pop_back();
376                 return;
377         }
378
379         top().backwardPos();
380
381         // entered another cell/paragraph from the right?
382         if (top().pos() == top().lastpos())
383                 return;
384
385         // move into an inset to the left if possible
386         Inset * n = 0;
387         if (inMathed())
388                 n = (top().cell().begin() + top().pos())->nucleus();
389         else
390                 n = paragraph().getInset(top().pos());
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                 LASSERT(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(Inset const * inset) const 
497 {
498         for (size_t l = 0; l != slices_.size(); ++l) {
499                 if (&slices_[l].inset() == 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