handler.cpp 15.5 KB
Newer Older
hjk's avatar
hjk committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/***************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact:  Qt Software Information (qt-info@nokia.com)
**
**
** Non-Open Source Usage
**
** Licensees may use this file in accordance with the Qt Beta Version
** License Agreement, Agreement version 2.2 provided with the Software or,
** alternatively, in accordance with the terms contained in a written
** agreement between you and Nokia.
**
** GNU General Public License Usage
**
** Alternatively, this file may be used under the terms of the GNU General
** Public License versions 2.0 or 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the packaging
** of this file.  Please review the following information to ensure GNU
** General Public Licensing requirements will be met:
**
** http://www.fsf.org/licensing/licenses/info/GPLv2.html and
** http://www.gnu.org/copyleft/gpl.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt GPL Exception
** version 1.3, included in the file GPL_EXCEPTION.txt in this package.
**
***************************************************************************/

#include "handler.h"

#include <QDebug>
#include <QKeyEvent>
#include <QLineEdit>
#include <QObject>
#include <QPlainTextEdit>
#include <QRegExp>
#include <QStack>
#include <QTextBlock>
#include <QTextEdit>
#include <QTextCursor>


using namespace FakeVim::Internal;

#define StartOfLine QTextCursor::StartOfLine
#define EndOfLine QTextCursor::EndOfLine
#define MoveAnchor QTextCursor::MoveAnchor
#define KeepAnchor QTextCursor::KeepAnchor
#define Up QTextCursor::Up
#define Down QTextCursor::Down
#define Right QTextCursor::Right
#define Left QTextCursor::Left


///////////////////////////////////////////////////////////////////////
//
// FakeVimHandler
//
///////////////////////////////////////////////////////////////////////
const int ParagraphSeparator = 0x00002029;

using namespace Qt;

enum Mode { InsertMode, CommandMode, ExMode };

enum SubMode { NoSubMode, RegisterSubMode, ChangeSubMode, DeleteSubMode };

class FakeVimHandler::Private
{
public:
    Private(FakeVimHandler *parent);

    bool eventFilter(QObject *ob, QEvent *ev);

    static int shift(int key) { return key + 32; }
    static int control(int key) { return key + 256; }

    void init();
84
85
86
87
88
    void handleKey(int key, const QString &text);
    void handleInsertMode(int key, const QString &text);
    void handleCommandMode(int key, const QString &text);
    void handleRegisterMode(int key, const QString &text);
    void handleExMode(int key, const QString &text);
hjk's avatar
hjk committed
89
90
91
92
93
94
95
96
97
    void finishMovement();
    void updateCommandBuffer();
    void search(const QString &needle, bool backwards);
    void showMessage(const QString &msg);

    int count() const { return m_count.isEmpty() ? 1 : m_count.toInt(); }
    int leftDist() const { return m_tc.position() - m_tc.block().position(); }
    int rightDist() const { return m_tc.block().length() - leftDist() - 1; }
    bool atEol() const { return m_tc.atBlockEnd() && m_tc.block().length()>1; }
hjk's avatar
hjk committed
98
    void moveToFirstNonBlankOnLine();
hjk's avatar
hjk committed
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156

    FakeVimHandler *q;
    Mode m_mode;
    SubMode m_submode;
    QString m_input;
    QPlainTextEdit *m_editor;
    QTextCursor m_tc;
    QHash<int, QString> m_registers;
    int m_register;
    QString m_count;

    QStack<QString> m_undoStack;
    QStack<QString> m_redoStack;

    bool m_fakeEnd;

    int m_commandCode; // ?, /, : ...
    QString m_commandBuffer;

    bool m_lastSearchBackward;
    QString m_lastSearchString;
};

FakeVimHandler::Private::Private(FakeVimHandler *parent)
{
    q = parent;
    m_mode = CommandMode;
    m_fakeEnd = false;
    m_lastSearchBackward = false;
    m_register = '"';
}

