From eeffd9beb078a3cce8226ed1d79fb70f266e3a47 Mon Sep 17 00:00:00 2001
From: hjk <qtc-committer@nokia.com>
Date: Sat, 27 Dec 2008 12:24:50 +0100
Subject: [PATCH] make <count> work for 'i'

---
 src/plugins/fakevim/handler.cpp | 18 +++++++++++-
 tests/auto/fakevim/fakevim.pro  |  9 ++++++
 tests/auto/fakevim/main.cpp     | 49 +++++++++++++++++++++++++++++++++
 3 files changed, 75 insertions(+), 1 deletion(-)
 create mode 100644 tests/auto/fakevim/fakevim.pro
 create mode 100644 tests/auto/fakevim/main.cpp

diff --git a/src/plugins/fakevim/handler.cpp b/src/plugins/fakevim/handler.cpp
index 01808c1a51b..cb2bba2b8b8 100644
--- a/src/plugins/fakevim/handler.cpp
+++ b/src/plugins/fakevim/handler.cpp
@@ -160,6 +160,7 @@ public:
     QString m_commandBuffer;
 
     bool m_lastSearchForward;
+    QString m_lastInsertion;
 
     // History for '/'
     QString lastSearchString() const;
@@ -388,6 +389,7 @@ void FakeVimHandler::Private::handleCommandMode(int key, const QString &text)
         moveToFirstNonBlankOnLine();
         finishMovement();
     } else if (key == 'i') {
+        m_lastInsertion.clear();
         m_mode = InsertMode;
     } else if (key == 'j' || key == Key_Down) {
         m_tc.movePosition(Down, KeepAnchor, count());
@@ -483,28 +485,42 @@ void FakeVimHandler::Private::handleInsertMode(int key, const QString &text)
 {
     if (key == Key_Escape) {
         m_mode = CommandMode;
-        m_tc.movePosition(Left, KeepAnchor, qMin(1, leftDist()));
+        for (int i = 1; i < count(); ++i)
+            m_tc.insertText(m_lastInsertion);
+        m_tc.movePosition(Left, MoveAnchor, qMin(1, leftDist()));
     } else if (key == Key_Left) {
         m_tc.movePosition(Left, MoveAnchor, 1);
+        m_lastInsertion.clear();
     } else if (key == Key_Down) {
         m_tc.movePosition(Down, MoveAnchor, 1);
+        m_lastInsertion.clear();
     } else if (key == Key_Up) {
         m_tc.movePosition(Up, MoveAnchor, 1);
+        m_lastInsertion.clear();
     } else if (key == Key_Right) {
         m_tc.movePosition(Right, MoveAnchor, 1);
+        m_lastInsertion.clear();
     } else if (key == Key_Return) {
         m_tc.insertBlock();
+        m_lastInsertion.clear();
     } else if (key == Key_Backspace) {
         m_tc.deletePreviousChar();
+        m_lastInsertion = m_lastInsertion.left(m_lastInsertion.size() - 1);
     } else if (key == Key_Delete) {
         m_tc.deleteChar();
+        m_lastInsertion.clear();
     } else if (key == Key_PageDown || key == control('f')) {
         m_tc.movePosition(Down, KeepAnchor, count() * (linesOnScreen() - 2));
+        m_lastInsertion.clear();
+    } else if (key == Key_Backspace) {
         finishMovement();
     } else if (key == Key_PageUp || key == control('b')) {
         m_tc.movePosition(Up, KeepAnchor, count() * (linesOnScreen() - 2));
+        m_lastInsertion.clear();
+    } else if (key == Key_Backspace) {
         finishMovement();
     } else {
+        m_lastInsertion.append(text);
         m_tc.insertText(text);
     }    
 }
diff --git a/tests/auto/fakevim/fakevim.pro b/tests/auto/fakevim/fakevim.pro
new file mode 100644
index 00000000000..696feeec31c
--- /dev/null
+++ b/tests/auto/fakevim/fakevim.pro
@@ -0,0 +1,9 @@
+
+load(qttest_p4)
+
+SOURCES += \
+    main.cpp \
+    handler.cpp
+
+HEADER += \
+    handler.h
diff --git a/tests/auto/fakevim/main.cpp b/tests/auto/fakevim/main.cpp
new file mode 100644
index 00000000000..28a05dcd530
--- /dev/null
+++ b/tests/auto/fakevim/main.cpp
@@ -0,0 +1,49 @@
+/***************************************************************************
+**
+** This file is part of Qt Creator
+**
+** Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies).
+**
+** Contact:  Qt Software Information (qt-info@nokia.com)
+**
+**
+** Non-Open Source Usage
+**
+** Licensees may use this file in accordance with the Qt Beta Version
+** License Agreement, Agreement version 2.2 provided with the Software or,
+** alternatively, in accordance with the terms contained in a written
+** agreement between you and Nokia.
+**
+** GNU General Public License Usage
+**
+** Alternatively, this file may be used under the terms of the GNU General
+** Public License versions 2.0 or 3.0 as published by the Free Software
+** Foundation and appearing in the file LICENSE.GPL included in the packaging
+** of this file.  Please review the following information to ensure GNU
+** General Public Licensing requirements will be met:
+**
+** http://www.fsf.org/licensing/licenses/info/GPLv2.html and
+** http://www.gnu.org/copyleft/gpl.html.
+**
+** In addition, as a special exception, Nokia gives you certain additional
+** rights. These rights are described in the Nokia Qt GPL Exception
+** version 1.3, included in the file GPL_EXCEPTION.txt in this package.
+**
+***************************************************************************/
+
+#include "handler.h"
+
+#include <QtTest/QtTest>
+#include <QtCore/QSet>
+
+class tst_FakeVim : public QObject
+{
+    Q_OBJECT
+
+private slots:
+};
+
+
+QTEST_MAIN(tst_FakeVim)
+
+#include "main.moc"
-- 
GitLab