add notion of currentFileName to support ':w' without file name

This commit is contained in:
hjk
2008-12-28 01:02:54 +01:00
committed by Roberto Raggi
parent 87aa0759b2
commit 8beff13ef6

View File

@@ -168,6 +168,8 @@ public:
int m_gflag; // whether current command started with 'g' int m_gflag; // whether current command started with 'g'
QString m_commandBuffer; QString m_commandBuffer;
QString m_currentFileName;
QString m_currentMessage;
bool m_lastSearchForward; bool m_lastSearchForward;
QString m_lastInsertion; QString m_lastInsertion;
@@ -274,16 +276,23 @@ void FakeVimHandler::Private::finishMovement()
void FakeVimHandler::Private::updateMiniBuffer() void FakeVimHandler::Private::updateMiniBuffer()
{ {
if (m_tc.isNull())
return;
QString msg; QString msg;
msg = QChar(m_commandCode ? m_commandCode : ' '); if (m_currentMessage.isEmpty()) {
for (int i = 0; i != m_commandBuffer.size(); ++i) { msg = QChar(m_commandCode ? m_commandCode : ' ');
QChar c = m_commandBuffer.at(i); for (int i = 0; i != m_commandBuffer.size(); ++i) {
if (c.unicode() < 32) { QChar c = m_commandBuffer.at(i);
msg += '^'; if (c.unicode() < 32) {
msg += QChar(c.unicode() + 64); msg += '^';
} else { msg += QChar(c.unicode() + 64);
msg += c; } else {
msg += c;
}
} }
} else {
msg = m_currentMessage;
m_currentMessage.clear();
} }
int l = cursorLineInDocument(); int l = cursorLineInDocument();
int w = columnsOnScreen(); int w = columnsOnScreen();
@@ -299,7 +308,8 @@ void FakeVimHandler::Private::updateMiniBuffer()
void FakeVimHandler::Private::showMessage(const QString &msg) void FakeVimHandler::Private::showMessage(const QString &msg)
{ {
m_commandBuffer = msg; //qDebug() << "MSG: " << msg;
m_currentMessage = msg;
updateMiniBuffer(); updateMiniBuffer();
} }
@@ -609,7 +619,6 @@ void FakeVimHandler::Private::handleExMode(int key, const QString &text)
m_commandCode = 0; m_commandCode = 0;
} }
m_mode = CommandMode; m_mode = CommandMode;
updateMiniBuffer();
} else if (key == Key_Return && isSearchCommand()) { } else if (key == Key_Return && isSearchCommand()) {
if (!m_commandBuffer.isEmpty()) { if (!m_commandBuffer.isEmpty()) {
m_searchHistory.takeLast(); m_searchHistory.takeLast();
@@ -621,26 +630,30 @@ void FakeVimHandler::Private::handleExMode(int key, const QString &text)
} }
m_mode = CommandMode; m_mode = CommandMode;
updateMiniBuffer(); updateMiniBuffer();
} else if (key == Key_Up) { } else if (key == Key_Up && isSearchCommand()) {
if (isSearchCommand() && m_searchHistoryIndex > 0) { if (m_searchHistoryIndex > 0) {
--m_searchHistoryIndex; --m_searchHistoryIndex;
m_commandBuffer = m_searchHistory.at(m_searchHistoryIndex); m_commandBuffer = m_searchHistory.at(m_searchHistoryIndex);
} else if (m_commandCode == ':' && m_commandHistoryIndex > 0) { updateMiniBuffer();
}
} else if (key == Key_Up && m_commandCode == ':') {
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) { if (m_searchHistoryIndex < m_searchHistory.size() - 1) {
if (isSearchCommand()
&& m_searchHistoryIndex < m_searchHistory.size() - 1) {
++m_searchHistoryIndex; ++m_searchHistoryIndex;
m_commandBuffer = m_searchHistory.at(m_searchHistoryIndex); m_commandBuffer = m_searchHistory.at(m_searchHistoryIndex);
} else if (m_commandCode == ':' updateMiniBuffer();
&& m_commandHistoryIndex < m_commandHistory.size() - 1) { }
} else if (key == Key_Down && m_commandCode == ':') {
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);
updateMiniBuffer();
} }
updateMiniBuffer();
} else if (key == Key_Tab) { } else if (key == Key_Tab) {
m_commandBuffer += QChar(9); m_commandBuffer += QChar(9);
updateMiniBuffer(); updateMiniBuffer();
@@ -653,18 +666,23 @@ void FakeVimHandler::Private::handleExMode(int key, const QString &text)
void FakeVimHandler::Private::handleCommand(const QString &cmd) void FakeVimHandler::Private::handleCommand(const QString &cmd)
{ {
static QRegExp reGoto("^(\\d+)$"); static QRegExp reGoto("^(\\d+)$");
static QRegExp reWrite("^w!?( (.*))?$");
if (reGoto.indexIn(cmd) != -1) { if (reGoto.indexIn(cmd) != -1) {
int n = reGoto.cap(1).toInt(); int n = reGoto.cap(1).toInt();
m_tc.setPosition(m_tc.block().document() m_tc.setPosition(m_tc.block().document()
->findBlockByNumber(n - 1).position()); ->findBlockByNumber(n - 1).position());
showMessage(QString());
} else if (cmd == "q!" || cmd == "q") { } else if (cmd == "q!" || cmd == "q") {
if (m_textedit) if (m_textedit)
q->quitRequested(m_textedit); q->quitRequested(m_textedit);
else else
q->quitRequested(m_plaintextedit); q->quitRequested(m_plaintextedit);
} else if (cmd.startsWith("w ") || cmd.startsWith("w! ")) { showMessage(QString());
bool forced = cmd.startsWith("w! "); } else if (reWrite.indexIn(cmd) != -1) {
QString fileName = cmd.mid(forced ? 3 : 2); bool forced = cmd.startsWith("w!");
QString fileName = reWrite.cap(2);
if (fileName.isEmpty())
fileName = m_currentFileName;
QFile file(fileName); QFile file(fileName);
bool exists = file.exists(); bool exists = file.exists();
if (exists && !forced) { if (exists && !forced) {
@@ -681,11 +699,12 @@ void FakeVimHandler::Private::handleCommand(const QString &cmd)
.arg(ba.count('\n')).arg(ba.size())); .arg(ba.count('\n')).arg(ba.size()));
} }
} else if (cmd.startsWith("r ")) { } else if (cmd.startsWith("r ")) {
QString fileName = cmd.mid(2); m_currentFileName = cmd.mid(2);
QFile file(fileName); QFile file(m_currentFileName);
file.open(QIODevice::ReadOnly); file.open(QIODevice::ReadOnly);
QTextStream ts(&file); QTextStream ts(&file);
EDITOR(setPlainText(ts.readAll())); EDITOR(setPlainText(ts.readAll()));
showMessage(QString());
} }
} }