]> git.lyx.org Git - lyx.git/blob - src/dociterator.C
* src/frontends/qt4/QTocDialog.C (updateGui):
[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                 return prevAtom().nucleus();
82         return paragraph().isInset(pos() - 1) ? paragraph().getInset(pos() - 1) : 0;
83 }
84
85
86 InsetBase const * DocIterator::prevInset() const
87 {
88         BOOST_ASSERT(!empty());
89         if (pos() == 0)
90                 return 0;
91         if (inMathed())
92                 return prevAtom().nucleus();
93         return paragraph().isInset(pos() - 1) ? paragraph().getInset(pos() - 1) : 0;
94 }
95
96
97 InsetBase * DocIterator::realInset() const
98 {
99         BOOST_ASSERT(inTexted());
100         // if we are in a tabular, we need the cell
101         if (inset().lyxCode() == InsetBase::TABULAR_CODE) {
102                 InsetTabular & tabular = static_cast<InsetTabular&>(inset());
103                 return tabular.cell(idx()).get();
104         }
105         return &inset();
106 }
107
108
109 MathAtom const & DocIterator::prevAtom() const
110 {
111         BOOST_ASSERT(!empty());
112         BOOST_ASSERT(pos() > 0);
113         return cell()[pos() - 1];
114 }
115
116
117 MathAtom & DocIterator::prevAtom()
118 {
119         BOOST_ASSERT(!empty());
120         BOOST_ASSERT(pos() > 0);
121         return cell()[pos() - 1];
122 }
123
124
125 MathAtom const & DocIterator::nextAtom() const
126 {
127         BOOST_ASSERT(!empty());
128         //lyxerr << "lastpos: " << lastpos() << " next atom:\n" << *this << endl;
129         BOOST_ASSERT(pos() < lastpos());
130         return cell()[pos()];
131 }
132
133
134 MathAtom & DocIterator::nextAtom()
135 {
136         BOOST_ASSERT(!empty());
137         //lyxerr << "lastpos: " << lastpos() << " next atom:\n" << *this << endl;
138         BOOST_ASSERT(pos() < lastpos());
139         return cell()[pos()];
140 }
141
142
143 LyXText * DocIterator::text()
144 {
145         BOOST_ASSERT(!empty());
146         return top().text();
147 }
148
149 LyXText const * DocIterator::text() const
150 {
151         BOOST_ASSERT(!empty());
152         return top().text();
153 }
154
155
156 Paragraph & DocIterator::paragraph()
157 {
158         if (!inTexted())
159                 lyxerr << *this << endl;
160         BOOST_ASSERT(inTexted());
161         return top().paragraph();
162 }
163
164
165 Paragraph const & DocIterator::paragraph() const
166 {
167         BOOST_ASSERT(inTexted());
168         return top().paragraph();
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 MathArray const & DocIterator::cell() const
224 {
225         BOOST_ASSERT(inMathed());
226         return top().cell();
227 }
228
229
230 MathArray & DocIterator::cell()
231 {
232         BOOST_ASSERT(inMathed());
233         return top().cell();
234 }
235
236
237 bool DocIterator::inMathed() const
238 {
239         return !empty() && inset().inMathed();
240 }
241
242
243 bool DocIterator::inTexted() const
244 {
245         return !empty() && !inset().inMathed();
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         // jump over collapsables if they are collapsed
290         // FIXME: the check for asInsetMath() shouldn't be necessary
291         // but math insets do not return a sensible editable() state yet.
292         if (ignorecollapsed && nextInset() && (!nextInset()->asInsetMath()
293             && nextInset()->editable() != InsetBase::HIGHLY_EDITABLE)) {
294                 ++top().pos();
295                 return;
296         }
297
298         CursorSlice & tip = top();
299         //lyxerr << "XXX\n" << *this << endl;
300
301         // this is used twice and shows up in the profiler!
302         pos_type const lastp = lastpos();
303
304         // move into an inset to the right if possible
305         InsetBase * n = 0;
306
307         if (tip.pos() != lastp) {
308                 // this is impossible for pos() == size()
309                 if (inMathed()) {
310                         n = (tip.cell().begin() + tip.pos())->nucleus();
311                 } else {
312                         if (paragraph().isInset(tip.pos()))
313                                 n = paragraph().getInset(tip.pos());
314                 }
315         }
316
317         if (n && n->isActive()) {
318                 //lyxerr << "... descend" << endl;
319                 push_back(CursorSlice(*n));
320                 return;
321         }
322
323         // otherwise move on one position if possible
324         if (tip.pos() < lastp) {
325                 //lyxerr << "... next pos" << endl;
326                 ++tip.pos();
327                 return;
328         }
329         //lyxerr << "... no next pos" << endl;
330
331         // otherwise move on one paragraph if possible
332         if (tip.pit() < lastpit()) {
333                 //lyxerr << "... next par" << endl;
334                 ++tip.pit();
335                 tip.pos() = 0;
336                 return;
337         }
338         //lyxerr << "... no next pit" << endl;
339
340         // otherwise try to move on one cell if possible
341         if (tip.idx() < lastidx()) {
342                 //lyxerr << "... next idx" << endl;
343                 ++tip.idx();
344                 tip.pit() = 0;
345                 tip.pos() = 0;
346                 return;
347         }
348         //lyxerr << "... no next idx" << endl;
349
350         // otherwise leave inset and jump over inset as a whole
351         pop_back();
352         // 'top' is invalid now...
353         if (!empty())
354                 ++top().pos();
355 }
356
357
358 void DocIterator::forwardPosNoDescend()
359 {
360         CursorSlice & tip = top();
361         pos_type const lastp = lastpos();
362
363         //  move on one position if possible
364         if (tip.pos() < lastp) {
365                 //lyxerr << "... next pos" << endl;
366                 ++tip.pos();
367                 return;
368         }
369         //lyxerr << "... no next pos" << endl;
370
371         // otherwise move on one paragraph if possible
372         if (tip.pit() < lastpit()) {
373                 //lyxerr << "... next par" << endl;
374                 ++tip.pit();
375                 tip.pos() = 0;
376                 return;
377         }
378         //lyxerr << "... no next pit" << endl;
379
380         // otherwise try to move on one cell if possible
381         if (tip.idx() < lastidx()) {
382                 //lyxerr << "... next idx" << endl;
383                 ++tip.idx();
384                 tip.pit() = 0;
385                 tip.pos() = 0;
386                 return;
387         }
388         //lyxerr << "... no next idx" << endl;
389
390         // otherwise leave inset and jump over inset as a whole
391         pop_back();
392         // 'top' is invalid now...
393         if (!empty())
394                 ++top().pos();
395 }
396
397
398 void DocIterator::forwardPar()
399 {
400         forwardPos();
401
402         while (!empty() && (!inTexted() || pos() != 0)) {
403                 if (inTexted()) {
404                         pos_type const lastp = lastpos();
405                         Paragraph const & par = paragraph();
406                         pos_type & pos = top().pos();
407                         while (pos < lastp && !par.isInset(pos))
408                                 ++pos;
409                 }
410                 forwardPos();
411         }
412 }
413
414
415 void DocIterator::forwardChar()
416 {
417         forwardPos();
418         while (!empty() && pos() == lastpos())
419                 forwardPos();
420 }
421
422
423 void DocIterator::forwardInset()
424 {
425         forwardPos();
426
427         while (!empty() && !nextInset()) {
428                 if (inTexted()) {
429                         pos_type const lastp = lastpos();
430                         Paragraph const & par = paragraph();
431                         pos_type & pos = top().pos();
432                         while (pos < lastp && !par.isInset(pos))
433                                 ++pos;
434                         if (pos < lastp)
435                                 break;
436                 }
437                 forwardPos();
438         }
439 }
440
441
442 void DocIterator::backwardChar()
443 {
444         backwardPos();
445         while (!empty() && pos() == lastpos())
446                 backwardPos();
447 }
448
449
450 void DocIterator::backwardPos()
451 {
452         //this dog bites his tail
453         if (empty()) {
454                 push_back(CursorSlice(*inset_));
455                 top().idx() = lastidx();
456                 top().pit() = lastpit();
457                 top().pos() = lastpos();
458                 return;
459         }
460
461         CursorSlice & tip = top();
462
463         if (tip.pos() != 0) {
464                 --tip.pos();
465         } else if (tip.pit() != 0) {
466                 --tip.pit();
467                 tip.pos() = lastpos();
468                 return;
469         } else if (tip.idx() != 0) {
470                 --tip.idx();
471                 tip.pit() = lastpit();
472                 tip.pos() = lastpos();
473                 return;
474         } else {
475                 pop_back();
476                 return;
477         }
478
479         // move into an inset to the left if possible
480         InsetBase * n = 0;
481
482         if (inMathed()) {
483                 n = (tip.cell().begin() + tip.pos())->nucleus();
484         } else {
485                 if (paragraph().isInset(tip.pos()))
486                         n = paragraph().getInset(tip.pos());
487         }
488
489         if (n && n->isActive()) {
490                 push_back(CursorSlice(*n));
491                 top().idx() = lastidx();
492                 top().pit() = lastpit();
493                 top().pos() = lastpos();
494         }
495 }
496
497
498 bool DocIterator::hasPart(DocIterator const & it) const
499 {
500         // it can't be a part if it is larger
501         if (it.depth() > depth())
502                 return false;
503
504         // as inset adresses are the 'last' level
505         return &it.top().inset() == &slices_[it.depth() - 1].inset();
506 }
507
508
509 void DocIterator::updateInsets(InsetBase * inset)
510 {
511         // this function re-creates the cache of inset pointers.
512         // code taken in part from StableDocIterator::asDocIterator.
513         //lyxerr << "converting:\n" << *this << endl;
514         DocIterator dit = DocIterator(*inset);
515         size_t const n = slices_.size();
516         for (size_t i = 0 ; i < n; ++i) {
517                 BOOST_ASSERT(inset);
518                 dit.push_back(slices_[i]);
519                 dit.top().inset_ = inset;
520                 if (i + 1 != n)
521                         inset = dit.nextInset();
522         }
523         //lyxerr << "converted:\n" << *this << endl;
524         operator=(dit);
525 }
526
527
528 std::ostream & operator<<(std::ostream & os, DocIterator const & dit)
529 {
530         for (size_t i = 0, n = dit.depth(); i != n; ++i)
531                 os << " " << dit[i] << "\n";
532         return os;
533 }
534
535
536
537 ///////////////////////////////////////////////////////
538
539 StableDocIterator::StableDocIterator(DocIterator const & dit)
540 {
541         data_ = dit.internalData();
542         for (size_t i = 0, n = data_.size(); i != n; ++i)
543                 data_[i].inset_ = 0;
544 }
545
546
547 DocIterator StableDocIterator::asDocIterator(InsetBase * inset) const
548 {
549         // this function re-creates the cache of inset pointers
550         //lyxerr << "converting:\n" << *this << endl;
551         DocIterator dit = DocIterator(*inset);
552         for (size_t i = 0, n = data_.size(); i != n; ++i) {
553                 if (inset == 0) {
554                         // FIXME
555                         lyxerr << BOOST_CURRENT_FUNCTION
556                                << " Should not happen, but does e.g. after C-n C-l C-z S-C-z"
557                                 << '\n' << "dit: " << dit << '\n'
558                                 << " lastpos: " << dit.lastpos() << endl;
559                         //break;
560                         BOOST_ASSERT(false);
561                 }
562                 dit.push_back(data_[i]);
563                 dit.top().inset_ = inset;
564                 if (i + 1 != n)
565                         inset = dit.nextInset();
566         }
567         //lyxerr << "convert:\n" << *this << " to:\n" << dit << endl;
568         return dit;
569 }
570
571
572 std::ostream & operator<<(std::ostream & os, StableDocIterator const & dit)
573 {
574         for (size_t i = 0, n = dit.data_.size(); i != n; ++i)
575                 os << " " << dit.data_[i] << "\n";
576         return os;
577 }
578
579
580 bool operator==(StableDocIterator const & dit1, StableDocIterator const & dit2)
581 {
582         return dit1.data_ == dit2.data_;
583 }
584
585
586 } // namespace lyx