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