]> git.lyx.org Git - lyx.git/blob - src/DocIterator.h
0653de86e950b7f20286b5e8d5e9539ff4a1062c
[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>
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         /// 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;
44
45 public:
46         ///
47         DocIterator();
48         ///
49         explicit DocIterator(Buffer *buf);
50
51         /// access to owning buffer
52         Buffer * buffer() const { return buffer_; }
53         /// access to owning buffer
54         void setBuffer(Buffer * buf) { buffer_ = buf; }
55
56         /// Clone this for given \p buffer.
57         /// \p buffer must be a clone of buffer_.
58         DocIterator clone(Buffer * buffer) const;
59
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); }
66
67         /// is the iterator valid?
68         explicit operator bool() const { return !empty(); }
69
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(); }
74         /// is this the last possible position?
75         bool atLastPos() const { return pit() == lastpit() && pos() == lastpos(); }
76
77         /// checks the cursor slices for disabled spell checker insets
78         bool allowSpellCheck() const;
79
80         //
81         // access to slice at tip
82         //
83         /// access to tip
84         CursorSlice & top() { return slices_.back(); }
85         /// access to tip
86         CursorSlice const & top() const { return slices_.back(); }
87         /// access to outermost slice
88         CursorSlice & bottom() { return slices_.front(); }
89         /// access to outermost slice
90         CursorSlice const & bottom() const { return slices_.front(); }
91         /// how many nested insets do we have?
92         size_t depth() const { return slices_.size(); }
93         /// the containing inset
94         Inset & inset() const { return top().inset(); }
95         /// return the cell of the inset this cursor is in
96         idx_type idx() const { return top().idx(); }
97         /// return the cell of the inset this cursor is in
98         idx_type & idx() { return top().idx(); }
99         /// return the last possible cell in this inset
100         idx_type lastidx() const;
101         /// return the paragraph this cursor is in
102         pit_type pit() const { return top().pit(); }
103         /// return the paragraph this cursor is in
104         pit_type & pit() { return top().pit(); }
105         /// return the last possible paragraph in this inset
106         pit_type lastpit() const;
107         /// return the position within the paragraph
108         pos_type pos() const { return top().pos(); }
109         /// return the position within the paragraph
110         pos_type & pos() { return top().pos(); }
111         /// return the last position within the paragraph
112         pos_type lastpos() const;
113
114         /// return the number of embedded cells
115         size_t nargs() const;
116         /// return the number of embedded cells
117         size_t ncols() const;
118         /// return the number of embedded cells
119         size_t nrows() const;
120         /// return the grid row of the top cell
121         row_type row() const;
122         /// return the last row of the top grid
123         row_type lastrow() const { return nrows() - 1; }
124         /// return the grid column of the top cell
125         col_type col() const;
126         /// return the last column of the top grid
127         col_type lastcol() const { return ncols() - 1; }
128         /// the inset just behind the cursor
129         /// returns 0 if there is no inset (e.g. normal text)
130         Inset * nextInset() const;
131         /// the inset just in front of the cursor
132         Inset * prevInset() const;
133         ///
134         bool boundary() const { return boundary_; }
135         ///
136         void boundary(bool b) { boundary_ = b; }
137
138         // the two methods below have been inlined out because of
139         // profiling results under linux when opening a document.
140         /// are we in mathed?.
141         bool inMathed() const
142         { return !empty() && inset().inMathed(); }
143         /// are we in texted?.
144         bool inTexted() const
145         { return !empty() && !inset().inMathed(); }
146         /// are we in regexp-mode ?
147         bool inRegexped() const;
148
149         //
150         // math-specific part
151         //
152         /// return the mathed cell this cursor is in
153         MathData & cell() const;
154         ///
155         InsetMath & nextMath();
156         ///
157         InsetMath & prevMath();
158         /// the mathatom left of the cursor
159         MathAtom & prevAtom() const;
160         /// the mathatom right of the cursor
161         MathAtom & nextAtom() const;
162
163         // text-specific part
164         //
165         /// the paragraph we're in in text mode.
166         /// \warning only works within text!
167         Paragraph & paragraph() const;
168         /// the paragraph we're in in any case.
169         /// This method will give the containing paragraph even
170         /// if not in text mode (ex: in mathed).
171         Paragraph & innerParagraph() const;
172         /// return the inner text slice.
173         CursorSlice const & innerTextSlice() const;
174         // convert a DocIterator into an argument to LFUN_PARAGRAPH_GOTO
175         docstring paragraphGotoArgument() const;
176         /// returns a DocIterator for the containing text inset
177         DocIterator getInnerText() const;
178         /// the first and last positions of a word at top cursor slice
179         /// \warning only works within text!
180         FontSpan locateWord(word_location const loc) const;
181         ///
182         Text * text() const;
183         /// the containing inset or the cell, respectively
184         Inset * realInset() const;
185         ///
186         Inset * innerInsetOfType(int code) const;
187         ///
188         Text * innerText() const;
189
190         //
191         // elementary moving
192         //
193         /// move one step backwards
194         bool posBackward();
195         /// move one step forward
196         bool posForward();
197         /**
198          * move on one logical position, descend into nested insets
199          * including collapsed insets
200          */
201         void forwardPos();
202         /**
203          * move on one logical position, descend into nested insets
204          * skip collapsed insets
205          */
206         void forwardPosIgnoreCollapsed();
207         /// move on one physical character or inset
208         void forwardChar();
209         /// move on one paragraph
210         void forwardPar();
211         /// move on to the next closest inset
212         void forwardInset();
213         /// move backward one logical position
214         void backwardPos();
215         /// move backward one physical character or inset
216         void backwardChar();
217         /// move backward one paragraph
218         void backwardPar();
219         /// move backward one inset
220         /// not used currently, uncomment if you need it
221         //void backwardInset();
222
223         /// are we some 'extension' (i.e. deeper nested) of the given iterator
224         bool hasPart(DocIterator const & it) const;
225
226         /// output
227         friend std::ostream &
228         operator<<(std::ostream & os, DocIterator const & cur);
229         friend LyXErr & operator<<(LyXErr & os, DocIterator const & it);
230         ///
231         friend bool operator==(DocIterator const &, DocIterator const &);
232         friend bool operator<(DocIterator const &, DocIterator const &);
233         friend bool operator>(DocIterator const &, DocIterator const &);
234         friend bool operator<=(DocIterator const &, DocIterator const &);
235         ///
236         friend class StableDocIterator;
237 //protected:
238         ///
239         void clear() { slices_.clear(); }
240         ///
241         void push_back(CursorSlice const & sl) { slices_.push_back(sl); }
242         ///
243         void pop_back() { slices_.pop_back(); }
244         /// recompute the inset parts of the cursor from the document data
245         void updateInsets(Inset * inset);
246         /// fix DocIterator in circumstances that should never happen.
247         /// \return true if the DocIterator was fixed.
248         bool fixIfBroken();
249         /// Repopulate the slices insets from bottom to top. Useful
250         /// for stable iterators or Undo data.
251         void sanitize();
252         ///
253         bool isInside(Inset const *) const;
254         /// make sure we are outside of given inset
255         void leaveInset(Inset const & inset);
256
257         /// find index of CursorSlice with &cell() == &cell (or -1 if not found)
258         int find(MathData const & cell) const;
259         /// find index of CursorSlice with inset() == inset (or -1 of not found)
260         int find(Inset const * inset) const;
261         /// cut off CursorSlices with index > above and store cut off slices in cut.
262         void cutOff(int above, std::vector<CursorSlice> & cut);
263         /// cut off CursorSlices with index > above
264         void cutOff(int above);
265         /// push CursorSlices on top
266         void append(std::vector<CursorSlice> const & x);
267         /// push one CursorSlice on top and set its index and position
268         void append(idx_type idx, pos_type pos);
269
270         ///
271         docstring getPossibleLabel() const;
272
273         ///
274         Encoding const * getEncoding() const;
275 private:
276         friend class InsetIterator;
277         friend DocIterator doc_iterator_begin(Buffer const * buf, Inset const * inset);
278         friend DocIterator doc_iterator_end(Buffer const * buf, Inset const * inset);
279         ///
280         explicit DocIterator(Buffer * buf, Inset * inset);
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_;
307         ///
308         std::vector<CursorSlice> const & internalData() const { return slices_; }
309         ///
310         std::vector<CursorSlice> slices_;
311         ///
312         Inset * inset_;
313         ///
314         Buffer * buffer_;
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