From ef4d2c301a96843c0fc6f178eb0e75dbdfd91456 Mon Sep 17 00:00:00 2001
From: Rhys Weatherley <rhys.weatherley@nokia.com>
Date: Fri, 29 May 2009 08:34:06 +1000
Subject: [PATCH] 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.
---
 src/plugins/fakevim/fakevimhandler.cpp | 26 +++++++++++++++++++++-----
 src/plugins/fakevim/fakevimhandler.h   |  1 +
 src/plugins/fakevim/fakevimplugin.cpp  |  8 ++++++++
 3 files changed, 30 insertions(+), 5 deletions(-)

diff --git a/src/plugins/fakevim/fakevimhandler.cpp b/src/plugins/fakevim/fakevimhandler.cpp
index fd1be75a56e..15b03ea48d1 100644
--- a/src/plugins/fakevim/fakevimhandler.cpp
+++ b/src/plugins/fakevim/fakevimhandler.cpp
@@ -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));
         }
diff --git a/src/plugins/fakevim/fakevimhandler.h b/src/plugins/fakevim/fakevimhandler.h
index 61e9ed097d7..c1f859657ae 100644
--- a/src/plugins/fakevim/fakevimhandler.h
+++ b/src/plugins/fakevim/fakevimhandler.h
@@ -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);
diff --git a/src/plugins/fakevim/fakevimplugin.cpp b/src/plugins/fakevim/fakevimplugin.cpp
index d656b0df16e..df084665f4e 100644
--- a/src/plugins/fakevim/fakevimplugin.cpp
+++ b/src/plugins/fakevim/fakevimplugin.cpp
@@ -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)
 {
-- 
GitLab