Commit ef4d2c30 authored by Rhys Weatherley's avatar Rhys Weatherley

Add :q/:w/:x commands that are normally expected in vi's

Common things like :wq, :x, and :qa didn't work - now they do.
parent 8fa417bd
......@@ -1745,27 +1745,31 @@ void FakeVimHandler::Private::handleExCommand(const QString &cmd0)
//qDebug() << "RANGE: " << beginLine << endLine << cmd << cmd0 << m_marks;
static QRegExp reQuit("^qa?!?$");
static QRegExp reDelete("^d( (.*))?$");
static QRegExp reHistory("^his(tory)?( (.*))?$");
static QRegExp reNormal("^norm(al)?( (.*))?$");
static QRegExp reSet("^set?( (.*))?$");
static QRegExp reWrite("^w!?( (.*))?$");
static QRegExp reWrite("^[wx]q?a?!?( (.*))?$");
static QRegExp reSubstitute("^s(.)(.*)\\1(.*)\\1([gi]*)");
if (cmd.isEmpty()) {
setPosition(firstPositionInLine(beginLine));
showBlackMessage(QString());
enterCommandMode();
} else if (cmd == "q!" || cmd == "q") { // :q
} else if (reQuit.indexIn(cmd) != -1) { // :q
showBlackMessage(QString());
q->quitRequested(cmd == "q!");
if (cmd.contains(QChar('a')))
q->quitAllRequested(cmd.contains(QChar('!')));
else
q->quitRequested(cmd.contains(QChar('!')));
} else if (reDelete.indexIn(cmd) != -1) { // :d
selectRange(beginLine, endLine);
QString reg = reDelete.cap(2);
QString text = removeSelectedText();
if (!reg.isEmpty())
m_registers[reg.at(0).unicode()] = text;
} else if (reWrite.indexIn(cmd) != -1) { // :w
} else if (reWrite.indexIn(cmd) != -1) { // :w and :x
enterCommandMode();
bool noArgs = (beginLine == -1);
if (beginLine == -1)
......@@ -1773,7 +1777,15 @@ void FakeVimHandler::Private::handleExCommand(const QString &cmd0)
if (endLine == -1)
endLine = linesInDocument();
//qDebug() << "LINES: " << beginLine << endLine;
bool forced = cmd.startsWith("w!");
int indexOfSpace = cmd.indexOf(QChar(' '));
QString prefix;
if (indexOfSpace < 0)
prefix = cmd;
else
prefix = cmd.left(indexOfSpace);
bool forced = prefix.contains(QChar('!'));
bool quit = prefix.contains(QChar('q')) || prefix.contains(QChar('x'));
bool quitAll = quit && prefix.contains(QChar('a'));
QString fileName = reWrite.cap(2);
if (fileName.isEmpty())
fileName = m_currentFileName;
......@@ -1809,6 +1821,10 @@ void FakeVimHandler::Private::handleExCommand(const QString &cmd0)
showBlackMessage(tr("\"%1\" %2 %3L, %4C written")
.arg(fileName).arg(exists ? " " : " [New] ")
.arg(ba.count('\n')).arg(ba.size()));
if (quitAll)
q->quitAllRequested(forced);
else if (quit)
q->quitRequested(forced);
} else {
showRedMessage(tr("Cannot open file '%1' for reading").arg(fileName));
}
......
......@@ -66,6 +66,7 @@ signals:
void statusDataChanged(const QString &msg);
void extraInformationChanged(const QString &msg);
void quitRequested(bool force);
void quitAllRequested(bool force);
void selectionChanged(const QList<QTextEdit::ExtraSelection> &selection);
void writeFileRequested(bool *handled,
const QString &fileName, const QString &contents);
......
......@@ -242,6 +242,7 @@ private slots:
void changeSelection(const QList<QTextEdit::ExtraSelection> &selections);
void writeFile(bool *handled, const QString &fileName, const QString &contents);
void quitFile(bool forced);
void quitAllFiles(bool forced);
void moveToMatchingParenthesis(bool *moved, bool *forward, QTextCursor *cursor);
void indentRegion(int *amount, int beginLine, int endLine, QChar typedChar);
......@@ -391,6 +392,8 @@ void FakeVimPluginPrivate::editorOpened(Core::IEditor *editor)
this, SLOT(showCommandBuffer(QString)));
connect(handler, SIGNAL(quitRequested(bool)),
this, SLOT(quitFile(bool)), Qt::QueuedConnection);
connect(handler, SIGNAL(quitAllRequested(bool)),
this, SLOT(quitAllFiles(bool)), Qt::QueuedConnection);
connect(handler, SIGNAL(writeFileRequested(bool*,QString,QString)),
this, SLOT(writeFile(bool*,QString,QString)));
connect(handler, SIGNAL(selectionChanged(QList<QTextEdit::ExtraSelection>)),
......@@ -460,6 +463,11 @@ void FakeVimPluginPrivate::quitFile(bool forced)
Core::EditorManager::instance()->closeEditors(editors, !forced);
}
void FakeVimPluginPrivate::quitAllFiles(bool forced)
{
Core::EditorManager::instance()->closeAllEditors(!forced);
}
void FakeVimPluginPrivate::writeFile(bool *handled,
const QString &fileName, const QString &contents)
{
......
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