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