Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
194 changes: 168 additions & 26 deletions keyberon/src/layout.rs

Large diffs are not rendered by default.

6 changes: 0 additions & 6 deletions parser/src/cfg/alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,6 @@ impl Allocations {
self.bref_slice(v.into_boxed_slice())
}

/// Returns a `&'static [&'static T]` by leaking a newly created box and boxed slice of `v`.
pub(crate) fn sref_slice<T>(&self, v: T) -> &'static [&'static T] {
log::debug!("sref_slice {}", std::any::type_name::<T>());
self.bref_slice(vec![self.sref(v)].into_boxed_slice())
}

/// Returns a `&'static str` by leaking a String.
pub(crate) fn sref_str(&self, v: String) -> &'static str {
if !v.capacity() == 0 {
Expand Down
4 changes: 1 addition & 3 deletions parser/src/cfg/arbitrary_code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,5 @@ pub(crate) fn parse_arbitrary_code(
.map(str::parse::<u16>)
.and_then(|c| c.ok())
.ok_or_else(|| anyhow!("{ERR_MSG}: got {:?}", ac_params[0]))?;
Ok(s.a.sref(Action::Custom(
s.a.sref(s.a.sref_slice(CustomAction::SendArbitraryCode(code))),
)))
custom(CustomAction::SendArbitraryCode(code), &s.a)
}
44 changes: 22 additions & 22 deletions parser/src/cfg/caps_word.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub(crate) fn parse_caps_word(
bail!("{ERR_STR}\nFound {} params instead of 1", ac_params.len());
}
let timeout = parse_non_zero_u16(&ac_params[0], s, "timeout")?;
Ok(s.a.sref(Action::Custom(s.a.sref(s.a.sref_slice(
custom(
CustomAction::CapsWord(CapsWordCfg {
repress_behaviour,
keys_to_capitalize: &[
Expand Down Expand Up @@ -74,7 +74,8 @@ pub(crate) fn parse_caps_word(
],
timeout,
}),
)))))
&s.a,
)
}

pub(crate) fn parse_caps_word_custom(
Expand All @@ -87,24 +88,23 @@ pub(crate) fn parse_caps_word_custom(
bail!("{ERR_STR}\nFound {} params instead of 3", ac_params.len());
}
let timeout = parse_non_zero_u16(&ac_params[0], s, "timeout")?;
Ok(s.a.sref(Action::Custom(
s.a.sref(
s.a.sref_slice(CustomAction::CapsWord(CapsWordCfg {
repress_behaviour,
keys_to_capitalize: s.a.sref_vec(
parse_key_list(&ac_params[1], s, "keys-to-capitalize")?
.into_iter()
.map(KeyCode::from)
.collect(),
),
keys_nonterminal: s.a.sref_vec(
parse_key_list(&ac_params[2], s, "extra-non-terminal-keys")?
.into_iter()
.map(KeyCode::from)
.collect(),
),
timeout,
})),
),
)))
custom(
CustomAction::CapsWord(CapsWordCfg {
repress_behaviour,
keys_to_capitalize: s.a.sref_vec(
parse_key_list(&ac_params[1], s, "keys-to-capitalize")?
.into_iter()
.map(KeyCode::from)
.collect(),
),
keys_nonterminal: s.a.sref_vec(
parse_key_list(&ac_params[2], s, "extra-non-terminal-keys")?
.into_iter()
.map(KeyCode::from)
.collect(),
),
timeout,
}),
&s.a,
)
}
2 changes: 1 addition & 1 deletion parser/src/cfg/chord.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ impl<'a> ChordTranslation<'a> {
timeout: &'a SExpr,
release_behaviour: &'a SExpr,
disabled_layers: &'a SExpr,
first_layer: &[Action<'static, &&[&CustomAction]>],
first_layer: &[Action<'static, KanataCustom>],
) -> Self {
let postprocess_map: FxHashMap<String, String> = [
("semicolon", ";"),
Expand Down
2 changes: 1 addition & 1 deletion parser/src/cfg/chord_v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ pub(crate) fn find_chords_coords(
}

pub(crate) fn fill_chords(
chord_groups: &[&'static ChordsGroup<&&[&CustomAction]>],
chord_groups: &[&'static ChordsGroup<KanataCustom>],
action: &KanataAction,
s: &ParserState,
) -> Option<KanataAction> {
Expand Down
22 changes: 9 additions & 13 deletions parser/src/cfg/clipboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ pub(crate) fn parse_clipboard_set(
}
};
let clip_string = clip_string.t.trim_atom_quotes();
Ok(s.a.sref(Action::Custom(s.a.sref(s.a.sref_slice(
custom(
CustomAction::ClipboardSet(s.a.sref_str(clip_string.to_string())),
)))))
&s.a,
)
}

pub(crate) fn parse_clipboard_save(
Expand All @@ -34,9 +35,7 @@ pub(crate) fn parse_clipboard_save(
bail!("{CLIPBOARD_SAVE} {ERR_MSG}, found {}", ac_params.len());
}
let id = parse_u16(&ac_params[0], s, "clipboard save ID")?;
Ok(s.a.sref(Action::Custom(
s.a.sref(s.a.sref_slice(CustomAction::ClipboardSave(id))),
)))
custom(CustomAction::ClipboardSave(id), &s.a)
}

pub(crate) fn parse_clipboard_restore(
Expand All @@ -48,9 +47,7 @@ pub(crate) fn parse_clipboard_restore(
bail!("{CLIPBOARD_RESTORE} {ERR_MSG}, found {}", ac_params.len());
}
let id = parse_u16(&ac_params[0], s, "clipboard save ID")?;
Ok(s.a.sref(Action::Custom(
s.a.sref(s.a.sref_slice(CustomAction::ClipboardRestore(id))),
)))
custom(CustomAction::ClipboardRestore(id), &s.a)
}

pub(crate) fn parse_clipboard_save_swap(
Expand All @@ -64,9 +61,7 @@ pub(crate) fn parse_clipboard_save_swap(
}
let id1 = parse_u16(&ac_params[0], s, "clipboard save ID")?;
let id2 = parse_u16(&ac_params[1], s, "clipboard save ID")?;
Ok(s.a.sref(Action::Custom(
s.a.sref(s.a.sref_slice(CustomAction::ClipboardSaveSwap(id1, id2))),
)))
custom(CustomAction::ClipboardSaveSwap(id1, id2), &s.a)
}

pub(crate) fn parse_clipboard_save_set(
Expand All @@ -81,7 +76,8 @@ pub(crate) fn parse_clipboard_save_set(
let save_content = ac_params[1]
.atom(s.vars())
.ok_or_else(|| anyhow_expr!(&ac_params[1], "save content must be a string"))?;
Ok(s.a.sref(Action::Custom(s.a.sref(s.a.sref_slice(
custom(
CustomAction::ClipboardSaveSet(id, s.a.sref_str(save_content.into())),
)))))
&s.a,
)
}
18 changes: 11 additions & 7 deletions parser/src/cfg/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,10 @@ pub(crate) fn parse_cmd_log(ac_params: &[SExpr], s: &ParserState) -> Result<&'st
bail!(ERR_STR);
}
let cmds = cmd.into_iter().map(|v| s.a.sref_str(v)).collect();
Ok(s.a.sref(Action::Custom(s.a.sref(s.a.sref_slice(
custom(
CustomAction::CmdLog(log_level, error_log_level, s.a.sref_vec(cmds)),
)))))
&s.a,
)
}

#[allow(unused_variables)]
Expand Down Expand Up @@ -80,9 +81,10 @@ pub(crate) fn parse_cmd(
bail_expr!(&ac_params[1], "{CLIPBOARD_SAVE_CMD_SET} {ERR_STR}");
}
let cmds = cmd.into_iter().map(|v| s.a.sref_str(v)).collect();
return Ok(s.a.sref(Action::Custom(s.a.sref(s.a.sref_slice(
return custom(
CustomAction::ClipboardSaveCmdSet(save_id, s.a.sref_vec(cmds)),
)))));
&s.a,
);
}

const ERR_STR: &str = "cmd expects at least one string";
Expand All @@ -96,13 +98,15 @@ pub(crate) fn parse_cmd(
}
let cmds = cmd.into_iter().map(|v| s.a.sref_str(v)).collect();
let cmds = s.a.sref_vec(cmds);
Ok(s.a
.sref(Action::Custom(s.a.sref(s.a.sref_slice(match cmd_type {
custom(
match cmd_type {
CmdType::Standard => CustomAction::Cmd(cmds),
CmdType::OutputKeys => CustomAction::CmdOutputKeys(cmds),
CmdType::ClipboardSet => CustomAction::ClipboardCmdSet(cmds),
CmdType::ClipboardSaveSet => unreachable!(),
})))))
},
&s.a,
)
}
}

Expand Down
44 changes: 21 additions & 23 deletions parser/src/cfg/fake_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,7 @@ pub(crate) fn parse_on_press_fake_key_op(
) -> Result<&'static KanataAction> {
let (coord, action) = parse_fake_key_op_coord_action(ac_params, s, ON_PRESS_FAKEKEY)?;
set_virtual_key_reference_lsp_hint(&ac_params[0], s);
Ok(s.a.sref(Action::Custom(
s.a.sref(s.a.sref_slice(CustomAction::FakeKey { coord, action })),
)))
custom(CustomAction::FakeKey { coord, action }, &s.a)
}

pub(crate) fn parse_on_release_fake_key_op(
Expand All @@ -121,9 +119,7 @@ pub(crate) fn parse_on_release_fake_key_op(
) -> Result<&'static KanataAction> {
let (coord, action) = parse_fake_key_op_coord_action(ac_params, s, ON_RELEASE_FAKEKEY)?;
set_virtual_key_reference_lsp_hint(&ac_params[0], s);
Ok(s.a.sref(Action::Custom(s.a.sref(
s.a.sref_slice(CustomAction::FakeKeyOnRelease { coord, action }),
))))
custom(CustomAction::FakeKeyOnRelease { coord, action }, &s.a)
}

pub(crate) fn parse_on_idle_fakekey(
Expand Down Expand Up @@ -170,13 +166,14 @@ pub(crate) fn parse_on_idle_fakekey(
let (x, y) = get_fake_key_coords(y);
let coord = Coord { x, y };
set_virtual_key_reference_lsp_hint(&ac_params[0], s);
Ok(s.a.sref(Action::Custom(s.a.sref(s.a.sref_slice(
custom(
CustomAction::FakeKeyOnIdle(FakeKeyOnIdle {
coord,
action,
idle_duration,
}),
)))))
&s.a,
)
}

fn parse_fake_key_op_coord_action(
Expand Down Expand Up @@ -256,11 +253,13 @@ fn parse_delay(
.map(str::parse::<u16>)
.ok_or_else(|| anyhow!("{ERR_MSG}"))?
.map_err(|e| anyhow!("{ERR_MSG}: {e}"))?;
Ok(s.a
.sref(Action::Custom(s.a.sref(s.a.sref_slice(match is_release {
custom(
match is_release {
false => CustomAction::Delay(delay),
true => CustomAction::DelayOnRelease(delay),
})))))
},
&s.a,
)
}

pub(crate) fn parse_vkey_coord(param: &SExpr, s: &ParserState) -> Result<Coord> {
Expand Down Expand Up @@ -309,9 +308,7 @@ pub(crate) fn parse_on_press(
let action = parse_vkey_action(&ac_params[0], s)?;
let coord = parse_vkey_coord(&ac_params[1], s)?;

Ok(s.a.sref(Action::Custom(
s.a.sref(s.a.sref_slice(CustomAction::FakeKey { coord, action })),
)))
custom(CustomAction::FakeKey { coord, action }, &s.a)
}

pub(crate) fn parse_on_release(
Expand All @@ -325,9 +322,7 @@ pub(crate) fn parse_on_release(
let action = parse_vkey_action(&ac_params[0], s)?;
let coord = parse_vkey_coord(&ac_params[1], s)?;

Ok(s.a.sref(Action::Custom(s.a.sref(
s.a.sref_slice(CustomAction::FakeKeyOnRelease { coord, action }),
))))
custom(CustomAction::FakeKeyOnRelease { coord, action }, &s.a)
}

pub(crate) fn parse_on_idle(ac_params: &[SExpr], s: &ParserState) -> Result<&'static KanataAction> {
Expand All @@ -339,13 +334,14 @@ pub(crate) fn parse_on_idle(ac_params: &[SExpr], s: &ParserState) -> Result<&'st
let action = parse_vkey_action(&ac_params[1], s)?;
let coord = parse_vkey_coord(&ac_params[2], s)?;

Ok(s.a.sref(Action::Custom(s.a.sref(s.a.sref_slice(
custom(
CustomAction::FakeKeyOnIdle(FakeKeyOnIdle {
coord,
action,
idle_duration,
}),
)))))
&s.a,
)
}

pub(crate) fn parse_on_physical_idle(
Expand All @@ -361,13 +357,14 @@ pub(crate) fn parse_on_physical_idle(
let action = parse_vkey_action(&ac_params[1], s)?;
let coord = parse_vkey_coord(&ac_params[2], s)?;

Ok(s.a.sref(Action::Custom(s.a.sref(s.a.sref_slice(
custom(
CustomAction::FakeKeyOnPhysicalIdle(FakeKeyOnIdle {
coord,
action,
idle_duration,
}),
)))))
&s.a,
)
}

pub(crate) fn parse_hold_for_duration(
Expand All @@ -381,10 +378,11 @@ pub(crate) fn parse_hold_for_duration(
let hold_duration = parse_non_zero_u16(&ac_params[0], s, "hold-duration")?;
let coord = parse_vkey_coord(&ac_params[1], s)?;

Ok(s.a.sref(Action::Custom(s.a.sref(s.a.sref_slice(
custom(
CustomAction::FakeKeyHoldForDuration(FakeKeyHoldForDuration {
coord,
hold_duration,
}),
)))))
&s.a,
)
}
16 changes: 6 additions & 10 deletions parser/src/cfg/key_outputs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,18 +113,14 @@ pub(crate) fn add_key_output_from_action_to_key_pos(
add_key_output_from_action_to_key_pos(osc_slot, case.1, outputs, overrides);
}
}
Action::Custom(cacs) => {
for ac in cacs.iter() {
match ac {
CustomAction::Unmodded { keys, .. } | CustomAction::Unshifted { keys } => {
for k in keys.iter() {
add_kc_output(osc_slot, k.into(), outputs, overrides);
}
}
_ => {}
Action::Custom(ac) => match ac {
CustomAction::Unmodded { keys, .. } | CustomAction::Unshifted { keys } => {
for k in keys.iter() {
add_kc_output(osc_slot, k.into(), outputs, overrides);
}
}
}
_ => {}
},
Action::Src => {
add_kc_output(osc_slot, osc_slot, outputs, overrides);
}
Expand Down
13 changes: 6 additions & 7 deletions parser/src/cfg/live_reload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,9 @@ pub(crate) fn parse_live_reload_num(
bail!("{LIVE_RELOAD_NUM} {ERR_MSG}, found {}", ac_params.len());
}
let num = parse_non_zero_u16(&ac_params[0], s, "config argument position")?;
Ok(s.a.sref(Action::Custom(
// Note: for user-friendliness (hopefully), begin at 1 for parsing.
// But for use as an index when stored as data, subtract 1 for 0-based indexing.
s.a.sref(s.a.sref_slice(CustomAction::LiveReloadNum(num - 1))),
)))
// Note: for user-friendliness (hopefully), begin at 1 for parsing.
// But for use as an index when stored as data, subtract 1 for 0-based indexing.
custom(CustomAction::LiveReloadNum(num - 1), &s.a)
}

pub(crate) fn parse_live_reload_file(
Expand All @@ -35,7 +33,8 @@ pub(crate) fn parse_live_reload_file(
}
};
let lrld_file_path = spanned_filepath.t.trim_atom_quotes();
Ok(s.a.sref(Action::Custom(s.a.sref(s.a.sref_slice(
custom(
CustomAction::LiveReloadFile(s.a.sref_str(lrld_file_path.to_string())),
)))))
&s.a,
)
}
Loading
Loading