]> git.lyx.org Git - lyx.git/blob - src/DocIterator.h
DocBook: fix file inclusion.
[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 namespace lyx {
20
21 class DocIterator;
22 class Encoding;
23 class FontSpan;
24 class InsetIterator;
25 class LyXErr;
26 class MathAtom;
27 class Paragraph;
28 class Text;
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 begin position?
72         bool atBegin() const { return depth() == 1 && pit() == 0 && pos() == 0; }
73         /// is this the end position?
74         bool atEnd() const { return slices_.empty(); }
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         /// returns 0 if there is no inset (e.g. normal text)
129         Inset * nextInset() const;
130         /// the inset just in front of the cursor
131         Inset * prevInset() const;
132         ///
133         bool boundary() const { return boundary_; }
134         ///
135         void boundary(bool b) { boundary_ = b; }
136
137         // the two methods below have been inlined out because of
138         // profiling results under linux when opening a document.
139         /// are we in mathed?.
140         bool inMathed() const
141         { return !empty() && inset().inMathed(); }
142         /// are we in texted?.
143         bool inTexted() const
144         { return !empty() && !inset().inMathed(); }
145         /// are we in regexp-mode ?
146         bool inRegexped() const;
147
148         //
149         // math-specific part
150         //
151         /// return the mathed cell this cursor is in
152         MathData & cell() const;
153         ///
154         InsetMath & nextMath();
155         ///
156         InsetMath & prevMath();
157         /// the mathatom left of the cursor
158         MathAtom & prevAtom() const;
159         /// the mathatom right of the cursor
160         MathAtom & nextAtom() const;
161
162         // text-specific part
163         //
164         /// the paragraph we're in in text mode.
165         /// \warning only works within text!
166         Paragraph & paragraph() const;
167         /// the paragraph we're in in any case.
168         /// This method will give the containing paragraph even
169         /// if not in text mode (ex: in mathed).
170         Paragraph & innerParagraph() const;
171         /// return the inner text slice.
172         CursorSlice const & innerTextSlice() const;
173         // convert a DocIterator into an argument to LFUN_PARAGRAPH_GOTO
174         docstring paragraphGotoArgument() const;
175         /// returns a DocIterator for the containing text inset
176         DocIterator getInnerText() const;
177         /// the first and last positions of a word at top cursor slice
178         /// \warning only works within text!
179         FontSpan locateWord(word_location const loc) const;
180         ///
181         Text * text() const;
182         /// the containing inset or the cell, respectively
183         Inset * realInset() const;
184         ///
185         Inset * innerInsetOfType(int code) const;
186         ///
187         Text * innerText() const;
188
189         //
190         // elementary moving
191         //
192         /// move one step backwards
193         bool posBackward();
194         /// move one step forward
195         bool posForward();
196         /**
197          * move on one logical position, descend into nested insets
198          * including collapsed insets
199          */
200         void forwardPos();
201         /**
202          * move on one logical position, descend into nested insets
203          * skip collapsed insets
204          */
205         void forwardPosIgnoreCollapsed();
206         /// move on one physical character or inset
207         void forwardChar();
208         /// move on one paragraph
209         void forwardPar();
210         /// move on to the next closest inset
211         void forwardInset();
212         /// move backward one logical position
213         void backwardPos();
214         /// move backward one logical position, skip collapsed insets
215         void backwardPosIgnoreCollapsed();
216         /// move backward one physical character or inset
217         void backwardChar();
218         /// move backward one paragraph
219         void backwardPar();
220         /// move backward one inset
221         /// not used currently, uncomment if you need it
222         //void backwardInset();
223
224         /// are we some 'extension' (i.e. deeper nested) of the given iterator
225         bool hasPart(DocIterator const & it) const;
226
227         /// output
228         friend std::ostream &
229         operator<<(std::ostream & os, DocIterator const & cur);
230         friend LyXErr & operator<<(LyXErr & os, DocIterator const & it);
231         ///
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         friend bool operator<=(DocIterator const &, DocIterator const &);
236         ///
237         friend class StableDocIterator;
238 //protected:
239         ///
240         void clear() { slices_.clear(); }
241         ///
242         void push_back(CursorSlice const & sl) { slices_.push_back(sl); }
243         ///
244         void pop_back() { slices_.pop_back(); }
245         /// recompute the inset parts of the cursor from the document data
246         void updateInsets(Inset * inset);
247         /// fix DocIterator in circumstances that should never happen.
248         /// \return true if the DocIterator was fixed.
249         bool fixIfBroken();
250         /// Repopulate the slices insets from bottom to top. Useful
251         /// for stable iterators or Undo data.
252         void sanitize();
253         ///
254         bool isInside(Inset const *) const;
255         /// make sure we are outside of given inset
256         void leaveInset(Inset const & inset);
257
258         /// find index of CursorSlice with &cell() == &cell (or -1 if not found)
259         int find(MathData const & cell) const;
260         /// find index of CursorSlice with inset() == inset (or -1 of not found)
261         int find(Inset const * inset) const;
262         /// cut off CursorSlices with index > above and store cut off slices in cut.
263         void cutOff(int above, std::vector<CursorSlice> & cut);
264         /// cut off CursorSlices with index > above
265         void cutOff(int above);
266         /// push CursorSlices on top
267         void append(std::vector<CursorSlice> const & x);
268         /// push one CursorSlice on top and set its index and position
269         void append(idx_type idx, pos_type pos);
270
271         ///
272         docstring getPossibleLabel() const;
273
274         ///
275         Encoding const * getEncoding() const;
276 private:
277         friend class InsetIterator;
278         friend DocIterator doc_iterator_begin(Buffer const * buf, Inset const * inset);
279         friend DocIterator doc_iterator_end(Buffer const * buf, Inset const * inset);
280         ///
281         explicit DocIterator(Buffer * buf, Inset * inset);
282         /**
283          * Normally, when the cursor is at position i, it is painted *before*
284          * the character at position i. However, what if we want the cursor
285          * painted *after* position i? That's what boundary_ is for: if
286          * boundary_==true, the cursor is painted *after* position i-1, instead
287          * of before position i.
288          *
289          * Note 1: Usually, after i-1 or before i are actually the same place!
290          * However, this is not the case when i-1 and i are not painted
291          * contiguously, and in these cases we sometimes do want to have control
292          * over whether to paint before i or after i-1.
293          * Some concrete examples of where this happens:
294          * a. i-1 at the end of one row, i at the beginning of next row
295          * b. in bidi text, at transitions between RTL and LTR or vice versa
296          *
297          * Note 2: Why i and i-1? Why, if boundary_==false means: *before* i,
298          * couldn't boundary_==true mean: *after* i?
299          * Well, the reason is this: cursor position is not used only for
300          * painting the cursor, but it also affects other things, for example:
301          * where the next insertion will be placed (it is inserted at the current
302          * position, pushing anything at the current position and beyond forward).
303          * Now, when the current position is i and boundary_==true, insertion would
304          * happen *before* i. If the cursor, however, were painted *after* i, that
305          * would be very unnatural...
306          */
307         bool boundary_;
308         ///
309         std::vector<CursorSlice> const & internalData() const { return slices_; }
310         ///
311         std::vector<CursorSlice> slices_;
312         ///
313         Inset * inset_;
314         ///
315         Buffer * buffer_;
316 };
317
318
319 inline bool operator==(DocIterator const & di1, DocIterator const & di2)
320 {
321         return di1.slices_ == di2.slices_;
322 }
323
324
325 inline bool operator!=(DocIterator const & di1, DocIterator const & di2)
326 {
327         return !(di1 == di2);
328 }
329
330
331 inline
332 bool operator<(DocIterator const & p, DocIterator const & q)
333 {
334         size_t depth = std::min(p.depth(), q.depth());
335         for (size_t i = 0 ; i < depth ; ++i) {
336                 if (p[i] != q[i])
337                         return p[i] < q[i];
338         }
339         return p.depth() < q.depth();
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 !(q < p);
354 }
355
356
357 inline
358 bool operator>=(DocIterator const & p, DocIterator const & q)
359 {
360         return !(p < q);
361 }
362
363
364 // The difference to a ('non stable') DocIterator is the removed
365 // (overwritten by 0...) part of the CursorSlice data items. So this thing
366 // is suitable for external storage, but not for iteration as such.
367
368 class StableDocIterator
369 {
370 public:
371         ///
372         StableDocIterator() {}
373         /// non-explicit intended
374         StableDocIterator(const DocIterator & it);
375         ///
376         DocIterator asDocIterator(Buffer * buf) const;
377         ///
378         size_t size() const { return data_.size(); }
379         ///  return the position within the paragraph
380         pos_type pos() const { return data_.back().pos(); }
381         ///  return the position within the paragraph
382         pos_type & pos() { return data_.back().pos(); }
383         ///
384         friend std::ostream &
385         operator<<(std::ostream & os, StableDocIterator const & cur);
386         ///
387         friend std::istream &
388         operator>>(std::istream & is, StableDocIterator & cur);
389         ///
390         friend bool
391         operator==(StableDocIterator const &, StableDocIterator const &);
392 private:
393         std::vector<CursorSlice> data_;
394 };
395
396 } // namespace lyx
397
398 #endif // DOCITERATOR_H