Commit 1b0ba433 authored by Miquel Sabaté's avatar Miquel Sabaté

vimode: don't insert garbage characters when pressing r<backspace>.

REVIEW: 118955
parent 5a65d0b4
......@@ -506,6 +506,17 @@ int KateViKeyParser::vi2qt( const QString &keypress ) const
}
int KateViKeyParser::encoded2qt(const QString &keypress) const
{
QString key = KateViKeyParser::self()->decodeKeySequence(keypress);
if (key.length() > 2 && key[0] == QLatin1Char('<') &&
key[key.length() - 1] == QLatin1Char('>')) {
key = key.mid(1, key.length() - 2);
}
return (m_katevi2qt->contains(key) ? m_katevi2qt->value(key) : -1);
}
const QString KateViKeyParser::encodeKeySequence( const QString &keys ) const
{
QString encodedSequence;
......
......@@ -47,6 +47,7 @@ public:
const QString decodeKeySequence( const QString &keys ) const;
QString qt2vi( int key ) const;
int vi2qt( const QString &keypress ) const;
int encoded2qt(const QString &keypress) const;
const QChar KeyEventToQChar(const QKeyEvent &keyEvent);
private:
......
......@@ -1409,6 +1409,19 @@ bool KateViNormalMode::commandDeleteCharBackward()
bool KateViNormalMode::commandReplaceCharacter()
{
QString key = KateViKeyParser::self()->decodeKeySequence(m_keys.right(1));
// Filter out some special keys.
const int keyCode = KateViKeyParser::self()->encoded2qt(m_keys.right(1));
switch (keyCode) {
case Qt::Key_Left: case Qt::Key_Right: case Qt::Key_Up:
case Qt::Key_Down: case Qt::Key_Home: case Qt::Key_End:
case Qt::Key_PageUp: case Qt::Key_PageDown: case Qt::Key_Delete:
case Qt::Key_Insert: case Qt::Key_Backspace: case Qt::Key_CapsLock:
return true;
case Qt::Key_Return: case Qt::Key_Enter:
key = QLatin1String("\n");
}
bool r;
if ( m_viInputModeManager->isAnyVisualMode()) {
......@@ -1419,7 +1432,7 @@ bool KateViNormalMode::commandReplaceCharacter()
if (m == LineWise)
text = text.left(text.size() - 1); // don't need '\n' at the end;
text.replace( QRegExp( "[^\n]" ), m_keys.right( 1 ) );
text.replace( QRegExp( "[^\n]" ), key );
m_commandRange.normalize();
Cursor start( m_commandRange.startLine, m_commandRange.startColumn );
......@@ -1439,7 +1452,7 @@ bool KateViNormalMode::commandReplaceCharacter()
return false;
}
r = doc()->replaceText( Range( c1, c2 ), m_keys.right( 1 ).repeated(getCount()) );
r = doc()->replaceText( Range( c1, c2 ), key.repeated(getCount()) );
updateCursor( c1 );
}
......
......@@ -844,6 +844,12 @@ void ViModeTest::ReplaceModeTests()
DoTest(" a\nbb", "jR\\ctrl-y\\ctrl-y", " a\n a");
DoTest("\tb\n", "jR\\ctrl-y\\ctrl-y", "\tb\n\tb");
// Replace character.
DoTest("", "rr", "");
DoTest("a", "rb", "b");
DoTest("abc", "lr\\enter", "a\nc");
DoTest("abc", "l\\backspace", "abc");
DoTest("abc", "l\\left", "abc");
}
void ViModeTest::InsertModeTests() {
......
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