use alloc::string::{String, ToString};
use alloc::vec::Vec;
use super::keys::is_the_name_of_a_key;
pub(crate) fn is_the_name_of_a_command(name: &str) -> bool {
super::table::BUILTINS.iter().any(|builtin| {
builtin.kind == crate::builtins::BuiltinKind::Command
&& builtin.name.eq_ignore_ascii_case(name)
})
}
pub(crate) fn the_context_of_the_v1_is_refused_in_a_hotkey(name: &str) -> bool {
matches!(
name.to_lowercase().as_str(),
"ifwinactive"
| "ifwinnotactive"
| "ifwinexist"
| "ifwinnotexist"
| "ifexist"
| "ifnotexist"
| "ifequal"
| "ifnotequal"
| "ifgreater"
| "ifless"
| "ifbetween"
| "ifnotbetween"
| "ifin"
| "ifnotin"
| "ifinstring"
| "ifnotinstring"
| "ifis"
| "ifisnot"
)
}
pub(crate) struct TheName {
pub spelling: String,
pub identity: String,
}
pub(crate) fn the_name_of_a_hotkey(name: &str) -> Option<TheName> {
let mut rest = name; let mut wildcard = false;
let mut tilde = false;
let mut hook = false;
while let Some(first) = rest.chars().next() {
match first {
'*' if !wildcard => {
wildcard = true;
rest = &rest[1..];
}
'~' if !tilde => {
tilde = true;
rest = &rest[1..];
}
'$' if !hook => {
hook = true;
rest = &rest[1..];
}
_ => break,
}
}
let mut identity = String::new();
if wildcard {
identity.push('*');
}
let (key, up) = the_name_without_the_up(rest);
if key.contains('&') {
let mut parts = key.split('&').map(|part| part.trim());
let first = parts.next()?;
let second = parts.next()?;
if parts.next().is_some() {
return None;
}
if !is_a_key(first) || !is_a_key(second) {
return None;
}
identity.push_str(&first.to_lowercase());
identity.push('&');
identity.push_str(&second.to_lowercase());
if up {
identity.push('u');
}
return Some(TheName {
spelling: name.to_string(),
identity,
});
}
let mut modifiers: Vec<String> = Vec::new();
let mut left = false;
let mut rest = key;
while let Some(first) = rest.chars().next() {
match first {
'<' => {
left = true;
rest = &rest[1..];
}
'^' | '!' | '+' | '#' => {
rest = &rest[1..];
let right = rest.starts_with('>');
if right {
rest = &rest[1..];
}
let mut modifier = String::new();
if left {
modifier.push('l');
}
modifier.push(match first {
'^' => 'c',
'!' => 'a',
'+' => 's',
_ => 'w',
});
if right {
modifier.push('r');
}
modifiers.push(modifier);
left = false;
}
_ => break,
}
}
if left {
return None;
}
modifiers.sort();
for modifier in &modifiers {
identity.push_str(modifier);
}
if !is_a_key(rest) {
return None;
}
identity.push_str(&rest.to_lowercase());
if up {
identity.push('u');
}
Some(TheName {
spelling: name.to_string(),
identity,
})
}
fn the_name_without_the_up(name: &str) -> (&str, bool) {
let trimmed = name.trim_end();
if trimmed.len() < 3 {
return (name, false);
}
let rest = &trimmed[trimmed.len() - 2..];
if !rest.eq_ignore_ascii_case("up") {
return (name, false);
}
let at = trimmed.len() - 2;
if at == 0 || !trimmed[..at].ends_with(char::is_whitespace) {
return (name, false);
}
(trimmed[..at].trim_end(), true)
}
fn is_a_key(text: &str) -> bool {
if text.starts_with(char::is_whitespace) || text.ends_with(char::is_whitespace) {
return false;
}
is_the_name_of_a_key(text)
}
#[cfg(test)]
mod tests {
use super::the_name_of_a_hotkey;
#[test]
fn the_names_of_the_hotkeys() {
assert!(the_name_of_a_hotkey("^!a").is_some());
assert!(the_name_of_a_hotkey("a & b").is_some());
assert!(the_name_of_a_hotkey("RCtrl & RShift").is_some());
assert!(the_name_of_a_hotkey("a & b & c").is_none());
assert!(the_name_of_a_hotkey("^!a b").is_none());
}
}