]> git.lyx.org Git - lyx.git/blob - src/DocIterator.h
Avoid full metrics computation with Update:FitCursor
[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 = nullptr);
32 DocIterator doc_iterator_end(Buffer const * buf, Inset const * inset = nullptr);
33
34
35 class DocIterator
36 {
37 public:
38         ///
39         DocIterator() = default;
40
41         // We could be able to get rid of this if only every BufferView were
42         // associated to a buffer on construction.
43         explicit DocIterator(Buffer *buf)
44                 : buffer_(buf)
45         {}
46
47         /// access to owning buffer
48         Buffer * buffer() const { return buffer_; }
49         /// access to owning buffer
50         void setBuffer(Buffer * buf) { buffer_ = buf; }
51
52         /// Clone this for given \p buffer.
53         /// \p buffer must be a clone of buffer_.
54         DocIterator clone(Buffer * buffer) const;
55
56         /// access slice at position \p i
57         CursorSlice const & operator[](size_t i) const { return slices_[i]; }
58         /// access slice at position \p i
59         CursorSlice & operator[](size_t i) { return slices_[i]; }
60         /// chop a few slices from the iterator
61         void resize(size_t i) { slices_.resize(i); }
62
63         /// is the iterator valid?
64         explicit operator bool() const { return !empty(); }
65
66         /// does this iterator have any content?
67         bool empty() const { return slices_.empty(); }
68         /// is this the begin position?
69         bool atBegin() const { return depth() == 1 && pit() == 0 && pos() == 0; }
70         /// is this the end position?
71         bool atEnd() const { return slices_.empty(); }
72
73         /// checks the cursor slices for disabled spell checker insets
74         bool allowSpellCheck() const;
75
76         //
77         // access to slice at tip
78         //
79         /// access to tip
80         CursorSlice & top() { return slices_.back(); }
81         /// access to tip
82         CursorSlice const & top() const { return slices_.back(); }
83         /// access to outermost slice
84         CursorSlice & bottom() { return slices_.front(); }
85         /// access to outermost slice
86         CursorSlice const & bottom() const { return slices_.front(); }
87         /// how many nested insets do we have?
88         size_t depth() const { return slices_.size(); }
89         /// the containing inset
90         Inset & inset() const { return top().inset(); }
91         /// return the cell of the inset this cursor is in
92         idx_type idx() const { return top().idx(); }
93         /// return the cell of the inset this cursor is in
94         idx_type & idx() { return top().idx(); }
95         /// return the last possible cell in this inset
96         idx_type lastidx() const;
97         /// return the paragraph this cursor is in
98         pit_type pit() const { return top().pit(); }
99         /// return the paragraph this cursor is in
100         pit_type & pit() { return top().pit(); }
101         /// return the last possible paragraph in this inset
102         pit_type lastpit() const;
103         /// return the position within the paragraph
104         pos_type pos() const { return top().pos(); }
105         /// return the position within the paragraph
106         pos_type & pos() { return top().pos(); }
107         /// return the last position within the paragraph
108         pos_type lastpos() const;
109
110         /// return the number of embedded cells
111         size_t nargs() const;
112         /// return the number of embedded cells
113         size_t ncols() const;
114         /// return the number of embedded cells
115         size_t nrows() const;
116         /// return the grid row of the top cell
117         row_type row() const;
118         /// return the last row of the top grid
119         row_type lastrow() const { return nrows() - 1; }
120         /// return the grid column of the top cell
121         col_type col() const;
122         /// return the last column of the top grid
123         col_type lastcol() const { return ncols() - 1; }
124         /// the inset just behind the cursor
125         /// returns 0 if there is no inset (e.g. normal text)
126         Inset * nextInset() const;
127         /// the inset just in front of the cursor
128         Inset * prevInset() const;
129         ///
130         bool boundary() const { return boundary_; }
131         ///
132         void boundary(bool b) { boundary_ = b; }
133
134         // the two methods below have been inlined out because of
135         // profiling results under linux when opening a document.
136         /// are we in mathed?.
137         bool inMathed() const
138         { return !empty() && inset().inMathed(); }
139         /// are we in texted?.
140         bool inTexted() const
141         { return !empty() && !inset().inMathed(); }
142         /// are we in regexp-mode ?
143         bool inRegexped() const;
144
145         //
146         // math-specific part
147         //
148         /// return the mathed cell this cursor is in
149         MathData & cell() const;
150         ///
151         InsetMath & nextMath();
152         ///
153         InsetMath & prevMath();
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         // convert a DocIterator into an argument to LFUN_PARAGRAPH_GOTO
171         docstring paragraphGotoArgument() const;
172         /// returns a DocIterator for the containing text inset
173         DocIterator getInnerText() const;
174         /// the first and last positions of a word at top cursor slice
175         /// \warning only works within text!
176         FontSpan locateWord(word_location const loc) const;
177         ///
178         Text * text() const;
179         /// the containing inset or the cell, respectively
180         Inset * realInset() const;
181         ///
182         Inset * innerInsetOfType(int code) const;
183         ///
184         Text * innerText() const;
185
186         //
187         // elementary moving
188         //
189         /// move one step backwards
190         bool posBackward();
191         /// move one step forward
192         bool posForward();
193         /**
194          * move on one logical position, descend into nested insets
195          * including collapsed insets
196          */
197         void forwardPos();
198         /**
199          * move on one logical position, descend into nested insets
200          * skip collapsed insets
201          */
202         void forwardPosIgnoreCollapsed();
203         /// move on one physical character or inset
204         void forwardChar();
205         /// move on one paragraph
206         void forwardPar();
207         /// move on to the next closest inset
208         void forwardInset();
209         /// move backward one logical position
210         void backwardPos();
211         /// move backward one logical position, skip collapsed insets
212         void backwardPosIgnoreCollapsed();
213         /// move backward one physical character or inset
214         void backwardChar();
215         /// move backward one paragraph
216         void backwardPar();
217         /// move backward one inset
218         void backwardInset();
219
220         /// are we some 'extension' (i.e. deeper nested) of the given iterator
221         bool hasPart(DocIterator const & it) const;
222
223         /// output
224         friend std::ostream &
225         operator<<(std::ostream & os, DocIterator const & cur);
226         friend LyXErr & operator<<(LyXErr & os, DocIterator const & it);
227         ///
228         friend bool operator==(DocIterator const &, DocIterator const &);
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         ///
233         friend class StableDocIterator;
234 //protected:
235         ///
236         void clear() { slices_.clear(); }
237         ///
238         void push_back(CursorSlice const & sl) { slices_.push_back(sl); }
239         ///
240         void pop_back() { slices_.pop_back(); }
241         /// recompute the inset parts of the cursor from the document data
242         void updateInsets(Inset * inset);
243         /// fix DocIterator in circumstances that should never happen.
244         /// \return true if the DocIterator was fixed.
245         bool fixIfBroken();
246         /// Repopulate the slices insets from bottom to top. Useful
247         /// for stable iterators or Undo data.
248         void sanitize();
249         ///
250         bool isInside(Inset const *) const;
251         /// make sure we are outside of given inset
252         void leaveInset(Inset const & inset);
253
254         /// find index of CursorSlice with &cell() == &cell (or -1 if not found)
255         int find(MathData const & cell) const;
256         /// find index of CursorSlice with inset() == inset (or -1 of not found)
257         int find(Inset const * inset) const;
258         /// cut off CursorSlices with index > above and store cut off slices in cut.
259         void cutOff(int above, std::vector<CursorSlice> & cut);
260         /// cut off CursorSlices with index > above
261         void cutOff(int above);
262         /// push CursorSlices on top
263         void append(std::vector<CursorSlice> const & x);
264         /// push one CursorSlice on top and set its index and position
265         void append(idx_type idx, pos_type pos);
266
267         ///
268         docstring getPossibleLabel() const;
269
270         ///
271         Encoding const * getEncoding() const;
272 private:
273         friend class InsetIterator;
274         friend DocIterator doc_iterator_begin(Buffer const * buf, Inset const * inset);
275         friend DocIterator doc_iterator_end(Buffer const * buf, Inset const * inset);
276         ///
277         explicit DocIterator(Buffer * buf, Inset * inset)
278                 : inset_(inset), buffer_(buf)
279         {}
280         
281         /**
282          * Normally, when the cursor is at position i, it is painted *before*
283          * the character at position i. However, what if we want the cursor
284          * painted *after* position i? That's what boundary_ is for: if
285          * boundary_==true, the cursor is painted *after* position i-1, instead
286          * of before position i.
287          *
288          * Note 1: Usually, after i-1 or before i are actually the same place!
289          * However, this is not the case when i-1 and i are not painted
290          * contiguously, and in these cases we sometimes do want to have control
291          * over whether to paint before i or after i-1.
292          * Some concrete examples of where this happens:
293          * a. i-1 at the end of one row, i at the beginning of next row
294          * b. in bidi text, at transitions between RTL and LTR or vice versa
295          *
296          * Note 2: Why i and i-1? Why, if boundary_==false means: *before* i,
297          * couldn't boundary_==true mean: *after* i?
298          * Well, the reason is this: cursor position is not used only for
299          * painting the cursor, but it also affects other things, for example:
300          * where the next insertion will be placed (it is inserted at the current
301          * position, pushing anything at the current position and beyond forward).
302          * Now, when the current position is i and boundary_==true, insertion would
303          * happen *before* i. If the cursor, however, were painted *after* i, that
304          * would be very unnatural...
305          */
306         bool boundary_ = false;
307         ///
308         std::vector<CursorSlice> const & internalData() const { return slices_; }
309         ///
310         std::vector<CursorSlice> slices_;
311         ///
312         Inset * inset_ = nullptr;
313         ///
314         Buffer * buffer_ = nullptr;
315 };
316
317
318 inline bool operator==(DocIterator const & di1, DocIterator const & di2)
319 {
320         return di1.slices_ == di2.slices_;
321 }
322
323
324 inline bool operator!=(DocIterator const & di1, DocIterator const & di2)
325 {
326         return !(di1 == di2);
327 }
328
329
330 inline
331 bool operator<(DocIterator const & p, DocIterator const & q)
332 {
333         size_t depth = std::min(p.depth(), q.depth());
334         for (size_t i = 0 ; i < depth ; ++i) {
335                 if (p[i] != q[i])
336                         return p[i] < q[i];
337         }
338         return p.depth() < q.depth();
339 }
340
341
342 inline
343 bool operator>(DocIterator const & p, DocIterator const & q)
344 {
345         return q < p;
346 }
347
348
349 inline
350 bool operator<=(DocIterator const & p, DocIterator const & q)
351 {
352         return !(q < p);
353 }
354
355
356 inline
357 bool operator>=(DocIterator const & p, DocIterator const & q)
358 {
359         return !(p < q);
360 }
361
362
363 // The difference to a ('non stable') DocIterator is the removed
364 // (overwritten by 0...) part of the CursorSlice data items. So this thing
365 // is suitable for external storage, but not for iteration as such.
366
367 class StableDocIterator
368 {
369 public:
370         ///
371         StableDocIterator() {}
372         /// non-explicit intended
373         StableDocIterator(const DocIterator & it);
374         ///
375         DocIterator asDocIterator(Buffer * buf) const;
376         ///
377         size_t size() const { return data_.size(); }
378         ///  return the position within the paragraph
379         pos_type pos() const { return data_.back().pos(); }
380         ///  return the position within the paragraph
381         pos_type & pos() { return data_.back().pos(); }
382         ///
383         friend std::ostream &
384         operator<<(std::ostream & os, StableDocIterator const & cur);
385         ///
386         friend std::istream &
387         operator>>(std::istream & is, StableDocIterator & cur);
388         ///
389         friend bool
390         operator==(StableDocIterator const &, StableDocIterator const &);
391 private:
392         std::vector<CursorSlice> data_;
393 };
394
395 } // namespace lyx
396
397 #endif // DOCITERATOR_H