Specify key modifiers using constexpr functions for brevity

This commit is contained in:
Maxime Coste 2013-10-26 18:42:36 +01:00
parent 0746e7309e
commit cf6c07d37d
4 changed files with 114 additions and 110 deletions

View File

@ -207,7 +207,7 @@ public:
m_callback(selected, MenuEvent::Validate, context()); m_callback(selected, MenuEvent::Validate, context());
return; 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) if (m_edit_filter)
{ {
@ -876,7 +876,7 @@ public:
bool update_completions = true; bool update_completions = true;
bool moved = false; 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()); context().hooks().run_hook("InsertEnd", "", context());
m_completer.reset(); m_completer.reset();
@ -909,25 +909,25 @@ public:
m_inserter.insert(codepoint_to_str(key.key)); m_inserter.insert(codepoint_to_str(key.key));
context().hooks().run_hook("InsertKey", key_to_str(key), context()); 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; m_mode = Mode::InsertReg;
else if ( key == Key{ Key::Modifiers::Control, 'm' }) else if ( key == ctrl('m'))
m_inserter.insert(String() + '\n'); m_inserter.insert(String() + '\n');
else if ( key == Key{ Key::Modifiers::Control, 'i' }) else if ( key == ctrl('i'))
m_inserter.insert(String() + '\t'); m_inserter.insert(String() + '\t');
else if ( key == Key{ Key::Modifiers::Control, 'n' }) else if ( key == ctrl('n'))
{ {
m_completer.select(1); m_completer.select(1);
update_completions = false; update_completions = false;
} }
else if ( key == Key{ Key::Modifiers::Control, 'p' }) else if ( key == ctrl('p'))
{ {
m_completer.select(-1); m_completer.select(-1);
update_completions = false; update_completions = false;
} }
else if ( key == Key{ Key::Modifiers::Control, 'x' }) else if ( key == ctrl('x'))
m_mode = Mode::Complete; m_mode = Mode::Complete;
else if ( key == Key{ Key::Modifiers::Control, 'u' }) else if ( key == ctrl('u'))
context().buffer().commit_undo_group(); context().buffer().commit_undo_group();
if (update_completions) if (update_completions)

View File

@ -56,6 +56,10 @@ typedef std::vector<Key> KeyList;
KeyList parse_keys(const String& str); KeyList parse_keys(const String& str);
String key_to_str(Key key); 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 namespace std

View File

@ -316,7 +316,7 @@ Key NCursesUI::get_key()
{ {
if (c == CTRL('l')) if (c == CTRL('l'))
redrawwin(stdscr); redrawwin(stdscr);
return {Key::Modifiers::Control, Codepoint(c) - 1 + 'a'}; return ctrl(Codepoint(c) - 1 + 'a');
} }
else if (c == 27) else if (c == 27)
{ {
@ -324,7 +324,7 @@ Key NCursesUI::get_key()
const Codepoint new_c = getch(); const Codepoint new_c = getch();
timeout(-1); timeout(-1);
if (new_c != ERR) if (new_c != ERR)
return {Key::Modifiers::Alt, new_c}; return alt(new_c);
else else
return Key::Escape; return Key::Escape;
} }

View File

@ -885,132 +885,132 @@ void move(Context& context, int count)
KeyMap keymap = KeyMap keymap =
{ {
{ { Key::Modifiers::None, 'h' }, move<CharCount, Backward> }, { 'h', move<CharCount, Backward> },
{ { Key::Modifiers::None, 'j' }, move<LineCount, Forward> }, { 'j', move<LineCount, Forward> },
{ { Key::Modifiers::None, 'k' }, move<LineCount, Backward> }, { 'k', move<LineCount, Backward> },
{ { Key::Modifiers::None, 'l' }, move<CharCount, Forward> }, { 'l', move<CharCount, Forward> },
{ { Key::Modifiers::None, 'H' }, move<CharCount, Backward, SelectMode::Extend> }, { 'H', move<CharCount, Backward, SelectMode::Extend> },
{ { Key::Modifiers::None, 'J' }, move<LineCount, Forward, SelectMode::Extend> }, { 'J', move<LineCount, Forward, SelectMode::Extend> },
{ { Key::Modifiers::None, 'K' }, move<LineCount, Backward, SelectMode::Extend> }, { 'K', move<LineCount, Backward, SelectMode::Extend> },
{ { Key::Modifiers::None, 'L' }, move<CharCount, Forward, SelectMode::Extend> }, { 'L', move<CharCount, Forward, SelectMode::Extend> },
{ { Key::Modifiers::None, 't' }, select_to_next_char<SelectFlags::None> }, { 't', select_to_next_char<SelectFlags::None> },
{ { Key::Modifiers::None, 'f' }, select_to_next_char<SelectFlags::Inclusive> }, { 'f', select_to_next_char<SelectFlags::Inclusive> },
{ { Key::Modifiers::None, 'T' }, select_to_next_char<SelectFlags::Extend> }, { 'T', select_to_next_char<SelectFlags::Extend> },
{ { Key::Modifiers::None, 'F' }, select_to_next_char<SelectFlags::Inclusive | SelectFlags::Extend> }, { 'F', select_to_next_char<SelectFlags::Inclusive | SelectFlags::Extend> },
{ { Key::Modifiers::Alt, 't' }, select_to_next_char<SelectFlags::Reverse> }, { alt('t'), select_to_next_char<SelectFlags::Reverse> },
{ { Key::Modifiers::Alt, 'f' }, select_to_next_char<SelectFlags::Inclusive | SelectFlags::Reverse> }, { alt('f'), select_to_next_char<SelectFlags::Inclusive | SelectFlags::Reverse> },
{ { Key::Modifiers::Alt, 'T' }, select_to_next_char<SelectFlags::Extend | SelectFlags::Reverse> }, { alt('T'), select_to_next_char<SelectFlags::Extend | SelectFlags::Reverse> },
{ { Key::Modifiers::Alt, 'F' }, select_to_next_char<SelectFlags::Inclusive | SelectFlags::Extend | SelectFlags::Reverse> }, { alt('F'), select_to_next_char<SelectFlags::Inclusive | SelectFlags::Extend | SelectFlags::Reverse> },
{ { Key::Modifiers::None, 'd' }, erase_selections }, { 'd', erase_selections },
{ { Key::Modifiers::None, 'c' }, change }, { 'c', change },
{ { Key::Modifiers::None, 'i' }, insert<InsertMode::Insert> }, { 'i', insert<InsertMode::Insert> },
{ { Key::Modifiers::None, 'I' }, insert<InsertMode::InsertAtLineBegin> }, { 'I', insert<InsertMode::InsertAtLineBegin> },
{ { Key::Modifiers::None, 'a' }, insert<InsertMode::Append> }, { 'a', insert<InsertMode::Append> },
{ { Key::Modifiers::None, 'A' }, insert<InsertMode::AppendAtLineEnd> }, { 'A', insert<InsertMode::AppendAtLineEnd> },
{ { Key::Modifiers::None, 'o' }, insert<InsertMode::OpenLineBelow> }, { 'o', insert<InsertMode::OpenLineBelow> },
{ { Key::Modifiers::None, 'O' }, insert<InsertMode::OpenLineAbove> }, { 'O', insert<InsertMode::OpenLineAbove> },
{ { Key::Modifiers::None, 'r' }, replace_with_char }, { 'r', replace_with_char },
{ { Key::Modifiers::None, 'g' }, goto_commands<SelectMode::Replace> }, { 'g', goto_commands<SelectMode::Replace> },
{ { Key::Modifiers::None, 'G' }, goto_commands<SelectMode::Extend> }, { 'G', goto_commands<SelectMode::Extend> },
{ { Key::Modifiers::None, 'v' }, view_commands }, { 'v', view_commands },
{ { Key::Modifiers::None, 'y' }, yank }, { 'y', yank },
{ { Key::Modifiers::None, 'Y' }, cat_yank }, { 'Y', cat_yank },
{ { Key::Modifiers::None, 'p' }, repeated(paste<InsertMode::Append>) }, { 'p', repeated(paste<InsertMode::Append>) },
{ { Key::Modifiers::None, 'P' }, repeated(paste<InsertMode::Insert>) }, { 'P', repeated(paste<InsertMode::Insert>) },
{ { Key::Modifiers::Alt, 'p' }, paste<InsertMode::Replace> }, { alt('p'), paste<InsertMode::Replace> },
{ { Key::Modifiers::None, 's' }, select_regex }, { 's', select_regex },
{ { Key::Modifiers::None, 'S' }, split_regex }, { 'S', split_regex },
{ { Key::Modifiers::Alt, 's' }, split_lines }, { 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 }, { ':', command },
{ { Key::Modifiers::None, '|' }, pipe }, { '|', pipe },
{ { Key::Modifiers::None, ' ' }, [](Context& context, int count) { if (count == 0) context.editor().clear_selections(); { ' ', [](Context& context, int count) { if (count == 0) context.editor().clear_selections();
else context.editor().keep_selection(count-1); } }, 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); } }, else context.editor().remove_selection(count-1); } },
{ { Key::Modifiers::None, 'w' }, repeated(select<SelectMode::Replace>(select_to_next_word<Word>)) }, { 'w', repeated(select<SelectMode::Replace>(select_to_next_word<Word>)) },
{ { Key::Modifiers::None, 'e' }, repeated(select<SelectMode::Replace>(select_to_next_word_end<Word>)) }, { 'e', repeated(select<SelectMode::Replace>(select_to_next_word_end<Word>)) },
{ { Key::Modifiers::None, 'b' }, repeated(select<SelectMode::Replace>(select_to_previous_word<Word>)) }, { 'b', repeated(select<SelectMode::Replace>(select_to_previous_word<Word>)) },
{ { Key::Modifiers::None, 'W' }, repeated(select<SelectMode::Extend>(select_to_next_word<Word>)) }, { 'W', repeated(select<SelectMode::Extend>(select_to_next_word<Word>)) },
{ { Key::Modifiers::None, 'E' }, repeated(select<SelectMode::Extend>(select_to_next_word_end<Word>)) }, { 'E', repeated(select<SelectMode::Extend>(select_to_next_word_end<Word>)) },
{ { Key::Modifiers::None, 'B' }, repeated(select<SelectMode::Extend>(select_to_previous_word<Word>)) }, { 'B', repeated(select<SelectMode::Extend>(select_to_previous_word<Word>)) },
{ { Key::Modifiers::Alt, 'w' }, repeated(select<SelectMode::Replace>(select_to_next_word<WORD>)) }, { alt('w'), repeated(select<SelectMode::Replace>(select_to_next_word<WORD>)) },
{ { Key::Modifiers::Alt, 'e' }, repeated(select<SelectMode::Replace>(select_to_next_word_end<WORD>)) }, { alt('e'), repeated(select<SelectMode::Replace>(select_to_next_word_end<WORD>)) },
{ { Key::Modifiers::Alt, 'b' }, repeated(select<SelectMode::Replace>(select_to_previous_word<WORD>)) }, { alt('b'), repeated(select<SelectMode::Replace>(select_to_previous_word<WORD>)) },
{ { Key::Modifiers::Alt, 'W' }, repeated(select<SelectMode::Extend>(select_to_next_word<WORD>)) }, { alt('W'), repeated(select<SelectMode::Extend>(select_to_next_word<WORD>)) },
{ { Key::Modifiers::Alt, 'E' }, repeated(select<SelectMode::Extend>(select_to_next_word_end<WORD>)) }, { alt('E'), repeated(select<SelectMode::Extend>(select_to_next_word_end<WORD>)) },
{ { Key::Modifiers::Alt, 'B' }, repeated(select<SelectMode::Extend>(select_to_previous_word<WORD>)) }, { alt('B'), repeated(select<SelectMode::Extend>(select_to_previous_word<WORD>)) },
{ { Key::Modifiers::Alt, 'l' }, repeated(select<SelectMode::Replace>(select_to_eol)) }, { alt('l'), repeated(select<SelectMode::Replace>(select_to_eol)) },
{ { Key::Modifiers::Alt, 'L' }, repeated(select<SelectMode::Extend>(select_to_eol)) }, { alt('L'), repeated(select<SelectMode::Extend>(select_to_eol)) },
{ { Key::Modifiers::Alt, 'h' }, repeated(select<SelectMode::Replace>(select_to_eol_reverse)) }, { alt('h'), repeated(select<SelectMode::Replace>(select_to_eol_reverse)) },
{ { Key::Modifiers::Alt, 'H' }, repeated(select<SelectMode::Extend>(select_to_eol_reverse)) }, { alt('H'), repeated(select<SelectMode::Extend>(select_to_eol_reverse)) },
{ { Key::Modifiers::None, 'x' }, repeated(select<SelectMode::Replace>(select_line)) }, { 'x', repeated(select<SelectMode::Replace>(select_line)) },
{ { Key::Modifiers::None, 'X' }, repeated(select<SelectMode::Extend>(select_line)) }, { 'X', repeated(select<SelectMode::Extend>(select_line)) },
{ { Key::Modifiers::Alt, 'x' }, select<SelectMode::Replace>(select_whole_lines) }, { alt('x'), select<SelectMode::Replace>(select_whole_lines) },
{ { Key::Modifiers::Alt, 'X' }, select<SelectMode::Replace>(trim_partial_lines) }, { alt('X'), select<SelectMode::Replace>(trim_partial_lines) },
{ { Key::Modifiers::None, 'm' }, select<SelectMode::Replace>(select_matching) }, { 'm', select<SelectMode::Replace>(select_matching) },
{ { Key::Modifiers::None, 'M' }, select<SelectMode::Extend>(select_matching) }, { 'M', select<SelectMode::Extend>(select_matching) },
{ { Key::Modifiers::None, '/' }, search<SelectMode::Replace, Forward> }, { '/', search<SelectMode::Replace, Forward> },
{ { Key::Modifiers::None, '?' }, search<SelectMode::Extend, Forward> }, { '?', search<SelectMode::Extend, Forward> },
{ { Key::Modifiers::Alt, '/' }, search<SelectMode::Replace, Backward> }, { alt('/'), search<SelectMode::Replace, Backward> },
{ { Key::Modifiers::Alt, '?' }, search<SelectMode::Extend, Backward> }, { alt('?'), search<SelectMode::Extend, Backward> },
{ { Key::Modifiers::None, 'n' }, search_next<SelectMode::Replace, Forward> }, { 'n', search_next<SelectMode::Replace, Forward> },
{ { Key::Modifiers::Alt, 'n' }, search_next<SelectMode::ReplaceMain, Forward> }, { alt('n'), search_next<SelectMode::ReplaceMain, Forward> },
{ { Key::Modifiers::None, 'N' }, search_next<SelectMode::Append, Forward> }, { 'N', search_next<SelectMode::Append, Forward> },
{ { Key::Modifiers::None, '*' }, use_selection_as_search_pattern<true> }, { '*', use_selection_as_search_pattern<true> },
{ { Key::Modifiers::Alt, '*' }, use_selection_as_search_pattern<false> }, { alt('*'), use_selection_as_search_pattern<false> },
{ { Key::Modifiers::None, '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().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().redo()) context.print_status({ "nothing left to redo", get_color("Information") }); }) },
{ { Key::Modifiers::Alt, 'i' }, select_object<ObjectFlags::ToBegin | ObjectFlags::ToEnd | ObjectFlags::Inner> }, { alt('i'), select_object<ObjectFlags::ToBegin | ObjectFlags::ToEnd | ObjectFlags::Inner> },
{ { Key::Modifiers::Alt, 'a' }, select_object<ObjectFlags::ToBegin | ObjectFlags::ToEnd> }, { alt('a'), select_object<ObjectFlags::ToBegin | ObjectFlags::ToEnd> },
{ { Key::Modifiers::None, ']' }, select_object<ObjectFlags::ToEnd> }, { ']', select_object<ObjectFlags::ToEnd> },
{ { Key::Modifiers::None, '[' }, select_object<ObjectFlags::ToBegin> }, { '[', select_object<ObjectFlags::ToBegin> },
{ { Key::Modifiers::None, '}' }, select_object<ObjectFlags::ToEnd, SelectMode::Extend> }, { '}', select_object<ObjectFlags::ToEnd, SelectMode::Extend> },
{ { Key::Modifiers::None, '{' }, select_object<ObjectFlags::ToBegin, SelectMode::Extend> }, { '{', select_object<ObjectFlags::ToBegin, SelectMode::Extend> },
{ { Key::Modifiers::Alt, 'j' }, join }, { alt('j'), join },
{ { Key::Modifiers::Alt, 'J' }, join_select_spaces }, { alt('J'), join_select_spaces },
{ { Key::Modifiers::Alt, 'k' }, keep<true> }, { alt('k'), keep<true> },
{ { Key::Modifiers::Alt, 'K' }, keep<false> }, { alt('K'), keep<false> },
{ { Key::Modifiers::None, '<' }, deindent }, { '<', deindent },
{ { Key::Modifiers::None, '>' }, indent }, { '>', indent },
{ { Key::Modifiers::Control, 'i' }, jump<Forward> }, { ctrl('i'), jump<Forward> },
{ { Key::Modifiers::Control, 'o' }, jump<Backward> }, { ctrl('o'), jump<Backward> },
{ { Key::Modifiers::Control, 's' }, save_selections }, { ctrl('s'), save_selections },
{ { Key::Modifiers::Alt, 'r' }, rotate_selections }, { alt('r'), rotate_selections },
{ { Key::Modifiers::Alt, 'R' }, rotate_selections_content }, { alt('R'), rotate_selections_content },
{ { Key::Modifiers::None, 'q' }, start_or_end_macro_recording }, { 'q', start_or_end_macro_recording },
{ { Key::Modifiers::None, 'Q' }, replay_macro }, { 'Q', replay_macro },
{ { Key::Modifiers::None, '`' }, for_each_char<to_lower> }, { '`', for_each_char<to_lower> },
{ { Key::Modifiers::None, '~' }, for_each_char<to_upper> }, { '~', for_each_char<to_upper> },
{ { Key::Modifiers::Alt, '`' }, for_each_char<swap_case> }, { alt('`'), for_each_char<swap_case> },
{ { Key::Modifiers::None, '&' }, align<false> }, { '&', align<false> },
{ { Key::Modifiers::Alt, '&' }, align<true> }, { alt('&'), align<true> },
{ Key::Left, move<CharCount, Backward> }, { Key::Left, move<CharCount, Backward> },
{ Key::Down, move<LineCount, Forward> }, { Key::Down, move<LineCount, Forward> },