-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcursor.c
More file actions
317 lines (315 loc) · 12.4 KB
/
cursor.c
File metadata and controls
317 lines (315 loc) · 12.4 KB
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
// Copyright 2024 Guillaume Stordeur <guillaume.stordeur@gmail.com>
// Copyright 2024 Matt Skalecki <ikcelaks@gmail.com>
// Copyright 2024 QKekos <q.kekos.q@gmail.com>
// SPDX-License-Identifier: Apache-2.0
#include "qmk_wrapper.h"
#include "triecodes.h"
#include "keybuffer.h"
#include "key_stack.h"
#include "trie.h"
#include "triecodes.h"
#include "utils.h"
#include "cursor.h"
#include "st_assert.h"
#include <ctype.h>
//////////////////////////////////////////////////////////////////
bool cursor_advance_to_valid_output(st_cursor_t *cursor)
{
const st_trie_payload_t *action = st_cursor_get_action(cursor);
if (!action) {
return false;
}
if (cursor->pos.sub_index < action->completion_len) {
// current sub-index is valid; no need to advance
return true;
}
// we have exceeded the length of the completion string
// advance to the next key that contains output
int backspaces = action->num_backspaces;
while (true) {
// move to next key in buffer
++cursor->pos.index;
st_key_buffer_advance_seq_ref_index(cursor->buffer, &cursor->seq_ref_index);
if (st_cursor_at_end(cursor)) {
return false;
}
const st_key_action_t *keyaction = st_key_buffer_get(cursor->buffer, cursor->pos.index);
st_assert(keyaction, "reached the end without finding the next output key");
if (keyaction->action_taken == ST_DEFAULT_KEY_ACTION) {
if (backspaces == 0) {
// This is a real keypress and no more backspaces to consume
cursor->pos.sub_index = 0;
return true;
}
// consume one backspace
--backspaces;
continue;
}
// Load payload of key that performed action
action = st_cursor_get_action(cursor);
if (backspaces < action->completion_len) {
// This action contains the next output key. Find it's sub_pos and return true
for (cursor->pos.sub_index = 0; cursor->pos.sub_index < backspaces; ++cursor->pos.sub_index) {
const int completion_char_index = action->completion_index + action->completion_len - 1 - cursor->pos.sub_index;
const uint8_t triecode = CDATA(cursor->trie, completion_char_index);
if (st_is_trans_seq_ref_triecode(triecode)) {
// This is a seq_ref, increment the seq_ref_index
++cursor->seq_ref_index;
}
}
return true;
}
backspaces -= action->completion_len - action->num_backspaces;
}
}
//////////////////////////////////////////////////////////////////
bool st_cursor_init(st_cursor_t *cursor, int history, uint8_t as_output)
{
cursor->pos.index = history;
cursor->pos.as_output = as_output;
cursor->pos.sub_index = 0;
cursor->pos.segment_len = 1;
cursor->cache_valid = 255;
cursor->seq_ref_index = 0;
if (as_output && !cursor_advance_to_valid_output(cursor)) {
// This is crazy, but it is theoretically possible that the
// entire buffer is full of backspaces such that no valid
// output key exists in the buffer!
// Set the cursor_pos to the `end` position and return false
cursor->pos.index = cursor->buffer->size;
cursor->pos.sub_index = 0;
return false;
}
// TODO: add an assert that the buffer isn't empty, or maybe
// that should be done in the key_buffer code
return true;
}
//////////////////////////////////////////////////////////////////
uint8_t st_cursor_get_triecode(st_cursor_t *cursor)
{
const st_key_action_t *keyaction = st_key_buffer_get(cursor->buffer, cursor->pos.index);
if (!keyaction) {
return '\0';
}
if (!cursor->pos.as_output
|| keyaction->action_taken == ST_DEFAULT_KEY_ACTION) {
// we need the actual key that was pressed
return keyaction->triecode;
}
// This is an output cursor focused on rule matching keypress
// get the character at the sub_indax of the transform completion
const st_trie_payload_t *action = st_cursor_get_action(cursor);
const int completion_char_index_delta = action->completion_len - 1 - cursor->pos.sub_index;
const int completion_char_index = action->completion_index + completion_char_index_delta;
st_assert(completion_char_index >= 0, "Invalid completion_char_index: %d at Cursor Pos: %d, %d; %d",
completion_char_index, cursor->pos.index, cursor->pos.sub_index, cursor->buffer->size);
const uint8_t triecode = CDATA(cursor->trie, completion_char_index);
if (st_is_trans_seq_ref_triecode(triecode)) {
return st_key_buffer_get_seq_ref(cursor->buffer, cursor->seq_ref_index);
}
if ((keyaction->key_flags & ST_KEY_FLAG_IS_FULL_SHIFT) ||
(completion_char_index_delta == 0 && (keyaction->key_flags & ST_KEY_FLAG_IS_ONE_SHOT_SHIFT))) {
return toupper(triecode);
}
return triecode;
}
//////////////////////////////////////////////////////////////////
uint16_t st_cursor_get_matched_rule(st_cursor_t *cursor)
{
const st_key_action_t *keyaction = st_key_buffer_get(cursor->buffer, cursor->pos.index);
if (cursor->pos.as_output || !keyaction) {
return ST_DEFAULT_KEY_ACTION;
}
return keyaction->action_taken;
}
//////////////////////////////////////////////////////////////////
// DO NOT USE externally when cursor is initialized to act
// as a virtual output. Behavior is not stable in the presence
// of `st_cursor_get_keycode` in virtual output mode
const st_trie_payload_t *st_cursor_get_action(st_cursor_t *cursor)
{
st_trie_payload_t *action = &cursor->cached_action;
if (cursor->cache_valid == cursor->pos.index) {
return action;
}
const st_key_action_t *keyaction = st_key_buffer_get(cursor->buffer, cursor->pos.index);
if (!keyaction) {
return NULL;
}
if (keyaction->action_taken == ST_DEFAULT_KEY_ACTION) {
action->completion_index = ST_DEFAULT_KEY_ACTION;
action->completion_len = 1;
action->num_backspaces = 0;
action->func_code = 0;
} else {
st_get_payload_from_match_index(cursor->trie, action, keyaction->action_taken);
}
cursor->cache_valid = cursor->pos.index;
return action;
}
//////////////////////////////////////////////////////////////////
uint8_t st_cursor_get_shift_of_nth(st_cursor_t *cursor, int nth)
{
st_cursor_pos_t original_pos = st_cursor_save(cursor);
st_cursor_init(cursor, 1, true);
for (int i = 0; i < nth - 1; ++i) {
if (!st_cursor_next(cursor)) {
st_cursor_restore(cursor, &original_pos);
return 0;
}
}
const st_trie_payload_t *action = st_cursor_get_action(cursor);
uint8_t key_flags = st_key_buffer_get(cursor->buffer, cursor->pos.index)->key_flags;
key_flags &= ~ST_KEY_FLAG_IS_ANCHOR_MATCH;
if (action->completion_len > cursor->pos.sub_index + 1) {
key_flags &= ~ST_KEY_FLAG_IS_ONE_SHOT_SHIFT;
}
st_cursor_restore(cursor, &original_pos);
return key_flags;
}
//////////////////////////////////////////////////////////////////
uint8_t st_cursor_get_seq_ascii(st_cursor_t *cursor, uint8_t triecode)
{
if (!st_is_trans_seq_ref_triecode(triecode)) {
return triecode;
}
int nth = st_get_seq_ref_triecode_pos(triecode);
st_cursor_pos_t original_pos = st_cursor_save(cursor);
cursor->pos.as_output = false;
cursor->pos.sub_index = 0;
while (nth > 0) {
if (st_cursor_at_end(cursor)) {
// nth character in the sequence is not currently available
st_cursor_restore(cursor, &original_pos);
return 0;
}
--nth;
if (!cursor->pos.as_output && (st_key_buffer_get(cursor->buffer, cursor->pos.index)->key_flags & ST_KEY_FLAG_IS_ANCHOR_MATCH)) {
// reached the anchor of the sequence, move past the match
// and get the rest of the sequence from the virtual output
st_cursor_next(cursor);
st_cursor_convert_to_output(cursor);
cursor_advance_to_valid_output(cursor);
} else {
st_cursor_next(cursor);
}
}
triecode = st_cursor_get_triecode(cursor);
st_cursor_restore(cursor, &original_pos);
return triecode;
}
//////////////////////////////////////////////////////////////////
bool st_cursor_at_end(const st_cursor_t *cursor)
{
return cursor->pos.index >= cursor->buffer->size || cursor->seq_ref_index >= cursor->buffer->seq_ref_capacity;
}
//////////////////////////////////////////////////////////////////
bool st_cursor_next(st_cursor_t *cursor)
{
if (!cursor->pos.as_output) {
++cursor->pos.index;
st_key_buffer_advance_seq_ref_index(cursor->buffer, &cursor->seq_ref_index);
if (st_cursor_at_end(cursor)) {
// leave `index` at the End position
cursor->pos.index = cursor->buffer->size;
return false;
}
++cursor->pos.segment_len;
return true;
}
// Continue processing if simulating output buffer
const st_key_action_t *keyaction = st_key_buffer_get(cursor->buffer, cursor->pos.index);
if (!keyaction) {
return false;
}
if (keyaction->action_taken == ST_DEFAULT_KEY_ACTION) {
// This is a normal keypress to consume
++cursor->pos.index;
st_key_buffer_advance_seq_ref_index(cursor->buffer, &cursor->seq_ref_index);
cursor->pos.sub_index = 0;
if (!cursor_advance_to_valid_output(cursor)) {
cursor->pos.index = cursor->buffer->size;
cursor->pos.sub_index = 0;
return false;
}
++cursor->pos.segment_len;
return true;
}
// This is a key with an action and completion, increment the sub_index
// and advance to the next key in the key buffer if we exceeded the completion length
++cursor->pos.sub_index;
const st_trie_payload_t *action = st_cursor_get_action(cursor);
if (action->completion_len > cursor->pos.sub_index) {
const int completion_char_index = action->completion_index + action->completion_len - 1 - cursor->pos.sub_index;
const uint8_t triecode = CDATA(cursor->trie, completion_char_index);
if (st_is_trans_seq_ref_triecode(triecode)) {
// This is a seq_ref, increment the seq_ref_index
++cursor->seq_ref_index;
}
}
if (cursor_advance_to_valid_output(cursor)) {
++cursor->pos.segment_len;
return true;
}
cursor->pos.index = cursor->buffer->size;
cursor->pos.sub_index = 0;
return false;
}
//////////////////////////////////////////////////////////////////
bool st_cursor_convert_to_output(st_cursor_t *cursor)
{
if (cursor->pos.as_output) {
return true;
}
cursor->pos.as_output = true;
return cursor_advance_to_valid_output(cursor);
}
//////////////////////////////////////////////////////////////////
st_cursor_pos_t st_cursor_save(const st_cursor_t *cursor)
{
return cursor->pos;
}
//////////////////////////////////////////////////////////////////
void st_cursor_restore(st_cursor_t *cursor, const st_cursor_pos_t *cursor_pos)
{
cursor->pos = *cursor_pos;
}
//////////////////////////////////////////////////////////////////
bool st_cursor_longer_than(const st_cursor_t *cursor, const st_cursor_pos_t *past_pos)
{
const int cur_pos = (cursor->pos.index << 8)
+ cursor->pos.sub_index;
const int old_pos = (past_pos->index << 8)
+ past_pos->sub_index;
return cur_pos > old_pos;
}
//////////////////////////////////////////////////////////////////
void st_cursor_print(st_cursor_t *cursor)
{
#ifndef NO_PRINT
st_cursor_pos_t cursor_pos = st_cursor_save(cursor);
uprintf("cursor: |");
while (!st_cursor_at_end(cursor)) {
const uint8_t code = st_cursor_get_triecode(cursor);
uprintf("%c", st_triecode_to_ascii(code));
st_cursor_next(cursor);
}
uprintf("| (%d:%d)\n", cursor->buffer->size, cursor->pos.segment_len);
st_cursor_restore(cursor, &cursor_pos);
#endif
}
//////////////////////////////////////////////////////////////////
bool st_cursor_push_to_stack(st_cursor_t *cursor,
st_key_stack_t *key_stack,
int count)
{
key_stack->size = 0;
for (; count > 0; --count, st_cursor_next(cursor)) {
const uint8_t triecode = st_cursor_get_triecode(cursor);
if (!triecode) {
return false;
}
st_key_stack_push(key_stack, triecode);
}
return true;
}