You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

301 lines
9.8 KiB

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