2023-10-06 22:35:27 -07:00
|
|
|
//! # Default Keybindings
|
|
|
|
//!
|
|
|
|
//! The keybindings are set up here. We define some iamb-specific keybindings, but the default Vim
|
|
|
|
//! keys come from [modalkit::env::vim::keybindings].
|
2022-12-29 18:00:59 -08:00
|
|
|
use modalkit::{
|
2024-04-21 18:19:53 -07:00
|
|
|
actions::{InsertTextAction, MacroAction, WindowAction},
|
2022-12-29 18:00:59 -08:00
|
|
|
env::vim::keybindings::{InputStep, VimBindings},
|
|
|
|
env::vim::VimMode,
|
2024-03-09 22:49:40 -08:00
|
|
|
env::CommonKeyClass,
|
2024-02-27 21:21:05 -08:00
|
|
|
key::TerminalKey,
|
|
|
|
keybindings::{EdgeEvent, EdgeRepeat, InputBindings},
|
2024-04-21 18:19:53 -07:00
|
|
|
prelude::*,
|
2022-12-29 18:00:59 -08:00
|
|
|
};
|
|
|
|
|
2023-03-01 18:46:33 -08:00
|
|
|
use crate::base::{IambAction, IambInfo, Keybindings, MATRIX_ID_WORD};
|
2024-03-09 22:49:40 -08:00
|
|
|
use crate::config::{ApplicationSettings, Keys};
|
2023-02-09 23:05:02 -08:00
|
|
|
|
2024-03-09 22:49:40 -08:00
|
|
|
pub type IambStep = InputStep<IambInfo>;
|
|
|
|
|
|
|
|
fn once(key: &TerminalKey) -> (EdgeRepeat, EdgeEvent<TerminalKey, CommonKeyClass>) {
|
|
|
|
(EdgeRepeat::Once, EdgeEvent::Key(*key))
|
|
|
|
}
|
2022-12-29 18:00:59 -08:00
|
|
|
|
2023-10-06 22:35:27 -07:00
|
|
|
/// Initialize the default keybinding state.
|
2022-12-29 18:00:59 -08:00
|
|
|
pub fn setup_keybindings() -> Keybindings {
|
|
|
|
let mut ism = Keybindings::empty();
|
|
|
|
|
|
|
|
let vim = VimBindings::default()
|
|
|
|
.submit_on_enter()
|
2023-03-01 18:46:33 -08:00
|
|
|
.cursor_open(MATRIX_ID_WORD.clone());
|
2022-12-29 18:00:59 -08:00
|
|
|
|
|
|
|
vim.setup(&mut ism);
|
|
|
|
|
2024-03-09 22:49:40 -08:00
|
|
|
let ctrl_w = "<C-W>".parse::<TerminalKey>().unwrap();
|
|
|
|
let ctrl_m = "<C-M>".parse::<TerminalKey>().unwrap();
|
|
|
|
let ctrl_z = "<C-Z>".parse::<TerminalKey>().unwrap();
|
|
|
|
let key_m_lc = "m".parse::<TerminalKey>().unwrap();
|
|
|
|
let key_z_lc = "z".parse::<TerminalKey>().unwrap();
|
2024-04-21 18:19:53 -07:00
|
|
|
let shift_enter = "<S-Enter>".parse::<TerminalKey>().unwrap();
|
2022-12-29 18:00:59 -08:00
|
|
|
|
2024-03-09 22:49:40 -08:00
|
|
|
let cwz = vec![once(&ctrl_w), once(&key_z_lc)];
|
|
|
|
let cwcz = vec![once(&ctrl_w), once(&ctrl_z)];
|
2023-02-09 23:05:02 -08:00
|
|
|
let zoom = IambStep::new()
|
|
|
|
.actions(vec![WindowAction::ZoomToggle.into()])
|
|
|
|
.goto(VimMode::Normal);
|
2022-12-29 18:00:59 -08:00
|
|
|
|
|
|
|
ism.add_mapping(VimMode::Normal, &cwz, &zoom);
|
2023-02-09 23:05:02 -08:00
|
|
|
ism.add_mapping(VimMode::Visual, &cwz, &zoom);
|
2022-12-29 18:00:59 -08:00
|
|
|
ism.add_mapping(VimMode::Normal, &cwcz, &zoom);
|
2023-02-09 23:05:02 -08:00
|
|
|
ism.add_mapping(VimMode::Visual, &cwcz, &zoom);
|
2022-12-29 18:00:59 -08:00
|
|
|
|
2024-03-09 22:49:40 -08:00
|
|
|
let cwm = vec![once(&ctrl_w), once(&key_m_lc)];
|
|
|
|
let cwcm = vec![once(&ctrl_w), once(&ctrl_m)];
|
2023-02-09 23:05:02 -08:00
|
|
|
let stoggle = IambStep::new()
|
|
|
|
.actions(vec![IambAction::ToggleScrollbackFocus.into()])
|
|
|
|
.goto(VimMode::Normal);
|
2022-12-29 18:00:59 -08:00
|
|
|
ism.add_mapping(VimMode::Normal, &cwm, &stoggle);
|
2023-02-09 23:05:02 -08:00
|
|
|
ism.add_mapping(VimMode::Visual, &cwm, &stoggle);
|
2022-12-29 18:00:59 -08:00
|
|
|
ism.add_mapping(VimMode::Normal, &cwcm, &stoggle);
|
2023-02-09 23:05:02 -08:00
|
|
|
ism.add_mapping(VimMode::Visual, &cwcm, &stoggle);
|
2024-04-21 18:19:53 -07:00
|
|
|
|
|
|
|
let shift_enter = vec![once(&shift_enter)];
|
|
|
|
let newline = IambStep::new().actions(vec![InsertTextAction::Type(
|
|
|
|
Char::Single('\n').into(),
|
|
|
|
MoveDir1D::Previous,
|
|
|
|
1.into(),
|
|
|
|
)
|
|
|
|
.into()]);
|
|
|
|
ism.add_mapping(VimMode::Insert, &cwm, &newline);
|
|
|
|
ism.add_mapping(VimMode::Insert, &shift_enter, &newline);
|
|
|
|
|
2024-03-09 22:49:40 -08:00
|
|
|
ism
|
|
|
|
}
|
|
|
|
|
|
|
|
impl InputBindings<TerminalKey, IambStep> for ApplicationSettings {
|
|
|
|
fn setup(&self, bindings: &mut Keybindings) {
|
|
|
|
for (modes, keys) in &self.macros {
|
|
|
|
for (Keys(input, _), Keys(_, run)) in keys {
|
|
|
|
let act = MacroAction::Run(run.clone(), Count::Contextual);
|
|
|
|
let step = IambStep::new().actions(vec![act.into()]);
|
|
|
|
let input = input.iter().map(once).collect::<Vec<_>>();
|
2022-12-29 18:00:59 -08:00
|
|
|
|
2024-03-09 22:49:40 -08:00
|
|
|
for mode in &modes.0 {
|
|
|
|
bindings.add_mapping(*mode, &input, &step);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-12-29 18:00:59 -08:00
|
|
|
}
|