2012-10-02 09:12:39 +02:00
|
|
|
/****************************************************************************
|
2011-02-18 14:48:16 +01:00
|
|
|
**
|
2016-01-15 14:55:33 +01:00
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
2011-02-18 14:48:16 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** This file is part of Qt Creator.
|
2011-02-18 14:48:16 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** Commercial License Usage
|
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
2016-01-15 14:55:33 +01:00
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
|
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
2011-02-18 14:48:16 +01:00
|
|
|
**
|
2016-01-15 14:55:33 +01:00
|
|
|
** GNU General Public License Usage
|
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU
|
|
|
|
|
** General Public License version 3 as published by the Free Software
|
|
|
|
|
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
|
|
|
|
** included in the packaging of this file. Please review the following
|
|
|
|
|
** information to ensure the GNU General Public License requirements will
|
|
|
|
|
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
2011-02-18 14:48:16 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
****************************************************************************/
|
2009-01-06 12:20:45 +01:00
|
|
|
|
2009-01-13 13:47:00 +01:00
|
|
|
#include "fakevimhandler.h"
|
2009-01-06 12:20:45 +01:00
|
|
|
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QApplication>
|
2013-03-08 17:50:16 +01:00
|
|
|
#include <QFontMetrics>
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QMainWindow>
|
|
|
|
|
#include <QMessageBox>
|
2013-03-08 17:50:16 +01:00
|
|
|
#include <QPainter>
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QPlainTextEdit>
|
|
|
|
|
#include <QStatusBar>
|
|
|
|
|
#include <QTextEdit>
|
2009-01-06 12:20:45 +01:00
|
|
|
|
|
|
|
|
using namespace FakeVim::Internal;
|
|
|
|
|
|
2013-03-08 17:50:16 +01:00
|
|
|
/**
|
|
|
|
|
* Simple editor widget.
|
|
|
|
|
* @tparam TextEdit QTextEdit or QPlainTextEdit as base class
|
|
|
|
|
*/
|
|
|
|
|
template <typename TextEdit>
|
|
|
|
|
class Editor : public TextEdit
|
|
|
|
|
{
|
|
|
|
|
public:
|
2017-01-23 18:34:17 +01:00
|
|
|
Editor()
|
2013-03-08 17:50:16 +01:00
|
|
|
{
|
|
|
|
|
TextEdit::setCursorWidth(0);
|
2017-01-23 18:34:17 +01:00
|
|
|
TextEdit::setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
2013-03-08 17:50:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void paintEvent(QPaintEvent *e)
|
|
|
|
|
{
|
|
|
|
|
TextEdit::paintEvent(e);
|
|
|
|
|
|
|
|
|
|
// Draw text cursor.
|
|
|
|
|
QRect rect = TextEdit::cursorRect();
|
|
|
|
|
if ( e->rect().contains(rect) ) {
|
|
|
|
|
QPainter painter(TextEdit::viewport());
|
|
|
|
|
|
|
|
|
|
if ( TextEdit::overwriteMode() ) {
|
|
|
|
|
QFontMetrics fm(TextEdit::font());
|
2017-01-23 18:34:17 +01:00
|
|
|
rect.setWidth(fm.width('m'));
|
2013-03-08 17:50:16 +01:00
|
|
|
painter.setPen(Qt::NoPen);
|
|
|
|
|
painter.setBrush(TextEdit::palette().color(QPalette::Base));
|
|
|
|
|
painter.setCompositionMode(QPainter::CompositionMode_Difference);
|
|
|
|
|
} else {
|
|
|
|
|
rect.setWidth(TextEdit::cursorWidth());
|
|
|
|
|
painter.setPen(TextEdit::palette().color(QPalette::Text));
|
|
|
|
|
}
|
|
|
|
|
painter.drawRect(rect);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2009-01-13 13:47:00 +01:00
|
|
|
class Proxy : public QObject
|
|
|
|
|
{
|
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
|
|
public:
|
2017-01-23 18:34:17 +01:00
|
|
|
Proxy(QMainWindow *mw)
|
|
|
|
|
: m_mainWindow(mw)
|
2009-01-23 15:12:04 +01:00
|
|
|
{}
|
2009-01-13 13:47:00 +01:00
|
|
|
|
2017-01-23 18:34:17 +01:00
|
|
|
void changeSelection(FakeVimHandler *handler, const QList<QTextEdit::ExtraSelection> &s)
|
2009-01-13 13:47:00 +01:00
|
|
|
{
|
2017-01-23 18:34:17 +01:00
|
|
|
QWidget *widget = handler->widget();
|
|
|
|
|
if (auto ed = qobject_cast<QPlainTextEdit *>(widget))
|
2009-01-13 13:47:00 +01:00
|
|
|
ed->setExtraSelections(s);
|
2017-01-23 18:34:17 +01:00
|
|
|
else if (auto ed = qobject_cast<QTextEdit *>(widget))
|
2009-01-13 13:47:00 +01:00
|
|
|
ed->setExtraSelections(s);
|
|
|
|
|
}
|
2009-01-15 17:29:30 +01:00
|
|
|
|
2017-01-23 18:34:17 +01:00
|
|
|
void changeStatusData(FakeVimHandler *, const QString &info)
|
2009-01-27 14:08:17 +01:00
|
|
|
{
|
|
|
|
|
m_statusData = info;
|
|
|
|
|
updateStatusBar();
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-23 18:34:17 +01:00
|
|
|
void highlightMatches(FakeVimHandler *handler, const QString &pattern)
|
2013-03-08 17:50:16 +01:00
|
|
|
{
|
2017-01-23 18:34:17 +01:00
|
|
|
QWidget *widget = handler->widget();
|
|
|
|
|
auto ed = qobject_cast<QTextEdit *>(widget);
|
2013-03-08 17:50:16 +01:00
|
|
|
if (!ed)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
// Clear previous highlights.
|
|
|
|
|
ed->selectAll();
|
|
|
|
|
QTextCursor cur = ed->textCursor();
|
|
|
|
|
QTextCharFormat fmt = cur.charFormat();
|
|
|
|
|
fmt.setBackground(Qt::transparent);
|
|
|
|
|
cur.setCharFormat(fmt);
|
|
|
|
|
|
|
|
|
|
// Highlight matches.
|
|
|
|
|
QTextDocument *doc = ed->document();
|
|
|
|
|
QRegExp re(pattern);
|
|
|
|
|
cur = doc->find(re);
|
|
|
|
|
|
|
|
|
|
int a = cur.position();
|
|
|
|
|
while ( !cur.isNull() ) {
|
|
|
|
|
if ( cur.hasSelection() ) {
|
|
|
|
|
fmt.setBackground(Qt::yellow);
|
|
|
|
|
cur.setCharFormat(fmt);
|
|
|
|
|
} else {
|
|
|
|
|
cur.movePosition(QTextCursor::NextCharacter);
|
|
|
|
|
}
|
|
|
|
|
cur = doc->find(re, cur);
|
|
|
|
|
int b = cur.position();
|
|
|
|
|
if (a == b) {
|
|
|
|
|
cur.movePosition(QTextCursor::NextCharacter);
|
|
|
|
|
cur = doc->find(re, cur);
|
|
|
|
|
b = cur.position();
|
|
|
|
|
if (a == b) break;
|
|
|
|
|
}
|
|
|
|
|
a = b;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-23 18:34:17 +01:00
|
|
|
void changeStatusMessage(FakeVimHandler *, const QString &contents, int cursorPos)
|
2009-01-27 14:08:17 +01:00
|
|
|
{
|
2011-08-05 12:36:15 +02:00
|
|
|
m_statusMessage = cursorPos == -1 ? contents
|
|
|
|
|
: contents.left(cursorPos) + QChar(10073) + contents.mid(cursorPos);
|
2009-01-27 14:08:17 +01:00
|
|
|
updateStatusBar();
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-23 18:34:17 +01:00
|
|
|
void changeExtraInformation(FakeVimHandler *handler, const QString &info)
|
2009-01-15 17:29:30 +01:00
|
|
|
{
|
2017-01-23 18:34:17 +01:00
|
|
|
QMessageBox::information(handler->widget(), tr("Information"), info);
|
2009-01-15 17:29:30 +01:00
|
|
|
}
|
2010-01-29 21:33:57 +01:00
|
|
|
|
2009-01-27 14:08:17 +01:00
|
|
|
void updateStatusBar()
|
|
|
|
|
{
|
|
|
|
|
int slack = 80 - m_statusMessage.size() - m_statusData.size();
|
2017-01-23 18:34:17 +01:00
|
|
|
QString msg = m_statusMessage + QString(slack, ' ') + m_statusData;
|
2009-01-27 14:08:17 +01:00
|
|
|
m_mainWindow->statusBar()->showMessage(msg);
|
|
|
|
|
}
|
2009-01-23 15:12:04 +01:00
|
|
|
|
2017-01-23 18:34:17 +01:00
|
|
|
void handleExCommand(FakeVimHandler *, bool *handled, const ExCommand &cmd)
|
2013-03-08 17:50:16 +01:00
|
|
|
{
|
2017-01-23 18:34:17 +01:00
|
|
|
if (cmd.matches("q", "quit") || cmd.matches("qa", "qall")) {
|
2013-03-08 17:50:16 +01:00
|
|
|
QApplication::quit();
|
2017-01-23 18:34:17 +01:00
|
|
|
*handled = true;
|
2013-03-08 17:50:16 +01:00
|
|
|
} else {
|
|
|
|
|
*handled = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-01-23 15:12:04 +01:00
|
|
|
private:
|
2009-01-27 14:08:17 +01:00
|
|
|
QMainWindow *m_mainWindow;
|
|
|
|
|
QString m_statusMessage;
|
|
|
|
|
QString m_statusData;
|
2009-01-13 13:47:00 +01:00
|
|
|
};
|
|
|
|
|
|
2013-03-08 17:50:16 +01:00
|
|
|
QWidget *createEditorWidget(bool usePlainTextEdit)
|
2009-01-06 12:20:45 +01:00
|
|
|
{
|
2013-03-08 17:50:16 +01:00
|
|
|
QWidget *editor = 0;
|
2017-01-23 18:34:17 +01:00
|
|
|
if (usePlainTextEdit)
|
|
|
|
|
editor = new Editor<QPlainTextEdit>;
|
|
|
|
|
else
|
|
|
|
|
editor = new Editor<QTextEdit>;
|
|
|
|
|
editor->setObjectName("Editor");
|
2013-03-08 17:50:16 +01:00
|
|
|
editor->setFocus();
|
|
|
|
|
|
|
|
|
|
return editor;
|
|
|
|
|
}
|
2009-01-06 12:20:45 +01:00
|
|
|
|
2013-03-08 17:50:16 +01:00
|
|
|
void initHandler(FakeVimHandler &handler)
|
|
|
|
|
{
|
|
|
|
|
// Set some Vim options.
|
2017-01-23 18:34:17 +01:00
|
|
|
handler.handleCommand("set expandtab");
|
|
|
|
|
handler.handleCommand("set shiftwidth=8");
|
|
|
|
|
handler.handleCommand("set tabstop=16");
|
|
|
|
|
handler.handleCommand("set autoindent");
|
2009-01-06 13:03:59 +01:00
|
|
|
|
2013-03-08 17:50:16 +01:00
|
|
|
// Try to source file "fakevimrc" from current directory.
|
2017-01-23 18:34:17 +01:00
|
|
|
handler.handleCommand("source fakevimrc");
|
2013-03-08 17:50:16 +01:00
|
|
|
|
|
|
|
|
handler.installEventFilter();
|
|
|
|
|
handler.setupWidget();
|
|
|
|
|
}
|
2009-01-06 12:20:45 +01:00
|
|
|
|
2013-03-08 17:50:16 +01:00
|
|
|
void initMainWindow(QMainWindow &mainWindow, QWidget *centralWidget, const QString &title)
|
|
|
|
|
{
|
2017-01-23 18:34:17 +01:00
|
|
|
mainWindow.setWindowTitle(QString("FakeVim (%1)").arg(title));
|
2013-03-08 17:50:16 +01:00
|
|
|
mainWindow.setCentralWidget(centralWidget);
|
|
|
|
|
mainWindow.resize(600, 650);
|
|
|
|
|
mainWindow.move(0, 0);
|
|
|
|
|
mainWindow.show();
|
2010-01-29 21:33:57 +01:00
|
|
|
|
2013-03-08 17:50:16 +01:00
|
|
|
// Set monospace font for editor and status bar.
|
|
|
|
|
QFont font = QApplication::font();
|
2017-01-23 18:34:17 +01:00
|
|
|
font.setFamily("Monospace");
|
2013-03-08 17:50:16 +01:00
|
|
|
centralWidget->setFont(font);
|
|
|
|
|
mainWindow.statusBar()->setFont(font);
|
|
|
|
|
}
|
2009-01-06 13:03:59 +01:00
|
|
|
|
2013-03-08 17:50:16 +01:00
|
|
|
void readFile(FakeVimHandler &handler, const QString &editFileName)
|
|
|
|
|
{
|
2017-01-23 18:34:17 +01:00
|
|
|
handler.handleCommand("r " + editFileName);
|
2013-03-08 17:50:16 +01:00
|
|
|
}
|
2009-01-06 12:20:45 +01:00
|
|
|
|
2013-03-08 17:50:16 +01:00
|
|
|
void connectSignals(FakeVimHandler &handler, Proxy &proxy)
|
|
|
|
|
{
|
2016-06-27 22:34:13 +03:00
|
|
|
QObject::connect(&handler, &FakeVimHandler::commandBufferChanged,
|
|
|
|
|
&proxy, &Proxy::changeStatusMessage);
|
|
|
|
|
QObject::connect(&handler, &FakeVimHandler::selectionChanged,
|
|
|
|
|
&proxy, &Proxy::changeSelection);
|
|
|
|
|
QObject::connect(&handler, &FakeVimHandler::extraInformationChanged,
|
|
|
|
|
&proxy, &Proxy::changeExtraInformation);
|
|
|
|
|
QObject::connect(&handler, &FakeVimHandler::statusDataChanged,
|
|
|
|
|
&proxy, &Proxy::changeStatusData);
|
|
|
|
|
QObject::connect(&handler, &FakeVimHandler::highlightMatches,
|
|
|
|
|
&proxy, &Proxy::highlightMatches);
|
|
|
|
|
QObject::connect(&handler, &FakeVimHandler::handleExCommandRequested,
|
|
|
|
|
&proxy, &Proxy::handleExCommand);
|
2013-03-08 17:50:16 +01:00
|
|
|
}
|
2009-01-06 12:20:45 +01:00
|
|
|
|
2013-03-08 17:50:16 +01:00
|
|
|
int main(int argc, char *argv[])
|
|
|
|
|
{
|
|
|
|
|
QApplication app(argc, argv);
|
2009-04-01 15:11:26 +02:00
|
|
|
|
2013-03-08 17:50:16 +01:00
|
|
|
QStringList args = app.arguments();
|
|
|
|
|
|
|
|
|
|
// If first argument is present use QPlainTextEdit instead on QTextEdit;
|
|
|
|
|
bool usePlainTextEdit = args.size() > 1;
|
|
|
|
|
// Second argument is path to file to edit.
|
2017-01-23 18:34:17 +01:00
|
|
|
const QString editFileName = args.value(2, "/usr/share/vim/vim73/tutor/tutor");
|
2013-03-08 17:50:16 +01:00
|
|
|
|
|
|
|
|
// Create editor widget.
|
|
|
|
|
QWidget *editor = createEditorWidget(usePlainTextEdit);
|
|
|
|
|
|
|
|
|
|
// Create FakeVimHandler instance which will emulate Vim behavior in editor widget.
|
2017-01-23 18:34:17 +01:00
|
|
|
FakeVimHandler handler(editor, nullptr);
|
2013-03-08 17:50:16 +01:00
|
|
|
|
|
|
|
|
// Create main window.
|
|
|
|
|
QMainWindow mainWindow;
|
2017-01-23 18:34:17 +01:00
|
|
|
initMainWindow(mainWindow, editor, usePlainTextEdit ? "QPlainTextEdit" : "QTextEdit");
|
2013-03-08 17:50:16 +01:00
|
|
|
|
|
|
|
|
// Connect slots to FakeVimHandler signals.
|
2017-01-23 18:34:17 +01:00
|
|
|
Proxy proxy(&mainWindow);
|
2013-03-08 17:50:16 +01:00
|
|
|
connectSignals(handler, proxy);
|
|
|
|
|
|
|
|
|
|
// Initialize FakeVimHandler.
|
|
|
|
|
initHandler(handler);
|
|
|
|
|
|
|
|
|
|
// Read file content to editor.
|
|
|
|
|
readFile(handler, editFileName);
|
2009-01-06 12:20:45 +01:00
|
|
|
|
|
|
|
|
return app.exec();
|
|
|
|
|
}
|
2009-01-13 13:47:00 +01:00
|
|
|
|
|
|
|
|
#include "main.moc"
|