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