Files
qt-creator/src/plugins/fakevim/fakevimhandler.cpp

2103 lines
66 KiB
C++
Raw Normal View History

/***************************************************************************
**
** This file is part of Qt Creator
**
2009-01-13 19:21:51 +01:00
** Copyright (c) 2008-2009 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 "fakevimhandler.h"
#include "fakevimconstants.h"
// Please do not add any direct dependencies to other Qt Creator code here.
// Instead emit signals and let the FakeVimPlugin channel the information to
// Qt Creator. The idea is to keep this file here in a "clean" state that
// allows easy reuse with any QTextEdit or QPlainTextEdit derived class.
//#include <indenter.h>
2008-12-19 16:20:39 +01:00
#include <QtCore/QDebug>
#include <QtCore/QFile>
2008-12-19 16:20:39 +01:00
#include <QtCore/QObject>
#include <QtCore/QPointer>
2009-01-08 17:21:51 +01:00
#include <QtCore/QProcess>
2008-12-19 16:20:39 +01:00
#include <QtCore/QRegExp>
#include <QtCore/QTextStream>
2009-01-16 09:56:08 +01:00
#include <QtCore/QtAlgorithms>
2008-12-19 16:20:39 +01:00
#include <QtCore/QStack>
2009-01-08 17:21:51 +01:00
#include <QtGui/QApplication>
2008-12-19 16:20:39 +01:00
#include <QtGui/QKeyEvent>
#include <QtGui/QLineEdit>
#include <QtGui/QPlainTextEdit>
#include <QtGui/QScrollBar>
#include <QtGui/QTextBlock>
#include <QtGui/QTextCursor>
2009-01-06 11:33:07 +01:00
#include <QtGui/QTextDocumentFragment>
2008-12-19 16:20:39 +01:00
#include <QtGui/QTextEdit>
using namespace FakeVim::Internal;
using namespace FakeVim::Constants;
#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
#define EndOfDocument QTextCursor::End
///////////////////////////////////////////////////////////////////////
//
// FakeVimHandler
//
///////////////////////////////////////////////////////////////////////
#define EDITOR(s) (m_textedit ? m_textedit->s : m_plaintextedit->s)
const int ParagraphSeparator = 0x00002029;
using namespace Qt;
2008-12-19 16:20:39 +01:00
enum Mode
{
InsertMode,
CommandMode,
ExMode,
SearchForwardMode,
SearchBackwardMode,
PassingMode, // lets keyevents to be passed to the main application
2008-12-19 16:20:39 +01:00
};
2008-12-19 16:20:39 +01:00
enum SubMode
{
NoSubMode,
RegisterSubMode,
ChangeSubMode,
DeleteSubMode,
2009-01-08 17:21:51 +01:00
FilterSubMode,
2009-01-22 15:08:50 +01:00
ReplaceSubMode, // used for R and r
2008-12-23 21:34:21 +01:00
YankSubMode,
IndentSubMode,
ZSubMode,
2008-12-19 16:20:39 +01:00
};
2008-12-26 00:18:03 +01:00
enum SubSubMode
{
2009-01-22 15:08:50 +01:00
// typically used for things that require one more data item
// and are 'nested' behind a mode
2008-12-26 00:18:03 +01:00
NoSubSubMode,
FtSubSubMode, // used for f, F, t, T
MarkSubSubMode, // used for m
BackTickSubSubMode, // used for `
TickSubSubMode, // used for '
2008-12-26 00:18:03 +01:00
};
enum VisualMode
{
NoVisualMode,
VisualCharMode,
VisualLineMode,
VisualBlockMode,
};
2009-01-16 16:57:00 +01:00
enum MoveType
{
MoveExclusive,
MoveInclusive,
2009-01-16 17:38:15 +01:00
MoveLineWise,
2009-01-16 16:57:00 +01:00
};
struct EditOperation
{
EditOperation() : position(-1), itemCount(0) {}
int position;
int itemCount; // used to combine several operations
QString from;
QString to;
};
2009-01-16 17:44:24 +01:00
QDebug &operator<<(QDebug &ts, const EditOperation &op)
2009-01-06 11:48:55 +01:00
{
if (op.itemCount > 0) {
ts << "\n EDIT BLOCK WITH " << op.itemCount << " ITEMS";
2009-01-06 11:48:55 +01:00
} else {
ts << "\n EDIT AT " << op.position
<< "\n FROM " << op.from << "\n TO " << op.to;
2009-01-06 11:48:55 +01:00
}
return ts;
}
2009-01-16 16:15:01 +01:00
int lineCount(const QString &text)
{
//return text.count(QChar(ParagraphSeparator));
return text.count(QChar('\n'));
}
class FakeVimHandler::Private
{
public:
Private(FakeVimHandler *parent, QWidget *widget);
bool handleEvent(QKeyEvent *ev);
void handleExCommand(const QString &cmd);
void setupWidget();
void restoreWidget();
private:
friend class FakeVimHandler;
static int shift(int key) { return key + 32; }
static int control(int key) { return key + 256; }
void init();
bool handleKey(int key, int unmodified, const QString &text);
bool handleInsertMode(int key, int unmodified, const QString &text);
bool handleCommandMode(int key, int unmodified, const QString &text);
bool handleRegisterMode(int key, int unmodified, const QString &text);
bool handleMiniBufferModes(int key, int unmodified, const QString &text);
2009-01-06 11:43:49 +01:00
void finishMovement(const QString &text = QString());
2008-12-26 00:18:03 +01:00
void search(const QString &needle, bool forward);
2008-12-25 22:41:09 +01:00
int mvCount() const { return m_mvcount.isEmpty() ? 1 : m_mvcount.toInt(); }
int opCount() const { return m_opcount.isEmpty() ? 1 : m_opcount.toInt(); }
int count() const { return mvCount() * opCount(); }
int leftDist() const { return m_tc.position() - m_tc.block().position(); }
int rightDist() const { return m_tc.block().length() - leftDist() - 1; }
bool atEndOfLine() const { return m_tc.atBlockEnd() && m_tc.block().length()>1; }
2008-12-19 16:20:39 +01:00
int lastPositionInDocument() const;
int positionForLine(int line) const; // 1 based line, 0 based pos
int lineForPosition(int pos) const; // 1 based line, 0 based pos
2009-01-06 11:11:31 +01:00
// all zero-based counting
int cursorLineOnScreen() const;
int linesOnScreen() const;
int columnsOnScreen() const;
int cursorLineInDocument() const;
int cursorColumnInDocument() const;
2008-12-28 02:15:26 +01:00
int linesInDocument() const;
2009-01-06 11:11:31 +01:00
void scrollToLineInDocument(int line);
// helper functions for indenting
2009-01-16 14:11:44 +01:00
bool isElectricCharacter(QChar c) const
{ return (c == '{' || c == '}' || c == '#'); }
int indentDist() const;
void indentRegion(QTextBlock first, QTextBlock last, QChar typedChar=0);
void indentCurrentLine(QChar typedChar);
2008-12-19 14:35:57 +01:00
void moveToFirstNonBlankOnLine();
void moveToDesiredColumn();
2008-12-26 00:18:03 +01:00
void moveToNextWord(bool simple);
void moveToMatchingParanthesis();
2008-12-26 00:18:03 +01:00
void moveToWordBoundary(bool simple, bool forward);
2009-01-16 14:11:44 +01:00
// to reduce line noise
typedef QTextCursor::MoveOperation MoveOperation;
typedef QTextCursor::MoveMode MoveMode;
2009-01-16 17:51:44 +01:00
void moveToEndOfDocument() { m_tc.movePosition(EndOfDocument, MoveAnchor); }
void moveToStartOfLine() { m_tc.movePosition(StartOfLine, MoveAnchor); }
void moveToEndOfLine() { m_tc.movePosition(EndOfLine, MoveAnchor); }
void moveUp(int n = 1) { m_tc.movePosition(Up, MoveAnchor, n); }
void moveDown(int n = 1) { m_tc.movePosition(Down, MoveAnchor, n); }
void moveRight(int n = 1) { m_tc.movePosition(Right, MoveAnchor, n); }
void moveLeft(int n = 1) { m_tc.movePosition(Left, MoveAnchor, n); }
2009-01-16 16:15:01 +01:00
void setAnchor() { m_anchor = m_tc.position(); }
QString selectedText() const;
2009-01-16 14:11:44 +01:00
2008-12-26 00:18:03 +01:00
void handleFfTt(int key);
2008-12-28 02:15:26 +01:00
// helper function for handleCommand. return 1 based line index.
int readLineCode(QString &cmd);
2009-01-16 16:15:01 +01:00
void selectRange(int beginLine, int endLine);
2008-12-28 02:15:26 +01:00
void enterInsertMode();
void enterCommandMode();
2009-01-08 17:40:27 +01:00
void showRedMessage(const QString &msg);
void showBlackMessage(const QString &msg);
void notImplementedYet();
void updateMiniBuffer();
void updateSelection();
2009-01-07 18:05:45 +01:00
void quit();
QWidget *editor() const;
public:
QTextEdit *m_textedit;
QPlainTextEdit *m_plaintextedit;
bool m_wasReadOnly; // saves read-only state of document
FakeVimHandler *q;
Mode m_mode;
SubMode m_submode;
2008-12-26 00:18:03 +01:00
SubSubMode m_subsubmode;
int m_subsubdata;
QString m_input;
QTextCursor m_tc;
2009-01-16 16:15:01 +01:00
int m_anchor;
QHash<int, QString> m_registers;
int m_register;
2008-12-25 22:41:09 +01:00
QString m_mvcount;
QString m_opcount;
2009-01-16 16:57:00 +01:00
MoveType m_moveType;
bool m_fakeEnd;
bool isSearchMode() const
{ return m_mode == SearchForwardMode || m_mode == SearchBackwardMode; }
2008-12-27 13:39:34 +01:00
int m_gflag; // whether current command started with 'g'
QString m_commandBuffer;
QString m_currentFileName;
QString m_currentMessage;
bool m_lastSearchForward;
2008-12-27 12:24:50 +01:00
QString m_lastInsertion;
2008-12-26 17:01:21 +01:00
// undo handling
2009-01-08 17:21:51 +01:00
void recordOperation(const EditOperation &op);
void recordInsert(int position, const QString &data);
2009-01-06 11:48:55 +01:00
void recordRemove(int position, const QString &data);
void recordRemove(int position, int length);
2009-01-16 13:10:42 +01:00
void recordRemoveNextChar();
void recordInsertText(const QString &data);
2009-01-16 16:15:01 +01:00
QString recordRemoveSelectedText();
void recordMove();
2009-01-16 13:10:42 +01:00
void recordBeginGroup();
void recordEndGroup();
2009-01-16 16:15:01 +01:00
int anchor() const { return m_anchor; }
int position() const { return m_tc.position(); }
2009-01-16 13:10:42 +01:00
void undo();
void redo();
QStack<EditOperation> m_undoStack;
QStack<EditOperation> m_redoStack;
2009-01-16 13:10:42 +01:00
QStack<int> m_undoGroupStack;
2009-01-06 11:43:49 +01:00
// extra data for '.'
QString m_dotCommand;
// history for '/'
2008-12-26 17:01:21 +01:00
QString lastSearchString() const;
QStringList m_searchHistory;
int m_searchHistoryIndex;
// history for ':'
2008-12-26 17:01:21 +01:00
QStringList m_commandHistory;
int m_commandHistoryIndex;
2008-12-27 21:28:22 +01:00
// visual line mode
void enterVisualMode(VisualMode visualMode);
void leaveVisualMode();
VisualMode m_visualMode;
// marks as lines
QHash<int, int> m_marks;
2008-12-27 21:28:22 +01:00
// vi style configuration
QHash<QString, QString> m_config;
2008-12-23 21:34:21 +01:00
// for restoring cursor position
2009-01-16 17:38:15 +01:00
int m_savedYankPosition;
int m_desiredColumn;
QPointer<QObject> m_extraData;
};
FakeVimHandler::Private::Private(FakeVimHandler *parent, QWidget *widget)
{
q = parent;
2008-12-27 22:50:58 +01:00
m_textedit = qobject_cast<QTextEdit *>(widget);
m_plaintextedit = qobject_cast<QPlainTextEdit *>(widget);
m_mode = CommandMode;
2008-12-26 00:18:03 +01:00
m_submode = NoSubMode;
m_subsubmode = NoSubSubMode;
m_fakeEnd = false;
m_lastSearchForward = true;
m_register = '"';
2008-12-27 13:39:34 +01:00
m_gflag = false;
m_visualMode = NoVisualMode;
m_desiredColumn = 0;
2009-01-16 16:57:00 +01:00
m_moveType = MoveInclusive;
2009-01-16 17:38:15 +01:00
m_anchor = 0;
m_savedYankPosition = 0;
2008-12-27 21:28:22 +01:00
m_config[ConfigStartOfLine] = ConfigOn;
m_config[ConfigTabStop] = "8";
m_config[ConfigSmartTab] = ConfigOff;
m_config[ConfigShiftWidth] = "8";
m_config[ConfigExpandTab] = ConfigOff;
m_config[ConfigAutoIndent] = ConfigOff;
}
bool FakeVimHandler::Private::handleEvent(QKeyEvent *ev)
{
int key = ev->key();
const int um = key; // keep unmodified key around
// FIXME
if (m_mode == PassingMode && key != Qt::Key_Control && key != Qt::Key_Shift) {
if (key == ',') { // use ',,' to leave, too.
quit();
return true;
}
m_mode = CommandMode;
return false;
}
if (key == Key_Shift || key == Key_Alt || key == Key_Control
|| key == Key_Alt || key == Key_AltGr || key == Key_Meta)
return false;
// Fake "End of line"
m_tc = EDITOR(textCursor());
m_tc.setVisualNavigation(true);
2008-12-27 22:50:58 +01:00
if (m_fakeEnd)
2009-01-16 17:38:15 +01:00
moveRight();
2008-12-24 16:20:31 +01:00
if ((ev->modifiers() & Qt::ControlModifier) != 0) {
key += 256;
2008-12-24 16:20:31 +01:00
key += 32; // make it lower case
} else if (key >= Key_A && key <= Key_Z
&& (ev->modifiers() & Qt::ShiftModifier) == 0) {
key += 32;
}
bool handled = handleKey(key, um, ev->text());
// We fake vi-style end-of-line behaviour
m_fakeEnd = (atEndOfLine() && m_mode == CommandMode);
2008-12-27 22:50:58 +01:00
if (m_fakeEnd)
2009-01-16 17:38:15 +01:00
moveLeft();
EDITOR(setTextCursor(m_tc));
EDITOR(ensureCursorVisible());
2009-01-08 13:43:24 +01:00
return handled;
}
void FakeVimHandler::Private::setupWidget()
{
enterCommandMode();
if (m_textedit) {
m_textedit->installEventFilter(q);
//m_textedit->setCursorWidth(QFontMetrics(ed->font()).width(QChar('x')));
m_textedit->setLineWrapMode(QTextEdit::NoWrap);
m_wasReadOnly = m_textedit->isReadOnly();
} else if (m_plaintextedit) {
m_plaintextedit->installEventFilter(q);
//plaintextedit->setCursorWidth(QFontMetrics(ed->font()).width(QChar('x')));
m_plaintextedit->setLineWrapMode(QPlainTextEdit::NoWrap);
m_wasReadOnly = m_plaintextedit->isReadOnly();
}
showBlackMessage("vi emulation mode.");
updateMiniBuffer();
}
void FakeVimHandler::Private::restoreWidget()
{
//showBlackMessage(QString());
//updateMiniBuffer();
if (m_textedit) {
m_textedit->removeEventFilter(q);
m_textedit->setReadOnly(m_wasReadOnly);
} else if (m_plaintextedit) {
m_plaintextedit->removeEventFilter(q);
m_plaintextedit->setReadOnly(m_wasReadOnly);
}
}
bool FakeVimHandler::Private::handleKey(int key, int unmodified, const QString &text)
{
//qDebug() << "KEY: " << key << text << "POS: " << m_tc.position();
//qDebug() << "\nUNDO: " << m_undoStack << "\nREDO: " << m_redoStack;
if (m_mode == InsertMode)
return handleInsertMode(key, unmodified, text);
if (m_mode == CommandMode)
return handleCommandMode(key, unmodified, text);
if (m_mode == ExMode || m_mode == SearchForwardMode
|| m_mode == SearchBackwardMode)
return handleMiniBufferModes(key, unmodified, text);
return false;
}
2009-01-06 11:43:49 +01:00
void FakeVimHandler::Private::finishMovement(const QString &dotCommand)
{
2009-01-08 17:21:51 +01:00
if (m_submode == FilterSubMode) {
2009-01-16 16:15:01 +01:00
int beginLine = lineForPosition(anchor());
int endLine = lineForPosition(position());
m_tc.setPosition(qMin(anchor(), position()));
2009-01-08 17:21:51 +01:00
m_mode = ExMode;
m_commandBuffer = QString(".,+%1!").arg(qAbs(endLine - beginLine));
m_commandHistory.append(QString());
m_commandHistoryIndex = m_commandHistory.size() - 1;
updateMiniBuffer();
return;
}
if (m_visualMode != NoVisualMode)
m_marks['>'] = m_tc.position();
if (m_submode == ChangeSubMode) {
2009-01-06 11:43:49 +01:00
if (!dotCommand.isEmpty())
m_dotCommand = "c" + dotCommand;
2009-01-16 16:15:01 +01:00
QString text = recordRemoveSelectedText();
2009-01-16 16:42:31 +01:00
qDebug() << "CHANGING TO INSERT MODE" << text;
2009-01-16 16:15:01 +01:00
m_registers[m_register] = text;
m_mode = InsertMode;
m_submode = NoSubMode;
} else if (m_submode == DeleteSubMode) {
if (m_moveType == MoveInclusive)
moveRight(); // correct
2009-01-06 11:43:49 +01:00
if (!dotCommand.isEmpty())
m_dotCommand = "d" + dotCommand;
2009-01-16 16:15:01 +01:00
m_registers[m_register] = recordRemoveSelectedText();
recordEndGroup();
m_submode = NoSubMode;
if (atEndOfLine())
2009-01-16 17:38:15 +01:00
moveLeft();
2008-12-23 21:34:21 +01:00
} else if (m_submode == YankSubMode) {
2009-01-16 17:38:15 +01:00
m_registers[m_register] = selectedText();
m_tc.setPosition(m_savedYankPosition);
2008-12-23 21:34:21 +01:00
m_submode = NoSubMode;
} else if (m_submode == ReplaceSubMode) {
m_submode = NoSubMode;
} else if (m_submode == IndentSubMode) {
QTextDocument *doc = EDITOR(document());
int start = m_tc.selectionStart();
int end = m_tc.selectionEnd();
if (start > end)
2009-01-16 09:56:08 +01:00
qSwap(start, end);
QTextBlock startBlock = doc->findBlock(start);
indentRegion(doc->findBlock(start), doc->findBlock(end).next());
m_tc.setPosition(startBlock.position());
moveToFirstNonBlankOnLine();
m_submode = NoSubMode;
}
m_moveType = MoveInclusive;
2008-12-25 22:41:09 +01:00
m_mvcount.clear();
m_opcount.clear();
2008-12-27 13:39:34 +01:00
m_gflag = false;
m_register = '"';
m_tc.clearSelection();
updateSelection();
2008-12-27 21:01:05 +01:00
updateMiniBuffer();
m_desiredColumn = leftDist();
}
void FakeVimHandler::Private::updateSelection()
{
QList<QTextEdit::ExtraSelection> selections;
if (m_visualMode != NoVisualMode) {
QTextEdit::ExtraSelection sel;
sel.cursor = m_tc;
sel.format = m_tc.blockCharFormat();
2009-01-08 17:40:27 +01:00
//sel.format.setFontWeight(QFont::Bold);
//sel.format.setFontUnderline(true);
sel.format.setForeground(Qt::white);
sel.format.setBackground(Qt::black);
int cursorPos = m_tc.position();
int anchorPos = m_marks['<'];
//qDebug() << "POS: " << cursorPos << " ANCHOR: " << anchorPos;
if (m_visualMode == VisualCharMode) {
sel.cursor.setPosition(anchorPos, KeepAnchor);
selections.append(sel);
} else if (m_visualMode == VisualLineMode) {
sel.cursor.setPosition(qMin(cursorPos, anchorPos), MoveAnchor);
sel.cursor.movePosition(StartOfLine, MoveAnchor);
sel.cursor.setPosition(qMax(cursorPos, anchorPos), KeepAnchor);
sel.cursor.movePosition(EndOfLine, KeepAnchor);
selections.append(sel);
} else if (m_visualMode == VisualBlockMode) {
QTextCursor tc = m_tc;
tc.setPosition(anchorPos);
tc.movePosition(StartOfLine, MoveAnchor);
QTextBlock anchorBlock = tc.block();
QTextBlock cursorBlock = m_tc.block();
int anchorColumn = anchorPos - anchorBlock.position();
int cursorColumn = cursorPos - cursorBlock.position();
int startColumn = qMin(anchorColumn, cursorColumn);
int endColumn = qMax(anchorColumn, cursorColumn);
int endPos = cursorBlock.position();
while (tc.position() <= endPos) {
if (startColumn < tc.block().length() - 1) {
int last = qMin(tc.block().length() - 1, endColumn);
int len = last - startColumn + 1;
sel.cursor = tc;
sel.cursor.movePosition(Right, MoveAnchor, startColumn);
sel.cursor.movePosition(Right, KeepAnchor, len);
selections.append(sel);
}
tc.movePosition(Down, MoveAnchor, 1);
}
}
}
emit q->selectionChanged(selections);
}
2008-12-27 21:01:05 +01:00
void FakeVimHandler::Private::updateMiniBuffer()
{
2008-12-27 21:01:05 +01:00
QString msg;
if (m_mode == PassingMode) {
msg = "-- PASSING --";
} else if (!m_currentMessage.isEmpty()) {
msg = m_currentMessage;
m_currentMessage.clear();
2009-01-08 17:21:51 +01:00
} else if (m_mode == CommandMode && m_visualMode != NoVisualMode) {
if (m_visualMode == VisualCharMode) {
msg = "-- VISUAL --";
} else if (m_visualMode == VisualLineMode) {
msg = "-- VISUAL LINE --";
} else if (m_visualMode == VisualBlockMode) {
msg = "-- VISUAL BLOCK --";
}
} else if (m_mode == InsertMode) {
msg = "-- INSERT --";
} else {
if (m_mode == SearchForwardMode)
msg += '/';
else if (m_mode == SearchBackwardMode)
msg += '?';
else if (m_mode == ExMode)
msg += ':';
2009-01-06 11:51:03 +01:00
foreach (QChar c, m_commandBuffer) {
if (c.unicode() < 32) {
msg += '^';
msg += QChar(c.unicode() + 64);
} else {
msg += c;
}
2008-12-27 22:41:47 +01:00
}
2009-01-08 17:21:51 +01:00
if (!msg.isEmpty() && m_mode != CommandMode)
2009-01-08 17:40:27 +01:00
msg += QChar(10073); // '|'; // FIXME: Use a real "cursor"
2008-12-27 22:41:47 +01:00
}
2009-01-07 18:05:45 +01:00
emit q->commandBufferChanged(msg);
int linesInDoc = linesInDocument();
int l = cursorLineInDocument();
QString status;
2008-12-27 21:01:05 +01:00
QString pos = tr("%1,%2").arg(l + 1).arg(cursorColumnInDocument() + 1);
2009-01-07 18:05:45 +01:00
status += tr("%1").arg(pos, -10);
2008-12-27 21:01:05 +01:00
// FIXME: physical "-" logical
2009-01-06 11:51:03 +01:00
if (linesInDoc != 0) {
2009-01-07 18:05:45 +01:00
status += tr("%1").arg(l * 100 / linesInDoc, 4);
status += "%";
2009-01-06 11:51:03 +01:00
} else {
2009-01-07 18:05:45 +01:00
status += "All";
2009-01-06 11:51:03 +01:00
}
2009-01-07 18:05:45 +01:00
emit q->statusDataChanged(status);
}
2009-01-08 17:40:27 +01:00
void FakeVimHandler::Private::showRedMessage(const QString &msg)
{
//qDebug() << "MSG: " << msg;
m_currentMessage = msg;
2008-12-27 21:01:05 +01:00
updateMiniBuffer();
}
2009-01-08 17:40:27 +01:00
void FakeVimHandler::Private::showBlackMessage(const QString &msg)
{
//qDebug() << "MSG: " << msg;
m_commandBuffer = msg;
updateMiniBuffer();
}
void FakeVimHandler::Private::notImplementedYet()
{
showRedMessage("Not implemented in FakeVim");
updateMiniBuffer();
}
bool FakeVimHandler::Private::handleCommandMode(int key, int unmodified,
const QString &text)
{
bool handled = true;
if (m_submode == RegisterSubMode) {
m_register = key;
m_submode = NoSubMode;
} else if (m_submode == ChangeSubMode && key == 'c') {
2009-01-16 16:15:01 +01:00
moveToStartOfLine();
setAnchor();
moveDown(count());
moveLeft();
2009-01-16 16:57:00 +01:00
m_registers[m_register] = recordRemoveSelectedText();
2009-01-16 16:15:01 +01:00
m_submode = NoSubMode;
m_mode = InsertMode;
2009-01-06 11:43:49 +01:00
finishMovement("c");
} else if (m_submode == DeleteSubMode && key == 'd') {
2009-01-16 16:15:01 +01:00
moveToStartOfLine();
2009-01-16 16:57:00 +01:00
setAnchor();
2009-01-16 16:15:01 +01:00
moveDown(count());
m_registers[m_register] = recordRemoveSelectedText();
2009-01-06 11:43:49 +01:00
finishMovement("d");
2008-12-23 21:34:21 +01:00
} else if (m_submode == YankSubMode && key == 'y') {
2009-01-16 16:15:01 +01:00
moveToStartOfLine();
2009-01-16 17:38:15 +01:00
setAnchor();
2009-01-16 16:15:01 +01:00
moveDown(count());
2009-01-16 17:38:15 +01:00
m_moveType = MoveLineWise;
finishMovement("y");
} else if (m_submode == IndentSubMode && key == '=') {
indentRegion(m_tc.block(), m_tc.block().next());
finishMovement();
2008-12-19 16:20:39 +01:00
} else if (m_submode == ZSubMode) {
if (key == Key_Return) {
2008-12-25 13:20:09 +01:00
// cursor line to top of window, cursor on first non-blank
2009-01-06 11:11:31 +01:00
scrollToLineInDocument(cursorLineInDocument());
2008-12-19 16:20:39 +01:00
moveToFirstNonBlankOnLine();
finishMovement();
2008-12-25 13:20:09 +01:00
} else {
2009-01-16 09:56:08 +01:00
qDebug() << "IGNORED Z_MODE " << key << text;
2008-12-19 16:20:39 +01:00
}
m_submode = NoSubMode;
2008-12-26 00:18:03 +01:00
} else if (m_subsubmode == FtSubSubMode) {
handleFfTt(key);
m_subsubmode = NoSubSubMode;
2009-01-06 11:43:49 +01:00
finishMovement(QString(QChar(m_subsubdata)) + QChar(key));
2009-01-22 15:08:50 +01:00
} else if (m_submode == ReplaceSubMode) {
if (count() < rightDist() && text.size() == 1
&& (text.at(0).isPrint() || text.at(0).isSpace())) {
recordBeginGroup();
setAnchor();
moveRight(count());
recordRemoveSelectedText();
recordInsertText(QString(count(), text.at(0)));
recordEndGroup();
m_moveType = MoveExclusive;
2009-01-22 15:08:50 +01:00
m_submode = NoSubMode;
m_dotCommand = QString("%1r%2").arg(count()).arg(text);
finishMovement();
} else {
2009-01-22 15:08:50 +01:00
m_submode = NoSubMode;
}
} else if (m_subsubmode == MarkSubSubMode) {
m_marks[key] = m_tc.position();
m_subsubmode = NoSubSubMode;
} else if (m_subsubmode == BackTickSubSubMode
|| m_subsubmode == TickSubSubMode) {
if (m_marks.contains(key)) {
2009-01-16 17:38:15 +01:00
m_tc.setPosition(m_marks[key]);
if (m_subsubmode == TickSubSubMode)
moveToFirstNonBlankOnLine();
finishMovement();
} else {
2009-01-08 17:40:27 +01:00
showRedMessage(tr("E20: Mark '%1' not set").arg(text));
}
m_subsubmode = NoSubSubMode;
} else if (key >= '0' && key <= '9') {
2008-12-25 22:41:09 +01:00
if (key == '0' && m_mvcount.isEmpty()) {
2008-12-28 02:49:14 +01:00
moveToFirstNonBlankOnLine();
finishMovement();
} else {
2008-12-25 22:41:09 +01:00
m_mvcount.append(QChar(key));
}
} else if (key == ':') {
m_mode = ExMode;
2009-01-06 11:50:30 +01:00
m_commandBuffer.clear();
if (m_visualMode != NoVisualMode)
m_commandBuffer = "'<,'>";
m_commandHistory.append(QString());
m_commandHistoryIndex = m_commandHistory.size() - 1;
updateMiniBuffer();
} else if (key == '/' || key == '?') {
m_mode = (key == '/') ? SearchForwardMode : SearchBackwardMode;
m_commandBuffer.clear();
m_searchHistory.append(QString());
m_searchHistoryIndex = m_searchHistory.size() - 1;
2008-12-27 21:01:05 +01:00
updateMiniBuffer();
} else if (key == '`') {
m_subsubmode = BackTickSubSubMode;
2009-01-16 09:56:08 +01:00
} else if (key == '#' || key == '*') {
// FIXME: That's not proper vim behaviour
m_tc.select(QTextCursor::WordUnderCursor);
QString needle = "\\<" + m_tc.selection().toPlainText() + "\\>";
m_searchHistory.append(needle);
m_lastSearchForward = (key == '*');
updateMiniBuffer();
search(needle, m_lastSearchForward);
} else if (key == '\'') {
m_subsubmode = TickSubSubMode;
} else if (key == '|') {
2009-01-16 16:15:01 +01:00
setAnchor();
moveToStartOfLine();
moveRight(qMin(count(), rightDist()) - 1);
finishMovement();
2009-01-08 17:21:51 +01:00
} else if (key == '!' && m_visualMode == NoVisualMode) {
m_submode = FilterSubMode;
} else if (key == '!' && m_visualMode == VisualLineMode) {
m_mode = ExMode;
m_commandBuffer = "'<,'>!";
m_commandHistory.append(QString());
m_commandHistoryIndex = m_commandHistory.size() - 1;
updateMiniBuffer();
} else if (key == '"') {
m_submode = RegisterSubMode;
} else if (unmodified == Key_Return) {
2009-01-16 16:15:01 +01:00
moveToStartOfLine();
moveDown();
moveToFirstNonBlankOnLine();
finishMovement();
} else if (key == Key_Home) {
2009-01-16 16:15:01 +01:00
moveToStartOfLine();
finishMovement();
} else if (key == '$' || key == Key_End) {
int submode = m_submode;
2009-01-16 16:15:01 +01:00
moveToEndOfLine();
finishMovement();
if (submode == NoSubMode)
m_desiredColumn = -1;
} else if (key == ',') {
// FIXME: use some other mechanism
m_mode = PassingMode;
updateMiniBuffer();
2009-01-06 11:43:49 +01:00
} else if (key == '.') {
qDebug() << "REPEATING" << m_dotCommand;
for (int i = count(); --i >= 0; )
foreach (QChar c, m_dotCommand)
handleKey(c.unicode(), c.unicode(), QString(c));
} else if (key == '=') {
m_submode = IndentSubMode;
} else if (key == '%') {
moveToMatchingParanthesis();
finishMovement();
2008-12-27 21:51:06 +01:00
} else if (key == 'a') {
2008-12-27 22:22:16 +01:00
m_mode = InsertMode;
2009-01-16 16:15:01 +01:00
recordBeginGroup();
2008-12-27 21:51:06 +01:00
m_lastInsertion.clear();
if (!atEndOfLine())
moveRight();
updateMiniBuffer();
} else if (key == 'A') {
2008-12-27 22:22:16 +01:00
m_mode = InsertMode;
2009-01-16 16:15:01 +01:00
moveToEndOfLine();
recordBeginGroup();
2008-12-27 21:51:06 +01:00
m_lastInsertion.clear();
2008-12-25 19:50:14 +01:00
} else if (key == 'b') {
m_moveType = MoveExclusive;
2008-12-26 00:18:03 +01:00
moveToWordBoundary(false, false);
2008-12-25 19:50:14 +01:00
finishMovement();
} else if (key == 'B') {
m_moveType = MoveExclusive;
2008-12-26 00:18:03 +01:00
moveToWordBoundary(true, false);
2008-12-25 19:50:14 +01:00
finishMovement();
} else if (key == 'c') {
2009-01-16 16:15:01 +01:00
setAnchor();
recordBeginGroup();
m_submode = ChangeSubMode;
} else if (key == 'C') {
2009-01-16 16:15:01 +01:00
setAnchor();
recordBeginGroup();
moveToEndOfLine();
m_registers[m_register] = recordRemoveSelectedText();
m_mode = InsertMode;
finishMovement();
} else if (key == 'd' && m_visualMode == NoVisualMode) {
if (atEndOfLine())
2009-01-16 16:15:01 +01:00
moveLeft();
setAnchor();
recordBeginGroup();
2009-01-06 11:43:49 +01:00
m_opcount = m_mvcount;
m_mvcount.clear();
m_submode = DeleteSubMode;
} else if (key == 'd') {
2009-01-16 16:15:01 +01:00
setAnchor();
leaveVisualMode();
int beginLine = lineForPosition(m_marks['<']);
int endLine = lineForPosition(m_marks['>']);
2009-01-16 16:15:01 +01:00
selectRange(beginLine, endLine);
2009-01-16 13:10:42 +01:00
recordRemoveSelectedText();
} else if (key == 'D') {
2009-01-16 16:15:01 +01:00
setAnchor();
recordBeginGroup();
m_submode = DeleteSubMode;
2009-01-16 16:15:01 +01:00
moveDown(qMax(count() - 1, 0));
moveRight(rightDist());
finishMovement();
2008-12-25 19:50:14 +01:00
} else if (key == 'e') {
m_moveType = MoveInclusive;
2008-12-26 00:18:03 +01:00
moveToWordBoundary(false, true);
2008-12-25 19:50:14 +01:00
finishMovement();
} else if (key == 'E') {
m_moveType = MoveInclusive;
2008-12-26 00:18:03 +01:00
moveToWordBoundary(true, true);
2008-12-25 19:50:14 +01:00
finishMovement();
2008-12-26 00:18:03 +01:00
} else if (key == 'f' || key == 'F') {
m_subsubmode = FtSubSubMode;
m_subsubdata = key;
2008-12-27 13:39:34 +01:00
} else if (key == 'g') {
m_gflag = true;
2008-12-27 21:28:22 +01:00
} else if (key == 'G') {
int n = m_mvcount.isEmpty() ? linesInDocument() : count();
2009-01-02 03:24:09 +01:00
m_tc.setPosition(positionForLine(n), KeepAnchor);
if (m_config[ConfigStartOfLine] == ConfigOn)
2008-12-27 21:28:22 +01:00
moveToFirstNonBlankOnLine();
finishMovement();
2009-01-16 16:15:01 +01:00
} else if (key == 'h' || key == Key_Left
|| key == Key_Backspace || key == control('h')) {
int n = qMin(count(), leftDist());
if (m_fakeEnd && m_tc.block().length() > 1)
++n;
2009-01-16 16:15:01 +01:00
moveLeft(n);
finishMovement();
2008-12-19 14:35:57 +01:00
} else if (key == 'H') {
m_tc = EDITOR(cursorForPosition(QPoint(0, 0)));
2009-01-16 16:15:01 +01:00
moveDown(qMax(count() - 1, 0));
2008-12-19 14:35:57 +01:00
moveToFirstNonBlankOnLine();
finishMovement();
} else if (key == 'i') {
enterInsertMode();
updateMiniBuffer();
if (atEndOfLine())
2009-01-16 17:38:15 +01:00
moveLeft();
2008-12-28 02:44:43 +01:00
} else if (key == 'I') {
2009-01-16 16:15:01 +01:00
setAnchor();
enterInsertMode();
2008-12-28 02:49:14 +01:00
if (m_gflag)
2009-01-16 16:15:01 +01:00
moveToStartOfLine();
2008-12-28 02:49:14 +01:00
else
moveToFirstNonBlankOnLine();
} else if (key == 'j' || key == Key_Down) {
int savedColumn = m_desiredColumn;
2009-01-16 13:10:42 +01:00
if (m_submode == NoSubMode || m_submode == ZSubMode
|| m_submode == RegisterSubMode) {
2009-01-16 16:15:01 +01:00
moveDown(count());
moveToDesiredColumn();
} else {
2009-01-16 16:15:01 +01:00
moveToStartOfLine();
moveDown(count() + 1);
}
finishMovement();
m_desiredColumn = savedColumn;
2008-12-27 13:39:34 +01:00
} else if (key == 'J') {
2009-01-16 13:10:42 +01:00
recordBeginGroup();
2008-12-27 13:39:34 +01:00
if (m_submode == NoSubMode) {
for (int i = qMax(count(), 2) - 1; --i >= 0; ) {
2009-01-16 16:15:01 +01:00
moveToEndOfLine();
2009-01-16 13:10:42 +01:00
recordRemoveNextChar();
2008-12-27 13:50:52 +01:00
if (!m_gflag)
2009-01-16 13:10:42 +01:00
recordInsertText(" ");
2008-12-27 13:39:34 +01:00
}
2008-12-27 13:50:52 +01:00
if (!m_gflag)
2009-01-16 16:15:01 +01:00
moveLeft();
2008-12-27 13:39:34 +01:00
}
2009-01-16 13:10:42 +01:00
recordEndGroup();
} else if (key == 'k' || key == Key_Up) {
int savedColumn = m_desiredColumn;
2009-01-16 16:15:01 +01:00
if (m_submode == NoSubMode || m_submode == ZSubMode
|| m_submode == RegisterSubMode) {
moveUp(count());
moveToDesiredColumn();
} else {
2009-01-16 16:15:01 +01:00
moveToStartOfLine();
moveDown();
moveUp(count() + 1);
}
finishMovement();
m_desiredColumn = savedColumn;
} else if (key == 'l' || key == Key_Right) {
2009-01-16 16:15:01 +01:00
moveRight(qMin(count(), rightDist()));
finishMovement();
2008-12-19 14:43:14 +01:00
} else if (key == 'L') {
2008-12-27 22:50:58 +01:00
m_tc = EDITOR(cursorForPosition(QPoint(0, EDITOR(height()))));
2009-01-16 16:15:01 +01:00
moveUp(qMax(count(), 1));
2008-12-19 14:43:14 +01:00
moveToFirstNonBlankOnLine();
finishMovement();
} else if (key == 'm') {
m_subsubmode = MarkSubSubMode;
2008-12-19 15:00:06 +01:00
} else if (key == 'M') {
2008-12-27 22:50:58 +01:00
m_tc = EDITOR(cursorForPosition(QPoint(0, EDITOR(height()) / 2)));
2008-12-19 15:00:06 +01:00
moveToFirstNonBlankOnLine();
finishMovement();
} else if (key == 'n') {
2008-12-26 17:01:21 +01:00
search(lastSearchString(), m_lastSearchForward);
} else if (key == 'N') {
2008-12-26 17:01:21 +01:00
search(lastSearchString(), !m_lastSearchForward);
} else if (key == 'o' || key == 'O') {
enterInsertMode();
moveToFirstNonBlankOnLine();
2009-01-16 16:15:01 +01:00
recordBeginGroup();
int numSpaces = leftDist();
2009-01-16 16:15:01 +01:00
moveUp();
if (key == 'o')
2009-01-16 16:15:01 +01:00
moveDown();
moveToEndOfLine();
recordInsertText("\n");
moveToStartOfLine();
if (m_config[ConfigAutoIndent] == ConfigOn)
2009-01-16 16:15:01 +01:00
recordInsertText(QString(indentDist(), ' '));
else
2009-01-16 16:15:01 +01:00
recordInsertText(QString(numSpaces, ' '));
recordEndGroup();
2009-01-08 13:43:24 +01:00
} else if (key == 'p' || key == 'P') {
2009-01-16 16:15:01 +01:00
recordBeginGroup();
QString text = m_registers[m_register];
2009-01-16 17:38:15 +01:00
int n = lineCount(text);
//qDebug() << "REGISTERS: " << m_registers << "MOVE: " << m_moveType;
//qDebug() << "LINES: " << n << text << m_register;
if (n > 0) {
recordMove();
2009-01-16 16:15:01 +01:00
moveToStartOfLine();
m_desiredColumn = 0;
for (int i = count(); --i >= 0; ) {
if (key == 'p')
moveDown();
recordInsertText(text);
moveUp(n);
}
} else {
m_desiredColumn = 0;
for (int i = count(); --i >= 0; ) {
if (key == 'p')
moveRight();
recordInsertText(text);
moveLeft();
}
}
2009-01-16 16:15:01 +01:00
recordEndGroup();
m_dotCommand = QString("%1p").arg(count());
finishMovement();
} else if (key == 'r') {
2009-01-22 15:08:50 +01:00
m_submode = ReplaceSubMode;
m_dotCommand = "r";
} else if (key == 'R') {
2009-01-22 15:08:50 +01:00
// FIXME: right now we repeat the insertion count() times,
// but not the deletion
2009-01-16 16:15:01 +01:00
recordBeginGroup();
2009-01-22 15:08:50 +01:00
m_lastInsertion.clear();
m_mode = InsertMode;
m_submode = ReplaceSubMode;
m_dotCommand = "R";
} else if (key == control('r')) {
redo();
2008-12-27 22:22:16 +01:00
} else if (key == 's') {
2009-01-16 16:15:01 +01:00
recordBeginGroup();
2008-12-27 22:22:16 +01:00
m_submode = ChangeSubMode;
2009-01-16 16:15:01 +01:00
moveRight(qMin(count(), rightDist()));
2008-12-26 00:18:03 +01:00
} else if (key == 't' || key == 'T') {
m_subsubmode = FtSubSubMode;
m_subsubdata = key;
} else if (key == 'u') {
undo();
} else if (key == 'U') {
// FIXME: this is non-vim, but as Ctrl-R is taken globally
// we have a substitute here
redo();
} else if (key == 'v') {
enterVisualMode(VisualCharMode);
} else if (key == 'V') {
enterVisualMode(VisualLineMode);
} else if (key == control('v')) {
enterVisualMode(VisualBlockMode);
} else if (key == 'w') {
2009-01-16 16:42:31 +01:00
// Special case: "cw" and "cW" work the same as "ce" and "cE" if the
// cursor is on a non-blank.
if (m_submode == ChangeSubMode)
moveToWordBoundary(false, true);
else
moveToNextWord(false);
2009-01-16 16:57:00 +01:00
m_moveType = MoveExclusive;
2009-01-06 11:43:49 +01:00
finishMovement("w");
} else if (key == 'W') {
2008-12-26 00:18:03 +01:00
moveToNextWord(true);
2009-01-06 11:43:49 +01:00
finishMovement("W");
} else if (key == 'x') { // = "dl"
if (atEndOfLine())
2009-01-16 17:38:15 +01:00
moveLeft();
2009-01-16 16:15:01 +01:00
recordBeginGroup();
2009-01-22 15:15:42 +01:00
setAnchor();
m_submode = DeleteSubMode;
2009-01-16 16:15:01 +01:00
moveRight(qMin(count(), rightDist()));
2009-01-06 11:43:49 +01:00
finishMovement("l");
} else if (key == 'X') {
if (leftDist() > 0) {
2009-01-16 16:15:01 +01:00
setAnchor();
moveLeft(qMin(count(), leftDist()));
recordRemoveSelectedText();
}
finishMovement();
2008-12-23 21:34:21 +01:00
} else if (key == 'y') {
2009-01-16 17:38:15 +01:00
m_savedYankPosition = m_tc.position();
if (atEndOfLine())
2009-01-16 16:15:01 +01:00
moveLeft();
recordBeginGroup();
2009-01-16 17:38:15 +01:00
setAnchor();
2008-12-23 21:34:21 +01:00
m_submode = YankSubMode;
2009-01-16 17:38:15 +01:00
} else if (key == 'Y') {
moveToStartOfLine();
setAnchor();
moveDown(count());
m_moveType = MoveLineWise;
finishMovement();
2008-12-19 16:20:39 +01:00
} else if (key == 'z') {
2009-01-16 16:15:01 +01:00
recordBeginGroup();
2008-12-19 16:20:39 +01:00
m_submode = ZSubMode;
} else if (key == '~' && !atEndOfLine()) {
2009-01-16 16:15:01 +01:00
recordBeginGroup();
setAnchor();
moveRight(qMin(count(), rightDist()));
QString str = recordRemoveSelectedText();
2008-12-25 22:22:41 +01:00
for (int i = str.size(); --i >= 0; ) {
QChar c = str.at(i);
str[i] = c.isUpper() ? c.toLower() : c.toUpper();
}
2009-01-16 16:15:01 +01:00
recordInsertText(str);
recordEndGroup();
} else if (key == control('d')) {
int sline = cursorLineOnScreen();
// FIXME: this should use the "scroll" option, and "count"
moveDown(linesOnScreen() / 2);
moveToFirstNonBlankOnLine();
scrollToLineInDocument(cursorLineInDocument() - sline);
finishMovement();
} else if (key == control('u')) {
int sline = cursorLineOnScreen();
// FIXME: this should use the "scroll" option, and "count"
moveUp(linesOnScreen() / 2);
moveToFirstNonBlankOnLine();
scrollToLineInDocument(cursorLineInDocument() - sline);
finishMovement();
} else if (key == Key_PageDown || key == control('f')) {
2009-01-16 16:15:01 +01:00
moveDown(count() * (linesOnScreen() - 2));
finishMovement();
} else if (key == Key_PageUp || key == control('b')) {
2009-01-16 16:15:01 +01:00
moveUp(count() * (linesOnScreen() - 2));
finishMovement();
} else if (key == Key_Delete) {
setAnchor();
moveRight(qMin(1, rightDist()));
recordRemoveSelectedText();
2008-12-25 22:57:21 +01:00
} else if (key == Key_Escape) {
if (m_visualMode != NoVisualMode)
leaveVisualMode();
} else {
2009-01-16 09:56:08 +01:00
qDebug() << "IGNORED IN COMMAND MODE: " << key << text;
if (text.isEmpty())
handled = false;
}
return handled;
}
bool FakeVimHandler::Private::handleInsertMode(int key, int, const QString &text)
{
if (key == Key_Escape) {
// start with '1', as one instance was already physically inserted
// while typing
QString data = m_lastInsertion;
for (int i = 1; i < count(); ++i) {
2008-12-27 12:24:50 +01:00
m_tc.insertText(m_lastInsertion);
data += m_lastInsertion;
}
recordInsert(m_tc.position() - m_lastInsertion.size(), data);
2009-01-16 16:15:01 +01:00
recordEndGroup();
//qDebug() << "UNDO: " << m_undoStack;
moveLeft(qMin(1, leftDist()));
enterCommandMode();
} else if (key == Key_Left) {
2009-01-16 16:15:01 +01:00
moveLeft(count());
2008-12-27 12:24:50 +01:00
m_lastInsertion.clear();
} else if (key == Key_Down) {
m_submode = NoSubMode;
2009-01-16 16:15:01 +01:00
moveDown(count());
2008-12-27 12:24:50 +01:00
m_lastInsertion.clear();
} else if (key == Key_Up) {
m_submode = NoSubMode;
2009-01-16 16:15:01 +01:00
moveUp(count());
2008-12-27 12:24:50 +01:00
m_lastInsertion.clear();
} else if (key == Key_Right) {
2009-01-16 16:15:01 +01:00
moveRight(count());
2008-12-27 12:24:50 +01:00
m_lastInsertion.clear();
} else if (key == Key_Return) {
m_submode = NoSubMode;
m_tc.insertBlock();
m_lastInsertion += "\n";
indentRegion(m_tc.block(), m_tc.block().next());
2009-01-09 15:15:09 +01:00
} else if (key == Key_Backspace || key == control('h')) {
m_tc.deletePreviousChar();
2008-12-27 12:24:50 +01:00
m_lastInsertion = m_lastInsertion.left(m_lastInsertion.size() - 1);
} else if (key == Key_Delete) {
m_tc.deleteChar();
2008-12-27 12:24:50 +01:00
m_lastInsertion.clear();
} else if (key == Key_PageDown || key == control('f')) {
2009-01-16 16:15:01 +01:00
moveDown(count() * (linesOnScreen() - 2));
2008-12-27 12:24:50 +01:00
m_lastInsertion.clear();
} else if (key == Key_PageUp || key == control('b')) {
2009-01-16 16:15:01 +01:00
moveUp(count() * (linesOnScreen() - 2));
2008-12-27 12:24:50 +01:00
m_lastInsertion.clear();
} else if (key == Key_Tab && m_config[ConfigExpandTab] == ConfigOn) {
QString str = QString(m_config[ConfigTabStop].toInt(), ' ');
m_lastInsertion.append(str);
m_tc.insertText(str);
} else if (!text.isEmpty()) {
2008-12-27 12:24:50 +01:00
m_lastInsertion.append(text);
if (m_submode == ReplaceSubMode) {
if (atEndOfLine())
m_submode = NoSubMode;
else
m_tc.deleteChar();
}
m_tc.insertText(text);
if (m_config[ConfigAutoIndent] == ConfigOn
&& isElectricCharacter(text.at(0))) {
const QString leftText = m_tc.block().text()
.left(m_tc.position() - 1 - m_tc.block().position());
if (leftText.simplified().isEmpty()) {
if (m_tc.hasSelection()) {
QTextDocument *doc = EDITOR(document());
QTextBlock block = doc->findBlock(qMin(m_tc.selectionStart(),
m_tc.selectionEnd()));
const QTextBlock end = doc->findBlock(qMax(m_tc.selectionStart(),
m_tc.selectionEnd())).next();
indentRegion(block, end, text.at(0));
} else {
indentCurrentLine(text.at(0));
}
}
}
} else {
return false;
}
updateMiniBuffer();
return true;
}
bool FakeVimHandler::Private::handleMiniBufferModes(int key, int unmodified,
const QString &text)
{
Q_UNUSED(text)
if (key == Key_Escape) {
m_commandBuffer.clear();
enterCommandMode();
2008-12-27 21:01:05 +01:00
updateMiniBuffer();
} else if (key == Key_Backspace) {
if (m_commandBuffer.isEmpty())
enterCommandMode();
else
m_commandBuffer.chop(1);
2008-12-27 21:01:05 +01:00
updateMiniBuffer();
2009-01-08 17:21:51 +01:00
} else if (key == Key_Left) {
// FIXME:
if (!m_commandBuffer.isEmpty())
m_commandBuffer.chop(1);
updateMiniBuffer();
} else if (unmodified == Key_Return && m_mode == ExMode) {
2008-12-28 00:32:07 +01:00
if (!m_commandBuffer.isEmpty()) {
m_commandHistory.takeLast();
m_commandHistory.append(m_commandBuffer);
2009-01-06 11:51:24 +01:00
handleExCommand(m_commandBuffer);
leaveVisualMode();
2008-12-28 00:32:07 +01:00
}
} else if (unmodified == Key_Return && isSearchMode()) {
2008-12-28 00:32:07 +01:00
if (!m_commandBuffer.isEmpty()) {
m_searchHistory.takeLast();
m_searchHistory.append(m_commandBuffer);
m_lastSearchForward = (m_mode == SearchForwardMode);
2008-12-28 00:32:07 +01:00
search(lastSearchString(), m_lastSearchForward);
}
enterCommandMode();
2008-12-27 21:01:05 +01:00
updateMiniBuffer();
} else if (key == Key_Up && isSearchMode()) {
// FIXME: This and the three cases below are wrong as vim
// takes only matching entires in the history into account.
if (m_searchHistoryIndex > 0) {
2008-12-26 17:01:21 +01:00
--m_searchHistoryIndex;
2009-01-08 17:40:27 +01:00
showBlackMessage(m_searchHistory.at(m_searchHistoryIndex));
}
} else if (key == Key_Up && m_mode == ExMode) {
if (m_commandHistoryIndex > 0) {
2008-12-28 00:32:07 +01:00
--m_commandHistoryIndex;
2009-01-08 17:40:27 +01:00
showBlackMessage(m_commandHistory.at(m_commandHistoryIndex));
2008-12-26 17:01:21 +01:00
}
} else if (key == Key_Down && isSearchMode()) {
if (m_searchHistoryIndex < m_searchHistory.size() - 1) {
2008-12-26 17:01:21 +01:00
++m_searchHistoryIndex;
2009-01-08 17:40:27 +01:00
showBlackMessage(m_searchHistory.at(m_searchHistoryIndex));
}
} else if (key == Key_Down && m_mode == ExMode) {
if (m_commandHistoryIndex < m_commandHistory.size() - 1) {
2008-12-28 00:32:07 +01:00
++m_commandHistoryIndex;
2009-01-08 17:40:27 +01:00
showBlackMessage(m_commandHistory.at(m_commandHistoryIndex));
2008-12-26 17:01:21 +01:00
}
2008-12-27 22:41:47 +01:00
} else if (key == Key_Tab) {
m_commandBuffer += QChar(9);
updateMiniBuffer();
} else {
m_commandBuffer += QChar(key);
2008-12-27 21:01:05 +01:00
updateMiniBuffer();
}
return true;
}
2008-12-28 02:15:26 +01:00
// 1 based.
int FakeVimHandler::Private::readLineCode(QString &cmd)
{
//qDebug() << "CMD: " << cmd;
if (cmd.isEmpty())
return -1;
QChar c = cmd.at(0);
cmd = cmd.mid(1);
if (c == '.')
return cursorLineInDocument() + 1;
if (c == '$')
return linesInDocument();
2008-12-29 14:47:42 +01:00
if (c == '\'' && !cmd.isEmpty()) {
int mark = m_marks.value(cmd.at(0).unicode());
if (!mark) {
2008-12-23 21:34:21 +01:00
showRedMessage(tr("E20: Mark '%1' not set").arg(cmd.at(0)));
cmd = cmd.mid(1);
2008-12-29 14:47:42 +01:00
return -1;
}
cmd = cmd.mid(1);
QTextCursor tc = m_tc;
tc.setPosition(mark);
return tc.block().blockNumber() + 1;
}
2008-12-28 02:15:26 +01:00
if (c == '-') {
int n = readLineCode(cmd);
return cursorLineInDocument() + 1 - (n == -1 ? 1 : n);
}
if (c == '+') {
int n = readLineCode(cmd);
return cursorLineInDocument() + 1 + (n == -1 ? 1 : n);
}
if (c == '\'' && !cmd.isEmpty()) {
2009-01-08 17:21:51 +01:00
int pos = m_marks.value(cmd.at(0).unicode(), -1);
//qDebug() << " MARK: " << cmd.at(0) << pos << lineForPosition(pos);
if (pos == -1) {
showRedMessage(tr("E20: Mark '%1' not set").arg(cmd.at(0)));
cmd = cmd.mid(1);
return -1;
}
cmd = cmd.mid(1);
return lineForPosition(pos);
}
2008-12-28 02:15:26 +01:00
if (c.isDigit()) {
int n = c.unicode() - '0';
while (!cmd.isEmpty()) {
c = cmd.at(0);
if (!c.isDigit())
break;
cmd = cmd.mid(1);
n = n * 10 + (c.unicode() - '0');
}
//qDebug() << "N: " << n;
return n;
}
// not parsed
cmd = c + cmd;
return -1;
2008-12-28 02:15:26 +01:00
}
2009-01-16 16:15:01 +01:00
void FakeVimHandler::Private::selectRange(int beginLine, int endLine)
2009-01-06 11:33:07 +01:00
{
2009-01-16 16:15:01 +01:00
m_tc.setPosition(positionForLine(beginLine), MoveAnchor);
if (endLine == linesInDocument()) {
2009-01-16 16:15:01 +01:00
m_tc.setPosition(positionForLine(endLine), KeepAnchor);
m_tc.movePosition(EndOfLine, KeepAnchor);
} else {
2009-01-16 16:15:01 +01:00
m_tc.setPosition(positionForLine(endLine + 1), KeepAnchor);
}
2009-01-06 11:33:07 +01:00
}
2009-01-06 11:51:24 +01:00
void FakeVimHandler::Private::handleExCommand(const QString &cmd0)
{
2008-12-28 02:15:26 +01:00
QString cmd = cmd0;
if (cmd.startsWith("%"))
cmd = "1,$" + cmd.mid(1);
2009-01-08 17:21:51 +01:00
2009-01-06 11:37:24 +01:00
int beginLine = -1;
int endLine = -1;
2008-12-28 02:15:26 +01:00
int line = readLineCode(cmd);
if (line != -1)
beginLine = line;
2008-12-28 02:15:26 +01:00
if (cmd.startsWith(',')) {
cmd = cmd.mid(1);
line = readLineCode(cmd);
if (line != -1)
endLine = line;
}
//qDebug() << "RANGE: " << beginLine << endLine << cmd << cmd0 << m_marks;
2008-12-28 02:15:26 +01:00
static QRegExp reWrite("^w!?( (.*))?$");
2009-01-06 11:33:07 +01:00
static QRegExp reDelete("^d( (.*))?$");
static QRegExp reSet("^set?( (.*))?$");
static QRegExp reHistory("^his(tory)?( (.*))?$");
2008-12-28 02:15:26 +01:00
if (cmd.isEmpty()) {
2009-01-06 11:33:07 +01:00
m_tc.setPosition(positionForLine(beginLine));
2009-01-08 17:40:27 +01:00
showBlackMessage(QString());
2009-01-06 11:33:07 +01:00
} else if (cmd == "q!" || cmd == "q") { // :q
2009-01-07 18:05:45 +01:00
quit();
2009-01-06 11:33:07 +01:00
} else if (reDelete.indexIn(cmd) != -1) { // :d
if (beginLine == -1)
beginLine = cursorLineInDocument();
if (endLine == -1)
endLine = cursorLineInDocument();
2009-01-16 16:15:01 +01:00
selectRange(beginLine, endLine);
2009-01-06 11:33:07 +01:00
QString reg = reDelete.cap(2);
2009-01-16 16:15:01 +01:00
QString text = recordRemoveSelectedText();
2009-01-06 11:33:07 +01:00
if (!reg.isEmpty())
2009-01-16 16:15:01 +01:00
m_registers[reg.at(0).unicode()] = text;
2009-01-06 11:33:07 +01:00
} else if (reWrite.indexIn(cmd) != -1) { // :w
enterCommandMode();
2009-01-06 11:51:24 +01:00
bool noArgs = (beginLine == -1);
2009-01-06 11:33:07 +01:00
if (beginLine == -1)
beginLine = 0;
if (endLine == -1)
endLine = linesInDocument();
qDebug() << "LINES: " << beginLine << endLine;
bool forced = cmd.startsWith("w!");
QString fileName = reWrite.cap(2);
if (fileName.isEmpty())
fileName = m_currentFileName;
2008-12-27 19:47:29 +01:00
QFile file(fileName);
bool exists = file.exists();
2009-01-06 11:51:24 +01:00
if (exists && !forced && !noArgs) {
2009-01-08 17:40:27 +01:00
showRedMessage(tr("File '%1' exists (add ! to override)").arg(fileName));
} else if (file.open(QIODevice::ReadWrite)) {
file.close();
2009-01-16 16:15:01 +01:00
selectRange(beginLine, endLine);
QString contents = selectedText();
bool handled = false;
emit q->writeFileRequested(&handled, fileName, contents);
// nobody cared, so act ourselves
if (!handled) {
qDebug() << "HANDLING MANUAL SAVE";
QFile file(fileName);
file.open(QIODevice::ReadWrite);
{ QTextStream ts(&file); ts << contents; }
file.close();
}
// check result by reading back
file.open(QIODevice::ReadOnly);
2008-12-27 19:47:29 +01:00
QByteArray ba = file.readAll();
2009-01-08 17:40:27 +01:00
showBlackMessage(tr("\"%1\" %2 %3L, %4C written")
2008-12-27 19:47:29 +01:00
.arg(fileName).arg(exists ? " " : " [New] ")
2009-01-08 17:40:27 +01:00
.arg(ba.count('\n')).arg(ba.size()));
} else {
2009-01-08 17:40:27 +01:00
showRedMessage(tr("Cannot open file '%1' for reading").arg(fileName));
2008-12-27 19:47:29 +01:00
}
2009-01-06 11:50:30 +01:00
} else if (cmd.startsWith("r ")) { // :r
m_currentFileName = cmd.mid(2);
QFile file(m_currentFileName);
file.open(QIODevice::ReadOnly);
QTextStream ts(&file);
2009-01-06 11:50:30 +01:00
QString data = ts.readAll();
EDITOR(setPlainText(data));
enterCommandMode();
2009-01-08 17:40:27 +01:00
showBlackMessage(tr("\"%1\" %2L, %3C")
.arg(m_currentFileName).arg(data.count('\n')).arg(data.size()));
2009-01-08 17:21:51 +01:00
} else if (cmd.startsWith("!")) {
if (beginLine == -1)
beginLine = cursorLineInDocument();
if (endLine == -1)
endLine = cursorLineInDocument();
2009-01-16 16:15:01 +01:00
selectRange(beginLine, endLine);
2009-01-08 17:21:51 +01:00
QString command = cmd.mid(1).trimmed();
2009-01-16 16:15:01 +01:00
recordBeginGroup();
QString text = recordRemoveSelectedText();
2009-01-08 17:21:51 +01:00
QProcess proc;
proc.start(cmd.mid(1));
proc.waitForStarted();
proc.write(text.toUtf8());
proc.closeWriteChannel();
proc.waitForFinished();
QString result = QString::fromUtf8(proc.readAllStandardOutput());
2009-01-16 16:15:01 +01:00
recordInsertText(result);
recordEndGroup();
2009-01-08 17:21:51 +01:00
leaveVisualMode();
m_tc.setPosition(positionForLine(beginLine));
EditOperation op;
// FIXME: broken for "upward selection"
op.position = m_tc.position();
op.from = text;
op.to = result;
2009-01-08 17:21:51 +01:00
recordOperation(op);
enterCommandMode();
//qDebug() << "FILTER: " << command;
2009-01-08 17:40:27 +01:00
showBlackMessage(tr("%1 lines filtered").arg(text.count('\n')));
2009-01-08 17:21:51 +01:00
} else if (cmd == "red" || cmd == "redo") { // :redo
redo();
enterCommandMode();
updateMiniBuffer();
} else if (reSet.indexIn(cmd) != -1) { // :set
QString arg = reSet.cap(2);
if (arg.isEmpty()) {
QString info;
foreach (const QString &key, m_config.keys())
info += key + ": " + m_config.value(key) + "\n";
emit q->extraInformationChanged(info);
} else {
notImplementedYet();
}
enterCommandMode();
updateMiniBuffer();
} else if (reHistory.indexIn(cmd) != -1) { // :history
QString arg = reSet.cap(3);
if (arg.isEmpty()) {
QString info;
info += "# command history\n";
int i = 0;
foreach (const QString &item, m_commandHistory) {
++i;
info += QString("%1 %2\n").arg(i, -8).arg(item);
}
emit q->extraInformationChanged(info);
} else {
notImplementedYet();
}
enterCommandMode();
updateMiniBuffer();
2008-12-28 02:15:26 +01:00
} else {
2009-01-08 17:40:27 +01:00
showRedMessage("E492: Not an editor command: " + cmd0);
}
}
2009-01-16 09:56:08 +01:00
void FakeVimHandler::Private::search(const QString &needle0, bool forward)
{
2009-01-16 17:44:24 +01:00
showBlackMessage((forward ? '/' : '?') + needle0);
2008-12-25 13:20:09 +01:00
QTextCursor orig = m_tc;
2008-12-26 17:01:21 +01:00
QTextDocument::FindFlags flags = QTextDocument::FindCaseSensitively;
2008-12-26 00:18:03 +01:00
if (!forward)
2009-01-16 09:56:08 +01:00
flags |= QTextDocument::FindBackward;
// FIXME: Rough mapping of a common case
QString needle = needle0;
if (needle.startsWith("\\<") && needle.endsWith("\\>"))
flags |= QTextDocument::FindWholeWords;
needle.replace("\\<", ""); // start of word
needle.replace("\\>", ""); // end of word
2009-01-16 17:44:24 +01:00
//qDebug() << "NEEDLE " << needle0 << needle << "FORWARD" << forward << flags;
2008-12-26 00:18:03 +01:00
if (forward)
2008-12-25 13:20:09 +01:00
m_tc.movePosition(Right, MoveAnchor, 1);
EDITOR(setTextCursor(m_tc));
if (EDITOR(find(needle, flags))) {
m_tc = EDITOR(textCursor());
2009-01-16 09:56:08 +01:00
m_tc.setPosition(m_tc.anchor());
return;
}
2008-12-26 00:18:03 +01:00
m_tc.setPosition(forward ? 0 : lastPositionInDocument() - 1);
EDITOR(setTextCursor(m_tc));
if (EDITOR(find(needle, flags))) {
m_tc = EDITOR(textCursor());
2009-01-16 09:56:08 +01:00
m_tc.setPosition(m_tc.anchor());
2008-12-26 00:18:03 +01:00
if (forward)
2009-01-08 17:40:27 +01:00
showRedMessage("search hit BOTTOM, continuing at TOP");
2008-12-26 00:18:03 +01:00
else
2009-01-08 17:40:27 +01:00
showRedMessage("search hit TOP, continuing at BOTTOM");
return;
}
2008-12-25 13:20:09 +01:00
m_tc = orig;
}
2008-12-19 14:35:57 +01:00
void FakeVimHandler::Private::moveToFirstNonBlankOnLine()
{
QTextBlock block = m_tc.block();
QTextDocument *doc = m_tc.document();
2009-01-02 03:24:09 +01:00
m_tc.movePosition(StartOfLine, KeepAnchor);
2008-12-19 14:35:57 +01:00
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;
}
}
}
int FakeVimHandler::Private::indentDist() const
{
#if 0
// FIXME: Make independent of TextEditor
if (!m_texteditor)
return 0;
TextEditor::TabSettings ts = m_texteditor->tabSettings();
typedef SharedTools::Indenter<TextEditor::TextBlockIterator> Indenter;
Indenter &indenter = Indenter::instance();
indenter.setIndentSize(ts.m_indentSize);
indenter.setTabSize(ts.m_tabSize);
QTextDocument *doc = EDITOR(document());
const TextEditor::TextBlockIterator current(m_tc.block());
const TextEditor::TextBlockIterator begin(doc->begin());
const TextEditor::TextBlockIterator end(m_tc.block().next());
return indenter.indentForBottomLine(current, begin, end, QChar(' '));
#endif
return 0;
}
void FakeVimHandler::Private::indentRegion(QTextBlock begin, QTextBlock end, QChar typedChar)
{
#if 0
// FIXME: Make independent of TextEditor
if (!m_texteditor)
return 0;
typedef SharedTools::Indenter<TextEditor::TextBlockIterator> Indenter;
Indenter &indenter = Indenter::instance();
indenter.setIndentSize(m_config[ConfigShiftWidth].toInt());
indenter.setTabSize(m_config[ConfigTabStop].toInt());
QTextDocument *doc = EDITOR(document());
const TextEditor::TextBlockIterator docStart(doc->begin());
for(QTextBlock cur = begin; cur != end; cur = cur.next()) {
if (typedChar != 0 && cur.text().simplified().isEmpty()) {
m_tc.setPosition(cur.position(), KeepAnchor);
while (!m_tc.atBlockEnd())
m_tc.deleteChar();
} else {
const TextEditor::TextBlockIterator current(cur);
const TextEditor::TextBlockIterator next(cur.next());
const int indent = indenter.indentForBottomLine(current, docStart, next, typedChar);
ts.indentLine(cur, indent);
}
}
#endif
Q_UNUSED(begin);
Q_UNUSED(end);
Q_UNUSED(typedChar);
}
void FakeVimHandler::Private::indentCurrentLine(QChar typedChar)
{
indentRegion(m_tc.block(), m_tc.block().next(), typedChar);
}
void FakeVimHandler::Private::moveToDesiredColumn()
{
if (m_desiredColumn == -1 || m_tc.block().length() <= m_desiredColumn)
m_tc.movePosition(EndOfLine, KeepAnchor);
else
m_tc.setPosition(m_tc.block().position() + m_desiredColumn, KeepAnchor);
}
static int charClass(QChar c, bool simple)
{
if (simple)
return c.isSpace() ? 0 : 1;
if (c.isLetterOrNumber() || c.unicode() == '_')
return 2;
return c.isSpace() ? 0 : 1;
}
2008-12-26 00:18:03 +01:00
void FakeVimHandler::Private::moveToWordBoundary(bool simple, bool forward)
2008-12-25 19:50:14 +01:00
{
2008-12-26 00:18:03 +01:00
int repeat = count();
2008-12-25 19:50:14 +01:00
QTextDocument *doc = m_tc.document();
2008-12-26 00:18:03 +01:00
int n = forward ? lastPositionInDocument() - 1 : 0;
2009-01-16 16:42:31 +01:00
int lastClass = -1;
2008-12-25 20:12:17 +01:00
while (true) {
QChar c = doc->characterAt(m_tc.position() + (forward ? 1 : -1));
2008-12-25 19:50:14 +01:00
int thisClass = charClass(c, simple);
2008-12-25 20:12:17 +01:00
if (thisClass != lastClass && lastClass != 0)
2008-12-25 19:50:14 +01:00
--repeat;
2009-01-16 16:57:00 +01:00
if (repeat == -1)
2008-12-25 20:12:17 +01:00
break;
2008-12-25 19:50:14 +01:00
lastClass = thisClass;
2008-12-26 00:18:03 +01:00
if (m_tc.position() == n)
2008-12-25 20:12:17 +01:00
break;
forward ? moveRight() : moveLeft();
2008-12-25 19:50:14 +01:00
}
}
2008-12-26 00:18:03 +01:00
void FakeVimHandler::Private::handleFfTt(int key)
2008-12-25 19:50:14 +01:00
{
2008-12-26 00:18:03 +01:00
// m_subsubmode \in { 'f', 'F', 't', 'T' }
bool forward = m_subsubdata == 'f' || m_subsubdata == 't';
int repeat = count();
2008-12-25 19:50:14 +01:00
QTextDocument *doc = m_tc.document();
2008-12-26 00:18:03 +01:00
QTextBlock block = m_tc.block();
int n = block.position();
if (forward)
n += block.length();
int pos = m_tc.position();
2008-12-25 20:12:17 +01:00
while (true) {
2008-12-26 00:18:03 +01:00
pos += forward ? 1 : -1;
if (pos == n)
break;
int uc = doc->characterAt(pos).unicode();
if (uc == ParagraphSeparator)
break;
if (uc == key)
2008-12-25 19:50:14 +01:00
--repeat;
2008-12-25 20:12:17 +01:00
if (repeat == 0) {
2008-12-26 00:18:03 +01:00
if (m_subsubdata == 't')
--pos;
2008-12-26 00:25:38 +01:00
else if (m_subsubdata == 'T')
2008-12-26 00:18:03 +01:00
++pos;
2008-12-26 00:25:38 +01:00
// FIXME: strange correction...
if (m_submode == DeleteSubMode && m_subsubdata == 'f')
++pos;
if (m_submode == DeleteSubMode && m_subsubdata == 't')
++pos;
2008-12-26 00:18:03 +01:00
if (forward)
m_tc.movePosition(Right, KeepAnchor, pos - m_tc.position());
else
m_tc.movePosition(Left, KeepAnchor, m_tc.position() - pos);
2008-12-25 20:12:17 +01:00
break;
2008-12-25 19:50:14 +01:00
}
}
}
2008-12-26 00:18:03 +01:00
void FakeVimHandler::Private::moveToNextWord(bool simple)
{
// FIXME: 'w' should stop on empty lines, too
2008-12-26 00:18:03 +01:00
int repeat = count();
QTextDocument *doc = m_tc.document();
int n = lastPositionInDocument() - 1;
2008-12-25 23:19:08 +01:00
QChar c = doc->characterAt(m_tc.position());
int lastClass = charClass(c, simple);
2008-12-25 20:12:17 +01:00
while (true) {
2008-12-25 23:19:08 +01:00
c = doc->characterAt(m_tc.position());
int thisClass = charClass(c, simple);
if (thisClass != lastClass && thisClass != 0)
--repeat;
2008-12-25 22:42:50 +01:00
if (repeat == 0)
break;
lastClass = thisClass;
2009-01-16 16:42:31 +01:00
moveRight();
2008-12-25 20:12:17 +01:00
if (m_tc.position() == n)
break;
}
}
void FakeVimHandler::Private::moveToMatchingParanthesis()
{
#if 0
// FIXME: remove TextEditor dependency
bool undoFakeEOL = false;
if (atEndOfLine()) {
m_tc.movePosition(Left, KeepAnchor, 1);
undoFakeEOL = true;
}
TextEditor::TextBlockUserData::MatchType match
= TextEditor::TextBlockUserData::matchCursorForward(&m_tc);
if (match == TextEditor::TextBlockUserData::Match) {
if (m_submode == NoSubMode || m_submode == ZSubMode || m_submode == RegisterSubMode)
m_tc.movePosition(Left, KeepAnchor, 1);
} else {
if (undoFakeEOL)
m_tc.movePosition(Right, KeepAnchor, 1);
if (match == TextEditor::TextBlockUserData::NoMatch) {
// backward matching is according to the character before the cursor
bool undoMove = false;
if (!m_tc.atBlockEnd()) {
m_tc.movePosition(Right, KeepAnchor, 1);
undoMove = true;
}
match = TextEditor::TextBlockUserData::matchCursorBackward(&m_tc);
if (match != TextEditor::TextBlockUserData::Match && undoMove)
m_tc.movePosition(Left, KeepAnchor, 1);
}
}
#endif
}
int FakeVimHandler::Private::cursorLineOnScreen() const
{
2009-01-06 11:50:30 +01:00
if (!editor())
return 0;
2009-01-06 11:11:31 +01:00
QRect rect = EDITOR(cursorRect());
return rect.y() / rect.height();
}
int FakeVimHandler::Private::linesOnScreen() const
{
2009-01-06 11:50:30 +01:00
if (!editor())
return 1;
2009-01-06 11:11:31 +01:00
QRect rect = EDITOR(cursorRect());
return EDITOR(height()) / rect.height();
}
2008-12-27 21:01:05 +01:00
int FakeVimHandler::Private::columnsOnScreen() const
{
2009-01-06 11:50:30 +01:00
if (!editor())
return 1;
2009-01-06 11:11:31 +01:00
QRect rect = EDITOR(cursorRect());
// qDebug() << "WID: " << EDITOR(width()) << "RECT: " << rect;
2009-01-06 11:11:31 +01:00
return EDITOR(width()) / rect.width();
2008-12-27 21:01:05 +01:00
}
int FakeVimHandler::Private::cursorLineInDocument() const
{
2009-01-06 11:11:31 +01:00
return m_tc.block().blockNumber();
}
2008-12-27 21:01:05 +01:00
int FakeVimHandler::Private::cursorColumnInDocument() const
{
2009-01-06 11:11:31 +01:00
return m_tc.position() - m_tc.block().position();
2008-12-27 21:01:05 +01:00
}
2008-12-28 02:15:26 +01:00
int FakeVimHandler::Private::linesInDocument() const
{
return m_tc.isNull() ? 0 : m_tc.document()->blockCount();
}
void FakeVimHandler::Private::scrollToLineInDocument(int line)
{
2009-01-06 11:11:31 +01:00
// FIXME: works only for QPlainTextEdit
QScrollBar *scrollBar = EDITOR(verticalScrollBar());
scrollBar->setValue(line);
}
int FakeVimHandler::Private::lastPositionInDocument() const
{
QTextBlock block = m_tc.block().document()->lastBlock();
return block.position() + block.length();
}
2008-12-26 17:01:21 +01:00
QString FakeVimHandler::Private::lastSearchString() const
{
return m_searchHistory.empty() ? QString() : m_searchHistory.back();
}
2009-01-16 16:15:01 +01:00
QString FakeVimHandler::Private::selectedText() const
{
QTextCursor tc = m_tc;
tc.setPosition(m_anchor, KeepAnchor);
QString text = tc.selection().toPlainText();
tc.clearSelection();
return text;
2009-01-16 16:15:01 +01:00
}
2009-01-06 11:33:07 +01:00
int FakeVimHandler::Private::positionForLine(int line) const
{
return m_tc.block().document()->findBlockByNumber(line - 1).position();
}
int FakeVimHandler::Private::lineForPosition(int pos) const
{
QTextCursor tc = m_tc;
tc.setPosition(pos);
return tc.block().blockNumber() + 1;
}
void FakeVimHandler::Private::enterVisualMode(VisualMode visualMode)
{
m_visualMode = visualMode;
m_marks['<'] = m_tc.position();
m_marks['>'] = m_tc.position();
updateMiniBuffer();
updateSelection();
}
void FakeVimHandler::Private::leaveVisualMode()
{
m_visualMode = NoVisualMode;
updateMiniBuffer();
updateSelection();
}
2009-01-06 11:50:30 +01:00
QWidget *FakeVimHandler::Private::editor() const
2009-01-06 11:43:27 +01:00
{
return m_textedit
? static_cast<QWidget *>(m_textedit)
: static_cast<QWidget *>(m_plaintextedit);
}
void FakeVimHandler::Private::undo()
{
2009-01-08 17:40:27 +01:00
if (m_undoStack.isEmpty()) {
showBlackMessage(tr("Already at oldest change"));
} else {
2009-01-08 17:40:27 +01:00
EditOperation op = m_undoStack.pop();
//qDebug() << "UNDO " << op;
if (op.itemCount > 0) {
for (int i = op.itemCount; --i >= 0; )
2009-01-08 17:40:27 +01:00
undo();
} else {
m_tc.setPosition(op.position, MoveAnchor);
if (!op.to.isEmpty()) {
m_tc.setPosition(op.position + op.to.size(), KeepAnchor);
2009-01-21 17:08:47 +01:00
m_tc.removeSelectedText();
2009-01-08 17:40:27 +01:00
}
if (!op.from.isEmpty())
m_tc.insertText(op.from);
m_tc.setPosition(op.position, MoveAnchor);
2009-01-06 11:48:55 +01:00
}
2009-01-08 17:40:27 +01:00
m_redoStack.push(op);
showBlackMessage(QString());
}
}
void FakeVimHandler::Private::redo()
{
2009-01-08 17:40:27 +01:00
if (m_redoStack.isEmpty()) {
showBlackMessage(tr("Already at newest change"));
} else {
2009-01-08 17:40:27 +01:00
EditOperation op = m_redoStack.pop();
//qDebug() << "REDO " << op;
if (op.itemCount > 0) {
for (int i = op.itemCount; --i >= 0; )
2009-01-08 17:40:27 +01:00
redo();
} else {
m_tc.setPosition(op.position, MoveAnchor);
if (!op.from.isEmpty()) {
m_tc.setPosition(op.position + op.from.size(), KeepAnchor);
2009-01-21 17:08:47 +01:00
m_tc.removeSelectedText();
2009-01-08 17:40:27 +01:00
}
if (!op.to.isEmpty())
m_tc.insertText(op.to);
m_tc.setPosition(op.position, MoveAnchor);
2009-01-06 11:48:55 +01:00
}
2009-01-08 17:40:27 +01:00
m_undoStack.push(op);
showBlackMessage(QString());
}
}
2009-01-16 13:10:42 +01:00
void FakeVimHandler::Private::recordBeginGroup()
{
2009-01-16 16:15:01 +01:00
//qDebug() << "PUSH";
2009-01-16 13:10:42 +01:00
m_undoGroupStack.push(m_undoStack.size());
2009-01-16 17:38:15 +01:00
EditOperation op;
op.position = m_tc.position();
2009-01-16 17:38:15 +01:00
recordOperation(op);
2009-01-16 13:10:42 +01:00
}
void FakeVimHandler::Private::recordEndGroup()
{
2009-01-21 17:08:47 +01:00
if (m_undoGroupStack.isEmpty()) {
qWarning("fakevim: undo groups not balanced.\n");
return;
}
2009-01-16 13:10:42 +01:00
EditOperation op;
op.itemCount = m_undoStack.size() - m_undoGroupStack.pop();
//qDebug() << "POP " << op.itemCount << m_undoStack;
2009-01-16 13:10:42 +01:00
recordOperation(op);
}
2009-01-16 16:15:01 +01:00
QString FakeVimHandler::Private::recordRemoveSelectedText()
2009-01-16 13:10:42 +01:00
{
EditOperation op;
//qDebug() << "POS: " << position() << " ANCHOR: " << anchor() << m_tc.anchor();
int pos = m_tc.position();
if (pos == anchor())
return QString();
m_tc.setPosition(anchor(), MoveAnchor);
m_tc.setPosition(pos, KeepAnchor);
op.position = qMin(pos, anchor());
op.from = m_tc.selection().toPlainText();
//qDebug() << "OP: " << op;
2009-01-16 13:10:42 +01:00
recordOperation(op);
2009-01-21 17:08:47 +01:00
m_tc.removeSelectedText();
return op.from;
2009-01-16 13:10:42 +01:00
}
void FakeVimHandler::Private::recordRemoveNextChar()
{
2009-01-16 16:15:01 +01:00
m_anchor = position();
moveRight();
2009-01-16 13:10:42 +01:00
recordRemoveSelectedText();
}
void FakeVimHandler::Private::recordInsertText(const QString &data)
{
EditOperation op;
op.position = m_tc.position();
op.to = data;
recordOperation(op);
2009-01-16 13:10:42 +01:00
m_tc.insertText(data);
}
void FakeVimHandler::Private::recordMove()
{
EditOperation op;
op.position = m_tc.position();
m_undoStack.push(op);
m_redoStack.clear();
//qDebug() << "MOVE: " << op;
//qDebug() << "\nSTACK: " << m_undoStack;
}
2009-01-08 17:21:51 +01:00
void FakeVimHandler::Private::recordOperation(const EditOperation &op)
{
//qDebug() << "OP: " << op;
// No need to record operations that actually do not change anything.
if (op.from.isEmpty() && op.to.isEmpty() && op.itemCount == 0)
return;
// No need to create groups with only one member.
if (op.itemCount == 1)
return;
2009-01-08 17:21:51 +01:00
m_undoStack.push(op);
m_redoStack.clear();
//qDebug() << "\nSTACK: " << m_undoStack;
}
void FakeVimHandler::Private::recordInsert(int position, const QString &data)
{
EditOperation op;
op.position = position;
op.to = data;
2009-01-08 17:21:51 +01:00
recordOperation(op);
}
void FakeVimHandler::Private::recordRemove(int position, int length)
{
QTextCursor tc = m_tc;
tc.setPosition(position, MoveAnchor);
tc.setPosition(position + length, KeepAnchor);
2009-01-06 11:48:55 +01:00
recordRemove(position, tc.selection().toPlainText());
}
void FakeVimHandler::Private::recordRemove(int position, const QString &data)
{
EditOperation op;
op.position = position;
op.from = data;
2009-01-08 17:21:51 +01:00
recordOperation(op);
}
void FakeVimHandler::Private::enterInsertMode()
{
EDITOR(setOverwriteMode(false));
m_mode = InsertMode;
m_lastInsertion.clear();
2009-01-21 17:08:47 +01:00
recordBeginGroup();
}
void FakeVimHandler::Private::enterCommandMode()
{
if (editor())
EDITOR(setOverwriteMode(true));
m_mode = CommandMode;
}
2009-01-07 18:05:45 +01:00
void FakeVimHandler::Private::quit()
{
EDITOR(setOverwriteMode(false));
q->quitRequested();
2009-01-07 18:05:45 +01:00
}
///////////////////////////////////////////////////////////////////////
//
// FakeVimHandler
//
///////////////////////////////////////////////////////////////////////
FakeVimHandler::FakeVimHandler(QWidget *widget, QObject *parent)
: QObject(parent), d(new Private(this, widget))
{}
FakeVimHandler::~FakeVimHandler()
{
qDebug() << "DELETING HANDLER" << this;
delete d;
}
bool FakeVimHandler::eventFilter(QObject *ob, QEvent *ev)
{
//if (ev->type() == QEvent::KeyPress || ev->type() == QEvent::ShortcutOverride)
// qDebug() << ob << ev->type() << qApp << d->editor()
// << QEvent::KeyPress << QEvent::ShortcutOverride;
if (ev->type() == QEvent::KeyPress && ob == d->editor())
return d->handleEvent(static_cast<QKeyEvent *>(ev));
if (ev->type() == QEvent::ShortcutOverride && ob == d->editor()) {
QKeyEvent *kev = static_cast<QKeyEvent *>(ev);
int key = kev->key();
int mods = kev->modifiers();
bool handleIt = (key == Qt::Key_Escape)
|| (key >= Key_A && key <= Key_Z && mods == Qt::ControlModifier);
if (handleIt && d->handleEvent(kev)) {
d->enterCommandMode();
ev->accept();
return true;
}
}
return QObject::eventFilter(ob, ev);
}
void FakeVimHandler::setupWidget()
{
d->setupWidget();
}
void FakeVimHandler::restoreWidget()
{
d->restoreWidget();
}
void FakeVimHandler::handleCommand(const QString &cmd)
{
2009-01-06 11:51:24 +01:00
d->handleExCommand(cmd);
}
void FakeVimHandler::setConfigValue(const QString &key, const QString &value)
{
d->m_config[key] = value;
}
2009-01-07 18:05:45 +01:00
void FakeVimHandler::quit()
{
d->quit();
}
void FakeVimHandler::setCurrentFileName(const QString &fileName)
{
d->m_currentFileName = fileName;
}
QWidget *FakeVimHandler::widget()
{
return d->editor();
}
void FakeVimHandler::setExtraData(QObject *data)
{
d->m_extraData = data;
}
QObject *FakeVimHandler::extraData() const
{
return d->m_extraData;
}