File indexing completed on 2024-05-26 05:52:13
0001 /* 0002 * SPDX-FileCopyrightText: 2022 Pablo Rauzy <r .at. uzy .dot. me> 0003 * SPDX-License-Identifier: LGPL-2.0-or-later 0004 */ 0005 0006 #include <QStringList> 0007 0008 #include <KLocalizedString> 0009 0010 #include <KTextEditor/Range> 0011 0012 #include "keyboardmacrosplugincommands.h" 0013 0014 KeyboardMacrosPluginCommands::KeyboardMacrosPluginCommands(KeyboardMacrosPlugin *plugin) 0015 : KTextEditor::Command(QStringList() << QStringLiteral("kmsave") << QStringLiteral("kmload") << QStringLiteral("kmplay") << QStringLiteral("kmwipe"), 0016 plugin) 0017 , m_plugin(plugin) 0018 { 0019 } 0020 0021 bool KeyboardMacrosPluginCommands::exec(KTextEditor::View *view, const QString &cmd, QString &msg, const KTextEditor::Range &) 0022 { 0023 const QStringList &actionAndName = cmd.split(QRegularExpression(QStringLiteral("\\s+"))); 0024 const QString &action = actionAndName[0]; 0025 // kmplay can take either zero or one argument, all other commands require exactly one 0026 if (actionAndName.length() > 2 || (action != QStringLiteral("kmplay") && actionAndName.length() != 2)) { 0027 msg = i18n("Usage: %1 <name>.", action); 0028 return false; 0029 } 0030 if (action == QStringLiteral("kmplay")) { 0031 // set focus on the view otherwise the macro is executed in the command line 0032 view->setFocus(); 0033 if (actionAndName.length() == 1) { 0034 // no argument: play the current macro 0035 m_plugin->play(); 0036 return true; 0037 } else { 0038 // otherwise play the given macro 0039 const QString &name = actionAndName[1]; 0040 if (!m_plugin->play(name)) { 0041 msg = i18n("No keyboard macro named '%1' found.", name); 0042 return false; 0043 } 0044 return true; 0045 } 0046 } 0047 const QString &name = actionAndName[1]; 0048 if (action == QStringLiteral("kmsave")) { 0049 if (!m_plugin->save(name)) { 0050 msg = i18n("Cannot save empty keyboard macro."); 0051 return false; 0052 } 0053 return true; 0054 } else if (action == QStringLiteral("kmload")) { 0055 if (!m_plugin->load(name)) { 0056 msg = i18n("No keyboard macro named '%1' found.", name); 0057 return false; 0058 } 0059 return true; 0060 } else if (action == QStringLiteral("kmwipe")) { 0061 if (!m_plugin->wipe(name)) { 0062 msg = i18n("No keyboard macro named '%1' found.", name); 0063 return false; 0064 } 0065 return true; 0066 } 0067 return false; 0068 } 0069 0070 bool KeyboardMacrosPluginCommands::help(KTextEditor::View *, const QString &cmd, QString &msg) 0071 { 0072 QString macros; 0073 if (!m_plugin->m_namedMacros.empty()) { 0074 QStringList keys; 0075 keys.reserve(m_plugin->m_namedMacros.size()); 0076 for (const auto &[key, _] : m_plugin->m_namedMacros) { 0077 keys.push_back(key); 0078 } 0079 macros = QStringLiteral("<p><b>Named macros:</b> ") + keys.join(QStringLiteral(", ")) + QStringLiteral(".</p>"); 0080 } 0081 if (cmd == QStringLiteral("kmsave")) { 0082 msg = i18n("<qt><p>Usage: <code>kmsave <name></code></p><p>Save current keyboard macro as <code><name></code>.</p>%1</qt>", macros); 0083 return true; 0084 } else if (cmd == QStringLiteral("kmload")) { 0085 msg = i18n("<qt><p>Usage: <code>kmload <name></code></p><p>Load saved keyboard macro <code><name></code> as current macro.</p>%1</qt>", 0086 macros); 0087 return true; 0088 } else if (cmd == QStringLiteral("kmplay")) { 0089 msg = i18n("<qt><p>Usage: <code>kmplay <name></code></p><p>Play saved keyboard macro <code><name></code> without loading it.</p>%1</qt>", 0090 macros); 0091 return true; 0092 } else if (cmd == QStringLiteral("kmwipe")) { 0093 msg = i18n("<qt><p>Usage: <code>kmwipe <name></code></p><p>Wipe saved keyboard macro <code><name></code>.</p>%1</qt>", macros); 0094 return true; 0095 } 0096 return false; 0097 } 0098 0099 #include "moc_keyboardmacrosplugincommands.cpp"