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