aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/fakevim/fakevimactions.cpp
blob: 007d9fcf5ac09883fcc90265dbad8bff44c33612 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/

#include "fakevimactions.h"
#include "fakevimhandler.h"

// Please do not add any direct dependencies to other Qt Creator code  here.
// Instead emit signals and let the FakeVimPlugin channel the information to
// Qt Creator. The idea is to keep this file here in a "clean" state that
// allows easy reuse with any QTextEdit or QPlainTextEdit derived class.


#include <utils/qtcassert.h>

#include <QDebug>

using namespace Utils;

namespace FakeVim {
namespace Internal {

DummyAction::DummyAction(void *)
{
}

void DummyAction::setValue(const QVariant &value)
{
    m_value = value;
}

QVariant DummyAction::value() const
{
    return m_value;
}

void DummyAction::setDefaultValue(const QVariant &value)
{
    m_defaultValue = value;
}

QVariant DummyAction::defaultValue() const
{
    return m_defaultValue;
}

void DummyAction::setSettingsKey(const QString &group, const QString &key)
{
    m_settingsGroup = group;
    m_settingsKey = key;
}

QString DummyAction::settingsKey() const
{
    return m_settingsKey;
}

FakeVimSettings::FakeVimSettings()
{
    // Specific FakeVim settings
    createAction(ConfigReadVimRc,  false,     "ReadVimRc");
    createAction(ConfigVimRcPath,  QString(), "VimRcPath");
#ifndef FAKEVIM_STANDALONE
    createAction(ConfigUseFakeVim, false,     "UseFakeVim");
    item(ConfigUseFakeVim)->setText(tr("Use Vim-style Editing"));
    item(ConfigReadVimRc)->setText(tr("Read .vimrc"));
    item(ConfigVimRcPath)->setText(tr("Path to .vimrc"));
#endif
    createAction(ConfigShowMarks,      false, "ShowMarks",      "sm");
    createAction(ConfigPassControlKey, false, "PassControlKey", "pck");
    createAction(ConfigPassKeys,       true,  "PassKeys",       "pk");

    // Emulated Vsetting
    createAction(ConfigStartOfLine,    true,  "StartOfLine",    "sol");
    createAction(ConfigTabStop,        8,     "TabStop",        "ts");
    createAction(ConfigSmartTab,       false, "SmartTab",       "sta");
    createAction(ConfigHlSearch,       true,  "HlSearch",       "hls");
    createAction(ConfigShiftWidth,     8,     "ShiftWidth",     "sw");
    createAction(ConfigExpandTab,      false, "ExpandTab",      "et");
    createAction(ConfigAutoIndent,     false, "AutoIndent",     "ai");
    createAction(ConfigSmartIndent,    false, "SmartIndent",    "si");
    createAction(ConfigIncSearch,      true,  "IncSearch",      "is");
    createAction(ConfigUseCoreSearch,  false, "UseCoreSearch",  "ucs");
    createAction(ConfigSmartCase,      false, "SmartCase",      "scs");
    createAction(ConfigIgnoreCase,     false, "IgnoreCase",     "ic");
    createAction(ConfigWrapScan,       true,  "WrapScan",       "ws");
    createAction(ConfigTildeOp,        false, "TildeOp",        "top");
    createAction(ConfigShowCmd,        true,  "ShowCmd",        "sc");
    createAction(ConfigRelativeNumber, false, "RelativeNumber", "rnu");
    createAction(ConfigBlinkingCursor, false, "BlinkingCursor", "bc");
    createAction(ConfigScrollOff,      0,     "ScrollOff",      "so");
    createAction(ConfigBackspace,      QString("indent,eol,start"), "ConfigBackspace", "bs");
    createAction(ConfigIsKeyword,      QString("@,48-57,_,192-255,a-z,A-Z"), "IsKeyword", "isk");
    createAction(ConfigClipboard,      QString(), "Clipboard", "cb");
}

FakeVimSettings::~FakeVimSettings()
{
    qDeleteAll(m_items);
}

void FakeVimSettings::insertItem(int code, FakeVimAction *item,
    const QString &longName, const QString &shortName)
{
    QTC_ASSERT(!m_items.contains(code), qDebug() << code; return);
    m_items[code] = item;
    if (!longName.isEmpty()) {
        m_nameToCode[longName] = code;
        m_codeToName[code] = longName;
    }
    if (!shortName.isEmpty())
        m_nameToCode[shortName] = code;
}

void FakeVimSettings::readSettings(QSettings *settings)
{
    foreach (FakeVimAction *item, m_items)
        item->readSettings(settings);
}

void FakeVimSettings::writeSettings(QSettings *settings)
{
    foreach (FakeVimAction *item, m_items)
        item->writeSettings(settings);
}

FakeVimAction *FakeVimSettings::item(int code)
{
    QTC_ASSERT(m_items.value(code, 0), qDebug() << "CODE: " << code; return nullptr);
    return m_items.value(code, 0);
}

FakeVimAction *FakeVimSettings::item(const QString &name)
{
    return m_items.value(m_nameToCode.value(name, -1), 0);
}

QString FakeVimSettings::trySetValue(const QString &name, const QString &value)
{
    int code = m_nameToCode.value(name, -1);
    if (code == -1)
        return tr("Unknown option: %1").arg(name);
    if (code == ConfigTabStop || code == ConfigShiftWidth) {
        if (value.toInt() <= 0)
            return tr("Argument must be positive: %1=%2")
                    .arg(name).arg(value);
    }
    FakeVimAction *act = item(code);
    if (!act)
        return tr("Unknown option: %1").arg(name);
    act->setValue(value);
    return QString();
}

void FakeVimSettings::createAction(int code, const QVariant &value,
                                   const QString &settingsKey,
                                   const QString &shortKey)
{
    auto item = new FakeVimAction(nullptr);
    item->setValue(value);
    item->setSettingsKey("FakeVim", settingsKey);
    item->setDefaultValue(value);
    item->setCheckable(value.canConvert<bool>());
    insertItem(code, item, settingsKey.toLower(), shortKey);
}

FakeVimSettings *theFakeVimSettings()
{
    static FakeVimSettings s;
    return &s;
}

FakeVimAction *theFakeVimSetting(int code)
{
    return theFakeVimSettings()->item(code);
}

} // namespace Internal
} // namespace FakeVim