bool FakeVimHandler::Private::eventFilter(QObject *ob, QEvent *ev)
{
    QKeyEvent *keyEvent = static_cast<QKeyEvent *>(ev);
    int key = keyEvent->key();
    if (key == Key_Shift || key == Key_Alt || key == Key_Control
        || key == Key_Alt || key == Key_AltGr || key == Key_Meta)
        return false;
    //qDebug() << "KEY: " << key << Qt::ShiftModifier;

    // Fake "End of line"
    m_editor = qobject_cast<QPlainTextEdit *>(ob);
    if (!m_editor)
        return false;

    m_tc = m_editor->textCursor();
    if (m_fakeEnd) {
        //m_fakeEnd = false;
        m_tc.movePosition(Right, MoveAnchor, 1);
        qDebug() << "Unfake EOL";
    }

    if (key >= Key_A && key <= Key_Z
        && (keyEvent->modifiers() & Qt::ShiftModifier) == 0)
        key += 32;
    if ((keyEvent->modifiers() & Qt::ControlModifier) != 0)
        key += 256;
157
    handleKey(key, keyEvent->text());
hjk's avatar
hjk committed
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179

    // We fake vi-style end-of-line behaviour
    m_fakeEnd = atEol() && m_mode == CommandMode;

    //qDebug() << "POS: " <<  m_tc.position()
    //    << "  BLOCK LEN: " << m_tc.block().length()
    //    << "  LEFT: " << leftDist() << " RIGHT: " << rightDist();

    if (m_fakeEnd) {
        m_tc.movePosition(Left, MoveAnchor, 1);
        qDebug() << "Fake EOL";
    }

    //qDebug() << "POS: " <<  m_tc.position()
    //    << "  BLOCK LEN: " << m_tc.block().length()
    //    << "  LEFT: " << leftDist() << " RIGHT: " << rightDist();

    m_editor->setTextCursor(m_tc);
    m_editor->ensureCursorVisible();
    return true;
}

180
void FakeVimHandler::Private::handleKey(int key, const QString &text)
hjk's avatar
hjk committed
181
182
{
    if (m_mode == InsertMode)
183
        handleInsertMode(key, text);
hjk's avatar
hjk committed
184
    else if (m_mode == CommandMode)
185
        handleCommandMode(key, text);
hjk's avatar
hjk committed
186
    else if (m_mode == ExMode)
187
        handleExMode(key, text);
hjk's avatar
hjk committed
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
}

void FakeVimHandler::Private::finishMovement()
{
    if (m_submode == ChangeSubMode) {
        m_registers[m_register] = m_tc.selectedText();
        m_tc.removeSelectedText();
        m_mode = InsertMode;
        m_submode = NoSubMode;
    } else if (m_submode == DeleteSubMode) {
        m_registers[m_register] = m_tc.selectedText();
        m_tc.removeSelectedText();
        m_submode = NoSubMode;
        if (atEol())
            m_tc.movePosition(Left, MoveAnchor, 1);
    }
    m_count.clear();
    m_register = '"';
    m_tc.clearSelection();
    updateCommandBuffer();
}

void FakeVimHandler::Private::updateCommandBuffer()
{
    //qDebug() << "CMD" << m_commandBuffer;
    QString msg = QChar(m_commandCode ? m_commandCode : ' ') + m_commandBuffer;
    emit q->commandBufferChanged(msg);
}

void FakeVimHandler::Private::showMessage(const QString &msg)
{
    emit q->commandBufferChanged(msg);
}

