Fixes: fakevim: keep selection when moving in and out fakevim

mode
This commit is contained in:
hjk
2009-02-05 17:06:45 +01:00
parent 0359ff60f6
commit 58e8aed6f0

View File

@@ -205,7 +205,8 @@ private:
{ return m_tc.atBlockEnd() && m_tc.block().length() > 1; } { return m_tc.atBlockEnd() && m_tc.block().length() > 1; }
int lastPositionInDocument() const; int lastPositionInDocument() const;
int positionForLine(int line) const; // 1 based line, 0 based pos int firstPositionInLine(int line) const; // 1 based line, 0 based pos
int lastPositionInLine(int line) const; // 1 based line, 0 based pos
int lineForPosition(int pos) const; // 1 based line, 0 based pos int lineForPosition(int pos) const; // 1 based line, 0 based pos
// all zero-based counting // all zero-based counting
@@ -242,8 +243,6 @@ private:
void moveLeft(int n = 1) { m_tc.movePosition(Left, MoveAnchor, n); } void moveLeft(int n = 1) { m_tc.movePosition(Left, MoveAnchor, n); }
void setAnchor() { m_anchor = m_tc.position(); } void setAnchor() { m_anchor = m_tc.position(); }
QString selectedText() const;
void handleFfTt(int key); void handleFfTt(int key);
// helper function for handleCommand. return 1 based line index. // helper function for handleCommand. return 1 based line index.
@@ -307,6 +306,8 @@ public:
void recordEndGroup(); void recordEndGroup();
int anchor() const { return m_anchor; } int anchor() const { return m_anchor; }
int position() const { return m_tc.position(); } int position() const { return m_tc.position(); }
QString selectedText() const;
void undo(); void undo();
void redo(); void redo();
@@ -442,6 +443,21 @@ void FakeVimHandler::Private::setupWidget()
} }
m_wasReadOnly = EDITOR(isReadOnly()); m_wasReadOnly = EDITOR(isReadOnly());
//EDITOR(setReadOnly(true)); //EDITOR(setReadOnly(true));
QTextCursor tc = EDITOR(textCursor());
if (tc.hasSelection()) {
int pos = tc.position();
int anc = tc.anchor();
m_marks['<'] = anc;
m_marks['>'] = pos;
m_anchor = anc;
m_visualMode = VisualCharMode;
tc.clearSelection();
EDITOR(setTextCursor(tc));
m_tc = tc; // needed in updateSelection
updateSelection();
}
showBlackMessage("vi emulation mode."); showBlackMessage("vi emulation mode.");
updateMiniBuffer(); updateMiniBuffer();
} }
@@ -452,6 +468,23 @@ void FakeVimHandler::Private::restoreWidget()
//updateMiniBuffer(); //updateMiniBuffer();
EDITOR(removeEventFilter(q)); EDITOR(removeEventFilter(q));
EDITOR(setReadOnly(m_wasReadOnly)); EDITOR(setReadOnly(m_wasReadOnly));
if (m_visualMode == VisualLineMode) {
m_tc = EDITOR(textCursor());
int beginLine = lineForPosition(m_marks['<']);
int endLine = lineForPosition(m_marks['>']);
m_tc.setPosition(firstPositionInLine(beginLine), MoveAnchor);
m_tc.setPosition(lastPositionInLine(endLine), KeepAnchor);
EDITOR(setTextCursor(m_tc));
} else if (m_visualMode == VisualCharMode) {
m_tc = EDITOR(textCursor());
m_tc.setPosition(m_marks['<'], MoveAnchor);
m_tc.setPosition(m_marks['>'], KeepAnchor);
EDITOR(setTextCursor(m_tc));
}
m_visualMode = NoVisualMode;
updateSelection();
} }
bool FakeVimHandler::Private::handleKey(int key, int unmodified, const QString &text) bool FakeVimHandler::Private::handleKey(int key, int unmodified, const QString &text)
@@ -696,21 +729,21 @@ bool FakeVimHandler::Private::handleCommandMode(int key, int unmodified,
//qDebug() << "Z_MODE " << cursorLineInDocument() << linesOnScreen(); //qDebug() << "Z_MODE " << cursorLineInDocument() << linesOnScreen();
if (key == Key_Return || key == 't') { // cursor line to top of window if (key == Key_Return || key == 't') { // cursor line to top of window
if (!m_mvcount.isEmpty()) if (!m_mvcount.isEmpty())
m_tc.setPosition(positionForLine(count())); m_tc.setPosition(firstPositionInLine(count()));
scrollToLineInDocument(cursorLineInDocument()); scrollToLineInDocument(cursorLineInDocument());
if (key == Key_Return) if (key == Key_Return)
moveToFirstNonBlankOnLine(); moveToFirstNonBlankOnLine();
finishMovement(); finishMovement();
} else if (key == '.' || key == 'z') { // cursor line to center of window } else if (key == '.' || key == 'z') { // cursor line to center of window
if (!m_mvcount.isEmpty()) if (!m_mvcount.isEmpty())
m_tc.setPosition(positionForLine(count())); m_tc.setPosition(firstPositionInLine(count()));
scrollToLineInDocument(cursorLineInDocument() - linesOnScreen() / 2); scrollToLineInDocument(cursorLineInDocument() - linesOnScreen() / 2);
if (key == '.') if (key == '.')
moveToFirstNonBlankOnLine(); moveToFirstNonBlankOnLine();
finishMovement(); finishMovement();
} else if (key == '-' || key == 'b') { // cursor line to bottom of window } else if (key == '-' || key == 'b') { // cursor line to bottom of window
if (!m_mvcount.isEmpty()) if (!m_mvcount.isEmpty())
m_tc.setPosition(positionForLine(count())); m_tc.setPosition(firstPositionInLine(count()));
scrollToLineInDocument(cursorLineInDocument() - linesOnScreen() - 1); scrollToLineInDocument(cursorLineInDocument() - linesOnScreen() - 1);
if (key == '-') if (key == '-')
moveToFirstNonBlankOnLine(); moveToFirstNonBlankOnLine();
@@ -921,7 +954,7 @@ bool FakeVimHandler::Private::handleCommandMode(int key, int unmodified,
m_gflag = true; m_gflag = true;
} else if (key == 'G') { } else if (key == 'G') {
int n = m_mvcount.isEmpty() ? linesInDocument() : count(); int n = m_mvcount.isEmpty() ? linesInDocument() : count();
m_tc.setPosition(positionForLine(n), KeepAnchor); m_tc.setPosition(firstPositionInLine(n), KeepAnchor);
if (m_config[ConfigStartOfLine] == ConfigOn) if (m_config[ConfigStartOfLine] == ConfigOn)
moveToFirstNonBlankOnLine(); moveToFirstNonBlankOnLine();
finishMovement(); finishMovement();
@@ -1413,12 +1446,12 @@ int FakeVimHandler::Private::readLineCode(QString &cmd)
void FakeVimHandler::Private::selectRange(int beginLine, int endLine) void FakeVimHandler::Private::selectRange(int beginLine, int endLine)
{ {
m_anchor = positionForLine(beginLine); m_anchor = firstPositionInLine(beginLine);
if (endLine == linesInDocument()) { if (endLine == linesInDocument()) {
m_tc.setPosition(positionForLine(endLine), MoveAnchor); m_tc.setPosition(firstPositionInLine(endLine), MoveAnchor);
m_tc.movePosition(EndOfLine, MoveAnchor); m_tc.movePosition(EndOfLine, MoveAnchor);
} else { } else {
m_tc.setPosition(positionForLine(endLine + 1), MoveAnchor); m_tc.setPosition(firstPositionInLine(endLine + 1), MoveAnchor);
} }
} }
@@ -1450,7 +1483,7 @@ void FakeVimHandler::Private::handleExCommand(const QString &cmd0)
static QRegExp reHistory("^his(tory)?( (.*))?$"); static QRegExp reHistory("^his(tory)?( (.*))?$");
if (cmd.isEmpty()) { if (cmd.isEmpty()) {
m_tc.setPosition(positionForLine(beginLine)); m_tc.setPosition(firstPositionInLine(beginLine));
showBlackMessage(QString()); showBlackMessage(QString());
} else if (cmd == "q!" || cmd == "q") { // :q } else if (cmd == "q!" || cmd == "q") { // :q
quit(); quit();
@@ -1471,7 +1504,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())
@@ -1541,7 +1574,7 @@ void FakeVimHandler::Private::handleExCommand(const QString &cmd0)
recordEndGroup(); recordEndGroup();
leaveVisualMode(); leaveVisualMode();
m_tc.setPosition(positionForLine(beginLine)); m_tc.setPosition(firstPositionInLine(beginLine));
EditOperation op; EditOperation op;
// FIXME: broken for "upward selection" // FIXME: broken for "upward selection"
op.position = m_tc.position(); op.position = m_tc.position();
@@ -1906,11 +1939,17 @@ QString FakeVimHandler::Private::selectedText() const
return tc.selection().toPlainText(); return tc.selection().toPlainText();
} }
int FakeVimHandler::Private::positionForLine(int line) const int FakeVimHandler::Private::firstPositionInLine(int line) const
{ {
return m_tc.block().document()->findBlockByNumber(line - 1).position(); return m_tc.block().document()->findBlockByNumber(line - 1).position();
} }
int FakeVimHandler::Private::lastPositionInLine(int line) const
{
QTextBlock block = m_tc.block().document()->findBlockByNumber(line - 1);
return block.position() + block.length() - 1;
}
int FakeVimHandler::Private::lineForPosition(int pos) const int FakeVimHandler::Private::lineForPosition(int pos) const
{ {
QTextCursor tc = m_tc; QTextCursor tc = m_tc;