From 658b6b0f1aad8dc85c2a49bb50d50e71d0174f1e Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Sat, 11 Dec 2021 08:12:08 +1100 Subject: [PATCH] Make space a named key to correctly handle shift modifier --- src/input_handler.cc | 2 +- src/keys.cc | 9 ++++++--- src/keys.hh | 1 + src/normal.cc | 8 ++++---- src/terminal_ui.cc | 6 ++++-- 5 files changed, 16 insertions(+), 10 deletions(-) diff --git a/src/input_handler.cc b/src/input_handler.cc index fbfadf816..ef25826b6 100644 --- a/src/input_handler.cc +++ b/src/input_handler.cc @@ -1003,7 +1003,7 @@ public: } else { - if (key == ' ' and + if (key == Key::Space and not (m_completions.flags & Completions::Flags::Quoted) and // if token is quoted, this space does not end it can_auto_insert_completion()) m_line_editor.insert_from(line.char_count_to(m_completions.start), diff --git a/src/keys.cc b/src/keys.cc index 25466fd30..8eb3fce0f 100644 --- a/src/keys.cc +++ b/src/keys.cc @@ -49,6 +49,8 @@ Optional Key::codepoint() const return '\n'; if (*this == Key::Tab) return '\t'; + if (*this == Key::Space) + return ' '; if (*this == Key::Escape) return 0x1B; if (modifiers == Modifiers::None and key > 27 and @@ -60,7 +62,7 @@ Optional Key::codepoint() const struct KeyAndName { const char* name; Codepoint key; }; static constexpr KeyAndName keynamemap[] = { { "ret", Key::Return }, - { "space", ' ' }, + { "space", Key::Space }, { "tab", Key::Tab }, { "lt", '<' }, { "gt", '>' }, @@ -99,6 +101,7 @@ KeyList parse_keys(StringView str) case '\r': return Key::Return; case '\b': return Key::Backspace; case '\t': return Key::Tab; + case ' ': return Key::Space; case '\033': return Key::Escape; default: return cp; } @@ -225,9 +228,9 @@ String key_to_str(Key key) UnitTest test_keys{[]() { KeyList keys{ - { ' ' }, + {Key::Space}, { 'c' }, - { Key::Up }, + {Key::Up}, alt('j'), ctrl('r'), shift(Key::Up), diff --git a/src/keys.hh b/src/keys.hh index da2895e79..9e74a55a6 100644 --- a/src/keys.hh +++ b/src/keys.hh @@ -53,6 +53,7 @@ struct Key End, Insert, Tab, + Space, F1, F2, F3, diff --git a/src/normal.cc b/src/normal.cc index a512f8d0a..c1a084a40 100644 --- a/src/normal.cc +++ b/src/normal.cc @@ -1293,7 +1293,7 @@ void select_object(Context& context, NormalParams params) { alt('w'), select_word }, { 's', select_sentence }, { 'p', select_paragraph }, - { ' ', select_whitespaces }, + { Key::Space, select_whitespaces }, { 'i', select_indent }, { 'n', select_number }, { 'u', select_argument }, @@ -1399,7 +1399,7 @@ void select_object(Context& context, NormalParams params) {{alt('w')}, "WORD"}, {{'s'}, "sentence"}, {{'p'}, "paragraph"}, - {{' '}, "whitespaces"}, + {{Key::Space}, "whitespaces"}, {{'i'}, "indent"}, {{'u'}, "argument"}, {{'n'}, "number"}, @@ -2278,8 +2278,8 @@ static constexpr HashMap { {'!'}, {"insert command output", insert_output} }, { {alt('!')}, {"append command output", insert_output} }, - { {' '}, {"remove all selections except main", keep_selection} }, - { {alt(' ')}, {"remove main selection", remove_selection} }, + { {Key::Space}, {"remove all selections except main", keep_selection} }, + { {alt(Key::Space)}, {"remove main selection", remove_selection} }, { {';'}, {"reduce selections to their cursor", clear_selections} }, { {alt(';')}, {"swap selections cursor and anchor", flip_selections} }, { {alt(':')}, {"ensure selection cursor is after anchor", ensure_forward} }, diff --git a/src/terminal_ui.cc b/src/terminal_ui.cc index 6746074d8..a20d27ee4 100644 --- a/src/terminal_ui.cc +++ b/src/terminal_ui.cc @@ -705,6 +705,8 @@ Optional TerminalUI::get_next_key() return Key::Return; if (c == control('i')) return Key::Tab; + if (c == ' ') + return Key::Space; if (c == m_original_termios.c_cc[VERASE]) return Key::Backspace; if (c == 127) // when it's not backspace @@ -719,7 +721,7 @@ Optional TerminalUI::get_next_key() // Special case: you can type NUL with Ctrl-2 or Ctrl-Shift-2 or // Ctrl-Backtick, but the most straightforward way is Ctrl-Space. if (c == 0) - return ctrl(' '); + return ctrl(Key::Space); // Represent Ctrl-letter combinations in lower-case, to be clear // that Shift is not involved. if (c < 27) @@ -908,7 +910,7 @@ Optional TerminalUI::get_next_key() switch (code) { - case ' ': return Key{mod, ' '}; + case ' ': return Key{mod, Key::Space}; case 'A': return Key{mod, Key::Up}; case 'B': return Key{mod, Key::Down}; case 'C': return Key{mod, Key::Right};