-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathselect_word.c
266 lines (235 loc) · 7.5 KB
/
select_word.c
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
// Copyright 2021-2025 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
* <https://getreuer.info/posts/keyboards/select-word>
*/
#include "select_word.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 "select_word: QMK version is too old to build. Please update QMK."
#else
__attribute__((weak)) uint16_t SELECT_WORD_KEYCODE = KC_NO;
static int8_t selection_dir = 0;
static bool reset_before_next_event = false;
static uint8_t registered_hotkey = KC_NO;
// Macro `IS_MAC` determines whether to use Mac vs. Windows/Linux hotkeys:
//
// * OS Detection is used if it is enabled.
//
// * With SELECT_WORD_OS_DYNAMIC, the user may define callback
// select_word_host_is_mac().
//
// * Otherwise, the assumed OS is set at compile time, to Window/Linux by
// default, or to Mac with SELECT_WORD_OS_MAC.
#if defined(SELECT_WORD_OS_DYNAMIC) || defined(OS_DETECTION_ENABLE)
__attribute__((weak)) bool select_word_host_is_mac(void) {
# ifdef OS_DETECTION_ENABLE // Use OS Detection if enabled.
switch (detected_host_os()) {
case OS_LINUX:
case OS_WINDOWS:
return false;
case OS_MACOS:
case OS_IOS:
return true;
default:
break;
}
# endif // OS_DETECTION_ENABLE
# ifdef SELECT_WORD_OS_MAC
return true;
# else
return false;
# endif // SELECT_WORD_OS_MAC
}
# define IS_MAC select_word_host_is_mac()
#else
# ifdef SELECT_WORD_OS_MAC
# define IS_MAC true
# else
# define IS_MAC false
# endif // SELECT_WORD_OS_MAC
#endif // defined(SELECT_WORD_OS_DYNAMIC) || defined(OS_DETECTION_ENABLE)
// Idle timeout timer to reset Select Word after a period of inactivity.
#if SELECT_WORD_TIMEOUT > 0
# if SELECT_WORD_TIMEOUT < 100 || SELECT_WORD_TIMEOUT > 30000
// Constrain timeout to a sensible range. With the 16-bit timer, the longest
// representable timeout is 32768 ms, rounded here to 30000 ms = half a minute.
# error "select_word: SELECT_WORD_TIMEOUT must be between 100 and 30000 ms"
# endif
static uint16_t idle_timer = 0;
static void restart_idle_timer(void) {
idle_timer = (timer_read() + SELECT_WORD_TIMEOUT) | 1;
}
void select_word_task(void) {
if (idle_timer && timer_expired(timer_read(), idle_timer)) {
idle_timer = 0;
selection_dir = 0;
}
}
#endif // SELECT_WORD_TIMEOUT > 0
static void clear_all_mods(void) {
clear_mods();
clear_weak_mods();
#ifndef NO_ACTION_ONESHOT
clear_oneshot_mods();
#endif // NO_ACTION_ONESHOT
}
static void select_word_in_dir(int8_t dir) {
// With Windows and Linux (non-Mac) systems:
// dir < 0: Backward word selection: Ctrl+Left, Ctrl+Right, Ctrl+Shift+Left.
// dir > 0: Forward word selection: Ctrl+Right, Ctrl+Left, Ctrl+Shift+Right.
// Or to extend an existing selection:
// dir < 0: Backward word selection: Ctrl+Shift+Left.
// dir > 0: Forward word selection: Ctrl+Shift+Right.
//
// With Mac OS, use Alt (Opt) instead of Ctrl:
// dir < 0: Backward word selection: Alt+Left, Alt+Right, Alt+Shift+Left.
// dir > 0: Forward word selection: Alt+Right, Alt+Left, Alt+Shift+Right.
// Or to extend an existing selection:
// dir < 0: Backward word selection: Alt+Shift+Left.
// dir > 0: Forward word selection: Alt+Shift+Right.
reset_before_next_event = false;
const uint8_t saved_mods = get_mods();
clear_all_mods();
if (selection_dir && (selection_dir < 0) != (dir < 0)) { // Reversal.
send_keyboard_report();
tap_code_delay((dir < 0) ? KC_RGHT : KC_LEFT, TAP_CODE_DELAY);
}
add_mods(IS_MAC ? MOD_BIT_LALT : MOD_BIT_LCTRL);
if (selection_dir == 0) { // Initial selection.
send_keyboard_report();
send_string_with_delay_P(
(dir < 0) ? PSTR(SS_TAP(X_LEFT) SS_TAP(X_RGHT))
: PSTR(SS_TAP(X_RGHT) SS_TAP(X_LEFT)),
TAP_CODE_DELAY);
}
register_mods(MOD_BIT_LSHIFT);
registered_hotkey = (dir < 0) ? KC_LEFT : KC_RGHT;
register_code(registered_hotkey);
set_mods(saved_mods);
selection_dir = dir;
}
static void select_line(void) {
// With Windows and Linux (non-Mac) systems:
// Home, Shift+End.
// Or to extend an existing selection:
// Shift+Down.
//
// With Mac OS, use GUI (Command) + arrows:
// GUI+Left, Shift+GUI+Right.
// Or to extend an existing selection:
// Shift+Down.
reset_before_next_event = false;
const uint8_t saved_mods = get_mods();
clear_all_mods();
if (selection_dir != 2) {
send_keyboard_report();
send_string_with_delay_P(
IS_MAC ? PSTR(SS_LGUI(SS_TAP(X_LEFT) SS_LSFT(SS_TAP(X_RGHT))))
: PSTR(SS_TAP(X_HOME) SS_LSFT(SS_TAP(X_END))),
TAP_CODE_DELAY);
} else {
register_mods(MOD_BIT_LSHIFT);
registered_hotkey = KC_DOWN;
register_code(KC_DOWN);
}
set_mods(saved_mods);
selection_dir = 2;
}
void select_word_register(char action) {
if (registered_hotkey) {
select_word_unregister();
}
switch (action) {
case 'W':
select_word_in_dir(1);
break;
case 'B':
select_word_in_dir(-1);
break;
case 'L':
select_line();
break;
}
#if SELECT_WORD_TIMEOUT > 0
idle_timer = 0;
#endif // SELECT_WORD_TIMEOUT > 0
}
void select_word_unregister(void) {
reset_before_next_event = false;
unregister_code(registered_hotkey);
registered_hotkey = KC_NO;
#if SELECT_WORD_TIMEOUT > 0
restart_idle_timer();
#endif // SELECT_WORD_TIMEOUT > 0
}
bool process_select_word(uint16_t keycode, keyrecord_t* record) {
if (selection_dir) {
if (reset_before_next_event) {
selection_dir = 0;
}
// Ignore most modifier and layer switch keys.
switch (keycode) {
case MODIFIER_KEYCODE_RANGE:
case QK_MOMENTARY ... QK_MOMENTARY_MAX:
case QK_LAYER_MOD ... QK_LAYER_MOD_MAX:
case QK_LAYER_TAP_TOGGLE ... QK_LAYER_TAP_TOGGLE_MAX:
case QK_TO ... QK_TO_MAX:
case QK_TOGGLE_LAYER ... QK_TOGGLE_LAYER_MAX:
#ifndef NO_ACTION_ONESHOT
case QK_ONE_SHOT_LAYER ... QK_ONE_SHOT_LAYER_MAX:
case QK_ONE_SHOT_MOD ... QK_ONE_SHOT_MOD_MAX:
#endif // NO_ACTION_ONESHOT
#ifdef LAYER_LOCK_ENABLE
case QK_LLCK:
#endif // LAYER_LOCK_ENABLE
return true;
#ifndef NO_ACTION_TAPPING
// Ignore hold events on mod-tap and layer-tap keys.
case QK_MOD_TAP ... QK_MOD_TAP_MAX:
case QK_LAYER_TAP ... QK_LAYER_TAP_MAX:
if (record->tap.count == 0) {
return true;
}
break;
#endif // NO_ACTION_TAPPING
}
reset_before_next_event = true;
}
#if SELECT_WORD_TIMEOUT > 0
if (idle_timer) {
restart_idle_timer();
}
#endif // SELECT_WORD_TIMEOUT > 0
if (SELECT_WORD_KEYCODE && keycode == SELECT_WORD_KEYCODE) {
const bool shifted = MOD_MASK_SHIFT & (get_mods() | get_weak_mods()
#ifndef NO_ACTION_ONESHOT
| get_oneshot_mods()
#endif // NO_ACTION_ONESHOT
);
if (record->event.pressed) {
select_word_register(shifted ? 'L' : 'W');
} else {
select_word_unregister();
}
return false;
}
return true;
}
#endif