]> git.lyx.org Git - features.git/blobdiff - src/insets/InsetInfo.cpp
InsetInfo: Move validateArgument() to params
[features.git] / src / insets / InsetInfo.cpp
index dac80c4a09add557a750a360376ef0003303ee80..54b8cf090e84ad031567c142f7d5c705a4e4a439 100644 (file)
@@ -75,6 +75,7 @@ NameTranslator const initTranslator()
        translator.addPair(InsetInfoParams::PACKAGE_INFO, "package");
        translator.addPair(InsetInfoParams::TEXTCLASS_INFO, "textclass");
        translator.addPair(InsetInfoParams::MENU_INFO, "menu");
+       translator.addPair(InsetInfoParams::L7N_INFO, "l7n");
        translator.addPair(InsetInfoParams::ICON_INFO, "icon");
        translator.addPair(InsetInfoParams::BUFFER_INFO, "buffer");
        translator.addPair(InsetInfoParams::LYX_INFO, "lyxinfo");
@@ -109,6 +110,7 @@ DefaultValueTranslator const initDVTranslator()
        translator.addPair(InsetInfoParams::PACKAGE_INFO, "graphics");
        translator.addPair(InsetInfoParams::TEXTCLASS_INFO, "article");
        translator.addPair(InsetInfoParams::MENU_INFO, "info-insert");
+       translator.addPair(InsetInfoParams::L7N_INFO, "");
        translator.addPair(InsetInfoParams::ICON_INFO, "info-insert");
        translator.addPair(InsetInfoParams::BUFFER_INFO, "name-noext");
        translator.addPair(InsetInfoParams::LYX_INFO, "version");
@@ -231,6 +233,10 @@ vector<pair<string,docstring>> InsetInfoParams::getArguments(Buffer const * buf,
                break;
        }
 
+       case L7N_INFO:
+               result.push_back(make_pair("custom", _("Custom")));
+               break;
+
        case LYXRC_INFO: {
                result.push_back(make_pair("custom", _("Custom")));
                set<string> rcs = lyxrc.getRCs();
@@ -266,7 +272,7 @@ vector<pair<string,docstring>> InsetInfoParams::getArguments(Buffer const * buf,
                result.push_back(make_pair("tree-revision", _("Tree revision")));
                result.push_back(make_pair("author", _("Author")));
                result.push_back(make_pair("date", _("Date")));
-               result.push_back(make_pair("time", _("Time")));
+               result.push_back(make_pair("time", _("Time[[of day]]")));
                break;
        }
 
@@ -324,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);
@@ -381,6 +486,10 @@ docstring InsetInfo::toolTip(BufferView const &, int, int) const
                result = bformat(_("The menu location for the function '%1$s'"),
                                from_utf8(params_.name));
                break;
+       case InsetInfoParams::L7N_INFO: 
+               result = bformat(_("The localization for the string '%1$s'"),
+                               from_utf8(params_.name));
+               break;
        case InsetInfoParams::ICON_INFO:
                result = bformat(_("The toolbar icon for the function '%1$s'"),
                                from_utf8(params_.name));
@@ -478,97 +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::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");
@@ -585,11 +603,12 @@ bool InsetInfo::getStatus(Cursor & cur, FuncRequest const & cmd,
 
        case LFUN_INSET_DIALOG_UPDATE:
        case LFUN_INSET_COPY_AS:
+       case LFUN_INSET_DISSOLVE:
                flag.setEnabled(true);
                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, ' '));
@@ -747,10 +766,84 @@ void InsetInfo::updateBuffer(ParIterator const & it, UpdateType utype) {
                        info(from_ascii("undefined"), params_.lang);
                        break;
                }
+               docstring sequence;
                if (params_.type == InsetInfoParams::SHORTCUT_INFO)
-                       setText(bindings.begin()->print(KeySequence::ForGui), guilang);
+                       sequence = bindings.begin()->print(KeySequence::ForGui);
                else
