select_word.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // Copyright 2021 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // https://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. //
  15. //
  16. // For full documentation, see
  17. // https://getreuer.info/posts/keyboards/select-word
  18. #include "select_word.h"
  19. // Mac users, uncomment this line:
  20. // #define MAC_HOTKEYS
  21. enum { STATE_NONE, STATE_SELECTED, STATE_WORD, STATE_FIRST_LINE, STATE_LINE };
  22. bool process_select_word(uint16_t keycode, keyrecord_t* record,
  23. uint16_t sel_keycode) {
  24. static uint8_t state = STATE_NONE;
  25. if (keycode == KC_LSFT || keycode == KC_RSFT) { return true; }
  26. if (keycode == sel_keycode && record->event.pressed) { // On key press.
  27. const uint8_t mods = get_mods();
  28. #ifndef NO_ACTION_ONESHOT
  29. const uint8_t all_mods = mods | get_oneshot_mods();
  30. #else
  31. const uint8_t all_mods = mods;
  32. #endif // NO_ACTION_ONESHOT
  33. if ((all_mods & MOD_MASK_SHIFT) == 0) { // Select word.
  34. #ifdef MAC_HOTKEYS
  35. register_code(KC_LALT);
  36. #else
  37. register_code(KC_LCTL);
  38. #endif // MAC_HOTKEYS
  39. if (state == STATE_NONE) {
  40. tap_code(KC_RGHT);
  41. tap_code(KC_LEFT);
  42. }
  43. register_code(KC_LSFT);
  44. register_code(KC_RGHT);
  45. state = STATE_WORD;
  46. } else { // Select line.
  47. if (state == STATE_NONE) {
  48. clear_mods();
  49. #ifndef NO_ACTION_ONESHOT
  50. clear_oneshot_mods();
  51. #endif // NO_ACTION_ONESHOT
  52. #ifdef MAC_HOTKEYS
  53. register_code16(LCTL(KC_A));
  54. tap_code16(LSFT(KC_E));
  55. unregister_code16(LCTL(KC_A));
  56. #else
  57. tap_code(KC_HOME);
  58. tap_code16(LSFT(KC_END));
  59. #endif // MAC_HOTKEYS
  60. set_mods(mods);
  61. state = STATE_FIRST_LINE;
  62. } else {
  63. register_code(KC_DOWN);
  64. state = STATE_LINE;
  65. }
  66. }
  67. return false;
  68. }
  69. // `sel_keycode` was released, or another key was pressed.
  70. switch (state) {
  71. case STATE_WORD:
  72. unregister_code(KC_RGHT);
  73. unregister_code(KC_LSFT);
  74. #ifdef MAC_HOTKEYS
  75. unregister_code(KC_LALT);
  76. #else
  77. unregister_code(KC_LCTL);
  78. #endif // MAC_HOTKEYS
  79. state = STATE_SELECTED;
  80. break;
  81. case STATE_FIRST_LINE:
  82. state = STATE_SELECTED;
  83. break;
  84. case STATE_LINE:
  85. unregister_code(KC_DOWN);
  86. state = STATE_SELECTED;
  87. break;
  88. case STATE_SELECTED:
  89. if (keycode == KC_ESC) {
  90. tap_code(KC_RGHT);
  91. state = STATE_NONE;
  92. return false;
  93. }
  94. // Fallthrough.
  95. default:
  96. state = STATE_NONE;
  97. }
  98. return true;
  99. }