]> git.lyx.org Git - lyx.git/blob - src/DocIterator.h
fix a crash when the inset containing the new word at cursor is deleted
[lyx.git] / src / DocIterator.h
1 // -*- C++ -*-
2 /**
3  * \file DocIterator.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author André Pönitz
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #ifndef DOCITERATOR_H
13 #define DOCITERATOR_H
14
15 #include "CursorSlice.h"
16
17 #include <vector>
18
19
20 namespace lyx {
21
22 class DocIterator;
23 class LyXErr;
24 class MathAtom;
25 class Paragraph;
26 class Text;
27 class InsetIterator;
28 class FontSpan;
29
30 DocIterator doc_iterator_begin(Buffer const * buf, Inset const * inset = 0);
31 DocIterator doc_iterator_end(Buffer const * buf, Inset const * inset = 0);
32
33
34 class DocIterator
35 {
36 public:
37         /// type for cell number in inset
38         typedef CursorSlice::idx_type idx_type;
39         /// type for row indices
40         typedef CursorSlice::row_type row_type;
41         /// type for col indices
42         typedef CursorSlice::col_type col_type;
43
44 public:
45         ///
46         DocIterator();
47         ///
48         explicit DocIterator(Buffer *buf);
49
50         /// access to owning buffer
51         Buffer * buffer() const { return buffer_; }
52         /// access to owning buffer
53         void setBuffer(Buffer * buf) { buffer_ = buf; }
54
55         /// Clone this for given \p buffer.
56         /// \p buffer must be a clone of buffer_.
57         DocIterator clone(Buffer * buffer) const;
58
59         /// access slice at position \p i
60         CursorSlice const & operator[](size_t i) const { return slices_[i]; }
61         /// access slice at position \p i
62         CursorSlice & operator[](size_t i) { return slices_[i]; }
63         /// chop a few slices from the iterator
64         void resize(size_t i) { slices_.resize(i); }
65
66         /// is the iterator valid?
67         operator const void*() const { return empty() ? 0 : this; }
68         /// is this iterator invalid?
69         bool operator!() const { return empty(); }
70
71         /// does this iterator have any content?
72         bool empty() const { return slices_.empty(); }
73         /// is this the end position?
74         bool atEnd() const { return slices_.empty(); }
75
76         //
77         // access to slice at tip
78         //
79         /// access to tip
80         CursorSlice & top() { return slices_.back(); }
81         /// access to tip
82         CursorSlice const & top() const { return slices_.back(); }
83         /// access to outermost slice
84         CursorSlice & bottom() { return slices_.front(); }
85         /// access to outermost slice
86         CursorSlice const & bottom() const { return slices_.front(); }
87         /// how many nested insets do we have?
88         size_t depth() const { return slices_.size(); }
89         /// the containing inset
90         Inset & inset() const { return top().inset(); }
91         /// return the cell of the inset this cursor is in
92         idx_type idx() const { return top().idx(); }
93         /// return the cell of the inset this cursor is in
94         idx_type & idx() { return top().idx(); }
95         /// return the last possible cell in this inset
96         idx_type lastidx() const;
97         /// return the paragraph this cursor is in
98         pit_type pit() const { return top().pit(); }
99         /// return the paragraph this cursor is in
100         pit_type & pit() { return top().pit(); }
101         /// return the last possible paragraph in this inset
102         pit_type lastpit() const;
103         /// return the position within the paragraph
104         pos_type pos() const { return top().pos(); }
105         /// return the position within the paragraph
106         pos_type & pos() { return top().pos(); }
107         /// return the last position within the paragraph
108         pos_type lastpos() const;
109
110         /// return the number of embedded cells
111         size_t nargs() const;
112         /// return the number of embedded cells
113         size_t ncols() const;
114         /// return the number of embedded cells
115         size_t nrows() const;
116         /// return the grid row of the top cell
117         row_type row() const;
118         /// return the last row of the top grid
119         row_type lastrow() const { return nrows() - 1; }
120         /// return the grid column of the top cell
121         col_type col() const;
122         /// return the last column of the top grid
123         col_type lastcol() const { return ncols() - 1; }
124         /// the inset just behind the cursor
125         Inset * nextInset() const;
126         /// the inset just in front of the cursor
127         Inset * prevInset() const;
128         ///
129         bool boundary() const { return boundary_; }
130         ///
131         void boundary(bool b) { boundary_ = b; }
132
133         // the two methods below have been inlined out because of
134         // profiling results under linux when opening a document.
135         /// are we in mathed?.
136         bool inMathed() const
137         { return !empty() && inset().inMathed(); }
138         /// are we in texted?.
139         bool inTexted() const
140         { return !empty() && !inset().inMathed(); }
141         /// are we in regexp-mode ?
142         bool inRegexped() const;
143
144         //
145         // math-specific part
146         //
147         /// return the mathed cell this cursor is in
148         MathData & cell() const;
149         /// the mathatom left of the cursor
150         MathAtom & prevAtom() const;
151         /// the mathatom right of the cursor
152         MathAtom & nextAtom() const;
153
154         // text-specific part
155         //
156         /// the paragraph we're in in text mode.
157         /// \warning only works within text!
158         Paragraph & paragraph() const;
159         /// the paragraph we're in in any case.
160         /// This method will give the containing paragraph even
161         /// if not in text mode (ex: in mathed).
162         Paragraph & innerParagraph() const;
163         /// return the inner text slice.
164         CursorSlice const & innerTextSlice() const;
165         /// the first and last positions of a word at top cursor slice
166         /// \warning only works within text!
167         FontSpan locateWord(word_location const loc) const;
168         ///
169         Text * text() const;
170         /// the containing inset or the cell, respectively
171         Inset * realInset() const;
172         ///
173         Inset * innerInsetOfType(int code) const;
174         ///
175         Text * innerText() const;
176
177         //
178         // elementary moving
179         //
180         /**
181          * move on one logical position, descend into nested insets
182          * including collapsed insets
183          */
184         void forwardPos();
185         /**
186          * move on one logical position, descend into nested insets
187          * skip collapsed insets
188          */
189         void forwardPosIgnoreCollapsed();
190         /// move on one physical character or inset
191         void forwardChar();
192         /// move on one paragraph
193         void forwardPar();
194         /// move on one inset
195         void forwardInset();
196         /// move backward one logical position
197         void backwardPos();
198         /// move backward one physical character or inset
199         void backwardChar();
200         /// move backward one paragraph
201         void backwardPar();
202         /// move backward one inset
203         /// FIXME: This is not implemented!
204         //void backwardInset();
205
206         /// are we some 'extension' (i.e. deeper nested) of the given iterator
207         bool hasPart(DocIterator const & it) const;
208
209         /// output
210         friend std::ostream &
211         operator<<(std::ostream & os, DocIterator const & cur);
212         friend LyXErr & operator<<(LyXErr & os, DocIterator const & it);
213         ///
214         friend bool operator==(DocIterator const &, DocIterator const &);
215         friend bool operator<(DocIterator const &, DocIterator const &);
216         friend bool operator>(DocIterator const &, DocIterator const &);
217         friend bool operator<=(DocIterator const &, DocIterator const &);
218         ///
219         friend class StableDocIterator;
220 //protected:
221         ///
222         void clear() { slices_.clear(); }
223         ///
224         void push_back(CursorSlice const & sl) { slices_.push_back(sl); }
225         ///
226         void pop_back() { slices_.pop_back(); }
227         /// recompute the inset parts of the cursor from the document data
228         void updateInsets(Inset * inset);
229         /// fix DocIterator in circumstances that should never happen.
230         /// \return true if the DocIterator was fixed.
231         bool fixIfBroken();
232
233         /// find index of CursorSlice with &cell() == &cell (or -1 if not found)
234         int find(MathData const & cell) const;
235         /// find index of CursorSlice with inset() == inset (or -1 of not found)
236         int find(Inset const * inset) const;
237         /// cut off CursorSlices with index > above and store cut off slices in cut.
238         void cutOff(int above, std::vector<CursorSlice> & cut);
239         /// cut off CursorSlices with index > above
240         void cutOff(int above);
241         /// push CursorSlices on top
242         void append(std::vector<CursorSlice> const & x);
243         /// push one CursorSlice on top and set its index and position
244         void append(idx_type idx, pos_type pos);
245
246 private:
247         friend class InsetIterator;
248         friend DocIterator doc_iterator_begin(Buffer const * buf, Inset const * inset);
249         friend DocIterator doc_iterator_end(Buffer const * buf, Inset const * inset);
250         ///
251         explicit DocIterator(Buffer * buf, Inset * inset);
252         /**
253          * Normally, when the cursor is at position i, it is painted *before*
254          * the character at position i. However, what if we want the cursor 
255          * painted *after* position i? That's what boundary_ is for: if
256          * boundary_==true, the cursor is painted *after* position i-1, instead
257          * of before position i.
258          *
259          * Note 1: Usually, after i-1 or before i are actually the same place!
260          * However, this is not the case when i-1 and i are not painted 
261          * contiguously, and in these cases we sometimes do want to have control
262          * over whether to paint before i or after i-1.
263          * Some concrete examples of where this happens:
264          * a. i-1 at the end of one row, i at the beginning of next row
265          * b. in bidi text, at transitions between RTL and LTR or vice versa
266          *
267          * Note 2: Why i and i-1? Why, if boundary_==false means: *before* i, 
268          * couldn't boundary_==true mean: *after* i? 
269          * Well, the reason is this: cursor position is not used only for 
270          * painting the cursor, but it also affects other things, for example:
271          * where the next insertion will be placed (it is inserted at the current
272          * position, pushing anything at the current position and beyond forward).
273          * Now, when the current position is i and boundary_==true, insertion would
274          * happen *before* i. If the cursor, however, were painted *after* i, that
275          * would be very unnatural...
276          */
277         bool boundary_;
278         ///
279         std::vector<CursorSlice> const & internalData() const { return slices_; }
280         ///
281         std::vector<CursorSlice> slices_;
282         ///
283         Inset * inset_;
284         ///
285         Buffer * buffer_;
286 };
287
288
289 inline bool operator==(DocIterator const & di1, DocIterator const & di2)
290 {
291         return di1.slices_ == di2.slices_;
292 }
293
294
295 inline bool operator!=(DocIterator const & di1, DocIterator const & di2)
296 {
297         return !(di1 == di2);
298 }
299
300
301 inline
302 bool operator<(DocIterator const & p, DocIterator const & q)
303 {
304         size_t depth = std::min(p.depth(), q.depth());
305         for (size_t i = 0 ; i < depth ; ++i) {
306                 if (p[i] != q[i])
307                         return p[i] < q[i];
308         }
309         return p.depth() < q.depth();
310 }
311
312
313 inline  
314 bool operator>(DocIterator const & p, DocIterator const & q)
315 {
316         return q < p;
317 }
318
319
320 inline  
321 bool operator<=(DocIterator const & p, DocIterator const & q)
322 {
323         return !(q < p);
324 }
325
326
327 inline  
328 bool operator>=(DocIterator const & p, DocIterator const & q)
329 {
330         return !(p < q);
331 }
332
333
334 // The difference to a ('non stable') DocIterator is the removed
335 // (overwritten by 0...) part of the CursorSlice data items. So this thing
336 // is suitable for external storage, but not for iteration as such.
337
338 class StableDocIterator
339 {
340 public:
341         ///
342         StableDocIterator() {}
343         /// non-explicit intended
344         StableDocIterator(const DocIterator & it);
345         ///
346         DocIterator asDocIterator(Buffer * buf) const;
347         ///
348         size_t size() const { return data_.size(); }
349         ///  return the position within the paragraph
350         pos_type pos() const { return data_.back().pos(); }
351         ///  return the position within the paragraph
352         pos_type & pos() { return data_.back().pos(); }
353         ///
354         friend std::ostream &
355         operator<<(std::ostream & os, StableDocIterator const & cur);
356         ///
357         friend std::istream &
358         operator>>(std::istream & is, StableDocIterator & cur);
359         ///
360         friend bool
361         operator==(StableDocIterator const &, StableDocIterator const &);
362 private:
363         std::vector<CursorSlice> data_;
364 };
365
366 } // namespace lyx
367
368 #endif // DOCITERATOR_H