Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Further autocorrect improvements
Browse files Browse the repository at this point in the history
drashna committed Dec 12, 2021
1 parent 561e0f2 commit 6966284
Showing 2 changed files with 47 additions and 20 deletions.
49 changes: 35 additions & 14 deletions users/drashna/keyrecords/autocorrection/autocorrection.c
Original file line number Diff line number Diff line change
@@ -27,22 +27,43 @@ bool process_autocorrection(uint16_t keycode, keyrecord_t* record) {
return true;
}

// Exclude Shift hold from resetting autocorrection.
if (keycode == KC_LSFT || keycode == KC_RSFT) {
return true;
} else if ((QK_MOD_TAP <= keycode && keycode <= QK_MOD_TAP_MAX && ((keycode >> 8) & 0x0f) == MOD_LSFT) && (record->event.pressed || !record->tap.count)) {
return true;
#ifndef NO_ACTION_ONESHOT
} else if ((QK_ONE_SHOT_MOD <= keycode && keycode <= QK_ONE_SHOT_MOD_MAX && (keycode & 0xF) == MOD_LSFT)) {
return true;
#endif
} else if (!record->event.pressed) {
return true;
switch (keycode) {
case KC_LSFT:
case KC_RSFT:
return true;
# ifndef NO_ACTION_TAPPING
case QK_MOD_TAP ... QK_MOD_TAP_MAX:
if (((keycode >> 8) & 0x0f) == MOD_LSFT && record->event.pressed || !record->tap.count) {
return true;
}
keycode &= 0xFF;
break;
# ifndef NO_ACTION_LAYER
case QK_LAYER_TAP ... QK_LAYER_TAP_MAX:
if (record->event.pressed || !record->tap.count) {
return true;
}
keycode &= 0xFF;
break;
# endif
# endif
# ifndef NO_ACTION_ONESHOT
case QK_ONE_SHOT_MOD ... QK_ONE_SHOT_MOD_MAX:
if ((keycode & 0xF) == MOD_LSFT) {
return true;
}
keycode &= 0xF;
break;
# endif
default:
if (!record->event.pressed) {
return true;
}
}

// Subtract buffer for Backspace key, reset for other non-alpha.
if (!(KC_A <= (uint8_t)keycode && (uint8_t)keycode <= KC_Z) && (uint8_t)keycode != KC_SPC) {
if ((uint8_t)keycode == KC_BSPC && typo_buffer_size) {
if (!(KC_A <= keycode && keycode <= KC_Z) && keycode != KC_SPC) {
if (keycode == KC_BSPC && typo_buffer_size) {
--typo_buffer_size;
} else {
typo_buffer_size = 0;
@@ -57,7 +78,7 @@ bool process_autocorrection(uint16_t keycode, keyrecord_t* record) {
}

// Append `keycode` to buffer.
typo_buffer[typo_buffer_size++] = (uint8_t)keycode;
typo_buffer[typo_buffer_size++] = keycode;
// Return if buffer is smaller than the shortest word.
if (typo_buffer_size < AUTOCORRECTION_MIN_LENGTH) {
return true;
Original file line number Diff line number Diff line change
@@ -12,9 +12,13 @@
$ python3 make_autocorrection_data.py
Each line of the autcorrections_dict.txt file defines one typo and its
correction with the syntax "typo -> correction". Blank lines or lines starting
with '#' are ignored. Example:
Or to read from a different typo dict file, pass it as the first argument like
$ python3 make_autocorrection_data.py dict.txt
Each line of the dict file defines one typo and its correction with the syntax
"typo -> correction". Blank lines or lines starting with '#' are ignored.
Example:
:thier -> their
fitler -> filter
@@ -245,12 +249,14 @@ def write_generated_code(autocorrections: List[Tuple[str, str]],
f.write(generated_code)


def main():
autocorrections = parse_file('autocorrection_dict.txt')
def main(argv):
dict_file = argv[1] if len(argv) > 1 else 'autocorrection_dict.txt'
autocorrections = parse_file(dict_file)
trie = make_trie(autocorrections)
data = serialize_trie(autocorrections, trie)
print(f'Processed %d autocorrection entries to table with %d bytes.'
% (len(autocorrections), len(data)))
write_generated_code(autocorrections, data, 'autocorrection_data.h')

main()
if __name__ == '__main__':
main(sys.argv)

0 comments on commit 6966284

Please sign in to comment.