]> git.lyx.org Git - lyx.git/blobdiff - src/mathed/math_cursor.C
Georg Baum's vspace change
[lyx.git] / src / mathed / math_cursor.C
index e628f4237cf23a4540de295cb7efc0a864cdbeb8..2d3400a699c5ea23095d0320112874d657b13960 100644 (file)
 
 #include <config.h>
 
-#include <lyxrc.h>
-
-#include "support/LAssert.h"
+#include "math_cursor.h"
+#include "lyxrc.h"
 #include "support/limited_stack.h"
+#include "dispatchresult.h"
 #include "debug.h"
 #include "support/std_sstream.h"
-#include "math_cursor.h"
 #include "formulabase.h"
 #include "funcrequest.h"
 #include "math_braceinset.h"
 #include "math_support.h"
 #include "math_unknowninset.h"
 
+#include <boost/assert.hpp>
 
 //#define FILEDEBUG 1
 
-using namespace lyx::support;
-
+using std::string;
 using std::endl;
+#ifndef CXX_GLOBAL_CSTD
 using std::isalpha;
+#endif
 using std::min;
 using std::swap;
 
@@ -435,8 +436,14 @@ bool MathCursor::backspace()
                }
        }
 
-       --pos();
-       plainErase();
+       if (hasPrevAtom() && prevAtom()->nargs() > 0) {
+               // let's require two backspaces for 'big stuff' and
+               // highlight on the first
+               left(true);
+       } else {
+               --pos();
+               plainErase();
+       }
        return true;
 }
 
@@ -618,10 +625,10 @@ void MathCursor::drawSelection(PainterInfo & pi) const
 }
 
 
-void MathCursor::handleNest(MathAtom const & a)
+void MathCursor::handleNest(MathAtom const & a, int c)
 {
        MathAtom at = a;
-       asArray(grabAndEraseSelection(), at.nucleus()->cell(0));
+       asArray(grabAndEraseSelection(), at.nucleus()->cell(c));
        insert(at);
        pushRight(prevAtom());
 }
@@ -824,28 +831,28 @@ bool MathCursor::hasNextAtom() const
 
 MathAtom const & MathCursor::prevAtom() const
 {
-       Assert(pos() > 0);
+       BOOST_ASSERT(pos() > 0);
        return array()[pos() - 1];
 }
 
 
 MathAtom & MathCursor::prevAtom()
 {
-       Assert(pos() > 0);
+       BOOST_ASSERT(pos() > 0);
        return array()[pos() - 1];
 }
 
 
 MathAtom const & MathCursor::nextAtom() const
 {
-       Assert(pos() < size());
+       BOOST_ASSERT(pos() < size());
        return array()[pos()];
 }
 
 
 MathAtom & MathCursor::nextAtom()
 {
-       Assert(pos() < size());
+       BOOST_ASSERT(pos() < size());
        return array()[pos()];
 }
 
@@ -911,14 +918,14 @@ void MathCursor::getSelection(CursorPos & i1, CursorPos & i2) const
 
 CursorPos & MathCursor::cursor()
 {
-       Assert(depth());
+       BOOST_ASSERT(depth());
        return Cursor_.back();
 }
 
 
 CursorPos const & MathCursor::cursor() const
 {
-       Assert(depth());
+       BOOST_ASSERT(depth());
        return Cursor_.back();
 }
 
@@ -939,28 +946,30 @@ bool MathCursor::goUpDown(bool up)
                xo = targetx_;
 
        // try neigbouring script insets
