summaryrefslogtreecommitdiff
path: root/quantum/process_keycode/process_autocorrect.c
diff options
context:
space:
mode:
authorDrashna Jaelre <drashna@live.com>2022-09-17 00:50:54 -0700
committerGitHub <noreply@github.com>2022-09-17 17:50:54 +1000
commitfb29c0ae53e32fb049516fcbe0f7863882ca9173 (patch)
tree989feaa47190248726a462bf761a98f46d91f406 /quantum/process_keycode/process_autocorrect.c
parentd67d388e776eeb8596181894639b6ed46c8a2d7d (diff)
[Core] Add getreuer's Autocorrect feature to core (#15699)
Co-authored-by: Albert Y <76888457+filterpaper@users.noreply.github.com>
Diffstat (limited to 'quantum/process_keycode/process_autocorrect.c')
-rw-r--r--quantum/process_keycode/process_autocorrect.c287
1 files changed, 287 insertions, 0 deletions
diff --git a/quantum/process_keycode/process_autocorrect.c b/quantum/process_keycode/process_autocorrect.c
new file mode 100644
index 0000000000..abae5e7811
--- /dev/null
+++ b/quantum/process_keycode/process_autocorrect.c
@@ -0,0 +1,287 @@
1// Copyright 2021 Google LLC
2// Copyright 2021 @filterpaper
3// SPDX-License-Identifier: Apache-2.0
4// Original source: https://getreuer.info/posts/keyboards/autocorrection
5
6#include "process_autocorrect.h"
7#include <string.h>
8#include "keycode_config.h"
9
10#if __has_include("autocorrect_data.h")
11# include "autocorrect_data.h"
12#else
13# pragma message "Autocorrect is using the default library."
14# include "autocorrect_data_default.h"
15#endif
16
17static uint8_t typo_buffer[AUTOCORRECT_MAX_LENGTH] = {KC_SPC};
18static uint8_t typo_buffer_size = 1;
19
20/**
21 * @brief function for querying the enabled state of autocorrect
22 *
23 * @return true if enabled
24 * @return false if disabled
25 */
26bool autocorrect_is_enabled(void) {
27 return keymap_config.autocorrect_enable;
28}
29
30/**
31 * @brief Enables autocorrect and saves state to eeprom
32 *
33 */
34void autocorrect_enable(void) {
35 keymap_config.autocorrect_enable = true;
36 eeconfig_update_keymap(keymap_config.raw);
37}
38
39/**
40 * @brief Disables autocorrect and saves state to eeprom
41 *
42 */
43void autocorrect_disable(void) {
44 keymap_config.autocorrect_enable = false;
45 typo_buffer_size = 0;
46 eeconfig_update_keymap(keymap_config.raw);
47}
48
49/**
50 * @brief Toggles autocorrect's status and save state to eeprom
51 *
52 */
53void autocorrect_toggle(void) {
54 keymap_config.autocorrect_enable = !keymap_config.autocorrect_enable;
55 typo_buffer_size = 0;
56 eeconfig_update_keymap(keymap_config.raw);
57}
58
59/**
60 * @brief handler for determining if autocorrect should process keypress
61 *
62 * @param keycode Keycode registered by matrix press, per keymap
63 * @param record keyrecord_t structure
64 * @param typo_buffer_size passed along to allow resetting of autocorrect buffer
65 * @param mods allow processing of mod status
66 * @return true Allow autocorection
67 * @return false Stop processing and escape from autocorrect.
68 */
69__attribute__((weak)) bool process_autocorrect_user(uint16_t *keycode, keyrecord_t *record, uint8_t *typo_buffer_size, uint8_t *mods) {
70 // See quantum_keycodes.h for reference on these matched ranges.
71 switch (*keycode) {
72 // Exclude these keycodes from processing.
73 case KC_LSFT:
74 case KC_RSFT:
75 case KC_CAPS:
76 case QK_TO ... QK_ONE_SHOT_LAYER_MAX:
77 case QK_LAYER_TAP_TOGGLE ... QK_LAYER_MOD_MAX:
78 case QK_ONE_SHOT_MOD ... QK_ONE_SHOT_MOD_MAX:
79 return false;
80
81 // Mask for base keycode from shifted keys.
82 case QK_LSFT ... QK_LSFT + 255:
83 case QK_RSFT ... QK_RSFT + 255:
84 if (*keycode >= QK_LSFT && *keycode <= (QK_LSFT + 255)) {
85 *mods |= MOD_LSFT;
86 } else {
87 *mods |= MOD_RSFT;
88 }
89 *keycode &= 0xFF; // Get the basic keycode.
90 return true;
91#ifndef NO_ACTION_TAPPING
92 // Exclude tap-hold keys when they are held down
93 // and mask for base keycode when they are tapped.
94 case QK_LAYER_TAP ... QK_LAYER_TAP_MAX:
95# ifdef NO_ACTION_LAYER
96 // Exclude Layer Tap, if layers are disabled
97 // but action tapping is still enabled.
98 return false;
99# endif
100 case QK_MOD_TAP ... QK_MOD_TAP_MAX:
101 // Exclude hold keycode
102 if (!record->tap.count) {
103 return false;
104 }
105 *keycode &= 0xFF;
106 break;
107#else
108 case QK_MOD_TAP ... QK_MOD_TAP_MAX:
109 case QK_LAYER_TAP ... QK_LAYER_TAP_MAX:
110 // Exclude if disabled
111 return false;
112#endif
113 // Exclude swap hands keys when they are held down
114 // and mask for base keycode when they are tapped.
115 case QK_SWAP_HANDS ... QK_SWAP_HANDS_MAX:
116#ifdef SWAP_HANDS_ENABLE
117 if (*keycode >= 0x56F0 || !record->tap.count) {
118 return false;
119 }
120 *keycode &= 0xFF;
121 break;
122#else
123 // Exclude if disabled
124 return false;
125#endif
126 }
127
128 // Disable autocorrect while a mod other than shift is active.
129 if ((*mods & ~MOD_MASK_SHIFT) != 0) {
130 *typo_buffer_size = 0;
131 return false;
132 }
133
134 return true;
135}
136
137/**
138 * @brief handling for when autocorrection has been triggered
139 *
140 * @param backspaces number of characters to remove
141 * @param str pointer to PROGMEM string to replace mistyped seletion with
142 * @return true apply correction
143 * @return false user handled replacement
144 */
145__attribute__((weak)) bool apply_autocorrect(uint8_t backspaces, const char *str) {
146 return true;
147}
148
149/**
150 * @brief Process handler for autocorrect feature
151 *
152 * @param keycode Keycode registered by matrix press, per keymap
153 * @param record keyrecord_t structure
154 * @return true Continue processing keycodes, and send to host
155 * @return false Stop processing keycodes, and don't send to host
156 */
157bool process_autocorrect(uint16_t keycode, keyrecord_t *record) {
158 uint8_t mods = get_mods();
159#ifndef NO_ACTION_ONESHOT
160 mods |= get_oneshot_mods();
161#endif
162
163 if ((keycode >= AUTOCORRECT_ON && keycode <= AUTOCORRECT_TOGGLE) && record->event.pressed) {
164 if (keycode == AUTOCORRECT_ON) {
165 autocorrect_enable();
166 } else if (keycode == AUTOCORRECT_OFF) {
167 autocorrect_disable();
168 } else if (keycode == AUTOCORRECT_TOGGLE) {
169 autocorrect_toggle();
170 } else {
171 return true;
172 }
173
174 return false;
175 }
176
177 if (!keymap_config.autocorrect_enable) {
178 typo_buffer_size = 0;
179 return true;
180 }
181
182 if (!record->event.pressed) {
183 return true;
184 }
185
186 // autocorrect keycode verification and extraction
187 if (!process_autocorrect_user(&keycode, record, &typo_buffer_size, &mods)) {
188 return true;
189 }
190
191 // keycode buffer check
192 switch (keycode) {
193 case KC_A ... KC_Z:
194 // process normally
195 break;
196 case KC_1 ... KC_0:
197 case KC_TAB ... KC_SEMICOLON:
198 case KC_GRAVE ... KC_SLASH:
199 // Set a word boundary if space, period, digit, etc. is pressed.
200 keycode = KC_SPC;
201 break;
202 case KC_ENTER:
203 // Behave more conservatively for the enter key. Reset, so that enter
204 // can't be used on a word ending.
205 typo_buffer_size = 0;
206 keycode = KC_SPC;
207 break;
208 case KC_BSPC:
209 // Remove last character from the buffer.
210 if (typo_buffer_size > 0) {
211 --typo_buffer_size;
212 }
213 return true;
214 case KC_QUOTE:
215 // Treat " (shifted ') as a word boundary.
216 if ((mods & MOD_MASK_SHIFT) != 0) {
217 keycode = KC_SPC;
218 }
219 break;
220 default:
221 // Clear state if some other non-alpha key is pressed.
222 typo_buffer_size = 0;
223 return true;
224 }
225
226 // Rotate oldest character if buffer is full.
227 if (typo_buffer_size >= AUTOCORRECT_MAX_LENGTH) {
228 memmove(typo_buffer, typo_buffer + 1, AUTOCORRECT_MAX_LENGTH - 1);
229 typo_buffer_size = AUTOCORRECT_MAX_LENGTH - 1;
230 }
231
232 // Append `keycode` to buffer.
233 typo_buffer[typo_buffer_size++] = keycode;
234 // Return if buffer is smaller than the shortest word.
235 if (typo_buffer_size < AUTOCORRECT_MIN_LENGTH) {
236 return true;
237 }
238
239 // Check for typo in buffer using a trie stored in `autocorrect_data`.
240 uint16_t state = 0;
241 uint8_t code = pgm_read_byte(autocorrect_data + state);
242 for (int8_t i = typo_buffer_size - 1; i >= 0; --i) {
243 uint8_t const key_i = typo_buffer[i];
244
245 if (code & 64) { // Check for match in node with multiple children.
246 code &= 63;
247 for (; code != key_i; code = pgm_read_byte(autocorrect_data + (state += 3))) {
248 if (!code) return true;
249 }
250 // Follow link to child node.
251 state = (pgm_read_byte(autocorrect_data + state + 1) | pgm_read_byte(autocorrect_data + state + 2) << 8);
252 // Check for match in node with single child.
253 } else if (code != key_i) {
254 return true;
255 } else if (!(code = pgm_read_byte(autocorrect_data + (++state)))) {
256 ++state;
257 }
258
259 // Stop if `state` becomes an invalid index. This should not normally
260 // happen, it is a safeguard in case of a bug, data corruption, etc.
261 if (state >= DICTIONARY_SIZE) {
262 return true;
263 }
264
265 code = pgm_read_byte(autocorrect_data + state);
266
267 if (code & 128) { // A typo was found! Apply autocorrect.
268 const uint8_t backspaces = (code & 63) + !record->event.pressed;
269 if (apply_autocorrect(backspaces, (char const *)(autocorrect_data + state + 1))) {
270 for (uint8_t i = 0; i < backspaces; ++i) {
271 tap_code(KC_BSPC);
272 }
273 send_string_P((char const *)(autocorrect_data + state + 1));
274 }
275
276 if (keycode == KC_SPC) {
277 typo_buffer[0] = KC_SPC;
278 typo_buffer_size = 1;
279 return true;
280 } else {
281 typo_buffer_size = 0;
282 return false;
283 }
284 }
285 }
286 return true;
287}