Commit b5b29532 authored by hjk's avatar hjk
Browse files

fakevim: add a setting for (rudimentary) reading of .vimrc on startup

parent 4a1c9e59
......@@ -124,6 +124,14 @@ FakeVimSettings *theFakeVimSettings()
item->setValue(false);
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->setValue(true);
item->setDefaultValue(true);
......
......@@ -42,6 +42,7 @@ namespace Internal {
enum FakeVimSettingsCode
{
ConfigUseFakeVim,
ConfigReadVimRc,
ConfigStartOfLine,
ConfigHlSearch,
ConfigTabStop,
......
......@@ -653,23 +653,25 @@ public:
QList<QTextEdit::ExtraSelection> m_searchSelections;
bool handleExCommandHelper(const QString &cmd); // Returns success.
QString extractCommand(const QString &line, int *beginLine, int *endLine);
bool handleExBangCommand(const QString &line);
bool handleExDeleteCommand(const QString &line);
bool handleExGotoCommand(const QString &line);
bool handleExHistoryCommand(const QString &line);
bool handleExMapCommand(const QString &line);
bool handleExNormalCommand(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 handleExNormalCommand(const QString &line);
bool handleExDeleteCommand(const QString &line);
bool handleExSetCommand(const QString &line);
bool handleExHistoryCommand(const QString &line);
bool handleExShiftRightCommand(const QString &line);
bool handleExSourceCommand(const QString &line);
bool handleExSubstituteCommand(const QString &line);
bool handleExMapCommand(const QString &line);
bool handleExWriteCommand(const QString &line);
// All mappings.
typedef QHash<char, ModeMapping> Mappings;
Mappings m_mappings;
static Mappings m_mappings;
QVector<Input> m_pendingInput;
......@@ -680,6 +682,7 @@ public:
QStringList FakeVimHandler::Private::m_searchHistory;
QStringList FakeVimHandler::Private::m_commandHistory;
QHash<int, Register> FakeVimHandler::Private::m_registers;
FakeVimHandler::Private::Mappings FakeVimHandler::Private::m_mappings;
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)
// :w, :x :q
// :w, :x, :q, :wq, ...
{
int beginLine, endLine;
QString cmd = extractCommand(line, &beginLine, &endLine);
......@@ -2979,27 +2982,67 @@ bool FakeVimHandler::Private::handleExGotoCommand(const QString &line) // :<nr>
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)
{
QString line = line0; // Make sure we have a copy to prevent aliasing.
enterCommandMode();
showBlackMessage(QString());
if (handleExCommandHelper(line))
return;
int beginLine, endLine;
passUnknownExCommand(extractCommand(line, &beginLine, &endLine));
}
if (!(handleExGotoCommand(line)
|| handleExReadCommand(line)
|| handleExWriteCommand(line)
bool FakeVimHandler::Private::handleExCommandHelper(const QString &line)
{
return handleExGotoCommand(line)
|| handleExBangCommand(line)
|| handleExShiftRightCommand(line)
|| handleExRedoCommand(line)
|| handleExHistoryCommand(line)
|| handleExMapCommand(line)
|| handleExNormalCommand(line)
|| handleExSubstituteCommand(line)
|| handleExReadCommand(line)
|| handleExRedoCommand(line)
|| handleExSetCommand(line)
|| handleExHistoryCommand(line)
|| handleExMapCommand(line)))
{
int beginLine, endLine;
passUnknownExCommand(extractCommand(line, &beginLine, &endLine));
}
|| handleExShiftRightCommand(line)
|| handleExSourceCommand(line)
|| handleExSubstituteCommand(line)
|| handleExWriteCommand(line);
}
void FakeVimHandler::Private::passUnknownExCommand(const QString &cmd)
......
......@@ -18,6 +18,13 @@
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="checkBoxReadVimRc">
<property name="text">
<string>Read .vimrc</string>
</property>
</widget>
</item>
<item>
<widget class="QGroupBox" name="groupBox">
<property name="title">
......
......@@ -79,6 +79,7 @@
#include <QtCore/QSettings>
#include <QtCore/QTextStream>
#include <QtGui/QDesktopServices>
#include <QtGui/QMessageBox>
#include <QtGui/QPlainTextEdit>
#include <QtGui/QShortcut>
......@@ -157,6 +158,8 @@ QWidget *FakeVimOptionPage::createPage(QWidget *parent)
m_group.clear();
m_group.insert(theFakeVimSetting(ConfigUseFakeVim),
m_ui.checkBoxUseFakeVim);
m_group.insert(theFakeVimSetting(ConfigReadVimRc),
m_ui.checkBoxReadVimRc);
m_group.insert(theFakeVimSetting(ConfigExpandTab),
m_ui.checkBoxExpandTab);
......@@ -488,6 +491,7 @@ private slots:
void find(bool reverse);
void findNext(bool reverse);
void showSettingsDialog();
void maybeReadVimRc();
void showCommandBuffer(const QString &contents);
void showExtraInformation(const QString &msg);
......@@ -532,14 +536,22 @@ FakeVimPluginPrivate::FakeVimPluginPrivate(FakeVimPlugin *plugin)
m_fakeVimOptionsPage = 0;
m_fakeVimExCommandsPage = 0;
s_defaultExCommandMap[Constants::CMD_FILE_NEXT] = QRegExp("^n(ext)?!?( (.*))?$");
s_defaultExCommandMap[Constants::CMD_FILE_PREV] = QRegExp("^(N(ext)?|prev(ious)?)!?( (.*))?$");
s_defaultExCommandMap[CppTools::Constants::SWITCH_HEADER_SOURCE] = QRegExp("^A$");
s_defaultExCommandMap[ProjectExplorer::Constants::BUILD] = 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?$");
s_defaultExCommandMap[Constants::CMD_FILE_NEXT] =
QRegExp("^n(ext)?!?( (.*))?$");
s_defaultExCommandMap[Constants::CMD_FILE_PREV] =
QRegExp("^(N(ext)?|prev(ious)?)!?( (.*))?$");
s_defaultExCommandMap[CppTools::Constants::SWITCH_HEADER_SOURCE] =
QRegExp("^A$");
s_defaultExCommandMap[ProjectExplorer::Constants::BUILD] =
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()
......@@ -596,6 +608,8 @@ bool FakeVimPluginPrivate::initialize()
this, SLOT(showSettingsDialog()));
connect(theFakeVimSetting(ConfigUseFakeVim), SIGNAL(valueChanged(QVariant)),
this, SLOT(setUseFakeVim(QVariant)));
connect(theFakeVimSetting(ConfigReadVimRc), SIGNAL(valueChanged(QVariant)),
this, SLOT(maybeReadVimRc()));
QAction *switchFileNextAction = new QAction(tr("Switch to next file"), this);
cmd = actionManager->registerAction(switchFileNextAction, Constants::CMD_FILE_NEXT, globalcontext);
......@@ -612,6 +626,8 @@ bool FakeVimPluginPrivate::initialize()
this, SLOT(handleDelayedQuit(bool,Core::IEditor*)), Qt::QueuedConnection);
connect(this, SIGNAL(delayedQuitAllRequested(bool)),
this, SLOT(handleDelayedQuitAll(bool)), Qt::QueuedConnection);
maybeReadVimRc();
// << "MODE: " << theFakeVimSetting(ConfigUseFakeVim)->value();
return true;
}
......@@ -625,8 +641,9 @@ void FakeVimPluginPrivate::writeSettings(QSettings *settings)
settings->beginWriteArray(QLatin1String(exCommandMapGroup));
int count = 0;
const QMap<QString, QRegExp>::const_iterator end = s_exCommandMap.constEnd();
for (QMap<QString, QRegExp>::const_iterator it = s_exCommandMap.constBegin(); it != end; ++it) {
typedef QMap<QString, QRegExp>::const_iterator Iterator;
const Iterator end = s_exCommandMap.constEnd();
for (Iterator it = s_exCommandMap.constBegin(); it != end; ++it) {
const QString &id = it.key();
const QRegExp &re = it.value();
......@@ -647,7 +664,7 @@ void FakeVimPluginPrivate::readSettings(QSettings *settings)
s_exCommandMap = s_defaultExCommandMap;
int size = settings->beginReadArray(QLatin1String(exCommandMapGroup));
for (int i=0; i<size; ++i) {
for (int i = 0; i < size; ++i) {
settings->setArrayIndex(i);
const QString id = settings->value(QLatin1String(idKey)).toString();
const QString re = settings->value(QLatin1String(reKey)).toString();
......@@ -656,10 +673,30 @@ void FakeVimPluginPrivate::readSettings(QSettings *settings)
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()
{
Core::ICore::instance()->showOptionsDialog(QLatin1String(Constants::SETTINGS_CATEGORY),
QLatin1String(Constants::SETTINGS_ID));
Core::ICore::instance()->showOptionsDialog(
QLatin1String(Constants::SETTINGS_CATEGORY),
QLatin1String(Constants::SETTINGS_ID));
}
void FakeVimPluginPrivate::triggerAction(const QString& code)
......@@ -922,8 +959,9 @@ void FakeVimPluginPrivate::handleExCommand(const QString &cmd)
bool forced = cmd.contains(QChar('!'));
emit delayedQuitAllRequested(forced);
} else {
const QMap<QString, QRegExp>::const_iterator end = s_exCommandMap.constEnd();
for (QMap<QString, QRegExp>::const_iterator it = s_exCommandMap.constBegin(); it != end; ++it) {
typedef QMap<QString, QRegExp>::const_iterator Iterator;
const Iterator end = s_exCommandMap.constEnd();
for (Iterator it = s_exCommandMap.constBegin(); it != end; ++it) {
const QString &id = it.key();
const QRegExp &re = it.value();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment