process_autocorrect.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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_ONE_SHOT_LAYER_MAX:
  69. case QK_LAYER_TAP_TOGGLE ... QK_LAYER_MOD_MAX:
  70. case QK_ONE_SHOT_MOD ... QK_ONE_SHOT_MOD_MAX:
  71. return false;
  72. // Mask for base keycode from shifted keys.
  73. case QK_LSFT ... QK_LSFT + 255:
  74. case QK_RSFT ... QK_RSFT + 255:
  75. if (*keycode >= QK_LSFT && *keycode <= (QK_LSFT + 255)) {
  76. *mods |= MOD_LSFT;
  77. } else {
  78. *mods |= MOD_RSFT;
  79. }
  80. *keycode &= 0xFF; // Get the basic keycode.
  81. return true;
  82. #ifndef NO_ACTION_TAPPING
  83. // Exclude tap-hold keys when they are held down
  84. // and mask for base keycode when they are tapped.
  85. case QK_LAYER_TAP ... QK_LAYER_TAP_MAX:
  86. # ifdef NO_ACTION_LAYER
  87. // Exclude Layer Tap, if layers are disabled
  88. // but action tapping is still enabled.
  89. return false;
  90. # endif
  91. case QK_MOD_TAP ... QK_MOD_TAP_MAX:
  92. // Exclude hold keycode
  93. if (!record->tap.count) {
  94. return false;
  95. }
  96. *keycode &= 0xFF;
  97. break;
  98. #else
  99. case QK_MOD_TAP ... QK_MOD_TAP_MAX:
  100. case QK_LAYER_TAP ... QK_LAYER_TAP_MAX:
  101. // Exclude if disabled
  102. return false;
  103. #endif
  104. // Exclude swap hands keys when they are held down
  105. // and mask for base keycode when they are tapped.
  106. case QK_SWAP_HANDS ... QK_SWAP_HANDS_MAX:
  107. #ifdef SWAP_HANDS_ENABLE
  108. if (*keycode >= 0x56F0 || !record->tap.count) {
  109. return false;
  110. }
  111. *keycode &= 0xFF;
  112. break;
  113. #else
  114. // Exclude if disabled
  115. return false;
  116. #endif
  117. }
  118. // Disable autocorrect while a mod other than shift is active.
  119. if ((*mods & ~MOD_MASK_SHIFT) != 0) {
  120. *typo_buffer_size = 0;
  121. return false;
  122. }
  123. return true;
  124. }
  125. /**
  126. * @brief handling for when autocorrection has been triggered
  127. *
  128. * @param backspaces number of characters to remove
  129. * @param str pointer to PROGMEM string to replace mistyped seletion with
  130. * @return true apply correction
  131. * @return false user handled replacement
  132. */
  133. __attribute__((weak)) bool apply_autocorrect(uint8_t backspaces, const char *str) {
  134. return true;
  135. }
  136. /**
  137. * @brief Process handler for autocorrect feature
  138. *
  139. * @param keycode Keycode registered by matrix press, per keymap
  140. * @param record keyrecord_t structure
  141. * @return true Continue processing keycodes, and send to host
  142. * @return false Stop processing keycodes, and don't send to host
  143. */
  144. bool process_autocorrect(uint16_t keycode, keyrecord_t *record) {
  145. uint8_t mods = get_mods();
  146. #ifndef NO_ACTION_ONESHOT
  147. mods |= get_oneshot_mods();
  148. #endif
  149. if ((keycode >= AUTOCORRECT_ON && keycode <= AUTOCORRECT_TOGGLE) && record->event.pressed) {
  150. if (keycode == AUTOCORRECT_ON) {
  151. autocorrect_enable();
  152. } else if (keycode == AUTOCORRECT_OFF) {
  153. autocorrect_disable();
  154. } else if (keycode == AUTOCORRECT_TOGGLE) {
  155. autocorrect_toggle();
  156. } else {
  157. return true;
  158. }
  159. return false;
  160. }
  161. if (!keymap_config.autocorrect_enable) {
  162. typo_buffer_size = 0;
  163. return true;
  164. }
  165. if (!record->event.pressed) {
  166. return true;
  167. }
  168. // autocorrect keycode verification and extraction
  169. if (!process_autocorrect_user(&keycode, record, &typo_buffer_size, &mods)) {
  170. return true;
  171. }
  172. // keycode buffer check
  173. switch (keycode) {
  174. case KC_A ... KC_Z:
  175. // process normally
  176. break;
  177. case KC_1 ... KC_0:
  178. case KC_TAB ... KC_SEMICOLON:
  179. case KC_GRAVE ... KC_SLASH:
  180. // Set a word boundary if space, period, digit, etc. is pressed.
  181. keycode = KC_SPC;
  182. break;
  183. case KC_ENTER:
  184. // Behave more conservatively for the enter key. Reset, so that enter
  185. // can't be used on a word ending.
  186. typo_buffer_size = 0;
  187. keycode = KC_SPC;
  188. break;
  189. case KC_BSPC:
  190. // Remove last character from the buffer.
  191. if (typo_buffer_size > 0) {
  192. --typo_buffer_size;
  193. }
  194. return true;
  195. case KC_QUOTE:
  196. // Treat " (shifted ') as a word boundary.
  197. if ((mods & MOD_MASK_SHIFT) != 0) {
  198. keycode = KC_SPC;
  199. }
  200. break;
  201. default:
  202. // Clear state if some other non-alpha key is pressed.
  203. typo_buffer_size = 0;
  204. return true;
  205. }
  206. // Rotate oldest character if buffer is full.
  207. if (typo_buffer_size >= AUTOCORRECT_MAX_LENGTH) {
  208. memmove(typo_buffer, typo_buffer + 1, AUTOCORRECT_MAX_LENGTH - 1);
  209. typo_buffer_size = AUTOCORRECT_MAX_LENGTH - 1;
  210. }
  211. // Append `keycode` to buffer.
  212. typo_buffer[typo_buffer_size++] = keycode;
  213. // Return if buffer is smaller than the shortest word.
  214. if (typo_buffer_size < AUTOCORRECT_MIN_LENGTH) {
  215. return true;
  216. }
  217. // Check for typo in buffer using a trie stored in `autocorrect_data`.
  218. uint16_t state = 0;
  219. uint8_t code = pgm_read_byte(autocorrect_data + state);
  220. for (int8_t i = typo_buffer_size - 1; i >= 0; --i) {
  221. uint8_t const key_i = typo_buffer[i];
  222. if (code & 64) { // Check for match in node with multiple children.
  223. code &= 63;
  224. for (; code != key_i; code = pgm_read_byte(autocorrect_data + (state += 3))) {
  225. if (!code) return true;
  226. }
  227. // Follow link to child node.
  228. state = (pgm_read_byte(autocorrect_data + state + 1) | pgm_read_byte(autocorrect_data + state + 2) << 8);
  229. // Check for match in node with single child.
  230. } else if (code != key_i) {
  231. return true;
  232. } else if (!(code = pgm_read_byte(autocorrect_data + (++state)))) {
  233. ++state;
  234. }
  235. // Stop if `state` becomes an invalid index. This should not normally
  236. // happen, it is a safeguard in case of a bug, data corruption, etc.
  237. if (state >= DICTIONARY_SIZE) {
  238. return true;
  239. }
  240. code = pgm_read_byte(autocorrect_data + state);
  241. if (code & 128) { // A typo was found! Apply autocorrect.
  242. const uint8_t backspaces = (code & 63) + !record->event.pressed;
  243. if (apply_autocorrect(backspaces, (char const *)(autocorrect_data + state + 1))) {
  244. for (uint8_t i = 0; i < backspaces; ++i) {
  245. tap_code(KC_BSPC);
  246. }
  247. send_string_P((char const *)(autocorrect_data + state + 1));
  248. }
  249. if (keycode == KC_SPC) {
  250. typo_buffer[0] = KC_SPC;
  251. typo_buffer_size = 1;
  252. return true;
  253. } else {
  254. typo_buffer_size = 0;
  255. return false;
  256. }
  257. }
  258. }
  259. return true;
  260. }