]> git.lyx.org Git - lyx.git/blobdiff - src/Cursor.cpp
ctests: uninvert two 008-greek-and-coptic tests
[lyx.git] / src / Cursor.cpp
index 2ad8bd4207a063dbbd969aa45d695575733b67e2..a6d799ec9c81d5ba9efcf395b6f2e1dfe6e555ea 100644 (file)
@@ -496,6 +496,56 @@ void CursorData::clearSelection()
 }
 
 
+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();
@@ -521,38 +571,18 @@ void CursorData::sanitize()
 }
 
 
-bool CursorData::isInside(Inset const * p) const
-{
-       for (size_t i = 0; i != depth(); ++i)
-               if (&operator[](i).inset() == p)
-                       return true;
-       return false;
-}
-
-
-void CursorData::leaveInset(Inset const & inset)
+bool CursorData::undoAction()
 {
-       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;
@@ -871,6 +901,31 @@ void Cursor::pushBackward(Inset & p)
 }
 
 
+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);
@@ -1491,14 +1546,9 @@ void Cursor::niceInsert(MathAtom const & t)
        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);
@@ -1635,14 +1685,14 @@ bool Cursor::down()
 }
 
 
-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());
+       editInsertedInset();
 }
 
 
@@ -1672,7 +1722,7 @@ void Cursor::setTargetX()
 }
 
 
-bool Cursor::macroModeClose()
+bool Cursor::macroModeClose(bool cancel)
 {
        if (!inMacroMode())
                return false;
@@ -1684,14 +1734,15 @@ bool Cursor::macroModeClose()
        --pos();
        cell().erase(pos());
 
-       // do nothing if the macro name is empty
-       if (s == "\\")
-               return false;
-
        // trigger updates of macros, at least, if no full
        // updates take place anyway
        screenUpdateFlags(Update::Force);
 
+       // do nothing if the macro name is empty
+       if (s == "\\" || cancel) {
+               return false;
+       }
+
        docstring const name = s.substr(1);
        InsetMathNest * const in = inset().asInsetMath()->asNestInset();
        if (in && in->interpretString(*this, s))
@@ -1703,6 +1754,7 @@ bool Cursor::macroModeClose()
        // try to put argument into macro, if we just inserted a macro
        bool macroArg = false;
        InsetMathMacro * atomAsMacro = atom.nucleus()->asMacro();
+       InsetMathNest * atomAsNest = atom.nucleus()->asNestInset();
        if (atomAsMacro) {
                // macros here are still unfolded (in init mode in fact). So
                // we have to resolve the macro here manually and check its arity
@@ -1717,8 +1769,8 @@ bool Cursor::macroModeClose()
        }
 
        // insert remembered selection into first argument of a non-macro
-       else if (atom.nucleus()->nargs() > 0)
-               atom.nucleus()->cell(0).append(selection);
+       else if (atomAsNest && atomAsNest->nargs() > 0)
+               atomAsNest->cell(atomAsNest->firstIdx()).append(selection);
 
        MathWordList const & words = mathedWordList();
        MathWordList::const_iterator it = words.find(name);
@@ -2104,15 +2156,15 @@ bool Cursor::upDownInText(bool up, bool & updateNeeded)
 
        // with and without selection are handled differently
        if (!selection()) {
-               int yo = bv().getPos(*this).y_;
+               int yo1 = bv().getPos(*this).y_;
                Cursor old = *this;
                // To next/previous row
                // FIXME: the y position is often guessed wrongly across styles and
                // insets, which leads to weird behaviour.
                if (up)
-                       tm.editXY(*this, xo, yo - textRow().ascent() - 1);
+                       tm.editXY(*this, xo, yo1 - textRow().ascent() - 1);
                else
-                       tm.editXY(*this, xo, yo + textRow().descent() + 1);
+                       tm.editXY(*this, xo, yo1 + textRow().descent() + 1);
                x_target_ = old.x_target_;
                clearSelection();
 
@@ -2140,10 +2192,10 @@ bool Cursor::upDownInText(bool up, bool & updateNeeded)
                                --next_row;
                        } else if (pit() > 0) {
                                --pit();
-                               TextMetrics & tm = bv_->textMetrics(text());
-                               if (!tm.contains(pit()))
-                                       tm.newParMetricsUp();
-                               ParagraphMetrics const & pmcur = tm.parMetrics(pit());
+                               TextMetrics & tm2 = bv_->textMetrics(text());
+                               if (!tm2.contains(pit()))
+                                       tm2.newParMetricsUp();
+                               ParagraphMetrics const & pmcur = tm2.parMetrics(pit());
                                next_row = pmcur.rows().size() - 1;
                        }
                } else {
@@ -2151,9 +2203,9 @@ bool Cursor::upDownInText(bool up, bool & updateNeeded)
                                ++next_row;
                        } else if (pit() + 1 < int(text()->paragraphs().size())) {
                                ++pit();
-                               TextMetrics & tm = bv_->textMetrics(text());
-                               if (!tm.contains(pit()))
-                                       tm.newParMetricsDown();
+                               TextMetrics & tm2 = bv_->textMetrics(text());
+                               if (!tm2.contains(pit()))
+                                       tm2.newParMetricsDown();
                                next_row = 0;
                        }
                }
@@ -2231,6 +2283,12 @@ void Cursor::screenUpdateFlags(Update::flags f) const
 }
 
 
+void Cursor::noScreenUpdate() const
+{
+       disp_.screenUpdate(Update::None);
+}
+
+
 void Cursor::forceBufferUpdate() const
 {
        disp_.forceBufferUpdate();
@@ -2249,12 +2307,6 @@ bool Cursor::needBufferUpdate() const
 }
 
 
-void Cursor::noScreenUpdate() const
-{
-       disp_.screenUpdate(Update::None);
-}
-
-
 Font Cursor::getFont() const
 {
        // The logic here should more or less match to the