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