]> 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 8b16128444aac97ea92545b8abe38bb7c9cdb153..2d3400a699c5ea23095d0320112874d657b13960 100644 (file)
@@ -1,41 +1,29 @@
-/*
- *  File:        math_cursor.C
- *  Purpose:     Interaction for mathed
- *  Author:      Alejandro Aguilar Sierra <asierra@servidor.unam.mx>
- *  Created:     January 1996
- *  Description: Math interaction for a WYSIWYG math editor.
+/**
+ * \file math_cursor.C
+ * This file is part of LyX, the document processor.
+ * Licence details can be found in the file COPYING.
  *
- *  Dependencies: Xlib, XForms
+ * \author Alejandro Aguilar Sierra
+ * \author André Pönitz
  *
- *  Copyright: 1996, Alejandro Aguilar Sierra
- *
- *   Version: 0.8beta, Math & Lyx project.
- *
- *   You are free to use and modify this code under the terms of
- *   the GNU General Public Licence version 2 or later.
+ * Full author contact details are available in file CREDITS.
  */
 
 #include <config.h>
-#include <lyxrc.h>
 
-#include "support/lstrings.h"
-#include "support/LAssert.h"
+#include "math_cursor.h"
+#include "lyxrc.h"
 #include "support/limited_stack.h"
+#include "dispatchresult.h"
 #include "debug.h"
-#include "Lsstream.h"
-#include "frontends/Painter.h"
-#include "math_cursor.h"
+#include "support/std_sstream.h"
 #include "formulabase.h"
 #include "funcrequest.h"
-#include "math_autocorrect.h"
 #include "math_braceinset.h"
 #include "math_commentinset.h"
 #include "math_charinset.h"
-#include "math_extern.h"
 #include "math_factory.h"
-#include "math_fontinset.h"
 #include "math_gridinset.h"
-#include "math_iterator.h"
 #include "math_macroarg.h"
 #include "math_macrotemplate.h"
 #include "math_mathmlstream.h"
 #include "math_support.h"
 #include "math_unknowninset.h"
 
-#include <algorithm>
-#include <cctype>
+#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::max;
 using std::swap;
-using std::vector;
+
 using std::ostringstream;
-using std::isalpha;
 
 
 // matheds own cut buffer
@@ -88,7 +75,7 @@ void MathCursor::push(MathAtom & t)
 
 void MathCursor::pushLeft(MathAtom & t)
 {
-       //lyxerr << "Entering atom " << t << " left\n";
+       //lyxerr << "Entering atom " << t << " left" << endl;
        push(t);
        t->idxFirst(idx(), pos());
 }
@@ -96,7 +83,7 @@ void MathCursor::pushLeft(MathAtom & t)
 
 void MathCursor::pushRight(MathAtom & t)
 {
-       //lyxerr << "Entering atom " << t << " right\n";
+       //lyxerr << "Entering atom " << t << " right" << endl;
        posLeft();
        push(t);
        t->idxLast(idx(), pos());
@@ -105,7 +92,7 @@ void MathCursor::pushRight(MathAtom & t)
 
 bool MathCursor::popLeft()
 {
-       //lyxerr << "Leaving atom to the left\n";
+       //lyxerr << "Leaving atom to the left" << endl;
        if (depth() <= 1) {
                if (depth() == 1)
                        inset()->notifyCursorLeaves(idx());
@@ -119,7 +106,7 @@ bool MathCursor::popLeft()
 
 bool MathCursor::popRight()
 {
-       //lyxerr << "Leaving atom "; inset()->write(cerr, false); cerr << " right\n";
+       //lyxerr << "Leaving atom "; inset()->write(cerr, false); cerr << " right" << endl;
        if (depth() <= 1) {
                if (depth() == 1)
                        inset()->notifyCursorLeaves(idx());
@@ -328,14 +315,14 @@ void MathCursor::plainErase()
 
 void MathCursor::markInsert()
 {
-       //lyxerr << "inserting mark\n";
+       //lyxerr << "inserting mark" << endl;
        array().insert(pos(), MathAtom(new MathCharInset(0)));
 }
 
 
 void MathCursor::markErase()
 {
-       //lyxerr << "deleting mark\n";
+       //lyxerr << "deleting mark" << endl;
        array().erase(pos());
 }
 
@@ -358,7 +345,7 @@ void MathCursor::insert2(string const & str)
 
 void MathCursor::insert(string const & str)
 {
-       //lyxerr << "inserting '" << str << "'\n";
+       //lyxerr << "inserting '" << str << "'" << endl;
        selClearOrDel();
        for (string::const_iterator it = str.begin(); it != str.end(); ++it)
                plainInsert(MathAtom(new MathCharInset(*it)));
@@ -367,7 +354,7 @@ void MathCursor::insert(string const & str)
 
 void MathCursor::insert(char c)
 {
-       //lyxerr << "inserting '" << c << "'\n";
+       //lyxerr << "inserting '" << c << "'" << endl;
        selClearOrDel();
        plainInsert(MathAtom(new MathCharInset(c)));
 }
@@ -449,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;
 }
 
@@ -533,7 +526,7 @@ void MathCursor::macroModeClose()
        // prevent entering of recursive macros
        if (formula()->lyxCode() == InsetOld::MATHMACRO_CODE
                        && formula()->getInsetName() == name)
-               lyxerr << "can't enter recursive macro\n";
+               lyxerr << "can't enter recursive macro" << endl;
 
        niceInsert(createMathInset(name));
 }
@@ -632,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());
 }
@@ -838,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()];
 }
 
