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