Support escape_next_key

pull/74/head
Takashi Kokubun 3 years ago
parent a311f6fa1e
commit 660084b18b
No known key found for this signature in database
GPG Key ID: 6FFC433B12EE23DD

@ -18,6 +18,8 @@ pub enum Action {
SetMark(bool),
#[serde(deserialize_with = "deserialize_with_mark")]
WithMark(KeyPress),
#[serde(deserialize_with = "deserialize_escape_next_key")]
EscapeNextKey(bool),
}
fn deserialize_remap<'de, D>(deserializer: D) -> Result<HashMap<KeyPress, Vec<Action>>, D::Error>
@ -72,6 +74,19 @@ where
Err(de::Error::custom("not a map with a single \"with_mark\" key"))
}
fn deserialize_escape_next_key<'de, D>(deserializer: D) -> Result<bool, D::Error>
where
D: Deserializer<'de>,
{
let mut action = HashMap::<String, bool>::deserialize(deserializer)?;
if let Some(set) = action.remove("escape_next_key") {
if action.is_empty() {
return Ok(set);
}
}
Err(de::Error::custom("not a map with a single \"escape_next_key\" key"))
}
// Used only for deserializing Vec<Action>
#[derive(Clone, Deserialize)]
#[serde(untagged)]

@ -27,6 +27,7 @@ pub struct EventHandler {
override_remap: Option<HashMap<KeyPress, Vec<Action>>>,
sigaction_set: bool,
mark_set: bool,
escape_next_key: bool,
}
impl EventHandler {
@ -43,6 +44,7 @@ impl EventHandler {
override_remap: None,
sigaction_set: false,
mark_set: false,
escape_next_key: false,
}
}
@ -66,11 +68,15 @@ impl EventHandler {
for (key, value) in key_values.into_iter() {
if MODIFIER_KEYS.contains(&key.code()) {
self.update_modifier(key.code(), value);
} else if let Some(actions) = self.find_keymap(config, &key, value) {
for action in &actions {
self.dispatch_action(action)?;
} else if is_pressed(value) {
if self.escape_next_key {
self.escape_next_key = false
} else if let Some(actions) = self.find_keymap(config, &key) {
for action in &actions {
self.dispatch_action(action)?;
}
continue;
}
continue;
}
self.send_key(&key, value)?;
}
@ -155,11 +161,7 @@ impl EventHandler {
None
}
fn find_keymap(&mut self, config: &Config, key: &Key, value: i32) -> Option<Vec<Action>> {
if !is_pressed(value) {
return None;
}
fn find_keymap(&mut self, config: &Config, key: &Key) -> Option<Vec<Action>> {
let key_press = KeyPress {
key: key.clone(),
shift: self.shift.to_modifier_state(),
@ -200,6 +202,7 @@ impl EventHandler {
Action::Launch(command) => self.run_command(command.clone()),
Action::SetMark(set) => self.mark_set = *set,
Action::WithMark(key_press) => self.send_key_press(&self.with_mark(key_press))?,
Action::EscapeNextKey(escape_next_key) => self.escape_next_key = *escape_next_key,
}
Ok(())
}

Loading…
Cancel
Save