4 * This file is part of LyX, the document processor.
5 * Licence details can be found in the file COPYING.
9 * Full author contact details are available in file CREDITS.
15 #include "CursorSlice.h"
29 DocIterator doc_iterator_begin(Buffer const * buf, Inset const * inset = 0);
30 DocIterator doc_iterator_end(Buffer const * buf, Inset const * inset = 0);
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;
47 explicit DocIterator(Buffer *buf);
49 /// access to owning buffer
50 Buffer * buffer() const { return buffer_; }
51 /// access to owning buffer
52 void setBuffer(Buffer * buf) { buffer_ = buf; }
54 /// Clone this for given \p buffer.
55 /// \p buffer must be a clone of buffer_.
56 DocIterator clone(Buffer * buffer) const;
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); }
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(); }
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(); }
76 // access to slice at tip
79 CursorSlice & top() { return slices_.back(); }
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;
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;
128 bool boundary() const { return boundary_; }
130 void boundary(bool b) { boundary_ = b; }
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;
144 // math-specific part
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;
153 // text-specific part
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;
166 /// the containing inset or the cell, respectively
167 Inset * realInset() const;
169 Inset * innerInsetOfType(int code) const;
171 Text * innerText() const;
177 * move on one logical position, descend into nested insets
178 * including collapsed insets
182 * move on one logical position, descend into nested insets
183 * skip collapsed insets
185 void forwardPosIgnoreCollapsed();
186 /// move on one physical character or inset
188 /// move on one paragraph
190 /// move on one inset
192 /// move backward one logical position
194 /// move backward one physical character or inset
196 /// move backward one paragraph
198 /// move backward one inset
199 /// FIXME: This is not implemented!
200 //void backwardInset();
202 /// are we some 'extension' (i.e. deeper nested) of the given iterator
203 bool hasPart(DocIterator const & it) const;
206 friend std::ostream &
207 operator<<(std::ostream & os, DocIterator const & cur);
208 friend LyXErr & operator<<(LyXErr & os, DocIterator const & it);
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 &);
215 friend class StableDocIterator;
218 void clear() { slices_.clear(); }
220 void push_back(CursorSlice const & sl) { slices_.push_back(sl); }
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.
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);
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);
247 explicit DocIterator(Buffer * buf, Inset * inset);
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.
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
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...
275 std::vector<CursorSlice> const & internalData() const { return slices_; }
277 std::vector<CursorSlice> slices_;
285 inline bool operator==(DocIterator const & di1, DocIterator const & di2)
287 return di1.slices_ == di2.slices_;
291 inline bool operator!=(DocIterator const & di1, DocIterator const & di2)
293 return !(di1 == di2);
298 bool operator<(DocIterator const & p, DocIterator const & q)
300 size_t depth = std::min(p.depth(), q.depth());
301 for (size_t i = 0 ; i < depth ; ++i) {
305 return p.depth() < q.depth();
310 bool operator>(DocIterator const & p, DocIterator const & q)
317 bool operator<=(DocIterator const & p, DocIterator const & q)
324 bool operator>=(DocIterator const & p, DocIterator const & q)
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.
334 class StableDocIterator
338 StableDocIterator() {}
339 /// non-explicit intended
340 StableDocIterator(const DocIterator & it);
342 DocIterator asDocIterator(Buffer * buf) const;
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(); }
350 friend std::ostream &
351 operator<<(std::ostream & os, StableDocIterator const & cur);
353 friend std::istream &
354 operator>>(std::istream & is, StableDocIterator & cur);
357 operator==(StableDocIterator const &, StableDocIterator const &);
359 std::vector<CursorSlice> data_;
364 #endif // DOCITERATOR_H