Skip to content

Commit

Permalink
libshell|AbstractLineEditor: Added word deletion
Browse files Browse the repository at this point in the history
The same modifier that is used for word jumping can now be used
together with Backspace to delete the word behind the cursor.
  • Loading branch information
skyjake committed Aug 5, 2013
1 parent d4bb99a commit 14619a9
Showing 1 changed file with 33 additions and 6 deletions.
39 changes: 33 additions & 6 deletions doomsday/libshell/src/abstractlineeditor.cpp
Expand Up @@ -172,6 +172,19 @@ DENG2_PIMPL(AbstractLineEditor)
}
}

void doWordBackspace()
{
rejectCompletion();

if(!text.isEmpty() && cursor > 0)
{
int to = wordJumpLeft(cursor);
text.remove(to, cursor - to);
cursor = to;
rewrapNow();
}
}

void doDelete()
{
if(text.size() > cursor)
Expand Down Expand Up @@ -203,19 +216,26 @@ DENG2_PIMPL(AbstractLineEditor)
}
}

void doWordLeft()
int wordJumpLeft(int pos) const
{
acceptCompletion();
pos = de::min(pos, text.size() - 1);

// First jump over any non-word chars.
while(cursor > 0 && !text[cursor].isLetterOrNumber()) cursor--;
while(pos > 0 && !text[pos].isLetterOrNumber()) pos--;

// At least move one character.
if(cursor > 0) cursor--;
if(pos > 0) pos--;

// We're inside a word, jump to its beginning.
while(cursor > 0 && text[cursor - 1].isLetterOrNumber()) cursor--;
while(pos > 0 && text[pos - 1].isLetterOrNumber()) pos--;

return pos;
}

void doWordLeft()
{
acceptCompletion();
cursor = wordJumpLeft(cursor);
self.cursorMoved();
}

Expand Down Expand Up @@ -538,7 +558,14 @@ bool AbstractLineEditor::handleControlKey(int qtKey, KeyModifiers const &mods)
switch(qtKey)
{
case Qt::Key_Backspace:
d->doBackspace();
if(mods.testFlag(WORD_JUMP_MODIFIER))
{
d->doWordBackspace();
}
else
{
d->doBackspace();
}
return true;

case Qt::Key_Delete:
Expand Down

0 comments on commit 14619a9

Please sign in to comment.