]> git.lyx.org Git - lyx.git/blobdiff - src/text3.C
more cursor dispatch
[lyx.git] / src / text3.C
index 05148e2f5c79598c7d635e0e1221b9151790a28f..ca40002a96bcbb4a3ab84564e511a76c51165dfb 100644 (file)
@@ -48,7 +48,7 @@
 #include "support/tostr.h"
 #include "support/std_sstream.h"
 
-#include "mathed/formulabase.h"
+#include "mathed/math_hullinset.h"
 
 #include <clocale>
 
@@ -69,7 +69,7 @@ using std::vector;
 extern string current_layout;
 
 // the selection possible is needed, that only motion events are
-// used, where the bottom press event was on the drawing area too
+// used, where the button press event was on the drawing area too
 bool selection_possible = false;
 
 
@@ -91,16 +91,16 @@ namespace {
 
                if (font.language() != ignore_language ||
                                font.number() != LyXFont::IGNORE) {
-                       LyXCursor & cursor = text->cursor;
+                       CursorSlice & cur = text->cursor();
                        Paragraph & par = *text->cursorPar();
                        text->bidi.computeTables(par, *bv->buffer(),
-                               *par.getRow(cursor.pos()));
-                       if (cursor.boundary() !=
+                               *par.getRow(cur.pos()));
+                       if (cur.boundary() !=
                                        text->bidi.isBoundary(*bv->buffer(), par,
-                                                       cursor.pos(),
+                                                       cur.pos(),
                                                        text->real_current_font))
-                               text->setCursor(cursor.par(), cursor.pos(),
-                                               false, !cursor.boundary());
+                               text->setCursor(cur.par(), cur.pos(),
+                                               false, !cur.boundary());
                }
        }
 
@@ -213,26 +213,20 @@ namespace {
        }
 
 
-       void moveCursor(BufferView * bv, bool selecting)
+       void moveCursor(LCursor & cur, bool selecting)
        {
-               LyXText * lt = bv->getLyXText();
-
-//             if (!lt->selection.set())
-//                     lt->selection.cursor = lt->cursor;
-
-               if (selecting || lt->selection.mark())
-                       lt->setSelection();
-
-               if (!lt->selection.set())
-                       bv->haveSelection(false);
-               bv->switchKeyMap();
+               if (selecting || cur.mark())
+                       cur.setSelection();
+               if (!cur.selection())
+                       cur.bv().haveSelection(false);
+               cur.bv().switchKeyMap();
        }
 
 
        void finishChange(BufferView * bv, bool selecting = false)
        {
                finishUndo();
-               moveCursor(bv, selecting);
+               moveCursor(bv->cursor(), selecting);
                bv->owner()->view_state_changed();
        }
 
@@ -252,14 +246,14 @@ string const freefont2string()
 }
 
 
