2004-03-01 17:12:09 +00:00
|
|
|
|
|
|
|
#include "dociterator.h"
|
|
|
|
|
2004-03-11 17:16:38 +00:00
|
|
|
#include "BufferView.h"
|
2004-03-01 17:12:09 +00:00
|
|
|
#include "debug.h"
|
|
|
|
#include "lyxtext.h"
|
|
|
|
#include "lyxrow.h"
|
|
|
|
#include "paragraph.h"
|
|
|
|
|
|
|
|
#include "mathed/math_data.h"
|
|
|
|
#include "mathed/math_inset.h"
|
|
|
|
|
|
|
|
#include <boost/assert.hpp>
|
|
|
|
|
|
|
|
|
2004-03-11 17:16:38 +00:00
|
|
|
|
|
|
|
DocumentIterator::DocumentIterator()
|
|
|
|
: bv_(0)
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
|
|
DocumentIterator::DocumentIterator(BufferView & bv)
|
|
|
|
: std::vector<CursorSlice>(1), bv_(&bv)
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
2004-03-01 17:12:09 +00:00
|
|
|
InsetBase * DocumentIterator::nextInset()
|
|
|
|
{
|
|
|
|
if (pos() == lastpos())
|
|
|
|
return 0;
|
|
|
|
if (inMathed())
|
|
|
|
return nextAtom().nucleus();
|
|
|
|
return paragraph().isInset(pos()) ? paragraph().getInset(pos()) : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
InsetBase * DocumentIterator::prevInset()
|
|
|
|
{
|
|
|
|
if (pos() == 0)
|
|
|
|
return 0;
|
|
|
|
if (inMathed())
|
|
|
|
return prevAtom().nucleus();
|
|
|
|
return paragraph().isInset(pos() - 1) ? paragraph().getInset(pos() - 1) : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
InsetBase const * DocumentIterator::prevInset() const
|
|
|
|
{
|
|
|
|
if (pos() == 0)
|
|
|
|
return 0;
|
|
|
|
if (inMathed())
|
|
|
|
return prevAtom().nucleus();
|
|
|
|
return paragraph().isInset(pos() - 1) ? paragraph().getInset(pos() - 1) : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
MathAtom const & DocumentIterator::prevAtom() const
|
|
|
|
{
|
|
|
|
BOOST_ASSERT(pos() > 0);
|
|
|
|
return cell()[pos() - 1];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
MathAtom & DocumentIterator::prevAtom()
|
|
|
|
{
|
|
|
|
BOOST_ASSERT(pos() > 0);
|
|
|
|
return cell()[pos() - 1];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
MathAtom const & DocumentIterator::nextAtom() const
|
|
|
|
{
|
|
|
|
BOOST_ASSERT(pos() < lastpos());
|
|
|
|
return cell()[pos()];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
MathAtom & DocumentIterator::nextAtom()
|
|
|
|
{
|
|
|
|
BOOST_ASSERT(pos() < lastpos());
|
|
|
|
return cell()[pos()];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
LyXText * DocumentIterator::text() const
|
|
|
|
{
|
2004-03-11 17:16:38 +00:00
|
|
|
return size() > 1 ? top().text() : bv().text();
|
2004-03-01 17:12:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Paragraph & DocumentIterator::paragraph()
|
|
|
|
{
|
|
|
|
BOOST_ASSERT(inTexted());
|
2004-03-11 17:16:38 +00:00
|
|
|
return size() > 1 ? top().paragraph() : *bv().text()->getPar(par());
|
2004-03-01 17:12:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Paragraph const & DocumentIterator::paragraph() const
|
|
|
|
{
|
|
|
|
BOOST_ASSERT(inTexted());
|
2004-03-11 17:16:38 +00:00
|
|
|
return size() > 1 ? top().paragraph() : *bv().text()->getPar(par());
|
2004-03-01 17:12:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Row & DocumentIterator::textRow()
|
|
|
|
{
|
|
|
|
return *paragraph().getRow(pos());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Row const & DocumentIterator::textRow() const
|
|
|
|
{
|
|
|
|
return *paragraph().getRow(pos());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DocumentIterator::par_type DocumentIterator::lastpar() const
|
|
|
|
{
|
|
|
|
return inMathed() ? 0 : text()->paragraphs().size() - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DocumentIterator::pos_type DocumentIterator::lastpos() const
|
|
|
|
{
|
|
|
|
return inMathed() ? cell().size() : paragraph().size();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DocumentIterator::row_type DocumentIterator::crow() const
|
|
|
|
{
|
|
|
|
return paragraph().row(pos());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DocumentIterator::row_type DocumentIterator::lastcrow() const
|
|
|
|
{
|
|
|
|
return paragraph().rows.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DocumentIterator::idx_type DocumentIterator::lastidx() const
|
|
|
|
{
|
2004-03-11 17:16:38 +00:00
|
|
|
return size() > 1 ? top().lastidx() : 0;
|
2004-03-01 17:12:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
size_t DocumentIterator::nargs() const
|
|
|
|
{
|
|
|
|
// assume 1x1 grid for main text
|
2004-03-11 17:16:38 +00:00
|
|
|
return size() > 1 ? top().nargs() : 1;
|
2004-03-01 17:12:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
size_t DocumentIterator::ncols() const
|
|
|
|
{
|
|
|
|
// assume 1x1 grid for main text
|
2004-03-11 17:16:38 +00:00
|
|
|
return size() > 1 ? top().ncols() : 1;
|
2004-03-01 17:12:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
size_t DocumentIterator::nrows() const
|
|
|
|
{
|
|
|
|
// assume 1x1 grid for main text
|
2004-03-11 17:16:38 +00:00
|
|
|
return size() > 1 ? top().nrows() : 1;
|
2004-03-01 17:12:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DocumentIterator::row_type DocumentIterator::row() const
|
|
|
|
{
|
2004-03-11 17:16:38 +00:00
|
|
|
return size() > 1 ? top().row() : 0;
|
2004-03-01 17:12:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DocumentIterator::col_type DocumentIterator::col() const
|
|
|
|
{
|
2004-03-11 17:16:38 +00:00
|
|
|
return size() > 1 ? top().col() : 0;
|
2004-03-01 17:12:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
MathArray const & DocumentIterator::cell() const
|
|
|
|
{
|
2004-03-11 17:16:38 +00:00
|
|
|
BOOST_ASSERT(size() > 1);
|
2004-03-01 17:12:09 +00:00
|
|
|
return top().cell();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
MathArray & DocumentIterator::cell()
|
|
|
|
{
|
2004-03-11 17:16:38 +00:00
|
|
|
BOOST_ASSERT(size() > 1);
|
2004-03-01 17:12:09 +00:00
|
|
|
return top().cell();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool DocumentIterator::inMathed() const
|
|
|
|
{
|
2004-03-11 17:16:38 +00:00
|
|
|
return size() > 1 && inset()->inMathed();
|
2004-03-01 17:12:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool DocumentIterator::inTexted() const
|
|
|
|
{
|
2004-03-11 17:16:38 +00:00
|
|
|
return !inMathed();
|
2004-03-01 17:12:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
LyXText * DocumentIterator::innerText() const
|
|
|
|
{
|
|
|
|
BOOST_ASSERT(!empty());
|
2004-03-11 17:16:38 +00:00
|
|
|
if (size() > 1) {
|
|
|
|
// go up until first non-0 text is hit
|
|
|
|
// (innermost text is 0 in mathed)
|
|
|
|
for (int i = size() - 1; i >= 1; --i)
|
|
|
|
if (operator[](i).text())
|
|
|
|
return operator[](i).text();
|
|
|
|
}
|
|
|
|
return bv().text();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
CursorSlice const & DocumentIterator::innerTextSlice() const
|
|
|
|
{
|
|
|
|
BOOST_ASSERT(!empty());
|
|
|
|
if (size() > 1) {
|
|
|
|
// go up until first non-0 text is hit
|
|
|
|
// (innermost text is 0 in mathed)
|
|
|
|
for (int i = size() - 1; i >= 1; --i)
|
|
|
|
if (operator[](i).text())
|
|
|
|
return operator[](i);
|
|
|
|
}
|
|
|
|
return operator[](0);
|
2004-03-01 17:12:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
InsetBase * DocumentIterator::innerInsetOfType(int code) const
|
|
|
|
{
|
2004-03-11 17:16:38 +00:00
|
|
|
for (int i = size() - 1; i >= 1; --i)
|
2004-03-01 17:12:09 +00:00
|
|
|
if (operator[](i).inset_->lyxCode() == code)
|
|
|
|
return operator[](i).inset_;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void DocumentIterator::forwardPos()
|
|
|
|
{
|
|
|
|
CursorSlice & top = back();
|
|
|
|
//lyxerr << "XXX\n" << *this << std::endl;
|
|
|
|
|
|
|
|
// move into an inset to the right if possible
|
|
|
|
InsetBase * n = 0;
|
|
|
|
if (top.pos() != lastpos()) {
|
|
|
|
// this is impossible for pos() == size()
|
|
|
|
if (inMathed()) {
|
|
|
|
n = (top.cell().begin() + top.pos())->nucleus();
|
|
|
|
} else {
|
|
|
|
if (paragraph().isInset(top.pos()))
|
|
|
|
n = paragraph().getInset(top.pos());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (n && n->isActive()) {
|
|
|
|
//lyxerr << "... descend" << std::endl;
|
2004-03-11 17:16:38 +00:00
|
|
|
push_back(CursorSlice(n));
|
2004-03-01 17:12:09 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// otherwise move on one cell back if possible
|
|
|
|
if (top.pos() < lastpos()) {
|
|
|
|
//lyxerr << "... next pos" << std::endl;
|
|
|
|
++top.pos();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
//lyxerr << "... no next pos" << std::endl;
|
|
|
|
|
|
|
|
// otherwise move on one cell back if possible
|
|
|
|
if (top.par() < lastpar()) {
|
|
|
|
//lyxerr << "... next par" << std::endl;
|
|
|
|
++top.par();
|
|
|
|
top.pos() = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
//lyxerr << "... no next par" << std::endl;
|
|
|
|
|
|
|
|
// otherwise try to move on one cell if possible
|
|
|
|
// [stupid hack for necessary for MathScriptInset]
|
|
|
|
while (top.idx() < lastidx()) {
|
|
|
|
//lyxerr << "... next idx" << std::endl;
|
|
|
|
++top.idx();
|
|
|
|
top.par() = 0;
|
|
|
|
top.pos() = 0;
|
2004-03-11 17:16:38 +00:00
|
|
|
if (top.inset() && top.inset()->validCell(top.idx())) {
|
2004-03-01 17:12:09 +00:00
|
|
|
//lyxerr << " ... ok" << std::endl;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//lyxerr << "... no next idx" << std::endl;
|
|
|
|
|
|
|
|
// otherwise leave inset an jump over inset as a whole
|
|
|
|
pop_back();
|
|
|
|
// 'top' is invalid now...
|
|
|
|
if (size())
|
|
|
|
++back().pos_;
|
|
|
|
//else
|
|
|
|
// lyxerr << "... no slice left" << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void DocumentIterator::forwardPar()
|
|
|
|
{
|
|
|
|
CursorSlice & top = back();
|
|
|
|
lyxerr << "XXX " << *this << std::endl;
|
|
|
|
|
|
|
|
// move into an inset to the right if possible
|
|
|
|
InsetBase * n = 0;
|
|
|
|
if (top.pos() != lastpos()) {
|
|
|
|
// this is impossible for pos() == size()
|
|
|
|
if (inMathed()) {
|
|
|
|
n = (top.cell().begin() + top.pos())->nucleus();
|
|
|
|
} else {
|
|
|
|
if (paragraph().isInset(top.pos()))
|
|
|
|
n = paragraph().getInset(top.pos());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (n && n->isActive()) {
|
|
|
|
lyxerr << "... descend" << std::endl;
|
2004-03-11 17:16:38 +00:00
|
|
|
push_back(CursorSlice(n));
|
2004-03-01 17:12:09 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// otherwise move on one cell back if possible
|
|
|
|
if (top.pos() < lastpos()) {
|
|
|
|
lyxerr << "... next pos" << std::endl;
|
|
|
|
++top.pos();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// otherwise move on one cell back if possible
|
|
|
|
if (top.par() < lastpar()) {
|
|
|
|
lyxerr << "... next par" << std::endl;
|
|
|
|
++top.par();
|
|
|
|
top.pos() = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// otherwise try to move on one cell if possible
|
|
|
|
// [stupid hack for necessary for MathScriptInset]
|
|
|
|
while (top.idx() < top.lastidx()) {
|
|
|
|
lyxerr << "... next idx"
|
|
|
|
<< " was: " << top.idx() << " max: " << top.lastidx() << std::endl;
|
|
|
|
++top.idx();
|
|
|
|
top.par() = 0;
|
|
|
|
top.pos() = 0;
|
2004-03-11 17:16:38 +00:00
|
|
|
if (top.inset() && top.inset()->validCell(top.idx())) {
|
2004-03-01 17:12:09 +00:00
|
|
|
lyxerr << " ... ok" << std::endl;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// otherwise leave inset an jump over inset as a whole
|
|
|
|
pop_back();
|
|
|
|
// 'top' is invalid now...
|
|
|
|
if (size())
|
|
|
|
++back().pos_;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-03-11 17:16:38 +00:00
|
|
|
DocumentIterator bufferBegin(BufferView & bv)
|
|
|
|
{
|
|
|
|
return DocumentIterator(bv);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DocumentIterator bufferEnd()
|
|
|
|
{
|
|
|
|
return DocumentIterator();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DocumentIterator insetBegin(BufferView & bv, InsetBase * p)
|
2004-03-01 17:12:09 +00:00
|
|
|
{
|
2004-03-11 17:16:38 +00:00
|
|
|
DocumentIterator it(bv);
|
|
|
|
it.back() = CursorSlice(p);
|
2004-03-01 17:12:09 +00:00
|
|
|
return it;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DocumentIterator insetEnd()
|
|
|
|
{
|
|
|
|
return DocumentIterator();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-03-08 21:14:45 +00:00
|
|
|
std::ostream & operator<<(std::ostream & os, DocumentIterator const & dit)
|
2004-03-01 17:12:09 +00:00
|
|
|
{
|
2004-03-11 17:16:38 +00:00
|
|
|
os << "bv: " << &dit.bv() << "\n";
|
2004-03-08 21:14:45 +00:00
|
|
|
for (size_t i = 0, n = dit.size(); i != n; ++i)
|
|
|
|
os << " " << dit.operator[](i) << "\n";
|
2004-03-01 17:12:09 +00:00
|
|
|
return os;
|
|
|
|
}
|
2004-03-08 21:14:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
StableDocumentIterator::StableDocumentIterator(const DocumentIterator & dit)
|
|
|
|
{
|
|
|
|
data_ = dit;
|
|
|
|
for (size_t i = 0, n = data_.size(); i != n; ++i)
|
|
|
|
data_[i].inset_ = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DocumentIterator
|
2004-03-11 17:16:38 +00:00
|
|
|
StableDocumentIterator::asDocumentIterator(BufferView & bv) const
|
2004-03-08 21:14:45 +00:00
|
|
|
{
|
|
|
|
// this function re-creates the cache of inset pointers
|
|
|
|
//lyxerr << "converting:\n" << *this << std::endl;
|
2004-03-11 17:16:38 +00:00
|
|
|
DocumentIterator dit(bv);
|
|
|
|
dit.clear();
|
|
|
|
InsetBase * inset = 0;
|
2004-03-08 21:14:45 +00:00
|
|
|
for (size_t i = 0, n = data_.size(); i != n; ++i) {
|
|
|
|
dit.push_back(data_[i]);
|
|
|
|
dit.back().inset_ = inset;
|
|
|
|
if (i + 1 != n)
|
|
|
|
inset = dit.nextInset();
|
|
|
|
}
|
|
|
|
//lyxerr << "convert:\n" << *this << " to:\n" << dit << std::endl;
|
|
|
|
return dit;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::ostream & operator<<(std::ostream & os, StableDocumentIterator const & dit)
|
|
|
|
{
|
|
|
|
for (size_t i = 0, n = dit.data_.size(); i != n; ++i)
|
|
|
|
os << " " << dit.data_[i] << "\n";
|
|
|
|
return os;
|
|
|
|
}
|
|
|
|
|