Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add sequence-noerase #1489

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions parser/src/cfg/list_actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ pub const CAPS_WORD_CUSTOM_TOGGLE_A: &str = "word⇪custom-toggle";
pub const DYNAMIC_MACRO_RECORD_STOP_TRUNCATE: &str = "dynamic-macro-record-stop-truncate";
pub const SWITCH: &str = "switch";
pub const SEQUENCE: &str = "sequence";
pub const SEQUENCE_NOERASE: &str = "sequence-noerase";
pub const UNMOD: &str = "unmod";
pub const UNSHIFT: &str = "unshift";
pub const UNSHIFT_A: &str = "un⇧";
Expand Down Expand Up @@ -219,6 +220,7 @@ pub fn is_list_action(ac: &str) -> bool {
DYNAMIC_MACRO_RECORD_STOP_TRUNCATE,
SWITCH,
SEQUENCE,
SEQUENCE_NOERASE,
UNMOD,
UNSHIFT,
UNSHIFT_A,
Expand Down
12 changes: 12 additions & 0 deletions parser/src/cfg/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1850,6 +1850,7 @@ fn parse_action_list(ac: &[SExpr], s: &ParserState) -> Result<&'static KanataAct
DYNAMIC_MACRO_RECORD_STOP_TRUNCATE => parse_macro_record_stop_truncate(&ac[1..], s),
SWITCH => parse_switch(&ac[1..], s),
SEQUENCE => parse_sequence_start(&ac[1..], s),
SEQUENCE_NOERASE => parse_sequence_noerase(&ac[1..], s),
UNMOD => parse_unmod(UNMOD, &ac[1..], s),
UNSHIFT | UNSHIFT_A => parse_unmod(UNSHIFT, &ac[1..], s),
LIVE_RELOAD_NUM => parse_live_reload_num(&ac[1..], s),
Expand Down Expand Up @@ -3816,6 +3817,17 @@ fn parse_sequence_start(ac_params: &[SExpr], s: &ParserState) -> Result<&'static
))))
}

fn parse_sequence_noerase(ac_params: &[SExpr], s: &ParserState) -> Result<&'static KanataAction> {
const ERR_MSG: &str = "sequence-noerase expects one: <noerase-count>";
if ac_params.len() != 1 {
bail!("{ERR_MSG}\nfound {} items", ac_params.len());
}
let count = parse_non_zero_u16(&ac_params[0], s, "noerase-count")?;
Ok(s.a.sref(Action::Custom(
s.a.sref(s.a.sref_slice(CustomAction::SequenceNoerase(count))),
)))
}

fn parse_unmod(
unmod_type: &str,
ac_params: &[SExpr],
Expand Down
6 changes: 6 additions & 0 deletions parser/src/custom_action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ pub enum CustomAction {
},
SequenceCancel,
SequenceLeader(u16, SequenceInputMode),
/// Purpose:
/// In case the user has dead keys in their OS layout, they may wish to send fewer backspaces upon
/// a successful completion of visible-backspaced sequences, because the number of key events
/// is larger than the number of backspace-able symbols typed within the application.
/// This custom action is a marker to accomplish the use case.
SequenceNoerase(u16),
LiveReload,
LiveReloadNext,
LiveReloadPrev,
Expand Down
6 changes: 6 additions & 0 deletions src/kanata/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1510,6 +1510,12 @@ impl Kanata {
self.sequence_state.activate(*input_mode, *timeout);
}
}
CustomAction::SequenceNoerase(noerase_count) => {
if let Some(state) = self.sequence_state.get_active() {
log::debug!("pressed cancel sequence key");
add_noerase(state, *noerase_count);
}
}
CustomAction::Repeat => {
let keycode = self.last_pressed_key;
let osc: OsCode = keycode.into();
Expand Down
16 changes: 14 additions & 2 deletions src/kanata/sequences.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ pub struct SequenceState {
pub sequence_timeout: u16,
/// Whether the sequence is active or not.
pub activity: SequenceActivity,
/// Counter to reduce number of backspaces typed.
noerase_count: u16,
}

impl SequenceState {
Expand All @@ -40,6 +42,7 @@ impl SequenceState {
ticks_until_timeout: 0,
sequence_timeout: 0,
activity: Inactive,
noerase_count: 0,
}
}

Expand All @@ -52,6 +55,7 @@ impl SequenceState {
self.sequence.clear();
self.overlapped_sequence.clear();
self.activity = Active;
self.noerase_count = 0;
}

pub fn is_active(&self) -> bool {
Expand Down Expand Up @@ -320,8 +324,12 @@ pub(super) fn do_successful_sequence_termination(
osc if osc.is_modifier() => continue,
osc if matches!(u16::from(osc), KEY_IGNORE_MIN..=KEY_IGNORE_MAX) => continue,
_ => {
kbd_out.press_key(OsCode::KEY_BACKSPACE)?;
kbd_out.release_key(OsCode::KEY_BACKSPACE)?;
if state.noerase_count > 0 {
state.noerase_count -= 1;
} else {
kbd_out.press_key(OsCode::KEY_BACKSPACE)?;
kbd_out.release_key(OsCode::KEY_BACKSPACE)?;
}
}
}
}
Expand Down Expand Up @@ -357,3 +365,7 @@ pub(super) fn cancel_sequence(state: &mut SequenceState, kbd_out: &mut KbdOut) -
}
Ok(())
}

pub(super) fn add_noerase(state: &mut SequenceState, noerase_count: u16) {
state.noerase_count += noerase_count;
}
29 changes: 29 additions & 0 deletions src/tests/sim_tests/seq_sim_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,3 +207,32 @@ fn chorded_keys_hidden_delaytype() {
);
}
*/

#[test]
fn noerase() {
let result = simulate(
"(defcfg sequence-input-mode visible-backspaced)
(defsrc)
(deflayermap (base)
0 sldr
u (t! maybe-noerase u)
)
(deftemplate maybe-noerase (char)
(multi
(switch
((key-history ' 1)) (sequence-noerase 1) fallthrough
() $char break
))
)
(defvirtualkeys s1 z)
(defseq s1 (' u))
",
"d:0 u:0 d:' t:50 d:u t:500",
)
.no_time()
.to_ascii();
assert_eq!(
"dn:Quote dn:U dn:BSpace up:BSpace up:Quote up:U dn:Z up:Z",
result,
);
}
Loading