-InsetOld * LyXText::checkInsetHit(int x, int y)
+InsetBase * LyXText::checkInsetHit(int x, int y)
 {
        ParagraphList::iterator pit;
        ParagraphList::iterator end;
 
        getParsInRange(paragraphs(),
-                      bv()->top_y() - yo_,
-                      bv()->top_y() - yo_ + bv()->workHeight(),
+                      bv()->top_y(),
+                      bv()->top_y() + bv()->workHeight(),
                       pit, end);
 
        lyxerr << "checkInsetHit: x: " << x << " y: " << y << endl;
@@ -267,18 +261,15 @@ InsetOld * LyXText::checkInsetHit(int x, int y)
                InsetList::iterator iit = pit->insetlist.begin();
                InsetList::iterator iend = pit->insetlist.end();
                for ( ; iit != iend; ++iit) {
-                       InsetOld * inset = iit->inset;
-                       //lyxerr << "examining inset " << inset
-                       //      << " xy: " << inset->x() << "/" << inset->y()
-                       //      << " x: " << inset->x() << "..." << inset->x() + inset->width()
-                       //      << " y: " << inset->y() - inset->ascent() << "..."
-                       //      << inset->y() + inset->descent()
-                       //      << endl;
-                       if (x >= inset->x()
-                           && x <= inset->x() + inset->width()
-                           && y >= inset->y() - inset->ascent()
-                           && y <= inset->y() + inset->descent())
-                       {
+                       InsetBase * inset = iit->inset;
+#if 1
+                       lyxerr << "examining inset " << inset
+                       //<< " xo/yo: " << inset->xo() << "/" << inset->yo()
+                               << " xo: " << inset->xo() << "..." << inset->xo() + inset->width()
+                               << " yo: " << inset->yo() - inset->ascent() << "..."
+                               << inset->yo() + inset->descent() << endl;
+#endif
+                       if (inset->covers(x, y - bv()->top_y())) {
                                lyxerr << "Hit inset: " << inset << endl;
                                return inset;
                        }
@@ -289,14 +280,14 @@ InsetOld * LyXText::checkInsetHit(int x, int y)
 }
 
 
-bool LyXText::gotoNextInset(vector<InsetOld::Code> const & codes,
+bool LyXText::gotoNextInset(vector<InsetOld_code> const & codes,
                            string const & contents)
 {
        ParagraphList::iterator end = paragraphs().end();
        ParagraphList::iterator pit = cursorPar();
-       pos_type pos = cursor.pos();
+       pos_type pos = cursor().pos();
 
-       InsetOld * inset;
+       InsetBase * inset;
        do {
                if (pos + 1 < pit->size()) {
                        ++pos;
@@ -321,55 +312,56 @@ bool LyXText::gotoNextInset(vector<InsetOld::Code> const & codes,
 }
 
 
-void LyXText::gotoInset(vector<InsetOld::Code> const & codes,
-                       bool same_content)
+void LyXText::gotoInset(vector<InsetOld_code> const & codes, bool same_content)
 {
-       clearSelection();
+       LCursor & cur = bv()->cursor();
+       cur.clearSelection();
 
        string contents;
-       if (same_content && cursor.pos() < cursorPar()->size()
-           && cursorPar()->isInset(cursor.pos())) {
-               InsetOld const * inset = cursorPar()->getInset(cursor.pos());
+       if (same_content
+           && cur.pos() < cur.lastpos()
+           && cur.paragraph().isInset(cur.pos())) {
+               InsetBase const * inset = cur.paragraph().getInset(cur.pos());
                if (find(codes.begin(), codes.end(), inset->lyxCode())
                    != codes.end())
                        contents = static_cast<InsetCommand const *>(inset)->getContents();
        }
 
        if (!gotoNextInset(codes, contents)) {
-               if (cursor.pos() || cursorPar() != paragraphs().begin()) {
-                       LyXCursor tmp = cursor;
-                       cursor.par(0);
-                       cursor.pos(0);
+               if (cur.pos() || cur.par() != 0) {
+                       CursorSlice tmp = cursor();
+                       cur.par() = 0;
+                       cur.pos() = 0;
                        if (!gotoNextInset(codes, contents)) {
-                               cursor = tmp;
-                               bv()->owner()->message(_("No more insets"));
+                               cursor() = tmp;
+                               cur.bv().owner()->message(_("No more insets"));
                        }
                } else {
-                       bv()->owner()->message(_("No more insets"));
+                       cur.bv().owner()->message(_("No more insets"));
                }
        }
-       bv()->update();
-       selection.cursor = cursor;
+       cur.bv().update();
+       cur.resetAnchor();
 }
 
 
-void LyXText::gotoInset(InsetOld::Code code, bool same_content)
+void LyXText::gotoInset(InsetOld_code code, bool same_content)
 {
-       gotoInset(vector<InsetOld::Code>(1, code), same_content);
+       gotoInset(vector<InsetOld_code>(1, code), same_content);
 }
 
 
 void LyXText::cursorPrevious()
 {
+       LCursor & cur = bv()->cursor();
+       pos_type cpos = cur.pos();
+       lyx::paroffset_type cpar = cur.par();
 
-       RowList::iterator crit = cursorRow();
-       ParagraphList::iterator cpar = cursorPar();
-
-       int x = bv()->x_target() - xo_;
-       int y = bv()->top_y() - yo_;
+       int x = bv()->cursor().x_target();
+       int y = bv()->top_y();
        setCursorFromCoordinates(x, y);
 
-       if (cpar == cursorPar() && crit == cursorRow()) {
+       if (cpar == cur.par() && cpos == cur.pos()) {
                // we have a row which is taller than the workarea. The
                // simplest solution is to move to the previous row instead.
                cursorUp(true);
@@ -382,14 +374,15 @@ void LyXText::cursorPrevious()
 
 void LyXText::cursorNext()
 {
-       RowList::iterator crit = cursorRow();
-       ParagraphList::iterator cpar = cursorPar();
+       LCursor & cur = bv()->cursor();
+       pos_type cpos = cur.pos();
+       lyx::paroffset_type cpar = cur.par();
 
-       int x = bv()->x_target() - xo_;
-       int y = bv()->top_y() + bv()->workHeight() - yo_;
+       int x = cur.x_target();
+       int y = bv()->top_y() + bv()->workHeight();
        setCursorFromCoordinates(x, y);
 
-       if (cpar == cursorPar() && crit == cursorRow()) {
+       if (cpar == cur.par() && cpos == cur.pos()) {
                // we have a row which is taller than the workarea. The
                // simplest solution is to move to the next row instead.
                cursorDown(true);
@@ -402,11 +395,11 @@ void LyXText::cursorNext()
 
 namespace {
 
-void specialChar(LyXText * lt, BufferView * bv, InsetSpecialChar::Kind kind)
+void specialChar(LyXText * text, BufferView * bv, InsetSpecialChar::Kind kind)
 {
        bv->update();
        InsetSpecialChar * new_inset = new InsetSpecialChar(kind);
-       replaceSelection(lt);
+       replaceSelection(text);
        if (!bv->insertInset(new_inset))
                delete new_inset;
        else
@@ -414,27 +407,26 @@ void specialChar(LyXText * lt, BufferView * bv, InsetSpecialChar::Kind kind)
 }
 
 
-void doInsertInset(LyXText const & lt, FuncRequest const & cmd,
-                  bool edit, bool pastesel)
+void doInsertInset(BufferView * bv, FuncRequest const & cmd,
+       bool edit, bool pastesel)
 {
-       InsetOld * inset = createInset(cmd);
+       InsetBase * inset = createInset(bv, cmd);
        if (!inset)
                return;
 
-       BufferView * bv = cmd.view();
-
        bool gotsel = false;
-       if (lt.selection.set()) {
+       if (bv->cursor().selection()) {
                bv->owner()->dispatch(FuncRequest(LFUN_CUT));
                gotsel = true;
        }
        if (bv->insertInset(inset)) {
                if (edit)
-                       inset->edit(bv, true);
+                       inset->edit(bv->cursor(), true);
                if (gotsel && pastesel)
                        bv->owner()->dispatch(FuncRequest(LFUN_PASTE));
-       } else
+       } else {
                delete inset;
+       }
 }
 
 } // anon namespace
@@ -454,11 +446,12 @@ bool LyXText::rtl() const
 }
 
 
-DispatchResult LyXText::dispatch(FuncRequest const & cmd)
+DispatchResult LyXText::dispatch(LCursor & cur, FuncRequest const & cmd)
 {
-       //lyxerr[Debug::ACTION] << "LyXText::dispatch: cmd: " << cmd << endl;
+       lyxerr[Debug::ACTION] << "LyXText::dispatch: cmd: " << cmd << endl;
+       //lyxerr << "*** LyXText::dispatch: cmd: " << cmd << endl;
 
-       BufferView * bv = cmd.view();
+       BufferView * bv = &cur.bv();
 
        switch (cmd.action) {
 
@@ -490,26 +483,26 @@ DispatchResult LyXText::dispatch(FuncRequest const & cmd)
        }
 
        case LFUN_DELETE_WORD_FORWARD:
-               clearSelection();
+               cur.clearSelection();
                deleteWordForward();
                finishChange(bv);
                break;
 
        case LFUN_DELETE_WORD_BACKWARD:
-               clearSelection();
+               cur.clearSelection();
                deleteWordBackward();
                finishChange(bv);
                break;
 
        case LFUN_DELETE_LINE_FORWARD:
-               clearSelection();
+               cur.clearSelection();
                deleteLineForward();
                finishChange(bv);
                break;
 
        case LFUN_WORDRIGHT:
-               if (!selection.mark())
-                       clearSelection();
+               if (!cur.mark())
+                       cur.clearSelection();
                if (rtl())
                        cursorLeftOneWord();
                else
@@ -518,8 +511,8 @@ DispatchResult LyXText::dispatch(FuncRequest const & cmd)
                break;
 
        case LFUN_WORDLEFT:
-               if (!selection.mark())
-                       clearSelection();
+               if (!cur.mark())
+                       cur.clearSelection();
                if (rtl())
                        cursorRightOneWord();
                else
@@ -528,96 +521,98 @@ DispatchResult LyXText::dispatch(FuncRequest const & cmd)
                break;
 
        case LFUN_BEGINNINGBUF:
-               if (!selection.mark())
-                       clearSelection();
+               if (!cur.mark())
+                       cur.clearSelection();
                cursorTop();
                finishChange(bv);
                break;
 
        case LFUN_ENDBUF:
-               if (selection.mark())
-                       clearSelection();
+               if (!cur.mark())
+                       cur.clearSelection();
                cursorBottom();
                finishChange(bv);
                break;
 
        case LFUN_RIGHTSEL:
-               if (!selection.set())
-                       selection.cursor = cursor;
+               if (!cur.selection())
+                       cur.resetAnchor();
                if (rtl())
-                       cursorLeft(bv);
+                       cursorLeft(true);
                else
-                       cursorRight(bv);
+                       cursorRight(true);
                finishChange(bv, true);
                break;
 
        case LFUN_LEFTSEL:
-               if (!selection.set())
-                       selection.cursor = cursor;
+               if (!cur.selection())
+                       cur.resetAnchor();
                if (rtl())
-                       cursorRight(bv);
+                       cursorRight(true);
                else
-                       cursorLeft(bv);
+                       cursorLeft(true);
                finishChange(bv, true);
                break;
 
        case LFUN_UPSEL:
-               if (!selection.set())
-                       selection.cursor = cursor;
+               if (!cur.selection())
+                       cur.resetAnchor();
                cursorUp(true);
                finishChange(bv, true);
                break;
 
        case LFUN_DOWNSEL:
-               if (!selection.set())
-                       selection.cursor = cursor;
+               if (!cur.selection())
+                       cur.resetAnchor();
                cursorDown(true);
                finishChange(bv, true);
                break;
 
        case LFUN_UP_PARAGRAPHSEL:
-               if (!selection.set())
-                       selection.cursor = cursor;
+               if (!cur.selection())
+                       cur.resetAnchor();
                cursorUpParagraph();
                finishChange(bv, true);
                break;
 
        case LFUN_DOWN_PARAGRAPHSEL:
-               if (!selection.set())
-                       selection.cursor = cursor;
+               if (!cur.selection())
+                       cur.resetAnchor();
                cursorDownParagraph();
                finishChange(bv, true);
                break;
 
        case LFUN_PRIORSEL:
-               if (!selection.set())
-                       selection.cursor = cursor;
+               if (!cur.selection())
+                       cur.resetAnchor();
                cursorPrevious();
                finishChange(bv, true);
                break;
 
        case LFUN_NEXTSEL:
-               if (!selection.set())
-                       selection.cursor = cursor;
+               if (!cur.selection())
+                       cur.resetAnchor();
                cursorNext();
                finishChange(bv, true);
                break;
 
        case LFUN_HOMESEL:
-               if (!selection.set())
-                       selection.cursor = cursor;
+               if (!cur.selection())
+                       cur.resetAnchor();
                cursorHome();
                finishChange(bv, true);
                break;
 
        case LFUN_ENDSEL:
-               if (!selection.set())
-                       selection.cursor = cursor;
+               if (!cur.selection())
+                       cur.resetAnchor();
                cursorEnd();
                finishChange(bv, true);
                break;
 
        case LFUN_WORDRIGHTSEL:
+               if (!cur.selection())
+                       cur.resetAnchor();
                if (rtl())
                        cursorLeftOneWord();
                else
@@ -626,6 +621,8 @@ DispatchResult LyXText::dispatch(FuncRequest const & cmd)
                break;
 
        case LFUN_WORDLEFTSEL:
+               if (!cur.selection())
+                       cur.resetAnchor();
                if (rtl())
                        cursorRightOneWord();
                else
@@ -634,12 +631,7 @@ DispatchResult LyXText::dispatch(FuncRequest const & cmd)
                break;
 
        case LFUN_WORDSEL: {
-               LyXCursor cur1 = cursor;
-               LyXCursor cur2;
-               getWord(cur1, cur2, lyx::WHOLE_WORD);
-               setCursor(cur1.par(), cur1.pos());
-               clearSelection();
-               setCursor(cur2.par(), cur2.pos());
+               selectWord(lyx::WHOLE_WORD);
                finishChange(bv, true);
                break;
        }
@@ -661,47 +653,47 @@ DispatchResult LyXText::dispatch(FuncRequest const & cmd)
                return moveDown();
 
        case LFUN_UP_PARAGRAPH:
-               if (!selection.mark())
-                       clearSelection();
+               if (!cur.mark())
+                       cur.clearSelection();
                cursorUpParagraph();
                finishChange(bv);
                break;
 
        case LFUN_DOWN_PARAGRAPH:
-               if (!selection.mark())
-                       clearSelection();
+               if (!cur.mark())
+                       cur.clearSelection();
                cursorDownParagraph();
                finishChange(bv, false);
                break;
 
        case LFUN_PRIOR:
-               if (!selection.mark())
-                       clearSelection();
+               if (!cur.mark())
+                       cur.clearSelection();
                finishChange(bv, false);
-               if (cursorPar() == firstPar() && cursorRow() == firstRow())
+               if (cur.par() == 0 && cursorRow() == firstRow())
                        return DispatchResult(false, FINISHED_UP);
                cursorPrevious();
                break;
 
        case LFUN_NEXT:
-               if (!selection.mark())
-                       clearSelection();
+               if (!cur.mark())
+                       cur.clearSelection();
                finishChange(bv, false);
-               if (cursorPar() == lastPar() && cursorRow() == lastRow())
+               if (cur.par() == cur.lastpar() && cursorRow() == lastRow())
                        return DispatchResult(false, FINISHED_DOWN);
                cursorNext();
                break;
 
        case LFUN_HOME:
-               if (!selection.mark())
-                       clearSelection();
+               if (!cur.mark())
+                       cur.clearSelection();
                cursorHome();
                finishChange(bv, false);
                break;
 
        case LFUN_END:
-               if (!selection.mark())
-                       clearSelection();
+               if (!cur.mark())
+                       cur.clearSelection();
                cursorEnd();
                finishChange(bv, false);
                break;
@@ -710,39 +702,39 @@ DispatchResult LyXText::dispatch(FuncRequest const & cmd)
                lyx::pos_type body = cursorPar()->beginOfBody();
 
                // Not allowed by LaTeX (labels or empty par)
-               if (cursor.pos() <= body)
+               if (cursor().pos() <= body)
                        break;
 
                replaceSelection(bv->getLyXText());
                insertInset(new InsetNewline);
-               moveCursor(bv, false);
+               moveCursor(cur, false);
                break;
        }
 
        case LFUN_DELETE:
-               if (!selection.set()) {
+               if (!cur.selection()) {
                        Delete();
-                       selection.cursor = cursor;
+                       cur.resetAnchor();
                        // It is possible to make it a lot faster still
                        // just comment out the line below...
                } else {
                        cutSelection(true, false);
                }
-               moveCursor(bv, false);
+               moveCursor(cur, false);
                bv->owner()->view_state_changed();
                break;
 
        case LFUN_DELETE_SKIP:
                // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
-               if (!selection.set()) {
-                       if (cursor.pos() == cursorPar()->size()) {
+               if (!cur.selection()) {
+                       if (cursor().pos() == cursorPar()->size()) {
                                cursorRight(bv);
                                cursorLeft(bv);
                                Delete();
-                               selection.cursor = cursor;
+                               cur.resetAnchor();
                        } else {
                                Delete();
-                               selection.cursor = cursor;
+                               cur.resetAnchor();
                        }
                } else {
                        cutSelection(true, false);
@@ -752,10 +744,10 @@ DispatchResult LyXText::dispatch(FuncRequest const & cmd)
 
 
        case LFUN_BACKSPACE:
-               if (!selection.set()) {
+               if (!cur.selection()) {
                        if (bv->owner()->getIntl().getTransManager().backspace()) {
                                backspace();
-                               selection.cursor = cursor;
+                               cur.resetAnchor();
                                // It is possible to make it a lot faster still
                                // just comment out the line below...
                        }
@@ -769,10 +761,11 @@ DispatchResult LyXText::dispatch(FuncRequest const & cmd)
 
        case LFUN_BACKSPACE_SKIP:
                // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
-               if (!selection.set()) {
-                       LyXCursor cur = cursor;
+               if (!cur.selection()) {
+#warning look here
+                       //CursorSlice cur = cursor();
                        backspace();
-                       selection.cursor = cur;
+                       //anchor() = cur;
                } else {
                        cutSelection(true, false);
                }
@@ -783,7 +776,7 @@ DispatchResult LyXText::dispatch(FuncRequest const & cmd)
                replaceSelection(bv->getLyXText());
                breakParagraph(bv->buffer()->paragraphs(), 0);
                bv->update();
-               selection.cursor = cursor;
+               cur.resetAnchor();
                bv->switchKeyMap();
                bv->owner()->view_state_changed();
                break;
@@ -792,7 +785,7 @@ DispatchResult LyXText::dispatch(FuncRequest const & cmd)
                replaceSelection(bv->getLyXText());
                breakParagraph(bv->buffer()->paragraphs(), 1);
                bv->update();
-               selection.cursor = cursor;
+               cur.resetAnchor();
                bv->switchKeyMap();
                bv->owner()->view_state_changed();
                break;
@@ -801,10 +794,11 @@ DispatchResult LyXText::dispatch(FuncRequest const & cmd)
                // When at the beginning of a paragraph, remove
                // indentation and add a "defskip" at the top.
                // Otherwise, do the same as LFUN_BREAKPARAGRAPH.
-               LyXCursor cur = cursor;
+#warning look here
+//             CursorSlice cur = cursor();
                replaceSelection(bv->getLyXText());
                if (cur.pos() == 0) {
-                       ParagraphParameters & params = getPar(cur)->params();
+                       ParagraphParameters & params = getPar(cur.current())->params();
                        setParagraph(
                                        params.spacing(),
                                        params.align(),
@@ -813,7 +807,7 @@ DispatchResult LyXText::dispatch(FuncRequest const & cmd)
                        breakParagraph(bv->buffer()->paragraphs(), 0);
                }
                bv->update();
-               selection.cursor = cur;
+//     anchor() = cur;
                bv->switchKeyMap();
                bv->owner()->view_state_changed();
                break;
@@ -864,29 +858,27 @@ DispatchResult LyXText::dispatch(FuncRequest const & cmd)
        case LFUN_INSET_APPLY: {
                string const name = cmd.getArg(0);
                InsetBase * inset = bv->owner()->getDialogs().getOpenInset(name);
-               if (inset) {
-                       FuncRequest fr(bv, LFUN_INSET_MODIFY, cmd.argument);
-                       inset->dispatch(fr);
-               } else {
-                       FuncRequest fr(bv, LFUN_INSET_INSERT, cmd.argument);
-                       dispatch(fr);
-               }
+               if (inset)
+                       inset->dispatch(cur, FuncRequest(LFUN_INSET_MODIFY, cmd.argument));
+               else
+                       dispatch(cur, FuncRequest(LFUN_INSET_INSERT, cmd.argument));
                break;
        }
 
        case LFUN_INSET_INSERT: {
-               InsetOld * inset = createInset(cmd);
+               InsetBase * inset = createInset(bv, cmd);
                if (inset && !bv->insertInset(inset))
                        delete inset;
                break;
        }
 
        case LFUN_INSET_SETTINGS:
-               bv->cursor().innerInset()->showInsetDialog(bv);
+               if (cur.inset() && cur.inset()->asUpdatableInset())
+                       cur.inset()->asUpdatableInset()->showInsetDialog(bv);
                break;
 
        case LFUN_INSET_TOGGLE:
-               clearSelection();
+               cur.clearSelection();
                if (!toggleInset())
                        return DispatchResult(false);
                bv->update();
@@ -897,8 +889,8 @@ DispatchResult LyXText::dispatch(FuncRequest const & cmd)
                if (cursorPar()->layout()->free_spacing)
                        insertChar(' ');
                else
-                       doInsertInset(*this, cmd, false, false);
-               moveCursor(bv, false);
+                       doInsertInset(bv, cmd, false, false);
+               moveCursor(cur, false);
                break;
 
        case LFUN_HYPHENATION:
@@ -921,33 +913,6 @@ DispatchResult LyXText::dispatch(FuncRequest const & cmd)
                specialChar(this, bv, InsetSpecialChar::MENU_SEPARATOR);
                break;
 
-       case LFUN_MARK_OFF:
-               clearSelection();
-               bv->update();
-               selection.cursor = cursor;
-               cmd.message(N_("Mark off"));
-               break;
-
-       case LFUN_MARK_ON:
-               clearSelection();
-               selection.mark(true);
-               bv->update();
-               selection.cursor = cursor;
-               cmd.message(N_("Mark on"));
-               break;
-
-       case LFUN_SETMARK:
-               clearSelection();
-               if (selection.mark()) {
-                       cmd.message(N_("Mark removed"));
-               } else {
-                       selection.mark(true);
-                       cmd.message(N_("Mark set"));
-               }
-               selection.cursor = cursor;
-               bv->update();
-               break;
-
        case LFUN_UPCASE_WORD:
                changeCase(LyXText::text_uppercase);
                bv->update();
@@ -964,20 +929,20 @@ DispatchResult LyXText::dispatch(FuncRequest const & cmd)
                break;
 
        case LFUN_TRANSPOSE_CHARS:
-               recUndo(cursor.par());
+               recUndo(cursor().par());
                redoParagraph();
                bv->update();
                break;
 
        case LFUN_PASTE:
-               cmd.message(_("Paste"));
+               cur.message(_("Paste"));
                replaceSelection(bv->getLyXText());
 #warning FIXME Check if the arg is in the domain of available selections.
                if (isStrUnsignedInt(cmd.argument))
                        pasteSelection(strToUnsignedInt(cmd.argument));
                else
                        pasteSelection(0);
-               clearSelection(); // bug 393
+               cur.clearSelection(); // bug 393
                bv->update();
                bv->switchKeyMap();
                finishUndo();
@@ -985,20 +950,20 @@ DispatchResult LyXText::dispatch(FuncRequest const & cmd)
 
        case LFUN_CUT:
                cutSelection(true, true);
-               cmd.message(_("Cut"));
+               cur.message(_("Cut"));
                bv->update();
                break;
 
        case LFUN_COPY:
                copySelection();
-               cmd.message(_("Copy"));
+               cur.message(_("Copy"));
                break;
 
        case LFUN_BEGINNINGBUFSEL:
                if (in_inset_)
                        return DispatchResult(false);
-               if (!selection.set())
-                       selection.cursor = cursor;
+               if (!cur.selection())
+                       cur.resetAnchor();
                cursorTop();
                finishChange(bv, true);
                break;
@@ -1006,14 +971,15 @@ DispatchResult LyXText::dispatch(FuncRequest const & cmd)
        case LFUN_ENDBUFSEL:
                if (in_inset_)
                        return DispatchResult(false);
-               if (!selection.set())
-                       selection.cursor = cursor;
+               if (!cur.selection())
+                       cur.resetAnchor();
                cursorBottom();
                finishChange(bv, true);
                break;
 
        case LFUN_GETXY:
-               cmd.message(tostr(cursorX()) + ' ' + tostr(cursorY()));
+               cur.message(tostr(cursorX(cur.current())) + ' '
+                         + tostr(cursorY(cur.current())));
                break;
 
        case LFUN_SETXY: {
@@ -1031,15 +997,15 @@ DispatchResult LyXText::dispatch(FuncRequest const & cmd)
 
        case LFUN_GETFONT:
                if (current_font.shape() == LyXFont::ITALIC_SHAPE)
-                       cmd.message("E");
+                       cur.message("E");
                else if (current_font.shape() == LyXFont::SMALLCAPS_SHAPE)
-                       cmd.message("N");
+                       cur.message("N");
                else
-                       cmd.message("0");
+                       cur.message("0");
                break;
 
        case LFUN_GETLAYOUT:
-               cmd.message(tostr(cursorPar()->layout()));
+               cur.message(cursorPar()->layout()->name());
                break;
 
        case LFUN_LAYOUT: {
@@ -1052,7 +1018,7 @@ DispatchResult LyXText::dispatch(FuncRequest const & cmd)
                // function list/array with information about what
                // functions needs arguments and their type.
                if (cmd.argument.empty()) {
-                       cmd.errorMessage(_("LyX function 'layout' needs an argument."));
+                       cur.errorMessage(_("LyX function 'layout' needs an argument."));
                        break;
                }
 
@@ -1070,18 +1036,18 @@ DispatchResult LyXText::dispatch(FuncRequest const & cmd)
                }
 
                if (!hasLayout) {
-                       cmd.errorMessage(string(N_("Layout ")) + cmd.argument +
+                       cur.errorMessage(string(N_("Layout ")) + cmd.argument +
                                N_(" not known"));
                        break;
                }
 
                bool change_layout = (current_layout != layout);
 
-               if (!change_layout && selection.set() &&
-                       selStart().par() != selEnd().par())
+               if (!change_layout && cur.selection() &&
+                       cur.selBegin().par() != cur.selEnd().par())
                {
-                       ParagraphList::iterator spit = getPar(selStart());
-                       ParagraphList::iterator epit = boost::next(getPar(selEnd()));
+                       ParagraphList::iterator spit = getPar(cur.selBegin());
+                       ParagraphList::iterator epit = boost::next(getPar(cur.selEnd()));
                        while (spit != epit) {
                                if (spit->layout()->name() != current_layout) {
                                        change_layout = true;
@@ -1102,9 +1068,7 @@ DispatchResult LyXText::dispatch(FuncRequest const & cmd)
        }
 
        case LFUN_PASTESELECTION: {
-               // this was originally a bv->text->clearSelection(), i.e
-               // the outermost LyXText!
-               clearSelection();
+               cur.clearSelection();
                string const clip = bv->getClipboard();
                if (!clip.empty()) {
                        if (cmd.argument == "paragraph")
@@ -1117,17 +1081,17 @@ DispatchResult LyXText::dispatch(FuncRequest const & cmd)
        }
 
        case LFUN_GOTOERROR:
-               gotoInset(InsetOld::ERROR_CODE, false);
+               gotoInset(InsetBase::ERROR_CODE, false);
                break;
 
        case LFUN_GOTONOTE:
-               gotoInset(InsetOld::NOTE_CODE, false);
+               gotoInset(InsetBase::NOTE_CODE, false);
                break;
 
        case LFUN_REFERENCE_GOTO: {
-               vector<InsetOld::Code> tmp;
-               tmp.push_back(InsetOld::LABEL_CODE);
-               tmp.push_back(InsetOld::REF_CODE);
+               vector<InsetOld_code> tmp;
+               tmp.push_back(InsetBase::LABEL_CODE);
+               tmp.push_back(InsetBase::REF_CODE);
                gotoInset(tmp, true);
                break;
        }
@@ -1135,7 +1099,7 @@ DispatchResult LyXText::dispatch(FuncRequest const & cmd)
        case LFUN_QUOTE: {
                replaceSelection(bv->getLyXText());
                ParagraphList::iterator pit = cursorPar();
-               lyx::pos_type pos = cursor.pos();
+               lyx::pos_type pos = cursor().pos();
                char c;
                if (!pos)
                        c = ' ';
@@ -1171,38 +1135,37 @@ DispatchResult LyXText::dispatch(FuncRequest const & cmd)
                for (int i = 0; i < datetmp_len; i++)
                        insertChar(datetmp[i]);
 
-               selection.cursor = cursor;
-               moveCursor(bv, false);
+               cur.resetAnchor();
+               moveCursor(cur, false);
                break;
        }
 
        case LFUN_MOUSE_TRIPLE:
-               if (!bv->buffer())
-                       break;
-               if (cmd.button() == mouse_button::button1) {
+               if (bv->buffer() && cmd.button() == mouse_button::button1) {
                        selection_possible = true;
                        cursorHome();
-                       selection.cursor = cursor;
+                       cur.resetAnchor();
                        cursorEnd();
-                       setSelection();
-                       bv->haveSelection(selection.set());
+                       cur.setSelection();
+                       bv->haveSelection(cur.selection());
                }
                break;
 
        case LFUN_MOUSE_DOUBLE:
-               if (!bv->buffer())
-                       break;
-               if (cmd.button() == mouse_button::button1) {
+               if (bv->buffer() && cmd.button() == mouse_button::button1) {
                        selection_possible = true;
                        selectWord(lyx::WHOLE_WORD_STRICT);
-                       bv->haveSelection(selection.set());
+                       bv->haveSelection(cur.selection());
                }
                break;
 
        case LFUN_MOUSE_MOTION: {
+#if 0
                // Only use motion with button 1
                //if (ev.button() != mouse_button::button1)
                //      return false;
+               // don't set anchor_
+               bv->cursor().cursor_ = cur.cursor_;
 
                if (!bv->buffer())
                        break;
@@ -1226,7 +1189,8 @@ DispatchResult LyXText::dispatch(FuncRequest const & cmd)
                        else if (cmd.y - bv->top_y() < 0)
                                cursorUp(true);
                }
-               setSelection();
+               cur.setSelection();
+#endif
                break;
        }
 
@@ -1253,7 +1217,7 @@ DispatchResult LyXText::dispatch(FuncRequest const & cmd)
                // it could get cleared on the unlocking of the inset so
                // we have to check this first
                bool paste_internally = false;
-               if (cmd.button() == mouse_button::button2 && selection.set()) {
+               if (cmd.button() == mouse_button::button2 && cur.selection()) {
                        bv->owner()->dispatch(FuncRequest(LFUN_COPY));
                        paste_internally = true;
                }
@@ -1261,7 +1225,7 @@ DispatchResult LyXText::dispatch(FuncRequest const & cmd)
                selection_possible = true;
 
                // Clear the selection
-               clearSelection();
+               cur.clearSelection();
 
                // Right click on a footnote flag opens float menu
                if (cmd.button() == mouse_button::button3) {
@@ -1269,10 +1233,13 @@ DispatchResult LyXText::dispatch(FuncRequest const & cmd)
                        break;
                }
 
-               setCursorFromCoordinates(cmd.x, cmd.y);
-               selection.cursor = cursor;
+               setCursorFromCoordinates(cur.current(), cmd.x, cmd.y);
+               cur.resetAnchor();
                finishUndo();
-               bv->x_target(cursorX() + xo_);
+               cur.x_target() = cursorX(cur.current());
+
+               // set cursor and anchor to this position
+               bv->cursor() = cur;
 
                if (bv->fitCursor())
                        selection_possible = false;
@@ -1306,7 +1273,7 @@ DispatchResult LyXText::dispatch(FuncRequest const & cmd)
 
                // finish selection
                if (cmd.button() == mouse_button::button1)
-                       bv->haveSelection(selection.set());
+                       bv->haveSelection(cur.selection());
 
                bv->switchKeyMap();
                bv->owner()->view_state_changed();
@@ -1326,12 +1293,12 @@ DispatchResult LyXText::dispatch(FuncRequest const & cmd)
                // true (on).
 
                if (lyxrc.auto_region_delete) {
-                       if (selection.set())
+                       if (cur.selection())
                                cutSelection(false, false);
                        bv->haveSelection(false);
                }
 
-               clearSelection();
+               cur.clearSelection();
                LyXFont const old_font = real_current_font;
 
                string::const_iterator cit = cmd.argument.begin();
@@ -1340,8 +1307,8 @@ DispatchResult LyXText::dispatch(FuncRequest const & cmd)
                        bv->owner()->getIntl().getTransManager().
                                TranslateAndInsert(*cit, this);
 
-               selection.cursor = cursor;
-               moveCursor(bv, false);
+               cur.resetAnchor();
+               moveCursor(cur, false);
 
                // real_current_font.number can change so we need to
                // update the minibuffer
@@ -1387,7 +1354,6 @@ DispatchResult LyXText::dispatch(FuncRequest const & cmd)
        case LFUN_INSET_FLOAT:
        case LFUN_INSET_FOOTNOTE:
        case LFUN_INSET_MARGINAL:
-       case LFUN_INSET_MINIPAGE:
        case LFUN_INSET_OPTARG:
        case LFUN_INSET_WIDE_FLOAT:
        case LFUN_INSET_WRAP:
@@ -1395,12 +1361,12 @@ DispatchResult LyXText::dispatch(FuncRequest const & cmd)
        case LFUN_ENVIRONMENT_INSERT:
                // Open the inset, and move the current selection
                // inside it.
-               doInsertInset(*this, cmd, true, true);
+               doInsertInset(bv, cmd, true, true);
                break;
 
        case LFUN_INDEX_INSERT:
                // Just open the inset
-               doInsertInset(*this, cmd, true, false);
+               doInsertInset(bv, cmd, true, false);
                break;
 
        case LFUN_INDEX_PRINT:
@@ -1409,7 +1375,7 @@ DispatchResult LyXText::dispatch(FuncRequest const & cmd)
        case LFUN_INSERT_LINE:
        case LFUN_INSERT_PAGEBREAK:
                // do nothing fancy
-               doInsertInset(*this, cmd, false, false);
+               doInsertInset(bv, cmd, false, false);
                break;
 
        case LFUN_DEPTH_MIN:
@@ -1435,7 +1401,7 @@ DispatchResult LyXText::dispatch(FuncRequest const & cmd)
        case LFUN_MATH_NUMBER:
        case LFUN_MATH_EXTERN:
        case LFUN_MATH_SIZE:
-               mathDispatch(cmd);
+               mathDispatch(cur, cmd);
                break;
 
        case LFUN_EMPH:
@@ -1498,24 +1464,32 @@ DispatchResult LyXText::dispatch(FuncRequest const & cmd)
                break;
 
        case LFUN_FINISHED_LEFT:
-               lyxerr << "swallow LFUN_FINISHED_LEFT" << endl;
+               lyxerr << "handle LFUN_FINISHED_LEFT" << endl;
+               cur.pop(cur.currentDepth());
+               cur.bv().cursor() = cur;
                if (rtl())
-                       cursorRight(true);
+                       cursorLeft(true);
                break;
 
        case LFUN_FINISHED_RIGHT:
-               lyxerr << "swallow LFUN_FINISHED_RIGHT" << endl;
+               lyxerr << "handle LFUN_FINISHED_RIGHT" << endl;
+               cur.pop(cur.currentDepth());
+               cur.bv().cursor() = cur;
                if (!rtl())
                        cursorRight(true);
                break;
 
        case LFUN_FINISHED_UP:
-               lyxerr << "swallow LFUN_FINISHED_UP" << endl;
+               lyxerr << "handle LFUN_FINISHED_UP" << endl;
+               cur.pop(cur.currentDepth());
+               cur.bv().cursor() = cur;
                cursorUp(true);
                break;
 
        case LFUN_FINISHED_DOWN:
-               lyxerr << "swallow LFUN_FINISHED_DOWN" << endl;
+               lyxerr << "handle LFUN_FINISHED_DOWN" << endl;
+               cur.pop(cur.currentDepth());
+               cur.bv().cursor() = cur;
                cursorDown(true);
                break;