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"
18 #include <algorithm> // std::min in MSVC 2017
31 DocIterator doc_iterator_begin(Buffer const * buf, Inset const * inset = 0);
32 DocIterator doc_iterator_end(Buffer const * buf, Inset const * inset = 0);
41 explicit DocIterator(Buffer *buf);
43 /// access to owning buffer
44 Buffer * buffer() const { return buffer_; }
45 /// access to owning buffer
46 void setBuffer(Buffer * buf) { buffer_ = buf; }
48 /// Clone this for given \p buffer.
49 /// \p buffer must be a clone of buffer_.
50 DocIterator clone(Buffer * buffer) const;
52 /// access slice at position \p i
53 CursorSlice const & operator[](size_t i) const { return slices_[i]; }
54 /// access slice at position \p i
55 CursorSlice & operator[](size_t i) { return slices_[i]; }
56 /// chop a few slices from the iterator
57 void resize(size_t i) { slices_.resize(i); }
59 /// is the iterator valid?
60 explicit operator bool() const { return !empty(); }
62 /// does this iterator have any content?
63 bool empty() const { return slices_.empty(); }
64 /// is this the begin position?
65 bool atBegin() const { return depth() == 1 && pit() == 0 && pos() == 0; }
66 /// is this the end position?
67 bool atEnd() const { return slices_.empty(); }
69 /// checks the cursor slices for disabled spell checker insets
70 bool allowSpellCheck() const;
73 // access to slice at tip
76 CursorSlice & top() { return slices_.back(); }
78 CursorSlice const & top() const { return slices_.back(); }
79 /// access to outermost slice
80 CursorSlice & bottom() { return slices_.front(); }
81 /// access to outermost slice
82 CursorSlice const & bottom() const { return slices_.front(); }
83 /// how many nested insets do we have?
84 size_t depth() const { return slices_.size(); }
85 /// the containing inset
86 Inset & inset() const { return top().inset(); }
87 /// return the cell of the inset this cursor is in
88 idx_type idx() const { return top().idx(); }
89 /// return the cell of the inset this cursor is in
90 idx_type & idx() { return top().idx(); }
91 /// return the last possible cell in this inset
92 idx_type lastidx() const;
93 /// return the paragraph this cursor is in
94 pit_type pit() const { return top().pit(); }
95 /// return the paragraph this cursor is in
96 pit_type & pit() { return top().pit(); }
97 /// return the last possible paragraph in this inset
98 pit_type lastpit() const;
99 /// return the position within the paragraph
100 pos_type pos() const { return top().pos(); }
101 /// return the position within the paragraph
102 pos_type & pos() { return top().pos(); }
103 /// return the last position within the paragraph
104 pos_type lastpos() const;
106 /// return the number of embedded cells
107 size_t nargs() const;
108 /// return the number of embedded cells
109 size_t ncols() const;
110 /// return the number of embedded cells
111 size_t nrows() const;
112 /// return the grid row of the top cell
113 row_type row() const;
114 /// return the last row of the top grid
115 row_type lastrow() const { return nrows() - 1; }
116 /// return the grid column of the top cell
117 col_type col() const;
118 /// return the last column of the top grid
119 col_type lastcol() const { return ncols() - 1; }
120 /// the inset just behind the cursor
121 /// returns 0 if there is no inset (e.g. normal text)
122 Inset * nextInset() const;
123 /// the inset just in front of the cursor
124 Inset * prevInset() const;
126 bool boundary() const { return boundary_; }
128 void boundary(bool b) { boundary_ = b; }
130 // the two methods below have been inlined out because of
131 // profiling results under linux when opening a document.
132 /// are we in mathed?.
133 bool inMathed() const
134 { return !empty() && inset().inMathed(); }
135 /// are we in texted?.
136 bool inTexted() const
137 { return !empty() && !inset().inMathed(); }
138 /// are we in regexp-mode ?
139 bool inRegexped() const;
142 // math-specific part
144 /// return the mathed cell this cursor is in
145 MathData & cell() const;
147 InsetMath & nextMath();
149 InsetMath & prevMath();
150 /// the mathatom left of the cursor
151 MathAtom & prevAtom() const;
152 /// the mathatom right of the cursor
153 MathAtom & nextAtom() const;
155 // text-specific part
157 /// the paragraph we're in in text mode.
158 /// \warning only works within text!
159 Paragraph & paragraph() const;
160 /// the paragraph we're in in any case.
161 /// This method will give the containing paragraph even
162 /// if not in text mode (ex: in mathed).
163 Paragraph & innerParagraph() const;
164 /// return the inner text slice.
165 CursorSlice const & innerTextSlice() const;
166 // convert a DocIterator into an argument to LFUN_PARAGRAPH_GOTO
167 docstring paragraphGotoArgument() const;
168 /// returns a DocIterator for the containing text inset
169 DocIterator getInnerText() 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;
175 /// the containing inset or the cell, respectively
176 Inset * realInset() const;
178 Inset * innerInsetOfType(int code) const;
180 Text * innerText() const;
185 /// move one step backwards
187 /// move one step forward
190 * move on one logical position, descend into nested insets
191 * including collapsed insets
195 * move on one logical position, descend into nested insets
196 * skip collapsed insets
198 void forwardPosIgnoreCollapsed();
199 /// move on one physical character or inset
201 /// move on one paragraph
203 /// move on to the next closest inset
205 /// move backward one logical position
207 /// move backward one logical position, skip collapsed insets
208 void backwardPosIgnoreCollapsed();
209 /// move backward one physical character or inset
211 /// move backward one paragraph
213 /// move backward one inset
214 /// not used currently, uncomment if you need it
215 //void backwardInset();
217 /// are we some 'extension' (i.e. deeper nested) of the given iterator
218 bool hasPart(DocIterator const & it) const;
221 friend std::ostream &
222 operator<<(std::ostream & os, DocIterator const & cur);
223 friend LyXErr & operator<<(LyXErr & os, DocIterator const & it);
225 friend bool operator==(DocIterator const &, DocIterator const &);
226 friend bool operator<(DocIterator const &, DocIterator const &);
227 friend bool operator>(DocIterator const &, DocIterator const &);
228 friend bool operator<=(DocIterator const &, DocIterator const &);
230 friend class StableDocIterator;
233 void clear() { slices_.clear(); }
235 void push_back(CursorSlice const & sl) { slices_.push_back(sl); }
237 void pop_back() { slices_.pop_back(); }
238 /// recompute the inset parts of the cursor from the document data
239 void updateInsets(Inset * inset);
240 /// fix DocIterator in circumstances that should never happen.
241 /// \return true if the DocIterator was fixed.
243 /// Repopulate the slices insets from bottom to top. Useful
244 /// for stable iterators or Undo data.
247 bool isInside(Inset const *) const;
248 /// make sure we are outside of given inset
249 void leaveInset(Inset const & inset);
251 /// find index of CursorSlice with &cell() == &cell (or -1 if not found)
252 int find(MathData const & cell) const;
253 /// find index of CursorSlice with inset() == inset (or -1 of not found)
254 int find(Inset const * inset) const;
255 /// cut off CursorSlices with index > above and store cut off slices in cut.
256 void cutOff(int above, std::vector<CursorSlice> & cut);
257 /// cut off CursorSlices with index > above
258 void cutOff(int above);
259 /// push CursorSlices on top
260 void append(std::vector<CursorSlice> const & x);
261 /// push one CursorSlice on top and set its index and position
262 void append(idx_type idx, pos_type pos);
265 docstring getPossibleLabel() const;
268 Encoding const * getEncoding() const;
270 friend class InsetIterator;
271 friend DocIterator doc_iterator_begin(Buffer const * buf, Inset const * inset);
272 friend DocIterator doc_iterator_end(Buffer const * buf, Inset const * inset);
274 explicit DocIterator(Buffer * buf, Inset * inset);
276 * Normally, when the cursor is at position i, it is painted *before*
277 * the character at position i. However, what if we want the cursor
278 * painted *after* position i? That's what boundary_ is for: if
279 * boundary_==true, the cursor is painted *after* position i-1, instead
280 * of before position i.
282 * Note 1: Usually, after i-1 or before i are actually the same place!
283 * However, this is not the case when i-1 and i are not painted
284 * contiguously, and in these cases we sometimes do want to have control
285 * over whether to paint before i or after i-1.
286 * Some concrete examples of where this happens:
287 * a. i-1 at the end of one row, i at the beginning of next row
288 * b. in bidi text, at transitions between RTL and LTR or vice versa
290 * Note 2: Why i and i-1? Why, if boundary_==false means: *before* i,
291 * couldn't boundary_==true mean: *after* i?
292 * Well, the reason is this: cursor position is not used only for
293 * painting the cursor, but it also affects other things, for example:
294 * where the next insertion will be placed (it is inserted at the current
295 * position, pushing anything at the current position and beyond forward).
296 * Now, when the current position is i and boundary_==true, insertion would
297 * happen *before* i. If the cursor, however, were painted *after* i, that
298 * would be very unnatural...
302 std::vector<CursorSlice> const & internalData() const { return slices_; }
304 std::vector<CursorSlice> slices_;
312 inline bool operator==(DocIterator const & di1, DocIterator const & di2)
314 return di1.slices_ == di2.slices_;
318 inline bool operator!=(DocIterator const & di1, DocIterator const & di2)
320 return !(di1 == di2);
325 bool operator<(DocIterator const & p, DocIterator const & q)
327 size_t depth = std::min(p.depth(), q.depth());
328 for (size_t i = 0 ; i < depth ; ++i) {
332 return p.depth() < q.depth();
337 bool operator>(DocIterator const & p, DocIterator const & q)
344 bool operator<=(DocIterator const & p, DocIterator const & q)
351 bool operator>=(DocIterator const & p, DocIterator const & q)
357 // The difference to a ('non stable') DocIterator is the removed
358 // (overwritten by 0...) part of the CursorSlice data items. So this thing
359 // is suitable for external storage, but not for iteration as such.
361 class StableDocIterator
365 StableDocIterator() {}
366 /// non-explicit intended
367 StableDocIterator(const DocIterator & it);
369 DocIterator asDocIterator(Buffer * buf) const;
371 size_t size() const { return data_.size(); }
372 /// return the position within the paragraph
373 pos_type pos() const { return data_.back().pos(); }
374 /// return the position within the paragraph
375 pos_type & pos() { return data_.back().pos(); }
377 friend std::ostream &
378 operator<<(std::ostream & os, StableDocIterator const & cur);
380 friend std::istream &
381 operator>>(std::istream & is, StableDocIterator & cur);
384 operator==(StableDocIterator const &, StableDocIterator const &);
386 std::vector<CursorSlice> data_;
391 #endif // DOCITERATOR_H