Refactor keymap definitions using macros

Adds a macro rule to the `key!` macro so that keymaps using `Left`,
`Home`, `Esc`, etc. will also be accepted.
pull/259/head
Gokul Soumya 3 years ago committed by Blaž Hrastnik
parent 6b8c6ed535
commit 9cfa163370

@ -100,6 +100,12 @@ pub type Keymaps = HashMap<Mode, Keymap>;
#[macro_export] #[macro_export]
macro_rules! key { macro_rules! key {
($key:ident) => {
KeyEvent {
code: KeyCode::$key,
modifiers: KeyModifiers::NONE,
}
};
($($ch:tt)*) => { ($($ch:tt)*) => {
KeyEvent { KeyEvent {
code: KeyCode::Char($($ch)*), code: KeyCode::Char($($ch)*),
@ -133,22 +139,10 @@ pub fn default() -> Keymaps {
key!('k') => commands::move_line_up, key!('k') => commands::move_line_up,
key!('l') => commands::move_char_right, key!('l') => commands::move_char_right,
KeyEvent { key!(Left) => commands::move_char_left,
code: KeyCode::Left, key!(Down) => commands::move_line_down,
modifiers: KeyModifiers::NONE key!(Up) => commands::move_line_up,
} => commands::move_char_left, key!(Right) => commands::move_char_right,
KeyEvent {
code: KeyCode::Down,
modifiers: KeyModifiers::NONE
} => commands::move_line_down,
KeyEvent {
code: KeyCode::Up,
modifiers: KeyModifiers::NONE
} => commands::move_line_up,
KeyEvent {
code: KeyCode::Right,
modifiers: KeyModifiers::NONE
} => commands::move_char_right,
key!('t') => commands::find_till_char, key!('t') => commands::find_till_char,
key!('f') => commands::find_next_char, key!('f') => commands::find_next_char,
@ -159,15 +153,8 @@ pub fn default() -> Keymaps {
key!('r') => commands::replace, key!('r') => commands::replace,
key!('R') => commands::replace_with_yanked, key!('R') => commands::replace_with_yanked,
KeyEvent { key!(Home) => commands::move_line_start,
code: KeyCode::Home, key!(End) => commands::move_line_end,
modifiers: KeyModifiers::NONE
} => commands::move_line_start,
KeyEvent {
code: KeyCode::End,
modifiers: KeyModifiers::NONE
} => commands::move_line_end,
key!('w') => commands::move_next_word_start, key!('w') => commands::move_next_word_start,
key!('b') => commands::move_prev_word_start, key!('b') => commands::move_prev_word_start,
@ -255,19 +242,10 @@ pub fn default() -> Keymaps {
// C / altC = copy (repeat) selections on prev/next lines // C / altC = copy (repeat) selections on prev/next lines
KeyEvent { key!(Esc) => commands::normal_mode,
code: KeyCode::Esc, key!(PageUp) => commands::page_up,
modifiers: KeyModifiers::NONE key!(PageDown) => commands::page_down,
} => commands::normal_mode,
KeyEvent {
code: KeyCode::PageUp,
modifiers: KeyModifiers::NONE
} => commands::page_up,
ctrl!('b') => commands::page_up, ctrl!('b') => commands::page_up,
KeyEvent {
code: KeyCode::PageDown,
modifiers: KeyModifiers::NONE
} => commands::page_down,
ctrl!('f') => commands::page_down, ctrl!('f') => commands::page_down,
ctrl!('u') => commands::half_page_up, ctrl!('u') => commands::half_page_up,
ctrl!('d') => commands::half_page_down, ctrl!('d') => commands::half_page_down,
@ -280,10 +258,8 @@ pub fn default() -> Keymaps {
// z family for save/restore/combine from/to sels from register // z family for save/restore/combine from/to sels from register
KeyEvent { // supposedly ctrl!('i') but did not work // supposedly ctrl!('i') but did not work
code: KeyCode::Tab, key!(Tab) => commands::jump_forward,
modifiers: KeyModifiers::NONE,
} => commands::jump_forward,
ctrl!('o') => commands::jump_backward, ctrl!('o') => commands::jump_backward,
// ctrl!('s') => commands::save_selection, // ctrl!('s') => commands::save_selection,
@ -303,22 +279,10 @@ pub fn default() -> Keymaps {
key!('k') => commands::extend_line_up, key!('k') => commands::extend_line_up,
key!('l') => commands::extend_char_right, key!('l') => commands::extend_char_right,
KeyEvent { key!(Left) => commands::extend_char_left,
code: KeyCode::Left, key!(Down) => commands::extend_line_down,
modifiers: KeyModifiers::NONE key!(Up) => commands::extend_line_up,
} => commands::extend_char_left, key!(Right) => commands::extend_char_right,
KeyEvent {
code: KeyCode::Down,
modifiers: KeyModifiers::NONE
} => commands::extend_line_down,
KeyEvent {
code: KeyCode::Up,
modifiers: KeyModifiers::NONE
} => commands::extend_line_up,
KeyEvent {
code: KeyCode::Right,
modifiers: KeyModifiers::NONE
} => commands::extend_char_right,
key!('w') => commands::extend_next_word_start, key!('w') => commands::extend_next_word_start,
key!('b') => commands::extend_prev_word_start, key!('b') => commands::extend_prev_word_start,
@ -329,18 +293,9 @@ pub fn default() -> Keymaps {
key!('T') => commands::extend_till_prev_char, key!('T') => commands::extend_till_prev_char,
key!('F') => commands::extend_prev_char, key!('F') => commands::extend_prev_char,
KeyEvent { key!(Home) => commands::extend_line_start,
code: KeyCode::Home, key!(End) => commands::extend_line_end,
modifiers: KeyModifiers::NONE key!(Esc) => commands::exit_select_mode,
} => commands::extend_line_start,
KeyEvent {
code: KeyCode::End,
modifiers: KeyModifiers::NONE
} => commands::extend_line_end,
KeyEvent {
code: KeyCode::Esc,
modifiers: KeyModifiers::NONE
} => commands::exit_select_mode,
) )
.into_iter(), .into_iter(),
); );
@ -351,26 +306,11 @@ pub fn default() -> Keymaps {
Mode::Normal => normal, Mode::Normal => normal,
Mode::Select => select, Mode::Select => select,
Mode::Insert => hashmap!( Mode::Insert => hashmap!(
KeyEvent { key!(Esc) => commands::normal_mode as Command,
code: KeyCode::Esc, key!(Backspace) => commands::insert::delete_char_backward,
modifiers: KeyModifiers::NONE key!(Delete) => commands::insert::delete_char_forward,
} => commands::normal_mode as Command, key!(Enter) => commands::insert::insert_newline,
KeyEvent { key!(Tab) => commands::insert::insert_tab,
code: KeyCode::Backspace,
modifiers: KeyModifiers::NONE
} => commands::insert::delete_char_backward,
KeyEvent {
code: KeyCode::Delete,
modifiers: KeyModifiers::NONE
} => commands::insert::delete_char_forward,
KeyEvent {
code: KeyCode::Enter,
modifiers: KeyModifiers::NONE
} => commands::insert::insert_newline,
KeyEvent {
code: KeyCode::Tab,
modifiers: KeyModifiers::NONE
} => commands::insert::insert_tab,
ctrl!('x') => commands::completion, ctrl!('x') => commands::completion,
ctrl!('w') => commands::insert::delete_word_backward, ctrl!('w') => commands::insert::delete_word_backward,

Loading…
Cancel
Save