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