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