2004-03-30 12:14:01 +00:00
|
|
|
|
/**
|
2007-04-26 04:41:58 +00:00
|
|
|
|
* \file DocIterator.cpp
|
2004-03-30 12:14:01 +00:00
|
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
|
* Licence details can be found in the file COPYING.
|
|
|
|
|
*
|
2004-07-25 00:04:42 +00:00
|
|
|
|
* \author Andr<EFBFBD> P<EFBFBD>nitz
|
2007-06-09 12:39:46 +00:00
|
|
|
|
* \author Alfredo Braunstein
|
2004-03-30 12:14:01 +00:00
|
|
|
|
*
|
|
|
|
|
* Full author contact details are available in file CREDITS.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include <config.h>
|
2004-03-01 17:12:09 +00:00
|
|
|
|
|
2007-04-26 04:41:58 +00:00
|
|
|
|
#include "DocIterator.h"
|
2004-03-01 17:12:09 +00:00
|
|
|
|
|
|
|
|
|
#include "debug.h"
|
2007-04-29 23:33:02 +00:00
|
|
|
|
#include "Text.h"
|
2007-04-26 04:41:58 +00:00
|
|
|
|
#include "Paragraph.h"
|
2004-03-01 17:12:09 +00:00
|
|
|
|
|
2007-04-26 16:06:39 +00:00
|
|
|
|
#include "mathed/MathData.h"
|
2006-09-17 09:14:18 +00:00
|
|
|
|
#include "mathed/InsetMath.h"
|
|
|
|
|
|
2007-04-25 01:24:38 +00:00
|
|
|
|
#include "insets/InsetTabular.h"
|
2004-03-01 17:12:09 +00:00
|
|
|
|
|
|
|
|
|
#include <boost/assert.hpp>
|
2005-03-07 11:03:45 +00:00
|
|
|
|
#include <boost/current_function.hpp>
|
2004-03-01 17:12:09 +00:00
|
|
|
|
|
2007-07-17 17:46:54 +00:00
|
|
|
|
using std::endl;
|
2006-10-21 00:16:43 +00:00
|
|
|
|
|
|
|
|
|
|
2007-07-17 17:46:54 +00:00
|
|
|
|
namespace lyx {
|
2004-03-25 09:16:36 +00:00
|
|
|
|
|
2004-03-01 17:12:09 +00:00
|
|
|
|
|
2004-04-13 06:27:29 +00:00
|
|
|
|
// We could be able to get rid of this if only every BufferView were
|
|
|
|
|
// associated to a buffer on construction.
|
|
|
|
|
DocIterator::DocIterator()
|
2005-07-17 12:26:25 +00:00
|
|
|
|
: boundary_(false), inset_(0)
|
2004-04-13 06:27:29 +00:00
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
|
2007-04-29 13:39:47 +00:00
|
|
|
|
DocIterator::DocIterator(Inset & inset)
|
2005-07-17 12:26:25 +00:00
|
|
|
|
: boundary_(false), inset_(&inset)
|
2004-03-18 16:41:45 +00:00
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
|
2007-04-29 13:39:47 +00:00
|
|
|
|
DocIterator doc_iterator_begin(Inset & inset)
|
2004-03-18 16:41:45 +00:00
|
|
|
|
{
|
2004-03-31 19:11:56 +00:00
|
|
|
|
DocIterator dit(inset);
|
2004-03-30 08:18:09 +00:00
|
|
|
|
dit.forwardPos();
|
|
|
|
|
return dit;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2007-04-29 13:39:47 +00:00
|
|
|
|
DocIterator doc_iterator_end(Inset & inset)
|
2004-03-30 08:18:09 +00:00
|
|
|
|
{
|
2004-03-31 19:11:56 +00:00
|
|
|
|
return DocIterator(inset);
|
2004-03-18 16:41:45 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2007-04-29 13:39:47 +00:00
|
|
|
|
Inset * DocIterator::nextInset()
|
2004-03-01 17:12:09 +00:00
|
|
|
|
{
|
2004-03-25 09:16:36 +00:00
|
|
|
|
BOOST_ASSERT(!empty());
|
2004-03-01 17:12:09 +00:00
|
|
|
|
if (pos() == lastpos())
|
|
|
|
|
return 0;
|
2004-10-23 11:04:41 +00:00
|
|
|
|
if (pos() > lastpos()) {
|
|
|
|
|
lyxerr << "Should not happen, but it does. " << endl;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2004-04-03 08:37:12 +00:00
|
|
|
|
if (inMathed())
|
2004-03-01 17:12:09 +00:00
|
|
|
|
return nextAtom().nucleus();
|
|
|
|
|
return paragraph().isInset(pos()) ? paragraph().getInset(pos()) : 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2007-04-29 13:39:47 +00:00
|
|
|
|
Inset * DocIterator::prevInset()
|
2004-03-01 17:12:09 +00:00
|
|
|
|
{
|
2004-03-25 09:16:36 +00:00
|
|
|
|
BOOST_ASSERT(!empty());
|
2004-03-01 17:12:09 +00:00
|
|
|
|
if (pos() == 0)
|
|
|
|
|
return 0;
|
2004-04-03 08:37:12 +00:00
|
|
|
|
if (inMathed())
|
2007-02-08 08:44:45 +00:00
|
|
|
|
if (cell().empty())
|
|
|
|
|
// FIXME: this should not happen but it does.
|
|
|
|
|
// See bug 3189
|
|
|
|
|
// http://bugzilla.lyx.org/show_bug.cgi?id=3189
|
|
|
|
|
return 0;
|
|
|
|
|
else
|
|
|
|
|
return prevAtom().nucleus();
|
2004-03-01 17:12:09 +00:00
|
|
|
|
return paragraph().isInset(pos() - 1) ? paragraph().getInset(pos() - 1) : 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2007-04-29 13:39:47 +00:00
|
|
|
|
Inset const * DocIterator::prevInset() const
|
2004-03-01 17:12:09 +00:00
|
|
|
|
{
|
2004-03-25 09:16:36 +00:00
|
|
|
|
BOOST_ASSERT(!empty());
|
2004-03-01 17:12:09 +00:00
|
|
|
|
if (pos() == 0)
|
|
|
|
|
return 0;
|
2004-04-03 08:37:12 +00:00
|
|
|
|
if (inMathed())
|
2007-02-08 08:44:45 +00:00
|
|
|
|
if (cell().empty())
|
|
|
|
|
// FIXME: this should not happen but it does.
|
|
|
|
|
// See bug 3189
|
|
|
|
|
// http://bugzilla.lyx.org/show_bug.cgi?id=3189
|
|
|
|
|
return 0;
|
|
|
|
|
else
|
|
|
|
|
return prevAtom().nucleus();
|
2004-03-01 17:12:09 +00:00
|
|
|
|
return paragraph().isInset(pos() - 1) ? paragraph().getInset(pos() - 1) : 0;
|
2005-11-24 16:22:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2007-04-29 13:39:47 +00:00
|
|
|
|
Inset * DocIterator::realInset() const
|
2005-11-24 16:22:39 +00:00
|
|
|
|
{
|
|
|
|
|
BOOST_ASSERT(inTexted());
|
|
|
|
|
// if we are in a tabular, we need the cell
|
2007-04-29 13:39:47 +00:00
|
|
|
|
if (inset().lyxCode() == Inset::TABULAR_CODE) {
|
2005-11-24 16:22:39 +00:00
|
|
|
|
InsetTabular & tabular = static_cast<InsetTabular&>(inset());
|
|
|
|
|
return tabular.cell(idx()).get();
|
|
|
|
|
}
|
|
|
|
|
return &inset();
|
2004-03-01 17:12:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2004-03-31 19:11:56 +00:00
|
|
|
|
MathAtom const & DocIterator::prevAtom() const
|
2004-03-01 17:12:09 +00:00
|
|
|
|
{
|
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];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2004-03-31 19:11:56 +00:00
|
|
|
|
MathAtom & DocIterator::prevAtom()
|
2004-03-01 17:12:09 +00:00
|
|
|
|
{
|
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];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2004-03-31 19:11:56 +00:00
|
|
|
|
MathAtom const & DocIterator::nextAtom() const
|
2004-03-01 17:12:09 +00:00
|
|
|
|
{
|
2004-03-25 09:16:36 +00:00
|
|
|
|
BOOST_ASSERT(!empty());
|
2004-04-13 06:27:29 +00:00
|
|
|
|
//lyxerr << "lastpos: " << lastpos() << " next atom:\n" << *this << endl;
|
2004-03-01 17:12:09 +00:00
|
|
|
|
BOOST_ASSERT(pos() < lastpos());
|
|
|
|
|
return cell()[pos()];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2004-03-31 19:11:56 +00:00
|
|
|
|
MathAtom & DocIterator::nextAtom()
|
2004-03-01 17:12:09 +00:00
|
|
|
|
{
|
2004-03-25 09:16:36 +00:00
|
|
|
|
BOOST_ASSERT(!empty());
|
2004-04-13 06:27:29 +00:00
|
|
|
|
//lyxerr << "lastpos: " << lastpos() << " next atom:\n" << *this << endl;
|
2004-03-01 17:12:09 +00:00
|
|
|
|
BOOST_ASSERT(pos() < lastpos());
|
|
|
|
|
return cell()[pos()];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2007-04-29 23:33:02 +00:00
|
|
|
|
Text * DocIterator::text()
|
2005-01-31 16:29:48 +00:00
|
|
|
|
{
|
|
|
|
|
BOOST_ASSERT(!empty());
|
|
|
|
|
return top().text();
|
|
|
|
|
}
|
|
|
|
|
|
2007-04-29 23:33:02 +00:00
|
|
|
|
Text const * DocIterator::text() const
|
2004-03-01 17:12:09 +00:00
|
|
|
|
{
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2004-03-31 19:11:56 +00:00
|
|
|
|
Paragraph & DocIterator::paragraph()
|
2004-03-01 17:12:09 +00:00
|
|
|
|
{
|
2005-04-26 11:12:20 +00:00
|
|
|
|
if (!inTexted())
|
2005-02-14 08:17:23 +00:00
|
|
|
|
lyxerr << *this << endl;
|
2004-03-01 17:12:09 +00:00
|
|
|
|
BOOST_ASSERT(inTexted());
|
2004-03-18 12:53:43 +00:00
|
|
|
|
return top().paragraph();
|
2004-03-01 17:12:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2004-03-31 19:11:56 +00:00
|
|
|
|
Paragraph const & DocIterator::paragraph() const
|
2004-03-01 17:12:09 +00:00
|
|
|
|
{
|
|
|
|
|
BOOST_ASSERT(inTexted());
|
2004-03-18 12:53:43 +00:00
|
|
|
|
return top().paragraph();
|
2004-03-01 17:12:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2007-04-30 11:46:02 +00:00
|
|
|
|
Paragraph const & DocIterator::innerParagraph() const
|
|
|
|
|
{
|
|
|
|
|
BOOST_ASSERT(!empty());
|
|
|
|
|
// go up until first non-0 text is hit
|
|
|
|
|
// (innermost text is 0 in mathed)
|
|
|
|
|
for (int i = depth() - 1; i >= 0; --i)
|
|
|
|
|
if (slices_[i].text())
|
|
|
|
|
return slices_[i].paragraph();
|
|
|
|
|
|
|
|
|
|
// This case is in principe not possible. We _must_
|
2007-05-03 13:31:16 +00:00
|
|
|
|
// be inside a Paragraph.
|
2007-04-30 11:46:02 +00:00
|
|
|
|
BOOST_ASSERT(false);
|
|
|
|
|
return paragraph();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2006-10-21 00:16:43 +00:00
|
|
|
|
pit_type DocIterator::lastpit() const
|
2004-03-01 17:12:09 +00:00
|
|
|
|
{
|
|
|
|
|
return inMathed() ? 0 : text()->paragraphs().size() - 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2006-10-21 00:16:43 +00:00
|
|
|
|
pos_type DocIterator::lastpos() const
|
2004-03-01 17:12:09 +00:00
|
|
|
|
{
|
|
|
|
|
return inMathed() ? cell().size() : paragraph().size();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2004-03-31 19:11:56 +00:00
|
|
|
|
DocIterator::idx_type DocIterator::lastidx() const
|
2004-03-01 17:12:09 +00:00
|
|
|
|
{
|
2004-03-18 12:53:43 +00:00
|
|
|
|
return top().lastidx();
|
2004-03-01 17:12:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2004-03-31 19:11:56 +00:00
|
|
|
|
size_t DocIterator::nargs() const
|
2004-03-01 17:12:09 +00:00
|
|
|
|
{
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2004-03-31 19:11:56 +00:00
|
|
|
|
size_t DocIterator::ncols() const
|
2004-03-01 17:12:09 +00:00
|
|
|
|
{
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2004-03-31 19:11:56 +00:00
|
|
|
|
size_t DocIterator::nrows() const
|
2004-03-01 17:12:09 +00:00
|
|
|
|
{
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2004-03-31 19:11:56 +00:00
|
|
|
|
DocIterator::row_type DocIterator::row() const
|
2004-03-01 17:12:09 +00:00
|
|
|
|
{
|
2004-03-18 12:53:43 +00:00
|
|
|
|
return top().row();
|
2004-03-01 17:12:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2004-03-31 19:11:56 +00:00
|
|
|
|
DocIterator::col_type DocIterator::col() const
|
2004-03-01 17:12:09 +00:00
|
|
|
|
{
|
2004-03-18 12:53:43 +00:00
|
|
|
|
return top().col();
|
2004-03-01 17:12:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2007-04-26 16:06:39 +00:00
|
|
|
|
MathData const & DocIterator::cell() const
|
2004-03-01 17:12:09 +00:00
|
|
|
|
{
|
2007-03-26 13:43:49 +00:00
|
|
|
|
// BOOST_ASSERT(inMathed());
|
2004-03-01 17:12:09 +00:00
|
|
|
|
return top().cell();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2007-04-26 16:06:39 +00:00
|
|
|
|
MathData & DocIterator::cell()
|
2004-03-01 17:12:09 +00:00
|
|
|
|
{
|
2007-03-26 13:43:49 +00:00
|
|
|
|
// BOOST_ASSERT(inMathed());
|
2004-03-01 17:12:09 +00:00
|
|
|
|
return top().cell();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2007-04-29 23:33:02 +00:00
|
|
|
|
Text * DocIterator::innerText()
|
2005-01-31 16:29:48 +00:00
|
|
|
|
{
|
|
|
|
|
BOOST_ASSERT(!empty());
|
2005-02-08 13:18:05 +00:00
|
|
|
|
// Go up until first non-0 text is hit
|
2005-01-31 16:29:48 +00:00
|
|
|
|
// (innermost text is 0 in mathed)
|
2005-02-08 13:18:05 +00:00
|
|
|
|
for (int i = depth() - 1; i >= 0; --i)
|
|
|
|
|
if (slices_[i].text())
|
|
|
|
|
return slices_[i].text();
|
2005-01-31 16:29:48 +00:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2007-04-29 23:33:02 +00:00
|
|
|
|
Text const * DocIterator::innerText() const
|
2004-03-01 17:12:09 +00:00
|
|
|
|
{
|
|
|
|
|
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)
|
2005-02-08 13:18:05 +00:00
|
|
|
|
for (int i = depth() - 1; i >= 0; --i)
|
|
|
|
|
if (slices_[i].text())
|
|
|
|
|
return slices_[i].text();
|
2004-03-18 12:53:43 +00:00
|
|
|
|
return 0;
|
2004-03-01 17:12:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2007-04-29 13:39:47 +00:00
|
|
|
|
Inset * DocIterator::innerInsetOfType(int code) const
|
2004-03-01 17:12:09 +00:00
|
|
|
|
{
|
2005-02-08 13:18:05 +00:00
|
|
|
|
for (int i = depth() - 1; i >= 0; --i)
|
|
|
|
|
if (slices_[i].inset_->lyxCode() == code)
|
|
|
|
|
return slices_[i].inset_;
|
2004-03-01 17:12:09 +00:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2006-01-28 09:46:58 +00:00
|
|
|
|
void DocIterator::forwardPos(bool ignorecollapsed)
|
2004-03-01 17:12:09 +00:00
|
|
|
|
{
|
2004-03-30 08:18:09 +00:00
|
|
|
|
//this dog bites his tail
|
|
|
|
|
if (empty()) {
|
|
|
|
|
push_back(CursorSlice(*inset_));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2007-04-29 13:39:47 +00:00
|
|
|
|
Inset * const nextinset = nextInset();
|
2006-01-28 09:46:58 +00:00
|
|
|
|
// jump over collapsables if they are collapsed
|
2006-09-16 18:11:38 +00:00
|
|
|
|
// FIXME: the check for asInsetMath() shouldn't be necessary
|
2006-01-28 09:46:58 +00:00
|
|
|
|
// but math insets do not return a sensible editable() state yet.
|
2007-01-11 18:25:11 +00:00
|
|
|
|
if (ignorecollapsed && nextinset && (!nextinset->asInsetMath()
|
2007-04-29 13:39:47 +00:00
|
|
|
|
&& nextinset->editable() != Inset::HIGHLY_EDITABLE)) {
|
2006-01-28 09:46:58 +00:00
|
|
|
|
++top().pos();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2005-02-08 13:18:05 +00:00
|
|
|
|
CursorSlice & tip = top();
|
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
|
2007-04-29 13:39:47 +00:00
|
|
|
|
Inset * n = 0;
|
2004-03-25 09:16:36 +00:00
|
|
|
|
|
2005-02-08 13:18:05 +00:00
|
|
|
|
if (tip.pos() != lastp) {
|
2004-03-01 17:12:09 +00:00
|
|
|
|
// this is impossible for pos() == size()
|
|
|
|
|
if (inMathed()) {
|
2005-02-08 13:18:05 +00:00
|
|
|
|
n = (tip.cell().begin() + tip.pos())->nucleus();
|
2004-03-01 17:12:09 +00:00
|
|
|
|
} else {
|
2007-01-11 18:20:20 +00:00
|
|
|
|
if (paragraph().isInset(tip.pos()))
|
|
|
|
|
n = paragraph().getInset(tip.pos());
|
2004-03-01 17:12:09 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
2005-02-08 13:18:05 +00:00
|
|
|
|
if (tip.pos() < lastp) {
|
2004-03-25 09:16:36 +00:00
|
|
|
|
//lyxerr << "... next pos" << endl;
|
2005-02-08 13:18:05 +00:00
|
|
|
|
++tip.pos();
|
2004-03-01 17:12:09 +00:00
|
|
|
|
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
|
2005-02-08 13:18:05 +00:00
|
|
|
|
if (tip.pit() < lastpit()) {
|
2004-03-25 09:16:36 +00:00
|
|
|
|
//lyxerr << "... next par" << endl;
|
2005-02-08 13:18:05 +00:00
|
|
|
|
++tip.pit();
|
|
|
|
|
tip.pos() = 0;
|
2004-03-01 17:12:09 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
2004-11-30 01:59:49 +00:00
|
|
|
|
//lyxerr << "... no next pit" << endl;
|
2004-03-01 17:12:09 +00:00
|
|
|
|
|
|
|
|
|
// otherwise try to move on one cell if possible
|
2005-02-08 13:18:05 +00:00
|
|
|
|
if (tip.idx() < lastidx()) {
|
2004-03-25 09:16:36 +00:00
|
|
|
|
//lyxerr << "... next idx" << endl;
|
2005-02-08 13:18:05 +00:00
|
|
|
|
++tip.idx();
|
|
|
|
|
tip.pit() = 0;
|
|
|
|
|
tip.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...
|
2005-02-08 13:18:05 +00:00
|
|
|
|
if (!empty())
|
|
|
|
|
++top().pos();
|
2004-03-25 09:16:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2005-02-14 08:17:23 +00:00
|
|
|
|
void DocIterator::forwardPosNoDescend()
|
|
|
|
|
{
|
|
|
|
|
CursorSlice & tip = top();
|
|
|
|
|
pos_type const lastp = lastpos();
|
|
|
|
|
|
|
|
|
|
// move on one position if possible
|
|
|
|
|
if (tip.pos() < lastp) {
|
|
|
|
|
//lyxerr << "... next pos" << endl;
|
|
|
|
|
++tip.pos();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
//lyxerr << "... no next pos" << endl;
|
|
|
|
|
|
|
|
|
|
// otherwise move on one paragraph if possible
|
|
|
|
|
if (tip.pit() < lastpit()) {
|
|
|
|
|
//lyxerr << "... next par" << endl;
|
|
|
|
|
++tip.pit();
|
|
|
|
|
tip.pos() = 0;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
//lyxerr << "... no next pit" << endl;
|
|
|
|
|
|
|
|
|
|
// otherwise try to move on one cell if possible
|
|
|
|
|
if (tip.idx() < lastidx()) {
|
|
|
|
|
//lyxerr << "... next idx" << endl;
|
|
|
|
|
++tip.idx();
|
|
|
|
|
tip.pit() = 0;
|
|
|
|
|
tip.pos() = 0;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
//lyxerr << "... no next idx" << endl;
|
|
|
|
|
|
2006-08-13 15:29:59 +00:00
|
|
|
|
// otherwise leave inset and jump over inset as a whole
|
|
|
|
|
pop_back();
|
|
|
|
|
// 'top' is invalid now...
|
|
|
|
|
if (!empty())
|
|
|
|
|
++top().pos();
|
2005-02-14 08:17:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2004-03-31 19:11:56 +00:00
|
|
|
|
void DocIterator::forwardPar()
|
2004-03-28 22:00:22 +00:00
|
|
|
|
{
|
|
|
|
|
forwardPos();
|
2005-02-25 11:55:36 +00:00
|
|
|
|
|
|
|
|
|
while (!empty() && (!inTexted() || pos() != 0)) {
|
|
|
|
|
if (inTexted()) {
|
|
|
|
|
pos_type const lastp = lastpos();
|
|
|
|
|
Paragraph const & par = paragraph();
|
|
|
|
|
pos_type & pos = top().pos();
|
2007-01-11 17:40:23 +00:00
|
|
|
|
if (par.insetlist.empty())
|
|
|
|
|
pos = lastp;
|
|
|
|
|
else
|
|
|
|
|
while (pos < lastp && !par.isInset(pos))
|
|
|
|
|
++pos;
|
2005-02-25 11:55:36 +00:00
|
|
|
|
}
|
2004-03-28 22:00:22 +00:00
|
|
|
|
forwardPos();
|
2005-02-25 11:55:36 +00:00
|
|
|
|
}
|
2004-03-28 22:00:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2007-05-22 10:51:38 +00:00
|
|
|
|
void DocIterator::forwardIdx()
|
|
|
|
|
{
|
|
|
|
|
CursorSlice & tip = top();
|
|
|
|
|
|
|
|
|
|
//prevent endless loops
|
|
|
|
|
BOOST_ASSERT(tip.idx() < lastidx());
|
|
|
|
|
|
|
|
|
|
++tip.idx();
|
|
|
|
|
tip.pit() = 0;
|
|
|
|
|
tip.pos() = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2004-03-31 19:11:56 +00:00
|
|
|
|
void DocIterator::forwardChar()
|
2004-03-25 09:16:36 +00:00
|
|
|
|
{
|
2004-03-28 22:00:22 +00:00
|
|
|
|
forwardPos();
|
2005-02-08 13:18:05 +00:00
|
|
|
|
while (!empty() && pos() == lastpos())
|
2004-04-03 08:37:12 +00:00
|
|
|
|
forwardPos();
|
2004-03-25 09:16:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2004-03-31 19:11:56 +00:00
|
|
|
|
void DocIterator::forwardInset()
|
2004-03-27 12:37:41 +00:00
|
|
|
|
{
|
2004-04-03 08:37:12 +00:00
|
|
|
|
forwardPos();
|
2006-11-10 23:31:21 +00:00
|
|
|
|
|
|
|
|
|
while (!empty() && !nextInset()) {
|
|
|
|
|
if (inTexted()) {
|
|
|
|
|
pos_type const lastp = lastpos();
|
|
|
|
|
Paragraph const & par = paragraph();
|
|
|
|
|
pos_type & pos = top().pos();
|
|
|
|
|
while (pos < lastp && !par.isInset(pos))
|
|
|
|
|
++pos;
|
|
|
|
|
if (pos < lastp)
|
|
|
|
|
break;
|
|
|
|
|
}
|
2004-03-27 12:37:41 +00:00
|
|
|
|
forwardPos();
|
2006-11-10 23:31:21 +00:00
|
|
|
|
}
|
2004-03-27 12:37:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2004-03-31 19:11:56 +00:00
|
|
|
|
void DocIterator::backwardChar()
|
2004-03-25 09:16:36 +00:00
|
|
|
|
{
|
2004-03-30 08:18:09 +00:00
|
|
|
|
backwardPos();
|
2005-02-08 13:18:05 +00:00
|
|
|
|
while (!empty() && pos() == lastpos())
|
2004-04-03 08:37:12 +00:00
|
|
|
|
backwardPos();
|
2004-03-30 08:18:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2004-03-31 19:11:56 +00:00
|
|
|
|
void DocIterator::backwardPos()
|
2004-03-30 08:18:09 +00:00
|
|
|
|
{
|
|
|
|
|
//this dog bites his tail
|
|
|
|
|
if (empty()) {
|
|
|
|
|
push_back(CursorSlice(*inset_));
|
2005-02-08 13:18:05 +00:00
|
|
|
|
top().idx() = lastidx();
|
|
|
|
|
top().pit() = lastpit();
|
|
|
|
|
top().pos() = lastpos();
|
2004-03-30 08:18:09 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2005-02-08 13:18:05 +00:00
|
|
|
|
CursorSlice & tip = top();
|
2004-03-30 08:18:09 +00:00
|
|
|
|
|
2005-02-08 13:18:05 +00:00
|
|
|
|
if (tip.pos() != 0) {
|
|
|
|
|
--tip.pos();
|
|
|
|
|
} else if (tip.pit() != 0) {
|
|
|
|
|
--tip.pit();
|
|
|
|
|
tip.pos() = lastpos();
|
2004-03-30 08:18:09 +00:00
|
|
|
|
return;
|
2005-02-08 13:18:05 +00:00
|
|
|
|
} else if (tip.idx() != 0) {
|
|
|
|
|
--tip.idx();
|
|
|
|
|
tip.pit() = lastpit();
|
|
|
|
|
tip.pos() = lastpos();
|
2004-03-30 08:18:09 +00:00
|
|
|
|
return;
|
|
|
|
|
} else {
|
|
|
|
|
pop_back();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// move into an inset to the left if possible
|
2007-04-29 13:39:47 +00:00
|
|
|
|
Inset * n = 0;
|
2004-04-03 08:37:12 +00:00
|
|
|
|
|
2004-03-30 08:18:09 +00:00
|
|
|
|
if (inMathed()) {
|
2005-02-08 13:18:05 +00:00
|
|
|
|
n = (tip.cell().begin() + tip.pos())->nucleus();
|
2004-03-30 08:18:09 +00:00
|
|
|
|
} else {
|
2005-02-08 13:18:05 +00:00
|
|
|
|
if (paragraph().isInset(tip.pos()))
|
|
|
|
|
n = paragraph().getInset(tip.pos());
|
2004-03-30 08:18:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (n && n->isActive()) {
|
|
|
|
|
push_back(CursorSlice(*n));
|
2005-02-08 13:18:05 +00:00
|
|
|
|
top().idx() = lastidx();
|
|
|
|
|
top().pit() = lastpit();
|
|
|
|
|
top().pos() = lastpos();
|
2004-03-30 08:18:09 +00:00
|
|
|
|
}
|
2004-03-01 17:12:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2004-08-14 21:03:55 +00:00
|
|
|
|
bool DocIterator::hasPart(DocIterator const & it) const
|
|
|
|
|
{
|
|
|
|
|
// it can't be a part if it is larger
|
2005-02-08 13:18:05 +00:00
|
|
|
|
if (it.depth() > depth())
|
2004-08-14 21:03:55 +00:00
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
// as inset adresses are the 'last' level
|
2005-02-08 13:18:05 +00:00
|
|
|
|
return &it.top().inset() == &slices_[it.depth() - 1].inset();
|
2004-08-14 21:03:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2007-04-29 13:39:47 +00:00
|
|
|
|
void DocIterator::updateInsets(Inset * inset)
|
2005-10-25 09:14:11 +00:00
|
|
|
|
{
|
|
|
|
|
// this function re-creates the cache of inset pointers.
|
|
|
|
|
// code taken in part from StableDocIterator::asDocIterator.
|
|
|
|
|
//lyxerr << "converting:\n" << *this << endl;
|
|
|
|
|
DocIterator dit = DocIterator(*inset);
|
|
|
|
|
size_t const n = slices_.size();
|
|
|
|
|
for (size_t i = 0 ; i < n; ++i) {
|
|
|
|
|
BOOST_ASSERT(inset);
|
|
|
|
|
dit.push_back(slices_[i]);
|
|
|
|
|
dit.top().inset_ = inset;
|
|
|
|
|
if (i + 1 != n)
|
|
|
|
|
inset = dit.nextInset();
|
|
|
|
|
}
|
|
|
|
|
//lyxerr << "converted:\n" << *this << endl;
|
|
|
|
|
operator=(dit);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2007-05-25 23:17:24 +00:00
|
|
|
|
bool DocIterator::fixIfBroken()
|
|
|
|
|
{
|
2007-06-09 12:39:46 +00:00
|
|
|
|
// Go through the slice stack from the bottom.
|
|
|
|
|
// Check that all coordinates (idx, pit, pos) are correct and
|
|
|
|
|
// that the inset is the one which is claimed to be there
|
|
|
|
|
Inset * inset = &slices_[0].inset();
|
|
|
|
|
size_t i = 0;
|
|
|
|
|
size_t n = slices_.size();
|
|
|
|
|
for (; i != n; ++i) {
|
|
|
|
|
CursorSlice & cs = slices_[i];
|
|
|
|
|
if (&cs.inset() != inset) {
|
|
|
|
|
// the whole slice is wrong, chop off this as well
|
|
|
|
|
--i;
|
|
|
|
|
LYXERR(Debug::DEBUG) << "fixIfBroken(): inset changed" << endl;
|
|
|
|
|
break;
|
|
|
|
|
} else if (cs.idx() > cs.lastidx()) {
|
|
|
|
|
cs.idx() = cs.lastidx();
|
|
|
|
|
cs.pit() = cs.lastpit();
|
|
|
|
|
cs.pos() = cs.lastpos();
|
|
|
|
|
LYXERR(Debug::DEBUG) << "fixIfBroken(): idx fixed" << endl;
|
|
|
|
|
break;
|
|
|
|
|
} else if (cs.pit() > cs.lastpit()) {
|
|
|
|
|
cs.pit() = cs.lastpit();
|
|
|
|
|
cs.pos() = cs.lastpos();
|
|
|
|
|
LYXERR(Debug::DEBUG) << "fixIfBroken(): pit fixed" << endl;
|
|
|
|
|
break;
|
|
|
|
|
} else if (cs.pos() > cs.lastpos()) {
|
|
|
|
|
cs.pos() = cs.lastpos();
|
|
|
|
|
LYXERR(Debug::DEBUG) << "fixIfBroken(): pos fixed" << endl;
|
|
|
|
|
break;
|
|
|
|
|
} else if (i != n - 1 && cs.pos() != cs.lastpos()) {
|
|
|
|
|
// get inset which is supposed to be in the next slice
|
|
|
|
|
if (cs.inset().inMathed())
|
|
|
|
|
inset = (cs.cell().begin() + cs.pos())->nucleus();
|
|
|
|
|
else if (cs.paragraph().isInset(cs.pos()))
|
|
|
|
|
inset = cs.paragraph().getInset(cs.pos());
|
|
|
|
|
else {
|
|
|
|
|
// there are slices left, so there must be another inset
|
|
|
|
|
break;
|
|
|
|
|
}
|
2007-05-25 23:17:24 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2007-06-09 12:39:46 +00:00
|
|
|
|
|
|
|
|
|
// Did we make it through the whole slice stack? Otherwise there
|
|
|
|
|
// was a problem at slice i, and we have to chop off above
|
|
|
|
|
if (i < n) {
|
|
|
|
|
LYXERR(Debug::DEBUG) << "fixIfBroken(): cursor chopped at " << i << endl;
|
|
|
|
|
resize(i + 1);
|
|
|
|
|
return true;
|
|
|
|
|
} else
|
|
|
|
|
return false;
|
2007-05-25 23:17:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2004-03-31 19:11:56 +00:00
|
|
|
|
std::ostream & operator<<(std::ostream & os, DocIterator const & dit)
|
2004-03-01 17:12:09 +00:00
|
|
|
|
{
|
2005-02-08 13:18:05 +00:00
|
|
|
|
for (size_t i = 0, n = dit.depth(); i != n; ++i)
|
|
|
|
|
os << " " << dit[i] << "\n";
|
2004-03-01 17:12:09 +00:00
|
|
|
|
return os;
|
|
|
|
|
}
|
2004-03-08 21:14:45 +00:00
|
|
|
|
|
|
|
|
|
|
2007-05-15 17:07:55 +00:00
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool operator>(DocIterator const & p, DocIterator const & q)
|
|
|
|
|
{
|
|
|
|
|
return q < p;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool operator<=(DocIterator const & p, DocIterator const & q)
|
|
|
|
|
{
|
|
|
|
|
return !(q < p);
|
|
|
|
|
}
|
|
|
|
|
|
2004-03-08 21:14:45 +00:00
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////
|
|
|
|
|
|
2005-02-08 13:18:05 +00:00
|
|
|
|
StableDocIterator::StableDocIterator(DocIterator const & dit)
|
2004-03-08 21:14:45 +00:00
|
|
|
|
{
|
2005-02-08 13:18:05 +00:00
|
|
|
|
data_ = dit.internalData();
|
2004-03-08 21:14:45 +00:00
|
|
|
|
for (size_t i = 0, n = data_.size(); i != n; ++i)
|
|
|
|
|
data_[i].inset_ = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2007-04-29 13:39:47 +00:00
|
|
|
|
DocIterator StableDocIterator::asDocIterator(Inset * 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-31 19:11:56 +00:00
|
|
|
|
DocIterator dit = DocIterator(*inset);
|
2004-03-08 21:14:45 +00:00
|
|
|
|
for (size_t i = 0, n = data_.size(); i != n; ++i) {
|
2004-10-23 11:04:41 +00:00
|
|
|
|
if (inset == 0) {
|
|
|
|
|
// FIXME
|
2005-02-08 13:18:05 +00:00
|
|
|
|
lyxerr << BOOST_CURRENT_FUNCTION
|
2007-05-25 23:17:24 +00:00
|
|
|
|
<< " Should not happen, but does e.g. after C-n C-l C-z S-C-z\n"
|
|
|
|
|
<< " or when a Buffer has been concurently edited by two views"
|
2005-02-08 13:18:05 +00:00
|
|
|
|
<< '\n' << "dit: " << dit << '\n'
|
2004-10-23 11:04:41 +00:00
|
|
|
|
<< " lastpos: " << dit.lastpos() << endl;
|
2007-05-25 23:17:24 +00:00
|
|
|
|
dit.fixIfBroken();
|
|
|
|
|
break;
|
2004-10-23 11:04:41 +00:00
|
|
|
|
}
|
2004-03-08 21:14:45 +00:00
|
|
|
|
dit.push_back(data_[i]);
|
2005-02-08 13:18:05 +00:00
|
|
|
|
dit.top().inset_ = inset;
|
2007-05-25 23:17:24 +00:00
|
|
|
|
if (dit.fixIfBroken())
|
|
|
|
|
break;
|
2004-03-08 21:14:45 +00:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2004-03-31 19:11:56 +00:00
|
|
|
|
std::ostream & operator<<(std::ostream & os, StableDocIterator const & dit)
|
2004-03-08 21:14:45 +00:00
|
|
|
|
{
|
|
|
|
|
for (size_t i = 0, n = dit.data_.size(); i != n; ++i)
|
|
|
|
|
os << " " << dit.data_[i] << "\n";
|
|
|
|
|
return os;
|
|
|
|
|
}
|
2005-07-01 12:36:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool operator==(StableDocIterator const & dit1, StableDocIterator const & dit2)
|
|
|
|
|
{
|
|
|
|
|
return dit1.data_ == dit2.data_;
|
|
|
|
|
}
|
2006-10-21 00:16:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace lyx
|