222
void FakeVimHandler::Private::handleCommandMode(int key, const QString &text)
hjk's avatar
hjk committed
223
{
224
225
    Q_UNUSED(text)

hjk's avatar
hjk committed
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
    //qDebug() << "-> MODE: " << m_mode << " KEY: " << key;
    if (m_submode == RegisterSubMode) {
        m_register = key;
        m_submode = NoSubMode;
    } else if (m_submode == ChangeSubMode && key == 'c') {
        m_tc.movePosition(StartOfLine, MoveAnchor);
        m_tc.movePosition(Down, KeepAnchor, count());
        m_registers[m_register] = m_tc.selectedText();
        finishMovement();
    } else if (m_submode == DeleteSubMode && key == 'd') {
        m_tc.movePosition(StartOfLine, MoveAnchor);
        m_tc.movePosition(Down, KeepAnchor, count());
        m_registers[m_register] = m_tc.selectedText();
        finishMovement();
    } else if (key >= '0' && key <= '9') {
        if (key == '0' && m_count.isEmpty()) {
            m_tc.movePosition(StartOfLine, KeepAnchor);
            finishMovement();
        } else {
            m_count.append(QChar(key));
        }
    } else if (key == ':' || key == '/' || key == '?') {
        m_commandCode = key;
        m_mode = ExMode;
        updateCommandBuffer();
    } else if (key == '|') {
        m_tc.movePosition(StartOfLine, KeepAnchor);
        m_tc.movePosition(Right, KeepAnchor, qMin(count(), rightDist()) - 1);
        finishMovement();
    } else if (key == '"') {
        m_submode = RegisterSubMode;
    } else if (key == Key_Return) {
        m_tc.movePosition(StartOfLine);
        m_tc.movePosition(Down);
    } else if (key == Key_Home) {
        m_tc.movePosition(StartOfLine, KeepAnchor);
        finishMovement();
    } else if (key == '$' || key == Key_End) {
        m_tc.movePosition(EndOfLine, KeepAnchor);
        finishMovement();
    } else if (key == 'A') {
        m_tc.movePosition(EndOfLine, MoveAnchor);
        m_mode = InsertMode;
    } else if (key == 'c') {
        m_submode = ChangeSubMode;
    } else if (key == 'C') {
        m_submode = ChangeSubMode;
        m_tc.movePosition(EndOfLine, KeepAnchor);
        finishMovement();
    } else if (key == 'd') {
        if (atEol())
            m_tc.movePosition(Left, MoveAnchor, 1);
        m_submode = DeleteSubMode;
    } else if (key == 'D') {
        m_submode = DeleteSubMode;
        m_tc.movePosition(EndOfLine, KeepAnchor);
        finishMovement();
    } else if (key == 'h' || key == Key_Left) {
        int n = qMin(count(), leftDist());
        if (m_fakeEnd && m_tc.block().length() > 1)
            ++n;
        m_tc.movePosition(Left, KeepAnchor, n);
        finishMovement();
hjk's avatar
hjk committed
289
    } else if (key == 'H') {
hjk's avatar
hjk committed
290
        m_tc = m_editor->cursorForPosition(QPoint(0, 0));
hjk's avatar
hjk committed
291
292
293
        m_tc.movePosition(Down, KeepAnchor, qMax(count() - 1, 0));
        moveToFirstNonBlankOnLine();
        finishMovement();
hjk's avatar
hjk committed
294
295
296
297
298
299
300
301
302
303
304
    } else if (key == 'i') {
        m_mode = InsertMode;
    } else if (key == 'j' || key == Key_Down) {
        m_tc.movePosition(Down, KeepAnchor, count());
        finishMovement();
    } else if (key == 'k' || key == Key_Up) {
        m_tc.movePosition(Up, KeepAnchor, count());
        finishMovement();
    } else if (key == 'l' || key == Key_Right) {
        m_tc.movePosition(Right, KeepAnchor, qMin(count(), rightDist()));
        finishMovement();
hjk's avatar
hjk committed
305
    } else if (key == 'L') {
hjk's avatar
hjk committed
306
307
308
        int heigth = m_editor->height();
        m_tc = m_editor->cursorForPosition(QPoint(0, heigth));
        m_tc.movePosition(Up, KeepAnchor, qMax(count() - 1, 0));
hjk's avatar
hjk committed
309
310
311
        m_tc.movePosition(Up, KeepAnchor, qMax(count(), 1));
        moveToFirstNonBlankOnLine();
        finishMovement();
hjk's avatar
hjk committed
312
313
314
315
316
    } else if (key == 'M') {
        int heigth = m_editor->height();
        m_tc = m_editor->cursorForPosition(QPoint(0, heigth / 2));
        moveToFirstNonBlankOnLine();
        finishMovement();
hjk's avatar
hjk committed
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
    } else if (key == 'n') {
        search(m_lastSearchString, m_lastSearchBackward);
    } else if (key == 'N') {
        search(m_lastSearchString, !m_lastSearchBackward);
    } else if (key == 'p') {
        QString text = m_registers[m_register];
        int n = text.count(QChar(ParagraphSeparator));
        if (n > 0) {
            m_tc.movePosition(Down);
            m_tc.movePosition(StartOfLine);
            m_tc.insertText(text);
            m_tc.movePosition(Up, MoveAnchor, n);
        } else {
            m_tc.movePosition(Right);
            m_tc.insertText(text);
            m_tc.movePosition(Left);
        }
    } else if (key == control('r')) {
        m_editor->redo();
    } else if (key == 'u') {
        m_editor->undo();
    } else if (key == 'x') { // = "dl"
        if (atEol())
            m_tc.movePosition(Left, MoveAnchor, 1);
        m_submode = DeleteSubMode;
        m_tc.movePosition(Right, KeepAnchor, qMin(count(), rightDist()));
        finishMovement();
    } else if (key == 'X') {
        if (leftDist() > 0) {
            m_tc.movePosition(Left, KeepAnchor, qMin(count(), leftDist()));
            m_tc.deleteChar();
        }
        finishMovement();
    } else if (key == Key_Backspace) {
        m_tc.deletePreviousChar();
352
353
    } else if (key == Key_Delete) {
        m_tc.deleteChar();
hjk's avatar
hjk committed
354
355
356
357
358
    } else {
        qDebug() << "Ignored" << key;
    }    
}

