]> git.lyx.org Git - lyx.git/blobdiff - src/LyXFunc.cpp
EmbeddedObjects.lyx, Math.lyx, UserGuide.lyx: Spanish translation updates by Ignacio
[lyx.git] / src / LyXFunc.cpp
index db13dfbd2d478bf62ae6145f4b223790cb1426a5..3a29fecedc14d46997b14a0ebbf9faa1a9e7a945 100644 (file)
@@ -4,15 +4,15 @@
  * Licence details can be found in the file COPYING.
  *
  * \author Alfredo Braunstein
- * \author Lars Gullik Bjønnes
+ * \author Lars Gullik Bjønnes
  * \author Jean-Marc Lasgouttes
  * \author Angus Leeming
  * \author John Levon
- * \author André Pönitz
+ * \author André Pönitz
  * \author Allan Rae
  * \author Dekel Tsur
  * \author Martin Vermeer
- * \author Jürgen Vigna
+ * \author Jürgen Vigna
  *
  * Full author contact details are available in file CREDITS.
  */
@@ -62,9 +62,9 @@
 #include "insets/InsetERT.h"
 #include "insets/InsetExternal.h"
 #include "insets/InsetFloat.h"
-#include "insets/InsetListings.h"
 #include "insets/InsetGraphics.h"
 #include "insets/InsetInclude.h"
+#include "insets/InsetListings.h"
 #include "insets/InsetNote.h"
 #include "insets/InsetSpace.h"
 #include "insets/InsetTabular.h"
