fakevim: fix 'Y'

This commit is contained in:
hjk
2009-01-16 17:38:15 +01:00
parent cb807286be
commit 62c2688ce6

View File

@@ -132,6 +132,7 @@ enum MoveType
{ {
MoveExclusive, MoveExclusive,
MoveInclusive, MoveInclusive,
MoveLineWise,
}; };
struct EditOperation struct EditOperation
@@ -219,19 +220,19 @@ private:
typedef QTextCursor::MoveOperation MoveOperation; typedef QTextCursor::MoveOperation MoveOperation;
typedef QTextCursor::MoveMode MoveMode; typedef QTextCursor::MoveMode MoveMode;
void moveToEndOfDocument() void moveToEndOfDocument()
{ m_tc.movePosition(QTextCursor::End, MoveAnchor); } { m_tc.movePosition(QTextCursor::End, QTextCursor::MoveAnchor); }
void moveToStartOfLine() void moveToStartOfLine()
{ m_tc.movePosition(QTextCursor::StartOfLine, MoveAnchor); } { m_tc.movePosition(QTextCursor::StartOfLine, QTextCursor::MoveAnchor); }
void moveToEndOfLine() void moveToEndOfLine()
{ m_tc.movePosition(QTextCursor::EndOfLine, MoveAnchor); } { m_tc.movePosition(QTextCursor::EndOfLine, QTextCursor::MoveAnchor); }
void moveUp(int n = 1) void moveUp(int n = 1)
{ m_tc.movePosition(QTextCursor::Up, MoveAnchor, n); } { m_tc.movePosition(QTextCursor::Up, QTextCursor::MoveAnchor, n); }
void moveDown(int n = 1) void moveDown(int n = 1)
{ m_tc.movePosition(QTextCursor::Down, MoveAnchor, n); } { m_tc.movePosition(QTextCursor::Down, QTextCursor::MoveAnchor, n); }
void moveRight(int n = 1) void moveRight(int n = 1)
{ m_tc.movePosition(QTextCursor::Right, MoveAnchor, n); } { m_tc.movePosition(QTextCursor::Right, QTextCursor::MoveAnchor, n); }
void moveLeft(int n = 1) void moveLeft(int n = 1)
{ m_tc.movePosition(QTextCursor::Left, MoveAnchor, n); } { m_tc.movePosition(QTextCursor::Left, QTextCursor::MoveAnchor, n); }
void setAnchor() { m_anchor = m_tc.position(); } void setAnchor() { m_anchor = m_tc.position(); }
QString selectedText() const; QString selectedText() const;
@@ -331,7 +332,7 @@ public:
QHash<QString, QString> m_config; QHash<QString, QString> m_config;
// for restoring cursor position // for restoring cursor position
int m_savedPosition; int m_savedYankPosition;
int m_desiredColumn; int m_desiredColumn;
}; };
@@ -351,6 +352,8 @@ FakeVimHandler::Private::Private(FakeVimHandler *parent)
m_visualMode = NoVisualMode; m_visualMode = NoVisualMode;
m_desiredColumn = 0; m_desiredColumn = 0;
m_moveType = MoveInclusive; m_moveType = MoveInclusive;
m_anchor = 0;
m_savedYankPosition = 0;
m_config[ConfigStartOfLine] = ConfigOn; m_config[ConfigStartOfLine] = ConfigOn;
m_config[ConfigTabStop] = "8"; m_config[ConfigTabStop] = "8";
@@ -383,7 +386,7 @@ bool FakeVimHandler::Private::handleEvent(QKeyEvent *ev)
m_tc.setVisualNavigation(true); m_tc.setVisualNavigation(true);
if (m_fakeEnd) if (m_fakeEnd)
moveRight(MoveAnchor); moveRight();
if ((ev->modifiers() & Qt::ControlModifier) != 0) { if ((ev->modifiers() & Qt::ControlModifier) != 0) {
key += 256; key += 256;
@@ -398,7 +401,7 @@ bool FakeVimHandler::Private::handleEvent(QKeyEvent *ev)
m_fakeEnd = (atEol() && m_mode == CommandMode); m_fakeEnd = (atEol() && m_mode == CommandMode);
if (m_fakeEnd) if (m_fakeEnd)
moveLeft(MoveAnchor); moveLeft();
EDITOR(setTextCursor(m_tc)); EDITOR(setTextCursor(m_tc));
EDITOR(ensureCursorVisible()); EDITOR(ensureCursorVisible());
@@ -409,7 +412,6 @@ bool FakeVimHandler::Private::handleKey(int key, int unmodified, const QString &
{ {
//qDebug() << "KEY: " << key << text << "POS: " << m_tc.position(); //qDebug() << "KEY: " << key << text << "POS: " << m_tc.position();
//qDebug() << "\nUNDO: " << m_undoStack << "\nREDO: " << m_redoStack; //qDebug() << "\nUNDO: " << m_undoStack << "\nREDO: " << m_redoStack;
m_savedPosition = m_tc.position();
if (m_mode == InsertMode) if (m_mode == InsertMode)
return handleInsertMode(key, unmodified, text); return handleInsertMode(key, unmodified, text);
if (m_mode == CommandMode) if (m_mode == CommandMode)
@@ -452,10 +454,10 @@ void FakeVimHandler::Private::finishMovement(const QString &dotCommand)
recordEndGroup(); recordEndGroup();
m_submode = NoSubMode; m_submode = NoSubMode;
if (atEol()) if (atEol())
moveLeft(MoveAnchor); moveLeft();
} else if (m_submode == YankSubMode) { } else if (m_submode == YankSubMode) {
m_registers[m_register] = m_tc.selectedText(); m_registers[m_register] = selectedText();
m_tc.setPosition(m_savedPosition); m_tc.setPosition(m_savedYankPosition);
m_submode = NoSubMode; m_submode = NoSubMode;
} else if (m_submode == ReplaceSubMode) { } else if (m_submode == ReplaceSubMode) {
m_submode = NoSubMode; m_submode = NoSubMode;
@@ -632,9 +634,10 @@ bool FakeVimHandler::Private::handleCommandMode(int key, int unmodified,
finishMovement("d"); finishMovement("d");
} else if (m_submode == YankSubMode && key == 'y') { } else if (m_submode == YankSubMode && key == 'y') {
moveToStartOfLine(); moveToStartOfLine();
setAnchor();
moveDown(count()); moveDown(count());
m_registers[m_register] = selectedText(); m_moveType = MoveLineWise;
finishMovement(); finishMovement("y");
} else if (m_submode == ReplaceSubMode) { } else if (m_submode == ReplaceSubMode) {
if (atEol()) if (atEol())
moveLeft(KeepAnchor); moveLeft(KeepAnchor);
@@ -664,7 +667,7 @@ bool FakeVimHandler::Private::handleCommandMode(int key, int unmodified,
} else if (m_subsubmode == BackTickSubSubMode } else if (m_subsubmode == BackTickSubSubMode
|| m_subsubmode == TickSubSubMode) { || m_subsubmode == TickSubSubMode) {
if (m_marks.contains(key)) { if (m_marks.contains(key)) {
m_tc.setPosition(m_marks[key], MoveAnchor); m_tc.setPosition(m_marks[key]);
if (m_subsubmode == TickSubSubMode) if (m_subsubmode == TickSubSubMode)
moveToFirstNonBlankOnLine(); moveToFirstNonBlankOnLine();
finishMovement(); finishMovement();
@@ -832,7 +835,7 @@ bool FakeVimHandler::Private::handleCommandMode(int key, int unmodified,
enterInsertMode(); enterInsertMode();
updateMiniBuffer(); updateMiniBuffer();
if (atEol()) if (atEol())
moveLeft(MoveAnchor); moveLeft();
} else if (key == 'I') { } else if (key == 'I') {
setAnchor(); setAnchor();
enterInsertMode(); enterInsertMode();
@@ -915,11 +918,13 @@ bool FakeVimHandler::Private::handleCommandMode(int key, int unmodified,
} else if (key == 'p' || key == 'P') { } else if (key == 'p' || key == 'P') {
recordBeginGroup(); recordBeginGroup();
QString text = m_registers[m_register]; QString text = m_registers[m_register];
int n = text.count(QChar(ParagraphSeparator)); int n = lineCount(text);
//qDebug() << "REGISTERS: " << m_registers << "MOVE: " << m_moveType;
//qDebug() << "LINES: " << n << text << m_register;
if (n > 0) { if (n > 0) {
moveToStartOfLine(); moveToStartOfLine();
if (key == 'p') if (key == 'p')
moveDown(MoveAnchor); moveDown();
recordInsertText(text); recordInsertText(text);
moveUp(n); moveUp(n);
} else { } else {
@@ -974,7 +979,7 @@ bool FakeVimHandler::Private::handleCommandMode(int key, int unmodified,
finishMovement("W"); finishMovement("W");
} else if (key == 'x') { // = "dl" } else if (key == 'x') { // = "dl"
if (atEol()) if (atEol())
moveLeft(MoveAnchor); moveLeft();
recordBeginGroup(); recordBeginGroup();
m_submode = DeleteSubMode; m_submode = DeleteSubMode;
moveRight(qMin(count(), rightDist())); moveRight(qMin(count(), rightDist()));
@@ -987,10 +992,18 @@ bool FakeVimHandler::Private::handleCommandMode(int key, int unmodified,
} }
finishMovement(); finishMovement();
} else if (key == 'y') { } else if (key == 'y') {
m_savedYankPosition = m_tc.position();
if (atEol()) if (atEol())
moveLeft(); moveLeft();
recordBeginGroup(); recordBeginGroup();
setAnchor();
m_submode = YankSubMode; m_submode = YankSubMode;
} else if (key == 'Y') {
moveToStartOfLine();
setAnchor();
moveDown(count());
m_moveType = MoveLineWise;
finishMovement();
} else if (key == 'z') { } else if (key == 'z') {
recordBeginGroup(); recordBeginGroup();
m_submode = ZSubMode; m_submode = ZSubMode;
@@ -1296,7 +1309,7 @@ void FakeVimHandler::Private::handleExCommand(const QString &cmd0)
beginLine = 0; beginLine = 0;
if (endLine == -1) if (endLine == -1)
endLine = linesInDocument(); endLine = linesInDocument();
qDebug() << "LINES: " << beginLine << endLine; //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())
@@ -1802,6 +1815,9 @@ void FakeVimHandler::Private::recordBeginGroup()
{ {
//qDebug() << "PUSH"; //qDebug() << "PUSH";
m_undoGroupStack.push(m_undoStack.size()); m_undoGroupStack.push(m_undoStack.size());
EditOperation op;
op.m_position = m_tc.position();
recordOperation(op);
} }
void FakeVimHandler::Private::recordEndGroup() void FakeVimHandler::Private::recordEndGroup()