Added keycode type checkers to spellcheck

master
Mark 2022-07-21 20:01:49 -07:00
parent 7be80e97fd
commit c757a1686c
Signed by: Mark
GPG Key ID: AD62BB059C2AAEE4
1 changed files with 89 additions and 30 deletions

View File

@ -1,11 +1,70 @@
#ifdef BETA_ENABLE_SPELLCHECK
#include "keymap_russian.h"
#include "keymap_us_international.h"
#include "features/spellcheck.h"
#include "features/beta_rawhid.h"
uint8_t spellcheck_buffer[SPELLCHECK_BUFFER_MAX] = {0};
uint8_t spellcheck_buffer_size = 0;
// Return one of the following.
#define SPELLCHECK_WORD 2
#define SPELLCHECK_SPACE 1
#define SPELLCHECK_NEITHER 0
uint8_t keycode_type_en(uint8_t mods, uint16_t keycode) {
if (
KC_A <= keycode && keycode <= KC_Z // basic letters
) {
return SPELLCHECK_WORD;
} else if (
( // Include these
(KC_1 <= keycode && keycode <= KC_SLSH) || // Symbols
// Treat " (shifted ') as a word boundary.
((keycode == KC_QUOT) && ((mods & MOD_MASK_SHIFT) != 0))
) && !( // Don't include these
(keycode == KC_ESC) ||
(keycode == KC_ENTER)
)
) {
return SPELLCHECK_SPACE;
} else {
return SPELLCHECK_NEITHER;
}
}
uint8_t keycode_type_ru(uint8_t mods, uint16_t keycode) {
if (
KC_A <= keycode && keycode <= KC_Z // basic letters
) {
return SPELLCHECK_WORD;
} else if (
KC_1 <= keycode && keycode <= KC_0
) {
return SPELLCHECK_SPACE;
}
switch (keycode) {
case RU_YO:
case RU_HA:
case RU_HARD:
case RU_E:
case RU_BE:
case RU_YU:
return SPELLCHECK_WORD;
case RU_MINS:
case RU_EQL:
case RU_DOT:
case RU_BSLS:
return SPELLCHECK_SPACE;
}
return SPELLCHECK_NEITHER;
}
bool process_spellcheck(uint16_t keycode, keyrecord_t* record) {
@ -71,42 +130,42 @@ bool process_spellcheck(uint16_t keycode, keyrecord_t* record) {
return true; // Ignore these keys.
}
if (keycode == KC_QUOT) {
// Treat " (shifted ') as a word boundary.
if ((mods & MOD_MASK_SHIFT) != 0) { keycode = KC_SPC; }
} else if (!(KC_A <= keycode && keycode <= KC_Z)) {
if (keycode == KC_BSPC) {
// Remove last character from the buffer.
// Remove last character from buffer
if (spellcheck_buffer_size > 0) { --spellcheck_buffer_size; }
return true;
} else if (KC_1 <= keycode && keycode <= KC_SLSH && keycode != KC_ESC) {
// Set a word boundary if space, period, digit, etc. is pressed.
// Behave more conservatively for the enter key. Reset, so that enter
// can't be used on a word ending.
if (keycode == KC_ENT) { spellcheck_buffer_size = 0; }
keycode = KC_SPC;
} else {
// Clear state if some other non-alpha key is pressed.
spellcheck_buffer_size = 0;
return true;
}
}
uint8_t keycode_type = keycode_type_en(mods, keycode);
switch(keycode_type) {
// No modifications are needed if this is a regular word character
case SPELLCHECK_WORD:
// If the buffer is full, rotate it to discard the oldest character.
if (spellcheck_buffer_size >= SPELLCHECK_BUFFER_MAX) {
memmove(spellcheck_buffer, spellcheck_buffer + 1, SPELLCHECK_BUFFER_MAX - 1);
spellcheck_buffer_size = SPELLCHECK_BUFFER_MAX - 1;
}
// NOTE: `keycode` must be a basic keycode (0-255) by this point.
// A to Z keycodes
if (keycode >= 0x04 && keycode <= 0x1D) {
spellcheck_buffer[spellcheck_buffer_size++] = (uint8_t) keycode;
} else if (spellcheck_buffer_size > 0) {
return true;
case SPELLCHECK_SPACE:
// If this is a word break, send word and reset buffer
if (spellcheck_buffer_size > 0) {
hid_send_word();
spellcheck_buffer_size = 0;
}
return true;
case SPELLCHECK_NEITHER:
spellcheck_buffer_size = 0;
return true;
}
return true;
}