7#include "katepartdebug.h"
8#include "kateviinputmode.h"
9#include <vimode/emulatedcommandbar/emulatedcommandbar.h>
10#include <vimode/inputmodemanager.h>
11#include <vimode/keyparser.h>
12#include <vimode/mappings.h>
14#include <KConfigGroup>
16using namespace KateVi;
20 readMappings(config, QStringLiteral(
"Normal"), NormalModeMapping);
21 readMappings(config, QStringLiteral(
"Visual"), VisualModeMapping);
22 readMappings(config, QStringLiteral(
"Insert"), InsertModeMapping);
23 readMappings(config, QStringLiteral(
"Command"), CommandModeMapping);
26void Mappings::writeConfig(KConfigGroup &config)
const
28 writeMappings(config, QStringLiteral(
"Normal"), NormalModeMapping);
29 writeMappings(config, QStringLiteral(
"Visual"), VisualModeMapping);
30 writeMappings(config, QStringLiteral(
"Insert"), InsertModeMapping);
31 writeMappings(config, QStringLiteral(
"Command"), CommandModeMapping);
34void Mappings::writeMappings(KConfigGroup &config,
const QString &mappingModeName, MappingMode mappingMode)
const
36 config.
writeEntry(mappingModeName + QLatin1String(
" Mode Mapping Keys"), getAll(mappingMode,
true));
38 QList<bool> recursives;
39 const auto all = getAll(mappingMode);
42 for (
const QString &s : all) {
43 l << KeyParser::self()->decodeKeySequence(get(mappingMode, s));
44 recursives << isRecursive(mappingMode, s);
46 config.
writeEntry(mappingModeName + QLatin1String(
" Mode Mappings"), l);
47 config.
writeEntry(mappingModeName + QLatin1String(
" Mode Mappings Recursion"), recursives);
50 config.
writeEntry(QStringLiteral(
"Map Leader"), QString(leader));
53void Mappings::readMappings(
const KConfigGroup &config,
const QString &mappingModeName, MappingMode mappingMode)
55 const QStringList keys = config.
readEntry(mappingModeName + QLatin1String(
" Mode Mapping Keys"), QStringList());
56 const QStringList mappings = config.
readEntry(mappingModeName + QLatin1String(
" Mode Mappings"), QStringList());
57 const QList<bool> isRecursive = config.
readEntry(mappingModeName + QLatin1String(
" Mode Mappings Recursion"), QList<bool>());
59 const QString &mapLeader = config.
readEntry(QStringLiteral(
"Map Leader"), QString());
64 for (
int i = 0; i < keys.
length(); i++) {
68 MappingRecursion recursion = Recursive;
69 if (isRecursive.size() > i && !isRecursive.at(i)) {
70 recursion = NonRecursive;
72 add(mappingMode, keys.
at(i), mappings.
at(i), recursion);
75 qCDebug(LOG_KTE) <<
"Error when reading mappings from " << mappingModeName <<
" config: number of keys != number of values";
79void Mappings::add(MappingMode mode,
const QString &from,
const QString &to, MappingRecursion recursion)
81 const QString &encodedMapping = KeyParser::self()->encodeKeySequence(from);
87 const QString encodedTo = KeyParser::self()->encodeKeySequence(to);
88 Mapping mapping = {encodedTo, (recursion == Recursive),
false};
91 m_mappings[mode][encodedMapping] = mapping;
94 if (mode == NormalModeMapping) {
96 other.
replace(QLatin1String(
"<leader>"), m_leader);
97 other = KeyParser::self()->encodeKeySequence(other);
98 if (other != encodedMapping) {
99 mapping.temporary =
true;
100 m_mappings[mode][other] = mapping;
105void Mappings::remove(MappingMode mode,
const QString &from)
107 const QString &encodedMapping = KeyParser::self()->encodeKeySequence(from);
108 m_mappings[mode].
remove(encodedMapping);
111void Mappings::clear(MappingMode mode)
113 m_mappings[mode].
clear();
116QString Mappings::get(MappingMode mode,
const QString &from,
bool decode,
bool includeTemporary)
const
118 if (!m_mappings[mode].contains(from)) {
122 const Mapping &mapping = m_mappings[mode][from];
123 if (mapping.temporary && !includeTemporary) {
127 const QString &ret = mapping.encoded;
129 return KeyParser::self()->decodeKeySequence(ret);
135QStringList Mappings::getAll(MappingMode mode,
bool decode,
bool includeTemporary)
const
137 QStringList mappings;
138 const QHash<QString, Mapping> mappingsForMode = m_mappings[mode];
140 for (
auto i = mappingsForMode.
begin(); i != mappingsForMode.
end(); i++) {
141 if (!includeTemporary && i.value().temporary) {
146 mappings << KeyParser::self()->decodeKeySequence(i.key());
154bool Mappings::isRecursive(MappingMode mode,
const QString &from)
const
156 if (!m_mappings[mode].contains(from)) {
160 return m_mappings[mode][from].recursive;
163void Mappings::setLeader(
const QChar &leader)
168Mappings::MappingMode Mappings::mappingModeForCurrentViMode(KateViInputMode *viInputMode)
170 if (viInputMode->viModeEmulatedCommandBar()->isActive()) {
171 return CommandModeMapping;
173 const ViMode mode = viInputMode->viInputModeManager()->getCurrentViMode();
175 case ViMode::NormalMode:
176 return NormalModeMapping;
177 case ViMode::VisualMode:
178 case ViMode::VisualLineMode:
179 case ViMode::VisualBlockMode:
180 return VisualModeMapping;
181 case ViMode::InsertMode:
182 case ViMode::ReplaceMode:
183 return InsertModeMapping;
185 Q_ASSERT(
false &&
"unrecognised ViMode!");
186 return NormalModeMapping;
void writeEntry(const char *key, const char *value, WriteConfigFlags pFlags=Normal)
QString readEntry(const char *key, const char *aDefault=nullptr) const
const_reference at(qsizetype i) const const
qsizetype length() const const
void reserve(qsizetype size)
bool isEmpty() const const
QString & remove(QChar ch, Qt::CaseSensitivity cs)
QString & replace(QChar before, QChar after, Qt::CaseSensitivity cs)