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