]> git.lyx.org Git - lyx.git/blob - src/DocIterator.h
Account for old versions of Pygments
[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 LyXErr;
24 class MathAtom;
25 class Paragraph;
26 class Text;
27 class InsetIterator;
28 class FontSpan;
29
30 DocIterator doc_iterator_begin(Buffer const * buf, Inset const * inset = 0);
31 DocIterator doc_iterator_end(Buffer const * buf, Inset const * inset = 0);
32
33
34 class DocIterator
35 {
36 public:
37         /// type for cell number in inset
38         typedef CursorSlice::idx_type idx_type;
39         /// type for row indices
40         typedef CursorSlice::row_type row_type;
41         /// type for col indices
42         typedef CursorSlice::col_type col_type;
43
44 public:
45         ///
46         DocIterator();
47         ///
48         explicit DocIterator(Buffer *buf);
49
50         /// access to owning buffer
51         Buffer * buffer() const { return buffer_; }
52         /// access to owning buffer
53         void setBuffer(Buffer * buf) { buffer_ = buf; }
54
55         /// Clone this for given \p buffer.
56         /// \p buffer must be a clone of buffer_.
57         DocIterator clone(Buffer * buffer) const;
58
59         /// access slice at position \p i
60         CursorSlice const & operator[](size_t i) const { return slices_[i]; }
61         /// access slice at position \p i
62         CursorSlice & operator[](size_t i) { return slices_[i]; }
63         /// chop a few slices from the iterator
64         void resize(size_t i) { slices_.resize(i); }
65
66         /// is the iterator valid?
67         explicit operator bool() const { return !empty(); }
68
69         /// does this iterator have any content?
70         bool empty() const { return slices_.empty(); }
71         /// is this the end position?
72         bool atEnd() const { return slices_.empty(); }
73         /// is this the last possible position?
74         bool atLastPos() const { return pit() == lastpit() && pos() == lastpos(); }
75
76         /// checks the cursor slices for disabled spell checker insets
77         bool allowSpellCheck() const;
78
79         //
80         // access to slice at tip
81         //
82         /// access to tip
83         CursorSlice & top() { return slices_.back(); }
84         /// access to tip
85         CursorSlice const & top() const { return slices_.back(); }
86         /// access to outermost slice
87         CursorSlice & bottom() { return slices_.front(); }
88         /// access to outermost slice
89         CursorSlice const & bottom() const { return slices_.front(); }
90         /// how many nested insets do we have?
91         size_t depth() const { return slices_.size(); }
92         /// the containing inset
93         Inset & inset() const { return top().inset(); }
94         /// return the cell of the inset this cursor is in
95         idx_type idx() const { return top().idx(); }
96         /// return the cell of the inset this cursor is in
97         idx_type & idx() { return top().idx(); }
98         /// return the last possible cell in this inset
99         idx_type lastidx() const;
100         /// return the paragraph this cursor is in
101         pit_type pit() const { return top().pit(); }
102         /// return the paragraph this cursor is in
103         pit_type & pit() { return top().pit(); }
104         /// return the last possible paragraph in this inset
105         pit_type lastpit() const;
106         /// return the position within the paragraph
107         pos_type pos() const { return top().pos(); }
108         /// return the position within the paragraph
109         pos_type & pos() { return top().pos(); }
110         /// return the last position within the paragraph
111         pos_type lastpos() const;
112
113         /// return the number of embedded cells
114         size_t nargs() const;
115         /// return the number of embedded cells
116         size_t ncols() const;
117         /// return the number of embedded cells
118         size_t nrows() const;
119         /// return the grid row of the top cell
120         row_type row() const;
121         /// return the last row of the top grid
122         row_type lastrow() const { return nrows() - 1; }
123         /// return the grid column of the top cell
124         col_type col() const;
125         /// return the last column of the top grid
126         col_type lastcol() const { return ncols() - 1; }
127         /// the inset just behind the cursor
128         Inset * nextInset() const;
129         /// the inset just in front of the cursor
130         Inset * prevInset() const;
131         ///
132         bool boundary() const { return boundary_; }
133         ///
134         void boundary(bool b) { boundary_ = b; }
135
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;
146
147         //
148         // math-specific part
149         //
150         /// return the mathed cell this cursor is in
151         MathData & cell() const;
152         /// the mathatom left of the cursor
153         MathAtom & prevAtom() const;
154         /// the mathatom right of the cursor
155         MathAtom & nextAtom() const;
156
157         // text-specific part
158         //
159         /// the paragraph we're in in text mode.
160         /// \warning only works within text!
161         Paragraph & paragraph() const;
162         /// the paragraph we're in in any case.
163         /// This method will give the containing paragraph even
164         /// if not in text mode (ex: in mathed).
165         Paragraph & innerParagraph() const;
166         /// return the inner text slice.
167         CursorSlice const & innerTextSlice() const;
168         // convert a DocIterator into an argument to LFUN_PARAGRAPH_GOTO
169         docstring paragraphGotoArgument() const;
170         /// returns a DocIterator for the containing text inset
171         DocIterator getInnerText() const;
172         /// the first and last positions of a word at top cursor slice
173         /// \warning only works within text!
174         FontSpan locateWord(word_location const loc) const;
175         ///
176         Text * text() const;
177         /// the containing inset or the cell, respectively
178         Inset * realInset() const;
179         ///
180         Inset * innerInsetOfType(int code) const;
181         ///
182         Text * innerText() const;
183
184         //
185         // elementary moving
186         //
187         /// move one step backwards
188         bool posBackward();
189         /// move one step forward
190         bool posForward();
191         /**
192          * move on one logical position, descend into nested insets
193          * including collapsed insets
194          */
195         void forwardPos();
196         /**
197          * move on one logical position, descend into nested insets
198          * skip collapsed insets
199          */
200         void forwardPosIgnoreCollapsed();
201         /// move on one physical character or inset
202         void forwardChar();
203         /// move on one paragraph
204         void forwardPar();
205         /// move on one inset
206         void forwardInset();
207         /// move backward one logical position
208         void backwardPos();
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         /// find index of CursorSlice with &cell() == &cell (or -1 if not found)
248         int find(MathData const & cell) const;
249         /// find index of CursorSlice with inset() == inset (or -1 of not found)
250         int find(Inset const * inset) const;
251         /// cut off CursorSlices with index > above and store cut off slices in cut.
252         void cutOff(int above, std::vector<CursorSlice> & cut);
253         /// cut off CursorSlices with index > above
254         void cutOff(int above);
255         /// push CursorSlices on top
256         void append(std::vector<CursorSlice> const & x);
257         /// push one CursorSlice on top and set its index and position
258         void append(idx_type idx, pos_type pos);
259
260 private:
261         friend class InsetIterator;
262         friend DocIterator doc_iterator_begin(Buffer const * buf, Inset const * inset);
263         friend DocIterator doc_iterator_end(Buffer const * buf, Inset const * inset);
264         ///
265         explicit DocIterator(Buffer * buf, Inset * inset);
266         /**
267          * Normally, when the cursor is at position i, it is painted *before*
268          * the character at position i. However, what if we want the cursor 
269          * painted *after* position i? That's what boundary_ is for: if
270          * boundary_==true, the cursor is painted *after* position i-1, instead
271          * of before position i.
272          *
273          * Note 1: Usually, after i-1 or before i are actually the same place!
274          * However, this is not the case when i-1 and i are not painted 
275          * contiguously, and in these cases we sometimes do want to have control
276          * over whether to paint before i or after i-1.
277          * Some concrete examples of where this happens:
278          * a. i-1 at the end of one row, i at the beginning of next row
279          * b. in bidi text, at transitions between RTL and LTR or vice versa
280          *
281          * Note 2: Why i and i-1? Why, if boundary_==false means: *before* i, 
282          * couldn't boundary_==true mean: *after* i? 
283          * Well, the reason is this: cursor position is not used only for 
284          * painting the cursor, but it also affects other things, for example:
285          * where the next insertion will be placed (it is inserted at the current
286          * position, pushing anything at the current position and beyond forward).
287          * Now, when the current position is i and boundary_==true, insertion would
288          * happen *before* i. If the cursor, however, were painted *after* i, that
289          * would be very unnatural...
290          */
291         bool boundary_;
292         ///
293         std::vector<CursorSlice> const & internalData() const { return slices_; }
294         ///
295         std::vector<CursorSlice> slices_;
296         ///
297         Inset * inset_;
298         ///
299         Buffer * buffer_;
300 };
301
302
303 inline bool operator==(DocIterator const & di1, DocIterator const & di2)
304 {
305         return di1.slices_ == di2.slices_;
306 }
307
308
309 inline bool operator!=(DocIterator const & di1, DocIterator const & di2)
310 {
311         return !(di1 == di2);
312 }
313
314
315 inline
316 bool operator<(DocIterator const & p, DocIterator const & q)
317 {
318         size_t depth = std::min(p.depth(), q.depth());
319         for (size_t i = 0 ; i < depth ; ++i) {
320                 if (p[i] != q[i])
321                         return p[i] < q[i];
322         }
323         return p.depth() < q.depth();
324 }
325
326
327 inline  
328 bool operator>(DocIterator const & p, DocIterator const & q)
329 {
330         return q < p;
331 }
332
333
334 inline  
335 bool operator<=(DocIterator const & p, DocIterator const & q)
336 {
337         return !(q < p);
338 }
339
340
341 inline  
342 bool operator>=(DocIterator const & p, DocIterator const & q)
343 {
344         return !(p < q);
345 }
346
347
348 // The difference to a ('non stable') DocIterator is the removed
349 // (overwritten by 0...) part of the CursorSlice data items. So this thing
350 // is suitable for external storage, but not for iteration as such.
351
352 class StableDocIterator
353 {
354 public:
355         ///
356         StableDocIterator() {}
357         /// non-explicit intended
358         StableDocIterator(const DocIterator & it);
359         ///
360         DocIterator asDocIterator(Buffer * buf) const;
361         ///
362         size_t size() const { return data_.size(); }
363         ///  return the position within the paragraph
364         pos_type pos() const { return data_.back().pos(); }
365         ///  return the position within the paragraph
366         pos_type & pos() { return data_.back().pos(); }
367         ///
368         friend std::ostream &
369         operator<<(std::ostream & os, StableDocIterator const & cur);
370         ///
371         friend std::istream &
372         operator>>(std::istream & is, StableDocIterator & cur);
373         ///
374         friend bool
375         operator==(StableDocIterator const &, StableDocIterator const &);
376 private:
377         std::vector<CursorSlice> data_;
378 };
379
380 } // namespace lyx
381
382 #endif // DOCITERATOR_H