-       // try left
-       if (hasPrevAtom()) {
-               MathScriptInset const * p = prevAtom()->asScriptInset();
-               if (p && p->has(up)) {
-                       --pos();
-                       push(nextAtom());
-                       idx() = up; // the superscript has index 1
-                       pos() = size();
-                       ///lyxerr << "updown: handled by scriptinset to the left" << endl;
-                       return true;
+       if (!selection()) {
+               // try left
+               if (hasPrevAtom()) {
+                       MathScriptInset const * p = prevAtom()->asScriptInset();
+                       if (p && p->has(up)) {
+                               --pos();
+                               push(nextAtom());
+                               idx() = up; // the superscript has index 1
+                               pos() = size();
+                               //lyxerr << "updown: handled by scriptinset to the left" << endl;
+                               return true;
+                       }
                }
-       }
 
-       // try right
-       if (hasNextAtom()) {
-               MathScriptInset const * p = nextAtom()->asScriptInset();
-               if (p && p->has(up)) {
-                       push(nextAtom());
-                       idx() = up;
-                       pos() = 0;
-                       ///lyxerr << "updown: handled by scriptinset to the right" << endl;
-                       return true;
+               // try right
+               if (hasNextAtom()) {
+                       MathScriptInset const * p = nextAtom()->asScriptInset();
+                       if (p && p->has(up)) {
+                               push(nextAtom());
+                               idx() = up;
+                               pos() = 0;
+                               //lyxerr << "updown: handled by scriptinset to the right" << endl;
+                               return true;
+                       }
                }
        }
 
@@ -980,7 +989,7 @@ bool MathCursor::goUpDown(bool up)
 
        // try to find an inset that knows better then we
        while (1) {
-               ///lyxerr << "updown: We are in " << *inset() << " idx: " << idx() << endl;
+               //lyxerr << "updown: We are in " << inset() << " idx: " << idx() << endl;
                // ask inset first
                if (inset()->idxUpDown(idx(), pos(), up, targetx_)) {
                        // try to find best position within this inset
@@ -990,7 +999,7 @@ bool MathCursor::goUpDown(bool up)
                }
 
                // no such inset found, just take something "above"
-               ///lyxerr << "updown: handled by strange case" << endl;
+               //lyxerr << "updown: handled by strange case" << endl;
                if (!popLeft())
                        return
                                bruteFind(xo, yo,
@@ -1239,7 +1248,7 @@ bool MathCursor::interpret(char c)
        }
 
        if (c == '{' || c == '}' || c == '#' || c == '&' || c == '$') {
-               createMathInset(string(1, c));
+               niceInsert(createMathInset(string(1, c)));
                return true;
        }
 
@@ -1292,7 +1301,7 @@ string MathCursor::info() const
        if (hasPrevAtom())
                prevAtom()->infoize2(os);
        os << "                    ";
-       return STRCONV(os.str());
+       return os.str();
 }
 
 
@@ -1393,7 +1402,7 @@ CursorPos MathCursor::normalAnchor() const
                Anchor_ = Cursor_;
                lyxerr << "unusual Anchor size" << endl;
        }
-       //lyx::Assert(Anchor_.size() >= cursor.depth());
+       //lyx::BOOST_ASSERT(Anchor_.size() >= cursor.depth());
        // use Anchor on the same level as Cursor
        CursorPos normal = Anchor_[depth() - 1];
        if (depth() < Anchor_.size() && !(normal < cursor())) {
@@ -1404,7 +1413,7 @@ CursorPos MathCursor::normalAnchor() const
 }
 
 
-dispatch_result MathCursor::dispatch(FuncRequest const & cmd)
+DispatchResult MathCursor::dispatch(FuncRequest const & cmd)
 {
        // mouse clicks are somewhat special
        // check
@@ -1414,17 +1423,19 @@ dispatch_result MathCursor::dispatch(FuncRequest const & cmd)
                case LFUN_MOUSE_RELEASE:
                case LFUN_MOUSE_DOUBLE: {
                        CursorPos & pos = Cursor_.back();
-                       dispatch_result res = UNDISPATCHED;
-                       int x = 0, y = 0;
+                       int x = 0;
+                       int y = 0;
                        getPos(x, y);
                        if (x < cmd.x && hasPrevAtom()) {
-                               res = prevAtom().nucleus()->dispatch(cmd, pos.idx_, pos.pos_);
-                               if (res != UNDISPATCHED)
+                               DispatchResult const res =
+                                       prevAtom().nucleus()->dispatch(cmd, pos.idx_, pos.pos_);
+                               if (res.dispatched())
                                        return res;
                        }
                        if (x > cmd.x && hasNextAtom()) {
-                               res = nextAtom().nucleus()->dispatch(cmd, pos.idx_, pos.pos_);
-                               if (res != UNDISPATCHED)
+                               DispatchResult const res =
+                                       nextAtom().nucleus()->dispatch(cmd, pos.idx_, pos.pos_);
+                               if (res.dispatched())
                                        return res;
                        }
                }
@@ -1434,16 +1445,17 @@ dispatch_result MathCursor::dispatch(FuncRequest const & cmd)
 
        for (int i = Cursor_.size() - 1; i >= 0; --i) {
                CursorPos & pos = Cursor_[i];
-               dispatch_result res = pos.inset_->dispatch(cmd, pos.idx_, pos.pos_);
-               if (res != UNDISPATCHED) {
-                       if (res == DISPATCHED_POP) {
+               DispatchResult const res =
+                       pos.inset_->dispatch(cmd, pos.idx_, pos.pos_);
+               if (res.dispatched()) {
+                       if (res.val() == FINISHED) {
                                Cursor_.shrink(i + 1);
                                selClear();
                        }
                        return res;
                }
        }
-       return UNDISPATCHED;
+       return DispatchResult(false);
 }