2004-03-01 17:12:09 +00:00
|
|
|
|
|
|
|
#include "dociterator.h"
|
|
|
|
|
|
|
|
#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-25 09:16:36 +00:00
|
|
|
using std::endl;
|
|
|
|
|
2004-03-01 17:12:09 +00:00
|
|
|
|
2004-03-18 16:41:45 +00:00
|
|
|
DocumentIterator::DocumentIterator()
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
|
|
DocumentIterator::DocumentIterator(InsetBase & inset)
|
|
|
|
{
|
|
|
|
push_back(CursorSlice(inset));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-03-01 17:12:09 +00:00
|
|
|
InsetBase * DocumentIterator::nextInset()
|
|
|
|
{
|
2004-03-25 09:16:36 +00:00
|
|
|
BOOST_ASSERT(!empty());
|
2004-03-01 17:12:09 +00:00
|
|
|
if (pos() == lastpos())
|
|
|
|
return 0;
|
|
|
|
if (inMathed())
|
|
|
|
return nextAtom().nucleus();
|
|
|
|
return paragraph().isInset(pos()) ? paragraph().getInset(pos()) : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
InsetBase * DocumentIterator::prevInset()
|
|
|
|
{
|
2004-03-25 09:16:36 +00:00
|
|
|
BOOST_ASSERT(!empty());
|
2004-03-01 17:12:09 +00:00
|
|
|
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
|
|
|
|
{
|
2004-03-25 09:16:36 +00:00
|
|
|
BOOST_ASSERT(!empty());
|
2004-03-01 17:12:09 +00:00
|
|
|
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
|
|
|
|
{
|
2004-03-25 09:16:36 +00:00
|
|
|
BOOST_ASSERT(!empty());
|
2004-03-01 17:12:09 +00:00
|
|
|
BOOST_ASSERT(pos() > 0);
|
|
|
|
return cell()[pos() - 1];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
MathAtom & DocumentIterator::prevAtom()
|
|
|
|
{
|
2004-03-25 09:16:36 +00:00
|
|
|
BOOST_ASSERT(!empty());
|
2004-03-01 17:12:09 +00:00
|
|
|
BOOST_ASSERT(pos() > 0);
|
|
|
|
return cell()[pos() - 1];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
MathAtom const & DocumentIterator::nextAtom() const
|
|
|
|
{
|
2004-03-25 09:16:36 +00:00
|
|
|
BOOST_ASSERT(!empty());
|
2004-03-01 17:12:09 +00:00
|
|
|
BOOST_ASSERT(pos() < lastpos());
|
|
|
|
return cell()[pos()];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
MathAtom & DocumentIterator::nextAtom()
|
|
|
|
{
|
2004-03-25 09:16:36 +00:00
|
|
|
BOOST_ASSERT(!empty());
|
2004-03-01 17:12:09 +00:00
|
|
|
BOOST_ASSERT(pos() < lastpos());
|
|
|
|
return cell()[pos()];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
LyXText * DocumentIterator::text() const
|
|
|
|
{
|
2004-03-25 09:16:36 +00:00
|
|
|
BOOST_ASSERT(!empty());
|
2004-03-18 12:53:43 +00:00
|
|
|
return top().text();
|
2004-03-01 17:12:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Paragraph & DocumentIterator::paragraph()
|
|
|
|
{
|
|
|
|
BOOST_ASSERT(inTexted());
|
2004-03-18 12:53:43 +00:00
|
|
|
return top().paragraph();
|
2004-03-01 17:12:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Paragraph const & DocumentIterator::paragraph() const
|
|
|
|
{
|
|
|
|
BOOST_ASSERT(inTexted());
|
2004-03-18 12:53:43 +00:00
|
|
|
return top().paragraph();
|
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-18 12:53:43 +00:00
|
|
|
return top().lastidx();
|
2004-03-01 17:12:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
size_t DocumentIterator::nargs() const
|
|
|
|
{
|
|
|
|
// assume 1x1 grid for main text
|
2004-03-18 12:53:43 +00:00
|
|
|
return top().nargs();
|
2004-03-01 17:12:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
size_t DocumentIterator::ncols() const
|
|
|
|
{
|
|
|
|
// assume 1x1 grid for main text
|
2004-03-18 12:53:43 +00:00
|
|
|
return top().ncols();
|
2004-03-01 17:12:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
size_t DocumentIterator::nrows() const
|
|
|
|
{
|
|
|
|
// assume 1x1 grid for main text
|
2004-03-18 12:53:43 +00:00
|
|
|
return top().nrows();
|
2004-03-01 17:12:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DocumentIterator::row_type DocumentIterator::row() const
|
|
|
|
{
|
2004-03-18 12:53:43 +00:00
|
|
|
return top().row();
|
2004-03-01 17:12:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DocumentIterator::col_type DocumentIterator::col() const
|
|
|
|
{
|
2004-03-18 12:53:43 +00:00
|
|
|
return top().col();
|
2004-03-01 17:12:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
MathArray const & DocumentIterator::cell() const
|
|
|
|
{
|
2004-03-18 12:53:43 +00:00
|
|
|
BOOST_ASSERT(inMathed());
|
2004-03-01 17:12:09 +00:00
|
|
|
return top().cell();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
MathArray & DocumentIterator::cell()
|
|
|
|
{
|
2004-03-18 12:53:43 +00:00
|
|
|
BOOST_ASSERT(inMathed());
|
2004-03-01 17:12:09 +00:00
|
|
|
return top().cell();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool DocumentIterator::inMathed() const
|
|
|
|
{
|
2004-03-18 12:53:43 +00:00
|
|
|
return !empty() && inset().inMathed();
|
2004-03-01 17:12:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool DocumentIterator::inTexted() const
|
|
|
|
{
|
2004-03-18 12:53:43 +00:00
|
|
|
return !empty() && !inset().inMathed();
|
2004-03-01 17:12:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
LyXText * DocumentIterator::innerText() const
|
|
|
|
{
|
|
|
|
BOOST_ASSERT(!empty());
|
2004-03-18 12:53:43 +00:00
|
|
|
// go up until first non-0 text is hit
|
|
|
|
// (innermost text is 0 in mathed)
|
|
|
|
for (int i = size() - 1; i >= 0; --i)
|
|
|
|
if (operator[](i).text())
|
|
|
|
return operator[](i).text();
|
|
|
|
return 0;
|
2004-03-01 17:12:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
InsetBase * DocumentIterator::innerInsetOfType(int code) const
|
|
|
|
{
|
2004-03-18 12:53:43 +00:00
|
|
|
for (int i = size() - 1; i >= 0; --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();
|
2004-03-25 09:16:36 +00:00
|
|
|
//lyxerr << "XXX\n" << *this << endl;
|
|
|
|
|
|
|
|
// this is used twice and shows up in the profiler!
|
|
|
|
pos_type const lastp = lastpos();
|
2004-03-01 17:12:09 +00:00
|
|
|
|
|
|
|
// move into an inset to the right if possible
|
|
|
|
InsetBase * n = 0;
|
2004-03-25 09:16:36 +00:00
|
|
|
|
|
|
|
if (top.pos() != lastp) {
|
2004-03-01 17:12:09 +00:00
|
|
|
// 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()) {
|
2004-03-25 09:16:36 +00:00
|
|
|
//lyxerr << "... descend" << endl;
|
2004-03-18 12:53:43 +00:00
|
|
|
push_back(CursorSlice(*n));
|
2004-03-01 17:12:09 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2004-03-25 09:16:36 +00:00
|
|
|
// otherwise move on one position if possible
|
|
|
|
if (top.pos() < lastp) {
|
|
|
|
//lyxerr << "... next pos" << endl;
|
2004-03-01 17:12:09 +00:00
|
|
|
++top.pos();
|
|
|
|
return;
|
|
|
|
}
|
2004-03-25 09:16:36 +00:00
|
|
|
//lyxerr << "... no next pos" << endl;
|
2004-03-01 17:12:09 +00:00
|
|
|
|
2004-03-25 09:16:36 +00:00
|
|
|
// otherwise move on one paragraph if possible
|
2004-03-01 17:12:09 +00:00
|
|
|
if (top.par() < lastpar()) {
|
2004-03-25 09:16:36 +00:00
|
|
|
//lyxerr << "... next par" << endl;
|
2004-03-01 17:12:09 +00:00
|
|
|
++top.par();
|
|
|
|
top.pos() = 0;
|
|
|
|
return;
|
|
|
|
}
|
2004-03-25 09:16:36 +00:00
|
|
|
//lyxerr << "... no next par" << endl;
|
2004-03-01 17:12:09 +00:00
|
|
|
|
|
|
|
// otherwise try to move on one cell if possible
|
2004-03-25 09:16:36 +00:00
|
|
|
if (top.idx() < lastidx()) {
|
|
|
|
//lyxerr << "... next idx" << endl;
|
2004-03-01 17:12:09 +00:00
|
|
|
++top.idx();
|
|
|
|
top.par() = 0;
|
|
|
|
top.pos() = 0;
|
2004-03-25 09:16:36 +00:00
|
|
|
return;
|
2004-03-01 17:12:09 +00:00
|
|
|
}
|
2004-03-25 09:16:36 +00:00
|
|
|
//lyxerr << "... no next idx" << endl;
|
2004-03-01 17:12:09 +00:00
|
|
|
|
2004-03-25 09:16:36 +00:00
|
|
|
// otherwise leave inset and jump over inset as a whole
|
2004-03-01 17:12:09 +00:00
|
|
|
pop_back();
|
|
|
|
// 'top' is invalid now...
|
|
|
|
if (size())
|
2004-03-18 16:41:45 +00:00
|
|
|
++back().pos();
|
2004-03-25 09:16:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void DocumentIterator::forwardChar()
|
|
|
|
{
|
|
|
|
forwardPos();
|
|
|
|
while (size() != 0 && pos() == lastpos())
|
|
|
|
forwardPos();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-03-27 12:37:41 +00:00
|
|
|
void DocumentIterator::forwardInset()
|
|
|
|
{
|
|
|
|
forwardPos();
|
|
|
|
while (size() != 0 && (pos() == lastpos() || nextInset() == 0))
|
|
|
|
forwardPos();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-03-25 09:16:36 +00:00
|
|
|
void DocumentIterator::backwardChar()
|
|
|
|
{
|
|
|
|
lyxerr << "not implemented" << endl;
|
|
|
|
BOOST_ASSERT(false);
|
2004-03-01 17:12:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void DocumentIterator::forwardPar()
|
|
|
|
{
|
|
|
|
CursorSlice & top = back();
|
2004-03-25 09:16:36 +00:00
|
|
|
lyxerr << "XXX " << *this << endl;
|
2004-03-01 17:12:09 +00:00
|
|
|
|
|
|
|
// 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()) {
|
2004-03-25 09:16:36 +00:00
|
|
|
lyxerr << "... descend" << endl;
|
2004-03-18 12:53:43 +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()) {
|
2004-03-25 09:16:36 +00:00
|
|
|
lyxerr << "... next pos" << endl;
|
2004-03-01 17:12:09 +00:00
|
|
|
++top.pos();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// otherwise move on one cell back if possible
|
|
|
|
if (top.par() < lastpar()) {
|
2004-03-25 09:16:36 +00:00
|
|
|
lyxerr << "... next par" << endl;
|
2004-03-01 17:12:09 +00:00
|
|
|
++top.par();
|
|
|
|
top.pos() = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// otherwise try to move on one cell if possible
|
|
|
|
while (top.idx() < top.lastidx()) {
|
|
|
|
lyxerr << "... next idx"
|
2004-03-25 09:16:36 +00:00
|
|
|
<< " was: " << top.idx() << " max: " << top.lastidx() << endl;
|
2004-03-01 17:12:09 +00:00
|
|
|
++top.idx();
|
|
|
|
top.par() = 0;
|
|
|
|
top.pos() = 0;
|
2004-03-25 09:16:36 +00:00
|
|
|
return;
|
2004-03-01 17:12:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// otherwise leave inset an jump over inset as a whole
|
|
|
|
pop_back();
|
|
|
|
// 'top' is invalid now...
|
|
|
|
if (size())
|
2004-03-18 16:41:45 +00:00
|
|
|
++back().pos();
|
2004-03-01 17:12:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
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-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-18 12:53:43 +00:00
|
|
|
StableDocumentIterator::asDocumentIterator(InsetBase * inset) const
|
2004-03-08 21:14:45 +00:00
|
|
|
{
|
|
|
|
// this function re-creates the cache of inset pointers
|
2004-03-25 09:16:36 +00:00
|
|
|
//lyxerr << "converting:\n" << *this << endl;
|
2004-03-18 12:53:43 +00:00
|
|
|
DocumentIterator dit;
|
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();
|
|
|
|
}
|
2004-03-25 09:16:36 +00:00
|
|
|
//lyxerr << "convert:\n" << *this << " to:\n" << dit << endl;
|
2004-03-08 21:14:45 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|