]> git.lyx.org Git - lyx.git/blobdiff - src/LyXFunc.cpp
Paragraph:
[lyx.git] / src / LyXFunc.cpp
index c0c3eb0b15fcfa4fff5fa082bb97011dd5ddba77..5fb648e05142531e7dd13e86c372a02e6e3c7f01 100644 (file)
@@ -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));
@@ -389,7 +389,7 @@ FuncStatus LyXFunc::getStatus(FuncRequest const & cmd) const
 
        if (cmd.action == LFUN_NOACTION) {
                flag.message(from_utf8(N_("Nothing to do")));
-               flag.enabled(false);
+               flag.setEnabled(false);
                return flag;
        }
 
@@ -399,7 +399,7 @@ FuncStatus LyXFunc::getStatus(FuncRequest const & cmd) const
        case LFUN_THESAURUS_ENTRY:
 #endif
                flag.unknown(true);
-               flag.enabled(false);
+               flag.setEnabled(false);
                break;
 
        default:
@@ -422,7 +422,7 @@ FuncStatus LyXFunc::getStatus(FuncRequest const & cmd) const
                // no, exit directly
                flag.message(from_utf8(N_("Command not allowed with"
                                    "out any document open")));
-               flag.enabled(false);
+               flag.setEnabled(false);
                return flag;
        }
 
@@ -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 
@@ -564,7 +583,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,8 +600,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_BUFFER_LANGUAGE:
        case LFUN_TEXTCLASS_APPLY:
        case LFUN_TEXTCLASS_LOAD:
@@ -624,14 +641,14 @@ FuncStatus LyXFunc::getStatus(FuncRequest const & cmd) const
        }
 
        if (!enable)
-               flag.enabled(false);
+               flag.setEnabled(false);
 
        // Can we use a readonly buffer?
        if (buf && buf->isReadonly()
            && !lyxaction.funcHasFlag(cmd.action, LyXAction::ReadOnly)
            && !lyxaction.funcHasFlag(cmd.action, LyXAction::NoBuffer)) {
                flag.message(from_utf8(N_("Document is read-only")));
-               flag.enabled(false);
+               flag.setEnabled(false);
        }
 
        // Are we in a DELETED change-tracking region?
@@ -640,7 +657,7 @@ FuncStatus LyXFunc::getStatus(FuncRequest const & cmd) const
            && !lyxaction.funcHasFlag(cmd.action, LyXAction::ReadOnly)
            && !lyxaction.funcHasFlag(cmd.action, LyXAction::NoBuffer)) {
                flag.message(from_utf8(N_("This portion of the document is deleted.")));
-               flag.enabled(false);
+               flag.setEnabled(false);
        }
 
        // the default error message if we disable the command
@@ -731,6 +748,8 @@ 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 {
                switch (action) {
 
@@ -1037,7 +1056,7 @@ void LyXFunc::dispatch(FuncRequest const & cmd)
                // --- version control -------------------------------
                case LFUN_VC_REGISTER:
                        LASSERT(lyx_view_ && lyx_view_->buffer(), /**/);
-                       if (!ensureBufferClean(view()))
+                       if (!ensureBufferClean(view()) || lyx_view_->buffer()->isUnnamed())
                                break;
                        if (!lyx_view_->buffer()->lyxvc().inUse()) {
                                lyx_view_->buffer()->lyxvc().registrer();
@@ -1052,7 +1071,7 @@ void LyXFunc::dispatch(FuncRequest const & cmd)
                                break;
                        if (lyx_view_->buffer()->lyxvc().inUse()
                                        && !lyx_view_->buffer()->isReadonly()) {
-                               lyx_view_->buffer()->lyxvc().checkIn();
+                               setMessage(from_utf8(lyx_view_->buffer()->lyxvc().checkIn()));
                                reloadBuffer();
                        }
                        break;
@@ -1061,9 +1080,8 @@ void LyXFunc::dispatch(FuncRequest const & cmd)
                        LASSERT(lyx_view_ && lyx_view_->buffer(), /**/);
                        if (!ensureBufferClean(view()))
                                break;
-                       if (lyx_view_->buffer()->lyxvc().inUse()
-                                       && lyx_view_->buffer()->isReadonly()) {
-                               lyx_view_->buffer()->lyxvc().checkOut();
+                       if (lyx_view_->buffer()->lyxvc().inUse()) {
+                               setMessage(from_utf8(lyx_view_->buffer()->lyxvc().checkOut()));
                                reloadBuffer();
                        }
                        break;
@@ -1081,7 +1099,7 @@ void LyXFunc::dispatch(FuncRequest const & cmd)
                        break;
 
                // --- lyxserver commands ----------------------------
-               case LFUN_SERVER_GET_NAME:
+               case LFUN_SERVER_GET_FILENAME:
                        LASSERT(lyx_view_ && lyx_view_->buffer(), /**/);
                        setMessage(from_utf8(lyx_view_->buffer()->absFileName()));
                        LYXERR(Debug::INFO, "FNAME["
@@ -1110,8 +1128,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;
@@ -1267,8 +1285,8 @@ void LyXFunc::dispatch(FuncRequest const & cmd)
                        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())));
@@ -1345,12 +1363,29 @@ void LyXFunc::dispatch(FuncRequest const & cmd)
                        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
@@ -1379,43 +1414,6 @@ 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() || !lyx_view_->buffer())
-                               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;
-               }
-
                case LFUN_BUFFER_LANGUAGE: {
                        LASSERT(lyx_view_, /**/);
                        Buffer & buffer = *lyx_view_->buffer();
@@ -1585,7 +1583,7 @@ void LyXFunc::dispatch(FuncRequest const & cmd)
                        break;
 
                case LFUN_BOOKMARK_CLEAR:
-                       LyX::ref().session().bookmarks().clear();
+                       theSession().bookmarks().clear();
                        break;
 
                default:
@@ -1599,18 +1597,27 @@ 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 (lyx_view_->view())
+                               view()->cursor().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();
+                                       view()->cursor().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;
+                               view()->cursor().endUndoGroup();
                                break;
                        }
 
@@ -1628,6 +1635,10 @@ void LyXFunc::dispatch(FuncRequest const & cmd)
                                        view()->cursor().fixIfBroken();
                        }
 
+                       // we assume here that the buffer view has not
+                       // changed since the beginUndoGroup.
+                       view()->cursor().endUndoGroup();
+
                        // update completion. We do it here and not in
                        // processKeySym to avoid another redraw just for a
                        // changed inline completion
@@ -1847,6 +1858,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:
@@ -1863,6 +1875,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:
@@ -1876,6 +1889,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: