Debian Codegraph @

helix-term/src/keymap/macros.rs

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 #[macro_export]
2 macro_rules! key {
3 ($key:ident) => {
4 ::helix_view::input::KeyEvent {
5 code: ::helix_view::keyboard::KeyCode::$key,
6 modifiers: ::helix_view::keyboard::KeyModifiers::NONE,
7 }
8 };
9 ($($ch:tt)*) => {
10 ::helix_view::input::KeyEvent {
11 code: ::helix_view::keyboard::KeyCode::Char($($ch)*),
12 modifiers: ::helix_view::keyboard::KeyModifiers::NONE,
13 }
14 };
15 }
16
17 #[macro_export]
18 macro_rules! shift {
19 ($key:ident) => {
20 ::helix_view::input::KeyEvent {
21 code: ::helix_view::keyboard::KeyCode::$key,
22 modifiers: ::helix_view::keyboard::KeyModifiers::SHIFT,
23 }
24 };
25 ($($ch:tt)*) => {
26 ::helix_view::input::KeyEvent {
27 code: ::helix_view::keyboard::KeyCode::Char($($ch)*),
28 modifiers: ::helix_view::keyboard::KeyModifiers::SHIFT,
29 }
30 };
31 }
32
33 #[macro_export]
34 macro_rules! ctrl {
35 ($key:ident) => {
36 ::helix_view::input::KeyEvent {
37 code: ::helix_view::keyboard::KeyCode::$key,
38 modifiers: ::helix_view::keyboard::KeyModifiers::CONTROL,
39 }
40 };
41 ($($ch:tt)*) => {
42 ::helix_view::input::KeyEvent {
43 code: ::helix_view::keyboard::KeyCode::Char($($ch)*),
44 modifiers: ::helix_view::keyboard::KeyModifiers::CONTROL,
45 }
46 };
47 }
48
49 #[macro_export]
50 macro_rules! alt {
51 ($key:ident) => {
52 ::helix_view::input::KeyEvent {
53 code: ::helix_view::keyboard::KeyCode::$key,
54 modifiers: ::helix_view::keyboard::KeyModifiers::ALT,
55 }
56 };
57 ($($ch:tt)*) => {
58 ::helix_view::input::KeyEvent {
59 code: ::helix_view::keyboard::KeyCode::Char($($ch)*),
60 modifiers: ::helix_view::keyboard::KeyModifiers::ALT,
61 }
62 };
63 }
64
65 /// Macro for defining a `KeyTrie`. Example:
66 ///
67 /// ```
68 /// # use helix_core::hashmap;
69 /// # use helix_term::keymap;
70 /// let normal_mode = keymap!({ "Normal mode"
71 /// "i" => insert_mode,
72 /// "g" => { "Goto"
73 /// "g" => goto_file_start,
74 /// "e" => goto_file_end,
75 /// },
76 /// "j" | "down" => move_line_down,
77 /// });
78 /// let keymap = normal_mode;
79 /// ```
80 #[macro_export]
81 macro_rules! keymap {
82 (@trie $cmd:ident) => {
83 $crate::keymap::KeyTrie::MappableCommand($crate::commands::MappableCommand::$cmd)
84 };
85
86 (@trie
87 { $label:literal $(sticky=$sticky:literal)? $($($key:literal)|+ => $value:tt,)+ }
88 ) => {
89 keymap!({ $label $(sticky=$sticky)? $($($key)|+ => $value,)+ })
90 };
91
92 (@trie [$($cmd:ident),* $(,)?]) => {
93 $crate::keymap::KeyTrie::Sequence(vec![$($crate::commands::MappableCommand::$cmd),*])
94 };
95
96 (
97 { $label:literal $(sticky=$sticky:literal)? $($($key:literal)|+ => $value:tt,)+ }
98 ) => {
99 // modified from the hashmap! macro
100 {
101 let _cap = hashmap!(@count $($($key),+),*);
102 let mut _map = ::indexmap::IndexMap::with_capacity(_cap);
103 $(
104 $(
105 let _key = $key.parse::<::helix_view::input::KeyEvent>().unwrap();
106 let _duplicate = _map.insert(
107 _key,
108 keymap!(@trie $value)
109 );
110 assert!(_duplicate.is_none(), "Duplicate key found: {:?}", _duplicate.unwrap());
111 )+
112 )*
113 let mut _node = $crate::keymap::KeyTrieNode::new($label, _map);
114 $( _node.is_sticky = $sticky; )?
115 $crate::keymap::KeyTrie::Node(_node)
116 }
117 };
118 }
119
120 pub use alt;
121 pub use ctrl;
122 pub use key;
123 pub use keymap;
124 pub use shift;