]> git.lyx.org Git - lyx.git/blobdiff - src/text3.C
more cursor dispatch
[lyx.git] / src / text3.C
index c3e533c83890be6c8a50e25560b628ab2b406fcf..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;
 
 
@@ -213,23 +213,20 @@ namespace {
        }
 
 
-       void moveCursor(BufferView * bv, bool selecting)
+       void moveCursor(LCursor & cur, bool selecting)
        {
-               LyXText * lt = bv->getLyXText();
-
-               if (selecting || bv->selection().mark())
-                       lt->setSelection();
-
-               if (!bv->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();
        }
 
@@ -249,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;
@@ -264,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;
                        }
@@ -286,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();
 
-       InsetOld * inset;
+       InsetBase * inset;
        do {
                if (pos + 1 < pit->size()) {
                        ++pos;
@@ -318,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()) {
+               if (cur.pos() || cur.par() != 0) {
                        CursorSlice tmp = cursor();
-                       cursor().par(0);
-                       cursor().pos(0);
+                       cur.par() = 0;
+                       cur.pos() = 0;
                        if (!gotoNextInset(codes, contents)) {
                                cursor() = tmp;
-                               bv()->owner()->message(_("No more insets"));
+                               cur.bv().owner()->message(_("No more insets"));
                        }
                } else {
-                       bv()->owner()->message(_("No more insets"));
+                       cur.bv().owner()->message(_("No more insets"));
                }
        }
-       bv()->update();
-       bv()->resetAnchor();
+       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);
@@ -379,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);
@@ -399,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
@@ -411,26 +407,26 @@ void specialChar(LyXText * lt, BufferView * bv, InsetSpecialChar::Kind kind)
 }
 
 
