]> git.lyx.org Git - lyx.git/blob - src/dociterator.C
Fix 3188, update the labels at each Caption insertion.
[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 LyXText * DocIterator::innerText()
238 {
239         BOOST_ASSERT(!empty());
240         // Go up until first non-0 text is hit
241         // (innermost text is 0 in mathed)
242         for (int i = depth() - 1; i >= 0; --i)
243                 if (slices_[i].text())
244                         return slices_[i].text();
245         return 0;
246 }
247
248 LyXText const * DocIterator::innerText() const
249 {
250         BOOST_ASSERT(!empty());
251         // go up until first non-0 text is hit
252         // (innermost text is 0 in mathed)
253         for (int i = depth() - 1; i >= 0; --i)
254                 if (slices_[i].text())
255                         return slices_[i].text();
256         return 0;
257 }
258
259
260 InsetBase * DocIterator::innerInsetOfType(int code) const
261 {
262         for (int i = depth() - 1; i >= 0; --i)
263                 if (slices_[i].inset_->lyxCode() == code)
264                         return slices_[i].inset_;
265         return 0;
266 }
267
268
269 void DocIterator::forwardPos(bool ignorecollapsed)
270 {
271         //this dog bites his tail
272         if (empty()) {
273                 push_back(CursorSlice(*inset_));
274                 return;
275         }
276
277         InsetBase * const nextinset = nextInset();
278         // jump over collapsables if they are collapsed
279         // FIXME: the check for asInsetMath() shouldn't be necessary
280         // but math insets do not return a sensible editable() state yet.
281         if (ignorecollapsed && nextinset && (!nextinset->asInsetMath()
282             && nextinset->editable() != InsetBase::HIGHLY_EDITABLE)) {
283                 ++top().pos();
284                 return;
285         }
286
287         CursorSlice & tip = top();
288         //lyxerr << "XXX\n" << *this << endl;
289
290         // this is used twice and shows up in the profiler!
291         pos_type const lastp = lastpos();
292
293         // move into an inset to the right if possible
294         InsetBase * n = 0;
295
296         if (tip.pos() != lastp) {
297                 // this is impossible for pos() == size()
298                 if (inMathed()) {
299                         n = (tip.cell().begin() + tip.pos())->nucleus();
300                 } else {
301                         if (paragraph().isInset(tip.pos()))
302                                 n = paragraph().getInset(tip.pos());
303                 }
304         }
305
306         if (n && n->isActive()) {
307                 //lyxerr << "... descend" << endl;
308                 push_back(CursorSlice(*n));
309                 return;
310         }
311
312         // otherwise move on one position if possible
313         if (tip.pos() < lastp) {
314                 //lyxerr << "... next pos" << endl;
315                 ++tip.pos();
316                 return;
317         }
318         //lyxerr << "... no next pos" << endl;
319
320         // otherwise move on one paragraph if possible
321         if (tip.pit() < lastpit()) {
322                 //lyxerr << "... next par" << endl;
323                 ++tip.pit();
324                 tip.pos() = 0;
325                 return;
326         }
327         //lyxerr << "... no next pit" << endl;
328
329         // otherwise try to move on one cell if possible
330         if (tip.idx() < lastidx()) {
331                 //lyxerr << "... next idx" << endl;
332                 ++tip.idx();
333                 tip.pit() = 0;
334                 tip.pos() = 0;
335                 return;
336         }
337         //lyxerr << "... no next idx" << endl;
338
339         // otherwise leave inset and jump over inset as a whole
340         pop_back();
341         // 'top' is invalid now...
342         if (!empty())
343                 ++top().pos();
344 }
345
346
347 void DocIterator::forwardPosNoDescend()
348 {
349         CursorSlice & tip = top();
350         pos_type const lastp = lastpos();
351
352         //  move on one position if possible
353         if (tip.pos() < lastp) {
354                 //lyxerr << "... next pos" << endl;
355                 ++tip.pos();
356                 return;
357         }
358         //lyxerr << "... no next pos" << endl;
359
360         // otherwise move on one paragraph if possible
361         if (tip.pit() < lastpit()) {
362                 //lyxerr << "... next par" << endl;
363                 ++tip.pit();
364                 tip.pos() = 0;
365                 return;
366         }
367         //lyxerr << "... no next pit" << endl;
368
369         // otherwise try to move on one cell if possible
370         if (tip.idx() < lastidx()) {
371                 //lyxerr << "... next idx" << endl;
372                 ++tip.idx();
373                 tip.pit() = 0;
374                 tip.pos() = 0;
375                 return;
376         }
377         //lyxerr << "... no next idx" << endl;
378
379         // otherwise leave inset and jump over inset as a whole
380         pop_back();
381         // 'top' is invalid now...
382         if (!empty())
383                 ++top().pos();
384 }
385
386
387 void DocIterator::forwardPar()
388 {
389         forwardPos();
390
391         while (!empty() && (!inTexted() || pos() != 0)) {
392                 if (inTexted()) {
393                         pos_type const lastp = lastpos();
394                         Paragraph const & par = paragraph();
395                         pos_type & pos = top().pos();
396                         if (par.insetlist.empty())
397                                 pos = lastp;
398                         else
399                                 while (pos < lastp && !par.isInset(pos))
400                                         ++pos;
401                 }
402                 forwardPos();
403         }
404 }
405
406
407 void DocIterator::forwardChar()
408 {
409         forwardPos();
410         while (!empty() && pos() == lastpos())
411                 forwardPos();
412 }
413
414
415 void DocIterator::forwardInset()
416 {
417         forwardPos();
418
419         while (!empty() && !nextInset()) {
420                 if (inTexted()) {
421                         pos_type const lastp = lastpos();
422                         Paragraph const & par = paragraph();
423                         pos_type & pos = top().pos();
424                         while (pos < lastp && !par.isInset(pos))
425                                 ++pos;
426                         if (pos < lastp)
427                                 break;
428                 }
429                 forwardPos();
430         }
431 }
432
433
434 void DocIterator::backwardChar()
435 {
436         backwardPos();
437         while (!empty() && pos() == lastpos())
438                 backwardPos();
439 }
440
441
442 void DocIterator::backwardPos()
443 {
444         //this dog bites his tail
445         if (empty()) {
446                 push_back(CursorSlice(*inset_));
447                 top().idx() = lastidx();
448                 top().pit() = lastpit();
449                 top().pos() = lastpos();
450                 return;
451         }
452
453         CursorSlice & tip = top();
454
455         if (tip.pos() != 0) {
456                 --tip.pos();
457         } else if (tip.pit() != 0) {
458                 --tip.pit();
459                 tip.pos() = lastpos();
460                 return;
461         } else if (tip.idx() != 0) {
462                 --tip.idx();
463                 tip.pit() = lastpit();
464                 tip.pos() = lastpos();
465                 return;
466         } else {
467                 pop_back();
468                 return;
469         }
470
471         // move into an inset to the left if possible
472         InsetBase * n = 0;
473
474         if (inMathed()) {
475                 n = (tip.cell().begin() + tip.pos())->nucleus();
476         } else {
477                 if (paragraph().isInset(tip.pos()))
478                         n = paragraph().getInset(tip.pos());
479         }
480
481         if (n && n->isActive()) {
482                 push_back(CursorSlice(*n));
483                 top().idx() = lastidx();
484                 top().pit() = lastpit();
485                 top().pos() = lastpos();
486         }
487 }
488
489
490 bool DocIterator::hasPart(DocIterator const & it) const
491 {
492         // it can't be a part if it is larger
493         if (it.depth() > depth())
494                 return false;
495
496         // as inset adresses are the 'last' level
497         return &it.top().inset() == &slices_[it.depth() - 1].inset();
498 }
499
500
501 void DocIterator::updateInsets(InsetBase * inset)
502 {
503         // this function re-creates the cache of inset pointers.
504         // code taken in part from StableDocIterator::asDocIterator.
505         //lyxerr << "converting:\n" << *this << endl;
506         DocIterator dit = DocIterator(*inset);
507         size_t const n = slices_.size();
508         for (size_t i = 0 ; i < n; ++i) {
509                 BOOST_ASSERT(inset);
510                 dit.push_back(slices_[i]);
511                 dit.top().inset_ = inset;
512                 if (i + 1 != n)
513                         inset = dit.nextInset();
514         }
515         //lyxerr << "converted:\n" << *this << endl;
516         operator=(dit);
517 }
518
519
520 std::ostream & operator<<(std::ostream & os, DocIterator const & dit)
521 {
522         for (size_t i = 0, n = dit.depth(); i != n; ++i)
523                 os << " " << dit[i] << "\n";
524         return os;
525 }
526
527
528
529 ///////////////////////////////////////////////////////
530
531 StableDocIterator::StableDocIterator(DocIterator const & dit)
532 {
533         data_ = dit.internalData();
534         for (size_t i = 0, n = data_.size(); i != n; ++i)
535                 data_[i].inset_ = 0;
536 }
537
538
539 DocIterator StableDocIterator::asDocIterator(InsetBase * inset) const
540 {
541         // this function re-creates the cache of inset pointers
542         //lyxerr << "converting:\n" << *this << endl;
543         DocIterator dit = DocIterator(*inset);
544         for (size_t i = 0, n = data_.size(); i != n; ++i) {
545                 if (inset == 0) {
546                         // FIXME
547                         lyxerr << BOOST_CURRENT_FUNCTION
548                                << " Should not happen, but does e.g. after C-n C-l C-z S-C-z"
549                                 << '\n' << "dit: " << dit << '\n'
550                                 << " lastpos: " << dit.lastpos() << endl;
551                         //break;
552                         BOOST_ASSERT(false);
553                 }
554                 dit.push_back(data_[i]);
555                 dit.top().inset_ = inset;
556                 if (i + 1 != n)
557                         inset = dit.nextInset();
558         }
559         //lyxerr << "convert:\n" << *this << " to:\n" << dit << endl;
560         return dit;
561 }
562
563
564 std::ostream & operator<<(std::ostream & os, StableDocIterator const & dit)
565 {
566         for (size_t i = 0, n = dit.data_.size(); i != n; ++i)
567                 os << " " << dit.data_[i] << "\n";
568         return os;
569 }
570
571
572 bool operator==(StableDocIterator const & dit1, StableDocIterator const & dit2)
573 {
574         return dit1.data_ == dit2.data_;
575 }
576
577
578 } // namespace lyx