@@ -240,24 +240,24 @@ void LyXFunc::handleKeyFunc(FuncCode action)
 void LyXFunc::gotoBookmark(unsigned int idx, bool openFile, bool switchToBuffer)
 {
        LASSERT(lyx_view_, /**/);
-       if (!LyX::ref().session().bookmarks().isValid(idx))
+       if (!theSession().bookmarks().isValid(idx))
                return;
-       BookmarksSection::Bookmark const & bm = LyX::ref().session().bookmarks().bookmark(idx);
+       BookmarksSection::Bookmark const & bm = theSession().bookmarks().bookmark(idx);
        LASSERT(!bm.filename.empty(), /**/);
        string const file = bm.filename.absFilename();
        // if the file is not opened, open it.
-       if (!theBufferList().exists(file)) {
+       if (!theBufferList().exists(bm.filename)) {
                if (openFile)
                        dispatch(FuncRequest(LFUN_FILE_OPEN, file));
                else
                        return;
        }
        // open may fail, so we need to test it again
-       if (!theBufferList().exists(file))
+       if (!theBufferList().exists(bm.filename))
                return;
 
        // if the current buffer is not that one, switch to it.
-       if (lyx_view_->buffer()->absFileName() != file) {
+       if (lyx_view_->buffer()->fileName() != bm.filename) {
                if (!switchToBuffer)
                        return;
                dispatch(FuncRequest(LFUN_BUFFER_SWITCH, file));
@@ -395,7 +395,7 @@ FuncStatus LyXFunc::getStatus(FuncRequest const & cmd) const
 
        switch (cmd.action) {
        case LFUN_UNKNOWN_ACTION:
-#ifndef HAVE_LIBAIKSAURUS
+#if !defined(HAVE_LIBMYTHES) && !defined(HAVE_LIBAIKSAURUS)
        case LFUN_THESAURUS_ENTRY:
 #endif
                flag.unknown(true);
@@ -458,18 +458,20 @@ FuncStatus LyXFunc::getStatus(FuncRequest const & cmd) const
                break;
 
        case LFUN_VC_REGISTER:
-               enable = !buf->lyxvc().inUse();
+               enable = !buf->lyxvc().inUse() && !buf->isUnnamed();
                break;
        case LFUN_VC_CHECK_IN:
-               enable = buf->lyxvc().inUse() && !buf->isReadonly();
+               enable = buf->lyxvc().checkInEnabled();
                break;
        case LFUN_VC_CHECK_OUT:
-               enable = buf->lyxvc().inUse() && buf->isReadonly();
+               enable = buf->lyxvc().checkOutEnabled();
                break;
        case LFUN_VC_REVERT:
-       case LFUN_VC_UNDO_LAST:
                enable = buf->lyxvc().inUse();
                break;
+       case LFUN_VC_UNDO_LAST:
+               enable = buf->lyxvc().undoLastEnabled();
+               break;
        case LFUN_BUFFER_RELOAD:
                enable = !buf->isUnnamed() && buf->fileName().exists()
                        && (!buf->isClean() || buf->isExternallyModified(Buffer::timestamp_method));
@@ -486,7 +488,7 @@ FuncStatus LyXFunc::getStatus(FuncRequest const & cmd) const
        /*
        case LFUN_BUFFER_WRITE:
        case LFUN_BUFFER_WRITE_AS: {
-               Buffer * b = theBufferList().getBuffer(cmd.getArg(0));
+               Buffer * b = theBufferList().getBuffer(FileName(cmd.getArg(0)));
                enable = b && (b->isUnnamed() || !b->isClean());
                break;
        }
@@ -512,12 +514,12 @@ FuncStatus LyXFunc::getStatus(FuncRequest const & cmd) const
 
        case LFUN_BOOKMARK_GOTO: {
                const unsigned int num = convert<unsigned int>(to_utf8(cmd.argument()));
-               enable = LyX::ref().session().bookmarks().isValid(num);
+               enable = theSession().bookmarks().isValid(num);
                break;
        }
 
        case LFUN_BOOKMARK_CLEAR:
-               enable = LyX::ref().session().bookmarks().size() > 0;
+               enable = theSession().bookmarks().size() > 0;
                break;
 
        // this one is difficult to get right. As a half-baked
@@ -531,13 +533,30 @@ FuncStatus LyXFunc::getStatus(FuncRequest const & cmd) const
                break;
        }
 
+       // we want to check if at least one of these is enabled
+       case LFUN_COMMAND_ALTERNATIVES: {
+               // argument contains ';'-terminated commands
+               string arg = to_utf8(cmd.argument());
+               while (!arg.empty()) {
+                       string first;
+                       arg = split(arg, first, ';');
+                       FuncRequest func(lyxaction.lookupFunc(first));
+                       func.origin = cmd.origin;
+                       flag = getStatus(func);
+                       // if this one is enabled, the whole thing is
+                       if (flag.enabled())
+                               break;
+               }
+               break;
+       }
+
        case LFUN_CALL: {
                FuncRequest func;
                string name = to_utf8(cmd.argument());
-               if (LyX::ref().topLevelCmdDef().lock(name, func)) {
+               if (theTopLevelCmdDef().lock(name, func)) {
                        func.origin = cmd.origin;
                        flag = getStatus(func);
-                       LyX::ref().topLevelCmdDef().release(name);
+                       theTopLevelCmdDef().release(name);
                } else {
                        // catch recursion or unknown command definiton
                        // all operations until the recursion or unknown command 
@@ -549,6 +568,7 @@ FuncStatus LyXFunc::getStatus(FuncRequest const & cmd) const
 
        case LFUN_WORD_FIND_FORWARD:
        case LFUN_WORD_FIND_BACKWARD:
+       case LFUN_WORD_FINDADV:
        case LFUN_COMMAND_PREFIX:
        case LFUN_COMMAND_EXECUTE:
        case LFUN_CANCEL:
@@ -564,7 +584,7 @@ FuncStatus LyXFunc::getStatus(FuncRequest const & cmd) const
        case LFUN_HELP_OPEN:
        case LFUN_DROP_LAYOUTS_CHOICE:
        case LFUN_MENU_OPEN:
-       case LFUN_SERVER_GET_NAME:
+       case LFUN_SERVER_GET_FILENAME:
        case LFUN_SERVER_NOTIFY:
        case LFUN_SERVER_GOTO_FILE_ROW:
        case LFUN_DIALOG_HIDE:
@@ -581,9 +601,6 @@ FuncStatus LyXFunc::getStatus(FuncRequest const & cmd) const
        case LFUN_PREFERENCES_SAVE:
        case LFUN_MESSAGE:
        case LFUN_INSET_EDIT:
-       case LFUN_ALL_INSETS_TOGGLE:
-       case LFUN_GRAPHICS_GROUPS_UNIFY:
-       case LFUN_NOTES_MUTATE:
        case LFUN_BUFFER_LANGUAGE:
        case LFUN_TEXTCLASS_APPLY:
        case LFUN_TEXTCLASS_LOAD:
@@ -732,7 +749,10 @@ void LyXFunc::dispatch(FuncRequest const & cmd)
                       << lyxaction.getActionName(action)
                       << " [" << action << "] is disabled at this location");
                setErrorMessage(flag.message());
+               if (lyx_view_)
+                       lyx_view_->restartCursor();
        } else {
+               Buffer * buffer = lyx_view_ ? lyx_view_->buffer() : 0;
                switch (action) {
 
                case LFUN_WORD_FIND_FORWARD:
@@ -767,7 +787,7 @@ void LyXFunc::dispatch(FuncRequest const & cmd)
                        LASSERT(lyx_view_ && lyx_view_->view(), /**/);
                        keyseq.reset();
                        meta_fake_bit = NoModifier;
-                       if (lyx_view_->buffer())
+                       if (buffer)
                                // cancel any selection
                                dispatch(FuncRequest(LFUN_MARK_OFF));
                        setMessage(from_ascii(N_("Cancel")));
@@ -779,24 +799,24 @@ void LyXFunc::dispatch(FuncRequest const & cmd)
                        break;
 
                case LFUN_BUFFER_TOGGLE_READ_ONLY: {
-                       LASSERT(lyx_view_ && lyx_view_->view() && lyx_view_->buffer(), /**/);
-                       Buffer * buf = lyx_view_->buffer();
-                       if (buf->lyxvc().inUse())
-                               buf->lyxvc().toggleReadOnly();
+                       LASSERT(lyx_view_ && lyx_view_->view() && buffer, /**/);
+                       if (buffer->lyxvc().inUse())
+                               buffer->lyxvc().toggleReadOnly();
                        else
-                               buf->setReadonly(!lyx_view_->buffer()->isReadonly());
+                               buffer->setReadonly(!buffer->isReadonly());
                        break;
                }
 
                // --- Menus -----------------------------------------------
                case LFUN_BUFFER_CLOSE:
                        lyx_view_->closeBuffer();
+                       buffer = 0;
                        updateFlags = Update::None;
                        break;
 
                case LFUN_BUFFER_RELOAD: {
-                       LASSERT(lyx_view_ && lyx_view_->buffer(), /**/);
-                       docstring const file = makeDisplayPath(lyx_view_->buffer()->absFileName(), 20);
+                       LASSERT(lyx_view_ && buffer, /**/);
+                       docstring const file = makeDisplayPath(buffer->absFileName(), 20);
                        docstring text = bformat(_("Any changes will be lost. Are you sure "
                                                             "you want to revert to the saved version of the document %1$s?"), file);
                        int const ret = Alert::prompt(_("Revert to saved document?"),
@@ -808,45 +828,45 @@ void LyXFunc::dispatch(FuncRequest const & cmd)
                }
 
                case LFUN_BUFFER_UPDATE:
-                       LASSERT(lyx_view_ && lyx_view_->buffer(), /**/);
-                       lyx_view_->buffer()->doExport(argument, true);
+                       LASSERT(lyx_view_ && buffer, /**/);
+                       buffer->doExport(argument, true);
                        break;
 
                case LFUN_BUFFER_VIEW:
-                       LASSERT(lyx_view_ && lyx_view_->buffer(), /**/);
-                       lyx_view_->buffer()->preview(argument);
+                       LASSERT(lyx_view_ && buffer, /**/);
+                       buffer->preview(argument);
                        break;
 
                case LFUN_MASTER_BUFFER_UPDATE:
-                       LASSERT(lyx_view_ && lyx_view_->buffer() && lyx_view_->buffer()->masterBuffer(), /**/);
-                       lyx_view_->buffer()->masterBuffer()->doExport(argument, true);
+                       LASSERT(lyx_view_ && buffer && buffer->masterBuffer(), /**/);
+                       buffer->masterBuffer()->doExport(argument, true);
                        break;
 
                case LFUN_MASTER_BUFFER_VIEW:
-                       LASSERT(lyx_view_ && lyx_view_->buffer() && lyx_view_->buffer()->masterBuffer(), /**/);
-                       lyx_view_->buffer()->masterBuffer()->preview(argument);
+                       LASSERT(lyx_view_ && buffer && buffer->masterBuffer(), /**/);
+                       buffer->masterBuffer()->preview(argument);
                        break;
 
                case LFUN_BUILD_PROGRAM:
-                       LASSERT(lyx_view_ && lyx_view_->buffer(), /**/);
-                       lyx_view_->buffer()->doExport("program", true);
+                       LASSERT(lyx_view_ && buffer, /**/);
+                       buffer->doExport("program", true);
                        break;
 
                case LFUN_BUFFER_CHKTEX:
-                       LASSERT(lyx_view_ && lyx_view_->buffer(), /**/);
-                       lyx_view_->buffer()->runChktex();
+                       LASSERT(lyx_view_ && buffer, /**/);
+                       buffer->runChktex();
                        break;
 
                case LFUN_BUFFER_EXPORT:
-                       LASSERT(lyx_view_ && lyx_view_->buffer(), /**/);
+                       LASSERT(lyx_view_ && buffer, /**/);
                        if (argument == "custom")
                                dispatch(FuncRequest(LFUN_DIALOG_SHOW, "sendto"));
                        else
-                               lyx_view_->buffer()->doExport(argument, false);
+                               buffer->doExport(argument, false);
                        break;
 
                case LFUN_BUFFER_EXPORT_CUSTOM: {
-                       LASSERT(lyx_view_ && lyx_view_->buffer(), /**/);
+                       LASSERT(lyx_view_ && buffer, /**/);
                        string format_name;
                        string command = split(argument, format_name, ' ');
                        Format const * format = formats.getFormat(format_name);
@@ -857,8 +877,6 @@ void LyXFunc::dispatch(FuncRequest const & cmd)
                                break;
                        }
 
-                       Buffer * buffer = lyx_view_->buffer();
-
                        // The name of the file created by the conversion process
                        string filename;
 
@@ -888,7 +906,7 @@ void LyXFunc::dispatch(FuncRequest const & cmd)
                }
 
                case LFUN_BUFFER_PRINT: {
-                       LASSERT(lyx_view_ && lyx_view_->buffer(), /**/);
+                       LASSERT(lyx_view_ && buffer, /**/);
                        // FIXME: cmd.getArg() might fail if one of the arguments
                        // contains double quotes
                        string target = cmd.getArg(0);
@@ -908,8 +926,6 @@ void LyXFunc::dispatch(FuncRequest const & cmd)
                                break;
                        }
 
-                       Buffer * buffer = lyx_view_->buffer();
-
                        if (!buffer->doExport("dvi", true)) {
                                showPrintError(buffer->absFileName());
                                break;
@@ -970,7 +986,7 @@ void LyXFunc::dispatch(FuncRequest const & cmd)
                        } else {
                                // case 1: print to a file
                                FileName const filename(makeAbsPath(target_name,
-                                                       lyx_view_->buffer()->filePath()));
+                                                       buffer->filePath()));
                                FileName const dvifile(makeAbsPath(dviname, path));
                                if (filename.exists()) {
                                        docstring text = bformat(
@@ -1002,7 +1018,7 @@ void LyXFunc::dispatch(FuncRequest const & cmd)
                */
 
                case LFUN_BUFFER_AUTO_SAVE:
-                       lyx_view_->buffer()->autoSave();
+                       buffer->autoSave();
                        break;
 
                case LFUN_RECONFIGURE:
@@ -1011,10 +1027,11 @@ void LyXFunc::dispatch(FuncRequest const & cmd)
                        break;
 
                case LFUN_HELP_OPEN: {
-                       LASSERT(lyx_view_, /**/);
+                       if (lyx_view_ == 0)
+                               theApp()->dispatch(FuncRequest(LFUN_WINDOW_NEW));
                        string const arg = argument;
                        if (arg.empty()) {
-                               setErrorMessage(from_ascii(N_("Missing argument")));
+                               setErrorMessage(from_utf8(N_("Missing argument")));
                                break;
                        }
                        FileName const fname = i18nLibFileSearch("doc", arg, "lyx");
@@ -1027,7 +1044,7 @@ void LyXFunc::dispatch(FuncRequest const & cmd)
                                makeDisplayPath(fname.absFilename())));
                        Buffer * buf = lyx_view_->loadDocument(fname, false);
                        if (buf) {
-                               updateLabels(*buf);
+                               buf->updateLabels();
                                lyx_view_->setBuffer(buf);
                                buf->errors("Parse");
                        }
@@ -1037,56 +1054,55 @@ void LyXFunc::dispatch(FuncRequest const & cmd)
 
                // --- version control -------------------------------
                case LFUN_VC_REGISTER:
-                       LASSERT(lyx_view_ && lyx_view_->buffer(), /**/);
-                       if (!ensureBufferClean(view()))
+                       LASSERT(lyx_view_ && buffer, /**/);
+                       if (!ensureBufferClean(view()) || buffer->isUnnamed())
                                break;
-                       if (!lyx_view_->buffer()->lyxvc().inUse()) {
-                               lyx_view_->buffer()->lyxvc().registrer();
+                       if (!buffer->lyxvc().inUse()) {
+                               buffer->lyxvc().registrer();
                                reloadBuffer();
                        }
                        updateFlags = Update::Force;
                        break;
 
                case LFUN_VC_CHECK_IN:
-                       LASSERT(lyx_view_ && lyx_view_->buffer(), /**/);
+                       LASSERT(lyx_view_ && buffer, /**/);
                        if (!ensureBufferClean(view()))
                                break;
-                       if (lyx_view_->buffer()->lyxvc().inUse()
-                                       && !lyx_view_->buffer()->isReadonly()) {
-                               lyx_view_->buffer()->lyxvc().checkIn();
+                       if (buffer->lyxvc().inUse()
+                                       && !buffer->isReadonly()) {
+                               setMessage(from_utf8(buffer->lyxvc().checkIn()));
                                reloadBuffer();
                        }
                        break;
 
                case LFUN_VC_CHECK_OUT:
-                       LASSERT(lyx_view_ && lyx_view_->buffer(), /**/);
+                       LASSERT(lyx_view_ && buffer, /**/);
                        if (!ensureBufferClean(view()))
                                break;
-                       if (lyx_view_->buffer()->lyxvc().inUse()
-                                       && lyx_view_->buffer()->isReadonly()) {
-                               lyx_view_->buffer()->lyxvc().checkOut();
+                       if (buffer->lyxvc().inUse()) {
+                               setMessage(from_utf8(buffer->lyxvc().checkOut()));
                                reloadBuffer();
                        }
                        break;
 
                case LFUN_VC_REVERT:
-                       LASSERT(lyx_view_ && lyx_view_->buffer(), /**/);
-                       lyx_view_->buffer()->lyxvc().revert();
+                       LASSERT(lyx_view_ && buffer, /**/);
+                       buffer->lyxvc().revert();
                        reloadBuffer();
                        break;
 
                case LFUN_VC_UNDO_LAST:
-                       LASSERT(lyx_view_ && lyx_view_->buffer(), /**/);
-                       lyx_view_->buffer()->lyxvc().undoLast();
+                       LASSERT(lyx_view_ && buffer, /**/);
+                       buffer->lyxvc().undoLast();
                        reloadBuffer();
                        break;
 
                // --- lyxserver commands ----------------------------
-               case LFUN_SERVER_GET_NAME:
-                       LASSERT(lyx_view_ && lyx_view_->buffer(), /**/);
-                       setMessage(from_utf8(lyx_view_->buffer()->absFileName()));
+               case LFUN_SERVER_GET_FILENAME:
+                       LASSERT(lyx_view_ && buffer, /**/);
+                       setMessage(from_utf8(buffer->absFileName()));
                        LYXERR(Debug::INFO, "FNAME["
-                               << lyx_view_->buffer()->absFileName() << ']');
+                               << buffer->absFileName() << ']');
                        break;
 
                case LFUN_SERVER_NOTIFY:
@@ -1111,8 +1127,8 @@ void LyXFunc::dispatch(FuncRequest const & cmd)
                                // and get full path
                                FileName const s = fileSearch(string(), changeExtension(file_name, ".lyx"), "lyx");
                                // Either change buffer or load the file
-                               if (theBufferList().exists(s.absFilename()))
-                                       buf = theBufferList().getBuffer(s.absFilename());
+                               if (theBufferList().exists(s))
+                                       buf = theBufferList().getBuffer(s);
                                else {
                                        buf = lyx_view_->loadDocument(s);
                                        loaded = true;
@@ -1124,7 +1140,7 @@ void LyXFunc::dispatch(FuncRequest const & cmd)
                                break;
                        }
 
-                       updateLabels(*buf);
+                       buf->updateLabels();
                        lyx_view_->setBuffer(buf);
                        view()->setCursorFromRow(row);
                        if (loaded)
@@ -1185,8 +1201,7 @@ void LyXFunc::dispatch(FuncRequest const & cmd)
                        } 
                        case EXTERNAL_CODE: {
                                InsetExternalParams p;
-                               Buffer const & buffer = *lyx_view_->buffer();
-                               data = InsetExternal::params2string(p, buffer);
+                               data = InsetExternal::params2string(p, *buffer);
                                break;
                        } 
                        case FLOAT_CODE:  {
@@ -1201,8 +1216,7 @@ void LyXFunc::dispatch(FuncRequest const & cmd)
                        } 
                        case GRAPHICS_CODE: {
                                InsetGraphicsParams p;
-                               Buffer const & buffer = *lyx_view_->buffer();
-                               data = InsetGraphics::params2string(p, buffer);
+                               data = InsetGraphics::params2string(p, *buffer);
                                break;
                        } 
                        case NOTE_CODE: {
@@ -1262,14 +1276,13 @@ void LyXFunc::dispatch(FuncRequest const & cmd)
                }
 
                case LFUN_BUFFER_CHILD_OPEN: {
-                       LASSERT(lyx_view_ && lyx_view_->buffer(), /**/);
-                       Buffer * parent = lyx_view_->buffer();
-                       FileName filename = makeAbsPath(argument, parent->filePath());
+                       LASSERT(lyx_view_ && buffer, /**/);
+                       FileName filename = makeAbsPath(argument, buffer->filePath());
                        view()->saveBookmark(false);
                        Buffer * child = 0;
                        bool parsed = false;
-                       if (theBufferList().exists(filename.absFilename())) {
-                               child = theBufferList().getBuffer(filename.absFilename());
+                       if (theBufferList().exists(filename)) {
+                               child = theBufferList().getBuffer(filename);
                        } else {
                                setMessage(bformat(_("Opening child document %1$s..."),
                                        makeDisplayPath(filename.absFilename())));
@@ -1280,8 +1293,8 @@ void LyXFunc::dispatch(FuncRequest const & cmd)
                                // Set the parent name of the child document.
                                // This makes insertion of citations and references in the child work,
                                // when the target is in the parent or another child document.
-                               child->setParent(parent);
-                               updateLabels(*child->masterBuffer());
+                               child->setParent(buffer);
+                               child->masterBuffer()->updateLabels();
                                lyx_view_->setBuffer(child);
                                if (parsed)
                                        child->errors("Parse");
@@ -1327,7 +1340,7 @@ void LyXFunc::dispatch(FuncRequest const & cmd)
                        istringstream is(countstr);
                        int count = 0;
                        is >> count;
-                       lyxerr << "repeat: count: " << count << " cmd: " << rest << endl;
+                       //lyxerr << "repeat: count: " << count << " cmd: " << rest << endl;
                        for (int i = 0; i < count; ++i)
                                dispatch(lyxaction.lookupFunc(rest));
                        break;
@@ -1336,6 +1349,8 @@ void LyXFunc::dispatch(FuncRequest const & cmd)
                case LFUN_COMMAND_SEQUENCE: {
                        // argument contains ';'-terminated commands
                        string arg = argument;
+                       if (theBufferList().isLoaded(buffer))
+                               buffer->undo().beginUndoGroup();
                        while (!arg.empty()) {
                                string first;
                                arg = split(arg, first, ';');
@@ -1343,15 +1358,34 @@ void LyXFunc::dispatch(FuncRequest const & cmd)
                                func.origin = cmd.origin;
                                dispatch(func);
                        }
+                       if (theBufferList().isLoaded(buffer))
+                               buffer->undo().endUndoGroup();
+                       break;
+               }
+
+               case LFUN_COMMAND_ALTERNATIVES: {
+                       // argument contains ';'-terminated commands
+                       string arg = argument;
+                       while (!arg.empty()) {
+                               string first;
+                               arg = split(arg, first, ';');
+                               FuncRequest func(lyxaction.lookupFunc(first));
+                               func.origin = cmd.origin;
+                               FuncStatus stat = getStatus(func);
+                               if (stat.enabled()) {
+                                       dispatch(func);
+                                       break;
+                               }
+                       }
                        break;
                }
 
                case LFUN_CALL: {
                        FuncRequest func;
-                       if (LyX::ref().topLevelCmdDef().lock(argument, func)) {
+                       if (theTopLevelCmdDef().lock(argument, func)) {
                                func.origin = cmd.origin;
                                dispatch(func);
-                               LyX::ref().topLevelCmdDef().release(argument);
+                               theTopLevelCmdDef().release(argument);
                        } else {
                                if (func.action == LFUN_UNKNOWN_ACTION) {
                                        // unknown command definition
@@ -1380,71 +1414,16 @@ void LyXFunc::dispatch(FuncRequest const & cmd)
                        lyx_view_->message(from_utf8(argument));
                        break;
 
-
-               case LFUN_ALL_INSETS_TOGGLE: {
-                       LASSERT(lyx_view_, /**/);
-                       string action;
-                       string const name = split(argument, action, ' ');
-                       InsetCode const inset_code = insetCode(name);
-
-                       Cursor & cur = view()->cursor();
-                       FuncRequest fr(LFUN_INSET_TOGGLE, action);
-
-                       Inset & inset = lyx_view_->buffer()->inset();
-                       InsetIterator it  = inset_iterator_begin(inset);
-                       InsetIterator const end = inset_iterator_end(inset);
-                       for (; it != end; ++it) {
-                               if (!it->asInsetMath()
-                                   && (inset_code == NO_CODE
-                                   || inset_code == it->lyxCode())) {
-                                       Cursor tmpcur = cur;
-                                       tmpcur.pushBackward(*it);
-                                       it->dispatch(tmpcur, fr);
-                               }
-                       }
-                       updateFlags = Update::Force | Update::FitCursor;
-                       break;
-               }
-
-               case LFUN_GRAPHICS_GROUPS_UNIFY: {
-                       LASSERT(lyx_view_, /**/);
-                       if (argument.empty())
-                               break;
-                       //view()->cursor().recordUndoFullDocument(); let inset-apply do that job
-                       graphics::unifyGraphicsGroups(*lyx_view_->buffer(), argument);
-                       lyx_view_->buffer()->markDirty();
-                       updateFlags = Update::Force | Update::FitCursor;
-                       break;
-               }
-
-               // BOTH GRAPHICS_GROUPS_UNIFY and NOTES_MUTATE should be in Buffer dispatch once
-               // view->cursor() is not needed.
-               // Also they could be rewriten using some command like forall <insetname> <command>
-               // once the insets refactoring is done.
-               case LFUN_NOTES_MUTATE: {
-                       LASSERT(lyx_view_ && lyx_view_->view(), /**/);
-                       if (argument.empty())
-                               break;
-                       view()->cursor().recordUndoFullDocument();
-
-                       if (mutateNotes(view(), cmd.getArg(0), cmd.getArg(1))) {
-                               lyx_view_->buffer()->markDirty();
-                               updateFlags = Update::Force | Update::FitCursor;
-                       }
-                       break;
-               }
-
                case LFUN_BUFFER_LANGUAGE: {
                        LASSERT(lyx_view_, /**/);
-                       Buffer & buffer = *lyx_view_->buffer();
-                       Language const * oldL = buffer.params().language;
+                       Language const * oldL = buffer->params().language;
                        Language const * newL = languages.getLanguage(argument);
                        if (!newL || oldL == newL)
                                break;
 
                        if (oldL->rightToLeft() == newL->rightToLeft()
-                           && !buffer.isMultiLingual())
-                               buffer.changeLanguage(oldL, newL);
+                           && !buffer->isMultiLingual())
+                               buffer->changeLanguage(oldL, newL);
                        break;
                }
 
@@ -1477,7 +1456,6 @@ void LyXFunc::dispatch(FuncRequest const & cmd)
                case LFUN_BUFFER_PARAMS_APPLY: {
                        LASSERT(lyx_view_, /**/);
                        
-                       Buffer * buffer = lyx_view_->buffer();
                        DocumentClass const * const oldClass = buffer->params().documentClassPtr();
                        Cursor & cur = view()->cursor();
                        cur.recordUndoFullDocument();
@@ -1506,7 +1484,6 @@ void LyXFunc::dispatch(FuncRequest const & cmd)
                
                case LFUN_LAYOUT_MODULES_CLEAR: {
                        LASSERT(lyx_view_, /**/);
-                       Buffer * buffer = lyx_view_->buffer();
                        DocumentClass const * const oldClass = buffer->params().documentClassPtr();
                        view()->cursor().recordUndoFullDocument();
                        buffer->params().clearLayoutModules();
@@ -1518,8 +1495,14 @@ void LyXFunc::dispatch(FuncRequest const & cmd)
                
                case LFUN_LAYOUT_MODULE_ADD: {
                        LASSERT(lyx_view_, /**/);
-                       Buffer * buffer = lyx_view_->buffer();
-                       DocumentClass const * const oldClass = buffer->params().documentClassPtr();
+                       BufferParams const & params = buffer->params();
+                       if (!params.moduleCanBeAdded(argument)) {
+                               LYXERR0("Module `" << argument << 
+                                               "' cannot be added due to failed requirements or "
+                                               "conflicts with installed modules.");
+                               break;
+                       }
+                       DocumentClass const * const oldClass = params.documentClassPtr();
                        view()->cursor().recordUndoFullDocument();
                        buffer->params().addLayoutModule(argument);
                        buffer->params().makeDocumentClass();
@@ -1530,7 +1513,6 @@ void LyXFunc::dispatch(FuncRequest const & cmd)
 
                case LFUN_TEXTCLASS_APPLY: {
                        LASSERT(lyx_view_, /**/);
-                       Buffer * buffer = lyx_view_->buffer();
 
                        if (!loadLayoutFile(argument, buffer->temppath()) &&
                                !loadLayoutFile(argument, buffer->filePath()))
@@ -1555,7 +1537,6 @@ void LyXFunc::dispatch(FuncRequest const & cmd)
                
                case LFUN_LAYOUT_RELOAD: {
                        LASSERT(lyx_view_, /**/);
-                       Buffer * buffer = lyx_view_->buffer();
                        DocumentClass const * const oldClass = buffer->params().documentClassPtr();
                        LayoutFileIndex bc = buffer->params().baseClassID();
                        LayoutFileList::get().reset(bc);
@@ -1567,8 +1548,8 @@ void LyXFunc::dispatch(FuncRequest const & cmd)
                }
 
                case LFUN_TEXTCLASS_LOAD:
-                       loadLayoutFile(argument, lyx_view_->buffer()->temppath()) ||
-                       loadLayoutFile(argument, lyx_view_->buffer()->filePath());
+                       loadLayoutFile(argument, buffer->temppath()) ||
+                       loadLayoutFile(argument, buffer->filePath());
                        break;
 
                case LFUN_LYXRC_APPLY: {
@@ -1603,7 +1584,7 @@ void LyXFunc::dispatch(FuncRequest const & cmd)
                        break;
 
                case LFUN_BOOKMARK_CLEAR:
-                       LyX::ref().session().bookmarks().clear();
+                       theSession().bookmarks().clear();
                        break;
 
                default:
@@ -1617,18 +1598,29 @@ void LyXFunc::dispatch(FuncRequest const & cmd)
                        if (lyx_view_ == 0)
                                break;
 
+                       // Start an undo group. This may be needed for
+                       // some stuff like inset-apply on labels.
+                       if (theBufferList().isLoaded(buffer))
+                               buffer->undo().beginUndoGroup();
+                               
                        // Let the current LyXView dispatch its own actions.
                        if (lyx_view_->dispatch(cmd)) {
-                               if (lyx_view_->view())
+                               if (lyx_view_->view()) {
                                        updateFlags = lyx_view_->view()->cursor().result().update();
+                                       if (theBufferList().isLoaded(buffer))
+                                               buffer->undo().endUndoGroup();
+                               }
                                break;
                        }
 
                        LASSERT(lyx_view_->view(), /**/);
+
                        // Let the current BufferView dispatch its own actions.
                        if (view()->dispatch(cmd)) {
                                // The BufferView took care of its own updates if needed.
                                updateFlags = Update::None;
+                               if (theBufferList().isLoaded(buffer))
+                                       buffer->undo().endUndoGroup();
                                break;
                        }
 
@@ -1641,11 +1633,14 @@ void LyXFunc::dispatch(FuncRequest const & cmd)
                        // notify insets we just left
                        if (view()->cursor() != old) {
                                old.fixIfBroken();
-                               bool badcursor = notifyCursorLeaves(old, view()->cursor());
+                               bool badcursor = notifyCursorLeavesOrEnters(old, view()->cursor());
                                if (badcursor)
                                        view()->cursor().fixIfBroken();
                        }
 
+                       if (theBufferList().isLoaded(buffer))
+                               buffer->undo().endUndoGroup();
+
                        // update completion. We do it here and not in
                        // processKeySym to avoid another redraw just for a
                        // changed inline completion
@@ -1661,6 +1656,12 @@ void LyXFunc::dispatch(FuncRequest const & cmd)
                        updateFlags = view()->cursor().result().update();
                }
 
+               // if we executed a mutating lfun, mark the buffer as dirty
+               if (theBufferList().isLoaded(buffer) && flag.enabled()
+                   && !lyxaction.funcHasFlag(action, LyXAction::NoBuffer)
+                   && !lyxaction.funcHasFlag(action, LyXAction::ReadOnly))
+                       buffer->markDirty();                    
+
                if (lyx_view_ && lyx_view_->buffer()) {
                        // BufferView::update() updates the ViewMetricsInfo and
                        // also initializes the position cache for all insets in
@@ -1668,12 +1669,6 @@ void LyXFunc::dispatch(FuncRequest const & cmd)
                        // We will redraw the screen only if needed.
                        view()->processUpdateFlags(updateFlags);
 
-                       // if we executed a mutating lfun, mark the buffer as dirty
-                       if (flag.enabled()
-                           && !lyxaction.funcHasFlag(action, LyXAction::NoBuffer)
-                           && !lyxaction.funcHasFlag(action, LyXAction::ReadOnly))
-                               lyx_view_->buffer()->markDirty();                       
-
                        // Do we have a selection?
                        theSelection().haveSelection(view()->cursor().selection());
                        
@@ -1716,7 +1711,7 @@ void LyXFunc::sendDispatchMessage(docstring const & msg, FuncRequest const & cmd
                }
        }
 
-       docstring const shortcuts = theTopLevelKeymap().printBindings(cmd);
+       docstring const shortcuts = theTopLevelKeymap().printBindings(cmd, KeySequence::ForGui);
 
        if (!shortcuts.empty())
                comname += ": " + shortcuts;
@@ -1740,11 +1735,14 @@ void LyXFunc::reloadBuffer()
        // The user has already confirmed that the changes, if any, should
        // be discarded. So we just release the Buffer and don't call closeBuffer();
        theBufferList().release(lyx_view_->buffer());
+       // if the lyx_view_ has been destroyed, create a new one
+       if (!lyx_view_)
+               theApp()->dispatch(FuncRequest(LFUN_WINDOW_NEW));
        Buffer * buf = lyx_view_->loadDocument(filename);
        docstring const disp_fn = makeDisplayPath(filename.absFilename());
        docstring str;
        if (buf) {
-               updateLabels(*buf);
+               buf->updateLabels();
                lyx_view_->setBuffer(buf);
                buf->errors("Parse");
                str = bformat(_("Document %1$s reloaded."), disp_fn);
@@ -1805,20 +1803,20 @@ bool LyXFunc::wasMetaKey() const
 }
 
 
-void LyXFunc::updateLayout(DocumentClass const * const oldlayout, Buffer * buffer)
+void LyXFunc::updateLayout(DocumentClass const * const oldlayout, Buffer * buf)
 {
        lyx_view_->message(_("Converting document to new document class..."));
        
        StableDocIterator backcur(view()->cursor());
-       ErrorList & el = buffer->errorList("Class Switch");
+       ErrorList & el = buf->errorList("Class Switch");
        cap::switchBetweenClasses(
-                       oldlayout, buffer->params().documentClassPtr(),
-                       static_cast<InsetText &>(buffer->inset()), el);
+                       oldlayout, buf->params().documentClassPtr(),
+                       static_cast<InsetText &>(buf->inset()), el);
 
-       view()->setCursor(backcur.asDocIterator(&(buffer->inset())));
+       view()->setCursor(backcur.asDocIterator(buf));
 
-       buffer->errors("Class Switch");
-       updateLabels(*buffer);
+       buf->errors("Class Switch");
+       buf->updateLabels();
 }
 
 
@@ -1835,8 +1833,8 @@ void actOnUpdatedPrefs(LyXRC const & lyxrc_orig, LyXRC const & lyxrc_new)
        switch (tag) {
        case LyXRC::RC_ACCEPT_COMPOUND:
        case LyXRC::RC_ALT_LANG:
-       case LyXRC::RC_PLAINTEXT_ROFF_COMMAND:
        case LyXRC::RC_PLAINTEXT_LINELEN:
+       case LyXRC::RC_PLAINTEXT_ROFF_COMMAND:
        case LyXRC::RC_AUTOREGIONDELETE:
        case LyXRC::RC_AUTORESET_OPTIONS:
        case LyXRC::RC_AUTOSAVE:
@@ -1865,6 +1863,7 @@ void actOnUpdatedPrefs(LyXRC const & lyxrc_orig, LyXRC const & lyxrc_new)
        case LyXRC::RC_CUSTOM_EXPORT_FORMAT:
        case LyXRC::RC_DATE_INSERT_FORMAT:
        case LyXRC::RC_DEFAULT_LANGUAGE:
+       case LyXRC::RC_GUI_LANGUAGE:
        case LyXRC::RC_DEFAULT_PAPERSIZE:
        case LyXRC::RC_DEFFILE:
        case LyXRC::RC_DIALOGS_ICONIFY_WITH_MAIN:
@@ -1881,6 +1880,7 @@ void actOnUpdatedPrefs(LyXRC const & lyxrc_orig, LyXRC const & lyxrc_new)
        case LyXRC::RC_FORMAT:
        case LyXRC::RC_GROUP_LAYOUTS:
        case LyXRC::RC_INDEX_COMMAND:
+       case LyXRC::RC_NOMENCL_COMMAND:
        case LyXRC::RC_INPUT:
        case LyXRC::RC_KBMAP:
        case LyXRC::RC_KBMAP_PRIMARY:
@@ -1894,6 +1894,7 @@ void actOnUpdatedPrefs(LyXRC const & lyxrc_orig, LyXRC const & lyxrc_new)
        case LyXRC::RC_LANGUAGE_GLOBAL_OPTIONS:
        case LyXRC::RC_LANGUAGE_PACKAGE:
        case LyXRC::RC_LANGUAGE_USE_BABEL:
+       case LyXRC::RC_MAC_LIKE_WORD_MOVEMENT:
        case LyXRC::RC_MACRO_EDIT_STYLE:
        case LyXRC::RC_MAKE_BACKUP:
        case LyXRC::RC_MARK_FOREIGN_LANGUAGE:
@@ -1949,6 +1950,7 @@ void actOnUpdatedPrefs(LyXRC const & lyxrc_orig, LyXRC const & lyxrc_new)
                if (lyxrc_orig.windows_style_tex_paths != lyxrc_new.windows_style_tex_paths) {
                        os::windows_style_tex_paths(lyxrc_new.windows_style_tex_paths);
                }
+       case LyXRC::RC_THESAURUSDIRPATH:
        case LyXRC::RC_UIFILE:
        case LyXRC::RC_USER_EMAIL:
        case LyXRC::RC_USER_NAME:
@@ -1976,6 +1978,4 @@ void actOnUpdatedPrefs(LyXRC const & lyxrc_orig, LyXRC const & lyxrc_new)
 }
 
 } // namespace anon
-
-
 } // namespace lyx