uses enums to distinguish searching and ex commands

This commit is contained in:
hjk
2009-01-06 16:04:34 +01:00
parent 8a77bac457
commit 9da009aaa1

View File

@@ -60,6 +60,7 @@ using namespace FakeVim::Internal;
#define Down QTextCursor::Down #define Down QTextCursor::Down
#define Right QTextCursor::Right #define Right QTextCursor::Right
#define Left QTextCursor::Left #define Left QTextCursor::Left
#define EndOfDocument QTextCursor::End
/////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////
@@ -78,7 +79,9 @@ enum Mode
{ {
InsertMode, InsertMode,
CommandMode, CommandMode,
ExMode ExMode,
SearchForwardMode,
SearchBackwardMode,
}; };
enum SubMode enum SubMode
@@ -87,7 +90,7 @@ enum SubMode
RegisterSubMode, RegisterSubMode,
ChangeSubMode, ChangeSubMode,
DeleteSubMode, DeleteSubMode,
ZSubMode ZSubMode,
}; };
enum SubSubMode enum SubSubMode
@@ -147,7 +150,7 @@ private:
void handleInsertMode(int key, const QString &text); void handleInsertMode(int key, const QString &text);
void handleCommandMode(int key, const QString &text); void handleCommandMode(int key, const QString &text);
void handleRegisterMode(int key, const QString &text); void handleRegisterMode(int key, const QString &text);
void handleExMode(int key, const QString &text); void handleMiniBufferModes(int key, const QString &text);
void finishMovement(const QString &text = QString()); void finishMovement(const QString &text = QString());
void updateMiniBuffer(); void updateMiniBuffer();
void updateSelection(); void updateSelection();
@@ -207,9 +210,8 @@ private:
bool m_fakeEnd; bool m_fakeEnd;
QWidget *editor() const; QWidget *editor() const;
bool isSearchCommand() const bool isSearchMode() const
{ return m_commandCode == '?' || m_commandCode == '/'; } { return m_mode == SearchForwardMode || m_mode == SearchBackwardMode; }
int m_commandCode; // ?, /, : ...
int m_gflag; // whether current command started with 'g' int m_gflag; // whether current command started with 'g'
QString m_commandBuffer; QString m_commandBuffer;
@@ -260,7 +262,6 @@ FakeVimHandler::Private::Private(FakeVimHandler *parent)
m_mode = CommandMode; m_mode = CommandMode;
m_submode = NoSubMode; m_submode = NoSubMode;
m_subsubmode = NoSubSubMode; m_subsubmode = NoSubSubMode;
m_commandCode = 0;
m_fakeEnd = false; m_fakeEnd = false;
m_lastSearchForward = true; m_lastSearchForward = true;
m_register = '"'; m_register = '"';
@@ -317,8 +318,9 @@ void FakeVimHandler::Private::handleKey(int key, const QString &text)
handleInsertMode(key, text); handleInsertMode(key, text);
else if (m_mode == CommandMode) else if (m_mode == CommandMode)
handleCommandMode(key, text); handleCommandMode(key, text);
else if (m_mode == ExMode) else if (m_mode == ExMode || m_mode == SearchForwardMode
handleExMode(key, text); || m_mode == SearchBackwardMode)
handleMiniBufferModes(key, text);
} }
void FakeVimHandler::Private::finishMovement(const QString &dotCommand) void FakeVimHandler::Private::finishMovement(const QString &dotCommand)
@@ -413,7 +415,12 @@ void FakeVimHandler::Private::updateMiniBuffer()
} else if (m_mode == InsertMode) { } else if (m_mode == InsertMode) {
msg = "-- INSERT --"; msg = "-- INSERT --";
} else { } else {
msg = QChar(m_commandCode ? m_commandCode : ' '); if (m_mode == SearchForwardMode)
msg += '/';
else if (m_mode == SearchBackwardMode)
msg += '?';
else if (m_mode == ExMode)
msg += ':';
foreach (QChar c, m_commandBuffer) { foreach (QChar c, m_commandBuffer) {
if (c.unicode() < 32) { if (c.unicode() < 32) {
msg += '^'; msg += '^';
@@ -502,17 +509,21 @@ void FakeVimHandler::Private::handleCommandMode(int key, const QString &text)
} else { } else {
m_mvcount.append(QChar(key)); m_mvcount.append(QChar(key));
} }
} else if (key == ':' || key == '/' || key == '?') { } else if (key == ':') {
m_commandCode = key;
m_mode = ExMode; m_mode = ExMode;
m_commandBuffer.clear(); m_commandBuffer.clear();
if (isSearchCommand()) { if (m_visualMode != NoVisualMode) {
m_searchHistory.append(QString()); m_commandBuffer = "'<,'>";
m_searchHistoryIndex = m_searchHistory.size() - 1; leaveVisualMode();
} else { }
m_commandHistory.append(QString()); m_commandHistory.append(QString());
m_commandHistoryIndex = m_commandHistory.size() - 1; m_commandHistoryIndex = m_commandHistory.size() - 1;
} updateMiniBuffer();
} else if (key == '/' || key == '?') {
m_mode = (key == '/') ? SearchForwardMode : SearchBackwardMode;
m_commandBuffer.clear();
m_searchHistory.append(QString());
m_searchHistoryIndex = m_searchHistory.size() - 1;
updateMiniBuffer(); updateMiniBuffer();
} else if (key == '`') { } else if (key == '`') {
m_subsubmode = BackTickSubSubMode; m_subsubmode = BackTickSubSubMode;
@@ -785,13 +796,12 @@ void FakeVimHandler::Private::handleInsertMode(int key, const QString &text)
updateMiniBuffer(); updateMiniBuffer();
} }
void FakeVimHandler::Private::handleExMode(int key, const QString &text) void FakeVimHandler::Private::handleMiniBufferModes(int key, const QString &text)
{ {
Q_UNUSED(text) Q_UNUSED(text)
if (key == Key_Escape) { if (key == Key_Escape) {
m_commandBuffer.clear(); m_commandBuffer.clear();
m_commandCode = 0;
enterCommandMode(); enterCommandMode();
updateMiniBuffer(); updateMiniBuffer();
} else if (key == Key_Backspace) { } else if (key == Key_Backspace) {
@@ -800,25 +810,23 @@ void FakeVimHandler::Private::handleExMode(int key, const QString &text)
else else
m_commandBuffer.chop(1); m_commandBuffer.chop(1);
updateMiniBuffer(); updateMiniBuffer();
} else if (key == Key_Return && m_commandCode == ':') { } else if (key == Key_Return && m_mode == ExMode) {
if (!m_commandBuffer.isEmpty()) { if (!m_commandBuffer.isEmpty()) {
m_commandHistory.takeLast(); m_commandHistory.takeLast();
m_commandHistory.append(m_commandBuffer); m_commandHistory.append(m_commandBuffer);
handleExCommand(m_commandBuffer); handleExCommand(m_commandBuffer);
m_commandCode = 0;
} }
enterCommandMode(); enterCommandMode();
} else if (key == Key_Return && isSearchCommand()) { } else if (key == Key_Return && isSearchMode()) {
if (!m_commandBuffer.isEmpty()) { if (!m_commandBuffer.isEmpty()) {
m_searchHistory.takeLast(); m_searchHistory.takeLast();
m_searchHistory.append(m_commandBuffer); m_searchHistory.append(m_commandBuffer);
m_lastSearchForward = (m_commandCode == '/'); m_lastSearchForward = (m_mode == SearchForwardMode);
search(lastSearchString(), m_lastSearchForward); search(lastSearchString(), m_lastSearchForward);
m_commandCode = 0;
} }
enterCommandMode(); enterCommandMode();
updateMiniBuffer(); updateMiniBuffer();
} else if (key == Key_Up && isSearchCommand()) { } else if (key == Key_Up && 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 entires in the history into account. // takes only matching entires in the history into account.
if (m_searchHistoryIndex > 0) { if (m_searchHistoryIndex > 0) {
@@ -826,19 +834,19 @@ void FakeVimHandler::Private::handleExMode(int key, const QString &text)
m_commandBuffer = m_searchHistory.at(m_searchHistoryIndex); m_commandBuffer = m_searchHistory.at(m_searchHistoryIndex);
updateMiniBuffer(); updateMiniBuffer();
} }
} else if (key == Key_Up && m_commandCode == ':') { } else if (key == Key_Up && m_mode == ExMode) {
if (m_commandHistoryIndex > 0) { if (m_commandHistoryIndex > 0) {
--m_commandHistoryIndex; --m_commandHistoryIndex;
m_commandBuffer = m_commandHistory.at(m_commandHistoryIndex); m_commandBuffer = m_commandHistory.at(m_commandHistoryIndex);
updateMiniBuffer(); updateMiniBuffer();
} }
} else if (key == Key_Down && isSearchCommand()) { } else if (key == Key_Down && isSearchMode()) {
if (m_searchHistoryIndex < m_searchHistory.size() - 1) { if (m_searchHistoryIndex < m_searchHistory.size() - 1) {
++m_searchHistoryIndex; ++m_searchHistoryIndex;
m_commandBuffer = m_searchHistory.at(m_searchHistoryIndex); m_commandBuffer = m_searchHistory.at(m_searchHistoryIndex);
updateMiniBuffer(); updateMiniBuffer();
} }
} else if (key == Key_Down && m_commandCode == ':') { } else if (key == Key_Down && m_mode == ExMode) {
if (m_commandHistoryIndex < m_commandHistory.size() - 1) { if (m_commandHistoryIndex < m_commandHistory.size() - 1) {
++m_commandHistoryIndex; ++m_commandHistoryIndex;
m_commandBuffer = m_commandHistory.at(m_commandHistoryIndex); m_commandBuffer = m_commandHistory.at(m_commandHistoryIndex);
@@ -875,6 +883,7 @@ int FakeVimHandler::Private::readLineCode(QString &cmd)
} }
if (c == '\'' && !cmd.isEmpty()) { if (c == '\'' && !cmd.isEmpty()) {
int pos = m_marks.value(cmd.at(0).unicode()); int pos = m_marks.value(cmd.at(0).unicode());
qDebug() << " MARK: " << cmd.at(0) << pos << lineForPosition(pos);
if (!pos) { if (!pos) {
showMessage(tr("E20: Mark '%1' not set").arg(cmd.at(0))); showMessage(tr("E20: Mark '%1' not set").arg(cmd.at(0)));
return -1; return -1;
@@ -903,7 +912,12 @@ QTextCursor FakeVimHandler::Private::selectRange(int beginLine, int endLine)
{ {
QTextCursor tc = m_tc; QTextCursor tc = m_tc;
tc.setPosition(positionForLine(beginLine), MoveAnchor); tc.setPosition(positionForLine(beginLine), MoveAnchor);
if (endLine == linesInDocument()) {
tc.setPosition(positionForLine(endLine), KeepAnchor);
tc.movePosition(EndOfLine, KeepAnchor);
} else {
tc.setPosition(positionForLine(endLine + 1), KeepAnchor); tc.setPosition(positionForLine(endLine + 1), KeepAnchor);
}
return tc; return tc;
} }
@@ -949,11 +963,13 @@ void FakeVimHandler::Private::handleExCommand(const QString &cmd0)
m_registers[reg.at(0).unicode()] = tc.selection().toPlainText(); m_registers[reg.at(0).unicode()] = tc.selection().toPlainText();
tc.removeSelectedText(); tc.removeSelectedText();
} else if (reWrite.indexIn(cmd) != -1) { // :w } else if (reWrite.indexIn(cmd) != -1) { // :w
enterCommandMode();
bool noArgs = (beginLine == -1); bool noArgs = (beginLine == -1);
if (beginLine == -1) if (beginLine == -1)
beginLine = 0; beginLine = 0;
if (endLine == -1) if (endLine == -1)
endLine = linesInDocument(); endLine = linesInDocument();
qDebug() << "LINES: " << beginLine << endLine;
bool forced = cmd.startsWith("w!"); bool forced = cmd.startsWith("w!");
QString fileName = reWrite.cap(2); QString fileName = reWrite.cap(2);
if (fileName.isEmpty()) if (fileName.isEmpty())
@@ -961,21 +977,21 @@ void FakeVimHandler::Private::handleExCommand(const QString &cmd0)
QFile file(fileName); QFile file(fileName);
bool exists = file.exists(); bool exists = file.exists();
if (exists && !forced && !noArgs) { if (exists && !forced && !noArgs) {
showMessage("E13: File exists (add ! to override)"); showMessage(tr("File '%1' exists (add ! to override)").arg(fileName));
} else { } else if (file.open(QIODevice::ReadWrite)) {
file.open(QIODevice::ReadWrite);
QTextCursor tc = selectRange(beginLine, endLine); QTextCursor tc = selectRange(beginLine, endLine);
QString text = tc.selection().toPlainText(); qDebug() << "ANCHOR: " << tc.position() << tc.anchor()
QTextStream ts(&file); << tc.selection().toPlainText();
ts << text; { QTextStream ts(&file); ts << tc.selection().toPlainText(); }
file.close(); file.close();
file.open(QIODevice::ReadWrite); file.open(QIODevice::ReadOnly);
QByteArray ba = file.readAll(); QByteArray ba = file.readAll();
m_commandBuffer = QString("\"%1\" %2 %3L, %4C written") m_commandBuffer = QString("\"%1\" %2 %3L, %4C written")
.arg(fileName).arg(exists ? " " : " [New] ") .arg(fileName).arg(exists ? " " : " [New] ")
.arg(ba.count('\n')).arg(ba.size()); .arg(ba.count('\n')).arg(ba.size());
enterCommandMode();
updateMiniBuffer(); updateMiniBuffer();
} else {
showMessage(tr("Cannot open file '%1' for reading").arg(fileName));
} }
} else if (cmd.startsWith("r ")) { // :r } else if (cmd.startsWith("r ")) { // :r
m_currentFileName = cmd.mid(2); m_currentFileName = cmd.mid(2);