helix-mods/helix-term/src/keymap.rs

233 lines
8.1 KiB
Rust
Raw Normal View History

use crate::commands::{self, Command};
2020-10-23 06:51:08 +02:00
use helix_core::hashmap;
use helix_view::document::Mode;
2020-06-07 17:28:17 +02:00
use std::collections::HashMap;
// Kakoune-inspired:
// mode = {
// normal = {
// q = record_macro
// w = (next) word
2020-09-28 18:00:35 +02:00
// W = next WORD
2020-06-07 17:28:17 +02:00
// e = end of word
2020-09-28 18:00:35 +02:00
// E = end of WORD
2020-06-07 17:28:17 +02:00
// r =
// t = 'till char
// y = yank
// u = undo
// U = redo
// i = insert
// I = INSERT (start of line)
// o = open below (insert on new line below)
// O = open above (insert on new line above)
// p = paste (before cursor)
// P = PASTE (after cursor)
// ` =
// [ = select to text object start (alt = select whole object)
// ] = select to text object end
// { = extend to inner object start
// } = extend to inner object end
// a = append
// A = APPEND (end of line)
// s = split
// S = select
// d = delete()
// f = find_char()
// g = goto (gg, G, gc, gd, etc)
//
// h = move_char_left(n)
// j = move_line_down(n)
// k = move_line_up(n)
// l = move_char_right(n)
// : = command line
// ; = collapse selection to cursor
// " = use register
// ` = convert case? (to lower) (alt = swap case)
// ~ = convert to upper case
// . = repeat last command
// \ = disable hook?
// / = search
// > = indent
// < = deindent
// % = select whole buffer (in vim = jump to matching bracket)
// * = search pattern in selection
// ( = rotate main selection backward
// ) = rotate main selection forward
// - = trim selections? (alt = merge contiguous sel together)
// @ = convert tabs to spaces
// & = align cursor
// ? = extend to next given regex match (alt = to prev)
//
2020-09-28 18:00:35 +02:00
// in kakoune these are alt-h alt-l / gh gl
// select from curs to begin end / move curs to begin end
// 0 = start of line
// ^ = start of line (first non blank char)
// $ = end of line
//
2020-06-07 17:28:17 +02:00
// z = save selections
// Z = restore selections
// x = select line
// X = extend line
// c = change selected text
// C = copy selection?
// v = view menu (viewport manipulation)
// b = select to previous word start
// B = select to previous WORD start
//
//
//
//
//
//
// = = align?
// + =
// }
//
// gd = goto definition
// gr = goto reference
2020-06-07 17:28:17 +02:00
// }
// #[cfg(feature = "term")]
pub use crossterm::event::{KeyCode, KeyEvent as Key, KeyModifiers as Modifiers};
2020-06-07 17:28:17 +02:00
// TODO: could be trie based
pub type Keymap = HashMap<Vec<Key>, Command>;
pub type Keymaps = HashMap<Mode, Keymap>;
2020-10-01 09:06:42 +02:00
macro_rules! key {
($ch:expr) => {
Key {
code: KeyCode::Char($ch),
modifiers: Modifiers::NONE,
}
};
}
macro_rules! shift {
($ch:expr) => {
Key {
code: KeyCode::Char($ch),
modifiers: Modifiers::SHIFT,
}
};
}
2020-10-05 17:58:16 +02:00
macro_rules! ctrl {
($ch:expr) => {
Key {
code: KeyCode::Char($ch),
modifiers: Modifiers::CONTROL,
}
};
}
2021-01-22 09:13:14 +01:00
macro_rules! alt {
($ch:expr) => {
Key {
code: KeyCode::Char($ch),
modifiers: Modifiers::ALT,
}
};
}
2020-10-09 09:58:43 +02:00
pub fn default() -> Keymaps {
2020-06-07 17:28:17 +02:00
hashmap!(
Mode::Normal =>
2020-10-01 09:06:42 +02:00
// as long as you cast the first item, rust is able to infer the other cases
hashmap!(
2020-10-01 09:06:42 +02:00
vec![key!('h')] => commands::move_char_left as Command,
vec![key!('j')] => commands::move_line_down,
vec![key!('k')] => commands::move_line_up,
vec![key!('0')] => commands::move_line_start,
vec![key!('$')] => commands::move_line_end,
vec![key!('l')] => commands::move_char_right,
vec![shift!('H')] => commands::extend_char_left,
vec![shift!('J')] => commands::extend_line_down,
vec![shift!('K')] => commands::extend_line_up,
vec![shift!('L')] => commands::extend_char_right,
vec![key!('w')] => commands::move_next_word_start,
vec![shift!('W')] => commands::extend_next_word_start,
2020-10-01 09:06:42 +02:00
vec![key!('b')] => commands::move_prev_word_start,
vec![shift!('B')] => commands::extend_prev_word_start,
2020-10-01 09:06:42 +02:00
vec![key!('e')] => commands::move_next_word_end,
// TODO: E
2020-10-04 23:47:37 +02:00
vec![key!('g')] => commands::goto_mode,
2020-10-01 09:06:42 +02:00
vec![key!('i')] => commands::insert_mode,
vec![shift!('I')] => commands::prepend_to_line,
vec![key!('a')] => commands::append_mode,
vec![shift!('A')] => commands::append_to_line,
vec![key!('o')] => commands::open_below,
vec![key!('d')] => commands::delete_selection,
vec![key!('c')] => commands::change_selection,
2021-01-22 09:13:14 +01:00
vec![key!('s')] => commands::select_regex,
vec![alt!('s')] => commands::split_selection_on_newline,
2020-12-14 08:02:07 +01:00
vec![shift!('S')] => commands::split_selection,
2020-10-01 11:44:46 +02:00
vec![key!(';')] => commands::collapse_selection,
2020-10-09 09:58:43 +02:00
// TODO should be alt(;)
2021-02-09 07:39:26 +01:00
vec![alt!(';')] => commands::flip_selections,
vec![key!('%')] => commands::select_all,
2020-10-07 11:31:04 +02:00
vec![key!('x')] => commands::select_line,
vec![key!('u')] => commands::undo,
vec![shift!('U')] => commands::redo,
2020-10-06 09:00:23 +02:00
vec![key!('y')] => commands::yank,
vec![key!('p')] => commands::paste,
2020-10-09 09:58:43 +02:00
vec![key!('>')] => commands::indent,
vec![key!('<')] => commands::unindent,
2020-10-13 18:57:55 +02:00
vec![key!(':')] => commands::command_mode,
vec![Key {
code: KeyCode::Esc,
modifiers: Modifiers::NONE
2020-10-01 09:06:42 +02:00
}] => commands::normal_mode,
2020-10-05 17:18:29 +02:00
vec![Key {
code: KeyCode::PageUp,
modifiers: Modifiers::NONE
}] => commands::page_up,
vec![Key {
code: KeyCode::PageDown,
modifiers: Modifiers::NONE
}] => commands::page_down,
2020-10-05 17:58:16 +02:00
vec![ctrl!('u')] => commands::half_page_up,
vec![ctrl!('d')] => commands::half_page_down,
vec![ctrl!('p')] => commands::file_picker,
2020-12-21 08:23:05 +01:00
vec![ctrl!('b')] => commands::buffer_picker,
vec![Key {
code: KeyCode::Tab,
modifiers: Modifiers::NONE
}] => commands::next_view,
2020-10-01 01:15:42 +02:00
),
Mode::Insert => hashmap!(
2020-10-01 01:15:42 +02:00
vec![Key {
code: KeyCode::Esc,
modifiers: Modifiers::NONE
}] => commands::normal_mode as Command,
vec![Key {
code: KeyCode::Backspace,
modifiers: Modifiers::NONE
}] => commands::insert::delete_char_backward,
2020-10-01 01:15:42 +02:00
vec![Key {
code: KeyCode::Delete,
modifiers: Modifiers::NONE
}] => commands::insert::delete_char_forward,
2020-10-01 01:15:42 +02:00
vec![Key {
code: KeyCode::Enter,
modifiers: Modifiers::NONE
}] => commands::insert::insert_newline,
2020-10-01 21:16:24 +02:00
vec![Key {
code: KeyCode::Tab,
modifiers: Modifiers::NONE
}] => commands::insert::insert_tab,
2020-12-23 08:20:49 +01:00
vec![ctrl!('x')] => commands::completion,
2020-10-04 23:47:37 +02:00
),
Mode::Goto => hashmap!(
2020-10-04 23:47:37 +02:00
vec![Key {
code: KeyCode::Esc,
modifiers: Modifiers::NONE
}] => commands::normal_mode as Command,
vec![key!('g')] => commands::move_file_start as Command,
vec![key!('e')] => commands::move_file_end as Command,
),
2020-06-07 17:31:11 +02:00
)
2020-06-07 17:28:17 +02:00
}