X-Git-Url: https://git.lyx.org/gitweb/?a=blobdiff_plain;f=src%2FCursorSlice.cpp;h=573ca9d4fe71b653bd2b3e1e02401c11171891b8;hb=28be7d552f62cc02fa86d7f79201d089bfb2d7b5;hp=3f52df8b3faabf2c3a21490fd03874d5900401b2;hpb=9d0ea8aeff32833a90b3fe64df0c5518a9e241be;p=lyx.git diff --git a/src/CursorSlice.cpp b/src/CursorSlice.cpp index 3f52df8b3f..573ca9d4fe 100644 --- a/src/CursorSlice.cpp +++ b/src/CursorSlice.cpp @@ -3,10 +3,10 @@ * This file is part of LyX, the document processor. * Licence details can be found in the file COPYING. * - * \author Lars Gullik Bjønnes + * \author Lars Gullik Bjønnes * \author Matthias Ettrich - * \author André Pönitz - * \author Jürgen Vigna + * \author André Pönitz + * \author Jürgen Vigna * * Full author contact details are available in file CREDITS. */ @@ -23,12 +23,15 @@ #include "insets/Inset.h" #include "mathed/InsetMath.h" -#include "mathed/MathData.h" +#include "mathed/MathMacro.h" -#include +#include "support/ExceptionMessage.h" +#include "support/gettext.h" +#include "support/lassert.h" #include +using namespace std; namespace lyx { @@ -41,7 +44,7 @@ CursorSlice::CursorSlice() CursorSlice::CursorSlice(Inset & p) : inset_(&p), idx_(0), pit_(0), pos_(0) { - BOOST_ASSERT(inset_); + LBUFERR(inset_); } @@ -59,14 +62,17 @@ Paragraph & CursorSlice::paragraph() const pos_type CursorSlice::lastpos() const { - BOOST_ASSERT(inset_); - return inset_->asInsetMath() ? cell().size() : paragraph().size(); + LBUFERR(inset_); + InsetMath const * math = inset_->asInsetMath(); + bool paramless_macro = math && math->asMacro() && !math->asMacro()->nargs(); + return math ? (paramless_macro ? 0 : cell().size()) + : (text()->empty() ? 0 : paragraph().size()); } pit_type CursorSlice::lastpit() const { - if (inset().inMathed()) + if (inset_->inMathed()) return 0; return text()->paragraphs().size() - 1; } @@ -74,122 +80,157 @@ pit_type CursorSlice::lastpit() const CursorSlice::row_type CursorSlice::row() const { - BOOST_ASSERT(asInsetMath()); - return asInsetMath()->row(idx_); + LASSERT(inset_, return 0); + return inset_->row(idx_); } CursorSlice::col_type CursorSlice::col() const { - BOOST_ASSERT(asInsetMath()); - return asInsetMath()->col(idx_); + LASSERT(inset_, return 0); + return inset_->col(idx_); +} + + +void CursorSlice::setPitPos(pit_type pit, pos_type pos) +{ + LASSERT(pit != int(text()->paragraphs().size()), return); + pit_ = pit; + pos_ = pos; + + // Now some strict checking. None of these should happen, but + // we're scaredy-cats + if (pos < 0) { + LYXERR0("Don't like -1!"); + LATTEST(false); + } + + if (pos > paragraph().size()) { + LYXERR0("Don't like 1, pos: " << pos + << " size: " << paragraph().size() + << " par: " << pit); + LATTEST(false); + } } void CursorSlice::forwardPos() { // move on one position if possible - if (pos() < lastpos()) { + if (pos_ < lastpos()) { //lyxerr << "... next pos" << endl; - ++pos(); + ++pos_; return; } // otherwise move on one paragraph if possible - if (pit() < lastpit()) { + if (pit_ < lastpit()) { //lyxerr << "... next par" << endl; - ++pit(); - pos() = 0; + ++pit_; + pos_ = 0; return; } // otherwise move on one cell //lyxerr << "... next idx" << endl; - BOOST_ASSERT(idx() < nargs()); + LASSERT(idx_ < nargs(), return); - ++idx(); - pit() = 0; - pos() = 0; + ++idx_; + pit_ = 0; + pos_ = 0; } void CursorSlice::forwardIdx() { - BOOST_ASSERT(idx() < nargs()); + LASSERT(idx_ < nargs(), return); - ++idx(); - pit() = 0; - pos() = 0; + ++idx_; + pit_ = 0; + pos_ = 0; } void CursorSlice::backwardPos() { - if (pos() != 0) { - --pos(); + if (pos_ != 0) { + --pos_; return; } - if (pit() != 0) { - --pit(); - pos() = lastpos(); + if (pit_ != 0) { + --pit_; + pos_ = lastpos(); return; } - if (idx() != 0) { - --idx(); - pit() = lastpit(); - pos() = lastpos(); + if (idx_ != 0) { + --idx_; + pit_ = lastpit(); + pos_ = lastpos(); return; } - BOOST_ASSERT(false); + LATTEST(false); +} + + +bool CursorSlice::at_cell_end() const +{ + return pit_ == lastpit() && pos_ == lastpos(); +} + + +bool CursorSlice::at_cell_begin() const +{ + return pit_ == 0 && pos_ == 0; } -bool CursorSlice::at_end() const +bool CursorSlice::at_end() const { - return idx() == lastidx() && pit() == lastpit() && pos() == lastpos(); + return idx_ == lastidx() && at_cell_end(); } bool CursorSlice::at_begin() const { - return idx() == 0 && pit() == 0 && pos() == 0; + return idx_ == 0 && at_cell_begin(); } bool operator==(CursorSlice const & p, CursorSlice const & q) { - return &p.inset() == &q.inset() - && p.idx() == q.idx() - && p.pit() == q.pit() - && p.pos() == q.pos(); + return p.inset_ == q.inset_ + && p.idx_ == q.idx_ + && p.pit_ == q.pit_ + && p.pos_ == q.pos_; } bool operator!=(CursorSlice const & p, CursorSlice const & q) { - return &p.inset() != &q.inset() - || p.idx() != q.idx() - || p.pit() != q.pit() - || p.pos() != q.pos(); + return p.inset_ != q.inset_ + || p.idx_ != q.idx_ + || p.pit_ != q.pit_ + || p.pos_ != q.pos_; } bool operator<(CursorSlice const & p, CursorSlice const & q) { - if (&p.inset() != &q.inset()) { + if (p.inset_ != q.inset_) { LYXERR0("can't compare cursor and anchor in different insets\n" << "p: " << p << '\n' << "q: " << q); - BOOST_ASSERT(false); + // It should be safe to continue, just registering the error. + LASSERT(false, return false); } - if (p.idx() != q.idx()) - return p.idx() < q.idx(); - if (p.pit() != q.pit()) - return p.pit() < q.pit(); - return p.pos() < q.pos(); + if (p.idx_ != q.idx_) + return p.idx_ < q.idx_; + if (p.pit_ != q.pit_) + return p.pit_ < q.pit_; + return p.pos_ < q.pos_; } @@ -205,16 +246,16 @@ bool operator<=(CursorSlice const & p, CursorSlice const & q) } -std::ostream & operator<<(std::ostream & os, CursorSlice const & item) +ostream & operator<<(ostream & os, CursorSlice const & item) { return os - << "inset: " << (void *)&item.inset() + << "inset: " << (void *)item.inset_ // << " text: " << item.text() - << " idx: " << item.idx() - << " par: " << item.pit() - << " pos: " << item.pos() -// << " x: " << item.inset().x() -// << " y: " << item.inset().y() + << " idx: " << item.idx_ + << " par: " << item.pit_ + << " pos: " << item.pos_ +// << " x: " << item.inset_->x() +// << " y: " << item.inset_->y() ; }