]> git.lyx.org Git - lyx.git/blobdiff - src/text3.C
Point fix, earlier forgotten
[lyx.git] / src / text3.C
index 25e64e602b49d2b251188862852703f4a34a0899..2203305b57043c42851a1ad68b8790623b97fa30 100644 (file)
@@ -1,17 +1,21 @@
-/* This file is part of
- * ======================================================
+/**
+ * \file text3.C
+ * This file is part of LyX, the document processor.
+ * Licence details can be found in the file COPYING.
  *
- *           LyX, The Document Processor
+ * \author Asger Alstrup
+ * \author Lars Gullik Bjønnes
+ * \author Alfredo Braunstein
+ * \author Angus Leeming
+ * \author John Levon
+ * \author André Pönitz
  *
- *           Copyright 1995 Matthias Ettrich
- *           Copyright 1995-2002 The LyX Team.
- *
- * ====================================================== */
+ * Full author contact details are available in file CREDITS.
+ */
 
 #include <config.h>
 
 #include "lyxtext.h"
-#include "lyxrow.h"
 #include "paragraph.h"
 #include "BufferView.h"
 #include "funcrequest.h"
@@ -20,6 +24,7 @@
 #include "debug.h"
 #include "bufferparams.h"
 #include "buffer.h"
+#include "bufferview_funcs.h"
 #include "ParagraphParameters.h"
 #include "gettext.h"
 #include "factory.h"
@@ -27,6 +32,8 @@
 #include "box.h"
 #include "language.h"
 #include "support/tostr.h"
+#include "support/lstrings.h"
+#include "support/LAssert.h"
 #include "frontends/LyXView.h"
 #include "frontends/screen.h"
 #include "frontends/Dialogs.h"
@@ -42,6 +49,9 @@
 #include <ctime>
 #include <clocale>
 
+using namespace lyx::support;
+using namespace bv_funcs;
+
 using std::endl;
 using std::find;
 using std::vector;
