forked from qt-creator/qt-creator
FakeVim: Make plugin standalone if necessary
It's possible to use FakeVim as standalone plugin for a QTextEdit or QPlainTextEdit widget, so there should be minimum dependencies on Qt Creator code. Change-Id: I415ed87f5e4d97ea78d9d25a8f0c82701ef1f70d Reviewed-by: hjk <hjk121@nokiamail.com>
This commit is contained in:
@@ -42,7 +42,11 @@
|
|||||||
#include <QObject>
|
#include <QObject>
|
||||||
#include <QCoreApplication>
|
#include <QCoreApplication>
|
||||||
|
|
||||||
|
#ifdef FAKEVIM_STANDALONE
|
||||||
|
using namespace FakeVim::Internal::Utils;
|
||||||
|
#else
|
||||||
using namespace Utils;
|
using namespace Utils;
|
||||||
|
#endif
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////
|
||||||
//
|
//
|
||||||
@@ -53,6 +57,29 @@ using namespace Utils;
|
|||||||
namespace FakeVim {
|
namespace FakeVim {
|
||||||
namespace Internal {
|
namespace Internal {
|
||||||
|
|
||||||
|
typedef QLatin1String _;
|
||||||
|
|
||||||
|
#ifdef FAKEVIM_STANDALONE
|
||||||
|
namespace Utils {
|
||||||
|
|
||||||
|
SavedAction::SavedAction(QObject *parent)
|
||||||
|
: QObject(parent)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
void SavedAction::setValue(const QVariant &value)
|
||||||
|
{
|
||||||
|
m_value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
QVariant SavedAction::value() const
|
||||||
|
{
|
||||||
|
return m_value;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace Utils
|
||||||
|
#endif // FAKEVIM_STANDALONE
|
||||||
|
|
||||||
FakeVimSettings::FakeVimSettings()
|
FakeVimSettings::FakeVimSettings()
|
||||||
{}
|
{}
|
||||||
|
|
||||||
@@ -64,7 +91,7 @@ FakeVimSettings::~FakeVimSettings()
|
|||||||
void FakeVimSettings::insertItem(int code, SavedAction *item,
|
void FakeVimSettings::insertItem(int code, SavedAction *item,
|
||||||
const QString &longName, const QString &shortName)
|
const QString &longName, const QString &shortName)
|
||||||
{
|
{
|
||||||
QTC_ASSERT(!m_items.contains(code), qDebug() << code << item->toString(); return);
|
QTC_ASSERT(!m_items.contains(code), qDebug() << code; return);
|
||||||
m_items[code] = item;
|
m_items[code] = item;
|
||||||
if (!longName.isEmpty()) {
|
if (!longName.isEmpty()) {
|
||||||
m_nameToCode[longName] = code;
|
m_nameToCode[longName] = code;
|
||||||
@@ -74,6 +101,7 @@ void FakeVimSettings::insertItem(int code, SavedAction *item,
|
|||||||
m_nameToCode[shortName] = code;
|
m_nameToCode[shortName] = code;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifndef FAKEVIM_STANDALONE
|
||||||
void FakeVimSettings::readSettings(QSettings *settings)
|
void FakeVimSettings::readSettings(QSettings *settings)
|
||||||
{
|
{
|
||||||
foreach (SavedAction *item, m_items)
|
foreach (SavedAction *item, m_items)
|
||||||
@@ -85,6 +113,7 @@ void FakeVimSettings::writeSettings(QSettings *settings)
|
|||||||
foreach (SavedAction *item, m_items)
|
foreach (SavedAction *item, m_items)
|
||||||
item->writeSettings(settings);
|
item->writeSettings(settings);
|
||||||
}
|
}
|
||||||
|
#endif // FAKEVIM_STANDALONE
|
||||||
|
|
||||||
SavedAction *FakeVimSettings::item(int code)
|
SavedAction *FakeVimSettings::item(int code)
|
||||||
{
|
{
|
||||||
@@ -114,162 +143,63 @@ QString FakeVimSettings::trySetValue(const QString &name, const QString &value)
|
|||||||
return QString();
|
return QString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
SavedAction *createAction(FakeVimSettings *instance, int code, const QVariant &value,
|
||||||
|
const QString &settingsKey = QString(),
|
||||||
|
const QString &shortKey = QString())
|
||||||
|
{
|
||||||
|
SavedAction *item = new SavedAction(instance);
|
||||||
|
item->setValue(value);
|
||||||
|
#ifndef FAKEVIM_STANDALONE
|
||||||
|
item->setSettingsKey(_("FakeVim"), settingsKey);
|
||||||
|
item->setDefaultValue(value);
|
||||||
|
item->setCheckable( value.canConvert<bool>() );
|
||||||
|
#endif
|
||||||
|
instance->insertItem(code, item, settingsKey.toLower(), shortKey);
|
||||||
|
return item;
|
||||||
|
}
|
||||||
|
|
||||||
FakeVimSettings *theFakeVimSettings()
|
FakeVimSettings *theFakeVimSettings()
|
||||||
{
|
{
|
||||||
static FakeVimSettings *instance = 0;
|
static FakeVimSettings *s = 0;
|
||||||
if (instance)
|
if (s)
|
||||||
return instance;
|
return s;
|
||||||
|
|
||||||
instance = new FakeVimSettings;
|
s = new FakeVimSettings;
|
||||||
|
|
||||||
typedef QLatin1String _;
|
// Specific FakeVim settings
|
||||||
SavedAction *item = 0;
|
createAction(s, ConfigUseFakeVim, true, _("UseFakeVim"));
|
||||||
|
createAction(s, ConfigReadVimRc, false, _("ReadVimRc"));
|
||||||
const QString group = _("FakeVim");
|
createAction(s, ConfigVimRcPath, QString(), _("VimRcPath"));
|
||||||
item = new SavedAction(instance);
|
#ifndef FAKEVIM_STANDALONE
|
||||||
item->setText(QCoreApplication::translate("FakeVim::Internal",
|
s->item(ConfigUseFakeVim)->setText(QCoreApplication::translate("FakeVim::Internal",
|
||||||
"Use Vim-style Editing"));
|
"Use Vim-style Editing"));
|
||||||
item->setSettingsKey(group, _("UseFakeVim"));
|
s->item(ConfigReadVimRc)->setText(QCoreApplication::translate("FakeVim::Internal",
|
||||||
item->setCheckable(true);
|
|
||||||
item->setValue(false);
|
|
||||||
instance->insertItem(ConfigUseFakeVim, item);
|
|
||||||
|
|
||||||
item = new SavedAction(instance);
|
|
||||||
item->setText(QCoreApplication::translate("FakeVim::Internal",
|
|
||||||
"Read .vimrc"));
|
"Read .vimrc"));
|
||||||
item->setSettingsKey(group, _("ReadVimRc"));
|
s->item(ConfigVimRcPath)->setText(QCoreApplication::translate("FakeVim::Internal",
|
||||||
item->setCheckable(true);
|
|
||||||
item->setValue(false);
|
|
||||||
instance->insertItem(ConfigReadVimRc, item);
|
|
||||||
|
|
||||||
item = new SavedAction(instance);
|
|
||||||
item->setText(QCoreApplication::translate("FakeVim::Internal",
|
|
||||||
"Path to .vimrc"));
|
"Path to .vimrc"));
|
||||||
item->setDefaultValue(QString());
|
#endif
|
||||||
item->setSettingsKey(group, _("VimRcPath"));
|
createAction(s, ConfigShowMarks, false, _("ShowMarks"), _("sm"));
|
||||||
instance->insertItem(ConfigVimRcPath, item);
|
createAction(s, ConfigPassControlKey, false, _("PassControlKey"), _("pck"));
|
||||||
|
|
||||||
item = new SavedAction(instance);
|
// Emulated Vim setting
|
||||||
item->setValue(true);
|
createAction(s, ConfigStartOfLine, true, _("StartOfLine"), _("sol"));
|
||||||
item->setDefaultValue(true);
|
createAction(s, ConfigTabStop, 8, _("TabStop"), _("ts"));
|
||||||
item->setSettingsKey(group, _("StartOfLine"));
|
createAction(s, ConfigSmartTab, false, _("SmartTab"), _("sta"));
|
||||||
item->setCheckable(true);
|
createAction(s, ConfigHlSearch, true, _("HlSearch"), _("hls"));
|
||||||
instance->insertItem(ConfigStartOfLine, item, _("startofline"), _("sol"));
|
createAction(s, ConfigShiftWidth, 8, _("ShiftWidth"), _("sw"));
|
||||||
|
createAction(s, ConfigExpandTab, false, _("ExpandTab"), _("et"));
|
||||||
|
createAction(s, ConfigAutoIndent, false, _("AutoIndent"), _("ai"));
|
||||||
|
createAction(s, ConfigSmartIndent, false, _("SmartIndent"), _("si"));
|
||||||
|
createAction(s, ConfigIncSearch, true, _("IncSearch"), _("is"));
|
||||||
|
createAction(s, ConfigUseCoreSearch, false, _("UseCoreSearch"), _("ucs"));
|
||||||
|
createAction(s, ConfigSmartCase, false, _("SmartCase"), _("scs"));
|
||||||
|
createAction(s, ConfigWrapScan, true, _("WrapScan"), _("ws"));
|
||||||
|
createAction(s, ConfigShowCmd, true, _("ShowCmd"), _("sc"));
|
||||||
|
createAction(s, ConfigBackspace, _("indent,eol,start"), _("ConfigBackspace"), _("bs"));
|
||||||
|
createAction(s, ConfigIsKeyword, _("@,48-57,_,192-255,a-z,A-Z"), _("IsKeyword"), _("isk"));
|
||||||
|
createAction(s, ConfigClipboard, QString(), _("Clipboard"), _("cb"));
|
||||||
|
|
||||||
item = new SavedAction(instance);
|
return s;
|
||||||
item->setDefaultValue(8);
|
|
||||||
item->setSettingsKey(group, _("TabStop"));
|
|
||||||
instance->insertItem(ConfigTabStop, item, _("tabstop"), _("ts"));
|
|
||||||
|
|
||||||
item = new SavedAction(instance);
|
|
||||||
item->setDefaultValue(false);
|
|
||||||
item->setValue(false);
|
|
||||||
item->setSettingsKey(group, _("SmartTab"));
|
|
||||||
instance->insertItem(ConfigSmartTab, item, _("smarttab"), _("sta"));
|
|
||||||
|
|
||||||
item = new SavedAction(instance);
|
|
||||||
item->setDefaultValue(true);
|
|
||||||
item->setValue(true);
|
|
||||||
item->setSettingsKey(group, _("HlSearch"));
|
|
||||||
item->setCheckable(true);
|
|
||||||
instance->insertItem(ConfigHlSearch, item, _("hlsearch"), _("hls"));
|
|
||||||
|
|
||||||
item = new SavedAction(instance);
|
|
||||||
item->setDefaultValue(8);
|
|
||||||
item->setSettingsKey(group, _("ShiftWidth"));
|
|
||||||
instance->insertItem(ConfigShiftWidth, item, _("shiftwidth"), _("sw"));
|
|
||||||
|
|
||||||
item = new SavedAction(instance);
|
|
||||||
item->setDefaultValue(false);
|
|
||||||
item->setValue(false);
|
|
||||||
item->setSettingsKey(group, _("ExpandTab"));
|
|
||||||
item->setCheckable(true);
|
|
||||||
instance->insertItem(ConfigExpandTab, item, _("expandtab"), _("et"));
|
|
||||||
|
|
||||||
item = new SavedAction(instance);
|
|
||||||
item->setDefaultValue(false);
|
|
||||||
item->setValue(false);
|
|
||||||
item->setSettingsKey(group, _("AutoIndent"));
|
|
||||||
item->setValue(false);
|
|
||||||
item->setCheckable(true);
|
|
||||||
instance->insertItem(ConfigAutoIndent, item, _("autoindent"), _("ai"));
|
|
||||||
|
|
||||||
item = new SavedAction(instance);
|
|
||||||
item->setDefaultValue(false);
|
|
||||||
item->setValue(false);
|
|
||||||
item->setSettingsKey(group, _("SmartIndent"));
|
|
||||||
item->setValue(false);
|
|
||||||
item->setCheckable(true);
|
|
||||||
instance->insertItem(ConfigSmartIndent, item, _("smartindent"), _("si"));
|
|
||||||
|
|
||||||
item = new SavedAction(instance);
|
|
||||||
item->setDefaultValue(true);
|
|
||||||
item->setValue(true);
|
|
||||||
item->setSettingsKey(group, _("IncSearch"));
|
|
||||||
item->setCheckable(true);
|
|
||||||
instance->insertItem(ConfigIncSearch, item, _("incsearch"), _("is"));
|
|
||||||
|
|
||||||
item = new SavedAction(instance);
|
|
||||||
item->setDefaultValue(false);
|
|
||||||
item->setValue(false);
|
|
||||||
item->setSettingsKey(group, _("UseCoreSearch")); item->setCheckable(true);
|
|
||||||
instance->insertItem(ConfigUseCoreSearch, item,
|
|
||||||
_("usecoresearch"), _("ucs"));
|
|
||||||
|
|
||||||
item = new SavedAction(instance);
|
|
||||||
item->setDefaultValue(false);
|
|
||||||
item->setValue(false);
|
|
||||||
item->setSettingsKey(group, _("SmartCase")); item->setCheckable(true);
|
|
||||||
item->setCheckable(true);
|
|
||||||
instance->insertItem(ConfigSmartCase, item, _("smartcase"), _("scs"));
|
|
||||||
|
|
||||||
item = new SavedAction(instance);
|
|
||||||
item->setDefaultValue(true);
|
|
||||||
item->setValue(true);
|
|
||||||
item->setSettingsKey(group, _("WrapScan")); item->setCheckable(true);
|
|
||||||
item->setCheckable(true);
|
|
||||||
instance->insertItem(ConfigWrapScan, item, _("wrapscan"), _("ws"));
|
|
||||||
|
|
||||||
item = new SavedAction(instance);
|
|
||||||
item->setDefaultValue(_("indent,eol,start"));
|
|
||||||
item->setSettingsKey(group, _("Backspace"));
|
|
||||||
instance->insertItem(ConfigBackspace, item, _("backspace"), _("bs"));
|
|
||||||
|
|
||||||
item = new SavedAction(instance);
|
|
||||||
item->setDefaultValue(_("@,48-57,_,192-255,a-z,A-Z"));
|
|
||||||
item->setSettingsKey(group, _("IsKeyword"));
|
|
||||||
instance->insertItem(ConfigIsKeyword, item, _("iskeyword"), _("isk"));
|
|
||||||
|
|
||||||
// Invented here.
|
|
||||||
item = new SavedAction(instance);
|
|
||||||
item->setDefaultValue(false);
|
|
||||||
item->setValue(false);
|
|
||||||
item->setSettingsKey(group, _("ShowMarks"));
|
|
||||||
item->setCheckable(true);
|
|
||||||
instance->insertItem(ConfigShowMarks, item, _("showmarks"), _("sm"));
|
|
||||||
|
|
||||||
item = new SavedAction(instance);
|
|
||||||
item->setDefaultValue(false);
|
|
||||||
item->setValue(false);
|
|
||||||
item->setSettingsKey(group, _("PassControlKey"));
|
|
||||||
item->setCheckable(true);
|
|
||||||
instance->insertItem(ConfigPassControlKey, item, _("passcontrolkey"), _("pck"));
|
|
||||||
|
|
||||||
item = new SavedAction(instance);
|
|
||||||
item->setDefaultValue(QString());
|
|
||||||
item->setValue(QString());
|
|
||||||
item->setSettingsKey(group, _("Clipboard"));
|
|
||||||
item->setCheckable(true);
|
|
||||||
instance->insertItem(ConfigClipboard, item, _("clipboard"), _("cb"));
|
|
||||||
|
|
||||||
item = new SavedAction(instance);
|
|
||||||
item->setDefaultValue(true);
|
|
||||||
item->setValue(true);
|
|
||||||
item->setSettingsKey(group, _("ShowCmd")); item->setCheckable(true);
|
|
||||||
item->setCheckable(true);
|
|
||||||
instance->insertItem(ConfigShowCmd, item, _("showcmd"), _("sc"));
|
|
||||||
|
|
||||||
return instance;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
SavedAction *theFakeVimSetting(int code)
|
SavedAction *theFakeVimSetting(int code)
|
||||||
|
|||||||
@@ -30,15 +30,33 @@
|
|||||||
#ifndef FAKEVIM_ACTIONS_H
|
#ifndef FAKEVIM_ACTIONS_H
|
||||||
#define FAKEVIM_ACTIONS_H
|
#define FAKEVIM_ACTIONS_H
|
||||||
|
|
||||||
|
#ifndef FAKEVIM_STANDALONE
|
||||||
# include <utils/savedaction.h>
|
# include <utils/savedaction.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
#include <QHash>
|
#include <QHash>
|
||||||
#include <QObject>
|
#include <QObject>
|
||||||
#include <QString>
|
#include <QString>
|
||||||
|
#include <QVariant>
|
||||||
|
|
||||||
namespace FakeVim {
|
namespace FakeVim {
|
||||||
namespace Internal {
|
namespace Internal {
|
||||||
|
|
||||||
|
#ifdef FAKEVIM_STANDALONE
|
||||||
|
namespace Utils {
|
||||||
|
|
||||||
|
class SavedAction : public QObject
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
SavedAction(QObject *parent);
|
||||||
|
void setValue(const QVariant &value);
|
||||||
|
QVariant value() const;
|
||||||
|
QVariant m_value;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace Utils
|
||||||
|
#endif // FAKEVIM_STANDALONE
|
||||||
|
|
||||||
enum FakeVimSettingsCode
|
enum FakeVimSettingsCode
|
||||||
{
|
{
|
||||||
ConfigUseFakeVim,
|
ConfigUseFakeVim,
|
||||||
@@ -90,8 +108,10 @@ public:
|
|||||||
Utils::SavedAction *item(const QString &name);
|
Utils::SavedAction *item(const QString &name);
|
||||||
QString trySetValue(const QString &name, const QString &value);
|
QString trySetValue(const QString &name, const QString &value);
|
||||||
|
|
||||||
|
#ifndef FAKEVIM_STANDALONE
|
||||||
void readSettings(QSettings *settings);
|
void readSettings(QSettings *settings);
|
||||||
void writeSettings(QSettings *settings);
|
void writeSettings(QSettings *settings);
|
||||||
|
#endif
|
||||||
|
|
||||||
private:
|
private:
|
||||||
QHash<int, Utils::SavedAction *> m_items;
|
QHash<int, Utils::SavedAction *> m_items;
|
||||||
|
|||||||
@@ -59,6 +59,8 @@
|
|||||||
|
|
||||||
#include "fakevimhandler.h"
|
#include "fakevimhandler.h"
|
||||||
|
|
||||||
|
#include "fakevimactions.h"
|
||||||
|
|
||||||
#include <utils/hostosinfo.h>
|
#include <utils/hostosinfo.h>
|
||||||
#include <utils/qtcassert.h>
|
#include <utils/qtcassert.h>
|
||||||
|
|
||||||
@@ -105,6 +107,9 @@
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
using namespace Utils;
|
using namespace Utils;
|
||||||
|
#ifdef FAKEVIM_STANDALONE
|
||||||
|
using namespace FakeVim::Internal::Utils;
|
||||||
|
#endif
|
||||||
|
|
||||||
namespace FakeVim {
|
namespace FakeVim {
|
||||||
namespace Internal {
|
namespace Internal {
|
||||||
@@ -1768,7 +1773,7 @@ public:
|
|||||||
// auto-indent
|
// auto-indent
|
||||||
QString tabExpand(int len) const;
|
QString tabExpand(int len) const;
|
||||||
Column indentation(const QString &line) const;
|
Column indentation(const QString &line) const;
|
||||||
void insertAutomaticIndentation(bool goingDown);
|
void insertAutomaticIndentation(bool goingDown, bool forceAutoIndent = false);
|
||||||
bool removeAutomaticIndentation(); // true if something removed
|
bool removeAutomaticIndentation(); // true if something removed
|
||||||
// number of autoindented characters
|
// number of autoindented characters
|
||||||
int m_justAutoIndented;
|
int m_justAutoIndented;
|
||||||
@@ -2045,12 +2050,14 @@ EventResult FakeVimHandler::Private::handleEvent(QKeyEvent *ev)
|
|||||||
return EventPassedToCore;
|
return EventPassedToCore;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifndef FAKEVIM_STANDALONE
|
||||||
bool inSnippetMode = false;
|
bool inSnippetMode = false;
|
||||||
QMetaObject::invokeMethod(editor(),
|
QMetaObject::invokeMethod(editor(),
|
||||||
"inSnippetMode", Q_ARG(bool *, &inSnippetMode));
|
"inSnippetMode", Q_ARG(bool *, &inSnippetMode));
|
||||||
|
|
||||||
if (inSnippetMode)
|
if (inSnippetMode)
|
||||||
return EventPassedToCore;
|
return EventPassedToCore;
|
||||||
|
#endif
|
||||||
|
|
||||||
// Fake "End of line"
|
// Fake "End of line"
|
||||||
//m_tc = cursor();
|
//m_tc = cursor();
|
||||||
@@ -4983,23 +4990,15 @@ bool FakeVimHandler::Private::handleExChangeCommand(const ExCommand &cmd)
|
|||||||
if (!cmd.matches(_("c"), _("change")))
|
if (!cmd.matches(_("c"), _("change")))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
const bool oldAutoIndent = hasConfig(ConfigAutoIndent);
|
|
||||||
// Temporarily set autoindent if ! is present.
|
|
||||||
if (cmd.hasBang)
|
|
||||||
theFakeVimSetting(ConfigAutoIndent)->setValue(true, false);
|
|
||||||
|
|
||||||
Range range = cmd.range;
|
Range range = cmd.range;
|
||||||
range.rangemode = RangeLineModeExclusive;
|
range.rangemode = RangeLineModeExclusive;
|
||||||
removeText(range);
|
removeText(range);
|
||||||
insertAutomaticIndentation(true);
|
insertAutomaticIndentation(true, cmd.hasBang);
|
||||||
|
|
||||||
// FIXME: In Vim same or less number of lines can be inserted and position after insertion is
|
// FIXME: In Vim same or less number of lines can be inserted and position after insertion is
|
||||||
// beginning of last inserted line.
|
// beginning of last inserted line.
|
||||||
enterInsertMode();
|
enterInsertMode();
|
||||||
|
|
||||||
if (cmd.hasBang && !oldAutoIndent)
|
|
||||||
theFakeVimSetting(ConfigAutoIndent)->setValue(false, false);
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -5189,7 +5188,7 @@ bool FakeVimHandler::Private::handleExBangCommand(const ExCommand &cmd) // :!
|
|||||||
QProcess proc;
|
QProcess proc;
|
||||||
proc.start(command);
|
proc.start(command);
|
||||||
proc.waitForStarted();
|
proc.waitForStarted();
|
||||||
if (Utils::HostOsInfo::isWindowsHost())
|
if (HostOsInfo::isWindowsHost())
|
||||||
text.replace(_("\n"), _("\r\n"));
|
text.replace(_("\n"), _("\r\n"));
|
||||||
proc.write(text.toUtf8());
|
proc.write(text.toUtf8());
|
||||||
proc.closeWriteChannel();
|
proc.closeWriteChannel();
|
||||||
@@ -6825,9 +6824,9 @@ QString FakeVimHandler::Private::tabExpand(int n) const
|
|||||||
+ QString(n % ts, QLatin1Char(' '));
|
+ QString(n % ts, QLatin1Char(' '));
|
||||||
}
|
}
|
||||||
|
|
||||||
void FakeVimHandler::Private::insertAutomaticIndentation(bool goingDown)
|
void FakeVimHandler::Private::insertAutomaticIndentation(bool goingDown, bool forceAutoIndent)
|
||||||
{
|
{
|
||||||
if (!hasConfig(ConfigAutoIndent) && !hasConfig(ConfigSmartIndent))
|
if (!forceAutoIndent && !hasConfig(ConfigAutoIndent) && !hasConfig(ConfigSmartIndent))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (hasConfig(ConfigSmartIndent)) {
|
if (hasConfig(ConfigSmartIndent)) {
|
||||||
|
|||||||
@@ -30,8 +30,6 @@
|
|||||||
#ifndef FAKEVIM_HANDLER_H
|
#ifndef FAKEVIM_HANDLER_H
|
||||||
#define FAKEVIM_HANDLER_H
|
#define FAKEVIM_HANDLER_H
|
||||||
|
|
||||||
#include "fakevimactions.h"
|
|
||||||
|
|
||||||
#include <QObject>
|
#include <QObject>
|
||||||
#include <QTextEdit>
|
#include <QTextEdit>
|
||||||
|
|
||||||
|
|||||||
@@ -29,6 +29,7 @@
|
|||||||
|
|
||||||
#include "fakevimplugin.h"
|
#include "fakevimplugin.h"
|
||||||
|
|
||||||
|
#include "fakevimactions.h"
|
||||||
#include "fakevimhandler.h"
|
#include "fakevimhandler.h"
|
||||||
#include "ui_fakevimoptions.h"
|
#include "ui_fakevimoptions.h"
|
||||||
|
|
||||||
|
|||||||
@@ -1,15 +1,19 @@
|
|||||||
include(../../auto/qttest.pri)
|
include(../../auto/qttest.pri)
|
||||||
include($$IDE_SOURCE_TREE/src/libs/utils/utils.pri)
|
include($$IDE_SOURCE_TREE/src/libs/utils/utils.pri)
|
||||||
|
DEFINES += FAKEVIM_STANDALONE
|
||||||
|
|
||||||
FAKEVIMDIR = $$IDE_SOURCE_TREE/src/plugins/fakevim
|
FAKEVIMDIR = $$IDE_SOURCE_TREE/src/plugins/fakevim
|
||||||
LIBSDIR = $$IDE_SOURCE_TREE/src/libs
|
UTILSDIR = $$IDE_SOURCE_TREE/src/libs/utils
|
||||||
|
|
||||||
SOURCES += main.cpp \
|
SOURCES += main.cpp \
|
||||||
$$FAKEVIMDIR/fakevimhandler.cpp \
|
$$FAKEVIMDIR/fakevimhandler.cpp \
|
||||||
$$FAKEVIMDIR/fakevimactions.cpp
|
$$FAKEVIMDIR/fakevimactions.cpp \
|
||||||
|
$$UTILSDIR/hostosinfo.cpp \
|
||||||
|
$$UTILSDIR/qtcassert.cpp
|
||||||
|
|
||||||
HEADERS += $$FAKEVIMDIR/fakevimhandler.h \
|
HEADERS += $$FAKEVIMDIR/fakevimhandler.h \
|
||||||
$$FAKEVIMDIR/fakevimactions.h
|
$$FAKEVIMDIR/fakevimactions.h \
|
||||||
|
$$UTILSDIR/hostosinfo.h \
|
||||||
INCLUDEPATH += $$FAKEVIMDIR $$LIBSDIR
|
$$UTILSDIR/qtcassert.h
|
||||||
|
|
||||||
|
INCLUDEPATH += $$FAKEVIMDIR $$UTILSDIR
|
||||||
|
|||||||
@@ -29,17 +29,56 @@
|
|||||||
|
|
||||||
#include "fakevimhandler.h"
|
#include "fakevimhandler.h"
|
||||||
|
|
||||||
#include <QDebug>
|
|
||||||
|
|
||||||
#include <QApplication>
|
#include <QApplication>
|
||||||
|
#include <QFontMetrics>
|
||||||
#include <QMainWindow>
|
#include <QMainWindow>
|
||||||
#include <QMessageBox>
|
#include <QMessageBox>
|
||||||
|
#include <QPainter>
|
||||||
#include <QPlainTextEdit>
|
#include <QPlainTextEdit>
|
||||||
#include <QStatusBar>
|
#include <QStatusBar>
|
||||||
#include <QTextEdit>
|
#include <QTextEdit>
|
||||||
|
|
||||||
using namespace FakeVim::Internal;
|
using namespace FakeVim::Internal;
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
class Proxy : public QObject
|
class Proxy : public QObject
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
@@ -64,6 +103,44 @@ public slots:
|
|||||||
updateStatusBar();
|
updateStatusBar();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void changeStatusMessage(const QString &contents, int cursorPos)
|
void changeStatusMessage(const QString &contents, int cursorPos)
|
||||||
{
|
{
|
||||||
m_statusMessage = cursorPos == -1 ? contents
|
m_statusMessage = cursorPos == -1 ? contents
|
||||||
@@ -83,6 +160,18 @@ public slots:
|
|||||||
m_mainWindow->statusBar()->showMessage(msg);
|
m_mainWindow->statusBar()->showMessage(msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void handleExCommand(bool *handled, const ExCommand &cmd)
|
||||||
|
{
|
||||||
|
if (cmd.matches(_("q"), _("quit")) || cmd.matches(_("qa"), _("qall"))) {
|
||||||
|
QApplication::quit();
|
||||||
|
} else {
|
||||||
|
*handled = false;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
*handled = true;
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
QWidget *m_widget;
|
QWidget *m_widget;
|
||||||
QMainWindow *m_mainWindow;
|
QMainWindow *m_mainWindow;
|
||||||
@@ -90,55 +179,63 @@ private:
|
|||||||
QString m_statusData;
|
QString m_statusData;
|
||||||
};
|
};
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
QWidget *createEditorWidget(bool usePlainTextEdit)
|
||||||
{
|
{
|
||||||
QApplication app(argc, argv);
|
QWidget *editor = 0;
|
||||||
|
|
||||||
QStringList args = app.arguments();
|
|
||||||
(void) args.takeFirst();
|
|
||||||
|
|
||||||
QWidget *widget = 0;
|
|
||||||
QString title;
|
|
||||||
bool usePlainTextEdit = args.size() < 2;
|
|
||||||
if (usePlainTextEdit) {
|
if (usePlainTextEdit) {
|
||||||
QPlainTextEdit *w = new QPlainTextEdit;
|
Editor<QPlainTextEdit> *w = new Editor<QPlainTextEdit>;
|
||||||
w->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
w->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
||||||
title = QLatin1String("PlainTextEdit");
|
editor = w;
|
||||||
widget = w;
|
|
||||||
} else {
|
} else {
|
||||||
QTextEdit *w = new QTextEdit;
|
Editor<QTextEdit> *w = new Editor<QTextEdit>;
|
||||||
w->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
w->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
||||||
title = QLatin1String("TextEdit");
|
editor = w;
|
||||||
widget = w;
|
|
||||||
}
|
}
|
||||||
widget->setObjectName(QLatin1String("Editor"));
|
editor->setObjectName(_("Editor"));
|
||||||
//widget->resize(450, 350);
|
editor->setFocus();
|
||||||
widget->setFocus();
|
|
||||||
|
|
||||||
QMainWindow mw;
|
return editor;
|
||||||
Proxy proxy(widget, &mw);
|
}
|
||||||
|
|
||||||
FakeVimHandler handler(widget, 0);
|
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"));
|
||||||
|
|
||||||
mw.setWindowTitle(QLatin1String("Fakevim (") + title + QLatin1Char(')'));
|
// Try to source file "fakevimrc" from current directory.
|
||||||
mw.setCentralWidget(widget);
|
handler.handleCommand(_("source fakevimrc"));
|
||||||
mw.resize(600, 650);
|
|
||||||
mw.move(0, 0);
|
|
||||||
mw.show();
|
|
||||||
|
|
||||||
QFont font = widget->font();
|
handler.installEventFilter();
|
||||||
//: -misc-fixed-medium-r-semicondensed--13-120-75-75-c-60-iso8859-1
|
handler.setupWidget();
|
||||||
//font.setFamily("Misc");
|
}
|
||||||
font.setFamily(QLatin1String("Monospace"));
|
|
||||||
//font.setStretch(QFont::SemiCondensed);
|
|
||||||
|
|
||||||
widget->setFont(font);
|
void initMainWindow(QMainWindow &mainWindow, QWidget *centralWidget, const QString &title)
|
||||||
mw.statusBar()->setFont(font);
|
{
|
||||||
|
mainWindow.setWindowTitle(QString(_("FakeVim (%1)")).arg(title));
|
||||||
|
mainWindow.setCentralWidget(centralWidget);
|
||||||
|
mainWindow.resize(600, 650);
|
||||||
|
mainWindow.move(0, 0);
|
||||||
|
mainWindow.show();
|
||||||
|
|
||||||
QObject::connect(&handler, SIGNAL(commandBufferChanged(QString,int)),
|
// Set monospace font for editor and status bar.
|
||||||
|
QFont font = QApplication::font();
|
||||||
|
font.setFamily(_("Monospace"));
|
||||||
|
centralWidget->setFont(font);
|
||||||
|
mainWindow.statusBar()->setFont(font);
|
||||||
|
}
|
||||||
|
|
||||||
|
void readFile(FakeVimHandler &handler, const QString &editFileName)
|
||||||
|
{
|
||||||
|
handler.handleCommand(QString(_("r %1")).arg(editFileName));
|
||||||
|
}
|
||||||
|
|
||||||
|
void connectSignals(FakeVimHandler &handler, Proxy &proxy)
|
||||||
|
{
|
||||||
|
QObject::connect(&handler, SIGNAL(commandBufferChanged(QString,int,int,int,QObject*)),
|
||||||
&proxy, SLOT(changeStatusMessage(QString,int)));
|
&proxy, SLOT(changeStatusMessage(QString,int)));
|
||||||
//QObject::connect(&handler, SIGNAL(quitRequested(bool)),
|
|
||||||
// &app, SLOT(quit()));
|
|
||||||
QObject::connect(&handler,
|
QObject::connect(&handler,
|
||||||
SIGNAL(selectionChanged(QList<QTextEdit::ExtraSelection>)),
|
SIGNAL(selectionChanged(QList<QTextEdit::ExtraSelection>)),
|
||||||
&proxy, SLOT(changeSelection(QList<QTextEdit::ExtraSelection>)));
|
&proxy, SLOT(changeSelection(QList<QTextEdit::ExtraSelection>)));
|
||||||
@@ -146,17 +243,42 @@ int main(int argc, char *argv[])
|
|||||||
&proxy, SLOT(changeExtraInformation(QString)));
|
&proxy, SLOT(changeExtraInformation(QString)));
|
||||||
QObject::connect(&handler, SIGNAL(statusDataChanged(QString)),
|
QObject::connect(&handler, SIGNAL(statusDataChanged(QString)),
|
||||||
&proxy, SLOT(changeStatusData(QString)));
|
&proxy, SLOT(changeStatusData(QString)));
|
||||||
|
QObject::connect(&handler, SIGNAL(highlightMatches(QString)),
|
||||||
|
&proxy, SLOT(highlightMatches(QString)));
|
||||||
|
QObject::connect(&handler, SIGNAL(handleExCommandRequested(bool*,ExCommand)),
|
||||||
|
&proxy, SLOT(handleExCommand(bool*,ExCommand)));
|
||||||
|
}
|
||||||
|
|
||||||
theFakeVimSetting(ConfigUseFakeVim)->setValue(true);
|
int main(int argc, char *argv[])
|
||||||
theFakeVimSetting(ConfigShiftWidth)->setValue(8);
|
{
|
||||||
theFakeVimSetting(ConfigTabStop)->setValue(8);
|
QApplication app(argc, argv);
|
||||||
theFakeVimSetting(ConfigAutoIndent)->setValue(true);
|
|
||||||
theFakeVimSetting(ConfigIsKeyword)->setValue(QLatin1String("@,48-57,_,192-255,a-z,A-Z"));
|
|
||||||
|
|
||||||
handler.installEventFilter();
|
QStringList args = app.arguments();
|
||||||
handler.setupWidget();
|
|
||||||
if (args.size() >= 1)
|
// If first argument is present use QPlainTextEdit instead on QTextEdit;
|
||||||
handler.handleCommand(QLatin1String("r ") + args.at(0));
|
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);
|
||||||
|
|
||||||
return app.exec();
|
return app.exec();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user