fakevim: Fix searching

While searching for expression that is not found keep text cursor on
position before search.

Make search work with visual mode.

Clear message on escape from search mode.

Change-Id: I44e843a5a16bcc45a1bdc573ecc409654c4eb910
Reviewed-by: hjk <qthjk@ovi.com>
This commit is contained in:
Lukas Holecek
2012-09-08 19:21:46 +02:00
committed by hjk
parent 0af79be114
commit 3841b85418
2 changed files with 24 additions and 17 deletions

View File

@@ -436,6 +436,7 @@ void FakeVimPlugin::test_vim_search()
KEYS("/x", X "abc" N "def" N "ghi"); KEYS("/x", X "abc" N "def" N "ghi");
KEYS("/x<CR>", X "abc" N "def" N "ghi"); KEYS("/x<CR>", X "abc" N "def" N "ghi");
KEYS("/x<ESC>", X "abc" N "def" N "ghi"); KEYS("/x<ESC>", X "abc" N "def" N "ghi");
KEYS("/ghX", X "abc" N "def" N "ghi");
KEYS("?def<ESC>", X "abc" N "def" N "ghi"); KEYS("?def<ESC>", X "abc" N "def" N "ghi");
KEYS("?x", X "abc" N "def" N "ghi"); KEYS("?x", X "abc" N "def" N "ghi");

View File

@@ -2010,6 +2010,10 @@ void FakeVimHandler::Private::updateMiniBuffer()
msg = "-- PASSING -- "; msg = "-- PASSING -- ";
} else if (!m_currentMessage.isEmpty() && !interactive) { } else if (!m_currentMessage.isEmpty() && !interactive) {
msg = m_currentMessage; msg = m_currentMessage;
} else if (!m_commandPrefix.isEmpty()) {
msg = m_commandPrefix + m_commandBuffer.display();
if (interactive)
cursorPos = m_commandPrefix.size() + m_commandBuffer.cursorPos();
} else if (m_mode == CommandMode && isVisualMode()) { } else if (m_mode == CommandMode && isVisualMode()) {
if (isVisualCharMode()) { if (isVisualCharMode()) {
msg = "-- VISUAL --"; msg = "-- VISUAL --";
@@ -2022,10 +2026,6 @@ void FakeVimHandler::Private::updateMiniBuffer()
msg = "-- INSERT --"; msg = "-- INSERT --";
} else if (m_mode == ReplaceMode) { } else if (m_mode == ReplaceMode) {
msg = "-- REPLACE --"; msg = "-- REPLACE --";
} else if (!m_commandPrefix.isEmpty()) {
msg = m_commandPrefix + m_commandBuffer.display();
if (interactive)
cursorPos = m_commandPrefix.size() + m_commandBuffer.cursorPos();
} else { } else {
QTC_CHECK(m_mode == CommandMode && m_subsubmode != SearchSubSubMode); QTC_CHECK(m_mode == CommandMode && m_subsubmode != SearchSubSubMode);
msg = "-- COMMAND --"; msg = "-- COMMAND --";
@@ -3434,6 +3434,7 @@ EventResult FakeVimHandler::Private::handleExMode(const Input &input)
EventResult FakeVimHandler::Private::handleSearchSubSubMode(const Input &input) EventResult FakeVimHandler::Private::handleSearchSubSubMode(const Input &input)
{ {
if (input.isEscape()) { if (input.isEscape()) {
m_currentMessage.clear();
m_commandBuffer.clear(); m_commandBuffer.clear();
g.searchHistory.append(m_searchCursor.selectedText()); g.searchHistory.append(m_searchCursor.selectedText());
m_searchCursor = QTextCursor(); m_searchCursor = QTextCursor();
@@ -4250,9 +4251,6 @@ void FakeVimHandler::Private::searchBalanced(bool forward, QChar needle, QChar o
void FakeVimHandler::Private::search(const SearchData &sd) void FakeVimHandler::Private::search(const SearchData &sd)
{ {
if (sd.needle.isEmpty())
return;
QTextDocument::FindFlags flags = QTextDocument::FindCaseSensitively; QTextDocument::FindFlags flags = QTextDocument::FindCaseSensitively;
if (!sd.forward) if (!sd.forward)
flags |= QTextDocument::FindBackward; flags |= QTextDocument::FindBackward;
@@ -4279,26 +4277,34 @@ void FakeVimHandler::Private::search(const SearchData &sd)
highlightMatches(QString()); highlightMatches(QString());
showRedMessage(FakeVimHandler::tr("Pattern not found: %1").arg(sd.needle)); showRedMessage(FakeVimHandler::tr("Pattern not found: %1").arg(sd.needle));
updateSelection(); updateSelection();
return; } else {
QString msg = sd.forward
? FakeVimHandler::tr("search hit BOTTOM, continuing at TOP")
: FakeVimHandler::tr("search hit TOP, continuing at BOTTOM");
showRedMessage(msg);
} }
QString msg = sd.forward
? FakeVimHandler::tr("search hit BOTTOM, continuing at TOP")
: FakeVimHandler::tr("search hit TOP, continuing at BOTTOM");
showRedMessage(msg);
} else { } else {
QString msg = sd.forward QString msg = sd.forward
? FakeVimHandler::tr("search hit BOTTOM without match for: %1") ? FakeVimHandler::tr("search hit BOTTOM without match for: %1")
: FakeVimHandler::tr("search hit TOP without match for: %1"); : FakeVimHandler::tr("search hit TOP without match for: %1");
showRedMessage(msg.arg(sd.needle)); showRedMessage(msg.arg(sd.needle));
return;
} }
} }
recordJump(); if (tc.isNull()) {
tc = cursor();
tc.setPosition(m_searchStartPosition);
}
// Set Cursor. In contrast to the main editor we have the cursor recordJump();
// position before the anchor position. if (isVisualMode()) {
setAnchorAndPosition(tc.position(), tc.anchor()); int d = tc.anchor() - tc.position();
setPosition(tc.position() + d);
} else {
// Set Cursor. In contrast to the main editor we have the cursor
// position before the anchor position.
setAnchorAndPosition(tc.position(), tc.anchor());
}
// Making this unconditional feels better, but is not "vim like". // Making this unconditional feels better, but is not "vim like".
if (oldLine != cursorLine() - cursorLineOnScreen()) if (oldLine != cursorLine() - cursorLineOnScreen())