8#include <vimode/keyparser.h>
10#include <KConfigGroup>
12using namespace KateVi;
14Macros::Macros() =
default;
16Macros::~Macros() =
default;
20 const auto macroKeys = m_macros.
keys();
22 for (
const QChar macroRegister : macroKeys) {
23 macroRegisters.
append(macroRegister);
26 for (
const QChar macroRegister : macroKeys) {
27 macroContents.
append(KeyParser::self()->decodeKeySequence(m_macros[macroRegister]));
30 for (
const QChar macroRegister : macroKeys) {
32 for (
const Completion &completionForMacro : m_completions[macroRegister]) {
33 macroCompletions.
append(encodeMacroCompletionForConfig(completionForMacro));
36 config.
writeEntry(
"Macro Registers", macroRegisters);
37 config.
writeEntry(
"Macro Contents", macroContents);
38 config.
writeEntry(
"Macro Completions", macroCompletions);
46 int macroCompletionsIndex = 0;
48 for (
int macroIndex = 0; macroIndex < macroRegisters.
length(); macroIndex++) {
49 const QChar macroRegister = macroRegisters[macroIndex].
at(0);
50 m_macros[macroRegister] = KeyParser::self()->encodeKeySequence(macroContents[macroIndex]);
51 macroCompletionsIndex = readMacroCompletions(macroRegister, macroCompletions, macroCompletionsIndex);
61void Macros::remove(
const QChar ®)
68 m_macros[reg].
clear();
72 for (
const KeyEvent &keyEvent : std::as_const(withoutClosingQ)) {
73 const QChar key = KeyParser::self()->KeyEventToQChar(keyEvent);
74 m_macros[reg].append(key);
76 m_completions[reg] = completions;
89int Macros::readMacroCompletions(
const QChar ®,
const QStringList &encodedMacroCompletions,
int macroCompletionsIndex)
91 if (macroCompletionsIndex < encodedMacroCompletions.
length()) {
92 bool parsedNumCompletionsSuccessfully =
false;
93 const QString numCompletionsAsString = encodedMacroCompletions[macroCompletionsIndex++];
94 const int numCompletions = numCompletionsAsString.
toInt(&parsedNumCompletionsSuccessfully);
96 m_completions[reg].
clear();
97 while (count < numCompletions && macroCompletionsIndex < encodedMacroCompletions.
length()) {
98 const QString encodedMacroCompletion = encodedMacroCompletions[macroCompletionsIndex++];
100 m_completions[reg].append(decodeMacroCompletionFromConfig(encodedMacroCompletion));
103 return macroCompletionsIndex;
106QString Macros::encodeMacroCompletionForConfig(
const Completion &completionForMacro)
108 const bool endedWithSemiColon = completionForMacro.completedText().
endsWith(
QLatin1Char(
';'));
110 if (completionForMacro.completionType() == Completion::FunctionWithArgs) {
112 }
else if (completionForMacro.completionType() == Completion::FunctionWithoutArgs) {
115 if (endedWithSemiColon) {
118 if (completionForMacro.removeTail()) {
121 return encodedMacroCompletion;
124Completion Macros::decodeMacroCompletionFromConfig(
const QString &encodedMacroCompletion)
127 Completion::CompletionType completionType = Completion::PlainText;
129 completionType = Completion::FunctionWithArgs;
131 completionType = Completion::FunctionWithoutArgs;
133 QString completionText = encodedMacroCompletion;
136 return Completion(completionText, removeTail, completionType);
void writeEntry(const char *key, const char *value, WriteConfigFlags pFlags=Normal)
QString readEntry(const char *key, const char *aDefault=nullptr) const
QEvent wrapper for copying/storing key events.
bool contains(const Key &key) const const
QList< Key > keys() const const
bool remove(const Key &key)
void append(QList< T > &&value)
const_reference at(qsizetype i) const const
bool isEmpty() const const
qsizetype length() const const
bool contains(QChar ch, Qt::CaseSensitivity cs) const const
bool endsWith(QChar c, Qt::CaseSensitivity cs) const const
QString number(double n, char format, int precision)
QString & remove(QChar ch, Qt::CaseSensitivity cs)
QString & replace(QChar before, QChar after, Qt::CaseSensitivity cs)
int toInt(bool *ok, int base) const const