forked from qmk/qmk_firmware
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xtonhasvim.c
643 lines (619 loc) · 16.4 KB
/
xtonhasvim.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
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
/* Copyright 2015-2017 Christon DeWan
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "xtonhasvim.h"
#include "fancylighting.h"
/************************************
* helper foo
************************************/
#define PRESS(kc) register_code(kc)
#define RELEASE(kc) unregister_code(kc)
static void TAP(uint16_t keycode) {
PRESS(keycode);
RELEASE(keycode);
}
static void CMD(uint16_t keycode) {
PRESS(KC_LGUI);
TAP(keycode);
RELEASE(KC_LGUI);
}
static void CTRL(uint16_t keycode) {
PRESS(KC_LCTRL);
TAP(keycode);
RELEASE(KC_LCTRL);
}
static void SHIFT(uint16_t keycode) {
PRESS(KC_LSHIFT);
TAP(keycode);
RELEASE(KC_LSHIFT);
}
static void ALT(uint16_t keycode) {
PRESS(KC_LALT);
TAP(keycode);
RELEASE(KC_LALT);
}
uint16_t vstate = VIM_START;
static bool yank_was_lines = false;
static bool SHIFTED = false;
static uint32_t mod_override_layer_state = 0;
static uint16_t mod_override_triggering_key = 0;
static void edit(void) { vstate = VIM_START; layer_clear(); }
#define EDIT edit()
static void simple_movement(uint16_t keycode) {
switch(keycode) {
case VIM_B:
PRESS(KC_LALT);
SHIFT(KC_LEFT); // select to start of this word
RELEASE(KC_LALT);
break;
case VIM_E:
PRESS(KC_LALT);
SHIFT(KC_RIGHT); // select to end of this word
RELEASE(KC_LALT);
break;
case VIM_H:
SHIFT(KC_LEFT);
break;
case VIM_J:
CMD(KC_LEFT);
SHIFT(KC_DOWN);
SHIFT(KC_DOWN);
break;
case VIM_K:
CMD(KC_LEFT);
TAP(KC_DOWN);
SHIFT(KC_UP);
SHIFT(KC_UP);
break;
case VIM_L:
SHIFT(KC_RIGHT);
break;
case VIM_W:
PRESS(KC_LALT);
SHIFT(KC_RIGHT); // select to end of this word
SHIFT(KC_RIGHT); // select to end of next word
SHIFT(KC_LEFT); // select to start of next word
RELEASE(KC_LALT);
break;
}
}
static void comma_period(uint16_t keycode) {
switch (keycode) {
case VIM_COMMA:
if (SHIFTED) {
// indent
CMD(KC_LBRACKET);
} else {
// toggle comment
CMD(KC_SLASH);
}
break;
case VIM_PERIOD:
if (SHIFTED) {
// outdent
CMD(KC_RBRACKET);
}
break;
}
}
__attribute__ ((weak))
bool process_record_keymap(uint16_t keycode, keyrecord_t *record) {
return true;
}
bool process_record_user(uint16_t keycode, keyrecord_t *record) {
/* keymap gets first whack */
if(!process_record_keymap(keycode, record)) return false;
/****** FIREY_RETURN *****/
if(record->event.pressed && keycode == FIREY_RETURN) {
start_firey_return();
TAP(KC_ENT);
}
/****** mod passthru *****/
if(record->event.pressed && layer_state_is(vim_cmd_layer()) && (IS_MOD(keycode) || keycode == LSFT(KC_LALT))) {
mod_override_layer_state = layer_state;
mod_override_triggering_key = keycode;
// TODO: change this to track key location instead
layer_clear();
return true; // let the event fall through...
}
if(mod_override_layer_state && !record->event.pressed && keycode == mod_override_triggering_key) {
layer_state_set(mod_override_layer_state);
mod_override_layer_state = 0;
mod_override_triggering_key = 0;
return true;
}
if (VIM_START <= keycode && keycode <= VIM_ESC) {
if(keycode == VIM_SHIFT) {
SHIFTED = record->event.pressed;
return false;
}
if (record->event.pressed) {
if(keycode == VIM_START) {
// entry from anywhere
layer_on(vim_cmd_layer());
vstate = VIM_START;
// reset state
yank_was_lines = false;
SHIFTED = false;
mod_override_layer_state = 0;
mod_override_triggering_key = 0;
return false;
}
switch(vstate) {
case VIM_START:
switch(keycode){
/*****************************
* ground state
*****************************/
case VIM_A:
if(SHIFTED) {
// CMD(KC_RIGHT);
CTRL(KC_E);
} else {
TAP(KC_RIGHT);
}
EDIT;
break;
case VIM_B:
PRESS(KC_LALT);
PRESS(KC_LEFT);
break;
case VIM_C:
if(SHIFTED) {
PRESS(KC_LSHIFT);
CMD(KC_RIGHT);
RELEASE(KC_LSHIFT);
CMD(KC_X);
yank_was_lines = false;
EDIT;
} else {
vstate = VIM_C;
}
break;
case VIM_D:
if(SHIFTED) {
CTRL(KC_K);
} else {
vstate = VIM_D;
}
break;
case VIM_E:
PRESS(KC_LALT);
PRESS(KC_RIGHT);
break;
case VIM_G:
if(SHIFTED) {
TAP(KC_END);
} else {
vstate = VIM_G;
}
break;
case VIM_H:
PRESS(KC_LEFT);
break;
case VIM_I:
if(SHIFTED){
CTRL(KC_A);
}
EDIT;
break;
case VIM_J:
if(SHIFTED) {
CMD(KC_RIGHT);
TAP(KC_DEL);
} else {
PRESS(KC_DOWN);
}
break;
case VIM_K:
PRESS(KC_UP);
break;
case VIM_L:
PRESS(KC_RIGHT);
break;
case VIM_O:
if(SHIFTED) {
CMD(KC_LEFT);
TAP(KC_ENTER);
TAP(KC_UP);
EDIT;
} else {
CMD(KC_RIGHT);
TAP(KC_ENTER);
EDIT;
}
break;
case VIM_P:
if(SHIFTED) {
CMD(KC_LEFT);
CMD(KC_V);
} else {
if(yank_was_lines) {
CMD(KC_RIGHT);
TAP(KC_RIGHT);
CMD(KC_V);
} else {
CMD(KC_V);
}
}
break;
case VIM_S:
// s for substitute?
if(SHIFTED) {
CMD(KC_LEFT);
PRESS(KC_LSHIFT);
CMD(KC_RIGHT);
RELEASE(KC_LSHIFT);
CMD(KC_X);
yank_was_lines = false;
EDIT;
} else {
SHIFT(KC_RIGHT);
CMD(KC_X);
yank_was_lines = false;
EDIT;
}
break;
case VIM_U:
if(SHIFTED) {
PRESS(KC_LSFT);
CMD(KC_Z);
RELEASE(KC_LSHIFT);
} else {
CMD(KC_Z);
}
break;
case VIM_V:
if(SHIFTED) {
CMD(KC_LEFT);
SHIFT(KC_DOWN);
vstate = VIM_VS;
} else {
vstate = VIM_V;
}
break;
case VIM_W:
PRESS(KC_LALT);
TAP(KC_RIGHT);
TAP(KC_RIGHT);
TAP(KC_LEFT);
RELEASE(KC_LALT);
break;
case VIM_X:
// SHIFT(KC_RIGHT);
// CMD(KC_X);
PRESS(KC_DEL);
break;
case VIM_Y:
if(SHIFTED) {
CMD(KC_LEFT);
SHIFT(KC_DOWN);
CMD(KC_C);
TAP(KC_RIGHT);
yank_was_lines = true;
} else {
vstate = VIM_Y;
}
break;
case VIM_COMMA:
case VIM_PERIOD:
comma_period(keycode);
break;
}
break;
case VIM_C:
/*****************************
* c- ...for change. I never use this...
*****************************/
switch(keycode) {
case VIM_B:
case VIM_E:
case VIM_H:
case VIM_J:
case VIM_K:
case VIM_L:
case VIM_W:
simple_movement(keycode);
CMD(KC_X);
yank_was_lines = false;
EDIT;
break;
case VIM_C:
CMD(KC_LEFT);
PRESS(KC_LSHIFT);
CMD(KC_RIGHT);
RELEASE(KC_LSHIFT);
CMD(KC_X);
yank_was_lines = false;
EDIT;
break;
case VIM_I:
vstate = VIM_CI;
break;
default:
vstate = VIM_START;
break;
}
break;
case VIM_CI:
/*****************************
* ci- ...change inner word
*****************************/
switch(keycode) {
case VIM_W:
ALT(KC_LEFT);
PRESS(KC_LSHIFT);
ALT(KC_RIGHT);
RELEASE(KC_LSHIFT);
CMD(KC_X);
yank_was_lines = false;
EDIT;
default:
vstate = VIM_START;
break;
}
break;
case VIM_D:
/*****************************
* d- ...delete stuff
*****************************/
switch(keycode) {
case VIM_B:
case VIM_E:
case VIM_H:
case VIM_J:
case VIM_K:
case VIM_L:
case VIM_W:
simple_movement(keycode);
CMD(KC_X);
yank_was_lines = false;
vstate = VIM_START;
break;
case VIM_D:
CMD(KC_LEFT);
SHIFT(KC_DOWN);
CMD(KC_X);
yank_was_lines = true;
vstate = VIM_START;
break;
case VIM_I:
vstate = VIM_DI;
break;
default:
vstate = VIM_START;
break;
}
break;
case VIM_DI:
/*****************************
* ci- ...delete a word... FROM THE INSIDE!
*****************************/
switch(keycode) {
case VIM_W:
ALT(KC_LEFT);
PRESS(KC_LSHIFT);
ALT(KC_RIGHT);
RELEASE(KC_LSHIFT);
CMD(KC_X);
yank_was_lines = false;
vstate = VIM_START;
default:
vstate = VIM_START;
break;
}
break;
case VIM_V:
/*****************************
* visual!
*****************************/
switch(keycode) {
case VIM_D:
case VIM_X:
CMD(KC_X);
yank_was_lines = false;
vstate = VIM_START;
break;
case VIM_B:
PRESS(KC_LALT);
PRESS(KC_LSHIFT);
PRESS(KC_LEFT);
// leave open for key repeat
break;
case VIM_E:
PRESS(KC_LALT);
PRESS(KC_LSHIFT);
PRESS(KC_RIGHT);
// leave open for key repeat
break;
case VIM_H:
PRESS(KC_LSHIFT);
PRESS(KC_LEFT);
break;
case VIM_I:
vstate = VIM_VI;
break;
case VIM_J:
PRESS(KC_LSHIFT);
PRESS(KC_DOWN);
break;
case VIM_K:
PRESS(KC_LSHIFT);
PRESS(KC_UP);
break;
case VIM_L:
PRESS(KC_LSHIFT);
PRESS(KC_RIGHT);
break;
case VIM_W:
PRESS(KC_LALT);
SHIFT(KC_RIGHT); // select to end of this word
SHIFT(KC_RIGHT); // select to end of next word
SHIFT(KC_LEFT); // select to start of next word
RELEASE(KC_LALT);
break;
case VIM_P:
CMD(KC_V);
vstate = VIM_START;
break;
case VIM_Y:
CMD(KC_C);
TAP(KC_RIGHT);
yank_was_lines = false;
vstate = VIM_START;
break;
case VIM_V:
case VIM_ESC:
TAP(KC_RIGHT);
vstate = VIM_START;
break;
case VIM_COMMA:
case VIM_PERIOD:
comma_period(keycode);
break;
default:
// do nothing
break;
}
break;
case VIM_VI:
/*****************************
* vi- ...select a word... FROM THE INSIDE!
*****************************/
switch(keycode) {
case VIM_W:
ALT(KC_LEFT);
PRESS(KC_LSHIFT);
ALT(KC_RIGHT);
RELEASE(KC_LSHIFT);
vstate = VIM_V;
default:
// ignore
vstate = VIM_V;
break;
}
break;
case VIM_VS:
/*****************************
* visual line
*****************************/
switch(keycode) {
case VIM_D:
case VIM_X:
CMD(KC_X);
yank_was_lines = true;
vstate = VIM_START;
break;
case VIM_J:
PRESS(KC_LSHIFT);
PRESS(KC_DOWN);
break;
case VIM_K:
PRESS(KC_LSHIFT);
PRESS(KC_UP);
break;
case VIM_Y:
CMD(KC_C);
yank_was_lines = true;
TAP(KC_RIGHT);
vstate = VIM_START;
break;
case VIM_P:
CMD(KC_V);
vstate = VIM_START;
break;
case VIM_V:
case VIM_ESC:
TAP(KC_RIGHT);
vstate = VIM_START;
break;
case VIM_COMMA:
case VIM_PERIOD:
comma_period(keycode);
break;
default:
// do nothing
break;
}
break;
case VIM_G:
/*****************************
* gg, and a grab-bag of other macros i find useful
*****************************/
switch(keycode) {
case VIM_G:
TAP(KC_HOME);
break;
// codes b
case VIM_H:
CTRL(KC_A);
break;
case VIM_J:
PRESS(KC_PGDN);
break;
case VIM_K:
PRESS(KC_PGUP);
break;
case VIM_L:
CTRL(KC_E);
break;
default:
// do nothing
break;
}
vstate = VIM_START;
break;
case VIM_Y:
/*****************************
* yoink!
*****************************/
switch(keycode) {
case VIM_B:
case VIM_E:
case VIM_H:
case VIM_J:
case VIM_K:
case VIM_L:
case VIM_W:
simple_movement(keycode);
CMD(KC_C);
TAP(KC_RIGHT);
yank_was_lines = false;
break;
case VIM_Y:
CMD(KC_LEFT);
SHIFT(KC_DOWN);
CMD(KC_C);
TAP(KC_RIGHT);
yank_was_lines = true;
break;
default:
// NOTHING
break;
}
vstate = VIM_START;
break;
}
} else {
/************************
* key release events
************************/
clear_keyboard();
}
return false;
} else {
return true;
}
}