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"
31 DocIterator doc_iterator_begin(Buffer const * buf, Inset const * inset = 0);
32 DocIterator doc_iterator_end(Buffer const * buf, Inset const * inset = 0);
38 /// type for cell number in inset
39 typedef CursorSlice::idx_type idx_type;
40 /// type for row indices
41 typedef CursorSlice::row_type row_type;
42 /// type for col indices
43 typedef CursorSlice::col_type col_type;
49 explicit DocIterator(Buffer *buf);
51 /// access to owning buffer
52 Buffer * buffer() const { return buffer_; }
53 /// access to owning buffer
54 void setBuffer(Buffer * buf) { buffer_ = buf; }
56 /// Clone this for given \p buffer.
57 /// \p buffer must be a clone of buffer_.
58 DocIterator clone(Buffer * buffer) const;
60 /// access slice at position \p i
61 CursorSlice const & operator[](size_t i) const { return slices_[i]; }
62 /// access slice at position \p i
63 CursorSlice & operator[](size_t i) { return slices_[i]; }
64 /// chop a few slices from the iterator
65 void resize(size_t i) { slices_.resize(i); }
67 /// is the iterator valid?
68 explicit operator bool() 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(); }
75 /// checks the cursor slices for disabled spell checker insets
76 bool allowSpellCheck() const;
79 // access to slice at tip
82 CursorSlice & top() { return slices_.back(); }
84 CursorSlice const & top() const { return slices_.back(); }
85 /// access to outermost slice
86 CursorSlice & bottom() { return slices_.front(); }
87 /// access to outermost slice
88 CursorSlice const & bottom() const { return slices_.front(); }
89 /// how many nested insets do we have?
90 size_t depth() const { return slices_.size(); }
91 /// the containing inset
92 Inset & inset() const { return top().inset(); }
93 /// return the cell of the inset this cursor is in
94 idx_type idx() const { return top().idx(); }
95 /// return the cell of the inset this cursor is in
96 idx_type & idx() { return top().idx(); }
97 /// return the last possible cell in this inset
98 idx_type lastidx() const;
99 /// return the paragraph this cursor is in
100 pit_type pit() const { return top().pit(); }
101 /// return the paragraph this cursor is in
102 pit_type & pit() { return top().pit(); }
103 /// return the last possible paragraph in this inset
104 pit_type lastpit() const;
105 /// return the position within the paragraph
106 pos_type pos() const { return top().pos(); }
107 /// return the position within the paragraph
108 pos_type & pos() { return top().pos(); }
109 /// return the last position within the paragraph
110 pos_type lastpos() const;
112 /// return the number of embedded cells
113 size_t nargs() const;
114 /// return the number of embedded cells
115 size_t ncols() const;
116 /// return the number of embedded cells
117 size_t nrows() const;
118 /// return the grid row of the top cell
119 row_type row() const;
120 /// return the last row of the top grid
121 row_type lastrow() const { return nrows() - 1; }
122 /// return the grid column of the top cell
123 col_type col() const;
124 /// return the last column of the top grid
125 col_type lastcol() const { return ncols() - 1; }
126 /// the inset just behind the cursor
127 /// returns 0 if there is no inset (e.g. normal text)
128 Inset * nextInset() const;
129 /// the inset just in front of the cursor
130 Inset * prevInset() const;
132 bool boundary() const { return boundary_; }
134 void boundary(bool b) { boundary_ = b; }
136 // the two methods below have been inlined out because of
137 // profiling results under linux when opening a document.
138 /// are we in mathed?.
139 bool inMathed() const
140 { return !empty() && inset().inMathed(); }
141 /// are we in texted?.
142 bool inTexted() const
143 { return !empty() && !inset().inMathed(); }
144 /// are we in regexp-mode ?
145 bool inRegexped() const;
148 // math-specific part
150 /// return the mathed cell this cursor is in
151 MathData & cell() const;
153 InsetMath & nextMath();
155 InsetMath & prevMath();
156 /// the mathatom left of the cursor
157 MathAtom & prevAtom() const;
158 /// the mathatom right of the cursor
159 MathAtom & nextAtom() const;
161 // text-specific part
163 /// the paragraph we're in in text mode.
164 /// \warning only works within text!
165 Paragraph & paragraph() const;
166 /// the paragraph we're in in any case.
167 /// This method will give the containing paragraph even
168 /// if not in text mode (ex: in mathed).
169 Paragraph & innerParagraph() const;
170 /// return the inner text slice.
171 CursorSlice const & innerTextSlice() const;
172 // convert a DocIterator into an argument to LFUN_PARAGRAPH_GOTO
173 docstring paragraphGotoArgument() const;
174 /// returns a DocIterator for the containing text inset
175 DocIterator getInnerText() const;
176 /// the first and last positions of a word at top cursor slice
177 /// \warning only works within text!
178 FontSpan locateWord(word_location const loc) const;
181 /// the containing inset or the cell, respectively
182 Inset * realInset() const;
184 Inset * innerInsetOfType(int code) const;
186 Text * innerText() const;
191 /// move one step backwards
193 /// move one step forward
196 * move on one logical position, descend into nested insets
197 * including collapsed insets
201 * move on one logical position, descend into nested insets
202 * skip collapsed insets
204 void forwardPosIgnoreCollapsed();
205 /// move on one physical character or inset
207 /// move on one paragraph
209 /// move on to the next closest inset
211 /// move backward one logical position
213 /// move backward one physical character or inset
215 /// move backward one paragraph
217 /// move backward one inset
218 /// not used currently, uncomment if you need it
219 //void backwardInset();
221 /// are we some 'extension' (i.e. deeper nested) of the given iterator
222 bool hasPart(DocIterator const & it) const;
225 friend std::ostream &
226 operator<<(std::ostream & os, DocIterator const & cur);
227 friend LyXErr & operator<<(LyXErr & os, DocIterator const & it);
229 friend bool operator==(DocIterator const &, DocIterator const &);
230 friend bool operator<(DocIterator const &, DocIterator const &);
231 friend bool operator>(DocIterator const &, DocIterator const &);
232 friend bool operator<=(DocIterator const &, DocIterator const &);
234 friend class StableDocIterator;
237 void clear() { slices_.clear(); }
239 void push_back(CursorSlice const & sl) { slices_.push_back(sl); }
241 void pop_back() { slices_.pop_back(); }
242 /// recompute the inset parts of the cursor from the document data
243 void updateInsets(Inset * inset);
244 /// fix DocIterator in circumstances that should never happen.
245 /// \return true if the DocIterator was fixed.
247 /// Repopulate the slices insets from bottom to top. Useful
248 /// for stable iterators or Undo data.
251 bool isInside(Inset const *) const;
252 /// make sure we are outside of given inset
253 void leaveInset(Inset const & inset);
255 /// find index of CursorSlice with &cell() == &cell (or -1 if not found)
256 int find(MathData const & cell) const;
257 /// find index of CursorSlice with inset() == inset (or -1 of not found)
258 int find(Inset const * inset) const;
259 /// cut off CursorSlices with index > above and store cut off slices in cut.
260 void cutOff(int above, std::vector<CursorSlice> & cut);
261 /// cut off CursorSlices with index > above
262 void cutOff(int above);
263 /// push CursorSlices on top
264 void append(std::vector<CursorSlice> const & x);
265 /// push one CursorSlice on top and set its index and position
266 void append(idx_type idx, pos_type pos);
269 docstring getPossibleLabel() const;
272 Encoding const * getEncoding() const;
274 friend class InsetIterator;
275 friend DocIterator doc_iterator_begin(Buffer const * buf, Inset const * inset);
276 friend DocIterator doc_iterator_end(Buffer const * buf, Inset const * inset);
278 explicit DocIterator(Buffer * buf, Inset * inset);
280 * Normally, when the cursor is at position i, it is painted *before*
281 * the character at position i. However, what if we want the cursor
282 * painted *after* position i? That's what boundary_ is for: if
283 * boundary_==true, the cursor is painted *after* position i-1, instead
284 * of before position i.
286 * Note 1: Usually, after i-1 or before i are actually the same place!
287 * However, this is not the case when i-1 and i are not painted
288 * contiguously, and in these cases we sometimes do want to have control
289 * over whether to paint before i or after i-1.
290 * Some concrete examples of where this happens:
291 * a. i-1 at the end of one row, i at the beginning of next row
292 * b. in bidi text, at transitions between RTL and LTR or vice versa
294 * Note 2: Why i and i-1? Why, if boundary_==false means: *before* i,
295 * couldn't boundary_==true mean: *after* i?
296 * Well, the reason is this: cursor position is not used only for
297 * painting the cursor, but it also affects other things, for example:
298 * where the next insertion will be placed (it is inserted at the current
299 * position, pushing anything at the current position and beyond forward).
300 * Now, when the current position is i and boundary_==true, insertion would
301 * happen *before* i. If the cursor, however, were painted *after* i, that
302 * would be very unnatural...
306 std::vector<CursorSlice> const & internalData() const { return slices_; }
308 std::vector<CursorSlice> slices_;
316 inline bool operator==(DocIterator const & di1, DocIterator const & di2)
318 return di1.slices_ == di2.slices_;
322 inline bool operator!=(DocIterator const & di1, DocIterator const & di2)
324 return !(di1 == di2);
329 bool operator<(DocIterator const & p, DocIterator const & q)
331 size_t depth = std::min(p.depth(), q.depth());
332 for (size_t i = 0 ; i < depth ; ++i) {
336 return p.depth() < q.depth();
341 bool operator>(DocIterator const & p, DocIterator const & q)
348 bool operator<=(DocIterator const & p, DocIterator const & q)
355 bool operator>=(DocIterator const & p, DocIterator const & q)
361 // The difference to a ('non stable') DocIterator is the removed
362 // (overwritten by 0...) part of the CursorSlice data items. So this thing
363 // is suitable for external storage, but not for iteration as such.
365 class StableDocIterator
369 StableDocIterator() {}
370 /// non-explicit intended
371 StableDocIterator(const DocIterator & it);
373 DocIterator asDocIterator(Buffer * buf) const;
375 size_t size() const { return data_.size(); }
376 /// return the position within the paragraph
377 pos_type pos() const { return data_.back().pos(); }
378 /// return the position within the paragraph
379 pos_type & pos() { return data_.back().pos(); }
381 friend std::ostream &
382 operator<<(std::ostream & os, StableDocIterator const & cur);
384 friend std::istream &
385 operator>>(std::istream & is, StableDocIterator & cur);
388 operator==(StableDocIterator const &, StableDocIterator const &);
390 std::vector<CursorSlice> data_;
395 #endif // DOCITERATOR_H