Browse Source

support shortcuts for external programs

1. External editor format: name=program %0,<shortcut>
2. Add "OpenViaDefaultProgram" shortcut to call system's default program
to open current note. "F12" by default.
Le Tan 8 years ago
parent
commit
cb8a1df337

+ 8 - 5
src/resources/docs/tips_external_program_en.md

@@ -6,13 +6,16 @@ A sample configuration may look like this:
 ```ini
 [external_editors]
 ; Define external editors which could be called to edit notes
-; One program per line with the format name="program %0 arg1 arg2"
-; in which %0 will be replaced with the note file path
+; One program per line with the format name="program \"%0\" arg1 arg2",<shortcut>
+; in which %0 will be replaced with the note file path (so it is better to enclose it
+; with double quotes)
+; Shortcut could be empty
 ; Need to escape \ and ", use double quotes to quote paths/arguments with spaces
+; SHOULD defined in user config file, not here
 
-GVim=C:\\\"Program Files (x86)\"\\Vim\\vim80\\gvim.exe %0
-Notepad=notepad %0
-Notepad%2B%2B=C:\\\"Program Files (x86)\"\\notepad++\\notepad++.exe %0
+GVim=C:\\\"Program Files (x86)\"\\Vim\\vim80\\gvim.exe \"%0\", F4
+Notepad=notepad \"%0\"
+Notepad%2B%2B=C:\\\"Program Files (x86)\"\\notepad++\\notepad++.exe \"%0\"
 ```
 
 A restart is needed to detect new external programs.

+ 8 - 5
src/resources/docs/tips_external_program_zh.md

@@ -6,13 +6,16 @@ VNote支持使用外部程序来打开笔记。VNote将外部程序的配置信
 ```ini
 [external_editors]
 ; Define external editors which could be called to edit notes
-; One program per line with the format name="program %0 arg1 arg2"
-; in which %0 will be replaced with the note file path
+; One program per line with the format name="program \"%0\" arg1 arg2",<shortcut>
+; in which %0 will be replaced with the note file path (so it is better to enclose it
+; with double quotes)
+; Shortcut could be empty
 ; Need to escape \ and ", use double quotes to quote paths/arguments with spaces
+; SHOULD defined in user config file, not here
 
-GVim=C:\\\"Program Files (x86)\"\\Vim\\vim80\\gvim.exe %0
-Notepad=notepad %0
-Notepad%2B%2B=C:\\\"Program Files (x86)\"\\notepad++\\notepad++.exe %0
+GVim=C:\\\"Program Files (x86)\"\\Vim\\vim80\\gvim.exe \"%0\", F4
+Notepad=notepad \"%0\"
+Notepad%2B%2B=C:\\\"Program Files (x86)\"\\notepad++\\notepad++.exe \"%0\"
 ```
 
 VNote需要重启以检测新的外部程序。

+ 6 - 2
src/resources/vnote.ini

@@ -222,6 +222,8 @@ ActivateNextTab=Ctrl+Tab
 ActivatePreviousTab=Ctrl+Shift+Tab
 ; Activate flash page
 FlashPage=Ctrl+Alt+L
+; Open via system's default program
+OpenViaDefaultProgram=F12
 
 [captain_mode_shortcuts]
 ; Define shortcuts in Captain mode here.
@@ -285,7 +287,9 @@ ApplySnippet=S
 
 [external_editors]
 ; Define external editors which could be called to edit notes
-; One program per line with the format name="program %0 arg1 arg2"
-; in which %0 will be replaced with the note file path
+; One program per line with the format name="program \"%0\" arg1 arg2",<shortcut>
+; in which %0 will be replaced with the note file path (so it is better to enclose it
+; with double quotes)
+; Shortcut could be empty
 ; Need to escape \ and ", use double quotes to quote paths/arguments with spaces
 ; SHOULD defined in user config file, not here

+ 20 - 3
src/vconfigmanager.cpp

@@ -1145,9 +1145,9 @@ QVector<VMagicWord> VConfigManager::getCustomMagicWords()
     return words;
 }
 
