fakevim: fix a few search related peculiarities

This commit is contained in:
hjk
2010-05-05 16:59:58 +02:00
parent a4a56a75e0
commit 46fa3aa7cc

View File

@@ -169,8 +169,7 @@ enum SubSubMode
UpCaseSubSubMode, // used for gU UpCaseSubSubMode, // used for gU
ReplaceSubSubMode, // used for r after visual mode ReplaceSubSubMode, // used for r after visual mode
TextObjectSubSubMode, // used for thing like iw, aW, as etc. TextObjectSubSubMode, // used for thing like iw, aW, as etc.
SearchForwardSubSubMode, SearchSubSubMode,
SearchBackwardSubSubMode,
}; };
enum VisualMode enum VisualMode
@@ -592,13 +591,9 @@ public:
bool m_anchorPastEnd; bool m_anchorPastEnd;
bool m_positionPastEnd; // '$' & 'l' in visual mode can move past eol bool m_positionPastEnd; // '$' & 'l' in visual mode can move past eol
bool isSearchSubSubMode() const
{
return m_subsubmode == SearchForwardSubSubMode
|| m_subsubmode == SearchBackwardSubSubMode;
}
int m_gflag; // whether current command started with 'g' int m_gflag; // whether current command started with 'g'
QString m_commandPrefix;
QString m_commandBuffer; QString m_commandBuffer;
QString m_currentFileName; QString m_currentFileName;
QString m_currentMessage; QString m_currentMessage;
@@ -792,7 +787,7 @@ bool FakeVimHandler::Private::wantsOverride(QKeyEvent *ev)
KEY_DEBUG("SHORTCUT OVERRIDE" << key << " PASSING: " << m_passing); KEY_DEBUG("SHORTCUT OVERRIDE" << key << " PASSING: " << m_passing);
if (key == Key_Escape) { if (key == Key_Escape) {
if (isSearchSubSubMode()) if (m_subsubmode == SearchSubSubMode)
return true; return true;
// Not sure this feels good. People often hit Esc several times // Not sure this feels good. People often hit Esc several times
if (isNoVisualMode() && m_mode == CommandMode) if (isNoVisualMode() && m_mode == CommandMode)
@@ -980,7 +975,7 @@ EventResult FakeVimHandler::Private::handleKey(const Input &input)
{ {
if (m_mode == ExMode) if (m_mode == ExMode)
return handleExMode(input); return handleExMode(input);
if (isSearchSubSubMode()) if (m_subsubmode == SearchSubSubMode)
return handleSearchSubSubMode(input); return handleSearchSubSubMode(input);
if (m_mode == InsertMode || m_mode == CommandMode) { if (m_mode == InsertMode || m_mode == CommandMode) {
g.pendingInput.append(input); g.pendingInput.append(input);
@@ -1113,6 +1108,7 @@ void FakeVimHandler::Private::finishMovement(const QString &dotCommand)
setPosition(qMin(anchor(), position())); setPosition(qMin(anchor(), position()));
enterExMode(); enterExMode();
m_currentMessage.clear(); m_currentMessage.clear();
m_commandPrefix = QChar();
m_commandBuffer = QString(".,+%1!").arg(qAbs(endLine - beginLine)); m_commandBuffer = QString(".,+%1!").arg(qAbs(endLine - beginLine));
g.commandHistory.append(QString()); g.commandHistory.append(QString());
g.commandHistoryIndex = g.commandHistory.size() - 1; g.commandHistoryIndex = g.commandHistory.size() - 1;
@@ -1341,12 +1337,10 @@ void FakeVimHandler::Private::updateMiniBuffer()
else else
msg = "-- INSERT --"; msg = "-- INSERT --";
} else { } else {
if (m_subsubmode == SearchForwardSubSubMode) if (m_subsubmode == SearchSubSubMode)
msg += '/'; msg += m_commandPrefix;
else if (m_subsubmode == SearchBackwardSubSubMode)
msg += '?';
else if (m_mode == ExMode) else if (m_mode == ExMode)
msg += ':'; msg += m_commandPrefix;
foreach (QChar c, m_commandBuffer) { foreach (QChar c, m_commandBuffer) {
if (c.unicode() < 32) { if (c.unicode() < 32) {
msg += '^'; msg += '^';
@@ -1631,6 +1625,7 @@ EventResult FakeVimHandler::Private::handleCommandMode(const Input &input)
} else if (input.is(':')) { } else if (input.is(':')) {
enterExMode(); enterExMode();
m_currentMessage.clear(); m_currentMessage.clear();
m_commandPrefix = input.text().at(0);
m_commandBuffer.clear(); m_commandBuffer.clear();
if (isVisualMode()) if (isVisualMode())
m_commandBuffer = "'<,'>"; m_commandBuffer = "'<,'>";
@@ -1638,10 +1633,10 @@ EventResult FakeVimHandler::Private::handleCommandMode(const Input &input)
g.commandHistoryIndex = g.commandHistory.size() - 1; g.commandHistoryIndex = g.commandHistory.size() - 1;
updateMiniBuffer(); updateMiniBuffer();
} else if (input.is('/') || input.is('?')) { } else if (input.is('/') || input.is('?')) {
m_lastSearchForward = input.is('/');
if (hasConfig(ConfigUseCoreSearch)) { if (hasConfig(ConfigUseCoreSearch)) {
// re-use the core dialog. // re-use the core dialog.
m_findPending = true; m_findPending = true;
m_lastSearchForward = (input.is('/'));
EDITOR(setTextCursor(m_tc)); EDITOR(setTextCursor(m_tc));
emit q->findRequested(!m_lastSearchForward); emit q->findRequested(!m_lastSearchForward);
m_tc = EDITOR(textCursor()); m_tc = EDITOR(textCursor());
@@ -1650,9 +1645,9 @@ EventResult FakeVimHandler::Private::handleCommandMode(const Input &input)
// FIXME: make core find dialog sufficiently flexible to // FIXME: make core find dialog sufficiently flexible to
// produce the "default vi" behaviour too. For now, roll our own. // produce the "default vi" behaviour too. For now, roll our own.
m_currentMessage.clear(); m_currentMessage.clear();
m_subsubmode = (input.is('/')) m_subsubmode = SearchSubSubMode;
? SearchForwardSubSubMode : SearchBackwardSubSubMode; m_commandPrefix = QLatin1Char(m_lastSearchForward ? '/' : '?');
m_commandBuffer.clear(); m_commandBuffer = QString();
g.searchHistory.append(QString()); g.searchHistory.append(QString());
g.searchHistoryIndex = g.searchHistory.size() - 1; g.searchHistoryIndex = g.searchHistory.size() - 1;
updateCursor(); updateCursor();
@@ -1665,7 +1660,10 @@ EventResult FakeVimHandler::Private::handleCommandMode(const Input &input)
m_tc.select(QTextCursor::WordUnderCursor); m_tc.select(QTextCursor::WordUnderCursor);
QString needle = "\\<" + m_tc.selection().toPlainText() + "\\>"; QString needle = "\\<" + m_tc.selection().toPlainText() + "\\>";
g.searchHistory.append(needle); g.searchHistory.append(needle);
m_lastSearchForward = (input.is('*')); m_lastSearchForward = input.is('*');
m_currentMessage.clear();
m_commandPrefix = QLatin1Char(m_lastSearchForward ? '/' : '?');
m_commandBuffer = needle;
updateMiniBuffer(); updateMiniBuffer();
search(needle, m_lastSearchForward); search(needle, m_lastSearchForward);
recordJump(); recordJump();
@@ -2003,9 +2001,12 @@ EventResult FakeVimHandler::Private::handleCommandMode(const Input &input)
m_tc = EDITOR(cursorForPosition(QPoint(0, EDITOR(height()) / 2))); m_tc = EDITOR(cursorForPosition(QPoint(0, EDITOR(height()) / 2)));
handleStartOfLine(); handleStartOfLine();
finishMovement(); finishMovement();
} else if (input.is('n') || input.is('N')) { } else if (input.is('n')) {
search(lastSearchString(), m_lastSearchForward); search(lastSearchString(), m_lastSearchForward);
recordJump(); recordJump();
} else if (input.is('N')) {
search(lastSearchString(), !m_lastSearchForward);
recordJump();
} else if (isVisualMode() && (input.is('o') || input.is('O'))) { } else if (isVisualMode() && (input.is('o') || input.is('O'))) {
int pos = position(); int pos = position();
setPosition(anchor()); setPosition(anchor());
@@ -2512,27 +2513,24 @@ EventResult FakeVimHandler::Private::handleSearchSubSubMode(const Input &input)
enterCommandMode(); enterCommandMode();
updateMiniBuffer(); updateMiniBuffer();
} else if (input.isKey(Key_Backspace)) { } else if (input.isKey(Key_Backspace)) {
if (m_commandBuffer.isEmpty()) { if (m_commandBuffer.isEmpty())
enterCommandMode(); enterCommandMode();
} else { else
m_commandBuffer.chop(1); m_commandBuffer.chop(1);
}
updateMiniBuffer(); updateMiniBuffer();
} else if (input.isKey(Key_Left)) { } else if (input.isKey(Key_Left)) {
// FIXME:
if (!m_commandBuffer.isEmpty()) if (!m_commandBuffer.isEmpty())
m_commandBuffer.chop(1); m_commandBuffer.chop(1);
updateMiniBuffer(); updateMiniBuffer();
} else if (input.isKey(Key_Return) && !hasConfig(ConfigIncSearch)) { } else if (input.isKey(Key_Return) && !hasConfig(ConfigIncSearch)) {
if (!m_commandBuffer.isEmpty()) { QString needle = m_commandBuffer;
if (!needle.isEmpty()) {
g.searchHistory.takeLast(); g.searchHistory.takeLast();
g.searchHistory.append(m_commandBuffer); g.searchHistory.append(needle);
m_lastSearchForward = (m_subsubmode == SearchForwardSubSubMode);
search(lastSearchString(), m_lastSearchForward); search(lastSearchString(), m_lastSearchForward);
recordJump(); recordJump();
} }
QString needle = m_commandBuffer.mid(1); // FIXME: why finishMovement(m_commandPrefix + needle + "\n");
finishMovement("/" + needle + "\n");
enterCommandMode(); enterCommandMode();
highlightMatches(needle); highlightMatches(needle);
updateMiniBuffer(); updateMiniBuffer();
@@ -2552,12 +2550,11 @@ EventResult FakeVimHandler::Private::handleSearchSubSubMode(const Input &input)
m_commandBuffer += QChar(9); m_commandBuffer += QChar(9);
updateMiniBuffer(); updateMiniBuffer();
} else if (input.isKey(Key_Return) && hasConfig(ConfigIncSearch)) { } else if (input.isKey(Key_Return) && hasConfig(ConfigIncSearch)) {
QString needle = m_commandBuffer;
enterCommandMode(); enterCommandMode();
QString needle = m_commandBuffer.mid(1); // FIXME: why
highlightMatches(needle); highlightMatches(needle);
updateMiniBuffer(); updateMiniBuffer();
} else if (hasConfig(ConfigIncSearch)) { } else if (hasConfig(ConfigIncSearch)) {
m_commandBuffer = m_commandBuffer.mid(1); // FIXME: why
QString needle = m_commandBuffer + input.text(); QString needle = m_commandBuffer + input.text();
search(needle, m_lastSearchForward, true); search(needle, m_lastSearchForward, true);
updateMiniBuffer(); updateMiniBuffer();
@@ -3192,7 +3189,7 @@ static void vimPatternToQtPattern(QString *needle, QTextDocument::FindFlags *fla
void FakeVimHandler::Private::search(const QString &needle0, bool forward, void FakeVimHandler::Private::search(const QString &needle0, bool forward,
bool incSearch) bool incSearch)
{ {
showBlackMessage((forward ? '/' : '?') + needle0); showBlackMessage(QLatin1Char(forward ? '/' : '?') + needle0);
CursorPosition origPosition = cursorPosition(); CursorPosition origPosition = cursorPosition();
QTextDocument::FindFlags flags = QTextDocument::FindCaseSensitively; QTextDocument::FindFlags flags = QTextDocument::FindCaseSensitively;
if (!forward) if (!forward)
@@ -3270,7 +3267,6 @@ void FakeVimHandler::Private::highlightMatches(const QString &needle0)
QString needle = needle0; QString needle = needle0;
vimPatternToQtPattern(&needle, &flags); vimPatternToQtPattern(&needle, &flags);
EDITOR(setTextCursor(tc)); EDITOR(setTextCursor(tc));
while (EDITOR(find(needle, flags))) { while (EDITOR(find(needle, flags))) {
tc = EDITOR(textCursor()); tc = EDITOR(textCursor());