Skip to content

Commit

Permalink
make it possible to unbind keys (#883)
Browse files Browse the repository at this point in the history
To remove a binding (without binding it to another verb), define a verb
without any execution, for example

    {
        key: alt-i
    }

Fix #632
  • Loading branch information
Canop authored May 31, 2024
1 parent ae9991f commit 7cbd2d8
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 25 deletions.
3 changes: 1 addition & 2 deletions src/app/panel_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ pub trait PanelState {
.unwrap_or(internal_exec.bang);
Ok(match internal_exec.internal {
Internal::apply_flags => {
info!("applying flags input_invocation: {:#?}", input_invocation);
debug!("applying flags input_invocation: {:#?}", input_invocation);
let flags = input_invocation.and_then(|inv| inv.args.as_ref());
if let Some(flags) = flags {
self.with_new_options(
Expand Down Expand Up @@ -1040,7 +1040,6 @@ pub trait PanelState {
)
} else {
let sel_info = self.sel_info(app_state);
info!("invocation: {:#?}", invocation);
match cc.app.con.verb_store.search_sel_info(
&invocation.name,
sel_info,
Expand Down
6 changes: 2 additions & 4 deletions src/verb/verb_invocation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,8 @@ impl From<&str> for VerbInvocation {
continue;
}
if name.is_empty() {
if c.is_alphabetic() {
name.push(c);
} else {
name.push(c);
name.push(c);
if !c.is_alphabetic() {
name_is_special = true;
}
continue;
Expand Down
67 changes: 48 additions & 19 deletions src/verb/verb_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use {
/// - if only one verb name starts with the input
pub struct VerbStore {
verbs: Vec<Verb>,
unbound_keys: Vec<KeyCombination>,
}

#[derive(Debug, Clone, PartialEq)]
Expand All @@ -36,7 +37,7 @@ pub enum PrefixSearchResult<'v, T> {

impl VerbStore {
pub fn new(conf: &mut Conf) -> Result<Self, ConfError> {
let mut store = Self { verbs: Vec::new() };
let mut store = Self { verbs: Vec::new(), unbound_keys: Vec::new() };
for vc in &conf.verbs {
if let Err(e) = store.add_from_conf(vc) {
eprintln!("Invalid verb configuration: {}", e);
Expand All @@ -51,6 +52,9 @@ impl VerbStore {
}
}
store.add_builtin_verbs()?; // at the end so that we can override them
for key in store.unbound_keys.clone() {
store.unbind_key(key)?;
}
Ok(store)
}

Expand Down Expand Up @@ -402,6 +406,23 @@ impl VerbStore {
details: "You can't simultaneously have leave_broot=false and from_shell=true".to_string(),
});
}

// we accept both key and keys. We merge both here
let mut unchecked_keys = vc.keys.clone();
if let Some(key) = &vc.key {
unchecked_keys.push(key.clone());
}
let mut checked_keys = Vec::new();
for key in &unchecked_keys {
let key = crokey::parse(key)?;
if keys::is_reserved(key) {
return Err(ConfError::ReservedKey {
key: keys::KEY_FORMAT.to_string(key)
});
}
checked_keys.push(key);
}

let invocation = vc.invocation.clone().filter(|i| !i.is_empty());
let internal = vc.internal.as_ref().filter(|i| !i.is_empty());
let external = vc.external.as_ref().filter(|i| !i.is_empty());
Expand Down Expand Up @@ -456,9 +477,12 @@ impl VerbStore {
sequence: Sequence::new(s, cmd_separator),
}),
_ => {
return Err(ConfError::InvalidVerbConf {
details: "You must define either internal, external or cmd".to_string(),
});
// there's no execution, this 'verbconf' is supposed to be dedicated to
// unbind keys
for key in checked_keys {
self.unbound_keys.push(key);
}
return Ok(());
}
};
let description = vc
Expand All @@ -471,21 +495,6 @@ impl VerbStore {
execution,
description,
)?;
// we accept both key and keys. We merge both here
let mut unchecked_keys = vc.keys.clone();
if let Some(key) = &vc.key {
unchecked_keys.push(key.clone());
}
let mut checked_keys = Vec::new();
for key in &unchecked_keys {
let key = crokey::parse(key)?;
if keys::is_reserved(key) {
return Err(ConfError::ReservedKey {
key: keys::KEY_FORMAT.to_string(key)
});
}
checked_keys.push(key);
}
for extension in &vc.extensions {
verb.file_extensions.push(extension.clone());
}
Expand All @@ -505,6 +514,26 @@ impl VerbStore {
Ok(())
}

pub fn unbind_key(
&mut self,
key: KeyCombination,
) -> Result<(), ConfError> {
debug!("unbinding key {:?}", key);
for verb in &mut self.verbs {
verb.keys.retain(|&k| k != key);
}
Ok(())
}
pub fn unbind_name(
&mut self,
name: &str,
) -> Result<(), ConfError> {
for verb in &mut self.verbs {
verb.names.retain(|n| n != name);
}
Ok(())
}

pub fn search_sel_info<'v>(
&'v self,
prefix: &str,
Expand Down

0 comments on commit 7cbd2d8

Please sign in to comment.