-QVector<QPair<QString, QString>> VConfigManager::getExternalEditors() const
+QVector<VExternalEditor> VConfigManager::getExternalEditors() const
 {
-    QVector<QPair<QString, QString>> ret;
+    QVector<VExternalEditor> ret;
     userSettings->beginGroup("external_editors");
     QStringList keys = userSettings->childKeys();
     for (auto const & key : keys) {
@@ -1155,7 +1155,24 @@ QVector<QPair<QString, QString>> VConfigManager::getExternalEditors() const
             continue;
         }
 
-        ret.push_back(QPair<QString, QString>(key, userSettings->value(key).toString()));
+        QStringList val = userSettings->value(key).toStringList();
+        if (val.size() > 2
+            || val.isEmpty()) {
+            continue;
+        }
+
+        VExternalEditor editor;
+        editor.m_name = key;
+        editor.m_cmd = val[0].trimmed();
+        if (editor.m_cmd.isEmpty()) {
+            continue;
+        }
+
+        if (val.size() == 2) {
+            editor.m_shortcut = val[1].trimmed();
+        }
+
+        ret.push_back(editor);
     }
 
     userSettings->endGroup();

+ 14 - 1
src/vconfigmanager.h

@@ -18,6 +18,7 @@
 class QJsonObject;
 class QString;
 
+
 enum MarkdownConverterType
 {
     Hoedown = 0,
@@ -26,12 +27,24 @@ enum MarkdownConverterType
     Showdown
 };
 
+
 struct VColor
 {
     QString m_name;
     QString m_color; // #RGB or color name.
 };
 
+
+struct VExternalEditor
+{
+    QString m_name;
+
+    QString m_cmd;
+
+    QString m_shortcut;
+};
+
+
 struct MarkdownitOption
 {
     MarkdownitOption(bool p_html, bool p_breaks, bool p_linkify)
@@ -390,7 +403,7 @@ public:
     bool getEnableBackupFile() const;
 
     // Get defined external editors.
-    QVector<QPair<QString, QString>> getExternalEditors() const;
+    QVector<VExternalEditor> getExternalEditors() const;
 
     const QString &getVimExemptionKeys() const;
 

+ 38 - 5
src/vfilelist.cpp

@@ -975,18 +975,51 @@ void VFileList::initOpenWithMenu()
 
     auto programs = g_config->getExternalEditors();
     for (auto const & pa : programs) {
-        QAction *act = new QAction(pa.first, this);
-        act->setToolTip(tr("Open current note with %1").arg(pa.first));
-        act->setStatusTip(pa.second);
-        act->setData(pa.second);
+        QKeySequence seq = QKeySequence(pa.m_shortcut);
+        QString name = pa.m_name;
+        if (!seq.isEmpty()) {
+            name = QString("%1\t%2").arg(pa.m_name)
+                                    .arg(VUtils::getShortcutText(pa.m_shortcut));
+        }
+
+        QAction *act = new QAction(name, this);
+        act->setToolTip(tr("Open current note with %1").arg(pa.m_name));
+        act->setStatusTip(pa.m_cmd);
+        act->setData(pa.m_cmd);
+
+        if (!seq.isEmpty()) {
+            QShortcut *shortcut = new QShortcut(seq, this);
+            shortcut->setContext(Qt::WidgetWithChildrenShortcut);
+            connect(shortcut, &QShortcut::activated,
+                    this, [act](){
+                        act->trigger();
+                    });
+        }
+
         connect(act, &QAction::triggered,
                 this, &VFileList::handleOpenWithActionTriggered);
 
         m_openWithMenu->addAction(act);
     }
 
-    QAction *defaultAct = new QAction(tr("System's Default Program"), this);
+    QKeySequence seq(g_config->getShortcutKeySequence("OpenViaDefaultProgram"));
+    QString name = tr("System's Default Program");
+    if (!seq.isEmpty()) {
+        name = QString("%1\t%2").arg(name)
+                                .arg(VUtils::getShortcutText(g_config->getShortcutKeySequence("OpenViaDefaultProgram")));
+    }
+
+    QAction *defaultAct = new QAction(name, this);
     defaultAct->setToolTip(tr("Open current note with system's default program"));
+    if (!seq.isEmpty()) {
+        QShortcut *shortcut = new QShortcut(seq, this);
+        shortcut->setContext(Qt::WidgetWithChildrenShortcut);
+        connect(shortcut, &QShortcut::activated,
+                this, [defaultAct](){
+                    defaultAct->trigger();
+                });
+    }
+
     connect(defaultAct, &QAction::triggered,
             this, [this]() {
                 QListWidgetItem *item = fileList->currentItem();