From 8c896d603437bc4f59bad94a7ffb43559bf02db1 Mon Sep 17 00:00:00 2001 From: Chris Zervakis Date: Mon, 14 Oct 2024 10:07:26 +0300 Subject: [PATCH] Create a usable keymap, with HRMs (experimental) --- .../keebio/iris_ce/keymaps/chzerv/config.h | 30 ++ .../keymaps/chzerv/features/achordion.c | 384 ++++++++++++++++++ .../keymaps/chzerv/features/achordion.h | 194 +++++++++ .../keymaps/chzerv/features/autocorrection.c | 208 ++++++++++ .../keymaps/chzerv/features/autocorrection.h | 115 ++++++ .../chzerv/features/autocorrection_data.h | 169 ++++++++ .../keymaps/chzerv/features/select_word.c | 146 +++++++ .../keymaps/chzerv/features/select_word.h | 66 +++ .../keebio/iris_ce/keymaps/chzerv/keymap.c | 246 +++++++++++ .../keebio/iris_ce/keymaps/chzerv/rules.mk | 15 + 10 files changed, 1573 insertions(+) create mode 100644 keyboards/keebio/iris_ce/keymaps/chzerv/config.h create mode 100644 keyboards/keebio/iris_ce/keymaps/chzerv/features/achordion.c create mode 100644 keyboards/keebio/iris_ce/keymaps/chzerv/features/achordion.h create mode 100644 keyboards/keebio/iris_ce/keymaps/chzerv/features/autocorrection.c create mode 100644 keyboards/keebio/iris_ce/keymaps/chzerv/features/autocorrection.h create mode 100644 keyboards/keebio/iris_ce/keymaps/chzerv/features/autocorrection_data.h create mode 100644 keyboards/keebio/iris_ce/keymaps/chzerv/features/select_word.c create mode 100644 keyboards/keebio/iris_ce/keymaps/chzerv/features/select_word.h create mode 100644 keyboards/keebio/iris_ce/keymaps/chzerv/keymap.c create mode 100644 keyboards/keebio/iris_ce/keymaps/chzerv/rules.mk diff --git a/keyboards/keebio/iris_ce/keymaps/chzerv/config.h b/keyboards/keebio/iris_ce/keymaps/chzerv/config.h new file mode 100644 index 00000000..0db09cd6 --- /dev/null +++ b/keyboards/keebio/iris_ce/keymaps/chzerv/config.h @@ -0,0 +1,30 @@ +#pragma once + +// Many settings are taken from https://github.com/getreuer/qmk-keymap/ + +#define TAPPING_TOGGLE 2 +#define SELECT_WORD_TIMEOUT 2000 // When idle, clear state after 2 seconds. + +// Slow down the rate at which macros are sent +#define TAP_CODE_DELAY 5 + +// +// Tap/hold configuration for home row mods +// +#define TAPPING_TERM 170 +#define TAPPING_TERM_PER_KEY +#define PERMISSIVE_HOLD +#define QUICK_TAP_TERM_PER_KEY + +// Enable streak detection for Achorion +#define ACHORDION_STREAK + +// +// RGB +// + +// Turn RGB off when PC goes on sleep +#define RGBLIGHT_SLEEP + +// the modes we do use for lights +#define RGBLIGHT_EFFECT_BREATHING diff --git a/keyboards/keebio/iris_ce/keymaps/chzerv/features/achordion.c b/keyboards/keebio/iris_ce/keymaps/chzerv/features/achordion.c new file mode 100644 index 00000000..93f228f5 --- /dev/null +++ b/keyboards/keebio/iris_ce/keymaps/chzerv/features/achordion.c @@ -0,0 +1,384 @@ +// Copyright 2022-2024 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +/** + * @file achordion.c + * @brief Achordion implementation + * + * For full documentation, see + * + */ + +#include "achordion.h" + +#if !defined(IS_QK_MOD_TAP) +// Attempt to detect out-of-date QMK installation, which would fail with +// implicit-function-declaration errors in the code below. +#error "achordion: QMK version is too old to build. Please update QMK." +#else + +// Copy of the `record` and `keycode` args for the current active tap-hold key. +static keyrecord_t tap_hold_record; +static uint16_t tap_hold_keycode = KC_NO; +// Timeout timer. When it expires, the key is considered held. +static uint16_t hold_timer = 0; +// Eagerly applied mods, if any. +static uint8_t eager_mods = 0; +// Flag to determine whether another key is pressed within the timeout. +static bool pressed_another_key_before_release = false; + +#ifdef ACHORDION_STREAK +// Timer for typing streak +static uint16_t streak_timer = 0; +#else +// When disabled, is_streak is never true +#define is_streak false +#endif + +// Achordion's current state. +enum { + // A tap-hold key is pressed, but hasn't yet been settled as tapped or held. + STATE_UNSETTLED, + // Achordion is inactive. + STATE_RELEASED, + // Active tap-hold key has been settled as tapped. + STATE_TAPPING, + // Active tap-hold key has been settled as held. + STATE_HOLDING, + // This state is set while calling `process_record()`, which will recursively + // call `process_achordion()`. This state is checked so that we don't process + // events generated by Achordion and potentially create an infinite loop. + STATE_RECURSING, +}; +static uint8_t achordion_state = STATE_RELEASED; + +#ifdef ACHORDION_STREAK +static void update_streak_timer(uint16_t keycode, keyrecord_t* record) { + if (achordion_streak_continue(keycode)) { + // We use 0 to represent an unset timer, so `| 1` to force a nonzero value. + streak_timer = record->event.time | 1; + } else { + streak_timer = 0; + } +} +#endif + +// Presses or releases eager_mods through process_action(), which skips the +// usual event handling pipeline. The action is considered as a mod-tap hold or +// release, with Retro Tapping if enabled. +static void process_eager_mods_action(void) { + action_t action; + action.code = ACTION_MODS_TAP_KEY( + eager_mods, QK_MOD_TAP_GET_TAP_KEYCODE(tap_hold_keycode)); + process_action(&tap_hold_record, action); +} + +// Calls `process_record()` with state set to RECURSING. +static void recursively_process_record(keyrecord_t* record, uint8_t state) { + achordion_state = STATE_RECURSING; +#if defined(POINTING_DEVICE_ENABLE) && defined(POINTING_DEVICE_AUTO_MOUSE_ENABLE) + int8_t mouse_key_tracker = get_auto_mouse_key_tracker(); +#endif + process_record(record); +#if defined(POINTING_DEVICE_ENABLE) && defined(POINTING_DEVICE_AUTO_MOUSE_ENABLE) + set_auto_mouse_key_tracker(mouse_key_tracker); +#endif + achordion_state = state; +} + +// Sends hold press event and settles the active tap-hold key as held. +static void settle_as_hold(void) { + if (eager_mods) { + // If eager mods are being applied, nothing needs to be done besides + // updating the state. + dprintln("Achordion: Settled eager mod as hold."); + achordion_state = STATE_HOLDING; + } else { + // Create hold press event. + dprintln("Achordion: Plumbing hold press."); + recursively_process_record(&tap_hold_record, STATE_HOLDING); + } +} + +// Sends tap press and release and settles the active tap-hold key as tapped. +static void settle_as_tap(void) { + if (eager_mods) { // Clear eager mods if set. +#if defined(RETRO_TAPPING) || defined(RETRO_TAPPING_PER_KEY) +#ifdef DUMMY_MOD_NEUTRALIZER_KEYCODE + neutralize_flashing_modifiers(get_mods()); +#endif // DUMMY_MOD_NEUTRALIZER_KEYCODE +#endif // defined(RETRO_TAPPING) || defined(RETRO_TAPPING_PER_KEY) + tap_hold_record.event.pressed = false; + // To avoid falsely triggering Retro Tapping, process eager mods release as + // a regular mods release rather than a mod-tap release. + action_t action; + action.code = ACTION_MODS(eager_mods); + process_action(&tap_hold_record, action); + eager_mods = 0; + } + + dprintln("Achordion: Plumbing tap press."); + tap_hold_record.event.pressed = true; + tap_hold_record.tap.count = 1; // Revise event as a tap. + tap_hold_record.tap.interrupted = true; + // Plumb tap press event. + recursively_process_record(&tap_hold_record, STATE_TAPPING); + + send_keyboard_report(); +#if TAP_CODE_DELAY > 0 + wait_ms(TAP_CODE_DELAY); +#endif // TAP_CODE_DELAY > 0 + + dprintln("Achordion: Plumbing tap release."); + tap_hold_record.event.pressed = false; + // Plumb tap release event. + recursively_process_record(&tap_hold_record, STATE_TAPPING); +} + +bool process_achordion(uint16_t keycode, keyrecord_t* record) { + // Don't process events that Achordion generated. + if (achordion_state == STATE_RECURSING) { + return true; + } + + // Determine whether the current event is for a mod-tap or layer-tap key. + const bool is_mt = IS_QK_MOD_TAP(keycode); + const bool is_tap_hold = is_mt || IS_QK_LAYER_TAP(keycode); + // Check that this is a normal key event, don't act on combos. + const bool is_key_event = IS_KEYEVENT(record->event); + + // Event while no tap-hold key is active. + if (achordion_state == STATE_RELEASED) { + if (is_tap_hold && record->tap.count == 0 && record->event.pressed && + is_key_event) { + // A tap-hold key is pressed and considered by QMK as "held". + const uint16_t timeout = achordion_timeout(keycode); + if (timeout > 0) { + achordion_state = STATE_UNSETTLED; + // Save info about this key. + tap_hold_keycode = keycode; + tap_hold_record = *record; + hold_timer = record->event.time + timeout; + pressed_another_key_before_release = false; + eager_mods = 0; + + if (is_mt) { // Apply mods immediately if they are "eager." + const uint8_t mod = mod_config(QK_MOD_TAP_GET_MODS(keycode)); + if ( +#if defined(CAPS_WORD_ENABLE) && defined(CAPS_WORD_INVERT_ON_SHIFT) + // Since eager mods bypass normal event handling, eager Shift does + // not work with CAPS_WORD_INVERT_ON_SHIFT. So if this option is + // enabled, we don't apply Shift eagerly when Caps Word is on. + !(is_caps_word_on() && (mod & MOD_LSFT) != 0) && +#endif // defined(CAPS_WORD_ENABLE) && defined(CAPS_WORD_INVERT_ON_SHIFT) + achordion_eager_mod(mod)) { + eager_mods = mod; + process_eager_mods_action(); + } + } + + dprintf("Achordion: Key 0x%04X pressed.%s\n", keycode, + eager_mods ? " Set eager mods." : ""); + return false; // Skip default handling. + } + } + +#ifdef ACHORDION_STREAK + update_streak_timer(keycode, record); +#endif + return true; // Otherwise, continue with default handling. + } else if (record->event.pressed && tap_hold_keycode != keycode) { + // Track whether another key was pressed while using a tap-hold key. + pressed_another_key_before_release = true; + } + + // Release of the active tap-hold key. + if (keycode == tap_hold_keycode && !record->event.pressed) { + if (eager_mods) { + dprintln("Achordion: Key released. Clearing eager mods."); + tap_hold_record.event.pressed = false; + process_eager_mods_action(); + } else if (achordion_state == STATE_HOLDING) { + dprintln("Achordion: Key released. Plumbing hold release."); + tap_hold_record.event.pressed = false; + // Plumb hold release event. + recursively_process_record(&tap_hold_record, STATE_RELEASED); + } else if (!pressed_another_key_before_release) { + // No other key was pressed between the press and release of the tap-hold + // key, plumb a hold press and then a release. + dprintln("Achordion: Key released. Plumbing hold press and release."); + recursively_process_record(&tap_hold_record, STATE_HOLDING); + tap_hold_record.event.pressed = false; + recursively_process_record(&tap_hold_record, STATE_RELEASED); + } else { + dprintln("Achordion: Key released."); + } + + achordion_state = STATE_RELEASED; + tap_hold_keycode = KC_NO; + return false; + } + + if (achordion_state == STATE_UNSETTLED && record->event.pressed) { +#ifdef ACHORDION_STREAK + const uint16_t s_timeout = + achordion_streak_chord_timeout(tap_hold_keycode, keycode); + const bool is_streak = + streak_timer && s_timeout && + !timer_expired(record->event.time, (streak_timer + s_timeout)); +#endif + + // Press event occurred on a key other than the active tap-hold key. + + // If the other key is *also* a tap-hold key and considered by QMK to be + // held, then we settle the active key as held. This way, things like + // chording multiple home row modifiers will work, but let's our logic + // consider simply a single tap-hold key as "active" at a time. + // + // Otherwise, we call `achordion_chord()` to determine whether to settle the + // tap-hold key as tapped vs. held. We implement the tap or hold by plumbing + // events back into the handling pipeline so that QMK features and other + // user code can see them. This is done by calling `process_record()`, which + // in turn calls most handlers including `process_record_user()`. + if (!is_streak && + (!is_key_event || (is_tap_hold && record->tap.count == 0) || + achordion_chord(tap_hold_keycode, &tap_hold_record, keycode, + record))) { + settle_as_hold(); + +#ifdef REPEAT_KEY_ENABLE + // Edge case involving LT + Repeat Key: in a sequence of "LT down, other + // down" where "other" is on the other layer in the same position as + // Repeat or Alternate Repeat, the repeated keycode is set instead of the + // the one on the switched-to layer. Here we correct that. + if (get_repeat_key_count() != 0 && IS_QK_LAYER_TAP(tap_hold_keycode)) { + record->keycode = KC_NO; // Forget the repeated keycode. + clear_weak_mods(); + } +#endif // REPEAT_KEY_ENABLE + } else { + settle_as_tap(); + +#ifdef ACHORDION_STREAK + update_streak_timer(keycode, record); + if (is_streak && is_key_event && is_tap_hold && record->tap.count == 0) { + // If we are in a streak and resolved the current tap-hold key as a tap + // consider the next tap-hold key as active to be resolved next. + update_streak_timer(tap_hold_keycode, &tap_hold_record); + const uint16_t timeout = achordion_timeout(keycode); + tap_hold_keycode = keycode; + tap_hold_record = *record; + hold_timer = record->event.time + timeout; + achordion_state = STATE_UNSETTLED; + pressed_another_key_before_release = false; + return false; + } +#endif + } + + recursively_process_record(record, achordion_state); // Re-process event. + return false; // Block the original event. + } + +#ifdef ACHORDION_STREAK + // update idle timer on regular keys event + update_streak_timer(keycode, record); +#endif + return true; +} + +void achordion_task(void) { + if (achordion_state == STATE_UNSETTLED && + timer_expired(timer_read(), hold_timer)) { + settle_as_hold(); // Timeout expired, settle the key as held. + } + +#ifdef ACHORDION_STREAK +#define MAX_STREAK_TIMEOUT 800 + if (streak_timer && + timer_expired(timer_read(), (streak_timer + MAX_STREAK_TIMEOUT))) { + streak_timer = 0; // Expired. + } +#endif +} + +// Returns true if `pos` on the left hand of the keyboard, false if right. +static bool on_left_hand(keypos_t pos) { +#ifdef SPLIT_KEYBOARD + return pos.row < MATRIX_ROWS / 2; +#else + return (MATRIX_COLS > MATRIX_ROWS) ? pos.col < MATRIX_COLS / 2 + : pos.row < MATRIX_ROWS / 2; +#endif +} + +bool achordion_opposite_hands(const keyrecord_t* tap_hold_record, + const keyrecord_t* other_record) { + return on_left_hand(tap_hold_record->event.key) != + on_left_hand(other_record->event.key); +} + +// By default, use the BILATERAL_COMBINATIONS rule to consider the tap-hold key +// "held" only when it and the other key are on opposite hands. +__attribute__((weak)) bool achordion_chord(uint16_t tap_hold_keycode, + keyrecord_t* tap_hold_record, + uint16_t other_keycode, + keyrecord_t* other_record) { + return achordion_opposite_hands(tap_hold_record, other_record); +} + +// By default, the timeout is 1000 ms for all keys. +__attribute__((weak)) uint16_t achordion_timeout(uint16_t tap_hold_keycode) { + return 1000; +} + +// By default, Shift and Ctrl mods are eager, and Alt and GUI are not. +__attribute__((weak)) bool achordion_eager_mod(uint8_t mod) { + return (mod & (MOD_LALT | MOD_LGUI)) == 0; +} + +#ifdef ACHORDION_STREAK +__attribute__((weak)) bool achordion_streak_continue(uint16_t keycode) { + // If any mods other than shift or AltGr are held, don't continue the streak + if (get_mods() & (MOD_MASK_CG | MOD_BIT_LALT)) return false; + // This function doesn't get called for holds, so convert to tap version of + // keycodes + if (IS_QK_MOD_TAP(keycode)) keycode = QK_MOD_TAP_GET_TAP_KEYCODE(keycode); + if (IS_QK_LAYER_TAP(keycode)) keycode = QK_LAYER_TAP_GET_TAP_KEYCODE(keycode); + // Regular letters and punctuation continue the streak. + if (keycode >= KC_A && keycode <= KC_Z) return true; + switch (keycode) { + case KC_DOT: + case KC_COMMA: + case KC_QUOTE: + case KC_SPACE: + return true; + } + // All other keys end the streak + return false; +} + +__attribute__((weak)) uint16_t achordion_streak_chord_timeout( + uint16_t tap_hold_keycode, uint16_t next_keycode) { + return achordion_streak_timeout(tap_hold_keycode); +} + +__attribute__((weak)) uint16_t +achordion_streak_timeout(uint16_t tap_hold_keycode) { + return 200; +} +#endif + +#endif // version check + diff --git a/keyboards/keebio/iris_ce/keymaps/chzerv/features/achordion.h b/keyboards/keebio/iris_ce/keymaps/chzerv/features/achordion.h new file mode 100644 index 00000000..a96987bc --- /dev/null +++ b/keyboards/keebio/iris_ce/keymaps/chzerv/features/achordion.h @@ -0,0 +1,194 @@ +// Copyright 2022-2024 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +/** + * @file achordion.h + * @brief Achordion: Customizing the tap-hold decision. + * + * Overview + * -------- + * + * This library customizes when tap-hold keys are considered held vs. tapped + * based on the next pressed key, like Manna Harbour's Bilateral Combinations or + * ZMK's positional hold. The library works on top of QMK's existing tap-hold + * implementation. You define mod-tap and layer-tap keys as usual and use + * Achordion to fine-tune the behavior. + * + * When QMK settles a tap-hold key as held, Achordion intercepts the event. + * Achordion then revises the event as a tap or passes it along as a hold: + * + * * Chord condition: On the next key press, a customizable `achordion_chord()` + * function is called, which takes the tap-hold key and the next key pressed + * as args. When the function returns true, the tap-hold key is settled as + * held, and otherwise as tapped. + * + * * Timeout: If no other key press occurs within a timeout, the tap-hold key + * is settled as held. This is customizable with `achordion_timeout()`. + * + * Achordion only changes the behavior when QMK considered the key held. It + * changes some would-be holds to taps, but no taps to holds. + * + * @note Some QMK features handle events before the point where Achordion can + * intercept them, particularly: Combos, Key Lock, and Dynamic Macros. It's + * still possible to use these features and Achordion in your keymap, but beware + * they might behave poorly when used simultaneously with tap-hold keys. + * + * + * For full documentation, see + * + */ + +#pragma once + +#include "quantum.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * Handler function for Achordion. + * + * Call this function from `process_record_user()` as + * + * #include "features/achordion.h" + * + * bool process_record_user(uint16_t keycode, keyrecord_t* record) { + * if (!process_achordion(keycode, record)) { return false; } + * // Your macros... + * return true; + * } + */ +bool process_achordion(uint16_t keycode, keyrecord_t* record); + +/** + * Matrix task function for Achordion. + * + * Call this function from `matrix_scan_user()` as + * + * void matrix_scan_user(void) { + * achordion_task(); + * } + */ +void achordion_task(void); + +/** + * Optional callback to customize which key chords are considered "held". + * + * In your keymap.c, define the callback + * + * bool achordion_chord(uint16_t tap_hold_keycode, + * keyrecord_t* tap_hold_record, + * uint16_t other_keycode, + * keyrecord_t* other_record) { + * // Conditions... + * } + * + * This callback is called if while `tap_hold_keycode` is pressed, + * `other_keycode` is pressed. Return true if the tap-hold key should be + * considered held, or false to consider it tapped. + * + * @param tap_hold_keycode Keycode of the tap-hold key. + * @param tap_hold_record keyrecord_t from the tap-hold press event. + * @param other_keycode Keycode of the other key. + * @param other_record keyrecord_t from the other key's press event. + * @return True if the tap-hold key should be considered held. + */ +bool achordion_chord(uint16_t tap_hold_keycode, keyrecord_t* tap_hold_record, + uint16_t other_keycode, keyrecord_t* other_record); + +/** + * Optional callback to define a timeout duration per keycode. + * + * In your keymap.c, define the callback + * + * uint16_t achordion_timeout(uint16_t tap_hold_keycode) { + * // ... + * } + * + * The callback determines Achordion's timeout duration for `tap_hold_keycode` + * in units of milliseconds. The timeout be in the range 0 to 32767 ms (upper + * bound is due to 16-bit timer limitations). Use a timeout of 0 to bypass + * Achordion. + * + * @param tap_hold_keycode Keycode of the tap-hold key. + * @return Timeout duration in milliseconds in the range 0 to 32767. + */ +uint16_t achordion_timeout(uint16_t tap_hold_keycode); + +/** + * Optional callback defining which mods are "eagerly" applied. + * + * This callback defines which mods are "eagerly" applied while a mod-tap + * key is still being settled. This is helpful to reduce delay particularly when + * using mod-tap keys with an external mouse. + * + * Define this callback in your keymap.c. The default callback is eager for + * Shift and Ctrl, and not for Alt and GUI: + * + * bool achordion_eager_mod(uint8_t mod) { + * return (mod & (MOD_LALT | MOD_LGUI)) == 0; + * } + * + * @note `mod` should be compared with `MOD_` prefixed codes, not `KC_` codes, + * described at . + * + * @param mod Modifier `MOD_` code. + * @return True if the modifier should be eagerly applied. + */ +bool achordion_eager_mod(uint8_t mod); + +/** + * Returns true if the args come from keys on opposite hands. + * + * @param tap_hold_record keyrecord_t from the tap-hold key's event. + * @param other_record keyrecord_t from the other key's event. + * @return True if the keys are on opposite hands. + */ +bool achordion_opposite_hands(const keyrecord_t* tap_hold_record, + const keyrecord_t* other_record); + +/** + * Suppress tap-hold mods within a *typing streak* by defining + * ACHORDION_STREAK. This can help preventing accidental mod + * activation when performing a fast tapping sequence. + * This is inspired by + * https://sunaku.github.io/home-row-mods.html#typing-streaks + * + * Enable with: + * + * #define ACHORDION_STREAK + * + * Adjust the maximum time between key events before modifiers can be enabled + * by defining the following callback in your keymap.c: + * + * uint16_t achordion_streak_chord_timeout( + * uint16_t tap_hold_keycode, uint16_t next_keycode) { + * return 200; // Default of 200 ms. + * } + */ +#ifdef ACHORDION_STREAK +uint16_t achordion_streak_chord_timeout(uint16_t tap_hold_keycode, + uint16_t next_keycode); + +bool achordion_streak_continue(uint16_t keycode); + +/** @deprecated Use `achordion_streak_chord_timeout()` instead. */ +uint16_t achordion_streak_timeout(uint16_t tap_hold_keycode); +#endif + +#ifdef __cplusplus +} +#endif + diff --git a/keyboards/keebio/iris_ce/keymaps/chzerv/features/autocorrection.c b/keyboards/keebio/iris_ce/keymaps/chzerv/features/autocorrection.c new file mode 100644 index 00000000..f3ca964f --- /dev/null +++ b/keyboards/keebio/iris_ce/keymaps/chzerv/features/autocorrection.c @@ -0,0 +1,208 @@ +// Copyright 2021-2023 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +/** + * @file autocorrection.c + * @brief Autocorrection implementation + * + * For full documentation, see + * + */ + +#include "autocorrection.h" + +#include + +#include "autocorrection_data.h" + +#pragma message \ + "Autocorrect is now a core QMK feature! To use it, update your QMK set up and see https://docs.qmk.fm/features/autocorrect" + +#if AUTOCORRECTION_MIN_LENGTH < 4 +// Odd output or hard locks on the board have been observed when the min typo +// length is 3 or lower (https://github.com/getreuer/qmk-keymap/issues/2). +// Additionally, autocorrection entries for short typos are more likely to false +// trigger, so it is suggested that typos be at least 5 characters. +#error "Min typo length is less than 4. Autocorrection may behave poorly." +#endif + +bool process_autocorrection(uint16_t keycode, keyrecord_t* record) { + static uint8_t typo_buffer[AUTOCORRECTION_MAX_LENGTH] = {0}; + static uint8_t typo_buffer_size = 0; + + // Ignore key release; we only process key presses. + if (!record->event.pressed) { + return true; + } + +#ifndef NO_ACTION_ONESHOT + const uint8_t mods = get_mods() | get_oneshot_mods(); +#else + const uint8_t mods = get_mods(); +#endif // NO_ACTION_ONESHOT + // Disable autocorrection while a mod other than shift is active. + if ((mods & ~MOD_MASK_SHIFT) != 0) { + typo_buffer_size = 0; + return true; + } + + // The following switch cases address various kinds of keycodes. This logic is + // split over two switches rather than merged into one. The first switch may + // extract a basic keycode which is then further handled by the second switch, + // e.g. a layer-tap key with Caps Lock `LT(layer, KC_CAPS)`. + switch (keycode) { +#ifndef NO_ACTION_TAPPING + case QK_MOD_TAP ... QK_MOD_TAP_MAX: // Tap-hold keys. +#ifndef NO_ACTION_LAYER + case QK_LAYER_TAP ... QK_LAYER_TAP_MAX: +#endif // NO_ACTION_LAYER + // Ignore when tap-hold keys are held. + if (record->tap.count == 0) { + return true; + } + // Otherwise when tapped, get the basic keycode. + // Fallthrough intended. +#endif // NO_ACTION_TAPPING + + // Handle shifted keys, e.g. symbols like KC_EXLM = S(KC_1). + case QK_LSFT ... QK_LSFT + 255: + case QK_RSFT ... QK_RSFT + 255: + keycode = QK_MODS_GET_BASIC_KEYCODE(keycode); + break; + + // NOTE: Space Cadet keys expose no info to check whether they are being + // tapped vs. held. This makes autocorrection ambiguous, e.g. KC_LCPO + // might be '(', which we would treat as a word break, or it might be + // shift, which we would treat as having no effect. To behave cautiously, + // we allow Space Cadet keycodes to fall to the logic below and clear + // autocorrection state. + } + + switch (keycode) { + // Ignore shifts, Caps Lock, one-shot mods, and layer switch keys. + case KC_NO: + case KC_LSFT: + case KC_RSFT: + case KC_CAPS: + case QK_ONE_SHOT_MOD ... QK_ONE_SHOT_MOD_MAX: + case QK_TO ... QK_TO_MAX: + case QK_MOMENTARY ... QK_MOMENTARY_MAX: + case QK_DEF_LAYER ... QK_DEF_LAYER_MAX: + case QK_TOGGLE_LAYER ... QK_TOGGLE_LAYER_MAX: + case QK_ONE_SHOT_LAYER ... QK_ONE_SHOT_LAYER_MAX: + case QK_LAYER_TAP_TOGGLE ... QK_LAYER_TAP_TOGGLE_MAX: + case QK_LAYER_MOD ... QK_LAYER_MOD_MAX: + 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. + if (typo_buffer_size > 0) { + --typo_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) { + typo_buffer_size = 0; + } + keycode = KC_SPC; + } else { + // Clear state if some other non-alpha key is pressed. + typo_buffer_size = 0; + return true; + } + } + + // If the buffer is full, rotate it to discard the oldest character. + if (typo_buffer_size >= AUTOCORRECTION_MAX_LENGTH) { + memmove(typo_buffer, typo_buffer + 1, AUTOCORRECTION_MAX_LENGTH - 1); + typo_buffer_size = AUTOCORRECTION_MAX_LENGTH - 1; + } + + // Append `keycode` to the buffer. + // NOTE: `keycode` must be a basic keycode (0-255) by this point. + typo_buffer[typo_buffer_size++] = (uint8_t)keycode; + // Early return if not many characters have been buffered so far. + if (typo_buffer_size < AUTOCORRECTION_MIN_LENGTH) { + return true; + } + + // Check whether the buffer ends in a typo. This is done using a trie + // stored in `autocorrection_data`. + uint16_t state = 0; + uint8_t code = pgm_read_byte(autocorrection_data + state); + for (int i = typo_buffer_size - 1; i >= 0; --i) { + const uint8_t key_i = typo_buffer[i]; + + if (code & 64) { // Check for match in node with multiple children. + code &= 63; + for (; code != key_i; + code = pgm_read_byte(autocorrection_data + (state += 3))) { + if (!code) { + return true; + } + } + + // Follow link to child node. + state = (uint16_t)((uint_fast16_t)pgm_read_byte(autocorrection_data + + state + 1) | + (uint_fast16_t)pgm_read_byte(autocorrection_data + + state + 2) + << 8); + // Otherwise check for match in node with a single child. + } else if (code != key_i) { + return true; + } else if (!(code = pgm_read_byte(autocorrection_data + (++state)))) { + ++state; + } + + // Stop if `state` becomes an invalid index. This should not normally + // happen, it is a safeguard in case of a bug, data corruption, etc. + if (state >= sizeof(autocorrection_data)) { + return true; + } + + // Read first byte of the next node. + code = pgm_read_byte(autocorrection_data + state); + + if (code & 128) { // A typo was found! Apply autocorrection. + const int backspaces = code & 63; + for (int i = 0; i < backspaces; ++i) { + tap_code(KC_BSPC); + } + send_string_P((char const*)(autocorrection_data + state + 1)); + + if (keycode == KC_SPC) { + typo_buffer[0] = KC_SPC; + typo_buffer_size = 1; + return true; + } else { + typo_buffer_size = 0; + return false; + } + } + } + + return true; +} + diff --git a/keyboards/keebio/iris_ce/keymaps/chzerv/features/autocorrection.h b/keyboards/keebio/iris_ce/keymaps/chzerv/features/autocorrection.h new file mode 100644 index 00000000..dd9d5dc5 --- /dev/null +++ b/keyboards/keebio/iris_ce/keymaps/chzerv/features/autocorrection.h @@ -0,0 +1,115 @@ +// Copyright 2021-2022 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +/** + * @file autocorrection.h + * @brief Autocorrection on your keyboard. + * + * Overview + * -------- + * + * @note Autocorrect is now a core QMK feature! See + * + * + * Some words are more prone to typos than others. This userspace QMK library + * implements rudimentary autocorrection, automatically detecting and fixing + * some misspellings. + * + * Features: + * + * * It runs on your keyboard, so it is always active no matter what software. + * * Low resource cost. + * * It is case insensitive. + * * It works within words, useful for programming to catch typos within longer + * identifiers. + * + * Limitations: + * + * * It is limited to alphabet characters a–z, apostrophes ', and word breaks. + * I'm sorry this probably isn't useful for languages besides English. + * * It does not follow mouse or hotkey driven cursor movement. + * + * Changing the autocorrection dictionary + * -------------------------------------- + * + * The file autocorrection_data.h encodes the typos to correct. While you could + * simply use the version of this file provided above for a practical + * configuration, you can make your own to personalize the autocorrection to + * your most troublesome typos: + * + * Step 1: First, create an autocorrection dictionary autocorrection_dict.txt, + * in a form like + * + * :thier -> their + * dosen't -> doesn't + * fitler -> filter + * ouput -> output + * widht -> width + * + * For a practical 71-entry example, see autocorrection_dict.txt. And for a yet + * larger 400-entry example, see autocorrection_dict_extra.txt. + * + * The syntax is `typo -> correction`. Typos and corrections are case + * insensitive, and any whitespace before or after the typo and correction is + * ignored. The typo must be only the characters a–z, ', or the special + * character : representing a word break. The correction may have just about any + * printable ASCII characters. + * + * Step 2: Use the make_autocorrection_data.py Python script to process the + * dictionary. Put autocorrection_dict.txt in the same directory as the Python + * script and run + * + * $ python3 make_autocorrection_data.py + * Processed 71 autocorrection entries to table with 1120 bytes. + * + * The script arranges the entries in autocorrection_dict.txt into a trie and + * generates autocorrection_data.h with the serialized trie embedded as an + * array. The .h file will be written in the same directory. + * + * Step 3: Finally, recompile and flash your keymap. + * + * For full documentation, see + * + * + * @author Pascal Getreuer + */ + +#pragma once + +#include "quantum.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * Handler function for autocorrection. + * + * Call this function in keymap.c from `process_record_user()` like + * + * #include "features/autocorrection.h" + * + * bool process_record_user(uint16_t keycode, keyrecord_t* record) { + * if (!process_autocorrection(keycode, record)) { return false; } + * // Your macros... + * + * return true; + * } + */ +bool process_autocorrection(uint16_t keycode, keyrecord_t* record); + +#ifdef __cplusplus +} +#endif + diff --git a/keyboards/keebio/iris_ce/keymaps/chzerv/features/autocorrection_data.h b/keyboards/keebio/iris_ce/keymaps/chzerv/features/autocorrection_data.h new file mode 100644 index 00000000..21efc570 --- /dev/null +++ b/keyboards/keebio/iris_ce/keymaps/chzerv/features/autocorrection_data.h @@ -0,0 +1,169 @@ +// Copyright 2021-2022 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// Generated code. + +// Autocorrection dictionary (71 entries): +// :guage -> gauge +// :the:the: -> the +// :thier -> their +// :ture -> true +// accomodate -> accommodate +// acommodate -> accommodate +// aparent -> apparent +// aparrent -> apparent +// apparant -> apparent +// apparrent -> apparent +// aquire -> acquire +// becuase -> because +// cauhgt -> caught +// cheif -> chief +// choosen -> chosen +// cieling -> ceiling +// collegue -> colleague +// concensus -> consensus +// contians -> contains +// cosnt -> const +// dervied -> derived +// dosen't -> doesn't +// fales -> false +// fasle -> false +// fitler -> filter +// flase -> false +// foward -> forward +// frequecy -> frequency +// gaurantee -> guarantee +// guaratee -> guarantee +// heigth -> height +// heirarchy -> hierarchy +// inclued -> include +// interator -> iterator +// intput -> input +// invliad -> invalid +// lenght -> length +// liasion -> liaison +// libary -> library +// listner -> listener +// looses: -> loses +// looup -> lookup +// manefist -> manifest +// namesapce -> namespace +// namespcae -> namespace +// occassion -> occasion +// occured -> occurred +// ouptut -> output +// ouput -> output +// overide -> override +// postion -> position +// priviledge -> privilege +// psuedo -> pseudo +// recieve -> receive +// refered -> referred +// relevent -> relevant +// repitition -> repetition +// retrun -> return +// retun -> return +// reuslt -> result +// reutrn -> return +// saftey -> safety +// seperate -> separate +// singed -> signed +// stirng -> string +// strign -> string +// swithc -> switch +// swtich -> switch +// thresold -> threshold +// udpate -> update +// widht -> width + +#define AUTOCORRECTION_MIN_LENGTH 5 // ":ture" +#define AUTOCORRECTION_MAX_LENGTH 10 // "accomodate" + +static const uint8_t autocorrection_data[1120] PROGMEM = { + 108, 43, 0, 6, 71, 0, 7, 81, 0, 8, 199, 0, 9, 240, 1, + 10, 250, 1, 11, 26, 2, 17, 53, 2, 18, 190, 2, 19, 202, 2, + 21, 212, 2, 22, 20, 3, 23, 67, 3, 28, 32, 4, 0, 72, 50, + 0, 22, 60, 0, 0, 11, 23, 44, 8, 11, 23, 44, 0, 132, 0, + 8, 22, 18, 18, 15, 0, 132, 115, 101, 115, 0, 11, 23, 12, 26, + 22, 0, 129, 99, 104, 0, 68, 94, 0, 8, 106, 0, 15, 174, 0, + 21, 187, 0, 0, 12, 15, 25, 17, 12, 0, 131, 97, 108, 105, 100, + 0, 74, 119, 0, 12, 129, 0, 21, 140, 0, 24, 165, 0, 0, 17, + 12, 22, 0, 131, 103, 110, 101, 100, 0, 25, 21, 8, 7, 0, 131, + 105, 118, 101, 100, 0, 72, 147, 0, 24, 156, 0, 0, 9, 8, 21, + 0, 129, 114, 101, 100, 0, 6, 6, 18, 0, 129, 114, 101, 100, 0, + 15, 6, 17, 12, 0, 129, 100, 101, 0, 18, 22, 8, 21, 11, 23, + 0, 130, 104, 111, 108, 100, 0, 4, 26, 18, 9, 0, 131, 114, 119, + 97, 114, 100, 0, 68, 233, 0, 6, 246, 0, 7, 4, 1, 8, 16, + 1, 10, 52, 1, 15, 81, 1, 21, 90, 1, 22, 117, 1, 23, 144, + 1, 24, 215, 1, 25, 228, 1, 0, 6, 19, 22, 8, 16, 4, 17, + 0, 130, 97, 99, 101, 0, 19, 4, 22, 8, 16, 4, 17, 0, 131, + 112, 97, 99, 101, 0, 12, 21, 8, 25, 18, 0, 130, 114, 105, 100, + 101, 0, 23, 0, 68, 25, 1, 17, 36, 1, 0, 21, 4, 24, 10, + 0, 130, 110, 116, 101, 101, 0, 4, 21, 24, 4, 10, 0, 135, 117, + 97, 114, 97, 110, 116, 101, 101, 0, 68, 59, 1, 7, 69, 1, 0, + 24, 10, 44, 0, 131, 97, 117, 103, 101, 0, 8, 15, 12, 25, 12, + 21, 19, 0, 130, 103, 101, 0, 22, 4, 9, 0, 130, 108, 115, 101, + 0, 76, 97, 1, 24, 109, 1, 0, 24, 20, 4, 0, 132, 99, 113, + 117, 105, 114, 101, 0, 23, 44, 0, 130, 114, 117, 101, 0, 4, 0, + 79, 126, 1, 24, 134, 1, 0, 9, 0, 131, 97, 108, 115, 101, 0, + 6, 8, 5, 0, 131, 97, 117, 115, 101, 0, 4, 0, 71, 156, 1, + 19, 193, 1, 21, 203, 1, 0, 18, 16, 0, 80, 166, 1, 18, 181, + 1, 0, 18, 6, 4, 0, 135, 99, 111, 109, 109, 111, 100, 97, 116, + 101, 0, 6, 6, 4, 0, 132, 109, 111, 100, 97, 116, 101, 0, 7, + 24, 0, 132, 112, 100, 97, 116, 101, 0, 8, 19, 8, 22, 0, 132, + 97, 114, 97, 116, 101, 0, 10, 8, 15, 15, 18, 6, 0, 130, 97, + 103, 117, 101, 0, 8, 12, 6, 8, 21, 0, 131, 101, 105, 118, 101, + 0, 12, 8, 11, 6, 0, 130, 105, 101, 102, 0, 17, 0, 76, 3, + 2, 21, 16, 2, 0, 15, 8, 12, 6, 0, 133, 101, 105, 108, 105, + 110, 103, 0, 12, 23, 22, 0, 131, 114, 105, 110, 103, 0, 70, 33, + 2, 23, 44, 2, 0, 12, 23, 26, 22, 0, 131, 105, 116, 99, 104, + 0, 10, 12, 8, 11, 0, 129, 104, 116, 0, 72, 69, 2, 10, 80, + 2, 18, 89, 2, 21, 156, 2, 24, 167, 2, 0, 22, 18, 18, 11, + 6, 0, 131, 115, 101, 110, 0, 12, 21, 23, 22, 0, 129, 110, 103, + 0, 12, 0, 86, 98, 2, 23, 124, 2, 0, 68, 105, 2, 22, 114, + 2, 0, 12, 15, 0, 131, 105, 115, 111, 110, 0, 4, 6, 6, 18, + 0, 131, 105, 111, 110, 0, 76, 131, 2, 22, 146, 2, 0, 23, 12, + 19, 8, 21, 0, 134, 101, 116, 105, 116, 105, 111, 110, 0, 18, 19, + 0, 131, 105, 116, 105, 111, 110, 0, 23, 24, 8, 21, 0, 131, 116, + 117, 114, 110, 0, 85, 174, 2, 23, 183, 2, 0, 23, 8, 21, 0, + 130, 117, 114, 110, 0, 8, 21, 0, 128, 114, 110, 0, 7, 8, 24, + 22, 19, 0, 131, 101, 117, 100, 111, 0, 24, 18, 18, 15, 0, 129, + 107, 117, 112, 0, 72, 219, 2, 18, 3, 3, 0, 76, 229, 2, 15, + 238, 2, 17, 248, 2, 0, 11, 23, 44, 0, 130, 101, 105, 114, 0, + 23, 12, 9, 0, 131, 108, 116, 101, 114, 0, 23, 22, 12, 15, 0, + 130, 101, 110, 101, 114, 0, 23, 4, 21, 8, 23, 17, 12, 0, 135, + 116, 101, 114, 97, 116, 111, 114, 0, 72, 30, 3, 17, 38, 3, 24, + 51, 3, 0, 15, 4, 9, 0, 129, 115, 101, 0, 4, 12, 23, 17, + 18, 6, 0, 131, 97, 105, 110, 115, 0, 22, 17, 8, 6, 17, 18, + 6, 0, 133, 115, 101, 110, 115, 117, 115, 0, 116, 89, 3, 10, 102, + 3, 11, 112, 3, 15, 134, 3, 17, 145, 3, 22, 234, 3, 24, 248, + 3, 0, 17, 8, 22, 18, 7, 0, 132, 101, 115, 110, 39, 116, 0, + 11, 24, 4, 6, 0, 130, 103, 104, 116, 0, 71, 119, 3, 10, 126, + 3, 0, 12, 26, 0, 129, 116, 104, 0, 17, 8, 15, 0, 129, 116, + 104, 0, 22, 24, 8, 21, 0, 131, 115, 117, 108, 116, 0, 68, 155, + 3, 8, 166, 3, 22, 226, 3, 0, 21, 4, 19, 19, 4, 0, 130, + 101, 110, 116, 0, 85, 173, 3, 25, 216, 3, 0, 68, 180, 3, 21, + 191, 3, 0, 19, 4, 0, 132, 112, 97, 114, 101, 110, 116, 0, 4, + 19, 0, 68, 201, 3, 19, 209, 3, 0, 133, 112, 97, 114, 101, 110, + 116, 0, 4, 0, 131, 101, 110, 116, 0, 8, 15, 8, 21, 0, 130, + 97, 110, 116, 0, 18, 6, 0, 130, 110, 115, 116, 0, 12, 9, 8, + 17, 4, 16, 0, 132, 105, 102, 101, 115, 116, 0, 83, 255, 3, 23, + 22, 4, 0, 87, 6, 4, 24, 14, 4, 0, 17, 12, 0, 131, 112, + 117, 116, 0, 18, 0, 130, 116, 112, 117, 116, 0, 19, 24, 18, 0, + 131, 116, 112, 117, 116, 0, 70, 45, 4, 8, 57, 4, 11, 67, 4, + 21, 85, 4, 0, 8, 24, 20, 8, 21, 9, 0, 129, 110, 99, 121, + 0, 23, 9, 4, 22, 0, 130, 101, 116, 121, 0, 6, 21, 4, 21, + 12, 8, 11, 0, 135, 105, 101, 114, 97, 114, 99, 104, 121, 0, 4, + 5, 12, 15, 0, 130, 114, 97, 114, 121, 0}; + diff --git a/keyboards/keebio/iris_ce/keymaps/chzerv/features/select_word.c b/keyboards/keebio/iris_ce/keymaps/chzerv/features/select_word.c new file mode 100644 index 00000000..a1bf939b --- /dev/null +++ b/keyboards/keebio/iris_ce/keymaps/chzerv/features/select_word.c @@ -0,0 +1,146 @@ +// Copyright 2021-2023 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +/** + * @file select_word.c + * @brief Select word implementation + * + * For full documentation, see + * + */ + +#include "select_word.h" + +// Mac users, uncomment this line: +// #define MAC_HOTKEYS + +// clang-format off +enum { + STATE_NONE, // No selection. + STATE_SELECTED, // Macro released with something selected. + STATE_WORD, // Macro held with word(s) selected. + STATE_FIRST_LINE, // Macro held with one line selected. + STATE_LINE // Macro held with multiple lines selected. +}; +// clang-format on +static uint8_t state = STATE_NONE; + +// Idle timeout timer to disable Select Word after a period of inactivity. +#if SELECT_WORD_TIMEOUT > 0 +static uint16_t idle_timer = 0; + +void select_word_task(void) { + if (state && timer_expired(timer_read(), idle_timer)) { + state = STATE_NONE; + } +} +#endif // SELECT_WORD_TIMEOUT > 0 + +bool process_select_word(uint16_t keycode, keyrecord_t* record, + uint16_t sel_keycode) { + if (keycode == KC_LSFT || keycode == KC_RSFT) { + return true; + } + +#if SELECT_WORD_TIMEOUT > 0 + idle_timer = record->event.time + SELECT_WORD_TIMEOUT; +#endif // SELECT_WORD_TIMEOUT > 0 + + if (keycode == sel_keycode && record->event.pressed) { // On key press. + const uint8_t mods = get_mods(); +#ifndef NO_ACTION_ONESHOT + const bool shifted = (mods | get_oneshot_mods()) & MOD_MASK_SHIFT; + clear_oneshot_mods(); +#else + const bool shifted = mods & MOD_MASK_SHIFT; +#endif // NO_ACTION_ONESHOT + + if (!shifted) { // Select word. +#ifdef MAC_HOTKEYS + set_mods(MOD_BIT(KC_LALT)); // Hold Left Alt (Option). +#else + set_mods(MOD_BIT(KC_LCTL)); // Hold Left Ctrl. +#endif // MAC_HOTKEYS + if (state == STATE_NONE) { + // On first use, tap Ctrl+Right then Ctrl+Left (or with Alt on Mac) to + // ensure the cursor is positioned at the beginning of the word. + send_keyboard_report(); + tap_code(KC_RGHT); + tap_code(KC_LEFT); + } + register_mods(MOD_BIT(KC_LSFT)); + register_code(KC_RGHT); + state = STATE_WORD; + } else { // Select line. + if (state == STATE_NONE) { +#ifdef MAC_HOTKEYS + // Tap GUI (Command) + Left, then Shift + GUI + Right. + set_mods(MOD_BIT(KC_LGUI)); + send_keyboard_report(); + tap_code(KC_LEFT); + register_mods(MOD_BIT(KC_LSFT)); + tap_code(KC_RGHT); +#else + // Tap Home, then Shift + End. + clear_mods(); + send_keyboard_report(); + tap_code(KC_HOME); + register_mods(MOD_BIT(KC_LSFT)); + tap_code(KC_END); +#endif // MAC_HOTKEYS + set_mods(mods); + state = STATE_FIRST_LINE; + } else { + register_code(KC_DOWN); + state = STATE_LINE; + } + } + return false; + } + + // `sel_keycode` was released, or another key was pressed. + switch (state) { + case STATE_WORD: + unregister_code(KC_RGHT); +#ifdef MAC_HOTKEYS + unregister_mods(MOD_BIT(KC_LSFT) | MOD_BIT(KC_LALT)); +#else + unregister_mods(MOD_BIT(KC_LSFT) | MOD_BIT(KC_LCTL)); +#endif // MAC_HOTKEYS + state = STATE_SELECTED; + break; + + case STATE_FIRST_LINE: + state = STATE_SELECTED; + break; + + case STATE_LINE: + unregister_code(KC_DOWN); + state = STATE_SELECTED; + break; + + case STATE_SELECTED: + if (keycode == KC_ESC) { + tap_code(KC_RGHT); + state = STATE_NONE; + return false; + } + // Fallthrough intended. + default: + state = STATE_NONE; + } + + return true; +} + diff --git a/keyboards/keebio/iris_ce/keymaps/chzerv/features/select_word.h b/keyboards/keebio/iris_ce/keymaps/chzerv/features/select_word.h new file mode 100644 index 00000000..82f07e2f --- /dev/null +++ b/keyboards/keebio/iris_ce/keymaps/chzerv/features/select_word.h @@ -0,0 +1,66 @@ +// Copyright 2021-2023 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +/** + * @file select_word.h + * @brief Select word/line macro. + * + * Overview + * -------- + * + * Implements a button that selects the current word, assuming conventional text + * editor hotkeys. Pressing it again extends the selection to the following + * word. The effect is similar to word selection (W) in the Kakoune editor. + * + * Pressing the button with shift selects the current line, and pressing the + * button again extends the selection to the following line. + * + * @note Note for Mac users: Windows/Linux editing hotkeys are assumed by + * default. Uncomment the `#define MAC_HOTKEYS` line in select_word.c for Mac + * hotkeys. The Mac implementation is untested, let me know if it has problems. + * + * For full documentation, see + * + */ + +#pragma once + +#include "quantum.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** Handler function for select word. */ +bool process_select_word(uint16_t keycode, keyrecord_t* record, + uint16_t sel_keycode); + +/** + * @fn select_word_task(void) + * Matrix task function for Select Word. + * + * If using `SELECT_WORD_TIMEOUT`, call this function from your + * `matrix_scan_user()` function in keymap.c. (If no timeout is set, calling + * `select_word_task()` has no effect.) + */ +#if SELECT_WORD_TIMEOUT > 0 +void select_word_task(void); +#else +static inline void select_word_task(void) {} +#endif // SELECT_WORD_TIMEOUT > 0 + +#ifdef __cplusplus +} +#endif + diff --git a/keyboards/keebio/iris_ce/keymaps/chzerv/keymap.c b/keyboards/keebio/iris_ce/keymaps/chzerv/keymap.c new file mode 100644 index 00000000..35e37da8 --- /dev/null +++ b/keyboards/keebio/iris_ce/keymaps/chzerv/keymap.c @@ -0,0 +1,246 @@ +#include QMK_KEYBOARD_H +#include "features/select_word.h" +#include "features/autocorrection.h" +#include "features/achordion.h" + +// Select word (https://getreuer.info/posts/keyboards/select-word/index.html) +enum custom_keycodes { + SELWORD = SAFE_RANGE, + UPDIR, + LITERAL, +}; + +// Layers +enum custom_layers { + _BASE, + _SYM, + _NAV, + _FUN +}; + +// Aliases for mappings +#define MO_SYM MO(_SYM) +#define MO_NAV MO(_NAV) +#define FUN_ENT LT(_FUN, KC_ENT) + +// `Escape` when tapped, `Control` when held +#define CTL_ESC MT(MOD_LCTL, KC_ESC) + +// One shot modifiers, used in the navigation layer (_NAV) +#define OSM_G OSM(MOD_LGUI) +#define OSM_A OSM(MOD_LALT) +#define OSM_S OSM(MOD_LSFT) +#define OSM_C OSM(MOD_LCTL) + +#define TAB_NXT C(KC_PGDN) +#define TAB_PRV C(KC_PGUP) +#define TAB_NEW C(KC_T) +#define TAB_EXT C(KC_W) +#define WORD_N C(KC_RIGHT) +#define WORD_P C(KC_LEFT) +#define TMUX_ C(KC_SPC) // TMUX prefix key (Ctrl+Spc) + +// Left-hand home row mods +#define HOME_A LGUI_T(KC_A) +#define HOME_S LALT_T(KC_S) +#define HOME_D LSFT_T(KC_D) +#define HOME_F LCTL_T(KC_F) + +// Right-hand home row mods +#define HOME_J RCTL_T(KC_J) +#define HOME_K RSFT_T(KC_K) +#define HOME_L LALT_T(KC_L) +#define HOME_SCLN RGUI_T(KC_SCLN) + + +//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++// +// KEYMAP // +const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = { + [_BASE] = LAYOUT( + //┌────────┬────────┬────────┬────────┬────────┬────────┐ ┌────────┬────────┬────────┬────────┬────────┬────────┐ + KC_GRV, KC_1, KC_2, KC_3, KC_4, KC_5, KC_6, KC_7, KC_8, KC_9, KC_0, KC_MINS, + //├────────┼────────┼────────┼────────┼────────┼────────┤ ├────────┼────────┼────────┼────────┼────────┼────────┤ + KC_TAB, KC_Q, KC_W, KC_E, KC_R, KC_T, KC_Y, KC_U, KC_I, KC_O, KC_P, KC_BSLS, + //├────────┼────────┼────────┼────────┼────────┼────────┤ ├────────┼────────┼────────┼────────┼────────┼────────┤ + CTL_ESC, HOME_A, HOME_S, HOME_D, HOME_F, KC_G, KC_H, HOME_J, HOME_K, HOME_L, HOME_SCLN, KC_QUOT, + //├────────┼────────┼────────┼────────┼────────┼────────┼────────┐ ┌────────┼────────┼────────┼────────┼────────┼────────┼────────┤ + OSM_S, KC_Z, KC_X, KC_C, KC_V, KC_B, TMUX_, KC_BSPC, KC_N, KC_M, KC_COMM, KC_DOT, KC_SLSH, OSM_S, + //└────────┴────────┴────────┴───┬────┴───┬────┴───┬────┴───┬────┘ └───┬────┴───┬────┴───┬────┴───┬────┴────────┴────────┴────────┘ + KC_LGUI, MO_NAV, FUN_ENT, KC_SPC, MO_SYM, OSM_A + // └────────┴────────┴────────┘ └────────┴────────┴────────┘ + ), + + // The number row stays so we can do `Mod+Number` for navigating DE/WM workspaces + [_NAV] = LAYOUT( + //┌────────┬────────┬────────┬────────┬────────┬────────┐ ┌────────┬────────┬────────┬────────┬────────┬────────┐ + _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, + //├────────┼────────┼────────┼────────┼────────┼────────┤ ├────────┼────────┼────────┼────────┼────────┼────────┤ + _______, _______, TAB_EXT, TAB_NXT, TAB_PRV, TAB_NEW, KC_HOME, KC_PGDN, KC_PGUP, KC_END, XXXXXXX, KC_BSPC, + //├────────┼────────┼────────┼────────┼────────┼────────┤ ├────────┼────────┼────────┼────────┼────────┼────────┤ + _______, OSM_G, OSM_A, OSM_S, OSM_C, _______, KC_LEFT, KC_DOWN, KC_UP, KC_RIGHT,SELWORD, XXXXXXX, + //├────────┼────────┼────────┼────────┼────────┼────────┼────────┐ ┌────────┼────────┼────────┼────────┼────────┼────────┼────────┤ + _______, KC_UNDO, KC_CUT, KC_COPY, KC_PSTE, _______, _______, _______, WORD_P, _______, _______, WORD_N, _______, _______, + //└────────┴────────┴────────┴───┬────┴───┬────┴───┬────┴───┬────┘ └───┬────┴───┬────┴───┬────┴───┬────┴────────┴────────┴────────┘ + _______, _______, _______, _______, _______, _______ + // └────────┴────────┴────────┘ └────────┴────────┴────────┘ + ), + + [_SYM] = LAYOUT( + //┌────────┬────────┬────────┬────────┬────────┬────────┐ ┌────────┬────────┬────────┬────────┬────────┬────────┐ + _______, KC_EXLM, KC_AT, KC_HASH, KC_DLR, KC_PERC, KC_CIRC, KC_AMPR, KC_ASTR, KC_LPRN, KC_RPRN, KC_MINS, + //├────────┼────────┼────────┼────────┼────────┼────────┤ ├────────┼────────┼────────┼────────┼────────┼────────┤ + TMUX_, KC_QUOT, KC_LT, KC_GT, KC_DQUO, KC_TILD, LITERAL, KC_LBRC, KC_RBRC, KC_RBRC, KC_AT, _______, + //├────────┼────────┼────────┼────────┼────────┼────────┤ ├────────┼────────┼────────┼────────┼────────┼────────┤ + _______, KC_HASH, KC_EQL, KC_UNDS, KC_DLR, KC_ASTR, KC_SLSH, KC_LPRN, KC_RPRN, KC_SCLN, KC_QUES, _______, + //├────────┼────────┼────────┼────────┼────────┼────────┼────────┐ ┌────────┼────────┼────────┼────────┼────────┼────────┼────────┤ + _______, KC_PIPE, KC_AMPR, KC_MINS, KC_PLUS, KC_PERC, _______, UPDIR, KC_TILD, KC_LCBR, KC_RCBR, KC_RCBR, KC_SLSH, _______, + //└────────┴────────┴────────┴───┬────┴───┬────┴───┬────┴───┬────┘ └───┬────┴───┬────┴───┬────┴───┬────┴────────┴────────┴────────┘ + _______, _______, _______, _______, _______, _______ + // └────────┴────────┴────────┘ └────────┴────────┴────────┘ + ), + + + [_FUN] = LAYOUT( + //┌────────┬────────┬────────┬────────┬────────┬────────┐ ┌────────┬────────┬────────┬────────┬────────┬────────┐ + KC_F12, KC_F1, KC_F2, KC_F3, KC_F4, KC_F5, KC_F6, KC_F7, KC_F8, KC_F9, KC_F10, KC_F11, + //├────────┼────────┼────────┼────────┼────────┼────────┤ ├────────┼────────┼────────┼────────┼────────┼────────┤ + XXXXXXX, MS_BTN1, MS_UP, MS_BTN2, MS_BTN3, XXXXXXX, UG_TOGG, KC_MPRV, KC_MPLY, KC_MNXT, XXXXXXX, KC_PSCR, + //├────────┼────────┼────────┼────────┼────────┼────────┤ ├────────┼────────┼────────┼────────┼────────┼────────┤ + _______, MS_LEFT, MS_DOWN, MS_RGHT, DT_UP, DT_DOWN, UG_NEXT, KC_VOLD, KC_MUTE, KC_VOLU, XXXXXXX, EE_CLR, + //├────────┼────────┼────────┼────────┼────────┼────────┼────────┐ ┌────────┼────────┼────────┼────────┼────────┼────────┼────────┤ + _______, KC_UNDO, KC_CUT, KC_COPY, KC_PASTE,XXXXXXX, XXXXXXX, _______, XXXXXXX, KC_BRID, XXXXXXX, KC_BRIU, XXXXXXX, QK_BOOT, + //└────────┴────────┴────────┴───┬────┴───┬────┴───┬────┴───┬────┘ └───┬────┴───┬────┴───┬────┴───┬────┴────────┴────────┴────────┘ + _______, _______, _______, _______, CW_TOGG, _______ + // └────────┴────────┴────────┘ └────────┴────────┴────────┘ + ) +}; + +// Replaces a mod-tap key's hold function with its one-shot counterpart. +static bool oneshot_mod_tap(uint16_t keycode, keyrecord_t* record) { + if (record->tap.count == 0) { // Key is being held. + if (record->event.pressed) { + const uint8_t mods = (keycode >> 8) & 0x1f; + add_oneshot_mods(((mods & 0x10) == 0) ? mods : (mods << 4)); + } + return false; // Skip default handling. + } + return true; // Continue default handling. +} + +bool process_record_user(uint16_t keycode, keyrecord_t* record) { + // Start achordion + if (!process_achordion(keycode, record)) { return false; } + + // Macros + if (record->event.pressed) { + switch (keycode) { + case UPDIR: + SEND_STRING("../"); + return false; + case LITERAL: + SEND_STRING("\"${}\""SS_TAP(X_LEFT)SS_TAP(X_LEFT)); + return false; + } + } + + // One shot modifiers when HRMs are held + // https://getreuer.info/posts/keyboards/achordion/index.html#one-shot-mod-tap-key + switch (keycode) { + case HOME_D: + case HOME_S: + case HOME_F: + case HOME_K: + case HOME_J: + case HOME_L: + return oneshot_mod_tap(keycode, record); + } + + // Other macros... + + // Select word + if (!process_select_word(keycode, record, SELWORD)) { return false; } + + if (!process_autocorrection(keycode, record)) { return false; } + + return true; +} + +void matrix_scan_user(void) { + achordion_task(); + + select_word_task(); + // Other tasks... +} + +// +// Achordion configuration +// https://getreuer.info/posts/keyboards/achordion/index.html +// +uint16_t achordion_timeout(uint16_t tap_hold_keycode) { + switch (tap_hold_keycode) { + default: + return 800; + } +} + +uint16_t achordion_streak_chord_timeout( + uint16_t tap_hold_keycode, uint16_t next_keycode) { + // Disable streak detection on LT keys. + if (IS_QK_LAYER_TAP(tap_hold_keycode)) { + return 0; + } + + // Disable streak detection on shortcuts such as Ctrl-C, Ctrl-V + // e.g., Ctrl-C + switch (tap_hold_keycode) { + case HOME_F: + case HOME_J: + if (next_keycode == KC_C || next_keycode == KC_V) { + return 0; + } + break; + } + + return 200; +} + + +uint16_t get_quick_tap_term(uint16_t keycode, keyrecord_t* record) { + // If you quickly hold a tap-hold key after tapping it, the tap action is + // repeated. Key repeating is useful e.g. for Vim navigation keys, but can + // lead to missed triggers in fast typing. + switch (keycode) { + case HOME_J: + case HOME_K: + case HOME_L: + return QUICK_TAP_TERM; // Enable key repeating. + default: + return 0; // Otherwise, force hold and disable key repeating. + } +} + +bool achordion_chord(uint16_t tap_hold_keycode, + keyrecord_t* tap_hold_record, + uint16_t other_keycode, + keyrecord_t* other_record) { + + return true; +} + +// +// Caps Word configuration +// https://docs.qmk.fm/features/caps_word +// +bool caps_word_press_user(uint16_t keycode) { + switch (keycode) { + // Configure keycodes that continue caps word + case KC_A ... KC_Z: + case KC_1 ... KC_0: + case KC_BSPC: + case KC_DEL: + case KC_UNDS: + return true; + default: + return false; // Deactivate Caps Word + } +} diff --git a/keyboards/keebio/iris_ce/keymaps/chzerv/rules.mk b/keyboards/keebio/iris_ce/keymaps/chzerv/rules.mk new file mode 100644 index 00000000..97af194e --- /dev/null +++ b/keyboards/keebio/iris_ce/keymaps/chzerv/rules.mk @@ -0,0 +1,15 @@ +LTO_ENABLE = yes + +# VIA_ENABLE = yes + +TRI_LAYER_ENABLE = yes +CAPS_WORD_ENABLE = yes +DYNAMIC_TAPPING_TERM_ENABLE = yes + +SRC += features/select_word.c +SRC += features/autocorrection.c +SRC += features/achordion.c + +# Diasble features we don't use to reduce firmware size +COMMAND_ENABLE = no +MAGIC_ENABLE = no