fakevimhandler.h 5.24 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
hjk's avatar
hjk committed
2
**
hjk's avatar
hjk committed
3
4
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
hjk's avatar
hjk committed
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
hjk's avatar
hjk committed
7
**
hjk's avatar
hjk committed
8
9
10
11
12
13
14
** 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
** a written agreement between you and Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
hjk's avatar
hjk committed
15
**
16
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
17
18
19
20
21
22
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
hjk's avatar
hjk committed
23
**
hjk's avatar
hjk committed
24
25
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
con's avatar
con committed
26
27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
28
****************************************************************************/
hjk's avatar
hjk committed
29
30
31
32

#ifndef FAKEVIM_HANDLER_H
#define FAKEVIM_HANDLER_H

33
#include "fakevimactions.h"
hjk's avatar
hjk committed
34

35
36
#include <QObject>
#include <QTextEdit>
hjk's avatar
hjk committed
37
38
39
40

namespace FakeVim {
namespace Internal {

41
42
enum RangeMode
{
43
44
    // Reordering first three enum items here will break
    // compatibility with clipboard format stored by Vim.
45
46
47
    RangeCharMode,         // v
    RangeLineMode,         // V
    RangeBlockMode,        // Ctrl-v
48
    RangeLineModeExclusive,
49
    RangeBlockAndTailMode // Ctrl-v for D and X
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
};

struct Range
{
    Range();
    Range(int b, int e, RangeMode m = RangeCharMode);
    QString toString() const;

    int beginPos;
    int endPos;
    RangeMode rangemode;
};

struct ExCommand
{
65
    ExCommand() : hasBang(false), count(1) {}
66
67
68
    ExCommand(const QString &cmd, const QString &args = QString(),
        const Range &range = Range());

69
    bool matches(const QString &min, const QString &full) const;
70

71
72
73
74
    QString cmd;
    bool hasBang;
    QString args;
    Range range;
75
    int count;
76
77
};

78
79
80
81
82
83
84
// message levels sorted by severity
enum MessageLevel
{
    MessageMode,    // show current mode (format "-- %1 --")
    MessageCommand, // show last Ex command or search
    MessageInfo,    // result of a command
    MessageWarning, // warning
85
86
    MessageError,   // error
    MessageShowCmd  // partial command
87
88
};

hjk's avatar
hjk committed
89
90
91
92
93
class FakeVimHandler : public QObject
{
    Q_OBJECT

public:
94
    explicit FakeVimHandler(QWidget *widget, QObject *parent = 0);
hjk's avatar
hjk committed
95
96
    ~FakeVimHandler();

97
98
    QWidget *widget();

99
100
101
    // call before widget is deleted
    void disconnectFromEditor();

hjk's avatar
hjk committed
102
public slots:
103
    void setCurrentFileName(const QString &fileName);
104
105
    QString currentFileName() const;

106
    void showMessage(MessageLevel level, const QString &msg);
107
108

    // This executes an "ex" style command taking context
109
    // information from the current widget.
110
    void handleCommand(const QString &cmd);
hjk's avatar
hjk committed
111
    void handleReplay(const QString &keys);
112
    void handleInput(const QString &keys);
113

114
115
    void installEventFilter();

116
117
    // Convenience
    void setupWidget();
118
    void restoreWidget(int tabSize);
119

120
121
122
123
124
    // Test only
    int physicalIndentation(const QString &line) const;
    int logicalIndentation(const QString &line) const;
    QString tabExpand(int n) const;

125
126
    void miniBufferTextEdited(const QString &text, int cursorPos);

127
128
129
    // Set text cursor position. Keeps anchor if in visual mode.
    void setTextCursorPosition(int position);

Lukas Holecek's avatar
Lukas Holecek committed
130
131
    bool jumpToLocalMark(QChar mark, bool backTickMode);

hjk's avatar
hjk committed
132
signals:
133
    void commandBufferChanged(const QString &msg, int pos, int messageLevel, QObject *eventFilter);
hjk's avatar
hjk committed
134
    void statusDataChanged(const QString &msg);
135
136
    void extraInformationChanged(const QString &msg);
    void selectionChanged(const QList<QTextEdit::ExtraSelection> &selection);
137
    void highlightMatches(const QString &needle);
138
    void writeAllRequested(QString *error);
139
    void moveToMatchingParenthesis(bool *moved, bool *forward, QTextCursor *cursor);
140
    void checkForElectricCharacter(bool *result, QChar c);
141
    void indentRegion(int beginLine, int endLine, QChar typedChar);
142
    void completionRequested();
143
    void simpleCompletionRequested(const QString &needle, bool forward);
hjk's avatar
hjk committed
144
    void windowCommandRequested(int key);
145
    void findRequested(bool reverse);
146
    void findNextRequested(bool reverse);
147
    void handleExCommandRequested(bool *handled, const ExCommand &cmd);
hjk's avatar
hjk committed
148
149
    void requestSetBlockSelection(bool on);
    void requestHasBlockSelection(bool *on);
150
151
152
    void foldToggle(int depth);
    void foldAll(bool fold);
    void fold(int depth, bool fold);
153
    void foldGoTo(int count, bool current);
Lukas Holecek's avatar
Lukas Holecek committed
154
    void jumpToGlobalMark(QChar mark, bool backTickMode, const QString &fileName);
hjk's avatar
hjk committed
155

156
157
158
public:
    class Private;

hjk's avatar
hjk committed
159
160
private:
    bool eventFilter(QObject *ob, QEvent *ev);
161

hjk's avatar
hjk committed
162
163
164
165
166
167
    Private *d;
};

} // namespace Internal
} // namespace FakeVim

168
Q_DECLARE_METATYPE(FakeVim::Internal::ExCommand)
169
170


171
#endif // FAKEVIM_HANDLER_H