add history of search strings

This commit is contained in:
hjk
2008-12-26 17:01:21 +01:00
committed by Roberto Raggi
parent 8e5b3a260c
commit 40886db56f

View File

@@ -150,11 +150,22 @@ public:
bool m_fakeEnd; bool m_fakeEnd;
bool isSearchCommand() const
{ return m_commandCode == '?' || m_commandCode == '/'; }
int m_commandCode; // ?, /, : ... int m_commandCode; // ?, /, : ...
QString m_commandBuffer; QString m_commandBuffer;
bool m_lastSearchForward; bool m_lastSearchForward;
QString m_lastSearchString;
// History for '/'
QString lastSearchString() const;
QStringList m_searchHistory;
int m_searchHistoryIndex;
// History for ':'
QStringList m_commandHistory;
int m_commandHistoryIndex;
}; };
FakeVimHandler::Private::Private(FakeVimHandler *parent) FakeVimHandler::Private::Private(FakeVimHandler *parent)
@@ -302,6 +313,13 @@ void FakeVimHandler::Private::handleCommandMode(int key, const QString &text)
} else if (key == ':' || key == '/' || key == '?') { } else if (key == ':' || key == '/' || key == '?') {
m_commandCode = key; m_commandCode = key;
m_mode = ExMode; m_mode = ExMode;
if (isSearchCommand()) {
m_searchHistory.append(QString());
m_searchHistoryIndex = m_searchHistory.size() - 1;
} else {
m_commandHistory.append(QString());
m_commandHistoryIndex = m_commandHistory.size() - 1;
}
updateCommandBuffer(); updateCommandBuffer();
} else if (key == '|') { } else if (key == '|') {
m_tc.movePosition(StartOfLine, KeepAnchor); m_tc.movePosition(StartOfLine, KeepAnchor);
@@ -387,9 +405,9 @@ void FakeVimHandler::Private::handleCommandMode(int key, const QString &text)
moveToFirstNonBlankOnLine(); moveToFirstNonBlankOnLine();
finishMovement(); finishMovement();
} else if (key == 'n') { } else if (key == 'n') {
search(m_lastSearchString, m_lastSearchForward); search(lastSearchString(), m_lastSearchForward);
} else if (key == 'N') { } else if (key == 'N') {
search(m_lastSearchString, !m_lastSearchForward); search(lastSearchString(), !m_lastSearchForward);
} else if (key == 'p') { } else if (key == 'p') {
QString text = m_registers[m_register]; QString text = m_registers[m_register];
int n = text.count(QChar(ParagraphSeparator)); int n = text.count(QChar(ParagraphSeparator));
@@ -510,14 +528,23 @@ void FakeVimHandler::Private::handleExMode(int key, const QString &text)
} }
m_commandBuffer.clear(); m_commandBuffer.clear();
m_mode = CommandMode; m_mode = CommandMode;
} else if (key == Key_Return } else if (key == Key_Return && isSearchCommand()) {
&& (m_commandCode == '/' || m_commandCode == '?')) {
m_lastSearchForward = (m_commandCode == '/'); m_lastSearchForward = (m_commandCode == '/');
m_lastSearchString = m_commandBuffer; m_searchHistory.append(m_commandBuffer);
search(m_lastSearchString, m_lastSearchForward); search(lastSearchString(), m_lastSearchForward);
m_commandBuffer.clear(); m_commandBuffer.clear();
m_commandCode = 0; m_commandCode = 0;
m_mode = CommandMode; m_mode = CommandMode;
} else if (key == Key_Up && isSearchCommand()) {
if (m_searchHistoryIndex > 0) {
--m_searchHistoryIndex;
m_commandBuffer = m_searchHistory.at(m_searchHistoryIndex);
}
} else if (key == Key_Down && isSearchCommand()) {
if (m_searchHistoryIndex < m_searchHistory.size() - 1) {
++m_searchHistoryIndex;
m_commandBuffer = m_searchHistory.at(m_searchHistoryIndex);
}
} else { } else {
m_commandBuffer += QChar(key); m_commandBuffer += QChar(key);
} }
@@ -528,7 +555,7 @@ void FakeVimHandler::Private::search(const QString &needle, bool forward)
{ {
//qDebug() << "NEEDLE " << needle << "BACKWARDS" << backwards; //qDebug() << "NEEDLE " << needle << "BACKWARDS" << backwards;
QTextCursor orig = m_tc; QTextCursor orig = m_tc;
QTextDocument::FindFlags flags; QTextDocument::FindFlags flags = QTextDocument::FindCaseSensitively;
if (!forward) if (!forward)
flags = QTextDocument::FindBackward; flags = QTextDocument::FindBackward;
@@ -538,7 +565,8 @@ void FakeVimHandler::Private::search(const QString &needle, bool forward)
m_editor->setTextCursor(m_tc); m_editor->setTextCursor(m_tc);
if (m_editor->find(needle, flags)) { if (m_editor->find(needle, flags)) {
m_tc = m_editor->textCursor(); m_tc = m_editor->textCursor();
m_tc.movePosition(Left, MoveAnchor, needle.size() - 1); // the qMax seems to be needed for QPlainTextEdit only
m_tc.movePosition(Left, MoveAnchor, qMax(1, needle.size() - 1));
return; return;
} }
@@ -546,7 +574,8 @@ void FakeVimHandler::Private::search(const QString &needle, bool forward)
m_editor->setTextCursor(m_tc); m_editor->setTextCursor(m_tc);
if (m_editor->find(needle, flags)) { if (m_editor->find(needle, flags)) {
m_tc = m_editor->textCursor(); m_tc = m_editor->textCursor();
m_tc.movePosition(Left, MoveAnchor, needle.size() - 1); // the qMax seems to be needed for QPlainTextEdit only
m_tc.movePosition(Left, MoveAnchor, qMax(1, needle.size() - 1));
if (forward) if (forward)
showMessage("search hit BOTTOM, continuing at TOP"); showMessage("search hit BOTTOM, continuing at TOP");
else else
@@ -696,6 +725,11 @@ int FakeVimHandler::Private::lastPositionInDocument() const
return block.position() + block.length(); return block.position() + block.length();
} }
QString FakeVimHandler::Private::lastSearchString() const
{
return m_searchHistory.empty() ? QString() : m_searchHistory.back();
}
/////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////
// //
// FakeVimHandler // FakeVimHandler