Skip to content

Commit 20b7d8e

Browse files
authored
Add unbind_default_keys config option (#1)
* Add `unbind_default_keys` config option Based on helix-editor#2733 * Fix lint issues
1 parent 9c56afe commit 20b7d8e

File tree

3 files changed

+39
-10
lines changed

3 files changed

+39
-10
lines changed

book/src/remapping.md

+18
Original file line numberDiff line numberDiff line change
@@ -75,5 +75,23 @@ Ctrl, Shift and Alt modifiers are encoded respectively with the prefixes
7575

7676
Keys can be disabled by binding them to the `no_op` command.
7777

78+
To remove all default bindings, `unbind-default-keys = true` can be added to the top level configuration.
79+
80+
```toml
81+
unbind_default_keys = true
82+
83+
# Only these normal mode bindings will be used
84+
[keys.normal]
85+
n = "normal_mode"
86+
t = "goto_definition"
87+
88+
# remember to add bindings to return to normal mode
89+
[keys.select]
90+
esc = "normal_mode"
91+
92+
[keys.insert]
93+
esc = "normal_mode"
94+
```
95+
7896
A list of commands is available in the [Keymap](https://docs.helix-editor.com/keymap.html) documentation
7997
and in the source code at [`helix-term/src/commands.rs`](https://github.com/helix-editor/helix/blob/master/helix-term/src/commands.rs) at the invocation of `static_commands!` macro and the `TypableCommandList`.

helix-term/src/config.rs

+12-2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ pub struct Config {
2020
#[serde(deny_unknown_fields)]
2121
pub struct ConfigRaw {
2222
pub theme: Option<String>,
23+
#[serde(default)]
24+
pub unbind_default_keys: bool,
2325
pub keys: Option<HashMap<Mode, KeyTrie>>,
2426
pub editor: Option<toml::Value>,
2527
}
@@ -66,7 +68,11 @@ impl Config {
6668
local.and_then(|file| toml::from_str(&file).map_err(ConfigLoadError::BadConfig));
6769
let res = match (global_config, local_config) {
6870
(Ok(global), Ok(local)) => {
69-
let mut keys = keymap::default();
71+
let mut keys;
72+
match local.unbind_default_keys {
73+
true => keys = HashMap::default(),
74+
false => keys = keymap::default(),
75+
}
7076
if let Some(global_keys) = global.keys {
7177
merge_keys(&mut keys, global_keys)
7278
}
@@ -96,7 +102,11 @@ impl Config {
96102
return Err(ConfigLoadError::BadConfig(err))
97103
}
98104
(Ok(config), Err(_)) | (Err(_), Ok(config)) => {
99-
let mut keys = keymap::default();
105+
let mut keys;
106+
match config.unbind_default_keys {
107+
true => keys = HashMap::default(),
108+
false => keys = keymap::default(),
109+
}
100110
if let Some(keymap) = config.keys {
101111
merge_keys(&mut keys, keymap);
102112
}

helix-term/src/keymap.rs

+9-8
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,10 @@ impl Keymaps {
309309
pub fn get(&mut self, mode: Mode, key: KeyEvent) -> KeymapResult {
310310
// TODO: remove the sticky part and look up manually
311311
let keymaps = &*self.map();
312-
let keymap = &keymaps[&mode];
312+
let keymap = match keymaps.get(&mode) {
313+
Some(keymap) => keymap,
314+
None => return KeymapResult::NotFound,
315+
};
313316

314317
if key!(Esc) == key {
315318
if !self.state.is_empty() {
@@ -364,14 +367,12 @@ impl Default for Keymaps {
364367
}
365368
}
366369

367-
/// Merge default config keys with user overwritten keys for custom user config.
370+
/// Merge existing config keys with user overwritten keys.
368371
pub fn merge_keys(dst: &mut HashMap<Mode, KeyTrie>, mut delta: HashMap<Mode, KeyTrie>) {
369-
for (mode, keys) in dst {
370-
keys.merge_nodes(
371-
delta
372-
.remove(mode)
373-
.unwrap_or_else(|| KeyTrie::Node(KeyTrieNode::default())),
374-
)
372+
for (mode, keys) in delta.drain() {
373+
dst.entry(mode)
374+
.or_insert(KeyTrie::Node(KeyTrieNode::default()))
375+
.merge_nodes(keys)
375376
}
376377
}
377378

0 commit comments

Comments
 (0)