Skip to content

Commit

Permalink
ui/Keys: add IsBackspace()
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxKellermann committed Sep 10, 2024
1 parent c6ffc04 commit f14c0be
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
17 changes: 9 additions & 8 deletions src/dialogs/TextInputDialog.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,15 @@ TextInputDialog::OnKey(const Window window, int key)
if (IsFKey(key))
return false;

if (IsBackspace(key)) {
if (cursor > 0) { /* - 1 from buf[n+1] to buf */
MoveCursorLeft();
DeleteChar();
}

return true;
}

switch (key) {
case KEY_TAB:
#ifndef NCMPC_MINI
Expand Down Expand Up @@ -287,14 +296,6 @@ TextInputDialog::OnKey(const Window window, int key)
DeleteChar();
}
break;
case KEY_BACKSPACE3:
case KEY_BACKSPACE2: /* handle backspace: copy all */
case KEY_BACKSPACE: /* chars starting from curpos */
if (cursor > 0) { /* - 1 from buf[n+1] to buf */
MoveCursorLeft();
DeleteChar();
}
break;
case KEY_DC: /* handle delete key. As above */
case KEY_CTL('D'):
if (cursor < value.length())
Expand Down
3 changes: 2 additions & 1 deletion src/page/FindSupport.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "Command.hxx"
#include "dialogs/TextInputDialog.hxx"
#include "ui/Bell.hxx"
#include "ui/Keys.hxx"
#include "ui/ListWindow.hxx"
#include "ui/Options.hxx"
#include "co/InvokeTask.hxx"
Expand Down Expand Up @@ -93,7 +94,7 @@ FindSupport::Jump(ListWindow &lw,
while(1) {
key = screen_getch(screen, buffer);
/* if backspace or delete was pressed, process instead of ending loop */
if (key == KEY_BACKSPACE || key == KEY_DC) {
if (IsBackspace(key) || key == KEY_DC) {
const char *prev = PrevCharMB(buffer, iter);
if (search_str <= prev)
iter = const_cast<char *>(prev);
Expand Down
8 changes: 8 additions & 0 deletions src/ui/Keys.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,11 @@ IsFKey(int key) noexcept
{
return key >= KEY_F(0) && key <= KEY_F(63);
}

constexpr bool
IsBackspace(int key) noexcept
{
return key == KEY_BACKSPACE ||
key == KEY_BACKSPACE2 ||
key == KEY_BACKSPACE3;
}

0 comments on commit f14c0be

Please sign in to comment.