From 14619a9793cb53c41242f257c105873d36dfeab9 Mon Sep 17 00:00:00 2001 From: skyjake Date: Mon, 5 Aug 2013 14:02:23 +0300 Subject: [PATCH] libshell|AbstractLineEditor: Added word deletion The same modifier that is used for word jumping can now be used together with Backspace to delete the word behind the cursor. --- doomsday/libshell/src/abstractlineeditor.cpp | 39 +++++++++++++++++--- 1 file changed, 33 insertions(+), 6 deletions(-) diff --git a/doomsday/libshell/src/abstractlineeditor.cpp b/doomsday/libshell/src/abstractlineeditor.cpp index a7aeb1c7b9..c118eed6a0 100644 --- a/doomsday/libshell/src/abstractlineeditor.cpp +++ b/doomsday/libshell/src/abstractlineeditor.cpp @@ -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) @@ -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(); } @@ -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: