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

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:
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, 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); }
const QChar &prompt() const { return m_prompt; }
......@@ -1106,6 +1109,7 @@ public:
bool isEmpty() const { return m_buffer.isEmpty(); }
int cursorPos() const { return m_pos; }
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 insertText(const QString &s)
......@@ -1189,16 +1193,16 @@ public:
} else if (input.isKey(Key_Down) || input.isKey(Key_PageDown)) {
historyDown();
} else if (input.isKey(Key_Delete)) {
if (m_anchor == m_pos) {
if (hasSelection()) {
deleteSelected();
} else {
if (m_pos < m_buffer.size())
m_buffer.remove(m_pos, 1);
else
deleteChar();
} else {
deleteSelected();
}
} else if (!input.text().isEmpty()) {
if (m_anchor != m_pos)
if (hasSelection())
deleteSelected();
insertText(input.text());
} else {
......@@ -1807,7 +1811,7 @@ public:
signed char m_charClass[256];
bool m_ctrlVActive;
void miniBufferTextEdited(const QString &text, int cursorPos);
void miniBufferTextEdited(const QString &text, int cursorPos, int anchorPos);
static struct GlobalData
{
......@@ -4368,6 +4372,8 @@ EventResult FakeVimHandler::Private::handleExMode(const Input &input)
if (g.commandBuffer.isEmpty()) {
enterCommandMode(g.returnToMode);
resetCommandMode();
} else if (g.commandBuffer.hasSelection()) {
g.commandBuffer.deleteSelected();
} else {
g.commandBuffer.deleteChar();
}
......@@ -5660,7 +5666,8 @@ int FakeVimHandler::Private::charClass(QChar c, bool simple) const
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) {
editor()->setFocus();
......@@ -5671,16 +5678,19 @@ void FakeVimHandler::Private::miniBufferTextEdited(const QString &text, int curs
updateCursorShape();
} else {
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
if (!text.startsWith(cmdBuf.prompt())) {
emit q->commandBufferChanged(cmdBuf.prompt() + text,
cmdBuf.cursorPos() + 1,
cmdBuf.anchorPos() + 1,
0, q);
cmdBuf.setContents(text, cursorPos - 1);
} else {
cmdBuf.setContents(text.mid(1), cursorPos - 1);
if (!buffer.startsWith(cmdBuf.prompt())) {
buffer.prepend(cmdBuf.prompt());
++pos;
++anchor;
}
// 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
if (m_subsubmode == SearchSubSubMode) {
updateFind(false);
......@@ -7443,9 +7453,9 @@ QString FakeVimHandler::tabExpand(int n) const
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)
......
......@@ -122,7 +122,7 @@ public slots:
int logicalIndentation(const QString &line) 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.
void setTextCursorPosition(int position);
......
......@@ -123,6 +123,7 @@ public:
m_edit->installEventFilter(this);
connect(m_edit, SIGNAL(textEdited(QString)), SLOT(changed()));
connect(m_edit, SIGNAL(cursorPositionChanged(int,int)), SLOT(changed()));
connect(m_edit, SIGNAL(selectionChanged()), SLOT(changed()));
m_label->setTextInteractionFlags(Qt::TextSelectableByMouse);
addWidget(m_label);
......@@ -164,7 +165,7 @@ public:
"*{border-radius:2px;padding-left:4px;padding-right:4px;%1}").arg(css));
if (m_edit->hasFocus())
emit edited(QString(), -1);
emit edited(QString(), -1, -1);
setCurrentWidget(m_label);
}
......@@ -172,12 +173,12 @@ public:
if (m_eventFilter != eventFilter) {
if (m_eventFilter != 0) {
m_edit->removeEventFilter(m_eventFilter);
disconnect(SIGNAL(edited(QString,int)));
disconnect(SIGNAL(edited(QString,int,int)));
}
if (eventFilter != 0) {
m_edit->installEventFilter(eventFilter);
connect(this, SIGNAL(edited(QString,int)),
eventFilter, SLOT(miniBufferTextEdited(QString,int)));
connect(this, SIGNAL(edited(QString,int,int)),
eventFilter, SLOT(miniBufferTextEdited(QString,int,int)));
}
m_eventFilter = eventFilter;
}
......@@ -191,12 +192,16 @@ public:
}
signals:
void edited(const QString &text, int cursorPos);
void edited(const QString &text, int cursorPos, int anchorPos);
private slots:
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)
......@@ -204,7 +209,7 @@ private slots:
// cancel editing on escape
if (m_eventFilter != 0 && ob == m_edit && ev->type() == QEvent::ShortcutOverride
&& static_cast<QKeyEvent*>(ev)->key() == Qt::Key_Escape) {
emit edited(QString(), -1);
emit edited(QString(), -1, -1);
ev->accept();
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