]> git.lyx.org Git - lyx.git/blob - src/DocIterator.h
Russian layouttranslations reviewed by Yuriy, Dec 13 2017.
[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         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 one inset
211         void forwardInset();
212         /// move backward one logical position
213         void backwardPos();
214         /// move backward one physical character or inset
215         void backwardChar();
216         /// move backward one paragraph
217         void backwardPar();
218         /// move backward one inset
219         /// not used currently, uncomment if you need it
220         //void backwardInset();
221
222         /// are we some 'extension' (i.e. deeper nested) of the given iterator
223         bool hasPart(DocIterator const & it) const;
224
225         /// output
226         friend std::ostream &
227         operator<<(std::ostream & os, DocIterator const & cur);
228         friend LyXErr & operator<<(LyXErr & os, DocIterator const & it);
229         ///
230         friend bool operator==(DocIterator const &, DocIterator const &);
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         ///
235         friend class StableDocIterator;
236 //protected:
237         ///
238         void clear() { slices_.clear(); }
239         ///
240         void push_back(CursorSlice const & sl) { slices_.push_back(sl); }
241         ///
242         void pop_back() { slices_.pop_back(); }
243         /// recompute the inset parts of the cursor from the document data
244         void updateInsets(Inset * inset);
245         /// fix DocIterator in circumstances that should never happen.
246         /// \return true if the DocIterator was fixed.
247         bool fixIfBroken();
248         /// Repopulate the slices insets from bottom to top. Useful
249         /// for stable iterators or Undo data.
250         void sanitize();
251
252         /// find index of CursorSlice with &cell() == &cell (or -1 if not found)
253         int find(MathData const & cell) const;
254         /// find index of CursorSlice with inset() == inset (or -1 of not found)
255         int find(Inset const * inset) const;
256         /// cut off CursorSlices with index > above and store cut off slices in cut.
257         void cutOff(int above, std::vector<CursorSlice> & cut);
258         /// cut off CursorSlices with index > above
259         void cutOff(int above);
260         /// push CursorSlices on top
261         void append(std::vector<CursorSlice> const & x);
262         /// push one CursorSlice on top and set its index and position
263         void append(idx_type idx, pos_type pos);
264
265         ///
266         docstring getPossibleLabel() const;
267
268         ///
269         Encoding const * getEncoding() const;
270 private:
271         friend class InsetIterator;
272         friend DocIterator doc_iterator_begin(Buffer const * buf, Inset const * inset);
273         friend DocIterator doc_iterator_end(Buffer const * buf, Inset const * inset);
274         ///
275         explicit DocIterator(Buffer * buf, Inset * inset);
276         /**
277          * Normally, when the cursor is at position i, it is painted *before*
278          * the character at position i. However, what if we want the cursor
279          * painted *after* position i? That's what boundary_ is for: if
280          * boundary_==true, the cursor is painted *after* position i-1, instead
281          * of before position i.
282          *
283          * Note 1: Usually, after i-1 or before i are actually the same place!
284          * However, this is not the case when i-1 and i are not painted
285          * contiguously, and in these cases we sometimes do want to have control
286          * over whether to paint before i or after i-1.
287          * Some concrete examples of where this happens:
288          * a. i-1 at the end of one row, i at the beginning of next row
289          * b. in bidi text, at transitions between RTL and LTR or vice versa
290          *
291          * Note 2: Why i and i-1? Why, if boundary_==false means: *before* i,
292          * couldn't boundary_==true mean: *after* i?
293          * Well, the reason is this: cursor position is not used only for
294          * painting the cursor, but it also affects other things, for example:
295          * where the next insertion will be placed (it is inserted at the current
296          * position, pushing anything at the current position and beyond forward).
297          * Now, when the current position is i and boundary_==true, insertion would
298          * happen *before* i. If the cursor, however, were painted *after* i, that
299          * would be very unnatural...
300          */
301         bool boundary_;
302         ///
303         std::vector<CursorSlice> const & internalData() const { return slices_; }
304         ///
305         std::vector<CursorSlice> slices_;
306         ///
307         Inset * inset_;
308         ///
309         Buffer * buffer_;
310 };
311
312
313 inline bool operator==(DocIterator const & di1, DocIterator const & di2)
314 {
315         return di1.slices_ == di2.slices_;
316 }
317
318
319 inline bool operator!=(DocIterator const & di1, DocIterator const & di2)
320 {
321         return !(di1 == di2);
322 }
323
324
325 inline
326 bool operator<(DocIterator const & p, DocIterator const & q)
327 {
328         size_t depth = std::min(p.depth(), q.depth());
329         for (size_t i = 0 ; i < depth ; ++i) {
330                 if (p[i] != q[i])
331                         return p[i] < q[i];
332         }
333         return p.depth() < q.depth();
334 }
335
336
337 inline
338 bool operator>(DocIterator const & p, DocIterator const & q)
339 {
340         return q < p;
341 }
342
343
344 inline
345 bool operator<=(DocIterator const & p, DocIterator const & q)
346 {
347         return !(q < p);
348 }
349
350
351 inline
352 bool operator>=(DocIterator const & p, DocIterator const & q)
353 {
354         return !(p < q);
355 }
356
357
358 // The difference to a ('non stable') DocIterator is the removed
359 // (overwritten by 0...) part of the CursorSlice data items. So this thing
360 // is suitable for external storage, but not for iteration as such.
361
362 class StableDocIterator
363 {
364 public:
365         ///
366         StableDocIterator() {}
367         /// non-explicit intended
368         StableDocIterator(const DocIterator & it);
369         ///
370         DocIterator asDocIterator(Buffer * buf) const;
371         ///
372         size_t size() const { return data_.size(); }
373         ///  return the position within the paragraph
374         pos_type pos() const { return data_.back().pos(); }
375         ///  return the position within the paragraph
376         pos_type & pos() { return data_.back().pos(); }
377         ///
378         friend std::ostream &
379         operator<<(std::ostream & os, StableDocIterator const & cur);
380         ///
381         friend std::istream &
382         operator>>(std::istream & is, StableDocIterator & cur);
383         ///
384         friend bool
385         operator==(StableDocIterator const &, StableDocIterator const &);
386 private:
387         std::vector<CursorSlice> data_;
388 };
389
390 } // namespace lyx
391
392 #endif // DOCITERATOR_H