]> git.lyx.org Git - lyx.git/blob - src/DocIterator.h
Amend 3093789e for cmake build
[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> // std::min in MSVC 2017
19
20 namespace lyx {
21
22 class DocIterator;
23 class Encoding;
24 class FontSpan;
25 class InsetIterator;
26 class LyXErr;
27 class MathAtom;
28 class Paragraph;
29 class Text;
30
31 DocIterator doc_iterator_begin(Buffer const * buf, Inset const * inset = 0);
32 DocIterator doc_iterator_end(Buffer const * buf, Inset const * inset = 0);
33
34
35 class DocIterator
36 {
37 public:
38         ///
39         DocIterator();
40         ///
41         explicit DocIterator(Buffer *buf);
42
43         /// access to owning buffer
44         Buffer * buffer() const { return buffer_; }
45         /// access to owning buffer
46         void setBuffer(Buffer * buf) { buffer_ = buf; }
47
48         /// Clone this for given \p buffer.
49         /// \p buffer must be a clone of buffer_.
50         DocIterator clone(Buffer * buffer) const;
51
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); }
58
59         /// is the iterator valid?
60         explicit operator bool() const { return !empty(); }
61
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(); }
68
69         /// checks the cursor slices for disabled spell checker insets
70         bool allowSpellCheck() const;
71
72         //
73         // access to slice at tip
74         //
75         /// access to tip
76         CursorSlice & top() { return slices_.back(); }
77         /// access to tip
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;
105
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;
125         ///
126         bool boundary() const { return boundary_; }
127         ///
128         void boundary(bool b) { boundary_ = b; }
129
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;
140
141         //
142         // math-specific part
143         //
144         /// return the mathed cell this cursor is in
145         MathData & cell() const;
146         ///
147         InsetMath & nextMath();
148         ///
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;
154
155         // text-specific part
156         //
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;
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         /// move one step backwards
186         bool posBackward();
187         /// move one step forward
188         bool posForward();
189         /**
190          * move on one logical position, descend into nested insets
191          * including collapsed insets
192          */
193         void forwardPos();
194         /**
195          * move on one logical position, descend into nested insets
196          * skip collapsed insets
197          */
198         void forwardPosIgnoreCollapsed();
199         /// move on one physical character or inset
200         void forwardChar();
201         /// move on one paragraph
202         void forwardPar();
203         /// move on to the next closest inset
204         void forwardInset();
205         /// move backward one logical position
206         void backwardPos();
207         /// move backward one logical position, skip collapsed insets
208         void backwardPosIgnoreCollapsed();
209         /// move backward one physical character or inset
210         void backwardChar();
211         /// move backward one paragraph
212         void backwardPar();
213         /// move backward one inset
214         /// not used currently, uncomment if you need it
215         //void backwardInset();
216
217         /// are we some 'extension' (i.e. deeper nested) of the given iterator
218         bool hasPart(DocIterator const & it) const;
219
220         /// output
221         friend std::ostream &
222         operator<<(std::ostream & os, DocIterator const & cur);
223         friend LyXErr & operator<<(LyXErr & os, DocIterator const & it);
224         ///
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 &);
229         ///
230         friend class StableDocIterator;
231 //protected:
232         ///
233         void clear() { slices_.clear(); }
234         ///
235         void push_back(CursorSlice const & sl) { slices_.push_back(sl); }
236         ///
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.
242         bool fixIfBroken();
243         /// Repopulate the slices insets from bottom to top. Useful
244         /// for stable iterators or Undo data.
245         void sanitize();
246         ///
247         bool isInside(Inset const *) const;
248         /// make sure we are outside of given inset
249         void leaveInset(Inset const & inset);
250
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);
263
264         ///
265         docstring getPossibleLabel() const;
266
267         ///
268         Encoding const * getEncoding() const;
269 private:
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);
273         ///
274         explicit DocIterator(Buffer * buf, Inset * inset);
275         /**
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.
281          *
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
289          *
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...
299          */
300         bool boundary_;
301         ///
302         std::vector<CursorSlice> const & internalData() const { return slices_; }
303         ///
304         std::vector<CursorSlice> slices_;
305         ///
306         Inset * inset_;
307         ///
308         Buffer * buffer_;
309 };
310
311
312 inline bool operator==(DocIterator const & di1, DocIterator const & di2)
313 {
314         return di1.slices_ == di2.slices_;
315 }
316
317
318 inline bool operator!=(DocIterator const & di1, DocIterator const & di2)
319 {
320         return !(di1 == di2);
321 }
322
323
324 inline
325 bool operator<(DocIterator const & p, DocIterator const & q)
326 {
327         size_t depth = std::min(p.depth(), q.depth());
328         for (size_t i = 0 ; i < depth ; ++i) {
329                 if (p[i] != q[i])
330                         return p[i] < q[i];
331         }
332         return p.depth() < q.depth();
333 }
334
335
336 inline
337 bool operator>(DocIterator const & p, DocIterator const & q)
338 {
339         return q < p;
340 }
341
342
343 inline
344 bool operator<=(DocIterator const & p, DocIterator const & q)
345 {
346         return !(q < p);
347 }
348
349
350 inline
351 bool operator>=(DocIterator const & p, DocIterator const & q)
352 {
353         return !(p < q);
354 }
355
356
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.
360
361 class StableDocIterator
362 {
363 public:
364         ///
365         StableDocIterator() {}
366         /// non-explicit intended
367         StableDocIterator(const DocIterator & it);
368         ///
369         DocIterator asDocIterator(Buffer * buf) const;
370         ///
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(); }
376         ///
377         friend std::ostream &
378         operator<<(std::ostream & os, StableDocIterator const & cur);
379         ///
380         friend std::istream &
381         operator>>(std::istream & is, StableDocIterator & cur);
382         ///
383         friend bool
384         operator==(StableDocIterator const &, StableDocIterator const &);
385 private:
386         std::vector<CursorSlice> data_;
387 };
388
389 } // namespace lyx
390
391 #endif // DOCITERATOR_H