@@ -328,26 +328,15 @@ impl<'a> KeymapResult<'a> {
330#[derive(Debug, Clone, PartialEq, Deserialize)]
330#[derive(Debug, Clone, PartialEq, Deserialize)]
331+#[serde(transparent)]
331pub struct Keymap {
332pub struct Keymap {
332 /// Always a Node
333 /// Always a Node
334 root: KeyTrie,
334 root: KeyTrie,
335− /// Stores pending keys waiting for the next key. This is relative to a
336− /// sticky node if one is in use.
338− state: Vec<KeyEvent>,
339− /// Stores the sticky node if one is activated.
341− sticky: Option<KeyTrieNode>,
344impl Keymap {
337impl Keymap {
345 pub fn new(root: KeyTrie) -> Self {
338 pub fn new(root: KeyTrie) -> Self {
346− Keymap {
339+ Keymap { root }
353 pub fn reverse_map(&self) -> HashMap<String, Vec<Vec<KeyEvent>>> {
342 pub fn reverse_map(&self) -> HashMap<String, Vec<Vec<KeyEvent>>> {
@@ -387,19 +376,65 @@ impl Keymap {
387 &self.root
376 &self.root
379+ pub fn merge(&mut self, other: Self) {
380+ self.root.merge_nodes(other.root);
384+impl Deref for Keymap {
390− pub fn sticky(&self) -> Option<&KeyTrieNode> {
385+ type Target = KeyTrieNode;
391− self.sticky.as_ref()
386+
387+ fn deref(&self) -> &Self::Target {
388+ self.root.node().unwrap()
392+impl Default for Keymap {
393+ fn default() -> Self {
394+ Self::new(KeyTrie::Node(KeyTrieNode::default()))
398+#[derive(Debug, Clone, PartialEq, Deserialize)]
401+ pub map: HashMap<Mode, Keymap>,
403+ /// Stores pending keys waiting for the next key. This is relative to a
404+ /// sticky node if one is in use.
406+ state: Vec<KeyEvent>,
408+ /// Stores the sticky node if one is activated.
410+ pub sticky: Option<KeyTrieNode>,
414+ pub fn new(map: HashMap<Mode, Keymap>) -> Self {
394− /// Returns list of keys waiting to be disambiguated.
422+ /// Returns list of keys waiting to be disambiguated in current mode.
395 pub fn pending(&self) -> &[KeyEvent] {
423 pub fn pending(&self) -> &[KeyEvent] {
396 &self.state
424 &self.state
427+ pub fn sticky(&self) -> Option<&KeyTrieNode> {
428+ self.sticky.as_ref()
399 /// Lookup `key` in the keymap to try and find a command to execute. Escape
431 /// Lookup `key` in the keymap to try and find a command to execute. Escape
400 /// key cancels pending keystrokes. If there are no pending keystrokes but a
432 /// key cancels pending keystrokes. If there are no pending keystrokes but a
401 /// sticky node is in use, it will be cleared.
433 /// sticky node is in use, it will be cleared.
402− pub fn get(&mut self, key: KeyEvent) -> KeymapResult {
434+ pub fn get(&mut self, mode: Mode, key: KeyEvent) -> KeymapResult {
435+ // TODO: remove the sticky part and look up manually
436+ let keymap = &self.map[&mode];
403 if key!(Esc) == key {
438 if key!(Esc) == key {
404 if !self.state.is_empty() {
439 if !self.state.is_empty() {
405 return KeymapResult::new(
440 return KeymapResult::new(
@@ -414,7 +449,7 @@ impl Keymap {
414 let first = self.state.get(0).unwrap_or(&key);
449 let first = self.state.get(0).unwrap_or(&key);
415 let trie_node = match self.sticky {
450 let trie_node = match self.sticky {
416 Some(ref trie) => Cow::Owned(KeyTrie::Node(trie.clone())),
451 Some(ref trie) => Cow::Owned(KeyTrie::Node(trie.clone())),
417− None => Cow::Borrowed(&self.root),
452+ None => Cow::Borrowed(&keymap.root),
420 let trie = match trie_node.search(&[*first]) {
455 let trie = match trie_node.search(&[*first]) {
@@ -457,55 +492,6 @@ impl Keymap {
461− pub fn merge(&mut self, other: Self) {
462− self.root.merge_nodes(other.root);
466−impl Deref for Keymap {
467− type Target = KeyTrieNode;
469− fn deref(&self) -> &Self::Target {
470− self.root.node().unwrap()
474−impl Default for Keymap {
475− fn default() -> Self {
476− Self::new(KeyTrie::Node(KeyTrieNode::default()))
480−#[derive(Debug, Clone, PartialEq, Deserialize)]
481−#[serde(transparent)]
482−pub struct Keymaps(pub HashMap<Mode, Keymap>);
485− /// Returns list of keys waiting to be disambiguated in current mode.
486− pub fn pending(&self) -> &[KeyEvent] {
489− .find_map(|keymap| match keymap.pending().is_empty() {
491− false => Some(keymap.pending()),
493− .unwrap_or_default()
497−impl Deref for Keymaps {
498− type Target = HashMap<Mode, Keymap>;
500− fn deref(&self) -> &Self::Target {
505−impl DerefMut for Keymaps {
506− fn deref_mut(&mut self) -> &mut Self::Target {
511impl Default for Keymaps {
497impl Default for Keymaps {
@@ -856,7 +842,7 @@ impl Default for Keymaps {
856 "C-x" => completion,
842 "C-x" => completion,
857 "C-r" => insert_register,
843 "C-r" => insert_register,
859− Self(hashmap!(
845+ Self::new(hashmap!(
860 Mode::Normal => Keymap::new(normal),
846 Mode::Normal => Keymap::new(normal),
861 Mode::Select => Keymap::new(select),
847 Mode::Select => Keymap::new(select),
862 Mode::Insert => Keymap::new(insert),
848 Mode::Insert => Keymap::new(insert),
@@ -867,8 +853,8 @@ impl Default for Keymaps {
867/// Merge default config keys with user overwritten keys for custom user config.
853/// Merge default config keys with user overwritten keys for custom user config.
868pub fn merge_keys(mut config: Config) -> Config {
854pub fn merge_keys(mut config: Config) -> Config {
869 let mut delta = std::mem::take(&mut config.keys);
855 let mut delta = std::mem::take(&mut config.keys);
870− for (mode, keys) in &mut *config.keys {
856+ for (mode, keys) in &mut config.keys.map {
871− keys.merge(delta.remove(mode).unwrap_or_default())
857+ keys.merge(delta.map.remove(mode).unwrap_or_default())
@@ -895,7 +881,7 @@ mod tests {
896 fn merge_partial_keys() {
882 fn merge_partial_keys() {
897 let config = Config {
883 let config = Config {
898− keys: Keymaps(hashmap! {
884+ keys: Keymaps::new(hashmap! {
899 Mode::Normal => Keymap::new(
885 Mode::Normal => Keymap::new(
900 keymap!({ "Normal mode"
886 keymap!({ "Normal mode"
901 "i" => normal_mode,
887 "i" => normal_mode,
@@ -913,23 +899,25 @@ mod tests {
913 let mut merged_config = merge_keys(config.clone());
899 let mut merged_config = merge_keys(config.clone());
914 assert_ne!(config, merged_config);
900 assert_ne!(config, merged_config);
916− let keymap = merged_config.keys.0.get_mut(&Mode::Normal).unwrap();
902+ let keymap = &mut merged_config.keys;
917 assert_eq!(
903 assert_eq!(
918− keymap.get(key!('i')).kind,
904+ keymap.get(Mode::Normal, key!('i')).kind,
919 KeymapResultKind::Matched(MappableCommand::normal_mode),
905 KeymapResultKind::Matched(MappableCommand::normal_mode),
920 "Leaf should replace leaf"
906 "Leaf should replace leaf"
922 assert_eq!(
908 assert_eq!(
923− keymap.get(key!('无')).kind,
909+ keymap.get(Mode::Normal, key!('无')).kind,
924 KeymapResultKind::Matched(MappableCommand::insert_mode),
910 KeymapResultKind::Matched(MappableCommand::insert_mode),
925 "New leaf should be present in merged keymap"
911 "New leaf should be present in merged keymap"
927 // Assumes that z is a node in the default keymap
913 // Assumes that z is a node in the default keymap
928 assert_eq!(
914 assert_eq!(
929− keymap.get(key!('z')).kind,
915+ keymap.get(Mode::Normal, key!('z')).kind,
930 KeymapResultKind::Matched(MappableCommand::jump_backward),
916 KeymapResultKind::Matched(MappableCommand::jump_backward),
931 "Leaf should replace node"
917 "Leaf should replace node"
920+ let keymap = merged_config.keys.map.get_mut(&Mode::Normal).unwrap();
933 // Assumes that `g` is a node in default keymap
921 // Assumes that `g` is a node in default keymap
934 assert_eq!(
922 assert_eq!(
935 keymap.root().search(&[key!('g'), key!('$')]).unwrap(),
923 keymap.root().search(&[key!('g'), key!('$')]).unwrap(),
@@ -949,14 +937,14 @@ mod tests {
949 "Old leaves in subnode should be present in merged node"
937 "Old leaves in subnode should be present in merged node"
952− assert!(merged_config.keys.0.get(&Mode::Normal).unwrap().len() > 1);
940+ assert!(merged_config.keys.map.get(&Mode::Normal).unwrap().len() > 1);
953− assert!(merged_config.keys.0.get(&Mode::Insert).unwrap().len() > 0);
941+ assert!(merged_config.keys.map.get(&Mode::Insert).unwrap().len() > 0);
957 fn order_should_be_set() {
945 fn order_should_be_set() {
958 let config = Config {
946 let config = Config {
959− keys: Keymaps(hashmap! {
947+ keys: Keymaps::new(hashmap! {
960 Mode::Normal => Keymap::new(
948 Mode::Normal => Keymap::new(
961 keymap!({ "Normal mode"
949 keymap!({ "Normal mode"
962 "space" => { ""
950 "space" => { ""
@@ -972,7 +960,7 @@ mod tests {
973 let mut merged_config = merge_keys(config.clone());
961 let mut merged_config = merge_keys(config.clone());
974 assert_ne!(config, merged_config);
962 assert_ne!(config, merged_config);
975− let keymap = merged_config.keys.0.get_mut(&Mode::Normal).unwrap();
963+ let keymap = merged_config.keys.map.get_mut(&Mode::Normal).unwrap();
976 // Make sure mapping works
964 // Make sure mapping works
977 assert_eq!(
965 assert_eq!(
@@ -990,7 +978,7 @@ mod tests {
991 fn aliased_modes_are_same_in_default_keymap() {
979 fn aliased_modes_are_same_in_default_keymap() {
992 let keymaps = Keymaps::default();
980 let keymaps = Keymaps::default();
993− let root = keymaps.get(&Mode::Normal).unwrap().root();
981+ let root = keymaps.map.get(&Mode::Normal).unwrap().root();
994 assert_eq!(
982 assert_eq!(
995 root.search(&[key!(' '), key!('w')]).unwrap(),
983 root.search(&[key!(' '), key!('w')]).unwrap(),
996 root.search(&["C-w".parse::<KeyEvent>().unwrap()]).unwrap(),
984 root.search(&["C-w".parse::<KeyEvent>().unwrap()]).unwrap(),