src/keybindings.rs

72 lines · 2495 bytes

 1use modalkit::{
 2    editing::action::WindowAction,
 3    editing::base::WordStyle,
 4    env::vim::keybindings::{InputStep, VimBindings},
 5    env::vim::VimMode,
 6    input::bindings::{EdgeEvent, EdgeRepeat, InputBindings},
 7    input::key::TerminalKey,
 8};
 9
10use crate::base::{IambAction, IambInfo, Keybindings};
11
12type IambStep = InputStep<IambInfo>;
13
14/// Find the boundaries for a Matrix username, room alias, or room ID.
15///
16/// Technically "[" and "]" should be here since IPv6 addresses are allowed
17/// in the server name, but in practice that should be uncommon, and people
18/// can just use `gf` and friends in Visual mode instead.
19fn is_mxid_char(c: char) -> bool {
20    return c >= 'a' && c <= 'z' ||
21        c >= 'A' && c <= 'Z' ||
22        c >= '0' && c <= '9' ||
23        ":-./@_#!".contains(c);
24}
25
26pub fn setup_keybindings() -> Keybindings {
27    let mut ism = Keybindings::empty();
28
29    let vim = VimBindings::default()
30        .submit_on_enter()
31        .cursor_open(WordStyle::CharSet(is_mxid_char));
32
33    vim.setup(&mut ism);
34
35    let ctrl_w = EdgeEvent::Key("<C-W>".parse::<TerminalKey>().unwrap());
36    let ctrl_m = EdgeEvent::Key("<C-M>".parse::<TerminalKey>().unwrap());
37    let ctrl_z = EdgeEvent::Key("<C-Z>".parse::<TerminalKey>().unwrap());
38    let key_m_lc = EdgeEvent::Key("m".parse::<TerminalKey>().unwrap());
39    let key_z_lc = EdgeEvent::Key("z".parse::<TerminalKey>().unwrap());
40
41    let cwz = vec![
42        (EdgeRepeat::Once, ctrl_w.clone()),
43        (EdgeRepeat::Once, key_z_lc),
44    ];
45    let cwcz = vec![
46        (EdgeRepeat::Once, ctrl_w.clone()),
47        (EdgeRepeat::Once, ctrl_z),
48    ];
49    let zoom = IambStep::new()
50        .actions(vec![WindowAction::ZoomToggle.into()])
51        .goto(VimMode::Normal);
52
53    ism.add_mapping(VimMode::Normal, &cwz, &zoom);
54    ism.add_mapping(VimMode::Visual, &cwz, &zoom);
55    ism.add_mapping(VimMode::Normal, &cwcz, &zoom);
56    ism.add_mapping(VimMode::Visual, &cwcz, &zoom);
57
58    let cwm = vec![
59        (EdgeRepeat::Once, ctrl_w.clone()),
60        (EdgeRepeat::Once, key_m_lc),
61    ];
62    let cwcm = vec![(EdgeRepeat::Once, ctrl_w), (EdgeRepeat::Once, ctrl_m)];
63    let stoggle = IambStep::new()
64        .actions(vec![IambAction::ToggleScrollbackFocus.into()])
65        .goto(VimMode::Normal);
66    ism.add_mapping(VimMode::Normal, &cwm, &stoggle);
67    ism.add_mapping(VimMode::Visual, &cwm, &stoggle);
68    ism.add_mapping(VimMode::Normal, &cwcm, &stoggle);
69    ism.add_mapping(VimMode::Visual, &cwcm, &stoggle);
70
71    return ism;
72}