2012-10-02 09:12:39 +02:00
|
|
|
/****************************************************************************
|
2011-02-18 14:48:16 +01:00
|
|
|
**
|
2015-01-14 18:07:15 +01:00
|
|
|
** Copyright (C) 2015 The Qt Company Ltd.
|
|
|
|
|
** Contact: http://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
|
2015-01-14 18:07:15 +01:00
|
|
|
** a written agreement between you and The Qt Company. For licensing terms and
|
|
|
|
|
** conditions see http://www.qt.io/terms-conditions. For further information
|
2014-10-01 13:21:18 +02:00
|
|
|
** use the contact form at http://www.qt.io/contact-us.
|
2011-02-18 14:48:16 +01:00
|
|
|
**
|
|
|
|
|
** GNU Lesser General Public License Usage
|
2012-10-02 09:12:39 +02:00
|
|
|
** Alternatively, this file may be used under the terms of the GNU Lesser
|
2014-10-01 13:21:18 +02:00
|
|
|
** General Public License version 2.1 or version 3 as published by the Free
|
|
|
|
|
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
|
|
|
|
|
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
|
|
|
|
|
** following information to ensure the GNU Lesser General Public License
|
|
|
|
|
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
|
|
|
|
|
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
2011-02-18 14:48:16 +01:00
|
|
|
**
|
2015-01-14 18:07:15 +01:00
|
|
|
** In addition, as a special exception, The Qt Company gives you certain additional
|
|
|
|
|
** rights. These rights are described in The Qt Company LGPL Exception
|
2011-02-18 14:48:16 +01:00
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
|
**
|
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
|
|
|
typedef QLatin1String _;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Simple editor widget.
|
|
|
|
|
* @tparam TextEdit QTextEdit or QPlainTextEdit as base class
|
|
|
|
|
*/
|
|
|
|
|
template <typename TextEdit>
|
|
|
|
|
class Editor : public TextEdit
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
Editor(QWidget *parent = 0) : TextEdit(parent)
|
|
|
|
|
{
|
|
|
|
|
TextEdit::setCursorWidth(0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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());
|
|
|
|
|
rect.setWidth(fm.width(QLatin1Char('m')));
|
|
|
|
|
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:
|
2009-01-27 14:08:17 +01:00
|
|
|
Proxy(QWidget *widget, QMainWindow *mw, QObject *parent = 0)
|
|
|
|
|
: QObject(parent), m_widget(widget), m_mainWindow(mw)
|
2009-01-23 15:12:04 +01:00
|
|
|
{}
|
2009-01-13 13:47:00 +01:00
|
|
|
|
|
|
|
|
public slots:
|
2009-01-23 15:12:04 +01:00
|
|
|
void changeSelection(const QList<QTextEdit::ExtraSelection> &s)
|
2009-01-13 13:47:00 +01:00
|
|
|
{
|
2009-01-23 16:37:32 +01:00
|
|
|
if (QPlainTextEdit *ed = qobject_cast<QPlainTextEdit *>(m_widget))
|
2009-01-13 13:47:00 +01:00
|
|
|
ed->setExtraSelections(s);
|
2009-01-23 16:37:32 +01:00
|
|
|
else if (QTextEdit *ed = qobject_cast<QTextEdit *>(m_widget))
|
2009-01-13 13:47:00 +01:00
|
|
|
ed->setExtraSelections(s);
|
|
|
|
|
}
|
2009-01-15 17:29:30 +01:00
|
|
|
|
2009-01-27 14:08:17 +01:00
|
|
|
void changeStatusData(const QString &info)
|
|
|
|
|
{
|
|
|
|
|
m_statusData = info;
|
|
|
|
|
updateStatusBar();
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-08 17:50:16 +01:00
|
|
|
void highlightMatches(const QString &pattern)
|
|
|
|
|
{
|
|
|
|
|
QTextEdit *ed = qobject_cast<QTextEdit *>(m_widget);
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-08-05 12:36:15 +02:00
|
|
|
void changeStatusMessage(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();
|
|
|
|
|
}
|
|
|
|
|
|
2009-01-23 15:12:04 +01:00
|
|
|
void changeExtraInformation(const QString &info)
|
2009-01-15 17:29:30 +01:00
|
|
|
{
|
2013-02-25 18:34:03 +01:00
|
|
|
QMessageBox::information(m_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();
|
2013-02-25 18:34:03 +01:00
|
|
|
QString msg = m_statusMessage + QString(slack, QLatin1Char(' ')) + m_statusData;
|
2009-01-27 14:08:17 +01:00
|
|
|
m_mainWindow->statusBar()->showMessage(msg);
|
|
|
|
|
}
|
2009-01-23 15:12:04 +01:00
|
|
|
|
2013-03-08 17:50:16 +01:00
|
|
|
void handleExCommand(bool *handled, const ExCommand &cmd)
|
|
|
|
|
{
|
|
|
|
|
if (cmd.matches(_("q"), _("quit")) || cmd.matches(_("qa"), _("qall"))) {
|
|
|
|
|
QApplication::quit();
|
|
|
|
|
} else {
|
|
|
|
|
*handled = false;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
*handled = true;
|
|
|
|
|
}
|
|
|
|
|
|
2009-01-23 15:12:04 +01:00
|
|
|
private:
|
|
|
|
|
QWidget *m_widget;
|
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;
|
2009-01-06 12:20:45 +01:00
|
|
|
if (usePlainTextEdit) {
|
2013-03-08 17:50:16 +01:00
|
|
|
Editor<QPlainTextEdit> *w = new Editor<QPlainTextEdit>;
|
2009-01-26 10:31:49 +01:00
|
|
|
w->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
2013-03-08 17:50:16 +01:00
|
|
|
editor = w;
|
2009-01-06 12:20:45 +01:00
|
|
|
} else {
|
2013-03-08 17:50:16 +01:00
|
|
|
Editor<QTextEdit> *w = new Editor<QTextEdit>;
|
2009-01-26 10:31:49 +01:00
|
|
|
w->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
2013-03-08 17:50:16 +01:00
|
|
|
editor = w;
|
2009-01-06 12:20:45 +01:00
|
|
|
}
|
2013-03-08 17:50:16 +01:00
|
|
|
editor->setObjectName(_("Editor"));
|
|
|
|
|
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.
|
|
|
|
|
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.
|
|
|
|
|
handler.handleCommand(_("source fakevimrc"));
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
|
mainWindow.setWindowTitle(QString(_("FakeVim (%1)")).arg(title));
|
|
|
|
|
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();
|
|
|
|
|
font.setFamily(_("Monospace"));
|
|
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
|
handler.handleCommand(QString(_("r %1")).arg(editFileName));
|
|
|
|
|
}
|
2009-01-06 12:20:45 +01:00
|
|
|
|
2013-03-08 17:50:16 +01:00
|
|
|
void connectSignals(FakeVimHandler &handler, Proxy &proxy)
|
|
|
|
|
{
|
|
|
|
|
QObject::connect(&handler, SIGNAL(commandBufferChanged(QString,int,int,int,QObject*)),
|
2011-08-05 12:36:15 +02:00
|
|
|
&proxy, SLOT(changeStatusMessage(QString,int)));
|
2009-01-13 13:47:00 +01:00
|
|
|
QObject::connect(&handler,
|
2009-01-23 15:12:04 +01:00
|
|
|
SIGNAL(selectionChanged(QList<QTextEdit::ExtraSelection>)),
|
|
|
|
|
&proxy, SLOT(changeSelection(QList<QTextEdit::ExtraSelection>)));
|
2009-01-27 14:08:17 +01:00
|
|
|
QObject::connect(&handler, SIGNAL(extraInformationChanged(QString)),
|
2009-01-23 15:12:04 +01:00
|
|
|
&proxy, SLOT(changeExtraInformation(QString)));
|
2009-01-27 14:08:17 +01:00
|
|
|
QObject::connect(&handler, SIGNAL(statusDataChanged(QString)),
|
|
|
|
|
&proxy, SLOT(changeStatusData(QString)));
|
2013-03-08 17:50:16 +01:00
|
|
|
QObject::connect(&handler, SIGNAL(highlightMatches(QString)),
|
|
|
|
|
&proxy, SLOT(highlightMatches(QString)));
|
|
|
|
|
QObject::connect(&handler, SIGNAL(handleExCommandRequested(bool*,ExCommand)),
|
|
|
|
|
&proxy, SLOT(handleExCommand(bool*,ExCommand)));
|
|
|
|
|
}
|
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.
|
|
|
|
|
const QString editFileName = args.value(2, QString(_("/usr/share/vim/vim73/tutor/tutor")));
|
|
|
|
|
|
|
|
|
|
// Create editor widget.
|
|
|
|
|
QWidget *editor = createEditorWidget(usePlainTextEdit);
|
|
|
|
|
|
|
|
|
|
// Create FakeVimHandler instance which will emulate Vim behavior in editor widget.
|
|
|
|
|
FakeVimHandler handler(editor, 0);
|
|
|
|
|
|
|
|
|
|
// Create main window.
|
|
|
|
|
QMainWindow mainWindow;
|
|
|
|
|
initMainWindow(mainWindow, editor, usePlainTextEdit ? _("QPlainTextEdit") : _("QTextEdit"));
|
|
|
|
|
|
|
|
|
|
// Connect slots to FakeVimHandler signals.
|
|
|
|
|
Proxy proxy(editor, &mainWindow);
|
|
|
|
|
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"
|