]> git.lyx.org Git - lyx.git/blobdiff - src/mathed/InsetMathNest.cpp
simplify GuiToc / TocWidget interaction. Much can still be simplified...
[lyx.git] / src / mathed / InsetMathNest.cpp
index d2c98e901a9a019e6be973307986c584daa7ba63..d63f03f34138d23976bff9b5bd0cfa49e8c2ae9c 100644 (file)
@@ -20,7 +20,6 @@
 #include "InsetMathComment.h"
 #include "InsetMathDelim.h"
 #include "InsetMathHull.h"
-//#include "InsetMathMBox.h"
 #include "InsetMathRef.h"
 #include "InsetMathScript.h"
 #include "InsetMathSpace.h"
 #include "InsetMathUnknown.h"
 #include "MathData.h"
 #include "MathFactory.h"
+#include "MathMacro.h"
 #include "MathMacroArgument.h"
 #include "MathParser.h"
 #include "MathStream.h"
 #include "MathSupport.h"
 
-#include "bufferview_funcs.h"
+#include "Bidi.h"
 #include "BufferView.h"
-#include "Color.h"
 #include "CoordCache.h"
 #include "Cursor.h"
 #include "CutAndPaste.h"
-#include "debug.h"
+#include "support/debug.h"
 #include "DispatchResult.h"
 #include "FuncRequest.h"
 #include "FuncStatus.h"
-#include "gettext.h"
+#include "LyXFunc.h"
+#include "LyXRC.h"
+#include "support/gettext.h"
 #include "Text.h"
 #include "OutputParams.h"
-#include "Undo.h"
 
 #include "support/lstrings.h"
 #include "support/textutils.h"
+#include "support/docstream.h"
 
 #include "frontends/Clipboard.h"
 #include "frontends/Painter.h"
@@ -59,6 +60,8 @@
 
 #include <sstream>
 
