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