Commit 2af80159 authored by Lukas Holecek's avatar Lukas Holecek Committed by hjk
Browse files

FakeVim: Allow to delete selected text in command buffer



Change-Id: Ia3074053da465b5ce999955f428c970f1b01a265
Reviewed-by: default avatarhjk <qthjk@ovi.com>
parent e3ca6f8d
...@@ -1098,7 +1098,10 @@ public: ...@@ -1098,7 +1098,10 @@ public:
void setPrompt(const QChar &prompt) { m_prompt = prompt; } void setPrompt(const QChar &prompt) { m_prompt = prompt; }
void setContents(const QString &s) { m_buffer = s; m_anchor = m_pos = s.size(); } void setContents(const QString &s) { m_buffer = s; m_anchor = m_pos = s.size(); }
void setContents(const QString &s, int pos) { m_buffer = s; m_anchor = m_pos = m_userPos = pos; } void setContents(const QString &s, int pos, int anchor = -1)
{
m_buffer = s; m_pos = m_userPos = pos; m_anchor = anchor >= 0 ? anchor : pos;
}
QStringRef userContents() const { return m_buffer.leftRef(m_userPos); } QStringRef userContents() const { return m_buffer.leftRef(m_userPos); }
const QChar &prompt() const { return m_prompt; } const QChar &prompt() const { return m_prompt; }
...@@ -1106,6 +1109,7 @@ public: ...@@ -1106,6 +1109,7 @@ public:
bool isEmpty() const { return m_buffer.isEmpty(); } bool isEmpty() const { return m_buffer.isEmpty(); }
int cursorPos() const { return m_pos; } int cursorPos() const { return m_pos; }
int anchorPos() const { return m_anchor; } int anchorPos() const { return m_anchor; }
bool hasSelection() const { return m_pos != m_anchor; }
void insertChar(QChar c) { m_buffer.insert(m_pos++, c); m_anchor = m_userPos = m_pos; } void insertChar(QChar c) { m_buffer.insert(m_pos++, c); m_anchor = m_userPos = m_pos; }
void insertText(const QString &s) void insertText(const QString &s)
...@@ -1189,16 +1193,16 @@ public: ...@@ -1189,16 +1193,16 @@ public:
} else if (input.isKey(Key_Down) || input.isKey(Key_PageDown)) { } else if (input.isKey(Key_Down) || input.isKey(Key_PageDown)) {
historyDown(); historyDown();
} else if (input.isKey(Key_Delete)) { } else if (input.isKey(Key_Delete)) {
if (m_anchor == m_pos) { if (hasSelection()) {
deleteSelected();
} else {
if (m_pos < m_buffer.size()) if (m_pos < m_buffer.size())
m_buffer.remove(m_pos, 1); m_buffer.remove(m_pos, 1);
else else
deleteChar(); deleteChar();
} else {
deleteSelected();
} }
} else if (!input.text().isEmpty()) { } else if (!input.text().isEmpty()) {
if (m_anchor != m_pos) if (hasSelection())
deleteSelected(); deleteSelected();
insertText(input.text()); insertText(input.text());
} else { } else {
...@@ -1807,7 +1811,7 @@ public: ...@@ -1807,7 +1811,7 @@ public:
signed char m_charClass[256]; signed char m_charClass[256];
bool m_ctrlVActive; bool m_ctrlVActive;
void miniBufferTextEdited(const QString &text, int cursorPos); void miniBufferTextEdited(const QString &text, int cursorPos, int anchorPos);
static struct GlobalData static struct GlobalData
{ {
...@@ -4368,6 +4372,8 @@ EventResult FakeVimHandler::Private::handleExMode(const Input &input) ...@@ -4368,6 +4372,8 @@ EventResult FakeVimHandler::Private::handleExMode(const Input &input)
if (g.commandBuffer.isEmpty()) { if (g.commandBuffer.isEmpty()) {
enterCommandMode(g.returnToMode); enterCommandMode(g.returnToMode);
resetCommandMode(); resetCommandMode();
} else if (g.commandBuffer.hasSelection()) {
g.commandBuffer.deleteSelected();
} else { } else {
g.commandBuffer.deleteChar(); g.commandBuffer.deleteChar();
} }
...@@ -5660,7 +5666,8 @@ int FakeVimHandler::Private::charClass(QChar c, bool simple) const ...@@ -5660,7 +5666,8 @@ int FakeVimHandler::Private::charClass(QChar c, bool simple) const
return c.isSpace() ? 0 : 1; return c.isSpace() ? 0 : 1;
} }
void FakeVimHandler::Private::miniBufferTextEdited(const QString &text, int cursorPos) void FakeVimHandler::Private::miniBufferTextEdited(const QString &text, int cursorPos,
int anchorPos)
{ {
if (m_subsubmode != SearchSubSubMode && m_mode != ExMode) { if (m_subsubmode != SearchSubSubMode && m_mode != ExMode) {
editor()->setFocus(); editor()->setFocus();
...@@ -5671,16 +5678,19 @@ void FakeVimHandler::Private::miniBufferTextEdited(const QString &text, int curs ...@@ -5671,16 +5678,19 @@ void FakeVimHandler::Private::miniBufferTextEdited(const QString &text, int curs
updateCursorShape(); updateCursorShape();
} else { } else {
CommandBuffer &cmdBuf = (m_mode == ExMode) ? g.commandBuffer : g.searchBuffer; CommandBuffer &cmdBuf = (m_mode == ExMode) ? g.commandBuffer : g.searchBuffer;
int pos = qMax(1, cursorPos);
int anchor = anchorPos == -1 ? pos : qMax(1, anchorPos);
QString buffer = text;
// prepend prompt character if missing // prepend prompt character if missing
if (!text.startsWith(cmdBuf.prompt())) { if (!buffer.startsWith(cmdBuf.prompt())) {
emit q->commandBufferChanged(cmdBuf.prompt() + text, buffer.prepend(cmdBuf.prompt());
cmdBuf.cursorPos() + 1, ++pos;
cmdBuf.anchorPos() + 1, ++anchor;
0, q);
cmdBuf.setContents(text, cursorPos - 1);
} else {
cmdBuf.setContents(text.mid(1), cursorPos - 1);
} }
// update command/search buffer
cmdBuf.setContents(buffer.mid(1), pos - 1, anchor - 1);
if (pos != cursorPos || anchor != anchorPos || buffer != text)
emit q->commandBufferChanged(buffer, pos, anchor, 0, q);
// update search expression // update search expression
if (m_subsubmode == SearchSubSubMode) { if (m_subsubmode == SearchSubSubMode) {
updateFind(false); updateFind(false);
...@@ -7443,9 +7453,9 @@ QString FakeVimHandler::tabExpand(int n) const ...@@ -7443,9 +7453,9 @@ QString FakeVimHandler::tabExpand(int n) const
return d->tabExpand(n); return d->tabExpand(n);
} }
void FakeVimHandler::miniBufferTextEdited(const QString &text, int cursorPos) void FakeVimHandler::miniBufferTextEdited(const QString &text, int cursorPos, int anchorPos)
{ {
d->miniBufferTextEdited(text, cursorPos); d->miniBufferTextEdited(text, cursorPos, anchorPos);
} }
void FakeVimHandler::setTextCursorPosition(int position) void FakeVimHandler::setTextCursorPosition(int position)
......
...@@ -122,7 +122,7 @@ public slots: ...@@ -122,7 +122,7 @@ public slots:
int logicalIndentation(const QString &line) const; int logicalIndentation(const QString &line) const;
QString tabExpand(int n) const; QString tabExpand(int n) const;
void miniBufferTextEdited(const QString &text, int cursorPos); void miniBufferTextEdited(const QString &text, int cursorPos, int anchorPos);
// Set text cursor position. Keeps anchor if in visual mode. // Set text cursor position. Keeps anchor if in visual mode.
void setTextCursorPosition(int position); void setTextCursorPosition(int position);
......
...@@ -123,6 +123,7 @@ public: ...@@ -123,6 +123,7 @@ public:
m_edit->installEventFilter(this); m_edit->installEventFilter(this);
connect(m_edit, SIGNAL(textEdited(QString)), SLOT(changed())); connect(m_edit, SIGNAL(textEdited(QString)), SLOT(changed()));
connect(m_edit, SIGNAL(cursorPositionChanged(int,int)), SLOT(changed())); connect(m_edit, SIGNAL(cursorPositionChanged(int,int)), SLOT(changed()));
connect(m_edit, SIGNAL(selectionChanged()), SLOT(changed()));
m_label->setTextInteractionFlags(Qt::TextSelectableByMouse); m_label->setTextInteractionFlags(Qt::TextSelectableByMouse);
addWidget(m_label); addWidget(m_label);
...@@ -164,7 +165,7 @@ public: ...@@ -164,7 +165,7 @@ public:
"*{border-radius:2px;padding-left:4px;padding-right:4px;%1}").arg(css)); "*{border-radius:2px;padding-left:4px;padding-right:4px;%1}").arg(css));
if (m_edit->hasFocus()) if (m_edit->hasFocus())
emit edited(QString(), -1); emit edited(QString(), -1, -1);
setCurrentWidget(m_label); setCurrentWidget(m_label);
} }
...@@ -172,12 +173,12 @@ public: ...@@ -172,12 +173,12 @@ public:
if (m_eventFilter != eventFilter) { if (m_eventFilter != eventFilter) {
if (m_eventFilter != 0) { if (m_eventFilter != 0) {
m_edit->removeEventFilter(m_eventFilter); m_edit->removeEventFilter(m_eventFilter);
disconnect(SIGNAL(edited(QString,int))); disconnect(SIGNAL(edited(QString,int,int)));
} }
if (eventFilter != 0) { if (eventFilter != 0) {
m_edit->installEventFilter(eventFilter); m_edit->installEventFilter(eventFilter);
connect(this, SIGNAL(edited(QString,int)), connect(this, SIGNAL(edited(QString,int,int)),
eventFilter, SLOT(miniBufferTextEdited(QString,int))); eventFilter, SLOT(miniBufferTextEdited(QString,int,int)));
} }
m_eventFilter = eventFilter; m_eventFilter = eventFilter;
} }
...@@ -191,12 +192,16 @@ public: ...@@ -191,12 +192,16 @@ public:
} }
signals: signals:
void edited(const QString &text, int cursorPos); void edited(const QString &text, int cursorPos, int anchorPos);
private slots: private slots:
void changed() void changed()
{ {
emit edited(m_edit->text(), m_edit->cursorPosition()); const int cursorPos = m_edit->cursorPosition();
int anchorPos = m_edit->selectionStart();
if (anchorPos == cursorPos)
anchorPos = cursorPos + m_edit->selectedText().length();
emit edited(m_edit->text(), cursorPos, anchorPos);
} }
bool eventFilter(QObject *ob, QEvent *ev) bool eventFilter(QObject *ob, QEvent *ev)
...@@ -204,7 +209,7 @@ private slots: ...@@ -204,7 +209,7 @@ private slots:
// cancel editing on escape // cancel editing on escape
if (m_eventFilter != 0 && ob == m_edit && ev->type() == QEvent::ShortcutOverride if (m_eventFilter != 0 && ob == m_edit && ev->type() == QEvent::ShortcutOverride
&& static_cast<QKeyEvent*>(ev)->key() == Qt::Key_Escape) { && static_cast<QKeyEvent*>(ev)->key() == Qt::Key_Escape) {
emit edited(QString(), -1); emit edited(QString(), -1, -1);
ev->accept(); ev->accept();
return true; return true;
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment