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