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