359
void FakeVimHandler::Private::handleInsertMode(int key, const QString &text)
hjk's avatar
hjk committed
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
{
    if (key == Key_Escape) {
        m_mode = CommandMode;
        m_tc.movePosition(Left, KeepAnchor, qMin(1, leftDist()));
    } else if (key == Key_Left) {
        m_tc.movePosition(Left, MoveAnchor, 1);
    } else if (key == Key_Down) {
        m_tc.movePosition(Down, MoveAnchor, 1);
    } else if (key == Key_Up) {
        m_tc.movePosition(Up, MoveAnchor, 1);
    } else if (key == Key_Right) {
        m_tc.movePosition(Right, MoveAnchor, 1);
    } else if (key == Key_Return) {
        m_tc.insertBlock();
    } else if (key == Key_Backspace) {
        m_tc.deletePreviousChar();
376
377
    } else if (key == Key_Delete) {
        m_tc.deleteChar();
hjk's avatar
hjk committed
378
    } else {
379
        m_tc.insertText(text);
hjk's avatar
hjk committed
380
381
382
    }    
}

383
void FakeVimHandler::Private::handleExMode(int key, const QString &text)
hjk's avatar
hjk committed
384
{
385
386
    Q_UNUSED(text)

hjk's avatar
hjk committed
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
    if (key == Key_Escape) {
        m_commandBuffer.clear();
        m_commandCode = 0;
        m_mode = CommandMode;
    } else if (key == Key_Backspace) {
        if (m_commandBuffer.isEmpty())
            m_mode = CommandMode;
        else
            m_commandBuffer.chop(1);
    } else if (key == Key_Return && m_commandCode == ':') {
        static QRegExp reGoto("^(\\d+)$");
        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") {
            q->quitRequested(m_editor);
        }
        m_commandBuffer.clear();
        m_mode = CommandMode;
    } else if (key == Key_Return
            && (m_commandCode == '/' || m_commandCode == '?')) {
        m_lastSearchBackward = (m_commandCode == '?');
        m_lastSearchString = m_commandBuffer;
        search(m_lastSearchString, m_lastSearchBackward);
        m_commandBuffer.clear();
        m_commandCode = 0;
        m_mode = CommandMode;
    } else {
        m_commandBuffer += QChar(key);
    }
    updateCommandBuffer();
}

void FakeVimHandler::Private::search(const QString &needle, bool backwards)
{
    //qDebug() << "NEEDLE " << needle << "BACKWARDS" << backwards;
    //int startPos = m_tc.position();
    QTextDocument::FindFlags flags;
    if (backwards)
        flags = QTextDocument::FindBackward;

    m_tc.movePosition(backwards? Left : Right, MoveAnchor, 1);
    m_editor->setTextCursor(m_tc);
    if (m_editor->find(needle, flags)) {
        m_tc = m_editor->textCursor();
        m_tc.movePosition(Left, MoveAnchor, needle.size());
        return;
    }

    int n = 0;
    if (backwards) {
        QTextBlock block = m_tc.block().document()->lastBlock();
        n = block.position() + block.length() - 1;
    }
    m_tc.setPosition(n);
    m_editor->setTextCursor(m_tc);
    if (m_editor->find(needle, flags)) {
        m_tc = m_editor->textCursor();
        m_tc.movePosition(Left, MoveAnchor, needle.size());
        if (backwards)
            showMessage("search hit TOP, continuing at BOTTOM");
        else
            showMessage("search hit BOTTOM, continuing at TOP");
        return;
    }

    m_tc.movePosition(backwards ? Right : Left, MoveAnchor, 1);
}

hjk's avatar
hjk committed
457
458
459
460
461
462
463
464
465
466
467
468
void FakeVimHandler::Private::moveToFirstNonBlankOnLine()
{
    QTextBlock block = m_tc.block();
    QTextDocument *doc = m_tc.document();
    int firstPos = m_tc.position();
    for (int i = firstPos, n = firstPos + block.length(); i < n; ++i) {
        if (!doc->characterAt(i).isSpace()) {
            m_tc.setPosition(i, KeepAnchor);
            return;
        }
    }
}
hjk's avatar
hjk committed
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491

///////////////////////////////////////////////////////////////////////
//
// FakeVimHandler
//
///////////////////////////////////////////////////////////////////////

FakeVimHandler::FakeVimHandler(QObject *parent)
    : QObject(parent), d(new Private(this))
{}

FakeVimHandler::~FakeVimHandler()
{
    delete d;
}

bool FakeVimHandler::eventFilter(QObject *ob, QEvent *ev)
{
    if (ev->type() != QEvent::KeyPress)
        return QObject::eventFilter(ob, ev);
    return d->eventFilter(ob, ev);
}