]> git.lyx.org Git - lyx.git/commitdiff
InsetInfo: Move validateArgument() to params
authorJuergen Spitzmueller <spitz@lyx.org>
Sat, 18 Aug 2018 12:29:15 +0000 (14:29 +0200)
committerJuergen Spitzmueller <spitz@lyx.org>
Sat, 18 Aug 2018 12:29:15 +0000 (14:29 +0200)
and use it for LFUN_INFO_INSERT validation

src/Text3.cpp
src/insets/InsetInfo.cpp
src/insets/InsetInfo.h

index 1b0788ddbb9eabe88cb4d108a341369282e5650a..7d1142c13929f9f7b70f2c70183d7340decea7bb 100644 (file)
@@ -60,6 +60,7 @@
 #include "insets/InsetFloatList.h"
 #include "insets/InsetGraphics.h"
 #include "insets/InsetGraphicsParams.h"
+#include "insets/InsetInfo.h"
 #include "insets/InsetIPAMacro.h"
 #include "insets/InsetNewline.h"
 #include "insets/InsetQuotes.h"
@@ -2949,6 +2950,7 @@ bool Text::getStatus(Cursor & cur, FuncRequest const & cmd,
                break;
        case LFUN_INFO_INSERT:
                code = INFO_CODE;
+               enable = infoparams.validateArgument(cur.buffer(), cmd.argument(), true);
                break;
        case LFUN_ARGUMENT_INSERT: {
                code = ARG_CODE;
index 4d3901596679706d410ff855d4d6c29f0b162453..54b8cf090e84ad031567c142f7d5c705a4e4a439 100644 (file)
@@ -330,6 +330,105 @@ vector<pair<string,docstring>> InsetInfoParams::getArguments(Buffer const * buf,
 }
 
 
+bool InsetInfoParams::validateArgument(Buffer const * buf, docstring const & arg,
+                                      bool const usedefaults) const
+{
+       string type;
+       string name = trim(split(to_utf8(arg), type, ' '));
+       if (name.empty() && usedefaults)
+               name = defaultValueTranslator().find(type);
+
+       switch (nameTranslator().find(type)) {
+       case UNKNOWN_INFO:
+               return false;
+
+       case SHORTCUT_INFO:
+       case SHORTCUTS_INFO:
+       case MENU_INFO: {
+               FuncRequest func = lyxaction.lookupFunc(name);
+               return func.action() != LFUN_UNKNOWN_ACTION;
+       }
+
+       case L7N_INFO:
+               return !name.empty();
+
+       case ICON_INFO: {
+               FuncCode const action = lyxaction.lookupFunc(name).action();
+               if (action == LFUN_UNKNOWN_ACTION) {
+                       string dir = "images";
+                       return !imageLibFileSearch(dir, name, "svgz,png").empty();
+               }
+               return true;
+       }
+
+       case LYXRC_INFO: {
+               set<string> rcs = lyxrc.getRCs();
+               return rcs.find(name) != rcs.end();
+       }
+
+       case PACKAGE_INFO:
+       case TEXTCLASS_INFO:
+               return true;
+
+       case BUFFER_INFO:
+               return (name == "name" || name == "name-noext"
+                       || name == "path" || name == "class");
+
+       case VCS_INFO:
+               if (name == "revision" || name == "tree-revision"
+                   || name == "author" || name == "date" || name == "time")
+                       return buf->lyxvc().inUse();
+               return false;
+
+       case LYX_INFO:
+               return name == "version";
+
+       case FIXDATE_INFO: {
+               string date;
+               string piece;
+               date = split(name, piece, '@');
+               if (!date.empty() && !QDate::fromString(toqstr(date), Qt::ISODate).isValid())
+                       return false;
+               if (!piece.empty())
+                       name = piece;
+       }
+       // fall through
+       case DATE_INFO:
+       case MODDATE_INFO: {
+               if (name == "long" || name == "short" || name == "ISO")
+                       return true;
+               else {
+                       QDate date = QDate::currentDate();
+                       return !date.toString(toqstr(name)).isEmpty();
+               }
+       }
+       case FIXTIME_INFO: {
+               string time;
+               string piece;
+               time = split(name, piece, '@');
+               if (!time.empty() && !QTime::fromString(toqstr(time), Qt::ISODate).isValid())
+                       return false;
+               if (!piece.empty())
+                       name = piece;
+       }
+       // fall through
+       case TIME_INFO:
+       case MODTIME_INFO: {
+               if (name == "long" || name == "short" || name == "ISO")
+                       return true;
+               else {
+                       QTime time = QTime::currentTime();
+                       return !time.toString(toqstr(name)).isEmpty();
+               }
+       }
+       }
+
+       return false;
+}
+
+
+
+
 string InsetInfoParams::infoType() const
 {
        return nameTranslator().find(type);
@@ -488,100 +587,6 @@ void InsetInfo::write(ostream & os) const
 }
 
 
-bool InsetInfo::validateModifyArgument(docstring const & arg) const
-{
-       string type;
-       string name = trim(split(to_utf8(arg), type, ' '));
-
-       switch (nameTranslator().find(type)) {
-       case InsetInfoParams::UNKNOWN_INFO:
-               return false;
-
-       case InsetInfoParams::SHORTCUT_INFO:
-       case InsetInfoParams::SHORTCUTS_INFO:
-       case InsetInfoParams::MENU_INFO: {
-               FuncRequest func = lyxaction.lookupFunc(name);
-               return func.action() != LFUN_UNKNOWN_ACTION;
-       }
-
-       case InsetInfoParams::L7N_INFO:
-               return !name.empty();
-
-       case InsetInfoParams::ICON_INFO: {
-               FuncCode const action = lyxaction.lookupFunc(name).action();
-               if (action == LFUN_UNKNOWN_ACTION) {
-                       string dir = "images";
-                       return !imageLibFileSearch(dir, name, "svgz,png").empty();
-               }
-               return true;
-       }
-
-       case InsetInfoParams::LYXRC_INFO: {
-               set<string> rcs = lyxrc.getRCs();
-               return rcs.find(name) != rcs.end();
-       }
-
-       case InsetInfoParams::PACKAGE_INFO:
-       case InsetInfoParams::TEXTCLASS_INFO:
-               return true;
-
-       case InsetInfoParams::BUFFER_INFO:
-               return (name == "name" || name == "name-noext"
-                       || name == "path" || name == "class");
-
-       case InsetInfoParams::VCS_INFO:
-               if (name == "revision" || name == "tree-revision"
-                   || name == "author" || name == "date" || name == "time")
-                       return buffer().lyxvc().inUse();
-               return false;
-
-       case InsetInfoParams::LYX_INFO:
-               return name == "version";
-
-       case InsetInfoParams::FIXDATE_INFO: {
-               string date;
-               string piece;
-               date = split(name, piece, '@');
-               if (!date.empty() && !QDate::fromString(toqstr(date), Qt::ISODate).isValid())
-                       return false;
-               if (!piece.empty())
-                       name = piece;
-       }
-       // fall through
-       case InsetInfoParams::DATE_INFO:
-       case InsetInfoParams::MODDATE_INFO: {
-               if (name == "long" || name == "short" || name == "ISO")
-                       return true;
-               else {
-                       QDate date = QDate::currentDate();
-                       return !date.toString(toqstr(name)).isEmpty();
-               }
-       }
-       case InsetInfoParams::FIXTIME_INFO: {
-               string time;
-               string piece;
-               time = split(name, piece, '@');
-               if (!time.empty() && !QTime::fromString(toqstr(time), Qt::ISODate).isValid())
-                       return false;
-               if (!piece.empty())
-                       name = piece;
-       }
-       // fall through
-       case InsetInfoParams::TIME_INFO:
-       case InsetInfoParams::MODTIME_INFO: {
-               if (name == "long" || name == "short" || name == "ISO")
-                       return true;
-               else {
-                       QTime time = QTime::currentTime();
-                       return !time.toString(toqstr(name)).isEmpty();
-               }
-       }
-       }
-
-       return false;
-}
-
-
 bool InsetInfo::showInsetDialog(BufferView * bv) const
 {
        bv->showDialog("info");
@@ -603,7 +608,7 @@ bool InsetInfo::getStatus(Cursor & cur, FuncRequest const & cmd,
                return true;
 
        case LFUN_INSET_MODIFY:
-               if (validateModifyArgument(cmd.argument())) {
+               if (params_.validateArgument(&buffer(), cmd.argument())) {
                        flag.setEnabled(true);
                        string typestr;
                        string name = trim(split(to_utf8(cmd.argument()), typestr, ' '));
index ada1f4cfaeca439f994afc56b7b008f7f74d811b..811153afb048a08d2071d0edd566349d2a0c2e75 100644 (file)
@@ -146,6 +146,9 @@ public:
        std::vector<std::pair<std::string,docstring>> getArguments(Buffer const * buf,
                                                                   std::string const &) const;
        ///
+       bool validateArgument(Buffer const * buf, docstring const & argument,
+                             bool const usedefault = false) const;
+       ///
        info_type type;
        ///
        std::string infoType() const;
@@ -184,7 +187,8 @@ public:
        ///
        void write(std::ostream & os) const;
        ///
-       bool validateModifyArgument(docstring const & argument) const;
+       bool validateModifyArgument(docstring const & argument) const {
+               return params_.validateArgument(&buffer(), argument); }
        ///
        bool showInsetDialog(BufferView * bv) const;
        ///