+using namespace std;
+using namespace lyx::support;
 
 namespace lyx {
 
@@ -68,10 +71,6 @@ using cap::cutSelection;
 using cap::replaceSelection;
 using cap::selClearOrDel;
 
-using std::endl;
-using std::string;
-using std::istringstream;
-
 
 InsetMathNest::InsetMathNest(idx_type nargs)
        : cells_(nargs), lock_(false), mouse_hover_(false)
@@ -111,7 +110,7 @@ void InsetMathNest::cursorPos(BufferView const & bv,
 // to touch all (math)inset's draw() methods. Right now, we'll store
 // absolute value, and make them here relative, only to make them
 // absolute again when actually drawing the cursor. What a mess.
-       BOOST_ASSERT(ptr_cmp(&sl.inset(), this));
+       BOOST_ASSERT(&sl.inset() == this);
        MathData const & ar = sl.cell();
        CoordCache const & coord_cache = bv.coordCache();
        if (!coord_cache.getArrays().has(&ar)) {
@@ -132,8 +131,8 @@ void InsetMathNest::cursorPos(BufferView const & bv,
        }
        Point const pt2 = coord_cache.getInsets().xy(this);
        //lyxerr << "retrieving position cache for MathData "
-       //      << pt.x_ << ' ' << pt.y_ << std::endl;
-       x = pt.x_ - pt2.x_ + ar.pos2x(sl.pos());
+       //      << pt.x_ << ' ' << pt.y_ << endl;
+       x = pt.x_ - pt2.x_ + ar.pos2x(&bv, sl.pos());
        y = pt.y_ - pt2.y_;
 //     lyxerr << "pt.y_ : " << pt.y_ << " pt2_.y_ : " << pt2.y_
 //             << " asc: " << ascent() << "  des: " << descent()
@@ -147,14 +146,16 @@ void InsetMathNest::cursorPos(BufferView const & bv,
 void InsetMathNest::metrics(MetricsInfo const & mi) const
 {
        MetricsInfo m = mi;
-       for (idx_type i = 0, n = nargs(); i != n; ++i)
-               cell(i).metrics(m);
+       for (idx_type i = 0, n = nargs(); i != n; ++i) {
+               Dimension dim;
+               cell(i).metrics(m, dim);
+       }
 }
 
 
 bool InsetMathNest::idxNext(Cursor & cur) const
 {
-       BOOST_ASSERT(ptr_cmp(&cur.inset(), this));
+       BOOST_ASSERT(&cur.inset() == this);
        if (cur.idx() == cur.lastidx())
                return false;
        ++cur.idx();
@@ -163,7 +164,7 @@ bool InsetMathNest::idxNext(Cursor & cur) const
 }
 
 
-bool InsetMathNest::idxRight(Cursor & cur) const
+bool InsetMathNest::idxForward(Cursor & cur) const
 {
        return idxNext(cur);
 }
@@ -171,7 +172,7 @@ bool InsetMathNest::idxRight(Cursor & cur) const
 
 bool InsetMathNest::idxPrev(Cursor & cur) const
 {
-       BOOST_ASSERT(ptr_cmp(&cur.inset(), this));
+       BOOST_ASSERT(&cur.inset() == this);
        if (cur.idx() == 0)
                return false;
        --cur.idx();
@@ -180,7 +181,7 @@ bool InsetMathNest::idxPrev(Cursor & cur) const
 }
 
 
-bool InsetMathNest::idxLeft(Cursor & cur) const
+bool InsetMathNest::idxBackward(Cursor & cur) const
 {
        return idxPrev(cur);
 }
@@ -188,7 +189,7 @@ bool InsetMathNest::idxLeft(Cursor & cur) const
 
 bool InsetMathNest::idxFirst(Cursor & cur) const
 {
-       BOOST_ASSERT(ptr_cmp(&cur.inset(), this));
+       BOOST_ASSERT(&cur.inset() == this);
        if (nargs() == 0)
                return false;
        cur.idx() = 0;
@@ -199,7 +200,7 @@ bool InsetMathNest::idxFirst(Cursor & cur) const
 
 bool InsetMathNest::idxLast(Cursor & cur) const
 {
-       BOOST_ASSERT(ptr_cmp(&cur.inset(), this));
+       BOOST_ASSERT(&cur.inset() == this);
        if (nargs() == 0)
                return false;
        cur.idx() = cur.lastidx();
@@ -227,7 +228,7 @@ void InsetMathNest::draw(PainterInfo & pi, int x, int y) const
 #if 0
        if (lock_)
                pi.pain.fillRectangle(x, y - ascent(), width(), height(),
-                                       Color::mathlockbg);
+                                       Color_mathlockbg);
 #endif
        setPosCache(pi, x, y);
 }
@@ -240,13 +241,14 @@ void InsetMathNest::drawSelection(PainterInfo & pi, int x, int y) const
        Cursor & cur = bv.cursor();
        if (!cur.selection())
                return;
-       if (!ptr_cmp(&cur.inset(), this))
+       if (&cur.inset() != this)
                return;
 
        // FIXME: hack to get position cache warm
+       bool const original_drawing_state = pi.pain.isDrawingEnabled();
        pi.pain.setDrawingEnabled(false);
        draw(pi, x, y);
-       pi.pain.setDrawingEnabled(true);
+       pi.pain.setDrawingEnabled(original_drawing_state);
 
        CursorSlice s1 = cur.selBegin();
        CursorSlice s2 = cur.selEnd();
@@ -255,11 +257,12 @@ void InsetMathNest::drawSelection(PainterInfo & pi, int x, int y) const
        //      << " s1: " << s1 << " s2: " << s2 << endl;
        if (s1.idx() == s2.idx()) {
                MathData const & c = cell(s1.idx());
-               int x1 = c.xo(bv) + c.pos2x(s1.pos());
-               int y1 = c.yo(bv) - c.ascent();
-               int x2 = c.xo(bv) + c.pos2x(s2.pos());
-               int y2 = c.yo(bv) + c.descent();
-               pi.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, Color::selection);
+               Geometry const & g = bv.coordCache().getArrays().geometry(&c);
+               int x1 = g.pos.x_ + c.pos2x(pi.base.bv, s1.pos());
+               int y1 = g.pos.y_ - g.dim.ascent();
+               int x2 = g.pos.x_ + c.pos2x(pi.base.bv, s2.pos());
+               int y2 = g.pos.y_ + g.dim.descent();
+               pi.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, Color_selection);
        //lyxerr << "InsetMathNest::drawing selection 3: "
        //      << " x1: " << x1 << " x2: " << x2
        //      << " y1: " << y1 << " y2: " << y2 << endl;
@@ -267,11 +270,12 @@ void InsetMathNest::drawSelection(PainterInfo & pi, int x, int y) const
                for (idx_type i = 0; i < nargs(); ++i) {
                        if (idxBetween(i, s1.idx(), s2.idx())) {
                                MathData const & c = cell(i);
-                               int x1 = c.xo(bv);
-                               int y1 = c.yo(bv) - c.ascent();
-                               int x2 = c.xo(bv) + c.width();
-                               int y2 = c.yo(bv) + c.descent();
-                               pi.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, Color::selection);
+                               Geometry const & g = bv.coordCache().getArrays().geometry(&c);
+                               int x1 = g.pos.x_;
+                               int y1 = g.pos.y_ - g.dim.ascent();
+                               int x2 = g.pos.x_ + g.dim.width();
+                               int y2 = g.pos.y_ + g.dim.descent();
+                               pi.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, Color_selection);
                        }
                }
        }
