From 671f297630d360cfa5d55dc69d4af98521a4b152 Mon Sep 17 00:00:00 2001 From: bb Date: Sun, 9 Oct 2022 17:33:16 +0200 Subject: [PATCH] Make stickiness configurable --- book/src/remapping.md | 2 ++ helix-term/src/keymap.rs | 22 ++++++++++++++++++---- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/book/src/remapping.md b/book/src/remapping.md index bd4ac7f81c86..37fcba2c7140 100644 --- a/book/src/remapping.md +++ b/book/src/remapping.md @@ -50,5 +50,7 @@ Control, Shift and Alt modifiers are encoded respectively with the prefixes Keys can be disabled by binding them to the `no_op` command. +Making a mode "sticky" can be achieved by adding `sticky = true` to the mapping. + Commands can be found at [Keymap](https://docs.helix-editor.com/keymap.html) Commands. > Commands can also be found 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`. diff --git a/helix-term/src/keymap.rs b/helix-term/src/keymap.rs index 088b3b6d2083..dbf8e3472dd9 100644 --- a/helix-term/src/keymap.rs +++ b/helix-term/src/keymap.rs @@ -200,11 +200,25 @@ impl<'de> serde::de::Visitor<'de> for KeyTrieVisitor { { let mut mapping = HashMap::new(); let mut order = Vec::new(); - while let Some((key, value)) = map.next_entry::()? { - mapping.insert(key, value); - order.push(key); + let mut is_sticky = false; + while let Some(toml_key) = map.next_key::()? { + let maybe_key_event = toml_key.clone().try_into::(); + match (maybe_key_event, toml_key.as_str()) { + (Ok(key_event), _) => { + mapping.insert(key_event, map.next_value::()?); + order.push(key_event); + } + (Err(_), Some("sticky")) => { + is_sticky = map.next_value::()?; + } + (Err(err), _) => { + return Err(serde::de::Error::custom(err.to_string())); + } + } } - Ok(KeyTrie::Node(KeyTrieNode::new("", mapping, order))) + let mut trie_node = KeyTrieNode::new("", mapping, order); + trie_node.is_sticky = is_sticky; + Ok(KeyTrie::Node(trie_node)) } }