summaryrefslogtreecommitdiff
path: root/src/config/keymap/template.rs
diff options
context:
space:
mode:
authorNathan Reiner <nathan@nathanreiner.xyz>2024-08-03 00:08:47 +0200
committerNathan Reiner <nathan@nathanreiner.xyz>2024-08-03 00:08:47 +0200
commiteafde55afcdf9dc4c17c6c97c1db472fc9ff9957 (patch)
tree838cab985495ead6dc005bd934f9e87c896e4ddd /src/config/keymap/template.rs
parentbb9944d086332ed0b8d6064316225e901c456bd7 (diff)
add keymap for view and global
Diffstat (limited to 'src/config/keymap/template.rs')
-rw-r--r--src/config/keymap/template.rs57
1 files changed, 57 insertions, 0 deletions
diff --git a/src/config/keymap/template.rs b/src/config/keymap/template.rs
new file mode 100644
index 0000000..bebcdc3
--- /dev/null
+++ b/src/config/keymap/template.rs
@@ -0,0 +1,57 @@
+macro_rules! KeyMapSections {
+ ($($name:ident => $key:ident),+ $(,)?) => {
+ $(
+ #[derive(Debug, Default, Clone)]
+ pub struct $name {
+ store: KeyMapStore,
+ }
+
+ impl $name {
+ pub const fn new() -> Self {
+ Self {
+ store: KeyMapStore::new(),
+ }
+ }
+
+ pub fn handle(event: KeyEvent) -> bool {
+ let func = { GlobalConfig::instance_mut().keymap.$key.store.get(event) };
+
+ if let Some(func) = func {
+ let func = func.lock().unwrap();
+ if let Err(_error) = func.run() {
+ // TODO: add error buffer
+ }
+ true
+ } else {
+ false
+ }
+ }
+
+ pub fn map(event: KeyEvent, func: impl Runnable + 'static) {
+ GlobalConfig::instance_mut()
+ .keymap
+ .$key
+ .store
+ .map(event, func)
+ }
+ }
+
+ impl UserData for $name {
+ fn add_methods<'lua, M: mlua::prelude::LuaUserDataMethods<'lua, Self>>(
+ methods: &mut M,
+ ) {
+ methods.add_function("map", |lua, (event, func): (String, Function<'_>)| {
+ let key = lua.create_registry_value(func)?;
+
+ if let Ok(event) = event_from_string(event) {
+ $name::map(event, key);
+ }
+ Ok(())
+ });
+ }
+ }
+ )*
+ };
+}
+
+pub(super) use KeyMapSections;