process_autocorrect.c 9.3 KB

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