}
+int CursorData::countInsetsInSelection(InsetCode const & inset_code)
+{
+ if (!selection_)
+ return 0;
+
+ DocIterator from, to;
+ from = selectionBegin();
+ to = selectionEnd();
+
+ int count = 0;
+
+ if (!from.nextInset()) //move to closest inset
+ from.forwardInset();
+
+ while (!from.empty() && from < to) {
+ Inset * inset = from.nextInset();
+ if (!inset)
+ break;
+ if (inset->lyxCode() == inset_code)
+ count ++;
+ from.forwardInset();
+ }
+ return count;
+}
+
+
+bool CursorData::insetInSelection(InsetCode const & inset_code)
+{
+ if (!selection_)
+ return false;
+
+ DocIterator from, to;
+ from = selectionBegin();
+ to = selectionEnd();
+
+ if (!from.nextInset()) //move to closest inset
+ from.forwardInset();
+
+ while (!from.empty() && from < to) {
+ Inset * inset = from.nextInset();
+ if (!inset)
+ break;
+ if (inset->lyxCode() == inset_code)
+ return true;
+ from.forwardInset();
+ }
+ return false;
+}
+
+
bool CursorData::fixIfBroken()
{
bool const broken_cursor = DocIterator::fixIfBroken();
}
-bool CursorData::isInside(Inset const * p) const
+bool CursorData::undoAction()
{
- for (size_t i = 0; i != depth(); ++i)
- if (&operator[](i).inset() == p)
- return true;
- return false;
-}
-
-
-void CursorData::leaveInset(Inset const & inset)
-{
- for (size_t i = 0; i != depth(); ++i) {
- if (&operator[](i).inset() == &inset) {
- resize(i);
- return;
- }
- }
-}
-
-
-bool CursorData::textUndo()
-{
- if (!buffer()->undo().textUndo(*this))
+ if (!buffer()->undo().undoAction(*this))
return false;
sanitize();
return true;
}
-bool CursorData::textRedo()
+bool CursorData::redoAction()
{
- if (!buffer()->undo().textRedo(*this))
+ if (!buffer()->undo().redoAction(*this))
return false;
sanitize();
return true;
}
+void Cursor::editInsertedInset()
+{
+ LASSERT(!empty(), return);
+ if (pos() == 0)
+ return;
+
+ InsetMath &p = prevMath();
+ if (!p.isActive())
+ return;
+
+ posBackward();
+ push(p);
+ p.idxFirst(*this);
+ // this could be a while() loop, but only one cell is not empty in
+ // cases we are interested in. The cell is not empty because we
+ // have inserted the selection in there.
+ if (!cell().empty()) {
+ // if it is not empty, move to the next one.
+ if (!inset().idxNext(*this))
+ // If there is no next one, exit the inset.
+ popForward();
+ }
+}
+
+
bool Cursor::popBackward()
{
LASSERT(!empty(), return false);
plainInsert(t);
// If possible, enter the new inset and move the contents of the selection
if (t->isActive()) {
- posBackward();
- // be careful here: don't use 'pushBackward(t)' as this we need to
- // push the clone, not the original
- pushBackward(*nextInset());
- // We may not use niceInsert here (recursion)
- MathData ar(buffer());
- asArray(safe, ar);
- insert(ar);
+ idx_type const idx = prevMath().asNestInset()->firstIdx();
+ asArray(safe, prevMath().cell(idx));
+ editInsertedInset();
} else if (t->asMacro() && !safe.empty()) {
MathData ar(buffer());
asArray(safe, ar);
}
-void Cursor::handleNest(MathAtom const & a, int c)
+void Cursor::handleNest(MathAtom const & a)
{
- //lyxerr << "Cursor::handleNest: " << c << endl;
+ idx_type const idx = a.nucleus()->asNestInset()->firstIdx();
+ //lyxerr << "Cursor::handleNest: " << idx << endl;
MathAtom t = a;
- asArray(cap::grabAndEraseSelection(*this), t.nucleus()->cell(c));
+ asArray(cap::grabAndEraseSelection(*this), t.nucleus()->cell(idx));
insert(t);
- posBackward();
- pushBackward(*nextInset());
-}
-
-
-void Cursor::handleNest(MathAtom const & a)
-{
- handleNest(a, a.nucleus()->asNestInset()->firstIdx());
+ editInsertedInset();
}