]> git.lyx.org Git - lyx.git/blob - src/DocIterator.h
configure.py: Check path accessibility before call to os.path.isfile.
[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
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         operator const void*() const { return empty() ? 0 : this; }
68         /// is this iterator invalid?
69         bool operator!() const { return empty(); }
70
71         /// does this iterator have any content?
72         bool empty() const { return slices_.empty(); }
73         /// is this the end position?
74         bool atEnd() const { return slices_.empty(); }
75         /// is this the last possible position?
76         bool atLastPos() const { return pit() == lastpit() && pos() == lastpos(); }
77
78         //
79         // access to slice at tip
80         //
81         /// access to tip
82         CursorSlice & top() { return slices_.back(); }
83         /// access to tip
84         CursorSlice const & top() const { return slices_.back(); }
85         /// access to outermost slice
86         CursorSlice & bottom() { return slices_.front(); }
87         /// access to outermost slice
88         CursorSlice const & bottom() const { return slices_.front(); }
89         /// how many nested insets do we have?
90         size_t depth() const { return slices_.size(); }
91         /// the containing inset
92         Inset & inset() const { return top().inset(); }
93         /// return the cell of the inset this cursor is in
94         idx_type idx() const { return top().idx(); }
95         /// return the cell of the inset this cursor is in
96         idx_type & idx() { return top().idx(); }
97         /// return the last possible cell in this inset
98         idx_type lastidx() const;
99         /// return the paragraph this cursor is in
100         pit_type pit() const { return top().pit(); }
101         /// return the paragraph this cursor is in
102         pit_type & pit() { return top().pit(); }
103         /// return the last possible paragraph in this inset
104         pit_type lastpit() const;
105         /// return the position within the paragraph
106         pos_type pos() const { return top().pos(); }
107         /// return the position within the paragraph
108         pos_type & pos() { return top().pos(); }
109         /// return the last position within the paragraph
110         pos_type lastpos() const;
111
112         /// return the number of embedded cells
113         size_t nargs() const;
114         /// return the number of embedded cells
115         size_t ncols() const;
116         /// return the number of embedded cells
117         size_t nrows() const;
118         /// return the grid row of the top cell
119         row_type row() const;
120         /// return the last row of the top grid
121         row_type lastrow() const { return nrows() - 1; }
122         /// return the grid column of the top cell
123         col_type col() const;
124         /// return the last column of the top grid
125         col_type lastcol() const { return ncols() - 1; }
126         /// the inset just behind the cursor
127         Inset * nextInset() const;
128         /// the inset just in front of the cursor
129         Inset * prevInset() const;
130         ///
131         bool boundary() const { return boundary_; }
132         ///
133         void boundary(bool b) { boundary_ = b; }
134
135         // the two methods below have been inlined out because of
136         // profiling results under linux when opening a document.
137         /// are we in mathed?.
138         bool inMathed() const
139         { return !empty() && inset().inMathed(); }
140         /// are we in texted?.
141         bool inTexted() const
142         { return !empty() && !inset().inMathed(); }
143         /// are we in regexp-mode ?
144         bool inRegexped() const;
145
146         //
147         // math-specific part
148         //
149         /// return the mathed cell this cursor is in
150         MathData & cell() const;
151         /// the mathatom left of the cursor
152         MathAtom & prevAtom() const;
153         /// the mathatom right of the cursor
154         MathAtom & nextAtom() const;
155
156         // text-specific part
157         //
158         /// the paragraph we're in in text mode.
159         /// \warning only works within text!
160         Paragraph & paragraph() const;
161         /// the paragraph we're in in any case.
162         /// This method will give the containing paragraph even
163         /// if not in text mode (ex: in mathed).
164         Paragraph & innerParagraph() const;
165         /// return the inner text slice.
166         CursorSlice const & innerTextSlice() const;
167         /// the first and last positions of a word at top cursor slice
168         /// \warning only works within text!
169         FontSpan locateWord(word_location const loc) const;
170         ///
171         Text * text() const;
172         /// the containing inset or the cell, respectively
173         Inset * realInset() const;
174         ///
175         Inset * innerInsetOfType(int code) const;
176         ///
177         Text * innerText() const;
178
179         //
180         // elementary moving
181         //
182         /**
183          * move on one logical position, descend into nested insets
184          * including collapsed insets
185          */
186         void forwardPos();
187         /**
188          * move on one logical position, descend into nested insets
189          * skip collapsed insets
190          */
191         void forwardPosIgnoreCollapsed();
192         /// move on one physical character or inset
193         void forwardChar();
194         /// move on one paragraph
195         void forwardPar();
196         /// move on one inset
197         void forwardInset();
198         /// move backward one logical position
199         void backwardPos();
200         /// move backward one physical character or inset
201         void backwardChar();
202         /// move backward one paragraph
203         void backwardPar();
204         /// move backward one inset
205         /// FIXME: This is not implemented!
206         //void backwardInset();
207
208         /// are we some 'extension' (i.e. deeper nested) of the given iterator
209         bool hasPart(DocIterator const & it) const;
210
211         /// output
212         friend std::ostream &
213         operator<<(std::ostream & os, DocIterator const & cur);
214         friend LyXErr & operator<<(LyXErr & os, DocIterator const & it);
215         ///
216         friend bool operator==(DocIterator const &, DocIterator const &);
217         friend bool operator<(DocIterator const &, DocIterator const &);
218         friend bool operator>(DocIterator const &, DocIterator const &);
219         friend bool operator<=(DocIterator const &, DocIterator const &);
220         ///
221         friend class StableDocIterator;
222 //protected:
223         ///
224         void clear() { slices_.clear(); }
225         ///
226         void push_back(CursorSlice const & sl) { slices_.push_back(sl); }
227         ///
228         void pop_back() { slices_.pop_back(); }
229         /// recompute the inset parts of the cursor from the document data
230         void updateInsets(Inset * inset);
231         /// fix DocIterator in circumstances that should never happen.
232         /// \return true if the DocIterator was fixed.
233         bool fixIfBroken();
234         /// Repopulate the slices insets from bottom to top. Useful
235         /// for stable iterators or Undo data.
236         void sanitize();
237
238         /// find index of CursorSlice with &cell() == &cell (or -1 if not found)
239         int find(MathData const & cell) const;
240         /// find index of CursorSlice with inset() == inset (or -1 of not found)
241         int find(Inset const * inset) const;
242         /// cut off CursorSlices with index > above and store cut off slices in cut.
243         void cutOff(int above, std::vector<CursorSlice> & cut);
244         /// cut off CursorSlices with index > above
245         void cutOff(int above);
246         /// push CursorSlices on top
247         void append(std::vector<CursorSlice> const & x);
248         /// push one CursorSlice on top and set its index and position
249         void append(idx_type idx, pos_type pos);
250
251 private:
252         friend class InsetIterator;
253         friend DocIterator doc_iterator_begin(Buffer const * buf, Inset const * inset);
254         friend DocIterator doc_iterator_end(Buffer const * buf, Inset const * inset);
255         ///
256         explicit DocIterator(Buffer * buf, Inset * inset);
257         /**
258          * Normally, when the cursor is at position i, it is painted *before*
259          * the character at position i. However, what if we want the cursor 
260          * painted *after* position i? That's what boundary_ is for: if
261          * boundary_==true, the cursor is painted *after* position i-1, instead
262          * of before position i.
263          *
264          * Note 1: Usually, after i-1 or before i are actually the same place!
265          * However, this is not the case when i-1 and i are not painted 
266          * contiguously, and in these cases we sometimes do want to have control
267          * over whether to paint before i or after i-1.
268          * Some concrete examples of where this happens:
269          * a. i-1 at the end of one row, i at the beginning of next row
270          * b. in bidi text, at transitions between RTL and LTR or vice versa
271          *
272          * Note 2: Why i and i-1? Why, if boundary_==false means: *before* i, 
273          * couldn't boundary_==true mean: *after* i? 
274          * Well, the reason is this: cursor position is not used only for 
275          * painting the cursor, but it also affects other things, for example:
276          * where the next insertion will be placed (it is inserted at the current
277          * position, pushing anything at the current position and beyond forward).
278          * Now, when the current position is i and boundary_==true, insertion would
279          * happen *before* i. If the cursor, however, were painted *after* i, that
280          * would be very unnatural...
281          */
282         bool boundary_;
283         ///
284         std::vector<CursorSlice> const & internalData() const { return slices_; }
285         ///
286         std::vector<CursorSlice> slices_;
287         ///
288         Inset * inset_;
289         ///
290         Buffer * buffer_;
291 };
292
293
294 inline bool operator==(DocIterator const & di1, DocIterator const & di2)
295 {
296         return di1.slices_ == di2.slices_;
297 }
298
299
300 inline bool operator!=(DocIterator const & di1, DocIterator const & di2)
301 {
302         return !(di1 == di2);
303 }
304
305
306 inline
307 bool operator<(DocIterator const & p, DocIterator const & q)
308 {
309         size_t depth = std::min(p.depth(), q.depth());
310         for (size_t i = 0 ; i < depth ; ++i) {
311                 if (p[i] != q[i])
312                         return p[i] < q[i];
313         }
314         return p.depth() < q.depth();
315 }
316
317
318 inline  
319 bool operator>(DocIterator const & p, DocIterator const & q)
320 {
321         return q < p;
322 }
323
324
325 inline  
326 bool operator<=(DocIterator const & p, DocIterator const & q)
327 {
328         return !(q < p);
329 }
330
331
332 inline  
333 bool operator>=(DocIterator const & p, DocIterator const & q)
334 {
335         return !(p < q);
336 }
337
338
339 // The difference to a ('non stable') DocIterator is the removed
340 // (overwritten by 0...) part of the CursorSlice data items. So this thing
341 // is suitable for external storage, but not for iteration as such.
342
343 class StableDocIterator
344 {
345 public:
346         ///
347         StableDocIterator() {}
348         /// non-explicit intended
349         StableDocIterator(const DocIterator & it);
350         ///
351         DocIterator asDocIterator(Buffer * buf) const;
352         ///
353         size_t size() const { return data_.size(); }
354         ///  return the position within the paragraph
355         pos_type pos() const { return data_.back().pos(); }
356         ///  return the position within the paragraph
357         pos_type & pos() { return data_.back().pos(); }
358         ///
359         friend std::ostream &
360         operator<<(std::ostream & os, StableDocIterator const & cur);
361         ///
362         friend std::istream &
363         operator>>(std::istream & is, StableDocIterator & cur);
364         ///
365         friend bool
366         operator==(StableDocIterator const &, StableDocIterator const &);
367 private:
368         std::vector<CursorSlice> data_;
369 };
370
371 } // namespace lyx
372
373 #endif // DOCITERATOR_H