macros.rs (4654B)
1 #[macro_export] 2 macro_rules! hashset { 3 ($( $val: expr ),*) => {{ 4 let mut set = ::std::collections::HashSet::new(); 5 $( set.insert($val); )* 6 set 7 }} 8 } 9 10 #[macro_export] 11 macro_rules! call( 12 ($($method:tt)+) => { 13 |arg| $($method)+(arg) 14 }; 15 ); 16 17 #[macro_export] 18 macro_rules! do_internal( 19 ($func:ident) => { 20 Box::new(|model: &mut $crate::model::Model<'_>| { 21 drop(model.$func()); 22 }) as $crate::binding::KeyAction 23 }; 24 25 ($func:ident, $($arg:expr),+) => { 26 Box::new(move |model: &mut $crate::model::Model<'_>| { 27 drop(model.$func($($arg),+)); 28 }) as $crate::binding::KeyAction 29 }; 30 ); 31 32 #[macro_export] 33 macro_rules! do_internal_block( 34 ($model:ident, $body:block) => { 35 Box::new(|$model: &mut $crate::model::Model<'_>| { 36 $body 37 }) as $crate::binding::KeyAction 38 }; 39 ); 40 41 #[macro_export] 42 macro_rules! do_nothing( 43 () => { 44 Box::new(|_: &mut $crate::model::Model<'_>, _| {}) as $crate::binding::MouseAction 45 }; 46 ); 47 48 #[macro_export] 49 macro_rules! do_internal_mouse( 50 ($func:ident) => { 51 Box::new(|model: &mut $crate::model::Model<'_>, _| { 52 drop(model.$func()); 53 }) as $crate::binding::MouseAction 54 }; 55 56 ($func:ident, $($arg:expr),+) => { 57 Box::new(|model: &mut $crate::model::Model<'_>, _| { 58 drop(model.$func($($arg),+)); 59 }) as $crate::binding::MouseAction 60 }; 61 ); 62 63 #[macro_export] 64 macro_rules! do_internal_mouse_block( 65 ($model:ident, $window:ident, $body:block) => { 66 Box::new(|$model: &mut $crate::model::Model<'_>, $window: Option<winsys::window::Window>| { 67 $body 68 }) as $crate::binding::MouseAction 69 }; 70 ); 71 72 #[macro_export] 73 macro_rules! spawn_external( 74 ($cmd:expr) => { 75 { 76 Box::new(move |_: &mut $crate::model::Model<'_>| { 77 $crate::util::Util::spawn($cmd); 78 }) as $crate::binding::KeyAction 79 } 80 }; 81 ); 82 83 #[macro_export] 84 macro_rules! spawn_from_shell( 85 ($cmd:expr) => { 86 { 87 Box::new(move |_: &mut $crate::model::Model<'_>| { 88 $crate::util::Util::spawn_shell($cmd); 89 }) as $crate::binding::KeyAction 90 } 91 }; 92 ); 93 94 #[macro_export] 95 macro_rules! build_key_bindings( 96 { @start $key_bindings:expr, $keycodes:expr, 97 $( $binding:expr ),+ => $action:expr, 98 $($tail:tt)* 99 } => { 100 $( 101 match $crate::util::Util::parse_key_binding($binding, &$keycodes) { 102 None => panic!("could not parse key binding: {}", $binding), 103 Some(keycode) => $key_bindings.insert(keycode, $action), 104 }; 105 )+ 106 build_key_bindings!(@start $key_bindings, $keycodes, $($tail)*); 107 }; 108 109 { @start $key_bindings:expr, $keycodes:expr, 110 $($tail:tt)* 111 } => { 112 $(compile_error!( 113 stringify!(incorrect syntax in build_key_bindings: $tail) 114 );)* 115 }; 116 117 { $($tokens:tt)+ } => { 118 { 119 let mut key_bindings = std::collections::HashMap::new(); 120 let keycodes = $crate::util::Util::system_keycodes(); 121 build_key_bindings!(@start key_bindings, keycodes, $($tokens)+); 122 key_bindings 123 } 124 }; 125 ); 126 127 #[macro_export] 128 macro_rules! build_mouse_bindings( 129 { @start $mouse_bindings:expr, 130 $( ( $kind:ident, $target:ident, $focus:expr ) : $binding:expr ),+ => $action:expr, 131 $($tail:tt)* 132 } => { 133 $( 134 match $crate::util::Util::parse_mouse_binding($binding) { 135 None => panic!("could not parse mouse binding: {}", $binding), 136 Some(shortcut) => $mouse_bindings.insert( 137 ( 138 winsys::input::MouseEventKey { 139 kind: winsys::input::MouseEventKind::$kind, 140 target: winsys::input::EventTarget::$target, 141 }, 142 shortcut 143 ), 144 ( 145 $action, 146 $focus 147 ) 148 ), 149 }; 150 )+ 151 build_mouse_bindings!(@start $mouse_bindings, $($tail)*); 152 }; 153 154 { @start $mouse_bindings:expr, 155 $($tail:tt)* 156 } => { 157 $(compile_error!( 158 stringify!(incorrect syntax in build_mouse_bindings: $tail) 159 );)* 160 }; 161 162 { $($tokens:tt)+ } => { 163 { 164 let mut mouse_bindings = std::collections::HashMap::new(); 165 build_mouse_bindings!(@start mouse_bindings, $($tokens)+); 166 mouse_bindings 167 } 168 }; 169 );