From cf6c07d37dc1c81ec50d50404b8161d5f041b398 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Sat, 26 Oct 2013 18:42:36 +0100 Subject: [PATCH] Specify key modifiers using constexpr functions for brevity --- src/client.cc | 18 ++--- src/keys.hh | 4 + src/ncurses.cc | 4 +- src/normal.cc | 198 ++++++++++++++++++++++++------------------------- 4 files changed, 114 insertions(+), 110 deletions(-) diff --git a/src/client.cc b/src/client.cc index 1bc4a2c1..a596a961 100644 --- a/src/client.cc +++ b/src/client.cc @@ -207,7 +207,7 @@ public: m_callback(selected, MenuEvent::Validate, context()); return; } - else if (key == Key::Escape or key == Key{ Key::Modifiers::Control, 'c' }) + else if (key == Key::Escape or key == ctrl('c')) { if (m_edit_filter) { @@ -876,7 +876,7 @@ public: bool update_completions = true; bool moved = false; - if (key == Key::Escape or key == Key{ Key::Modifiers::Control, 'c' }) + if (key == Key::Escape or key == ctrl('c')) { context().hooks().run_hook("InsertEnd", "", context()); m_completer.reset(); @@ -909,25 +909,25 @@ public: m_inserter.insert(codepoint_to_str(key.key)); context().hooks().run_hook("InsertKey", key_to_str(key), context()); } - else if (key == Key{ Key::Modifiers::Control, 'r' }) + else if (key == ctrl('r')) m_mode = Mode::InsertReg; - else if ( key == Key{ Key::Modifiers::Control, 'm' }) + else if ( key == ctrl('m')) m_inserter.insert(String() + '\n'); - else if ( key == Key{ Key::Modifiers::Control, 'i' }) + else if ( key == ctrl('i')) m_inserter.insert(String() + '\t'); - else if ( key == Key{ Key::Modifiers::Control, 'n' }) + else if ( key == ctrl('n')) { m_completer.select(1); update_completions = false; } - else if ( key == Key{ Key::Modifiers::Control, 'p' }) + else if ( key == ctrl('p')) { m_completer.select(-1); update_completions = false; } - else if ( key == Key{ Key::Modifiers::Control, 'x' }) + else if ( key == ctrl('x')) m_mode = Mode::Complete; - else if ( key == Key{ Key::Modifiers::Control, 'u' }) + else if ( key == ctrl('u')) context().buffer().commit_undo_group(); if (update_completions) diff --git a/src/keys.hh b/src/keys.hh index cfff452f..05254f62 100644 --- a/src/keys.hh +++ b/src/keys.hh @@ -56,6 +56,10 @@ typedef std::vector KeyList; KeyList parse_keys(const String& str); String key_to_str(Key key); +constexpr Key alt(Codepoint key) { return { Key::Modifiers::Alt, key }; } +constexpr Key ctrl(Codepoint key) { return { Key::Modifiers::Control, key }; } +constexpr Key ctrlalt(Codepoint key) { return { Key::Modifiers::ControlAlt, key }; } + } namespace std diff --git a/src/ncurses.cc b/src/ncurses.cc index 9689389f..4bd1b3b0 100644 --- a/src/ncurses.cc +++ b/src/ncurses.cc @@ -316,7 +316,7 @@ Key NCursesUI::get_key() { if (c == CTRL('l')) redrawwin(stdscr); - return {Key::Modifiers::Control, Codepoint(c) - 1 + 'a'}; + return ctrl(Codepoint(c) - 1 + 'a'); } else if (c == 27) { @@ -324,7 +324,7 @@ Key NCursesUI::get_key() const Codepoint new_c = getch(); timeout(-1); if (new_c != ERR) - return {Key::Modifiers::Alt, new_c}; + return alt(new_c); else return Key::Escape; } diff --git a/src/normal.cc b/src/normal.cc index 9b453907..7f2c9568 100644 --- a/src/normal.cc +++ b/src/normal.cc @@ -885,132 +885,132 @@ void move(Context& context, int count) KeyMap keymap = { - { { Key::Modifiers::None, 'h' }, move }, - { { Key::Modifiers::None, 'j' }, move }, - { { Key::Modifiers::None, 'k' }, move }, - { { Key::Modifiers::None, 'l' }, move }, + { 'h', move }, + { 'j', move }, + { 'k', move }, + { 'l', move }, - { { Key::Modifiers::None, 'H' }, move }, - { { Key::Modifiers::None, 'J' }, move }, - { { Key::Modifiers::None, 'K' }, move }, - { { Key::Modifiers::None, 'L' }, move }, + { 'H', move }, + { 'J', move }, + { 'K', move }, + { 'L', move }, - { { Key::Modifiers::None, 't' }, select_to_next_char }, - { { Key::Modifiers::None, 'f' }, select_to_next_char }, - { { Key::Modifiers::None, 'T' }, select_to_next_char }, - { { Key::Modifiers::None, 'F' }, select_to_next_char }, - { { Key::Modifiers::Alt, 't' }, select_to_next_char }, - { { Key::Modifiers::Alt, 'f' }, select_to_next_char }, - { { Key::Modifiers::Alt, 'T' }, select_to_next_char }, - { { Key::Modifiers::Alt, 'F' }, select_to_next_char }, + { 't', select_to_next_char }, + { 'f', select_to_next_char }, + { 'T', select_to_next_char }, + { 'F', select_to_next_char }, + { alt('t'), select_to_next_char }, + { alt('f'), select_to_next_char }, + { alt('T'), select_to_next_char }, + { alt('F'), select_to_next_char }, - { { Key::Modifiers::None, 'd' }, erase_selections }, - { { Key::Modifiers::None, 'c' }, change }, - { { Key::Modifiers::None, 'i' }, insert }, - { { Key::Modifiers::None, 'I' }, insert }, - { { Key::Modifiers::None, 'a' }, insert }, - { { Key::Modifiers::None, 'A' }, insert }, - { { Key::Modifiers::None, 'o' }, insert }, - { { Key::Modifiers::None, 'O' }, insert }, - { { Key::Modifiers::None, 'r' }, replace_with_char }, + { 'd', erase_selections }, + { 'c', change }, + { 'i', insert }, + { 'I', insert }, + { 'a', insert }, + { 'A', insert }, + { 'o', insert }, + { 'O', insert }, + { 'r', replace_with_char }, - { { Key::Modifiers::None, 'g' }, goto_commands }, - { { Key::Modifiers::None, 'G' }, goto_commands }, + { 'g', goto_commands }, + { 'G', goto_commands }, - { { Key::Modifiers::None, 'v' }, view_commands }, + { 'v', view_commands }, - { { Key::Modifiers::None, 'y' }, yank }, - { { Key::Modifiers::None, 'Y' }, cat_yank }, - { { Key::Modifiers::None, 'p' }, repeated(paste) }, - { { Key::Modifiers::None, 'P' }, repeated(paste) }, - { { Key::Modifiers::Alt, 'p' }, paste }, + { 'y', yank }, + { 'Y', cat_yank }, + { 'p', repeated(paste) }, + { 'P', repeated(paste) }, + { alt('p'), paste }, - { { Key::Modifiers::None, 's' }, select_regex }, - { { Key::Modifiers::None, 'S' }, split_regex }, - { { Key::Modifiers::Alt, 's' }, split_lines }, + { 's', select_regex }, + { 'S', split_regex }, + { alt('s'), split_lines }, - { { Key::Modifiers::None, '.' }, repeat_insert }, + { '.', repeat_insert }, - { { Key::Modifiers::None, '%' }, [](Context& context, int) { context.editor().clear_selections(); context.editor().select(select_whole_buffer); } }, + { '%', [](Context& context, int) { context.editor().clear_selections(); context.editor().select(select_whole_buffer); } }, - { { Key::Modifiers::None, ':' }, command }, - { { Key::Modifiers::None, '|' }, pipe }, - { { Key::Modifiers::None, ' ' }, [](Context& context, int count) { if (count == 0) context.editor().clear_selections(); + { ':', command }, + { '|', pipe }, + { ' ', [](Context& context, int count) { if (count == 0) context.editor().clear_selections(); else context.editor().keep_selection(count-1); } }, - { { Key::Modifiers::Alt, ' ' }, [](Context& context, int count) { if (count == 0) context.editor().flip_selections(); + { alt(' '), [](Context& context, int count) { if (count == 0) context.editor().flip_selections(); else context.editor().remove_selection(count-1); } }, - { { Key::Modifiers::None, 'w' }, repeated(select(select_to_next_word)) }, - { { Key::Modifiers::None, 'e' }, repeated(select(select_to_next_word_end)) }, - { { Key::Modifiers::None, 'b' }, repeated(select(select_to_previous_word)) }, - { { Key::Modifiers::None, 'W' }, repeated(select(select_to_next_word)) }, - { { Key::Modifiers::None, 'E' }, repeated(select(select_to_next_word_end)) }, - { { Key::Modifiers::None, 'B' }, repeated(select(select_to_previous_word)) }, + { 'w', repeated(select(select_to_next_word)) }, + { 'e', repeated(select(select_to_next_word_end)) }, + { 'b', repeated(select(select_to_previous_word)) }, + { 'W', repeated(select(select_to_next_word)) }, + { 'E', repeated(select(select_to_next_word_end)) }, + { 'B', repeated(select(select_to_previous_word)) }, - { { Key::Modifiers::Alt, 'w' }, repeated(select(select_to_next_word)) }, - { { Key::Modifiers::Alt, 'e' }, repeated(select(select_to_next_word_end)) }, - { { Key::Modifiers::Alt, 'b' }, repeated(select(select_to_previous_word)) }, - { { Key::Modifiers::Alt, 'W' }, repeated(select(select_to_next_word)) }, - { { Key::Modifiers::Alt, 'E' }, repeated(select(select_to_next_word_end)) }, - { { Key::Modifiers::Alt, 'B' }, repeated(select(select_to_previous_word)) }, + { alt('w'), repeated(select(select_to_next_word)) }, + { alt('e'), repeated(select(select_to_next_word_end)) }, + { alt('b'), repeated(select(select_to_previous_word)) }, + { alt('W'), repeated(select(select_to_next_word)) }, + { alt('E'), repeated(select(select_to_next_word_end)) }, + { alt('B'), repeated(select(select_to_previous_word)) }, - { { Key::Modifiers::Alt, 'l' }, repeated(select(select_to_eol)) }, - { { Key::Modifiers::Alt, 'L' }, repeated(select(select_to_eol)) }, - { { Key::Modifiers::Alt, 'h' }, repeated(select(select_to_eol_reverse)) }, - { { Key::Modifiers::Alt, 'H' }, repeated(select(select_to_eol_reverse)) }, + { alt('l'), repeated(select(select_to_eol)) }, + { alt('L'), repeated(select(select_to_eol)) }, + { alt('h'), repeated(select(select_to_eol_reverse)) }, + { alt('H'), repeated(select(select_to_eol_reverse)) }, - { { Key::Modifiers::None, 'x' }, repeated(select(select_line)) }, - { { Key::Modifiers::None, 'X' }, repeated(select(select_line)) }, - { { Key::Modifiers::Alt, 'x' }, select(select_whole_lines) }, - { { Key::Modifiers::Alt, 'X' }, select(trim_partial_lines) }, + { 'x', repeated(select(select_line)) }, + { 'X', repeated(select(select_line)) }, + { alt('x'), select(select_whole_lines) }, + { alt('X'), select(trim_partial_lines) }, - { { Key::Modifiers::None, 'm' }, select(select_matching) }, - { { Key::Modifiers::None, 'M' }, select(select_matching) }, + { 'm', select(select_matching) }, + { 'M', select(select_matching) }, - { { Key::Modifiers::None, '/' }, search }, - { { Key::Modifiers::None, '?' }, search }, - { { Key::Modifiers::Alt, '/' }, search }, - { { Key::Modifiers::Alt, '?' }, search }, - { { Key::Modifiers::None, 'n' }, search_next }, - { { Key::Modifiers::Alt, 'n' }, search_next }, - { { Key::Modifiers::None, 'N' }, search_next }, - { { Key::Modifiers::None, '*' }, use_selection_as_search_pattern }, - { { Key::Modifiers::Alt, '*' }, use_selection_as_search_pattern }, + { '/', search }, + { '?', search }, + { alt('/'), search }, + { alt('?'), search }, + { 'n', search_next }, + { alt('n'), search_next }, + { 'N', search_next }, + { '*', use_selection_as_search_pattern }, + { alt('*'), use_selection_as_search_pattern }, - { { Key::Modifiers::None, 'u' }, repeated([](Context& context, int) { if (not context.editor().undo()) context.print_status({ "nothing left to undo", get_color("Information") }); }) }, - { { Key::Modifiers::None, 'U' }, repeated([](Context& context, int) { if (not context.editor().redo()) context.print_status({ "nothing left to redo", get_color("Information") }); }) }, + { 'u', repeated([](Context& context, int) { if (not context.editor().undo()) context.print_status({ "nothing left to undo", get_color("Information") }); }) }, + { 'U', repeated([](Context& context, int) { if (not context.editor().redo()) context.print_status({ "nothing left to redo", get_color("Information") }); }) }, - { { Key::Modifiers::Alt, 'i' }, select_object }, - { { Key::Modifiers::Alt, 'a' }, select_object }, - { { Key::Modifiers::None, ']' }, select_object }, - { { Key::Modifiers::None, '[' }, select_object }, - { { Key::Modifiers::None, '}' }, select_object }, - { { Key::Modifiers::None, '{' }, select_object }, + { alt('i'), select_object }, + { alt('a'), select_object }, + { ']', select_object }, + { '[', select_object }, + { '}', select_object }, + { '{', select_object }, - { { Key::Modifiers::Alt, 'j' }, join }, - { { Key::Modifiers::Alt, 'J' }, join_select_spaces }, + { alt('j'), join }, + { alt('J'), join_select_spaces }, - { { Key::Modifiers::Alt, 'k' }, keep }, - { { Key::Modifiers::Alt, 'K' }, keep }, + { alt('k'), keep }, + { alt('K'), keep }, - { { Key::Modifiers::None, '<' }, deindent }, - { { Key::Modifiers::None, '>' }, indent }, + { '<', deindent }, + { '>', indent }, - { { Key::Modifiers::Control, 'i' }, jump }, - { { Key::Modifiers::Control, 'o' }, jump }, - { { Key::Modifiers::Control, 's' }, save_selections }, + { ctrl('i'), jump }, + { ctrl('o'), jump }, + { ctrl('s'), save_selections }, - { { Key::Modifiers::Alt, 'r' }, rotate_selections }, - { { Key::Modifiers::Alt, 'R' }, rotate_selections_content }, + { alt('r'), rotate_selections }, + { alt('R'), rotate_selections_content }, - { { Key::Modifiers::None, 'q' }, start_or_end_macro_recording }, - { { Key::Modifiers::None, 'Q' }, replay_macro }, + { 'q', start_or_end_macro_recording }, + { 'Q', replay_macro }, - { { Key::Modifiers::None, '`' }, for_each_char }, - { { Key::Modifiers::None, '~' }, for_each_char }, - { { Key::Modifiers::Alt, '`' }, for_each_char }, + { '`', for_each_char }, + { '~', for_each_char }, + { alt('`'), for_each_char }, - { { Key::Modifiers::None, '&' }, align }, - { { Key::Modifiers::Alt, '&' }, align }, + { '&', align }, + { alt('&'), align }, { Key::Left, move }, { Key::Down, move },