]> git.lyx.org Git - lyx.git/blobdiff - src/BufferView_pimpl.C
Part of IU.
[lyx.git] / src / BufferView_pimpl.C
index 76206336463998a3850529294b69810a25c9225d..a6db146074e208901149c6ce67e2e3ea341febb0 100644 (file)
@@ -62,6 +62,7 @@
 #include "mathed/formulabase.h"
 
 #include "support/filetools.h"
+#include "support/globbing.h"
 #include "support/path_defines.h"
 #include "support/tostr.h"
 
@@ -73,6 +74,7 @@ using lyx::pos_type;
 
 using lyx::support::AddPath;
 using lyx::support::bformat;
+using lyx::support::FileFilterList;
 using lyx::support::FileSearch;
 using lyx::support::IsDirWriteable;
 using lyx::support::MakeDisplayPath;
@@ -109,11 +111,13 @@ boost::signals::connection lostcon;
 } // anon namespace
 
 
-BufferView::Pimpl::Pimpl(BufferView * bv, LyXView * owner,
+BufferView::Pimpl::Pimpl(BufferView & bv, LyXView * owner,
             int xpos, int ypos, int width, int height)
-       : bv_(bv), owner_(owner), buffer_(0), cursor_timeout(400),
+       : bv_(&bv), owner_(owner), buffer_(0), cursor_timeout(400),
          using_xterm_cursor(false), cursor_(bv)
 {
+       xsel_cache_.set = false;
+
        workarea_.reset(WorkAreaFactory::create(xpos, ypos, width, height));
        screen_.reset(LyXScreenFactory::create(workarea()));
 
@@ -156,13 +160,20 @@ void BufferView::Pimpl::connectBuffer(Buffer & buf)
        if (errorConnection_.connected())
                disconnectBuffer();
 
-       errorConnection_ = buf.error.connect(boost::bind(&BufferView::Pimpl::addError, this, _1));
-       messageConnection_ = buf.message.connect(boost::bind(&LyXView::message, owner_, _1));
-       busyConnection_ = buf.busy.connect(boost::bind(&LyXView::busy, owner_, _1));
-       titleConnection_ = buf.updateTitles.connect(boost::bind(&LyXView::updateWindowTitle, owner_));
-       timerConnection_ = buf.resetAutosaveTimers.connect(boost::bind(&LyXView::resetAutosaveTimer, owner_));
-       readonlyConnection_ = buf.readonly.connect(boost::bind(&BufferView::Pimpl::showReadonly, this, _1));
-       closingConnection_ = buf.closing.connect(boost::bind(&BufferView::Pimpl::buffer, this, (Buffer *)0));
+       errorConnection_ =
+               buf.error.connect(boost::bind(&BufferView::Pimpl::addError, this, _1));
+       messageConnection_ =
+               buf.message.connect(boost::bind(&LyXView::message, owner_, _1));
+       busyConnection_ =
+               buf.busy.connect(boost::bind(&LyXView::busy, owner_, _1));
+       titleConnection_ =
+               buf.updateTitles.connect(boost::bind(&LyXView::updateWindowTitle, owner_));
+       timerConnection_ =
+               buf.resetAutosaveTimers.connect(boost::bind(&LyXView::resetAutosaveTimer, owner_));
+       readonlyConnection_ =
+               buf.readonly.connect(boost::bind(&BufferView::Pimpl::showReadonly, this, _1));
+       closingConnection_ =
+               buf.closing.connect(boost::bind(&BufferView::Pimpl::buffer, this, (Buffer *)0));
 }
 
 
@@ -287,8 +298,8 @@ void BufferView::Pimpl::buffer(Buffer * b)
                            << b << ')' << endl;
        if (buffer_) {
                disconnectBuffer();
-               delete bv_->text;
-               bv_->text = 0;
+               //delete bv_->text();
+               //bv_->setText(0);
        }
 
        // set current buffer
@@ -308,8 +319,12 @@ void BufferView::Pimpl::buffer(Buffer * b)
                lyxerr[Debug::INFO] << "Buffer addr: " << buffer_ << endl;
                connectBuffer(*buffer_);
 
+               buffer_->text().init(bv_);
+               buffer_->text().textwidth_ = workarea().workWidth();
+               buffer_->text().fullRebreak();
+
                // If we don't have a text object for this, we make one
-               if (bv_->text == 0)
+               if (bv_->text() == 0)
                        resizeCurrentBuffer();
 
                // FIXME: needed when ?
@@ -333,7 +348,7 @@ void BufferView::Pimpl::buffer(Buffer * b)
 
        // Don't forget to update the Layout
        if (buffer_)
-               owner_->setLayout(bv_->text->cursorPar()->layout()->name());
+               owner_->setLayout(bv_->text()->cursorPar()->layout()->name());
 
        if (lyx::graphics::Previews::activated() && buffer_)
                lyx::graphics::Previews::get().generateBufferPreviews(*buffer_);
@@ -354,11 +369,10 @@ bool BufferView::Pimpl::fitCursor()
 void BufferView::Pimpl::redoCurrentBuffer()
 {
        lyxerr[Debug::INFO] << "BufferView::redoCurrentBuffer" << endl;
-       if (buffer_ && bv_->text) {
+       if (buffer_ && bv_->text()) {
                resizeCurrentBuffer();
                updateScrollbar();
                owner_->updateLayoutChoice();
-               update();
        }
 }
 
@@ -374,44 +388,46 @@ void BufferView::Pimpl::resizeCurrentBuffer()
        pos_type pos = 0;
        pos_type selstartpos = 0;
        pos_type selendpos = 0;
-       bool selection = false;
+       bool sel = false;
        bool mark_set  = false;
 
        owner_->busy(true);
 
        owner_->message(_("Formatting document..."));
 
-       if (bv_->text) {
-               par = bv_->text->cursor.par();
-               pos = bv_->text->cursor.pos();
-               selstartpar = bv_->text->selection.start.par();
-               selstartpos = bv_->text->selection.start.pos();
-               selendpar = bv_->text->selection.end.par();
-               selendpos = bv_->text->selection.end.pos();
-               selection = bv_->text->selection.set();
-               mark_set = bv_->text->selection.mark();
-               bv_->text->fullRebreak();
-               update();
-       } else {
-               bv_->text = new LyXText(bv_, 0, false, bv_->buffer()->paragraphs());
-               bv_->text->init(bv_);
-       }
+       LyXText * text = bv_->text();
+       lyxerr << "### resizeCurrentBuffer: text " << text << endl;
+       if (!text)
+               return;
+
+       LCursor & cur = bv_->cursor();
+       par = cur.par();
+       pos = cur.pos();
+       selstartpar = cur.selStart().par();
+       selstartpos = cur.selStart().pos();
+       selendpar = cur.selEnd().par();
+       selendpos = cur.selEnd().pos();
+       sel = cur.selection();
+       mark_set = cur.mark();
+       text->textwidth_ = bv_->workWidth();
+       text->fullRebreak();
+       update();
 
        if (par != -1) {
-               bv_->text->selection.set(true);
+               cur.selection() = true;
                // At this point just to avoid the Delete-Empty-Paragraph-
                // Mechanism when setting the cursor.
-               bv_->text->selection.mark(mark_set);
-               if (selection) {
-                       bv_->text->setCursor(selstartpar, selstartpos);
-                       bv_->text->selection.cursor = bv_->text->cursor;
-                       bv_->text->setCursor(selendpar, selendpos);
-                       bv_->text->setSelection();
-                       bv_->text->setCursor(par, pos);
+               cur.mark() = mark_set;
+               if (sel) {
+                       text->setCursor(selstartpar, selstartpos);
+                       cur.resetAnchor();
+                       text->setCursor(selendpar, selendpos);
+                       cur.setSelection();
+                       text->setCursor(par, pos);
                } else {
-                       bv_->text->setCursor(par, pos);
-                       bv_->text->selection.cursor = bv_->text->cursor;
-                       bv_->text->selection.set(false);
+                       text->setCursor(par, pos);
+                       cur.resetAnchor();
+                       cur.selection() = false;
                }
        }
 
@@ -429,13 +445,13 @@ void BufferView::Pimpl::resizeCurrentBuffer()
 
 void BufferView::Pimpl::updateScrollbar()
 {
-       if (!bv_->text) {
+       if (!bv_->text()) {
                lyxerr[Debug::GUI] << "no text in updateScrollbar" << endl;
                workarea().setScrollbarParams(0, 0, 0);
                return;
        }
 
-       LyXText const & t = *bv_->text;
+       LyXText const & t = *bv_->text();
 
        lyxerr[Debug::GUI] << "Updating scrollbar: h " << t.height << ", top_y() "
                << top_y() << ", default height " << defaultRowHeight() << endl;
@@ -463,10 +479,10 @@ void BufferView::Pimpl::scrollDocView(int value)
        int const first = top_y() + height;
        int const last = top_y() + workarea().workHeight() - height;
 
-       LyXText * text = bv_->text;
-       if (text->cursor.y() < first)
+       LyXText * text = bv_->text();
+       if (text->cursorY() < first)
                text->setCursorFromCoordinates(0, first);
-       else if (text->cursor.y() > last)
+       else if (text->cursorY() > last)
                text->setCursorFromCoordinates(0, last);
 
        owner_->updateLayoutChoice();
@@ -478,7 +494,7 @@ void BufferView::Pimpl::scroll(int lines)
        if (!buffer_)
                return;
 
-       LyXText const * t = bv_->text;
+       LyXText const * t = bv_->text();
        int const line_height = defaultRowHeight();
 
        // The new absolute coordinate
@@ -521,22 +537,24 @@ void BufferView::Pimpl::selectionRequested()
        if (!available())
                return;
 
-       LyXText * text = bv_->getLyXText();
+       LCursor & cur = bv_->cursor();
 
-       if (text->selection.set() &&
-               (!bv_->text->xsel_cache.set() ||
-                text->selection.start != bv_->text->xsel_cache.start ||
-                text->selection.end != bv_->text->xsel_cache.end))
-       {
-               bv_->text->xsel_cache = text->selection;
-               sel = text->selectionAsString(*bv_->buffer(), false);
-       } else if (!text->selection.set()) {
-               sel = string();
-               bv_->text->xsel_cache.set(false);
-       }
-       if (!sel.empty()) {
-               workarea().putClipboard(sel);
+       if (!cur.selection()) {
+               xsel_cache_.set = false;
+               return;
        }
+
+       if (!xsel_cache_.set ||
+           cur.cursor_.back() != xsel_cache_.cursor ||
+           cur.anchor_.back() != xsel_cache_.anchor)
+       {
+               xsel_cache_.cursor = cur.cursor_.back();
+               xsel_cache_.anchor = cur.anchor_.back();
+               xsel_cache_.set = cur.selection();
+               sel = bv_->getLyXText()->selectionAsString(*bv_->buffer(), false);
+               if (!sel.empty())
+                       workarea().putClipboard(sel);
+       } 
 }
 
 
@@ -544,8 +562,8 @@ void BufferView::Pimpl::selectionLost()
 {
        if (available()) {
                screen().hideCursor();
-               bv_->getLyXText()->clearSelection();
-               bv_->text->xsel_cache.set(false);
+               bv_->cursor().clearSelection();
+               xsel_cache_.set = false;
        }
 }
 
@@ -591,8 +609,7 @@ void BufferView::Pimpl::update()
                getParsInRange(buffer_->paragraphs(),
                               top_y(), top_y() + workarea().workHeight(),
                               beg, end);
-               bv_->text->redoParagraphs(beg, end);
-               bv_->getLyXText()->redoCursor();
+               bv_->text()->redoParagraphs(beg, end);
                updateScrollbar();
        }
        screen().redraw(*bv_);
@@ -614,7 +631,7 @@ void BufferView::Pimpl::cursorToggle()
 
 bool BufferView::Pimpl::available() const
 {
-       return buffer_ && bv_->text;
+       return buffer_ && bv_->text();
 }
 
 
@@ -624,12 +641,13 @@ Change const BufferView::Pimpl::getCurrentChange()
                return Change(Change::UNCHANGED);
 
        LyXText * text = bv_->getLyXText();
+       LCursor & cur = bv_->cursor();
 
-       if (!text->selection.set())
+       if (!cur.selection())
                return Change(Change::UNCHANGED);
 
-       return text->getPar(text->selection.start)
-               ->lookupChangeFull(text->selection.start.pos());
+       return text->getPar(cur.selStart())
+               ->lookupChangeFull(cur.selStart().pos());
 }
 
 
@@ -638,8 +656,8 @@ void BufferView::Pimpl::savePosition(unsigned int i)
        if (i >= saved_positions_num)
                return;
        saved_positions[i] = Position(buffer_->fileName(),
-                                     bv_->text->cursorPar()->id(),
-                                     bv_->text->cursor.pos());
+                                     bv_->text()->cursorPar()->id(),
+                                     bv_->text()->cursor().pos());
        if (i > 0)
                owner_->message(bformat(_("Saved bookmark %1$s"), tostr(i)));
 }
@@ -652,7 +670,7 @@ void BufferView::Pimpl::restorePosition(unsigned int i)
 
        string const fname = saved_positions[i].filename;
 
-       bv_->text->clearSelection();
+       bv_->cursor().clearSelection();
 
        if (fname != buffer_->fileName()) {
                Buffer * b = 0;
@@ -670,10 +688,9 @@ void BufferView::Pimpl::restorePosition(unsigned int i)
        if (par == buffer_->par_iterator_end())
                return;
 
-       bv_->text->setCursor(par.pit(),
+       bv_->text()->setCursor(par.pit(),
                             min(par->size(), saved_positions[i].par_pos));
 
-       update();
        if (i > 0)
                owner_->message(bformat(_("Moved to bookmark %1$s"), tostr(i)));
 }
@@ -703,11 +720,11 @@ void BufferView::Pimpl::switchKeyMap()
 
 void BufferView::Pimpl::center()
 {
-       LyXText * text = bv_->text;
+       LyXText * text = bv_->text();
 
-       text->clearSelection();
+       bv_->cursor().clearSelection();
        int const half_height = workarea().workHeight() / 2;
-       int new_y = std::max(0, text->cursor.y() - half_height);
+       int new_y = std::max(0, text->cursorY() - half_height);
 
        // FIXME: look at this comment again ...
        // This updates top_y() but means the fitCursor() call
@@ -719,7 +736,6 @@ void BufferView::Pimpl::center()
        // updateScrollbar() currently. Never mind that this is a
        // pretty obfuscated way of updating t->top_y()
        top_y(new_y);
-       update();
 }
 
 
@@ -732,10 +748,10 @@ void BufferView::Pimpl::stuffClipboard(string const & stuff) const
 InsetOld * BufferView::Pimpl::getInsetByCode(InsetOld::Code code)
 {
 #if 0
-       LyXCursor cursor = bv_->getLyXText()->cursor;
+       CursorSlice cursor = bv_->getLyXText()->cursor;
        Buffer::inset_iterator it =
                find_if(Buffer::inset_iterator(
-                       cursorPar(), cursor.pos()),
+                       cursorPar(), cursor().pos()),
                        buffer_->inset_iterator_end(),
                        lyx::compare_memfun(&Inset::lyxCode, code));
        return it != buffer_->inset_iterator_end() ? (*it) : 0;
@@ -744,20 +760,20 @@ InsetOld * BufferView::Pimpl::getInsetByCode(InsetOld::Code code)
        // should work for now. Better infrastructure is coming. (Lgb)
 
        Buffer * b = bv_->buffer();
-       LyXText * text =  bv_->getLyXText();
+       LyXText * text = bv_->getLyXText();
 
        Buffer::inset_iterator beg = b->inset_iterator_begin();
        Buffer::inset_iterator end = b->inset_iterator_end();
 
-       bool cursorPar_seen = false;
+       bool cursor_par_seen = false;
 
        for (; beg != end; ++beg) {
                if (beg.getPar() == text->cursorPar()) {
-                       cursorPar_seen = true;
+                       cursor_par_seen = true;
                }
-               if (cursorPar_seen) {
+               if (cursor_par_seen) {
                        if (beg.getPar() == text->cursorPar()
-                           && beg.getPos() >= text->cursor.pos()) {
+                           && beg.getPos() >= text->cursor().pos()) {
                                break;
                        } else if (beg.getPar() != text->cursorPar()) {
                                break;
@@ -802,7 +818,8 @@ void BufferView::Pimpl::MenuInsertLyXFile(string const & filen)
 
                FileDialog::Result result =
                        fileDlg.open(initpath,
-                                      _("*.lyx| LyX Documents (*.lyx)"));
+                                    FileFilterList(_("LyX Documents (*.lyx)")),
+                                    string());
 
                if (result.first == FileDialog::Later)
                        return;
@@ -846,10 +863,8 @@ void BufferView::Pimpl::trackChanges()
                buf->undostack().clear();
        } else {
                update();
-               bv_->text->setCursor(0, 0);
+               bv_->text()->setCursor(0, 0);
 #warning changes FIXME
-               //moveCursorUpdate(false);
-
                bool found = lyx::find::findNextChange(bv_);
                if (found) {
                        owner_->getDialogs().show("changes");
@@ -866,27 +881,36 @@ void BufferView::Pimpl::trackChanges()
 }
 
 #warning remove me
-LCursor theTempCursor(0);
+CursorBase theTempCursor;
 
 namespace {
 
        InsetOld * insetFromCoords(BufferView * bv, int x, int y)
        {
-               LyXText * text = bv->text;
+               lyxerr << "insetFromCoords" << endl;
+               LyXText * text = bv->text();
                InsetOld * inset = 0;
-               theTempCursor = LCursor(bv);
+               theTempCursor.clear();
                while (true) {
-                       InsetOld * inset_hit = text->checkInsetHit(x, y);
-                       if (!inset_hit)
+                       InsetOld * const inset_hit = text->checkInsetHit(x, y);
+                       if (!inset_hit) {
+                               lyxerr << "no further inset hit" << endl;
                                break;
+                       }
                        inset = inset_hit;
-                       if (!inset_hit->descendable())
+                       if (!inset->descendable()) {
+                               lyxerr << "not descendable" << endl;
+                               break;
+                       }
+                       int const cell = inset->getCell(x, y + bv->top_y());
+                       if (cell == -1)
                                break;
-                       text = inset_hit->getText(0);
+                       text = inset_hit->getText(cell);
                        lyxerr << "Hit inset: " << inset << " at x: " << x
                                << " text: " << text << " y: " << y << endl;
-                       theTempCursor.push(static_cast<UpdatableInset*>(inset));
+                       theTempCursor.push_back(CursorSlice(inset));
                }
+               //lyxerr << "theTempCursor: " << theTempCursor << endl;
                return inset;
        }
 
@@ -895,9 +919,32 @@ namespace {
 
 bool BufferView::Pimpl::workAreaDispatch(FuncRequest const & cmd)
 {
+       LCursor & cur = bv_->cursor();
        switch (cmd.action) {
+       case LFUN_MOUSE_MOTION: {
+               if (!available())
+                       return false;
+               FuncRequest cmd1 = cmd;
+               InsetBase * inset = cur.inset();
+               DispatchResult res;
+               if (inset) {
+                       cmd1.x -= inset->x();
+                       cmd1.y -= inset->y();
+                       res = inset->dispatch(cur, cmd1);
+               } else {
+                       cmd1.y += bv_->top_y();
+                       res = cur.innerText()->dispatch(cur, cmd1);
+               }
+
+               if (bv_->fitCursor() || res.update()) {
+                       bv_->update();
+                       cur.updatePos();
+               }
+
+               return true;
+       }
+
        case LFUN_MOUSE_PRESS:
-       case LFUN_MOUSE_MOTION:
        case LFUN_MOUSE_RELEASE:
        case LFUN_MOUSE_DOUBLE:
        case LFUN_MOUSE_TRIPLE: {
@@ -917,31 +964,31 @@ bool BufferView::Pimpl::workAreaDispatch(FuncRequest const & cmd)
                // handle this event.
 
                // built temporary path to inset
-               InsetOld * inset = insetFromCoords(bv_, cmd.x, cmd.y); 
-               FuncRequest cmd1(cmd, bv_);
+               InsetOld * inset = insetFromCoords(bv_, cmd.x, cmd.y);
                DispatchResult res;
 
                // try to dispatch to that inset
                if (inset) {
-                       FuncRequest cmd2 = cmd1;
-                       lyxerr << "dispatching action " << cmd2.action 
-                               << " to inset " << inset << endl;
+                       FuncRequest cmd2 = cmd;
+                       lyxerr << "dispatching action " << cmd2.action
+                              << " to inset " << inset << endl;
                        cmd2.x -= inset->x();
                        cmd2.y -= inset->y();
-                       res = inset->dispatch(cmd2);
+                       res = inset->dispatch(cur, cmd2);
                        if (res.update()) {
                                bv_->update();
-                               bv_->cursor().updatePos();
+                               cur.updatePos();
                        }
                        res.update(false);
                        switch (res.val()) {
                                case FINISHED:
                                case FINISHED_RIGHT:
-                               case FINISHED_UP: 
+                               case FINISHED_UP:
                                case FINISHED_DOWN:
-                                       theTempCursor.pop();
-                                       bv_->cursor() = theTempCursor;
-                                       bv_->cursor().innerText()->setCursorFromCoordinates(cmd.x, top_y() + cmd.y);
+                                       theTempCursor.pop_back();
+                                       cur.cursor_ = theTempCursor;
+                                       cur.innerText()
+                                               ->setCursorFromCoordinates(cmd.x, top_y() + cmd.y);
                                        if (bv_->fitCursor())
                                                bv_->update();
                                        return true;
@@ -953,15 +1000,17 @@ bool BufferView::Pimpl::workAreaDispatch(FuncRequest const & cmd)
 
                // otherwise set cursor to surrounding LyXText
                if (!res.dispatched()) {
-                       lyxerr << "temp cursor is: " << theTempCursor << endl;
-                       lyxerr << "dispatching " << cmd1
-                              << " to surrounding LyXText "
-                              << theTempCursor.innerText() << endl;
-                       bv_->cursor() = theTempCursor;
-                       res = bv_->cursor().innerText()->dispatch(cmd1);
+                       //lyxerr << "temp cursor is: " << theTempCursor << endl;
+                       //lyxerr << "dispatching " << cmd
+                        //      << " to surrounding LyXText "
+                         //     << theTempCursor.innerText() << endl;
+                       cur.cursor_ = theTempCursor;
+                       FuncRequest cmd1 = cmd;
+                       cmd1.y += bv_->top_y();
+                       res = cur.innerText()->dispatch(cur, cmd1);
                        if (bv_->fitCursor() || res.update())
                                bv_->update();
-                       
+
                        //return DispatchResult(true, true);
                }
                // see workAreaKeyPress
@@ -988,12 +1037,9 @@ bool BufferView::Pimpl::workAreaDispatch(FuncRequest const & cmd)
 }
 
 
-bool BufferView::Pimpl::dispatch(FuncRequest const & ev_in)
+bool BufferView::Pimpl::dispatch(FuncRequest const & ev)
 {
        // Make sure that the cached BufferView is correct.
-       FuncRequest ev = ev_in;
-       ev.setView(bv_);
-
        lyxerr[Debug::ACTION] << "BufferView::Pimpl::Dispatch:"
                << " action[" << ev.action << ']'
                << " arg[" << ev.argument << ']'
@@ -1003,6 +1049,7 @@ bool BufferView::Pimpl::dispatch(FuncRequest const & ev_in)
                << endl;
 
        LyXTextClass const & tclass = buffer_->params().getLyXTextClass();
+       LCursor & cur = bv_->cursor();
 
        switch (ev.action) {
 
@@ -1099,27 +1146,13 @@ bool BufferView::Pimpl::dispatch(FuncRequest const & ev_in)
        case LFUN_MATH_IMPORT_SELECTION: // Imports LaTeX from the X selection
        case LFUN_MATH_DISPLAY:          // Open or create a displayed math inset
        case LFUN_MATH_MODE:             // Open or create an inlined math inset
-               mathDispatch(ev);
+               mathDispatch(cur, ev);
                break;
 
-       case LFUN_INSET_APPLY: {
-               string const name = ev.getArg(0);
-
-               InsetBase * inset = owner_->getDialogs().getOpenInset(name);
-               if (inset) {
-                       // This works both for 'original' and 'mathed' insets.
-                       // Note that the localDispatch performs update also.
-                       FuncRequest fr(bv_, LFUN_INSET_MODIFY, ev.argument);
-                       inset->dispatch(fr);
-               } else {
-                       FuncRequest fr(bv_, LFUN_INSET_INSERT, ev.argument);
-                       dispatch(fr);
-               }
-       }
-       break;
-
        case LFUN_INSET_INSERT: {
-               InsetOld * inset = createInset(ev);
+               // Same as above.
+               BOOST_ASSERT(false);
+               InsetOld * inset = createInset(bv_, ev);
                if (!inset || !insertInset(inset))
                        delete inset;
                break;
@@ -1181,25 +1214,19 @@ bool BufferView::Pimpl::dispatch(FuncRequest const & ev_in)
                break;
 
        case LFUN_ACCEPT_ALL_CHANGES: {
-               bv_->text->setCursor(0, 0);
+               bv_->text()->setCursor(0, 0);
 #warning FIXME changes
-               //moveCursorUpdate(false);
-
                while (lyx::find::findNextChange(bv_))
                        bv_->getLyXText()->acceptChange();
-
                update();
                break;
        }
 
        case LFUN_REJECT_ALL_CHANGES: {
-               bv_->text->setCursor(0, 0);
+               bv_->text()->setCursor(0, 0);
 #warning FIXME changes
-               //moveCursorUpdate(false);
-
                while (lyx::find::findNextChange(bv_))
                        bv_->getLyXText()->rejectChange();
-
                update();
                break;
        }
@@ -1216,12 +1243,47 @@ bool BufferView::Pimpl::dispatch(FuncRequest const & ev_in)
                break;
        }
 
+       case LFUN_WORD_FIND:
+               lyx::find::find(bv_, ev);
+               break;
+
+       case LFUN_WORD_REPLACE:
+               lyx::find::replace(bv_, ev);
+               break;
+
+       case LFUN_MARK_OFF:
+               cur.clearSelection();
+               bv_->update();
+               cur.resetAnchor();
+               ev.message(N_("Mark off"));
+               break;
+
+       case LFUN_MARK_ON:
+               cur.clearSelection();
+               cur.mark() = true;
+               bv_->update();
+               cur.resetAnchor();
+               ev.message(N_("Mark on"));
+               break;
+
+       case LFUN_SETMARK:
+               cur.clearSelection();
+               if (cur.mark()) {
+                       ev.message(N_("Mark removed"));
+               } else {
+                       cur.mark() = true;
+                       ev.message(N_("Mark set"));
+               }
+               cur.resetAnchor();
+               bv_->update();
+               break;
+
        case LFUN_UNKNOWN_ACTION:
                ev.errorMessage(N_("Unknown function!"));
                break;
 
        default:
-               return bv_->getLyXText()->dispatch(FuncRequest(ev, bv_)).dispatched();
+               return cur.dispatch(ev).dispatched();
        } // end of switch
 
        return true;
@@ -1230,45 +1292,27 @@ bool BufferView::Pimpl::dispatch(FuncRequest const & ev_in)
 
 bool BufferView::Pimpl::insertInset(InsetOld * inset, string const & lout)
 {
-#ifdef LOCK
-       // if we are in a locking inset we should try to insert the
-       // inset there otherwise this is a illegal function now
-       if (bv_->theLockingInset()) {
-               if (bv_->theLockingInset()->insetAllowed(inset))
-                       return bv_->theLockingInset()->insertInset(bv_, inset);
-               return false;
-       }
-#endif
-
        // not quite sure if we want this...
-       bv_->text->recUndo(bv_->text->cursor.par());
+       bv_->text()->recUndo(bv_->text()->cursor().par());
        freezeUndo();
 
-       bv_->text->clearSelection();
+       bv_->cursor().clearSelection();
        if (!lout.empty()) {
-               bv_->text->breakParagraph(bv_->buffer()->paragraphs());
+               bv_->text()->breakParagraph(bv_->buffer()->paragraphs());
 
-               if (!bv_->text->cursorPar()->empty()) {
-                       bv_->text->cursorLeft(bv_);
-                       bv_->text->breakParagraph(bv_->buffer()->paragraphs());
+               if (!bv_->text()->cursorPar()->empty()) {
+                       bv_->text()->cursorLeft(bv_);
+                       bv_->text()->breakParagraph(bv_->buffer()->paragraphs());
                }
 
                string lres = lout;
                LyXTextClass const & tclass = buffer_->params().getLyXTextClass();
                bool hasLayout = tclass.hasLayout(lres);
 
-               bv_->text->setLayout(hasLayout ? lres : tclass.defaultLayoutName());
-
-               bv_->text->setParagraph(
-                                  VSpace(VSpace::NONE), VSpace(VSpace::NONE),
-                                  Spacing(),
-                                  LYX_ALIGN_LAYOUT,
-                                  string(),
-                                  0);
+               bv_->text()->setLayout(hasLayout ? lres : tclass.defaultLayoutName());
+               bv_->text()->setParagraph(Spacing(), LYX_ALIGN_LAYOUT, string(), 0);
        }
        bv_->cursor().innerText()->insertInset(inset);
-       update();
-
        unFreezeUndo();
        return true;
 }
@@ -1278,10 +1322,7 @@ bool BufferView::Pimpl::ChangeInsets(InsetOld::Code code,
                                     string const & from, string const & to)
 {
        bool need_update = false;
-       LyXCursor cursor = bv_->text->cursor;
-       LyXCursor tmpcursor = cursor;
-       cursor.par(tmpcursor.par());
-       cursor.pos(tmpcursor.pos());
+       CursorSlice cur = bv_->text()->cursor();
 
        ParIterator end = bv_->buffer()->par_iterator_end();
        for (ParIterator it = bv_->buffer()->par_iterator_begin();
@@ -1302,15 +1343,15 @@ bool BufferView::Pimpl::ChangeInsets(InsetOld::Code code,
 
                        // FIXME
 
-                       // The test it.size()==1 was needed to prevent crashes.
-                       // How to set the cursor correctly when it.size()>1 ??
+                       // The test it.size() == 1 was needed to prevent crashes.
+                       // How to set the cursor correctly when it.size() > 1 ??
                        if (it.size() == 1) {
-                               bv_->text->setCursorIntern(bv_->text->parOffset(it.pit()), 0);
-                               bv_->text->redoParagraph(bv_->text->cursorPar());
+                               bv_->text()->setCursorIntern(bv_->text()->parOffset(it.pit()), 0);
+                               bv_->text()->redoParagraph(bv_->text()->cursorPar());
                        }
                }
        }
-       bv_->text->setCursorIntern(cursor.par(), cursor.pos());
+       bv_->text()->setCursorIntern(cur.par(), cur.pos());
        return need_update;
 }
 
@@ -1331,5 +1372,3 @@ void BufferView::Pimpl::updateParagraphDialog()
        data = "update " + tostr(accept) + '\n' + data;
        bv_->owner()->getDialogs().update("paragraph", data);
 }
-
-