@@ -369,9 +373,7 @@ bool InsetMathNest::setMouseHover(bool mouse_hover)
 
 bool InsetMathNest::notifyCursorLeaves(Cursor & /*cur*/)
 {
-#ifdef WITH_WARNINGS
-#warning look here
-#endif
+       // FIXME: look here
 #if 0
        MathData & ar = cur.cell();
        // remove base-only "scripts"
@@ -414,10 +416,10 @@ void InsetMathNest::handleFont
        // changes...
 
        if (cur.inset().asInsetMath()->name() == font) {
-               recordUndoInset(cur, Undo::ATOMIC);
+               cur.recordUndoInset();
                cur.handleFont(to_utf8(font));
        } else {
-               recordUndo(cur, Undo::ATOMIC);
+               cur.recordUndo();
                cur.handleNest(createInsetMath(font));
                cur.insert(arg);
        }
@@ -426,12 +428,12 @@ void InsetMathNest::handleFont
 
 void InsetMathNest::handleFont2(Cursor & cur, docstring const & arg)
 {
-       recordUndo(cur, Undo::ATOMIC);
+       cur.recordUndo();
        Font font;
        bool b;
-       bv_funcs::string2font(to_utf8(arg), font, b);
-       if (font.color() != Color::inherit) {
-               MathAtom at = MathAtom(new InsetMathColor(true, font.color()));
+       font.fromString(to_utf8(arg), b);
+       if (font.fontInfo().color() != Color_inherit) {
+               MathAtom at = MathAtom(new InsetMathColor(true, font.fontInfo().color()));
                cur.handleNest(at, 0);
        }
 }
@@ -439,13 +441,13 @@ void InsetMathNest::handleFont2(Cursor & cur, docstring const & arg)
 
 void InsetMathNest::doDispatch(Cursor & cur, FuncRequest & cmd)
 {
-       //lyxerr << "InsetMathNest: request: " << cmd << std::endl;
+       //lyxerr << "InsetMathNest: request: " << cmd << endl;
        //CursorSlice sl = cur.current();
 
        switch (cmd.action) {
 
        case LFUN_PASTE: {
-               recordUndo(cur);
+               cur.recordUndo();
                cur.message(_("Paste"));
                replaceSelection(cur);
                docstring topaste;
@@ -459,12 +461,12 @@ void InsetMathNest::doDispatch(Cursor & cur, FuncRequest & cmd)
                }
                cur.niceInsert(topaste);
                cur.clearSelection(); // bug 393
-               finishUndo();
+               cur.finishUndo();
                break;
        }
 
        case LFUN_CUT:
-               recordUndo(cur);
+               cur.recordUndo();
                cutSelection(cur, true, true);
                cur.message(_("Cut"));
                // Prevent stale position >= size crash
@@ -489,66 +491,84 @@ void InsetMathNest::doDispatch(Cursor & cur, FuncRequest & cmd)
                lfunMouseRelease(cur, cmd);
                break;
 
-       case LFUN_FINISHED_LEFT:
+       case LFUN_FINISHED_LEFT: // in math, left is backwards
+       case LFUN_FINISHED_BACKWARD:
                cur.bv().cursor() = cur;
                break;
 
-       case LFUN_FINISHED_RIGHT:
+       case LFUN_FINISHED_RIGHT: // in math, right is forward
+       case LFUN_FINISHED_FORWARD:
                ++cur.pos();
                cur.bv().cursor() = cur;
                break;
 
+       case LFUN_CHAR_RIGHT:
+       case LFUN_CHAR_LEFT:
+       case LFUN_CHAR_BACKWARD:
        case LFUN_CHAR_FORWARD:
                cur.updateFlags(Update::Decoration | Update::FitCursor);
-       case LFUN_CHAR_FORWARD_SELECT:
-               cur.selHandle(cmd.action == LFUN_CHAR_FORWARD_SELECT);
-               cur.autocorrect() = false;
-               cur.clearTargetX();
-               cur.macroModeClose();
-               if (reverseDirectionNeeded(cur))
-                       goto goto_char_backwards;
-
-goto_char_forwards:
-               if (cur.pos() != cur.lastpos() && cur.openable(cur.nextAtom())) {
-                       cur.pushLeft(*cur.nextAtom().nucleus());
-                       cur.inset().idxFirst(cur);
-               } else if (cur.posRight() || idxRight(cur)
-                       || cur.popRight() || cur.selection())
-                       ;
+       case LFUN_CHAR_RIGHT_SELECT:
+       case LFUN_CHAR_LEFT_SELECT:
+       case LFUN_CHAR_BACKWARD_SELECT:
+       case LFUN_CHAR_FORWARD_SELECT: {
+               // are we in a selection?
+               bool select = (cmd.action == LFUN_CHAR_RIGHT_SELECT 
+                                          || cmd.action == LFUN_CHAR_LEFT_SELECT
+                                          || cmd.action == LFUN_CHAR_BACKWARD_SELECT
+                                          || cmd.action == LFUN_CHAR_FORWARD_SELECT);
+               // are we moving forward or backwards? 
+               // If the command was RIGHT or LEFT, then whether we're moving forward
+               // or backwards depends on the cursor movement mode (logical or visual):
+               //  * in visual mode, since math is always LTR, right -> forward, 
+               //    left -> backwards
+               //  * in logical mode, the mapping is determined by the
+               //    reverseDirectionNeeded() function
+               
+               bool forward;
+               kb_action finish_lfun;
+
+               if (cmd.action == LFUN_CHAR_FORWARD 
+                               || cmd.action == LFUN_CHAR_FORWARD_SELECT) {
+                       forward = true;
+                       finish_lfun = LFUN_FINISHED_FORWARD;
+               }
+               else if (cmd.action == LFUN_CHAR_BACKWARD
+                               || cmd.action == LFUN_CHAR_BACKWARD_SELECT) {
+                       forward = false;
+                       finish_lfun = LFUN_FINISHED_BACKWARD;
+               }
                else {
-                       cmd = FuncRequest(LFUN_FINISHED_RIGHT);
-                       cur.undispatched();
+                       bool right = (cmd.action == LFUN_CHAR_RIGHT_SELECT
+                                                 || cmd.action == LFUN_CHAR_RIGHT);
+                       if (lyxrc.visual_cursor || !reverseDirectionNeeded(cur))
+                               forward = right;
+                       else 
+                               forward = !right;
+
+                       if (right)
+                               finish_lfun = LFUN_FINISHED_RIGHT;
+                       else
+                               finish_lfun = LFUN_FINISHED_LEFT;
                }
-               break;
-
-       case LFUN_CHAR_BACKWARD:
-               cur.updateFlags(Update::Decoration | Update::FitCursor);
-       case LFUN_CHAR_BACKWARD_SELECT:
-               cur.selHandle(cmd.action == LFUN_CHAR_BACKWARD_SELECT);
+               // Now that we know exactly what we want to do, let's do it!
+               cur.selHandle(select);
                cur.autocorrect() = false;
                cur.clearTargetX();
                cur.macroModeClose();
-               if (reverseDirectionNeeded(cur))
-                       goto goto_char_forwards;
-
-goto_char_backwards:
-               if (cur.pos() != 0 && cur.openable(cur.prevAtom())) {
-                       cur.posLeft();
-                       cur.push(*cur.nextAtom().nucleus());
-                       cur.inset().idxLast(cur);
-               } else if (cur.posLeft() || idxLeft(cur)
-                       || cur.popLeft() || cur.selection())
-                       ;
-               else {
-                       cmd = FuncRequest(LFUN_FINISHED_LEFT);
+               // try moving forward or backwards as necessary...
+               if (!(forward ? cursorMathForward(cur) : cursorMathBackward(cur))) {
+                       // ... and if movement failed, then finish forward or backwards
+                       // as necessary
+                       cmd = FuncRequest(finish_lfun);
                        cur.undispatched();
                }
                break;
+       }
 
        case LFUN_DOWN:
        case LFUN_UP:
                cur.updateFlags(Update::Decoration | Update::FitCursor);
-       case LFUN_DOWN_SELECT: 
+       case LFUN_DOWN_SELECT:
        case LFUN_UP_SELECT: {
                // close active macro
                if (cur.inMacroMode()) {
@@ -568,8 +588,16 @@ goto_char_backwards:
                        // notify left insets and give them chance to set update flags
                        lyx::notifyCursorLeaves(cur.beforeDispatchCursor(), cur);
                        cur.fixIfBroken();
-               }       else
-                       cur.undispatched();
+                       break;
+               }
+               
+               if (cur.fixIfBroken())
+                       // FIXME: Something bad happened. We pass the corrected Cursor
+                       // instead of letting things go worse.
+                       break;
+
+               // We did not manage to move the cursor.
+               cur.undispatched();
                break;
        }
 
@@ -582,7 +610,6 @@ goto_char_backwards:
                cur.selection() = true;
                cur.pos() = cur.lastpos();
                cur.idx() = cur.lastidx();
-               cap::saveSelection(cur);
                break;
 
        case LFUN_PARAGRAPH_UP:
@@ -594,10 +621,13 @@ goto_char_backwards:
 
        case LFUN_LINE_BEGIN:
        case LFUN_WORD_BACKWARD:
+       case LFUN_WORD_LEFT:
                cur.updateFlags(Update::Decoration | Update::FitCursor);
        case LFUN_LINE_BEGIN_SELECT:
        case LFUN_WORD_BACKWARD_SELECT:
+       case LFUN_WORD_LEFT_SELECT:
                cur.selHandle(cmd.action == LFUN_WORD_BACKWARD_SELECT ||
+                               cmd.action == LFUN_WORD_LEFT_SELECT || 
                                cmd.action == LFUN_LINE_BEGIN_SELECT);
                cur.macroModeClose();
                if (cur.pos() != 0) {
@@ -609,17 +639,20 @@ goto_char_backwards:
                        cur.idx() = 0;
                        cur.pos() = 0;
                } else {
-                       cmd = FuncRequest(LFUN_FINISHED_LEFT);
+                       cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
                        cur.undispatched();
                }
                break;
 
        case LFUN_WORD_FORWARD:
+       case LFUN_WORD_RIGHT:
        case LFUN_LINE_END:
                cur.updateFlags(Update::Decoration | Update::FitCursor);
        case LFUN_WORD_FORWARD_SELECT:
+       case LFUN_WORD_RIGHT_SELECT:
        case LFUN_LINE_END_SELECT:
                cur.selHandle(cmd.action == LFUN_WORD_FORWARD_SELECT ||
+                               cmd.action == LFUN_WORD_RIGHT_SELECT ||
                                cmd.action == LFUN_LINE_END_SELECT);
                cur.macroModeClose();
                cur.clearTargetX();
@@ -632,20 +665,18 @@ goto_char_backwards:
                        cur.idx() = cur.lastidx();
                        cur.pos() = cur.lastpos();
                } else {
-                       cmd = FuncRequest(LFUN_FINISHED_RIGHT);
+                       cmd = FuncRequest(LFUN_FINISHED_FORWARD);
                        cur.undispatched();
                }
                break;
 
        case LFUN_SCREEN_UP_SELECT:
-       case LFUN_SCREEN_UP:
-               cmd = FuncRequest(LFUN_FINISHED_LEFT);
+               cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
                cur.undispatched();
                break;
 
        case LFUN_SCREEN_DOWN_SELECT:
-       case LFUN_SCREEN_DOWN:
-               cmd = FuncRequest(LFUN_FINISHED_RIGHT);
+               cmd = FuncRequest(LFUN_FINISHED_FORWARD);
                cur.undispatched();
                break;
 
@@ -663,9 +694,9 @@ goto_char_backwards:
        case LFUN_CHAR_DELETE_BACKWARD:
                if (cur.pos() == 0)
                        // May affect external cell:
-                       recordUndoInset(cur, Undo::ATOMIC);
+                       cur.recordUndoInset();
                else
-                       recordUndo(cur, Undo::ATOMIC);
+                       cur.recordUndo();
                // if the inset can not be removed from within, delete it
                if (!cur.backspace()) {
                        FuncRequest cmd = FuncRequest(LFUN_CHAR_DELETE_FORWARD);
@@ -677,9 +708,9 @@ goto_char_backwards:
        case LFUN_CHAR_DELETE_FORWARD:
                if (cur.pos() == cur.lastpos())
                        // May affect external cell:
-                       recordUndoInset(cur, Undo::ATOMIC);
+                       cur.recordUndoInset();
                else
-                       recordUndo(cur, Undo::ATOMIC);
+                       cur.recordUndo();
                // if the inset can not be removed from within, delete it
                if (!cur.erase()) {
                        FuncRequest cmd = FuncRequest(LFUN_CHAR_DELETE_FORWARD);
@@ -691,20 +722,22 @@ goto_char_backwards:
                if (cur.selection())
                        cur.clearSelection();
                else  {
-                       cmd = FuncRequest(LFUN_FINISHED_RIGHT);
+                       cmd = FuncRequest(LFUN_FINISHED_FORWARD);
                        cur.undispatched();
                }
                break;
 
+       // 'Locks' the math inset. A 'locked' math inset behaves as a unit
+       // that is traversed by a single <CursorLeft>/<CursorRight>.
        case LFUN_INSET_TOGGLE:
-               recordUndo(cur);
+               cur.recordUndo();
                lock(!lock());
-               cur.popRight();
+               cur.popForward();
                break;
 
        case LFUN_SELF_INSERT:
                if (cmd.argument().size() != 1) {
-                       recordUndo(cur);
+                       cur.recordUndo();
                        docstring const arg = cmd.argument();
                        if (!interpretString(cur, arg))
                                cur.insert(arg);
@@ -720,7 +753,7 @@ goto_char_backwards:
                // A side effect is that an undo before the macro is finished
                // undoes the complete macro, not only the last character.
                if (!cur.inMacroMode())
-                       recordUndo(cur);
+                       cur.recordUndo();
 
                // spacial handling of space. If we insert an inset
                // via macro mode, we want to put the cursor inside it
@@ -730,11 +763,11 @@ goto_char_backwards:
                    && cur.macroModeClose()) {
                        MathAtom const atom = cur.prevAtom();
                        if (atom->asNestInset() && atom->isActive()) {
-                               cur.posLeft();
-                               cur.pushLeft(*cur.nextInset());
+                               cur.posBackward();
+                               cur.pushBackward(*cur.nextInset());
                        }
                } else if (!interpretChar(cur, cmd.argument()[0])) {
-                       cmd = FuncRequest(LFUN_FINISHED_RIGHT);
+                       cmd = FuncRequest(LFUN_FINISHED_FORWARD);
                        cur.undispatched();
                }
                break;
@@ -759,7 +792,7 @@ goto_char_backwards:
                if (cmd.argument().empty()) {
                        // do superscript if LyX handles
                        // deadkeys
-                       recordUndo(cur, Undo::ATOMIC);
+                       cur.recordUndo();
                        script(cur, true, grabAndEraseSelection(cur));
                }
                break;
@@ -810,7 +843,7 @@ goto_char_backwards:
                else
                        handleFont(cur, cmd.argument(), "mathrm");
                break;
-       case LFUN_FONT_CODE:
+       case LFUN_FONT_TYPEWRITER:
                if (currentMode() == TEXT_MODE)
                        handleFont(cur, cmd.argument(), "texttt");
                else
@@ -851,8 +884,8 @@ goto_char_backwards:
                selClearOrDel(cur);
                //cur.plainInsert(MathAtom(new InsetMathMBox(cur.bv())));
                cur.plainInsert(MathAtom(new InsetMathBox(from_ascii("mbox"))));
-               cur.posLeft();
-               cur.pushLeft(*cur.nextInset());
+               cur.posBackward();
+               cur.pushBackward(*cur.nextInset());
                cur.niceInsert(save_selection);
 #else
                if (currentMode() == Inset::TEXT_MODE) {
@@ -868,13 +901,13 @@ goto_char_backwards:
 
        case LFUN_MATH_SIZE:
 #if 0
-               recordUndo(cur);
+               cur.recordUndo();
                cur.setSize(arg);
 #endif
                break;
 
        case LFUN_MATH_MATRIX: {
-               recordUndo(cur, Undo::ATOMIC);
+               cur.recordUndo();
                unsigned int m = 1;
                unsigned int n = 1;
                docstring v_align;
@@ -893,13 +926,13 @@ goto_char_backwards:
 
        case LFUN_MATH_DELIM: {
                docstring ls;
-               docstring rs = support::split(cmd.argument(), ls, ' ');
+               docstring rs = split(cmd.argument(), ls, ' ');
                // Reasonable default values
                if (ls.empty())
                        ls = '(';
                if (rs.empty())
                        rs = ')';
-               recordUndo(cur, Undo::ATOMIC);
+               cur.recordUndo();
                cur.handleNest(MathAtom(new InsetMathDelim(ls, rs)));
                break;
        }
@@ -918,7 +951,7 @@ goto_char_backwards:
                // We mimic LFUN_MATH_DELIM in case we have an empty left
                // or right delimiter.
                if (have_l || have_r) {
-                       recordUndo(cur, Undo::ATOMIC);
+                       cur.recordUndo();
                        docstring const selection = grabAndEraseSelection(cur);
                        selClearOrDel(cur);
                        if (have_l)
@@ -936,31 +969,47 @@ goto_char_backwards:
 
        case LFUN_SPACE_INSERT:
        case LFUN_MATH_SPACE:
-               recordUndo(cur, Undo::ATOMIC);
+               cur.recordUndo();
                cur.insert(MathAtom(new InsetMathSpace(from_ascii(","))));
                break;
 
        case LFUN_ERT_INSERT:
                // interpret this as if a backslash was typed
-               recordUndo(cur, Undo::ATOMIC);
+               cur.recordUndo();
                interpretChar(cur, '\\');
                break;
 
        case LFUN_MATH_SUBSCRIPT:
                // interpret this as if a _ was typed
-               recordUndo(cur, Undo::ATOMIC);
+               cur.recordUndo();
                interpretChar(cur, '_');
                break;
 
        case LFUN_MATH_SUPERSCRIPT:
                // interpret this as if a ^ was typed
-               recordUndo(cur, Undo::ATOMIC);
+               cur.recordUndo();
                interpretChar(cur, '^');
                break;
+               
+       case LFUN_MATH_MACRO_FOLD:
+       case LFUN_MATH_MACRO_UNFOLD: {
+               Cursor it = cur;
+               bool fold = cmd.action == LFUN_MATH_MACRO_FOLD;
+               bool found = findMacroToFoldUnfold(it, fold);
+               if (found) {
+                       MathMacro * macro = it.nextInset()->asInsetMath()->asMacro();
+                       cur.recordUndoInset();
+                       if (fold)
+                               macro->fold(cur);
+                       else
+                               macro->unfold(cur);
+               }
+               break;
+       }
 
        case LFUN_QUOTE_INSERT:
                // interpret this as if a straight " was typed
-               recordUndo(cur, Undo::ATOMIC);
+               cur.recordUndo();
                interpretChar(cur, '\"');
                break;
 
@@ -968,7 +1017,7 @@ goto_char_backwards:
 // handling such that "self-insert" works on "arbitrary stuff" too, and
 // math-insert only handles special math things like "matrix".
        case LFUN_MATH_INSERT: {
-               recordUndo(cur, Undo::ATOMIC);
+               cur.recordUndo();
                if (cmd.argument() == "^" || cmd.argument() == "_") {
                        interpretChar(cur, cmd.argument()[0]);
                } else
@@ -983,19 +1032,25 @@ goto_char_backwards:
                        InsetMathRef tmp(name);
                        data = tmp.createDialogStr(to_utf8(name));
                }
-               cur.bv().showInsetDialog(to_utf8(name), data, 0);
+               cur.bv().showDialog(to_utf8(name), data);
                break;
        }
 
        case LFUN_INSET_INSERT: {
                MathData ar;
                if (createInsetMath_fromDialogStr(cmd.argument(), ar)) {
-                       recordUndo(cur);
+                       cur.recordUndo();
                        cur.insert(ar);
                } else
                        cur.undispatched();
                break;
        }
+       case LFUN_INSET_DISSOLVE:
+               if (!asHullInset()) {
+                       cur.recordUndoInset();
+                       cur.pullArg();
+               }
+               break;
 
        default:
                InsetMath::doDispatch(cur, cmd);
@@ -1004,6 +1059,40 @@ goto_char_backwards:
 }
 
 
+bool InsetMathNest::findMacroToFoldUnfold(Cursor & it, bool fold) const {
+       // look for macro to open/close, but stay in mathed
+       for (; !it.empty(); it.pop_back()) {
+                       
+               // go backward through the current cell
+               Inset * inset = it.nextInset();
+               while (inset && inset->asInsetMath()) {
+                       MathMacro * macro = inset->asInsetMath()->asMacro();
+                       if (macro) {
+                               // found the an macro to open/close?
+                               if (macro->folded() != fold)
+                                       return true;
+                               
+                               // Wrong folding state.
+                               // If this was the first we see in this slice, look further left,
+                               // otherwise go up.
+                               if (inset != it.nextInset())
+                                       break;
+                       }
+                       
+                       // go up if this was the left most position
+                       if (it.pos() == 0)
+                               break;
+                       
+                       // go left
+                       it.pos()--;
+                       inset = it.nextInset();
+               }
+       }
+       
+       return false;
+}
+
+
 bool InsetMathNest::getStatus(Cursor & cur, FuncRequest const & cmd,
                FuncStatus & flag) const
 {
@@ -1040,7 +1129,7 @@ bool InsetMathNest::getStatus(Cursor & cur, FuncRequest const & cmd,
        case LFUN_FONT_BOLD:
        case LFUN_FONT_SANS:
        case LFUN_FONT_EMPH:
-       case LFUN_FONT_CODE:
+       case LFUN_FONT_TYPEWRITER:
        case LFUN_FONT_NOUN:
        case LFUN_FONT_ROMAN:
        case LFUN_FONT_DEFAULT:
@@ -1055,8 +1144,6 @@ bool InsetMathNest::getStatus(Cursor & cur, FuncRequest const & cmd,
        case LFUN_MATH_SIZE:
        case LFUN_MATH_SPACE:
        case LFUN_MATH_LIMITS:
-       case LFUN_MATH_NONUMBER:
-       case LFUN_MATH_NUMBER:
        case LFUN_MATH_EXTERN:
                flag.enabled(true);
                break;
@@ -1095,16 +1182,24 @@ bool InsetMathNest::getStatus(Cursor & cur, FuncRequest const & cmd,
                // Don't do this with multi-cell selections
                flag.enabled(cur.selBegin().idx() == cur.selEnd().idx());
                break;
-
-       case LFUN_HYPHENATION_POINT_INSERT:
-       case LFUN_LIGATURE_BREAK_INSERT:
-       case LFUN_MENU_SEPARATOR_INSERT:
-       case LFUN_DOTS_INSERT:
-       case LFUN_END_OF_SENTENCE_PERIOD_INSERT:
+               
+       case LFUN_MATH_MACRO_FOLD:
+       case LFUN_MATH_MACRO_UNFOLD: {
+               Cursor it = cur;
+               bool found = findMacroToFoldUnfold(it, cmd.action == LFUN_MATH_MACRO_FOLD);
+               flag.enabled(found);
+               break;
+       }
+               
+       case LFUN_SPECIALCHAR_INSERT:
                // FIXME: These would probably make sense in math-text mode
                flag.enabled(false);
                break;
 
+       case LFUN_INSET_DISSOLVE:
+               flag.enabled(!asHullInset());
+               break;
+
        default:
                ret = false;
                break;
@@ -1113,11 +1208,13 @@ bool InsetMathNest::getStatus(Cursor & cur, FuncRequest const & cmd,
 }
 
 
-void InsetMathNest::edit(Cursor & cur, bool left)
+void InsetMathNest::edit(Cursor & cur, bool front, EntryDirection entry_from)
 {
        cur.push(*this);
-       cur.idx() = left ? 0 : cur.lastidx();
-       cur.pos() = left ? 0 : cur.lastpos();
+       bool enter_front = (entry_from == Inset::ENTRY_DIRECTION_RIGHT || 
+               (entry_from == Inset::ENTRY_DIRECTION_IGNORE && front));
+       cur.idx() = enter_front ? 0 : cur.lastidx();
+       cur.pos() = enter_front ? 0 : cur.lastpos();
        cur.resetAnchor();
        //lyxerr << "InsetMathNest::edit, cur:\n" << cur << endl;
 }
@@ -1137,7 +1234,7 @@ Inset * InsetMathNest::editXY(Cursor & cur, int x, int y)
        MathData & ar = cell(idx_min);
        cur.push(*this);
        cur.idx() = idx_min;
-       cur.pos() = ar.x2pos(x - ar.xo(cur.bv()));
+       cur.pos() = ar.x2pos(&cur.bv(), x - ar.xo(cur.bv()));
 
        //lyxerr << "found cell : " << idx_min << " pos: " << cur.pos() << endl;
        if (dist_min == 0) {
@@ -1154,29 +1251,32 @@ void InsetMathNest::lfunMousePress(Cursor & cur, FuncRequest & cmd)
 {
        //lyxerr << "## lfunMousePress: buttons: " << cmd.button() << endl;
        BufferView & bv = cur.bv();
+       bool do_selection = cmd.button() == mouse_button::button1
+               && cmd.argument() == "region-select";
+       bv.mouseSetCursor(cur, do_selection);
        if (cmd.button() == mouse_button::button1) {
                //lyxerr << "## lfunMousePress: setting cursor to: " << cur << endl;
-               bv.mouseSetCursor(cur);
                // Update the cursor update flags as needed:
                //
-               // Update::Decoration: tells to update the decoration (visual box
-               //                     corners that define the inset)/
-               // Update::FitCursor: adjust the screen to the cursor position if
-               //                    needed
+               // Update::Decoration: tells to update the decoration
+               //                     (visual box corners that define
+               //                     the inset)/
+               // Update::FitCursor: adjust the screen to the cursor
+               //                    position if needed
                // cur.result().update(): don't overwrite previously set flags.
-               cur.updateFlags(Update::Decoration | Update::FitCursor | cur.result().update());
+               cur.updateFlags(Update::Decoration | Update::FitCursor 
+                               | cur.result().update());
        } else if (cmd.button() == mouse_button::button2) {
-               MathData ar;
                if (cap::selection()) {
                        // See comment in Text::dispatch why we do this
                        cap::copySelectionToStack();
                        cmd = FuncRequest(LFUN_PASTE, "0");
-                       doDispatch(cur, cmd);
-               } else
+                       doDispatch(bv.cursor(), cmd);
+               } else {
+                       MathData ar;
                        asArray(theSelection().get(), ar);
-
-               cur.insert(ar);
-               bv.mouseSetCursor(cur);
+                       bv.cursor().insert(ar);
+               }
        }
 }
 
@@ -1207,7 +1307,6 @@ void InsetMathNest::lfunMouseRelease(Cursor & cur, FuncRequest & cmd)
                else {
                        Cursor & bvcur = cur.bv().cursor();
                        bvcur.selection() = true;
-                       cap::saveSelection(bvcur);
                }
                return;
        }
@@ -1364,7 +1463,7 @@ bool InsetMathNest::interpretChar(Cursor & cur, char_type c)
                        return true;
                }
 
-               if (cur.popRight()) {
+               if (cur.popForward()) {
                        // FIXME: we have to enable full redraw here because of the
                        // visual box corners that define the inset. If we know for
                        // sure that we stay within the same cell we can optimize for
@@ -1488,4 +1587,41 @@ bool InsetMathNest::script(Cursor & cur, bool up,
 }
 
 
+bool InsetMathNest::cursorMathForward(Cursor & cur)
+{
+       if (cur.pos() != cur.lastpos() && cur.openable(cur.nextAtom())) {
+               cur.pushBackward(*cur.nextAtom().nucleus());
+               cur.inset().idxFirst(cur);
+               return true;
+       } 
+       if (cur.posForward() || idxForward(cur) || cur.selection())
+               return true;
+       // try to pop forwards --- but don't pop out of math! leave that to
+       // the FINISH lfuns
+       int s = cur.depth() - 2;
+       if (s >= 0 && cur[s].inset().asInsetMath())
+               return cur.popForward();
+       return false;
+}
+
+
+bool InsetMathNest::cursorMathBackward(Cursor & cur)
+{
+       if (cur.pos() != 0 && cur.openable(cur.prevAtom())) {
+               cur.posBackward();
+               cur.push(*cur.nextAtom().nucleus());
+               cur.inset().idxLast(cur);
+               return true;
+       } 
+       if (cur.posBackward() || idxBackward(cur) || cur.selection())
+               return true;
+       // try to pop backwards --- but don't pop out of math! leave that to 
+       // the FINISH lfuns
+       int s = cur.depth() - 2;
+       if (s >= 0 && cur[s].inset().asInsetMath())
+               return cur.popBackward();
+       return false;
+}
+
+
 } // namespace lyx