fakevim: make Searching a 'sub-sub-mode' to allow d/

d/ does not properly work yet as it removes the search string, too.
This commit is contained in:
hjk
2010-05-04 17:58:53 +02:00
parent 1bd7a21ffd
commit c0f0c3e1bd

View File

@@ -135,8 +135,6 @@ enum Mode
InsertMode, InsertMode,
CommandMode, CommandMode,
ExMode, ExMode,
SearchForwardMode,
SearchBackwardMode,
}; };
enum SubMode enum SubMode
@@ -171,6 +169,8 @@ 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,
SearchBackwardSubSubMode,
}; };
enum VisualMode enum VisualMode
@@ -260,11 +260,14 @@ QString quoteUnprintable(const QString &ba)
{ {
QString res; QString res;
for (int i = 0, n = ba.size(); i != n; ++i) { for (int i = 0, n = ba.size(); i != n; ++i) {
QChar c = ba.at(i); const QChar c = ba.at(i);
const int cc = c.unicode();
if (c.isPrint()) if (c.isPrint())
res += c; res += c;
else if (cc == '\n')
res += QLatin1String("<CR>");
else else
res += QString("\\x%1").arg(c.unicode(), 2, 16); res += QString("\\x%1").arg(c.unicode(), 2, 16, QLatin1Char('0'));
} }
return res; return res;
} }
@@ -589,7 +592,10 @@ public:
bool m_positionPastEnd; // '$' & 'l' in visual mode can move past eol bool m_positionPastEnd; // '$' & 'l' in visual mode can move past eol
bool isSearchMode() const bool isSearchMode() const
{ return m_mode == SearchForwardMode || m_mode == SearchBackwardMode; } {
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_commandBuffer; QString m_commandBuffer;
@@ -774,6 +780,8 @@ 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 (isSearchMode())
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)
return false; return false;
@@ -958,6 +966,8 @@ void FakeVimHandler::Private::restoreWidget(int tabSize)
EventResult FakeVimHandler::Private::handleKey(const Input &input) EventResult FakeVimHandler::Private::handleKey(const Input &input)
{ {
if (m_mode == ExMode || isSearchMode())
return handleMiniBufferModes(input);
if (m_mode == InsertMode || m_mode == CommandMode) { if (m_mode == InsertMode || m_mode == CommandMode) {
m_pendingInput.append(input); m_pendingInput.append(input);
const char code = m_mode == InsertMode ? 'i' : 'n'; const char code = m_mode == InsertMode ? 'i' : 'n';
@@ -968,9 +978,6 @@ EventResult FakeVimHandler::Private::handleKey(const Input &input)
m_inputTimer = startTimer(1000); m_inputTimer = startTimer(1000);
return EventHandled; return EventHandled;
} }
if (m_mode == ExMode || m_mode == SearchForwardMode
|| m_mode == SearchBackwardMode)
return handleMiniBufferModes(input);
return EventUnhandled; return EventUnhandled;
} }
@@ -1210,20 +1217,19 @@ void FakeVimHandler::Private::finishMovement(const QString &dotCommand)
indentSelectedText(); indentSelectedText();
endEditBlock(); endEditBlock();
m_submode = NoSubMode; m_submode = NoSubMode;
updateMiniBuffer();
} else if (m_submode == ShiftRightSubMode) { } else if (m_submode == ShiftRightSubMode) {
recordJump(); recordJump();
shiftRegionRight(1); shiftRegionRight(1);
m_submode = NoSubMode; m_submode = NoSubMode;
updateMiniBuffer();
} else if (m_submode == ShiftLeftSubMode) { } else if (m_submode == ShiftLeftSubMode) {
recordJump(); recordJump();
shiftRegionLeft(1); shiftRegionLeft(1);
m_submode = NoSubMode; m_submode = NoSubMode;
updateMiniBuffer();
} }
resetCommandMode(); resetCommandMode();
updateSelection();
updateMiniBuffer();
updateCursor(); updateCursor();
} }
@@ -1236,9 +1242,6 @@ void FakeVimHandler::Private::resetCommandMode()
m_register = '"'; m_register = '"';
m_tc.clearSelection(); m_tc.clearSelection();
m_rangemode = RangeCharMode; m_rangemode = RangeCharMode;
updateSelection();
updateMiniBuffer();
} }
void FakeVimHandler::Private::updateSelection() void FakeVimHandler::Private::updateSelection()
@@ -1324,9 +1327,9 @@ void FakeVimHandler::Private::updateMiniBuffer()
else else
msg = "-- INSERT --"; msg = "-- INSERT --";
} else { } else {
if (m_mode == SearchForwardMode) if (m_subsubmode == SearchForwardSubSubMode)
msg += '/'; msg += '/';
else if (m_mode == SearchBackwardMode) else if (m_subsubmode == SearchBackwardSubSubMode)
msg += '?'; msg += '?';
else if (m_mode == ExMode) else if (m_mode == ExMode)
msg += ':'; msg += ':';
@@ -1475,6 +1478,8 @@ EventResult FakeVimHandler::Private::handleCommandMode(const Input &input)
finishMovement(); finishMovement();
} else { } else {
resetCommandMode(); resetCommandMode();
updateSelection();
updateMiniBuffer();
} }
} else if (m_subsubmode != NoSubSubMode) { } else if (m_subsubmode != NoSubSubMode) {
handleCommandSubSubMode(input); handleCommandSubSubMode(input);
@@ -1630,12 +1635,13 @@ EventResult FakeVimHandler::Private::handleCommandMode(const Input &input)
} else { } else {
// 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.
enterExMode(); // to get the cursor disabled
m_currentMessage.clear(); m_currentMessage.clear();
m_mode = (input.is('/')) ? SearchForwardMode : SearchBackwardMode; m_subsubmode = (input.is('/'))
? SearchForwardSubSubMode : SearchBackwardSubSubMode;
m_commandBuffer.clear(); m_commandBuffer.clear();
m_searchHistory.append(QString()); m_searchHistory.append(QString());
m_searchHistoryIndex = m_searchHistory.size() - 1; m_searchHistoryIndex = m_searchHistory.size() - 1;
updateCursor();
updateMiniBuffer(); updateMiniBuffer();
} }
} else if (input.is('`')) { } else if (input.is('`')) {
@@ -1703,7 +1709,8 @@ EventResult FakeVimHandler::Private::handleCommandMode(const Input &input)
} else if (input.is(',')) { } else if (input.is(',')) {
passShortcuts(true); passShortcuts(true);
} else if (input.is('.')) { } else if (input.is('.')) {
//qDebug() << "REPEATING" << quoteUnprintable(m_dotCommand) << count(); //qDebug() << "REPEATING" << quoteUnprintable(m_dotCommand) << count()
// << input;
QString savedCommand = m_dotCommand; QString savedCommand = m_dotCommand;
m_dotCommand.clear(); m_dotCommand.clear();
replay(savedCommand, count()); replay(savedCommand, count());
@@ -2452,6 +2459,20 @@ EventResult FakeVimHandler::Private::handleMiniBufferModes(const Input &input)
if (!m_commandBuffer.isEmpty()) if (!m_commandBuffer.isEmpty())
m_commandBuffer.chop(1); m_commandBuffer.chop(1);
updateMiniBuffer(); updateMiniBuffer();
} else if (input.isKey(Key_Return) && isSearchMode()
&& !hasConfig(ConfigIncSearch)) {
if (!m_commandBuffer.isEmpty()) {
m_searchHistory.takeLast();
m_searchHistory.append(m_commandBuffer);
m_lastSearchForward = (m_subsubmode == SearchForwardSubSubMode);
search(lastSearchString(), m_lastSearchForward);
recordJump();
}
QString needle = m_commandBuffer.mid(1); // FIXME: why
finishMovement("/" + needle + "\n");
enterCommandMode();
highlightMatches(needle);
updateMiniBuffer();
} else if (input.isKey(Key_Return) && m_mode == ExMode) { } else if (input.isKey(Key_Return) && m_mode == ExMode) {
if (!m_commandBuffer.isEmpty()) { if (!m_commandBuffer.isEmpty()) {
m_commandHistory.takeLast(); m_commandHistory.takeLast();
@@ -2461,17 +2482,6 @@ EventResult FakeVimHandler::Private::handleMiniBufferModes(const Input &input)
leaveVisualMode(); leaveVisualMode();
} }
} }
} else if (input.isKey(Key_Return) && isSearchMode()
&& !hasConfig(ConfigIncSearch)) {
if (!m_commandBuffer.isEmpty()) {
m_searchHistory.takeLast();
m_searchHistory.append(m_commandBuffer);
m_lastSearchForward = (m_mode == SearchForwardMode);
search(lastSearchString(), m_lastSearchForward);
recordJump();
}
enterCommandMode();
updateMiniBuffer();
} else if ((input.isKey(Key_Up) || input.isKey(Key_PageUp)) && isSearchMode()) { } else if ((input.isKey(Key_Up) || input.isKey(Key_PageUp)) && isSearchMode()) {
// FIXME: This and the three cases below are wrong as vim // FIXME: This and the three cases below are wrong as vim
// takes only matching entries in the history into account. // takes only matching entries in the history into account.
@@ -2854,7 +2864,7 @@ bool FakeVimHandler::Private::handleExNormalCommand(const QString &cmd) // :norm
static QRegExp reNormal("^norm(al)?( (.*))?$"); static QRegExp reNormal("^norm(al)?( (.*))?$");
if (reNormal.indexIn(cmd) == -1) if (reNormal.indexIn(cmd) == -1)
return false; return false;
//qDebug() << "REPLAY: " << reNormal.cap(3); //qDebug() << "REPLAY NORMAL: " << quoteUnprintable(reNormal.cap(3));
replay(reNormal.cap(3), 1); replay(reNormal.cap(3), 1);
return true; return true;
} }
@@ -3319,7 +3329,7 @@ void FakeVimHandler::Private::shiftRegionLeft(int repeat)
int targetPos = anchor(); int targetPos = anchor();
if (beginLine > endLine) { if (beginLine > endLine) {
qSwap(beginLine, endLine); qSwap(beginLine, endLine);
targetPos = position(); targetPos = position();
} }
const int shift = config(ConfigShiftWidth).toInt() * repeat; const int shift = config(ConfigShiftWidth).toInt() * repeat;
const int tab = config(ConfigTabStop).toInt(); const int tab = config(ConfigTabStop).toInt();
@@ -4068,6 +4078,8 @@ void FakeVimHandler::Private::enterInsertMode()
{ {
//leaveVisualMode(); //leaveVisualMode();
m_mode = InsertMode; m_mode = InsertMode;
m_submode = NoSubMode;
m_subsubmode = NoSubSubMode;
m_lastInsertion.clear(); m_lastInsertion.clear();
m_beginEditBlock = true; m_beginEditBlock = true;
updateCursor(); updateCursor();
@@ -4078,12 +4090,16 @@ void FakeVimHandler::Private::enterCommandMode()
if (atEndOfLine()) if (atEndOfLine())
moveLeft(); moveLeft();
m_mode = CommandMode; m_mode = CommandMode;
m_submode = NoSubMode;
m_subsubmode = NoSubSubMode;
updateCursor(); updateCursor();
} }
void FakeVimHandler::Private::enterExMode() void FakeVimHandler::Private::enterExMode()
{ {
m_mode = ExMode; m_mode = ExMode;
m_submode = NoSubMode;
m_subsubmode = NoSubSubMode;
updateCursor(); updateCursor();
} }
@@ -4170,7 +4186,7 @@ void FakeVimHandler::Private::handleStartOfLine()
void FakeVimHandler::Private::replay(const QString &command, int n) void FakeVimHandler::Private::replay(const QString &command, int n)
{ {
//qDebug() << "REPLAY: " << command; //qDebug() << "REPLAY: " << quoteUnprintable(command);
m_inReplay = true; m_inReplay = true;
for (int i = n; --i >= 0; ) { for (int i = n; --i >= 0; ) {
foreach (QChar c, command) { foreach (QChar c, command) {