forked from qt-creator/qt-creator
fakevim: add a setting for (rudimentary) reading of .vimrc on startup
This commit is contained in:
@@ -124,6 +124,14 @@ FakeVimSettings *theFakeVimSettings()
|
|||||||
item->setValue(false);
|
item->setValue(false);
|
||||||
instance->insertItem(ConfigUseFakeVim, item);
|
instance->insertItem(ConfigUseFakeVim, item);
|
||||||
|
|
||||||
|
item = new SavedAction(instance);
|
||||||
|
item->setText(QCoreApplication::translate("FakeVim::Internal",
|
||||||
|
"Read .vimrc"));
|
||||||
|
item->setSettingsKey(group, _("ReadVimRc"));
|
||||||
|
item->setCheckable(true);
|
||||||
|
item->setValue(false);
|
||||||
|
instance->insertItem(ConfigReadVimRc, item);
|
||||||
|
|
||||||
item = new SavedAction(instance);
|
item = new SavedAction(instance);
|
||||||
item->setValue(true);
|
item->setValue(true);
|
||||||
item->setDefaultValue(true);
|
item->setDefaultValue(true);
|
||||||
|
|||||||
@@ -42,6 +42,7 @@ namespace Internal {
|
|||||||
enum FakeVimSettingsCode
|
enum FakeVimSettingsCode
|
||||||
{
|
{
|
||||||
ConfigUseFakeVim,
|
ConfigUseFakeVim,
|
||||||
|
ConfigReadVimRc,
|
||||||
ConfigStartOfLine,
|
ConfigStartOfLine,
|
||||||
ConfigHlSearch,
|
ConfigHlSearch,
|
||||||
ConfigTabStop,
|
ConfigTabStop,
|
||||||
|
|||||||
@@ -653,23 +653,25 @@ public:
|
|||||||
|
|
||||||
QList<QTextEdit::ExtraSelection> m_searchSelections;
|
QList<QTextEdit::ExtraSelection> m_searchSelections;
|
||||||
|
|
||||||
|
bool handleExCommandHelper(const QString &cmd); // Returns success.
|
||||||
QString extractCommand(const QString &line, int *beginLine, int *endLine);
|
QString extractCommand(const QString &line, int *beginLine, int *endLine);
|
||||||
bool handleExGotoCommand(const QString &line);
|
|
||||||
bool handleExReadCommand(const QString &line);
|
|
||||||
bool handleExWriteCommand(const QString &line);
|
|
||||||
bool handleExRedoCommand(const QString &line);
|
|
||||||
bool handleExShiftRightCommand(const QString &line);
|
|
||||||
bool handleExBangCommand(const QString &line);
|
bool handleExBangCommand(const QString &line);
|
||||||
bool handleExNormalCommand(const QString &line);
|
|
||||||
bool handleExDeleteCommand(const QString &line);
|
bool handleExDeleteCommand(const QString &line);
|
||||||
bool handleExSetCommand(const QString &line);
|
bool handleExGotoCommand(const QString &line);
|
||||||
bool handleExHistoryCommand(const QString &line);
|
bool handleExHistoryCommand(const QString &line);
|
||||||
bool handleExSubstituteCommand(const QString &line);
|
|
||||||
bool handleExMapCommand(const QString &line);
|
bool handleExMapCommand(const QString &line);
|
||||||
|
bool handleExNormalCommand(const QString &line);
|
||||||
|
bool handleExReadCommand(const QString &line);
|
||||||
|
bool handleExRedoCommand(const QString &line);
|
||||||
|
bool handleExSetCommand(const QString &line);
|
||||||
|
bool handleExShiftRightCommand(const QString &line);
|
||||||
|
bool handleExSourceCommand(const QString &line);
|
||||||
|
bool handleExSubstituteCommand(const QString &line);
|
||||||
|
bool handleExWriteCommand(const QString &line);
|
||||||
|
|
||||||
// All mappings.
|
// All mappings.
|
||||||
typedef QHash<char, ModeMapping> Mappings;
|
typedef QHash<char, ModeMapping> Mappings;
|
||||||
Mappings m_mappings;
|
static Mappings m_mappings;
|
||||||
|
|
||||||
QVector<Input> m_pendingInput;
|
QVector<Input> m_pendingInput;
|
||||||
|
|
||||||
@@ -680,6 +682,7 @@ public:
|
|||||||
QStringList FakeVimHandler::Private::m_searchHistory;
|
QStringList FakeVimHandler::Private::m_searchHistory;
|
||||||
QStringList FakeVimHandler::Private::m_commandHistory;
|
QStringList FakeVimHandler::Private::m_commandHistory;
|
||||||
QHash<int, Register> FakeVimHandler::Private::m_registers;
|
QHash<int, Register> FakeVimHandler::Private::m_registers;
|
||||||
|
FakeVimHandler::Private::Mappings FakeVimHandler::Private::m_mappings;
|
||||||
|
|
||||||
FakeVimHandler::Private::Private(FakeVimHandler *parent, QWidget *widget)
|
FakeVimHandler::Private::Private(FakeVimHandler *parent, QWidget *widget)
|
||||||
{
|
{
|
||||||
@@ -2817,7 +2820,7 @@ bool FakeVimHandler::Private::handleExDeleteCommand(const QString &line) // :d
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool FakeVimHandler::Private::handleExWriteCommand(const QString &line)
|
bool FakeVimHandler::Private::handleExWriteCommand(const QString &line)
|
||||||
// :w, :x :q
|
// :w, :x, :q, :wq, ...
|
||||||
{
|
{
|
||||||
int beginLine, endLine;
|
int beginLine, endLine;
|
||||||
QString cmd = extractCommand(line, &beginLine, &endLine);
|
QString cmd = extractCommand(line, &beginLine, &endLine);
|
||||||
@@ -2979,27 +2982,67 @@ bool FakeVimHandler::Private::handleExGotoCommand(const QString &line) // :<nr>
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool FakeVimHandler::Private::handleExSourceCommand(const QString &line) // :source
|
||||||
|
{
|
||||||
|
int pos = line.indexOf(' ');
|
||||||
|
if (line.leftRef(pos) != "so" && line.leftRef(pos) != "source")
|
||||||
|
return false;
|
||||||
|
|
||||||
|
QString fileName = line.mid(pos + 1);
|
||||||
|
QFile file(fileName);
|
||||||
|
if (!file.open(QIODevice::ReadOnly)) {
|
||||||
|
showRedMessage(FakeVimHandler::tr("Can't open file %1").arg(fileName));
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool inFunction = false;
|
||||||
|
while (!file.atEnd()) {
|
||||||
|
QByteArray line = file.readLine();
|
||||||
|
line = line.trimmed();
|
||||||
|
if (line.startsWith("function")) {
|
||||||
|
//qDebug() << "IGNORING FUNCTION" << line;
|
||||||
|
inFunction = true;
|
||||||
|
} else if (inFunction && line.startsWith("endfunction")) {
|
||||||
|
inFunction = false;
|
||||||
|
} else if (line.startsWith("function")) {
|
||||||
|
//qDebug() << "IGNORING FUNCTION" << line;
|
||||||
|
inFunction = true;
|
||||||
|
} else if (line.startsWith('"')) {
|
||||||
|
// A comment.
|
||||||
|
} else if (!line.isEmpty() && !inFunction) {
|
||||||
|
//qDebug() << "EXECUTING: " << line;
|
||||||
|
handleExCommandHelper(QString::fromUtf8(line));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
file.close();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
void FakeVimHandler::Private::handleExCommand(const QString &line0)
|
void FakeVimHandler::Private::handleExCommand(const QString &line0)
|
||||||
{
|
{
|
||||||
QString line = line0; // Make sure we have a copy to prevent aliasing.
|
QString line = line0; // Make sure we have a copy to prevent aliasing.
|
||||||
enterCommandMode();
|
enterCommandMode();
|
||||||
showBlackMessage(QString());
|
showBlackMessage(QString());
|
||||||
|
if (handleExCommandHelper(line))
|
||||||
if (!(handleExGotoCommand(line)
|
return;
|
||||||
|| handleExReadCommand(line)
|
|
||||||
|| handleExWriteCommand(line)
|
|
||||||
|| handleExBangCommand(line)
|
|
||||||
|| handleExShiftRightCommand(line)
|
|
||||||
|| handleExRedoCommand(line)
|
|
||||||
|| handleExNormalCommand(line)
|
|
||||||
|| handleExSubstituteCommand(line)
|
|
||||||
|| handleExSetCommand(line)
|
|
||||||
|| handleExHistoryCommand(line)
|
|
||||||
|| handleExMapCommand(line)))
|
|
||||||
{
|
|
||||||
int beginLine, endLine;
|
int beginLine, endLine;
|
||||||
passUnknownExCommand(extractCommand(line, &beginLine, &endLine));
|
passUnknownExCommand(extractCommand(line, &beginLine, &endLine));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool FakeVimHandler::Private::handleExCommandHelper(const QString &line)
|
||||||
|
{
|
||||||
|
return handleExGotoCommand(line)
|
||||||
|
|| handleExBangCommand(line)
|
||||||
|
|| handleExHistoryCommand(line)
|
||||||
|
|| handleExMapCommand(line)
|
||||||
|
|| handleExNormalCommand(line)
|
||||||
|
|| handleExReadCommand(line)
|
||||||
|
|| handleExRedoCommand(line)
|
||||||
|
|| handleExSetCommand(line)
|
||||||
|
|| handleExShiftRightCommand(line)
|
||||||
|
|| handleExSourceCommand(line)
|
||||||
|
|| handleExSubstituteCommand(line)
|
||||||
|
|| handleExWriteCommand(line);
|
||||||
}
|
}
|
||||||
|
|
||||||
void FakeVimHandler::Private::passUnknownExCommand(const QString &cmd)
|
void FakeVimHandler::Private::passUnknownExCommand(const QString &cmd)
|
||||||
|
|||||||
@@ -18,6 +18,13 @@
|
|||||||
</property>
|
</property>
|
||||||
</widget>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
|
<item>
|
||||||
|
<widget class="QCheckBox" name="checkBoxReadVimRc">
|
||||||
|
<property name="text">
|
||||||
|
<string>Read .vimrc</string>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
<item>
|
<item>
|
||||||
<widget class="QGroupBox" name="groupBox">
|
<widget class="QGroupBox" name="groupBox">
|
||||||
<property name="title">
|
<property name="title">
|
||||||
|
|||||||
@@ -79,6 +79,7 @@
|
|||||||
#include <QtCore/QSettings>
|
#include <QtCore/QSettings>
|
||||||
#include <QtCore/QTextStream>
|
#include <QtCore/QTextStream>
|
||||||
|
|
||||||
|
#include <QtGui/QDesktopServices>
|
||||||
#include <QtGui/QMessageBox>
|
#include <QtGui/QMessageBox>
|
||||||
#include <QtGui/QPlainTextEdit>
|
#include <QtGui/QPlainTextEdit>
|
||||||
#include <QtGui/QShortcut>
|
#include <QtGui/QShortcut>
|
||||||
@@ -157,6 +158,8 @@ QWidget *FakeVimOptionPage::createPage(QWidget *parent)
|
|||||||
m_group.clear();
|
m_group.clear();
|
||||||
m_group.insert(theFakeVimSetting(ConfigUseFakeVim),
|
m_group.insert(theFakeVimSetting(ConfigUseFakeVim),
|
||||||
m_ui.checkBoxUseFakeVim);
|
m_ui.checkBoxUseFakeVim);
|
||||||
|
m_group.insert(theFakeVimSetting(ConfigReadVimRc),
|
||||||
|
m_ui.checkBoxReadVimRc);
|
||||||
|
|
||||||
m_group.insert(theFakeVimSetting(ConfigExpandTab),
|
m_group.insert(theFakeVimSetting(ConfigExpandTab),
|
||||||
m_ui.checkBoxExpandTab);
|
m_ui.checkBoxExpandTab);
|
||||||
@@ -488,6 +491,7 @@ private slots:
|
|||||||
void find(bool reverse);
|
void find(bool reverse);
|
||||||
void findNext(bool reverse);
|
void findNext(bool reverse);
|
||||||
void showSettingsDialog();
|
void showSettingsDialog();
|
||||||
|
void maybeReadVimRc();
|
||||||
|
|
||||||
void showCommandBuffer(const QString &contents);
|
void showCommandBuffer(const QString &contents);
|
||||||
void showExtraInformation(const QString &msg);
|
void showExtraInformation(const QString &msg);
|
||||||
@@ -532,14 +536,22 @@ FakeVimPluginPrivate::FakeVimPluginPrivate(FakeVimPlugin *plugin)
|
|||||||
m_fakeVimOptionsPage = 0;
|
m_fakeVimOptionsPage = 0;
|
||||||
m_fakeVimExCommandsPage = 0;
|
m_fakeVimExCommandsPage = 0;
|
||||||
|
|
||||||
s_defaultExCommandMap[Constants::CMD_FILE_NEXT] = QRegExp("^n(ext)?!?( (.*))?$");
|
s_defaultExCommandMap[Constants::CMD_FILE_NEXT] =
|
||||||
s_defaultExCommandMap[Constants::CMD_FILE_PREV] = QRegExp("^(N(ext)?|prev(ious)?)!?( (.*))?$");
|
QRegExp("^n(ext)?!?( (.*))?$");
|
||||||
s_defaultExCommandMap[CppTools::Constants::SWITCH_HEADER_SOURCE] = QRegExp("^A$");
|
s_defaultExCommandMap[Constants::CMD_FILE_PREV] =
|
||||||
s_defaultExCommandMap[ProjectExplorer::Constants::BUILD] = QRegExp("^make$");
|
QRegExp("^(N(ext)?|prev(ious)?)!?( (.*))?$");
|
||||||
s_defaultExCommandMap["Coreplugin.OutputPane.previtem"] = QRegExp("^(cN(ext)?|cp(revious)?)!?( (.*))?$");
|
s_defaultExCommandMap[CppTools::Constants::SWITCH_HEADER_SOURCE] =
|
||||||
s_defaultExCommandMap["Coreplugin.OutputPane.nextitem"] = QRegExp("^cn(ext)?!?( (.*))?$");
|
QRegExp("^A$");
|
||||||
s_defaultExCommandMap[CppEditor::Constants::JUMP_TO_DEFINITION] = QRegExp("^tag?$");
|
s_defaultExCommandMap[ProjectExplorer::Constants::BUILD] =
|
||||||
s_defaultExCommandMap[Core::Constants::GO_BACK] = QRegExp("^pop?$");
|
QRegExp("^make$");
|
||||||
|
s_defaultExCommandMap["Coreplugin.OutputPane.previtem"] =
|
||||||
|
QRegExp("^(cN(ext)?|cp(revious)?)!?( (.*))?$");
|
||||||
|
s_defaultExCommandMap["Coreplugin.OutputPane.nextitem"] =
|
||||||
|
QRegExp("^cn(ext)?!?( (.*))?$");
|
||||||
|
s_defaultExCommandMap[CppEditor::Constants::JUMP_TO_DEFINITION] =
|
||||||
|
QRegExp("^tag?$");
|
||||||
|
s_defaultExCommandMap[Core::Constants::GO_BACK] =
|
||||||
|
QRegExp("^pop?$");
|
||||||
}
|
}
|
||||||
|
|
||||||
FakeVimPluginPrivate::~FakeVimPluginPrivate()
|
FakeVimPluginPrivate::~FakeVimPluginPrivate()
|
||||||
@@ -596,6 +608,8 @@ bool FakeVimPluginPrivate::initialize()
|
|||||||
this, SLOT(showSettingsDialog()));
|
this, SLOT(showSettingsDialog()));
|
||||||
connect(theFakeVimSetting(ConfigUseFakeVim), SIGNAL(valueChanged(QVariant)),
|
connect(theFakeVimSetting(ConfigUseFakeVim), SIGNAL(valueChanged(QVariant)),
|
||||||
this, SLOT(setUseFakeVim(QVariant)));
|
this, SLOT(setUseFakeVim(QVariant)));
|
||||||
|
connect(theFakeVimSetting(ConfigReadVimRc), SIGNAL(valueChanged(QVariant)),
|
||||||
|
this, SLOT(maybeReadVimRc()));
|
||||||
|
|
||||||
QAction *switchFileNextAction = new QAction(tr("Switch to next file"), this);
|
QAction *switchFileNextAction = new QAction(tr("Switch to next file"), this);
|
||||||
cmd = actionManager->registerAction(switchFileNextAction, Constants::CMD_FILE_NEXT, globalcontext);
|
cmd = actionManager->registerAction(switchFileNextAction, Constants::CMD_FILE_NEXT, globalcontext);
|
||||||
@@ -612,6 +626,8 @@ bool FakeVimPluginPrivate::initialize()
|
|||||||
this, SLOT(handleDelayedQuit(bool,Core::IEditor*)), Qt::QueuedConnection);
|
this, SLOT(handleDelayedQuit(bool,Core::IEditor*)), Qt::QueuedConnection);
|
||||||
connect(this, SIGNAL(delayedQuitAllRequested(bool)),
|
connect(this, SIGNAL(delayedQuitAllRequested(bool)),
|
||||||
this, SLOT(handleDelayedQuitAll(bool)), Qt::QueuedConnection);
|
this, SLOT(handleDelayedQuitAll(bool)), Qt::QueuedConnection);
|
||||||
|
maybeReadVimRc();
|
||||||
|
// << "MODE: " << theFakeVimSetting(ConfigUseFakeVim)->value();
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -625,8 +641,9 @@ void FakeVimPluginPrivate::writeSettings(QSettings *settings)
|
|||||||
settings->beginWriteArray(QLatin1String(exCommandMapGroup));
|
settings->beginWriteArray(QLatin1String(exCommandMapGroup));
|
||||||
|
|
||||||
int count = 0;
|
int count = 0;
|
||||||
const QMap<QString, QRegExp>::const_iterator end = s_exCommandMap.constEnd();
|
typedef QMap<QString, QRegExp>::const_iterator Iterator;
|
||||||
for (QMap<QString, QRegExp>::const_iterator it = s_exCommandMap.constBegin(); it != end; ++it) {
|
const Iterator end = s_exCommandMap.constEnd();
|
||||||
|
for (Iterator it = s_exCommandMap.constBegin(); it != end; ++it) {
|
||||||
const QString &id = it.key();
|
const QString &id = it.key();
|
||||||
const QRegExp &re = it.value();
|
const QRegExp &re = it.value();
|
||||||
|
|
||||||
@@ -647,7 +664,7 @@ void FakeVimPluginPrivate::readSettings(QSettings *settings)
|
|||||||
s_exCommandMap = s_defaultExCommandMap;
|
s_exCommandMap = s_defaultExCommandMap;
|
||||||
|
|
||||||
int size = settings->beginReadArray(QLatin1String(exCommandMapGroup));
|
int size = settings->beginReadArray(QLatin1String(exCommandMapGroup));
|
||||||
for (int i=0; i<size; ++i) {
|
for (int i = 0; i < size; ++i) {
|
||||||
settings->setArrayIndex(i);
|
settings->setArrayIndex(i);
|
||||||
const QString id = settings->value(QLatin1String(idKey)).toString();
|
const QString id = settings->value(QLatin1String(idKey)).toString();
|
||||||
const QString re = settings->value(QLatin1String(reKey)).toString();
|
const QString re = settings->value(QLatin1String(reKey)).toString();
|
||||||
@@ -656,9 +673,29 @@ void FakeVimPluginPrivate::readSettings(QSettings *settings)
|
|||||||
settings->endArray();
|
settings->endArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void FakeVimPluginPrivate::maybeReadVimRc()
|
||||||
|
{
|
||||||
|
qDebug() << theFakeVimSetting(ConfigReadVimRc)
|
||||||
|
<< theFakeVimSetting(ConfigReadVimRc)->value();
|
||||||
|
qDebug() << theFakeVimSetting(ConfigShiftWidth)->value();
|
||||||
|
if (!theFakeVimSetting(ConfigReadVimRc)->value().toBool())
|
||||||
|
return;
|
||||||
|
QString fileName =
|
||||||
|
QDesktopServices::storageLocation(QDesktopServices::HomeLocation)
|
||||||
|
+ "/.vimrc";
|
||||||
|
//qDebug() << "READING VIMRC: " << fileName;
|
||||||
|
// Read it into a temporary handler for effects modifying global state.
|
||||||
|
QPlainTextEdit editor;
|
||||||
|
FakeVimHandler handler(&editor);
|
||||||
|
handler.handleCommand("source " + fileName);
|
||||||
|
theFakeVimSettings()->writeSettings(Core::ICore::instance()->settings());
|
||||||
|
qDebug() << theFakeVimSetting(ConfigShiftWidth)->value();
|
||||||
|
}
|
||||||
|
|
||||||
void FakeVimPluginPrivate::showSettingsDialog()
|
void FakeVimPluginPrivate::showSettingsDialog()
|
||||||
{
|
{
|
||||||
Core::ICore::instance()->showOptionsDialog(QLatin1String(Constants::SETTINGS_CATEGORY),
|
Core::ICore::instance()->showOptionsDialog(
|
||||||
|
QLatin1String(Constants::SETTINGS_CATEGORY),
|
||||||
QLatin1String(Constants::SETTINGS_ID));
|
QLatin1String(Constants::SETTINGS_ID));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -922,8 +959,9 @@ void FakeVimPluginPrivate::handleExCommand(const QString &cmd)
|
|||||||
bool forced = cmd.contains(QChar('!'));
|
bool forced = cmd.contains(QChar('!'));
|
||||||
emit delayedQuitAllRequested(forced);
|
emit delayedQuitAllRequested(forced);
|
||||||
} else {
|
} else {
|
||||||
const QMap<QString, QRegExp>::const_iterator end = s_exCommandMap.constEnd();
|
typedef QMap<QString, QRegExp>::const_iterator Iterator;
|
||||||
for (QMap<QString, QRegExp>::const_iterator it = s_exCommandMap.constBegin(); it != end; ++it) {
|
const Iterator end = s_exCommandMap.constEnd();
|
||||||
|
for (Iterator it = s_exCommandMap.constBegin(); it != end; ++it) {
|
||||||
const QString &id = it.key();
|
const QString &id = it.key();
|
||||||
const QRegExp &re = it.value();
|
const QRegExp &re = it.value();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user