handle ! at least in visual line mode

This commit is contained in:
hjk
2009-01-08 17:21:51 +01:00
parent ef05697fdb
commit 3f53aa16fb

View File

@@ -36,10 +36,12 @@
#include <QtCore/QDebug> #include <QtCore/QDebug>
#include <QtCore/QFile> #include <QtCore/QFile>
#include <QtCore/QObject> #include <QtCore/QObject>
#include <QtCore/QProcess>
#include <QtCore/QRegExp> #include <QtCore/QRegExp>
#include <QtCore/QTextStream> #include <QtCore/QTextStream>
#include <QtCore/QStack> #include <QtCore/QStack>
#include <QtGui/QApplication>
#include <QtGui/QKeyEvent> #include <QtGui/QKeyEvent>
#include <QtGui/QLineEdit> #include <QtGui/QLineEdit>
#include <QtGui/QPlainTextEdit> #include <QtGui/QPlainTextEdit>
@@ -90,6 +92,7 @@ enum SubMode
RegisterSubMode, RegisterSubMode,
ChangeSubMode, ChangeSubMode,
DeleteSubMode, DeleteSubMode,
FilterSubMode,
ZSubMode, ZSubMode,
}; };
@@ -223,6 +226,7 @@ private:
QString m_lastInsertion; QString m_lastInsertion;
// undo handling // undo handling
void recordOperation(const EditOperation &op);
void recordInsert(int position, const QString &data); void recordInsert(int position, const QString &data);
void recordRemove(int position, const QString &data); void recordRemove(int position, const QString &data);
void recordRemove(int position, int length); void recordRemove(int position, int length);
@@ -328,6 +332,18 @@ bool FakeVimHandler::Private::handleKey(int key, const QString &text)
void FakeVimHandler::Private::finishMovement(const QString &dotCommand) void FakeVimHandler::Private::finishMovement(const QString &dotCommand)
{ {
if (m_submode == FilterSubMode) {
int beginLine = lineForPosition(m_tc.anchor());
int endLine = lineForPosition(m_tc.position());
m_tc.setPosition(qMin(m_tc.anchor(), m_tc.position()), MoveAnchor);
m_mode = ExMode;
m_commandBuffer = QString(".,+%1!").arg(qAbs(endLine - beginLine));
m_commandHistory.append(QString());
m_commandHistoryIndex = m_commandHistory.size() - 1;
updateMiniBuffer();
return;
}
if (m_submode == ChangeSubMode) { if (m_submode == ChangeSubMode) {
if (!dotCommand.isEmpty()) if (!dotCommand.isEmpty())
m_dotCommand = "c" + dotCommand; m_dotCommand = "c" + dotCommand;
@@ -409,12 +425,14 @@ void FakeVimHandler::Private::updateMiniBuffer()
if (!m_currentMessage.isEmpty()) { if (!m_currentMessage.isEmpty()) {
msg = m_currentMessage; msg = m_currentMessage;
m_currentMessage.clear(); m_currentMessage.clear();
} else if (m_visualMode == VisualCharMode) { } else if (m_mode == CommandMode && m_visualMode != NoVisualMode) {
if (m_visualMode == VisualCharMode) {
msg = "-- VISUAL --"; msg = "-- VISUAL --";
} else if (m_visualMode == VisualLineMode) { } else if (m_visualMode == VisualLineMode) {
msg = "-- VISUAL LINE --"; msg = "-- VISUAL LINE --";
} else if (m_visualMode == VisualBlockMode) { } else if (m_visualMode == VisualBlockMode) {
msg = "-- VISUAL BLOCK --"; msg = "-- VISUAL BLOCK --";
}
} else if (m_mode == InsertMode) { } else if (m_mode == InsertMode) {
msg = "-- INSERT --"; msg = "-- INSERT --";
} else { } else {
@@ -432,6 +450,8 @@ void FakeVimHandler::Private::updateMiniBuffer()
msg += c; msg += c;
} }
} }
if (!msg.isEmpty() && m_mode != CommandMode)
msg += '|'; // FIXME: Use a real "cursor"
} }
emit q->commandBufferChanged(msg); emit q->commandBufferChanged(msg);
@@ -533,6 +553,15 @@ bool FakeVimHandler::Private::handleCommandMode(int key, const QString &text)
m_tc.movePosition(StartOfLine, KeepAnchor); m_tc.movePosition(StartOfLine, KeepAnchor);
m_tc.movePosition(Right, KeepAnchor, qMin(count(), rightDist()) - 1); m_tc.movePosition(Right, KeepAnchor, qMin(count(), rightDist()) - 1);
finishMovement(); finishMovement();
} else if (key == '!' && m_visualMode == NoVisualMode) {
m_submode = FilterSubMode;
} else if (key == '!' && m_visualMode == VisualLineMode) {
m_mode = ExMode;
m_marks['>'] = m_tc.position();
m_commandBuffer = "'<,'>!";
m_commandHistory.append(QString());
m_commandHistoryIndex = m_commandHistory.size() - 1;
updateMiniBuffer();
} else if (key == '"') { } else if (key == '"') {
m_submode = RegisterSubMode; m_submode = RegisterSubMode;
} else if (key == Key_Return) { } else if (key == Key_Return) {
@@ -744,7 +773,7 @@ bool FakeVimHandler::Private::handleCommandMode(int key, const QString &text)
if (m_visualMode != NoVisualMode) if (m_visualMode != NoVisualMode)
leaveVisualMode(); leaveVisualMode();
} else { } else {
qDebug() << "Ignored" << key << text; qDebug() << "Ignored in command mode: " << key << text;
return false; return false;
} }
return true; return true;
@@ -818,6 +847,11 @@ bool FakeVimHandler::Private::handleMiniBufferModes(int key, const QString &text
else else
m_commandBuffer.chop(1); m_commandBuffer.chop(1);
updateMiniBuffer(); updateMiniBuffer();
} else if (key == Key_Left) {
// FIXME:
if (!m_commandBuffer.isEmpty())
m_commandBuffer.chop(1);
updateMiniBuffer();
} else if (key == Key_Return && m_mode == ExMode) { } else if (key == Key_Return && m_mode == ExMode) {
if (!m_commandBuffer.isEmpty()) { if (!m_commandBuffer.isEmpty()) {
m_commandHistory.takeLast(); m_commandHistory.takeLast();
@@ -890,9 +924,9 @@ int FakeVimHandler::Private::readLineCode(QString &cmd)
return cursorLineInDocument() + 1 + (n == -1 ? 1 : n); return cursorLineInDocument() + 1 + (n == -1 ? 1 : n);
} }
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(), -1);
qDebug() << " MARK: " << cmd.at(0) << pos << lineForPosition(pos); //qDebug() << " MARK: " << cmd.at(0) << pos << lineForPosition(pos);
if (!pos) { if (pos == -1) {
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;
} }
@@ -935,6 +969,7 @@ void FakeVimHandler::Private::handleExCommand(const QString &cmd0)
if (cmd.startsWith("%")) if (cmd.startsWith("%"))
cmd = "1,$" + cmd.mid(1); cmd = "1,$" + cmd.mid(1);
m_marks['>'] = m_tc.position();
int beginLine = -1; int beginLine = -1;
int endLine = -1; int endLine = -1;
@@ -949,7 +984,7 @@ void FakeVimHandler::Private::handleExCommand(const QString &cmd0)
endLine = line; endLine = line;
} }
qDebug() << "RANGE: " << beginLine << endLine << cmd << cmd0; //qDebug() << "RANGE: " << beginLine << endLine << cmd << cmd0;
static QRegExp reWrite("^w!?( (.*))?$"); static QRegExp reWrite("^w!?( (.*))?$");
static QRegExp reDelete("^d( (.*))?$"); static QRegExp reDelete("^d( (.*))?$");
@@ -1011,8 +1046,41 @@ void FakeVimHandler::Private::handleExCommand(const QString &cmd0)
.arg(m_currentFileName).arg(data.count('\n')).arg(data.size()); .arg(m_currentFileName).arg(data.count('\n')).arg(data.size());
enterCommandMode(); enterCommandMode();
updateMiniBuffer(); updateMiniBuffer();
} else if (cmd.startsWith("!")) {
if (beginLine == -1)
beginLine = cursorLineInDocument();
if (endLine == -1)
endLine = cursorLineInDocument();
QTextCursor tc = selectRange(beginLine, endLine);
QString text = tc.selection().toPlainText();
tc.removeSelectedText();
QString command = cmd.mid(1).trimmed();
QProcess proc;
proc.start(cmd.mid(1));
proc.waitForStarted();
proc.write(text.toUtf8());
proc.closeWriteChannel();
proc.waitForFinished();
QString result = QString::fromUtf8(proc.readAllStandardOutput());
m_tc.insertText(result);
leaveVisualMode();
m_tc.setPosition(positionForLine(beginLine));
EditOperation op;
op.m_position = m_tc.position();
op.m_from = text;
op.m_to = result;
recordOperation(op);
m_commandBuffer = tr("%1 lines filtered").arg(text.count('\n'));
enterCommandMode();
updateMiniBuffer();
} else if (cmd == "red" || cmd == "redo") { // :redo
redo();
enterCommandMode();
updateMiniBuffer();
} else { } else {
showMessage("E492: Not an editor command: " + cmd0); showMessage("E492: Not an editor command: " + cmd0 + "(" + cmd + ")");
} }
} }
@@ -1303,13 +1371,18 @@ void FakeVimHandler::Private::redo()
#endif #endif
} }
void FakeVimHandler::Private::recordOperation(const EditOperation &op)
{
m_undoStack.push(op);
m_redoStack.clear();
}
void FakeVimHandler::Private::recordMove(int position, int nestedCount) void FakeVimHandler::Private::recordMove(int position, int nestedCount)
{ {
EditOperation op; EditOperation op;
op.m_position = position; op.m_position = position;
op.m_itemCount = nestedCount; op.m_itemCount = nestedCount;
m_undoStack.push(op); recordOperation(op);
m_redoStack.clear();
} }
void FakeVimHandler::Private::recordInsert(int position, const QString &data) void FakeVimHandler::Private::recordInsert(int position, const QString &data)
@@ -1317,8 +1390,7 @@ void FakeVimHandler::Private::recordInsert(int position, const QString &data)
EditOperation op; EditOperation op;
op.m_position = position; op.m_position = position;
op.m_to = data; op.m_to = data;
m_undoStack.push(op); recordOperation(op);
m_redoStack.clear();
} }
void FakeVimHandler::Private::recordRemove(int position, int length) void FakeVimHandler::Private::recordRemove(int position, int length)
@@ -1334,8 +1406,7 @@ void FakeVimHandler::Private::recordRemove(int position, const QString &data)
EditOperation op; EditOperation op;
op.m_position = position; op.m_position = position;
op.m_from = data; op.m_from = data;
m_undoStack.push(op); recordOperation(op);
m_redoStack.clear();
} }
void FakeVimHandler::Private::enterInsertMode() void FakeVimHandler::Private::enterInsertMode()
@@ -1411,7 +1482,6 @@ void FakeVimHandler::handleCommand(QWidget *widget, const QString &cmd)
d->handleExCommand(cmd); d->handleExCommand(cmd);
} }
void FakeVimHandler::quit() void FakeVimHandler::quit()
{ {
d->quit(); d->quit();