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