@@ -61,22 +71,16 @@ namespace {
        {
                LyXText * lt = bv->getLyXText();
 
-               if (selecting || lt->selection.mark()) {
+               //if (!lt->selection.set())
+    // lt->selection.cursor = lt->cursor;
+
+               if (selecting || lt->selection.mark())
                        lt->setSelection();
-                       if (lt->isInInset())
-                               bv->updateInset(lt->inset_owner);
-                       else
-                               bv->toggleToggle();
-               }
-               if (!lt->isInInset()) {
-                       bv->update(lt, BufferView::SELECT);
-               } else if (bv->text->refreshStatus() != LyXText::REFRESH_NONE) {
-                       bv->update(BufferView::SELECT);
-               }
 
                if (!lt->selection.set())
                        bv->haveSelection(false);
 
+               bv->update();
                bv->switchKeyMap();
        }
 
@@ -91,48 +95,48 @@ namespace {
        // check if the given co-ordinates are inside an inset at the
        // given cursor, if one exists. If so, the inset is returned,
        // and the co-ordinates are made relative. Otherwise, 0 is returned.
-       Inset * checkInset(BufferView * bv, LyXText & text,
+       InsetOld * checkInset(BufferView * /*bv*/, LyXText & text,
                LyXCursor const & cur, int & x, int & y)
        {
                lyx::pos_type const pos = cur.pos();
-               Paragraph /*const*/ & par = *cur.par();
+               ParagraphList::iterator par = cur.par();
 
-               if (pos >= par.size() || !par.isInset(pos))
+               if (pos >= par->size() || !par->isInset(pos))
                        return 0;
 
-               Inset /*const*/ * inset = par.getInset(pos);
+               InsetOld /*const*/ * inset = par->getInset(pos);
 
                if (!isEditableInset(inset))
                        return 0;
 
                // get inset dimensions
-               lyx::Assert(par.getInset(pos));
+               Assert(par->getInset(pos));
 
-               LyXFont const & font = text.getFont(bv->buffer(), &par, pos);
+               LyXFont const & font = text.getFont(par, pos);
 
-               int const width = inset->width(bv, font);
+               int const width = inset->width();
                int const inset_x = font.isVisibleRightToLeft()
-                       ? (cur.ix() - width) : cur.ix();
+                       ? (cur.x() - width) : cur.x();
 
                Box b(
                        inset_x + inset->scroll(),
                        inset_x + width,
-                       cur.iy() - inset->ascent(bv, font),
-                       cur.iy() + inset->descent(bv, font)
+                       cur.y() - inset->ascent(),
+                       cur.y() + inset->descent()
                );
 
-               if (!b.contained(x, y)) {
+               if (!b.contains(x, y)) {
                        lyxerr[Debug::GUI] << "Missed inset at x,y "
                                           << x << ',' << y
                                           << " box " << b << endl;
                        return 0;
                }
 
-               text.setCursor(&par, pos, true);
+               text.setCursor(par, pos, true);
 
                x -= b.x1;
                // The origin of an inset is on the baseline
-               y -= text.cursor.iy();
+               y -= text.cursor.y();
 
                return inset;
        }
@@ -140,14 +144,14 @@ namespace {
 } // anon namespace
 
 
-Inset * LyXText::checkInsetHit(int & x, int & y)
+InsetOld * LyXText::checkInsetHit(int & x, int & y)
 {
-       int y_tmp = y + top_y();
+       int y_tmp = y + bv_owner->top_y();
 
        LyXCursor cur;
        setCursorFromCoordinates(cur, x, y_tmp);
 
-       Inset * inset = checkInset(bv(), *this, cur, x, y_tmp);
+       InsetOld * inset = checkInset(bv(), *this, cur, x, y_tmp);
        if (inset) {
                y = y_tmp;
                return inset;
@@ -167,14 +171,14 @@ Inset * LyXText::checkInsetHit(int & x, int & y)
 }
 
 
-bool LyXText::gotoNextInset(vector<Inset::Code> const & codes,
+bool LyXText::gotoNextInset(vector<InsetOld::Code> const & codes,
                            string const & contents)
 {
        ParagraphList::iterator end = ownerParagraphs().end();
        ParagraphList::iterator pit = cursor.par();
        pos_type pos = cursor.pos();
 
-       Inset * inset;
+       InsetOld * inset;
        do {
                if (pos + 1 < pit->size()) {
                        ++pos;
@@ -199,16 +203,15 @@ bool LyXText::gotoNextInset(vector<Inset::Code> const & codes,
 }
 
 
-void LyXText::gotoInset(vector<Inset::Code> const & codes,
+void LyXText::gotoInset(vector<InsetOld::Code> const & codes,
                        bool same_content)
 {
        bv()->beforeChange(this);
-       update();
 
        string contents;
        if (same_content && cursor.pos() < cursor.par()->size()
            && cursor.par()->isInset(cursor.pos())) {
-               Inset const * inset = cursor.par()->getInset(cursor.pos());
+               InsetOld const * inset = cursor.par()->getInset(cursor.pos());
                if (find(codes.begin(), codes.end(), inset->lyxCode())
                    != codes.end())
                        contents = static_cast<InsetCommand const *>(inset)->getContents();
@@ -227,102 +230,92 @@ void LyXText::gotoInset(vector<Inset::Code> const & codes,
                        bv()->owner()->message(_("No more insets"));
                }
        }
-       update();
+       bv()->update();
        selection.cursor = cursor;
 }
 
 
-void LyXText::gotoInset(Inset::Code code, bool same_content)
+void LyXText::gotoInset(InsetOld::Code code, bool same_content)
 {
-       gotoInset(vector<Inset::Code>(1, code), same_content);
+       gotoInset(vector<InsetOld::Code>(1, code), same_content);
 }
 
 
 void LyXText::cursorPrevious()
 {
-       int y = top_y();
+       int y = bv_owner->top_y();
+
+       RowList::iterator rit = cursorRow();
 
-       if (cursor.row() == rows().begin()) {
-               if (y > 0) {
-                       int new_y = bv()->text->top_y() - bv()->workHeight();
-                       bv()->screen().draw(bv()->text, bv(), new_y < 0 ? 0 : new_y);
+       if (rit == firstRow()) {
+               if (y > 0)
                        bv()->updateScrollbar();
-               }
                return;
        }
 
-       RowList::iterator cursorrow = cursor.row();
-
        setCursorFromCoordinates(cursor.x_fix(), y);
        finishUndo();
 
        int new_y;
-       if (cursorrow == bv()->text->cursor.row()) {
+       if (rit == bv()->text->cursorRow()) {
                // we have a row which is taller than the workarea. The
                // simplest solution is to move to the previous row instead.
                cursorUp(true);
                return;
                // This is what we used to do, so we wouldn't skip right past
                // tall rows, but it's not working right now.
-#if 0
-               new_y = bv->text->top_y() - bv->workHeight();
-#endif
        } else {
                if (inset_owner) {
-                       new_y = bv()->text->cursor.iy()
+                       new_y = bv()->text->cursor.y()
                                + bv()->theLockingInset()->insetInInsetY() + y
-                               + cursor.row()->height()
+                               + rit->height()
                                - bv()->workHeight() + 1;
                } else {
                        new_y = cursor.y()
-                               - cursor.row()->baseline()
-                               + cursor.row()->height()
+                               - rit->baseline()
+                               + rit->height()
                                - bv()->workHeight() + 1;
                }
        }
-       bv()->screen().draw(bv()->text, bv(), new_y < 0 ? 0 : new_y);
-       if (cursor.row() != rows().begin()) {
-               LyXCursor cur;
-               setCursor(cur, boost::prior(cursor.row())->par(),
-                         boost::prior(cursor.row())->pos(), false);
-               if (cur.y() > top_y()) {
-                       cursorUp(true);
-               }
-       }
+
+       LyXCursor cur;
+       ParagraphList::iterator pit = cursor.par();
+       previousRow(pit, rit);
+       setCursor(cur, pit, rit->pos(), false);
+       if (cur.y() > bv_owner->top_y())
+               cursorUp(true);
        bv()->updateScrollbar();
 }
 
 
 void LyXText::cursorNext()
 {
-       int topy = top_y();
+       int topy = bv_owner->top_y();
 
-       if (boost::next(cursor.row()) == rows().end()) {
-               int y = cursor.y() - cursor.row()->baseline() +
-                       cursor.row()->height();
-               if (y > topy + bv()->workHeight()) {
-                       bv()->screen().draw(bv()->text, bv(), bv()->text->top_y() + bv()->workHeight());
-                       bv()->updateScrollbar();
-               }
+       RowList::iterator rit = cursorRow();
+       if (rit == lastRow()) {
+               int y = cursor.y() - rit->baseline() + cursorRow()->height();
+               if (y > topy + bv()->workHeight())
+                       bv_owner->updateScrollbar();
                return;
        }
 
-       int y = topy + bv()->workHeight();
+       int y = topy + bv_owner->workHeight();
        if (inset_owner && !topy) {
-               y -= (bv()->text->cursor.iy()
-                         - bv()->text->top_y()
-                         + bv()->theLockingInset()->insetInInsetY());
+               y -= (bv_owner->text->cursor.y()
+                         - bv_owner->top_y()
+                         + bv_owner->theLockingInset()->insetInInsetY());
        }
 
-       getRowNearY(y);
+       ParagraphList::iterator dummypit;
+       y = getRowNearY(y, dummypit)->y();
 
-       RowList::iterator cursorrow = cursor.row();
        setCursorFromCoordinates(cursor.x_fix(), y);
        // + bv->workHeight());
        finishUndo();
 
        int new_y;
-       if (cursorrow == bv()->text->cursor.row()) {
+       if (rit == bv_owner->text->cursorRow()) {
                // we have a row which is taller than the workarea. The
                // simplest solution is to move to the next row instead.
                cursorDown(true);
@@ -330,41 +323,35 @@ void LyXText::cursorNext()
                // This is what we used to do, so we wouldn't skip right past
                // tall rows, but it's not working right now.
 #if 0
-               new_y = bv->text->top_y() + bv->workHeight();
+               new_y = bv->top_y() + bv->workHeight();
 #endif
        } else {
                if (inset_owner) {
-                       new_y = bv()->text->cursor.iy()
+                       new_y = bv()->text->cursor.y()
                                + bv()->theLockingInset()->insetInInsetY()
-                               + y - cursor.row()->baseline();
+                               + y - rit->baseline();
                } else {
-                       new_y =  cursor.y() - cursor.row()->baseline();
-               }
-       }
-       bv()->screen().draw(bv()->text, bv(), new_y);
-       if (boost::next(cursor.row()) != rows().end()) {
-               LyXCursor cur;
-               setCursor(cur, boost::next(cursor.row())->par(),
-                         boost::next(cursor.row())->pos(), false);
-               if (cur.y() < top_y() + bv()->workHeight()) {
-                       cursorDown(true);
+                       new_y = cursor.y() - cursorRow()->baseline();
                }
        }
+
+       ParagraphList::iterator pit = cursor.par();
+       nextRow(pit, rit);
+       LyXCursor cur;
+       setCursor(cur, pit, rit->pos(), false);
+       if (cur.y() < bv_owner->top_y() + bv()->workHeight())
+               cursorDown(true);
        bv()->updateScrollbar();
 }
 
 
-void LyXText::update()
-{
-       bv()->update(this, BufferView::SELECT);
-}
-
 namespace {
 
 void specialChar(LyXText * lt, BufferView * bv, InsetSpecialChar::Kind kind)
 {
-       lt->update();
+       bv->update();
        InsetSpecialChar * new_inset = new InsetSpecialChar(kind);
+       replaceSelection(lt);
        if (!bv->insertInset(new_inset))
                delete new_inset;
        else
@@ -375,13 +362,13 @@ void specialChar(LyXText * lt, BufferView * bv, InsetSpecialChar::Kind kind)
 void doInsertInset(LyXText * lt, FuncRequest const & cmd,
                   bool edit, bool pastesel)
 {
-       Inset * inset = createInset(cmd);
+       InsetOld * inset = createInset(cmd);
        BufferView * bv = cmd.view();
 
        if (inset) {
                bool gotsel = false;
                if (lt->selection.set()) {
-                       lt->cutSelection(true, false);
+                       bv->owner()->dispatch(FuncRequest(LFUN_CUT));
                        gotsel = true;
                }
                if (bv->insertInset(inset)) {
@@ -390,7 +377,7 @@ void doInsertInset(LyXText * lt, FuncRequest const & cmd,
                                inset->localDispatch(cmd);
                        }
                        if (gotsel && pastesel)
-                               bv->owner()->dispatch(FuncRequest(LFUN_PASTESELECTION));
+                               bv->owner()->dispatch(FuncRequest(LFUN_PASTE));
                }
                else
                        delete inset;
@@ -399,7 +386,7 @@ void doInsertInset(LyXText * lt, FuncRequest const & cmd,
 
 }
 
-Inset::RESULT LyXText::dispatch(FuncRequest const & cmd)
+InsetOld::RESULT LyXText::dispatch(FuncRequest const & cmd)
 {
        lyxerr[Debug::ACTION] << "LyXFunc::dispatch: action[" << cmd.action
                              <<"] arg[" << cmd.argument << ']' << endl;
@@ -418,54 +405,45 @@ Inset::RESULT LyXText::dispatch(FuncRequest const & cmd)
 
                for (; tmp != end; ++tmp) {
                        if (tmp->params().startOfAppendix()) {
-                               setUndo(bv, Undo::EDIT, tmp, boost::next(tmp));
+                               recordUndo(bv, Undo::ATOMIC, tmp);
                                tmp->params().startOfAppendix(false);
-                               int tmpy;
-                               setHeightOfRow(getRow(tmp, 0, tmpy));
+                               redoParagraph(tmp);
                                break;
                        }
                }
 
-               setUndo(bv, Undo::EDIT, pit, boost::next(pit));
+               recordUndo(bv, Undo::ATOMIC, pit);
                pit->params().startOfAppendix(start);
 
                // we can set the refreshing parameters now
                updateCounters();
-               redoHeightOfParagraph();
-               postPaint(0);
+               redoParagraph(cursor.par());
                setCursor(cursor.par(), cursor.pos());
-               update();
+               bv->update();
                break;
        }
 
        case LFUN_DELETE_WORD_FORWARD:
                bv->beforeChange(this);
-               update();
                deleteWordForward();
-               update();
                finishChange(bv);
                break;
 
        case LFUN_DELETE_WORD_BACKWARD:
                bv->beforeChange(this);
-               update();
                deleteWordBackward();
-               update();
                finishChange(bv);
                break;
 
        case LFUN_DELETE_LINE_FORWARD:
                bv->beforeChange(this);
-               update();
                deleteLineForward();
-               update();
                finishChange(bv);
                break;
 
        case LFUN_WORDRIGHT:
                if (!selection.mark())
                        bv->beforeChange(this);
-               update();
                if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
                        cursorLeftOneWord();
                else
@@ -476,7 +454,6 @@ Inset::RESULT LyXText::dispatch(FuncRequest const & cmd)
        case LFUN_WORDLEFT:
                if (!selection.mark())
                        bv->beforeChange(this);
-               update();
                if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
                        cursorRightOneWord();
                else
@@ -487,7 +464,6 @@ Inset::RESULT LyXText::dispatch(FuncRequest const & cmd)
        case LFUN_BEGINNINGBUF:
                if (!selection.mark())
                        bv->beforeChange(this);
-               update();
                cursorTop();
                finishChange(bv);
                break;
@@ -495,13 +471,13 @@ Inset::RESULT LyXText::dispatch(FuncRequest const & cmd)
        case LFUN_ENDBUF:
                if (selection.mark())
                        bv->beforeChange(this);
-               update();
                cursorBottom();
                finishChange(bv);
                break;
 
        case LFUN_RIGHTSEL:
-               update();
+               if (!selection.set())
+                       selection.cursor = cursor;
                if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
                        cursorLeft(bv);
                else
@@ -510,7 +486,8 @@ Inset::RESULT LyXText::dispatch(FuncRequest const & cmd)
                break;
 
        case LFUN_LEFTSEL:
-               update();
+               if (!selection.set())
+                       selection.cursor = cursor;
                if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
                        cursorRight(bv);
                else
@@ -519,55 +496,62 @@ Inset::RESULT LyXText::dispatch(FuncRequest const & cmd)
                break;
 
        case LFUN_UPSEL:
-               update();
+               if (!selection.set())
+                       selection.cursor = cursor;
                cursorUp(true);
                finishChange(bv, true);
                break;
 
        case LFUN_DOWNSEL:
-               update();
+               if (!selection.set())
+                       selection.cursor = cursor;
                cursorDown(true);
                finishChange(bv, true);
                break;
 
        case LFUN_UP_PARAGRAPHSEL:
-               update();
+               if (!selection.set())
+                       selection.cursor = cursor;
                cursorUpParagraph();
                finishChange(bv, true);
                break;
 
        case LFUN_DOWN_PARAGRAPHSEL:
-               update();
+               if (!selection.set())
+                       selection.cursor = cursor;
                cursorDownParagraph();
                finishChange(bv, true);
                break;
 
        case LFUN_PRIORSEL:
-               update();
+               if (!selection.set())
+                       selection.cursor = cursor;
                cursorPrevious();
                finishChange(bv, true);
                break;
 
        case LFUN_NEXTSEL:
-               update();
+               if (!selection.set())
+                       selection.cursor = cursor;
                cursorNext();
                finishChange(bv, true);
                break;
 
        case LFUN_HOMESEL:
-               update();
+               if (!selection.set())
+                       selection.cursor = cursor;
                cursorHome();
                finishChange(bv, true);
                break;
 
        case LFUN_ENDSEL:
-               update();
+               if (!selection.set())
+                       selection.cursor = cursor;
                cursorEnd();
                finishChange(bv, true);
                break;
 
        case LFUN_WORDRIGHTSEL:
-               update();
                if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
                        cursorLeftOneWord();
                else
@@ -576,7 +560,6 @@ Inset::RESULT LyXText::dispatch(FuncRequest const & cmd)
                break;
 
        case LFUN_WORDLEFTSEL:
-               update();
                if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
                        cursorRightOneWord();
                else
@@ -585,10 +568,9 @@ Inset::RESULT LyXText::dispatch(FuncRequest const & cmd)
                break;
 
        case LFUN_WORDSEL: {
-               update();
-               LyXCursor cur1;
+               LyXCursor cur1 = cursor;
                LyXCursor cur2;
-               getWord(cur1, cur2, WHOLE_WORD);
+               ::getWord(cur1, cur2, lyx::WHOLE_WORD, ownerParagraphs());
                setCursor(cur1.par(), cur1.pos());
                bv->beforeChange(this);
                setCursor(cur2.par(), cur2.pos());
@@ -600,15 +582,14 @@ Inset::RESULT LyXText::dispatch(FuncRequest const & cmd)
                bool is_rtl = cursor.par()->isRightToLeftPar(bv->buffer()->params);
                if (!selection.mark())
                        bv->beforeChange(this);
-               update();
                if (is_rtl)
                        cursorLeft(false);
                if (cursor.pos() < cursor.par()->size()
                    && cursor.par()->isInset(cursor.pos())
                    && isHighlyEditableInset(cursor.par()->getInset(cursor.pos()))) {
-                       Inset * tmpinset = cursor.par()->getInset(cursor.pos());
+                       InsetOld * tmpinset = cursor.par()->getInset(cursor.pos());
                        cmd.message(tmpinset->editMessage());
-                       FuncRequest cmd1(bv, LFUN_INSET_EDIT, is_rtl ? "left" : "right");
+                       FuncRequest cmd1(bv, LFUN_INSET_EDIT, is_rtl ? "right" : "left");
                        tmpinset->localDispatch(cmd1);
                        break;
                }
@@ -624,7 +605,6 @@ Inset::RESULT LyXText::dispatch(FuncRequest const & cmd)
                bool const is_rtl = cursor.par()->isRightToLeftPar(bv->buffer()->params);
                if (!selection.mark())
                        bv->beforeChange(this);
-               update();
                LyXCursor const cur = cursor;
                if (!is_rtl)
                        cursorLeft(false);
@@ -632,9 +612,9 @@ Inset::RESULT LyXText::dispatch(FuncRequest const & cmd)
                    cursor.pos() < cursor.par()->size() &&
                    cursor.par()->isInset(cursor.pos()) &&
                    isHighlyEditableInset(cursor.par()->getInset(cursor.pos()))) {
-                       Inset * tmpinset = cursor.par()->getInset(cursor.pos());
+                       InsetOld * tmpinset = cursor.par()->getInset(cursor.pos());
                        cmd.message(tmpinset->editMessage());
-                       FuncRequest cmd1(bv, LFUN_INSET_EDIT, is_rtl ? "right" : "left");
+                       FuncRequest cmd1(bv, LFUN_INSET_EDIT, is_rtl ? "left" : "right");
                        tmpinset->localDispatch(cmd1);
                        break;
                }
@@ -647,23 +627,20 @@ Inset::RESULT LyXText::dispatch(FuncRequest const & cmd)
        case LFUN_UP:
                if (!selection.mark())
                        bv->beforeChange(this);
-               bv->update(this, BufferView::UPDATE);
-               cursorUp(bv);
+               cursorUp(false);
                finishChange(bv);
                break;
 
        case LFUN_DOWN:
                if (!selection.mark())
                        bv->beforeChange(this);
-               bv->update(this, BufferView::UPDATE);
-               cursorDown(bv);
+               cursorDown(false);
                finishChange(bv);
                break;
 
        case LFUN_UP_PARAGRAPH:
                if (!selection.mark())
                        bv->beforeChange(this);
-               bv->update(this, BufferView::UPDATE);
                cursorUpParagraph();
                finishChange(bv);
                break;
@@ -671,7 +648,6 @@ Inset::RESULT LyXText::dispatch(FuncRequest const & cmd)
        case LFUN_DOWN_PARAGRAPH:
                if (!selection.mark())
                        bv->beforeChange(this);
-               bv->update(this, BufferView::UPDATE);
                cursorDownParagraph();
                finishChange(bv, false);
                break;
@@ -679,19 +655,13 @@ Inset::RESULT LyXText::dispatch(FuncRequest const & cmd)
        case LFUN_PRIOR:
                if (!selection.mark())
                        bv->beforeChange(this);
-               bv->update(this, BufferView::UPDATE);
                cursorPrevious();
                finishChange(bv, false);
-               // was:
-               // finishUndo();
-               // moveCursorUpdate(bv, false, false);
-               // owner_->view_state_changed();
                break;
 
        case LFUN_NEXT:
                if (!selection.mark())
                        bv->beforeChange(this);
-               bv->update(this, BufferView::UPDATE);
                cursorNext();
                finishChange(bv, false);
                break;
@@ -699,7 +669,6 @@ Inset::RESULT LyXText::dispatch(FuncRequest const & cmd)
        case LFUN_HOME:
                if (!selection.mark())
                        bv->beforeChange(this);
-               update();
                cursorHome();
                finishChange(bv, false);
                break;
@@ -707,7 +676,6 @@ Inset::RESULT LyXText::dispatch(FuncRequest const & cmd)
        case LFUN_END:
                if (!selection.mark())
                        bv->beforeChange(this);
-               update();
                cursorEnd();
                finishChange(bv, false);
                break;
@@ -719,9 +687,8 @@ Inset::RESULT LyXText::dispatch(FuncRequest const & cmd)
                if (cursor.pos() <= body)
                        break;
 
-               bv->beforeChange(this);
+               replaceSelection(bv->getLyXText());
                insertInset(new InsetNewline);
-               update();
                setCursor(cursor.par(), cursor.pos());
                moveCursorUpdate(bv, false);
                break;
@@ -731,17 +698,13 @@ Inset::RESULT LyXText::dispatch(FuncRequest const & cmd)
                if (!selection.set()) {
                        Delete();
                        selection.cursor = cursor;
-                       update();
                        // It is possible to make it a lot faster still
                        // just comment out the line below...
                } else {
-                       update();
-                       cutSelection(bv, true);
-                       update();
+                       cutSelection(true, false);
                }
                moveCursorUpdate(bv, false);
                bv->owner()->view_state_changed();
-               bv->switchKeyMap();
                break;
 
        case LFUN_DELETE_SKIP:
@@ -765,7 +728,6 @@ Inset::RESULT LyXText::dispatch(FuncRequest const & cmd)
                                                 cur.par()->params().align(),
                                                 cur.par()->params().labelWidthString(), 0);
                                        cursorLeft(bv);
-                                       update();
                                } else {
                                        cursorLeft(bv);
                                        Delete();
@@ -776,10 +738,9 @@ Inset::RESULT LyXText::dispatch(FuncRequest const & cmd)
                                selection.cursor = cursor;
                        }
                } else {
-                       update();
-                       cutSelection(bv, true);
+                       cutSelection(true, false);
                }
-               update();
+               bv->update();
                break;
 
 
@@ -788,17 +749,15 @@ Inset::RESULT LyXText::dispatch(FuncRequest const & cmd)
                        if (bv->owner()->getIntl().getTransManager().backspace()) {
                                backspace();
                                selection.cursor = cursor;
-                               update();
                                // It is possible to make it a lot faster still
                                // just comment out the line below...
                        }
                } else {
-                       update();
-                       cutSelection(bv, true);
-                       update();
+                       cutSelection(true, false);
                }
                bv->owner()->view_state_changed();
                bv->switchKeyMap();
+               bv->update();
                break;
 
        case LFUN_BACKSPACE_SKIP:
@@ -806,14 +765,14 @@ Inset::RESULT LyXText::dispatch(FuncRequest const & cmd)
                if (!selection.set()) {
                        LyXCursor cur = cursor;
                        if (cur.pos() == 0
-                           && !(cur.par()->params().spaceTop()
-                                == VSpace (VSpace::NONE))) {
+                           && !(cur.par()->params().spaceTop() == VSpace(VSpace::NONE))) {
                                setParagraph(
                                         cur.par()->params().lineTop(),
                                         cur.par()->params().lineBottom(),
                                         cur.par()->params().pagebreakTop(),
                                         cur.par()->params().pagebreakBottom(),
-                                        VSpace(VSpace::NONE), cur.par()->params().spaceBottom(),
+                                        VSpace(VSpace::NONE),
+                                  cur.par()->params().spaceBottom(),
                                         cur.par()->params().spacing(),
                                         cur.par()->params().align(),
                                         cur.par()->params().labelWidthString(), 0);
@@ -822,25 +781,24 @@ Inset::RESULT LyXText::dispatch(FuncRequest const & cmd)
                                selection.cursor = cur;
                        }
                } else {
-                       update();
-                       cutSelection(bv, true);
+                       cutSelection(true, false);
                }
-               update();
+               bv->update();
                break;
 
        case LFUN_BREAKPARAGRAPH:
-               bv->beforeChange(this);
+               replaceSelection(bv->getLyXText());
                breakParagraph(bv->buffer()->paragraphs, 0);
-               update();
+               bv->update();
                selection.cursor = cursor;
                bv->switchKeyMap();
                bv->owner()->view_state_changed();
                break;
 
        case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
-               bv->beforeChange(this);
+               replaceSelection(bv->getLyXText());
                breakParagraph(bv->buffer()->paragraphs, 1);
-               update();
+               bv->update();
                selection.cursor = cursor;
                bv->switchKeyMap();
                bv->owner()->view_state_changed();
@@ -851,7 +809,7 @@ Inset::RESULT LyXText::dispatch(FuncRequest const & cmd)
                // indentation and add a "defskip" at the top.
                // Otherwise, do the same as LFUN_BREAKPARAGRAPH.
                LyXCursor cur = cursor;
-               bv->beforeChange(this);
+               replaceSelection(bv->getLyXText());
                if (cur.pos() == 0) {
                        if (cur.par()->params().spaceTop() == VSpace(VSpace::NONE)) {
                                setParagraph(
@@ -868,7 +826,7 @@ Inset::RESULT LyXText::dispatch(FuncRequest const & cmd)
                else {
                        breakParagraph(bv->buffer()->paragraphs, 0);
                }
-               update();
+               bv->update();
                selection.cursor = cur;
                bv->switchKeyMap();
                bv->owner()->view_state_changed();
@@ -912,31 +870,28 @@ Inset::RESULT LyXText::dispatch(FuncRequest const & cmd)
                if (cur_spacing != new_spacing || cur_value != new_value) {
                        pit->params().spacing(Spacing(new_spacing, new_value));
                        redoParagraph();
-                       update();
+                       bv->update();
                }
                break;
        }
 
        case LFUN_INSET_SETTINGS:
-               lyx::Assert(bv->theLockingInset());
+               Assert(bv->theLockingInset());
                bv->theLockingInset()->getLockingInset()->showInsetDialog(bv);
                break;
 
        case LFUN_INSET_TOGGLE:
                bv->beforeChange(this);
-               update();
                toggleInset();
-               update();
+               bv->update();
                bv->switchKeyMap();
                break;
 
        case LFUN_SPACE_INSERT:
-               if (cursor.par()->layout()->free_spacing) {
+               if (cursor.par()->layout()->free_spacing)
                        insertChar(' ');
-                       update();
-               } else {
+               else
                        doInsertInset(this, cmd, false, false);
-               }
                moveCursorUpdate(bv, false);
                break;
 
@@ -962,7 +917,7 @@ Inset::RESULT LyXText::dispatch(FuncRequest const & cmd)
 
        case LFUN_MARK_OFF:
                bv->beforeChange(this);
-               update();
+               bv->update();
                selection.cursor = cursor;
                cmd.message(N_("Mark off"));
                break;
@@ -970,7 +925,7 @@ Inset::RESULT LyXText::dispatch(FuncRequest const & cmd)
        case LFUN_MARK_ON:
                bv->beforeChange(this);
                selection.mark(true);
-               update();
+               bv->update();
                selection.cursor = cursor;
                cmd.message(N_("Mark on"));
                break;
@@ -978,65 +933,57 @@ Inset::RESULT LyXText::dispatch(FuncRequest const & cmd)
        case LFUN_SETMARK:
                bv->beforeChange(this);
                if (selection.mark()) {
-                       update();
                        cmd.message(N_("Mark removed"));
                } else {
                        selection.mark(true);
-                       update();
                        cmd.message(N_("Mark set"));
                }
                selection.cursor = cursor;
+               bv->update();
                break;
 
        case LFUN_UPCASE_WORD:
-               update();
                changeCase(LyXText::text_uppercase);
-               if (inset_owner)
-                       bv->updateInset(inset_owner);
-               update();
+               bv->update();
                break;
 
        case LFUN_LOWCASE_WORD:
-               update();
                changeCase(LyXText::text_lowercase);
-               if (inset_owner)
-                       bv->updateInset(inset_owner);
-               update();
+               bv->update();
                break;
 
        case LFUN_CAPITALIZE_WORD:
-               update();
                changeCase(LyXText::text_capitalization);
-               if (inset_owner)
-                       bv->updateInset(inset_owner);
-               update();
+               bv->update();
                break;
 
        case LFUN_TRANSPOSE_CHARS:
-               update();
-               transposeChars(*this, cursor);
-               if (inset_owner)
-                       bv->updateInset(inset_owner);
-               update();
+               recordUndo(bv, Undo::ATOMIC, cursor.par());
+               redoParagraph();
+               bv->update();
                break;
 
-       case LFUN_PASTE:
+       case LFUN_PASTE: {
                cmd.message(_("Paste"));
-               // clear the selection
-               bv->toggleSelection();
-               clearSelection();
-               update();
-               pasteSelection();
+               replaceSelection(bv->getLyXText());
+               size_t sel_index = 0;
+               string const & arg = cmd.argument;
+               if (isStrUnsignedInt(arg)) {
+                       size_t const paste_arg = strToUnsignedInt(arg);
+#warning FIXME Check if the arg is in the domain of available selections.
+                       sel_index = paste_arg;
+               }
+               pasteSelection(sel_index);
                clearSelection(); // bug 393
-               update();
+               bv->update();
                bv->switchKeyMap();
                break;
+       }
 
        case LFUN_CUT:
-               update();
-               cutSelection(bv, true);
-               update();
+               cutSelection(true, true);
                cmd.message(_("Cut"));
+               bv->update();
                break;
 
        case LFUN_COPY:
@@ -1047,7 +994,6 @@ Inset::RESULT LyXText::dispatch(FuncRequest const & cmd)
        case LFUN_BEGINNINGBUFSEL:
                if (inset_owner)
                        return UNDISPATCHED;
-               update();
                cursorTop();
                finishChange(bv, true);
                break;
@@ -1055,7 +1001,6 @@ Inset::RESULT LyXText::dispatch(FuncRequest const & cmd)
        case LFUN_ENDBUFSEL:
                if (inset_owner)
                        return UNDISPATCHED;
-               update();
                cursorBottom();
                finishChange(bv, true);
                break;
@@ -1141,18 +1086,15 @@ Inset::RESULT LyXText::dispatch(FuncRequest const & cmd)
 
                if (change_layout) {
                        current_layout = layout;
-                       update();
                        setLayout(layout);
                        bv->owner()->setLayout(layout);
-                       update();
+                       bv->update();
                        bv->switchKeyMap();
                }
                break;
        }
 
        case LFUN_PASTESELECTION: {
-               if (!bv->buffer())
-                       break;
                // this was originally a beforeChange(bv->text), i.e
                // the outermost LyXText!
                bv->beforeChange(this);
@@ -1163,29 +1105,30 @@ Inset::RESULT LyXText::dispatch(FuncRequest const & cmd)
                        else
                                insertStringAsLines(clip);
                        clearSelection();
-                       update();
+                       bv->update();
                }
                break;
        }
 
        case LFUN_GOTOERROR:
-               gotoInset(Inset::ERROR_CODE, false);
+               gotoInset(InsetOld::ERROR_CODE, false);
                break;
 
        case LFUN_GOTONOTE:
-               gotoInset(Inset::NOTE_CODE, false);
+               gotoInset(InsetOld::NOTE_CODE, false);
                break;
 
        case LFUN_REFERENCE_GOTO:
        {
-               vector<Inset::Code> tmp;
-               tmp.push_back(Inset::LABEL_CODE);
-               tmp.push_back(Inset::REF_CODE);
+               vector<InsetOld::Code> tmp;
+               tmp.push_back(InsetOld::LABEL_CODE);
+               tmp.push_back(InsetOld::REF_CODE);
                gotoInset(tmp, true);
                break;
        }
 
        case LFUN_QUOTE: {
+               replaceSelection(bv->getLyXText());
                ParagraphList::iterator pit = cursor.par();
                lyx::pos_type pos = cursor.pos();
                char c;
@@ -1207,6 +1150,7 @@ Inset::RESULT LyXText::dispatch(FuncRequest const & cmd)
        }
 
        case LFUN_DATE_INSERT: {
+               replaceSelection(bv->getLyXText());
                time_t now_time_t = time(NULL);
                struct tm * now_tm = localtime(&now_time_t);
                setlocale(LC_TIME, "");
@@ -1219,10 +1163,9 @@ Inset::RESULT LyXText::dispatch(FuncRequest const & cmd)
                int const datetmp_len =
                        ::strftime(datetmp, 32, arg.c_str(), now_tm);
 
-               for (int i = 0; i < datetmp_len; i++) {
+               for (int i = 0; i < datetmp_len; i++)
                        insertChar(datetmp[i]);
-                       update();
-               }
+
                selection.cursor = cursor;
                moveCursorUpdate(bv, false);
                break;
@@ -1234,16 +1177,11 @@ Inset::RESULT LyXText::dispatch(FuncRequest const & cmd)
                if (!isInInset() && bv->theLockingInset())
                        break;
                if (cmd.button() == mouse_button::button1) {
-                       if (!isInInset()) {
-                               bv->screen().toggleSelection(this, bv);
-                       }
                        cursorHome();
                        selection.cursor = cursor;
                        cursorEnd();
                        setSelection();
-                       if (!isInInset())
-                               bv->screen().toggleSelection(this, bv, false);
-                       update();
+                       bv->update();
                        bv->haveSelection(selection.set());
                }
                break;
@@ -1254,14 +1192,8 @@ Inset::RESULT LyXText::dispatch(FuncRequest const & cmd)
                if (!isInInset() && bv->theLockingInset())
                        break;
                if (cmd.button() == mouse_button::button1) {
-                       if (!isInInset()) {
-                               bv->screen().toggleSelection(this, bv);
-                               selectWord(LyXText::WHOLE_WORD_STRICT);
-                               bv->screen().toggleSelection(this, bv, false);
-                       } else {
-                               selectWord(LyXText::WHOLE_WORD_STRICT);
-                       }
-                       update();
+                       selectWord(lyx::WHOLE_WORD_STRICT);
+                       bv->update();
                        bv->haveSelection(selection.set());
                }
                break;
@@ -1277,17 +1209,16 @@ Inset::RESULT LyXText::dispatch(FuncRequest const & cmd)
 
                // Check for inset locking
                if (bv->theLockingInset()) {
-                       Inset * tli = bv->theLockingInset();
+                       InsetOld * tli = bv->theLockingInset();
                        LyXCursor cursor = bv->text->cursor;
-                       LyXFont font = bv->text->getFont(bv->buffer(),
-                                                        cursor.par(), cursor.pos());
-                       int width = tli->width(bv, font);
+                       LyXFont font = bv->text->getFont(cursor.par(), cursor.pos());
+                       int width = tli->width();
                        int inset_x = font.isVisibleRightToLeft()
-                               ? cursor.ix() - width : cursor.ix();
+                               ? cursor.x() - width : cursor.x();
                        int start_x = inset_x + tli->scroll();
                        FuncRequest cmd1 = cmd;
                        cmd1.x = cmd.x - start_x;
-                       cmd1.y = cmd.y - cursor.iy() + bv->text->top_y();
+                       cmd1.y = cmd.y - cursor.y() + bv->top_y();
                        tli->localDispatch(cmd1);
                        break;
                }
@@ -1301,8 +1232,8 @@ Inset::RESULT LyXText::dispatch(FuncRequest const & cmd)
                        break;
                }
 
-               RowList::iterator cursorrow = bv->text->cursor.row();
-               bv->text->setCursorFromCoordinates(cmd.x, cmd.y + bv->text->top_y());
+               RowList::iterator cursorrow = bv->text->cursorRow();
+               bv->text->setCursorFromCoordinates(cmd.x, cmd.y + bv->top_y());
        #if 0
                // sorry for this but I have a strange error that the y value jumps at
                // a certain point. This seems like an error in my xforms library or
@@ -1313,19 +1244,15 @@ Inset::RESULT LyXText::dispatch(FuncRequest const & cmd)
                               << bv->text->cursor.y() << endl;
        #endif
                // This is to allow jumping over large insets
-               if (cursorrow == bv->text->cursor.row()) {
+               if (cursorrow == bv->text->cursorRow()) {
                        if (cmd.y >= bv->workHeight())
                                bv->text->cursorDown(false);
                        else if (cmd.y < 0)
                                bv->text->cursorUp(false);
                }
 
-               // Maybe an empty line was deleted
-               if (!bv->text->selection.set())
-                       bv->update(BufferView::UPDATE);
                bv->text->setSelection();
-               bv->screen().toggleToggle(bv->text, bv);
-               bv->fitCursor();
+               bv->update();
                break;
        }
 
@@ -1350,7 +1277,7 @@ Inset::RESULT LyXText::dispatch(FuncRequest const & cmd)
 
                int x = cmd.x;
                int y = cmd.y;
-               Inset * inset_hit = bv->text->checkInsetHit(x, y);
+               InsetOld * inset_hit = bv->text->checkInsetHit(x, y);
 
                // Middle button press pastes if we have a selection
                // We do this here as if the selection was inside an inset
@@ -1362,7 +1289,7 @@ Inset::RESULT LyXText::dispatch(FuncRequest const & cmd)
                        paste_internally = true;
                }
 
-               int const screen_first = bv->text->top_y();
+               int const screen_first = bv->top_y();
 
                if (bv->theLockingInset()) {
                        // We are in inset locking mode
@@ -1381,9 +1308,7 @@ Inset::RESULT LyXText::dispatch(FuncRequest const & cmd)
                        selection_possible = true;
 
                // Clear the selection
-               bv->screen().toggleSelection(bv->text, bv);
                bv->text->clearSelection();
-               bv->text->fullRebreak();
                bv->update();
                bv->updateScrollbar();
 
@@ -1445,7 +1370,7 @@ Inset::RESULT LyXText::dispatch(FuncRequest const & cmd)
                // inset, inset_hit is 0, and inset_x == x, inset_y == y.
                int x = cmd.x;
                int y = cmd.y;
-               Inset * inset_hit = bv->text->checkInsetHit(x, y);
+               InsetOld * inset_hit = bv->text->checkInsetHit(x, y);
 
                if (bv->theLockingInset()) {
                        // We are in inset locking mode.
@@ -1503,10 +1428,10 @@ Inset::RESULT LyXText::dispatch(FuncRequest const & cmd)
                        // stack. They don't *have* to
                        // alter the document...
                        // (Joacim)
-                       // ...or maybe the SetCursorParUndo()
+                       // ...or maybe the recordUndo()
                        // below isn't necessary at all anylonger?
-                       if (inset_hit->lyxCode() == Inset::REF_CODE)
-                               setCursorParUndo(bv);
+                       if (inset_hit->lyxCode() == InsetOld::REF_CODE)
+                               recordUndo(bv, Undo::ATOMIC);
 
                        bv->owner()->message(inset_hit->editMessage());
 
@@ -1528,15 +1453,13 @@ Inset::RESULT LyXText::dispatch(FuncRequest const & cmd)
                // true (on).
 
                if (lyxrc.auto_region_delete) {
-                       if (selection.set()) {
+                       if (selection.set())
                                cutSelection(false, false);
-                               update();
-                       }
                        bv->haveSelection(false);
                }
 
                bv->beforeChange(this);
-               LyXFont const old_font(real_current_font);
+               LyXFont const old_font = real_current_font;
 
                string::const_iterator cit = cmd.argument.begin();
                string::const_iterator end = cmd.argument.end();
@@ -1544,7 +1467,6 @@ Inset::RESULT LyXText::dispatch(FuncRequest const & cmd)
                        bv->owner()->getIntl().getTransManager().
                                TranslateAndInsert(*cit, this);
 
-               update();
                selection.cursor = cursor;
                moveCursorUpdate(bv, false);
 
@@ -1552,6 +1474,7 @@ Inset::RESULT LyXText::dispatch(FuncRequest const & cmd)
                // update the minibuffer
                if (old_font != real_current_font)
                        bv->owner()->view_state_changed();
+               bv->updateScrollbar();
                break;
        }
 
@@ -1576,6 +1499,7 @@ Inset::RESULT LyXText::dispatch(FuncRequest const & cmd)
        case LFUN_INSET_CAPTION:
 #endif
        case LFUN_INSERT_NOTE:
+       case LFUN_INSERT_BRANCH:
        case LFUN_INSERT_BIBITEM:
        case LFUN_INSET_ERT:
        case LFUN_INSET_FLOAT:
@@ -1598,7 +1522,6 @@ Inset::RESULT LyXText::dispatch(FuncRequest const & cmd)
                break;
 
        case LFUN_INDEX_PRINT:
-       case LFUN_PARENTINSERT:
        case LFUN_TOC_INSERT:
        case LFUN_HFILL:
                // do nothing fancy