Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
hariseldon78 committed Dec 27, 2024
1 parent 3187c19 commit a8f19c5
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 42 deletions.
2 changes: 1 addition & 1 deletion docs/src/keyboard_configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ The key string should follow several rules:

The definitions of those operations are same with QMK, you can found [here](https://docs.qmk.fm/#/feature_layers). If you want other actions, please [fire an issue](https://github.com/HaoboGu/rmk/issues/new).

4. For modifier-tap-hold, use `MTH(key, modifier)` where the modifier can be a chain like explained on point 1
4. For modifier-tap-hold, use `MTH(key, modifier)` where the modifier can be a chain like explained on point 1. For example for a Home row modifier config you can use `MTH(F,LShift)`

5. For generic key tap-hold, use `TH(key-tap, key-hold)`.

Expand Down
82 changes: 44 additions & 38 deletions rmk-macro/src/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ fn parse_modifiers(modifiers_str: &str) -> (bool, bool, bool, bool, bool){
"LShift" => shift = true,
"LCtrl" => ctrl = true,
"LAlt" => alt = true,
"Lgui" => gui = true,
"LGui" => gui = true,
"RShift" => {
right = true;
shift = true;
Expand Down Expand Up @@ -219,52 +219,58 @@ fn parse_key(key: String) -> TokenStream2 {
::rmk::df!(#layer)
}
}
"MTH(" => {
let keys: Vec<&str> = key
.trim_start_matches("MTH(")
.trim_end_matches(")")
.split_terminator(",")
.map(|w| w.trim())
.filter(|w| w.len() > 0)
.collect();
if keys.len() != 2 {
return quote! {
compile_error!("keyboard.toml: MTH(modifiers, key) invalid, please check the documentation: https://haobogu.github.io/rmk/keyboard_configuration.html");
};
}
let ident = format_ident!("{}", keys[0].to_string());

let (right, gui, alt, shift, ctrl) = parse_modifiers(keys[1]);
"MTH" => {
if let Some(internal) = key.trim_start_matches("MTH(").strip_suffix(")") {
let keys: Vec<&str> = internal
.split_terminator(",")
.map(|w| w.trim())
.filter(|w| w.len() > 0)
.collect();
if keys.len() != 2 {
return quote! {
compile_error!("keyboard.toml: MTH(key, modifier) invalid, please check the documentation: https://haobogu.github.io/rmk/keyboard_configuration.html");
};
}
let ident = format_ident!("{}", keys[0].to_string());
let (right, gui, alt, shift, ctrl) = parse_modifiers(keys[1]);

if (gui || alt || shift || ctrl) == false {
if (gui || alt || shift || ctrl) == false {
return quote! {
compile_error!("keyboard.toml: modifier in MTH(key, modifier) is not valid! Please check the documentation: https://haobogu.github.io/rmk/keyboard_configuration.html");
};
}
quote! {
::rmk::mth!(#ident, ::rmk::keycode::ModifierCombination::new_from(#right, #gui, #alt, #shift, #ctrl))
}
} else {
return quote! {
compile_error!("keyboard.toml: modifier in MTH(modifier, key) is not valid! Please check the documentation: https://haobogu.github.io/rmk/keyboard_configuration.html");
compile_error!("keyboard.toml: MTH(key, modifier) invalid, please check the documentation: https://haobogu.github.io/rmk/keyboard_configuration.html");
};
}
quote! {
::rmk::mth!(#ident, ::rmk::keycode::ModifierCombination::new_from(#right, #gui, #alt, #shift, #ctrl))
}

}
"TH(" => {
let keys: Vec<&str> = key
.trim_start_matches("TH(")
.trim_end_matches(")")
.split_terminator(",")
.map(|w| w.trim())
.filter(|w| w.len() > 0)
.collect();
if keys.len() != 2 {
if let Some(internal) = key.trim_start_matches("TH(").strip_suffix(")") {
let keys: Vec<&str> = internal
.split_terminator(",")
.map(|w| w.trim())
.filter(|w| w.len() > 0)
.collect();
if keys.len() != 2 {
return quote! {
compile_error!("keyboard.toml: TH(key_tap, key_hold) invalid, please check the documentation: https://haobogu.github.io/rmk/keyboard_configuration.html");
};
}
let ident1 = format_ident!("{}", keys[0].to_string());
let ident2 = format_ident!("{}", keys[1].to_string());

quote! {
::rmk::th!(#ident1, #ident2)
}
} else {
return quote! {
compile_error!("keyboard.toml: TH(modifiers, key) invalid, please check the documentation: https://haobogu.github.io/rmk/keyboard_configuration.html");
compile_error!("keyboard.toml: TH(key_tap, key_hold) invalid, please check the documentation: https://haobogu.github.io/rmk/keyboard_configuration.html");
};
}
let ident1 = format_ident!("{}", keys[0].to_string());
let ident2 = format_ident!("{}", keys[1].to_string());

quote! {
::rmk::th!(#ident1, #ident2)
}

}
_ => {
Expand Down
6 changes: 3 additions & 3 deletions rmk/src/layout_macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ macro_rules! lt {
macro_rules! mth {
($k: ident, $m: expr) => {
$crate::action::KeyAction::ModifierTapHold(
$m,
$crate::action::Action::Key($crate::keycode::KeyCode::$k),
$m,
)
};
}
Expand All @@ -76,8 +76,8 @@ macro_rules! mth {
macro_rules! th {
($t: ident, $h: ident) => {
$crate::action::KeyAction::TapHold(
$t,
$h,
$crate::action::Action::Key($crate::keycode::KeyCode::$t),
$crate::action::Action::Key($crate::keycode::KeyCode::$h),
)
};
}
Expand Down

0 comments on commit a8f19c5

Please sign in to comment.