@@ -869,12 +862,12 @@ MathArray & MathCursor::array() const
        static MathArray dummy;
 
        if (idx() >= inset()->nargs()) {
-               lyxerr << "############  idx_ " << idx() << " not valid\n";
+               lyxerr << "############  idx_ " << idx() << " not valid" << endl;
                return dummy;
        }
 
        if (depth() == 0) {
-               lyxerr << "############  depth() == 0 not valid\n";
+               lyxerr << "############  depth() == 0 not valid" << endl;
                return dummy;
        }
 
@@ -925,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();
 }
 
@@ -953,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\n";
-                       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\n";
-                       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;
+                       }
                }
        }
 
@@ -988,13 +983,13 @@ bool MathCursor::goUpDown(bool up)
        //else
        //      ylow = yo + 4;
        //if (bruteFind(xo, yo, xlow, xhigh, ylow, yhigh)) {
-       //      lyxerr << "updown: handled by brute find in the same cell\n";
+       //      lyxerr << "updown: handled by brute find in the same cell" << endl;
        //      return true;
        //}
 
        // try to find an inset that knows better then we
        while (1) {
-               ///lyxerr << "updown: We are in " << *inset() << " idx: " << idx() << '\n';
+               //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
@@ -1004,7 +999,7 @@ bool MathCursor::goUpDown(bool up)
                }
 
                // no such inset found, just take something "above"
-               ///lyxerr << "updown: handled by strange case\n";
+               //lyxerr << "updown: handled by strange case" << endl;
                if (!popLeft())
                        return
                                bruteFind(xo, yo,
@@ -1149,7 +1144,7 @@ bool MathCursor::script(bool up)
 
 bool MathCursor::interpret(char c)
 {
-       //lyxerr << "interpret 2: '" << c << "'\n";
+       //lyxerr << "interpret 2: '" << c << "'" << endl;
        targetx_ = -1; // "no target"
        if (inMacroArgMode()) {
                --pos();
@@ -1168,7 +1163,7 @@ bool MathCursor::interpret(char c)
        // handle macroMode
        if (inMacroMode()) {
                string name = macroName();
-               //lyxerr << "interpret name: '" << name << "'\n";
+               //lyxerr << "interpret name: '" << name << "'" << endl;
 
                if (isalpha(c)) {
                        activeMacro()->setName(activeMacro()->name() + c);
@@ -1221,7 +1216,7 @@ bool MathCursor::interpret(char c)
        selClearOrDel();
 
        if (c == '\\') {
-               //lyxerr << "starting with macro\n";
+               //lyxerr << "starting with macro" << endl;
                insert(MathAtom(new MathUnknownInset("\\", false)));
                return true;
        }
@@ -1253,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;
        }
 
@@ -1306,7 +1301,7 @@ string MathCursor::info() const
        if (hasPrevAtom())
                prevAtom()->infoize2(os);
        os << "                    ";
-       return STRCONV(os.str());
+       return os.str();
 }
 
 
@@ -1405,9 +1400,9 @@ CursorPos MathCursor::normalAnchor() const
 {
        if (Anchor_.size() < depth()) {
                Anchor_ = Cursor_;
-               lyxerr << "unusual Anchor size\n";
+               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())) {
@@ -1418,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
@@ -1428,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;
                        }
                }
@@ -1448,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);
 }