]> git.lyx.org Git - lyx.git/blobdiff - src/mathed/InsetMathScript.cpp
visual mode for bidi cursor movement
[lyx.git] / src / mathed / InsetMathScript.cpp
index 3fd94d11d88ad3d02724a09a9dd1463ac58ad15b..c146e071b15e7fc9008d1e2d99c63c420e401b1f 100644 (file)
 
 #include <config.h>
 
+#include "BufferView.h"
+#include "Cursor.h"
+#include "DispatchResult.h"
+#include "FuncRequest.h"
+#include "InsetMathFont.h"
 #include "InsetMathScript.h"
+#include "InsetMathSymbol.h"
 #include "MathData.h"
 #include "MathStream.h"
 #include "MathSupport.h"
-#include "InsetMathSymbol.h"
-#include "InsetMathFont.h"
-#include "DispatchResult.h"
-#include "Cursor.h"
+
 #include "support/debug.h"
-#include "FuncRequest.h"
 
 #include <boost/assert.hpp>
 
@@ -664,34 +666,54 @@ bool InsetMathScript::notifyCursorLeaves(Cursor & cur)
 
        //LYXERR0("InsetMathScript::notifyCursorLeaves: 1 " << cur);
 
-       // remove empty scripts if possible
-       if (nargs() > 2) {
-               // Case of two scripts. In this case, 1 = super, 2 = sub
-               if (cur.idx() == 2 && cell(2).empty()) {
+       // Remove empty scripts if possible:
+
+       // The case of two scripts, but only one got empty (1 = super, 2 = sub).
+       // We keep the script inset, but remove the empty script.
+       if (nargs() > 2 && (!cell(1).empty() || !cell(2).empty())) {
+               if (cell(2).empty()) {
                        // must be a subscript...
-                       cur.recordUndoInset();
                        removeScript(false);
+                       cur.updateFlags(cur.disp_.update() | Update::SinglePar);
                        return true;
-               } else if (cur.idx() == 1 && cell(1).empty()) {
+               } else if (cell(1).empty()) {
                        // must be a superscript...
-                       cur.recordUndoInset();
                        removeScript(true);
+                       cur.updateFlags(cur.disp_.update() | Update::SinglePar);
                        return true;
                }
-       } else if (nargs() > 1 && cur.idx() == 1 && cell(1).empty()) {
-               // could be either subscript or super script
-               cur.recordUndoInset();
-               removeScript(cell_1_is_up_);
+       }
+       // Now the two suicide cases:
+       // * we have only one script which is empty
+       // * we have two scripts which are both empty.
+       // The script inset is removed completely.
+       if ((nargs() == 2 && cell(1).empty())
+           || (nargs() == 3 && cell(1).empty() && cell(2).empty())) {
+               // Make undo step. We cannot use cur for this because
+               // it does not necessarily point to us. The BufferView
+               // cursor though should do.
+               int scriptSlice
+               = cur.bv().cursor().find(this);
+               BOOST_ASSERT(scriptSlice != -1);
+               Cursor & bvCur = cur.bv().cursor();
+               bvCur.cutOff(scriptSlice);
+               bvCur.recordUndoInset();
+
                // Let the script inset commit suicide. This is
                // modelled on Cursor.pullArg(), but tries not to
                // invoke notifyCursorLeaves again and does not touch
                // cur (since the top slice will be deleted
-               // afterwards))
+               // afterwards)
                MathData ar = cell(0);
-               Cursor tmpcur = cur;
-               tmpcur.pop();
-               tmpcur.cell().erase(tmpcur.pos());
-               tmpcur.cell().insert(tmpcur.pos(), ar);
+               bvCur.pop();
+               bvCur.cell().erase(bvCur.pos());
+               bvCur.cell().insert(bvCur.pos(), ar);
+
+               // put cursor behind
+               bvCur.pos() += ar.size();
+
+               // redraw
+               cur.updateFlags(cur.disp_.update() | Update::SinglePar);
                return true;
        }