2004-03-01 17:12:09 +00:00
|
|
|
// -*- C++ -*-
|
|
|
|
/**
|
2007-04-26 04:41:58 +00:00
|
|
|
* \file DocIterator.h
|
2004-03-01 17:12:09 +00:00
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
* Licence details can be found in the file COPYING.
|
|
|
|
*
|
2008-11-14 15:58:50 +00:00
|
|
|
* \author André Pönitz
|
2004-03-01 17:12:09 +00:00
|
|
|
*
|
|
|
|
* Full author contact details are available in file CREDITS.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef DOCITERATOR_H
|
|
|
|
#define DOCITERATOR_H
|
|
|
|
|
2007-04-26 04:41:58 +00:00
|
|
|
#include "CursorSlice.h"
|
2004-03-01 17:12:09 +00:00
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
|
2006-10-21 00:16:43 +00:00
|
|
|
|
|
|
|
namespace lyx {
|
|
|
|
|
2008-11-17 11:46:07 +00:00
|
|
|
class DocIterator;
|
2007-11-28 22:12:03 +00:00
|
|
|
class LyXErr;
|
2004-03-18 12:53:43 +00:00
|
|
|
class MathAtom;
|
2004-03-01 17:12:09 +00:00
|
|
|
class Paragraph;
|
2007-11-28 22:12:03 +00:00
|
|
|
class Text;
|
2008-02-09 15:23:05 +00:00
|
|
|
class InsetIterator;
|
2004-03-01 17:12:09 +00:00
|
|
|
|
2008-11-17 11:46:07 +00:00
|
|
|
DocIterator doc_iterator_begin(Buffer const * buf, Inset const * inset = 0);
|
|
|
|
DocIterator doc_iterator_end(Buffer const * buf, Inset const * inset = 0);
|
2004-03-01 17:12:09 +00:00
|
|
|
|
2008-11-17 11:46:07 +00:00
|
|
|
|
|
|
|
class DocIterator
|
2004-03-01 17:12:09 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
/// type for cell number in inset
|
|
|
|
typedef CursorSlice::idx_type idx_type;
|
|
|
|
/// type for row indices
|
|
|
|
typedef CursorSlice::row_type row_type;
|
|
|
|
/// type for col indices
|
|
|
|
typedef CursorSlice::col_type col_type;
|
|
|
|
|
|
|
|
public:
|
2004-03-18 16:41:45 +00:00
|
|
|
///
|
2004-03-31 19:11:56 +00:00
|
|
|
DocIterator();
|
2008-11-17 11:46:07 +00:00
|
|
|
///
|
|
|
|
explicit DocIterator(Buffer *buf);
|
|
|
|
|
|
|
|
/// access to owning buffer
|
|
|
|
Buffer * buffer() const { return buffer_; }
|
|
|
|
/// access to owning buffer
|
|
|
|
void setBuffer(Buffer * buf) { buffer_ = buf; }
|
2004-03-18 16:41:45 +00:00
|
|
|
|
2010-01-09 14:47:47 +00:00
|
|
|
/// Clone this for given \p buffer.
|
|
|
|
/// \p buffer must be a clone of buffer_.
|
|
|
|
DocIterator clone(Buffer * buffer) const;
|
|
|
|
|
2005-07-06 07:28:16 +00:00
|
|
|
/// access slice at position \p i
|
2005-07-15 15:49:40 +00:00
|
|
|
CursorSlice const & operator[](size_t i) const { return slices_[i]; }
|
2005-07-06 07:28:16 +00:00
|
|
|
/// access slice at position \p i
|
2005-07-15 15:49:40 +00:00
|
|
|
CursorSlice & operator[](size_t i) { return slices_[i]; }
|
|
|
|
/// chop a few slices from the iterator
|
2005-02-08 13:18:05 +00:00
|
|
|
void resize(size_t i) { slices_.resize(i); }
|
|
|
|
|
2004-03-27 12:37:41 +00:00
|
|
|
/// is the iterator valid?
|
|
|
|
operator const void*() const { return empty() ? 0 : this; }
|
|
|
|
/// is this iterator invalid?
|
|
|
|
bool operator!() const { return empty(); }
|
|
|
|
|
2005-07-06 07:28:16 +00:00
|
|
|
/// does this iterator have any content?
|
2005-02-08 13:18:05 +00:00
|
|
|
bool empty() const { return slices_.empty(); }
|
2008-02-09 15:23:05 +00:00
|
|
|
/// is this the end position?
|
|
|
|
bool atEnd() const { return slices_.empty(); }
|
2005-02-08 13:18:05 +00:00
|
|
|
|
2004-03-01 17:12:09 +00:00
|
|
|
//
|
|
|
|
// access to slice at tip
|
|
|
|
//
|
|
|
|
/// access to tip
|
2005-02-08 13:18:05 +00:00
|
|
|
CursorSlice & top() { return slices_.back(); }
|
2004-03-01 17:12:09 +00:00
|
|
|
/// access to tip
|
2005-02-08 13:18:05 +00:00
|
|
|
CursorSlice const & top() const { return slices_.back(); }
|
2004-04-03 08:37:12 +00:00
|
|
|
/// access to outermost slice
|
2005-02-08 13:18:05 +00:00
|
|
|
CursorSlice & bottom() { return slices_.front(); }
|
2005-07-06 07:28:16 +00:00
|
|
|
/// access to outermost slice
|
2005-02-08 13:18:05 +00:00
|
|
|
CursorSlice const & bottom() const { return slices_.front(); }
|
2004-03-01 17:12:09 +00:00
|
|
|
/// how many nested insets do we have?
|
2005-02-08 13:18:05 +00:00
|
|
|
size_t depth() const { return slices_.size(); }
|
2004-03-01 17:12:09 +00:00
|
|
|
/// the containing inset
|
2007-04-29 13:39:47 +00:00
|
|
|
Inset & inset() const { return top().inset(); }
|
2004-03-01 17:12:09 +00:00
|
|
|
/// return the cell of the inset this cursor is in
|
2005-02-08 13:18:05 +00:00
|
|
|
idx_type idx() const { return top().idx(); }
|
2004-03-01 17:12:09 +00:00
|
|
|
/// return the cell of the inset this cursor is in
|
2005-02-08 13:18:05 +00:00
|
|
|
idx_type & idx() { return top().idx(); }
|
2004-03-01 17:12:09 +00:00
|
|
|
/// return the last possible cell in this inset
|
|
|
|
idx_type lastidx() const;
|
|
|
|
/// return the paragraph this cursor is in
|
2005-02-08 13:18:05 +00:00
|
|
|
pit_type pit() const { return top().pit(); }
|
2004-03-01 17:12:09 +00:00
|
|
|
/// return the paragraph this cursor is in
|
2005-02-08 13:18:05 +00:00
|
|
|
pit_type & pit() { return top().pit(); }
|
2004-03-01 17:12:09 +00:00
|
|
|
/// return the last possible paragraph in this inset
|
2004-11-24 21:53:46 +00:00
|
|
|
pit_type lastpit() const;
|
2004-03-01 17:12:09 +00:00
|
|
|
/// return the position within the paragraph
|
2005-02-08 13:18:05 +00:00
|
|
|
pos_type pos() const { return top().pos(); }
|
2004-03-01 17:12:09 +00:00
|
|
|
/// return the position within the paragraph
|
2005-02-08 13:18:05 +00:00
|
|
|
pos_type & pos() { return top().pos(); }
|
2004-03-01 17:12:09 +00:00
|
|
|
/// return the last position within the paragraph
|
|
|
|
pos_type lastpos() const;
|
|
|
|
|
|
|
|
/// return the number of embedded cells
|
|
|
|
size_t nargs() const;
|
|
|
|
/// return the number of embedded cells
|
|
|
|
size_t ncols() const;
|
|
|
|
/// return the number of embedded cells
|
|
|
|
size_t nrows() const;
|
|
|
|
/// return the grid row of the top cell
|
|
|
|
row_type row() const;
|
|
|
|
/// return the last row of the top grid
|
|
|
|
row_type lastrow() const { return nrows() - 1; }
|
|
|
|
/// return the grid column of the top cell
|
|
|
|
col_type col() const;
|
|
|
|
/// return the last column of the top grid
|
|
|
|
col_type lastcol() const { return ncols() - 1; }
|
|
|
|
/// the inset just behind the cursor
|
2007-08-23 21:13:01 +00:00
|
|
|
Inset * nextInset() const;
|
2004-03-01 17:12:09 +00:00
|
|
|
/// the inset just in front of the cursor
|
2007-08-23 21:13:01 +00:00
|
|
|
Inset * prevInset() const;
|
2005-07-15 15:49:40 +00:00
|
|
|
///
|
|
|
|
bool boundary() const { return boundary_; }
|
|
|
|
///
|
|
|
|
void boundary(bool b) { boundary_ = b; }
|
2004-03-01 17:12:09 +00:00
|
|
|
|
2007-01-08 15:30:17 +00:00
|
|
|
// the two methods below have been inlined out because of
|
|
|
|
// profiling results under linux when opening a document.
|
2007-01-08 10:50:15 +00:00
|
|
|
/// are we in mathed?.
|
2007-01-08 15:30:17 +00:00
|
|
|
bool inMathed() const
|
|
|
|
{ return !empty() && inset().inMathed(); }
|
2007-01-08 10:50:15 +00:00
|
|
|
/// are we in texted?.
|
2007-01-08 15:30:17 +00:00
|
|
|
bool inTexted() const
|
|
|
|
{ return !empty() && !inset().inMathed(); }
|
2008-11-16 00:12:21 +00:00
|
|
|
/// are we in regexp-mode ?
|
|
|
|
bool inRegexped() const;
|
2004-03-01 17:12:09 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// math-specific part
|
|
|
|
//
|
|
|
|
/// return the mathed cell this cursor is in
|
2007-08-23 21:13:01 +00:00
|
|
|
MathData & cell() const;
|
2004-03-01 17:12:09 +00:00
|
|
|
/// the mathatom left of the cursor
|
2007-08-23 21:13:01 +00:00
|
|
|
MathAtom & prevAtom() const;
|
2004-03-01 17:12:09 +00:00
|
|
|
/// the mathatom right of the cursor
|
2007-08-23 21:13:01 +00:00
|
|
|
MathAtom & nextAtom() const;
|
2004-03-01 17:12:09 +00:00
|
|
|
|
|
|
|
// text-specific part
|
|
|
|
//
|
2007-04-30 11:46:02 +00:00
|
|
|
/// the paragraph we're in in text mode.
|
|
|
|
/// \warning only works within text!
|
2007-08-23 21:13:01 +00:00
|
|
|
Paragraph & paragraph() const;
|
2007-04-30 11:46:02 +00:00
|
|
|
/// the paragraph we're in in any case.
|
2007-09-14 12:52:39 +00:00
|
|
|
/// This method will give the containing paragraph even
|
|
|
|
/// if not in text mode (ex: in mathed).
|
2007-08-23 21:13:01 +00:00
|
|
|
Paragraph & innerParagraph() const;
|
2007-09-14 12:52:39 +00:00
|
|
|
/// return the inner text slice.
|
|
|
|
CursorSlice const & innerTextSlice() const;
|
2005-01-31 16:29:48 +00:00
|
|
|
///
|
2007-08-23 21:13:01 +00:00
|
|
|
Text * text() const;
|
2005-11-24 16:22:39 +00:00
|
|
|
/// the containing inset or the cell, respectively
|
2007-04-29 13:39:47 +00:00
|
|
|
Inset * realInset() const;
|
2004-03-01 17:12:09 +00:00
|
|
|
///
|
2007-04-29 13:39:47 +00:00
|
|
|
Inset * innerInsetOfType(int code) const;
|
2004-03-01 17:12:09 +00:00
|
|
|
///
|
2007-08-23 21:13:01 +00:00
|
|
|
Text * innerText() const;
|
2004-03-01 17:12:09 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// elementary moving
|
|
|
|
//
|
2006-01-28 09:46:58 +00:00
|
|
|
/**
|
|
|
|
* move on one logical position, descend into nested insets
|
2008-01-12 23:08:21 +00:00
|
|
|
* including collapsed insets
|
2006-01-28 09:46:58 +00:00
|
|
|
*/
|
2008-01-12 23:08:21 +00:00
|
|
|
void forwardPos();
|
|
|
|
/**
|
|
|
|
* move on one logical position, descend into nested insets
|
|
|
|
* skip collapsed insets
|
|
|
|
*/
|
|
|
|
void forwardPosIgnoreCollapsed();
|
2004-03-25 09:16:36 +00:00
|
|
|
/// move on one physical character or inset
|
|
|
|
void forwardChar();
|
2004-03-01 17:12:09 +00:00
|
|
|
/// move on one paragraph
|
|
|
|
void forwardPar();
|
|
|
|
/// move on one inset
|
|
|
|
void forwardInset();
|
2004-03-25 09:16:36 +00:00
|
|
|
/// move backward one logical position
|
|
|
|
void backwardPos();
|
|
|
|
/// move backward one physical character or inset
|
|
|
|
void backwardChar();
|
|
|
|
/// move backward one paragraph
|
|
|
|
void backwardPar();
|
|
|
|
/// move backward one inset
|
2007-02-07 21:49:00 +00:00
|
|
|
/// FIXME: This is not implemented!
|
|
|
|
//void backwardInset();
|
2004-03-25 09:16:36 +00:00
|
|
|
|
2004-08-14 21:03:55 +00:00
|
|
|
/// are we some 'extension' (i.e. deeper nested) of the given iterator
|
|
|
|
bool hasPart(DocIterator const & it) const;
|
|
|
|
|
2004-03-08 21:14:45 +00:00
|
|
|
/// output
|
|
|
|
friend std::ostream &
|
2004-03-31 19:11:56 +00:00
|
|
|
operator<<(std::ostream & os, DocIterator const & cur);
|
2007-11-28 22:12:03 +00:00
|
|
|
friend LyXErr & operator<<(LyXErr & os, DocIterator const & it);
|
2005-07-15 15:49:40 +00:00
|
|
|
///
|
2005-02-08 13:18:05 +00:00
|
|
|
friend bool operator==(DocIterator const &, DocIterator const &);
|
2007-05-15 17:07:55 +00:00
|
|
|
friend bool operator<(DocIterator const &, DocIterator const &);
|
|
|
|
friend bool operator>(DocIterator const &, DocIterator const &);
|
|
|
|
friend bool operator<=(DocIterator const &, DocIterator const &);
|
2005-07-15 15:49:40 +00:00
|
|
|
///
|
2005-02-08 13:18:05 +00:00
|
|
|
friend class StableDocIterator;
|
2005-08-03 20:21:11 +00:00
|
|
|
//protected:
|
2005-07-15 15:49:40 +00:00
|
|
|
///
|
2005-02-08 13:18:05 +00:00
|
|
|
void clear() { slices_.clear(); }
|
2005-07-15 15:49:40 +00:00
|
|
|
///
|
|
|
|
void push_back(CursorSlice const & sl) { slices_.push_back(sl); }
|
|
|
|
///
|
|
|
|
void pop_back() { slices_.pop_back(); }
|
2005-10-25 09:14:11 +00:00
|
|
|
/// recompute the inset parts of the cursor from the document data
|
2007-04-29 13:39:47 +00:00
|
|
|
void updateInsets(Inset * inset);
|
2007-05-25 23:17:24 +00:00
|
|
|
/// fix DocIterator in circumstances that should never happen.
|
|
|
|
/// \return true if the DocIterator was fixed.
|
|
|
|
bool fixIfBroken();
|
2005-10-25 09:14:11 +00:00
|
|
|
|
2007-11-01 10:56:18 +00:00
|
|
|
/// find index of CursorSlice with &cell() == &cell (or -1 if not found)
|
2008-02-09 22:05:24 +00:00
|
|
|
int find(MathData const & cell) const;
|
2007-11-01 10:56:18 +00:00
|
|
|
/// find index of CursorSlice with inset() == inset (or -1 of not found)
|
2008-02-26 19:19:59 +00:00
|
|
|
int find(Inset const * inset) const;
|
2008-02-09 22:05:24 +00:00
|
|
|
/// cut off CursorSlices with index > above and store cut off slices in cut.
|
|
|
|
void cutOff(int above, std::vector<CursorSlice> & cut);
|
2007-11-01 10:56:18 +00:00
|
|
|
/// cut off CursorSlices with index > above
|
2008-02-09 22:05:24 +00:00
|
|
|
void cutOff(int above);
|
2007-11-01 10:56:18 +00:00
|
|
|
/// push CursorSlices on top
|
|
|
|
void append(std::vector<CursorSlice> const & x);
|
|
|
|
/// push one CursorSlice on top and set its index and position
|
|
|
|
void append(idx_type idx, pos_type pos);
|
|
|
|
|
2004-03-30 08:18:09 +00:00
|
|
|
private:
|
2008-02-09 15:23:05 +00:00
|
|
|
friend class InsetIterator;
|
2008-11-17 11:46:07 +00:00
|
|
|
friend DocIterator doc_iterator_begin(Buffer const * buf, Inset const * inset);
|
|
|
|
friend DocIterator doc_iterator_end(Buffer const * buf, Inset const * inset);
|
2008-02-09 15:23:05 +00:00
|
|
|
///
|
2008-11-17 11:46:07 +00:00
|
|
|
explicit DocIterator(Buffer * buf, Inset * inset);
|
2005-07-15 15:49:40 +00:00
|
|
|
/**
|
2008-01-31 22:50:16 +00:00
|
|
|
* Normally, when the cursor is at position i, it is painted *before*
|
|
|
|
* the character at position i. However, what if we want the cursor
|
|
|
|
* painted *after* position i? That's what boundary_ is for: if
|
|
|
|
* boundary_==true, the cursor is painted *after* position i-1, instead
|
|
|
|
* of before position i.
|
2005-07-15 15:49:40 +00:00
|
|
|
*
|
2008-01-31 22:50:16 +00:00
|
|
|
* Note 1: Usually, after i-1 or before i are actually the same place!
|
|
|
|
* However, this is not the case when i-1 and i are not painted
|
|
|
|
* contiguously, and in these cases we sometimes do want to have control
|
|
|
|
* over whether to paint before i or after i-1.
|
|
|
|
* Some concrete examples of where this happens:
|
|
|
|
* a. i-1 at the end of one row, i at the beginning of next row
|
|
|
|
* b. in bidi text, at transitions between RTL and LTR or vice versa
|
|
|
|
*
|
|
|
|
* Note 2: Why i and i-1? Why, if boundary_==false means: *before* i,
|
|
|
|
* couldn't boundary_==true mean: *after* i?
|
|
|
|
* Well, the reason is this: cursor position is not used only for
|
|
|
|
* painting the cursor, but it also affects other things, for example:
|
|
|
|
* where the next insertion will be placed (it is inserted at the current
|
|
|
|
* position, pushing anything at the current position and beyond forward).
|
|
|
|
* Now, when the current position is i and boundary_==true, insertion would
|
|
|
|
* happen *before* i. If the cursor, however, were painted *after* i, that
|
|
|
|
* would be very unnatural...
|
2005-07-15 15:49:40 +00:00
|
|
|
*/
|
|
|
|
bool boundary_;
|
|
|
|
///
|
2008-11-17 11:46:07 +00:00
|
|
|
std::vector<CursorSlice> const & internalData() const { return slices_; }
|
2005-07-15 15:49:40 +00:00
|
|
|
///
|
2005-02-08 13:18:05 +00:00
|
|
|
std::vector<CursorSlice> slices_;
|
2005-07-15 15:49:40 +00:00
|
|
|
///
|
2007-04-29 13:39:47 +00:00
|
|
|
Inset * inset_;
|
2008-11-17 11:46:07 +00:00
|
|
|
///
|
|
|
|
Buffer * buffer_;
|
2004-03-01 17:12:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2007-11-28 22:12:03 +00:00
|
|
|
inline bool operator==(DocIterator const & di1, DocIterator const & di2)
|
2005-02-08 13:18:05 +00:00
|
|
|
{
|
|
|
|
return di1.slices_ == di2.slices_;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-11-28 22:12:03 +00:00
|
|
|
inline bool operator!=(DocIterator const & di1, DocIterator const & di2)
|
2005-02-08 13:18:05 +00:00
|
|
|
{
|
|
|
|
return !(di1 == di2);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-12-21 20:42:46 +00:00
|
|
|
inline
|
|
|
|
bool operator<(DocIterator const & p, DocIterator const & q)
|
|
|
|
{
|
|
|
|
size_t depth = std::min(p.depth(), q.depth());
|
|
|
|
for (size_t i = 0 ; i < depth ; ++i) {
|
|
|
|
if (p[i] != q[i])
|
|
|
|
return p[i] < q[i];
|
|
|
|
}
|
|
|
|
return p.depth() < q.depth();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline
|
|
|
|
bool operator>(DocIterator const & p, DocIterator const & q)
|
|
|
|
{
|
|
|
|
return q < p;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline
|
|
|
|
bool operator<=(DocIterator const & p, DocIterator const & q)
|
|
|
|
{
|
|
|
|
return !(q < p);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-08-12 09:51:00 +00:00
|
|
|
inline
|
|
|
|
bool operator>=(DocIterator const & p, DocIterator const & q)
|
|
|
|
{
|
|
|
|
return !(p < q);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-03-31 19:11:56 +00:00
|
|
|
// The difference to a ('non stable') DocIterator is the removed
|
2007-01-04 22:01:43 +00:00
|
|
|
// (overwritten by 0...) part of the CursorSlice data items. So this thing
|
2004-03-08 21:14:45 +00:00
|
|
|
// is suitable for external storage, but not for iteration as such.
|
|
|
|
|
2008-11-17 11:46:07 +00:00
|
|
|
class StableDocIterator
|
|
|
|
{
|
2004-03-08 21:14:45 +00:00
|
|
|
public:
|
|
|
|
///
|
2004-03-31 19:11:56 +00:00
|
|
|
StableDocIterator() {}
|
2004-03-08 21:14:45 +00:00
|
|
|
/// non-explicit intended
|
2004-03-31 19:11:56 +00:00
|
|
|
StableDocIterator(const DocIterator & it);
|
2004-03-08 21:14:45 +00:00
|
|
|
///
|
2008-11-17 11:46:07 +00:00
|
|
|
DocIterator asDocIterator(Buffer * buf) const;
|
2004-03-08 21:14:45 +00:00
|
|
|
///
|
|
|
|
size_t size() const { return data_.size(); }
|
2005-10-13 17:20:30 +00:00
|
|
|
/// return the position within the paragraph
|
|
|
|
pos_type pos() const { return data_.back().pos(); }
|
|
|
|
/// return the position within the paragraph
|
|
|
|
pos_type & pos() { return data_.back().pos(); }
|
2004-03-08 21:14:45 +00:00
|
|
|
///
|
|
|
|
friend std::ostream &
|
2004-03-31 19:11:56 +00:00
|
|
|
operator<<(std::ostream & os, StableDocIterator const & cur);
|
2004-03-08 21:14:45 +00:00
|
|
|
///
|
|
|
|
friend std::istream &
|
2004-03-31 19:11:56 +00:00
|
|
|
operator>>(std::istream & is, StableDocIterator & cur);
|
2005-07-01 12:36:28 +00:00
|
|
|
///
|
|
|
|
friend bool
|
|
|
|
operator==(StableDocIterator const &, StableDocIterator const &);
|
2004-03-08 21:14:45 +00:00
|
|
|
private:
|
|
|
|
std::vector<CursorSlice> data_;
|
|
|
|
};
|
|
|
|
|
2006-10-21 00:16:43 +00:00
|
|
|
} // namespace lyx
|
|
|
|
|
2008-11-17 11:46:07 +00:00
|
|
|
#endif // DOCITERATOR_H
|