-                       setText(theTopLevelKeymap().printBindings(func, KeySequence::ForGui), guilang);
+                       sequence = theTopLevelKeymap().printBindings(func, KeySequence::ForGui);
+               // QKeySequence returns special characters for keys on the mac
+               // Since these are not included in many fonts, we
+               // re-translate them to textual names (see #10641)
+               odocstringstream ods;
+               string const lcode = params_.lang->code();
+               for (size_t n = 0; n < sequence.size(); ++n) {
+                       char_type const c = sequence[n];
+                       switch(c) {
+                       case 0x21b5://Return
+                               gui = _("Return[[Key]]");
+                               ods << translateIfPossible(from_ascii("Return[[Key]]"), lcode);
+                               break;
+                       case 0x21b9://Tab both directions (Win)
+                               gui = _("Tab[[Key]]");
+                               ods << translateIfPossible(from_ascii("Tab[[Key]]"), lcode);
+                               break;
+                       case 0x21de://Qt::Key_PageUp
+                               gui = _("PgUp");
+                               ods << translateIfPossible(from_ascii("PgUp"), lcode);
+                               break;
+                       case 0x21df://Qt::Key_PageDown
+                               gui = _("PgDown");
+                               ods << translateIfPossible(from_ascii("PgDown"), lcode);
+                               break;
+                       case 0x21e4://Qt::Key_Backtab
+                               gui = _("Backtab");
+                               ods << translateIfPossible(from_ascii("Backtab"), lcode);
+                               break;
+                       case 0x21e5://Qt::Key_Tab
+                               gui = _("Tab");
+                               ods << translateIfPossible(from_ascii("Tab"), lcode);
+                               break;
+                       case 0x21e7://Shift
+                               gui = _("Shift");
+                               ods << translateIfPossible(from_ascii("Shift"), lcode);
+                               break;
+                       case 0x21ea://Qt::Key_CapsLock
+                               gui = _("CapsLock");
+                               ods << translateIfPossible(from_ascii("CapsLock"), lcode);
+                               break;
+                       case 0x2303://Control
+                               gui = _("Control[[Key]]");
+                               ods << translateIfPossible(from_ascii("Control[[Key]]"), lcode);
+                               break;
+                       case 0x2318://CMD
+                               gui = _("Command[[Key]]");
+                               ods << translateIfPossible(from_ascii("Command[[Key]]"), lcode);
+                               break;
+                       case 0x2324://Qt::Key_Enter
+                               gui = _("Return[[Key]]");
+                               ods << translateIfPossible(from_ascii("Return[[Key]]"), lcode);
+                               break;
+                       case 0x2325://Option key
+                               gui = _("Option[[Key]]");
+                               ods << translateIfPossible(from_ascii("Option[[Key]]"), lcode);
+                               break;
+                       case 0x2326://Qt::Key_Delete
+                               gui = _("Delete[[Key]]");
+                               ods << translateIfPossible(from_ascii("Delete[[Key]]"), lcode);
+                               break;
+                       case 0x232b://Qt::Key_Backspace
+                               gui = _("Fn+Del");
+                               ods << translateIfPossible(from_ascii("Fn+Delete"), lcode);
+                               break;
+                       case 0x238b://Qt::Key_Escape
+                               gui = _("Esc");
+                               ods << translateIfPossible(from_ascii("Esc"), lcode);
+                               break;
+                       default:
+                               ods.put(c);
+                       }
+               }
+               setText(ods.str(), guilang);
                params_.force_ltr = !guilang->rightToLeft() && !params_.lang->rightToLeft();
                break;
        }
@@ -786,6 +879,9 @@ void InsetInfo::updateBuffer(ParIterator const & it, UpdateType utype) {
                // remove \n and ""
                result = rtrim(result, "\n");
                result = trim(result, "\"");
+               gui = _("not set");
+               if (result.empty())
+                       result = "not set";
                setText(from_utf8(result), params_.lang);
                break;
        }
@@ -824,21 +920,27 @@ void InsetInfo::updateBuffer(ParIterator const & it, UpdateType utype) {
                // only need to do this once.
                if (initialized_)
                        break;
-               // and we will not keep trying if we fail
-               initialized_ = true;
                docstring_list names;
-               FuncRequest const func = lyxaction.lookupFunc(params_.name);
+               FuncRequest func = lyxaction.lookupFunc(params_.name);
                if (func.action() == LFUN_UNKNOWN_ACTION) {
                        gui = _("Unknown action %1$s");
                        error(from_ascii("Unknown action %1$s"), params_.lang);
                        break;
                }
+               if (func.action() == LFUN_BUFFER_VIEW || func.action() == LFUN_BUFFER_UPDATE)
+                       // The default output format is in the menu without argument,
+                       // so strip it here.
+                       if (func.argument() == from_ascii(buffer().params().getDefaultOutputFormat()))
+                               func = FuncRequest(func.action());
                // iterate through the menubackend to find it
                if (!theApp()) {
                        gui = _("Can't determine menu entry for action %1$s in batch mode");
                        error(from_ascii("Can't determine menu entry for action %1$s in batch mode"), params_.lang);
+                       initialized_ = true;
                        break;
                }
+               // and we will not keep trying if we fail
+               initialized_ = theApp()->hasBufferView();
                if (!theApp()->searchMenu(func, names)) {
                        gui = _("No menu entry for action %1$s");
                        error(from_ascii("No menu entry for action %1$s"), params_.lang);
@@ -866,6 +968,24 @@ void InsetInfo::updateBuffer(ParIterator const & it, UpdateType utype) {
                }
                break;
        }
+       case InsetInfoParams::L7N_INFO: {
+               docstring locstring = _(params_.name);
+               // Remove trailing colons
+               locstring = rtrim(locstring, ":");
+               // Remove menu accelerators
+               if (contains(locstring, from_ascii("|"))) {
+                       docstring nlocstring;
+                       rsplit(locstring, nlocstring, '|');
+                       locstring = nlocstring;
+               }
+               // Remove Qt accelerators, but keep literal ampersands
+               locstring = subst(locstring, from_ascii(" & "), from_ascii("</amp;>"));
+               locstring = subst(locstring, from_ascii("&"), docstring());
+               locstring = subst(locstring, from_ascii("</amp;>"), from_ascii(" & "));
+               setText(locstring, guilang);
+               params_.force_ltr = !guilang->rightToLeft() && !params_.lang->rightToLeft();
+               break;
+       }
        case InsetInfoParams::ICON_INFO: {
                // only need to do this once.
                if (initialized_)