-void doInsertInset(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 (bv->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
@@ -450,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) {
 
@@ -486,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 (!bv->selection().mark())
-                       clearSelection();
+               if (!cur.mark())
+                       cur.clearSelection();
                if (rtl())
                        cursorLeftOneWord();
                else
@@ -514,8 +511,8 @@ DispatchResult LyXText::dispatch(FuncRequest const & cmd)
                break;
 
        case LFUN_WORDLEFT:
-               if (!bv->selection().mark())
-                       clearSelection();
+               if (!cur.mark())
+                       cur.clearSelection();
                if (rtl())
                        cursorRightOneWord();
                else
@@ -524,98 +521,98 @@ DispatchResult LyXText::dispatch(FuncRequest const & cmd)
                break;
 
        case LFUN_BEGINNINGBUF:
-               if (!bv->selection().mark())
-                       clearSelection();
+               if (!cur.mark())
+                       cur.clearSelection();
                cursorTop();
                finishChange(bv);
                break;
 
        case LFUN_ENDBUF:
-               if (bv->selection().mark())
-                       clearSelection();
+               if (!cur.mark())
+                       cur.clearSelection();
                cursorBottom();
                finishChange(bv);
                break;
 
        case LFUN_RIGHTSEL:
-               if (!bv->selection().set())
-                       bv->resetAnchor();
+               if (!cur.selection())
+                       cur.resetAnchor();
                if (rtl())
-                       cursorLeft(bv);
+                       cursorLeft(true);
                else
-                       cursorRight(bv);
+                       cursorRight(true);
                finishChange(bv, true);
                break;
 
        case LFUN_LEFTSEL:
-               if (!bv->selection().set())
-                       bv->resetAnchor();
+               if (!cur.selection())
+                       cur.resetAnchor();
                if (rtl())
-                       cursorRight(bv);
+                       cursorRight(true);
                else
-                       cursorLeft(bv);
+                       cursorLeft(true);
                finishChange(bv, true);
                break;
 
        case LFUN_UPSEL:
-               if (!bv->selection().set())
-                       bv->resetAnchor();
+               if (!cur.selection())
+                       cur.resetAnchor();
                cursorUp(true);
                finishChange(bv, true);
                break;
 
        case LFUN_DOWNSEL:
-               if (!bv->selection().set())
-                       bv->resetAnchor();
+               if (!cur.selection())
+                       cur.resetAnchor();
                cursorDown(true);
                finishChange(bv, true);
                break;
 
        case LFUN_UP_PARAGRAPHSEL:
-               if (!bv->selection().set())
-                       bv->resetAnchor();
+               if (!cur.selection())
+                       cur.resetAnchor();
                cursorUpParagraph();
                finishChange(bv, true);
                break;
 
        case LFUN_DOWN_PARAGRAPHSEL:
-               if (!bv->selection().set())
-                       bv->resetAnchor();
+               if (!cur.selection())
+                       cur.resetAnchor();
                cursorDownParagraph();
                finishChange(bv, true);
                break;
 
        case LFUN_PRIORSEL:
-               if (!bv->selection().set())
-                       bv->resetAnchor();
+               if (!cur.selection())
+                       cur.resetAnchor();
                cursorPrevious();
                finishChange(bv, true);
                break;
 
        case LFUN_NEXTSEL:
-               if (!bv->selection().set())
-                       bv->resetAnchor();
+               if (!cur.selection())
+                       cur.resetAnchor();
                cursorNext();
                finishChange(bv, true);
                break;
 
        case LFUN_HOMESEL:
-               if (!bv->selection().set())
-                       bv->resetAnchor();
+               if (!cur.selection())
+                       cur.resetAnchor();
                cursorHome();
                finishChange(bv, true);
                break;
 
        case LFUN_ENDSEL:
-               if (!bv->selection().set())
-                       bv->resetAnchor();
+               if (!cur.selection())
+                       cur.resetAnchor();
                cursorEnd();
                finishChange(bv, true);
                break;
 
        case LFUN_WORDRIGHTSEL:
-               if (!bv->selection().set())
-                       bv->resetAnchor();
+               if (!cur.selection())
+                       cur.resetAnchor();
                if (rtl())
                        cursorLeftOneWord();
                else
@@ -624,8 +621,8 @@ DispatchResult LyXText::dispatch(FuncRequest const & cmd)
                break;
 
        case LFUN_WORDLEFTSEL:
-               if (!bv->selection().set())
-                       bv->resetAnchor();
+               if (!cur.selection())
+                       cur.resetAnchor();
                if (rtl())
                        cursorRightOneWord();
                else
@@ -656,47 +653,47 @@ DispatchResult LyXText::dispatch(FuncRequest const & cmd)
                return moveDown();
 
        case LFUN_UP_PARAGRAPH:
-               if (!bv->selection().mark())
-                       clearSelection();
+               if (!cur.mark())
+                       cur.clearSelection();
                cursorUpParagraph();
                finishChange(bv);
                break;
 
        case LFUN_DOWN_PARAGRAPH:
-               if (!bv->selection().mark())
-                       clearSelection();
+               if (!cur.mark())
+                       cur.clearSelection();
                cursorDownParagraph();
                finishChange(bv, false);
                break;
 
        case LFUN_PRIOR:
-               if (!bv->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 (!bv->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 (!bv->selection().mark())
-                       clearSelection();
+               if (!cur.mark())
+                       cur.clearSelection();
                cursorHome();
                finishChange(bv, false);
                break;
 
        case LFUN_END:
-               if (!bv->selection().mark())
-                       clearSelection();
+               if (!cur.mark())
+                       cur.clearSelection();
                cursorEnd();
                finishChange(bv, false);
                break;
@@ -710,34 +707,34 @@ DispatchResult LyXText::dispatch(FuncRequest const & cmd)
 
                replaceSelection(bv->getLyXText());
                insertInset(new InsetNewline);
-               moveCursor(bv, false);
+               moveCursor(cur, false);
                break;
        }
 
        case LFUN_DELETE:
-               if (!bv->selection().set()) {
+               if (!cur.selection()) {
                        Delete();
-                       bv->resetAnchor();
+                       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 (!bv->selection().set()) {
+               if (!cur.selection()) {
                        if (cursor().pos() == cursorPar()->size()) {
                                cursorRight(bv);
                                cursorLeft(bv);
                                Delete();
-                               bv->resetAnchor();
+                               cur.resetAnchor();
                        } else {
                                Delete();
-                               bv->resetAnchor();
+                               cur.resetAnchor();
                        }
                } else {
                        cutSelection(true, false);
@@ -747,10 +744,10 @@ DispatchResult LyXText::dispatch(FuncRequest const & cmd)
 
 
        case LFUN_BACKSPACE:
-               if (!bv->selection().set()) {
+               if (!cur.selection()) {
                        if (bv->owner()->getIntl().getTransManager().backspace()) {
                                backspace();
-                               bv->resetAnchor();
+                               cur.resetAnchor();
                                // It is possible to make it a lot faster still
                                // just comment out the line below...
                        }
@@ -764,10 +761,11 @@ DispatchResult LyXText::dispatch(FuncRequest const & cmd)
 
        case LFUN_BACKSPACE_SKIP:
                // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
-               if (!bv->selection().set()) {
-                       CursorSlice cur = cursor();
+               if (!cur.selection()) {
+#warning look here
+                       //CursorSlice cur = cursor();
                        backspace();
-                       anchor() = cur;
+                       //anchor() = cur;
                } else {
                        cutSelection(true, false);
                }
@@ -778,7 +776,7 @@ DispatchResult LyXText::dispatch(FuncRequest const & cmd)
                replaceSelection(bv->getLyXText());
                breakParagraph(bv->buffer()->paragraphs(), 0);
                bv->update();
-               bv->resetAnchor();
+               cur.resetAnchor();
                bv->switchKeyMap();
                bv->owner()->view_state_changed();
                break;
@@ -787,7 +785,7 @@ DispatchResult LyXText::dispatch(FuncRequest const & cmd)
                replaceSelection(bv->getLyXText());
                breakParagraph(bv->buffer()->paragraphs(), 1);
                bv->update();
-               bv->resetAnchor();
+               cur.resetAnchor();
                bv->switchKeyMap();
                bv->owner()->view_state_changed();
                break;
@@ -796,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.
-               CursorSlice 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(),
@@ -808,7 +807,7 @@ DispatchResult LyXText::dispatch(FuncRequest const & cmd)
                        breakParagraph(bv->buffer()->paragraphs(), 0);
                }
                bv->update();
-               anchor() = cur;
+//     anchor() = cur;
                bv->switchKeyMap();
                bv->owner()->view_state_changed();
                break;
@@ -859,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->fullCursor().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();
@@ -892,8 +889,8 @@ DispatchResult LyXText::dispatch(FuncRequest const & cmd)
                if (cursorPar()->layout()->free_spacing)
                        insertChar(' ');
                else
-                       doInsertInset(cmd, false, false);
-               moveCursor(bv, false);
+                       doInsertInset(bv, cmd, false, false);
+               moveCursor(cur, false);
                break;
 
        case LFUN_HYPHENATION:
@@ -916,33 +913,6 @@ DispatchResult LyXText::dispatch(FuncRequest const & cmd)
                specialChar(this, bv, InsetSpecialChar::MENU_SEPARATOR);
                break;
 
-       case LFUN_MARK_OFF:
-               clearSelection();
-               bv->update();
-               bv->resetAnchor();
-               cmd.message(N_("Mark off"));
-               break;
-
-       case LFUN_MARK_ON:
-               clearSelection();
-               bv->selection().mark(true);
-               bv->update();
-               bv->resetAnchor();
-               cmd.message(N_("Mark on"));
-               break;
-
-       case LFUN_SETMARK:
-               clearSelection();
-               if (bv->selection().mark()) {
-                       cmd.message(N_("Mark removed"));
-               } else {
-                       bv->selection().mark(true);
-                       cmd.message(N_("Mark set"));
-               }
-               bv->resetAnchor();
-               bv->update();
-               break;
-
        case LFUN_UPCASE_WORD:
                changeCase(LyXText::text_uppercase);
                bv->update();
@@ -965,14 +935,14 @@ DispatchResult LyXText::dispatch(FuncRequest const & cmd)
                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();
@@ -980,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 (!bv->selection().set())
-                       bv->resetAnchor();
+               if (!cur.selection())
+                       cur.resetAnchor();
                cursorTop();
                finishChange(bv, true);
                break;
@@ -1001,14 +971,15 @@ DispatchResult LyXText::dispatch(FuncRequest const & cmd)
        case LFUN_ENDBUFSEL:
                if (in_inset_)
                        return DispatchResult(false);
-               if (!bv->selection().set())
-                       bv->resetAnchor();
+               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: {
@@ -1026,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(cursorPar()->layout()->name());
+               cur.message(cursorPar()->layout()->name());
                break;
 
        case LFUN_LAYOUT: {
@@ -1047,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;
                }
 
@@ -1065,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 && bv->selection().set() &&
-                       bv->selStart().par() != bv->selEnd().par())
+               if (!change_layout && cur.selection() &&
+                       cur.selBegin().par() != cur.selEnd().par())
                {
-                       ParagraphList::iterator spit = getPar(bv->selStart());
-                       ParagraphList::iterator epit = boost::next(getPar(bv->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;
@@ -1097,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")
@@ -1112,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;
        }
@@ -1166,38 +1135,37 @@ DispatchResult LyXText::dispatch(FuncRequest const & cmd)
                for (int i = 0; i < datetmp_len; i++)
                        insertChar(datetmp[i]);
 
-               bv->resetAnchor();
-               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();
-                       bv->resetAnchor();
+                       cur.resetAnchor();
                        cursorEnd();
-                       setSelection();
-                       bv->haveSelection(bv->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(bv->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;
@@ -1221,7 +1189,8 @@ DispatchResult LyXText::dispatch(FuncRequest const & cmd)
                        else if (cmd.y - bv->top_y() < 0)
                                cursorUp(true);
                }
-               setSelection();
+               cur.setSelection();
+#endif
                break;
        }
 
@@ -1248,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 && bv->selection().set()) {
+               if (cmd.button() == mouse_button::button2 && cur.selection()) {
                        bv->owner()->dispatch(FuncRequest(LFUN_COPY));
                        paste_internally = true;
                }
@@ -1256,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) {
@@ -1264,10 +1233,13 @@ DispatchResult LyXText::dispatch(FuncRequest const & cmd)
                        break;
                }
 
-               setCursorFromCoordinates(cmd.x, cmd.y);
-               bv->resetAnchor();
+               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;
@@ -1301,7 +1273,7 @@ DispatchResult LyXText::dispatch(FuncRequest const & cmd)
 
                // finish selection
                if (cmd.button() == mouse_button::button1)
-                       bv->haveSelection(bv->selection().set());
+                       bv->haveSelection(cur.selection());
 
                bv->switchKeyMap();
                bv->owner()->view_state_changed();
@@ -1321,12 +1293,12 @@ DispatchResult LyXText::dispatch(FuncRequest const & cmd)
                // true (on).
 
                if (lyxrc.auto_region_delete) {
-                       if (bv->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();
@@ -1335,8 +1307,8 @@ DispatchResult LyXText::dispatch(FuncRequest const & cmd)
                        bv->owner()->getIntl().getTransManager().
                                TranslateAndInsert(*cit, this);
 
-               bv->resetAnchor();
-               moveCursor(bv, false);
+               cur.resetAnchor();
+               moveCursor(cur, false);
 
                // real_current_font.number can change so we need to
                // update the minibuffer
@@ -1389,12 +1361,12 @@ DispatchResult LyXText::dispatch(FuncRequest const & cmd)
        case LFUN_ENVIRONMENT_INSERT:
                // Open the inset, and move the current selection
                // inside it.
-               doInsertInset(cmd, true, true);
+               doInsertInset(bv, cmd, true, true);
                break;
 
        case LFUN_INDEX_INSERT:
                // Just open the inset
-               doInsertInset(cmd, true, false);
+               doInsertInset(bv, cmd, true, false);
                break;
 
        case LFUN_INDEX_PRINT:
@@ -1403,7 +1375,7 @@ DispatchResult LyXText::dispatch(FuncRequest const & cmd)
        case LFUN_INSERT_LINE:
        case LFUN_INSERT_PAGEBREAK:
                // do nothing fancy
-               doInsertInset(cmd, false, false);
+               doInsertInset(bv, cmd, false, false);
                break;
 
        case LFUN_DEPTH_MIN:
@@ -1429,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:
@@ -1492,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;