summaryrefslogtreecommitdiff
path: root/src/config
diff options
context:
space:
mode:
authorNathan Reiner <nathan@nathanreiner.xyz>2024-08-03 11:06:14 +0200
committerNathan Reiner <nathan@nathanreiner.xyz>2024-08-03 11:06:14 +0200
commit90daf295c7dd5fedc3219b8947fac110f05cc522 (patch)
treea8cc7dba65f1c8a6b7ed5e528e65bc86885d3c22 /src/config
parentcd907dd59a48c2aa9d602aa3fb2f24563994420e (diff)
add editor keymap
Diffstat (limited to 'src/config')
-rw-r--r--src/config/keymap/keymap_store.rs14
-rw-r--r--src/config/keymap/mod.rs54
-rw-r--r--src/config/keymap/template.rs10
3 files changed, 42 insertions, 36 deletions
diff --git a/src/config/keymap/keymap_store.rs b/src/config/keymap/keymap_store.rs
index c782219..233ac52 100644
--- a/src/config/keymap/keymap_store.rs
+++ b/src/config/keymap/keymap_store.rs
@@ -48,17 +48,16 @@ pub trait Runnable
where
Self: Send,
{
- fn run(&self) -> Result<()>;
+ fn run(&self) -> Result<bool>;
}
impl<T> Runnable for T
where
- T: Fn(),
+ T: Fn() -> bool,
Self: Send,
{
- fn run(&self) -> Result<()> {
- self();
- Ok(())
+ fn run(&self) -> Result<bool> {
+ Ok(self())
}
}
@@ -66,11 +65,10 @@ impl Runnable for RegistryKey
where
Self: Send,
{
- fn run(&self) -> Result<()> {
+ fn run(&self) -> Result<bool> {
let lua = lua::get();
let func: Function = lua.registry_value(self)?;
- func.call::<(), ()>(())?;
- Ok(())
+ Ok(func.call::<(), bool>(()).unwrap_or(false))
}
}
diff --git a/src/config/keymap/mod.rs b/src/config/keymap/mod.rs
index e9995f2..dc97ccc 100644
--- a/src/config/keymap/mod.rs
+++ b/src/config/keymap/mod.rs
@@ -9,33 +9,41 @@ use self::{
use super::{GlobalConfig, DUMMY_CONFIG};
use template::KeyMapSections;
-KeyMapSections!(
- ViewKeyMap => view,
- GlobalKeyMap => global,
-);
-
-#[derive(Debug, Default, Clone)]
-pub struct KeyMap {
- pub view: ViewKeyMap,
- pub global: GlobalKeyMap,
-}
-
pub mod event_from_string;
pub mod keymap_store;
pub mod template;
-impl KeyMap {
- pub const fn new() -> Self {
- Self {
- view: ViewKeyMap::new(),
- global: GlobalKeyMap::new(),
+macro_rules! KeyMap {
+ ($($name:ident => $key:ident),+ $(,)?) => {
+ KeyMapSections!($($name => $key,)*);
+
+ #[derive(Debug, Default, Clone)]
+ pub struct KeyMap {
+ $($key: $name,)*
}
- }
-}
-impl UserData for KeyMap {
- fn add_fields<'lua, F: mlua::prelude::LuaUserDataFields<'lua, Self>>(fields: &mut F) {
- fields.add_field_function_get("view", |_, _| Ok(DUMMY_CONFIG.keymap.view.clone()));
- fields.add_field_function_get("global", |_, _| Ok(DUMMY_CONFIG.keymap.global.clone()));
- }
+ impl KeyMap {
+ pub const fn new() -> Self {
+ Self {
+ $($key: $name::new(),)*
+ }
+ }
+ }
+
+ impl UserData for KeyMap {
+ fn add_fields<'lua, F: mlua::prelude::LuaUserDataFields<'lua, Self>>(fields: &mut F) {
+ $(
+ fields.add_field_function_get(stringify!($key), |_, _|
+ Ok(DUMMY_CONFIG.keymap.$key.clone()));
+ )*
+ }
+ }
+ };
}
+
+KeyMap!(
+ ViewKeyMap => view,
+ GlobalKeyMap => global,
+ EditorKeyMap => editor,
+);
+
diff --git a/src/config/keymap/template.rs b/src/config/keymap/template.rs
index bebcdc3..c5f21dc 100644
--- a/src/config/keymap/template.rs
+++ b/src/config/keymap/template.rs
@@ -13,17 +13,17 @@ macro_rules! KeyMapSections {
}
}
- pub fn handle(event: KeyEvent) -> bool {
+ pub fn handle(event: KeyEvent) -> Option<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
+ match func.run() {
+ Ok(value) => Some(value),
+ _ => None
}
- true
} else {
- false
+ None
}
}