summaryrefslogtreecommitdiffstats
path: root/keyboards/planck/keymaps/ariccb/features/select_word.c
blob: c0ffe0a90d05a4af0c781f92c98dc1e8960844c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Copyright 2021 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.
//
//
// For full documentation, see
// https://getreuer.info/posts/keyboards/select-word

#include "select_word.h"

// Mac users, uncomment this line:
// #define MAC_HOTKEYS

enum { STATE_NONE, STATE_SELECTED, STATE_WORD, STATE_FIRST_LINE, STATE_LINE };

bool process_select_word(uint16_t keycode, keyrecord_t* record,
                         uint16_t sel_keycode) {
  static uint8_t state = STATE_NONE;

  if (keycode == KC_LSFT || keycode == KC_RSFT) { return true; }

  if (keycode == sel_keycode && record->event.pressed) {  // On key press.
    const uint8_t mods = get_mods();
#ifndef NO_ACTION_ONESHOT
    const uint8_t all_mods = mods | get_oneshot_mods();
#else
    const uint8_t all_mods = mods;
#endif  // NO_ACTION_ONESHOT
    if ((all_mods & MOD_MASK_SHIFT) == 0) {  // Select word.
#ifdef MAC_HOTKEYS
      register_code(KC_LALT);
#else
      register_code(KC_LCTL);
#endif  // MAC_HOTKEYS
      if (state == STATE_NONE) {
        tap_code(KC_RGHT);
        tap_code(KC_LEFT);
      }
      register_code(KC_LSFT);
      register_code(KC_RGHT);
      state = STATE_WORD;
    } else {  // Select line.
      if (state == STATE_NONE) {
        clear_mods();
#ifndef NO_ACTION_ONESHOT
        clear_oneshot_mods();
#endif  // NO_ACTION_ONESHOT
#ifdef MAC_HOTKEYS
        register_code16(LCTL(KC_A));
        tap_code16(LSFT(KC_E));
        unregister_code16(LCTL(KC_A));
#else
        tap_code(KC_HOME);
        tap_code16(LSFT(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);
      unregister_code(KC_LSFT);
#ifdef MAC_HOTKEYS
      unregister_code(KC_LALT);
#else
      unregister_code(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.
    default:
      state = STATE_NONE;
  }

  return true;
}