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.
This commit is contained in:
Le Tan 2017-12-25 19:44:12 +08:00
parent 7b8c522a53
commit cb8a1df337
6 changed files with 94 additions and 21 deletions

View File

@ -6,13 +6,16 @@ A sample configuration may look like this:
```ini ```ini
[external_editors] [external_editors]
; Define external editors which could be called to edit notes ; Define external editors which could be called to edit notes
; One program per line with the format name="program %0 arg1 arg2" ; One program per line with the format name="program \"%0\" arg1 arg2",<shortcut>
; in which %0 will be replaced with the note file path ; 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 ; 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 GVim=C:\\\"Program Files (x86)\"\\Vim\\vim80\\gvim.exe \"%0\", F4
Notepad=notepad %0 Notepad=notepad \"%0\"
Notepad%2B%2B=C:\\\"Program Files (x86)\"\\notepad++\\notepad++.exe %0 Notepad%2B%2B=C:\\\"Program Files (x86)\"\\notepad++\\notepad++.exe \"%0\"
``` ```
A restart is needed to detect new external programs. A restart is needed to detect new external programs.

View File

@ -6,13 +6,16 @@ VNote支持使用外部程序来打开笔记。VNote将外部程序的配置信
```ini ```ini
[external_editors] [external_editors]
; Define external editors which could be called to edit notes ; Define external editors which could be called to edit notes
; One program per line with the format name="program %0 arg1 arg2" ; One program per line with the format name="program \"%0\" arg1 arg2",<shortcut>
; in which %0 will be replaced with the note file path ; 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 ; 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 GVim=C:\\\"Program Files (x86)\"\\Vim\\vim80\\gvim.exe \"%0\", F4
Notepad=notepad %0 Notepad=notepad \"%0\"
Notepad%2B%2B=C:\\\"Program Files (x86)\"\\notepad++\\notepad++.exe %0 Notepad%2B%2B=C:\\\"Program Files (x86)\"\\notepad++\\notepad++.exe \"%0\"
``` ```
VNote需要重启以检测新的外部程序。 VNote需要重启以检测新的外部程序。

View File

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

View File

@ -1145,9 +1145,9 @@ QVector<VMagicWord> VConfigManager::getCustomMagicWords()
return words; 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"); userSettings->beginGroup("external_editors");
QStringList keys = userSettings->childKeys(); QStringList keys = userSettings->childKeys();
for (auto const & key : keys) { for (auto const & key : keys) {
@ -1155,7 +1155,24 @@ QVector<QPair<QString, QString>> VConfigManager::getExternalEditors() const
continue; 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(); userSettings->endGroup();

View File

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

View File

@ -975,18 +975,51 @@ void VFileList::initOpenWithMenu()
auto programs = g_config->getExternalEditors(); auto programs = g_config->getExternalEditors();
for (auto const & pa : programs) { for (auto const & pa : programs) {
QAction *act = new QAction(pa.first, this); QKeySequence seq = QKeySequence(pa.m_shortcut);
act->setToolTip(tr("Open current note with %1").arg(pa.first)); QString name = pa.m_name;
act->setStatusTip(pa.second); if (!seq.isEmpty()) {
act->setData(pa.second); 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, connect(act, &QAction::triggered,
this, &VFileList::handleOpenWithActionTriggered); this, &VFileList::handleOpenWithActionTriggered);
m_openWithMenu->addAction(act); 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")); 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, connect(defaultAct, &QAction::triggered,
this, [this]() { this, [this]() {
QListWidgetItem *item = fileList->currentItem(); QListWidgetItem *item = fileList->currentItem();