forked from qt-creator/qt-creator
small refactoring, make ex command interface public
This commit is contained in:
@@ -34,8 +34,10 @@
|
|||||||
#include "handler.h"
|
#include "handler.h"
|
||||||
|
|
||||||
#include <QtCore/QDebug>
|
#include <QtCore/QDebug>
|
||||||
|
#include <QtCore/QFile>
|
||||||
#include <QtCore/QObject>
|
#include <QtCore/QObject>
|
||||||
#include <QtCore/QRegExp>
|
#include <QtCore/QRegExp>
|
||||||
|
#include <QtCore/QTextStream>
|
||||||
#include <QtCore/QStack>
|
#include <QtCore/QStack>
|
||||||
|
|
||||||
#include <QtGui/QKeyEvent>
|
#include <QtGui/QKeyEvent>
|
||||||
@@ -133,6 +135,7 @@ public:
|
|||||||
void moveToNextWord(bool simple);
|
void moveToNextWord(bool simple);
|
||||||
void moveToWordBoundary(bool simple, bool forward);
|
void moveToWordBoundary(bool simple, bool forward);
|
||||||
void handleFfTt(int key);
|
void handleFfTt(int key);
|
||||||
|
void handleCommand(const QString &cmd);
|
||||||
|
|
||||||
FakeVimHandler *q;
|
FakeVimHandler *q;
|
||||||
Mode m_mode;
|
Mode m_mode;
|
||||||
@@ -184,6 +187,8 @@ FakeVimHandler::Private::Private(FakeVimHandler *parent)
|
|||||||
m_lastSearchForward = true;
|
m_lastSearchForward = true;
|
||||||
m_register = '"';
|
m_register = '"';
|
||||||
m_gflag = false;
|
m_gflag = false;
|
||||||
|
m_textedit = 0;
|
||||||
|
m_plaintextedit = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool FakeVimHandler::Private::eventFilter(QObject *ob, QEvent *ev)
|
bool FakeVimHandler::Private::eventFilter(QObject *ob, QEvent *ev)
|
||||||
@@ -555,17 +560,7 @@ void FakeVimHandler::Private::handleExMode(int key, const QString &text)
|
|||||||
else
|
else
|
||||||
m_commandBuffer.chop(1);
|
m_commandBuffer.chop(1);
|
||||||
} else if (key == Key_Return && m_commandCode == ':') {
|
} else if (key == Key_Return && m_commandCode == ':') {
|
||||||
static QRegExp reGoto("^(\\d+)$");
|
handleCommand(m_commandBuffer);
|
||||||
if (reGoto.indexIn(m_commandBuffer) != -1) {
|
|
||||||
int n = reGoto.cap(1).toInt();
|
|
||||||
m_tc.setPosition(m_tc.block().document()
|
|
||||||
->findBlockByNumber(n - 1).position());
|
|
||||||
} else if (m_commandBuffer == "q!" || m_commandBuffer == "q") {
|
|
||||||
if (m_textedit)
|
|
||||||
q->quitRequested(m_textedit);
|
|
||||||
else
|
|
||||||
q->quitRequested(m_plaintextedit);
|
|
||||||
}
|
|
||||||
m_commandBuffer.clear();
|
m_commandBuffer.clear();
|
||||||
m_mode = CommandMode;
|
m_mode = CommandMode;
|
||||||
} else if (key == Key_Return && isSearchCommand()) {
|
} else if (key == Key_Return && isSearchCommand()) {
|
||||||
@@ -591,6 +586,30 @@ void FakeVimHandler::Private::handleExMode(int key, const QString &text)
|
|||||||
updateCommandBuffer();
|
updateCommandBuffer();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void FakeVimHandler::Private::handleCommand(const QString &cmd)
|
||||||
|
{
|
||||||
|
static QRegExp reGoto("^(\\d+)$");
|
||||||
|
if (reGoto.indexIn(cmd) != -1) {
|
||||||
|
int n = reGoto.cap(1).toInt();
|
||||||
|
m_tc.setPosition(m_tc.block().document()
|
||||||
|
->findBlockByNumber(n - 1).position());
|
||||||
|
} else if (cmd == "q!" || cmd == "q") {
|
||||||
|
if (m_textedit)
|
||||||
|
q->quitRequested(m_textedit);
|
||||||
|
else
|
||||||
|
q->quitRequested(m_plaintextedit);
|
||||||
|
} else if (cmd.startsWith("r ")) {
|
||||||
|
QString fileName = cmd.mid(2);
|
||||||
|
QFile file(fileName);
|
||||||
|
file.open(QIODevice::ReadOnly);
|
||||||
|
QTextStream ts(&file);
|
||||||
|
if (m_textedit)
|
||||||
|
m_textedit->setText(ts.readAll());
|
||||||
|
else if (m_plaintextedit)
|
||||||
|
m_plaintextedit->setPlainText(ts.readAll());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void FakeVimHandler::Private::search(const QString &needle, bool forward)
|
void FakeVimHandler::Private::search(const QString &needle, bool forward)
|
||||||
{
|
{
|
||||||
//qDebug() << "NEEDLE " << needle << "BACKWARDS" << backwards;
|
//qDebug() << "NEEDLE " << needle << "BACKWARDS" << backwards;
|
||||||
@@ -792,3 +811,30 @@ bool FakeVimHandler::eventFilter(QObject *ob, QEvent *ev)
|
|||||||
return d->eventFilter(ob, ev);
|
return d->eventFilter(ob, ev);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void FakeVimHandler::addWidget(QWidget *widget)
|
||||||
|
{
|
||||||
|
widget->installEventFilter(this);
|
||||||
|
QFont font = widget->font();
|
||||||
|
font.setFamily("Monospace");
|
||||||
|
widget->setFont(font);
|
||||||
|
if (QTextEdit *ed = qobject_cast<QTextEdit *>(widget)) {
|
||||||
|
ed->setCursorWidth(QFontMetrics(ed->font()).width(QChar('x')));
|
||||||
|
ed->setLineWrapMode(QTextEdit::NoWrap);
|
||||||
|
} else if (QPlainTextEdit *ed = qobject_cast<QPlainTextEdit *>(widget)) {
|
||||||
|
ed->setCursorWidth(QFontMetrics(ed->font()).width(QChar('x')));
|
||||||
|
ed->setLineWrapMode(QPlainTextEdit::NoWrap);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void FakeVimHandler::removeWidget(QWidget *widget)
|
||||||
|
{
|
||||||
|
widget->removeEventFilter(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
void FakeVimHandler::handleCommand(QWidget *widget, const QString &cmd)
|
||||||
|
{
|
||||||
|
d->m_textedit = qobject_cast<QTextEdit *>(widget);
|
||||||
|
d->m_plaintextedit = qobject_cast<QPlainTextEdit *>(widget);
|
||||||
|
d->handleCommand(cmd);
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -52,8 +52,16 @@ public:
|
|||||||
FakeVimHandler(QObject *parent = 0);
|
FakeVimHandler(QObject *parent = 0);
|
||||||
~FakeVimHandler();
|
~FakeVimHandler();
|
||||||
|
|
||||||
|
// The same handler can be installed on several widgets
|
||||||
|
void addWidget(QWidget *widget);
|
||||||
|
void removeWidget(QWidget *widget);
|
||||||
|
|
||||||
|
// This executes an "ex" style command taking context
|
||||||
|
// information from \p widget;
|
||||||
|
void handleCommand(QWidget *widget, const QString &cmd);
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
void commandBufferChanged(const QString &);
|
void commandBufferChanged(const QString &msg);
|
||||||
void quitRequested(QObject *);
|
void quitRequested(QObject *);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|||||||
Reference in New Issue
Block a user